]> git.lizzy.rs Git - minetest.git/blobdiff - src/shader.cpp
dofile error reporting for syntax errors
[minetest.git] / src / shader.cpp
index cf3bbd76174239332573a337431375087de5f146..a467c2ea96dae18568bb2a56fd099ed06a5eabcf 100644 (file)
@@ -18,15 +18,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
+#include <fstream>
+#include <iterator>
 #include "shader.h"
 #include "irrlichttypes_extrabloated.h"
 #include "debug.h"
-#include "main.h" // for g_settings
 #include "filesys.h"
 #include "util/container.h"
 #include "util/thread.h"
 #include "settings.h"
-#include <iterator>
 #include <ICameraSceneNode.h>
 #include <IGPUProgrammingServices.h>
 #include <IMaterialRenderer.h>
@@ -36,7 +36,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "gamedef.h"
 #include "strfnd.h" // trim()
-#include "tile.h"
+#include "client/tile.h"
 
 /*
        A cache from shader name to shader path
@@ -103,10 +103,8 @@ std::string getShaderPath(const std::string &name_of_shader,
 class SourceShaderCache
 {
 public:
-       void insert(const std::string &name_of_shader,
-                       const std::string &filename,
-                       const std::string &program,
-                       bool prefer_local)
+       void insert(const std::string &name_of_shader, const std::string &filename,
+               const std::string &program, bool prefer_local)
        {
                std::string combined = name_of_shader + DIR_DELIM + filename;
                // Try to use local shader instead if asked to
@@ -122,42 +120,43 @@ class SourceShaderCache
                }
                m_programs[combined] = program;
        }
+
        std::string get(const std::string &name_of_shader,
-                       const std::string &filename)
+               const std::string &filename)
        {
                std::string combined = name_of_shader + DIR_DELIM + filename;
-               std::map<std::string, std::string>::iterator n;
-               n = m_programs.find(combined);
-               if(n != m_programs.end())
+               StringMap::iterator n = m_programs.find(combined);
+               if (n != m_programs.end())
                        return n->second;
                return "";
        }
+
        // Primarily fetches from cache, secondarily tries to read from filesystem
        std::string getOrLoad(const std::string &name_of_shader,
-                       const std::string &filename)
+               const std::string &filename)
        {
                std::string combined = name_of_shader + DIR_DELIM + filename;
-               std::map<std::string, std::string>::iterator n;
-               n = m_programs.find(combined);
-               if(n != m_programs.end())
+               StringMap::iterator n = m_programs.find(combined);
+               if (n != m_programs.end())
                        return n->second;
                std::string path = getShaderPath(name_of_shader, filename);
-               if(path == ""){
-                       infostream<<"SourceShaderCache::getOrLoad(): No path found for \""
-                                       <<combined<<"\""<<std::endl;
+               if (path == "") {
+                       infostream << "SourceShaderCache::getOrLoad(): No path found for \""
+                               << combined << "\"" << std::endl;
                        return "";
                }
-               infostream<<"SourceShaderCache::getOrLoad(): Loading path \""<<path
-                               <<"\""<<std::endl;
+               infostream << "SourceShaderCache::getOrLoad(): Loading path \""
+                       << path << "\"" << std::endl;
                std::string p = readFile(path);
-               if(p != ""){
+               if (p != "") {
                        m_programs[combined] = p;
                        return p;
                }
                return "";
        }
 private:
-       std::map<std::string, std::string> m_programs;
+       StringMap m_programs;
+
        std::string readFile(const std::string &path)
        {
                std::ifstream is(path.c_str(), std::ios::binary);
@@ -196,7 +195,7 @@ class ShaderCallback : public video::IShaderConstantSetCallBack
        virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData)
        {
                video::IVideoDriver *driver = services->getVideoDriver();
-               assert(driver);
+               sanity_check(driver != NULL);
 
                bool is_highlevel = userData;
 
@@ -219,7 +218,7 @@ class MainShaderConstantSetter : public IShaderConstantSetter
                        bool is_highlevel)
        {
                video::IVideoDriver *driver = services->getVideoDriver();
-               assert(driver);
+               sanity_check(driver);
 
                // set inverted world matrix
                core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);
@@ -274,23 +273,23 @@ class ShaderSource : public IWritableShaderSource, public IShaderConstantSetterR
 
                The id 0 points to a null shader. Its material is EMT_SOLID.
        */
-       u32 getShaderIdDirect(const std::string &name, 
+       u32 getShaderIdDirect(const std::string &name,
                const u8 material_type, const u8 drawtype);
 
        /*
                If shader specified by the name pointed by the id doesn't
-               exist, create it, then return id. 
+               exist, create it, then return id.
 
                Can be called from any thread. If called from some other thread
                and not found in cache, the call is queued to the main thread
                for processing.
        */
-       
+
        u32 getShader(const std::string &name,
                const u8 material_type, const u8 drawtype);
-       
+
        ShaderInfo getShaderInfo(u32 id);
-       
+
        // Processes queued shader requests from other threads.
        // Shall be called from the main thread.
        void processQueue();
@@ -364,7 +363,7 @@ void load_shaders(std::string name, SourceShaderCache *sourcecache,
 ShaderSource::ShaderSource(IrrlichtDevice *device):
                m_device(device)
 {
-       assert(m_device);
+       assert(m_device); // Pre-condition
 
        m_shader_callback = new ShaderCallback(this, "default");
 
@@ -391,7 +390,7 @@ ShaderSource::~ShaderSource()
        }
 }
 
-u32 ShaderSource::getShader(const std::string &name, 
+u32 ShaderSource::getShader(const std::string &name,
                const u8 material_type, const u8 drawtype)
 {
        /*
@@ -435,7 +434,7 @@ u32 ShaderSource::getShader(const std::string &name,
 /*
        This method generates all the shaders
 */
-u32 ShaderSource::getShaderIdDirect(const std::string &name, 
+u32 ShaderSource::getShaderIdDirect(const std::string &name,
                const u8 material_type, const u8 drawtype)
 {
        //infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl;
@@ -494,7 +493,7 @@ ShaderInfo ShaderSource::getShaderInfo(u32 id)
 
 void ShaderSource::processQueue()
 {
+
 
 }
 
@@ -505,7 +504,7 @@ void ShaderSource::insertSourceShader(const std::string &name_of_shader,
                        "name_of_shader=\""<<name_of_shader<<"\", "
                        "filename=\""<<filename<<"\""<<std::endl;*/
 
-       assert(get_current_thread_id() == m_main_thread);
+       sanity_check(get_current_thread_id() == m_main_thread);
 
        m_sourcecache.insert(name_of_shader, filename, program, true);
 }
@@ -572,13 +571,13 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
                        shaderinfo.base_material = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
                break;
        }
-       
+
        bool enable_shaders = g_settings->getBool("enable_shaders");
        if(!enable_shaders)
                return shaderinfo;
 
        video::IVideoDriver* driver = device->getVideoDriver();
-       assert(driver);
+       sanity_check(driver);
 
        video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
        if(!gpu){
@@ -645,9 +644,10 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
                "NDT_RAILLIKE",
                "NDT_NODEBOX",
                "NDT_GLASSLIKE_FRAMED",
-               "NDT_FIRELIKE"
+               "NDT_FIRELIKE",
+               "NDT_GLASSLIKE_FRAMED_OPTIONAL"
        };
-       
+
        for (int i = 0; i < 14; i++){
                shaders_header += "#define ";
                shaders_header += drawTypes[i];
@@ -740,10 +740,10 @@ ShaderInfo generate_shader(std::string name, u8 material_type, u8 drawtype,
        shaders_header += "#define ENABLE_WAVING_LEAVES ";
        if (g_settings->getBool("enable_waving_leaves"))
                shaders_header += "1\n";
-       else    
+       else
                shaders_header += "0\n";
 
-       shaders_header += "#define ENABLE_WAVING_PLANTS ";              
+       shaders_header += "#define ENABLE_WAVING_PLANTS ";
        if (g_settings->getBool("enable_waving_plants"))
                shaders_header += "1\n";
        else