2
0
Эх сурвалжийг харах

Added back the "view texture" feature to prevent the final rendertarget being possibly sampled during light pre-pass rendering.

Lasse Öörni 14 жил өмнө
parent
commit
486867cb6c

+ 23 - 7
Engine/Graphics/Direct3D9/D3D9Graphics.cpp

@@ -1209,7 +1209,7 @@ void Graphics::SetTexture(unsigned index, Texture* texture)
     // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
     if (texture)
     {
-        if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
+        if (texture == viewTexture_ || (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture))
             texture = texture->GetBackupTexture();
     }
     
@@ -1349,11 +1349,11 @@ void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
     }
 }
 
-void Graphics::SetRenderTarget(unsigned index, Texture2D* renderTexture)
+void Graphics::SetRenderTarget(unsigned index, Texture2D* texture)
 {
     RenderSurface* renderTarget = 0;
-    if (renderTexture)
-        renderTarget = renderTexture->GetRenderSurface();
+    if (texture)
+        renderTarget = texture->GetRenderSurface();
     
     SetRenderTarget(index, renderTarget);
 }
@@ -1378,15 +1378,29 @@ void Graphics::SetDepthStencil(RenderSurface* depthStencil)
     }
 }
 
-void Graphics::SetDepthStencil(Texture2D* depthTexture)
+void Graphics::SetDepthStencil(Texture2D* texture)
 {
     RenderSurface* depthStencil = 0;
-    if (depthTexture)
-        depthStencil = depthTexture->GetRenderSurface();
+    if (texture)
+        depthStencil = texture->GetRenderSurface();
     
     SetDepthStencil(depthStencil);
 }
 
+void Graphics::SetViewTexture(Texture* texture)
+{
+    viewTexture_ = texture;
+    
+    if (viewTexture_)
+    {
+        for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
+        {
+            if (textures_[i] == viewTexture_)
+                SetTexture(i, textures_[i]->GetBackupTexture());
+        }
+    }
+}
+
 void Graphics::SetViewport(const IntRect& rect)
 {
     IntVector2 size = GetRenderTargetDimensions();
@@ -2208,8 +2222,10 @@ void Graphics::ResetCachedState()
         renderTargets_[i] = 0;
         impl_->colorSurfaces_[i] = 0;
     }
+    
     depthStencil_ = 0;
     impl_->depthStencilSurface_ = 0;
+    viewTexture_ = 0;
     viewport_ = IntRect(0, 0, width_, height_);
     
     for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)

+ 6 - 2
Engine/Graphics/Direct3D9/D3D9Graphics.h

@@ -141,11 +141,13 @@ public:
     /// %Set render target.
     void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
     /// %Set render target.
-    void SetRenderTarget(unsigned index, Texture2D* renderTexture);
+    void SetRenderTarget(unsigned index, Texture2D* texture);
     /// %Set depth stencil buffer.
     void SetDepthStencil(RenderSurface* depthStencil);
     /// %Set depth stencil buffer.
-    void SetDepthStencil(Texture2D* depthTexture);
+    void SetDepthStencil(Texture2D* texture);
+    /// %Set view texture (light pre-pass final output render target) to prevent it from being sampled.
+    void SetViewTexture(Texture* texture);
     /// %Set viewport.
     void SetViewport(const IntRect& rect);
     /// %Set alpha test.
@@ -446,6 +448,8 @@ private:
     RenderSurface* depthStencil_;
     /// Backbuffer depth stencil texture.
     SharedPtr<Texture2D> depthTexture_;
+    /// View texture.
+    Texture* viewTexture_;
     /// Viewport coordinates.
     IntRect viewport_;
     /// Alpha test enable flag.

+ 22 - 7
Engine/Graphics/OpenGL/OGLGraphics.cpp

@@ -1024,7 +1024,7 @@ void Graphics::SetTexture(unsigned index, Texture* texture)
     // Check if texture is currently bound as a render target. In that case, use its backup texture, or blank if not defined
     if (texture)
     {
-        if (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture)
+        if (texture == viewTexture_ || (renderTargets_[0] && renderTargets_[0]->GetParentTexture() == texture))
             texture = texture->GetBackupTexture();
     }
     
@@ -1202,11 +1202,11 @@ void Graphics::SetRenderTarget(unsigned index, RenderSurface* renderTarget)
     }
 }
 
-void Graphics::SetRenderTarget(unsigned index, Texture2D* renderTexture)
+void Graphics::SetRenderTarget(unsigned index, Texture2D* texture)
 {
     RenderSurface* renderTarget = 0;
-    if (renderTexture)
-        renderTarget = renderTexture->GetRenderSurface();
+    if (texture)
+        renderTarget = texture->GetRenderSurface();
     
     SetRenderTarget(index, renderTarget);
 }
@@ -1311,15 +1311,29 @@ void Graphics::SetDepthStencil(RenderSurface* depthStencil)
     SetViewport(IntRect(0, 0, viewSize.x_, viewSize.y_));
 }
 
-void Graphics::SetDepthStencil(Texture2D* depthTexture)
+void Graphics::SetDepthStencil(Texture2D* texture)
 {
     RenderSurface* depthStencil = 0;
-    if (depthTexture)
-        depthStencil = depthTexture->GetRenderSurface();
+    if (texture)
+        depthStencil = texture->GetRenderSurface();
     
     SetDepthStencil(depthStencil);
 }
 
+void Graphics::SetViewTexture(Texture* texture)
+{
+    viewTexture_ = texture;
+    
+    if (viewTexture_)
+    {
+        for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
+        {
+            if (textures_[i] == viewTexture_)
+                SetTexture(i, textures_[i]->GetBackupTexture());
+        }
+    }
+}
+
 void Graphics::SetViewport(const IntRect& rect)
 {
     IntVector2 rtSize = GetRenderTargetDimensions();
@@ -1832,6 +1846,7 @@ void Graphics::ResetCachedState()
         renderTargets_[i] = 0;
     
     depthStencil_ = 0;
+    viewTexture_ = 0;
     viewport_ = IntRect(0, 0, 0, 0);
     indexBuffer_ = 0;
     vertexShader_ = 0;

+ 6 - 2
Engine/Graphics/OpenGL/OGLGraphics.h

@@ -163,11 +163,13 @@ public:
     /// Set render target.
     void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
     /// Set render target.
-    void SetRenderTarget(unsigned index, Texture2D* renderTexture);
+    void SetRenderTarget(unsigned index, Texture2D* texture);
     /// Set depth stencil buffer.
     void SetDepthStencil(RenderSurface* depthStencil);
     /// Set depth stencil buffer.
-    void SetDepthStencil(Texture2D* depthTexture);
+    void SetDepthStencil(Texture2D* texture);
+    /// %Set view texture (light pre-pass final output render target) to prevent it from being sampled.
+    void SetViewTexture(Texture* texture);
     /// Set viewport.
     void SetViewport(const IntRect& rect);
     /// Set alpha test.
@@ -422,6 +424,8 @@ private:
     RenderSurface* renderTargets_[MAX_RENDERTARGETS];
     /// Depth stencil buffer in use.
     RenderSurface* depthStencil_;
+    /// View texture.
+    Texture* viewTexture_;
     /// Viewport coordinates.
     IntRect viewport_;
     /// Alpha test enable flag.

+ 5 - 0
Engine/Graphics/View.cpp

@@ -995,6 +995,9 @@ void View::RenderBatchesForward()
 
 void View::RenderBatchesLightPrepass()
 {
+    if (renderTarget_)
+        graphics_->SetViewTexture(renderTarget_->GetParentTexture());
+    
     // If not reusing shadowmaps, render all of them first
     if (!renderer_->GetReuseShadowMaps() && renderer_->GetDrawShadows() && !lightQueues_.Empty())
     {
@@ -1147,6 +1150,8 @@ void View::RenderBatchesLightPrepass()
     graphics_->SetTexture(TU_DIFFUSE, normalBuffer);
     DrawFullscreenQuad(camera_, false);
     #endif
+    
+    graphics_->SetViewTexture(0);
 }
 
 void View::UpdateOccluders(PODVector<Drawable*>& occluders, Camera* camera)