I have a shader that is not loading correctly and causing my Vertex Shader object to be a nullptr and Visual Studio is throwing an exception. The specific issue that is occurring is at the following lines below:
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
and because of that not compiling correctly, the corresponding call create the shader is throwing the nullptr exception.
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
I am guessing that it is due to a syntax error within the shader. I have a cbuffer that holds the view and projection matrices and would like to multiply those with the vertices as they are passed. What is the correct syntax to use the variables in the cbuffer in another functino within the shader file?
windowSource.cpp
void ParseGraphics() {
// Build constant buffer for matrix data
D3D11_BUFFER_DESC matrixBufferDesc;
ZeroMemory(&matrixBufferDesc, sizeof(matrixBufferDesc));
matrixBufferDesc.ByteWidth = sizeof(VS_CONSTANT_BUFFER);
matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
matrixBufferDesc.Usage = D3D11_USAGE_DEFAULT;
matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
matrixBufferDesc.MiscFlags = 0;
matrixBufferDesc.StructureByteStride = 0;
D3D11_SUBRESOURCE_DATA matrixSubResourceData;
matrixSubResourceData.pSysMem = &shaderMatrixBuffer;
matrixSubResourceData.SysMemPitch = 0;
matrixSubResourceData.SysMemSlicePitch = 0;
hr = dev->CreateBuffer(&matrixBufferDesc, &matrixSubResourceData, &pShaderMatrixConstantBuffer);
if (FAILED(hr)) {
OutputDebugString(L"Matrix Buffer Creation Failed");
}
devContext->VSSetConstantBuffers(0, 1, &pShaderMatrixConstantBuffer);
// Create Vertex Buffer
D3D11_BUFFER_DESC v_bd;
ZeroMemory(&v_bd, sizeof(v_bd));
v_bd.Usage = D3D11_USAGE_DYNAMIC;
v_bd.ByteWidth = sizeof(Vertex) * 6;
v_bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
v_bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dev->CreateBuffer(&v_bd, NULL, &pVBuffer);
// copy vertices into buffer
D3D11_MAPPED_SUBRESOURCE msr;
devContext->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &msr);
memcpy(msr.pData, square, sizeof(square));
devContext->Unmap(pVBuffer, NULL);
}
void BuildPipeline() {
// Load and Compile Shaders
ID3D10Blob *VS, *PS;
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_5_0", 0, 0, 0, &VS, 0, 0);
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "PShader", "ps_5_0", 0, 0, 0, &PS, 0, 0);
// Create shaders from the data in the Blobs Buffer
dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS);
// Apply Shaders to the device context
devContext->VSSetShader(pVS, 0, 0);
devContext->PSSetShader(pPS, 0, 0);
// Define the layout of the input given to the shaders
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 3 * sizeof(float), D3D11_INPUT_PER_VERTEX_DATA, 0},
};
dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devContext->IASetInputLayout(pLayout);
}
shaders.shader
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
cbuffer VS_CONSTANT_BUFFER : register(b0) {
matrix viewMatrix;
matrix projMatrix
}
VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
VOut output;
output.position = mul(position, viewMatrix);
output.position = mul(output.position, projMatrix);
output.color = color;
return output;
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
UPDATED CODE
New shaders.shader file
struct Vin {
float3 position : POSITION;
float4 color : COLOR0;
};
struct VOut{
float4 position : SV_POSITION;
float4 color : COLOR1;
};
cbuffer VS_CONSTANT_BUFFER : register(b0) {
matrix viewMatrix;
matrix projMatrix;
};
VOut VShader(Vin input)
{
VOut output;
output.position = mul(float4(input.position, 1), viewMatrix);
output.position = mul(output.position, projMatrix);
output.color = input.color;
return output;
}
float4 PShader(VOut output) : SV_TARGET
{
return color;
}