Translated using Weblate (Chinese (Simplified))
[oweals/minetest.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         virtual void onSetMaterial(const video::SMaterial& material)
71         { }
72 };
73
74
75 class IShaderConstantSetterFactory {
76 public:
77         virtual ~IShaderConstantSetterFactory() = default;
78         virtual IShaderConstantSetter* create() = 0;
79 };
80
81
82 template <typename T, std::size_t count=1>
83 class CachedShaderSetting {
84         const char *m_name;
85         T m_sent[count];
86         bool has_been_set = false;
87         bool is_pixel;
88 protected:
89         CachedShaderSetting(const char *name, bool is_pixel) :
90                 m_name(name), is_pixel(is_pixel)
91         {}
92 public:
93         void set(const T value[count], video::IMaterialRendererServices *services)
94         {
95                 if (has_been_set && std::equal(m_sent, m_sent + count, value))
96                         return;
97                 if (is_pixel)
98                         services->setPixelShaderConstant(m_name, value, count);
99                 else
100                         services->setVertexShaderConstant(m_name, value, count);
101                 std::copy(value, value + count, m_sent);
102                 has_been_set = true;
103         }
104 };
105
106 template <typename T, std::size_t count = 1>
107 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
108 public:
109         CachedPixelShaderSetting(const char *name) :
110                 CachedShaderSetting<T, count>(name, true){}
111 };
112
113 template <typename T, std::size_t count = 1>
114 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
115 public:
116         CachedVertexShaderSetting(const char *name) :
117                 CachedShaderSetting<T, count>(name, false){}
118 };
119
120
121 /*
122         ShaderSource creates and caches shaders.
123 */
124
125 class IShaderSource {
126 public:
127         IShaderSource() = default;
128         virtual ~IShaderSource() = default;
129
130         virtual u32 getShaderIdDirect(const std::string &name,
131                 const u8 material_type, const u8 drawtype){return 0;}
132         virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
133         virtual u32 getShader(const std::string &name,
134                 const u8 material_type, const u8 drawtype){return 0;}
135 };
136
137 class IWritableShaderSource : public IShaderSource {
138 public:
139         IWritableShaderSource() = default;
140         virtual ~IWritableShaderSource() = default;
141
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();
156
157 void dumpShaderProgram(std::ostream &output_stream,
158         const std::string &program_type, const std::string &program);