chore: sync workspace state
This commit is contained in:
@@ -1,28 +1,32 @@
|
||||
#include <windows.h>
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
#include <filesystem>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_4.h>
|
||||
|
||||
#include "XCEngine/RHI/RHIEnums.h"
|
||||
#include "XCEngine/RHI/RHITypes.h"
|
||||
#include "XCEngine/RHI/RHIBuffer.h"
|
||||
#include "XCEngine/RHI/RHIDescriptorPool.h"
|
||||
#include "XCEngine/RHI/RHIDescriptorSet.h"
|
||||
#include "XCEngine/RHI/RHIPipelineLayout.h"
|
||||
#include "XCEngine/RHI/RHIPipelineState.h"
|
||||
#include "XCEngine/RHI/RHIResourceView.h"
|
||||
#include "XCEngine/RHI/RHISampler.h"
|
||||
#include "XCEngine/RHI/RHITexture.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Device.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12CommandQueue.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12CommandAllocator.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12CommandList.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12DescriptorHeap.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Fence.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
|
||||
#include "XCEngine/Debug/Logger.h"
|
||||
#include "XCEngine/Debug/ConsoleLogSink.h"
|
||||
#include "XCEngine/Debug/FileLogSink.h"
|
||||
@@ -40,6 +44,48 @@ using namespace XCEngine::Containers;
|
||||
#pragma comment(lib,"d3dcompiler.lib")
|
||||
#pragma comment(lib,"winmm.lib")
|
||||
|
||||
namespace {
|
||||
|
||||
struct Vertex {
|
||||
float pos[4];
|
||||
float uv[2];
|
||||
};
|
||||
|
||||
constexpr Vertex kQuadVertices[] = {
|
||||
{ { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
{ { -0.5f, 0.5f, 0.0f, 1.0f }, { 0.0f, 0.0f } },
|
||||
{ { 0.5f, -0.5f, 0.0f, 1.0f }, { 1.0f, 1.0f } },
|
||||
{ { 0.5f, 0.5f, 0.0f, 1.0f }, { 1.0f, 0.0f } },
|
||||
};
|
||||
|
||||
constexpr uint32_t kQuadIndices[] = { 0, 1, 2, 2, 1, 3 };
|
||||
|
||||
const char kQuadHlsl[] = R"(
|
||||
Texture2D gTexture : register(t0);
|
||||
SamplerState gSampler : register(s0);
|
||||
|
||||
struct VSInput {
|
||||
float4 position : POSITION;
|
||||
float2 texcoord : TEXCOORD;
|
||||
};
|
||||
|
||||
struct PSInput {
|
||||
float4 position : SV_POSITION;
|
||||
float2 texcoord : TEXCOORD;
|
||||
};
|
||||
|
||||
PSInput MainVS(VSInput input) {
|
||||
PSInput output;
|
||||
output.position = input.position;
|
||||
output.texcoord = input.texcoord;
|
||||
return output;
|
||||
}
|
||||
|
||||
float4 MainPS(PSInput input) : SV_TARGET {
|
||||
return gTexture.Sample(gSampler, input.texcoord);
|
||||
}
|
||||
)";
|
||||
|
||||
D3D12Device gDevice;
|
||||
D3D12CommandQueue gCommandQueue;
|
||||
D3D12SwapChain gSwapChain;
|
||||
@@ -48,17 +94,22 @@ D3D12CommandList gCommandList;
|
||||
D3D12Texture gDepthStencil;
|
||||
D3D12DescriptorHeap gRTVHeap;
|
||||
D3D12DescriptorHeap gDSVHeap;
|
||||
D3D12DescriptorHeap gSRVHeap;
|
||||
D3D12ResourceView gRTVs[2];
|
||||
D3D12ResourceView gDSV;
|
||||
|
||||
D3D12Shader gVertexShader;
|
||||
D3D12Shader gPixelShader;
|
||||
D3D12RootSignature gRootSignature;
|
||||
D3D12PipelineState gPipelineState;
|
||||
D3D12Buffer gVertexBuffer;
|
||||
D3D12Texture gDiffuseTexture;
|
||||
D3D12ResourceView gDiffuseSRV;
|
||||
RHIBuffer* gVertexBuffer = nullptr;
|
||||
RHIResourceView* gVertexBufferView = nullptr;
|
||||
RHIBuffer* gIndexBuffer = nullptr;
|
||||
RHIResourceView* gIndexBufferView = nullptr;
|
||||
RHITexture* gTexture = nullptr;
|
||||
RHIResourceView* gTextureView = nullptr;
|
||||
RHISampler* gSampler = nullptr;
|
||||
RHIDescriptorPool* gTexturePool = nullptr;
|
||||
RHIDescriptorSet* gTextureSet = nullptr;
|
||||
RHIDescriptorPool* gSamplerPool = nullptr;
|
||||
RHIDescriptorSet* gSamplerSet = nullptr;
|
||||
RHIPipelineLayout* gPipelineLayout = nullptr;
|
||||
RHIPipelineState* gPipelineState = nullptr;
|
||||
|
||||
UINT gRTVDescriptorSize = 0;
|
||||
UINT gDSVDescriptorSize = 0;
|
||||
@@ -68,6 +119,15 @@ HWND gHWND = nullptr;
|
||||
int gWidth = 1280;
|
||||
int gHeight = 720;
|
||||
|
||||
template <typename T>
|
||||
void ShutdownAndDelete(T*& object) {
|
||||
if (object != nullptr) {
|
||||
object->Shutdown();
|
||||
delete object;
|
||||
object = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Log(const char* format, ...) {
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
@@ -77,6 +137,275 @@ void Log(const char* format, ...) {
|
||||
Logger::Get().Debug(LogCategory::Rendering, String(buffer));
|
||||
}
|
||||
|
||||
std::filesystem::path GetExecutableDirectory() {
|
||||
char exePath[MAX_PATH] = {};
|
||||
const DWORD length = GetModuleFileNameA(nullptr, exePath, MAX_PATH);
|
||||
if (length == 0 || length >= MAX_PATH) {
|
||||
return std::filesystem::current_path();
|
||||
}
|
||||
|
||||
return std::filesystem::path(exePath).parent_path();
|
||||
}
|
||||
|
||||
std::filesystem::path ResolveRuntimePath(const char* relativePath) {
|
||||
return GetExecutableDirectory() / relativePath;
|
||||
}
|
||||
|
||||
bool LoadTexture() {
|
||||
const std::filesystem::path texturePath = ResolveRuntimePath("Res/Image/earth.png");
|
||||
const std::string texturePathString = texturePath.string();
|
||||
|
||||
stbi_set_flip_vertically_on_load(0);
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int channels = 0;
|
||||
stbi_uc* pixels = stbi_load(texturePathString.c_str(), &width, &height, &channels, STBI_rgb_alpha);
|
||||
if (pixels == nullptr) {
|
||||
Log("[ERROR] Failed to load texture: %s", texturePathString.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
TextureDesc textureDesc = {};
|
||||
textureDesc.width = static_cast<uint32_t>(width);
|
||||
textureDesc.height = static_cast<uint32_t>(height);
|
||||
textureDesc.depth = 1;
|
||||
textureDesc.mipLevels = 1;
|
||||
textureDesc.arraySize = 1;
|
||||
textureDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
|
||||
textureDesc.textureType = static_cast<uint32_t>(TextureType::Texture2D);
|
||||
textureDesc.sampleCount = 1;
|
||||
textureDesc.sampleQuality = 0;
|
||||
textureDesc.flags = 0;
|
||||
|
||||
gTexture = gDevice.CreateTexture(
|
||||
textureDesc,
|
||||
pixels,
|
||||
static_cast<size_t>(width) * static_cast<size_t>(height) * 4,
|
||||
static_cast<uint32_t>(width) * 4);
|
||||
stbi_image_free(pixels);
|
||||
|
||||
if (gTexture == nullptr) {
|
||||
Log("[ERROR] Failed to create RHI texture");
|
||||
return false;
|
||||
}
|
||||
|
||||
ResourceViewDesc textureViewDesc = {};
|
||||
textureViewDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
|
||||
textureViewDesc.dimension = ResourceViewDimension::Texture2D;
|
||||
textureViewDesc.mipLevel = 0;
|
||||
gTextureView = gDevice.CreateShaderResourceView(gTexture, textureViewDesc);
|
||||
if (gTextureView == nullptr) {
|
||||
Log("[ERROR] Failed to create texture SRV");
|
||||
return false;
|
||||
}
|
||||
|
||||
SamplerDesc samplerDesc = {};
|
||||
samplerDesc.filter = static_cast<uint32_t>(FilterMode::Linear);
|
||||
samplerDesc.addressU = static_cast<uint32_t>(TextureAddressMode::Clamp);
|
||||
samplerDesc.addressV = static_cast<uint32_t>(TextureAddressMode::Clamp);
|
||||
samplerDesc.addressW = static_cast<uint32_t>(TextureAddressMode::Clamp);
|
||||
samplerDesc.mipLodBias = 0.0f;
|
||||
samplerDesc.maxAnisotropy = 1;
|
||||
samplerDesc.comparisonFunc = static_cast<uint32_t>(ComparisonFunc::Always);
|
||||
samplerDesc.borderColorR = 0.0f;
|
||||
samplerDesc.borderColorG = 0.0f;
|
||||
samplerDesc.borderColorB = 0.0f;
|
||||
samplerDesc.borderColorA = 0.0f;
|
||||
samplerDesc.minLod = 0.0f;
|
||||
samplerDesc.maxLod = 1000.0f;
|
||||
gSampler = gDevice.CreateSampler(samplerDesc);
|
||||
if (gSampler == nullptr) {
|
||||
Log("[ERROR] Failed to create sampler");
|
||||
return false;
|
||||
}
|
||||
|
||||
DescriptorPoolDesc texturePoolDesc = {};
|
||||
texturePoolDesc.type = DescriptorHeapType::CBV_SRV_UAV;
|
||||
texturePoolDesc.descriptorCount = 1;
|
||||
texturePoolDesc.shaderVisible = true;
|
||||
gTexturePool = gDevice.CreateDescriptorPool(texturePoolDesc);
|
||||
if (gTexturePool == nullptr) {
|
||||
Log("[ERROR] Failed to create texture descriptor pool");
|
||||
return false;
|
||||
}
|
||||
|
||||
DescriptorSetLayoutBinding textureBinding = {};
|
||||
textureBinding.binding = 0;
|
||||
textureBinding.type = static_cast<uint32_t>(DescriptorType::SRV);
|
||||
textureBinding.count = 1;
|
||||
|
||||
DescriptorSetLayoutDesc textureLayoutDesc = {};
|
||||
textureLayoutDesc.bindings = &textureBinding;
|
||||
textureLayoutDesc.bindingCount = 1;
|
||||
|
||||
gTextureSet = gTexturePool->AllocateSet(textureLayoutDesc);
|
||||
if (gTextureSet == nullptr) {
|
||||
Log("[ERROR] Failed to allocate texture descriptor set");
|
||||
return false;
|
||||
}
|
||||
gTextureSet->Update(0, gTextureView);
|
||||
|
||||
DescriptorPoolDesc samplerPoolDesc = {};
|
||||
samplerPoolDesc.type = DescriptorHeapType::Sampler;
|
||||
samplerPoolDesc.descriptorCount = 1;
|
||||
samplerPoolDesc.shaderVisible = true;
|
||||
gSamplerPool = gDevice.CreateDescriptorPool(samplerPoolDesc);
|
||||
if (gSamplerPool == nullptr) {
|
||||
Log("[ERROR] Failed to create sampler descriptor pool");
|
||||
return false;
|
||||
}
|
||||
|
||||
DescriptorSetLayoutBinding samplerBinding = {};
|
||||
samplerBinding.binding = 0;
|
||||
samplerBinding.type = static_cast<uint32_t>(DescriptorType::Sampler);
|
||||
samplerBinding.count = 1;
|
||||
|
||||
DescriptorSetLayoutDesc samplerLayoutDesc = {};
|
||||
samplerLayoutDesc.bindings = &samplerBinding;
|
||||
samplerLayoutDesc.bindingCount = 1;
|
||||
|
||||
gSamplerSet = gSamplerPool->AllocateSet(samplerLayoutDesc);
|
||||
if (gSamplerSet == nullptr) {
|
||||
Log("[ERROR] Failed to allocate sampler descriptor set");
|
||||
return false;
|
||||
}
|
||||
gSamplerSet->UpdateSampler(0, gSampler);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GraphicsPipelineDesc CreateQuadPipelineDesc() {
|
||||
GraphicsPipelineDesc desc = {};
|
||||
desc.pipelineLayout = gPipelineLayout;
|
||||
desc.topologyType = static_cast<uint32_t>(PrimitiveTopologyType::Triangle);
|
||||
desc.renderTargetFormats[0] = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
|
||||
desc.depthStencilFormat = static_cast<uint32_t>(Format::Unknown);
|
||||
desc.sampleCount = 1;
|
||||
|
||||
desc.rasterizerState.fillMode = static_cast<uint32_t>(FillMode::Solid);
|
||||
desc.rasterizerState.cullMode = static_cast<uint32_t>(CullMode::None);
|
||||
desc.rasterizerState.frontFace = static_cast<uint32_t>(FrontFace::CounterClockwise);
|
||||
desc.rasterizerState.depthClipEnable = true;
|
||||
|
||||
desc.depthStencilState.depthTestEnable = false;
|
||||
desc.depthStencilState.depthWriteEnable = false;
|
||||
desc.depthStencilState.stencilEnable = false;
|
||||
|
||||
InputElementDesc position = {};
|
||||
position.semanticName = "POSITION";
|
||||
position.semanticIndex = 0;
|
||||
position.format = static_cast<uint32_t>(Format::R32G32B32A32_Float);
|
||||
position.inputSlot = 0;
|
||||
position.alignedByteOffset = 0;
|
||||
desc.inputLayout.elements.push_back(position);
|
||||
|
||||
InputElementDesc texcoord = {};
|
||||
texcoord.semanticName = "TEXCOORD";
|
||||
texcoord.semanticIndex = 0;
|
||||
texcoord.format = static_cast<uint32_t>(Format::R32G32_Float);
|
||||
texcoord.inputSlot = 0;
|
||||
texcoord.alignedByteOffset = sizeof(float) * 4;
|
||||
desc.inputLayout.elements.push_back(texcoord);
|
||||
|
||||
desc.vertexShader.source.assign(kQuadHlsl, kQuadHlsl + strlen(kQuadHlsl));
|
||||
desc.vertexShader.sourceLanguage = ShaderLanguage::HLSL;
|
||||
desc.vertexShader.entryPoint = L"MainVS";
|
||||
desc.vertexShader.profile = L"vs_5_0";
|
||||
|
||||
desc.fragmentShader.source.assign(kQuadHlsl, kQuadHlsl + strlen(kQuadHlsl));
|
||||
desc.fragmentShader.sourceLanguage = ShaderLanguage::HLSL;
|
||||
desc.fragmentShader.entryPoint = L"MainPS";
|
||||
desc.fragmentShader.profile = L"ps_5_0";
|
||||
|
||||
return desc;
|
||||
}
|
||||
|
||||
bool InitializeQuadResources() {
|
||||
BufferDesc vertexBufferDesc = {};
|
||||
vertexBufferDesc.size = sizeof(kQuadVertices);
|
||||
vertexBufferDesc.stride = sizeof(Vertex);
|
||||
vertexBufferDesc.bufferType = static_cast<uint32_t>(BufferType::Vertex);
|
||||
gVertexBuffer = gDevice.CreateBuffer(vertexBufferDesc);
|
||||
if (gVertexBuffer == nullptr) {
|
||||
Log("[ERROR] Failed to create vertex buffer");
|
||||
return false;
|
||||
}
|
||||
gVertexBuffer->SetData(kQuadVertices, sizeof(kQuadVertices));
|
||||
gVertexBuffer->SetStride(sizeof(Vertex));
|
||||
gVertexBuffer->SetBufferType(BufferType::Vertex);
|
||||
|
||||
ResourceViewDesc vertexViewDesc = {};
|
||||
vertexViewDesc.dimension = ResourceViewDimension::Buffer;
|
||||
vertexViewDesc.structureByteStride = sizeof(Vertex);
|
||||
gVertexBufferView = gDevice.CreateVertexBufferView(gVertexBuffer, vertexViewDesc);
|
||||
if (gVertexBufferView == nullptr) {
|
||||
Log("[ERROR] Failed to create vertex buffer view");
|
||||
return false;
|
||||
}
|
||||
|
||||
BufferDesc indexBufferDesc = {};
|
||||
indexBufferDesc.size = sizeof(kQuadIndices);
|
||||
indexBufferDesc.stride = sizeof(uint32_t);
|
||||
indexBufferDesc.bufferType = static_cast<uint32_t>(BufferType::Index);
|
||||
gIndexBuffer = gDevice.CreateBuffer(indexBufferDesc);
|
||||
if (gIndexBuffer == nullptr) {
|
||||
Log("[ERROR] Failed to create index buffer");
|
||||
return false;
|
||||
}
|
||||
gIndexBuffer->SetData(kQuadIndices, sizeof(kQuadIndices));
|
||||
gIndexBuffer->SetStride(sizeof(uint32_t));
|
||||
gIndexBuffer->SetBufferType(BufferType::Index);
|
||||
|
||||
ResourceViewDesc indexViewDesc = {};
|
||||
indexViewDesc.dimension = ResourceViewDimension::Buffer;
|
||||
indexViewDesc.format = static_cast<uint32_t>(Format::R32_UInt);
|
||||
gIndexBufferView = gDevice.CreateIndexBufferView(gIndexBuffer, indexViewDesc);
|
||||
if (gIndexBufferView == nullptr) {
|
||||
Log("[ERROR] Failed to create index buffer view");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!LoadTexture()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
pipelineLayoutDesc.textureCount = 1;
|
||||
pipelineLayoutDesc.samplerCount = 1;
|
||||
gPipelineLayout = gDevice.CreatePipelineLayout(pipelineLayoutDesc);
|
||||
if (gPipelineLayout == nullptr) {
|
||||
Log("[ERROR] Failed to create pipeline layout");
|
||||
return false;
|
||||
}
|
||||
|
||||
GraphicsPipelineDesc pipelineDesc = CreateQuadPipelineDesc();
|
||||
gPipelineState = gDevice.CreatePipelineState(pipelineDesc);
|
||||
if (gPipelineState == nullptr || !gPipelineState->IsValid()) {
|
||||
Log("[ERROR] Failed to create pipeline state");
|
||||
return false;
|
||||
}
|
||||
|
||||
Log("[INFO] Quad resources initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
void ShutdownQuadResources() {
|
||||
ShutdownAndDelete(gPipelineState);
|
||||
ShutdownAndDelete(gPipelineLayout);
|
||||
ShutdownAndDelete(gTextureSet);
|
||||
ShutdownAndDelete(gSamplerSet);
|
||||
ShutdownAndDelete(gTexturePool);
|
||||
ShutdownAndDelete(gSamplerPool);
|
||||
ShutdownAndDelete(gSampler);
|
||||
ShutdownAndDelete(gTextureView);
|
||||
ShutdownAndDelete(gTexture);
|
||||
ShutdownAndDelete(gVertexBufferView);
|
||||
ShutdownAndDelete(gIndexBufferView);
|
||||
ShutdownAndDelete(gVertexBuffer);
|
||||
ShutdownAndDelete(gIndexBuffer);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
switch (msg) {
|
||||
case WM_CLOSE:
|
||||
@@ -86,39 +415,8 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
return DefWindowProc(hwnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
bool LoadTexture(const char* filename, D3D12Texture& texture, D3D12ResourceView& srv, ID3D12Device* device, D3D12DescriptorHeap& srvHeap, ID3D12GraphicsCommandList* commandList, D3D12CommandAllocator& allocator, D3D12CommandQueue& queue) {
|
||||
int width, height, channels;
|
||||
stbi_uc* pixels = stbi_load(filename, &width, &height, &channels, STBI_rgb_alpha);
|
||||
if (!pixels) {
|
||||
Log("[ERROR] Failed to load texture: %s", filename);
|
||||
return false;
|
||||
}
|
||||
|
||||
allocator.Reset();
|
||||
commandList->Reset(allocator.GetCommandAllocator(), nullptr);
|
||||
|
||||
if (!texture.InitializeFromData(device, commandList, pixels, width, height, DXGI_FORMAT_R8G8B8A8_UNORM)) {
|
||||
Log("[ERROR] Failed to initialize texture");
|
||||
stbi_image_free(pixels);
|
||||
return false;
|
||||
}
|
||||
|
||||
commandList->Close();
|
||||
ID3D12CommandList* lists[] = { commandList };
|
||||
queue.ExecuteCommandListsInternal(1, lists);
|
||||
queue.WaitForIdle();
|
||||
|
||||
texture.SetName(filename);
|
||||
|
||||
srvHeap.Initialize(device, DescriptorHeapType::CBV_SRV_UAV, 1, true);
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = D3D12ResourceView::CreateShaderResourceDesc(Format::R8G8B8A8_UNorm, D3D12_SRV_DIMENSION_TEXTURE2D);
|
||||
srv.InitializeAsShaderResource(device, texture.GetResource(), &srvDesc, &srvHeap, 0);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InitD3D12() {
|
||||
RHIDeviceDesc deviceDesc;
|
||||
RHIDeviceDesc deviceDesc = {};
|
||||
deviceDesc.adapterIndex = 0;
|
||||
deviceDesc.enableDebugLayer = false;
|
||||
deviceDesc.enableGPUValidation = false;
|
||||
@@ -141,131 +439,48 @@ bool InitD3D12() {
|
||||
return false;
|
||||
}
|
||||
|
||||
gDepthStencil.InitializeDepthStencil(device, gWidth, gHeight);
|
||||
if (!gDepthStencil.InitializeDepthStencil(device, gWidth, gHeight)) {
|
||||
Log("[ERROR] Failed to initialize depth stencil");
|
||||
return false;
|
||||
}
|
||||
|
||||
gRTVHeap.Initialize(device, DescriptorHeapType::RTV, 2);
|
||||
if (!gRTVHeap.Initialize(device, DescriptorHeapType::RTV, 2)) {
|
||||
Log("[ERROR] Failed to initialize RTV heap");
|
||||
return false;
|
||||
}
|
||||
gRTVDescriptorSize = gDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType::RTV);
|
||||
|
||||
gDSVHeap.Initialize(device, DescriptorHeapType::DSV, 1);
|
||||
if (!gDSVHeap.Initialize(device, DescriptorHeapType::DSV, 1)) {
|
||||
Log("[ERROR] Failed to initialize DSV heap");
|
||||
return false;
|
||||
}
|
||||
gDSVDescriptorSize = gDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType::DSV);
|
||||
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
D3D12Texture& backBuffer = gSwapChain.GetBackBuffer(i);
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(Format::R8G8B8A8_UNorm, D3D12_RTV_DIMENSION_TEXTURE2D);
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = D3D12ResourceView::CreateRenderTargetDesc(
|
||||
Format::R8G8B8A8_UNorm,
|
||||
D3D12_RTV_DIMENSION_TEXTURE2D);
|
||||
gRTVs[i].InitializeAsRenderTarget(device, backBuffer.GetResource(), &rtvDesc, &gRTVHeap, i);
|
||||
}
|
||||
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(Format::D24_UNorm_S8_UInt, D3D12_DSV_DIMENSION_TEXTURE2D);
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = D3D12ResourceView::CreateDepthStencilDesc(
|
||||
Format::D24_UNorm_S8_UInt,
|
||||
D3D12_DSV_DIMENSION_TEXTURE2D);
|
||||
gDSV.InitializeAsDepthStencil(device, gDepthStencil.GetResource(), &dsvDesc, &gDSVHeap, 0);
|
||||
|
||||
gCommandAllocator.Initialize(device, CommandQueueType::Direct);
|
||||
gCommandList.Initialize(device, CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator());
|
||||
|
||||
if (!gVertexShader.CompileFromFile(L"Res/Shader/quad.hlsl", "MainVS", "vs_5_1")) {
|
||||
Log("[ERROR] Failed to compile vertex shader");
|
||||
return false;
|
||||
}
|
||||
Log("[INFO] Vertex shader compiled, bytecode size: %zu", gVertexShader.GetBytecodeSize());
|
||||
|
||||
if (!gPixelShader.CompileFromFile(L"Res/Shader/quad.hlsl", "MainPS", "ps_5_1")) {
|
||||
Log("[ERROR] Failed to compile pixel shader");
|
||||
return false;
|
||||
}
|
||||
Log("[INFO] Pixel shader compiled, bytecode size: %zu", gPixelShader.GetBytecodeSize());
|
||||
|
||||
D3D12_DESCRIPTOR_RANGE descriptorRange = D3D12RootSignature::CreateDescriptorRange(
|
||||
D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 0, 1, 0);
|
||||
|
||||
D3D12_ROOT_PARAMETER rootParameters[1];
|
||||
rootParameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
|
||||
rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
|
||||
rootParameters[0].DescriptorTable.NumDescriptorRanges = 1;
|
||||
rootParameters[0].DescriptorTable.pDescriptorRanges = &descriptorRange;
|
||||
|
||||
D3D12_STATIC_SAMPLER_DESC samplerDesc = D3D12RootSignature::CreateStaticSampler(
|
||||
0,
|
||||
D3D12RootSignature::CreateSamplerDesc(
|
||||
FilterMode::Linear,
|
||||
TextureAddressMode::Clamp,
|
||||
D3D12_FLOAT32_MAX
|
||||
),
|
||||
ShaderVisibility::Pixel
|
||||
);
|
||||
|
||||
D3D12_ROOT_SIGNATURE_DESC rsDesc = D3D12RootSignature::CreateDesc(
|
||||
rootParameters, 1,
|
||||
&samplerDesc, 1,
|
||||
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT
|
||||
);
|
||||
|
||||
if (!gRootSignature.Initialize(device, rsDesc)) {
|
||||
Log("[ERROR] Failed to initialize root signature");
|
||||
if (!gCommandAllocator.Initialize(device, CommandQueueType::Direct)) {
|
||||
Log("[ERROR] Failed to initialize command allocator");
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_INPUT_ELEMENT_DESC inputElements[] = {
|
||||
D3D12PipelineState::CreateInputElement("POSITION", 0, Format::R32G32B32A32_Float, 0, 0),
|
||||
D3D12PipelineState::CreateInputElement("TEXCOORD", 0, Format::R32G32B32A32_Float, 0, 16),
|
||||
};
|
||||
|
||||
D3D12_SHADER_BYTECODE emptyGs = {};
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
|
||||
psoDesc.pRootSignature = gRootSignature.GetRootSignature();
|
||||
psoDesc.VS = gVertexShader.GetD3D12Bytecode();
|
||||
psoDesc.PS = gPixelShader.GetD3D12Bytecode();
|
||||
psoDesc.GS = emptyGs;
|
||||
psoDesc.InputLayout.NumElements = 2;
|
||||
psoDesc.InputLayout.pInputElementDescs = inputElements;
|
||||
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
psoDesc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
psoDesc.SampleDesc.Count = 1;
|
||||
psoDesc.SampleDesc.Quality = 0;
|
||||
psoDesc.SampleMask = 0xffffffff;
|
||||
psoDesc.NumRenderTargets = 1;
|
||||
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||
psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
|
||||
psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
|
||||
psoDesc.RasterizerState.FrontCounterClockwise = FALSE;
|
||||
psoDesc.RasterizerState.DepthClipEnable = TRUE;
|
||||
psoDesc.DepthStencilState.DepthEnable = FALSE;
|
||||
psoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
|
||||
psoDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
||||
psoDesc.BlendState.RenderTarget[0].BlendEnable = FALSE;
|
||||
psoDesc.BlendState.RenderTarget[0].SrcBlend = D3D12_BLEND_ONE;
|
||||
psoDesc.BlendState.RenderTarget[0].DestBlend = D3D12_BLEND_ZERO;
|
||||
psoDesc.BlendState.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
|
||||
psoDesc.BlendState.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
|
||||
psoDesc.BlendState.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
|
||||
psoDesc.BlendState.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
|
||||
psoDesc.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
|
||||
|
||||
if (!gPipelineState.Initialize(device, psoDesc)) {
|
||||
Log("[ERROR] Failed to initialize pipeline state");
|
||||
if (!gCommandList.Initialize(device, CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator())) {
|
||||
Log("[ERROR] Failed to initialize command list");
|
||||
return false;
|
||||
}
|
||||
|
||||
struct Vertex {
|
||||
float pos[4];
|
||||
float texcoord[4];
|
||||
};
|
||||
|
||||
Vertex vertices[] = {
|
||||
{ { -0.5f, -0.5f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 0.0f } },
|
||||
{ { -0.5f, 0.5f, 0.0f, 1.0f }, { 0.0f, 0.0f, 0.0f, 0.0f } },
|
||||
{ { 0.5f, -0.5f, 0.0f, 1.0f }, { 1.0f, 1.0f, 0.0f, 0.0f } },
|
||||
{ { 0.5f, 0.5f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 0.0f } },
|
||||
};
|
||||
|
||||
if (!gVertexBuffer.InitializeWithData(device, gCommandList.GetCommandList(), vertices, sizeof(vertices), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER)) {
|
||||
Log("[ERROR] Failed to initialize vertex buffer");
|
||||
return false;
|
||||
}
|
||||
gVertexBuffer.SetStride(sizeof(Vertex));
|
||||
gVertexBuffer.SetBufferType(BufferType::Vertex);
|
||||
|
||||
if (!LoadTexture("Res/Image/earth.png", gDiffuseTexture, gDiffuseSRV, device, gSRVHeap, gCommandList.GetCommandList(), gCommandAllocator, gCommandQueue)) {
|
||||
Log("[ERROR] Failed to load texture");
|
||||
if (!InitializeQuadResources()) {
|
||||
Log("[ERROR] Failed to initialize quad resources");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -287,8 +502,7 @@ void BeginRender() {
|
||||
gCurrentRTIndex = gSwapChain.GetCurrentBackBufferIndex();
|
||||
|
||||
D3D12Texture& currentBackBuffer = gSwapChain.GetBackBuffer(gCurrentRTIndex);
|
||||
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(),
|
||||
ResourceStates::Present, ResourceStates::RenderTarget);
|
||||
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(), ResourceStates::Present, ResourceStates::RenderTarget);
|
||||
|
||||
CPUDescriptorHandle rtvCpuHandle = gRTVHeap.GetCPUDescriptorHandle(gCurrentRTIndex);
|
||||
CPUDescriptorHandle dsvCpuHandle = gDSVHeap.GetCPUDescriptorHandle(0);
|
||||
@@ -297,37 +511,59 @@ void BeginRender() {
|
||||
|
||||
gCommandList.SetRenderTargetsHandle(1, &rtvHandle, &dsvHandle);
|
||||
|
||||
Viewport viewport = { 0.0f, 0.0f, (float)gWidth, (float)gHeight, 0.0f, 1.0f };
|
||||
Viewport viewport = { 0.0f, 0.0f, static_cast<float>(gWidth), static_cast<float>(gHeight), 0.0f, 1.0f };
|
||||
Rect scissorRect = { 0, 0, gWidth, gHeight };
|
||||
gCommandList.SetViewport(viewport);
|
||||
gCommandList.SetScissorRect(scissorRect);
|
||||
|
||||
float clearColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||
const float clearColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
|
||||
gCommandList.ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
|
||||
gCommandList.ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
|
||||
gCommandList.ClearDepthStencilView(
|
||||
dsvHandle,
|
||||
D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL,
|
||||
1.0f,
|
||||
0,
|
||||
0,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
void EndRender() {
|
||||
D3D12Texture& currentBackBuffer = gSwapChain.GetBackBuffer(gCurrentRTIndex);
|
||||
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(),
|
||||
ResourceStates::RenderTarget, ResourceStates::Present);
|
||||
gCommandList.TransitionBarrier(currentBackBuffer.GetResource(), ResourceStates::RenderTarget, ResourceStates::Present);
|
||||
}
|
||||
|
||||
void RenderQuad() {
|
||||
RHIDescriptorSet* descriptorSets[] = { gTextureSet, gSamplerSet };
|
||||
RHIResourceView* vertexBuffers[] = { gVertexBufferView };
|
||||
const uint64_t offsets[] = { 0 };
|
||||
const uint32_t strides[] = { sizeof(Vertex) };
|
||||
|
||||
gCommandList.SetPipelineState(gPipelineState);
|
||||
gCommandList.SetGraphicsDescriptorSets(0, 2, descriptorSets, gPipelineLayout);
|
||||
gCommandList.SetPrimitiveTopology(PrimitiveTopology::TriangleList);
|
||||
gCommandList.SetVertexBuffers(0, 1, vertexBuffers, offsets, strides);
|
||||
gCommandList.SetIndexBuffer(gIndexBufferView, 0);
|
||||
gCommandList.DrawIndexed(static_cast<uint32_t>(sizeof(kQuadIndices) / sizeof(kQuadIndices[0])));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
|
||||
Logger::Get().Initialize();
|
||||
Logger::Get().AddSink(std::make_unique<ConsoleLogSink>());
|
||||
Logger::Get().AddSink(std::make_unique<FileLogSink>("quad_test.log"));
|
||||
Logger::Get().SetMinimumLevel(LogLevel::Debug);
|
||||
|
||||
Log("[INFO] D3D12 Quad Test Starting");
|
||||
|
||||
WNDCLASSEX wc = {};
|
||||
wc.cbSize = sizeof(WNDCLASSEX);
|
||||
WNDCLASSEXW wc = {};
|
||||
wc.cbSize = sizeof(WNDCLASSEXW);
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW;
|
||||
wc.lpfnWndProc = WindowProc;
|
||||
wc.hInstance = hInstance;
|
||||
wc.lpszClassName = L"D3D12QuadTest";
|
||||
|
||||
if (!RegisterClassEx(&wc)) {
|
||||
if (!RegisterClassExW(&wc)) {
|
||||
Log("[ERROR] Failed to register window class");
|
||||
return -1;
|
||||
}
|
||||
@@ -335,10 +571,19 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
RECT rect = { 0, 0, gWidth, gHeight };
|
||||
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
|
||||
gHWND = CreateWindowEx(0, L"D3D12QuadTest", L"D3D12 Quad Test",
|
||||
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
rect.right - rect.left, rect.bottom - rect.top,
|
||||
NULL, NULL, hInstance, NULL);
|
||||
gHWND = CreateWindowExW(
|
||||
0,
|
||||
L"D3D12QuadTest",
|
||||
L"D3D12 Quad Test",
|
||||
WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT,
|
||||
CW_USEDEFAULT,
|
||||
rect.right - rect.left,
|
||||
rect.bottom - rect.top,
|
||||
NULL,
|
||||
NULL,
|
||||
hInstance,
|
||||
NULL);
|
||||
|
||||
if (!gHWND) {
|
||||
Log("[ERROR] Failed to create window");
|
||||
@@ -378,31 +623,22 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
gCommandList.Reset();
|
||||
|
||||
BeginRender();
|
||||
|
||||
gCommandList.SetRootSignature(gRootSignature.GetRootSignature());
|
||||
gCommandList.SetPipelineState(gPipelineState.GetPipelineState());
|
||||
|
||||
ID3D12DescriptorHeap* heaps[] = { gSRVHeap.GetDescriptorHeap() };
|
||||
gCommandList.SetDescriptorHeaps(1, heaps);
|
||||
gCommandList.SetGraphicsRootDescriptorTable(0, gSRVHeap.GetGPUDescriptorHandleForHeapStart());
|
||||
gCommandList.SetPrimitiveTopology(PrimitiveTopology::TriangleStrip);
|
||||
gCommandList.SetVertexBuffer(0, gVertexBuffer.GetResource(), 0, gVertexBuffer.GetStride());
|
||||
gCommandList.Draw(4, 1, 0, 0);
|
||||
RenderQuad();
|
||||
|
||||
frameCount++;
|
||||
|
||||
if (frameCount >= targetFrameCount) {
|
||||
Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount);
|
||||
ExecuteCommandList();
|
||||
if (RenderDocCapture::Get().EndCapture()) {
|
||||
Log("[INFO] RenderDoc capture ended");
|
||||
}
|
||||
Log("[INFO] Reached target frame count %d - taking screenshot!", targetFrameCount);
|
||||
WaitForGPU();
|
||||
bool screenshotResult = D3D12Screenshot::Capture(
|
||||
const bool screenshotResult = D3D12Screenshot::Capture(
|
||||
gDevice,
|
||||
gCommandQueue,
|
||||
gSwapChain.GetBackBuffer(gCurrentRTIndex),
|
||||
"quad.ppm"
|
||||
);
|
||||
"quad.ppm");
|
||||
if (screenshotResult) {
|
||||
Log("[INFO] Screenshot saved to quad.ppm");
|
||||
} else {
|
||||
@@ -423,6 +659,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
}
|
||||
}
|
||||
|
||||
ShutdownQuadResources();
|
||||
gCommandList.Shutdown();
|
||||
gCommandAllocator.Shutdown();
|
||||
gSwapChain.Shutdown();
|
||||
@@ -430,7 +667,5 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
|
||||
RenderDocCapture::Get().Shutdown();
|
||||
Logger::Get().Shutdown();
|
||||
|
||||
Log("[INFO] D3D12 Quad Test Finished");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user