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