最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Passing depth buffer values as a texture to pixel shader - Stack Overflow

programmeradmin1浏览0评论

I'm very new to shader programming, so five me for any knowledge gaps or mistakes I've made.

I am currently following a technique discussed in Nvidia's GPU Gems: . In here, it is mentioned that they passed the depth buffer into the pixel shader as a texture to find the world space positions of objects.

I've started my project using Visual Studio's Universal C++ template of a DirectX11 application that gave me the following vertex shader starter program:

cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
    matrix model;
    matrix view;
    matrix projection;
};

struct VertexShaderInput
{
    float3 pos : POSITION;
    float3 color : COLOR0;
};

struct PixelShaderInput
{
    float4 pos : SV_POSITION;
    float3 color : COLOR0;
};

PixelShaderInput main(VertexShaderInput input)
{
    PixelShaderInput output;
    float4 pos = float4(input.pos, 1.0f);

    pos = mul(pos, model);
    pos = mul(pos, view);
    pos = mul(pos, projection);
    output.pos = pos;

    output.color = input.color;

    return output;
}

Along with the following starter pixel shader program:

struct PixelShaderInput
{
    float4 pos : SV_POSITION;
    float3 color : COLOR0;
};

float4 main(PixelShaderInput input) : SV_TARGET
{
    return float4(input.color, 1.0f);
}

And in the Nvidia Article, the following pixel shader code snippet:

// Get the depth buffer value at this pixel.
float zOverW = tex2D(
    depthTexture,
    texCoord); // H is the viewport position at this pixel in the range -1 to 1.
float4 H = float4(texCoord.x * 2 - 1, (1 - texCoord.y) * 2 - 1, zOverW,
                  1); // Transform by the view-projection inverse.
float4 D = mul(
    H, g_ViewProjectionInverseMatrix); // Divide by w to get the world position.
float4 worldPos = D / D.w;

seems to imply that their pixel shader was already set up to access the "depth buffer texture". I'm not sure what I need to do to access the depth buffer value in my code.

发布评论

评论列表(0)

  1. 暂无评论