2014-02-25 6 views
0

그래서 나는 아직 해결 방법을 알아 내야하는 이상한 버그가 있습니다. 내 hlsl 픽셀 쉐이더 내 cbuffer에 쓰려고하는 임.HLSL cbuffer가 완전히 기록되지 않습니까?

//this is in the hlsl 
cbuffer LightBuffer : register(b2) 
{ 
float4 lightRange   ;//will right but only to lightRange.x 
float3 lightPos   ;//will right correctly 
float3 lightColor   ;//will right correctly for x and y but not z 
    float3 lightDirection  ;//will not right correctly 
float2 spotLightAngles  ;//???? all around 
float padding; //useless padding 

}; 



//This is in another file 

struct LightBufferType 
{ 
D3DXVECTOR4 LightRange; 
D3DXVECTOR3 LightPosition; 
D3DXVECTOR3 LightColor; 
D3DXVECTOR3 lightDirection; 
D3DXVECTOR2 SpotLightAngles; 
float padding; 
}; 

//createing the buffer 
// Setup the description of the light dynamic constant buffer that is in the pixel shader. 
lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC; 
lightBufferDesc.ByteWidth = sizeof(LightBufferType); 
lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; 
lightBufferDesc.MiscFlags = 0; 
lightBufferDesc.StructureByteStride = 0; 


result = device->CreateBuffer(&lightBufferDesc, NULL, &m_lightBuffer); 
if(FAILED(result)) 
{ 
ERROR_LOGGER->WriteErrorMessage("Could not Create LightBuffer"); 
return false; 
} 

//mapping the data 
result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); 
if(FAILED(result)) 
{ 
    ERROR_LOGGER->WriteErrorMessage("Could not Map Light Buffer"); 
    return false; 
} 
// Get a pointer to the data in the constant buffer. 
dataPtr2 = (LightBufferType*)mappedResource.pData; 
// Copy the lighting variables into the constant buffer. 
dataPtr2->LightRange = lightRange; 
dataPtr2->LightPosition = lightPosition; 
dataPtr2->LightColor = LightColor; 
dataPtr2->lightDirection = LightDirection; 
dataPtr2->SpotLightAngles = SpotLAngles; 
// Unlock the constant buffer. 
deviceContext->Unmap(m_lightBuffer, 0); 
// Set the position of the light constant buffer in the pixel shader. 
bufferNumber = 2; 
// Finally set the light constant buffer in the pixel shader with the updated values. 
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer); 

내 코드의 모든을 알고 있지만이 두 버퍼는 64 바이트 크고 정말 이상한 오류가이 디버그 그래픽 장치 (D3D11_CREATE_DEVICE_DEBUG)에

답변

1

의 전원을 켜고 작동합니다 것을 당신은 볼 수 귀하 상수 버퍼가 예상대로 HLSL에서 64 바이트가 아닙니다.

// cbuffer LightBuffer 
// { 
// 
// float4 lightRange;     // Offset: 0 Size: 16 
// float3 lightPos;     // Offset: 16 Size: 12 
// float3 lightColor;     // Offset: 32 Size: 12 
// float3 lightDirection;    // Offset: 48 Size: 12 
// float2 spotLightAngles;   // Offset: 64 Size:  8 
// float padding;      // Offset: 72 Size:  4 
// 
// } 

상수 버퍼의 크기는 80 바이트입니다. 한편 C++ 측의 구조체는 HLSL 패킹 규칙 (float4 레지스터)을 따르지 않는 두 개의 유형이 정렬되지 않는 플로트 구조로 구성됩니다.