Rename ShaderSet to Pipeline. Get rid of scissorEnable setting (scissor is always enabled).

This commit is contained in:
Henrik Rydgård 2016-12-26 11:06:17 +01:00
parent e56f251f74
commit 4462a8cc99
12 changed files with 114 additions and 141 deletions

View File

@ -144,7 +144,7 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) {
thin3d->SetSamplerStates(0, 1, &sampler);
thin3d->SetDepthStencilState(depth);
thin3d->SetRasterState(rasterNoCull);
thin3d->SetScissorEnabled(false);
thin3d->SetScissorRect(0, 0, dstwidth, dstheight);
float u0 = 0.0f;
float u1;
@ -232,7 +232,7 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(int srcwidth, int srcheight) {
idata->SetData((const uint8_t *)indexes, sizeof(indexes));
thin3d->BindTexture(0, fbTex);
ShaderSet *texColor = thin3d->GetShaderSetPreset(SS_TEXTURE_COLOR_2D);
Pipeline *texColor = thin3d->GetShaderSetPreset(SS_TEXTURE_COLOR_2D);
static const float identity4x4[16] = {
1.0f, 0.0f, 0.0f, 0.0f,

View File

@ -65,7 +65,7 @@ void DrawBuffer::Shutdown() {
inited_ = false;
}
void DrawBuffer::Begin(Draw::ShaderSet *program, DrawBufferPrimitiveMode dbmode) {
void DrawBuffer::Begin(Draw::Pipeline *program, DrawBufferPrimitiveMode dbmode) {
shaderSet_ = program;
count_ = 0;
mode_ = dbmode;

View File

@ -43,7 +43,7 @@ enum {
};
namespace Draw {
class ShaderSet;
class Pipeline;
}
enum DrawBufferPrimitiveMode {
@ -63,7 +63,7 @@ public:
DrawBuffer();
~DrawBuffer();
void Begin(Draw::ShaderSet *shaders, DrawBufferPrimitiveMode mode = DBMODE_NORMAL);
void Begin(Draw::Pipeline *shaders, DrawBufferPrimitiveMode mode = DBMODE_NORMAL);
void End();
// TODO: Enforce these. Now Init is autocalled and shutdown not called.
@ -160,7 +160,7 @@ private:
Draw::DrawContext *t3d_;
Draw::Buffer *vbuf_;
Draw::InputLayout *vformat_;
Draw::ShaderSet *shaderSet_;
Draw::Pipeline *shaderSet_;
Vertex *verts_;
int count_;

View File

@ -162,8 +162,8 @@ void DrawContext::CreatePresets() {
fsPresets_[FS_TEXTURE_COLOR_2D] = CreateShaderModule(ShaderStage::FRAGMENT, glsl_fsTexCol, hlslFsTexCol, vulkan_fsTexCol);
fsPresets_[FS_COLOR_2D] = CreateShaderModule(ShaderStage::FRAGMENT, glsl_fsCol, hlslFsCol, vulkan_fsCol);
ssPresets_[SS_TEXTURE_COLOR_2D] = CreateShaderSet({ {vsPresets_[VS_TEXTURE_COLOR_2D], fsPresets_[FS_TEXTURE_COLOR_2D]} });
ssPresets_[SS_COLOR_2D] = CreateShaderSet({{vsPresets_[VS_COLOR_2D], fsPresets_[FS_COLOR_2D]}});
ssPresets_[SS_TEXTURE_COLOR_2D] = CreatePipeline({ {vsPresets_[VS_TEXTURE_COLOR_2D], fsPresets_[FS_TEXTURE_COLOR_2D]} });
ssPresets_[SS_COLOR_2D] = CreatePipeline({{vsPresets_[VS_COLOR_2D], fsPresets_[FS_COLOR_2D]}});
}
DrawContext::~DrawContext() {

View File

@ -359,7 +359,7 @@ public:
virtual ShaderStage GetStage() const = 0;
};
class ShaderSet : public RefCountedObject {
class Pipeline : public RefCountedObject {
public:
// TODO: Use a uniform-buffer based interface instead.
virtual void SetVector(const char *name, float *value, int n) = 0;
@ -435,7 +435,7 @@ struct RasterStateDesc {
Facing facing;
};
struct ShaderSetDesc {
struct PipelineDesc {
std::vector<ShaderModule *> shaders;
};
@ -450,7 +450,7 @@ public:
virtual SamplerState *CreateSamplerState(const SamplerStateDesc &desc) = 0;
virtual RasterState *CreateRasterState(const RasterStateDesc &desc) = 0;
virtual Buffer *CreateBuffer(size_t size, uint32_t usageFlags) = 0;
virtual ShaderSet *CreateShaderSet(const ShaderSetDesc &desc) = 0;
virtual Pipeline *CreatePipeline(const PipelineDesc &desc) = 0;
virtual InputLayout *CreateVertexFormat(const std::vector<VertexComponent> &components, int stride, ShaderModule *vshader) = 0;
virtual Texture *CreateTexture() = 0; // To be later filled in by ->LoadFromFile or similar.
@ -463,7 +463,7 @@ public:
// Note that these DO NOT AddRef so you must not ->Release presets unless you manually AddRef them.
ShaderModule *GetVshaderPreset(VertexShaderPreset preset) { return fsPresets_[preset]; }
ShaderModule *GetFshaderPreset(FragmentShaderPreset preset) { return vsPresets_[preset]; }
ShaderSet *GetShaderSetPreset(ShaderSetPreset preset) { return ssPresets_[preset]; }
Pipeline *GetShaderSetPreset(ShaderSetPreset preset) { return ssPresets_[preset]; }
// The implementation makes the choice of which shader code to use.
virtual ShaderModule *CreateShaderModule(ShaderStage stage, const char *glsl_source, const char *hlsl_source, const char *vulkan_source) = 0;
@ -480,14 +480,13 @@ public:
} // from sampler 0 and upwards
// Raster state
virtual void SetScissorEnabled(bool enable) = 0;
virtual void SetScissorRect(int left, int top, int width, int height) = 0;
virtual void SetViewports(int count, Viewport *viewports) = 0;
// TODO: Add more sophisticated draws with buffer offsets, and multidraws.
virtual void Draw(Primitive prim, ShaderSet *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) = 0;
virtual void DrawIndexed(Primitive prim, ShaderSet *pipeline, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) = 0;
virtual void DrawUP(Primitive prim, ShaderSet *pipeline, InputLayout *format, const void *vdata, int vertexCount) = 0;
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;
// Render pass management. Default implementations here.
virtual void Begin(bool clear, uint32_t colorval, float depthVal, int stencilVal) {
@ -510,7 +509,7 @@ protected:
ShaderModule *vsPresets_[VS_MAX_PRESET];
ShaderModule *fsPresets_[FS_MAX_PRESET];
ShaderSet *ssPresets_[SS_MAX_PRESET];
Pipeline *ssPresets_[SS_MAX_PRESET];
int targetWidth_;
int targetHeight_;

View File

@ -115,6 +115,7 @@ public:
void Apply(LPDIRECT3DDEVICE9 device) {
device->SetRenderState(D3DRS_CULLMODE, cullMode);
device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
}
};
@ -279,9 +280,9 @@ private:
LPD3DXCONSTANTTABLE constantTable_;
};
class D3D9ShaderSet : public ShaderSet {
class D3D9Pipeline : public Pipeline {
public:
D3D9ShaderSet(LPDIRECT3DDEVICE9 device) : device_(device) {}
D3D9Pipeline(LPDIRECT3DDEVICE9 device) : device_(device) {}
D3D9ShaderModule *vshader;
D3D9ShaderModule *pshader;
void Apply(LPDIRECT3DDEVICE9 device);
@ -453,47 +454,46 @@ public:
D3D9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, IDirect3DDevice9 *device, IDirect3DDevice9Ex *deviceEx);
~D3D9Context();
DepthStencilState *CreateDepthStencilState(const DepthStencilStateDesc &desc);
DepthStencilState *CreateDepthStencilState(const DepthStencilStateDesc &desc) override;
BlendState *CreateBlendState(const BlendStateDesc &desc) override;
SamplerState *CreateSamplerState(const SamplerStateDesc &desc) override;
RasterState *CreateRasterState(const RasterStateDesc &desc) override;
Buffer *CreateBuffer(size_t size, uint32_t usageFlags) override;
ShaderSet *CreateShaderSet(const ShaderSetDesc &desc) override;
Pipeline *CreatePipeline(const PipelineDesc &desc) override;
InputLayout *CreateVertexFormat(const std::vector<VertexComponent> &components, int stride, ShaderModule *vshader) override;
Texture *CreateTexture() override;
Texture *CreateTexture(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) override;
ShaderModule *CreateShaderModule(ShaderStage stage, const char *glsl_source, const char *hlsl_source, const char *vulkan_source) override;
// Bound state objects. Too cumbersome to add them all as parameters to Draw.
void SetBlendState(BlendState *state) {
void SetBlendState(BlendState *state) override {
D3D9BlendState *bs = static_cast<D3D9BlendState *>(state);
bs->Apply(device_);
}
void SetSamplerStates(int start, int count, SamplerState **states) {
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) {
void SetDepthStencilState(DepthStencilState *state) override {
Thin3DDX9DepthStencilState *bs = static_cast<Thin3DDX9DepthStencilState *>(state);
bs->Apply(device_);
}
void SetRasterState(RasterState *state) {
void SetRasterState(RasterState *state) override {
D3D9RasterState *bs = static_cast<D3D9RasterState *>(state);
bs->Apply(device_);
}
void BindTextures(int start, int count, Texture **textures);
void BindTextures(int start, int count, Texture **textures) override;
// Raster state
void SetScissorEnabled(bool enable);
void SetScissorRect(int left, int top, int width, int height);
void SetViewports(int count, Viewport *viewports);
void SetScissorRect(int left, int top, int width, int height) override;
void SetViewports(int count, Viewport *viewports) override;
void Draw(Primitive prim, ShaderSet *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override;
void DrawIndexed(Primitive prim, ShaderSet *pipeline, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override;
void DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) 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 Clear(int mask, uint32_t colorval, float depthVal, int stencilVal);
std::string GetInfoString(InfoField info) const override {
@ -543,12 +543,12 @@ ShaderModule *D3D9Context::CreateShaderModule(ShaderStage stage, const char *gls
}
}
ShaderSet *D3D9Context::CreateShaderSet(const ShaderSetDesc &desc) {
Pipeline *D3D9Context::CreatePipeline(const PipelineDesc &desc) {
if (!desc.shaders.size()) {
ELOG("ShaderSet requires at least one shader");
return NULL;
}
D3D9ShaderSet *shaderSet = new D3D9ShaderSet(device_);
D3D9Pipeline *shaderSet = new D3D9Pipeline(device_);
for (auto iter : desc.shaders) {
if (iter->GetStage() == ShaderStage::FRAGMENT) {
shaderSet->pshader = static_cast<D3D9ShaderModule *>(iter);
@ -698,15 +698,15 @@ Buffer *D3D9Context::CreateBuffer(size_t size, uint32_t usageFlags) {
return new Thin3DDX9Buffer(device_, size, usageFlags);
}
void D3D9ShaderSet::Apply(LPDIRECT3DDEVICE9 device) {
void D3D9Pipeline::Apply(LPDIRECT3DDEVICE9 device) {
vshader->Apply(device);
pshader->Apply(device);
}
void D3D9Context::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
void D3D9Context::Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
Thin3DDX9Buffer *vbuf = static_cast<Thin3DDX9Buffer *>(vdata);
Thin3DDX9VertexFormat *fmt = static_cast<Thin3DDX9VertexFormat *>(format);
D3D9ShaderSet *ss = static_cast<D3D9ShaderSet*>(shaderSet);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(shaderSet);
vbuf->BindAsVertexBuf(device_, fmt->GetStride(), offset);
ss->Apply(device_);
@ -714,11 +714,11 @@ void D3D9Context::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format
device_->DrawPrimitive(primToD3D9[(int)prim], offset, vertexCount / 3);
}
void D3D9Context::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
void D3D9Context::DrawIndexed(Primitive prim, Pipeline *shaderSet, 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);
D3D9ShaderSet *ss = static_cast<D3D9ShaderSet*>(shaderSet);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(shaderSet);
ss->Apply(device_);
fmt->Apply(device_);
@ -727,9 +727,9 @@ void D3D9Context::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout
device_->DrawIndexedPrimitive(primToD3D9[(int)prim], 0, 0, vertexCount, 0, vertexCount / primCountDivisor[(int)prim]);
}
void D3D9Context::DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
void D3D9Context::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
Thin3DDX9VertexFormat *fmt = static_cast<Thin3DDX9VertexFormat *>(format);
D3D9ShaderSet *ss = static_cast<D3D9ShaderSet*>(shaderSet);
D3D9Pipeline *ss = static_cast<D3D9Pipeline*>(shaderSet);
ss->Apply(device_);
fmt->Apply(device_);
@ -749,10 +749,6 @@ void D3D9Context::Clear(int mask, uint32_t colorval, float depthVal, int stencil
device_->Clear(0, NULL, d3dMask, (D3DCOLOR)SwapRB(colorval), depthVal, stencilVal);
}
void D3D9Context::SetScissorEnabled(bool enable) {
device_->SetRenderState(D3DRS_SCISSORTESTENABLE, enable);
}
void D3D9Context::SetScissorRect(int left, int top, int width, int height) {
RECT rc;
rc.left = left;

View File

@ -192,6 +192,7 @@ public:
glEnable(GL_CULL_FACE);
glFrontFace(frontFace);
glCullFace(cullMode);
glEnable(GL_SCISSOR_TEST);
}
GLboolean cullEnable;
@ -354,15 +355,14 @@ struct UniformInfo {
int loc_;
};
// TODO: Fold BlendState into this? Seems likely to be right for DX12 etc.
// TODO: Add Uniform Buffer support.
class OpenGLShaderSet : public ShaderSet, GfxResourceHolder {
class OpenGLPipeline : public Pipeline, GfxResourceHolder {
public:
OpenGLShaderSet() {
OpenGLPipeline() {
program_ = 0;
register_gl_resource_holder(this);
}
~OpenGLShaderSet() {
~OpenGLPipeline() {
unregister_gl_resource_holder(this);
for (auto iter : shaders) {
iter->Release();
@ -410,7 +410,7 @@ public:
SamplerState *CreateSamplerState(const SamplerStateDesc &desc) override;
RasterState *CreateRasterState(const RasterStateDesc &desc) override;
Buffer *CreateBuffer(size_t size, uint32_t usageFlags) override;
ShaderSet *CreateShaderSet(const ShaderSetDesc &desc) override;
Pipeline *CreatePipeline(const PipelineDesc &desc) override;
InputLayout *CreateVertexFormat(const std::vector<VertexComponent> &components, int stride, ShaderModule *vshader) override;
Texture *CreateTexture(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) override;
Texture *CreateTexture() override;
@ -456,14 +456,6 @@ public:
ShaderModule *CreateShaderModule(ShaderStage stage, const char *glsl_source, const char *hlsl_source, const char *vulkan_source) override;
void SetScissorEnabled(bool enable) override {
if (enable) {
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
}
void SetScissorRect(int left, int top, int width, int height) override {
glScissor(left, targetHeight_ - (top + height), width, height);
}
@ -481,9 +473,9 @@ public:
void BindTextures(int start, int count, Texture **textures) override;
// TODO: Add more sophisticated draws.
void Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override;
void DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override;
void DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) override;
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 Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;
std::string GetInfoString(InfoField info) const override {
@ -806,12 +798,12 @@ Buffer *OpenGLContext::CreateBuffer(size_t size, uint32_t usageFlags) {
return new OpenGLBuffer(size, usageFlags);
}
ShaderSet *OpenGLContext::CreateShaderSet(const ShaderSetDesc &desc) {
Pipeline *OpenGLContext::CreatePipeline(const PipelineDesc &desc) {
if (!desc.shaders.size()) {
ELOG("ShaderSet requires at least one shader");
return NULL;
}
OpenGLShaderSet *shaderSet = new OpenGLShaderSet();
OpenGLPipeline *shaderSet = new OpenGLPipeline();
for (auto iter : desc.shaders) {
iter->AddRef();
shaderSet->shaders.push_back(static_cast<OpenGLShaderModule *>(iter));
@ -848,7 +840,7 @@ ShaderModule *OpenGLContext::CreateShaderModule(ShaderStage stage, const char *g
}
}
bool OpenGLShaderSet::Link() {
bool OpenGLPipeline::Link() {
program_ = glCreateProgram();
for (auto iter : shaders) {
glAttachShader(program_, iter->GetShader());
@ -897,7 +889,7 @@ bool OpenGLShaderSet::Link() {
return true;
}
int OpenGLShaderSet::GetUniformLoc(const char *name) {
int OpenGLPipeline::GetUniformLoc(const char *name) {
auto iter = uniforms_.find(name);
int loc = -1;
if (iter != uniforms_.end()) {
@ -911,7 +903,7 @@ int OpenGLShaderSet::GetUniformLoc(const char *name) {
return loc;
}
void OpenGLShaderSet::SetVector(const char *name, float *value, int n) {
void OpenGLPipeline::SetVector(const char *name, float *value, int n) {
glUseProgram(program_);
int loc = GetUniformLoc(name);
if (loc != -1) {
@ -924,7 +916,7 @@ void OpenGLShaderSet::SetVector(const char *name, float *value, int n) {
}
}
void OpenGLShaderSet::SetMatrix4x4(const char *name, const float value[16]) {
void OpenGLPipeline::SetMatrix4x4(const char *name, const float value[16]) {
glUseProgram(program_);
int loc = GetUniformLoc(name);
if (loc != -1) {
@ -932,16 +924,16 @@ void OpenGLShaderSet::SetMatrix4x4(const char *name, const float value[16]) {
}
}
void OpenGLShaderSet::Apply() {
void OpenGLPipeline::Apply() {
glUseProgram(program_);
}
void OpenGLShaderSet::Unapply() {
void OpenGLPipeline::Unapply() {
glUseProgram(0);
}
void OpenGLContext::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
OpenGLShaderSet *ss = static_cast<OpenGLShaderSet *>(shaderSet);
void OpenGLContext::Draw(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
OpenGLPipeline *ss = static_cast<OpenGLPipeline *>(shaderSet);
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(format);
@ -955,8 +947,8 @@ void OpenGLContext::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *form
fmt->Unapply();
}
void OpenGLContext::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
OpenGLShaderSet *ss = static_cast<OpenGLShaderSet *>(shaderSet);
void OpenGLContext::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
OpenGLPipeline *ss = static_cast<OpenGLPipeline *>(shaderSet);
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
OpenGLBuffer *ibuf = static_cast<OpenGLBuffer *>(idata);
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(format);
@ -973,8 +965,8 @@ void OpenGLContext::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayou
fmt->Unapply();
}
void OpenGLContext::DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
OpenGLShaderSet *ss = static_cast<OpenGLShaderSet *>(shaderSet);
void OpenGLContext::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
OpenGLPipeline *ss = static_cast<OpenGLPipeline *>(shaderSet);
OpenGLVertexFormat *fmt = static_cast<OpenGLVertexFormat *>(format);
fmt->Apply(vdata);

View File

@ -327,14 +327,14 @@ public:
int stride_;
};
class VKShaderSet : public ShaderSet {
class VKPipeline : public Pipeline {
public:
VKShaderSet() {
VKPipeline() {
// HACK! Hardcoded
uboSize_ = 16 * sizeof(float); // WorldViewProj
ubo_ = new uint8_t[uboSize_];
}
~VKShaderSet() {
~VKPipeline() {
for (auto iter : shaders) {
iter->Release();
}
@ -366,7 +366,7 @@ private:
struct PipelineKey {
VKDepthStencilState *depthStencil;
VKBlendState *blend;
VKShaderSet *shaderSet;
VKPipeline *shaderSet;
VkPrimitiveTopology topology;
VKRasterState *raster;
@ -412,7 +412,7 @@ public:
InputLayout *CreateVertexFormat(const std::vector<VertexComponent> &components, int stride, ShaderModule *vshader) override;
SamplerState *CreateSamplerState(const SamplerStateDesc &desc) override;
RasterState *CreateRasterState(const RasterStateDesc &desc) override;
ShaderSet *CreateShaderSet(const ShaderSetDesc &desc) override;
Pipeline *CreatePipeline(const PipelineDesc &desc) override;
// The implementation makes the choice of which shader code to use.
ShaderModule *CreateShaderModule(ShaderStage stage, const char *glsl_source, const char *hlsl_source, const char *vulkan_source) override;
@ -437,11 +437,6 @@ public:
curRasterState_ = s;
}
void SetScissorEnabled(bool enable) override {
scissorEnabled_ = enable;
scissorDirty_ = true;
}
void SetScissorRect(int left, int top, int width, int height) override;
void SetViewports(int count, Viewport *viewports) override;
@ -451,9 +446,9 @@ public:
void SetSamplerStates(int start, int count, SamplerState **state) override;
// TODO: Add more sophisticated draws.
void Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) override;
void DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) override;
void DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) override;
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 Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;
@ -491,7 +486,7 @@ private:
// These are used to compose the pipeline cache key.
VKBlendState *curBlendState_;
VKDepthStencilState *curDepthStencilState_;
VKShaderSet *curShaderSet_;
VKPipeline *curPipeline_;
VkPrimitiveTopology curPrim_;
VKVertexFormat *curVertexFormat_;
VKRasterState *curRasterState_;
@ -513,9 +508,6 @@ private:
VkViewport viewport_;
bool scissorDirty_;
VkRect2D scissor_;
bool scissorEnabled_;
VkRect2D noScissor_; // Simply a scissor covering the screen.
enum {MAX_BOUND_TEXTURES = 1};
VKTexture *boundTextures_[MAX_BOUND_TEXTURES];
@ -658,11 +650,10 @@ VKContext::VKContext(VulkanContext *vulkan)
queue_ = vulkan->GetGraphicsQueue();
queueFamilyIndex_ = vulkan->GetGraphicsQueueFamilyIndex();
noScissor_.offset.x = 0;
noScissor_.offset.y = 0;
noScissor_.extent.width = pixel_xres;
noScissor_.extent.height = pixel_yres;
scissor_ = noScissor_;
scissor_.offset.x = 0;
scissor_.offset.y = 0;
scissor_.extent.width = pixel_xres;
scissor_.extent.height = pixel_yres;
viewport_.x = 0;
viewport_.y = 0;
viewport_.width = pixel_xres;
@ -769,8 +760,8 @@ void VKContext::Begin(bool clear, uint32_t colorval, float depthVal, int stencil
VkResult result = vkResetDescriptorPool(device_, frame->descriptorPool, 0);
assert(result == VK_SUCCESS);
noScissor_.extent.width = pixel_xres;
noScissor_.extent.height = pixel_yres;
scissor_.extent.width = pixel_xres;
scissor_.extent.height = pixel_yres;
scissorDirty_ = true;
viewportDirty_ = true;
}
@ -812,7 +803,7 @@ VkDescriptorSet VKContext::GetOrCreateDescriptorSet(VkBuffer buf) {
VkDescriptorBufferInfo bufferDesc;
bufferDesc.buffer = buf;
bufferDesc.offset = 0;
bufferDesc.range = curShaderSet_->GetUBOSize();
bufferDesc.range = curPipeline_->GetUBOSize();
VkDescriptorImageInfo imageDesc;
imageDesc.imageView = boundTextures_[0]->GetImageView();
@ -850,7 +841,7 @@ VkPipeline VKContext::GetOrCreatePipeline() {
PipelineKey key;
key.blend = curBlendState_;
key.depthStencil = curDepthStencilState_;
key.shaderSet = curShaderSet_;
key.shaderSet = curPipeline_;
key.topology = curPrim_;
key.raster = curRasterState_;
@ -860,9 +851,9 @@ VkPipeline VKContext::GetOrCreatePipeline() {
}
std::vector<VkPipelineShaderStageCreateInfo> stages;
stages.resize(curShaderSet_->shaders.size());
stages.resize(curPipeline_->shaders.size());
int i = 0;
for (auto &iter : curShaderSet_->shaders) {
for (auto &iter : curPipeline_->shaders) {
VkPipelineShaderStageCreateInfo &stage = stages[i++];
stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stage.pNext = nullptr;
@ -959,11 +950,7 @@ void VKContext::SetViewports(int count, Viewport *viewports) {
void VKContext::ApplyDynamicState() {
if (scissorDirty_) {
if (scissorEnabled_) {
vkCmdSetScissor(cmd_, 0, 1, &scissor_);
} else {
vkCmdSetScissor(cmd_, 0, 1, &noScissor_);
}
vkCmdSetScissor(cmd_, 0, 1, &scissor_);
scissorDirty_ = false;
}
if (viewportDirty_) {
@ -1035,12 +1022,12 @@ Buffer *VKContext::CreateBuffer(size_t size, uint32_t usageFlags) {
return new Thin3DVKBuffer(size, usageFlags);
}
ShaderSet *VKContext::CreateShaderSet(const ShaderSetDesc &desc) {
Pipeline *VKContext::CreatePipeline(const PipelineDesc &desc) {
if (!desc.shaders.size()) {
ELOG("ShaderSet requires at least one shader");
return NULL;
}
VKShaderSet *shaderSet = new VKShaderSet();
VKPipeline *shaderSet = new VKPipeline();
for (auto iter : desc.shaders) {
iter->AddRef();
shaderSet->shaders.push_back(static_cast<VKShaderModule *>(iter));
@ -1070,12 +1057,12 @@ ShaderModule *VKContext::CreateShaderModule(ShaderStage stage, const char *glsl_
}
}
bool VKShaderSet::Link() {
bool VKPipeline::Link() {
// There is no link step. However, we will create and cache Pipeline objects in the device context.
return true;
}
int VKShaderSet::GetUniformLoc(const char *name) {
int VKPipeline::GetUniformLoc(const char *name) {
int loc = -1;
// HACK! As we only use one uniform we hardcode it.
@ -1086,11 +1073,11 @@ int VKShaderSet::GetUniformLoc(const char *name) {
return loc;
}
void VKShaderSet::SetVector(const char *name, float *value, int n) {
void VKPipeline::SetVector(const char *name, float *value, int n) {
// TODO: Implement
}
void VKShaderSet::SetMatrix4x4(const char *name, const float value[16]) {
void VKPipeline::SetMatrix4x4(const char *name, const float value[16]) {
int loc = GetUniformLoc(name);
if (loc != -1) {
memcpy(ubo_ + loc, value, 16 * sizeof(float));
@ -1115,21 +1102,21 @@ inline VkPrimitiveTopology PrimToVK(Primitive prim) {
}
}
void VKContext::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
void VKContext::Draw(Primitive prim, Pipeline *pipeline, InputLayout *format, Buffer *vdata, int vertexCount, int offset) {
ApplyDynamicState();
curPrim_ = PrimToVK(prim);
curShaderSet_ = (VKShaderSet *)shaderSet;
curPipeline_ = (VKPipeline *)pipeline;
curVertexFormat_ = (VKVertexFormat *)format;
Thin3DVKBuffer *vbuf = static_cast<Thin3DVKBuffer *>(vdata);
VkBuffer vulkanVbuf;
VkBuffer vulkanUBObuf;
uint32_t ubo_offset = (uint32_t)curShaderSet_->PushUBO(push_, vulkan_, &vulkanUBObuf);
uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf);
size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), &vulkanVbuf);
VkPipeline pipeline = GetOrCreatePipeline();
vkCmdBindPipeline(cmd_, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
VkPipeline vkpipeline = GetOrCreatePipeline();
vkCmdBindPipeline(cmd_, VK_PIPELINE_BIND_POINT_GRAPHICS, vkpipeline);
VkDescriptorSet descSet = GetOrCreateDescriptorSet(vulkanUBObuf);
vkCmdBindDescriptorSets(cmd_, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout_, 0, 1, &descSet, 1, &ubo_offset);
VkBuffer buffers[1] = { vulkanVbuf };
@ -1138,18 +1125,18 @@ void VKContext::Draw(Primitive prim, ShaderSet *shaderSet, InputLayout *format,
vkCmdDraw(cmd_, vertexCount, 1, offset, 0);
}
void VKContext::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
void VKContext::DrawIndexed(Primitive prim, Pipeline *shaderSet, InputLayout *format, Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
ApplyDynamicState();
curPrim_ = PrimToVK(prim);
curShaderSet_ = (VKShaderSet *)shaderSet;
curPipeline_ = (VKPipeline *)shaderSet;
curVertexFormat_ = (VKVertexFormat *)format;
Thin3DVKBuffer *ibuf = static_cast<Thin3DVKBuffer *>(idata);
Thin3DVKBuffer *vbuf = static_cast<Thin3DVKBuffer *>(vdata);
VkBuffer vulkanVbuf, vulkanIbuf, vulkanUBObuf;
uint32_t ubo_offset = (uint32_t)curShaderSet_->PushUBO(push_, vulkan_, &vulkanUBObuf);
uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf);
size_t vbBindOffset = push_->Push(vbuf->GetData(), vbuf->GetSize(), &vulkanVbuf);
size_t ibBindOffset = push_->Push(ibuf->GetData(), ibuf->GetSize(), &vulkanIbuf);
@ -1167,16 +1154,16 @@ void VKContext::DrawIndexed(Primitive prim, ShaderSet *shaderSet, InputLayout *f
vkCmdDrawIndexed(cmd_, vertexCount, 1, 0, offset, 0);
}
void VKContext::DrawUP(Primitive prim, ShaderSet *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
void VKContext::DrawUP(Primitive prim, Pipeline *shaderSet, InputLayout *format, const void *vdata, int vertexCount) {
ApplyDynamicState();
curPrim_ = PrimToVK(prim);
curShaderSet_ = (VKShaderSet *)shaderSet;
curPipeline_ = (VKPipeline *)shaderSet;
curVertexFormat_ = (VKVertexFormat *)format;
VkBuffer vulkanVbuf, vulkanUBObuf;
size_t vbBindOffset = push_->Push(vdata, vertexCount * curVertexFormat_->stride_, &vulkanVbuf);
uint32_t ubo_offset = (uint32_t)curShaderSet_->PushUBO(push_, vulkan_, &vulkanUBObuf);
uint32_t ubo_offset = (uint32_t)curPipeline_->PushUBO(push_, vulkan_, &vulkanUBObuf);
VkPipeline pipeline = GetOrCreatePipeline();
vkCmdBindPipeline(cmd_, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);

View File

@ -15,7 +15,7 @@
DrawBuffer ui_draw2d;
DrawBuffer ui_draw2d_front;
void UIBegin(Draw::ShaderSet *shaderSet) {
void UIBegin(Draw::Pipeline *shaderSet) {
ui_draw2d.Begin(shaderSet);
ui_draw2d_front.Begin(shaderSet);
}

View File

@ -61,7 +61,7 @@ private:
// Call at start of frame
void UIBegin(Draw::ShaderSet *shaderSet);
void UIBegin(Draw::Pipeline *shaderSet);
// Call at end of frame.

View File

@ -6,7 +6,7 @@
#include "gfx_es2/draw_text.h"
UIContext::UIContext()
: uishader_(0), uitexture_(0), uidrawbuffer_(0), uidrawbufferTop_(0) {
: ui_pipeline_(0), uitexture_(0), uidrawbuffer_(0), uidrawbufferTop_(0) {
fontScaleX_ = 1.0f;
fontScaleY_ = 1.0f;
fontStyle_ = new UI::FontStyle();
@ -23,7 +23,7 @@ UIContext::~UIContext() {
blendNormal_->Release();
}
void UIContext::Init(Draw::DrawContext *thin3d, Draw::ShaderSet *uishader, Draw::ShaderSet *uishadernotex, Draw::Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop) {
void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pipeline *uipipenotex, Draw::Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop) {
using namespace Draw;
thin3d_ = thin3d;
blendNormal_ = thin3d_->CreateBlendState({ true, BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA });
@ -33,8 +33,8 @@ void UIContext::Init(Draw::DrawContext *thin3d, Draw::ShaderSet *uishader, Draw:
desc.cull = CullMode::NONE;
desc.facing = Facing::CCW;
rasterNoCull_ = thin3d_->CreateRasterState(desc);
uishader_ = uishader;
uishadernotex_ = uishadernotex;
ui_pipeline_ = uipipe;
ui_pipeline_notex_ = uipipenotex;
uitexture_ = uitexture;
uidrawbuffer_ = uidrawbuffer;
uidrawbufferTop_ = uidrawbufferTop;
@ -51,15 +51,14 @@ void UIContext::Begin() {
thin3d_->SetDepthStencilState(depth_);
thin3d_->SetRasterState(rasterNoCull_);
thin3d_->BindTexture(0, uitexture_);
thin3d_->SetScissorEnabled(false);
UIBegin(uishader_);
UIBegin(ui_pipeline_);
}
void UIContext::BeginNoTex() {
thin3d_->SetBlendState(blendNormal_);
thin3d_->SetSamplerStates(0, 1, &sampler_);
thin3d_->SetRasterState(rasterNoCull_);
UIBegin(uishadernotex_);
UIBegin(ui_pipeline_notex_);
}
void UIContext::RebindTexture() const {
@ -114,9 +113,9 @@ void UIContext::ActivateTopScissor() {
int w = scale * bounds.w;
int h = scale * bounds.h;
thin3d_->SetScissorRect(x, y, w, h);
thin3d_->SetScissorEnabled(true);
} else {
thin3d_->SetScissorEnabled(false);
}
else {
thin3d_->SetScissorRect(bounds_.x, bounds_.y, bounds_.w, bounds_.h);
}
}

View File

@ -11,7 +11,7 @@
namespace Draw {
class DrawContext;
class ShaderSet;
class Pipeline;
class DepthStencilState;
class Texture;
class BlendState;
@ -36,7 +36,7 @@ public:
UIContext();
~UIContext();
void Init(Draw::DrawContext *thin3d, Draw::ShaderSet *uiShaderTex, Draw::ShaderSet *uiShaderNoTex, Draw::Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop);
void Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pipeline *uipipenotex, Draw::Texture *uitexture, DrawBuffer *uidrawbuffer, DrawBuffer *uidrawbufferTop);
void Begin();
void BeginNoTex();
@ -90,8 +90,8 @@ private:
Draw::SamplerState *sampler_;
Draw::RasterState *rasterNoCull_;
Draw::BlendState *blendNormal_;
Draw::ShaderSet *uishader_;
Draw::ShaderSet *uishadernotex_;
Draw::Pipeline *ui_pipeline_;
Draw::Pipeline *ui_pipeline_notex_;
Draw::Texture *uitexture_;
DrawBuffer *uidrawbuffer_;