Depth Buffer

This commit is contained in:
Isuru Samarathunga
2025-09-13 23:33:26 +05:30
parent c6a8a8a76c
commit 0c4205acbb
3 changed files with 47 additions and 4 deletions

View File

@ -9,6 +9,7 @@ namespace ia::iae::game
RefPtr<iae::Scene> 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;
}
}

View File

@ -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
},
};

View File

@ -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));