]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/programdata.h
Move WindowView::render to the backend
[libs/gl.git] / source / render / programdata.h
index 97f293430c1c718e06f594d84ddf0d2de1a84263..9b8a25cb15be498a916bf0ffcf28c868ea56bb02 100644 (file)
@@ -1,12 +1,11 @@
 #ifndef MSP_GL_PROGRAMDATA_H_
 #define MSP_GL_PROGRAMDATA_H_
 
-#include <map>
 #include <stdexcept>
 #include <msp/datafile/objectloader.h>
 #include "datatype.h"
 #include "matrix.h"
-#include "program.h"
+#include "reflectdata.h"
 #include "tag.h"
 #include "vector.h"
 
@@ -21,15 +20,17 @@ public:
 };
 
 class Buffer;
-class Uniform;
+class PipelineState;
+class Program;
 class UniformBlock;
 struct Color;
 
 /**
-Stores uniform variables 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.
+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.
@@ -101,64 +102,63 @@ private:
        struct TaggedUniform
        {
                Tag tag;
-               Uniform *value;
-
-               TaggedUniform();
-
-               void replace_value(Uniform *);
+               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
                {
-                       UInt8 type_flag;
-                       UInt8 values[16];
+                       std::uint8_t type_flag;
+                       std::uint8_t values[16];
                        struct
                        {
-                               UInt8 type_flag;
-                               UInt8 *values;
+                               std::uint8_t type_flag;
+                               std::uint8_t *values;
                        } dynamic;
                } indices;
 
-               SharedBlock(UniformBlock *);
+               SharedBlock(ReflectData::LayoutHash);
 
-               const UInt8 *get_uniform_indices() const;
+               const std::uint8_t *get_uniform_indices() const;
        };
 
        struct ProgramBlock
        {
+               ReflectData::LayoutHash prog_hash;
                int bind_point;
-               UniformBlock *block;
-               SharedBlock *shared;
-
-               ProgramBlock();
-               ProgramBlock(int, SharedBlock *);
-       };
-
-       struct ProgramUniforms
-       {
-               std::vector<ProgramBlock> blocks;
-               Mask used;
-               Mask dirty;
+               int block_index;
+               union
+               {
+                       UniformBlock *block;
+                       struct
+                       {
+                               Mask used;
+                               Mask dirty;
+                       } masks;
+               };
 
-               ProgramUniforms();
+               ProgramBlock(ReflectData::LayoutHash);
        };
 
-       typedef std::map<Program::LayoutHash, SharedBlock> BlockMap;
-       typedef std::map<Program::LayoutHash, ProgramUniforms> ProgramMap;
-
        // XXX All these mutables are a bit silly, but I'm out of better ideas
        const Program *tied_program;
        std::vector<TaggedUniform> uniforms;
-       mutable BlockMap blocks;
-       mutable ProgramMap programs;
-       mutable UniformBlock *last_block;
-       mutable Buffer *buffer;
-       mutable Mask dirty;
+       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;
+       mutable Mask dirty = 0;
+       std::string debug_name;
 
 public:
        ProgramData(const Program * = 0);
@@ -168,57 +168,56 @@ public:
        ~ProgramData();
 
 private:
-       void uniform(Tag, Uniform *);
-       template<typename T, typename V>
-       void uniform(Tag, V);
-       template<typename T, typename V>
-       void uniform_array(Tag, unsigned, V);
+       void uniform(Tag, DataType, unsigned, const void *);
        bool validate_tag(Tag) const;
-       void add_uniform(Tag, Uniform *);
+       void mark_dirty(Mask);
 public:
-       void uniform(Tag, const Uniform &);
        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 uniform(Tag, const Vector3 &);
        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 Vector4 &);
        void uniform(Tag, const Color &);
        void uniform4(Tag, const int *);
+       void uniform4(Tag, const unsigned *);
        void uniform4(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 2, 2> &);
        void uniform_matrix2(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 2, 3> &);
        void uniform_matrix3x2(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 2, 4> &);
        void uniform_matrix4x2(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 3, 2> &);
        void uniform_matrix2x3(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 3, 3> &);
        void uniform_matrix3(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 3, 4> &);
        void uniform_matrix4x3(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 4, 2> &);
        void uniform_matrix2x4(Tag, const float *);
-       void uniform(Tag, const LinAl::Matrix<float, 4, 3> &);
        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 *);
@@ -229,24 +228,57 @@ public:
        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;
-       const Uniform &get_uniform(Tag) const;
-       const Uniform *find_uniform(Tag) const;
+       void copy_uniform(const ProgramData &, Tag);
 
 private:
        int find_uniform_index(Tag) const;
-       void update_block_uniform_indices(SharedBlock &, const Program::UniformBlockInfo &) const;
-       void update_block(SharedBlock &, const Program::UniformBlockInfo &) const;
-       SharedBlock *get_shared_block(const Program::UniformBlockInfo &) const;
+       std::vector<ProgramBlock>::iterator get_program(const Program &) 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:
-       /** Applies uniform blocks for the currently bound program, creating them
-       if needed. */
-       void apply() const;
+       /** Creates or updates UniformBlocks for a specific program if necessary,
+       then sets them to the PipelineState. */
+       void apply(const Program &, PipelineState &) 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