Replace CreateShaderFromFile with D3D12Shader wrapper

This commit is contained in:
2026-03-15 18:57:01 +08:00
parent c3feeda5d4
commit f25672c7d6

View File

@@ -1,7 +1,7 @@
#include <windows.h> #include <windows.h>
#include <d3d12.h> #include <d3d12.h>
#include <dxgi1_4.h> #include <dxgi1_4.h>
#include <d3dcompiler.h> #include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include <DirectXMath.h> #include <DirectXMath.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -69,6 +69,9 @@ XCEngine::RHI::D3D12CommandAllocator gCommandAllocator;
XCEngine::RHI::D3D12CommandList gCommandList; XCEngine::RHI::D3D12CommandList gCommandList;
XCEngine::RHI::D3D12RootSignature gRootSignature; XCEngine::RHI::D3D12RootSignature gRootSignature;
XCEngine::RHI::D3D12PipelineState gPipelineState; XCEngine::RHI::D3D12PipelineState gPipelineState;
XCEngine::RHI::D3D12Shader gVertexShader;
XCEngine::RHI::D3D12Shader gGeometryShader;
XCEngine::RHI::D3D12Shader gPixelShader;
// 同步对象 // 同步对象
XCEngine::RHI::D3D12Fence gFence; XCEngine::RHI::D3D12Fence gFence;
@@ -286,31 +289,6 @@ ID3D12RootSignature* InitRootSignature() {
return gRootSignature.GetRootSignature(); return gRootSignature.GetRootSignature();
} }
//=================================================================================
// 着色器加载函数
// 从.hlsl文件编译着色器 (VS/GS/PS)
//=================================================================================
void CreateShaderFromFile(
LPCTSTR inShaderFilePath,
const char* inMainFunctionName,
const char* inTarget,
D3D12_SHADER_BYTECODE* inShader) {
ID3DBlob* shaderBuffer = nullptr;
ID3DBlob* errorBuffer = nullptr;
HRESULT hResult = D3DCompileFromFile(inShaderFilePath, nullptr, nullptr,
inMainFunctionName, inTarget, D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION,
0, &shaderBuffer, &errorBuffer);
if (FAILED(hResult)) {
char szLog[1024] = { 0 };
strcpy_s(szLog, (char*)errorBuffer->GetBufferPointer());
Log("CreateShaderFromFile error : [%s][%s]:[%s]\n", inMainFunctionName, inTarget, szLog);
errorBuffer->Release();
return;
}
inShader->pShaderBytecode = shaderBuffer->GetBufferPointer();
inShader->BytecodeLength = shaderBuffer->GetBufferSize();
}
//================================================================================= //=================================================================================
// 常量缓冲 (Constant Buffer) 创建与更新 // 常量缓冲 (Constant Buffer) 创建与更新
// UPLOAD堆: CPU可写, GPU可读 // UPLOAD堆: CPU可写, GPU可读
@@ -850,11 +828,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
staticMeshComponent.InitFromFile(gCommandList.GetCommandList(), "Res/Model/Sphere.lhsm"); staticMeshComponent.InitFromFile(gCommandList.GetCommandList(), "Res/Model/Sphere.lhsm");
ID3D12RootSignature* rootSignature = InitRootSignature(); ID3D12RootSignature* rootSignature = InitRootSignature();
D3D12_SHADER_BYTECODE vs, gs, ps; gVertexShader.CompileFromFile(L"Res/Shader/gs.hlsl", "MainVS", "vs_5_1");
CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainVS", "vs_5_1", &vs); gGeometryShader.CompileFromFile(L"Res/Shader/gs.hlsl", "MainGS", "gs_5_1");
CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainGS", "gs_5_1", &gs); gPixelShader.CompileFromFile(L"Res/Shader/gs.hlsl", "MainPS", "ps_5_1");
CreateShaderFromFile(L"Res/Shader/gs.hlsl", "MainPS", "ps_5_1", &ps); ID3D12PipelineState* pso = CreatePSO(rootSignature, gVertexShader.GetBytecode(), gPixelShader.GetBytecode(), gGeometryShader.GetBytecode());
ID3D12PipelineState* pso = CreatePSO(rootSignature, vs, ps, gs);
ID3D12Resource* cb = CreateConstantBufferObject(65536); ID3D12Resource* cb = CreateConstantBufferObject(65536);
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH( DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(