1 Shader "Hidden/PostProcessing/Debug/Histogram"
5 #pragma exclude_renderers gles gles3 d3d11_9x
7 #include "../StdLib.hlsl"
10 #define HISTOGRAM_BINS 128
12 #define HISTOGRAM_BINS 256
15 struct VaryingsHistogram
17 float4 vertex : SV_POSITION;
18 float2 texcoord : TEXCOORD0;
19 float maxValue : TEXCOORD1;
22 StructuredBuffer<uint> _HistogramBuffer;
23 float2 _Params; // x: width, y: height
25 float FindMaxHistogramValue()
30 for (uint i = 0; i < HISTOGRAM_BINS; i++)
32 uint h = _HistogramBuffer[i];
33 maxValue = max(maxValue, h);
36 return float(maxValue);
39 VaryingsHistogram Vert(AttributesDefault v)
42 o.vertex = float4(v.vertex.xy, 0.0, 1.0);
43 o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
45 #if UNITY_UV_STARTS_AT_TOP
46 o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
49 #if SHADER_API_GLES3 // No texture loopup in VS on GLES3/Android
52 o.maxValue = _Params.y / FindMaxHistogramValue();
58 float4 Frag(VaryingsHistogram i) : SV_Target
61 float maxValue = _Params.y / FindMaxHistogramValue();
63 float maxValue = i.maxValue;
66 const float kBinsMinusOne = HISTOGRAM_BINS - 1.0;
67 float remapI = i.texcoord.x * kBinsMinusOne;
68 uint index = floor(remapI);
69 float delta = frac(remapI);
70 float v1 = float(_HistogramBuffer[index]) * maxValue;
71 float v2 = float(_HistogramBuffer[min(index + 1, kBinsMinusOne)]) * maxValue;
72 float h = v1 * (1.0 - delta) + v2 * delta;
73 uint y = (uint)round(i.texcoord.y * _Params.y);
75 float3 color = (0.0).xxx;
76 float fill = step(y, h);
77 color = lerp(color, (1.0).xxx, fill);
78 return float4(color, 1.0);
85 Cull Off ZWrite Off ZTest Always