]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/programdata.cpp
Redesign framebuffer attachment management
[libs/gl.git] / source / render / programdata.cpp
index d934b48782c23ce181a5022b5fcfeb6009ad83b4..79b559019d116b196502267222e5c37e6985642d 100644 (file)
@@ -7,6 +7,7 @@
 #include "color.h"
 #include "error.h"
 #include "matrix.h"
+#include "pipelinestate.h"
 #include "program.h"
 #include "programdata.h"
 #include "uniform.h"
@@ -20,6 +21,7 @@ namespace GL {
 
 ProgramData::ProgramData(const Program *p):
        tied_program(p),
+       generation(0),
        last_buffer_block(0),
        buffer(0),
        dirty(0)
@@ -29,6 +31,7 @@ ProgramData::ProgramData(const Program *p):
 ProgramData::ProgramData(const ProgramData &other):
        tied_program(other.tied_program),
        uniforms(other.uniforms),
+       generation(other.generation),
        last_buffer_block(0),
        buffer(0),
        dirty(0)
@@ -107,7 +110,7 @@ void ProgramData::uniform(Tag tag, Uniform *uni)
                return add_uniform(tag, uni);
 
        uniforms[i].replace_value(uni);
-       dirty |= 1<<i;
+       mark_dirty(1<<i);
 }
 
 template<typename T, typename V>
@@ -125,7 +128,7 @@ void ProgramData::uniform(Tag tag, V value)
        else
                uniforms[i].replace_value(new T(value));
 
-       dirty |= 1<<i;
+       mark_dirty(1<<i);
 }
 
 template<typename T, typename V>
@@ -144,7 +147,7 @@ void ProgramData::uniform_array(Tag tag, unsigned n, V value)
        else
                uniforms[i].replace_value(new UniformArray<T>(n, value));
 
-       dirty |= 1<<i;
+       mark_dirty(1<<i);
 }
 
 bool ProgramData::validate_tag(Tag tag) const
@@ -185,7 +188,14 @@ void ProgramData::add_uniform(Tag tag, Uniform *uni)
        nu.value = uni;
        uniforms.insert(j, nu);
 
-       dirty = ALL_ONES;
+       mark_dirty(ALL_ONES);
+}
+
+void ProgramData::mark_dirty(Mask bits)
+{
+       if(!dirty)
+               ++generation;
+       dirty |= bits;
 }
 
 void ProgramData::uniform(Tag tag, const Uniform &u)
@@ -428,7 +438,7 @@ void ProgramData::remove_uniform(Tag tag)
        delete i->value;
        uniforms.erase(i);
 
-       dirty = ALL_ONES;
+       mark_dirty(ALL_ONES);
 }
 
 vector<Tag> ProgramData::get_uniform_tags() const
@@ -556,7 +566,14 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program
                if(info.bind_point>=0)
                {
                        if(!buffer)
-                               buffer = new Buffer(UNIFORM_BUFFER);
+                       {
+                               buffer = new Buffer();
+
+#ifdef DEBUG
+                               if(!debug_name.empty())
+                                       buffer->set_debug_name(debug_name);
+#endif
+                       }
 
                        BufferBackedUniformBlock *bb_block = new BufferBackedUniformBlock(info.data_size);
                        block.block = bb_block;
@@ -580,15 +597,10 @@ void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockIn
        }
 }
 
-void ProgramData::apply() const
+vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
 {
-       const Program *prog = Program::current();
-       if(!prog)
-               throw invalid_operation("ProgramData::apply");
-
        BufferBackedUniformBlock *old_last_block = last_buffer_block;
-       vector<ProgramBlock>::iterator prog_begin = get_program(*prog);
-       Program::LayoutHash prog_hash = prog->get_uniform_layout_hash();
+       vector<ProgramBlock>::iterator prog_begin = get_program(prog);
 
        Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
        Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
@@ -607,7 +619,7 @@ void ProgramData::apply() const
                        dirty = 0;
                }
 
-               const vector<Program::UniformBlockInfo> &block_infos = prog->get_uniform_blocks();
+               const vector<Program::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
 
                if(prog_begin->masks.dirty==ALL_ONES)
                {
@@ -641,11 +653,6 @@ void ProgramData::apply() const
 
                prog_begin->masks.dirty = 0;
 
-               /* If any blocks stored in the buffer were updated, bind the buffer here
-               to avoid state thrashing. */
-               if(buffered_blocks_updated && !ARB_direct_state_access)
-                       buffer->bind();
-
                if(last_buffer_block!=old_last_block)
                {
                        unsigned required_size = last_buffer_block->get_required_buffer_size();
@@ -654,8 +661,13 @@ void ProgramData::apply() const
                                if(buffer->get_size()>0)
                                {
                                        delete buffer;
-                                       buffer = new Buffer(UNIFORM_BUFFER);
+                                       buffer = new Buffer();
                                        last_buffer_block->change_buffer(buffer);
+
+#ifdef DEBUG
+                                       if(!debug_name.empty())
+                                               buffer->set_debug_name(debug_name);
+#endif
                                }
 
                                buffer->storage(required_size);
@@ -663,9 +675,36 @@ void ProgramData::apply() const
                }
        }
 
-       for(vector<ProgramBlock>::iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
+       return prog_begin;
+}
+
+void ProgramData::apply(const Program &prog, PipelineState &state) const
+{
+       vector<ProgramBlock>::const_iterator prog_begin = prepare_program(prog);
+       Program::LayoutHash prog_hash = prog_begin->prog_hash;
+       for(vector<ProgramBlock>::const_iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
                if(i->block)
-                       i->block->apply(i->bind_point);
+               {
+                       if(i->bind_point<0)
+                               state.set_uniforms(static_cast<const DefaultUniformBlock *>(i->block));
+                       else
+                       {
+                               const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
+                               block->refresh();
+                               state.set_uniform_block(i->bind_point, block);
+                       }
+               }
+}
+
+void ProgramData::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       debug_name = name;
+       if(buffer)
+               buffer->set_debug_name(name);
+#else
+       (void)name;
+#endif
 }