Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit e053c92

Browse files
committed
Improved waveform performances on consoles
Will need to rewrite this at some point, it uses quite a naive approach
1 parent adda4b3 commit e053c92

File tree

5 files changed

+23
-20
lines changed

5 files changed

+23
-20
lines changed

PostProcessing/Runtime/Monitors/WaveformMonitor.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ internal override void Render(PostProcessRenderContext context)
4545
cmd.BeginSample("Waveform");
4646

4747
var parameters = new Vector4(
48-
context.width / 2,
49-
context.height / 2,
48+
width,
49+
height,
5050
RuntimeUtilities.isLinearColorSpace ? 1 : 0,
5151
0f
5252
);
@@ -55,15 +55,21 @@ internal override void Render(PostProcessRenderContext context)
5555
int kernel = compute.FindKernel("KWaveformClear");
5656
cmd.SetComputeBufferParam(compute, kernel, "_WaveformBuffer", m_Data);
5757
cmd.SetComputeVectorParam(compute, "_Params", parameters);
58-
cmd.SetComputeVectorParam(compute, "_BufferParams", new Vector4(width, height, 0f, 0f));
5958
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(width / 16f), Mathf.CeilToInt(height / 16f), 1);
6059

60+
// For performance reasons, especially on consoles, we'll just downscale the source
61+
// again to reduce VMEM stalls. Eventually the whole algorithm needs to be rewritten as
62+
// it's currently pretty naive.
63+
cmd.GetTemporaryRT(ShaderIDs.WaveformSource, width, height, 0, FilterMode.Bilinear, context.sourceFormat);
64+
cmd.BlitFullscreenTriangle(ShaderIDs.HalfResFinalCopy, ShaderIDs.WaveformSource);
65+
6166
// Gather all pixels and fill in our waveform
6267
kernel = compute.FindKernel("KWaveformGather");
6368
cmd.SetComputeBufferParam(compute, kernel, "_WaveformBuffer", m_Data);
64-
cmd.SetComputeTextureParam(compute, kernel, "_Source", ShaderIDs.HalfResFinalCopy);
69+
cmd.SetComputeTextureParam(compute, kernel, "_Source", ShaderIDs.WaveformSource);
6570
cmd.SetComputeVectorParam(compute, "_Params", parameters);
66-
cmd.DispatchCompute(compute, kernel, Mathf.CeilToInt(context.width / 2f), Mathf.CeilToInt(context.width / 2f / 256f), 1);
71+
cmd.DispatchCompute(compute, kernel, width, Mathf.CeilToInt(height / 256f), 1);
72+
cmd.ReleaseTemporaryRT(ShaderIDs.WaveformSource);
6773

6874
// Generate the waveform texture
6975
var sheet = context.propertySheets.Get(context.resources.shaders.waveform);

PostProcessing/Runtime/PostProcessMonitors.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ internal void Render(PostProcessRenderContext context)
5656

5757
if (needHalfRes)
5858
{
59-
var format = RenderTextureFormat.ARGBHalf;
60-
format = SystemInfo.SupportsRenderTextureFormat(format) ? format : RenderTextureFormat.ARGB32;
61-
cmd.GetTemporaryRT(ShaderIDs.HalfResFinalCopy, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, format);
59+
cmd.GetTemporaryRT(ShaderIDs.HalfResFinalCopy, context.width / 2, context.height / 2, 0, FilterMode.Bilinear, context.sourceFormat);
6260
cmd.Blit(context.destination, ShaderIDs.HalfResFinalCopy);
6361
}
6462

PostProcessing/Runtime/Utils/ShaderIDs.cs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static class ShaderIDs
101101
internal static readonly int Interp = Shader.PropertyToID("_Interp");
102102

103103
internal static readonly int HalfResFinalCopy = Shader.PropertyToID("_HalfResFinalCopy");
104+
internal static readonly int WaveformSource = Shader.PropertyToID("_WaveformSource");
104105
internal static readonly int WaveformBuffer = Shader.PropertyToID("_WaveformBuffer");
105106
internal static readonly int VectorscopeBuffer = Shader.PropertyToID("_VectorscopeBuffer");
106107
}

PostProcessing/Shaders/Debug/Waveform.compute

+9-11
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,36 @@ SamplerState sampler_Source;
77

88
CBUFFER_START(Params)
99
float4 _Params; // x: source width, y: source height, z: linear, w: unused
10-
float4 _BufferParams; // x: buffer width, y: buffer height, zw: unused
1110
CBUFFER_END
1211

1312
#pragma kernel KWaveformGather
1413
[numthreads(1, 256, 1)]
15-
void KWaveformGather(uint2 dispatchThreadId : SV_DispatchThreadID, uint2 groupThreadId : SV_GroupThreadID)
14+
void KWaveformGather(uint2 dispatchThreadId : SV_DispatchThreadID)
1615
{
1716
// Gather local group histogram
1817
if (dispatchThreadId.x < uint(_Params.x) && dispatchThreadId.y < uint(_Params.y))
1918
{
20-
float2 coords = float2(dispatchThreadId) / _Params.xy;
21-
float3 color = _Source.SampleLevel(sampler_Source, coords, 0).xyz;
19+
float3 color = _Source[dispatchThreadId].rgb;
2220
color = saturate(color);
2321

2422
// We want a gamma-corrected histogram (like Photoshop & all)
2523
if (_Params.z > 0)
2624
color = LinearToSRGB(color);
2725

2826
// Convert channel values to histogram bins
29-
uint3 idx = (uint3)(round(color * (_BufferParams.y - 1)));
27+
uint3 idx = (uint3)(round(color * (_Params.y - 1)));
28+
idx += dispatchThreadId.x * _Params.y;
3029

31-
uint bufferX = uint(round(coords.x * (_BufferParams.x - 1)));
32-
if (idx.x > 0u) InterlockedAdd(_WaveformBuffer[idx.x * uint(_BufferParams.x) + bufferX].x, 1u); // Red
33-
if (idx.y > 0u) InterlockedAdd(_WaveformBuffer[idx.y * uint(_BufferParams.x) + bufferX].y, 1u); // Green
34-
if (idx.z > 0u) InterlockedAdd(_WaveformBuffer[idx.z * uint(_BufferParams.x) + bufferX].z, 1u); // Blue
30+
if (idx.x > 0u) InterlockedAdd(_WaveformBuffer[idx.x].x, 1u); // Red
31+
if (idx.y > 0u) InterlockedAdd(_WaveformBuffer[idx.y].y, 1u); // Green
32+
if (idx.z > 0u) InterlockedAdd(_WaveformBuffer[idx.z].z, 1u); // Blue
3533
}
3634
}
3735

3836
#pragma kernel KWaveformClear
3937
[numthreads(16, 16, 1)]
4038
void KWaveformClear(uint2 dispatchThreadId : SV_DispatchThreadID)
4139
{
42-
if (dispatchThreadId.x < uint(_BufferParams.x) && dispatchThreadId.y < uint(_BufferParams.y))
43-
_WaveformBuffer[dispatchThreadId.y * uint(_BufferParams.x) + dispatchThreadId.x] = uint4(0u, 0u, 0u, 0u);
40+
if (dispatchThreadId.x < uint(_Params.x) && dispatchThreadId.y < uint(_Params.y))
41+
_WaveformBuffer[dispatchThreadId.y * uint(_Params.x) + dispatchThreadId.x] = uint4(0u, 0u, 0u, 0u);
4442
}

PostProcessing/Shaders/Debug/Waveform.shader

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Shader "Hidden/PostProcessing/Debug/Waveform"
2727
float3 color = float3(0.0, 0.0, 0.0);
2828

2929
uint2 uvI = i.vertex.xy;
30-
float3 w = _WaveformBuffer[uvI.y * _Params.x + uvI.x];
30+
float3 w = _WaveformBuffer[uvI.x * _Params.y + uvI.y].xyz;
3131

3232
color += red * w.r;
3333
color += green * w.g;

0 commit comments

Comments
 (0)