3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Kahrl <kahrl@gmx.net>
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.
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.
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.
23 #include <IMaterialRendererServices.h>
24 #include "irrlichttypes_bloated.h"
30 shader.{h,cpp}: Shader handling stuff.
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.
38 If not found, returns "".
40 Utilizes a thread-safe cache.
42 std::string getShaderPath(const std::string &name_of_shader,
43 const std::string &filename);
46 std::string name = "";
47 video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
48 video::E_MATERIAL_TYPE material = video::EMT_SOLID;
52 ShaderInfo() = default;
53 virtual ~ShaderInfo() = default;
57 Setter of constants for shaders
60 namespace irr { namespace video {
61 class IMaterialRendererServices;
65 class IShaderConstantSetter {
67 virtual ~IShaderConstantSetter() = default;
68 virtual void onSetConstants(video::IMaterialRendererServices *services,
69 bool is_highlevel) = 0;
73 class IShaderConstantSetterFactory {
75 virtual ~IShaderConstantSetterFactory() = default;
76 virtual IShaderConstantSetter* create() = 0;
80 template <typename T, std::size_t count=1>
81 class CachedShaderSetting {
84 bool has_been_set = false;
87 CachedShaderSetting(const char *name, bool is_pixel) :
88 m_name(name), is_pixel(is_pixel)
91 void set(const T value[count], video::IMaterialRendererServices *services)
93 if (has_been_set && std::equal(m_sent, m_sent + count, value))
96 services->setPixelShaderConstant(m_name, value, count);
98 services->setVertexShaderConstant(m_name, value, count);
99 std::copy(value, value + count, m_sent);
104 template <typename T, std::size_t count = 1>
105 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
107 CachedPixelShaderSetting(const char *name) :
108 CachedShaderSetting<T, count>(name, true){}
111 template <typename T, std::size_t count = 1>
112 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
114 CachedVertexShaderSetting(const char *name) :
115 CachedShaderSetting<T, count>(name, false){}
120 ShaderSource creates and caches shaders.
123 class IShaderSource {
125 IShaderSource() = default;
126 virtual ~IShaderSource() = default;
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;}
135 class IWritableShaderSource : public IShaderSource {
137 IWritableShaderSource() = default;
138 virtual ~IWritableShaderSource() = default;
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;}
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;
153 IWritableShaderSource *createShaderSource();
155 void dumpShaderProgram(std::ostream &output_stream,
156 const std::string &program_type, const std::string &program);