0
\$\begingroup\$

I am trying to implement a program in Direct3D 11 which uses techniques like deferred shading and shadowmapping, but when I create my gBuffers, the position looks strange. After researching a bit I think my problem is that my positions are in screen space.

test Position, Normals, Diffuse and final picture

My problem is that I don't understand what translations I am supposed to make with what matrix, and where?

my pipeline currently looks like this

Geometry stage: Vertexshader -> Hullshader (tessellation) -> Domainshader (currently where I multiply my positions with the world, view and projection matrices) -> Lightshader (put position, normals and diffusion into the gbuffers)

Shading stage: Computeshader (use all the gbuffers together with light information and shadowmaps to create final image)

I've seen people talking about reconstructing word position from the depth buffer, but how would I achieve this? Would that be done in the Computeshader?

Thank you in advance!

Edit: Heres the Domain and Pixelshader as requested, keep in mind that most of the domainshader is tessellation stuff.

Domainshader:

cbuffer MatrixBuffer
{
    matrix world;
    matrix view;
    matrix proj;

};

struct ConstantOutput
{
    float edges[3] : SV_TessFactor;
    float inside : SV_InsideTessFactor;
};

struct DomainShaderInput
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD0;
    float3 normal : NORMAL;
};

struct DomainShaderOutput
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
    float3 normal : NORMAL;
};

// DOMAIN SHADER

[domain("tri")]

DomainShaderOutput DeferredDomainShader(ConstantOutput input, const OutputPatch<DomainShaderInput, 3> patch, float3 barycentric : SV_DomainLocation)
{

    float4 worldPos;
    float3 vertexPosition, flatPosition;
    DomainShaderOutput output;

    output.texcoord = barycentric.x * patch[0].texcoord + barycentric.y * patch[1].texcoord + barycentric.z * patch[2].texcoord;
    output.normal = barycentric.x * patch[0].normal + barycentric.y * patch[1].normal + barycentric.z * patch[2].normal;


    vertexPosition = barycentric.x * patch[0].position + barycentric.y * patch[1].position + barycentric.z * patch[2].position;

    float3 vecProj0 = dot(patch[0].position - vertexPosition, patch[0].normal) * patch[0].normal;
    float3 vecProj1 = dot(patch[1].position - vertexPosition, patch[1].normal) * patch[1].normal;
    float3 vecProj2 = dot(patch[2].position - vertexPosition, patch[2].normal) * patch[2].normal;


    float3 vecOffset = barycentric.x * vecProj0 + barycentric.y * vecProj1 + barycentric.z * vecProj2;

    vertexPosition += 0.5f * vecOffset;
    
    output.position = mul(float4(vertexPosition, 1.0f), world);
    output.position = mul(output.position, view);
    output.position = mul(output.position, proj);

    output.normal = mul(output.normal, (float3x3)world);
    output.normal = normalize(output.normal);

    return output;
}

Pixelshader:

Texture2D diffuseTexture : register(t0);
Texture2D specularTexture : register(t1);
Texture2D ambientTexture : register(t2);

SamplerState sampleTypeWrap : register(s0);

cbuffer MaterialBuffer
{
    float specularPower;
    float3 pad3;
};

struct PixelShaderInput 
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
    float3 normal : NORMAL;
};

struct PixelShaderOutput
{
    float4 position : SV_Target0;
    float4 normal   : SV_Target1;
    float4 diffuse  : SV_Target2;
    float4 specular : SV_Target3;
    float4 ambient  : SV_Target4;
};

PixelShaderOutput DeferredPixelShader(PixelShaderInput input) : SV_TARGET
{
    PixelShaderOutput output;

    output.position = float4(input.position.xyz, 1.0f);

    input.normal = normalize(input.normal);
    output.normal = float4(input.normal, 1.0f);

    output.diffuse = diffuseTexture.Sample(sampleTypeWrap, input.texcoord);

    output.specular = specularTexture.Sample(sampleTypeWrap, input.texcoord);

    output.ambient = ambientTexture.Sample(sampleTypeWrap, input.texcoord);

    output.specular.w = specularPower;

    return output;
}
\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Since you're using D3D11, the deferred rendering algorithm that includes reconstructing position from the depth buffer, is explained thoroughly with code in Practical Rendering & Computation with Direct3D 11 by Zink et al.

\$\endgroup\$
1
  • \$\begingroup\$ This answer would be even better if it presented an outline of this technique. \$\endgroup\$
    – DMGregory
    Commented May 22, 2023 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .