]> git.lizzy.rs Git - dragonfireclient.git/blob - src/shader.h
Sound: Add pitch option (#5960)
[dragonfireclient.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 <string>
27
28 class IGameDef;
29
30 /*
31         shader.{h,cpp}: Shader handling stuff.
32 */
33
34 /*
35         Gets the path to a shader by first checking if the file
36           name_of_shader/filename
37         exists in shader_path and if not, using the data path.
38
39         If not found, returns "".
40
41         Utilizes a thread-safe cache.
42 */
43 std::string getShaderPath(const std::string &name_of_shader,
44                 const std::string &filename);
45
46 struct ShaderInfo {
47         std::string name;
48         video::E_MATERIAL_TYPE base_material;
49         video::E_MATERIAL_TYPE material;
50         u8 drawtype;
51         u8 material_type;
52         s32 user_data;
53
54         ShaderInfo(): name(""), base_material(video::EMT_SOLID),
55                 material(video::EMT_SOLID),
56                 drawtype(0), material_type(0) {}
57         virtual ~ShaderInfo() {}
58 };
59
60 /*
61         Setter of constants for shaders
62 */
63
64 namespace irr { namespace video {
65         class IMaterialRendererServices;
66 } }
67
68
69 class IShaderConstantSetter {
70 public:
71         virtual ~IShaderConstantSetter(){};
72         virtual void onSetConstants(video::IMaterialRendererServices *services,
73                         bool is_highlevel) = 0;
74 };
75
76
77 class IShaderConstantSetterFactory {
78 public:
79         virtual ~IShaderConstantSetterFactory() {};
80         virtual IShaderConstantSetter* create() = 0;
81 };
82
83
84 template <typename T, std::size_t count=1>
85 class CachedShaderSetting {
86         const char *m_name;
87         T m_sent[count];
88         bool has_been_set;
89         bool is_pixel;
90 protected:
91         CachedShaderSetting(const char *name, bool is_pixel) :
92                 m_name(name), has_been_set(false), is_pixel(is_pixel)
93         {}
94 public:
95         void set(const T value[count], video::IMaterialRendererServices *services)
96         {
97                 if (has_been_set && std::equal(m_sent, m_sent + count, value))
98                         return;
99                 if (is_pixel)
100                         services->setPixelShaderConstant(m_name, value, count);
101                 else
102                         services->setVertexShaderConstant(m_name, value, count);
103                 std::copy(value, value + count, m_sent);
104                 has_been_set = true;
105         }
106 };
107
108 template <typename T, std::size_t count = 1>
109 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
110 public:
111         CachedPixelShaderSetting(const char *name) :
112                 CachedShaderSetting<T, count>(name, true){}
113 };
114
115 template <typename T, std::size_t count = 1>
116 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
117 public:
118         CachedVertexShaderSetting(const char *name) :
119                 CachedShaderSetting<T, count>(name, false){}
120 };
121
122
123 /*
124         ShaderSource creates and caches shaders.
125 */
126
127 class IShaderSource {
128 public:
129         IShaderSource(){}
130         virtual ~IShaderSource(){}
131         virtual u32 getShaderIdDirect(const std::string &name,
132                 const u8 material_type, const u8 drawtype){return 0;}
133         virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
134         virtual u32 getShader(const std::string &name,
135                 const u8 material_type, const u8 drawtype){return 0;}
136 };
137
138 class IWritableShaderSource : public IShaderSource {
139 public:
140         IWritableShaderSource(){}
141         virtual ~IWritableShaderSource(){}
142         virtual u32 getShaderIdDirect(const std::string &name,
143                 const u8 material_type, const u8 drawtype){return 0;}
144         virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
145         virtual u32 getShader(const std::string &name,
146                 const u8 material_type, const u8 drawtype){return 0;}
147
148         virtual void processQueue()=0;
149         virtual void insertSourceShader(const std::string &name_of_shader,
150                 const std::string &filename, const std::string &program)=0;
151         virtual void rebuildShaders()=0;
152         virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
153 };
154
155 IWritableShaderSource* createShaderSource(IrrlichtDevice *device);
156
157 void dumpShaderProgram(std::ostream &output_stream,
158         const std::string &program_type, const std::string &program);
159
160 #endif