Minetest  5.4.0
shader.h
Go to the documentation of this file.
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 "irrlichttypes_bloated.h"
24 #include <IMaterialRendererServices.h>
25 #include <string>
26 #include "tile.h"
27 #include "nodedef.h"
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 = video::EMT_SOLID;
50  video::E_MATERIAL_TYPE material = video::EMT_SOLID;
53 
54  ShaderInfo() = default;
55  virtual ~ShaderInfo() = default;
56 };
57 
58 /*
59  Setter of constants for shaders
60 */
61 
62 namespace irr { namespace video {
63  class IMaterialRendererServices;
64 } }
65 
66 
68 public:
69  virtual ~IShaderConstantSetter() = default;
70  virtual void onSetConstants(video::IMaterialRendererServices *services) = 0;
71  virtual void onSetMaterial(const video::SMaterial& material)
72  { }
73 };
74 
75 
77 public:
78  virtual ~IShaderConstantSetterFactory() = default;
79  virtual IShaderConstantSetter* create() = 0;
80 };
81 
82 
83 template <typename T, std::size_t count=1>
85  const char *m_name;
86  T m_sent[count];
87  bool has_been_set = false;
88  bool is_pixel;
89 protected:
90  CachedShaderSetting(const char *name, bool is_pixel) :
91  m_name(name), is_pixel(is_pixel)
92  {}
93 public:
94  void set(const T value[count], video::IMaterialRendererServices *services)
95  {
96  if (has_been_set && std::equal(m_sent, m_sent + count, value))
97  return;
98  if (is_pixel)
99  services->setPixelShaderConstant(m_name, value, count);
100  else
101  services->setVertexShaderConstant(m_name, value, count);
102  std::copy(value, value + count, m_sent);
103  has_been_set = true;
104  }
105 };
106 
107 template <typename T, std::size_t count = 1>
109 public:
110  CachedPixelShaderSetting(const char *name) :
111  CachedShaderSetting<T, count>(name, true){}
112 };
113 
114 template <typename T, std::size_t count = 1>
116 public:
117  CachedVertexShaderSetting(const char *name) :
118  CachedShaderSetting<T, count>(name, false){}
119 };
120 
121 
122 /*
123  ShaderSource creates and caches shaders.
124 */
125 
127 public:
128  IShaderSource() = default;
129  virtual ~IShaderSource() = default;
130 
131  virtual u32 getShaderIdDirect(const std::string &name,
132  MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
133  virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
134  virtual u32 getShader(const std::string &name,
135  MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
136 };
137 
139 public:
141  virtual ~IWritableShaderSource() = default;
142 
143  virtual void processQueue()=0;
144  virtual void insertSourceShader(const std::string &name_of_shader,
145  const std::string &filename, const std::string &program)=0;
146  virtual void rebuildShaders()=0;
147 
150 };
151 
153 
154 void dumpShaderProgram(std::ostream &output_stream,
155  const std::string &program_type, const std::string &program);
Definition: shader.h:108
CachedPixelShaderSetting(const char *name)
Definition: shader.h:110
Definition: shader.h:84
bool has_been_set
Definition: shader.h:87
T m_sent[count]
Definition: shader.h:86
bool is_pixel
Definition: shader.h:88
void set(const T value[count], video::IMaterialRendererServices *services)
Definition: shader.h:94
CachedShaderSetting(const char *name, bool is_pixel)
Definition: shader.h:90
const char * m_name
Definition: shader.h:85
Definition: shader.h:115
CachedVertexShaderSetting(const char *name)
Definition: shader.h:117
Definition: gamedef.h:49
Definition: shader.h:76
virtual ~IShaderConstantSetterFactory()=default
virtual IShaderConstantSetter * create()=0
Definition: shader.h:67
virtual void onSetConstants(video::IMaterialRendererServices *services)=0
virtual void onSetMaterial(const video::SMaterial &material)
Definition: shader.h:71
virtual ~IShaderConstantSetter()=default
Definition: shader.h:126
IShaderSource()=default
virtual ShaderInfo getShaderInfo(u32 id)
Definition: shader.h:133
virtual ~IShaderSource()=default
virtual u32 getShader(const std::string &name, MaterialType material_type, NodeDrawType drawtype=NDT_NORMAL)
Definition: shader.h:134
virtual u32 getShaderIdDirect(const std::string &name, MaterialType material_type, NodeDrawType drawtype=NDT_NORMAL)
Definition: shader.h:131
Definition: shader.h:138
virtual void rebuildShaders()=0
virtual ~IWritableShaderSource()=default
IWritableShaderSource()=default
virtual void insertSourceShader(const std::string &name_of_shader, const std::string &filename, const std::string &program)=0
virtual void processQueue()=0
virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter)=0
Definition: clouds.h:32
NodeDrawType
Definition: nodedef.h:170
@ NDT_NORMAL
Definition: nodedef.h:172
std::string getShaderPath(const std::string &name_of_shader, const std::string &filename)
Definition: shader.cpp:72
IWritableShaderSource * createShaderSource()
Definition: shader.cpp:372
void dumpShaderProgram(std::ostream &output_stream, const std::string &program_type, const std::string &program)
Definition: shader.cpp:713
Definition: shader.h:47
std::string name
Definition: shader.h:48
virtual ~ShaderInfo()=default
video::E_MATERIAL_TYPE base_material
Definition: shader.h:49
MaterialType material_type
Definition: shader.h:52
NodeDrawType drawtype
Definition: shader.h:51
video::E_MATERIAL_TYPE material
Definition: shader.h:50
ShaderInfo()=default
MaterialType
Definition: tile.h:141
@ TILE_MATERIAL_BASIC
Definition: tile.h:142