Browse Source

D3D11 clip plane support. Check that multisample level is supported. Various shader fixes.

Lasse Öörni 10 years ago
parent
commit
e106f1b389

+ 1 - 0
Docs/Reference.dox

@@ -1133,6 +1133,7 @@ Direct3D9 and Direct3D11 share the same HLSL shader code. Macros and some condit
 - Uniforms are organized into constant buffers. See the file Uniforms.hlsl for the built-in uniforms. See TerrainBlend.hlsl for an example of defining your own uniforms into the "custom" constant buffer slot.
 - Uniforms are organized into constant buffers. See the file Uniforms.hlsl for the built-in uniforms. See TerrainBlend.hlsl for an example of defining your own uniforms into the "custom" constant buffer slot.
 - Both textures and samplers are defined for each texture unit. The macros in Samplers.hlsl (Sample2D, SampleCube etc.) can be used to write code that works on both APIs. These take the texture unit name without the 's' prefix.
 - Both textures and samplers are defined for each texture unit. The macros in Samplers.hlsl (Sample2D, SampleCube etc.) can be used to write code that works on both APIs. These take the texture unit name without the 's' prefix.
 - Vertex shader output position and pixel shader output color need to use the SV_POSITION and SV_TARGET semantics. The macros OUTPOSITION and OUTCOLOR0-3 can be used to select the correct semantic on both APIs. In the vertex shader, the output position should be specified last, as otherwise other interpolator outputs may not function correctly.
 - Vertex shader output position and pixel shader output color need to use the SV_POSITION and SV_TARGET semantics. The macros OUTPOSITION and OUTCOLOR0-3 can be used to select the correct semantic on both APIs. In the vertex shader, the output position should be specified last, as otherwise other interpolator outputs may not function correctly.
+- On Direct3D11 the clip plane coordinate must be calculated manually. This is indicated by the CLIPPLANE compilation define, which is added automatically by the Graphics class. See for example the LitSolid.hlsl shader.
 
 
 \section Shaders_Precaching Shader precaching
 \section Shaders_Precaching Shader precaching
 
 

+ 26 - 3
Source/Urho3D/Graphics/Direct3D11/D3D11Graphics.cpp

@@ -411,7 +411,10 @@ bool Graphics::SetMode(int width, int height, bool fullscreen, bool borderless,
     if (borderless)
     if (borderless)
         fullscreen = false;
         fullscreen = false;
     
     
-    multiSample = Clamp(multiSample, 1, 16);
+    // Check that multisample level is supported
+    PODVector<int> multiSampleLevels = GetMultiSampleLevels();
+    if (!multiSampleLevels.Contains(multiSample))
+        multiSample = 1;
     
     
     // If nothing changes, do not reset the device
     // If nothing changes, do not reset the device
     if (width == width_ && height == height_ && fullscreen == fullscreen_ && borderless == borderless_ && resizable == resizable_ &&
     if (width == width_ && height == height_ && fullscreen == fullscreen_ && borderless == borderless_ && resizable == resizable_ &&
@@ -897,6 +900,16 @@ void Graphics::SetIndexBuffer(IndexBuffer* buffer)
 
 
 void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
 void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
 {
 {
+    // Switch to the clip plane variations if necessary
+    /// \todo Causes overhead and string manipulation per drawcall
+    if (useClipPlane_)
+    {
+        if (vs)
+            vs = vs->GetOwner()->GetVariation(VS, vs->GetDefines() + " CLIPPLANE");
+        if (ps)
+            ps = ps->GetOwner()->GetVariation(PS, ps->GetDefines() + " CLIPPLANE");
+    }
+
     if (vs == vertexShader_ && ps == pixelShader_)
     if (vs == vertexShader_ && ps == pixelShader_)
         return;
         return;
     
     
@@ -996,6 +1009,10 @@ void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
     // Store shader combination if shader dumping in progress
     // Store shader combination if shader dumping in progress
     if (shaderPrecache_)
     if (shaderPrecache_)
         shaderPrecache_->StoreShaders(vertexShader_, pixelShader_);
         shaderPrecache_->StoreShaders(vertexShader_, pixelShader_);
+
+    // Update clip plane parameter if necessary
+    if (useClipPlane_)
+        SetShaderParameter(VSP_CLIPPLANE, clipPlane_);
 }
 }
 
 
 void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
 void Graphics::SetShaderParameter(StringHash param, const float* data, unsigned count)
@@ -1671,7 +1688,14 @@ void Graphics::SetStencilTest(bool enable, CompareMode mode, StencilOp pass, Ste
 
 
 void Graphics::SetClipPlane(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection)
 void Graphics::SetClipPlane(bool enable, const Plane& clipPlane, const Matrix3x4& view, const Matrix4& projection)
 {
 {
-    /// \todo Not implemented
+    useClipPlane_ = enable;
+
+    if (enable)
+    {
+        Matrix4 viewProj = projection * view;
+        clipPlane_ = clipPlane.Transformed(viewProj).ToVector4();
+        SetShaderParameter(VSP_CLIPPLANE, clipPlane_);
+    }
 }
 }
 
 
 void Graphics::BeginDumpShaders(const String& fileName)
 void Graphics::BeginDumpShaders(const String& fileName)
@@ -1741,7 +1765,6 @@ PODVector<int> Graphics::GetMultiSampleLevels() const
     ret.Push(2);
     ret.Push(2);
     ret.Push(4);
     ret.Push(4);
     ret.Push(8);
     ret.Push(8);
-    ret.Push(16);
 
 
     return ret;
     return ret;
 }
 }

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

@@ -565,6 +565,8 @@ private:
     unsigned stencilCompareMask_;
     unsigned stencilCompareMask_;
     /// Stencil write bitmask.
     /// Stencil write bitmask.
     unsigned stencilWriteMask_;
     unsigned stencilWriteMask_;
+    /// Current custom clip plane in post-projection space.
+    Vector4 clipPlane_;
     /// Stencil test enable flag.
     /// Stencil test enable flag.
     bool stencilTest_;
     bool stencilTest_;
     /// Custom clip plane enable flag.
     /// Custom clip plane enable flag.

+ 1 - 0
Source/Urho3D/Graphics/GraphicsDefs.cpp

@@ -34,6 +34,7 @@ extern URHO3D_API const StringHash VSP_AMBIENTENDCOLOR("AmbientEndColor");
 extern URHO3D_API const StringHash VSP_BILLBOARDROT("BillboardRot");
 extern URHO3D_API const StringHash VSP_BILLBOARDROT("BillboardRot");
 extern URHO3D_API const StringHash VSP_CAMERAPOS("CameraPos");
 extern URHO3D_API const StringHash VSP_CAMERAPOS("CameraPos");
 extern URHO3D_API const StringHash VSP_CAMERAROT("CameraRot");
 extern URHO3D_API const StringHash VSP_CAMERAROT("CameraRot");
+extern URHO3D_API const StringHash VSP_CLIPPLANE("ClipPlane");
 extern URHO3D_API const StringHash VSP_NEARCLIP("NearClip");
 extern URHO3D_API const StringHash VSP_NEARCLIP("NearClip");
 extern URHO3D_API const StringHash VSP_FARCLIP("FarClip");
 extern URHO3D_API const StringHash VSP_FARCLIP("FarClip");
 extern URHO3D_API const StringHash VSP_DEPTHMODE("DepthMode");
 extern URHO3D_API const StringHash VSP_DEPTHMODE("DepthMode");

+ 1 - 0
Source/Urho3D/Graphics/GraphicsDefs.h

@@ -280,6 +280,7 @@ extern URHO3D_API const StringHash VSP_AMBIENTENDCOLOR;
 extern URHO3D_API const StringHash VSP_BILLBOARDROT;
 extern URHO3D_API const StringHash VSP_BILLBOARDROT;
 extern URHO3D_API const StringHash VSP_CAMERAPOS;
 extern URHO3D_API const StringHash VSP_CAMERAPOS;
 extern URHO3D_API const StringHash VSP_CAMERAROT;
 extern URHO3D_API const StringHash VSP_CAMERAROT;
+extern URHO3D_API const StringHash VSP_CLIPPLANE;
 extern URHO3D_API const StringHash VSP_NEARCLIP;
 extern URHO3D_API const StringHash VSP_NEARCLIP;
 extern URHO3D_API const StringHash VSP_FARCLIP;
 extern URHO3D_API const StringHash VSP_FARCLIP;
 extern URHO3D_API const StringHash VSP_DEPTHMODE;
 extern URHO3D_API const StringHash VSP_DEPTHMODE;

+ 10 - 0
bin/CoreData/Shaders/HLSL/Basic.hlsl

@@ -23,12 +23,19 @@ void VS(float4 iPos : POSITION,
     #ifdef DIFFMAP
     #ifdef DIFFMAP
         out float2 oTexCoord : TEXCOORD0,
         out float2 oTexCoord : TEXCOORD0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oPos : OUTPOSITION)
     out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
     float3 worldPos = GetWorldPos(modelMatrix);
     float3 worldPos = GetWorldPos(modelMatrix);
     oPos = GetClipPos(worldPos);
     oPos = GetClipPos(worldPos);
 
 
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
+
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         oColor = iColor;
         oColor = iColor;
     #endif
     #endif
@@ -44,6 +51,9 @@ void PS(
     #if defined(DIFFMAP) || defined(ALPHAMAP)
     #if defined(DIFFMAP) || defined(ALPHAMAP)
         float2 iTexCoord : TEXCOORD0,
         float2 iTexCoord : TEXCOORD0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif    
     out float4 oColor : OUTCOLOR0)
     out float4 oColor : OUTCOLOR0)
 {
 {
     float4 diffColor = cMatDiffColor;
     float4 diffColor = cMatDiffColor;

+ 10 - 0
bin/CoreData/Shaders/HLSL/LitParticle.hlsl

@@ -40,6 +40,9 @@ void VS(float4 iPos : POSITION,
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         out float4 oColor : COLOR0,
         out float4 oColor : COLOR0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oPos : OUTPOSITION)
     out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
@@ -48,6 +51,10 @@ void VS(float4 iPos : POSITION,
     oTexCoord = GetTexCoord(iTexCoord);
     oTexCoord = GetTexCoord(iTexCoord);
     oWorldPos = float4(worldPos, GetDepth(oPos));
     oWorldPos = float4(worldPos, GetDepth(oPos));
 
 
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
+
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         oColor = iColor;
         oColor = iColor;
     #endif
     #endif
@@ -98,6 +105,9 @@ void PS(float2 iTexCoord : TEXCOORD0,
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         float4 iColor : COLOR0,
         float4 iColor : COLOR0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oColor : OUTCOLOR0)
     out float4 oColor : OUTCOLOR0)
 {
 {
     // Get material diffuse albedo
     // Get material diffuse albedo

+ 10 - 0
bin/CoreData/Shaders/HLSL/LitSolid.hlsl

@@ -54,6 +54,9 @@ void VS(float4 iPos : POSITION,
             out float2 oTexCoord2 : TEXCOORD7,
             out float2 oTexCoord2 : TEXCOORD7,
         #endif
         #endif
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oPos : OUTPOSITION)
     out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
@@ -61,6 +64,10 @@ void VS(float4 iPos : POSITION,
     oPos = GetClipPos(worldPos);
     oPos = GetClipPos(worldPos);
     oNormal = GetWorldNormal(modelMatrix);
     oNormal = GetWorldNormal(modelMatrix);
     oWorldPos = float4(worldPos, GetDepth(oPos));
     oWorldPos = float4(worldPos, GetDepth(oPos));
+    
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
 
 
     #ifdef NORMALMAP
     #ifdef NORMALMAP
         float3 tangent = GetWorldTangent(modelMatrix);
         float3 tangent = GetWorldTangent(modelMatrix);
@@ -141,6 +148,9 @@ void PS(
             float2 iTexCoord2 : TEXCOORD7,
             float2 iTexCoord2 : TEXCOORD7,
         #endif
         #endif
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif
     #ifdef PREPASS
     #ifdef PREPASS
         out float4 oDepth : OUTCOLOR1,
         out float4 oDepth : OUTCOLOR1,
     #endif
     #endif

+ 10 - 0
bin/CoreData/Shaders/HLSL/TerrainBlend.hlsl

@@ -69,6 +69,9 @@ void VS(float4 iPos : POSITION,
         out float3 oVertexLight : TEXCOORD4,
         out float3 oVertexLight : TEXCOORD4,
         out float4 oScreenPos : TEXCOORD5,
         out float4 oScreenPos : TEXCOORD5,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oPos : OUTPOSITION)
     out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
@@ -79,6 +82,10 @@ void VS(float4 iPos : POSITION,
     oTexCoord = GetTexCoord(iTexCoord);
     oTexCoord = GetTexCoord(iTexCoord);
     oDetailTexCoord = cDetailTiling * oTexCoord;
     oDetailTexCoord = cDetailTiling * oTexCoord;
 
 
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
+
     #ifdef PERPIXEL
     #ifdef PERPIXEL
         // Per-pixel forward lighting
         // Per-pixel forward lighting
         float4 projWorldPos = float4(worldPos.xyz, 1.0);
         float4 projWorldPos = float4(worldPos.xyz, 1.0);
@@ -127,6 +134,9 @@ void PS(float2 iTexCoord : TEXCOORD0,
         float3 iVertexLight : TEXCOORD4,
         float3 iVertexLight : TEXCOORD4,
         float4 iScreenPos : TEXCOORD5,
         float4 iScreenPos : TEXCOORD5,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif
     #ifdef PREPASS
     #ifdef PREPASS
         out float4 oDepth : OUTCOLOR1,
         out float4 oDepth : OUTCOLOR1,
     #endif
     #endif

+ 26 - 14
bin/CoreData/Shaders/HLSL/Text.hlsl

@@ -2,21 +2,33 @@
 #include "Samplers.hlsl"
 #include "Samplers.hlsl"
 #include "Transform.hlsl"
 #include "Transform.hlsl"
 
 
-#ifdef TEXT_EFFECT_SHADOW
+#ifndef D3D11
+
+// D3D9 uniforms
 uniform float2 cShadowOffset;
 uniform float2 cShadowOffset;
 uniform float4 cShadowColor;
 uniform float4 cShadowColor;
+uniform float4 cStrokeColor;
+
+#else
+
+#ifdef COMPILEPS
+// D3D11 constant buffers
+cbuffer CustomPS : register(b6)
+{
+    float2 cShadowOffset;
+    float4 cShadowColor;
+    float4 cStrokeColor;
+}
 #endif
 #endif
 
 
-#ifdef TEXT_EFFECT_STROKE
-uniform float4 cStrokeColor;
 #endif
 #endif
 
 
 void VS(float4 iPos : POSITION,
 void VS(float4 iPos : POSITION,
-        float4 iColor : COLOR0,
-        float2 iTexCoord : TEXCOORD0,
-        out float4 oPos : POSITION,
-        out float4 oColor : COLOR0,
-        out float2 oTexCoord : TEXCOORD0)
+    float4 iColor : COLOR0,
+    float2 iTexCoord : TEXCOORD0,
+    out float4 oColor : COLOR0,
+    out float2 oTexCoord : TEXCOORD0,
+    out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
     float3 worldPos = GetWorldPos(modelMatrix);
     float3 worldPos = GetWorldPos(modelMatrix);
@@ -26,17 +38,17 @@ void VS(float4 iPos : POSITION,
 }
 }
 
 
 void PS(float4 iColor : COLOR0,
 void PS(float4 iColor : COLOR0,
-        float2 iTexCoord : TEXCOORD0,
-        out float4 oColor : COLOR0)
+    float2 iTexCoord : TEXCOORD0,
+    out float4 oColor : OUTCOLOR0)
 {
 {
     oColor.rgb = iColor.rgb;
     oColor.rgb = iColor.rgb;
 
 
 #ifdef SIGNED_DISTANCE_FIELD
 #ifdef SIGNED_DISTANCE_FIELD
-    float distance = tex2D(sDiffMap, iTexCoord).a;
+    float distance = Sample2D(DiffMap, iTexCoord).a;
     if (distance < 0.5f)
     if (distance < 0.5f)
     {
     {
     #ifdef TEXT_EFFECT_SHADOW
     #ifdef TEXT_EFFECT_SHADOW
-        if (tex2D(sDiffMap, iTexCoord - cShadowOffset).a > 0.5f)
+        if (Sample2D(DiffMap, iTexCoord - cShadowOffset).a > 0.5f)
             oColor = cShadowColor;
             oColor = cShadowColor;
         else
         else
     #endif
     #endif
@@ -50,13 +62,13 @@ void PS(float4 iColor : COLOR0,
     #endif
     #endif
 
 
     #ifdef TEXT_EFFECT_SHADOW
     #ifdef TEXT_EFFECT_SHADOW
-        if (tex2D(sDiffMap, iTexCoord + cShadowOffset).a < 0.5f)
+        if (Sample2D(DiffMap, iTexCoord + cShadowOffset).a < 0.5f)
             oColor.a = iColor.a;
             oColor.a = iColor.a;
         else
         else
     #endif
     #endif
         oColor.a = iColor.a * smoothstep(0.5f, 0.505f, distance);
         oColor.a = iColor.a * smoothstep(0.5f, 0.505f, distance);
     }
     }
 #else
 #else
-    oColor.a = iColor.a * tex2D(sDiffMap, iTexCoord).a;
+    oColor.a = iColor.a * Sample2D(DiffMap, iTexCoord).a;
 #endif
 #endif
 }
 }

+ 1 - 0
bin/CoreData/Shaders/HLSL/Uniforms.hlsl

@@ -87,6 +87,7 @@ cbuffer CameraVS : register(b1)
     float3 cFrustumSize;
     float3 cFrustumSize;
     float4 cGBufferOffsets;
     float4 cGBufferOffsets;
     float4x4 cViewProj;
     float4x4 cViewProj;
+    float4 cClipPlane;
 }
 }
 
 
 cbuffer ZoneVS : register(b2)
 cbuffer ZoneVS : register(b2)

+ 10 - 0
bin/CoreData/Shaders/HLSL/Unlit.hlsl

@@ -23,6 +23,9 @@ void VS(float4 iPos : POSITION,
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         out float4 oColor : COLOR0,
         out float4 oColor : COLOR0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
     out float4 oPos : OUTPOSITION)
     out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
@@ -31,6 +34,10 @@ void VS(float4 iPos : POSITION,
     oTexCoord = GetTexCoord(iTexCoord);
     oTexCoord = GetTexCoord(iTexCoord);
     oWorldPos = float4(worldPos, GetDepth(oPos));
     oWorldPos = float4(worldPos, GetDepth(oPos));
 
 
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
+    
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         oColor = iColor;
         oColor = iColor;
     #endif
     #endif
@@ -41,6 +48,9 @@ void PS(float2 iTexCoord : TEXCOORD0,
     #ifdef VERTEXCOLOR
     #ifdef VERTEXCOLOR
         float4 iColor : COLOR0,
         float4 iColor : COLOR0,
     #endif
     #endif
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif
     #ifdef PREPASS
     #ifdef PREPASS
         out float4 oDepth : OUTCOLOR1,
         out float4 oDepth : OUTCOLOR1,
     #endif
     #endif

+ 1 - 1
bin/CoreData/Shaders/HLSL/Urho2D.hlsl

@@ -22,6 +22,6 @@ void PS(float4 iColor : COLOR0,
         out float4 oColor : OUTCOLOR0)
         out float4 oColor : OUTCOLOR0)
 {
 {
     float4 diffColor = cMatDiffColor * iColor;
     float4 diffColor = cMatDiffColor * iColor;
-    float4 diffInput = Sample2D(sDiffMap, iTexCoord);
+    float4 diffInput = Sample2D(DiffMap, iTexCoord);
     oColor = diffColor * diffInput;
     oColor = diffColor * diffInput;
 }
 }

+ 8 - 1
bin/CoreData/Shaders/HLSL/Vegetation.hlsl

@@ -57,7 +57,10 @@ void VS(float4 iPos : POSITION,
             out float2 oTexCoord2 : TEXCOORD7,
             out float2 oTexCoord2 : TEXCOORD7,
         #endif
         #endif
     #endif
     #endif
-    out float4 oPos : POSITION)
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
+    out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
     float3 worldPos = GetWorldPos(modelMatrix);
     float3 worldPos = GetWorldPos(modelMatrix);
@@ -72,6 +75,10 @@ void VS(float4 iPos : POSITION,
     oNormal = GetWorldNormal(modelMatrix);
     oNormal = GetWorldNormal(modelMatrix);
     oWorldPos = float4(worldPos, GetDepth(oPos));
     oWorldPos = float4(worldPos, GetDepth(oPos));
 
 
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
+    
     #ifdef NORMALMAP
     #ifdef NORMALMAP
         float3 tangent = GetWorldTangent(modelMatrix);
         float3 tangent = GetWorldTangent(modelMatrix);
         float3 bitangent = cross(tangent, oNormal) * iTangent.w;
         float3 bitangent = cross(tangent, oNormal) * iTangent.w;

+ 38 - 5
bin/CoreData/Shaders/HLSL/Water.hlsl

@@ -4,12 +4,35 @@
 #include "ScreenPos.hlsl"
 #include "ScreenPos.hlsl"
 #include "Fog.hlsl"
 #include "Fog.hlsl"
 
 
+#ifndef D3D11
+
+// D3D9 uniforms
 uniform float2 cNoiseSpeed;
 uniform float2 cNoiseSpeed;
 uniform float cNoiseTiling;
 uniform float cNoiseTiling;
 uniform float cNoiseStrength;
 uniform float cNoiseStrength;
 uniform float cFresnelPower;
 uniform float cFresnelPower;
 uniform float3 cWaterTint;
 uniform float3 cWaterTint;
 
 
+#else
+
+// D3D11 constant buffers
+#ifdef COMPILEVS
+cbuffer CustomVS : register(b6)
+{
+    float2 cNoiseSpeed;
+    float cNoiseTiling;
+}
+#else
+cbuffer CustomPS : register(b6)
+{
+    float cNoiseStrength;
+    float cFresnelPower;
+    float3 cWaterTint;
+}
+#endif
+
+#endif
+
 void VS(float4 iPos : POSITION,
 void VS(float4 iPos : POSITION,
     float3 iNormal: NORMAL,
     float3 iNormal: NORMAL,
     float2 iTexCoord : TEXCOORD0,
     float2 iTexCoord : TEXCOORD0,
@@ -18,7 +41,10 @@ void VS(float4 iPos : POSITION,
     out float2 oWaterUV : TEXCOORD2,
     out float2 oWaterUV : TEXCOORD2,
     out float3 oNormal : TEXCOORD3,
     out float3 oNormal : TEXCOORD3,
     out float4 oEyeVec : TEXCOORD4,
     out float4 oEyeVec : TEXCOORD4,
-    out float4 oPos : POSITION)
+    #if defined(D3D11) && defined(CLIPPLANE)
+        out float oClip : SV_CLIPDISTANCE0,
+    #endif
+    out float4 oPos : OUTPOSITION)
 {
 {
     float4x3 modelMatrix = iModelMatrix;
     float4x3 modelMatrix = iModelMatrix;
     float3 worldPos = GetWorldPos(modelMatrix);
     float3 worldPos = GetWorldPos(modelMatrix);
@@ -31,6 +57,10 @@ void VS(float4 iPos : POSITION,
     oWaterUV = iTexCoord * cNoiseTiling + cElapsedTime * cNoiseSpeed;
     oWaterUV = iTexCoord * cNoiseTiling + cElapsedTime * cNoiseSpeed;
     oNormal = GetWorldNormal(modelMatrix);
     oNormal = GetWorldNormal(modelMatrix);
     oEyeVec = float4(cCameraPos - worldPos, GetDepth(oPos));
     oEyeVec = float4(cCameraPos - worldPos, GetDepth(oPos));
+
+    #if defined(D3D11) && defined(CLIPPLANE)
+        oClip = dot(oPos, cClipPlane);
+    #endif
 }
 }
 
 
 void PS(
 void PS(
@@ -39,12 +69,15 @@ void PS(
     float2 iWaterUV : TEXCOORD2,
     float2 iWaterUV : TEXCOORD2,
     float3 iNormal : TEXCOORD3,
     float3 iNormal : TEXCOORD3,
     float4 iEyeVec : TEXCOORD4,
     float4 iEyeVec : TEXCOORD4,
-    out float4 oColor : COLOR0)
+    #if defined(D3D11) && defined(CLIPPLANE)
+        float iClip : SV_CLIPDISTANCE0,
+    #endif
+    out float4 oColor : OUTCOLOR0)
 {
 {
     float2 refractUV = iScreenPos.xy / iScreenPos.w;
     float2 refractUV = iScreenPos.xy / iScreenPos.w;
     float2 reflectUV = iReflectUV.xy / iScreenPos.w;
     float2 reflectUV = iReflectUV.xy / iScreenPos.w;
 
 
-    float2 noise = (tex2D(sNormalMap, iWaterUV).rg - 0.5) * cNoiseStrength;
+    float2 noise = (Sample2D(NormalMap, iWaterUV).rg - 0.5) * cNoiseStrength;
     refractUV += noise;
     refractUV += noise;
     // Do not shift reflect UV coordinate upward, because it will reveal the clipping of geometry below water
     // Do not shift reflect UV coordinate upward, because it will reveal the clipping of geometry below water
     if (noise.y < 0.0)
     if (noise.y < 0.0)
@@ -52,8 +85,8 @@ void PS(
     reflectUV += noise;
     reflectUV += noise;
 
 
     float fresnel = pow(1.0 - saturate(dot(normalize(iEyeVec.xyz), iNormal)), cFresnelPower);
     float fresnel = pow(1.0 - saturate(dot(normalize(iEyeVec.xyz), iNormal)), cFresnelPower);
-    float3 refractColor = tex2D(sEnvMap, refractUV).rgb * cWaterTint;
-    float3 reflectColor = tex2D(sDiffMap, reflectUV).rgb;
+    float3 refractColor = Sample2D(EnvMap, refractUV).rgb * cWaterTint;
+    float3 reflectColor = Sample2D(DiffMap, reflectUV).rgb;
     float3 finalColor = lerp(refractColor, reflectColor, fresnel);
     float3 finalColor = lerp(refractColor, reflectColor, fresnel);
 
 
     oColor = float4(GetFog(finalColor, GetFogFactor(iEyeVec.w)), 1.0);
     oColor = float4(GetFog(finalColor, GetFogFactor(iEyeVec.w)), 1.0);