Problem with HLSL shaders.
I'm working with D3D12, and I've run into another problem. I managed to fix my command list problem, but apparently something is going wrong with my shaders? My code currently is
struct Vertex {
DirectX::XMFLOAT3 position; // 3D position
DirectX::XMFLOAT4 color; // Color (RGBA)
};
// Define three vertices of a triangle
Vertex triangleVertices[] = {
{ { 0.0f, 0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }, // Top (Red)
{ { 0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // Right (Green)
{ { -0.5f, -0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } } // Left (Blue)
};
ID3D12Resource* vertexBuffer = nullptr;
D3D12_VERTEX_BUFFER_VIEW vertexBufferView = {};
// Vertex buffer size
const UINT vertexBufferSize = sizeof(triangleVertices);
// Describe the vertex buffer resource
D3D12_HEAP_PROPERTIES heapProperties = {};
heapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
D3D12_RESOURCE_DESC resourceDesc = {};
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
resourceDesc.Width = vertexBufferSize;
resourceDesc.Height = 1;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
resourceDesc.SampleDesc.Count = 1;
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
// Create the vertex buffer resource
HRESULT hr = pImpl->device->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&vertexBuffer)
);
if (FAILED(hr)) {
MessageBox(0, L"Failed to create vertex buffer.", 0, 0);
return;
}
// Copy the vertex data into the buffer
void* mappedData = nullptr;
D3D12_RANGE readRange = {}; // We do not intend to read this resource on the CPU
vertexBuffer->Map(0, &readRange, &mappedData);
memcpy(mappedData, triangleVertices, sizeof(triangleVertices));
vertexBuffer->Unmap(0, nullptr);
// Describe the vertex buffer view
vertexBufferView.BufferLocation = vertexBuffer->GetGPUVirtualAddress();
vertexBufferView.StrideInBytes = sizeof(Vertex);
vertexBufferView.SizeInBytes = vertexBufferSize;
ID3D12RootSignature* rootSignature = nullptr;
D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc = {};
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
ID3DBlob* serializedRootSignature = nullptr;
ID3DBlob* errorBlob = nullptr;
hr = D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &serializedRootSignature, &errorBlob);
if (FAILED(hr)) {
MessageBox(0, L"Failed to serialize root signature.", 0, 0);
return;
}
hr = pImpl->device->CreateRootSignature(0, serializedRootSignature->GetBufferPointer(), serializedRootSignature->GetBufferSize(), IID_PPV_ARGS(&rootSignature));
if (FAILED(hr)) {
MessageBox(0, L"Failed to create root signature.", 0, 0);
return;
}
ID3D10Blob* vsBlob = nullptr;
ID3D10Blob* psBlob = nullptr;
D3D_SHADER_MACRO defines[] = { "VERTEX_SHADER", "1", nullptr, nullptr };
hr = D3DCompileFromFile(
L"Shaders.hlsl", // File path
defines, // No macros
nullptr, // No include handler
"main", // Entry point function
"vs_5_0", // Target vertex shader model
D3DCOMPILE_DEBUG, // Compilation flags
0, // Effect flags
&vsBlob, // Output bytecode blob
&errorBlob // Error message blob
);
if (FAILED(hr)) {
if (errorBlob) {
OutputDebugStringA((char*)errorBlob->GetBufferPointer());
errorBlob->Release();
}
return;
}
D3D_SHADER_MACRO defines2[] = { "PIXEL_SHADER", "1", nullptr, nullptr };
hr = D3DCompileFromFile(
L"Shaders.hlsl", // File path
defines2, // No macros
nullptr, // No include handler
"main", // Entry point function
"ps_5_0", // Target pixel shader model
D3DCOMPILE_DEBUG, // Compilation flags
0, // Effect flags
&psBlob, // Output bytecode blob
&errorBlob // Error message blob
);
if (FAILED(hr)) {
if (errorBlob) {
OutputDebugStringA((char*)errorBlob->GetBufferPointer());
errorBlob->Release();
}
return;
}
if (errorBlob) {
errorBlob->Release();
}
D3D12_INPUT_ELEMENT_DESC inputElementDesc[] = {
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
};
ID3D12PipelineState* pipelineState = nullptr;
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.InputLayout = { inputElementDesc, _countof(inputElementDesc) };
psoDesc.pRootSignature = rootSignature;
psoDesc.VS = { vsBlob->GetBufferPointer(), vsBlob->GetBufferSize() };
psoDesc.PS = { psBlob->GetBufferPointer(), psBlob->GetBufferSize() };
// Manually define the RasterizerState (equivalent to CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT))
D3D12_RASTERIZER_DESC rasterizerDesc = {};
rasterizerDesc.FillMode = D3D12_FILL_MODE_SOLID;
rasterizerDesc.CullMode = D3D12_CULL_MODE_BACK;
rasterizerDesc.FrontCounterClockwise = FALSE;
rasterizerDesc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
rasterizerDesc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
rasterizerDesc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
rasterizerDesc.DepthClipEnable = TRUE;
rasterizerDesc.MultisampleEnable = FALSE;
rasterizerDesc.AntialiasedLineEnable = FALSE;
rasterizerDesc.ForcedSampleCount = 0;
rasterizerDesc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
psoDesc.RasterizerState = rasterizerDesc;
// Manually define the BlendState (equivalent to CD3DX12_BLEND_DESC(D3D12_DEFAULT))
D3D12_BLEND_DESC blendDesc = {};
blendDesc.AlphaToCoverageEnable = FALSE;
blendDesc.IndependentBlendEnable = FALSE;
const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc = {
FALSE, FALSE,
D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
D3D12_LOGIC_OP_NOOP, D3D12_COLOR_WRITE_ENABLE_ALL };
blendDesc.RenderTarget[0] = defaultRenderTargetBlendDesc;
psoDesc.BlendState = blendDesc;
// DepthStencilState setup (if not using depth or stencil)
D3D12_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = FALSE;
depthStencilDesc.StencilEnable = FALSE;
psoDesc.DepthStencilState = depthStencilDesc;
// SampleMask, Topology, and RenderTarget setup
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
psoDesc.SampleDesc.Count = 1;
// Create the pipeline state object (PSO)
hr = pImpl->device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&pipelineState));
if (FAILED(hr)) {
_com_error err(hr);
MessageBoxW(0, err.ErrorMessage(), L"Failed to create pipeline state", 0);
return;
}
D3D12_VIEWPORT viewport = {};
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
viewport.Width = static_cast<float>(pImpl->width);
viewport.Height = static_cast<float>(pImpl->height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
D3D12_RECT scissorRect = {};
scissorRect.left = 0;
scissorRect.top = 0;
scissorRect.right = static_cast<LONG>(pImpl->width);
scissorRect.bottom = static_cast<LONG>(pImpl->height);
pImpl->commandAllocator->Reset();
pImpl->commandList->Reset(pImpl->commandAllocator, pipelineState);
pImpl->commandList->SetGraphicsRootSignature(rootSignature);
pImpl->commandList->RSSetViewports(1, &viewport);
pImpl->commandList->RSSetScissorRects(1, &scissorRect);
// Set the render target
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = pImpl->rtvHeap->GetCPUDescriptorHandleForHeapStart();
pImpl->commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
// Record commands to draw the triangle
pImpl->commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pImpl->commandList->IASetVertexBuffers(0, 1, &vertexBufferView);
// Clear the render target
const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
pImpl->commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
// Issue the draw call
pImpl->commandList->DrawInstanced(3, 1, 0, 0);
// Close the command list
pImpl->commandList->Close();
// Synchronize GPU and CPU
pImpl->fenceValue++;
pImpl->commandQueue->Signal(pImpl->fence, pImpl->fenceValue);
if (pImpl->fence->GetCompletedValue() < pImpl->fenceValue) {
pImpl->fence->SetEventOnCompletion(pImpl->fenceValue, pImpl->fenceEvent);
WaitForSingleObject(pImpl->fenceEvent, INFINITE);
}
my shaders look like
// Define the shader type for switching between vertex and pixel shader
// Use the preprocessor macro to differentiate between shaders
#ifdef VERTEX_SHADER
// Vertex Shader Code
struct VSInput {
float3 position : POSITION;
float4 color : COLOR;
};
struct PSInput {
float4 position : SV_POSITION;
float4 color : COLOR;
};
PSInput main(VSInput input) {
PSInput output;
output.position = float4(input.position, 1.0f);
output.color = input.color;
return output;
}
#endif
#ifdef PIXEL_SHADER
struct PSInput {
float4 position : SV_POSITION;
float4 color : COLOR;
};
// Pixel Shader Code
float4 main(PSInput input) : SV_TARGET {
return input.color; // Use the color from the PSInput structure
}
#endif
and the relevant console lines are
'Game.exe' (Win32): Loaded 'C:\Users\Caden\source\repos\Game\x64\Debug\Game.exe'. Symbols loaded.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\user32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\win32u.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\gdi32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\gdi32full.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\msvcp_win.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbase.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\dxgi.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\D3D12.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\D3DCompiler_47.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140d.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140_1d.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\cryptsp.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\imm32.dll'.
The thread 23840 has exited with code 0 (0x0).
'Game.exe' (Win32): Loaded 'C:\Windows\System32\uxtheme.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\combase.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\msctf.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\msvcrt.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\kernel.appcore.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\bcryptprimitives.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\sechost.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\bcrypt.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\TextInputFramework.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\oleaut32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\CoreMessaging.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\CoreUIComponents.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\WinTypes.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\advapi32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\cryptbase.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\D3D12Core.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DXGIDebug.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\d3d12SDKLayers.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DXCore.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\cfgmgr32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\ResourcePolicyClient.dll'.
'Game.exe' (Win32): Unloaded 'C:\Windows\System32\ResourcePolicyClient.dll'
'Game.exe' (Win32): Loaded 'C:\Windows\System32\directxdatabasehelper.dll'.
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(876)\directxdatabasehelper.dll!00007FFB701EF924: (caller: 00007FFB701EEEB9) ReturnHr(1) tid(2e40) 80004002 No such interface supported
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igd12umd64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igd12um64xel.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\crypt32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\ole32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\shell32.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\shlwapi.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\setupapi.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\drvstore.dll'.
'Game.exe' (Win32): Unloaded 'C:\Windows\System32\setupapi.dll'
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igdext64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\version.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\IntelControlLib.dll'.
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(876)\directxdatabasehelper.dll!00007FFB701EF924: (caller: 00007FFB701EEEB9) ReturnHr(2) tid(2e40) 80004002 No such interface supported
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(876)\directxdatabasehelper.dll!00007FFB701EF924: (caller: 00007FFB701EEEB9) ReturnHr(3) tid(2e40) 80004002 No such interface supported
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igdgmm64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\setupapi.dll'.
'Game.exe' (Win32): Unloaded 'C:\Windows\System32\setupapi.dll'
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igc64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igc-default64.dll'.
'Game.exe' (Win32): Unloaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igc-default64.dll'
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igc-default64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\windows.storage.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\SHCore.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\profapi.dll'.
Exception thrown at 0x00007FFB784CFE4C in Game.exe: Microsoft C++ exception: ShaderCache::ShaderCacheFileBackedEmptyCacheFileException at memory location 0x0000002999AFDF30.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\D3DSCache.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\userenv.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igd12dxva64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\setupapi.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igddxvacommon64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\media_bin_64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_181a492eed27d42f\igdinfo64.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\twinapi.appcore.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\dxilconv.dll'.
'Game.exe' (Win32): Unloaded 'C:\Windows\System32\dxilconv.dll'
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(876)\directxdatabasehelper.dll!00007FFB701EF924: (caller: 00007FFB701EEEB9) ReturnHr(4) tid(2e40) 80004002 No such interface supported
'Game.exe' (Win32): Loaded 'C:\Windows\System32\dcomp.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\dwmapi.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\rsaenh.dll'.
'Game.exe' (Win32): Loaded 'C:\Windows\System32\dxilconv.dll'.