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"
32 shader.{h,cpp}: Shader handling stuff.
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.
40 If not found, returns "".
42 Utilizes a thread-safe cache.
44 std::string getShaderPath(const std::string &name_of_shader,
45 const std::string &filename);
48 std::string name = "";
49 video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
50 video::E_MATERIAL_TYPE material = video::EMT_SOLID;
51 NodeDrawType drawtype = NDT_NORMAL;
52 MaterialType material_type = TILE_MATERIAL_BASIC;
54 ShaderInfo() = default;
55 virtual ~ShaderInfo() = default;
59 Setter of constants for shaders
62 namespace irr { namespace video {
63 class IMaterialRendererServices;
67 class IShaderConstantSetter {
69 virtual ~IShaderConstantSetter() = default;
70 virtual void onSetConstants(video::IMaterialRendererServices *services) = 0;
71 virtual void onSetMaterial(const video::SMaterial& material)
76 class IShaderConstantSetterFactory {
78 virtual ~IShaderConstantSetterFactory() = default;
79 virtual IShaderConstantSetter* create() = 0;
83 template <typename T, std::size_t count=1>
84 class CachedShaderSetting {
87 bool has_been_set = false;
90 CachedShaderSetting(const char *name, bool is_pixel) :
91 m_name(name), is_pixel(is_pixel)
94 void set(const T value[count], video::IMaterialRendererServices *services)
96 if (has_been_set && std::equal(m_sent, m_sent + count, value))
99 services->setPixelShaderConstant(m_name, value, count);
101 services->setVertexShaderConstant(m_name, value, count);
102 std::copy(value, value + count, m_sent);
107 template <typename T, std::size_t count = 1>
108 class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
110 CachedPixelShaderSetting(const char *name) :
111 CachedShaderSetting<T, count>(name, true){}
114 template <typename T, std::size_t count = 1>
115 class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
117 CachedVertexShaderSetting(const char *name) :
118 CachedShaderSetting<T, count>(name, false){}
123 ShaderSource creates and caches shaders.
126 class IShaderSource {
128 IShaderSource() = default;
129 virtual ~IShaderSource() = default;
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;}
138 class IWritableShaderSource : public IShaderSource {
140 IWritableShaderSource() = default;
141 virtual ~IWritableShaderSource() = default;
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;
148 /// @note Takes ownership of @p setter.
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);