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;
70 virtual void onSetMaterial(const video::SMaterial& material)
75 class IShaderConstantSetterFactory {
77 virtual ~IShaderConstantSetterFactory() = default;
78 virtual IShaderConstantSetter* create() = 0;
82 template <typename T, std::size_t count=1>
83 class CachedShaderSetting {
86 bool has_been_set = false;
89 CachedShaderSetting(const char *name, bool is_pixel) :
90 m_name(name), is_pixel(is_pixel)
93 void set(const T value[count], video::IMaterialRendererServices *services)
95 if (has_been_set && std::equal(m_sent, m_sent + count, value))
98 services->setPixelShaderConstant(m_name, value, count);
100 services->setVertexShaderConstant(m_name, value, count);
101 std::copy(value, value + count, m_sent);
106 template <typename T, std::size_t count = 1>
107 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
109 CachedPixelShaderSetting(const char *name) :
110 CachedShaderSetting<T, count>(name, true){}
113 template <typename T, std::size_t count = 1>
114 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
116 CachedVertexShaderSetting(const char *name) :
117 CachedShaderSetting<T, count>(name, false){}
122 ShaderSource creates and caches shaders.
125 class IShaderSource {
127 IShaderSource() = default;
128 virtual ~IShaderSource() = default;
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;}
137 class IWritableShaderSource : public IShaderSource {
139 IWritableShaderSource() = default;
140 virtual ~IWritableShaderSource() = default;
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;}
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;
155 IWritableShaderSource *createShaderSource();
157 void dumpShaderProgram(std::ostream &output_stream,
158 const std::string &program_type, const std::string &program);