1
votes

I'm trying to port my engine to DirectX and I'm currently having issues with depth reconstruction. It works perfectly in OpenGL (even though I use a bit of an expensive method). Every part besides the depth reconstruction works so far. I use GLM because it's a good math library that has no need to install any dependencies or anything for the user.

So basically I get my GLM matrices:

struct DefferedUBO {
    glm::mat4 view;
    glm::mat4 invProj;
    glm::vec4 eyePos;
    glm::vec4 resolution;
};

DefferedUBO deffUBOBuffer;
// ...

glm::mat4 projection = glm::perspective(engine.settings.fov, aspectRatio, 0.1f, 100.0f);
// Get My Camera
CTransform *transform = &engine.transformSystem.components[engine.entities[entityID].components[COMPONENT_TRANSFORM]];
// Get the View Matrix
glm::mat4 view = glm::lookAt(
    transform->GetPosition(),
    transform->GetPosition() + transform->GetForward(),
    transform->GetUp()
);

deffUBOBuffer.invProj = glm::inverse(projection);
deffUBOBuffer.view = glm::inverse(view);

if (engine.settings.graphicsLanguage == GRAPHICS_DIRECTX) {
    deffUBOBuffer.invProj = glm::transpose(deffUBOBuffer.invProj);
    deffUBOBuffer.view = glm::transpose(deffUBOBuffer.view);
}

// Abstracted so I can use OGL, DX, VK, or even Metal when I get around to it.
deffUBO->UpdateUniformBuffer(&deffUBOBuffer);
deffUBO->Bind());

Then in HLSL, I simply use the following:

cbuffer MatrixInfoType {
    matrix invView;
    matrix invProj;
    float4 eyePos;
    float4 resolution;
};

float4 ViewPosFromDepth(float depth, float2 TexCoord) {
    float z = depth; // * 2.0 - 1.0;

    float4 clipSpacePosition = float4(TexCoord * 2.0 - 1.0, z, 1.0);
    float4 viewSpacePosition = mul(invProj, clipSpacePosition);
    viewSpacePosition /= viewSpacePosition.w;

    return viewSpacePosition;
}

float3 WorldPosFromViewPos(float4 view) {
    float4 worldSpacePosition = mul(invView, view);

    return worldSpacePosition.xyz;
}

float3 WorldPosFromDepth(float depth, float2 TexCoord) {
    return WorldPosFromViewPos(ViewPosFromDepth(depth, TexCoord));
}

// ...

// Sample the hardware depth buffer.
float  depth    = shaderTexture[3].Sample(SampleType[0], input.texCoord).r;
float3 position = WorldPosFromDepth(depth, input.texCoord).rgb;

Here's the result:
image1 This just looks like random colors multiplied with the depth.

Ironically when I remove transposing, I get something closer to the truth, but not quite:
image2 You're looking at Crytek Sponza. As you can see, the green area moves and rotates with the bottom of the camera. I have no idea at all why.

The correct version, along with Albedo, Specular, and Normals.
image3

1
take a look hereA. Knorre

1 Answers

1
votes

I fixed my problem at gamedev.net. There was a matrix majorness issue as well as a depth handling issue.

https://www.gamedev.net/forums/topic/692095-d3d-glm-depth-reconstruction-issues