Blender V5.3
gpu_shader_sequencer_scope.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
12
13/* Match eSpaceSeq_RegionType */
20
23
36
38
39 [[storage(0, read_write)]] SeqScopeRasterData (&raster_buf)[];
40
41 void put_pixel(int x, int y, float4 col)
42 {
43 int index = y * view_width + x;
44
45 /* Use 24.8 fixed point; this allows 16M points to hit the same
46 * location without overflowing the values. */
47 uint4 col_fx = uint4(col * 255.0f + 0.5f);
48 atomicAdd(raster_buf[index].col_r, col_fx.r);
49 atomicAdd(raster_buf[index].col_g, col_fx.g);
50 atomicAdd(raster_buf[index].col_b, col_fx.b);
51 atomicAdd(raster_buf[index].col_a, col_fx.a);
52 }
53
54 /* Convert from sRGB to linear using the sRGB EOTF. */
55 float srgb_to_linear(float c) const
56 {
57 return (c < 0.04045f) ? c / 12.92f : pow((c + 0.055f) / 1.055f, 2.4f);
58 }
59
60 /* Convert from scope space to linear. See libocio_scope.cc for details
61 * on this mapping. */
62 float scope_to_linear(float c) const
63 {
64 const float SCOPE_SDR_IN_HDR_SCOPE = 0.75f;
65 const float SCOPE_HDR_B = 0.0368291f;
66 const float SCOPE_HDR_C = 0.8882908f;
67 const float SCOPE_HDR_D = 0.8307242f;
68
69 if (scope_is_hdr) {
70 if (c > SCOPE_SDR_IN_HDR_SCOPE) {
71 return exp((c - SCOPE_HDR_D) / SCOPE_HDR_B) + SCOPE_HDR_C;
72 }
73 c /= SCOPE_SDR_IN_HDR_SCOPE;
74 }
75 return srgb_to_linear(c);
76 }
77
78 /* Convert from linear Rec.709 to extended sRGB. */
79 float linear_to_extended_srgb(float c) const
80 {
81 float c_sign = sign(c);
82 c = abs(c);
83 c = (c < 0.0031308f) ? c * 12.92f : 1.055f * pow(c, 1.0f / 2.4f) - 0.055f;
84 return c_sign * c;
85 }
86
87 /* Convert from scope space color (that is suitable for plotting positions)
88 * to extended sRGB (the color space of the frame-buffer). */
90 {
91 float3 scope_linear = float3(scope_to_linear(max(color.r, 0.0f)),
92 scope_to_linear(max(color.g, 0.0f)),
93 scope_to_linear(max(color.b, 0.0f)));
94
95 float3 rec709_linear = scope_gamut_to_rec709 * scope_linear;
96
97 return float3(linear_to_extended_srgb(rec709_linear.r),
98 linear_to_extended_srgb(rec709_linear.g),
99 linear_to_extended_srgb(rec709_linear.b));
100 }
101};
102
103/* Compute shader that rasterizes scope points into screen-sized
104 * raster buffer, with accumulated R,G,B,A values in fixed point.
105 *
106 * For pixels covered by each point, just do atomic adds into
107 * the buffer. Even if this has potential to be highly contented
108 * on the same memory locations, it is still way faster than rasterizing
109 * points using regular GPU pipeline. */
111 [[global_invocation_id]] const uint3 global_id)
112{
113 /* Fetch pixel from the input image, corresponding to current point. */
114 int2 texel = int2(global_id.xy);
115 if (any(greaterThanEqual(texel, int2(srt.image_width, srt.image_height)))) {
116 return;
117 }
118
119 float4 image_color = texelFetch(srt.image, texel, 0);
120 if (srt.img_premultiplied) {
121 color_alpha_unpremultiply(image_color, image_color);
122 }
123
124 /* Convert scope space image color to position and frame-buffer color. */
125 float3 position = clamp(image_color.rgb, 0.0f, 1.0f);
126 float4 color = float4(srt.scope_to_extended_srgb(image_color.rgb), 1.0f);
127
128 /* Calculate point position based on scope mode; possibly adjust color too. */
129 float2 pos = float2(0.0);
130 if (srt.scope_mode == SEQ_DRAW_IMG_WAVEFORM) {
131 /* Waveform: pixel height based on luminance. */
132 pos.x = texel.x - srt.image_width / 2;
133 pos.y = (get_luminance(position, srt.scope_luma_coeffs) - 0.5f) * srt.image_height;
134 }
135 else if (srt.scope_mode == SEQ_DRAW_IMG_RGBPARADE) {
136 /* RGB parade: similar to waveform, except three different "bands"
137 * for each R/G/B intensity. */
138 int channel = texel.x % 3;
139 int column = texel.x / 3;
140
141 /* Use a bit desaturated color, and blend in a bit of original pixel color. */
142 float other_channels = 0.6f;
143 float factor = 0.4f;
144 if (channel == 0) {
145 pos.x = column - srt.image_width / 2;
146 pos.y = (position.r - 0.5f) * srt.image_height;
147 color.rgb = mix(color.rgb, float3(1, other_channels, other_channels), factor);
148 }
149 if (channel == 1) {
150 pos.x = column - srt.image_width / 2 + srt.image_width / 3;
151 pos.y = (position.g - 0.5f) * srt.image_height;
152 color.rgb = mix(color.rgb, float3(other_channels, 1, other_channels), factor);
153 }
154 if (channel == 2) {
155 pos.x = column - srt.image_width / 2 + srt.image_width * 2 / 3;
156 pos.y = (position.b - 0.5f) * srt.image_height;
157 color.rgb = mix(color.rgb, float3(other_channels, other_channels, 1), factor);
158 }
159 }
160 else if (srt.scope_mode == SEQ_DRAW_IMG_VECTORSCOPE) {
161 /* Vectorscope: pixel position is based on U,V of the color. */
162 float3 yuv = srt.scope_yuv_matrix * position;
163 float vec_size = min(srt.image_width, srt.image_height);
164 pos = yuv.yz * vec_size;
165 }
166
167 /* Determine final point color: we want to keep the hue, desaturate it a bit,
168 * and use full brightness. */
169 float4 hsv;
170 rgb_to_hsv(color, hsv);
171 if (srt.scope_mode != SEQ_DRAW_IMG_RGBPARADE) {
172 /* Saturation adjustments for parade mode are already done above. */
173 hsv.y *= 0.5f;
174 }
175 hsv.z = 1.0f;
176 hsv_to_rgb(hsv, color);
177
178 /* Calculate final point position in integer pixels. */
179 float4 clip_pos = srt.ModelViewProjectionMatrix * float4(pos * srt.inv_render_scale, 0.0f, 1.0f);
180 int2 view_pos = int2((clip_pos.xy * 0.5f + float2(0.5f)) *
181 float2(srt.view_width, srt.view_height));
182 if (any(lessThan(view_pos, int2(0))) ||
183 any(greaterThanEqual(view_pos, int2(srt.view_width, srt.view_height))))
184 {
185 /* Outside of view. */
186 return;
187 }
188
189 /* Optimization for highly contended raster regions (e.g. vectorscope center):
190 * if there's 50+ points rendered into this location already, stop adding more. */
191 int index = view_pos.y * srt.view_width + view_pos.x;
192 if (srt.raster_buf[index].col_a > 50 * 255) {
193 return;
194 }
195
196 /* Adjust point transparency based on ratio of wanted point size vs
197 * quantized to integer pixel count point size. */
198 float raster_size = srt.scope_point_size;
199 int px_size = max(int(ceil(raster_size)), 1);
200 float factor = max(raster_size / px_size * srt.inv_render_scale, 1.0f / 255.0f);
201 color.rgb *= factor;
202 color.a = factor;
203
204 if (px_size <= 1) {
205 /* Single pixel. */
206 srt.put_pixel(view_pos.x, view_pos.y, color);
207 }
208 else {
209 /* Multiple pixels. */
210 px_size = min(px_size, 16);
211 int x_min = view_pos.x - px_size / 2;
212 int x_max = x_min + px_size;
213 int y_min = view_pos.y - px_size / 2;
214 int y_max = y_min + px_size;
215 x_min = clamp(x_min, 0, srt.view_width);
216 x_max = clamp(x_max, 0, srt.view_width);
217 y_min = clamp(y_min, 0, srt.view_height);
218 y_max = clamp(y_max, 0, srt.view_height);
219 for (int y = y_min; y < y_max; y++) {
220 for (int x = x_min; x < x_max; x++) {
221 srt.put_pixel(x, y, color);
222 }
223 }
224 }
225}
226
231 /* Result of the raster stage. */
232 [[storage(0, read)]] const SeqScopeRasterData (&raster_buf)[];
233};
234
235[[vertex]] void resolve_vert([[vertex_id]] const int vert_id, [[position]] float4 &out_pos)
236{
237 fullscreen_vertex(vert_id, out_pos);
238}
239
240struct FragOut {
242};
243
245 [[out]] FragOut &frag_out,
246 [[frag_coord]] const float4 frag_co)
247{
248 /* Compute shader based scope point rasterizer produced screen-sized
249 * raster buffer, with accumulated R,G,B,A values in fixed point.
250 * Fetch data from that buffer and calculate resulting color. */
251 int2 view_pos = int2(frag_co.xy);
252 frag_out.color = float4(0.0f);
253 if (any(lessThan(view_pos, int2(0))) ||
254 any(greaterThanEqual(view_pos, int2(srt.view_width, srt.view_height))))
255 {
256 return;
257 }
258
259 int view_index = view_pos.y * srt.view_width + view_pos.x;
260 SeqScopeRasterData data = srt.raster_buf[view_index];
261 if (data.col_a != 0) {
262 float4 pix = float4(data.col_r, data.col_g, data.col_b, data.col_a) / 255.0f;
263 frag_out.color.rgb = pix.rgb / pix.a;
264
265 /* Use tonemap-like curve to map amount of points to transparency. */
266 frag_out.color.w = 1.0f - exp(-pix.a * srt.alpha_exponent);
267 }
268}
269
270} // namespace sequencer::scope
271
btScalar max
void color_alpha_unpremultiply(float4 color, float4 &result)
void rgb_to_hsv(float4 rgb, float4 &outcol)
float get_luminance(float3 color, float3 luminance_coefficients)
void hsv_to_rgb(float4 hsv, float4 &outcol)
#define storage(slot, qualifiers)
#define position
#define index(i)
#define frag_coord
#define push_constant
#define resource_table
#define sampler(slot)
#define global_invocation_id
#define fragment
#define vertex_id
#define compute
#define vertex
#define frag_color(slot)
#define local_size(...)
VecBase< bool, D > greaterThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
#define pow
#define exp
constexpr T sign(T) RET
#define abs
#define ceil
int atomicAdd(int &, int) RET
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
TEX_TEMPLATE DataVec texelFetch(T, IntCoord, int) RET
SamplerBase< float, 2 > sampler2D
void fullscreen_vertex(int vertex_id, float4 &out_position)
PipelineCompute gpu_shader_sequencer_scope_raster(sequencer::scope::raster_comp)
PipelineGraphic gpu_shader_sequencer_scope_resolve(sequencer::scope::resolve_vert, sequencer::scope::resolve_frag)
MatBase< float, 4, 4 > float4x4
void raster_comp(ScopeRaster &srt, const uint3 global_id)
void resolve_vert(const int vert_id, float4 &out_pos)
void resolve_frag(const ScopeResolve &srt, FragOut &frag_out, const float4 frag_co)
#define mix
#define min(a, b)
Definition sort.cc:38
float x
float y
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float w
Definition sky_math.h:225
float3 scope_to_extended_srgb(float3 color) const
void put_pixel(int x, int y, float4 col)