From 19dcf476e8f9b079191bd43ee8da9e9cfe5e3e64 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Wed, 4 Jul 2018 16:21:24 -0700 Subject: Corrected depth-related GLSL definitions for Vulkan case --- Graphics/GLSLTools/include/GLSLSourceBuilder.h | 2 +- Graphics/GLSLTools/src/GLSLSourceBuilder.cpp | 7 ++++++- Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp | 2 +- .../include/GLSLDefinitions.h | 23 ++++++++++++++-------- .../include/GLSLDefinitions_inc.h | 23 ++++++++++++++-------- 5 files changed, 38 insertions(+), 19 deletions(-) (limited to 'Graphics') diff --git a/Graphics/GLSLTools/include/GLSLSourceBuilder.h b/Graphics/GLSLTools/include/GLSLSourceBuilder.h index 572ea180..b77b1055 100644 --- a/Graphics/GLSLTools/include/GLSLSourceBuilder.h +++ b/Graphics/GLSLTools/include/GLSLSourceBuilder.h @@ -35,6 +35,6 @@ enum TargetGLSLCompiler driver }; -String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs, TargetGLSLCompiler TargetCompiler); +String BuildGLSLSourceString(const ShaderCreationAttribs& CreationAttribs, TargetGLSLCompiler TargetCompiler, const char* ExtraDefinitions = nullptr); } \ No newline at end of file diff --git a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp index e37206b1..d5a8ee09 100644 --- a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp +++ b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp @@ -32,7 +32,7 @@ namespace Diligent { -String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs, TargetGLSLCompiler TargetCompiler) +String BuildGLSLSourceString(const ShaderCreationAttribs& CreationAttribs, TargetGLSLCompiler TargetCompiler, const char* ExtraDefinitions) { String GLSLSource; @@ -189,6 +189,11 @@ String BuildGLSLSourceString(const ShaderCreationAttribs &CreationAttribs, Targe } GLSLSource += ShaderTypeDefine; + if(ExtraDefinitions != nullptr) + { + GLSLSource.append(ExtraDefinitions); + } + if (CreationAttribs.Macros != nullptr) { auto *pMacro = CreationAttribs.Macros; diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp index e875a923..264f695c 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVkImpl.cpp @@ -43,7 +43,7 @@ ShaderVkImpl::ShaderVkImpl(IReferenceCounters* pRefCounters, RenderDeviceVkImpl* m_StaticResCache(ShaderResourceCacheVk::DbgCacheContentType::StaticShaderResources), m_StaticVarsMgr(*this) { - auto GLSLSource = BuildGLSLSourceString(CreationAttribs, TargetGLSLCompiler::glslang); + auto GLSLSource = BuildGLSLSourceString(CreationAttribs, TargetGLSLCompiler::glslang, "#define TARGET_API_VULKAN 1\n"); m_SPIRV = GLSLtoSPIRV(m_Desc.ShaderType, GLSLSource.c_str()); if(m_SPIRV.empty()) { diff --git a/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions.h b/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions.h index 8fb4fd70..35c4c839 100644 --- a/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions.h +++ b/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions.h @@ -981,25 +981,32 @@ uvec4 _ToUvec( vec4 f4 ){ return _ToUvec4( f4.x, f4.y, f4.z, f4.w ); } // Helper functions +#ifdef TARGET_API_VULKAN + +#define NDC_MIN_Z 0.0 // Minimal z in the normalized device space +#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 1.0) + +#else + +#define NDC_MIN_Z -1.0 // Minimal z in the normalized device space +#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5) + +#endif + float2 NormalizedDeviceXYToTexUV( float2 f2ProjSpaceXY ) { - return float2(0.5,0.5) + float2(0.5,0.5) * f2ProjSpaceXY.xy; + return float2(0.5,0.5) + F3NDC_XYZ_TO_UVD_SCALE.xy * f2ProjSpaceXY.xy; } float NormalizedDeviceZToDepth(float fNDC_Z) { - return fNDC_Z * 0.5 + 0.5; // [-1, +1] -> [0, 1] + return (fNDC_Z - NDC_MIN_Z) * F3NDC_XYZ_TO_UVD_SCALE.z; } - float DepthToNormalizedDeviceZ(float fDepth) { - return fDepth * 2.0 - 1.0; // [0, 1] -> [-1, +1] + return fDepth / F3NDC_XYZ_TO_UVD_SCALE.z + NDC_MIN_Z; } -#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5) - -#define NDC_MIN_Z -1.0 // Minimal z in the normalized device space - #define MATRIX_ELEMENT(mat, row, col) mat[col][row] float4x4 MatrixFromRows(float4 row0, float4 row1, float4 row2, float4 row3) diff --git a/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions_inc.h b/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions_inc.h index cdbc0527..20b9e2f0 100644 --- a/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions_inc.h +++ b/Graphics/HLSL2GLSLConverterLib/include/GLSLDefinitions_inc.h @@ -981,25 +981,32 @@ "\n" "// Helper functions\n" "\n" +"#ifdef TARGET_API_VULKAN\n" +"\n" +"#define NDC_MIN_Z 0.0 // Minimal z in the normalized device space\n" +"#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 1.0)\n" +"\n" +"#else\n" +"\n" +"#define NDC_MIN_Z -1.0 // Minimal z in the normalized device space\n" +"#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5)\n" +"\n" +"#endif\n" +"\n" "float2 NormalizedDeviceXYToTexUV( float2 f2ProjSpaceXY )\n" "{\n" -" return float2(0.5,0.5) + float2(0.5,0.5) * f2ProjSpaceXY.xy;\n" +" return float2(0.5,0.5) + F3NDC_XYZ_TO_UVD_SCALE.xy * f2ProjSpaceXY.xy;\n" "}\n" "\n" "float NormalizedDeviceZToDepth(float fNDC_Z)\n" "{\n" -" return fNDC_Z * 0.5 + 0.5; // [-1, +1] -> [0, 1]\n" +" return (fNDC_Z - NDC_MIN_Z) * F3NDC_XYZ_TO_UVD_SCALE.z;\n" "}\n" -"\n" "float DepthToNormalizedDeviceZ(float fDepth)\n" "{\n" -" return fDepth * 2.0 - 1.0; // [0, 1] -> [-1, +1]\n" +" return fDepth / F3NDC_XYZ_TO_UVD_SCALE.z + NDC_MIN_Z;\n" "}\n" "\n" -"#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5)\n" -"\n" -"#define NDC_MIN_Z -1.0 // Minimal z in the normalized device space\n" -"\n" "#define MATRIX_ELEMENT(mat, row, col) mat[col][row]\n" "\n" "float4x4 MatrixFromRows(float4 row0, float4 row1, float4 row2, float4 row3)\n" -- cgit v1.2.3