1

So I moved from this problem: Previous problem to this one :).

I made 2 constant buffers in C++ which I'm passing to my HLSL Shader but when I debug and look into the buffers they are just filled with zeros except the last 3 numbers (which are -572662307). I was following this example https://msdn.microsoft.com/en-us/library/windows/desktop/ff476896(v=vs.85).aspx

This is the code I'm using to create the buffer.

    // Define the constant data used to communicate with shaders.
__declspec(align(4)) struct GS_CONSTANT_BUFFER_EDGETABLE
{
    int edgeTable[256];
};
__declspec(align(4)) struct GS_CONSTANT_BUFFER_TRITABLE
{
    int triTable[256][16];
};

// Supply the gs constant data.
GS_CONSTANT_BUFFER_EDGETABLE GsConstDataEdge;
GS_CONSTANT_BUFFER_TRITABLE GsConstDataTri;
for (int i = 0; i < 256; ++i)
{
    GsConstDataEdge.edgeTable[i] = edgeTable[i];
    for (int j = 0; j < 16; ++j)        
        GsConstDataTri.triTable[i][j] = triTable[i][j];     
}

//EDGE
// Fill in a buffer description.
D3D11_BUFFER_DESC cbDesc1;
cbDesc1.ByteWidth = sizeof(GsConstDataEdge);
cbDesc1.Usage = D3D11_USAGE_DYNAMIC;
cbDesc1.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc1.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc1.MiscFlags = 0;
cbDesc1.StructureByteStride = 0;

// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA InitData1;
InitData1.pSysMem = &GsConstDataEdge;
InitData1.SysMemPitch = 0;
InitData1.SysMemSlicePitch = 0;

// Create the buffer. 
auto hr = gameContext.pDevice->CreateBuffer(&cbDesc1, &InitData1, &g_pConstantBuffer1);

if (FAILED(hr)) 
    std::cout << "Error buffer creation1" << endl;  

// Set the buffer.
gameContext.pDeviceContext->GSSetConstantBuffers(0, 1, &g_pConstantBuffer1);
//

// TRI
// Fill in a buffer description.
D3D11_BUFFER_DESC cbDesc2;
cbDesc2.ByteWidth = sizeof(GsConstDataTri);
cbDesc2.Usage = D3D11_USAGE_DYNAMIC;
cbDesc2.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc2.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc2.MiscFlags = 0;
cbDesc2.StructureByteStride = 0;

// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA InitData2;
InitData2.pSysMem = &GsConstDataTri;
InitData2.SysMemPitch = 0;
InitData2.SysMemSlicePitch = 0;

// Create the buffer. 
hr = gameContext.pDevice->CreateBuffer(&cbDesc2, &InitData2, &g_pConstantBuffer2);

if (FAILED(hr)) 
    std::cout << "Error buffer creation2" << endl;  

// Set the buffer.
gameContext.pDeviceContext->GSSetConstantBuffers(1, 1, &g_pConstantBuffer2);

This is the struct in my HLSL shader.

cbuffer GS_CONSTANT_BUFFER_EDGETABLE : register(b0)
{
    int edgeTable[256];
}

cbuffer GS_CONSTANT_BUFFER_TRITABLE : register(b1)
{
    int triTable[256][16];
}

EDIT: http://imgur.com/Xv7B1SO Now I do have the 2 buffers I created but they don't seems to get assigned to the cbuffers in the HLSL code.

Community
  • 1
  • 1
Achille Depla
  • 97
  • 1
  • 10
  • Does sizeof(GsConstDataTri) give you the right size? Off the top of my head, I'm not sure it will give you the full size of the array, it might only give you the size of the pointer. – cehnehdeh Jun 09 '15 at 21:08
  • It does give the right size (16384) but when I check the buffer which gets used it says size 65536. I don't think that's correct? (same for the other buffer ofc) – Achille Depla Jun 09 '15 at 21:14
  • I think you need to not pass the address of the pointers when you set pSysMem, so remove the &. That pointer is a pointer to the memory. This makes it a void*, which means all type security is thrown out the window. – cehnehdeh Jun 09 '15 at 21:18
  • I was following this example of MS https://msdn.microsoft.com/en-us/library/windows/desktop/ff476896(v=vs.85).aspx. When I remove the & I get an error message because pSysMem require a void pointer. – Achille Depla Jun 09 '15 at 21:24
  • Never mind, you're right, I thought it was dynamically allocated. Due to really weird byte-alignment stuff, there might be an offset. Try giving the address of the actual array, so GsConstDataTri.triTable – cehnehdeh Jun 09 '15 at 21:26
  • Nope still the same result, I'm really wondering where those 3 last values every time come from http://i.imgur.com/4M02X2h.png. – Achille Depla Jun 09 '15 at 21:30
  • The actual data you're filling it with is valid, correct? – cehnehdeh Jun 09 '15 at 21:30
  • Yes, when I check it in the C++ debugger the buffers are filled with the correct data.http://i.imgur.com/IMXb1Ok.png – Achille Depla Jun 09 '15 at 21:34
  • What shader is this for? You're binding this to the geometry shader, is that correct? – cehnehdeh Jun 09 '15 at 21:35
  • Yes, I need this data in the geometry shader stage to use them for a marching cube algorithm – Achille Depla Jun 09 '15 at 21:36
  • Does anything get bound to that cb slot afterwards? – cehnehdeh Jun 09 '15 at 21:37
  • I can't say with 100% certainty since I didn't code everything in this engine, but I don't think so. – Achille Depla Jun 09 '15 at 21:42
  • In your shader, try changing it from int blahblah[size] to vector name, and the other to vector, 256> name. I looked around online, it's the only thing I can find. – cehnehdeh Jun 09 '15 at 21:44
  • Still the same results, maybe it actually is something with the engine. – Achille Depla Jun 09 '15 at 21:57
  • I pulled up my code for constant buffers, everything checks out. I'd say that buffer is being set somewhere else, try using buffer slots 14 and 15. Make sure to set this both in your source and shader code. That would be the first parameter of SetConstantBuffers. – cehnehdeh Jun 09 '15 at 21:58
  • How are you 'looking into the buffers' with the debugger? Using graphics diagnostics (if not, I would suggest trying it? – MuertoExcobito Jun 09 '15 at 21:58
  • Yes I've tried using higher slots, the same results. – Achille Depla Jun 09 '15 at 22:02
  • And yes I'm looking at the buffer in graphics diagnostics. – Achille Depla Jun 09 '15 at 22:02
  • This is the correct way to access the value in fx right? int triCount = edgeTable[index] * 3; – Achille Depla Jun 09 '15 at 22:04
  • Alrigt I'm a bit closer now, I can see the two correct buffers in the graphics diagnostics, but they don't yet get assigned to a register. – Achille Depla Jun 10 '15 at 02:50

1 Answers1

0

Fixed it, I simply added it to the pipeline to soon. I had to assign it just before calling it the draw but after the get technique.

Achille Depla
  • 97
  • 1
  • 10