상수 버퍼를 사용하여 모든 프레임에서 데이터를 내 셰이더에 전달하고 일부 버퍼 멤버의 값이 같은 점을 가리키는 문제가 발생합니다. 기억.상수 버퍼 멤버가 동일한 메모리에 액세스합니다.
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을 디버깅 할 때와 : 나는 비주얼 스튜디오 2012 디버깅 도구를 사용하는 경우
는, 데이터 다소 제대로 버퍼에 설정되는 것 같습니다 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);
}