Added: simple vertex & pixel shader

This commit is contained in:
2026-10-06 18:27:21 +02:00
parent 2fb45a5c02
commit daa04be788
7 changed files with 114 additions and 0 deletions
Binary file not shown.
@@ -0,0 +1,4 @@
float4 main() : SV_TARGET
{
return float4(1.0f, 1.0f, 1.0f, 1.0f);
}
Binary file not shown.
@@ -0,0 +1,6 @@
// semantics: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics
float4 main( float4 pos : POSITION ) : SV_POSITION
{
return pos;
}
+20
View File
@@ -197,6 +197,26 @@
<ClInclude Include="Vertex.h" /> <ClInclude Include="Vertex.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<FxCompile Include="ColorPixelShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Pixel</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="ColorVertexShader.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">Vertex</ShaderType>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).cso</ObjectFileOutput>
</FxCompile>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
@@ -72,4 +72,12 @@
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup>
<FxCompile Include="ColorVertexShader.hlsl">
<Filter>Resource Files</Filter>
</FxCompile>
<FxCompile Include="ColorPixelShader.hlsl">
<Filter>Resource Files</Filter>
</FxCompile>
</ItemGroup>
</Project> </Project>
+76
View File
@@ -22,10 +22,23 @@ UINT Material::init(ID3D11Device* pD3DDevice, LPCWSTR textureFileName)
void Material::render(ID3D11DeviceContext* pD3DDeviceContext) void Material::render(ID3D11DeviceContext* pD3DDeviceContext)
{ {
// setup shaders
pD3DDeviceContext->VSSetShader(_pVertexShader, nullptr, 0);
pD3DDeviceContext->PSSetShader(_pPixelShader, nullptr, 0);
// setup the input layout
pD3DDeviceContext->IASetInputLayout(_pInputLayout);
} }
void Material::deInit() void Material::deInit()
{ {
safeRelease(_pSamplerState);
safeRelease(_pTexture);
safeRelease(_pMatrixBuffer);
safeRelease(_pInputLayout);
safeRelease(_pPixelShader);
safeRelease(_pVertexShader);
} }
void Material::setMatrixBuffer(ID3D11DeviceContext* pD3DDeviceContext, const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix) void Material::setMatrixBuffer(ID3D11DeviceContext* pD3DDeviceContext, const XMFLOAT4X4& worldMatrix, const XMFLOAT4X4& viewMatrix, const XMFLOAT4X4& projectionMatrix)
@@ -34,16 +47,79 @@ void Material::setMatrixBuffer(ID3D11DeviceContext* pD3DDeviceContext, const XMF
UINT Material::createVertexShader(ID3D11Device* pD3DDevice) UINT Material::createVertexShader(ID3D11Device* pD3DDevice)
{ {
HRESULT hr = {};
ID3DBlob* pCompiledShaderCode = nullptr;
// 1. get compiled shader code
// 2 options for creating a vertex shader object
// option 1: compile at runtime
//hr = D3DCompileFromFile(
// L"ColorVertexShader.hlsl", // shader file name
// nullptr, // optional define class
// nullptr, // optional include class
// "main", // entry point of the shader
// "vs_4_0", // shader type and version
// 0, // optional flags
// 0, // optional flags
// &pCompiledShaderCode, // target object for compiled shader code
// nullptr // optional target object for compiler error messages
//);
// option 2: load compiled shader from disk
hr = D3DReadFileToBlob(L"ColorVertexShader.cso", &pCompiledShaderCode);
CheckFailed(hr, 600);
// 2. create vertex shader object
hr = pD3DDevice->CreateVertexShader(
pCompiledShaderCode->GetBufferPointer(), // pointer to compiled shader code
pCompiledShaderCode->GetBufferSize(), // size of compiled shader code
nullptr, // optional linkage class
&_pVertexShader
);
CheckFailed(hr, 610);
// 3. create input layout
UINT error = createInputLayout(pD3DDevice, pCompiledShaderCode);
CheckError(error);
// 4. tidy up
safeRelease(pCompiledShaderCode);
return 0; return 0;
} }
UINT Material::createPixelShader(ID3D11Device* pD3DDevice) UINT Material::createPixelShader(ID3D11Device* pD3DDevice)
{ {
ID3DBlob* pCompiledShaderCode = nullptr;
HRESULT hr = D3DReadFileToBlob(L"ColorPixelShader.cso", &pCompiledShaderCode);
CheckFailed(hr, 620);
hr = pD3DDevice->CreatePixelShader(pCompiledShaderCode->GetBufferPointer(), pCompiledShaderCode->GetBufferSize(), nullptr, &_pPixelShader);
CheckFailed(hr, 630);
safeRelease(pCompiledShaderCode);
return 0; return 0;
} }
UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode) UINT Material::createInputLayout(ID3D11Device* pD3DDevice, ID3DBlob* pCompiledVertexShaderCode)
{ {
D3D11_INPUT_ELEMENT_DESC elements[1] = {};
// position
elements[0].SemanticName = "POSITION";
elements[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
HRESULT hr = pD3DDevice->CreateInputLayout(
elements, // array of input layout elements
std::size(elements), // count of elements
pCompiledVertexShaderCode->GetBufferPointer(), pCompiledVertexShaderCode->GetBufferSize(), // compiled vertex shader code
&_pInputLayout
);
CheckFailed(hr, 650);
return 0; return 0;
} }