why this shader variable doesn't change?(dx12, hlsl)

Started by
1 comment, last by MJP 2 years, 8 months ago

I'm working with dx12 and hlsl, and this specific root constant seems doesn't change or doesn't work for index.

this is hlsl code

#include "LightUtil.hlsli"

cbuffer cbCameraInfo : register(b1)

{

matrix gmtxViewProj : packoffset(c0);

float3 cameraPos : packoffset(c4);

//size : 19

};

cbuffer cbLightInfo : register(b2)

{

int nLights : packoffset(c0);

int nShadowIndex : packoffset(c4); // - this is the constant

}

cbuffer cbShadowInfo : register(b3)

{

matrix gmtxShadowTransform[3];

matrix gmtxLightViewProj[3];

matrix gmtxCascadedViewProj[3];

float3 ShadowCameraPos;

}

Texture2D gShadowMap[3] : register(t0);

Texture2D gTextureMaps[6] : register(t3);

struct INSTANCED_GAMEOBJECT_INFO

{

matrix m_mtxGameObject;

int m_nTextrueIndex;

};

StructuredBuffer<INSTANCED_GAMEOBJECT_INFO> gGameObjectInfos : register(t0, space1);

StructuredBuffer<LIGHT_INFO> light : register(t1, space1);

SamplerState gsamPointWrap : register(s0);

SamplerState gsamPointClamp : register(s1);

SamplerState gsamLinearWrap : register(s2);

SamplerState gsamLinearClamp : register(s3);

SamplerState gsamAnisotropicWrap : register(s4);

SamplerState gsamAnisotropicClamp : register(s5);

SamplerComparisonState gsamShadow : register(s6);

and when I use nShadowIndex like this

#include "Common.hlsli"

struct VertexIn

{

float3 PosL : POSITION;

};

struct VertexOut

{

float4 PosH : SV_POSITION;

};

VertexOut VS(VertexIn vin)

{

VertexOut vout = (VertexOut) 0.0f;

// Transform to world space.

float4 posW = mul(float4(vin.PosL, 1.0f), gmtxWorld);

// Transform to homogeneous clip space.

vout.PosH = mul(posW, gmtxLightViewProj[nShadowIndex]);

return vout;

}

// This is only used for alpha cut out geometry, so that shadows

// show up correctly. Geometry that does not need to sample a

// texture can use a NULL pixel shader for depth pass.

void PS(VertexOut pin)

{

// Fetch the material data.

MATERIAL matData = material;

float4 diffuseAlbedo = matData.DiffuseAlbedo;

}

this shader always returns same result because of nShadowIndex always fixed to 0.

and this is how i set variables. (there's many other vars, but not that important for this)

as you see, what i'm trying to do is cascaded shadow map.

void CShadowShader::UpdateShaderVariables(ID3D12GraphicsCommandList* pd3dCommandList, XMFLOAT3 xmf3TargetPos, float fBoundary, int nShadowIndex)

{

XMVECTOR lightPos = XMLoadFloat3(&m_pLight->GetPosition());

XMVECTOR TargetPos = XMLoadFloat3(&xmf3TargetPos);

XMVECTOR lightUp = XMLoadFloat3(&m_pLight->GetUp());

XMVECTOR lightDir = XMVectorSubtract(lightPos, TargetPos);

float lightLength = XMVector3Length(lightDir).m128_f32[0];

XMMATRIX lightView = XMMatrixLookAtLH(lightPos, TargetPos, lightUp);

// Transform bounding sphere to light space.

XMFLOAT3 xmf3CenterLS;

XMStoreFloat3(&xmf3CenterLS, XMVector3TransformCoord(XMLoadFloat3(&xmf3TargetPos), lightView));

// Ortho frustum in light space encloses scene.

float l = xmf3CenterLS.x - fBoundary;

float b = xmf3CenterLS.y - fBoundary;

float n = xmf3CenterLS.z - fBoundary;

float r = xmf3CenterLS.x + fBoundary;

float t = xmf3CenterLS.y + fBoundary;

float f = xmf3CenterLS.z + fBoundary;

XMMATRIX lightProj;

lightProj = XMMatrixOrthographicOffCenterLH(l, r, b, t, n, f);

// Transform NDC space [-1,+1]^2 to texture space [0,1]^2

XMMATRIX T(

0.5f, 0.0f, 0.0f, 0.0f,

0.0f, -0.5f, 0.0f, 0.0f,

0.0f, 0.0f, 1.0f, 0.0f,

0.5f, 0.5f, 0.0f, 1.0f);

XMMATRIX S = lightView * lightProj;

XMStoreFloat4x4(&m_xmf4x4LightViewProj[nShadowIndex], XMMatrixTranspose(S));

S = S * T;

XMStoreFloat4x4(&m_xmf4x4ShadowTransform[nShadowIndex], XMMatrixTranspose(S));

CB_SHADOW cbShadow{ m_xmf4x4ShadowTransform, m_xmf4x4LightViewProj, m_pLight->GetPosition(), m_xmf4x4CascadedViewProj};

m_ubShadowCB->CopyData(0, cbShadow);

pd3dCommandList->SetGraphicsRootConstantBufferView(3, m_ubShadowCB->Resource()->GetGPUVirtualAddress());

pd3dCommandList->SetGraphicsRoot32BitConstants(2, 1, &nShadowIndex, 1);

}

Advertisement

What does your root signature look like? You have “packoffset(c4)” following your shadow index which means “this variable will be located 16 bytes from the start of the constant buffer”, which is almost certainly not what you want.

This topic is closed to new replies.

Advertisement