]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/shader.cpp
make formspec textarea wordwrap
[dragonfireclient.git] / src / shader.cpp
index ed8cc5b739f23d0726cce7b4fc400d02a176143e..4013add6cf9c2d37e4076f58d0e8a756712a8662 100644 (file)
@@ -210,8 +210,7 @@ class ShaderCallback : public video::IShaderConstantSetCallBack
 class MainShaderConstantSetter : public IShaderConstantSetter
 {
 public:
-       MainShaderConstantSetter(IrrlichtDevice *device):
-               m_device(device)
+       MainShaderConstantSetter(IrrlichtDevice *device)
        {}
        ~MainShaderConstantSetter() {}
 
@@ -240,16 +239,21 @@ class MainShaderConstantSetter : public IShaderConstantSetter
                        services->setVertexShaderConstant(worldViewProj.pointer(), 4, 4);
 
                // set transposed world matrix
+               core::matrix4 transWorld = driver->getTransform(video::ETS_WORLD);
+               transWorld = transWorld.getTransposed();
+               if(is_highlevel)
+                       services->setVertexShaderConstant("mTransWorld", transWorld.pointer(), 16);
+               else
+                       services->setVertexShaderConstant(transWorld.pointer(), 8, 4);
+
+               // set world matrix
                core::matrix4 world = driver->getTransform(video::ETS_WORLD);
-               world = world.getTransposed();
                if(is_highlevel)
-                       services->setVertexShaderConstant("mTransWorld", world.pointer(), 16);
+                       services->setVertexShaderConstant("mWorld", world.pointer(), 16);
                else
                        services->setVertexShaderConstant(world.pointer(), 8, 4);
-       }
 
-private:
-       IrrlichtDevice *m_device;
+       }
 };
 
 /*
@@ -373,8 +377,6 @@ ShaderSource::ShaderSource(IrrlichtDevice *device):
 
        m_shader_callback = new ShaderCallback(this, "default");
 
-       m_shaderinfo_cache_mutex.Init();
-
        m_main_thread = get_current_thread_id();
 
        // Add a dummy ShaderInfo as the first index, named ""
@@ -417,31 +419,30 @@ u32 ShaderSource::getShaderId(const std::string &name)
        if(get_current_thread_id() == m_main_thread){
                return getShaderIdDirect(name);
        } else {
-               infostream<<"getShaderId(): Queued: name=\""<<name<<"\""<<std::endl;
+               /*errorstream<<"getShaderId(): Queued: name=\""<<name<<"\""<<std::endl;*/
 
                // We're gonna ask the result to be put into here
-               ResultQueue<std::string, u32, u8, u8> result_queue;
+
+               static ResultQueue<std::string, u32, u8, u8> result_queue;
 
                // Throw a request in
                m_get_shader_queue.add(name, 0, 0, &result_queue);
 
-               infostream<<"Waiting for shader from main thread, name=\""
-                               <<name<<"\""<<std::endl;
+               /* infostream<<"Waiting for shader from main thread, name=\""
+                               <<name<<"\""<<std::endl;*/
 
-               try{
-                       // Wait result for a second
+               while(true) {
                        GetResult<std::string, u32, u8, u8>
-                                       result = result_queue.pop_front(1000);
-
-                       // Check that at least something worked OK
-                       assert(result.key == name);
+                               result = result_queue.pop_frontNoEx();
 
-                       return result.item;
-               }
-               catch(ItemNotFoundException &e){
-                       infostream<<"Waiting for shader timed out."<<std::endl;
-                       return 0;
+                       if (result.key == name) {
+                               return result.item;
+                       }
+                       else {
+                               errorstream << "Got shader with invalid name: " << result.key << std::endl;
+                       }
                }
+
        }
 
        infostream<<"getShaderId(): Failed"<<std::endl;
@@ -537,22 +538,17 @@ void ShaderSource::processQueue()
        /*
                Fetch shaders
        */
+       //NOTE this is only thread safe for ONE consumer thread!
        if(!m_get_shader_queue.empty()){
                GetRequest<std::string, u32, u8, u8>
                                request = m_get_shader_queue.pop();
 
-               /*infostream<<"ShaderSource::processQueue(): "
+               /**errorstream<<"ShaderSource::processQueue(): "
                                <<"got shader request with "
                                <<"name=\""<<request.key<<"\""
-                               <<std::endl;*/
+                               <<std::endl;**/
 
-               GetResult<std::string, u32, u8, u8>
-                               result;
-               result.key = request.key;
-               result.callers = request.callers;
-               result.item = getShaderIdDirect(request.key);
-
-               request.dest->push_back(result);
+               m_get_shader_queue.pushResult(request,getShaderIdDirect(request.key));
        }
 }
 
@@ -600,7 +596,7 @@ void ShaderSource::onSetConstants(video::IMaterialRendererServices *services,
                setter->onSetConstants(services, is_highlevel);
        }
 }
+
 ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
                video::IShaderConstantSetCallBack *callback,
                SourceShaderCache *sourcecache)
@@ -679,6 +675,77 @@ ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
        if(vertex_program == "" && pixel_program == "" && geometry_program == "")
                return shaderinfo;
 
+       // Create shaders header
+       std::string shaders_header = "#version 120\n";
+
+       if (g_settings->getBool("generate_normalmaps")){
+               shaders_header += "#define GENERATE_NORMALMAPS\n";
+               shaders_header += "#define NORMALMAPS_STRENGTH ";
+               shaders_header += ftos(g_settings->getFloat("normalmaps_strength"));
+               shaders_header += "\n";
+               float sample_step;
+               int smooth = (int)g_settings->getFloat("normalmaps_smooth");
+               switch (smooth){
+               case 0:
+                       sample_step = 0.0078125; // 1.0 / 128.0
+                       break;
+               case 1:
+                       sample_step = 0.00390625; // 1.0 / 256.0
+                       break;
+               case 2:
+                       sample_step = 0.001953125; // 1.0 / 512.0
+                       break;
+               default:
+                       sample_step = 0.0078125;
+                       break;
+               }
+               shaders_header += "#define SAMPLE_STEP ";
+               shaders_header += ftos(sample_step);
+               shaders_header += "\n";
+       }
+
+       if (g_settings->getBool("enable_bumpmapping"))
+               shaders_header += "#define ENABLE_BUMPMAPPING\n";
+
+       if (g_settings->getBool("enable_parallax_occlusion")){
+               shaders_header += "#define ENABLE_PARALLAX_OCCLUSION\n";
+               shaders_header += "#define PARALLAX_OCCLUSION_SCALE ";
+               shaders_header += ftos(g_settings->getFloat("parallax_occlusion_scale"));
+               shaders_header += "\n";
+               shaders_header += "#define PARALLAX_OCCLUSION_BIAS ";
+               shaders_header += ftos(g_settings->getFloat("parallax_occlusion_bias"));
+               shaders_header += "\n";
+       }
+
+       if (g_settings->getBool("enable_bumpmapping") || g_settings->getBool("enable_parallax_occlusion"))
+               shaders_header += "#define USE_NORMALMAPS\n";
+
+       if (g_settings->getBool("enable_waving_water")){
+               shaders_header += "#define ENABLE_WAVING_WATER\n";
+               shaders_header += "#define WATER_WAVE_HEIGHT ";
+               shaders_header += ftos(g_settings->getFloat("water_wave_height"));
+               shaders_header += "\n";
+               shaders_header += "#define WATER_WAVE_LENGTH ";
+               shaders_header += ftos(g_settings->getFloat("water_wave_length"));
+               shaders_header += "\n";
+               shaders_header += "#define WATER_WAVE_SPEED ";
+               shaders_header += ftos(g_settings->getFloat("water_wave_speed"));
+               shaders_header += "\n";
+       }
+
+       if (g_settings->getBool("enable_waving_leaves"))
+               shaders_header += "#define ENABLE_WAVING_LEAVES\n";
+
+       if (g_settings->getBool("enable_waving_plants"))
+               shaders_header += "#define ENABLE_WAVING_PLANTS\n";
+
+       if(pixel_program != "")
+               pixel_program = shaders_header + pixel_program;
+       if(vertex_program != "")
+               vertex_program = shaders_header + vertex_program;
+       if(geometry_program != "")
+               geometry_program = shaders_header + geometry_program;
+
        // Call addHighLevelShaderMaterial() or addShaderMaterial()
        const c8* vertex_program_ptr = 0;
        const c8* pixel_program_ptr = 0;