简体   繁体   English

HLSL cbuffer不能完全写入吗?

[英]HLSL cbuffer does not get written to completely?

so I have this weird bug that I have yet to figure out how to fix. 所以我有一个奇怪的错误,我还没有弄清楚如何解决。 Im trying to write to my cbuffer in my hlsl pixel shader. 我试图在我的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);

I know its not all of my code but this is a really weird error both buffers are 64 bytes big this should work 我不知道我的代码不是全部,但这是一个非常奇怪的错误,两个缓冲区都为64字节大,应该可以使用

Turn on the Debug Graphics Device (D3D11_CREATE_DEVICE_DEBUG) and you should see that your constant buffer is not 64 bytes in HLSL as you expect. 打开调试图形设备(D3D11_CREATE_DEVICE_DEBUG),您应该看到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  
//  
// }  

All told, your constant buffer is 80 bytes in size. 总而言之,您的常量缓冲区的大小为80个字节。 Meanwhile your struct on the C++ side is made up of structures of floats which don't adhere to the HLSL packing rules (float4 registers) so your two types don't align. 同时,您在C ++端的结构由不遵循HLSL打包规则(float4寄存器)的float组成,因此您的两种类型不对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM