]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/programdata.cpp
Use standard fixed-size integer types
[libs/gl.git] / source / render / programdata.cpp
index b21c395cf04c27f1084492294dbd2635993af5df..1ef013ddac86e5a6be72643fe711764f01428d30 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"
@@ -35,8 +36,8 @@ ProgramData::ProgramData(const ProgramData &other):
        buffer(0),
        dirty(0)
 {
-       for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               i->value = i->value->clone();
+       for(TaggedUniform &u: uniforms)
+               u.value = u.value->clone();
 }
 
 ProgramData::ProgramData(const ProgramData &other, const Program *p):
@@ -47,13 +48,13 @@ ProgramData::ProgramData(const ProgramData &other, const Program *p):
 {
        if(tied_program)
        {
-               for(vector<TaggedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
-                       validate_tag(i->tag);
+               for(const TaggedUniform &u: other.uniforms)
+                       validate_tag(u.tag);
        }
 
        uniforms = other.uniforms;
-       for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               i->value = i->value->clone();
+       for(TaggedUniform &u: uniforms)
+               u.value = u.value->clone();
 }
 
 ProgramData &ProgramData::operator=(const ProgramData &other)
@@ -61,11 +62,12 @@ ProgramData &ProgramData::operator=(const ProgramData &other)
        tied_program = other.tied_program;
 
        uniforms = other.uniforms;
-       for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               i->value = i->value->clone();
+       for(TaggedUniform &u: uniforms)
+               u.value = u.value->clone();
 
-       for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
-               delete i->block;
+       for(SharedBlock &b: blocks)
+               delete b.block;
+       blocks.clear();
        programs.clear();
 
        last_buffer_block = 0;
@@ -77,13 +79,13 @@ ProgramData &ProgramData::operator=(const ProgramData &other)
 
 ProgramData::~ProgramData()
 {
-       for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               delete i->value;
-       for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+       for(TaggedUniform &u: uniforms)
+               delete u.value;
+       for(SharedBlock &b: blocks)
        {
-               if(i->indices.type_flag==0xFE)
-                       delete[] i->indices.dynamic.values;
-               delete i->block;
+               if(b.indices.type_flag==0xFE)
+                       delete[] b.indices.dynamic.values;
+               delete b.block;
        }
        delete buffer;
 }
@@ -180,7 +182,7 @@ void ProgramData::add_uniform(Tag tag, Uniform *uni)
                throw too_many_uniforms(tag.str());
        }
 
-       vector<TaggedUniform>::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+       auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
 
        TaggedUniform nu;
        nu.tag = tag;
@@ -430,7 +432,7 @@ void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
 
 void ProgramData::remove_uniform(Tag tag)
 {
-       vector<TaggedUniform>::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+       auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
        if(i==uniforms.end() || i->tag!=tag)
                return;
 
@@ -444,8 +446,8 @@ vector<Tag> ProgramData::get_uniform_tags() const
 {
        vector<Tag> tags;
        tags.reserve(uniforms.size());
-       for(vector<TaggedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               tags.push_back(i->tag);
+       for(const TaggedUniform &u: uniforms)
+               tags.push_back(u.tag);
        return tags;
 }
 
@@ -465,14 +467,14 @@ const Uniform *ProgramData::find_uniform(Tag tag) const
 
 int ProgramData::find_uniform_index(Tag tag) const
 {
-       vector<TaggedUniform>::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+       auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
        return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
 }
 
 vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
 {
        Program::LayoutHash prog_hash = prog.get_uniform_layout_hash();
-       vector<ProgramBlock>::iterator i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
+       auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
        if(i!=programs.end() && i->prog_hash==prog_hash)
                return i;
 
@@ -484,8 +486,8 @@ vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Progr
        the hashes so they can be matched up later. */
        vector<Program::LayoutHash> block_hashes;
        block_hashes.reserve(programs.size());
-       for(vector<ProgramBlock>::iterator j=programs.begin(); j!=programs.end(); ++j)
-               block_hashes.push_back(j->block_index>=0 ? blocks[j->block_index].block_hash : 0);
+       for(const ProgramBlock &b: programs)
+               block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
 
        for(unsigned j=0; j<block_infos.size(); ++j)
        {
@@ -493,7 +495,7 @@ vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Progr
                block_hashes[index+1+j] = info.layout_hash;
                programs[index+1+j].bind_point = info.bind_point;
 
-               vector<SharedBlock>::iterator k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
+               auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
                if(k==blocks.end() || k->block_hash!=info.layout_hash)
                {
                        k = blocks.insert(k, SharedBlock(info.layout_hash));
@@ -507,7 +509,7 @@ vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Progr
                unsigned hash = block_hashes[j];
                if(hash)
                {
-                       vector<SharedBlock>::const_iterator k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
+                       auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
                        programs[j].block_index = k-blocks.begin();
                }
                else
@@ -519,12 +521,12 @@ vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Progr
 
 void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program::UniformBlockInfo &info) const
 {
-       UInt8 *indices = block.indices.values;
+       uint8_t *indices = block.indices.values;
        if(info.uniforms.size()>16)
        {
                if(block.indices.type_flag==0xFD)
                {
-                       block.indices.dynamic.values = new UInt8[info.uniforms.size()];
+                       block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
                        block.indices.type_flag = 0xFE;
                }
                indices = block.indices.dynamic.values;
@@ -586,7 +588,7 @@ void ProgramData::update_block_uniform_indices(SharedBlock &block, const Program
 
 void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockInfo &info) const
 {
-       const UInt8 *indices = block.get_uniform_indices();
+       const uint8_t *indices = block.get_uniform_indices();
        for(unsigned i=0; i<info.uniforms.size(); ++i)
        {
                if(is_image(info.uniforms[i]->type))
@@ -596,15 +598,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();
+       auto prog_begin = get_program(prog);
 
        Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
        Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
@@ -615,44 +612,46 @@ void ProgramData::apply() const
                program will cause this to happen if there's any dirty uniforms. */
                if(affected)
                {
-                       for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
-                               i->dirty |= (dirty&i->used) | force_dirty;
-                       for(vector<ProgramBlock>::iterator i=programs.begin(); i!=programs.end(); ++i)
-                               if(i->block_index<0)
-                                       i->masks.dirty |= (dirty&i->masks.used) | force_dirty;
+                       for(SharedBlock &b: blocks)
+                               b.dirty |= (dirty&b.used) | force_dirty;
+                       for(ProgramBlock &b: programs)
+                               if(b.block_index<0)
+                                       b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
                        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)
                {
                        /* The set of uniforms has changed since this program was last used.
                        Refresh uniform indices within the program's blocks. */
                        prog_begin->masks.used = 0;
-                       vector<ProgramBlock>::iterator j = prog_begin+1;
-                       for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
+                       auto j = prog_begin+1;
+                       for(const Program::UniformBlockInfo &b: block_infos)
                        {
                                SharedBlock &shared = blocks[j->block_index];
                                if(shared.dirty==ALL_ONES)
-                                       update_block_uniform_indices(shared, *i);
+                                       update_block_uniform_indices(shared, b);
                                prog_begin->masks.used |= shared.used;
                                j->block = (shared.used ? shared.block : 0);
+                               ++j;
                        }
                }
 
                // Update the contents of all dirty blocks.
                bool buffered_blocks_updated = false;
-               vector<ProgramBlock>::iterator j = prog_begin+1;
-               for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
+               auto j = prog_begin+1;
+               for(const Program::UniformBlockInfo &b: block_infos)
                {
                        SharedBlock &shared = blocks[j->block_index];
                        if(shared.dirty)
                        {
-                               update_block(shared, *i);
+                               update_block(shared, b);
                                shared.dirty = 0;
                                buffered_blocks_updated |= (j->bind_point>=0);
                        }
+                       ++j;
                }
 
                prog_begin->masks.dirty = 0;
@@ -679,9 +678,25 @@ 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
+{
+       auto prog_begin = prepare_program(prog);
+       Program::LayoutHash prog_hash = prog_begin->prog_hash;
+       for(auto 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)
@@ -719,7 +734,7 @@ ProgramData::SharedBlock::SharedBlock(Program::LayoutHash h):
        indices.type_flag = 0xFD;
 }
 
-const UInt8 *ProgramData::SharedBlock::get_uniform_indices() const
+const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const
 {
        return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
 }