Browse Source

Fixed vertex position not getting passed to pixel shader. Rework the mechanism used to keep track of dirty constant buffers.

Lasse Öörni 10 years ago
parent
commit
d4524e25c7

+ 124 - 33
Source/Urho3D/Graphics/Direct3D11/D3D11Graphics.cpp

@@ -930,9 +930,19 @@ void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, count * sizeof(float), data);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, count * sizeof(float), data);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, count * sizeof(float), data);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, count * sizeof(float), data);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, float value)
 void Graphics::SetShaderParameter(StringHash param, float value)
@@ -942,9 +952,19 @@ void Graphics::SetShaderParameter(StringHash param, float value)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(float), &value);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(float), &value);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(float), &value);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(float), &value);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, bool value)
 void Graphics::SetShaderParameter(StringHash param, bool value)
@@ -954,9 +974,19 @@ void Graphics::SetShaderParameter(StringHash param, bool value)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(bool), &value);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(bool), &value);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(bool), &value);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(bool), &value);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Color& color)
 void Graphics::SetShaderParameter(StringHash param, const Color& color)
@@ -966,9 +996,19 @@ void Graphics::SetShaderParameter(StringHash param, const Color& color)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Color), &color);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Color), &color);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Color), &color);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Color), &color);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Vector2& vector)
 void Graphics::SetShaderParameter(StringHash param, const Vector2& vector)
@@ -978,9 +1018,19 @@ void Graphics::SetShaderParameter(StringHash param, const Vector2& vector)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector2), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector2), &vector);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector2), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector2), &vector);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
 void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
@@ -990,9 +1040,19 @@ void Graphics::SetShaderParameter(StringHash param, const Matrix3& matrix)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetVector3ArrayParameter(i->second_.offset_, 3, &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetVector3ArrayParameter(i->second_.offset_, 3, &matrix);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetVector3ArrayParameter(i->second_.offset_, 3, &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetVector3ArrayParameter(i->second_.offset_, 3, &matrix);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
 void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
@@ -1002,9 +1062,19 @@ void Graphics::SetShaderParameter(StringHash param, const Vector3& vector)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector3), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector3), &vector);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector3), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector3), &vector);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
 void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
@@ -1014,9 +1084,19 @@ void Graphics::SetShaderParameter(StringHash param, const Matrix4& matrix)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Matrix4), &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Matrix4), &matrix);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Matrix4), &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Matrix4), &matrix);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
 void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
@@ -1026,9 +1106,19 @@ void Graphics::SetShaderParameter(StringHash param, const Vector4& vector)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector4), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector4), &vector);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Vector4), &vector);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Vector4), &vector);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
 void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
@@ -1038,9 +1128,19 @@ void Graphics::SetShaderParameter(StringHash param, const Matrix3x4& matrix)
         return;
         return;
 
 
     if (i->second_.type_ == VS)
     if (i->second_.type_ == VS)
-        shaderProgram_->vsConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Matrix3x4), &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->vsConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Matrix3x4), &matrix);
+    }
     else
     else
-        shaderProgram_->psConstantBuffers_[i->second_.buffer_]->SetParameter(i->second_.offset_, sizeof(Matrix3x4), &matrix);
+    {
+        ConstantBuffer* buffer = shaderProgram_->psConstantBuffers_[i->second_.buffer_];
+        if (!buffer->IsDirty())
+            dirtyConstantBuffers_.Push(buffer);
+        buffer->SetParameter(i->second_.offset_, sizeof(Matrix3x4), &matrix);
+    }
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const Variant& value)
 void Graphics::SetShaderParameter(StringHash param, const Variant& value)
@@ -2258,6 +2358,7 @@ void Graphics::ResetCachedState()
     depthStateHash_ = M_MAX_UNSIGNED;
     depthStateHash_ = M_MAX_UNSIGNED;
     rasterizerStateHash_ = M_MAX_UNSIGNED;
     rasterizerStateHash_ = M_MAX_UNSIGNED;
     firstDirtyTexture_ = lastDirtyTexture_ = M_MAX_UNSIGNED;
     firstDirtyTexture_ = lastDirtyTexture_ = M_MAX_UNSIGNED;
+    dirtyConstantBuffers_.Clear();
 }
 }
 
 
 void Graphics::PrepareDraw()
 void Graphics::PrepareDraw()
@@ -2447,19 +2548,9 @@ void Graphics::PrepareDraw()
         scissorRectDirty_ = false;
         scissorRectDirty_ = false;
     }
     }
 
 
-    if (shaderProgram_)
-    {
-        for (unsigned i = 0; i < MAX_SHADER_PARAMETER_GROUPS; ++i)
-        {
-            ConstantBuffer* cb = shaderProgram_->vsConstantBuffers_[i];
-            if (cb && cb->IsDirty())
-                cb->Apply();
-
-            cb = shaderProgram_->psConstantBuffers_[i];
-            if (cb && cb->IsDirty())
-                cb->Apply();
-        }
-    }
+    for (unsigned i = 0; i < dirtyConstantBuffers_.Size(); ++i)
+        dirtyConstantBuffers_[i]->Apply();
+    dirtyConstantBuffers_.Clear();
 }
 }
 
 
 void Graphics::SetTextureUnitMappings()
 void Graphics::SetTextureUnitMappings()

+ 2 - 0
Source/Urho3D/Graphics/Direct3D11/D3D11Graphics.h

@@ -601,6 +601,8 @@ private:
     HashMap<unsigned long long, SharedPtr<VertexDeclaration> > vertexDeclarations_;
     HashMap<unsigned long long, SharedPtr<VertexDeclaration> > vertexDeclarations_;
     /// Constant buffers.
     /// Constant buffers.
     HashMap<unsigned, SharedPtr<ConstantBuffer> > constantBuffers_;
     HashMap<unsigned, SharedPtr<ConstantBuffer> > constantBuffers_;
+    /// Currently dirty constant buffers.
+    PODVector<ConstantBuffer*> dirtyConstantBuffers_;
     /// Shader programs.
     /// Shader programs.
     ShaderProgramMap shaderPrograms_;
     ShaderProgramMap shaderPrograms_;
     /// Shader program in use.
     /// Shader program in use.

+ 2 - 2
Source/Urho3D/Graphics/Direct3D11/D3D11ShaderVariation.cpp

@@ -235,7 +235,7 @@ bool ShaderVariation::Compile()
     // Set the entrypoint, profile and flags according to the shader being compiled
     // Set the entrypoint, profile and flags according to the shader being compiled
     const char* entryPoint = 0;
     const char* entryPoint = 0;
     const char* profile = 0;
     const char* profile = 0;
-    unsigned flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
+    unsigned flags = D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
     
     
     if (type_ == VS)
     if (type_ == VS)
     {
     {
@@ -248,7 +248,7 @@ bool ShaderVariation::Compile()
         entryPoint = "PS";
         entryPoint = "PS";
         defines.Push("COMPILEPS");
         defines.Push("COMPILEPS");
         profile = "ps_4_0";
         profile = "ps_4_0";
-        flags |= D3DCOMPILE_PREFER_FLOW_CONTROL | D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
+        flags |= D3DCOMPILE_PREFER_FLOW_CONTROL;
     }
     }
     
     
     // Collect defines into macros
     // Collect defines into macros