From 9d4c08ba448c9a173aafcf8a6e522e125c8a88ed Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Mon, 12 Jul 2021 17:32:26 +0200 Subject: [PATCH] voxel gi secondary bounce fix --- WickedEngine/shaders/objectPS_voxelizer.hlsl | 8 ++--- WickedEngine/shaders/voxelConeTracingHF.hlsli | 6 +++- WickedEngine/shaders/voxelHF.hlsli | 35 ++----------------- .../voxelRadianceSecondaryBounceCS.hlsl | 5 ++- .../shaders/voxelSceneCopyClearCS.hlsl | 4 +-- WickedEngine/wiRenderer.cpp | 2 +- WickedEngine/wiVersion.cpp | 2 +- 7 files changed, 16 insertions(+), 46 deletions(-) diff --git a/WickedEngine/shaders/objectPS_voxelizer.hlsl b/WickedEngine/shaders/objectPS_voxelizer.hlsl index 6f1ffb181..0f06e7a8a 100644 --- a/WickedEngine/shaders/objectPS_voxelizer.hlsl +++ b/WickedEngine/shaders/objectPS_voxelizer.hlsl @@ -18,7 +18,7 @@ struct PSInput void main(PSInput input) { - float3 N = input.N; + float3 N = normalize(input.N); float3 P = input.P; float3 diff = (P - g_xFrame_VoxelRadianceDataCenter) * g_xFrame_VoxelRadianceDataRes_rcp * g_xFrame_VoxelRadianceDataSize_rcp; @@ -51,8 +51,6 @@ void main(PSInput input) emissiveColor *= emissiveMap; } - N = normalize(N); - #ifdef TERRAIN color = 0; @@ -318,8 +316,8 @@ void main(PSInput input) color.rgb += emissiveColor.rgb * emissiveColor.a; - uint color_encoded = EncodeColor(color); - uint normal_encoded = EncodeNormal(N); + uint color_encoded = PackVoxelColor(color); + uint normal_encoded = pack_unitvector(N); // output: uint3 writecoord = floor(uvw * g_xFrame_VoxelRadianceDataRes); diff --git a/WickedEngine/shaders/voxelConeTracingHF.hlsli b/WickedEngine/shaders/voxelConeTracingHF.hlsli index 66cc31253..a65f451ed 100644 --- a/WickedEngine/shaders/voxelConeTracingHF.hlsli +++ b/WickedEngine/shaders/voxelConeTracingHF.hlsli @@ -2,6 +2,10 @@ #define WI_VOXEL_CONERACING_HF #include "globals.hlsli" +#ifndef VOXEL_INITIAL_OFFSET +#define VOXEL_INITIAL_OFFSET 2 +#endif // VOXEL_INITIAL_OFFSET + // voxels: 3D Texture containing voxel scene with direct diffuse lighting (or direct + secondary indirect bounce) // P: world-space position of receiving surface // N: world-space normal vector of receiving surface @@ -15,7 +19,7 @@ inline float4 ConeTrace(in Texture3D voxels, in float3 P, in float3 N, i // We need to offset the cone start position to avoid sampling its own voxel (self-occlusion): // Unfortunately, it will result in disconnection between nearby surfaces :( float dist = g_xFrame_VoxelRadianceDataSize; // offset by cone dir so that first sample of all cones are not the same - float3 startPos = P + N * g_xFrame_VoxelRadianceDataSize * 2 * SQRT2; // sqrt2 is diagonal voxel half-extent + float3 startPos = P + N * g_xFrame_VoxelRadianceDataSize * VOXEL_INITIAL_OFFSET * SQRT2; // sqrt2 is diagonal voxel half-extent // We will break off the loop if the sampling distance is too far for performance reasons: while (dist < g_xFrame_VoxelRadianceMaxDistance && alpha < 1) diff --git a/WickedEngine/shaders/voxelHF.hlsli b/WickedEngine/shaders/voxelHF.hlsli index 3d4560400..7e8a52e3e 100644 --- a/WickedEngine/shaders/voxelHF.hlsli +++ b/WickedEngine/shaders/voxelHF.hlsli @@ -11,7 +11,7 @@ static const float __hdrRange = 10.0f; // Encode HDR color to a 32 bit uint // Alpha is 1 bit + 7 bit HDR remapping -uint EncodeColor(in float4 color) +uint PackVoxelColor(in float4 color) { // normalize color to LDR float hdr = length(color.rgb); @@ -30,7 +30,7 @@ uint EncodeColor(in float4 color) } // Decode 32 bit uint into HDR color with 1 bit alpha -float4 DecodeColor(in uint colorMask) +float4 UnpackVoxelColor(in uint colorMask) { float hdr; float4 color; @@ -50,35 +50,4 @@ float4 DecodeColor(in uint colorMask) return color; } -// Encode specified normal (normalized) into an unsigned integer. Each axis of -// the normal is encoded into 9 bits (1 for the sign/ 8 for the value). -uint EncodeNormal(in float3 normal) -{ - int3 iNormal = int3(normalize(normal)*255.0f); - uint3 iNormalSigns; - iNormalSigns.x = (iNormal.x >> 5) & 0x04000000; - iNormalSigns.y = (iNormal.y >> 14) & 0x00020000; - iNormalSigns.z = (iNormal.z >> 23) & 0x00000100; - iNormal = abs(iNormal); - uint normalMask = iNormalSigns.x | (iNormal.x << 18) | iNormalSigns.y | (iNormal.y << 9) | iNormalSigns.z | iNormal.z; - return normalMask; -} - -// Decode specified mask into a float3 normal (normalized). -float3 DecodeNormal(in uint normalMask) -{ - int3 iNormal; - iNormal.x = (normalMask >> 18) & 0x000000ff; - iNormal.y = (normalMask >> 9) & 0x000000ff; - iNormal.z = normalMask & 0x000000ff; - int3 iNormalSigns; - iNormalSigns.x = (normalMask >> 25) & 0x00000002; - iNormalSigns.y = (normalMask >> 16) & 0x00000002; - iNormalSigns.z = (normalMask >> 7) & 0x00000002; - iNormalSigns = 1 - iNormalSigns; - float3 normal = float3(iNormal) / 255.0f; - normal *= iNormalSigns; - return normalize(normal); -} - #endif // WI_VOXEL_HF diff --git a/WickedEngine/shaders/voxelRadianceSecondaryBounceCS.hlsl b/WickedEngine/shaders/voxelRadianceSecondaryBounceCS.hlsl index 758692b9d..9fe34a5d3 100644 --- a/WickedEngine/shaders/voxelRadianceSecondaryBounceCS.hlsl +++ b/WickedEngine/shaders/voxelRadianceSecondaryBounceCS.hlsl @@ -1,3 +1,4 @@ +#define VOXEL_INITIAL_OFFSET 4 #include "globals.hlsli" #include "voxelHF.hlsli" #include "voxelConeTracingHF.hlsli" @@ -6,8 +7,6 @@ TEXTURE3D(input_emission, float4, 0); STRUCTUREDBUFFER(input_voxelscene, VoxelType, 1); RWTEXTURE3D(output, float4, 0); - - [numthreads(8, 8, 8)] void main( uint3 DTid : SV_DispatchThreadID ) { @@ -17,7 +16,7 @@ void main( uint3 DTid : SV_DispatchThreadID ) if (emission.a > 0) { - float3 N = DecodeNormal(input_voxelscene[DTid.x].normalMask); + float3 N = unpack_unitvector(input_voxelscene[DTid.x].normalMask); float3 P = ((float3)writecoord + 0.5f) * g_xFrame_VoxelRadianceDataRes_rcp; P = P * 2 - 1; diff --git a/WickedEngine/shaders/voxelSceneCopyClearCS.hlsl b/WickedEngine/shaders/voxelSceneCopyClearCS.hlsl index a296657e8..9b193bc6e 100644 --- a/WickedEngine/shaders/voxelSceneCopyClearCS.hlsl +++ b/WickedEngine/shaders/voxelSceneCopyClearCS.hlsl @@ -9,7 +9,7 @@ void main( uint3 DTid : SV_DispatchThreadID ) { VoxelType voxel = input_output[DTid.x]; - const float4 color = DecodeColor(voxel.colorMask); + const float4 color = UnpackVoxelColor(voxel.colorMask); const uint3 writecoord = unflatten3D(DTid.x, g_xFrame_VoxelRadianceDataRes); @@ -41,4 +41,4 @@ void main( uint3 DTid : SV_DispatchThreadID ) // delete emission data, but keep normals (no need to delete, we will only read normal values of filled voxels) input_output[DTid.x].colorMask = 0; -} \ No newline at end of file +} diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 5a8e619bb..e30a9888b 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -6290,7 +6290,7 @@ void DrawDebugWorld( device->BindPipelineState(&PSO_debug[DEBUGRENDERING_VOXEL], cmd); - device->BindResource(VS, &textures[TEXTYPE_3D_VOXELRADIANCE], TEXSLOT_VOXELRADIANCE, cmd); + device->BindResource(VS, GetVoxelRadianceSecondaryBounceEnabled() ? &textures[TEXTYPE_3D_VOXELRADIANCE_HELPER] : &textures[TEXTYPE_3D_VOXELRADIANCE], TEXSLOT_VOXELRADIANCE, cmd); MiscCB sb; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 80e7526ae..d596f1ae6 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates, breaking compatibility changes const int minor = 56; // minor bug fixes, alterations, refactors, updates - const int revision = 68; + const int revision = 69; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);