X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fcore%2Fpipelinestate.cpp;h=8d59a31624eddc823e23cd7260ed444c4efc9129;hp=97f353dcee8b572c835e1ac5569ca9769489b246;hb=HEAD;hpb=009918e76dda88b0cb68fdaa20c63d6e952af260 diff --git a/source/core/pipelinestate.cpp b/source/core/pipelinestate.cpp index 97f353dc..6078db5d 100644 --- a/source/core/pipelinestate.cpp +++ b/source/core/pipelinestate.cpp @@ -1,64 +1,18 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include "blend.h" -#include "buffer.h" -#include "deviceinfo.h" -#include "depthtest.h" -#include "framebuffer.h" +#include +#include "error.h" #include "pipelinestate.h" #include "program.h" -#include "rect.h" -#include "sampler.h" -#include "stenciltest.h" -#include "texture.h" #include "uniformblock.h" -#include "vertexsetup.h" using namespace std; namespace Msp { namespace GL { -const PipelineState *PipelineState::last_applied = 0; -vector PipelineState::bound_tex_targets; -vector PipelineState::bound_uniform_blocks; -unsigned PipelineState::restart_index = 0; - -PipelineState::PipelineState(): - framebuffer(0), - viewport(0), - scissor(0), - shprog(0), - vertex_setup(0), - front_face(COUNTERCLOCKWISE), - face_cull(NO_CULL), - enabled_clip_planes(0), - depth_test(0), - stencil_test(0), - blend(0), - changes(0) -{ - if(bound_tex_targets.empty()) - bound_tex_targets.resize(DeviceInfo::get_global().limits.max_texture_bindings); - if(bound_uniform_blocks.empty()) - bound_uniform_blocks.resize(DeviceInfo::get_global().limits.max_uniform_bindings); -} - -PipelineState::~PipelineState() -{ - if(this==last_applied) - last_applied = 0; -} - template -void PipelineState::set(T &target, T value, unsigned flag) +void PipelineState::set(T &target, const T &value, unsigned flag) { if(value!=target) { @@ -69,15 +23,15 @@ void PipelineState::set(T &target, T value, unsigned flag) void PipelineState::set_framebuffer(const Framebuffer *f) { - set(framebuffer, f, FRAMEBUFFER|VIEWPORT); + set(framebuffer, f, FRAMEBUFFER); } -void PipelineState::set_viewport(const Rect *v) +void PipelineState::set_viewport(const Rect &v) { set(viewport, v, VIEWPORT); } -void PipelineState::set_scissor(const Rect *s) +void PipelineState::set_scissor(const Rect &s) { set(scissor, s, SCISSOR); } @@ -87,305 +41,125 @@ void PipelineState::set_shader_program(const Program *p) set(shprog, p, SHPROG); } -void PipelineState::set_vertex_setup(const VertexSetup *s) +void PipelineState::set_uniform_block(int binding, const UniformBlock *block) { - set(vertex_setup, s, VERTEX_SETUP); + auto i = lower_bound_member(resources, binding, &BoundResource::binding); + if(i==resources.end() || i->binding!=binding) + i = resources.insert(i, BoundResource(binding)); + + ResourceType type = (block ? UNIFORM_BLOCK : NO_RESOURCE); + const Buffer *buffer = (block ? block->get_buffer() : 0); + if(i->type!=type || block!=i->block || buffer!=i->buffer || binding<0) + { + i->type = type; + i->block = block; + i->buffer = buffer; + i->changed = true; + i->used = block; + changes |= RESOURCES; + } } -void PipelineState::set_front_face(FaceWinding w) +void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp) { - set(front_face, w, FACE_CULL); + set_texture(binding, tex, -1, samp); } -void PipelineState::set_face_cull(CullMode c) +void PipelineState::set_texture(unsigned binding, const Texture *tex, int level, const Sampler *samp) { - set(face_cull, c, FACE_CULL); + if((tex!=0)!=(samp!=0)) + throw invalid_argument("PipelineState::set_texture"); + if(level>=0 && !can_bind_tex_level(level)) + throw invalid_operation("PipelineState::set_texture"); + + set_texture_resource(binding, tex, level, samp); } -void PipelineState::set_enabled_clip_planes(unsigned p) +void PipelineState::set_storage_texture(unsigned binding, const Texture *tex) { - set(enabled_clip_planes, p, CLIP_PLANES); + set_texture_resource(binding, tex, 0, 0); } -void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp) +void PipelineState::set_texture_resource(unsigned binding, const Texture *tex, int level, const Sampler *samp) { - if((tex!=0)!=(samp!=0)) - throw invalid_argument("PipelineState::set_texture"); + auto i = lower_bound_member(resources, static_cast(binding), &BoundResource::binding); + if(i==resources.end() || i->binding!=static_cast(binding)) + i = resources.insert(i, BoundResource(binding)); - auto i = lower_bound_member(textures, binding, &BoundTexture::binding); - if(i==textures.end() || i->binding!=binding) - i = textures.insert(i, BoundTexture(binding)); - if(tex!=i->texture || samp!=i->sampler) + ResourceType type = (tex ? samp ? SAMPLED_TEXTURE : STORAGE_TEXTURE : NO_RESOURCE); + if(i->type!=type || tex!=i->texture || level!=i->mip_level || samp!=i->sampler) { + i->type = type; i->texture = tex; i->sampler = samp; + i->mip_level = level; i->changed = true; - changes |= TEXTURES; + i->used = tex; + changes |= RESOURCES; } } -void PipelineState::set_uniform_block(int binding, const UniformBlock *block) +void PipelineState::set_vertex_setup(const VertexSetup *s) { - auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding); - if(i==uniform_blocks.end() || i->binding!=binding) - i = uniform_blocks.insert(i, BoundUniformBlock(binding)); - if(block!=i->block || binding<0) - { - i->block = block; - i->changed = true; - changes |= UNIFORMS; - } + set(vertex_setup, s, VERTEX_SETUP); } -void PipelineState::set_depth_test(const DepthTest *dt) +void PipelineState::set_primitive_type(PrimitiveType t) { - set(depth_test, dt, DEPTH_TEST); + set(primitive_type, t, PRIMITIVE_TYPE); } -void PipelineState::set_stencil_test(const StencilTest *st) +void PipelineState::set_patch_size(unsigned s) { - set(stencil_test, st, STENCIL_TEST); + set(patch_size, s, PATCH_SIZE); } -void PipelineState::set_blend(const Blend *b) +void PipelineState::set_front_face(FaceWinding w) { - set(blend, b, BLEND); + set(front_face, w, FACE_CULL); } -void PipelineState::apply() const +void PipelineState::set_face_cull(CullMode c) { - if(!last_applied) - Texture::unbind_scratch(); - - apply(this==last_applied ? changes : ~0U); + set(face_cull, c, FACE_CULL); } -void PipelineState::apply(unsigned mask) const +void PipelineState::set_depth_test(const DepthTest &dt) { - if(mask&FRAMEBUFFER) - { - glBindFramebuffer(GL_FRAMEBUFFER, framebuffer ? framebuffer->id : 0); - if(framebuffer) - { - framebuffer->refresh(); - framebuffer->require_complete(); - } - } - - if(mask&VIEWPORT) - { - if(viewport) - glViewport(viewport->left, viewport->bottom, viewport->width, viewport->height); - else if(framebuffer) - glViewport(0, 0, framebuffer->get_width(), framebuffer->get_height()); - } - - if(mask&SCISSOR) - { - if(scissor) - { - glEnable(GL_SCISSOR_TEST); - glScissor(scissor->left, scissor->bottom, scissor->width, scissor->height); - } - else - glDisable(GL_SCISSOR_TEST); - } - - if(mask&SHPROG) - glUseProgram(shprog ? shprog->id : 0); - - if(mask&VERTEX_SETUP) - { - glBindVertexArray(vertex_setup ? vertex_setup->id : 0); - if(vertex_setup) - { - static Require _req(MSP_primitive_restart); - - vertex_setup->refresh(); - unsigned ri = (vertex_setup->get_index_type()==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF); - if(ri!=restart_index) - { - if(!restart_index) - glEnable(GL_PRIMITIVE_RESTART); - glPrimitiveRestartIndex(ri); - restart_index = ri; - } - } - } - - if(mask&FACE_CULL) - { - glFrontFace(front_face==CLOCKWISE ? GL_CW : GL_CCW); - - if(face_cull!=NO_CULL && front_face!=NON_MANIFOLD) - { - glEnable(GL_CULL_FACE); - glCullFace(face_cull==CULL_FRONT ? GL_FRONT : GL_BACK); - } - else - glDisable(GL_CULL_FACE); - } - - if(mask&CLIP_PLANES) - { - unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes; - for(unsigned i=0; i>i)&1) - glEnable(GL_CLIP_PLANE0+i); - else - glDisable(GL_CLIP_PLANE0+i); - } - } - - if(mask&TEXTURES) - { - for(const BoundTexture &t: textures) - if(t.changed || mask==~0U) - { - if(t.texture && t.sampler) - { - if(ARB_direct_state_access) - glBindTextureUnit(t.binding, t.texture->id); - else - { - glActiveTexture(GL_TEXTURE0+t.binding); - if(bound_tex_targets[t.binding] && static_cast(t.texture->target)!=bound_tex_targets[t.binding]) - glBindTexture(bound_tex_targets[t.binding], 0); - glBindTexture(t.texture->target, t.texture->id); - } - - bound_tex_targets[t.binding] = t.texture->target; - - glBindSampler(t.binding, t.sampler->id); - t.sampler->refresh(); - } - - t.changed = false; - } - } - - if(mask&UNIFORMS) - { - for(const BoundUniformBlock &u: uniform_blocks) - if(u.changed || mask==~0U) - { - if(u.block) - { - if(u.binding>=0) - { - glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, u.block->get_buffer()->id, u.block->get_offset(), u.block->get_data_size()); - bound_uniform_blocks[u.binding] = 1; - } - else if(shprog) - { - const char *data = static_cast(u.block->get_data_pointer()); - for(const Program::UniformCall &call: shprog->uniform_calls) - call.func(call.location, call.size, data+call.location*16); - } - } + set(depth_test, dt, DEPTH_TEST); +} - u.changed = false; - } - } +void PipelineState::set_stencil_test(const StencilTest &st) +{ + set(stencil_test, st, STENCIL_TEST); +} - if(mask&DEPTH_TEST) - { - if(depth_test && depth_test->enabled) - { - glEnable(GL_DEPTH_TEST); - glDepthFunc(get_gl_predicate(depth_test->compare)); - } - else - glDisable(GL_DEPTH_TEST); +void PipelineState::set_blend(const Blend &b) +{ + set(blend, b, BLEND); +} - glDepthMask(!depth_test || depth_test->write); - } +void PipelineState::check_bound_resources() const +{ + if(!shprog) + return; - if(mask&STENCIL_TEST) - { - if(stencil_test && stencil_test->enabled) + for(const ReflectData::UniformBlockInfo &b: shprog->get_uniform_blocks()) + if(b.bind_point!=ReflectData::DEFAULT_BLOCK) { - glEnable(GL_STENCIL_TEST); - glStencilFunc(get_gl_predicate(stencil_test->compare), stencil_test->reference, 0xFFFFFFFF); - glStencilOp(get_gl_stencil_op(stencil_test->stencil_fail_op), get_gl_stencil_op(stencil_test->depth_fail_op), get_gl_stencil_op(stencil_test->depth_pass_op)); + auto i = lower_bound_member(resources, b.bind_point, &PipelineState::BoundResource::binding); + if(i==resources.end() || i->binding!=b.bind_point) + IO::print(IO::cerr, "Warning: No resource present for uniform block binding %d:%d (%s)\n", b.bind_point>>20, b.bind_point&0xFFFFF, b.name); } - else - glDisable(GL_STENCIL_TEST); - } - if(mask&BLEND) - { - if(blend && blend->enabled) + for(const ReflectData::UniformInfo &u: shprog->get_uniforms()) + if(u.binding>=0 && is_image(u.type)) { - glEnable(GL_BLEND); - glBlendEquation(get_gl_blend_equation(blend->equation)); - glBlendFunc(get_gl_blend_factor(blend->src_factor), get_gl_blend_factor(blend->dst_factor)); - glBlendColor(blend->constant.r, blend->constant.g, blend->constant.b, blend->constant.a); + auto i = lower_bound_member(resources, u.binding, &PipelineState::BoundResource::binding); + if(i==resources.end() || i->binding!=u.binding) + IO::print(IO::cerr, "Warning: No resource present for texture binding %d:%d (%s)\n", u.binding>>20, u.binding&0xFFFFF, u.name); } - else - glDisable(GL_BLEND); - } - - last_applied = this; - changes &= ~mask; } -void PipelineState::clear() -{ - if(last_applied) - { - glUseProgram(0); - glBindVertexArray(0); - - unsigned max_clip_planes = DeviceInfo::get_global().limits.max_clip_planes; - for(unsigned i=0; ienabled_clip_planes>>i)&1) - glDisable(GL_CLIP_PLANE0+i); - - for(unsigned i=0; i