0
我使用一個常量緩衝區來傳遞數據到我的着色器在每一幀,我遇到一個問題,其中一些緩衝區的成員的值指向相同記憶。恆定的緩衝區成員訪問相同的內存
當我使用Visual Studio 2012的調試工具,它看起來像被在緩衝器中設置或多或少正確的數據:
0 [0x00000000-0x00000003] | +0
1 [0x00000004-0x00000007] | +1
2 [0x00000008-0x0000000b] | +1
3 [0x0000000c-0x0000000f] | +1
4 [0x00000010-0x00000013] | +0.78539819
5 [0x00000014-0x00000017] | +1.1760513
6 [0x00000018-0x0000001b] | +0
7 [0x0000001c-0x0000001f] | +1
的問題是,當我調試着色器內,sunAngle和phaseFunction都具有相同的值 - 具體0.78539819
,它應該只是sunAngle的值。如果我交換兩個花車的順序,它確實改變爲1.1760513
,但兩者仍然相同。我以爲我會正確地把所有東西打包在一起,但是我錯過了如何確切地定義緩衝區的每個部分中的常量?
下面是我使用的C++結構:
struct SunData {
DirectX::XMFLOAT4 sunPosition;
float sunAngle;
float phaseFunctionResult;
};
而且着色器緩衝器看起來是這樣的:
// updated as the sun moves through the sky
cbuffer sunDependent : register(b1)
{
float4 sunPosition;
float sunAngle; // theta
float phaseFunctionResult; // F(theta, g)
}
下面是我使用初始化緩衝區的代碼:
XMVECTOR pos = XMVectorSet(0, 1, 1, 1);
XMStoreFloat3(&_sunPosition, pos);
XMStoreFloat4(&_sun.sunPosition, pos);
_sun.sunAngle = XMVectorGetX(
XMVector3AngleBetweenVectors(pos, XMVectorSet(0, 1, 0, 0))
);
_sun.phaseFunctionResult = _planet.phaseFunction(_sun.sunAngle);
// Fill in a buffer description.
D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = sizeof(SunData) + 8;
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;
// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA data;
data.pSysMem = &_sun;
data.SysMemPitch = 0;
data.SysMemSlicePitch = 0;
// Create the buffer.
ID3D11Buffer *constantBuffer = nullptr;
HRESULT hr = _d3dDevice->CreateBuffer(
&cbDesc,
&data,
&constantBuffer
);
assert(SUCCEEDED(hr));
// Set the buffer.
_d3dDeviceContext->VSSetConstantBuffers(1, 1, &constantBuffer);
_d3dDeviceContext->PSSetConstantBuffers(1, 1, &constantBuffer);
Release(constantBuffer);
這裏是使用值的像素着色器:
float4 main(in ATMOS_PS_INPUT input) : SV_TARGET
{
float R = sunAngle * sunPosition.x * sunIntensity.x
* attenuationCoefficient.x
* phaseFunctionResult;
return float4(R, 1, 1, 1);
}