From 09779e0f446f12d4052b0c0a3fd9e1fed97e3619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 26 Nov 2024 19:06:16 +0100 Subject: [PATCH] ImGui drawing: Allow varying the pipeline when binding textures --- Common/GPU/D3D11/thin3d_d3d11.cpp | 5 +++++ Common/GPU/D3D9/thin3d_d3d9.cpp | 9 ++++++++- Common/GPU/OpenGL/thin3d_gl.cpp | 5 +++++ Common/GPU/Vulkan/thin3d_vulkan.cpp | 5 +++++ Common/GPU/thin3d.h | 1 + GPU/Common/FramebufferManagerCommon.cpp | 2 +- UI/ImDebugger/ImDebugger.cpp | 5 +++++ UI/ImDebugger/ImDebugger.h | 17 ++++++++++------- UI/ImDebugger/ImGe.cpp | 11 +++++++++++ ext/imgui/imgui_impl_thin3d.cpp | 23 ++++++++++++++++++++--- ext/imgui/imgui_impl_thin3d.h | 13 ++++++------- 11 files changed, 77 insertions(+), 19 deletions(-) diff --git a/Common/GPU/D3D11/thin3d_d3d11.cpp b/Common/GPU/D3D11/thin3d_d3d11.cpp index 4192f7b3be..1baa6512be 100644 --- a/Common/GPU/D3D11/thin3d_d3d11.cpp +++ b/Common/GPU/D3D11/thin3d_d3d11.cpp @@ -1420,6 +1420,11 @@ void D3D11DrawContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCo ApplyCurrentState(); for (int i = 0; i < draws.size(); i++) { + if (draws[i].pipeline != curPipeline_) { + curPipeline_ = (D3D11Pipeline *)draws[i].pipeline; + ApplyCurrentState(); + } + if (draws[i].bindTexture) { ID3D11ShaderResourceView *view = ((D3D11Texture *)draws[i].bindTexture)->View(); context_->PSSetShaderResources(0, 1, &view); diff --git a/Common/GPU/D3D9/thin3d_d3d9.cpp b/Common/GPU/D3D9/thin3d_d3d9.cpp index 733ec78c2f..f931db5141 100644 --- a/Common/GPU/D3D9/thin3d_d3d9.cpp +++ b/Common/GPU/D3D9/thin3d_d3d9.cpp @@ -1208,11 +1208,18 @@ void D3D9Context::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, curPipeline_->Apply(device_, stencilRef_, stencilWriteMask_, stencilCompareMask_); ApplyDynamicState(); - // Suboptimal! + // Suboptimal! Should dirty-track textures. for (int i = 0; i < draws.size(); i++) { + if (draws[i].pipeline != curPipeline_) { + D3D9Pipeline *d3d9Pipeline = (D3D9Pipeline *)draws[i].pipeline; + d3d9Pipeline->Apply(device_, stencilRef_, stencilWriteMask_, stencilCompareMask_); + curPipeline_ = d3d9Pipeline; + } + if (draws[i].bindTexture) { device_->SetTexture(0, ((D3D9Texture *)draws[i].bindTexture)->TexturePtr()); } else if (draws[i].bindFramebufferAsTex) { + // We ignore aspect in D3D9 :( device_->SetTexture(0, ((D3D9Framebuffer *)draws[i].bindFramebufferAsTex)->tex.Get()); } diff --git a/Common/GPU/OpenGL/thin3d_gl.cpp b/Common/GPU/OpenGL/thin3d_gl.cpp index bfb61dbaf6..bf7bf064ad 100644 --- a/Common/GPU/OpenGL/thin3d_gl.cpp +++ b/Common/GPU/OpenGL/thin3d_gl.cpp @@ -1463,6 +1463,11 @@ void OpenGLContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount ApplySamplers(); for (auto &draw : draws) { + if (draw.pipeline != curPipeline_) { + OpenGLPipeline *glPipeline = (OpenGLPipeline *)draw.pipeline; + _dbg_assert_(glPipeline->inputLayout->stride == stride); + BindPipeline(glPipeline); // this updated curPipeline_. + } if (draw.bindTexture) { renderManager_.BindTexture(0, ((OpenGLTexture *)draw.bindTexture)->GetTex()); } else if (draw.bindFramebufferAsTex) { diff --git a/Common/GPU/Vulkan/thin3d_vulkan.cpp b/Common/GPU/Vulkan/thin3d_vulkan.cpp index c8f9194251..8480dca335 100644 --- a/Common/GPU/Vulkan/thin3d_vulkan.cpp +++ b/Common/GPU/Vulkan/thin3d_vulkan.cpp @@ -1585,6 +1585,11 @@ void VKContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, co ApplyDynamicState(); for (auto &draw : draws) { + if (draw.pipeline != curPipeline_) { + VKPipeline *vkPipe = (VKPipeline *)draw.pipeline; + renderManager_.BindPipeline(vkPipe->pipeline, vkPipe->flags, pipelineLayout_); + curPipeline_ = (VKPipeline *)draw.pipeline; + } // TODO: Dirty-check these. if (draw.bindTexture) { BindTexture(0, draw.bindTexture); diff --git a/Common/GPU/thin3d.h b/Common/GPU/thin3d.h index 3e2076cc8c..d5d00c546f 100644 --- a/Common/GPU/thin3d.h +++ b/Common/GPU/thin3d.h @@ -705,6 +705,7 @@ struct ClippedDraw { s16 cliph; Draw::Texture *bindTexture; Draw::Framebuffer *bindFramebufferAsTex; + Draw::Pipeline *pipeline; }; class DrawContext { diff --git a/GPU/Common/FramebufferManagerCommon.cpp b/GPU/Common/FramebufferManagerCommon.cpp index bc3674a901..bb76796699 100644 --- a/GPU/Common/FramebufferManagerCommon.cpp +++ b/GPU/Common/FramebufferManagerCommon.cpp @@ -3708,7 +3708,7 @@ void FramebufferManagerCommon::DrawImGuiDebug(int &selected) const { if (selected != -1) { // Now, draw the image of the selected framebuffer. Draw::Framebuffer *fb = vfbs_[selected]->fbo; - ImTextureID texId = ImGui_ImplThin3d_AddFBAsTextureTemp(fb); + ImTextureID texId = ImGui_ImplThin3d_AddFBAsTextureTemp(fb, Draw::FB_COLOR_BIT); ImGui::Image(texId, ImVec2(fb->Width(), fb->Height())); } } diff --git a/UI/ImDebugger/ImDebugger.cpp b/UI/ImDebugger/ImDebugger.cpp index 1a245d4ddc..8f43629a90 100644 --- a/UI/ImDebugger/ImDebugger.cpp +++ b/UI/ImDebugger/ImDebugger.cpp @@ -4,6 +4,7 @@ #include "ext/imgui/imgui_internal.h" #include "Common/StringUtils.h" +#include "Common/Data/Format/IniFile.h" #include "Core/Config.h" #include "Core/System.h" #include "Core/RetroAchievements.h" @@ -914,3 +915,7 @@ void ImDisasmWindow::Draw(MIPSDebugInterface *mipsDebug, bool *open, CoreState c } ImGui::End(); } + +void ImDebugger::LoadConfig() { + IniFile ini; +} diff --git a/UI/ImDebugger/ImDebugger.h b/UI/ImDebugger/ImDebugger.h index a1ba864dd9..88db3754d0 100644 --- a/UI/ImDebugger/ImDebugger.h +++ b/UI/ImDebugger/ImDebugger.h @@ -37,6 +37,7 @@ public: void DirtySymbolMap() { symsDirty_ = true; } + private: // We just keep the state directly in the window. Can refactor later. @@ -56,11 +57,6 @@ private: char searchTerm_[64]{}; }; -class ImLuaConsole { -public: - // Stub -}; - struct ImConfig { bool disasmOpen = true; bool demoOpen = false; @@ -73,6 +69,7 @@ struct ImConfig { bool atracOpen = true; bool structViewerOpen = false; bool framebuffersOpen = false; + bool displayOpen = false; bool styleEditorOpen = false; bool filesystemBrowserOpen = false; bool kernelObjectsOpen = false; @@ -96,14 +93,20 @@ struct ImUiCommand { ImUiCmd cmd; }; -struct ImDebugger { +class ImDebugger { +public: ImDebugger(); + void Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebug); +private: + // We use a separate ini file from the main PPSSPP config. + void LoadConfig(); + void SaveConfig(); + RequesterToken reqToken_; ImDisasmWindow disasm_; - ImLuaConsole luaConsole_; ImStructViewer structViewer_; // Open variables. diff --git a/UI/ImDebugger/ImGe.cpp b/UI/ImDebugger/ImGe.cpp index 0945030ca0..b0fa43f198 100644 --- a/UI/ImDebugger/ImGe.cpp +++ b/UI/ImDebugger/ImGe.cpp @@ -2,6 +2,8 @@ #include "UI/ImDebugger/ImDebugger.h" #include "GPU/Common/FramebufferManagerCommon.h" +#include "Core/HLE/sceDisplay.h" + void DrawFramebuffersWindow(ImConfig &cfg, FramebufferManagerCommon *framebufferManager) { if (!ImGui::Begin("Framebuffers", &cfg.framebuffersOpen)) { ImGui::End(); @@ -12,3 +14,12 @@ void DrawFramebuffersWindow(ImConfig &cfg, FramebufferManagerCommon *framebuffer ImGui::End(); } + +void DrawDisplayWindow(ImConfig &cfg) { + if (!ImGui::Begin("Display", &cfg.framebuffersOpen)) { + ImGui::End(); + return; + } + + ImGui::End(); +} diff --git a/ext/imgui/imgui_impl_thin3d.cpp b/ext/imgui/imgui_impl_thin3d.cpp index ac0fc0a694..2398c0d574 100644 --- a/ext/imgui/imgui_impl_thin3d.cpp +++ b/ext/imgui/imgui_impl_thin3d.cpp @@ -16,8 +16,17 @@ struct RegisteredTexture { bool isFramebuffer; union { Draw::Texture *texture; - Draw::Framebuffer *framebuffer; + struct { + Draw::Framebuffer *framebuffer; + Draw::FBChannel aspect; + }; }; + Draw::Pipeline *pipeline; +}; + +enum { + IMGUI_PIPELINE_TEXTURE_BLEND, + IMGUI_PIPELINE_TEXTURE, }; struct BackendData { @@ -94,6 +103,7 @@ void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *d std::vector draws; Draw::Texture *boundTexture; Draw::Framebuffer *boundFBAsTexture; + Draw::Pipeline *boundPipeline = bd->pipeline; // Render command lists for (int n = 0; n < draw_data->CmdListsCount; n++) { @@ -114,6 +124,7 @@ void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *d if (!pcmd->TextureId) { boundTexture = bd->fontImage; boundFBAsTexture = nullptr; + boundPipeline = bd->pipeline; } else { size_t index = (size_t)pcmd->TextureId - TEX_ID_OFFSET; _dbg_assert_(index < bd->tempTextures.size()); @@ -124,7 +135,9 @@ void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *d boundTexture = bd->tempTextures[index].texture; boundFBAsTexture = nullptr; } + boundPipeline = bd->tempTextures[index].pipeline ? bd->tempTextures[index].pipeline : bd->pipeline; } + // Project scissor/clipping rectangles into framebuffer space ImVec2 clip_min((pcmd->ClipRect.x - clip_off.x) * clip_scale.x, (pcmd->ClipRect.y - clip_off.y) * clip_scale.y); ImVec2 clip_max((pcmd->ClipRect.z - clip_off.x) * clip_scale.x, (pcmd->ClipRect.w - clip_off.y) * clip_scale.y); @@ -138,6 +151,7 @@ void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *d continue; Draw::ClippedDraw clippedDraw; + clippedDraw.pipeline = boundPipeline; clippedDraw.bindTexture = boundTexture; clippedDraw.bindFramebufferAsTex = boundFBAsTexture; clippedDraw.clipx = clip_min.x; @@ -310,21 +324,24 @@ void ImGui_ImplThin3d_NewFrame(Draw::DrawContext *draw, Lin::Matrix4x4 drawMatri g_drawMatrix = drawMatrix; } -ImTextureID ImGui_ImplThin3d_AddTextureTemp(Draw::Texture *texture) { +ImTextureID ImGui_ImplThin3d_AddTextureTemp(Draw::Texture *texture, Draw::Pipeline *pipeline) { BackendData* bd = ImGui_ImplThin3d_GetBackendData(); RegisteredTexture tex{ false }; tex.texture = texture; + tex.pipeline = pipeline; bd->tempTextures.push_back(tex); return (ImTextureID)(uint64_t)(TEX_ID_OFFSET + bd->tempTextures.size() - 1); } -ImTextureID ImGui_ImplThin3d_AddFBAsTextureTemp(Draw::Framebuffer *framebuffer) { +ImTextureID ImGui_ImplThin3d_AddFBAsTextureTemp(Draw::Framebuffer *framebuffer, Draw::FBChannel aspect, Draw::Pipeline *pipeline) { BackendData* bd = ImGui_ImplThin3d_GetBackendData(); RegisteredTexture tex{ true }; tex.framebuffer = framebuffer; + tex.aspect = aspect; + tex.pipeline = pipeline; bd->tempTextures.push_back(tex); return (ImTextureID)(uint64_t)(TEX_ID_OFFSET + bd->tempTextures.size() - 1); diff --git a/ext/imgui/imgui_impl_thin3d.h b/ext/imgui/imgui_impl_thin3d.h index 98cad60f64..3d83262894 100644 --- a/ext/imgui/imgui_impl_thin3d.h +++ b/ext/imgui/imgui_impl_thin3d.h @@ -43,19 +43,18 @@ IMGUI_IMPL_API void ImGui_ImplThin3d_DestroyDeviceObjects(); // These register a texture for imgui drawing, but just for the current frame. // Textures are unregistered again in RenderDrawData. This is just simpler. -IMGUI_IMPL_API ImTextureID ImGui_ImplThin3d_AddTextureTemp(Draw::Texture *texture); -IMGUI_IMPL_API ImTextureID ImGui_ImplThin3d_AddFBAsTextureTemp(Draw::Framebuffer *framebuffer); +IMGUI_IMPL_API ImTextureID ImGui_ImplThin3d_AddTextureTemp(Draw::Texture *texture, Draw::Pipeline *pipeline = nullptr); +IMGUI_IMPL_API ImTextureID ImGui_ImplThin3d_AddFBAsTextureTemp(Draw::Framebuffer *framebuffer, Draw::FBChannel aspect = Draw::FB_COLOR_BIT, Draw::Pipeline *pipeline = nullptr); void ImGui_PushFixedFont(); void ImGui_PopFont(); // Helper structure to hold the data needed by one rendering context into one OS window // (Used by example's main.cpp. Used by multi-viewport features. Probably NOT used by your own engine/app.) -struct ImGui_ImplThin3dH_Window -{ - int Width = 0; - int Height = 0; - bool ClearEnable = true; +struct ImGui_ImplThin3dH_Window { + int Width = 0; + int Height = 0; + bool ClearEnable = true; }; #endif // #ifndef IMGUI_DISABLE