+#include <msp/core/algorithm.h>
+#include <msp/debug/demangle.h>
+#include <msp/io/print.h>
+#include "buffer.h"
+#include "color.h"
+#include "error.h"
+#include "matrix.h"
+#include "pipelinestate.h"
+#include "program.h"
+#include "programdata.h"
+#include "uniformblock.h"
+#include "vector.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+ProgramData::ProgramData(const Program *p):
+ tied_program(p)
+{ }
+
+ProgramData::ProgramData(ProgramData &&other):
+ tied_program(other.tied_program),
+ uniforms(move(other.uniforms)),
+ uniform_data(move(other.uniform_data)),
+ generation(other.generation),
+ blocks(move(other.blocks)),
+ programs(move(other.programs)),
+ last_buffer_block(other.last_buffer_block),
+ buffer(other.buffer),
+ dirty(other.dirty),
+ debug_name(move(other.debug_name))
+{
+ other.blocks.clear();
+ other.buffer = 0;
+}
+
+ProgramData::~ProgramData()
+{
+ for(SharedBlock &b: blocks)
+ {
+ if(b.indices.type_flag==0xFE)
+ delete[] b.indices.dynamic.values;
+ delete b.block;
+ }
+ delete buffer;
+}
+
+void ProgramData::uniform(Tag tag, DataType type, unsigned array_size, const void *value)
+{
+ if(!validate_tag(tag))
+ return;
+
+ auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+ if(i==uniforms.end() || i->tag!=tag)
+ {
+ if(uniforms.size()>=MASK_BITS)
+ throw too_many_uniforms(tag.str());
+
+ TaggedUniform tu;
+ tu.tag = tag;
+ tu.type = type;
+ tu.array_size = array_size;
+ tu.data_offset = uniform_data.size();
+ tu.data_size = array_size*get_type_size(type);
+ i = uniforms.insert(i, tu);
+ uniform_data.resize(tu.data_offset+tu.data_size);
+
+ mark_dirty(ALL_ONES);
+ }
+ else if(type!=i->type)
+ throw invalid_operation("ProgramData::uniform");
+ else if(array_size>i->array_size)
+ {
+ unsigned add_bytes = (array_size-i->array_size)*get_type_size(type);
+ uniform_data.insert(uniform_data.begin()+i->data_offset+i->data_size, add_bytes, 0);
+ for(TaggedUniform &u: uniforms)
+ if(u.data_offset>i->data_offset)
+ u.data_offset += add_bytes;
+ i->array_size = array_size;
+ i->data_size = array_size*get_type_size(type);
+ }
+
+ const char *val_begin = static_cast<const char *>(value);
+ const char *val_end = val_begin+array_size*get_type_size(type);
+ char *store_begin = uniform_data.data()+i->data_offset;
+ copy(val_begin, val_end, store_begin);
+
+ mark_dirty(1<<(i-uniforms.begin()));
+}
+
+bool ProgramData::validate_tag(Tag tag) const
+{
+#ifdef DEBUG
+ try
+#endif
+ {
+ if(tied_program)
+ {
+ const ReflectData::UniformInfo &info = tied_program->get_uniform_info(tag);
+ if(is_image(info.type))
+ throw invalid_operation("ProgramData::uniform");
+ }
+ return true;
+ }
+#ifdef DEBUG
+ catch(const exception &e)
+ {
+ IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", tag, Debug::demangle(typeid(e).name()), e.what());
+ return false;
+ }
+#endif
+}
+
+void ProgramData::mark_dirty(Mask bits)
+{
+ if(!dirty)
+ {
+ if(generation && !streaming)
+ {
+ streaming = true;
+ if(buffer && buffer->get_size())
+ recreate_buffer();
+ }
+ ++generation;
+ }
+ dirty |= bits;
+}
+
+void ProgramData::uniform(Tag tag, int v)
+{
+ uniform(tag, INT, 1, &v);
+}
+
+void ProgramData::uniform(Tag tag, unsigned v)
+{
+ uniform(tag, UNSIGNED_INT, 1, &v);
+}
+
+void ProgramData::uniform(Tag tag, float v)
+{
+ uniform(tag, FLOAT, 1, &v);
+}
+
+void ProgramData::uniform(Tag tag, int v0, int v1)
+{
+ int va[2] = { v0, v1 };
+ uniform2(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1)
+{
+ unsigned va[2] = { v0, v1 };
+ uniform2(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, float v0, float v1)
+{
+ float va[2] = { v0, v1 };
+ uniform2(tag, va);
+}
+
+void ProgramData::uniform2(Tag tag, const int *v)
+{
+ uniform(tag, INT_VEC2, 1, v);
+}
+
+void ProgramData::uniform2(Tag tag, const unsigned *v)
+{
+ uniform(tag, UINT_VEC2, 1, v);
+}
+
+void ProgramData::uniform2(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_VEC2, 1, v);
+}
+
+void ProgramData::uniform(Tag tag, int v0, int v1, int v2)
+{
+ int va[3] = { v0, v1, v2 };
+ uniform3(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2)
+{
+ unsigned va[3] = { v0, v1, v2 };
+ uniform3(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, float v0, float v1, float v2)
+{
+ float va[3] = { v0, v1, v2 };
+ uniform3(tag, va);
+}
+
+void ProgramData::uniform3(Tag tag, const int *v)
+{
+ uniform(tag, INT_VEC3, 1, v);
+}
+
+void ProgramData::uniform3(Tag tag, const unsigned *v)
+{
+ uniform(tag, UINT_VEC3, 1, v);
+}
+
+void ProgramData::uniform3(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_VEC3, 1, v);
+}
+
+void ProgramData::uniform(Tag tag, int v0, int v1, int v2, int v3)
+{
+ int va[4] = { v0, v1, v2, v3 };
+ uniform4(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, unsigned v0, unsigned v1, unsigned v2, unsigned v3)
+{
+ unsigned va[4] = { v0, v1, v2, v3 };
+ uniform4(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3)
+{
+ float va[4] = { v0, v1, v2, v3 };
+ uniform4(tag, va);
+}
+
+void ProgramData::uniform(Tag tag, const Color &c)
+{
+ uniform(tag, c.r, c.g, c.b, c.a);
+}
+
+void ProgramData::uniform4(Tag tag, const int *v)
+{
+ uniform(tag, INT_VEC4, 1, v);
+}
+
+void ProgramData::uniform4(Tag tag, const unsigned *v)
+{
+ uniform(tag, UINT_VEC4, 1, v);
+}
+
+void ProgramData::uniform4(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_VEC4, 1, v);
+}
+
+void ProgramData::uniform_matrix2(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT2, 1, v);
+}
+
+void ProgramData::uniform_matrix3x2(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT3x2, 1, v);
+}
+
+void ProgramData::uniform_matrix4x2(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT4x2, 1, v);
+}
+
+void ProgramData::uniform_matrix2x3(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT2x3, 1, v);
+}
+
+void ProgramData::uniform_matrix3(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT3, 1, v);
+}
+
+void ProgramData::uniform_matrix4x3(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT4x3, 1, v);
+}
+
+void ProgramData::uniform_matrix2x4(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT2x4, 1, v);
+}
+
+void ProgramData::uniform_matrix3x4(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT3x4, 1, v);
+}
+
+void ProgramData::uniform(Tag tag, const Matrix &m)
+{
+ uniform_matrix4(tag, m.data());
+}
+
+void ProgramData::uniform_matrix4(Tag tag, const float *v)
+{
+ uniform(tag, FLOAT_MAT4, 1, v);
+}
+
+void ProgramData::uniform_array(Tag tag, unsigned n, const int *v)
+{
+ uniform(tag, INT, n, v);
+}
+
+void ProgramData::uniform_array(Tag tag, unsigned n, const unsigned *v)
+{
+ uniform(tag, UNSIGNED_INT, n, v);
+}
+
+void ProgramData::uniform_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT, n, v);
+}
+
+void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v)
+{
+ uniform(tag, INT, n, v);
+}
+
+void ProgramData::uniform1_array(Tag tag, unsigned n, const unsigned *v)
+{
+ uniform(tag, UNSIGNED_INT, n, v);
+}
+
+void ProgramData::uniform1_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT, n, v);
+}
+
+void ProgramData::uniform2_array(Tag tag, unsigned n, const int *v)
+{
+ uniform(tag, INT_VEC2, n, v);
+}
+
+void ProgramData::uniform2_array(Tag tag, unsigned n, const unsigned *v)
+{
+ uniform(tag, UINT_VEC2, n, v);
+}
+
+void ProgramData::uniform2_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_VEC2, n, v);
+}
+
+void ProgramData::uniform3_array(Tag tag, unsigned n, const int *v)
+{
+ uniform(tag, INT_VEC3, n, v);
+}
+
+void ProgramData::uniform3_array(Tag tag, unsigned n, const unsigned *v)
+{
+ uniform(tag, INT_VEC3, n, v);
+}
+
+void ProgramData::uniform3_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_VEC3, n, v);
+}
+
+void ProgramData::uniform4_array(Tag tag, unsigned n, const int *v)
+{
+ uniform(tag, INT_VEC4, n, v);
+}
+
+void ProgramData::uniform4_array(Tag tag, unsigned n, const unsigned *v)
+{
+ uniform(tag, UINT_VEC4, n, v);
+}
+
+void ProgramData::uniform4_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_VEC4, n, v);
+}
+
+void ProgramData::uniform_matrix2_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT2, n, v);
+}
+
+void ProgramData::uniform_matrix3x2_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT3x2, n, v);
+}
+
+void ProgramData::uniform_matrix4x2_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT4x2, n, v);
+}
+
+void ProgramData::uniform_matrix2x3_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT2x3, n, v);
+}
+
+void ProgramData::uniform_matrix3_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT3, n, v);
+}
+
+void ProgramData::uniform_matrix4x3_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT4x3, n, v);
+}
+
+void ProgramData::uniform_matrix2x4_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT2x4, n, v);
+}
+
+void ProgramData::uniform_matrix3x4_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT3x4, n, v);
+}
+
+void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v)
+{
+ uniform(tag, FLOAT_MAT4, n, v);
+}
+
+void ProgramData::remove_uniform(Tag tag)
+{
+ auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+ if(i==uniforms.end() || i->tag!=tag)
+ return;
+
+ uniform_data.erase(uniform_data.begin()+i->data_offset, uniform_data.begin()+i->data_offset+i->data_size);
+ for(TaggedUniform &u: uniforms)
+ if(u.data_offset>i->data_offset)
+ u.data_offset -= i->data_size;
+ uniforms.erase(i);
+
+ mark_dirty(ALL_ONES);
+}
+
+vector<Tag> ProgramData::get_uniform_tags() const
+{
+ vector<Tag> tags;
+ tags.reserve(uniforms.size());
+ for(const TaggedUniform &u: uniforms)
+ tags.push_back(u.tag);
+ return tags;
+}
+
+void ProgramData::copy_uniform(const ProgramData &source, Tag tag)
+{
+ int i = source.find_uniform_index(tag);
+ if(i<0)
+ throw key_error(tag);
+ const TaggedUniform &tu = source.uniforms[i];
+ uniform(tag, tu.type, tu.array_size, source.uniform_data.data()+tu.data_offset);
+}
+
+void ProgramData::copy_uniforms(const ProgramData &source)
+{
+ for(const TaggedUniform &u: source.uniforms)
+ uniform(u.tag, u.type, u.array_size, source.uniform_data.data()+u.data_offset);
+}
+
+int ProgramData::find_uniform_index(Tag tag) const
+{
+ 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
+{
+ ReflectData::LayoutHash prog_hash = prog.get_uniform_layout_hash();
+ auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
+ if(i!=programs.end() && i->prog_hash==prog_hash)
+ return i;
+
+ const vector<ReflectData::UniformBlockInfo> &block_infos = prog.get_uniform_blocks();
+ unsigned index = i-programs.begin();
+ programs.insert(i, 1+block_infos.size(), ProgramBlock(prog_hash));
+
+ /* Block indices may change if new shared blocks need to be inserted. Store
+ the hashes so they can be matched up later. */
+ vector<ReflectData::LayoutHash> block_hashes;
+ block_hashes.reserve(programs.size());
+ 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)
+ {
+ const ReflectData::UniformBlockInfo &info = block_infos[j];
+ block_hashes[index+1+j] = info.layout_hash;
+ programs[index+1+j].bind_point = info.bind_point;
+
+ 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));
+ update_block_uniform_indices(*k, info);
+ }
+ }
+
+ /* Reassign shared block indices from the stored hashes. */
+ for(unsigned j=0; j<programs.size(); ++j)
+ {
+ unsigned hash = block_hashes[j];
+ if(hash)
+ {
+ auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
+ programs[j].block_index = k-blocks.begin();
+ }
+ else
+ programs[j].block_index = -1;
+ }
+
+ return programs.begin()+index;
+}
+
+void ProgramData::recreate_buffer() const
+{
+ Buffer *old_buffer = buffer;
+ // Create the new buffer first to ensure it has a different address
+ buffer = new Buffer;
+ delete old_buffer;
+ if(last_buffer_block)
+ last_buffer_block->change_buffer(buffer);
+
+#ifdef DEBUG
+ if(!debug_name.empty())
+ buffer->set_debug_name(debug_name);
+#endif
+}
+
+void ProgramData::update_block_uniform_indices(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
+{
+ uint8_t *indices = block.indices.values;
+ if(info.uniforms.size()>16)
+ {
+ if(block.indices.type_flag==0xFD)
+ {
+ block.indices.dynamic.values = new uint8_t[info.uniforms.size()];
+ block.indices.type_flag = 0xFE;
+ }
+ indices = block.indices.dynamic.values;
+ }
+
+ bool any_missing = false;
+
+ block.used = 0;
+ for(unsigned i=0; i<info.uniforms.size(); ++i)
+ {
+ int j = find_uniform_index(info.uniforms[i]->tag);
+ if(j>=0)
+ {
+ indices[i] = j;
+ if(static_cast<unsigned>(j)<MASK_BITS)
+ block.used |= 1<<j;
+ }
+ else
+ {
+ indices[i] = 0xFF;
+ any_missing = true;
+ }
+ }
+
+ if(block.used && any_missing && info.bind_point>=0)
+ {
+#ifdef DEBUG
+ IO::print(IO::cerr, "Warning: not all uniforms for block %s are present\n", info.name);
+#else
+ throw incomplete_uniform_block(info.name);
+#endif
+ }
+
+ block.dirty = block.used;
+
+ if(block.used && !block.block)
+ {
+ block.block = new UniformBlock(info);
+ if(info.bind_point>=0)
+ {
+ if(!buffer)
+ recreate_buffer();
+
+ block.block->use_buffer(buffer, last_buffer_block);
+ last_buffer_block = block.block;
+ }
+ }
+}
+
+void ProgramData::update_block(SharedBlock &block, const ReflectData::UniformBlockInfo &info) const
+{
+ const uint8_t *indices = block.get_uniform_indices();
+ for(unsigned i=0; i<info.uniforms.size(); ++i)
+ {
+ if(is_image(info.uniforms[i]->type))
+ ; // Temporarily ignore deprecated use of sampler uniforms in ProgramData
+ else if(indices[i]!=0xFF)
+ {
+ const TaggedUniform &tu = uniforms[indices[i]];
+ block.block->store(*info.uniforms[i], tu.array_size, uniform_data.data()+tu.data_offset);
+ }
+ }
+}
+
+vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
+{
+ UniformBlock *old_last_block = last_buffer_block;
+ auto prog_begin = get_program(prog);
+
+ Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
+ Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
+ if(affected|prog_begin->masks.dirty)
+ {
+ /* If the global dirty flag affects this program, add it to per-block and
+ per-program dirty flags and clear the global flag. A previously unseen
+ program will cause this to happen if there's any dirty uniforms. */
+ if(affected)
+ {
+ 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<ReflectData::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;
+ auto j = prog_begin+1;
+ for(const ReflectData::UniformBlockInfo &b: block_infos)
+ {
+ SharedBlock &shared = blocks[j->block_index];
+ if(shared.dirty==ALL_ONES)
+ 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;
+ auto j = prog_begin+1;
+ for(const ReflectData::UniformBlockInfo &b: block_infos)
+ {
+ SharedBlock &shared = blocks[j->block_index];
+ if(shared.dirty)
+ {
+ update_block(shared, b);
+ shared.dirty = 0;
+ buffered_blocks_updated |= (j->bind_point>=0);
+ }
+ ++j;
+ }
+
+ prog_begin->masks.dirty = 0;
+
+ if(last_buffer_block!=old_last_block || (buffer && !buffer->get_size()))
+ {
+ unsigned required_size = last_buffer_block->get_required_buffer_size(streaming);
+ if(last_buffer_block->get_required_buffer_size()>buffer->get_size())
+ {
+ if(buffer->get_size()>0)
+ recreate_buffer();
+
+ buffer->storage(required_size, (streaming ? STREAMING : STATIC));
+ }
+ }
+ }
+
+ return prog_begin;
+}
+
+void ProgramData::apply(const Program &prog, PipelineState &state, unsigned frame) const
+{
+ auto prog_begin = prepare_program(prog);
+ ReflectData::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)
+ {
+ state.set_uniform_block(i->bind_point, i->block);
+ if(i->bind_point>=0)
+ i->block->refresh(frame);
+ }
+}
+
+void ProgramData::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+ debug_name = name;
+ if(buffer)
+ buffer->set_debug_name(name);
+#else
+ (void)name;
+#endif
+}
+
+
+ProgramData::SharedBlock::SharedBlock(ReflectData::LayoutHash h):
+ block_hash(h),
+ used(0),
+ dirty(0),
+ block(0)
+{
+ indices.type_flag = 0xFD;
+}
+
+const uint8_t *ProgramData::SharedBlock::get_uniform_indices() const
+{
+ return (indices.type_flag==0xFE ? indices.dynamic.values : indices.values);
+}
+
+
+ProgramData::ProgramBlock::ProgramBlock(ReflectData::LayoutHash h):
+ prog_hash(h),
+ bind_point(-1),
+ block_index(-1)
+{
+ masks.used = ALL_ONES;
+ masks.dirty = ALL_ONES;
+}
+
+
+ProgramData::Loader::Loader(ProgramData &pd):
+ DataFile::ObjectLoader<ProgramData>(pd)
+{
+ add("uniform", &Loader::uniform1i);
+ add("uniform1i", &Loader::uniform1i);
+ add("uniform", &Loader::uniform1f);
+ add("uniform1f", &Loader::uniform1f);
+ add("uniform", &Loader::uniform2i);
+ add("uniform2i", &Loader::uniform2i);
+ add("uniform", &Loader::uniform2f);
+ add("uniform2f", &Loader::uniform2f);
+ add("uniform", &Loader::uniform3i);
+ add("uniform3i", &Loader::uniform3i);
+ add("uniform", &Loader::uniform3f);
+ add("uniform3f", &Loader::uniform3f);
+ add("uniform", &Loader::uniform4i);
+ add("uniform4i", &Loader::uniform4i);
+ add("uniform", &Loader::uniform4f);
+ add("uniform4f", &Loader::uniform4f);
+ add("uniform1i_array", &Loader::uniform1i_array);
+ add("uniform1f_array", &Loader::uniform1f_array);
+ add("uniform2f_array", &Loader::uniform2f_array);
+ add("uniform3f_array", &Loader::uniform3f_array);
+ add("uniform4f_array", &Loader::uniform4f_array);
+ add("uniform_array", &Loader::uniform_array);
+}
+
+void ProgramData::Loader::uniform1i(const string &n, int v)
+{
+ obj.uniform(n, v);
+}
+
+void ProgramData::Loader::uniform1f(const string &n, float v)
+{
+ obj.uniform(n, v);
+}
+
+void ProgramData::Loader::uniform2i(const string &n, int v0, int v1)
+{
+ obj.uniform(n, v0, v1);
+}
+
+void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
+{
+ obj.uniform(n, v0, v1);
+}
+
+void ProgramData::Loader::uniform3i(const string &n, int v0, int v1, int v2)
+{
+ obj.uniform(n, v0, v1, v2);
+}
+
+void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
+{
+ obj.uniform(n, v0, v1, v2);
+}
+
+void ProgramData::Loader::uniform4i(const string &n, int v0, int v1, int v2, int v3)
+{
+ obj.uniform(n, v0, v1, v2, v3);
+}
+
+void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
+{
+ obj.uniform(n, v0, v1, v2, v3);
+}
+
+void ProgramData::Loader::uniform_array_(const string &n, DataType t, unsigned e)
+{
+ ArrayLoader ldr(t, e);
+ load_sub_with(ldr);
+ unsigned size = ldr.get_size();
+ if(!size)
+ throw logic_error("empty uniform array");
+
+ DataType type = ldr.get_data_type();
+ unsigned elem_size = ldr.get_element_size();
+ if(type==INT)
+ {
+ const int *data = reinterpret_cast<const int *>(ldr.get_data());
+ if(elem_size==1)
+ obj.uniform1_array(n, size, data);
+ else if(elem_size==2)
+ obj.uniform2_array(n, size, data);
+ else if(elem_size==3)
+ obj.uniform3_array(n, size, data);
+ else if(elem_size==4)
+ obj.uniform4_array(n, size, data);
+ else
+ throw logic_error("unsupported combination of array type and element size");
+ }
+ else if(type==FLOAT)
+ {
+ const float *data = reinterpret_cast<const float *>(ldr.get_data());
+ if(elem_size==1)
+ obj.uniform1_array(n, size, data);
+ else if(elem_size==2)
+ obj.uniform2_array(n, size, data);
+ else if(elem_size==3)
+ obj.uniform3_array(n, size, data);
+ else if(elem_size==4)
+ obj.uniform4_array(n, size, data);
+ else
+ throw logic_error("unsupported combination of array type and element size");
+ }
+ else
+ throw logic_error("unsupported array type");
+}
+
+void ProgramData::Loader::uniform1i_array(const string &n)
+{
+ uniform_array_(n, INT, 1);
+}
+
+void ProgramData::Loader::uniform1f_array(const string &n)
+{
+ uniform_array_(n, FLOAT, 1);
+}
+
+void ProgramData::Loader::uniform2i_array(const string &n)
+{
+ uniform_array_(n, INT, 2);
+}
+
+void ProgramData::Loader::uniform2f_array(const string &n)
+{
+ uniform_array_(n, FLOAT, 2);
+}
+
+void ProgramData::Loader::uniform3i_array(const string &n)
+{
+ uniform_array_(n, INT, 3);
+}
+
+void ProgramData::Loader::uniform3f_array(const string &n)
+{
+ uniform_array_(n, FLOAT, 3);
+}
+
+void ProgramData::Loader::uniform4i_array(const string &n)
+{
+ uniform_array_(n, INT, 4);
+}
+
+void ProgramData::Loader::uniform4f_array(const string &n)
+{
+ uniform_array_(n, FLOAT, 4);
+}
+
+void ProgramData::Loader::uniform_array(const string &n)
+{
+ uniform_array_(n, static_cast<DataType>(0), 0);
+}
+
+
+ProgramData::ArrayLoader::ArrayLoader(DataType t, unsigned e):
+ type(t),
+ element_size(e)
+{
+ add("uniform", &ArrayLoader::uniform1i);
+ add("uniform1i", &ArrayLoader::uniform1i);
+ add("uniform", &ArrayLoader::uniform1f);
+ add("uniform1f", &ArrayLoader::uniform1f);
+ add("uniform", &ArrayLoader::uniform2f);
+ add("uniform2f", &ArrayLoader::uniform2f);
+ add("uniform", &ArrayLoader::uniform3f);
+ add("uniform3f", &ArrayLoader::uniform3f);
+ add("uniform", &ArrayLoader::uniform4f);
+ add("uniform4f", &ArrayLoader::uniform4f);
+}
+
+void ProgramData::ArrayLoader::uniform(DataType t, unsigned e, const void *v)
+{
+ if(element_size && (t!=type || e!=element_size))
+ throw logic_error("heterogeneous array contents");
+
+ if(!element_size)
+ {
+ type = t;
+ element_size = e;
+ }
+
+ const char *cv = reinterpret_cast<const char *>(v);
+ data.insert(data.end(), cv, cv+element_size*4);
+}
+
+void ProgramData::ArrayLoader::uniform1i(int v)
+{
+ uniform(INT, 1, &v);
+}
+
+void ProgramData::ArrayLoader::uniform1f(float v)
+{
+ uniform(FLOAT, 1, &v);
+}
+
+void ProgramData::ArrayLoader::uniform2i(int v0, int v1)
+{
+ int va[2] = { v0, v1 };
+ uniform(INT, 2, va);
+}
+
+void ProgramData::ArrayLoader::uniform2f(float v0, float v1)
+{
+ float va[2] = { v0, v1 };
+ uniform(FLOAT, 2, va);
+}
+
+void ProgramData::ArrayLoader::uniform3i(int v0, int v1, int v2)
+{
+ int va[3] = { v0, v1, v2 };
+ uniform(INT, 3, va);
+}
+
+void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2)
+{
+ float va[3] = { v0, v1, v2 };
+ uniform(FLOAT, 3, va);
+}
+
+void ProgramData::ArrayLoader::uniform4i(int v0, int v1, int v2, int v3)
+{
+ int va[4] = { v0, v1, v2, v3 };
+ uniform(INT, 4, va);
+}
+
+void ProgramData::ArrayLoader::uniform4f(float v0, float v1, float v2, float v3)
+{
+ float va[4] = { v0, v1, v2, v3 };
+ uniform(FLOAT, 4, va);
+}
+
+} // namespace GL
+} // namespace Msp