Skip to content

Commit

Permalink
[ScreenSpaceShadows] Fixed all screen glitches by using a sampler as …
Browse files Browse the repository at this point in the history
…defined in the comments of the shader
  • Loading branch information
PanosK92 committed Dec 1, 2023
1 parent 0d51132 commit 38849b8
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 50 deletions.
2 changes: 1 addition & 1 deletion data/shaders/common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ float3 get_normal(uint2 pos)

float3 get_normal(float2 uv)
{
return tex_normal.SampleLevel(samplers[sampler_point_clamp], uv, 0).xyz;
return tex_normal.SampleLevel(samplers[sampler_point_clamp_edge], uv, 0).xyz;
}

float3 get_normal_view_space(uint2 pos)
Expand Down
17 changes: 9 additions & 8 deletions data/shaders/common_samplers.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// comparsion
static const int sampler_compare_depth = 0;
static const int sampler_compare_depth = 0;

// regular
static const int sampler_point_clamp = 0;
static const int sampler_point_wrap = 1;
static const int sampler_bilinear_clamp = 2;
static const int sampler_bilinear_wrap = 3;
static const int sampler_trilinear_clamp = 4;
static const int sampler_anisotropic_wrap = 5;
static const int sampler_point_clamp_edge = 0;
static const int sampler_point_clamp_border = 1;
static const int sampler_point_wrap = 2;
static const int sampler_bilinear_clamp = 3;
static const int sampler_bilinear_wrap = 4;
static const int sampler_trilinear_clamp = 5;
static const int sampler_anisotropic_wrap = 6;

// samplers
SamplerComparisonState samplers_comparison[1] : register(s0, space1);
SamplerState samplers[6] : register(s1, space2);
SamplerState samplers[7] : register(s1, space2);
2 changes: 1 addition & 1 deletion data/shaders/motion_blur.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ float2 get_velocity_3x3_average(float2 uv)
for (int x = -1; x <= 1; ++x)
{
float2 offset = float2(x, y) * get_rt_texel_size();
float2 velocity = tex_velocity.SampleLevel(samplers[sampler_point_clamp], uv + offset, 0).xy;
float2 velocity = tex_velocity.SampleLevel(samplers[sampler_point_clamp_edge], uv + offset, 0).xy;

total_velocity += velocity;
++sample_count;
Expand Down
20 changes: 10 additions & 10 deletions data/shaders/screen_space_shadows/bend_sss.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ void mainCS
{
DispatchParameters in_parameters;
in_parameters.SetDefaults();
in_parameters.LightCoordinate = pass_get_f4_value(); // Values stored in DispatchList::LightCoordinate_Shader by BuildDispatchList()
in_parameters.WaveOffset = pass_get_resolution_in(); // Values stored in DispatchData::WaveOffset_Shader by BuildDispatchList()
in_parameters.NearDepthValue = pass_get_f3_value().x; // Set to the Depth Buffer Value for the near clip plane, as determined by renderer projection matrix setup (typically 1).
in_parameters.FarDepthValue = pass_get_f3_value().y; // Set to the Depth Buffer Value for the far clip plane, as determined by renderer projection matrix setup (typically 0).
in_parameters.ArraySliceIndex = pass_get_f3_value().z;
in_parameters.InvDepthTextureSize = pass_get_f3_value2().xy; // Inverse of the texture dimensions for 'DepthTexture' (used to convert from pixel coordinates to UVs)
in_parameters.LightCoordinate = pass_get_f4_value(); // Values stored in DispatchList::LightCoordinate_Shader by BuildDispatchList()
in_parameters.WaveOffset = pass_get_resolution_in(); // Values stored in DispatchData::WaveOffset_Shader by BuildDispatchList()
in_parameters.NearDepthValue = pass_get_f3_value().x; // Set to the Depth Buffer Value for the near clip plane, as determined by renderer projection matrix setup (typically 1).
in_parameters.FarDepthValue = pass_get_f3_value().y; // Set to the Depth Buffer Value for the far clip plane, as determined by renderer projection matrix setup (typically 0).
in_parameters.ArraySliceIndex = pass_get_f3_value().z;
in_parameters.InvDepthTextureSize = pass_get_f3_value2().xy; // Inverse of the texture dimensions for 'DepthTexture' (used to convert from pixel coordinates to UVs)
in_parameters.DepthTexture = tex;
in_parameters.OutputTexture = tex_uav_sss;
in_parameters.PointBorderSampler = samplers[sampler_point_wrap]; // A point sampler, with Wrap Mode set to Clamp-To-Border-Color (D3D12_TEXTURE_ADDRESS_MODE_BORDER), and Border Color set to "FarDepthValue" (typically zero), or some other far-depth value out of DepthBounds.
in_parameters.DebugOutputEdgeMask = false; // Use this to visualize edges, for tuning the 'BilinearThreshold' value.
in_parameters.DebugOutputThreadIndex = false; // Debug output to visualize layout of compute threads
in_parameters.DebugOutputWaveIndex = false; // Debug output to visualize layout of compute wavefronts, useful to sanity check the Light Coordinate is being computed correctly.
in_parameters.PointBorderSampler = samplers[sampler_point_clamp_border]; // A point sampler, with Wrap Mode set to Clamp-To-Border-Color (D3D12_TEXTURE_ADDRESS_MODE_BORDER), and Border Color set to "FarDepthValue" (typically zero), or some other far-depth value out of DepthBounds.
in_parameters.DebugOutputEdgeMask = false; // Use this to visualize edges, for tuning the 'BilinearThreshold' value.
in_parameters.DebugOutputThreadIndex = false; // Debug output to visualize layout of compute threads
in_parameters.DebugOutputWaveIndex = false; // Debug output to visualize layout of compute wavefronts, useful to sanity check the Light Coordinate is being computed correctly.

WriteScreenSpaceShadow(in_parameters, Gid, GTid.x);
}
Expand Down
5 changes: 0 additions & 5 deletions data/shaders/screen_space_shadows/bend_sss_gpu.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,6 @@ void WriteScreenSpaceShadow(struct DispatchParameters inParameters, int3 inGroup
// If the first samples are always producing a hard shadow, then compute this value separately.
result = min(hard_shadow, result);

//= PANOS - SCREEN EDGE FADE ===========================
float2 uv = write_xy * inParameters.InvDepthTextureSize;
result = saturate(result + 1.0 - screen_fade(uv));
//======================================================

//write the result
{
if (inParameters.DebugOutputEdgeMask)
Expand Down
12 changes: 6 additions & 6 deletions data/shaders/shadow_mapping.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ float shadow_sample_depth(float3 uv)
{
// float3 -> uv, slice
if (light_is_directional())
return tex_light_directional_depth.SampleLevel(samplers[sampler_point_clamp], uv, 0).r;
return tex_light_directional_depth.SampleLevel(samplers[sampler_point_clamp_edge], uv, 0).r;

// float3 -> direction
if (light_is_point())
return tex_light_point_depth.SampleLevel(samplers[sampler_point_clamp], uv, 0).r;
return tex_light_point_depth.SampleLevel(samplers[sampler_point_clamp_edge], uv, 0).r;

// float3 -> uv, 0
if (light_is_spot())
return tex_light_spot_depth.SampleLevel(samplers[sampler_point_clamp], uv.xy, 0).r;
return tex_light_spot_depth.SampleLevel(samplers[sampler_point_clamp_edge], uv.xy, 0).r;

return 0.0f;
}
Expand All @@ -90,15 +90,15 @@ float3 shadow_sample_color(float3 uv)
{
// float3 -> uv, slice
if (light_is_directional())
return tex_light_directional_color.SampleLevel(samplers[sampler_point_clamp], uv, 0).rgb;
return tex_light_directional_color.SampleLevel(samplers[sampler_point_clamp_edge], uv, 0).rgb;

// float3 -> direction
if (light_is_point())
return tex_light_point_color.SampleLevel(samplers[sampler_point_clamp], uv, 0).rgb;
return tex_light_point_color.SampleLevel(samplers[sampler_point_clamp_edge], uv, 0).rgb;

// float3 -> uv, 0
if (light_is_spot())
return tex_light_spot_color.SampleLevel(samplers[sampler_point_clamp], uv.xy, 0).rgb;
return tex_light_spot_color.SampleLevel(samplers[sampler_point_clamp_edge], uv.xy, 0).rgb;

return 0.0f;
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/RHI/RHI_Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ namespace Spartan
{
Wrap,
Mirror,
Clamp,
Border,
ClampToEdge,
ClampToBorder,
MirrorOnce,
};

Expand Down
2 changes: 1 addition & 1 deletion runtime/RHI/RHI_Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace Spartan
static std::unordered_map<uint64_t, RHI_DescriptorSet>& GetDescriptorSets();
static void* GetDescriptorSet(const RHI_Device_Resource resource_type);
static void* GetDescriptorSetLayout(const RHI_Device_Resource resource_type);
static void SetBindlessSamplers(const std::array<std::shared_ptr<RHI_Sampler>, 7>& samplers);
static void SetBindlessSamplers(const std::array<std::shared_ptr<RHI_Sampler>, 8>& samplers);

// Pipelines
static void GetOrCreatePipeline(RHI_PipelineState& pso, RHI_Pipeline*& pipeline, RHI_DescriptorSetLayout*& descriptor_set_layout);
Expand Down
15 changes: 8 additions & 7 deletions runtime/RHI/Vulkan/Vulkan_Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ namespace Spartan
return VkDescriptorType::VK_DESCRIPTOR_TYPE_MAX_ENUM;
}

void RHI_Device::SetBindlessSamplers(const array<shared_ptr<RHI_Sampler>, 7>& samplers)
void RHI_Device::SetBindlessSamplers(const array<shared_ptr<RHI_Sampler>, 8>& samplers)
{
descriptors::pipelines.clear();

Expand Down Expand Up @@ -1431,12 +1431,13 @@ namespace Spartan

vector<shared_ptr<RHI_Sampler>> samplers_regular =
{
samplers[1], // point_clamp
samplers[2], // point_wrap
samplers[3], // bilinear_clamp
samplers[4], // bilinear_wrap
samplers[5], // trilinear_clamp
samplers[6] // anisotropic_wrap
samplers[1], // point_clamp_edge
samplers[2], // point_clamp_border
samplers[3], // point_wrap
samplers[4], // bilinear_clamp
samplers[5], // bilinear_wrap
samplers[6], // trilinear_clamp
samplers[7] // anisotropic_wrap
};

descriptors::create_descriptor_set_samplers(samplers_regular, 1, RHI_Device_Resource::sampler_regular);
Expand Down
3 changes: 2 additions & 1 deletion runtime/Rendering/Renderer_Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ namespace Spartan
enum class Renderer_Sampler
{
Compare_depth,
Point_clamp,
Point_clamp_edge,
Point_clamp_border,
Point_wrap,
Bilinear_clamp,
Bilinear_wrap,
Expand Down
17 changes: 9 additions & 8 deletions runtime/Rendering/Renderer_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace Spartan
// renderer resources
array<shared_ptr<RHI_Texture>, render_target_count> m_render_targets;
array<shared_ptr<RHI_Shader>, shader_count> m_shaders;
array<shared_ptr<RHI_Sampler>, 7> m_samplers;
array<shared_ptr<RHI_Sampler>, 8> m_samplers;
array<shared_ptr<RHI_ConstantBuffer>, 3> m_constant_buffers;
array<shared_ptr<RHI_StructuredBuffer>, 2> m_structured_buffers;

Expand Down Expand Up @@ -148,13 +148,14 @@ namespace Spartan
#define sampler(x) m_samplers[static_cast<uint8_t>(x)]
if (!create_only_anisotropic)
{
// arguments: min, max, mip, address mode, comparison, anisotropy, comparison enabled
sampler(Renderer_Sampler::Compare_depth) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Clamp, RHI_Comparison_Function::Greater, 0.0f, true); // reverse-z
sampler(Renderer_Sampler::Point_clamp) = make_shared<RHI_Sampler>(RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Clamp, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Point_wrap) = make_shared<RHI_Sampler>(RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Wrap, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Bilinear_clamp) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Clamp, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Bilinear_wrap) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Wrap, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Trilinear_clamp) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Linear, RHI_Sampler_Address_Mode::Clamp, RHI_Comparison_Function::Always);
// arguments: min, max, mip, address mode, comparison, anisotropy, comparison enabled
sampler(Renderer_Sampler::Compare_depth) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::ClampToEdge, RHI_Comparison_Function::Greater, 0.0f, true); // reverse-z
sampler(Renderer_Sampler::Point_clamp_edge) = make_shared<RHI_Sampler>(RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::ClampToEdge, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Point_clamp_border) = make_shared<RHI_Sampler>(RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::ClampToBorder, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Point_wrap) = make_shared<RHI_Sampler>(RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Wrap, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Bilinear_clamp) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::ClampToEdge, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Bilinear_wrap) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Nearest, RHI_Sampler_Address_Mode::Wrap, RHI_Comparison_Function::Always);
sampler(Renderer_Sampler::Trilinear_clamp) = make_shared<RHI_Sampler>(RHI_Filter::Linear, RHI_Filter::Linear, RHI_Filter::Linear, RHI_Sampler_Address_Mode::ClampToEdge, RHI_Comparison_Function::Always);
}

float anisotropy = GetOption<float>(Renderer_Option::Anisotropy);
Expand Down

0 comments on commit 38849b8

Please sign in to comment.