ソースを参照

~ effect parsing now takes into account combos and assigns the shader constants to the proper shader pass

Signed-off-by: Alexis Maiquez <almamu@almamu.com>
Alexis Maiquez 7 年 前
親
コミット
e32bc4d6f1

+ 66 - 62
src/WallpaperEngine/Core/Objects/CEffect.cpp

@@ -98,7 +98,7 @@ CEffect* CEffect::fromJSON (json data, Core::CObject* object)
         }
 
         effect->insertMaterial (
-                Images::CMaterial::fromFile ((*materialfile).get <std::string> ().c_str ())
+            Images::CMaterial::fromFile ((*materialfile).get <std::string> ().c_str ())
         );
     }
 
@@ -118,82 +118,96 @@ CEffect* CEffect::fromJSON (json data, Core::CObject* object)
         for (int passNumber = 0; cur != end; cur ++, passNumber ++)
         {
             auto constants_it = (*cur).find ("constantshadervalues");
-
-            if (constants_it == (*cur).end ())
-                continue;
-
-            auto constantCur = (*constants_it).begin ();
-            auto constantEnd = (*constants_it).end ();
-
-            for (; constantCur != constantEnd; constantCur ++)
-            {
-                Effects::CShaderConstant* constant = nullptr;
-
-                if ((*constantCur).is_number_float () == true)
-                {
-                    constant = new Effects::CShaderConstantFloat ((*constantCur).get <irr::f32> ());
-                }
-                else if ((*constantCur).is_number_integer () == true)
-                {
-                    constant = new Effects::CShaderConstantInteger ((*constantCur).get <irr::s32> ());
-                }
-                else if ((*constantCur).is_string () == true)
-                {
-                    constant = new Effects::CShaderConstantVector3 (WallpaperEngine::Core::ato3vf (*constantCur));
-                }
-                else
-                {
-                    throw std::runtime_error ("unknown shader constant type");
-                }
-
-                effect->insertConstant (constantCur.key (), constant);
-            }
-
+            auto combos_it = (*cur).find ("combos");
             auto textures_it = (*cur).find ("textures");
 
-            if (textures_it == (*cur).end ())
+            if (constants_it == (*cur).end () && combos_it == (*cur).end () && textures_it == (*cur).end ())
                 continue;
 
             Images::CMaterial* material = effect->getMaterials ().at (passNumber);
+
             auto passCur = material->getPasses ().begin ();
             auto passEnd = material->getPasses ().end ();
 
             for (; passCur != passEnd; passCur ++)
             {
-                auto texturesCur = (*textures_it).begin ();
-                auto texturesEnd = (*textures_it).end ();
-
-                for (int textureNumber = 0; texturesCur != texturesEnd; texturesCur ++)
+                if (textures_it != (*cur).end ())
                 {
-                    std::string texture;
+                    auto texturesCur = (*textures_it).begin ();
+                    auto texturesEnd = (*textures_it).end ();
 
-                    if ((*texturesCur).is_null () == true)
+                    for (int textureNumber = 0; texturesCur != texturesEnd; texturesCur ++)
                     {
-                        if (object->is<CImage>() == false)
+                        std::string texture;
+
+                        if ((*texturesCur).is_null () == true)
+                        {
+                            if (object->is <CImage> () == false)
+                            {
+                                throw std::runtime_error ("unexpected null texture for non-image object");
+                            }
+
+                            CImage* image = object->as <CImage> ();
+
+                            texture = (*(*image->getMaterial ()->getPasses ().begin ())->getTextures ()->begin ());
+                        }
+                        else
                         {
-                            throw std::runtime_error ("unexpected null texture for non-image object");
+                            texture = *texturesCur;
                         }
 
-                        CImage* image = object->as<CImage>();
+                        std::vector<std::string>* passTextures = (*passCur)->getTextures ();
+
+                        if (textureNumber < passTextures->size ())
+                            passTextures->at (textureNumber) = texture;
+                        else
+                            passTextures->push_back (texture);
 
-                        texture = (*(*image->getMaterial ()->getPasses ().begin ())->getTextures ()->begin ());
+                        textureNumber ++;
                     }
-                    else
+                }
+
+                if (combos_it != (*cur).end ())
+                {
+                    auto comboCur = (*combos_it).begin ();
+                    auto comboEnd = (*combos_it).end ();
+
+                    for (; comboCur != comboEnd; comboCur ++)
                     {
-                        texture = *texturesCur;
+                        (*passCur)->insertCombo (comboCur.key (), *comboCur);
                     }
+                }
+
+                if (constants_it != (*cur).end ())
+                {
+                    auto constantCur = (*constants_it).begin ();
+                    auto constantEnd = (*constants_it).end ();
 
-                    std::vector<std::string>* passTextures = (*passCur)->getTextures ();
+                    for (; constantCur != constantEnd; constantCur ++)
+                    {
+                        Effects::CShaderConstant* constant = nullptr;
 
-                    if (textureNumber < passTextures->size ())
-                        passTextures->at (textureNumber) = texture;
-                    else
-                        passTextures->push_back (texture);
+                        if ((*constantCur).is_number_float () == true)
+                        {
+                            constant = new Effects::CShaderConstantFloat ((*constantCur).get <irr::f32> ());
+                        }
+                        else if ((*constantCur).is_number_integer () == true)
+                        {
+                            constant = new Effects::CShaderConstantInteger ((*constantCur).get <irr::s32> ());
+                        }
+                        else if ((*constantCur).is_string () == true)
+                        {
+                            constant = new Effects::CShaderConstantVector3 (WallpaperEngine::Core::ato3vf (*constantCur));
+                        }
+                        else
+                        {
+                            throw std::runtime_error ("unknown shader constant type");
+                        }
 
-                    textureNumber ++;
+                        (*passCur)->insertConstant (constantCur.key (), constant);
+                    }
                 }
             }
-
         }
     }
 
@@ -210,11 +224,6 @@ const std::vector<Images::CMaterial*>& CEffect::getMaterials () const
     return this->m_materials;
 }
 
-const std::map<std::string, Effects::CShaderConstant*>& CEffect::getConstants () const
-{
-    return this->m_constants;
-}
-
 void CEffect::insertDependency (const std::string& dep)
 {
     this->m_dependencies.push_back (dep);
@@ -223,9 +232,4 @@ void CEffect::insertDependency (const std::string& dep)
 void CEffect::insertMaterial (Images::CMaterial* material)
 {
     this->m_materials.push_back (material);
-}
-
-void CEffect::insertConstant (const std::string& name, Effects::CShaderConstant* constant)
-{
-    this->m_constants.insert (std::pair <std::string, Effects::CShaderConstant*> (name, constant));
 }

+ 0 - 3
src/WallpaperEngine/Core/Objects/CEffect.h

@@ -31,11 +31,9 @@ namespace WallpaperEngine::Core::Objects
 
         const std::vector<std::string>& getDependencies () const;
         const std::vector<Images::CMaterial*>& getMaterials () const;
-        const std::map<std::string, Effects::CShaderConstant*>& getConstants () const;
     protected:
         void insertDependency (const std::string& dep);
         void insertMaterial (Images::CMaterial* material);
-        void insertConstant (const std::string& name, Effects::CShaderConstant* constant);
     private:
         std::string m_name;
         std::string m_description;
@@ -45,6 +43,5 @@ namespace WallpaperEngine::Core::Objects
 
         std::vector<std::string> m_dependencies;
         std::vector<Images::CMaterial*> m_materials;
-        std::map<std::string, Effects::CShaderConstant*> m_constants;
     };
 }

+ 11 - 0
src/WallpaperEngine/Core/Objects/Images/Materials/CPassess.cpp

@@ -1,5 +1,6 @@
 #include "CPassess.h"
 
+using namespace WallpaperEngine::Core::Objects::Effects;
 using namespace WallpaperEngine::Core::Objects::Images::Materials;
 
 CPassess::CPassess (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader) :
@@ -119,6 +120,11 @@ std::vector<std::string>* CPassess::getTextures ()
     return &this->m_textures;
 }
 
+const std::map<std::string, CShaderConstant*>& CPassess::getConstants () const
+{
+    return this->m_constants;
+}
+
 const std::map<std::string, int>& CPassess::getCombos () const
 {
     return this->m_combos;
@@ -147,4 +153,9 @@ const std::string& CPassess::getDepthTest () const
 const std::string& CPassess::getDepthWrite ()const
 {
     return this->m_depthwrite;
+}
+
+void CPassess::insertConstant (const std::string& name, CShaderConstant* constant)
+{
+    this->m_constants.insert (std::pair <std::string, CShaderConstant*> (name, constant));
 }

+ 7 - 1
src/WallpaperEngine/Core/Objects/Images/Materials/CPassess.h

@@ -2,6 +2,8 @@
 
 #include <nlohmann/json.hpp>
 
+#include "WallpaperEngine/Core/Objects/Effects/CShaderConstant.h"
+
 namespace WallpaperEngine::Core::Objects::Images::Materials
 {
     using json = nlohmann::json;
@@ -12,6 +14,7 @@ namespace WallpaperEngine::Core::Objects::Images::Materials
         static CPassess* fromJSON (json data);
 
         std::vector<std::string>* getTextures ();
+        const std::map<std::string, Effects::CShaderConstant*>& getConstants () const;
 
         const std::map<std::string, int>& getCombos () const;
         const std::string& getShader () const;
@@ -20,11 +23,13 @@ namespace WallpaperEngine::Core::Objects::Images::Materials
         const std::string& getDepthTest () const;
         const std::string& getDepthWrite () const;
 
+        void insertCombo (const std::string& name, int value);
+        void insertConstant (const std::string& name, Effects::CShaderConstant* constant);
+
     protected:
         CPassess (std::string blending, std::string cullmode, std::string depthtest, std::string depthwrite, std::string shader);
 
         void insertTexture (const std::string& texture);
-        void insertCombo (const std::string& name, int value);
 
     private:
         std::string m_blending;
@@ -34,5 +39,6 @@ namespace WallpaperEngine::Core::Objects::Images::Materials
         std::string m_shader;
         std::vector<std::string> m_textures;
         std::map<std::string, int> m_combos;
+        std::map<std::string, Core::Objects::Effects::CShaderConstant*> m_constants;
     };
 }

+ 31 - 34
src/WallpaperEngine/Render/Objects/CImage.cpp

@@ -174,47 +174,44 @@ void CImage::generatePass (Core::Objects::Images::Materials::CPassess* pass, Cor
             this, irr::video::EMT_TRANSPARENT_ALPHA_CHANNEL, this->m_passes, irr::video::EGSL_DEFAULT
         );
 
-    if (effect)
+    // find variables in the shaders and set the value with the constants if possible
+    auto cur = pass->getConstants ().begin ();
+    auto end = pass->getConstants ().end ();
+
+    for (; cur != end; cur ++)
     {
-        // find variables in the shaders and set the value with the constants if possible
-        auto cur = effect->getConstants ().begin ();
-        auto end = effect->getConstants ().end ();
+        CShaderVariable* vertexVar = vertexShader->findParameter ((*cur).first);
+        CShaderVariable* pixelVar = pixelShader->findParameter ((*cur).first);
 
-        for (; cur != end; cur ++)
+        if (pixelVar)
         {
-            CShaderVariable* vertexVar = vertexShader->findParameter ((*cur).first);
-            CShaderVariable* pixelVar = pixelShader->findParameter ((*cur).first);
-
-            if (pixelVar)
+            if (pixelVar->is <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ())
+            {
+                pixelVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ());
+            }
+            else if (pixelVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ())
+            {
+                pixelVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
+            }
+            else if (pixelVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
             {
-                if (pixelVar->is <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ())
-                {
-                    pixelVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ());
-                }
-                else if (pixelVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ())
-                {
-                    pixelVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
-                }
-                else if (pixelVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
-                {
-                    pixelVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
-                }
+                pixelVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
             }
+        }
 
-            if (vertexVar)
+        if (vertexVar)
+        {
+            if (vertexVar->is <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ())
+            {
+                vertexVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ());
+            }
+            else if (vertexVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ())
+            {
+                vertexVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
+            }
+            else if (vertexVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
             {
-                if (vertexVar->is <CShaderVariableFloat> () && (*cur).second->is <CShaderConstantFloat> ())
-                {
-                    vertexVar->as <CShaderVariableFloat> ()->setValue (*(*cur).second->as <CShaderConstantFloat> ()->getValue ());
-                }
-                else if (vertexVar->is <CShaderVariableInteger> () && (*cur).second->is <CShaderConstantInteger> ())
-                {
-                    vertexVar->as <CShaderVariableInteger> ()->setValue (*(*cur).second->as <CShaderConstantInteger> ()->getValue ());
-                }
-                else if (vertexVar->is <CShaderVariableVector3> () && (*cur).second->is <CShaderConstantVector3> ())
-                {
-                    vertexVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
-                }
+                vertexVar->as <CShaderVariableVector3> ()->setValue (*(*cur).second->as <CShaderConstantVector3> ()->getValue ());
             }
         }
     }