Cpp11 initializers: last src root changeset (#6022)
[oweals/minetest.git] / src / shader.h
index b8aa88bce17f92db0c8f66fccb914bff89cfafb7..979318c95a96990a7215722910da945e408a2813 100644 (file)
@@ -21,8 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef SHADER_HEADER
 #define SHADER_HEADER
 
+#include <IMaterialRendererServices.h>
 #include "irrlichttypes_extrabloated.h"
-#include "threads.h"
 #include <string>
 
 class IGameDef;
@@ -43,18 +43,14 @@ class IGameDef;
 std::string getShaderPath(const std::string &name_of_shader,
                const std::string &filename);
 
-struct ShaderInfo
-{
-       std::string name;
-       video::E_MATERIAL_TYPE base_material;
-       video::E_MATERIAL_TYPE material;
-       u8 drawtype;
-       u8 material_type;
-       s32 user_data;
-
-       ShaderInfo(): name(""), base_material(video::EMT_SOLID),
-               material(video::EMT_SOLID),
-               drawtype(0), material_type(0) {}
+struct ShaderInfo {
+       std::string name = "";
+       video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
+       video::E_MATERIAL_TYPE material = video::EMT_SOLID;
+       u8 drawtype = 0;
+       u8 material_type = 0;
+
+       ShaderInfo() {}
        virtual ~ShaderInfo() {}
 };
 
@@ -66,20 +62,66 @@ namespace irr { namespace video {
        class IMaterialRendererServices;
 } }
 
-class IShaderConstantSetter
-{
+
+class IShaderConstantSetter {
 public:
        virtual ~IShaderConstantSetter(){};
        virtual void onSetConstants(video::IMaterialRendererServices *services,
                        bool is_highlevel) = 0;
 };
 
+
+class IShaderConstantSetterFactory {
+public:
+       virtual ~IShaderConstantSetterFactory() {};
+       virtual IShaderConstantSetter* create() = 0;
+};
+
+
+template <typename T, std::size_t count=1>
+class CachedShaderSetting {
+       const char *m_name;
+       T m_sent[count];
+       bool has_been_set = false;
+       bool is_pixel;
+protected:
+       CachedShaderSetting(const char *name, bool is_pixel) :
+               m_name(name), is_pixel(is_pixel)
+       {}
+public:
+       void set(const T value[count], video::IMaterialRendererServices *services)
+       {
+               if (has_been_set && std::equal(m_sent, m_sent + count, value))
+                       return;
+               if (is_pixel)
+                       services->setPixelShaderConstant(m_name, value, count);
+               else
+                       services->setVertexShaderConstant(m_name, value, count);
+               std::copy(value, value + count, m_sent);
+               has_been_set = true;
+       }
+};
+
+template <typename T, std::size_t count = 1>
+class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
+public:
+       CachedPixelShaderSetting(const char *name) :
+               CachedShaderSetting<T, count>(name, true){}
+};
+
+template <typename T, std::size_t count = 1>
+class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
+public:
+       CachedVertexShaderSetting(const char *name) :
+               CachedShaderSetting<T, count>(name, false){}
+};
+
+
 /*
        ShaderSource creates and caches shaders.
 */
 
-class IShaderSource
-{
+class IShaderSource {
 public:
        IShaderSource(){}
        virtual ~IShaderSource(){}
@@ -90,8 +132,7 @@ public:
                const u8 material_type, const u8 drawtype){return 0;}
 };
 
-class IWritableShaderSource : public IShaderSource
-{
+class IWritableShaderSource : public IShaderSource {
 public:
        IWritableShaderSource(){}
        virtual ~IWritableShaderSource(){}
@@ -105,7 +146,7 @@ public:
        virtual void insertSourceShader(const std::string &name_of_shader,
                const std::string &filename, const std::string &program)=0;
        virtual void rebuildShaders()=0;
-       virtual void addGlobalConstantSetter(IShaderConstantSetter *setter)=0;
+       virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
 };
 
 IWritableShaderSource* createShaderSource(IrrlichtDevice *device);