Antigroup

Members
  • Content count

    3
  • Joined

  • Last visited

Community Reputation

104 Neutral

About Antigroup

  • Rank
    Newbie
  1. Thanks. I'm not using D3DX, but I found the option for fxc to use different matrix packing.   I'm not sure how the order of the constant buffers is decided, but I got it to work as I'd expect it to using this code:   context->VSSetConstantBuffers(0, 1, &perFrameConstantBuffer); context->VSSetConstantBuffers(1, 1, &perObjectConstantBuffer); context->VSSetShader(vertexShader, NULL, 0); context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer); context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer); context->PSSetShader(pixelShader, NULL, 0); In the shader files, the buffers are the exact same: cbuffer PerFrameConstantBuffer : register(b0) { float4 lightColor; float4 lightAmbientColor; float4 lightDirection; float4 cameraPosition; }; cbuffer PerObjectConstantBuffer : register(b1) { float4x4 world; float4x4 wvp; float4 materialDiffuse; float4 materialSpecular; float4 materialAmbient; };   This works, I'm just not sure why, but I guess I'll figure it out eventually.   Thanks for the help.
  2. I thought they should be able to use the same slots, but something in another post I read while trying to figure this out seemed to imply that you should use different slots, and when I used the same slots for both, I got a warning about the constant buffer being smaller than expected for the pixel shader.   I just tried using slots 0 and 1 on both again, and for some reason the pixel shader has the buffers in the opposite slots from the way the vertex shader has them, but it works not using this code.   context->VSSetConstantBuffers(1, 1, &perFrameConstantBuffer); context->VSSetConstantBuffers(0, 1, &perObjectConstantBuffer); context->VSSetShader(vertexShader, NULL, 0); context->PSSetConstantBuffers(1, 1, &perObjectConstantBuffer); context->PSSetConstantBuffers(0, 1, &perFrameConstantBuffer); context->PSSetShader(pixelShader, NULL, 0);     I'm not sure how the slots for constant buffers are chosen. I thought they would be in the order declared in the shader files. Maybe I'll see how I can use reflection to figure that out rather than trial and error.   While I'm asking about constant buffers, when using xnamath matrices (with XMFLOAT4X4 for members), is there a reason for it to be necessary to transpose them before sending them to the constant buffer? I didn't see this mentioned anywhere, but a while back I found that not transposing them lead to very wrong results visually, but everything works fine when you transpose them. Does anybody know why this would be, or should I just keep doing it?
  3. I'm learning Direct3D 11 and I'm working on getting lighting to work, but for some reason the geometry I'm drawing is showing up black, and I've traced what seems to be the problem to the code that actually updates the constant buffers.   Here's the relevant code:   class LitEffect: public Effect { private: static ID3D11InputLayout *inputLayout; static ID3D11VertexShader *vertexShader; static ID3D11PixelShader *pixelShader; static ID3D11Buffer *perFrameConstantBuffer; static ID3D11Buffer *perObjectConstantBuffer; struct PerFrameConstants { XMFLOAT4 lightColor; XMFLOAT4 lightAmbientColor; XMFLOAT4 lightDirection; XMFLOAT4 cameraPosition; } perFrameCBuffer; bool perFrameChanged; struct PerObjectConstants { XMFLOAT4X4 world; XMFLOAT4X4 wvp; XMFLOAT4 diffuseColor; XMFLOAT4 specularColor; XMFLOAT4 ambientColor; } perObjectCBuffer; bool perObjectChanged; public: LitEffect(void); LitEffect(ID3D11Device * device); ~LitEffect(void); void setWVP(XMFLOAT4X4 wvp); void setWorld(XMFLOAT4X4 world); void setMaterial(XMFLOAT4 diffuse, XMFLOAT4 specular, XMFLOAT4 ambient); void setLight(XMFLOAT4 color, XMFLOAT4 ambient, XMFLOAT4 direction); void setCameraPosition(XMFLOAT4 position); void apply(ID3D11DeviceContext * context); };   Effect is just an abstract class that defines the apply(ID3D11DeviceContext*) method. I'm using two constant buffers based on how often they should be updated.   The problem seems to be happening in the apply() method. void LitEffect::apply(ID3D11DeviceContext * context) { if(perObjectChanged) { D3D11_MAPPED_SUBRESOURCE sr = {0}; //context->Map(perObjectConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sr); ////*((PerObjectConstants*)sr.pData) = perObjectCBuffer; //memcpy(sr.pData, &perObjectCBuffer, sr.RowPitch); //context->Unmap(perObjectConstantBuffer, 0); context->UpdateSubresource(perObjectConstantBuffer, 0, NULL, &perObjectCBuffer, sizeof(PerObjectConstants), sizeof(PerObjectConstants)); perObjectChanged = false; } if(perFrameChanged) { D3D11_MAPPED_SUBRESOURCE sr = {0}; //context->Map(perFrameConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &sr); ////*((PerFrameConstants*)sr.pData) = perFrameCBuffer; //memcpy(sr.pData, &perFrameCBuffer, sr.RowPitch); //context->Unmap(perFrameConstantBuffer, 0); context->UpdateSubresource(perFrameConstantBuffer, 0, NULL, &perFrameCBuffer, sizeof(PerFrameConstants), sizeof(PerFrameConstants)); perFrameChanged = false; } ID3D11Buffer *buffers[2]; buffers[0] = perObjectConstantBuffer; buffers[1] = perFrameConstantBuffer; context->IASetInputLayout(inputLayout); context->VSSetConstantBuffers(0, 2, buffers); context->VSSetShader(vertexShader, NULL, 0); context->PSSetConstantBuffers(2, 2, buffers); context->PSSetShader(pixelShader, NULL, 0); }   From using the Visual Studio debugging tools, I've noticed that before calling Map() or UpdateSubresource(), the data in the constant buffer structs is as it should be. Afterwards, however, much of the data is changed to random really small floating-point values, but somehow the world-view-projection matrix is fine, and the geometry appears on screen, but black.   I've done plenty of checking on the shaders and they seem to work correctly, but the correct data never makes it to them.   Is there something I'm not understanding about using constant buffers?