Move KeyList & InputHandler from game.h to client/inputhandler.h (#5752)
[oweals/minetest.git] / src / shader.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Kahrl <kahrl@gmx.net>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #ifndef SHADER_HEADER
22 #define SHADER_HEADER
23
24 #include <IMaterialRendererServices.h>
25 #include "irrlichttypes_extrabloated.h"
26 #include "threads.h"
27 #include <string>
28
29 class IGameDef;
30
31 /*
32         shader.{h,cpp}: Shader handling stuff.
33 */
34
35 /*
36         Gets the path to a shader by first checking if the file
37           name_of_shader/filename
38         exists in shader_path and if not, using the data path.
39
40         If not found, returns "".
41
42         Utilizes a thread-safe cache.
43 */
44 std::string getShaderPath(const std::string &name_of_shader,
45                 const std::string &filename);
46
47 struct ShaderInfo {
48         std::string name;
49         video::E_MATERIAL_TYPE base_material;
50         video::E_MATERIAL_TYPE material;
51         u8 drawtype;
52         u8 material_type;
53         s32 user_data;
54
55         ShaderInfo(): name(""), base_material(video::EMT_SOLID),
56                 material(video::EMT_SOLID),
57                 drawtype(0), material_type(0) {}
58         virtual ~ShaderInfo() {}
59 };
60
61 /*
62         Setter of constants for shaders
63 */
64
65 namespace irr { namespace video {
66         class IMaterialRendererServices;
67 } }
68
69
70 class IShaderConstantSetter {
71 public:
72         virtual ~IShaderConstantSetter(){};
73         virtual void onSetConstants(video::IMaterialRendererServices *services,
74                         bool is_highlevel) = 0;
75 };
76
77
78 class IShaderConstantSetterFactory {
79 public:
80         virtual ~IShaderConstantSetterFactory() {};
81         virtual IShaderConstantSetter* create() = 0;
82 };
83
84
85 template <typename T, std::size_t count=1>
86 class CachedShaderSetting {
87         const char *m_name;
88         T m_sent[count];
89         bool has_been_set;
90         bool is_pixel;
91 protected:
92         CachedShaderSetting(const char *name, bool is_pixel) :
93                 m_name(name), has_been_set(false), is_pixel(is_pixel)
94         {}
95 public:
96         void set(const T value[count], video::IMaterialRendererServices *services)
97         {
98                 if (has_been_set && std::equal(m_sent, m_sent + count, value))
99                         return;
100                 if (is_pixel)
101                         services->setPixelShaderConstant(m_name, value, count);
102                 else
103                         services->setVertexShaderConstant(m_name, value, count);
104                 std::copy(value, value + count, m_sent);
105                 has_been_set = true;
106         }
107 };
108
109 template <typename T, std::size_t count = 1>
110 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
111 public:
112         CachedPixelShaderSetting(const char *name) :
113                 CachedShaderSetting<T, count>(name, true){}
114 };
115
116 template <typename T, std::size_t count = 1>
117 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
118 public:
119         CachedVertexShaderSetting(const char *name) :
120                 CachedShaderSetting<T, count>(name, false){}
121 };
122
123
124 /*
125         ShaderSource creates and caches shaders.
126 */
127
128 class IShaderSource {
129 public:
130         IShaderSource(){}
131         virtual ~IShaderSource(){}
132         virtual u32 getShaderIdDirect(const std::string &name,
133                 const u8 material_type, const u8 drawtype){return 0;}
134         virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
135         virtual u32 getShader(const std::string &name,
136                 const u8 material_type, const u8 drawtype){return 0;}
137 };
138
139 class IWritableShaderSource : public IShaderSource {
140 public:
141         IWritableShaderSource(){}
142         virtual ~IWritableShaderSource(){}
143         virtual u32 getShaderIdDirect(const std::string &name,
144                 const u8 material_type, const u8 drawtype){return 0;}
145         virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
146         virtual u32 getShader(const std::string &name,
147                 const u8 material_type, const u8 drawtype){return 0;}
148
149         virtual void processQueue()=0;
150         virtual void insertSourceShader(const std::string &name_of_shader,
151                 const std::string &filename, const std::string &program)=0;
152         virtual void rebuildShaders()=0;
153         virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
154 };
155
156 IWritableShaderSource* createShaderSource(IrrlichtDevice *device);
157
158 void dumpShaderProgram(std::ostream &output_stream,
159         const std::string &program_type, const std::string &program);
160
161 #endif