diff --git a/GPU/Software/SoftGpu.cpp b/GPU/Software/SoftGpu.cpp index f638b19fcd27..25c5a1af2a31 100644 --- a/GPU/Software/SoftGpu.cpp +++ b/GPU/Software/SoftGpu.cpp @@ -141,7 +141,7 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) { } else { sampler = samplerLinear; } - thin3d->SetSamplerStates(0, 1, &sampler); + thin3d->BindSamplerStates(0, 1, &sampler); thin3d->SetDepthStencilState(depth); thin3d->SetRasterState(rasterNoCull); thin3d->SetScissorRect(0, 0, dstwidth, dstheight); @@ -242,7 +242,8 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) { }; texColor->SetMatrix4x4("WorldViewProj", identity4x4); - thin3d->DrawIndexed(Primitive::TRIANGLE_LIST, texColor, vformat, vdata, idata, 6, 0); + thin3d->BindPipeline(texColor); + thin3d->DrawIndexed(Primitive::TRIANGLE_LIST, vformat, vdata, idata, 6, 0); } void SoftGPU::CopyDisplayToOutput() diff --git a/ext/native/gfx_es2/draw_buffer.cpp b/ext/native/gfx_es2/draw_buffer.cpp index 2a32506805a6..6bb276e6161f 100644 --- a/ext/native/gfx_es2/draw_buffer.cpp +++ b/ext/native/gfx_es2/draw_buffer.cpp @@ -66,7 +66,7 @@ void DrawBuffer::Shutdown() { } void DrawBuffer::Begin(Draw::Pipeline *program, DrawBufferPrimitiveMode dbmode) { - shaderSet_ = program; + pipeline_ = program; count_ = 0; mode_ = dbmode; } @@ -77,7 +77,7 @@ void DrawBuffer::End() { void DrawBuffer::Flush(bool set_blend_state) { using namespace Draw; - if (!shaderSet_) { + if (!pipeline_) { ELOG("No program set!"); return; } @@ -85,14 +85,14 @@ void DrawBuffer::Flush(bool set_blend_state) { if (count_ == 0) return; - shaderSet_->SetMatrix4x4("WorldViewProj", drawMatrix_.getReadPtr()); - + pipeline_->SetMatrix4x4("WorldViewProj", drawMatrix_.getReadPtr()); + t3d_->BindPipeline(pipeline_); if (vbuf_) { vbuf_->SubData((const uint8_t *)verts_, 0, sizeof(Vertex) * count_); int offset = 0; - t3d_->Draw(mode_ == DBMODE_NORMAL ? Primitive::TRIANGLE_LIST : Primitive::LINE_LIST, shaderSet_, vformat_, vbuf_, count_, offset); + t3d_->Draw(mode_ == DBMODE_NORMAL ? Primitive::TRIANGLE_LIST : Primitive::LINE_LIST, vformat_, vbuf_, count_, offset); } else { - t3d_->DrawUP(mode_ == DBMODE_NORMAL ? Primitive::TRIANGLE_LIST : Primitive::LINE_LIST, shaderSet_, vformat_, (const void *)verts_, count_); + t3d_->DrawUP(mode_ == DBMODE_NORMAL ? Primitive::TRIANGLE_LIST : Primitive::LINE_LIST, vformat_, (const void *)verts_, count_); } count_ = 0; } diff --git a/ext/native/gfx_es2/draw_buffer.h b/ext/native/gfx_es2/draw_buffer.h index a2fbf7866d4a..9ceffc5e5802 100644 --- a/ext/native/gfx_es2/draw_buffer.h +++ b/ext/native/gfx_es2/draw_buffer.h @@ -160,7 +160,7 @@ class DrawBuffer { Draw::DrawContext *t3d_; Draw::Buffer *vbuf_; Draw::InputLayout *vformat_; - Draw::Pipeline *shaderSet_; + Draw::Pipeline *pipeline_; Vertex *verts_; int count_; diff --git a/ext/native/thin3d/thin3d.h b/ext/native/thin3d/thin3d.h index afe9318f29c6..ab8279f836e5 100644 --- a/ext/native/thin3d/thin3d.h +++ b/ext/native/thin3d/thin3d.h @@ -470,7 +470,7 @@ class DrawContext : public RefCountedObject { // Bound state objects. Too cumbersome to add them all as parameters to Draw. virtual void SetBlendState(BlendState *state) = 0; - virtual void SetSamplerStates(int start, int count, SamplerState **state) = 0; + virtual void BindSamplerStates(int start, int count, SamplerState **state) = 0; virtual void SetDepthStencilState(DepthStencilState *state) = 0; virtual void SetRasterState(RasterState *state) = 0; @@ -483,10 +483,12 @@ class DrawContext : public RefCountedObject { virtual void SetScissorRect(int left, int top, int width, int height) = 0; virtual void SetViewports(int count, Viewport *viewports) = 0; + virtual void BindPipeline(Pipeline *pipeline) = 0; + // TODO: Add more sophisticated draws with buffer offsets, and multidraws. - virtual void Draw(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) = 0; - virtual void DrawIndexed(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) = 0; - virtual void DrawUP(Primitive prim, Pipeline *pipeline, InputLayout *format, const void *vdata, int vertexCount) = 0; + virtual void Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) = 0; + virtual void DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) = 0; + virtual void DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) = 0; // Render pass management. Default implementations here. virtual void Begin(bool clear, uint32_t colorval, float depthVal, int stencilVal) { diff --git a/ext/native/thin3d/thin3d_d3d9.cpp b/ext/native/thin3d/thin3d_d3d9.cpp index 1509e2f26433..a603a2fb2578 100644 --- a/ext/native/thin3d/thin3d_d3d9.cpp +++ b/ext/native/thin3d/thin3d_d3d9.cpp @@ -470,12 +470,6 @@ class D3D9Context : public DrawContext { D3D9BlendState *bs = static_cast(state); bs->Apply(device_); } - void SetSamplerStates(int start, int count, SamplerState **states) override { - for (int i = 0; i < count; ++i) { - D3D9SamplerState *s = static_cast(states[start + i]); - s->Apply(device_, start + i); - } - } void SetDepthStencilState(DepthStencilState *state) override { Thin3DDX9DepthStencilState *bs = static_cast(state); bs->Apply(device_); @@ -486,14 +480,23 @@ class D3D9Context : public DrawContext { } void BindTextures(int start, int count, Texture **textures) override; + void BindSamplerStates(int start, int count, SamplerState **states) override { + for (int i = 0; i < count; ++i) { + D3D9SamplerState *s = static_cast(states[start + i]); + s->Apply(device_, start + i); + } + } + void BindPipeline(Pipeline *pipeline) { + curPipeline_ = (D3D9Pipeline *)pipeline; + } // Raster state void SetScissorRect(int left, int top, int width, int height) override; void SetViewports(int count, Viewport *viewports) override; - void Draw(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; - void DrawIndexed(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; - void DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) override; + void Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; + void DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; + void DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) override; void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal); std::string GetInfoString(InfoField info) const override { @@ -517,6 +520,7 @@ class D3D9Context : public DrawContext { D3DADAPTER_IDENTIFIER9 identifier_; D3DCAPS9 caps_; char shadeLangVersion_[64]; + D3D9Pipeline *curPipeline_; }; D3D9Context::D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, IDirect3DDevice9 *device, IDirect3DDevice9Ex *deviceEx) @@ -703,35 +707,32 @@ void D3D9Pipeline::Apply(LPDIRECT3DDEVICE9 device) { pshader->Apply(device); } -void D3D9Context::Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { +void D3D9Context::Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { Thin3DDX9Buffer *vbuf = static_cast(vdata); Thin3DDX9VertexFormat *fmt = static_cast(format); - D3D9Pipeline *ss = static_cast(shaderSet); vbuf->BindAsVertexBuf(device_, fmt->GetStride(), offset); - ss->Apply(device_); + curPipeline_->Apply(device_); fmt->Apply(device_); device_->DrawPrimitive(primToD3D9[(int)prim], offset, vertexCount / 3); } -void D3D9Context::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { +void D3D9Context::DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { Thin3DDX9Buffer *vbuf = static_cast(vdata); Thin3DDX9Buffer *ibuf = static_cast(idata); Thin3DDX9VertexFormat *fmt = static_cast(format); - D3D9Pipeline *ss = static_cast(shaderSet); - ss->Apply(device_); + curPipeline_->Apply(device_); fmt->Apply(device_); vbuf->BindAsVertexBuf(device_, fmt->GetStride(), offset); ibuf->BindAsIndexBuf(device_); device_->DrawIndexedPrimitive(primToD3D9[(int)prim], 0, 0, vertexCount, 0, vertexCount / primCountDivisor[(int)prim]); } -void D3D9Context::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) { +void D3D9Context::DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) { Thin3DDX9VertexFormat *fmt = static_cast(format); - D3D9Pipeline *ss = static_cast(shaderSet); - ss->Apply(device_); + curPipeline_->Apply(device_); fmt->Apply(device_); device_->DrawPrimitiveUP(primToD3D9[(int)prim], vertexCount / 3, vdata, fmt->GetStride()); } diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index 304234946737..74a121785577 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -421,7 +421,7 @@ class OpenGLContext : public DrawContext { s->Apply(); } - void SetSamplerStates(int start, int count, SamplerState **states) override { + void BindSamplerStates(int start, int count, SamplerState **states) override { if (samplerStates_.size() < (size_t)(start + count)) { samplerStates_.resize(start + count); } @@ -471,11 +471,14 @@ class OpenGLContext : public DrawContext { } void BindTextures(int start, int count, Texture **textures) override; + void BindPipeline(Pipeline *pipeline) { + curPipeline_ = (OpenGLPipeline *)pipeline; + } // TODO: Add more sophisticated draws. - void Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; - void DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; - void DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) override; + void Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; + void DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; + void DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) override; void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override; std::string GetInfoString(InfoField info) const override { @@ -510,6 +513,7 @@ class OpenGLContext : public DrawContext { } std::vector samplerStates_; + OpenGLPipeline *curPipeline_; }; OpenGLContext::OpenGLContext() { @@ -932,51 +936,48 @@ void OpenGLPipeline::Unapply() { glUseProgram(0); } -void OpenGLContext::Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { - OpenGLPipeline *ss = static_cast(shaderSet); +void OpenGLContext::Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { OpenGLBuffer *vbuf = static_cast(vdata); OpenGLVertexFormat *fmt = static_cast(format); vbuf->Bind(); fmt->Apply(); - ss->Apply(); + curPipeline_->Apply(); glDrawArrays(primToGL[(int)prim], offset, vertexCount); - ss->Unapply(); + curPipeline_->Unapply(); fmt->Unapply(); } -void OpenGLContext::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { - OpenGLPipeline *ss = static_cast(shaderSet); +void OpenGLContext::DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { OpenGLBuffer *vbuf = static_cast(vdata); OpenGLBuffer *ibuf = static_cast(idata); OpenGLVertexFormat *fmt = static_cast(format); vbuf->Bind(); fmt->Apply(); - ss->Apply(); + curPipeline_->Apply(); // Note: ibuf binding is stored in the VAO, so call this after binding the fmt. ibuf->Bind(); glDrawElements(primToGL[(int)prim], vertexCount, GL_UNSIGNED_INT, (const void *)(size_t)offset); - ss->Unapply(); + curPipeline_->Unapply(); fmt->Unapply(); } -void OpenGLContext::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) { - OpenGLPipeline *ss = static_cast(shaderSet); +void OpenGLContext::DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) { OpenGLVertexFormat *fmt = static_cast(format); fmt->Apply(vdata); - ss->Apply(); + curPipeline_->Apply(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glDrawArrays(primToGL[(int)prim], 0, vertexCount); - ss->Unapply(); + curPipeline_->Unapply(); fmt->Unapply(); } diff --git a/ext/native/thin3d/thin3d_vulkan.cpp b/ext/native/thin3d/thin3d_vulkan.cpp index 1c39486ab55c..99f70c34ea38 100644 --- a/ext/native/thin3d/thin3d_vulkan.cpp +++ b/ext/native/thin3d/thin3d_vulkan.cpp @@ -441,14 +441,16 @@ class VKContext : public DrawContext { void SetViewports(int count, Viewport *viewports) override; + void BindSamplerStates(int start, int count, SamplerState **state) override; void BindTextures(int start, int count, Texture **textures) override; - - void SetSamplerStates(int start, int count, SamplerState **state) override; + void BindPipeline(Pipeline *pipeline) override { + curPipeline_ = (VKPipeline *)pipeline; + } // TODO: Add more sophisticated draws. - void Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; - void DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; - void DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) override; + void Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override; + void DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override; + void DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) override; void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override; @@ -584,7 +586,7 @@ RasterState *VKContext::CreateRasterState(const RasterStateDesc &desc) { return new VKRasterState(vulkan_, desc); } -void VKContext::SetSamplerStates(int start, int count, SamplerState **state) { +void VKContext::BindSamplerStates(int start, int count, SamplerState **state) { for (int i = start; i < start + count; i++) { boundSamplers_[i] = (VKSamplerState *)state[i]; } @@ -1102,11 +1104,10 @@ inline VkPrimitiveTopology PrimToVK(Primitive prim) { } } -void VKContext::Draw(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { +void VKContext::Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) { ApplyDynamicState(); curPrim_ = PrimToVK(prim); - curPipeline_ = (VKPipeline *)pipeline; curVertexFormat_ = (VKVertexFormat *)format; Thin3DVKBuffer *vbuf = static_cast(vdata); @@ -1125,11 +1126,10 @@ void VKContext::Draw(Primitive prim, Pipeline *pipeline, InputLayout *format, Bu vkCmdDraw(cmd_, vertexCount, 1, offset, 0); } -void VKContext::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { +void VKContext::DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) { ApplyDynamicState(); curPrim_ = PrimToVK(prim); - curPipeline_ = (VKPipeline *)shaderSet; curVertexFormat_ = (VKVertexFormat *)format; Thin3DVKBuffer *ibuf = static_cast(idata); @@ -1154,11 +1154,10 @@ void VKContext::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *fo vkCmdDrawIndexed(cmd_, vertexCount, 1, 0, offset, 0); } -void VKContext::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) { +void VKContext::DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) { ApplyDynamicState(); curPrim_ = PrimToVK(prim); - curPipeline_ = (VKPipeline *)shaderSet; curVertexFormat_ = (VKVertexFormat *)format; VkBuffer vulkanVbuf, vulkanUBObuf; diff --git a/ext/native/ui/ui_context.cpp b/ext/native/ui/ui_context.cpp index 7c9c3f0b95a5..c7fc6e940019 100644 --- a/ext/native/ui/ui_context.cpp +++ b/ext/native/ui/ui_context.cpp @@ -47,7 +47,7 @@ void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pi void UIContext::Begin() { thin3d_->SetBlendState(blendNormal_); - thin3d_->SetSamplerStates(0, 1, &sampler_); + thin3d_->BindSamplerStates(0, 1, &sampler_); thin3d_->SetDepthStencilState(depth_); thin3d_->SetRasterState(rasterNoCull_); thin3d_->BindTexture(0, uitexture_); @@ -56,7 +56,7 @@ void UIContext::Begin() { void UIContext::BeginNoTex() { thin3d_->SetBlendState(blendNormal_); - thin3d_->SetSamplerStates(0, 1, &sampler_); + thin3d_->BindSamplerStates(0, 1, &sampler_); thin3d_->SetRasterState(rasterNoCull_); UIBegin(ui_pipeline_notex_); }