X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Frender%2Fprogramdata.h;h=f1c5996eb910e3e51368571472e94e6902d12bec;hp=e5c3e4f5259be76a3965f052e077a67bcaba1d34;hb=HEAD;hpb=5bb193f930fb8738d099d630c4d625d82c1215b5 diff --git a/source/render/programdata.h b/source/render/programdata.h deleted file mode 100644 index e5c3e4f5..00000000 --- a/source/render/programdata.h +++ /dev/null @@ -1,272 +0,0 @@ -#ifndef MSP_GL_PROGRAMDATA_H_ -#define MSP_GL_PROGRAMDATA_H_ - -#include -#include -#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 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. - -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: - class Loader: public DataFile::ObjectLoader - { - 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 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(-1) - }; - - struct TaggedUniform - { - Tag tag; - DataType type; - unsigned array_size; - unsigned data_offset; - unsigned data_size; - - TaggedUniform(); - }; - - 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 uniforms; - std::vector uniform_data; - unsigned generation; - mutable std::vector blocks; - mutable std::vector programs; - mutable UniformBlock *last_buffer_block; - mutable Buffer *buffer; - mutable Mask dirty; - std::string debug_name; - -public: - ProgramData(const Program * = 0); - ProgramData(const ProgramData &); - ProgramData(const ProgramData &, const Program *); - ProgramData &operator=(const 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, float); - void uniform(Tag, int, int); - void uniform(Tag, float, float); - void uniform2(Tag, const int *); - void uniform2(Tag, const float *); - void uniform(Tag, int, int, int); - void uniform(Tag, float, float, float); - void uniform3(Tag, const int *); - void uniform3(Tag, const float *); - void uniform(Tag, int, int, int, int); - void uniform(Tag, float, float, float, float); - void uniform(Tag, const Color &); - void uniform4(Tag, const int *); - 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 float *); - void uniform1_array(Tag, unsigned, const int *); - void uniform1_array(Tag, unsigned, const float *); - void uniform2_array(Tag, unsigned, const int *); - void uniform2_array(Tag, unsigned, const float *); - void uniform3_array(Tag, unsigned, const int *); - void uniform3_array(Tag, unsigned, const float *); - void uniform4_array(Tag, unsigned, const int *); - 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 - void uniform(Tag, const LinAl::Vector &); - - template - void uniform(Tag, const LinAl::Matrix &); - - template - void uniform_array(Tag, unsigned, const LinAl::Vector *); - - template - void uniform_array(Tag, unsigned, const LinAl::Matrix *); - - void remove_uniform(Tag); - - unsigned get_generation() const { return generation; } - - std::vector get_uniform_tags() const; - void copy_uniform(const ProgramData &, Tag); - -private: - int find_uniform_index(Tag) const; - std::vector::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::const_iterator prepare_program(const Program &) const; -public: - void apply(const Program &, PipelineState &) const; - - void set_debug_name(const std::string &); -}; - -template -void ProgramData::uniform(Tag tag, const LinAl::Vector &v) -{ uniform(tag, TypeTraits>::type, 1, &v.x); } - -template -void ProgramData::uniform(Tag tag, const LinAl::Matrix &v) -{ uniform(tag, TypeTraits>::type, 1, &v(0, 0)); } - -template -void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector *v) -{ uniform(tag, TypeTraits>::type, n, &v[0].x); } - -template -void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix *v) -{ uniform(tag, TypeTraits>::type, n, &v[0](0, 0)); } - -} // namespace GL -} // namespace Msp - -#endif