[email protected] 8 years ago
parent
commit
6978813935

+ 3 - 3
examples/Demo/src/TestOpenGL.h

@@ -75,7 +75,7 @@ public:
 
 
     void doRender(const RenderState& rs)
     void doRender(const RenderState& rs)
     {
     {
-		MaterialX::null->apply();
+        MaterialX::null->apply();
 
 
 
 
         glEnable(GL_BLEND);
         glEnable(GL_BLEND);
@@ -142,8 +142,8 @@ public:
 
 
         CHECKGL();
         CHECKGL();
 
 
-		//reset states to defaults after direct opengl usage
-		rc().reset();
+        //reset states to defaults after direct opengl usage
+        rc().reset();
     }
     }
 };
 };
 
 

+ 2 - 0
oxygine/SDL/win32/oxygine.vcxproj

@@ -169,6 +169,7 @@
     <ClCompile Include="..\..\src\oxygine\MaskedSprite.cpp" />
     <ClCompile Include="..\..\src\oxygine\MaskedSprite.cpp" />
     <ClCompile Include="..\..\src\oxygine\Material.cpp" />
     <ClCompile Include="..\..\src\oxygine\Material.cpp" />
     <ClCompile Include="..\..\src\oxygine\Image.cpp" />
     <ClCompile Include="..\..\src\oxygine\Image.cpp" />
+    <ClCompile Include="..\..\src\oxygine\MaterialCache.cpp" />
     <ClCompile Include="..\..\src\oxygine\MaterialX.cpp" />
     <ClCompile Include="..\..\src\oxygine\MaterialX.cpp" />
     <ClCompile Include="..\..\src\oxygine\minizip\ioapi.c" />
     <ClCompile Include="..\..\src\oxygine\minizip\ioapi.c" />
     <ClCompile Include="..\..\src\oxygine\minizip\ioapi_mem.c" />
     <ClCompile Include="..\..\src\oxygine\minizip\ioapi_mem.c" />
@@ -282,6 +283,7 @@
     <ClInclude Include="..\..\src\oxygine\MaskedRenderer.h" />
     <ClInclude Include="..\..\src\oxygine\MaskedRenderer.h" />
     <ClInclude Include="..\..\src\oxygine\MaskedSprite.h" />
     <ClInclude Include="..\..\src\oxygine\MaskedSprite.h" />
     <ClInclude Include="..\..\src\oxygine\Material.h" />
     <ClInclude Include="..\..\src\oxygine\Material.h" />
+    <ClInclude Include="..\..\src\oxygine\MaterialCache.h" />
     <ClInclude Include="..\..\src\oxygine\MaterialX.h" />
     <ClInclude Include="..\..\src\oxygine\MaterialX.h" />
     <ClInclude Include="..\..\src\oxygine\math\OBBox.h" />
     <ClInclude Include="..\..\src\oxygine\math\OBBox.h" />
     <ClInclude Include="..\..\src\oxygine\Image.h" />
     <ClInclude Include="..\..\src\oxygine\Image.h" />

+ 6 - 0
oxygine/SDL/win32/oxygine.vcxproj.filters

@@ -351,6 +351,9 @@
     <ClCompile Include="..\..\src\oxygine\MaterialX.cpp">
     <ClCompile Include="..\..\src\oxygine\MaterialX.cpp">
       <Filter>src</Filter>
       <Filter>src</Filter>
     </ClCompile>
     </ClCompile>
+    <ClCompile Include="..\..\src\oxygine\MaterialCache.cpp">
+      <Filter>src</Filter>
+    </ClCompile>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\oxygine\closure\closure.h">
     <ClInclude Include="..\..\src\oxygine\closure\closure.h">
@@ -755,6 +758,9 @@
     <ClInclude Include="..\..\src\oxygine\MaterialX.h">
     <ClInclude Include="..\..\src\oxygine\MaterialX.h">
       <Filter>src</Filter>
       <Filter>src</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="..\..\src\oxygine\MaterialCache.h">
+      <Filter>src</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <None Include="ReadMe.txt" />
     <None Include="ReadMe.txt" />

+ 1 - 1
oxygine/src/oxygine-framework.h

@@ -48,7 +48,7 @@
 #include "oxygine/TweenGlow.h"
 #include "oxygine/TweenGlow.h"
 #include "oxygine/TweenAlphaFade.h"
 #include "oxygine/TweenAlphaFade.h"
 #include "oxygine/MaterialX.h"
 #include "oxygine/MaterialX.h"
-
+#include "oxygine/MaterialCache.h"
 
 
 //closure/
 //closure/
 #include "oxygine/closure/closure.h"
 #include "oxygine/closure/closure.h"

+ 1 - 0
oxygine/src/oxygine/ColorRectSprite.cpp

@@ -3,6 +3,7 @@
 #include "Material.h"
 #include "Material.h"
 #include "Serialize.h"
 #include "Serialize.h"
 #include "utils/stringUtils.h"
 #include "utils/stringUtils.h"
+#include "MaterialCache.h"
 
 
 namespace oxygine
 namespace oxygine
 {
 {

+ 2 - 1
oxygine/src/oxygine/DebugActor.cpp

@@ -10,6 +10,7 @@
 #include "core/ZipFileSystem.h"
 #include "core/ZipFileSystem.h"
 #include "core/system_data.h"
 #include "core/system_data.h"
 #include "STDRenderer.h"
 #include "STDRenderer.h"
+#include "MaterialCache.h"
 
 
 #include "dev_tools/DeveloperMenu.h"
 #include "dev_tools/DeveloperMenu.h"
 #include "dev_tools/TreeInspector.h"
 #include "dev_tools/TreeInspector.h"
@@ -348,7 +349,7 @@ namespace oxygine
         s << "update=" << aligned(getStage()->_statUpdate, 2) << "ms ";
         s << "update=" << aligned(getStage()->_statUpdate, 2) << "ms ";
         s << "render=" << aligned(vstats.duration, 2) << "ms ";
         s << "render=" << aligned(vstats.duration, 2) << "ms ";
         s << "textures=" << aligned(NativeTexture::created, 2) << " ";
         s << "textures=" << aligned(NativeTexture::created, 2) << " ";
-        s << "mats=" << aligned(mc()._materials.size(), 2) << " ";
+        s << "mats=" << aligned(mc().getTotalMaterials(), 2) << " ";
 
 
 #ifdef __APPLE__
 #ifdef __APPLE__
         size_t mem;
         size_t mem;

+ 54 - 0
oxygine/src/oxygine/MaterialCache.cpp

@@ -0,0 +1,54 @@
+#include "MaterialCache.h"
+#include "core/oxygine.h"
+#include "MaterialX.h"
+namespace oxygine
+{
+    MaterialX* MaterialCache::clone_(const MaterialX& other)
+    {
+        OX_ASSERT(core::isMainThread());
+        size_t hash;
+        MaterialX::compare cm;
+        other.update(hash, cm);
+
+        MaterialX* free = 0;
+        for (auto m_ : _materials)
+        {
+            MaterialX* mat = m_.get();
+            if (mat->_compare != cm)
+                continue;
+            if (mat->_ref_counter == 1)
+                free = mat;
+            if (mat->_hash != hash)
+                continue;
+            bool same = cm(mat, &other);
+            if (same)
+                return mat;
+        }
+        if (free)
+        {
+            free->copyFrom(other);
+            free->_hash = hash;
+            free->_compare = cm;
+            return free;
+        }
+
+        MaterialX* copy = other.clone();
+        copy->_hash = hash;
+        copy->_compare = cm;
+        _materials.push_back(copy);
+
+        return copy;
+    }
+
+    void MaterialCache::clear()
+    {
+        _materials.clear();
+    }
+
+    static MaterialCache mcache;
+
+    MaterialCache& mc()
+    {
+        return mcache;
+    }
+}

+ 42 - 0
oxygine/src/oxygine/MaterialCache.h

@@ -0,0 +1,42 @@
+#pragma once
+#include "oxygine-include.h"
+#include "core/ref_counter.h"
+#include "core/intrusive_ptr.h"
+#include <vector>
+
+namespace oxygine
+{
+    inline void hash_combine(std::size_t& seed) { }
+
+    template <typename T, typename... Rest>
+    inline void hash_combine(std::size_t& seed, const T& v, Rest... rest)
+    {
+        std::hash<T> hasher;
+        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
+        hash_combine(seed, rest...);
+    }
+
+    class MaterialX;
+    typedef intrusive_ptr<MaterialX> spMaterialX;
+
+    class MaterialCache
+    {
+    public:
+        template<class T>
+        intrusive_ptr<T> cache(const T& other)
+        {
+            return (T*)clone_(other);
+        }
+
+        void clear();
+        size_t getTotalMaterials() const { return _materials.size(); }
+
+    protected:
+        typedef std::vector<spMaterialX> materials;
+        materials _materials;
+
+        MaterialX* clone_(const MaterialX& other);
+    };
+
+    MaterialCache& mc();
+}

+ 2 - 1
oxygine/src/oxygine/MaterialX.cpp

@@ -1,6 +1,8 @@
 #include "MaterialX.h"
 #include "MaterialX.h"
 #include "core/UberShaderProgram.h"
 #include "core/UberShaderProgram.h"
+#include "core/oxygine.h"
 #include "STDRenderer.h"
 #include "STDRenderer.h"
+#include "MaterialCache.h"
 
 
 namespace oxygine
 namespace oxygine
 {
 {
@@ -104,5 +106,4 @@ namespace oxygine
         _hash = r._hash;
         _hash = r._hash;
         return *this;
         return *this;
     }
     }
-
 }
 }

+ 1 - 69
oxygine/src/oxygine/MaterialX.h

@@ -1,4 +1,5 @@
 #pragma once
 #pragma once
+#include "oxygine-include.h"
 #include "core/ref_counter.h"
 #include "core/ref_counter.h"
 #include "core/Renderer.h"
 #include "core/Renderer.h"
 
 
@@ -105,73 +106,4 @@ namespace oxygine
     };
     };
 
 
     DECLARE_SMART(STDMaterialX, spSTDMaterialX);
     DECLARE_SMART(STDMaterialX, spSTDMaterialX);
-
-
-
-    inline void hash_combine(std::size_t& seed) { }
-
-    template <typename T, typename... Rest>
-    inline void hash_combine(std::size_t& seed, const T& v, Rest... rest)
-    {
-        std::hash<T> hasher;
-        seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
-        hash_combine(seed, rest...);
-    }
-
-
-    class MaterialCache
-    {
-    public:
-
-        typedef std::vector<spMaterialX> materials;
-        materials _materials;
-
-
-        template<class T>
-        intrusive_ptr<T> cache(const T& other)
-        {
-            size_t hash;
-            MaterialX::compare cm;
-            other.update(hash, cm);
-
-            T* fre = 0;
-            for (auto m_ : _materials)
-            {
-                MaterialX* mat = m_.get();
-                if (mat->_compare != cm)
-                    continue;
-                if (mat->_ref_counter == 1)
-                    fre = (T*)mat;
-                if (mat->_hash != hash)
-                    continue;
-                bool same = cm(mat, &other);
-                if (same)
-                    return (T*)mat;
-            }
-            if (fre)
-            {
-                fre->copyFrom(other);
-                fre->_hash = hash;
-                fre->_compare = cm;
-                return fre;
-            }
-
-            T* copy = other.clone();
-            copy->_hash = hash;
-            copy->_compare = cm;
-            _materials.push_back(copy);
-
-            return copy;
-        }
-    };
-
-    inline MaterialCache& mc()
-    {
-        static MaterialCache cache;
-        return cache;
-    }
-
-
-    extern spMaterialX currentMat;
-
 }
 }

+ 0 - 1
oxygine/src/oxygine/STDMaterial.cpp

@@ -212,7 +212,6 @@ namespace oxygine
 
 
         dc.primary = rs.getFinalColor(tf->getColor()).premultiplied();
         dc.primary = rs.getFinalColor(tf->getColor()).premultiplied();
         dc.color = tf->getStyle().color * dc.primary;
         dc.color = tf->getStyle().color * dc.primary;
-        dc.renderer = renderer;
 
 
         //renderer->setBlendMode(tf->getBlendMode());
         //renderer->setBlendMode(tf->getBlendMode());
         renderer->setTransform(rs.transform);
         renderer->setTransform(rs.transform);

+ 0 - 10
oxygine/src/oxygine/STDRenderer.cpp

@@ -347,12 +347,6 @@ namespace oxygine
         _sphookLast->next = 0;
         _sphookLast->next = 0;
     }
     }
 
 
-    void STDRenderer::resetSettings()
-    {
-        xresetSettings();
-        //_driver->setState(IVideoDriver::STATE_BLEND, 0);
-    }
-
     void STDRenderer::begin()
     void STDRenderer::begin()
     {
     {
         OX_ASSERT(_verticesData.empty() == true);
         OX_ASSERT(_verticesData.empty() == true);
@@ -360,7 +354,6 @@ namespace oxygine
         _transform.identity();
         _transform.identity();
 
 
         MaterialX::null->apply();
         MaterialX::null->apply();
-        resetSettings();
 
 
         xbegin();
         xbegin();
 
 
@@ -545,9 +538,6 @@ namespace oxygine
         begin();
         begin();
     }
     }
 
 
-    void STDRenderer::xresetSettings()
-    {
-    }
 
 
     void STDRenderer::addQuad(const Color& clr, const RectF& srcRect, const RectF& destRect)
     void STDRenderer::addQuad(const Color& clr, const RectF& srcRect, const RectF& destRect)
     {
     {

+ 1 - 3
oxygine/src/oxygine/STDRenderer.h

@@ -39,7 +39,7 @@ namespace oxygine
         std::function< void() > hook;
         std::function< void() > hook;
     };
     };
 
 
-    class STDRenderer : public IElementRenderer, public ShaderProgramChangedHook
+    class STDRenderer : public ShaderProgramChangedHook
     {
     {
     public:
     public:
 
 
@@ -96,7 +96,6 @@ namespace oxygine
         void end();
         void end();
         /**initializes View + Projection matrices where TopLeft is (0,0) and RightBottom is (width, height). use flipU = true for render to texture*/
         /**initializes View + Projection matrices where TopLeft is (0,0) and RightBottom is (width, height). use flipU = true for render to texture*/
         void initCoordinateSystem(int width, int height, bool flipU = false);
         void initCoordinateSystem(int width, int height, bool flipU = false);
-        void resetSettings();
 
 
         /**Draws existing batch immediately.*/
         /**Draws existing batch immediately.*/
         void flush();
         void flush();
@@ -141,7 +140,6 @@ namespace oxygine
         Matrix _vp;
         Matrix _vp;
 
 
         virtual void xbegin();
         virtual void xbegin();
-        virtual void xresetSettings();
 
 
         ShaderProgramChangedHook* _sphookFirst;
         ShaderProgramChangedHook* _sphookFirst;
         ShaderProgramChangedHook* _sphookLast;
         ShaderProgramChangedHook* _sphookLast;

+ 1 - 0
oxygine/src/oxygine/Sprite.cpp

@@ -7,6 +7,7 @@
 #include "Serialize.h"
 #include "Serialize.h"
 #include "Material.h"
 #include "Material.h"
 #include "core/UberShaderProgram.h"
 #include "core/UberShaderProgram.h"
+#include "MaterialCache.h"
 
 
 namespace oxygine
 namespace oxygine
 {
 {

+ 0 - 3
oxygine/src/oxygine/TextField.h

@@ -37,10 +37,7 @@ namespace oxygine
         bool                        getMultiline() const;
         bool                        getMultiline() const;
         bool                        getBreakLongWords() const;
         bool                        getBreakLongWords() const;
         text::Symbol*               getSymbolAt(int pos) const;
         text::Symbol*               getSymbolAt(int pos) const;
-        const Color&                getOutlineColor() const;
-        float                       getOutline() const;
         const Color&                getStyleColor() const;
         const Color&                getStyleColor() const;
-        float                       getWeight() const;
         float                       getBaselineScale() const;
         float                       getBaselineScale() const;
         unsigned int                getOptions() const;
         unsigned int                getOptions() const;
 
 

+ 1 - 0
oxygine/src/oxygine/VisualStyle.cpp

@@ -4,6 +4,7 @@
 #include "RenderState.h"
 #include "RenderState.h"
 #include "Serialize.h"
 #include "Serialize.h"
 #include "utils/stringUtils.h"
 #include "utils/stringUtils.h"
+#include "MaterialCache.h"
 
 
 namespace oxygine
 namespace oxygine
 {
 {

+ 0 - 6
oxygine/src/oxygine/core/Renderer.h

@@ -217,12 +217,6 @@ namespace oxygine
     class MaterialX;
     class MaterialX;
     typedef intrusive_ptr<MaterialX> spMaterialX;
     typedef intrusive_ptr<MaterialX> spMaterialX;
 
 
-    class IElementRenderer
-    {
-    public:
-        virtual ~IElementRenderer() {}
-    };
-
     /**Returns View matrix where Left Top corner is (0,0), and right bottom is (w,h)*/
     /**Returns View matrix where Left Top corner is (0,0), and right bottom is (w,h)*/
     Matrix makeViewMatrix(int w, int h, bool flipU = false);
     Matrix makeViewMatrix(int w, int h, bool flipU = false);
 }
 }

+ 2 - 0
oxygine/src/oxygine/core/oxygine.cpp

@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdio.h>
 #include "log.h"
 #include "log.h"
 #include "VideoDriver.h"
 #include "VideoDriver.h"
+#include "../MaterialCache.h"
 #include "../res/CreateResourceContext.h"
 #include "../res/CreateResourceContext.h"
 #include "../res/Resources.h"
 #include "../res/Resources.h"
 
 
@@ -862,6 +863,7 @@ namespace oxygine
             Event ev(EVENT_EXIT);
             Event ev(EVENT_EXIT);
             _dispatcher->dispatchEvent(&ev);
             _dispatcher->dispatchEvent(&ev);
 
 
+            mc().clear();
 
 
             STDRenderer::release();
             STDRenderer::release();
             delete STDMaterial::instance;
             delete STDMaterial::instance;

+ 1 - 0
oxygine/src/oxygine/oxygine-forwards.h

@@ -68,6 +68,7 @@ namespace oxygine
     DECLARE_SMART(Tween, spTween);
     DECLARE_SMART(Tween, spTween);
     DECLARE_SMART(TweenQueue, spTweenQueue);
     DECLARE_SMART(TweenQueue, spTweenQueue);
     DECLARE_SMART(WebImage, spWebImage);
     DECLARE_SMART(WebImage, spWebImage);
+    DECLARE_SMART(MaterialX, spMaterialX);
 
 
     class Actor;
     class Actor;
     class AnimationFrame;
     class AnimationFrame;

+ 1 - 1
oxygine/src/oxygine/text_utils/Node.cpp

@@ -6,7 +6,7 @@
 #include "../AnimationFrame.h"
 #include "../AnimationFrame.h"
 #include "../res/ResFont.h"
 #include "../res/ResFont.h"
 #include "STDMaterial.h"
 #include "STDMaterial.h"
-
+#include "../MaterialCache.h"
 namespace oxygine
 namespace oxygine
 {
 {
     namespace text
     namespace text

+ 1 - 4
oxygine/src/oxygine/text_utils/Node.h

@@ -20,8 +20,6 @@ namespace oxygine
     class Aligner;
     class Aligner;
     class STDRenderer;
     class STDRenderer;
     class RenderState;
     class RenderState;
-    class TextRenderer2;
-    class IElementRenderer;
 
 
     namespace text
     namespace text
     {
     {
@@ -31,11 +29,10 @@ namespace oxygine
         class DrawContext
         class DrawContext
         {
         {
         public:
         public:
-            DrawContext(): renderer(0) {}
+            DrawContext() {}
 
 
             Color color;
             Color color;
             Color primary;
             Color primary;
-            IElementRenderer* renderer;
         };
         };
 
 
         class Node: public PoolObject
         class Node: public PoolObject