You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. #include "UnityCG.cginc"
  2. RWStructuredBuffer<uint4> _Waveform;
  3. Texture2D<float4> _Source;
  4. CBUFFER_START (Params)
  5. uint _IsLinear;
  6. uint4 _Channels;
  7. CBUFFER_END
  8. #define COLUMNS 384
  9. #pragma kernel KWaveform
  10. [numthreads(1,COLUMNS,1)]
  11. void KWaveform(uint2 dispatchThreadId : SV_DispatchThreadID)
  12. {
  13. // We want a gamma corrected colors
  14. float3 color = _Source[dispatchThreadId].rgb;
  15. if (_IsLinear > 0u)
  16. color = LinearToGammaSpace(color);
  17. color = saturate(color);
  18. // Convert color & luminance to histogram bins
  19. const float kColumnsMinusOne = COLUMNS - 1.0;
  20. uint3 idx_c = (uint3)(round(color * kColumnsMinusOne));
  21. uint idx_l = (uint)(round(dot(color.rgb, float3(0.2126, 0.7152, 0.0722)) * kColumnsMinusOne));
  22. // A lot of atomic operations will be skipped so there's no need to over-think this one.
  23. uint j = dispatchThreadId.x * COLUMNS;
  24. if (_Channels.x > 0u && idx_c.x > 0u) InterlockedAdd(_Waveform[j + idx_c.x].x, 1u); // Red
  25. if (_Channels.y > 0u && idx_c.y > 0u) InterlockedAdd(_Waveform[j + idx_c.y].y, 1u); // Green
  26. if (_Channels.z > 0u && idx_c.z > 0u) InterlockedAdd(_Waveform[j + idx_c.z].z, 1u); // Blue
  27. if (_Channels.w > 0u) InterlockedAdd(_Waveform[j + idx_l].w, 1u); // Luminance
  28. }
  29. #pragma kernel KWaveformClear
  30. [numthreads(1, COLUMNS, 1)]
  31. void KWaveformClear(uint2 dispatchThreadId : SV_DispatchThreadID)
  32. {
  33. _Waveform[dispatchThreadId.x * COLUMNS + dispatchThreadId.y] = uint4(0u, 0u, 0u, 0u);
  34. }