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 General Public License as published by
8 the Free Software Foundation; either version 2 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 General Public License for more details.
16 You should have received a copy of the GNU 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.
22 #include "irrlichttypes_extrabloated.h"
24 #include "main.h" // for g_settings
26 #include "util/container.h"
27 #include "util/thread.h"
30 #include <ICameraSceneNode.h>
31 #include <IGPUProgrammingServices.h>
32 #include <IMaterialRenderer.h>
33 #include <IMaterialRendererServices.h>
34 #include <IShaderConstantSetCallBack.h>
35 #include "EShaderTypes.h"
38 #include "strfnd.h" // trim()
41 A cache from shader name to shader path
43 MutexedMap<std::string, std::string> g_shadername_to_path_cache;
46 Gets the path to a shader by first checking if the file
47 name_of_shader/filename
48 exists in shader_path and if not, using the data path.
50 If not found, returns "".
52 Utilizes a thread-safe cache.
54 std::string getShaderPath(const std::string &name_of_shader,
55 const std::string &filename)
57 std::string combined = name_of_shader + DIR_DELIM + filename;
58 std::string fullpath = "";
62 bool incache = g_shadername_to_path_cache.get(combined, &fullpath);
67 Check from shader_path
69 std::string shader_path = g_settings->get("shader_path");
72 std::string testpath = shader_path + DIR_DELIM + combined;
73 if(fs::PathExists(testpath))
78 Check from default data directory
82 std::string rel_path = std::string("client") + DIR_DELIM
83 + "shaders" + DIR_DELIM
84 + name_of_shader + DIR_DELIM
86 std::string testpath = porting::path_share + DIR_DELIM + rel_path;
87 if(fs::PathExists(testpath))
91 // Add to cache (also an empty result is cached)
92 g_shadername_to_path_cache.set(combined, fullpath);
99 SourceShaderCache: A cache used for storing source shaders.
102 class SourceShaderCache
105 void insert(const std::string &name_of_shader,
106 const std::string &filename,
107 const std::string &program,
110 std::string combined = name_of_shader + DIR_DELIM + filename;
111 // Try to use local shader instead if asked to
113 std::string path = getShaderPath(name_of_shader, filename);
115 std::string p = readFile(path);
117 m_programs[combined] = p;
122 m_programs[combined] = program;
124 std::string get(const std::string &name_of_shader,
125 const std::string &filename)
127 std::string combined = name_of_shader + DIR_DELIM + filename;
128 std::map<std::string, std::string>::iterator n;
129 n = m_programs.find(combined);
130 if(n != m_programs.end())
134 // Primarily fetches from cache, secondarily tries to read from filesystem
135 std::string getOrLoad(const std::string &name_of_shader,
136 const std::string &filename)
138 std::string combined = name_of_shader + DIR_DELIM + filename;
139 std::map<std::string, std::string>::iterator n;
140 n = m_programs.find(combined);
141 if(n != m_programs.end())
143 std::string path = getShaderPath(name_of_shader, filename);
145 infostream<<"SourceShaderCache::getOrLoad(): No path found for \""
146 <<combined<<"\""<<std::endl;
149 infostream<<"SourceShaderCache::getOrLoad(): Loading path \""<<path
151 std::string p = readFile(path);
153 m_programs[combined] = p;
159 std::map<std::string, std::string> m_programs;
160 std::string readFile(const std::string &path)
162 std::ifstream is(path.c_str(), std::ios::binary);
165 std::ostringstream tmp_os;
166 tmp_os << is.rdbuf();
172 ShaderCallback: Sets constants that can be used in shaders
175 class IShaderConstantSetterRegistry
178 virtual ~IShaderConstantSetterRegistry(){};
179 virtual void onSetConstants(video::IMaterialRendererServices *services,
180 bool is_highlevel, const std::string &name) = 0;
183 class ShaderCallback : public video::IShaderConstantSetCallBack
185 IShaderConstantSetterRegistry *m_scsr;
189 ShaderCallback(IShaderConstantSetterRegistry *scsr, const std::string &name):
195 virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData)
197 video::IVideoDriver *driver = services->getVideoDriver();
200 bool is_highlevel = userData;
202 m_scsr->onSetConstants(services, is_highlevel, m_name);
207 MainShaderConstantSetter: Set basic constants required for almost everything
210 class MainShaderConstantSetter : public IShaderConstantSetter
213 MainShaderConstantSetter(IrrlichtDevice *device):
216 ~MainShaderConstantSetter() {}
218 virtual void onSetConstants(video::IMaterialRendererServices *services,
221 video::IVideoDriver *driver = services->getVideoDriver();
224 // set inverted world matrix
225 core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
226 invWorld.makeInverse();
228 services->setVertexShaderConstant("mInvWorld", invWorld.pointer(), 16);
230 services->setVertexShaderConstant(invWorld.pointer(), 0, 4);
233 core::matrix4 worldViewProj;
234 worldViewProj = driver->getTransform(video::ETS_PROJECTION);
235 worldViewProj *= driver->getTransform(video::ETS_VIEW);
236 worldViewProj *= driver->getTransform(video::ETS_WORLD);
238 services->setVertexShaderConstant("mWorldViewProj", worldViewProj.pointer(), 16);
240 services->setVertexShaderConstant(worldViewProj.pointer(), 4, 4);
242 // set transposed world matrix
243 core::matrix4 world = driver->getTransform(video::ETS_WORLD);
244 world = world.getTransposed();
246 services->setVertexShaderConstant("mTransWorld", world.pointer(), 16);
248 services->setVertexShaderConstant(world.pointer(), 8, 4);
252 IrrlichtDevice *m_device;
259 class ShaderSource : public IWritableShaderSource, public IShaderConstantSetterRegistry
262 ShaderSource(IrrlichtDevice *device);
266 Gets a shader material id from cache or
267 - if main thread, from getShaderIdDirect
268 - if other thread, adds to request queue and waits for main thread
270 u32 getShaderId(const std::string &name);
273 - If shader material specified by name is found from cache,
274 return the cached id.
275 - Otherwise generate the shader material, add to cache and return id.
277 The id 0 points to a null shader. Its material is EMT_SOLID.
279 u32 getShaderIdDirect(const std::string &name);
281 // Finds out the name of a cached shader.
282 std::string getShaderName(u32 id);
285 If shader specified by the name pointed by the id doesn't
286 exist, create it, then return the cached shader.
288 Can be called from any thread. If called from some other thread
289 and not found in cache, the call is queued to the main thread
292 ShaderInfo getShader(u32 id);
294 ShaderInfo getShader(const std::string &name)
296 return getShader(getShaderId(name));
299 // Processes queued shader requests from other threads.
300 // Shall be called from the main thread.
303 // Insert a shader program into the cache without touching the
304 // filesystem. Shall be called from the main thread.
305 void insertSourceShader(const std::string &name_of_shader,
306 const std::string &filename, const std::string &program);
308 // Rebuild shaders from the current set of source shaders
309 // Shall be called from the main thread.
310 void rebuildShaders();
312 void addGlobalConstantSetter(IShaderConstantSetter *setter)
314 m_global_setters.push_back(setter);
317 void onSetConstants(video::IMaterialRendererServices *services,
318 bool is_highlevel, const std::string &name);
322 // The id of the thread that is allowed to use irrlicht directly
323 threadid_t m_main_thread;
324 // The irrlicht device
325 IrrlichtDevice *m_device;
326 // The set-constants callback
327 ShaderCallback *m_shader_callback;
329 // Cache of source shaders
330 // This should be only accessed from the main thread
331 SourceShaderCache m_sourcecache;
333 // A shader id is index in this array.
334 // The first position contains a dummy shader.
335 std::vector<ShaderInfo> m_shaderinfo_cache;
336 // Maps a shader name to an index in the former.
337 std::map<std::string, u32> m_name_to_id;
338 // The two former containers are behind this mutex
339 JMutex m_shaderinfo_cache_mutex;
341 // Queued shader fetches (to be processed by the main thread)
342 RequestQueue<std::string, u32, u8, u8> m_get_shader_queue;
344 // Global constant setters
345 // TODO: Delete these in the destructor
346 std::vector<IShaderConstantSetter*> m_global_setters;
349 IWritableShaderSource* createShaderSource(IrrlichtDevice *device)
351 return new ShaderSource(device);
355 Generate shader given the shader name.
357 ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
358 video::IShaderConstantSetCallBack *callback,
359 SourceShaderCache *sourcecache);
364 void load_shaders(std::string name, SourceShaderCache *sourcecache,
365 video::E_DRIVER_TYPE drivertype, s32 enable_shaders,
366 std::string &vertex_program, std::string &pixel_program,
367 std::string &geometry_program, bool &is_highlevel);
369 ShaderSource::ShaderSource(IrrlichtDevice *device):
374 m_shader_callback = new ShaderCallback(this, "default");
376 m_shaderinfo_cache_mutex.Init();
378 m_main_thread = get_current_thread_id();
380 // Add a dummy ShaderInfo as the first index, named ""
381 m_shaderinfo_cache.push_back(ShaderInfo());
382 m_name_to_id[""] = 0;
384 // Add main global constant setter
385 addGlobalConstantSetter(new MainShaderConstantSetter(device));
388 ShaderSource::~ShaderSource()
390 //m_shader_callback->drop();
393 u32 ShaderSource::getShaderId(const std::string &name)
395 //infostream<<"getShaderId(): \""<<name<<"\""<<std::endl;
399 See if shader already exists
401 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
402 std::map<std::string, u32>::iterator n;
403 n = m_name_to_id.find(name);
404 if(n != m_name_to_id.end())
411 if(get_current_thread_id() == m_main_thread){
412 return getShaderIdDirect(name);
414 infostream<<"getShaderId(): Queued: name=\""<<name<<"\""<<std::endl;
416 // We're gonna ask the result to be put into here
417 ResultQueue<std::string, u32, u8, u8> result_queue;
419 // Throw a request in
420 m_get_shader_queue.add(name, 0, 0, &result_queue);
422 infostream<<"Waiting for shader from main thread, name=\""
423 <<name<<"\""<<std::endl;
426 // Wait result for a second
427 GetResult<std::string, u32, u8, u8>
428 result = result_queue.pop_front(1000);
430 // Check that at least something worked OK
431 assert(result.key == name);
435 catch(ItemNotFoundException &e){
436 infostream<<"Waiting for shader timed out."<<std::endl;
441 infostream<<"getShaderId(): Failed"<<std::endl;
447 This method generates all the shaders
449 u32 ShaderSource::getShaderIdDirect(const std::string &name)
451 //infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl;
453 // Empty name means shader 0
455 infostream<<"getShaderIdDirect(): name is empty"<<std::endl;
460 Calling only allowed from main thread
462 if(get_current_thread_id() != m_main_thread){
463 errorstream<<"ShaderSource::getShaderIdDirect() "
464 "called not from main thread"<<std::endl;
469 See if shader already exists
472 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
474 std::map<std::string, u32>::iterator n;
475 n = m_name_to_id.find(name);
476 if(n != m_name_to_id.end()){
477 /*infostream<<"getShaderIdDirect(): \""<<name
478 <<"\" found in cache"<<std::endl;*/
483 /*infostream<<"getShaderIdDirect(): \""<<name
484 <<"\" NOT found in cache. Creating it."<<std::endl;*/
486 ShaderInfo info = generate_shader(name, m_device,
487 m_shader_callback, &m_sourcecache);
490 Add shader to caches (add dummy shaders too)
493 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
495 u32 id = m_shaderinfo_cache.size();
496 m_shaderinfo_cache.push_back(info);
497 m_name_to_id[name] = id;
499 /*infostream<<"getShaderIdDirect(): "
500 <<"Returning id="<<id<<" for name \""<<name<<"\""<<std::endl;*/
505 std::string ShaderSource::getShaderName(u32 id)
507 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
509 if(id >= m_shaderinfo_cache.size()){
510 errorstream<<"ShaderSource::getShaderName(): id="<<id
511 <<" >= m_shaderinfo_cache.size()="
512 <<m_shaderinfo_cache.size()<<std::endl;
516 return m_shaderinfo_cache[id].name;
519 ShaderInfo ShaderSource::getShader(u32 id)
521 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
523 if(id >= m_shaderinfo_cache.size())
526 return m_shaderinfo_cache[id];
529 void ShaderSource::processQueue()
534 if(!m_get_shader_queue.empty()){
535 GetRequest<std::string, u32, u8, u8>
536 request = m_get_shader_queue.pop();
538 /*infostream<<"ShaderSource::processQueue(): "
539 <<"got shader request with "
540 <<"name=\""<<request.key<<"\""
543 GetResult<std::string, u32, u8, u8>
545 result.key = request.key;
546 result.callers = request.callers;
547 result.item = getShaderIdDirect(request.key);
549 request.dest->push_back(result);
553 void ShaderSource::insertSourceShader(const std::string &name_of_shader,
554 const std::string &filename, const std::string &program)
556 /*infostream<<"ShaderSource::insertSourceShader(): "
557 "name_of_shader=\""<<name_of_shader<<"\", "
558 "filename=\""<<filename<<"\""<<std::endl;*/
560 assert(get_current_thread_id() == m_main_thread);
562 m_sourcecache.insert(name_of_shader, filename, program, true);
565 void ShaderSource::rebuildShaders()
567 JMutexAutoLock lock(m_shaderinfo_cache_mutex);
569 /*// Oh well... just clear everything, they'll load sometime.
570 m_shaderinfo_cache.clear();
571 m_name_to_id.clear();*/
574 FIXME: Old shader materials can't be deleted in Irrlicht,
576 (This would be nice to do in the destructor too)
580 for(u32 i=0; i<m_shaderinfo_cache.size(); i++){
581 ShaderInfo *info = &m_shaderinfo_cache[i];
582 *info = generate_shader(info->name, m_device,
583 m_shader_callback, &m_sourcecache);
587 void ShaderSource::onSetConstants(video::IMaterialRendererServices *services,
588 bool is_highlevel, const std::string &name)
590 for(u32 i=0; i<m_global_setters.size(); i++){
591 IShaderConstantSetter *setter = m_global_setters[i];
592 setter->onSetConstants(services, is_highlevel);
596 ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
597 video::IShaderConstantSetCallBack *callback,
598 SourceShaderCache *sourcecache)
600 /*infostream<<"generate_shader(): "
601 "\""<<name<<"\""<<std::endl;*/
603 ShaderInfo shaderinfo;
604 shaderinfo.name = name;
605 shaderinfo.material = video::EMT_SOLID;
608 Get the base material
610 std::string base_material_name =
611 trim(sourcecache->getOrLoad(name, "base.txt"));
612 for(s32 i = 0; video::sBuiltInMaterialTypeNames[i] != 0; i++){
613 if(video::sBuiltInMaterialTypeNames[i] == base_material_name){
614 shaderinfo.material = (video::E_MATERIAL_TYPE) i;
619 // 0 = off, 1 = assembly shaders only, 2 = highlevel or assembly
620 s32 enable_shaders = g_settings->getS32("enable_shaders");
621 if(enable_shaders <= 0)
624 video::IVideoDriver* driver = device->getVideoDriver();
627 video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
629 errorstream<<"generate_shader(): "
630 "failed to generate \""<<name<<"\", "
631 "GPU programming not supported."
636 // Choose shader language depending on driver type and settings
638 std::string vertex_program;
639 std::string pixel_program;
640 std::string geometry_program;
642 load_shaders(name, sourcecache, driver->getDriverType(),
643 enable_shaders, vertex_program, pixel_program,
644 geometry_program, is_highlevel);
646 // Check hardware/driver support
647 if(vertex_program != "" &&
648 !driver->queryFeature(video::EVDF_VERTEX_SHADER_1_1) &&
649 !driver->queryFeature(video::EVDF_ARB_VERTEX_PROGRAM_1)){
650 infostream<<"generate_shader(): vertex shaders disabled "
651 "because of missing driver/hardware support."
655 if(pixel_program != "" &&
656 !driver->queryFeature(video::EVDF_PIXEL_SHADER_1_1) &&
657 !driver->queryFeature(video::EVDF_ARB_FRAGMENT_PROGRAM_1)){
658 infostream<<"generate_shader(): pixel shaders disabled "
659 "because of missing driver/hardware support."
663 if(geometry_program != "" &&
664 !driver->queryFeature(video::EVDF_GEOMETRY_SHADER)){
665 infostream<<"generate_shader(): geometry shaders disabled "
666 "because of missing driver/hardware support."
668 geometry_program = "";
671 // If no shaders are used, don't make a separate material type
672 if(vertex_program == "" && pixel_program == "" && geometry_program == "")
675 // Call addHighLevelShaderMaterial() or addShaderMaterial()
676 const c8* vertex_program_ptr = 0;
677 const c8* pixel_program_ptr = 0;
678 const c8* geometry_program_ptr = 0;
679 if(vertex_program != "")
680 vertex_program_ptr = vertex_program.c_str();
681 if(pixel_program != "")
682 pixel_program_ptr = pixel_program.c_str();
683 if(geometry_program != "")
684 geometry_program_ptr = geometry_program.c_str();
687 infostream<<"Compiling high level shaders for "<<name<<std::endl;
688 shadermat = gpu->addHighLevelShaderMaterial(
689 vertex_program_ptr, // Vertex shader program
690 "vertexMain", // Vertex shader entry point
691 video::EVST_VS_1_1, // Vertex shader version
692 pixel_program_ptr, // Pixel shader program
693 "pixelMain", // Pixel shader entry point
694 video::EPST_PS_1_1, // Pixel shader version
695 geometry_program_ptr, // Geometry shader program
696 "geometryMain", // Geometry shader entry point
697 video::EGST_GS_4_0, // Geometry shader version
698 scene::EPT_TRIANGLES, // Geometry shader input
699 scene::EPT_TRIANGLE_STRIP, // Geometry shader output
700 0, // Support maximum number of vertices
701 callback, // Set-constant callback
702 shaderinfo.material, // Base material
703 1 // Userdata passed to callback
707 errorstream<<"generate_shader(): "
708 "failed to generate \""<<name<<"\", "
709 "addHighLevelShaderMaterial failed."
715 infostream<<"Compiling assembly shaders for "<<name<<std::endl;
716 shadermat = gpu->addShaderMaterial(
717 vertex_program_ptr, // Vertex shader program
718 pixel_program_ptr, // Pixel shader program
719 callback, // Set-constant callback
720 shaderinfo.material, // Base material
721 0 // Userdata passed to callback
725 errorstream<<"generate_shader(): "
726 "failed to generate \""<<name<<"\", "
727 "addShaderMaterial failed."
733 // HACK, TODO: investigate this better
734 // Grab the material renderer once more so minetest doesn't crash on exit
735 driver->getMaterialRenderer(shadermat)->grab();
737 // Apply the newly created material type
738 shaderinfo.material = (video::E_MATERIAL_TYPE) shadermat;
742 void load_shaders(std::string name, SourceShaderCache *sourcecache,
743 video::E_DRIVER_TYPE drivertype, s32 enable_shaders,
744 std::string &vertex_program, std::string &pixel_program,
745 std::string &geometry_program, bool &is_highlevel)
749 geometry_program = "";
750 is_highlevel = false;
752 if(enable_shaders >= 2){
753 // Look for high level shaders
754 if(drivertype == video::EDT_DIRECT3D9){
756 // (All shaders in one file)
757 vertex_program = sourcecache->getOrLoad(name, "d3d9.hlsl");
758 pixel_program = vertex_program;
759 geometry_program = vertex_program;
761 else if(drivertype == video::EDT_OPENGL){
763 vertex_program = sourcecache->getOrLoad(name, "opengl_vertex.glsl");
764 pixel_program = sourcecache->getOrLoad(name, "opengl_fragment.glsl");
765 geometry_program = sourcecache->getOrLoad(name, "opengl_geometry.glsl");
767 if(vertex_program != "" || pixel_program != "" || geometry_program != ""){
773 if(enable_shaders >= 1){
774 // Look for assembly shaders
775 if(drivertype == video::EDT_DIRECT3D8){
776 // Direct3D 8 assembly shaders
777 vertex_program = sourcecache->getOrLoad(name, "d3d8_vertex.asm");
778 pixel_program = sourcecache->getOrLoad(name, "d3d8_pixel.asm");
780 else if(drivertype == video::EDT_DIRECT3D9){
781 // Direct3D 9 assembly shaders
782 vertex_program = sourcecache->getOrLoad(name, "d3d9_vertex.asm");
783 pixel_program = sourcecache->getOrLoad(name, "d3d9_pixel.asm");
785 else if(drivertype == video::EDT_OPENGL){
786 // OpenGL assembly shaders
787 vertex_program = sourcecache->getOrLoad(name, "opengl_vertex.asm");
788 pixel_program = sourcecache->getOrLoad(name, "opengl_fragment.asm");
790 if(vertex_program != "" || pixel_program != "")