post processing
[WindSway-HDRP.git] / Library / PackageCache / com.unity.postprocessing@2.1.6 / PostProcessing / Runtime / Monitors / HistogramMonitor.cs
blob33c015a0bc7ffe86f890878895baf45b92d8600a
1 using System;
3 namespace UnityEngine.Rendering.PostProcessing
5 /// <summary>
6 /// This class holds settings for the Histogram monitor.
7 /// </summary>
8 [Serializable]
9 public sealed class HistogramMonitor : Monitor
11 /// <summary>
12 /// Displayable channels.
13 /// </summary>
14 public enum Channel
16 /// <summary>
17 /// The red channel.
18 /// </summary>
19 Red,
21 /// <summary>
22 /// The green channel.
23 /// </summary>
24 Green,
26 /// <summary>
27 /// The blue channel.
28 /// </summary>
29 Blue,
31 /// <summary>
32 /// The master (luminance) channel.
33 /// </summary>
34 Master
37 /// <summary>
38 /// The width of the rendered histogram.
39 /// </summary>
40 public int width = 512;
42 /// <summary>
43 /// The height of the rendered histogram.
44 /// </summary>
45 public int height = 256;
47 /// <summary>
48 /// The channel to render.
49 /// </summary>
50 public Channel channel = Channel.Master;
52 ComputeBuffer m_Data;
53 const int k_NumBins = 256;
54 const int k_ThreadGroupSizeX = 16;
55 const int k_ThreadGroupSizeY = 16;
57 internal override void OnDisable()
59 base.OnDisable();
61 if (m_Data != null)
62 m_Data.Release();
64 m_Data = null;
67 internal override bool NeedsHalfRes()
69 return true;
72 internal override bool ShaderResourcesAvailable(PostProcessRenderContext context)
74 return context.resources.computeShaders.gammaHistogram;
77 internal override void Render(PostProcessRenderContext context)
79 CheckOutput(width, height);
81 if (m_Data == null)
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(
96 context.width / 2,
97 context.height / 2,
98 RuntimeUtilities.isLinearColorSpace ? 1 : 0,
99 (int)channel
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");