Skip to content

Commit

Permalink
Make pipelines bound state
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Dec 27, 2016
1 parent 4462a8c commit 166243e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 61 deletions.
5 changes: 3 additions & 2 deletions GPU/Software/SoftGpu.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions ext/native/gfx_es2/draw_buffer.cpp
Expand Up @@ -66,7 +66,7 @@ void DrawBuffer::Shutdown() {
}

void DrawBuffer::Begin(Draw::Pipeline *program, DrawBufferPrimitiveMode dbmode) {
shaderSet_ = program;
pipeline_ = program;
count_ = 0;
mode_ = dbmode;
}
Expand All @@ -77,22 +77,22 @@ void DrawBuffer::End() {

void DrawBuffer::Flush(bool set_blend_state) {
using namespace Draw;
if (!shaderSet_) {
if (!pipeline_) {
ELOG("No program set!");
return;
}

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;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/native/gfx_es2/draw_buffer.h
Expand Up @@ -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_;
Expand Down
10 changes: 6 additions & 4 deletions ext/native/thin3d/thin3d.h
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
37 changes: 19 additions & 18 deletions ext/native/thin3d/thin3d_d3d9.cpp
Expand Up @@ -470,12 +470,6 @@ class D3D9Context : public DrawContext {
D3D9BlendState *bs = static_cast<D3D9BlendState *>(state);
bs->Apply(device_);
}
void SetSamplerStates(int start, int count, SamplerState **states) override {
for (int i = 0; i < count; ++i) {
D3D9SamplerState *s = static_cast<D3D9SamplerState *>(states[start + i]);
s->Apply(device_, start + i);
}
}
void SetDepthStencilState(DepthStencilState *state) override {
Thin3DDX9DepthStencilState *bs = static_cast<Thin3DDX9DepthStencilState *>(state);
bs->Apply(device_);
Expand All @@ -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<D3D9SamplerState *>(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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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<Thin3DDX9Buffer *>(vdata);
Thin3DDX9VertexFormat *fmt = static_cast<Thin3DDX9VertexFormat *>(format);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(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<Thin3DDX9Buffer *>(vdata);
Thin3DDX9Buffer *ibuf = static_cast<Thin3DDX9Buffer *>(idata);
Thin3DDX9VertexFormat *fmt = static_cast<Thin3DDX9VertexFormat *>(format);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(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<Thin3DDX9VertexFormat *>(format);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(shaderSet);

ss->Apply(device_);
curPipeline_->Apply(device_);
fmt->Apply(device_);
device_->DrawPrimitiveUP(primToD3D9[(int)prim], vertexCount / 3, vdata, fmt->GetStride());
}
Expand Down
33 changes: 17 additions & 16 deletions ext/native/thin3d/thin3d_gl.cpp
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -510,6 +513,7 @@ class OpenGLContext : public DrawContext {
}

std::vector<OpenGLSamplerState *> samplerStates_;
OpenGLPipeline *curPipeline_;
};

OpenGLContext::OpenGLContext() {
Expand Down Expand Up @@ -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<OpenGLPipeline *>(shaderSet);
void OpenGLContext::Draw(Primitive prim, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(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<OpenGLPipeline *>(shaderSet);
void OpenGLContext::DrawIndexed(Primitive prim, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
OpenGLBuffer *ibuf = static_cast<OpenGLBuffer *>(idata);
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(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<OpenGLPipeline *>(shaderSet);
void OpenGLContext::DrawUP(Primitive prim, InputLayout *format, const void *vdata, int vertexCount) {
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(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();
}

Expand Down
23 changes: 11 additions & 12 deletions ext/native/thin3d/thin3d_vulkan.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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<Thin3DVKBuffer *>(vdata);

Expand All @@ -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<Thin3DVKBuffer *>(idata);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions ext/native/ui/ui_context.cpp
Expand Up @@ -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_);
Expand All @@ -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_);
}
Expand Down

0 comments on commit 166243e

Please sign in to comment.