Explorar o código

Fixed postprocess rendertarget allocation divisor mode to use the viewport size, not the whole rendertarget.
Refactored Material to not store textures in a Vector, as the amount of texture units is fixed.

Lasse Öörni %!s(int64=14) %!d(string=hai) anos
pai
achega
d76c21c4f2

+ 5 - 0
Bin/Data/PostProcess/GreyScale.xml

@@ -0,0 +1,5 @@
+<postprocess>
+    <pass vs="GreyScale" ps="GreyScale" output="viewport">
+        <texture unit="diffuse" name="viewport" />
+    </pass>
+</postprocess>

+ 1 - 1
Engine/Graphics/Batch.cpp

@@ -580,7 +580,7 @@ void Batch::Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransfo
                 graphics->SetShaderParameter(i->first_, i->second_.value_);
         }
         
-        const Vector<SharedPtr<Texture> >& textures = material_->GetTextures();
+        const SharedPtr<Texture>* textures = material_->GetTextures();
         if (graphics->NeedTextureUnit(TU_DIFFUSE))
             graphics->SetTexture(TU_DIFFUSE, textures[TU_DIFFUSE]);
         if (graphics->NeedTextureUnit(TU_NORMAL))

+ 6 - 8
Engine/Graphics/Material.cpp

@@ -99,7 +99,6 @@ Material::Material(Context* context) :
     occlusion_(true)
 {
     SetNumTechniques(1);
-    textures_.Resize(MAX_MATERIAL_TEXTURE_UNITS);
     
     // Setup often used default parameters
     SetShaderParameter("UOffset", Vector4(1.0f, 0.0f, 0.0f, 0.0f));
@@ -200,7 +199,7 @@ bool Material::Load(Deserializer& source)
     unsigned memoryUse = sizeof(Material);
     
     memoryUse += techniques_.Size() * sizeof(TechniqueEntry);
-    memoryUse += textures_.Size() * sizeof(SharedPtr<Texture>);
+    memoryUse += MAX_MATERIAL_TEXTURE_UNITS * sizeof(SharedPtr<Texture>);
     memoryUse += shaderParameters_.Size() * sizeof(MaterialShaderParameter);
     
     SetMemoryUse(memoryUse);
@@ -286,10 +285,8 @@ void Material::SetShaderParameter(const String& name, const Vector4& value)
 
 void Material::SetTexture(TextureUnit unit, Texture* texture)
 {
-    if (unit >= MAX_MATERIAL_TEXTURE_UNITS)
-        return;
-    
-    textures_[unit] = texture;
+    if (unit < MAX_MATERIAL_TEXTURE_UNITS)
+        textures_[unit] = texture;
 }
 
 void Material::SetUVTransform(const Vector2& offset, float rotation, const Vector2& repeat)
@@ -359,7 +356,8 @@ SharedPtr<Material> Material::Clone(const String& cloneName) const
     ret->SetName(cloneName);
     ret->techniques_ = techniques_;
     ret->shaderParameters_ = shaderParameters_;
-    ret->textures_ = textures_;
+    for (unsigned i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
+        ret->textures_[i] = textures_[i];
     ret->occlusion_ = occlusion_;
     ret->cullMode_ = cullMode_;
     ret->shadowCullMode_ = shadowCullMode_;
@@ -391,7 +389,7 @@ Pass* Material::GetPass(unsigned index, PassType pass) const
 
 Texture* Material::GetTexture(TextureUnit unit) const
 {
-    return (unsigned)unit < textures_.Size() ? textures_[unit] : (Texture*)0;
+    return unit < MAX_MATERIAL_TEXTURE_UNITS ? textures_[unit] : (Texture*)0;
 }
 
 const Vector4& Material::GetShaderParameter(const String& name) const

+ 3 - 3
Engine/Graphics/Material.h

@@ -114,10 +114,10 @@ public:
     Technique* GetTechnique(unsigned index) const;
     /// Return pass by technique index and pass type.
     Pass* GetPass(unsigned index, PassType pass) const;
-    /// Return all textures.
-    const Vector<SharedPtr<Texture> >& GetTextures() const { return textures_; }
     /// Return texture by unit.
     Texture* GetTexture(TextureUnit unit) const;
+   /// Return all textures.
+    const SharedPtr<Texture>* GetTextures() const { return &textures_[0]; }
     /// Return shader parameter.
     const Vector4& GetShaderParameter(const String& name) const;
     /// Return all shader parameters.
@@ -145,7 +145,7 @@ private:
     /// Techniques.
     Vector<TechniqueEntry> techniques_;
     /// Textures.
-    Vector<SharedPtr<Texture> > textures_;
+    SharedPtr<Texture> textures_[MAX_MATERIAL_TEXTURE_UNITS];
     /// Shader parameters.
     HashMap<StringHash, MaterialShaderParameter> shaderParameters_;
     /// Last auxiliary view rendered frame number.

+ 11 - 10
Engine/Graphics/View.cpp

@@ -1268,7 +1268,6 @@ void View::RunPostProcesses()
     graphics_->SetAlphaTest(false);
     graphics_->SetBlendMode(BLEND_REPLACE);
     graphics_->SetDepthTest(CMP_ALWAYS);
-    graphics_->SetDepthWrite(true);
     graphics_->SetScissorTest(false);
     graphics_->SetStencilTest(false);
     
@@ -1295,16 +1294,15 @@ void View::RunPostProcesses()
         for (HashMap<StringHash, PostProcessRenderTarget>::ConstIterator j = renderTargetInfos.Begin(); j !=
             renderTargetInfos.End(); ++j)
         {
+            unsigned width = j->second_.size_.x_;
+            unsigned height = j->second_.size_.y_;
             if (j->second_.sizeDivisor_)
             {
-                renderTargets[j->first_] = renderer_->GetScreenBuffer(rtSize_.x_ / j->second_.size_.x_,
-                    rtSize_.y_ / j->second_.size_.y_, j->second_.format_, j->second_.filtered_);
-            }
-            else
-            {
-                renderTargets[j->first_] = renderer_->GetScreenBuffer(j->second_.size_.x_, j->second_.size_.y_,
-                    j->second_.format_, j->second_.filtered_);
+                width = viewSize_.x_ / width;
+                height = viewSize_.y_ / height;
             }
+            
+            renderTargets[j->first_] = renderer_->GetScreenBuffer(width, height, j->second_.format_, j->second_.filtered_);
         }
         
         // Run each effect pass
@@ -1314,6 +1312,9 @@ void View::RunPostProcesses()
             bool lastPass = (i == lastActiveEffect) && (j == effect->GetNumPasses() - 1);
             bool swapBuffers = false;
             
+            // Write depth on the last pass only
+            graphics_->SetDepthWrite(lastPass);
+            
             // Set output rendertarget
             RenderSurface* rt = 0;
             String output = pass->GetOutput().ToLower();
@@ -2185,9 +2186,9 @@ Technique* View::GetTechnique(Drawable* drawable, Material*& material)
 
 void View::CheckMaterialForAuxView(Material* material)
 {
-    const Vector<SharedPtr<Texture> >& textures = material->GetTextures();
+    const SharedPtr<Texture>* textures = material->GetTextures();
     
-    for (unsigned i = 0; i < textures.Size(); ++i)
+    for (unsigned i = 0; i < MAX_MATERIAL_TEXTURE_UNITS; ++i)
     {
         // Have to check cube & 2D textures separately
         Texture* texture = textures[i];

+ 1 - 0
SourceAssets/GLSLShaders/CMakeLists.txt

@@ -6,6 +6,7 @@ add_shader (CopyFramebuffer)
 add_shader (EdgeFilter)
 add_shader (ForwardLit)
 add_shader (GBuffer)
+add_shader (GreyScale)
 add_shader (LightVolume)
 add_shader (LitParticle)
 add_shader (Material)

+ 12 - 0
SourceAssets/GLSLShaders/GreyScale.frag

@@ -0,0 +1,12 @@
+#include "Uniforms.frag"
+#include "Samplers.frag"
+#include "Lighting.frag"
+
+varying vec2 vScreenPos;
+
+void main()
+{
+    vec3 input = texture2D(sDiffMap, vScreenPos).rgb;
+    float intensity = GetIntensity(input);
+    gl_FragColor = vec4(intensity, intensity, intensity, 1.0);
+}

+ 13 - 0
SourceAssets/GLSLShaders/GreyScale.vert

@@ -0,0 +1,13 @@
+#include "Uniforms.vert"
+#include "Transform.vert"
+#include "ScreenPos.vert"
+
+varying vec2 vScreenPos;
+
+void main()
+{
+    mat4 modelMatrix = iModelMatrix;
+    vec3 worldPos = GetWorldPos(modelMatrix);
+    gl_Position = GetClipPos(worldPos);
+    vScreenPos = GetScreenPosPreDiv(gl_Position);
+}

+ 4 - 0
SourceAssets/GLSLShaders/GreyScale.xml

@@ -0,0 +1,4 @@
+<shaders>
+    <shader name="GreyScale" type="vs" />
+    <shader name="GreyScale" type="ps" />
+</shaders>

+ 1 - 0
SourceAssets/HLSLShaders/CMakeLists.txt

@@ -5,6 +5,7 @@ add_shader (Basic)
 add_shader (EdgeFilter)
 add_shader (ForwardLit)
 add_shader (GBuffer)
+add_shader (GreyScale)
 add_shader (LitParticle)
 add_shader (LightVolume)
 add_shader (Material)

+ 23 - 0
SourceAssets/HLSLShaders/GreyScale.hlsl

@@ -0,0 +1,23 @@
+#include "Uniforms.hlsl"
+#include "Samplers.hlsl"
+#include "Transform.hlsl"
+#include "ScreenPos.hlsl"
+#include "Lighting.hlsl"
+
+void VS(float4 iPos : POSITION,
+    out float4 oPos : POSITION,
+    out float2 oScreenPos : TEXCOORD0)
+{
+    float4x3 modelMatrix = iModelMatrix;
+    float3 worldPos = GetWorldPos(modelMatrix);
+    oPos = GetClipPos(worldPos);
+    oScreenPos = GetScreenPosPreDiv(oPos);
+}
+
+void PS(float2 iScreenPos : TEXCOORD0,
+    out float4 oColor : COLOR0)
+{
+    float3 input = Sample(sDiffMap, iScreenPos).rgb;
+    float intensity = GetIntensity(input);
+    oColor = float4(intensity, intensity, intensity, 1.0);
+}

+ 4 - 0
SourceAssets/HLSLShaders/GreyScale.xml

@@ -0,0 +1,4 @@
+<shaders>
+    <shader name="GreyScale" type="vs" />
+    <shader name="GreyScale" type="ps" />
+</shaders>