From 7f76ba83502c31dc79062554418170ad19a2d10e Mon Sep 17 00:00:00 2001 From: Nikita Krupitskas Date: Sat, 27 Jul 2024 20:37:33 +0200 Subject: [PATCH] Enhancing imgui integration --- Yasno/Graphics/EngineStats.hpp | 13 ++ Yasno/Graphics/RenderStats.hpp | 13 -- Yasno/Renderer/D3D12Renderer.cpp | 10 ++ Yasno/Renderer/D3D12Renderer.hpp | 7 + Yasno/System/Gui.cpp | 201 ++++++++++++++++-------- Yasno/System/Gui.hpp | 15 +- Yasno/Techniques/TonemapPostprocess.cpp | 83 ++++++---- Yasno/Techniques/TonemapPostprocess.hpp | 2 +- Yasno/Yasno.vcxproj | 2 +- Yasno/Yasno.vcxproj.filters | 2 +- Yasno/Yasno/Yasno.cpp | 180 ++++++++------------- Yasno/Yasno/Yasno.hpp | 3 - vcpkg.json | 3 +- 13 files changed, 293 insertions(+), 241 deletions(-) create mode 100644 Yasno/Graphics/EngineStats.hpp delete mode 100644 Yasno/Graphics/RenderStats.hpp diff --git a/Yasno/Graphics/EngineStats.hpp b/Yasno/Graphics/EngineStats.hpp new file mode 100644 index 0000000..29c8d99 --- /dev/null +++ b/Yasno/Graphics/EngineStats.hpp @@ -0,0 +1,13 @@ +#pragma once + +namespace engine_stats +{ + static uint32_t fps; + static uint32_t cpu_frame_ms; + static uint32_t gpu_frame_ms; + //uint32_t triangle_count; // draw.indexCount / 3; + //uint32_t drawcall_count; // increase per draw call + + // Frustum culling stats + // Occlusion culling stats +}; diff --git a/Yasno/Graphics/RenderStats.hpp b/Yasno/Graphics/RenderStats.hpp deleted file mode 100644 index ccda313..0000000 --- a/Yasno/Graphics/RenderStats.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include - -struct EngineStats -{ - float frametime; // elapsed time - uint32_t triangle_count; // draw.indexCount / 3; - uint32_t drawcall_count; // increase per draw call - - // Frustum culling stats - // Occlusion culling stats -}; diff --git a/Yasno/Renderer/D3D12Renderer.cpp b/Yasno/Renderer/D3D12Renderer.cpp index fe2ece4..507c913 100644 --- a/Yasno/Renderer/D3D12Renderer.cpp +++ b/Yasno/Renderer/D3D12Renderer.cpp @@ -232,6 +232,16 @@ namespace ysn return m_device; } + DXGI_FORMAT D3D12Renderer::GetHdrFormat() const + { + return hdr_format; + } + + DXGI_FORMAT D3D12Renderer::GetBackBufferFormat() const + { + return backbuffer_format; + } + void D3D12Renderer::FlushQueues() { m_direct_command_queue->Flush(); diff --git a/Yasno/Renderer/D3D12Renderer.hpp b/Yasno/Renderer/D3D12Renderer.hpp index bf0479f..e825e1c 100644 --- a/Yasno/Renderer/D3D12Renderer.hpp +++ b/Yasno/Renderer/D3D12Renderer.hpp @@ -35,6 +35,9 @@ namespace ysn std::shared_ptr GetShaderManager(); + DXGI_FORMAT GetHdrFormat() const; + DXGI_FORMAT GetBackBufferFormat() const; + void FlushQueues(); bool IsTearingSupported(); @@ -53,6 +56,10 @@ namespace ysn // High level objects std::shared_ptr m_shader_manager; + // Formats + DXGI_FORMAT hdr_format = DXGI_FORMAT_R16G16B16A16_FLOAT; + DXGI_FORMAT backbuffer_format = DXGI_FORMAT_R8G8B8A8_UNORM; + // Resources wil::com_ptr m_back_buffers[SWAP_CHAIN_BUFFER_COUNT]; diff --git a/Yasno/System/Gui.cpp b/Yasno/System/Gui.cpp index a57ec9f..6166a3d 100644 --- a/Yasno/System/Gui.cpp +++ b/Yasno/System/Gui.cpp @@ -1,74 +1,137 @@ #include "Gui.hpp" #include +#include "imgui_impl_dx12.h" +#include "imgui_impl_win32.h" -void ysn::SetupColorScheme() +#include +#include +#include + +namespace ysn { - ImVec4* colors = ImGui::GetStyle().Colors; - colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); - colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); - colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); - colors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f); - colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); - colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); - colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); - colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.21f, 0.22f, 0.54f); - colors[ImGuiCol_FrameBgHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.40f); - colors[ImGuiCol_FrameBgActive] = ImVec4(0.18f, 0.18f, 0.18f, 0.67f); - colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); - colors[ImGuiCol_TitleBgActive] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f); - colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); - colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); - colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); - colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); - colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); - colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); - colors[ImGuiCol_CheckMark] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); - colors[ImGuiCol_SliderGrab] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); - colors[ImGuiCol_SliderGrabActive] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); - colors[ImGuiCol_Button] = ImVec4(0.44f, 0.44f, 0.44f, 0.40f); - colors[ImGuiCol_ButtonHovered] = ImVec4(0.46f, 0.47f, 0.48f, 1.00f); - colors[ImGuiCol_ButtonActive] = ImVec4(0.42f, 0.42f, 0.42f, 1.00f); - colors[ImGuiCol_Header] = ImVec4(0.70f, 0.70f, 0.70f, 0.31f); - colors[ImGuiCol_HeaderHovered] = ImVec4(0.70f, 0.70f, 0.70f, 0.80f); - colors[ImGuiCol_HeaderActive] = ImVec4(0.48f, 0.50f, 0.52f, 1.00f); - colors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); - colors[ImGuiCol_SeparatorHovered] = ImVec4(0.72f, 0.72f, 0.72f, 0.78f); - colors[ImGuiCol_SeparatorActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); - colors[ImGuiCol_ResizeGrip] = ImVec4(0.91f, 0.91f, 0.91f, 0.25f); - colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.67f); - colors[ImGuiCol_ResizeGripActive] = ImVec4(0.46f, 0.46f, 0.46f, 0.95f); - colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); - colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); - colors[ImGuiCol_PlotHistogram] = ImVec4(0.73f, 0.60f, 0.15f, 1.00f); - colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); - colors[ImGuiCol_TextSelectedBg] = ImVec4(0.87f, 0.87f, 0.87f, 0.35f); - // colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); - colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); - colors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); - colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); - - ImGuiStyle& style = ImGui::GetStyle(); - style.WindowPadding = ImVec2(8.00f, 8.00f); - style.FramePadding = ImVec2(5.00f, 2.00f); - style.CellPadding = ImVec2(6.00f, 6.00f); - style.ItemSpacing = ImVec2(6.00f, 6.00f); - style.ItemInnerSpacing = ImVec2(6.00f, 6.00f); - style.TouchExtraPadding = ImVec2(0.00f, 0.00f); - style.IndentSpacing = 25; - style.ScrollbarSize = 15; - style.GrabMinSize = 10; - style.WindowBorderSize = 1; - style.ChildBorderSize = 1; - style.PopupBorderSize = 1; - style.FrameBorderSize = 1; - style.TabBorderSize = 1; - style.WindowRounding = 0; - style.ChildRounding = 0; - style.FrameRounding = 0; - style.PopupRounding = 0; - style.ScrollbarRounding = 0; - style.GrabRounding = 0; - style.LogSliderDeadzone = 4; - style.TabRounding = 0; -} + void SetupColorScheme() + { + ImVec4* colors = ImGui::GetStyle().Colors; + colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f); + colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f); + colors[ImGuiCol_WindowBg] = ImVec4(0.06f, 0.06f, 0.06f, 0.94f); + colors[ImGuiCol_ChildBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.00f); + colors[ImGuiCol_PopupBg] = ImVec4(0.08f, 0.08f, 0.08f, 0.94f); + colors[ImGuiCol_Border] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); + colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f); + colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.21f, 0.22f, 0.54f); + colors[ImGuiCol_FrameBgHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.40f); + colors[ImGuiCol_FrameBgActive] = ImVec4(0.18f, 0.18f, 0.18f, 0.67f); + colors[ImGuiCol_TitleBg] = ImVec4(0.04f, 0.04f, 0.04f, 1.00f); + colors[ImGuiCol_TitleBgActive] = ImVec4(0.29f, 0.29f, 0.29f, 1.00f); + colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 0.51f); + colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f); + colors[ImGuiCol_ScrollbarBg] = ImVec4(0.02f, 0.02f, 0.02f, 0.53f); + colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.31f, 0.31f, 0.31f, 1.00f); + colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.41f, 0.41f, 0.41f, 1.00f); + colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); + colors[ImGuiCol_CheckMark] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f); + colors[ImGuiCol_SliderGrab] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); + colors[ImGuiCol_SliderGrabActive] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f); + colors[ImGuiCol_Button] = ImVec4(0.44f, 0.44f, 0.44f, 0.40f); + colors[ImGuiCol_ButtonHovered] = ImVec4(0.46f, 0.47f, 0.48f, 1.00f); + colors[ImGuiCol_ButtonActive] = ImVec4(0.42f, 0.42f, 0.42f, 1.00f); + colors[ImGuiCol_Header] = ImVec4(0.70f, 0.70f, 0.70f, 0.31f); + colors[ImGuiCol_HeaderHovered] = ImVec4(0.70f, 0.70f, 0.70f, 0.80f); + colors[ImGuiCol_HeaderActive] = ImVec4(0.48f, 0.50f, 0.52f, 1.00f); + colors[ImGuiCol_Separator] = ImVec4(0.43f, 0.43f, 0.50f, 0.50f); + colors[ImGuiCol_SeparatorHovered] = ImVec4(0.72f, 0.72f, 0.72f, 0.78f); + colors[ImGuiCol_SeparatorActive] = ImVec4(0.51f, 0.51f, 0.51f, 1.00f); + colors[ImGuiCol_ResizeGrip] = ImVec4(0.91f, 0.91f, 0.91f, 0.25f); + colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.81f, 0.81f, 0.81f, 0.67f); + colors[ImGuiCol_ResizeGripActive] = ImVec4(0.46f, 0.46f, 0.46f, 0.95f); + colors[ImGuiCol_PlotLines] = ImVec4(0.61f, 0.61f, 0.61f, 1.00f); + colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f); + colors[ImGuiCol_PlotHistogram] = ImVec4(0.73f, 0.60f, 0.15f, 1.00f); + colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f); + colors[ImGuiCol_TextSelectedBg] = ImVec4(0.87f, 0.87f, 0.87f, 0.35f); + // colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); + colors[ImGuiCol_DragDropTarget] = ImVec4(1.00f, 1.00f, 0.00f, 0.90f); + colors[ImGuiCol_NavHighlight] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f); + colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 1.00f, 1.00f, 0.70f); + + ImGuiStyle& style = ImGui::GetStyle(); + style.WindowPadding = ImVec2(8.00f, 8.00f); + style.FramePadding = ImVec2(5.00f, 2.00f); + style.CellPadding = ImVec2(6.00f, 6.00f); + style.ItemSpacing = ImVec2(6.00f, 6.00f); + style.ItemInnerSpacing = ImVec2(6.00f, 6.00f); + style.TouchExtraPadding = ImVec2(0.00f, 0.00f); + style.IndentSpacing = 25; + style.ScrollbarSize = 15; + style.GrabMinSize = 10; + style.WindowBorderSize = 1; + style.ChildBorderSize = 1; + style.PopupBorderSize = 1; + style.FrameBorderSize = 1; + style.TabBorderSize = 1; + style.WindowRounding = 0; + style.ChildRounding = 0; + style.FrameRounding = 0; + style.PopupRounding = 0; + style.ScrollbarRounding = 0; + style.GrabRounding = 0; + style.LogSliderDeadzone = 4; + style.TabRounding = 0; + } + + void InitializeImgui(std::shared_ptr window, std::shared_ptr renderer) + { + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; + + // io.IniFilename = nullptr; + io.LogFilename = nullptr; + + ysn::SetupColorScheme(); + + ImGui_ImplWin32_Init(window->GetWindowHandle()); + + auto imgui_srv_descriptor = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle(); + + ImGui_ImplDX12_Init( + renderer->GetDevice().get(), + window->BufferCount, + renderer->GetBackBufferFormat(), + renderer->GetCbvSrvUavDescriptorHeap()->GetHeapPtr(), + imgui_srv_descriptor.cpu, + imgui_srv_descriptor.gpu); + } + + void ShutdownImgui() + { + ImGui_ImplDX12_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); + } + + void ImguiPrepareNewFrame() + { + ImGui_ImplDX12_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); + } + + void ImguiRenderFrame(wil::com_ptr command_list) + { + GpuMarker imgui_marker(command_list, "Imgui"); + + + ImGui::Render(); + ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), command_list.get()); + + // PIXEndEvent(command_list.get()); + + imgui_marker.EndEvent(); + } +} \ No newline at end of file diff --git a/Yasno/System/Gui.hpp b/Yasno/System/Gui.hpp index 046a4d4..a36e927 100644 --- a/Yasno/System/Gui.hpp +++ b/Yasno/System/Gui.hpp @@ -1,10 +1,17 @@ #pragma once -#include "imgui.h" -#include "imgui_impl_dx12.h" -#include "imgui_impl_win32.h" +#include +#include + +#include namespace ysn { - void SetupColorScheme(); + class Window; + class D3D12Renderer; + + void InitializeImgui(std::shared_ptr window, std::shared_ptr renderer); + void ShutdownImgui(); + void ImguiPrepareNewFrame(); + void ImguiRenderFrame(wil::com_ptr command_list); } diff --git a/Yasno/Techniques/TonemapPostprocess.cpp b/Yasno/Techniques/TonemapPostprocess.cpp index 50faac5..dd0b062 100644 --- a/Yasno/Techniques/TonemapPostprocess.cpp +++ b/Yasno/Techniques/TonemapPostprocess.cpp @@ -1,12 +1,13 @@ #include "TonemapPostprocess.hpp" -// #include - #include #include #include #include #include +#include +#include +#include namespace ysn { @@ -53,54 +54,68 @@ namespace ysn parameters_buffer->Unmap(0, nullptr); } - void TonemapPostprocess::Initialize() + bool TonemapPostprocess::Initialize() { - CreateParameters(parameters_buffer); + CD3DX12_DESCRIPTOR_RANGE hdrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); + CD3DX12_DESCRIPTOR_RANGE ldrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); + + CD3DX12_ROOT_PARAMETER rootParameters[3] = {}; + rootParameters[0].InitAsDescriptorTable(1, &hdrTextureUAV); + rootParameters[1].InitAsDescriptorTable(1, &ldrTextureUAV); + rootParameters[2].InitAsConstantBufferView(0); - // // Tonemap pass data - //{ - // // TODO: Volatile??? - // CD3DX12_DESCRIPTOR_RANGE hdrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); - // CD3DX12_DESCRIPTOR_RANGE ldrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); + // Setting Root Signature + D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {}; + root_signature_desc.NumParameters = 3; + root_signature_desc.pParameters = &rootParameters[0]; - // CD3DX12_ROOT_PARAMETER rootParameters[2]; - // rootParameters[0].InitAsDescriptorTable(1, &hdrTextureUAV); - // rootParameters[1].InitAsDescriptorTable(1, &ldrTextureUAV); + Application::Get().GetRenderer()->CreateRootSignature(&root_signature_desc, &root_signature); - // // Setting Root Signature - // D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {}; - // root_signature_desc.NumParameters = 2; - // root_signature_desc.pParameters = &rootParameters[0]; + ShaderCompileParameters tonemap_shader_parameters; + tonemap_shader_parameters.shader_type = ShaderType::Compute; + tonemap_shader_parameters.shader_path = GetVirtualFilesystemPath(L"Shaders/Tonemap.hlsl"); - // renderer->CreateRootSignature(&root_signature_desc, &tonemap_pass.root_signature); + const auto tonemap_shader = Application::Get().GetRenderer()->GetShaderManager()->CompileShader(&tonemap_shader_parameters); - // wil::com_ptr computeShaderBlob; - // ThrowIfFailed(D3DReadFileToBlob(L"shaders/bin/Tonemap.cso", &computeShaderBlob)); + if (!tonemap_shader.has_value()) + { + LOG(Severity::error) << "Can't load tonemap shader\n"; + return false; + } - // // Create the Compute pipeline state object - // struct PipelineStateStream - // { - // CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature; - // CD3DX12_PIPELINE_STATE_STREAM_CS CS; - // } pipelineStateStream; + // Create the Compute pipeline state object + struct PipelineStateStream + { + CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature; + CD3DX12_PIPELINE_STATE_STREAM_CS CS; + } pipelineStateStream; - // // Setting the root signature and the compute shader to the PSO - // pipelineStateStream.pRootSignature = tonemap_pass.root_signature.get(); - // pipelineStateStream.CS = CD3DX12_SHADER_BYTECODE(computeShaderBlob.get()); + // Setting the root signature and the compute shader to the PSO + pipelineStateStream.pRootSignature = root_signature.get(); + pipelineStateStream.CS = { tonemap_shader.value()->GetBufferPointer(), tonemap_shader.value()->GetBufferSize() }; - // D3D12_PIPELINE_STATE_STREAM_DESC pipelineStateStreamDesc = { - // sizeof(PipelineStateStream), &pipelineStateStream - // }; + D3D12_PIPELINE_STATE_STREAM_DESC pipelineStateStreamDesc = { sizeof(PipelineStateStream), &pipelineStateStream }; - // ThrowIfFailed(device->CreatePipelineState(&pipelineStateStreamDesc, IID_PPV_ARGS(&tonemap_pass.pipeline_state))); - //} + if (auto result = Application::Get().GetDevice()->CreatePipelineState(&pipelineStateStreamDesc, IID_PPV_ARGS(&pipeline_state)); result != S_OK) + { + LOG(Severity::error) << "Can't create tonemap pipeline state\n"; + return false; + } + + if(!CreateParameters(parameters_buffer)) + { + LOG(Severity::error) << "Can't create tonemap parameters\n"; + return false; + } + + return true; } void TonemapPostprocess::Render(TonemapPostprocessParameters* parameters) { UpdateParameters(parameters_buffer, parameters->backbuffer_width, parameters->backbuffer_height); - wil::com_ptr command_list = parameters->command_queue->GetCommandList(); + auto command_list = parameters->command_queue->GetCommandList(); GpuMarker tonemap_marker(command_list, "Tonemap"); diff --git a/Yasno/Techniques/TonemapPostprocess.hpp b/Yasno/Techniques/TonemapPostprocess.hpp index f09edb9..9154ca2 100644 --- a/Yasno/Techniques/TonemapPostprocess.hpp +++ b/Yasno/Techniques/TonemapPostprocess.hpp @@ -33,7 +33,7 @@ namespace ysn struct TonemapPostprocess { - void Initialize(); + bool Initialize(); void Render(TonemapPostprocessParameters* parameters); wil::com_ptr parameters_buffer; diff --git a/Yasno/Yasno.vcxproj b/Yasno/Yasno.vcxproj index 10d263c..e3d1ab1 100644 --- a/Yasno/Yasno.vcxproj +++ b/Yasno/Yasno.vcxproj @@ -296,7 +296,7 @@ - + diff --git a/Yasno/Yasno.vcxproj.filters b/Yasno/Yasno.vcxproj.filters index 06f5c29..bc66d99 100644 --- a/Yasno/Yasno.vcxproj.filters +++ b/Yasno/Yasno.vcxproj.filters @@ -271,7 +271,7 @@ Source\Graphics - + Source\Graphics diff --git a/Yasno/Yasno/Yasno.cpp b/Yasno/Yasno/Yasno.cpp index 4326f2f..bb8e139 100644 --- a/Yasno/Yasno/Yasno.cpp +++ b/Yasno/Yasno/Yasno.cpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ysn { @@ -36,9 +37,7 @@ namespace ysn { Game::Destroy(); - ImGui_ImplDX12_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); + ShutdownImgui(); } void Yasno::UpdateBufferResource( @@ -132,88 +131,12 @@ namespace ysn wil::com_ptr commandList = command_queue->GetCommandList(); - // TODO(Imgui) - { - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; - io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; - io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; - - // io.IniFilename = nullptr; - io.LogFilename = nullptr; - } - tonemap_pass.Initialize(); - // Tonemap pass data - { - // TODO: Volatile??? - CD3DX12_DESCRIPTOR_RANGE hdrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); - CD3DX12_DESCRIPTOR_RANGE ldrTextureUAV(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 1, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); - //CD3DX12_DESCRIPTOR_RANGE parametersCBV(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_NONE); - - CD3DX12_ROOT_PARAMETER rootParameters[3] = {}; - rootParameters[0].InitAsDescriptorTable(1, &hdrTextureUAV); - rootParameters[1].InitAsDescriptorTable(1, &ldrTextureUAV); - rootParameters[2].InitAsConstantBufferView(0); - - // Setting Root Signature - D3D12_ROOT_SIGNATURE_DESC root_signature_desc = {}; - root_signature_desc.NumParameters = 3; - root_signature_desc.pParameters = &rootParameters[0]; - - renderer->CreateRootSignature(&root_signature_desc, &tonemap_pass.root_signature); - - ShaderCompileParameters tonemap_shader_parameters; - tonemap_shader_parameters.shader_type = ShaderType::Compute; - tonemap_shader_parameters.shader_path = GetVirtualFilesystemPath(L"Shaders/Tonemap.hlsl"); - - const auto tonemap_shader = renderer->GetShaderManager()->CompileShader(&tonemap_shader_parameters); - - if (!tonemap_shader.has_value()) - { - LOG(Severity::error) << "Can't load tonemap shader\n"; - return false; - } - - // Create the Compute pipeline state object - struct PipelineStateStream - { - CD3DX12_PIPELINE_STATE_STREAM_ROOT_SIGNATURE pRootSignature; - CD3DX12_PIPELINE_STATE_STREAM_CS CS; - } pipelineStateStream; - - // Setting the root signature and the compute shader to the PSO - pipelineStateStream.pRootSignature = tonemap_pass.root_signature.get(); - pipelineStateStream.CS = { tonemap_shader.value()->GetBufferPointer(), tonemap_shader.value()->GetBufferSize() }; - - D3D12_PIPELINE_STATE_STREAM_DESC pipelineStateStreamDesc = { sizeof(PipelineStateStream), &pipelineStateStream }; - - ThrowIfFailed(device->CreatePipelineState(&pipelineStateStreamDesc, IID_PPV_ARGS(&tonemap_pass.pipeline_state))); - } - - // Create imgui - { - ysn::SetupColorScheme(); - - ImGui_ImplWin32_Init(m_pWindow->GetWindowHandle()); - - auto imgui_srv_descriptor = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle(); - - ImGui_ImplDX12_Init( - device.get(), - m_pWindow->BufferCount, - backbuffer_format, - renderer->GetCbvSrvUavDescriptorHeap()->GetHeapPtr(), - imgui_srv_descriptor.cpu, - imgui_srv_descriptor.gpu); - } + InitializeImgui(m_pWindow, renderer); const auto enviroment_hdr_descriptor_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle(); - environment_texture = - LoadHDRTextureFromFile("assets/HDRI/tears_of_steel_bridge_4k.hdr", commandList, device, enviroment_hdr_descriptor_handle); + environment_texture = LoadHDRTextureFromFile("assets/HDRI/tears_of_steel_bridge_4k.hdr", commandList, device, enviroment_hdr_descriptor_handle); m_hdr_uav_descriptor_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle(); m_backbuffer_uav_descriptor_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle(); @@ -230,8 +153,6 @@ namespace ysn auto fenceValue = command_queue->ExecuteCommandList(commandList); command_queue->WaitForFenceValue(fenceValue); - // PIXEndCapture(false); - m_ContentLoaded = true; // Resize/Create the depth buffer. @@ -317,7 +238,7 @@ namespace ysn auto renderer = Application::Get().GetRenderer(); D3D12_CLEAR_VALUE optimizedClearValue = {}; - optimizedClearValue.Format = hdr_format; + optimizedClearValue.Format = Application::Get().GetRenderer()->GetHdrFormat(); optimizedClearValue.Color[0] = 0.0f; optimizedClearValue.Color[1] = 0.0f; optimizedClearValue.Color[2] = 0.0f; @@ -326,7 +247,7 @@ namespace ysn const auto defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); const auto resourceDesc = CD3DX12_RESOURCE_DESC::Tex2D( - hdr_format, width, height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); + Application::Get().GetRenderer()->GetHdrFormat(), width, height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); ThrowIfFailed(device->CreateCommittedResource( &defaultHeapProperties, @@ -341,7 +262,7 @@ namespace ysn #endif D3D12_RENDER_TARGET_VIEW_DESC rtv = {}; - rtv.Format = hdr_format; + rtv.Format = Application::Get().GetRenderer()->GetHdrFormat(); rtv.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D; rtv.Texture2D.MipSlice = 0; @@ -351,7 +272,7 @@ namespace ysn { D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {}; - uavDesc.Format = hdr_format; + uavDesc.Format = Application::Get().GetRenderer()->GetHdrFormat(); uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; device->CreateUnorderedAccessView(m_scene_color_buffer.get(), nullptr, &uavDesc, m_hdr_uav_descriptor_handle.cpu); @@ -372,7 +293,7 @@ namespace ysn auto device = Application::Get().GetDevice(); D3D12_CLEAR_VALUE optimizedClearValue = {}; - optimizedClearValue.Format = backbuffer_format; + optimizedClearValue.Format = Application::Get().GetRenderer()->GetBackBufferFormat(); optimizedClearValue.Color[0] = 0.0f; optimizedClearValue.Color[1] = 0.0f; optimizedClearValue.Color[2] = 0.0f; @@ -381,7 +302,7 @@ namespace ysn const auto defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); const auto resourceDesc = CD3DX12_RESOURCE_DESC::Tex2D( - backbuffer_format, width, height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); + Application::Get().GetRenderer()->GetBackBufferFormat(), width, height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); ThrowIfFailed(device->CreateCommittedResource( &defaultHeapProperties, @@ -397,7 +318,7 @@ namespace ysn { D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {}; - uavDesc.Format = backbuffer_format; + uavDesc.Format = Application::Get().GetRenderer()->GetBackBufferFormat(); uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D; device->CreateUnorderedAccessView(m_back_buffer.get(), nullptr, &uavDesc, m_backbuffer_uav_descriptor_handle.cpu); @@ -431,7 +352,7 @@ namespace ysn if (totalTime > 1.0) { - //double fps = frameCount / totalTime; + engine_stats::fps = uint32_t(frameCount / totalTime); frameCount = 0; totalTime = 0.0; @@ -500,20 +421,60 @@ namespace ysn void Yasno::RenderUi() { - ImGui::Begin("Controls"); + if (ImGui::BeginMainMenuBar()) + { + if (ImGui::BeginMenu("File")) + { + if (ImGui::MenuItem("Exit", "Esc")) + { + Application::Get().Quit(0); + } + ImGui::EndMenu(); + } + ImGui::EndMainMenuBar(); + } - // Lighting - // Direction - // Color - // Shadows - // Enable - // Tonemapping - // ACES - // Reinhard + { + ImGui::Begin("Controls"); - ImGui::Button("Hello!"); + if (ImGui::CollapsingHeader("Lighting"), ImGuiTreeNodeFlags_DefaultOpen) + { + static float col[3] = { 1.0f, 1.0f, 1.0f }; + ImGui::ColorEdit3("Color", col); + + float intensity = 0; + ImGui::SliderFloat("Intensity", &intensity, 0.0f, 1000.0f); + } + + if (ImGui::CollapsingHeader("Shadows"), ImGuiTreeNodeFlags_DefaultOpen) + { + bool shadows_enabled = false; + ImGui::Checkbox("Enabled", &shadows_enabled); + } + + if (ImGui::CollapsingHeader("Tonemapping"), ImGuiTreeNodeFlags_DefaultOpen) + { + const char* items[] = { "Reinhard", "ACES", }; + static int item_current = 0; + ImGui::Combo("Tonemapper", &item_current, items, IM_ARRAYSIZE(items)); + } - ImGui::End(); + if (ImGui::CollapsingHeader("System"), ImGuiTreeNodeFlags_DefaultOpen) + { + bool vsync_enabled = false; + ImGui::Checkbox("Vsync", &vsync_enabled); + } + + ImGui::End(); + } + + { + ImGui::Begin("Stats"); + + ImGui::Text(std::format("FPS: {}", engine_stats::fps).c_str()); + + ImGui::End(); + } } void Yasno::OnRender(RenderEventArgs& e) @@ -522,10 +483,7 @@ namespace ysn // Render imgui { - ImGui_ImplDX12_NewFrame(); - ImGui_ImplWin32_NewFrame(); - ImGui::NewFrame(); - + ImguiPrepareNewFrame(); RenderUi(); } @@ -680,16 +638,11 @@ namespace ysn { auto command_list = command_queue->GetCommandList(); - // PIXBeginEvent(command_list.get(), PIX_COLOR_DEFAULT, "Imgui"); - ID3D12DescriptorHeap* ppHeaps[] = { Application::Get().GetRenderer()->GetCbvSrvUavDescriptorHeap()->GetHeapPtr() }; command_list->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps); command_list->OMSetRenderTargets(1, &backbuffer_handle, FALSE, &m_depth_dsv_descriptor_handle.cpu); - ImGui::Render(); - ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), command_list.get()); - - // PIXEndEvent(command_list.get()); + ImguiRenderFrame(command_list); command_queue->ExecuteCommandList(command_list); } @@ -698,8 +651,7 @@ namespace ysn { auto command_list = command_queue->GetCommandList(); - CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition( - current_back_buffer.get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT); + CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(current_back_buffer.get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT); command_list->ResourceBarrier(1, &barrier); m_FenceValues[currentBackBufferIndex] = command_queue->ExecuteCommandList(command_list); diff --git a/Yasno/Yasno/Yasno.hpp b/Yasno/Yasno/Yasno.hpp index a8053c0..2b020e6 100644 --- a/Yasno/Yasno/Yasno.hpp +++ b/Yasno/Yasno/Yasno.hpp @@ -86,9 +86,6 @@ namespace ysn bool m_ContentLoaded; bool m_IsFirstFrame = true; - DXGI_FORMAT hdr_format = DXGI_FORMAT_R16G16B16A16_FLOAT; - DXGI_FORMAT backbuffer_format = DXGI_FORMAT_R8G8B8A8_UNORM; - // Techniques CascadedShadowMaps shadow_pass; TonemapPostprocess tonemap_pass; diff --git a/vcpkg.json b/vcpkg.json index c508728..8afb8fb 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -14,6 +14,7 @@ "stb", "imguizmo", "wil", - "aixlog" + "aixlog", + "tracy" ] }