From 0c4205acbb3526b9f10f8c0adf8f8bdbbadd95e3 Mon Sep 17 00:00:00 2001 From: Isuru Samarathunga Date: Sat, 13 Sep 2025 23:33:26 +0530 Subject: [PATCH] Depth Buffer --- Src/IAESandbox/imp/cpp/Game.cpp | 7 +++-- .../imp/cpp/Rendering/Pipeline/UnlitMesh.cpp | 14 +++++++++ Src/IAEngine/imp/cpp/Rendering/Renderer.cpp | 30 +++++++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Src/IAESandbox/imp/cpp/Game.cpp b/Src/IAESandbox/imp/cpp/Game.cpp index 8a9e344..1e8c0c2 100644 --- a/Src/IAESandbox/imp/cpp/Game.cpp +++ b/Src/IAESandbox/imp/cpp/Game.cpp @@ -9,6 +9,7 @@ namespace ia::iae::game RefPtr scene; Texture g_tex; + Texture g_tex2; VOID Game::Initialize() { @@ -17,6 +18,7 @@ namespace ia::iae::game const auto d = File::ReadToVector("Graphics/1.jpg"); g_tex = Engine::CreateTexture(d.data(), d.size()); + g_tex2 = Engine::CreateTexture(d.data(), d.size()); } VOID Game::Terminate() @@ -25,7 +27,8 @@ namespace ia::iae::game VOID Game::Update() { - g_tex.Draw({200.0f, 150.0f, 0.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f}); - iae::Renderer::GetCamera()->Position().x += 0.1f; + g_tex.Draw({200.0f, 150.0f, 10.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 1.0f, 1.0f, 1.0f}); + g_tex2.Draw({300.0f, 150.0f, 15.0f}, {1.0f, 1.0f, 1.0f}, 0.0f, false, false, {1.0f, 0.0f, 1.0f, 1.0f}); + //iae::Renderer::GetCamera()->Position().x += 0.1f; } } \ No newline at end of file diff --git a/Src/IAEngine/imp/cpp/Rendering/Pipeline/UnlitMesh.cpp b/Src/IAEngine/imp/cpp/Rendering/Pipeline/UnlitMesh.cpp index ebb8d87..d062d79 100644 --- a/Src/IAEngine/imp/cpp/Rendering/Pipeline/UnlitMesh.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/Pipeline/UnlitMesh.cpp @@ -62,10 +62,24 @@ namespace ia::iae .num_vertex_attributes = sizeof(vertexAttributes) / sizeof(vertexAttributes[0])}, .primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .rasterizer_state = SDL_GPURasterizerState{ + .fill_mode = SDL_GPU_FILLMODE_FILL, + .cull_mode = SDL_GPU_CULLMODE_NONE, + .front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE + }, + .depth_stencil_state = SDL_GPUDepthStencilState{ + .compare_op = SDL_GPU_COMPAREOP_LESS, + .write_mask = 0xFF, + .enable_depth_test = true, + .enable_depth_write = true, + .enable_stencil_test = false + }, .target_info = { .color_target_descriptions = colorTargets, .num_color_targets = sizeof(colorTargets) / sizeof(colorTargets[0]), + .depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM, + .has_depth_stencil_target = true }, }; diff --git a/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp index fabde9f..904d23a 100644 --- a/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp +++ b/Src/IAEngine/imp/cpp/Rendering/Renderer.cpp @@ -41,6 +41,7 @@ namespace ia::iae SDL_GPUCommandBuffer *g_cmdBuffer{}; SDL_GPURenderPass *g_renderPass{}; SDL_GPUTexture *g_swpChainTexture{}; + SDL_GPUTexture *g_depthBufferTexture{}; // ImGUI State ImGuiIO g_imGUIIO{}; @@ -96,13 +97,26 @@ namespace ia::iae if (!GPUBuffer::InitializeStagingBuffer()) return false; + { + SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D, + .format = SDL_GPU_TEXTUREFORMAT_D16_UNORM, + .usage = SDL_GPU_TEXTUREUSAGE_SAMPLER | + SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, + .width = (UINT32)s_width, + .height = (UINT32)s_height, + .layer_count_or_depth = 1, + .num_levels = 1, + .sample_count = SDL_GPU_SAMPLECOUNT_1}; + g_depthBufferTexture = SDL_CreateGPUTexture(g_gpuDevice, &createInfo); + } + GPUTexture::Initialize(); g_pipelineUnlitMesh = Pipeline_UnlitMesh::Create(); QuadMesh::Initialize(); - matProjection = glm::orthoLH(0.0f, (FLOAT32) s_width, (FLOAT32) s_height, 0.0f, -0.1f, 100.0f); + matProjection = glm::orthoLH(0.0f, (FLOAT32) s_width, (FLOAT32) s_height, 0.0f, -1000.0f, 1000.0f); return true; } @@ -119,6 +133,8 @@ namespace ia::iae GPUBuffer::TerminateStagingBuffer(); + SDL_ReleaseGPUTexture(g_gpuDevice, g_depthBufferTexture); + ImGui_ImplSDL3_Shutdown(); ImGui_ImplSDLGPU3_Shutdown(); ImGui::DestroyContext(); @@ -174,7 +190,17 @@ namespace ia::iae colorTargetInfo.load_op = SDL_GPU_LOADOP_CLEAR; colorTargetInfo.store_op = SDL_GPU_STOREOP_STORE; - g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, NULL); + SDL_GPUDepthStencilTargetInfo depthStencilTargetInfo = {0}; + depthStencilTargetInfo.texture = g_depthBufferTexture; + depthStencilTargetInfo.cycle = true; + depthStencilTargetInfo.clear_depth = 1; + depthStencilTargetInfo.clear_stencil = 0; + depthStencilTargetInfo.load_op = SDL_GPU_LOADOP_CLEAR; + depthStencilTargetInfo.store_op = SDL_GPU_STOREOP_STORE; + depthStencilTargetInfo.stencil_load_op = SDL_GPU_LOADOP_CLEAR; + depthStencilTargetInfo.stencil_store_op = SDL_GPU_STOREOP_STORE; + + g_renderPass = SDL_BeginGPURenderPass(g_cmdBuffer, &colorTargetInfo, 1, &depthStencilTargetInfo); g_pipelineUnlitMesh->Bind((Handle) g_renderPass); SDL_PushGPUVertexUniformData(g_cmdBuffer, 0, &matProjection, sizeof(matProjection));