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.
24 #include <IMaterialRendererServices.h>
25 #include "irrlichttypes_bloated.h"
31 shader.{h,cpp}: Shader handling stuff.
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.
39 If not found, returns "".
41 Utilizes a thread-safe cache.
43 std::string getShaderPath(const std::string &name_of_shader,
44 const std::string &filename);
47 std::string name = "";
48 video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
49 video::E_MATERIAL_TYPE material = video::EMT_SOLID;
54 virtual ~ShaderInfo() {}
58 Setter of constants for shaders
61 namespace irr { namespace video {
62 class IMaterialRendererServices;
66 class IShaderConstantSetter {
68 virtual ~IShaderConstantSetter(){};
69 virtual void onSetConstants(video::IMaterialRendererServices *services,
70 bool is_highlevel) = 0;
74 class IShaderConstantSetterFactory {
76 virtual ~IShaderConstantSetterFactory() {};
77 virtual IShaderConstantSetter* create() = 0;
81 template <typename T, std::size_t count=1>
82 class CachedShaderSetting {
85 bool has_been_set = false;
88 CachedShaderSetting(const char *name, bool is_pixel) :
89 m_name(name), is_pixel(is_pixel)
92 void set(const T value[count], video::IMaterialRendererServices *services)
94 if (has_been_set && std::equal(m_sent, m_sent + count, value))
97 services->setPixelShaderConstant(m_name, value, count);
99 services->setVertexShaderConstant(m_name, value, count);
100 std::copy(value, value + count, m_sent);
105 template <typename T, std::size_t count = 1>
106 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
108 CachedPixelShaderSetting(const char *name) :
109 CachedShaderSetting<T, count>(name, true){}
112 template <typename T, std::size_t count = 1>
113 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
115 CachedVertexShaderSetting(const char *name) :
116 CachedShaderSetting<T, count>(name, false){}
121 ShaderSource creates and caches shaders.
124 class IShaderSource {
127 virtual ~IShaderSource(){}
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(){}
138 virtual ~IWritableShaderSource(){}
139 virtual u32 getShaderIdDirect(const std::string &name,
140 const u8 material_type, const u8 drawtype){return 0;}
141 virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
142 virtual u32 getShader(const std::string &name,
143 const u8 material_type, const u8 drawtype){return 0;}
145 virtual void processQueue()=0;
146 virtual void insertSourceShader(const std::string &name_of_shader,
147 const std::string &filename, const std::string &program)=0;
148 virtual void rebuildShaders()=0;
149 virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
152 IWritableShaderSource *createShaderSource();
154 void dumpShaderProgram(std::ostream &output_stream,
155 const std::string &program_type, const std::string &program);