]> git.tdb.fi Git - libs/gl.git/blobdiff - source/materials/programdata.h
Move ProgramData to materials
[libs/gl.git] / source / materials / programdata.h
diff --git a/source/materials/programdata.h b/source/materials/programdata.h
new file mode 100644 (file)
index 0000000..dbbf5f4
--- /dev/null
@@ -0,0 +1,287 @@
+#ifndef MSP_GL_PROGRAMDATA_H_
+#define MSP_GL_PROGRAMDATA_H_
+
+#include <stdexcept>
+#include <msp/core/noncopyable.h>
+#include <msp/datafile/objectloader.h>
+#include "datatype.h"
+#include "matrix.h"
+#include "reflectdata.h"
+#include "tag.h"
+#include "vector.h"
+
+namespace Msp {
+namespace GL {
+
+class too_many_uniforms: public std::runtime_error
+{
+public:
+       too_many_uniforms(const std::string &w): std::runtime_error(w) { }
+       virtual ~too_many_uniforms() throw() { }
+};
+
+class Buffer;
+class PipelineState;
+class Program;
+class UniformBlock;
+struct Color;
+
+/**
+Stores uniform values for shader programs.
+
+The uniforms are stored in a program-independent way, and UniformBlocks are
+created to match the uniform layouts of different programs.  If multiple
+programs have the same layout, the same block is used for them.
+
+The class is optimized for an access pattern where the set of uniforms and
+programs stays constants, with only the values changing.
+*/
+class ProgramData: public NonCopyable
+{
+public:
+       class Loader: public DataFile::ObjectLoader<ProgramData>
+       {
+       public:
+               Loader(ProgramData &);
+       private:
+               void uniform1i(const std::string &, int);
+               void uniform1f(const std::string &, float);
+               void uniform2i(const std::string &, int, int);
+               void uniform2f(const std::string &, float, float);
+               void uniform3i(const std::string &, int, int, int);
+               void uniform3f(const std::string &, float, float, float);
+               void uniform4i(const std::string &, int, int, int, int);
+               void uniform4f(const std::string &, float, float, float, float);
+               void uniform_array_(const std::string &, DataType, unsigned);
+               void uniform1i_array(const std::string &);
+               void uniform1f_array(const std::string &);
+               void uniform2i_array(const std::string &);
+               void uniform2f_array(const std::string &);
+               void uniform3i_array(const std::string &);
+               void uniform3f_array(const std::string &);
+               void uniform4i_array(const std::string &);
+               void uniform4f_array(const std::string &);
+               void uniform_array(const std::string &);
+       };
+
+private:
+       class ArrayLoader: public DataFile::Loader
+       {
+       private:
+               DataType type;
+               unsigned element_size;
+               std::vector<char> data;
+
+       public:
+               ArrayLoader(DataType, unsigned);
+
+               DataType get_data_type() const { return type; }
+               unsigned get_element_size() const { return element_size; }
+               const void *get_data() const { return &data[0]; }
+               unsigned get_size() const { return data.size()/(4*element_size); }
+
+       private:
+               void uniform(DataType, unsigned, const void *);
+               void uniform1i(int);
+               void uniform1f(float);
+               void uniform2i(int, int);
+               void uniform2f(float, float);
+               void uniform3i(int, int, int);
+               void uniform3f(float, float, float);
+               void uniform4i(int, int, int, int);
+               void uniform4f(float, float, float, float);
+       };
+
+       typedef unsigned Mask;
+
+       enum
+       {
+               MASK_BITS = sizeof(Mask)*8,
+               ALL_ONES = static_cast<Mask>(-1)
+       };
+
+       struct TaggedUniform
+       {
+               Tag tag;
+               DataType type = VOID;
+               unsigned array_size = 0;
+               unsigned data_offset = 0;
+               unsigned data_size = 0;
+       };
+
+       struct SharedBlock
+       {
+               ReflectData::LayoutHash block_hash;
+               Mask used;
+               Mask dirty;
+               UniformBlock *block;
+               union
+               {
+                       std::uint8_t type_flag;
+                       std::uint8_t values[16];
+                       struct
+                       {
+                               std::uint8_t type_flag;
+                               std::uint8_t *values;
+                       } dynamic;
+               } indices;
+
+               SharedBlock(ReflectData::LayoutHash);
+
+               const std::uint8_t *get_uniform_indices() const;
+       };
+
+       struct ProgramBlock
+       {
+               ReflectData::LayoutHash prog_hash;
+               int bind_point;
+               int block_index;
+               union
+               {
+                       UniformBlock *block;
+                       struct
+                       {
+                               Mask used;
+                               Mask dirty;
+                       } masks;
+               };
+
+               ProgramBlock(ReflectData::LayoutHash);
+       };
+
+       // XXX All these mutables are a bit silly, but I'm out of better ideas
+       const Program *tied_program;
+       std::vector<TaggedUniform> uniforms;
+       std::vector<char> uniform_data;
+       unsigned generation = 0;
+       mutable std::vector<SharedBlock> blocks;
+       mutable std::vector<ProgramBlock> programs;
+       mutable UniformBlock *last_buffer_block = 0;
+       mutable Buffer *buffer = 0;
+       bool streaming = false;
+       mutable Mask dirty = 0;
+       std::string debug_name;
+
+public:
+       ProgramData(const Program * = 0);
+       ProgramData(ProgramData &&);
+       ~ProgramData();
+
+private:
+       void uniform(Tag, DataType, unsigned, const void *);
+       bool validate_tag(Tag) const;
+       void mark_dirty(Mask);
+public:
+       void uniform(Tag, int);
+       void uniform(Tag, unsigned);
+       void uniform(Tag, float);
+       void uniform(Tag, int, int);
+       void uniform(Tag, unsigned, unsigned);
+       void uniform(Tag, float, float);
+       void uniform2(Tag, const int *);
+       void uniform2(Tag, const unsigned *);
+       void uniform2(Tag, const float *);
+       void uniform(Tag, int, int, int);
+       void uniform(Tag, unsigned, unsigned, unsigned);
+       void uniform(Tag, float, float, float);
+       void uniform3(Tag, const int *);
+       void uniform3(Tag, const unsigned *);
+       void uniform3(Tag, const float *);
+       void uniform(Tag, int, int, int, int);
+       void uniform(Tag, unsigned, unsigned, unsigned, unsigned);
+       void uniform(Tag, float, float, float, float);
+       void uniform(Tag, const Color &);
+       void uniform4(Tag, const int *);
+       void uniform4(Tag, const unsigned *);
+       void uniform4(Tag, const float *);
+       void uniform_matrix2(Tag, const float *);
+       void uniform_matrix3x2(Tag, const float *);
+       void uniform_matrix4x2(Tag, const float *);
+       void uniform_matrix2x3(Tag, const float *);
+       void uniform_matrix3(Tag, const float *);
+       void uniform_matrix4x3(Tag, const float *);
+       void uniform_matrix2x4(Tag, const float *);
+       void uniform_matrix3x4(Tag, const float *);
+       void uniform(Tag, const Matrix &);
+       void uniform_matrix4(Tag, const float *);
+       void uniform_array(Tag, unsigned, const int *);
+       void uniform_array(Tag, unsigned, const unsigned *);
+       void uniform_array(Tag, unsigned, const float *);
+       void uniform1_array(Tag, unsigned, const int *);
+       void uniform1_array(Tag, unsigned, const unsigned *);
+       void uniform1_array(Tag, unsigned, const float *);
+       void uniform2_array(Tag, unsigned, const int *);
+       void uniform2_array(Tag, unsigned, const unsigned *);
+       void uniform2_array(Tag, unsigned, const float *);
+       void uniform3_array(Tag, unsigned, const int *);
+       void uniform3_array(Tag, unsigned, const unsigned *);
+       void uniform3_array(Tag, unsigned, const float *);
+       void uniform4_array(Tag, unsigned, const int *);
+       void uniform4_array(Tag, unsigned, const unsigned *);
+       void uniform4_array(Tag, unsigned, const float *);
+       void uniform_matrix2_array(Tag, unsigned, const float *);
+       void uniform_matrix3x2_array(Tag, unsigned, const float *);
+       void uniform_matrix4x2_array(Tag, unsigned, const float *);
+       void uniform_matrix2x3_array(Tag, unsigned, const float *);
+       void uniform_matrix3_array(Tag, unsigned, const float *);
+       void uniform_matrix4x3_array(Tag, unsigned, const float *);
+       void uniform_matrix2x4_array(Tag, unsigned, const float *);
+       void uniform_matrix3x4_array(Tag, unsigned, const float *);
+       void uniform_matrix4_array(Tag, unsigned, const float *);
+
+       template<typename T, unsigned N>
+       void uniform(Tag, const LinAl::Vector<T, N> &);
+
+       template<typename T, unsigned R, unsigned C>
+       void uniform(Tag, const LinAl::Matrix<T, R, C> &);
+
+       template<typename T, unsigned N>
+       void uniform_array(Tag, unsigned, const LinAl::Vector<T, N> *);
+
+       template<typename T, unsigned R, unsigned C>
+       void uniform_array(Tag, unsigned, const LinAl::Matrix<T, R, C> *);
+
+       void remove_uniform(Tag);
+
+       unsigned get_generation() const { return generation; }
+
+       std::vector<Tag> get_uniform_tags() const;
+       void copy_uniform(const ProgramData &, Tag);
+       void copy_uniforms(const ProgramData &);
+
+private:
+       int find_uniform_index(Tag) const;
+       std::vector<ProgramBlock>::iterator get_program(const Program &) const;
+       void recreate_buffer() const;
+       void update_block_uniform_indices(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
+       void update_block(SharedBlock &, const ReflectData::UniformBlockInfo &) const;
+
+       std::vector<ProgramBlock>::const_iterator prepare_program(const Program &) const;
+public:
+       /** Creates or updates UniformBlocks for a specific program if necessary,
+       then sets them to the PipelineState. */
+       void apply(const Program &, PipelineState &, unsigned) const;
+
+       void set_debug_name(const std::string &);
+};
+
+template<typename T, unsigned N>
+void ProgramData::uniform(Tag tag, const LinAl::Vector<T, N> &v)
+{ uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, 1, &v.x); }
+
+template<typename T, unsigned R, unsigned C>
+void ProgramData::uniform(Tag tag, const LinAl::Matrix<T, R, C> &v)
+{ uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, 1, &v(0, 0)); }
+
+template<typename T, unsigned N>
+void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector<T, N> *v)
+{ uniform(tag, TypeTraits<LinAl::Vector<T, N>>::type, n, &v[0].x); }
+
+template<typename T, unsigned R, unsigned C>
+void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix<T, R, C> *v)
+{ uniform(tag, TypeTraits<LinAl::Matrix<T, R, C>>::type, n, &v[0](0, 0)); }
+
+} // namespace GL
+} // namespace Msp
+
+#endif