Browse Source

Fix alpha blend issue

Josh Engebretson 10 years ago
parent
commit
ed7dc04cab
2 changed files with 18 additions and 6 deletions
  1. 14 5
      Source/Atomic/Atomic2D/Renderer2D.cpp
  2. 4 1
      Source/Atomic/Atomic2D/Renderer2D.h

+ 14 - 5
Source/Atomic/Atomic2D/Renderer2D.cpp

@@ -72,6 +72,7 @@ Renderer2D::Renderer2D(Context* context) :
     pass->SetVertexShader("Atomic2D");
     pass->SetPixelShader("Atomic2D");
     pass->SetDepthWrite(false);
+    cachedTechniques_[BLEND_REPLACE] = tech;
 
     material_->SetTechnique(0, tech);
     material_->SetCullMode(CULL_NONE);
@@ -251,9 +252,6 @@ void Renderer2D::RemoveDrawable(Drawable2D* drawable)
 
 Material* Renderer2D::GetMaterial(Texture2D* texture, BlendMode blendMode)
 {
-    if (!material_)
-        return 0;
-
     if (!texture)
         return material_;
 
@@ -296,13 +294,24 @@ SharedPtr<Material> Renderer2D::CreateMaterial(Texture2D* texture, BlendMode ble
 {
     SharedPtr<Material> newMaterial = material_->Clone();
 
+    HashMap<int, SharedPtr<Technique> >::Iterator techIt = cachedTechniques_.Find((int)blendMode);
+    if (techIt == cachedTechniques_.End())
+    {
+        SharedPtr<Technique> tech(new Technique(context_));
+        Pass* pass = tech->CreatePass("alpha");
+        pass->SetVertexShader("Atomic2D");
+        pass->SetPixelShader("Atomic2D");
+        pass->SetDepthWrite(false);
+        pass->SetBlendMode(blendMode);
+        techIt = cachedTechniques_.Insert(MakePair((int)blendMode, tech));
+    }
+
+    newMaterial->SetTechnique(0, techIt->second_.Get());
     newMaterial->SetName(texture->GetName() + "_" + blendModeNames[blendMode]);
     newMaterial->SetTexture(TU_DIFFUSE, texture);
-    newMaterial->GetTechnique(0)->GetPass("alpha")->SetBlendMode(blendMode);
 
     return newMaterial;
 }
-
 void CheckDrawableVisibility(const WorkItem* item, unsigned threadIndex)
 {
     Renderer2D* renderer = reinterpret_cast<Renderer2D*>(item->aux_);

+ 4 - 1
Source/Atomic/Atomic2D/Renderer2D.h

@@ -30,6 +30,7 @@ namespace Atomic
 class Drawable2D;
 class IndexBuffer;
 class Material;
+class Technique;
 class VertexBuffer;
 struct FrameInfo;
 struct SourceBatch2D;
@@ -60,7 +61,7 @@ struct ViewBatchInfo2D
     Vector<SharedPtr<Geometry> > geometries_;
 };
 
-/// 2D renderer components.
+/// 2D renderer component.
 class ATOMIC_API Renderer2D : public Drawable
 {
     OBJECT(Renderer2D);
@@ -128,6 +129,8 @@ private:
     BoundingBox frustumBoundingBox_;
     /// Cached materials.
     HashMap<Texture2D*, HashMap<int, SharedPtr<Material> > > cachedMaterials_;
+    /// Cached techniques per blend mode.
+    HashMap<int, SharedPtr<Technique> > cachedTechniques_;
     /// Whether or not the renderer containts tris (default is quads)
     bool useTris_;
 };