From 0d177ce06bfa642f64d8af4de1197ad1bcb862d4 Mon Sep 17 00:00:00 2001 From: Filip Strugar Date: Tue, 28 Dec 2021 01:44:55 +0000 Subject: [PATCH] Refactor of UAV clearing - bug fixes and etc. --- Source/Rendering/Shaders/vaMisc.hlsl | 43 +++++++++++++++++++ Source/Rendering/Shaders/vaPostProcess.hlsl | 32 -------------- .../Rendering/Shaders/vaPostProcessShared.h | 1 + Source/Rendering/vaRenderDevice.cpp | 16 +++++-- Source/Rendering/vaRenderDevice.h | 2 +- VisualStudio/AllModules/AllModules.vcxproj | 5 +++ .../AllModules/AllModules.vcxproj.filters | 3 ++ 7 files changed, 66 insertions(+), 36 deletions(-) create mode 100644 Source/Rendering/Shaders/vaMisc.hlsl diff --git a/Source/Rendering/Shaders/vaMisc.hlsl b/Source/Rendering/Shaders/vaMisc.hlsl new file mode 100644 index 0000000..24a33ab --- /dev/null +++ b/Source/Rendering/Shaders/vaMisc.hlsl @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016-2021, Intel Corporation +// +// SPDX-License-Identifier: MIT +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// Author(s): Filip Strugar (filip.strugar@intel.com) +// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#include "vaPostProcessShared.h" + +#if defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_1F ) // 1D textures +RWTexture1D g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_1F( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = g_postProcessConsts.Bounds.x; } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_4F ) +RWTexture1D g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_4F( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = g_postProcessConsts.Bounds.xyzw; } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_1U ) +RWTexture1D g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_1U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = asuint(g_postProcessConsts.Bounds.x); } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_4U ) +RWTexture1D g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_4U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = asuint(g_postProcessConsts.Bounds.xyzw); } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_1F ) // 2D textures +RWTexture2D g_clearTarget : register( u0 ); +[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_1F( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.xy)] = g_postProcessConsts.Bounds.x; } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_4F ) +RWTexture2D g_clearTarget : register( u0 ); +[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_4F( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.xy)] = g_postProcessConsts.Bounds.xyzw; } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_1U ) +RWTexture2D g_clearTarget : register( u0 ); +[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_1U( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.xy)] = asuint(g_postProcessConsts.Bounds.x); } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_4U ) +RWTexture2D g_clearTarget : register( u0 ); +[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_4U( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.xy)] = asuint(g_postProcessConsts.Bounds.xyzw); } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_BUFF_1U ) // buffers +RWBuffer g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Buff_1U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = asuint(g_postProcessConsts.Bounds.x); } +#elif defined( VA_POSTPROCESS_CLEAR_UAV_BUFF_4U ) +RWBuffer g_clearTarget : register( u0 ); +[numthreads(64, 1, 1)] void CSClearUAV_Buff_4U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[min(dispatchThreadID, g_postProcessConsts.Bounds.x)] = asuint(g_postProcessConsts.Bounds.xyzw); } +#endif \ No newline at end of file diff --git a/Source/Rendering/Shaders/vaPostProcess.hlsl b/Source/Rendering/Shaders/vaPostProcess.hlsl index e9a9101..a0bfce0 100644 --- a/Source/Rendering/Shaders/vaPostProcess.hlsl +++ b/Source/Rendering/Shaders/vaPostProcess.hlsl @@ -461,35 +461,3 @@ void CSGenerateMotionVectors( uint2 dispatchThreadID : SV_DispatchThreadID ) } #endif - -#if defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_1F ) // 1D textures -RWTexture1D g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_1F( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = g_postProcessConsts.Param1.x; } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_4F ) -RWTexture1D g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_4F( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = g_postProcessConsts.Param1.xyzw; } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_1U ) -RWTexture1D g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_1U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.x); } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX1D_4U ) -RWTexture1D g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Tex1D_4U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.xyzw); } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_1F ) // 2D textures -RWTexture2D g_clearTarget : register( u0 ); -[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_1F( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = g_postProcessConsts.Param1.x; } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_4F ) -RWTexture2D g_clearTarget : register( u0 ); -[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_4F( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = g_postProcessConsts.Param1.xyzw; } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_1U ) -RWTexture2D g_clearTarget : register( u0 ); -[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_1U( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.x); } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_TEX2D_4U ) -RWTexture2D g_clearTarget : register( u0 ); -[numthreads(8, 8, 1)] void CSClearUAV_Tex2D_4U( uint2 dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.xyzw); } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_BUFF_1U ) // buffers -RWBuffer g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Buff_1U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.x); } -#elif defined( VA_POSTPROCESS_CLEAR_UAV_BUFF_4U ) -RWBuffer g_clearTarget : register( u0 ); -[numthreads(64, 1, 1)] void CSClearUAV_Buff_4U( uint dispatchThreadID : SV_DispatchThreadID ) { g_clearTarget[dispatchThreadID] = asuint(g_postProcessConsts.Param1.xyzw); } -#endif \ No newline at end of file diff --git a/Source/Rendering/Shaders/vaPostProcessShared.h b/Source/Rendering/Shaders/vaPostProcessShared.h index 7b49740..da99b65 100644 --- a/Source/Rendering/Shaders/vaPostProcessShared.h +++ b/Source/Rendering/Shaders/vaPostProcessShared.h @@ -50,6 +50,7 @@ namespace Vanilla vaVector4 Param2; vaVector4 Param3; vaVector4 Param4; + vaVector4ui Bounds; }; // all of this is unused at the moment diff --git a/Source/Rendering/vaRenderDevice.cpp b/Source/Rendering/vaRenderDevice.cpp index 9d7d585..35361de 100644 --- a/Source/Rendering/vaRenderDevice.cpp +++ b/Source/Rendering/vaRenderDevice.cpp @@ -663,11 +663,14 @@ vaDrawResultFlags vaRenderDevice::ClearUAV( vaRenderDeviceContext & renderContex assert( false ); // codepath never tested/debugged through, sorry - test it and remove assert please :) assert( !vaResourceFormatHelpers::IsFloat(buffer->GetResourceFormat()) ); assert( vaResourceFormatHelpers::GetChannelCount(buffer->GetResourceFormat()) == 4 ); + assert( buffer->GetElementCount() > 0 ); // clear data - PostProcessConstants consts; reinterpret_cast(consts.Param1) = clearValue; + PostProcessConstants consts; memset( &consts, 0, sizeof(consts) ); reinterpret_cast(consts.Param1) = clearValue; + consts.Bounds.x = (int)buffer->GetElementCount()-1; m_PPConstants->Upload( renderContext, &consts, sizeof(consts) ); // setup dispatch vaComputeItem computeItem; + computeItem.ConstantBuffers[POSTPROCESS_CONSTANTSBUFFERSLOT] = m_PPConstants; computeItem.ComputeShader = m_CSClearUAV_Buff_4U; computeItem.SetDispatch( (uint32)(buffer->GetElementCount() + 63) / 64 ); return renderContext.ExecuteSingleItem( computeItem, vaRenderOutputs::FromUAVs( buffer ), nullptr ); @@ -678,20 +681,27 @@ vaDrawResultFlags vaRenderDevice::ClearUAV( vaRenderDeviceContext & renderContex assert( false ); // codepath never tested/debugged through, sorry - test it and remove assert please :) assert( !vaResourceFormatHelpers::IsFloat(buffer->GetResourceFormat()) ); assert( vaResourceFormatHelpers::GetChannelCount(buffer->GetResourceFormat()) == 1 ); + assert( buffer->GetElementCount() > 0 ); // clear data - PostProcessConstants consts; reinterpret_cast(consts.Param1).x = clearValue; + PostProcessConstants consts; memset( &consts, 0, sizeof(consts) ); reinterpret_cast(consts.Param1).x = clearValue; + consts.Bounds.x = (int)buffer->GetElementCount()-1; m_PPConstants->Upload( renderContext, &consts, sizeof(consts) ); // setup dispatch vaComputeItem computeItem; + computeItem.ConstantBuffers[POSTPROCESS_CONSTANTSBUFFERSLOT] = m_PPConstants; computeItem.ComputeShader = m_CSClearUAV_Buff_1U; computeItem.SetDispatch( (uint32)(buffer->GetElementCount() + 63) / 64 ); return renderContext.ExecuteSingleItem( computeItem, vaRenderOutputs::FromUAVs( buffer ), nullptr ); } -vaDrawResultFlags vaRenderDevice::ClearTextureUAVGeneric( vaRenderDeviceContext & renderContext, const shared_ptr & texture, const shared_ptr & computeShader, const PostProcessConstants & clearValue ) +vaDrawResultFlags vaRenderDevice::ClearTextureUAVGeneric( vaRenderDeviceContext & renderContext, const shared_ptr & texture, const shared_ptr & computeShader, PostProcessConstants clearValue ) { + clearValue.Bounds.x = texture->GetWidth()-1; + clearValue.Bounds.y = texture->GetHeight()-1; + m_PPConstants->Upload( renderContext, &clearValue, sizeof(clearValue) ); vaComputeItem computeItem; + computeItem.ConstantBuffers[POSTPROCESS_CONSTANTSBUFFERSLOT] = m_PPConstants; computeItem.ComputeShader = computeShader; if( texture->GetType() == vaTextureType::Texture1D ) computeItem.SetDispatch( (uint32)(texture->GetWidth() + 63) / 64 ); diff --git a/Source/Rendering/vaRenderDevice.h b/Source/Rendering/vaRenderDevice.h index 00313de..d67bb62 100644 --- a/Source/Rendering/vaRenderDevice.h +++ b/Source/Rendering/vaRenderDevice.h @@ -449,7 +449,7 @@ namespace Vanilla vaDrawResultFlags ClearUAV( vaRenderDeviceContext & renderContext, const shared_ptr & texture, uint clearValue ); vaDrawResultFlags ClearUAV( vaRenderDeviceContext & renderContext, const shared_ptr & texture, const vaVector4ui & clearValue ); private: - vaDrawResultFlags ClearTextureUAVGeneric( vaRenderDeviceContext & renderContext, const shared_ptr & texture, const shared_ptr & computeShader, const PostProcessConstants & clearValue ); + vaDrawResultFlags ClearTextureUAVGeneric( vaRenderDeviceContext & renderContext, const shared_ptr & texture, const shared_ptr & computeShader, PostProcessConstants clearValue ); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public: diff --git a/VisualStudio/AllModules/AllModules.vcxproj b/VisualStudio/AllModules/AllModules.vcxproj index ccb83e7..2a4cb5a 100644 --- a/VisualStudio/AllModules/AllModules.vcxproj +++ b/VisualStudio/AllModules/AllModules.vcxproj @@ -417,6 +417,11 @@ Document + + + Document + + {30430E2D-C12E-4537-866E-52B1B49F6410} Vanilla diff --git a/VisualStudio/AllModules/AllModules.vcxproj.filters b/VisualStudio/AllModules/AllModules.vcxproj.filters index 3d644c4..88a1986 100644 --- a/VisualStudio/AllModules/AllModules.vcxproj.filters +++ b/VisualStudio/AllModules/AllModules.vcxproj.filters @@ -870,6 +870,9 @@ Rendering + + Rendering\Shaders +