Blender V5.3
gpencil_engine_c.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#include "DRW_engine.hh"
9#include "DRW_render.hh"
10
11#include "BKE_compositor.hh"
12#include "BKE_context.hh"
13#include "BKE_curves.hh"
14#include "BKE_grease_pencil.hh"
15#include "BKE_material.hh"
16#include "BKE_object.hh"
17#include "BKE_paint.hh"
18#include "BKE_shader_fx.hh"
19
20#include "BKE_camera.h"
21
22#include "BLI_listbase.hh"
23#include "BLI_memblock.hh"
24#include "BLI_virtual_array.hh"
25
26#include "BLT_translation.hh"
27
28#include "DNA_camera_types.h"
29#include "DNA_material_types.h"
30#include "DNA_screen_types.h"
31#include "DNA_view3d_types.h"
32#include "DNA_world_types.h"
33
34#include "GPU_texture.hh"
35#include "GPU_uniform_buffer.hh"
36
37#include "draw_cache.hh"
38#include "draw_manager.hh"
39#include "draw_view.hh"
40
41#include "gpencil_engine.hh"
43
45
46#include "ED_grease_pencil.hh"
47#include "ED_screen.hh"
48#include "ED_view3d.hh"
49
50#include "GPU_debug.hh"
51
52namespace blender::draw::gpencil {
53
55{
56 this->draw_ctx = DRW_context_get();
57
58 const View3D *v3d = draw_ctx->v3d;
59
60 if (!dummy_texture.is_valid()) {
61 const float pixels[1][4] = {{1.0f, 0.0f, 1.0f, 1.0f}};
63 gpu::TextureFormat::UNORM_8_8_8_8, int2(1), GPU_TEXTURE_USAGE_SHADER_READ, &pixels[0][0]);
64 }
65 if (!dummy_depth.is_valid()) {
66 const float pixels[1] = {1.0f};
68 gpu::TextureFormat::SFLOAT_32_DEPTH, int2(1), GPU_TEXTURE_USAGE_SHADER_READ, &pixels[0]);
69 }
70
71 /* Resize and reset memory-blocks. */
72 BLI_memblock_clear(this->gp_light_pool, light_pool_free);
73 BLI_memblock_clear(this->gp_material_pool, material_pool_free);
74 BLI_memblock_clear(this->gp_object_pool, nullptr);
75 this->gp_layer_pool->clear();
76 this->gp_vfx_pool->clear();
77 BLI_memblock_clear(this->gp_maskbit_pool, nullptr);
78
80 this->scene = draw_ctx->scene;
81 this->v3d = draw_ctx->v3d;
82 this->last_light_pool = nullptr;
83 this->last_material_pool = nullptr;
84 this->tobjects.first = nullptr;
85 this->tobjects.last = nullptr;
86 this->tobjects_infront.first = nullptr;
87 this->tobjects_infront.last = nullptr;
88 this->sbuffer_tobjects.first = nullptr;
89 this->sbuffer_tobjects.last = nullptr;
90 this->dummy_tx = this->dummy_texture;
91 this->draw_wireframe = (v3d && v3d->shading.type == OB_WIRE);
92 this->scene_depth_tx = nullptr;
93 this->scene_fb = nullptr;
94 this->is_render = this->render_depth_tx.is_valid() || (v3d && v3d->shading.type == OB_RENDER);
95 this->is_viewport = (v3d != nullptr);
98 /* Small HACK: we don't want the global pool to be reused,
99 * so we set the last light pool to nullptr. */
100 this->last_light_pool = nullptr;
101 this->is_sorted = false;
102
103 bool use_scene_lights = false;
104 bool use_scene_world = false;
105
106 if (v3d) {
107 use_scene_lights = V3D_USES_SCENE_LIGHTS(v3d);
108
109 use_scene_world = V3D_USES_SCENE_WORLD(v3d);
110
112 /* Special case: If we're in Vertex Paint mode, enforce #V3D_SHADING_VERTEX_COLOR setting. */
113 if (v3d->shading.type == OB_SOLID && draw_ctx->obact &&
115 {
117 }
118
120
121 /* For non active frame, use only lines in multiedit mode. */
122 const bool overlays_on = (v3d->flag2 & V3D_HIDE_OVERLAYS) == 0;
123 this->use_multiedit_lines_only = overlays_on &&
125
126 const bool shmode_xray_support = v3d->shading.type <= OB_SOLID;
127 this->xray_alpha = (shmode_xray_support && XRAY_ENABLED(v3d)) ? XRAY_ALPHA(v3d) : 1.0f;
129 }
130 else if (this->is_render) {
131 use_scene_lights = true;
132 use_scene_world = true;
133 this->use_multiedit_lines_only = false;
134 this->xray_alpha = 1.0f;
135 this->v3d_color_type = -1;
136 this->force_stroke_order_3d = false;
137 }
138
139 this->use_lighting = (v3d && v3d->shading.type > OB_SOLID) || this->is_render;
140 this->use_lights = use_scene_lights;
141
142 gpencil_light_ambient_add(this->shadeless_light_pool, float3{1.0f, 1.0f, 1.0f});
143
144 World *world = draw_ctx->scene->world;
145 if (world != nullptr && use_scene_world) {
146 gpencil_light_ambient_add(this->global_light_pool, &world->horr);
147 }
148 else if (v3d) {
149 float world_light[3];
152 }
153
154 float4x4 viewmatinv = View::default_get().viewinv();
155 copy_v3_v3(this->camera_z_axis, viewmatinv[2]);
156 copy_v3_v3(this->camera_pos, viewmatinv[3]);
157 this->camera_z_offset = dot_v3v3(viewmatinv[3], viewmatinv[2]);
158
159 if (draw_ctx && draw_ctx->rv3d && v3d) {
160 this->camera = (draw_ctx->rv3d->persp == RV3D_CAMOB) ? v3d->camera : nullptr;
161 }
162 else {
163 this->camera = nullptr;
164 }
165}
166
168{
169 this->cfra = int(DEG_get_ctime(draw_ctx->depsgraph));
171 this->use_layer_fb = false;
172 this->use_object_fb = false;
173 this->use_mask_fb = false;
174
175 const bool use_viewport_compositor = draw_ctx->is_viewport_compositor_used();
178 this->need_combined_pass = use_viewport_compositor &&
179 needed_passes.contains(RE_PASSNAME_COMBINED);
180 this->need_grease_pencil_pass = use_viewport_compositor &&
181 needed_passes.contains(RE_PASSNAME_GREASE_PENCIL);
182 this->use_signed_fb = !this->is_viewport;
183
184 if (draw_ctx->v3d) {
185 const bool hide_overlay = ((draw_ctx->v3d->flag2 & V3D_HIDE_OVERLAYS) != 0);
186 const bool show_onion = ((draw_ctx->v3d->gp_flag & V3D_GP_SHOW_ONION_SKIN) != 0);
187 const bool playing = (draw_ctx->evil_C != nullptr) ?
189 nullptr :
190 false;
191 this->do_onion = show_onion && !hide_overlay && !playing;
194 this->playing = playing;
195 /* Save simplify flags (can change while drawing, so it's better to save). */
200
201 /* Fade Layer. */
202 const bool is_fade_layer = ((!hide_overlay) && (!this->is_render) &&
204 this->fade_layer_opacity = (is_fade_layer) ? draw_ctx->v3d->overlay.gpencil_fade_layer : -1.0f;
206 /* Fade GPencil Objects. */
207 const bool is_fade_object = ((!hide_overlay) && (!this->is_render) &&
210 this->fade_gp_object_opacity = (is_fade_object) ?
212 -1.0f;
213 this->fade_3d_object_opacity = ((!hide_overlay) && (!this->is_render) &&
216 -1.0f;
217 }
218 else {
219 this->do_onion = true;
222 this->simplify_fx = GPENCIL_SIMPLIFY_FX(scene, false);
223 this->fade_layer_opacity = -1.0f;
224 this->playing = false;
225 }
226
227 {
228 this->stroke_batch = nullptr;
229 this->fill_batch = nullptr;
230
231 this->obact = draw_ctx->obact;
232 }
233
234 /* Free unneeded buffers. */
235 this->snapshot_depth_tx.free();
236 this->snapshot_color_tx.free();
237 this->snapshot_reveal_tx.free();
238
239 {
240 PassSimple &pass = this->merge_depth_ps;
241 pass.init();
243 pass.shader_set(ShaderCache::get().depth_merge.get());
244 pass.bind_texture("depth_buf", &this->depth_tx);
245 pass.push_constant("stroke_order3d", &this->is_stroke_order_3d);
246 pass.push_constant("gp_model_matrix", &this->object_bound_mat);
247 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
248 }
249 {
250 /* Merges the object's depth to the viewport compositor depth pass. */
251 PassSimple &pass = this->merge_depth_pass_ps;
252 pass.init();
254 pass.shader_set(ShaderCache::get().depth_pass_merge.get());
255 pass.bind_texture("depth_buf", &this->depth_tx);
256 pass.bind_image("depth_pass_img", &this->depth_pass_img);
257 pass.push_constant("stroke_order3d", &this->is_stroke_order_3d);
258 pass.push_constant("gp_model_matrix", &this->object_bound_mat);
259 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
260 }
261 {
262 PassSimple &pass = this->mask_invert_ps;
263 pass.init();
266 pass.draw_procedural(GPU_PRIM_TRIS, 1, 3);
267 }
268
269 Camera *cam = id_cast<Camera *>(
270 (this->camera != nullptr && this->camera->type == OB_CAMERA) ? this->camera->data : nullptr);
271
272 /* Pseudo DOF setup. */
273 if (cam && (cam->dof.flag & CAM_DOF_ENABLED)) {
274 const float2 vp_size = draw_ctx->viewport_size_get();
275 float fstop = cam->dof.aperture_fstop;
276 float sensor = BKE_camera_sensor_size(cam->sensor_fit, cam->sensor_x, cam->sensor_y);
277 float focus_dist = BKE_camera_object_dof_distance(this->camera);
278 float focal_len = cam->lens;
279
280 const float scale_camera = 0.001f;
281 /* We want radius here for the aperture number. */
282 float aperture = 0.5f * scale_camera * focal_len / fstop;
283 float focal_len_scaled = scale_camera * focal_len;
284 float sensor_scaled = scale_camera * sensor;
285
286 if (draw_ctx->rv3d != nullptr) {
287 sensor_scaled *= draw_ctx->rv3d->viewcamtexcofac[0];
288 }
289
290 this->dof_params[1] = aperture * fabsf(focal_len_scaled / (focus_dist - focal_len_scaled));
291 this->dof_params[1] *= vp_size[0] / sensor_scaled;
292 this->dof_params[0] = -focus_dist * this->dof_params[1];
293 }
294 else {
295 /* Disable DoF blur scaling. */
296 this->camera = nullptr;
297 }
298}
299
300#define DISABLE_BATCHING 0
301
302bool Instance::is_used_as_layer_mask_in_viewlayer(const GreasePencil &grease_pencil,
303 const bke::greasepencil::Layer &mask_layer,
304 const ViewLayer &view_layer)
305{
306 using namespace bke::greasepencil;
307 for (const Layer *layer : grease_pencil.layers()) {
308 if (layer->view_layer_name().is_empty() ||
309 !STREQ(view_layer.name, layer->view_layer_name().c_str()))
310 {
311 continue;
312 }
313
315 continue;
316 }
317
318 for (GreasePencilLayerMask &mask : layer->masks) {
319 if (STREQ(mask.layer_name, mask_layer.name().c_str())) {
320 return true;
321 }
322 }
323 }
324 return false;
325}
326
327bool Instance::use_layer_in_render(const GreasePencil &grease_pencil,
328 const bke::greasepencil::Layer &layer,
329 const ViewLayer &view_layer,
330 bool &r_is_used_as_mask)
331{
332 if (!layer.view_layer_name().is_empty() &&
333 !STREQ(view_layer.name, layer.view_layer_name().c_str()))
334 {
335 /* Do not skip layers that are masks when rendering the viewlayer so that it can still be used
336 * to clip/mask other layers. */
337 if (is_used_as_layer_mask_in_viewlayer(grease_pencil, layer, view_layer)) {
338 r_is_used_as_mask = true;
339 }
340 else {
341 return false;
342 }
343 }
344 return true;
345}
346
347tObject *Instance::object_sync_do(Object *ob, ResourceHandleRange res_handle)
348{
349 using namespace ed::greasepencil;
350 using namespace bke::greasepencil;
352 const bool is_vertex_mode = (ob->mode & OB_MODE_VERTEX_PAINT) != 0;
353 const Bounds<float3> bounds = grease_pencil.bounds_min_max_eval().value_or(Bounds(float3(0)));
354
355 const bool do_onion = !this->is_render && this->do_onion &&
356 (this->do_onion_only_active_object ? this->obact == ob : true);
357 const bool do_multi_frame = (((this->scene->toolsettings->gpencil_flags &
359 (ob->mode != OB_MODE_OBJECT));
360 const bool use_stroke_order_3d = this->force_stroke_order_3d ||
361 ((grease_pencil.flag & GREASE_PENCIL_STROKE_ORDER_3D) != 0);
362 tObject *tgp_ob = gpencil_object_cache_add(this, ob, use_stroke_order_3d, bounds);
363
364 int mat_ofs = 0;
365 MaterialPool *matpool = gpencil_material_pool_create(this, ob, &mat_ofs, is_vertex_mode);
366
367 gpu::Texture *tex_fill = this->dummy_tx;
368 gpu::Texture *tex_stroke = this->dummy_tx;
369
370 gpu::Batch *iter_geom = nullptr;
371 PassSimple *last_pass = nullptr;
372 int vfirst = 0;
373 int vcount = 0;
374
375 const auto drawcall_flush = [&](PassSimple &pass) {
376#if !DISABLE_BATCHING
377 if (iter_geom != nullptr) {
378 pass.draw(iter_geom, 1, vcount, vfirst, res_handle);
379 }
380#endif
381 iter_geom = nullptr;
382 vfirst = -1;
383 vcount = 0;
384 };
385
386 const auto drawcall_add =
387 [&](PassSimple &pass, gpu::Batch *draw_geom, const int v_first, const int v_count) {
388#if DISABLE_BATCHING
389 pass.draw(iter_geom, 1, vcount, vfirst, res_handle);
390 return;
391#endif
392 int last = vfirst + vcount;
393 /* Interrupt draw-call grouping if the sequence is not consecutive. */
394 if ((draw_geom != iter_geom) || (v_first - last > 0)) {
395 drawcall_flush(pass);
396 }
397 iter_geom = draw_geom;
398 if (vfirst == -1) {
399 vfirst = v_first;
400 }
401 vcount = v_first + v_count - vfirst;
402 };
403
404 int t_offset = 0;
405 /* Note that we loop over all the drawings (including the onion skinned ones) to make sure we
406 * match the offsets of the batch cache. */
407 const Vector<DrawingInfo> drawings = retrieve_visible_drawings(
408 *this->scene, grease_pencil, true);
409 const Span<const Layer *> layers = grease_pencil.layers();
410 for (const DrawingInfo info : drawings) {
411 const Layer &layer = *layers[info.layer_index];
412
413 const std::optional<GroupedSpan<int3>> triangles = info.drawing.triangles();
414 const bke::CurvesGeometry &curves = info.drawing.strokes();
415 const OffsetIndices<int> points_by_curve = curves.evaluated_points_by_curve();
416 const bke::AttributeAccessor attributes = curves.attributes();
417 const VArray<bool> cyclic = *attributes.lookup_or_default<bool>(
418 "cyclic", bke::AttrDomain::Curve, false);
419
420 IndexMaskMemory memory;
421 const IndexMask visible_strokes = ed::greasepencil::retrieve_visible_strokes(
422 *ob, info.drawing, memory);
423 const IndexMask visible_fills = ed::greasepencil::retrieve_visible_fills(
424 *ob, info.drawing, memory);
425 const std::optional<GroupedSpan<int>> fills = info.drawing.fills();
426 const int num_fills = fills.has_value() ? fills->size() : 0;
427
428 /* Precompute all the triangle and vertex counts.
429 * In case the drawing should not be rendered, we need to compute the offset where the next
430 * drawing begins. */
431 Array<int> num_triangles_per_fill(num_fills);
432 Array<int> num_vertices_per_curve(curves.curves_num());
433 int total_num_triangles = 0;
434 int total_num_vertices = 0;
435 if (triangles) {
436 visible_fills.foreach_index([&](const int fill_index) {
437 const int num_stroke_triangles = (*triangles)[fill_index].size();
438 num_triangles_per_fill[fill_index] = num_stroke_triangles;
439 total_num_triangles += num_stroke_triangles;
440 });
441 }
442
443 visible_strokes.foreach_index([&](const int curve_i) {
444 const IndexRange points = points_by_curve[curve_i];
445 const int num_stroke_vertices = (points.size() +
446 int(cyclic[curve_i] && (points.size() >= 3)));
447 num_vertices_per_curve[curve_i] = num_stroke_vertices;
448 total_num_vertices += num_stroke_vertices;
449 });
450
451 bool is_layer_used_as_mask = false;
452 const bool show_drawing_in_render = use_layer_in_render(
453 grease_pencil, layer, *this->view_layer, is_layer_used_as_mask);
454 if (!show_drawing_in_render) {
455 /* Skip over the entire drawing. */
456 t_offset += total_num_triangles;
457 t_offset += total_num_vertices * 2;
458 continue;
459 }
460
461 if (last_pass) {
462 drawcall_flush(*last_pass);
463 }
464
465 tLayer *tgp_layer = grease_pencil_layer_cache_add(
466 this, ob, layer, info.onion_id, is_layer_used_as_mask, tgp_ob);
467 PassSimple &pass = *tgp_layer->geom_ps;
468 last_pass = &pass;
469
470 const bool use_lights = this->use_lighting &&
471 ((layer.base.flag & GP_LAYER_TREE_NODE_USE_LIGHTS) != 0) &&
472 (ob->dtx & OB_USE_GPENCIL_LIGHTS);
473
474 gpu::UniformBuf *lights_ubo = (use_lights) ? this->global_light_pool->ubo :
475 this->shadeless_light_pool->ubo;
476
477 gpu::UniformBuf *ubo_mat;
478 gpencil_material_resources_get(matpool, 0, nullptr, nullptr, &ubo_mat);
479
480 pass.bind_ubo("gp_lights", lights_ubo);
481 pass.bind_ubo("gp_materials", ubo_mat);
482 pass.bind_texture("gp_fill_tx", tex_fill);
483 pass.bind_texture("gp_stroke_tx", tex_stroke);
484 pass.push_constant("gp_material_offset", mat_ofs);
485 /* Since we don't use the sbuffer in GPv3, this is always 0. */
486 pass.push_constant("gp_stroke_index_offset", 0.0f);
487 pass.push_constant("viewport_size", float2(draw_ctx->viewport_size_get()));
488
489 const VArray<int> stroke_materials = *attributes.lookup_or_default<int>(
490 "material_index", bke::AttrDomain::Curve, 0);
491 const VArray<bool> is_fill_guide = *attributes.lookup_or_default<bool>(
492 ".is_fill_guide", bke::AttrDomain::Curve, false);
493
494 const VArray<bool> hide_stroke = *attributes.lookup_or_default<bool>(
495 "hide_stroke", bke::AttrDomain::Curve, false);
496 const VArray<int> fill_ids = *attributes.lookup_or_default<int>(
497 "fill_id", bke::AttrDomain::Curve, 0);
498
499 const bool only_lines = !ELEM(ob->mode,
503 info.frame_number != this->cfra && this->use_multiedit_lines_only &&
504 do_multi_frame;
505 const bool is_onion = info.onion_id != 0;
506
507 int fill_index = 0;
508
509 Array<int> fill_index_by_curves(curves.curves_num(), -1);
510 Array<int> first_curves(curves.curves_num());
512
513 for (const int curve_i : curves.curves_range()) {
514 const bool is_filled = fill_ids[curve_i] != 0;
515 const bool active_filled = is_filled && (fill_index_by_curves[curve_i] == -1);
516
517 /* Keep track of already rendered fills. */
518 if (active_filled) {
519 const Span<int> fill = (*fills)[fill_index];
520 const int first_curve = fill.first();
521 for (const int pos : fill.index_range()) {
522 const int curve_i = fill[pos];
523 fill_index_by_curves[curve_i] = fill_index;
524 first_curves[curve_i] = first_curve;
525 }
526
527 fill_index++;
528 }
529 }
530
531 visible_strokes.foreach_index([&](const int curve_i) {
532 /* Will be `-1` if not a fill. */
533 const int fill_index = fill_index_by_curves[curve_i];
534
535 const bool is_filled = fill_index != -1;
536 const bool active_filled = is_filled && (first_curves[curve_i] == curve_i);
537
538 /* The material index is allowed to be negative as it's stored as a generic attribute. We
539 * clamp it here to avoid crashing in the rendering code. Any stroke with a material < 0 will
540 * use the first material in the first material slot. */
541 const int material_index = std::max(stroke_materials[curve_i], 0);
542 const MaterialGPencilStyle *gp_style = BKE_gpencil_material_settings(ob, material_index + 1);
543
544 const bool is_fill_guide_stroke = is_fill_guide[curve_i];
545
546 const bool has_triangles = active_filled && triangles && !triangles->is_empty() &&
547 !(*triangles)[fill_index].is_empty();
548
549 const bool hide_material = (gp_style->flag & GP_MATERIAL_HIDE) != 0;
550 const bool show_stroke = !hide_stroke[curve_i] || is_fill_guide_stroke;
551 const bool show_fill = (has_triangles) && active_filled && (!this->simplify_fill) &&
552 !is_fill_guide_stroke;
553 const bool hide_onion = is_onion && ((gp_style->flag & GP_MATERIAL_HIDE_ONIONSKIN) != 0 ||
554 (!do_onion && !do_multi_frame));
555 const bool skip_stroke = hide_material || (!show_stroke && !show_fill) ||
556 (only_lines && !do_onion && is_onion) || hide_onion;
557
558 if (skip_stroke) {
559 if (active_filled) {
560 t_offset += num_triangles_per_fill[fill_index];
561 }
562 t_offset += num_vertices_per_curve[curve_i] * 2;
563
564 return;
565 }
566
567 gpu::UniformBuf *new_ubo_mat;
568 gpu::Texture *new_tex_fill = nullptr;
569 gpu::Texture *new_tex_stroke = nullptr;
571 matpool, mat_ofs + material_index, &new_tex_stroke, &new_tex_fill, &new_ubo_mat);
572
573 const bool resource_changed = (ubo_mat != new_ubo_mat) ||
574 (new_tex_fill && (new_tex_fill != tex_fill)) ||
575 (new_tex_stroke && (new_tex_stroke != tex_stroke));
576
577 if (resource_changed) {
578 drawcall_flush(pass);
579
580 if (new_ubo_mat != ubo_mat) {
581 pass.bind_ubo("gp_materials", new_ubo_mat);
582 ubo_mat = new_ubo_mat;
583 }
584 if (new_tex_fill) {
585 pass.bind_texture("gp_fill_tx", new_tex_fill);
586 tex_fill = new_tex_fill;
587 }
588 if (new_tex_stroke) {
589 pass.bind_texture("gp_stroke_tx", new_tex_stroke);
590 tex_stroke = new_tex_stroke;
591 }
592 }
593
594 gpu::Batch *geom = DRW_cache_grease_pencil_get(this->scene, ob);
595 if (iter_geom != geom) {
596 drawcall_flush(pass);
597
598 gpu::VertBuf *position_tx = DRW_cache_grease_pencil_position_buffer_get(this->scene, ob);
600 pass.bind_texture("gp_pos_tx", position_tx);
601 pass.bind_texture("gp_col_tx", color_tx);
602 }
603
604 if (show_fill) {
605 const int v_first = t_offset * 3;
606 const int v_count = num_triangles_per_fill[fill_index] * 3;
607 drawcall_add(pass, geom, v_first, v_count);
608 }
609
610 if (active_filled) {
611 t_offset += num_triangles_per_fill[fill_index];
612 }
613
614 if (show_stroke) {
615 const int v_first = t_offset * 3;
616 const int v_count = num_vertices_per_curve[curve_i] * 2 * 3;
617 drawcall_add(pass, geom, v_first, v_count);
618 }
619
620 t_offset += num_vertices_per_curve[curve_i] * 2;
621 });
622 }
623
624 if (last_pass) {
625 drawcall_flush(*last_pass);
626 }
627
628 return tgp_ob;
629}
630
632{
633 Object *ob = ob_ref.object;
634
635 /* object must be visible */
637 return;
638 }
639
640 if (ob->data && (ob->type == OB_GREASE_PENCIL) && (ob->dt >= OB_SOLID)) {
641 ResourceHandleRange res_handle = manager.unique_handle(ob_ref);
642
643 tObject *tgp_ob = object_sync_do(ob, res_handle);
644 vfx_sync(ob, tgp_ob);
645 }
646
647 if (ob->type == OB_LAMP && this->use_lights) {
649 }
650}
651
653{
654 /* Upload UBO data. */
657 MaterialPool *pool;
658 while ((pool = static_cast<MaterialPool *>(BLI_memblock_iterstep(&iter)))) {
659 GPU_uniformbuf_update(pool->ubo, pool->mat_data);
660 }
661
663 LightPool *lpool;
664 while ((lpool = static_cast<LightPool *>(BLI_memblock_iterstep(&iter)))) {
665 GPU_uniformbuf_update(lpool->ubo, lpool->light_data);
666 }
667}
668
670{
671 /* Create frame-buffers only if needed. */
672 if (this->tobjects.first == nullptr) {
673 return;
674 }
675
676 const int2 size = int2(draw_ctx->viewport_size_get());
677
678 const gpu::TextureFormat format_color = gpu::TextureFormat::SFLOAT_16_16_16_16;
679 const gpu::TextureFormat format_reveal = this->use_signed_fb ?
680 gpu::TextureFormat::SFLOAT_16_16_16_16 :
681 gpu::TextureFormat::UNORM_10_10_10_2;
682
683 this->depth_tx.acquire_2d(size, gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
684 this->color_tx.acquire_2d(size, format_color);
685 this->reveal_tx.acquire_2d(size, format_reveal);
686
688 GPU_ATTACHMENT_TEXTURE(this->color_tx),
690
691 if (this->use_layer_fb) {
692 this->color_layer_tx.acquire_2d(size, format_color);
693 this->reveal_layer_tx.acquire_2d(size, format_reveal);
694
698 }
699
700 if (this->use_object_fb) {
701 this->color_object_tx.acquire_2d(size, format_color);
702 this->reveal_object_tx.acquire_2d(size, format_reveal);
703
707 }
708
709 if (this->use_mask_fb) {
710 /* Use high quality format for render. */
711 const gpu::TextureFormat mask_format = this->is_render ? gpu::TextureFormat::UNORM_16 :
712 gpu::TextureFormat::UNORM_8;
713 /* We need an extra depth to not disturb the normal drawing. */
714 this->mask_depth_tx.acquire_2d(size, gpu::TextureFormat::SFLOAT_32_DEPTH_UINT_8);
715 /* The mask_color_tx is needed for frame-buffer completeness. */
716 this->mask_color_tx.acquire_2d(size, gpu::TextureFormat::UNORM_8);
717 this->mask_tx.acquire_2d(size, mask_format);
718
722 }
723
724 /* The engine might not support passes, so check if the combined pass actually exists before
725 * rendering grease pencil to it. */
726 const bool combined_pass_exists = DRW_viewport_pass_texture_exists(RE_PASSNAME_COMBINED);
727 if (this->need_combined_pass && combined_pass_exists) {
730 }
731
732 if (this->need_grease_pencil_pass) {
733 const int2 size = int2(draw_ctx->viewport_size_get());
736 grease_pencil_pass.acquire_2d(size, gpu::TextureFormat::SFLOAT_16_16_16_16);
738 }
739
742 this->depth_pass_img = depth_pass.gpu_texture();
743 }
744}
745
747{
748 this->depth_tx.release();
749 this->color_tx.release();
750 this->reveal_tx.release();
751 this->color_layer_tx.release();
752 this->reveal_layer_tx.release();
753 this->color_object_tx.release();
755 this->mask_depth_tx.release();
756 this->mask_color_tx.release();
757 this->mask_tx.release();
758 this->smaa_edge_tx.release();
759 this->smaa_weight_tx.release();
760}
761
762void Instance::draw_mask(View &view, tObject *ob, tLayer *layer)
763{
764 Manager *manager = DRW_manager_get();
765
766 bool inverted = false;
767 /* OPTI(@fclem): we could optimize by only clearing if the new mask_bits does not contain all
768 * the masks already rendered in the buffer, and drawing only the layers not already drawn. */
769 bool cleared = false;
770
771 GPU_debug_group_begin("GPencil Mask");
772
774
775 for (int i = 0; i < GP_MAX_MASKBITS; i++) {
776 if (!BLI_BITMAP_TEST(layer->mask_bits, i)) {
777 continue;
778 }
779
780 if (BLI_BITMAP_TEST_BOOL(layer->mask_invert_bits, i) != inverted) {
781 if (cleared) {
782 manager->submit(this->mask_invert_ps);
783 }
784 inverted = !inverted;
785 }
786
787 if (!cleared) {
788 cleared = true;
790 this->mask_fb, {1.0, 1.0, 1.0, 1.0}, ob->is_drawmode3d ? 1.0f : 0.0f);
791 }
792
793 tLayer *mask_layer = grease_pencil_layer_cache_get(ob, i, true);
794 /* When filtering by view-layer, the mask could be null and must be ignored. */
795 if (mask_layer == nullptr) {
796 continue;
797 }
798
799 manager->submit(*mask_layer->geom_ps, view);
800 }
801
802 if (!inverted) {
803 /* Blend shader expect an opacity mask not a revealage buffer. */
804 manager->submit(this->mask_invert_ps);
805 }
806
808}
809
810void Instance::draw_object(View &view, tObject *ob)
811{
812 Manager *manager = DRW_manager_get();
813
814 const std::array<double4, 2> clear_cols = {double4{0, 0, 0, 0}, double4{1, 1, 1, 1}};
815
816 GPU_debug_group_begin("GPencil Object");
817
818 gpu::FrameBuffer *fb_object = (ob->vfx.first) ? this->object_fb : this->gpencil_fb;
819
820 GPU_framebuffer_bind(fb_object);
821 GPU_framebuffer_clear_depth_stencil(fb_object, ob->is_drawmode3d ? 1.0f : 0.0f, 0x00);
822
823 if (ob->vfx.first) {
824 GPU_framebuffer_multi_clear(fb_object, clear_cols);
825 }
826
827 for (tLayer *layer = ob->layers.first; layer; layer = layer->next) {
828 if (layer->mask_bits) {
829 draw_mask(view, ob, layer);
830 }
831
832 if (layer->blend_ps) {
834 GPU_framebuffer_multi_clear(this->layer_fb, clear_cols);
835 }
836 else {
837 GPU_framebuffer_bind(fb_object);
838 }
839
840 manager->submit(*layer->geom_ps, view);
841
842 if (layer->blend_ps) {
843 GPU_framebuffer_bind(fb_object);
844 manager->submit(*layer->blend_ps);
845 }
846 }
847
848 for (tVfx *vfx = ob->vfx.first; vfx; vfx = vfx->next) {
849 GPU_framebuffer_bind(*(vfx->target_fb));
850 manager->submit(*vfx->vfx_ps);
851 }
852
853 this->object_bound_mat = float4x4(ob->plane_mat);
854 this->is_stroke_order_3d = ob->is_drawmode3d;
855
856 if (this->scene_fb) {
858 manager->submit(this->merge_depth_ps, view);
859 }
860
862 manager->submit(this->merge_depth_pass_ps, view);
863 }
864
866}
867
869{
872
873 if (this->render_depth_tx.is_valid()) {
874 this->scene_depth_tx = this->render_depth_tx;
875 this->scene_fb = this->render_fb;
876 }
877 else {
878 this->scene_fb = dfbl->default_fb;
879 this->scene_depth_tx = dtxl->depth;
880 }
882
883 std::array<double4, 2> clear_cols = {double4{0.0f, 0.0f, 0.0f, 0.0f},
884 double4{1.0f, 1.0f, 1.0f, 1.0f}};
885
886 /* Fade 3D objects. */
887 if ((!this->is_render) && (this->fade_3d_object_opacity > -1.0f) && (this->obact != nullptr) &&
888 ELEM(this->obact->type, OB_GREASE_PENCIL))
889 {
890 float3 background_color;
891 ED_view3d_background_color_get(this->scene, this->v3d, background_color);
892 /* Blend color. */
893 background_color = math::interpolate(
894 background_color, float3(clear_cols[0]), this->fade_3d_object_opacity);
895
896 clear_cols[0] = double4(background_color, 0.0f);
897 clear_cols[1] = double4(float4(clear_cols[1]) * this->fade_3d_object_opacity);
898 }
899
900 /* Sort object by decreasing Z to avoid most of alpha ordering issues. */
902
903 if (this->tobjects.first == nullptr) {
904 return;
905 }
906
908
909 antialiasing_init();
910
911 this->acquire_resources();
912
913 if (this->tobjects.first) {
915 GPU_framebuffer_multi_clear(this->gpencil_fb, clear_cols);
916 }
917
918 View &view = View::default_get();
919
920 for (tObject *ob = this->tobjects.first; ob; ob = ob->next) {
921 draw_object(view, ob);
922 }
923
924 if (this->scene_fb) {
925 antialiasing_draw(manager);
926 }
927
928 this->release_resources();
929
931}
932
934{
935 return new Instance();
936}
937
942
943} // namespace blender::draw::gpencil
Camera data-block and utility functions.
Low-level operations for curves.
Low-level operations for grease pencil.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_TEST_BOOL(_bitmap, _index)
Definition BLI_bitmap.hh:73
#define RE_PASSNAME_COMBINED
#define RE_PASSNAME_DEPTH
#define RE_PASSNAME_GREASE_PENCIL
#define V3D_USES_SCENE_WORLD(v3d)
#define V3D_USES_SCENE_LIGHTS(v3d)
#define XRAY_ENABLED(v3d)
#define XRAY_ALPHA(v3d)
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define ELEM(...)
#define STREQ(a, b)
bool contains(const Key &key) const
Definition BLI_set.hh:310
void ensure(GPUAttachment depth=GPU_ATTACHMENT_NONE, GPUAttachment color1=GPU_ATTACHMENT_NONE, GPUAttachment color2=GPU_ATTACHMENT_NONE, GPUAttachment color3=GPU_ATTACHMENT_NONE, GPUAttachment color4=GPU_ATTACHMENT_NONE, GPUAttachment color5=GPU_ATTACHMENT_NONE, GPUAttachment color6=GPU_ATTACHMENT_NONE, GPUAttachment color7=GPU_ATTACHMENT_NONE, GPUAttachment color8=GPU_ATTACHMENT_NONE)
ResourceHandleRange unique_handle(const ObjectRef &ref)
void submit(PassSimple &pass, View &view)
bool acquire_2d(int2 extent, gpu::TextureFormat format, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, int mip_len=1)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
gpu::Texture * gpu_texture()
static View & default_get()
Definition draw_view.cc:317
const float4x4 & viewinv(int view_id=0) const
Definition draw_view.hh:142
void draw_procedural(GPUPrimType primitive, uint instance_len, uint vertex_len, uint vertex_first=-1, ResourceIDRange res_id={}, uint custom_id=0)
Definition draw_pass.hh:976
void shader_set(gpu::Shader *shader)
void bind_texture(const char *name, gpu::Texture *texture, GPUSamplerState state=sampler_auto)
void bind_image(const char *name, gpu::Texture *image)
void state_set(DRWState state, int clip_plane_count=0)
void push_constant(const char *name, const float &data)
#define GP_MAX_MASKBITS
#define GPENCIL_SIMPLIFY_AA(scene)
#define GPENCIL_SIMPLIFY_FILL(scene, playing)
#define GPENCIL_SIMPLIFY_FX(scene, playing)
#define layer
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
ccl_device_inline float3x3 inverted(const float3x3 m)
void fill_index_range(MutableSpan< T > span, const T start=0)
Set< std::string > get_used_passes(const Scene &scene, const ViewLayer *view_layer, ExecutionMode mode)
void gpencil_light_ambient_add(LightPool *lightpool, const float color[3])
void gpencil_object_cache_sort(Instance *inst)
void gpencil_light_pool_populate(LightPool *lightpool, Object *ob)
LightPool * gpencil_light_pool_add(Instance *inst)
tLayer * grease_pencil_layer_cache_add(Instance *inst, const Object *ob, const bke::greasepencil::Layer &layer, const int onion_id, const bool is_used_as_mask, tObject *tgp_ob)
void gpencil_material_resources_get(MaterialPool *first_pool, int mat_id, gpu::Texture **r_tex_stroke, gpu::Texture **r_tex_fill, gpu::UniformBuf **r_ubo_mat)
tLayer * grease_pencil_layer_cache_get(tObject *tgp_ob, int layer_id, const bool skip_onion)
MaterialPool * gpencil_material_pool_create(Instance *inst, Object *ob, int *ofs, const bool is_vertex_mode)
tObject * gpencil_object_cache_add(Instance *inst, Object *ob, const bool is_stroke_order_3d, const Bounds< float3 > bounds)
gpu::Batch * DRW_cache_grease_pencil_get(const Scene *scene, Object *ob)
detail::Pass< command::DrawCommandBuf > PassSimple
gpu::VertBuf * DRW_cache_grease_pencil_position_buffer_get(const Scene *scene, Object *ob)
gpu::VertBuf * DRW_cache_grease_pencil_color_buffer_get(const Scene *scene, Object *ob)
Vector< DrawingInfo > retrieve_visible_drawings(const Scene &scene, const GreasePencil &grease_pencil, const bool do_onion_skinning)
IndexMask retrieve_visible_strokes(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
IndexMask retrieve_visible_fills(Object &object, const bke::greasepencil::Drawing &drawing, IndexMaskMemory &memory)
BLI_NOINLINE void fill(T *__restrict data, const T &value, const IndexMask &mask)
T interpolate(const T &a, const T &b, const FactorT &t)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GP_LAYER_TREE_NODE_DISABLE_MASKS_IN_VIEWLAYER
draw::TextureFromPool & DRW_viewport_pass_texture_get(const char *pass_name)
void ED_view3d_background_color_get(const Scene *scene, const View3D *v3d, float r_color[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BLI_memblock_clear(BLI_memblock *mblk, MemblockValFreeFP free_callback) ATTR_NONNULL(1)
void GPU_framebuffer_clear_depth_stencil(gpu::FrameBuffer *fb, float clear_depth, uint clear_stencil)
MatBase< float, 4, 4 > float4x4
int DRW_object_visibility_in_active_context(const Object *ob)
@ GP_MATERIAL_HIDE_ONIONSKIN
VecBase< float, 4 > float4
MaterialGPencilStyle * BKE_gpencil_material_settings(Object *ob, short act)
float BKE_camera_object_dof_distance(const struct Object *ob)
@ GP_USE_MULTI_FRAME_EDITING
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
@ GPU_TEXTURE_USAGE_SHADER_READ
T & DRW_object_get_data_for_drawing(const Object &object)
@ OB_MODE_VERTEX_GREASE_PENCIL
@ OB_MODE_WEIGHT_GREASE_PENCIL
@ OB_MODE_PAINT_GREASE_PENCIL
wmWindowManager * CTX_wm_manager(const bContext *C)
@ V3D_GP_ONION_SKIN_ACTIVE_OBJECT
@ V3D_GP_FADE_NOACTIVE_LAYERS
@ V3D_GP_FORCE_STROKE_ORDER_3D
@ V3D_GP_SHOW_MULTIEDIT_LINES
@ V3D_GP_FADE_NOACTIVE_GPENCIL
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS
Definition draw_state.hh:39
@ DRW_STATE_LOGIC_INVERT
Definition draw_state.hh:66
void DRW_submission_end()
const DRWContext * DRW_context_get()
@ V3D_SHADING_VERTEX_COLOR
void BLI_memblock_iternew(BLI_memblock *mblk, BLI_memblock_iter *iter) ATTR_NONNULL()
void * BLI_memblock_iterstep(BLI_memblock_iter *iter) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void GPU_uniformbuf_update(gpu::UniformBuf *ubo, const void *data)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_framebuffer_multi_clear(gpu::FrameBuffer *fb, Span< double4 > clear_colors)
float BKE_camera_sensor_size(int sensor_fit, float sensor_x, float sensor_y)
MINLINE void copy_v3_fl(float r[3], float f)
void DRW_submission_start()
draw::Manager * DRW_manager_get()
VecBase< float, 3 > float3
bool DRW_viewport_pass_texture_exists(const char *pass_name)
void GPU_framebuffer_clear_color_depth(gpu::FrameBuffer *fb, const double4 clear_col, float clear_depth)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float DEG_get_ctime(const Depsgraph *graph)
VecBase< double, 4 > double4
@ OB_VISIBLE_SELF
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
#define fabsf
Bounds()=default
DefaultTextureList * viewport_texture_list_get() const
RegionView3D * rv3d
ViewLayer * view_layer
const bContext * evil_C
Depsgraph * depsgraph
float2 viewport_size_get() const
DefaultFramebufferList * viewport_framebuffer_list_get() const
bool is_viewport_compositor_used() const
char info[GPU_INFO_SIZE]
Definition DRW_render.hh:74
eRegionView3D_Persp persp
struct World * world
struct ToolSettings * toolsettings
eGPencil_Flags gpencil_flags
eV3DShadingColorType color_type
View3DOverlay overlay
eView3D_Flag2 flag2
eView3D_GPFlag gp_flag
struct Object * camera
View3DShading shading
DrawEngine * create_instance() final
struct blender::draw::gpencil::Instance::@250045130013031054234100054276160145124267353263 tobjects_infront
void draw(Manager &manager) final
void object_sync(ObjectRef &ob_ref, Manager &manager) final
struct blender::draw::gpencil::Instance::@250045130013031054234100054276160145124267353263 tobjects
struct blender::draw::gpencil::Instance::@270103242060374244001011226234131367140002277206 sbuffer_tobjects
gpLight light_data[GPENCIL_LIGHT_BUFFER_LEN]
gpMaterial mat_data[GPENCIL_MATERIAL_BUFFER_LEN]