Blender V5.3
overlay_grid.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
12
13#include "DNA_camera_types.h"
14#include "DNA_screen_types.h"
15#include "DNA_space_types.h"
16
17#include "BLI_math_color_c.hh"
18#include "BLI_math_vector.hh"
19
20#include "ED_image.hh"
21#include "ED_view3d.hh"
22#include "GPU_texture.hh"
23
24#include "draw_shader_shared.hh"
25#include "overlay_base.hh"
26
27namespace blender::draw::overlay {
28
32class Grid : Overlay {
33 private:
34 /* Shader data */
35 PassSimple grid_ps_ = {"grid_ps_"};
37 StorageVectorBuffer<float4> tile_pos_buf_;
38
39 /* Config data */
40 int grid_flag_ = 0;
41 int axis_flag_ = 0;
42 uint num_iters_ = 0;
43
44 public:
45 void begin_sync(Resources &res, const State &state) final
46 {
47 enabled_ = init(state);
48 if (!enabled_) {
49 grid_ps_.init();
50 return;
51 }
52
54
55 grid_ps_.init();
56 grid_ps_.bind_ubo(OVERLAY_GLOBALS_SLOT, &res.globals_buf);
57 grid_ps_.bind_ubo(DRW_CLIPPING_UBO_SLOT, &res.clip_planes_buf);
58 grid_ps_.state_set(ps_draw_state);
59
60 /* Background quad draw in UV/Image editor. */
61 if (state.is_space_image()) {
62 float3 tile_scale(grid_ubo_.clip_rect.x, grid_ubo_.clip_rect.y, 0.0f);
63 const float4 color_back = math::interpolate(
64 res.theme.colors.background, res.theme.colors.grid, 0.5f);
65
66 auto &sub = grid_ps_.sub("grid_background");
67 sub.shader_set(res.shaders->grid_background.get());
68 sub.state_set(ps_draw_state | DRW_STATE_DEPTH_LESS_EQUAL);
69 sub.push_constant("ucolor", color_back);
70 sub.push_constant("tile_scale", tile_scale);
71 sub.draw(res.shapes.quad_solid.get());
72 }
73
74 /* Grid and axis line draws. */
75 {
76 const uint axis_vertex_count = 6;
77 const uint grid_vertex_count = 4 * OVERLAY_GRID_STEPS_DRAW * grid_ubo_.num_lines;
78 const auto grid_draw_state = ps_draw_state | DRW_STATE_DEPTH_LESS_EQUAL |
80
81 auto &sub = grid_ps_.sub("grid");
82 sub.shader_set(res.shaders->grid.get());
83 sub.state_set(grid_draw_state);
84 sub.bind_ubo("grid_buf", &grid_ubo_);
85
86 for (int grid_iter = 0; grid_iter < num_iters_; grid_iter++) {
87 /* NOTE(not_mark): Only the first iteration draws to depth as a workaround for
88 * clipping with the mesh edit overlay while it's drawn after (See #154540). */
89 if (grid_iter == 0) {
90 sub.state_set(grid_draw_state | DRW_STATE_WRITE_DEPTH);
91 }
92 else {
93 sub.state_set(grid_draw_state);
94 }
95
96 sub.push_constant("grid_iter", grid_iter);
97 if (axis_flag_) {
98 sub.push_constant("grid_flag", &axis_flag_);
99 sub.draw_procedural(GPUPrimType::GPU_PRIM_LINES, -1, axis_vertex_count, 0);
100 }
101 if (grid_flag_) {
102 sub.push_constant("grid_flag", &grid_flag_);
103 sub.draw_procedural(GPUPrimType::GPU_PRIM_LINES, -1, grid_vertex_count, 0);
104 }
105 }
106 }
107
108 /* Outline draw in UV/Image editors. */
109 if (state.is_space_image()) {
110 float4 theme_color;
111 ui::theme::get_color_shade_4fv(TH_BACK, 60, theme_color);
112 srgb_to_linearrgb_v4(theme_color, theme_color);
113
114 auto &sub = grid_ps_.sub("wire_border");
115 sub.shader_set(res.shaders->grid_image.get());
116 sub.push_constant("ucolor", theme_color);
117 sub.bind_ssbo("tile_pos_buf", &tile_pos_buf_);
118 sub.draw(res.shapes.quad_wire.get(), tile_pos_buf_.size());
119 }
120 }
121
122 void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
123 {
124 if (!enabled_) {
125 return;
126 }
127
128 grid_ubo_.push_update();
129 GPU_framebuffer_bind(framebuffer);
130 manager.submit(grid_ps_, view);
131 }
132
133 private:
134 bool init(const State &state)
135 {
136 /* Set config flags to default values. */
137 grid_flag_ = axis_flag_ = 0;
138
139 if (state.hide_overlays) {
140 return false;
141 }
142
143 if (state.is_space_v3d()) {
144 return init_v3d(state);
145 }
146 if (state.is_space_image()) {
147 return init_space_image(state);
148 }
149 /* Grid is currently unsupported in SPACE_NODE and such. */
150 return false;
151 }
152
153 bool init_space_image(const State &state)
154 {
155 const View2D *v2d = &state.region->v2d;
156 SpaceImage *sima = (SpaceImage *)state.space_data;
157
158 /* Grid is currently visible in UV and Image editors, if enabled. */
159 const bool show_grid = bool(sima->overlay.flag & SI_OVERLAY_SHOW_GRID_BACKGROUND);
160 if (!show_grid) {
161 return false;
162 }
163
164 /* Configure grid flags s.t. GRID_OVER_IMAGE is taken into account. */
165 grid_flag_ = SHOW_GRID | GRID_SIMA;
166 if (sima->flag & SI_GRID_OVER_IMAGE) {
167 grid_flag_ |= GRID_OVER_IMAGE;
168 }
169
170 /* Query grid step/level scaling; these can differ per axis. */
171 std::array<float, SI_GRID_STEPS_LEN> steps_x, steps_y;
172 ED_space_image_grid_steps(sima, steps_x.data(), steps_y.data(), SI_GRID_STEPS_LEN);
173 for (int i : IndexRange(SI_GRID_STEPS_LEN)) {
174 /* NOTE(not_mark): grid steps data appears to be flipped on X/Y. */
175 grid_ubo_.steps[i].x = steps_x[i];
176 grid_ubo_.steps[i].y = grid_ubo_.steps[i].z = steps_y[i];
177 }
178
179 /* Clip rectangle can be specified in UV editor view. */
180 grid_ubo_.clip_rect.x = float(sima->tile_grid_shape[0]);
181 grid_ubo_.clip_rect.y = float(sima->tile_grid_shape[1]);
182 /* Outline draws lines around tile grid */
183 tile_pos_buf_.clear();
184 for (const int x : IndexRange(sima->tile_grid_shape[0])) {
185 for (const int y : IndexRange(sima->tile_grid_shape[1])) {
186 tile_pos_buf_.append(float4(x, y, 0.0f, 0.0f));
187 }
188 }
189 tile_pos_buf_.push_update();
190
191 /* Align the grid to the camera center. */
192 grid_ubo_.offset.x = 0.5f * (v2d->cur.xmax + v2d->cur.xmin);
193 grid_ubo_.offset.y = 0.5f * (v2d->cur.ymax + v2d->cur.ymin);
194 /* Restrict center to the clip rect. */
195 grid_ubo_.offset = math::clamp(grid_ubo_.offset, float2(0.0f), grid_ubo_.clip_rect);
196
197 /* Query grid image zoom level. Then find the lowest relevant grid level + fractional. */
198 float dist = ED_space_image_zoom_level(v2d, SI_GRID_STEPS_LEN) * 4.0f;
199 for (int i : IndexRange(SI_GRID_STEPS_LEN + 1)) {
200 float prev = (i > 0) ? std::min(grid_ubo_.steps[i - 1].x, grid_ubo_.steps[i - 1].y) : 0.0f;
201 float curr = (i < OVERLAY_GRID_STEPS_LEN) ?
202 std::min(grid_ubo_.steps[i].x, grid_ubo_.steps[i].y) :
203 std::numeric_limits<float>::infinity();
204 if (curr >= dist || i == OVERLAY_GRID_STEPS_LEN) {
205 grid_ubo_.level = static_cast<float>(i) + safe_divide(dist - prev, curr - prev);
206 break;
207 }
208 }
209
210 /* Determine the practical size of the viewport in line steps. */
211 const uint lowest_level = std::min(uint(grid_ubo_.level), uint(OVERLAY_GRID_STEPS_LEN - 1));
212 const float2 lowest_step = grid_ubo_.steps[lowest_level].xy();
213 float2 viewport_size = float2((v2d->cur.xmax - v2d->cur.xmin),
214 (v2d->cur.ymax - v2d->cur.ymin));
215 viewport_size = math::min(viewport_size, grid_ubo_.clip_rect);
216
217 /* We limit to 8192 lines per axis. This hits the current maximum framebuffer size on
218 * the VK roadmap. In addition, lines are faded when they are 2-4 pixels apart. */
219 const float viewport_extent = math::reduce_max(viewport_size);
220 uint max_lines_x = 2u * uint(ceilf(viewport_extent / lowest_step.x)) + 1u;
221 uint max_lines_y = 2u * uint(ceilf(viewport_extent / lowest_step.y)) + 1u;
222 grid_ubo_.num_lines = std::min(std::max(max_lines_x, max_lines_y), 8192u);
223 num_iters_ = 1u;
224
225 return true;
226 }
227
228 bool init_v3d(const State &state)
229 {
230 /* Query different options from overlay state */
231 const bool show_axis_x = (state.v3d_gridflag & V3D_SHOW_X) != 0;
232 const bool show_axis_y = (state.v3d_gridflag & V3D_SHOW_Y) != 0;
233 const bool show_axis_z = (state.v3d_gridflag & V3D_SHOW_Z) != 0;
234 const bool show_persp = (state.v3d_gridflag & V3D_SHOW_FLOOR) != 0;
235 const bool show_ortho = (state.v3d_gridflag & V3D_SHOW_ORTHO_GRID) != 0;
236
237 if (!(show_axis_x || show_axis_y || show_axis_z || show_persp || show_ortho)) {
238 return false;
239 }
240
241 const View3D *v3d = state.v3d;
242 const RegionView3D *rv3d = state.rv3d;
243
244 /* Set `grid_flag_` dependent on view configuration. */
245 if (rv3d->is_persp || rv3d->view == RV3D_VIEW_USER) {
246 /* Perspective/orthographic; set selected axes and plane (floor = XY) bits. */
247 axis_flag_ |= (show_axis_x ? AXIS_X : OVERLAY_GridBits(0));
248 axis_flag_ |= (show_axis_y ? AXIS_Y : OVERLAY_GridBits(0));
249 axis_flag_ |= (show_axis_z ? AXIS_Z : OVERLAY_GridBits(0));
250 grid_flag_ |= (show_persp ? PLANE_XY : OVERLAY_GridBits(0));
251
252 /* If any options were set, set SHOW_AXES/SHOW_GRID. */
253 axis_flag_ |= (axis_flag_ ? SHOW_AXES : OVERLAY_GridBits(0));
254 grid_flag_ |= (grid_flag_ ? SHOW_GRID : OVERLAY_GridBits(0));
255
256 /* Axes are passed to the grid flag for correct occlusion. */
257 if (grid_flag_) {
258 grid_flag_ |= (show_axis_x ? AXIS_X : OVERLAY_GridBits(0));
259 grid_flag_ |= (show_axis_y ? AXIS_Y : OVERLAY_GridBits(0));
260 grid_flag_ |= (show_axis_z ? AXIS_Z : OVERLAY_GridBits(0));
261 }
262 }
263 else {
264 /* Fixed plane orthographic: set axis/plane bits dependent on the view
265 * (top, right, left, etc.) that is selected. */
266 if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) {
267 axis_flag_ = (show_axis_y ? (AXIS_Y | GRID_ALIGNED) : OVERLAY_GridBits(0)) |
268 (show_axis_z ? (AXIS_Z | GRID_ALIGNED) : OVERLAY_GridBits(0));
269 grid_flag_ = (show_ortho ? (PLANE_YZ | GRID_ALIGNED) : OVERLAY_GridBits(0));
270 }
271 else if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
272 axis_flag_ = (show_axis_x ? (AXIS_X | GRID_ALIGNED) : OVERLAY_GridBits(0)) |
273 (show_axis_y ? (AXIS_Y | GRID_ALIGNED) : OVERLAY_GridBits(0));
274 grid_flag_ = (show_ortho ? (PLANE_XY | GRID_ALIGNED) : OVERLAY_GridBits(0));
275 }
276 else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
277 axis_flag_ = (show_axis_x ? (AXIS_X | GRID_ALIGNED) : OVERLAY_GridBits(0)) |
278 (show_axis_z ? (AXIS_Z | GRID_ALIGNED) : OVERLAY_GridBits(0));
279 grid_flag_ = (show_ortho ? (PLANE_XZ | GRID_ALIGNED) : OVERLAY_GridBits(0));
280 }
281
282 /* If any axes are set, set SHOW_AXES. If `grid` is toggled, set SHOW_GRID.
283 * We also set `GRID_BEHIND_GEOMETRY` for fixed plane views, to place it on
284 * the far plane. */
285 axis_flag_ |= (axis_flag_ ? (SHOW_AXES | GRID_BEHIND_GEOMETRY) : OVERLAY_GridBits(0));
286 grid_flag_ |= (grid_flag_ ? (SHOW_GRID | GRID_BEHIND_GEOMETRY) : OVERLAY_GridBits(0));
287
288 /* Axes are passed to the grid flag for correct occlusion. */
289 if (grid_flag_) {
290 grid_flag_ |= (show_axis_x ? AXIS_X : OVERLAY_GridBits(0));
291 grid_flag_ |= (show_axis_y ? AXIS_Y : OVERLAY_GridBits(0));
292 grid_flag_ |= (show_axis_z ? AXIS_Z : OVERLAY_GridBits(0));
293 }
294 }
295
296 /* Disable grid rendering when no axis or grid is enabled. */
297 if (grid_flag_ == 0 && axis_flag_ == 0) {
298 return false;
299 }
300
301 /* Query grid scales from unit/scaling; this range suffices for user-visible levels. */
302 Array<float, SI_GRID_STEPS_LEN> steps(SI_GRID_STEPS_LEN);
303 ED_view3d_grid_steps(state.scene, v3d, rv3d, steps.data());
304 for (int i : IndexRange(SI_GRID_STEPS_LEN)) {
305 /* If the current level is not specified, or the same size as the previous level,
306 * we apply a 10x scale to the previous level and use that. */
307 if (i > 0 && (steps[i] == 0.0f || steps[i] == steps[i - 1])) {
308 grid_ubo_.steps[i] = grid_ubo_.steps[i - 1] * 10.0f;
309 }
310 else {
311 grid_ubo_.steps[i] = float4(steps[i]);
312 }
313 }
314
315 /* Camera parameters. */
316 float3 drw_view_position = rv3d->viewinv[3], drw_view_forward = rv3d->viewinv[2];
317
318 /* Compute distance to a relevant floor point-of-interest from the camera. The grid translates
319 * with this point and is only drawn around it. */
320 float dist;
321 if (rv3d->is_persp) {
322 /* Scale depends on distance to a point on the floor plane; we interpolate between the
323 * point viewed by the camera and the point directly below it, dependent on azimuth. */
324 dist = interpolate(abs(drw_view_position.z / drw_view_forward.z),
325 abs(drw_view_position.z),
326 1.0f - abs(drw_view_forward.z));
327 }
328 else if (bool(grid_flag_ & GRID_ALIGNED) || bool(axis_flag_ & GRID_ALIGNED)) {
329 /* #155497: use the same zoom-derived distance as `ED_view3d_grid_view_scale()` for
330 * axis-aligned orthographic views. */
331 dist = 10.0f * 12.0f / (state.region->sizex * rv3d->winmat[0][0]);
332 /* Keep the selected level from going below the smallest available grid step. */
333 const float min_step = std::min(grid_ubo_.steps[0].x, grid_ubo_.steps[0].y);
334 dist = std::max(dist, min_step);
335 }
336 else {
337 /* Scale is simply specified by orthographic view. */
338 dist = rv3d->dist;
339 }
340
341 /* Extract 2D grid offset for moving grid "with the camera" on the floor plane. */
342 if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) {
343 grid_ubo_.offset = drw_view_position.yz();
344 }
345 else if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
346 grid_ubo_.offset = drw_view_position.xy();
347 }
348 else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
349 grid_ubo_.offset = float2(drw_view_position.x, drw_view_position.z);
350 }
351 else if (rv3d->is_persp) {
352 float3 camera_offs = drw_view_position - dist * drw_view_forward;
353 grid_ubo_.offset = camera_offs.xy();
354 }
355 else { /* Orthographic. */
356 const float forward_z = drw_view_forward.z;
357 constexpr float eps = 1e-6f;
358 if (abs(forward_z) > eps) {
359 /* Center the grid on the intersection of the orthographic view ray with the XY plane. */
360 float3 camera_offs = drw_view_position -
361 drw_view_forward * safe_divide(drw_view_position.z, forward_z);
362 grid_ubo_.offset = camera_offs.xy();
363 }
364 else {
365 grid_ubo_.offset = drw_view_position.xy();
366 }
367 }
368
369 /* Find the lowest relevant grid level for the above distance. */
370 for (int i : IndexRange(SI_GRID_STEPS_LEN)) {
371 float curr = std::min(grid_ubo_.steps[i].x, grid_ubo_.steps[i].y);
372 float next = (i < SI_GRID_STEPS_LEN - 1) ?
373 std::min(grid_ubo_.steps[i + 1].x, grid_ubo_.steps[i + 1].y) :
374 curr * 10.0f;
375 if (next >= dist || i == OVERLAY_GRID_STEPS_LEN - 1) {
376 grid_ubo_.level = static_cast<float>(i) + safe_divide(dist - curr, next - curr);
377 break;
378 }
379 }
380
381 /* Set clipping rectangle for lines, dependent on camera/viewport. */
382 /* TODO(not_mark): use for finite grid clipping. */
383 if (rv3d->persp == RV3D_CAMOB && v3d->camera && v3d->camera->type == OB_CAMERA) {
384 Object *camera_object = DEG_get_evaluated(state.depsgraph, v3d->camera);
385 /* Only set the GRID_CAMERA flag when the grid/axis is being drawn. Otherwise this could lead
386 * to an out of bound access in the shader. */
387 if (grid_flag_) {
388 grid_flag_ |= GRID_CAMERA;
389 }
390 if (axis_flag_) {
391 axis_flag_ |= GRID_CAMERA;
392 }
393
394 float clip_dist = ((Camera *)(camera_object->data))->clip_end;
395 grid_ubo_.clip_rect = float2(clip_dist);
396 }
397 else {
398 bool use_clip_end = rv3d->is_persp ||
399 ((v3d->flag & (V3D_XR_SESSION_SURFACE | V3D_XR_SESSION_MIRROR)) != 0);
400 grid_ubo_.clip_rect = use_clip_end ? float2(v3d->clip_end) :
401 float2(std::max(4.0f / rv3d->winmat[0][0],
402 4.0f / rv3d->winmat[1][1]));
403 }
404
405 /* This suffices for most cases, and in others we fade to hide it. */
406 if (rv3d->is_persp) {
407 /* In perspective views, we draw a fixed number of lines, but draw them N times
408 * at different Z to mitigate flickering. */
409 grid_ubo_.num_lines = 151u;
410 num_iters_ = OVERLAY_GRID_ITER_LEN;
411 }
412 else if ((grid_flag_ & GRID_ALIGNED) != 0) {
413 /* Determine the practical size of the viewport in line steps. */
414 const uint lowest_level = std::min(uint(grid_ubo_.level), uint(OVERLAY_GRID_STEPS_LEN - 1));
415 const float2 lowest_step = grid_ubo_.steps[lowest_level].xy();
416 float2 viewport_size = float2(1.0f / rv3d->winmat[0][0], 1.0f / rv3d->winmat[1][1]);
417
418 /* We limit to 8192 lines per axis. This hits the current maximum framebuffer size on
419 * the VK roadmap. In addition, lines are faded when they are 2-4 pixels apart. */
420 uint max_lines_x = 2u * uint(ceilf(viewport_size.x / lowest_step.x)) + 1u;
421 uint max_lines_y = 2u * uint(ceilf(viewport_size.y / lowest_step.y)) + 1u;
422 grid_ubo_.num_lines = std::min(std::max(max_lines_x, max_lines_y), 8192u);
423 num_iters_ = 1u;
424 }
425 else {
426 /* In other orthographic views, we draw a fixed number of lines. */
427 grid_ubo_.num_lines = 301u;
428 num_iters_ = 1;
429 }
430
431 return true;
432 }
433};
434} // namespace blender::draw::overlay
#define SI_GRID_STEPS_LEN
#define ELEM(...)
btScalar max
constexpr IndexRange()=default
void draw_line(Framebuffer &framebuffer, Manager &manager, View &view) final
void begin_sync(Resources &res, const State &state) final
#define DRW_CLIPPING_UBO_SLOT
#define OVERLAY_GLOBALS_SLOT
#define abs
Quaternion interpolate(Quaternion a, Quaternion b, float t)
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
T clamp(const T &a, const T &min, const T &max)
T reduce_max(const VecBase< T, Size > &a)
T min(const T &a, const T &b)
T interpolate(const T &a, const T &b, const FactorT &t)
SymEdge< T > * prev(const SymEdge< T > *se)
void get_color_shade_4fv(int colorid, int offset, float col[4])
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
unsigned int uint
Definition BKE_text.h:55
void ED_view3d_grid_steps(const Scene *scene, const View3D *v3d, const RegionView3D *rv3d, float r_grid_steps[8])
@ V3D_XR_SESSION_SURFACE
@ V3D_XR_SESSION_MIRROR
MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4])
@ SI_OVERLAY_SHOW_GRID_BACKGROUND
float ED_space_image_zoom_level(const View2D *v2d, int grid_dimension)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_BLEND_ALPHA
Definition draw_state.hh:57
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_ADD
Definition draw_state.hh:53
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
MINLINE float safe_divide(float a, float b)
void ED_space_image_grid_steps(SpaceImage *sima, float grid_steps_x[SI_GRID_STEPS_LEN], float grid_steps_y[SI_GRID_STEPS_LEN], int grid_dimension)
@ GRID_BEHIND_GEOMETRY
@ GRID_OVER_IMAGE
#define OVERLAY_GRID_STEPS_LEN
#define OVERLAY_GRID_ITER_LEN
#define OVERLAY_GRID_STEPS_DRAW
const btScalar eps
Definition poly34.cpp:11
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
float z
Definition sky_math.h:136
float x
Definition sky_math.h:136