Skip to content

Commit

Permalink
ImGui: Finish implementing pipeline switching for all backends
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Nov 27, 2024
1 parent 09779e0 commit 6763c13
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 116 deletions.
14 changes: 12 additions & 2 deletions Common/GPU/D3D11/thin3d_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class D3D11DrawContext : public DrawContext {
void DrawIndexed(int indexCount, int offset) override;
void DrawUP(const void *vdata, int vertexCount) override;
void DrawIndexedUP(const void *vdata, int vertexCount, const void *idata, int indexCount) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) override;

void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;

Expand Down Expand Up @@ -1407,7 +1407,13 @@ void D3D11DrawContext::DrawIndexedUP(const void *vdata, int vertexCount, const v
DrawIndexed(indexCount, 0);
}

void D3D11DrawContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) {
void D3D11DrawContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) {
if (draws.is_empty() || !vertexCount || !indexCount) {
return;
}

curPipeline_ = (D3D11Pipeline *)draws[0].pipeline;

int vbyteSize = vertexCount * curPipeline_->input->stride;
int ibyteSize = indexCount * sizeof(u16);

Expand All @@ -1417,12 +1423,14 @@ void D3D11DrawContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCo
UpdateBuffer(upIBuffer_, (const uint8_t *)idata, 0, ibyteSize, Draw::UPDATE_DISCARD);
BindIndexBuffer(upIBuffer_, 0);

UpdateDynamicUniformBuffer(ub, ubSize);
ApplyCurrentState();

for (int i = 0; i < draws.size(); i++) {
if (draws[i].pipeline != curPipeline_) {
curPipeline_ = (D3D11Pipeline *)draws[i].pipeline;
ApplyCurrentState();
UpdateDynamicUniformBuffer(ub, ubSize);
}

if (draws[i].bindTexture) {
Expand All @@ -1432,6 +1440,8 @@ void D3D11DrawContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCo
ID3D11ShaderResourceView *view = ((D3D11Framebuffer *)draws[i].bindFramebufferAsTex)->colorSRView;
context_->PSSetShaderResources(0, 1, &view);
}
ID3D11SamplerState *sstate = ((D3D11SamplerState *)draws[i].samplerState)->ss;
context_->PSSetSamplers(0, 1, &sstate);
D3D11_RECT rc;
rc.left = draws[i].clipx;
rc.top = draws[i].clipy;
Expand Down
10 changes: 8 additions & 2 deletions Common/GPU/D3D9/thin3d_d3d9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class D3D9Context : public DrawContext {
void DrawIndexed(int vertexCount, int offset) override;
void DrawUP(const void *vdata, int vertexCount) override;
void DrawIndexedUP(const void *vdata, int vertexCount, const void *idata, int indexCount) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) override;

void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;

Expand Down Expand Up @@ -1203,10 +1203,16 @@ void D3D9Context::DrawIndexedUP(const void *vdata, int vertexCount, const void *
vdata, curPipeline_->inputLayout->GetStride());
}

void D3D9Context::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) {
void D3D9Context::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) {
if (draws.is_empty() || !vertexCount || !indexCount) {
return;
}

BindPipeline(draws[0].pipeline);
curPipeline_->inputLayout->Apply(device_);
curPipeline_->Apply(device_, stencilRef_, stencilWriteMask_, stencilCompareMask_);
ApplyDynamicState();
UpdateDynamicUniformBuffer(ub, ubSize);

// Suboptimal! Should dirty-track textures.
for (int i = 0; i < draws.size(); i++) {
Expand Down
12 changes: 10 additions & 2 deletions Common/GPU/OpenGL/thin3d_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class OpenGLContext : public DrawContext {
void DrawIndexed(int vertexCount, int offset) override;
void DrawUP(const void *vdata, int vertexCount) override;
void DrawIndexedUP(const void *vdata, int vertexCount, const void *idata, int indexCount) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) override;

void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;

Expand Down Expand Up @@ -1442,7 +1442,14 @@ void OpenGLContext::DrawIndexedUP(const void *vdata, int vertexCount, const void
renderManager_.DrawIndexed(curPipeline_->inputLayout->inputLayout_, vbuf, voffset, ibuf, ioffset, curPipeline_->prim, indexCount, GL_UNSIGNED_SHORT, 1);
}

void OpenGLContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) {
void OpenGLContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) {
if (draws.is_empty() || !vertexCount || !indexCount) {
return;
}

BindPipeline(draws[0].pipeline);
UpdateDynamicUniformBuffer(ub, ubSize);

_assert_(curPipeline_->inputLayout != nullptr);
int stride = curPipeline_->inputLayout->stride;
uint32_t vdataSize = stride * vertexCount;
Expand All @@ -1467,6 +1474,7 @@ void OpenGLContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount
OpenGLPipeline *glPipeline = (OpenGLPipeline *)draw.pipeline;
_dbg_assert_(glPipeline->inputLayout->stride == stride);
BindPipeline(glPipeline); // this updated curPipeline_.
UpdateDynamicUniformBuffer(ub, ubSize);
}
if (draw.bindTexture) {
renderManager_.BindTexture(0, ((OpenGLTexture *)draw.bindTexture)->GetTex());
Expand Down
11 changes: 9 additions & 2 deletions Common/GPU/Vulkan/thin3d_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ class VKContext : public DrawContext {
void DrawUP(const void *vdata, int vertexCount) override;
void DrawIndexedUP(const void *vdata, int vertexCount, const void *idata, int indexCount) override;
// Specialized for quick IMGUI drawing.
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw>) override;
void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw>, const void *dynUniforms, size_t size) override;

void BindCurrentPipeline();
void ApplyDynamicState();
Expand Down Expand Up @@ -1557,13 +1557,15 @@ void VKContext::DrawIndexedUP(const void *vdata, int vertexCount, const void *id
renderManager_.DrawIndexed(descSetIndex, 1, &ubo_offset, vulkanVbuf, (int)vbBindOffset, vulkanIbuf, (int)ibBindOffset, indexCount, 1);
}

void VKContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) {
void VKContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *ub, size_t ubSize) {
_dbg_assert_(vertexCount >= 0);
_dbg_assert_(indexCount >= 0);
if (vertexCount <= 0 || indexCount <= 0 || draws.is_empty()) {
return;
}

curPipeline_ = (VKPipeline *)draws[0].pipeline;

VkBuffer vulkanVbuf, vulkanIbuf, vulkanUBObuf;
size_t vdataSize = vertexCount * curPipeline_->stride;
uint32_t vbBindOffset;
Expand All @@ -1579,6 +1581,8 @@ void VKContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, co
_assert_(idataPtr != nullptr);
memcpy(idataPtr, idata, idataSize);

curPipeline_->SetDynamicUniformData(ub, ubSize);

uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf);

BindCurrentPipeline();
Expand All @@ -1589,13 +1593,16 @@ void VKContext::DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, co
VKPipeline *vkPipe = (VKPipeline *)draw.pipeline;
renderManager_.BindPipeline(vkPipe->pipeline, vkPipe->flags, pipelineLayout_);
curPipeline_ = (VKPipeline *)draw.pipeline;
curPipeline_->SetDynamicUniformData(ub, ubSize);
}
// TODO: Dirty-check these.
if (draw.bindTexture) {
BindTexture(0, draw.bindTexture);
} else if (draw.bindFramebufferAsTex) {
BindFramebufferAsTexture(draw.bindFramebufferAsTex, 0, FBChannel::FB_COLOR_BIT, 0);
}
Draw::SamplerState *sstate = draw.samplerState;
BindSamplerStates(0, 1, &sstate);
int descSetIndex;
PackedDescriptor *descriptors = renderManager_.PushDescriptorSet(4, &descSetIndex);
BindDescriptors(vulkanUBObuf, descriptors);
Expand Down
3 changes: 2 additions & 1 deletion Common/GPU/thin3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ struct ClippedDraw {
s16 cliph;
Draw::Texture *bindTexture;
Draw::Framebuffer *bindFramebufferAsTex;
Draw::SamplerState *samplerState;
Draw::Pipeline *pipeline;
};

Expand Down Expand Up @@ -844,7 +845,7 @@ class DrawContext {
virtual void DrawUP(const void *vdata, int vertexCount) = 0;
virtual void DrawIndexedUP(const void *vdata, int vertexCount, const void *idata, int indexCount) = 0;
// Intended for ImGui display lists, easier to do optimally this way.
virtual void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws) = 0;
virtual void DrawIndexedClippedBatchUP(const void *vdata, int vertexCount, const void *idata, int indexCount, Slice<ClippedDraw> draws, const void *dynUniforms, size_t size) = 0;

// Frame management (for the purposes of sync and resource management, necessary with modern APIs). Default implementations here.
virtual void BeginFrame(DebugFlags debugFlags) = 0;
Expand Down
7 changes: 6 additions & 1 deletion GPU/Common/FramebufferManagerCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3705,10 +3705,15 @@ void FramebufferManagerCommon::DrawImGuiDebug(int &selected) const {
}
ImGui::EndTable();

// Fix out-of-bounds issues when framebuffers are removed.
if (selected >= vfbs_.size()) {
selected = -1;
}

if (selected != -1) {
// Now, draw the image of the selected framebuffer.
Draw::Framebuffer *fb = vfbs_[selected]->fbo;
ImTextureID texId = ImGui_ImplThin3d_AddFBAsTextureTemp(fb, Draw::FB_COLOR_BIT);
ImTextureID texId = ImGui_ImplThin3d_AddFBAsTextureTemp(fb, Draw::FB_COLOR_BIT, ImGuiPipeline::TexturedOpaque);
ImGui::Image(texId, ImVec2(fb->Width(), fb->Height()));
}
}
Loading

0 comments on commit 6763c13

Please sign in to comment.