3 namespace UnityEngine
.Rendering
.PostProcessing
6 /// This class holds settings for the Histogram monitor.
9 public sealed class HistogramMonitor
: Monitor
12 /// Displayable channels.
22 /// The green channel.
32 /// The master (luminance) channel.
38 /// The width of the rendered histogram.
40 public int width
= 512;
43 /// The height of the rendered histogram.
45 public int height
= 256;
48 /// The channel to render.
50 public Channel channel
= Channel
.Master
;
53 const int k_NumBins
= 256;
54 const int k_ThreadGroupSizeX
= 16;
55 const int k_ThreadGroupSizeY
= 16;
57 internal override void OnDisable()
67 internal override bool NeedsHalfRes()
72 internal override bool ShaderResourcesAvailable(PostProcessRenderContext context
)
74 return context
.resources
.computeShaders
.gammaHistogram
;
77 internal override void Render(PostProcessRenderContext context
)
79 CheckOutput(width
, height
);
82 m_Data
= new ComputeBuffer(k_NumBins
, sizeof(uint));
84 var compute
= context
.resources
.computeShaders
.gammaHistogram
;
85 var cmd
= context
.command
;
86 cmd
.BeginSample("GammaHistogram");
88 // Clear the buffer on every frame as we use it to accumulate values on every frame
89 int kernel
= compute
.FindKernel("KHistogramClear");
90 cmd
.SetComputeBufferParam(compute
, kernel
, "_HistogramBuffer", m_Data
);
91 cmd
.DispatchCompute(compute
, kernel
, Mathf
.CeilToInt(k_NumBins
/ (float)k_ThreadGroupSizeX
), 1, 1);
93 // Gather all pixels and fill in our histogram
94 kernel
= compute
.FindKernel("KHistogramGather");
95 var parameters
= new Vector4(
98 RuntimeUtilities
.isLinearColorSpace
? 1 : 0,
102 cmd
.SetComputeVectorParam(compute
, "_Params", parameters
);
103 cmd
.SetComputeTextureParam(compute
, kernel
, "_Source", ShaderIDs
.HalfResFinalCopy
);
104 cmd
.SetComputeBufferParam(compute
, kernel
, "_HistogramBuffer", m_Data
);
105 cmd
.DispatchCompute(compute
, kernel
,
106 Mathf
.CeilToInt(parameters
.x
/ k_ThreadGroupSizeX
),
107 Mathf
.CeilToInt(parameters
.y
/ k_ThreadGroupSizeY
),
111 // Generate the histogram texture
112 var sheet
= context
.propertySheets
.Get(context
.resources
.shaders
.gammaHistogram
);
113 sheet
.properties
.SetVector(ShaderIDs
.Params
, new Vector4(width
, height
, 0f
, 0f
));
114 sheet
.properties
.SetBuffer(ShaderIDs
.HistogramBuffer
, m_Data
);
115 cmd
.BlitFullscreenTriangle(BuiltinRenderTextureType
.None
, output
, sheet
, 0);
117 cmd
.EndSample("GammaHistogram");