From 9afdf8a0813de5c4d5277b0ccbfe86051af5624e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 12 Sep 2016 15:08:35 +0300 Subject: [PATCH] Support loading uniform arrays from data files --- source/programdata.cpp | 132 +++++++++++++++++++++++++++++++++++++++++ source/programdata.h | 32 ++++++++++ 2 files changed, 164 insertions(+) diff --git a/source/programdata.cpp b/source/programdata.cpp index 9b27af50..d0de3c05 100644 --- a/source/programdata.cpp +++ b/source/programdata.cpp @@ -376,6 +376,12 @@ ProgramData::Loader::Loader(ProgramData &pd): add("uniform3f", &Loader::uniform3f); 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) @@ -403,5 +409,131 @@ void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v 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(ldr.get_data()); + if(elem_size==1) + obj.uniform1_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(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::uniform2f_array(const string &n) +{ + uniform_array_(n, FLOAT, 2); +} + +void ProgramData::Loader::uniform3f_array(const string &n) +{ + uniform_array_(n, FLOAT, 3); +} + +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(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(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::uniform2f(float v0, float v1) +{ + float va[2] = { v0, v1 }; + uniform(FLOAT, 2, va); +} + +void ProgramData::ArrayLoader::uniform3f(float v0, float v1, float v2) +{ + float va[3] = { v0, v1, v2 }; + uniform(FLOAT, 3, 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 diff --git a/source/programdata.h b/source/programdata.h index 6e4b53fe..07e1bb64 100644 --- a/source/programdata.h +++ b/source/programdata.h @@ -3,6 +3,7 @@ #include #include +#include "datatype.h" #include "matrix.h" #include "program.h" #include "vector.h" @@ -37,9 +38,40 @@ public: void uniform2f(const std::string &, float, float); void uniform3f(const std::string &, float, float, float); 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 uniform2f_array(const std::string &); + void uniform3f_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 uniform2f(float, float); + void uniform3f(float, float, float); + void uniform4f(float, float, float, float); + }; + typedef unsigned Mask; enum -- 2.45.2