Blender V5.3
gpencil_engine_private.hh
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
9#pragma once
10
11#include "BLI_bitmap.hh"
12#include "BLI_memblock.hh"
13
14#include "DNA_shader_fx_types.h"
15#include "DRW_render.hh"
16
17#include "BKE_grease_pencil.hh"
18
19#include "GPU_batch.hh"
20
21#include "draw_pass.hh"
22#include "draw_view_data.hh"
23
24#define GP_LIGHT
25
26#include "gpencil_defines.hh"
27#include "gpencil_shader.hh"
29
30namespace blender {
31
32struct GpencilBatchCache;
33struct Object;
34struct RenderEngine;
35struct RenderLayer;
36struct View3D;
37
38/* used to convert pixel scale. */
39#define GPENCIL_PIXEL_FACTOR 2000.0f
40
41/* used to expand VBOs. Size has a big impact in the speed */
42#define GPENCIL_VBO_BLOCK_SIZE 128
43
44#define GP_MAX_MASKBITS 256
45
46#define GPENCIL_SIMPLIFY(scene) \
47 ((scene->r.mode & R_SIMPLIFY) && (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ENABLE))
48#define GPENCIL_SIMPLIFY_ONPLAY(playing) \
49 (((playing == true) && (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ON_PLAY)) || \
50 ((scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_ON_PLAY) == 0))
51#define GPENCIL_SIMPLIFY_FILL(scene, playing) \
52 ((GPENCIL_SIMPLIFY_ONPLAY(playing) && GPENCIL_SIMPLIFY(scene) && \
53 (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_FILL)))
54#define GPENCIL_SIMPLIFY_FX(scene, playing) \
55 ((GPENCIL_SIMPLIFY_ONPLAY(playing) && GPENCIL_SIMPLIFY(scene) && \
56 (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_FX)))
57#define GPENCIL_SIMPLIFY_TINT(scene) \
58 (GPENCIL_SIMPLIFY(scene) && (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_TINT))
59#define GPENCIL_SIMPLIFY_AA(scene) \
60 (GPENCIL_SIMPLIFY(scene) && (scene->r.simplify_gpencil & SIMPLIFY_GPENCIL_AA))
61
62namespace draw::gpencil {
63
65 /* Single linked-list. */
67 /* GPU representation of materials. */
69 /* Matching ubo. */
71 /* Texture per material. NULL means none. */
74 /* Number of material used in this pool. */
76};
77
78struct LightPool {
79 /* GPU representation of materials. */
81 /* Matching ubo. */
83 /* Number of light in the pool. */
85};
86
87/* Temporary gpencil FX reflection used by the gpencil::Instance. */
88struct tVfx {
90 struct tVfx *next = nullptr;
91 ::std::unique_ptr<PassSimple> vfx_ps = ::std::make_unique<PassSimple>("vfx");
92 /* Frame-buffer reference since it may not be allocated yet. */
94};
95
96/* Temporary gpencil layer reflection used by the gpencil::Instance. */
97struct tLayer {
99 struct tLayer *next;
101 ::std::unique_ptr<PassSimple> geom_ps;
103 ::std::unique_ptr<PassSimple> blend_ps;
111};
112
113/* Temporary object reflection used by the gpencil::Instance. */
114struct tObject {
116 struct tObject *next;
117
118 struct {
119 tLayer *first, *last;
121
122 struct {
123 tVfx *first, *last;
125
126 /* Distance to camera. Used for sorting. */
127 float camera_z;
128 /* Normal used for shading. Based on view angle. */
130 /* Used for drawing depth merge pass. */
131 float plane_mat[4][4];
132
134
135 /* Use Material Holdout. */
137};
138
139/* *********** LISTS *********** */
140
141struct Instance final : public DrawEngine {
142 PassSimple smaa_edge_ps = {"smaa_edge"};
143 PassSimple smaa_weight_ps = {"smaa_weight"};
144 PassSimple smaa_resolve_ps = {"smaa_resolve"};
145 PassSimple accumulate_ps = {"aa_accumulate"};
146 /* Composite the object depth to the default depth buffer to occlude overlays. */
147 PassSimple merge_depth_ps = {"merge_depth_ps"};
148 /* Composite the object depth to the depth pass. */
149 PassSimple merge_depth_pass_ps = {"merge_depth_pass_ps"};
150 /* Invert mask buffer content. */
151 PassSimple mask_invert_ps = {"mask_invert_ps"};
152
154
155 /* Dummy texture to avoid errors cause by empty sampler. */
156 Texture dummy_texture = {"dummy_texture"};
157 Texture dummy_depth = {"dummy_depth"};
158 /* Textures used during render. Containing underlying rendered scene. */
159 Texture render_depth_tx = {"render_depth_tx"};
160 Texture render_color_tx = {"render_color_tx"};
161 /* Snapshot for smoother drawing. */
162 Texture snapshot_depth_tx = {"snapshot_depth_tx"};
163 Texture snapshot_color_tx = {"snapshot_color_tx"};
164 Texture snapshot_reveal_tx = {"snapshot_reveal_tx"};
165 /* Textures used by Anti-aliasing. */
166 Texture smaa_area_tx = {"smaa_area_tx"};
167 Texture smaa_search_tx = {"smaa_search_tx"};
168
169 /* Stores the viewport compositor depth pass if needed. */
171
172 /* Temp Textures (shared with other engines). */
173 TextureFromPool depth_tx = {"depth_tx"};
174 TextureFromPool color_tx = {"color_tx"};
175 TextureFromPool color_layer_tx = {"color_layer_tx"};
176 TextureFromPool color_object_tx = {"color_object_tx"};
177 /* Revealage is 1 - alpha */
178 TextureFromPool reveal_tx = {"reveal_tx"};
179 TextureFromPool reveal_layer_tx = {"reveal_layer_tx"};
180 TextureFromPool reveal_object_tx = {"reveal_object_tx"};
181 /* Mask texture */
182 TextureFromPool mask_depth_tx = {"mask_depth_tx"};
183 TextureFromPool mask_color_tx = {"mask_color_tx"};
185 /* Anti-Aliasing. */
186 TextureFromPool smaa_edge_tx = {"smaa_edge_tx"};
187 TextureFromPool smaa_weight_tx = {"smaa_weight_tx"};
188
189 Framebuffer render_fb = {"render_fb"};
190 Framebuffer gpencil_fb = {"gpencil_fb"};
191 Framebuffer combined_pass_fb = {"combined_pass_fb"};
192 Framebuffer gpencil_pass_fb = {"gpencil_pass_fb"};
193 Framebuffer snapshot_fb = {"snapshot_fb"};
194 Framebuffer layer_fb = {"layer_fb"};
195 Framebuffer object_fb = {"object_fb"};
196 Framebuffer mask_fb = {"mask_fb"};
197 Framebuffer smaa_edge_fb = {"smaa_edge_fb"};
198 Framebuffer smaa_weight_fb = {"smaa_weight_fb"};
199
200 /* NOTE: These do not preserve the PassSimple memory across frames.
201 * If that becomes a bottleneck, these containers can be improved. */
204
205 /* tObject */
207 /* tLayer */
209 /* tVfx */
211 /* MaterialPool */
213 /* LightPool */
215 /* BLI_bitmap */
217
218 const DRWContext *draw_ctx = nullptr;
219
220 /* Last used material pool. */
222 /* Last used light pool. */
224 /* Common lightpool containing all lights in the scene. */
226 /* Common lightpool containing one ambient white light. */
228 /* Linked list of tObjects. */
232 /* Used to record whether the `tobjects` list is sorted. Do not sort drawings again in separate
233 * pass rendering to avoid generating infinite lists. */
235 /* Pointer to dtxl->depth */
238 /* Used for render accumulation antialiasing. */
239 Texture accumulation_tx = {"gp_accumulation_tx"};
240 Framebuffer accumulation_fb = {"gp_accumulation_fb"};
241 /* Copy of txl->dummy_tx */
243 /* Copy of v3d->shading.single_color. */
245 /* Copy of v3d->shading.color_type or -1 to ignore. */
247 /* Current frame */
248 int cfra;
249 /* If we are rendering for final render (F12).
250 * NOTE: set to false for viewport and opengl rendering (including sequencer scene rendering),
251 * but set to true when rendering in #OB_RENDER shading mode (viewport or opengl rendering). */
253 /* If we are in viewport display (used for VFX). */
255 /* Is shading set to wire-frame. */
257 /* Used by the depth merge step. */
259 /* Used for computing object distance to camera. */
261 float camera_pos[3];
262 /* Pseudo depth of field parameter. Used to scale blur radius. */
263 float dof_params[2];
264 /* Used for DoF Setup. */
266 /* Copy of draw_ctx->view_layer for convenience. */
268 /* Copy of draw_ctx->scene for convenience. */
269 struct Scene *scene;
270 /* Copy of draw_ctx->vie3d for convenience. */
271 struct View3D *v3d;
272
273 /* Active object. */
275 /* List of temp objects containing the stroke. */
279 /* Batches containing the temp stroke. */
280 gpu::Batch *stroke_batch;
281 gpu::Batch *fill_batch;
283
284 /* Display onion skinning */
286 /* Show only the onion skins of the active object. */
288 /* Playing animation */
290 /* simplify settings */
294 /* Use scene lighting or flat shading (global setting). */
296 /* Use physical lights or just ambient lighting. */
298 /* Do we need additional frame-buffers? */
302 /* The viewport compositor needs the combined pass, so we need to render to it. */
304 /* The viewport compositor needs the grease pencil pass, so we need to render to it. */
306 /* Some blend mode needs to add negative values.
307 * This is only supported if target texture is signed. Only switch for the `reveal_tex`. */
309 /* Use only lines for multiedit and not active frame. */
311 /* Layer opacity for fading. */
313 /* Opacity for fading gpencil objects. */
315 /* Opacity for fading 3D objects. */
317 /* Mask opacity uniform. */
319 /* X-ray transparency in solid mode. */
321 /* Mask invert uniform. */
323 /* Vertex Paint opacity. */
325 /* Force 3D depth rendering. */
327
329 {
330 BLI_memblock_destroy(gp_light_pool, light_pool_free);
331 BLI_memblock_destroy(gp_material_pool, material_pool_free);
334 delete gp_layer_pool;
335 delete gp_vfx_pool;
336 }
337
338 void acquire_resources();
339 void release_resources();
340
342 {
343 return "Grease Pencil";
344 }
345
346 void init() final;
347
348 void begin_sync() final;
349 void object_sync(ObjectRef &ob_ref, Manager &manager) final;
350 void end_sync() final;
351
352 void draw(Manager &manager) final;
353
354 void antialiasing_accumulate(Manager &manager, float alpha);
355
356 static float2 antialiasing_sample_get(int sample_index, int sample_count);
357
358 private:
359 tObject *object_sync_do(Object *ob, ResourceHandleRange res_handle);
360
361 /* Check if the passed in layer is used by any other layer as a mask (in the viewlayer). */
362 bool is_used_as_layer_mask_in_viewlayer(const GreasePencil &grease_pencil,
363 const bke::greasepencil::Layer &mask_layer,
364 const ViewLayer &view_layer);
365
366 /* Returns true if this layer should be rendered (as part of the viewlayer). */
367 bool use_layer_in_render(const GreasePencil &grease_pencil,
368 const bke::greasepencil::Layer &layer,
369 const ViewLayer &view_layer,
370 bool &r_is_used_as_mask);
371
372 void draw_mask(View &view, tObject *ob, tLayer *layer);
373 void draw_object(View &view, tObject *ob);
374
375 void antialiasing_init();
376 void antialiasing_draw(Manager &manager);
377
378 struct VfxFramebufferRef {
379 /* These may not be allocated yet, use address of future pointer. */
383 };
384
385 SwapChain<VfxFramebufferRef, 2> vfx_swapchain_;
386
387 PassSimple &vfx_pass_create(const char *name,
389 gpu::Shader *sh,
390 tObject *tgp_ob,
392
393 void vfx_blur_sync(BlurShaderFxData *fx, Object *ob, tObject *tgp_ob);
394 void vfx_colorize_sync(ColorizeShaderFxData *fx, Object *ob, tObject *tgp_ob);
395 void vfx_flip_sync(FlipShaderFxData *fx, Object *ob, tObject *tgp_ob);
396 void vfx_rim_sync(RimShaderFxData *fx, Object *ob, tObject *tgp_ob);
397 void vfx_pixelize_sync(PixelShaderFxData *fx, Object *ob, tObject *tgp_ob);
398 void vfx_shadow_sync(ShadowShaderFxData *fx, Object *ob, tObject *tgp_ob);
399 void vfx_glow_sync(GlowShaderFxData *fx, Object *ob, tObject *tgp_ob);
400 void vfx_wave_sync(WaveShaderFxData *fx, Object *ob, tObject *tgp_ob);
401 void vfx_swirl_sync(SwirlShaderFxData *fx, Object *ob, tObject *tgp_ob);
402
403 void vfx_sync(Object *ob, tObject *tgp_ob);
404
405 static void material_pool_free(void *storage)
406 {
407 MaterialPool *matpool = static_cast<MaterialPool *>(storage);
408 GPU_UBO_FREE_SAFE(matpool->ubo);
409 }
410
411 static void light_pool_free(void *storage)
412 {
413 LightPool *lightpool = static_cast<LightPool *>(storage);
414 GPU_UBO_FREE_SAFE(lightpool->ubo);
415 }
416};
417
419 void *engine_type; /* Required */
421
423};
424
425/* geometry batch cache functions */
426struct GpencilBatchCache *gpencil_batch_cache_get(struct Object *ob, int cfra);
427
429 Object *ob,
430 bool is_stroke_order_3d,
433
434tLayer *grease_pencil_layer_cache_get(tObject *tgp_ob, int layer_id, bool skip_onion);
435
437 const Object *ob,
439 int onion_id,
440 bool is_used_as_mask,
441 tObject *tgp_ob);
448 Object *ob,
449 int *ofs,
450 bool is_vertex_mode);
452 int mat_id,
453 gpu::Texture **r_tex_stroke,
454 gpu::Texture **r_tex_fill,
455 gpu::UniformBuf **r_ubo_mat);
456
457void gpencil_light_ambient_add(LightPool *lightpool, const float color[3]);
458void gpencil_light_pool_populate(LightPool *lightpool, Object *ob);
464
465} // namespace draw::gpencil
466} // namespace blender
Low-level operations for grease pencil.
#define BLI_BITMAP_SIZE(_num)
Definition BLI_bitmap.hh:34
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define BLI_memblock_create(elem_size)
#define GPU_UBO_FREE_SAFE(ubo)
#define GPU_INFO_SIZE
#define GPENCIL_MATERIAL_BUFFER_LEN
#define GPENCIL_LIGHT_BUFFER_LEN
#define GP_MAX_MASKBITS
#define storage(slot, qualifiers)
#define sampler(slot)
#define layer
const int state
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)
struct GpencilBatchCache * gpencil_batch_cache_get(struct Object *ob, int cfra)
tLayer * grease_pencil_layer_cache_get(tObject *tgp_ob, int layer_id, const bool skip_onion)
LightPool * gpencil_light_pool_create(Instance *inst, Object *)
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)
detail::Pass< command::DrawCommandBuf > PassSimple
BMesh Iterators.
BLI_INLINE float fb(float length, float L)
MatBase< float, 4, 4 > float4x4
VecBase< float, 2 > float2
void BLI_memblock_destroy(BLI_memblock *mblk, MemblockValFreeFP free_callback) ATTR_NONNULL(1)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
VecBase< float, 3 > float3
static constexpr GPUSamplerState internal_sampler()
draw::detail::SubPassVector< tLayer > tLayer_Pool
struct blender::draw::gpencil::Instance::@250045130013031054234100054276160145124267353263 tobjects_infront
static float2 antialiasing_sample_get(int sample_index, int sample_count)
void object_sync(ObjectRef &ob_ref, Manager &manager) final
struct blender::draw::gpencil::Instance::@250045130013031054234100054276160145124267353263 tobjects
draw::detail::SubPassVector< tVfx > tVfx_Pool
struct blender::draw::gpencil::Instance::@270103242060374244001011226234131367140002277206 sbuffer_tobjects
void antialiasing_accumulate(Manager &manager, float alpha)
gpLight light_data[GPENCIL_LIGHT_BUFFER_LEN]
gpu::Texture * tex_fill[GPENCIL_MATERIAL_BUFFER_LEN]
gpu::Texture * tex_stroke[GPENCIL_MATERIAL_BUFFER_LEN]
gpMaterial mat_data[GPENCIL_MATERIAL_BUFFER_LEN]
::std::unique_ptr< PassSimple > blend_ps
::std::unique_ptr< PassSimple > geom_ps
struct blender::draw::gpencil::tObject::@156157250037170237043104125276354377130037130163 layers
struct blender::draw::gpencil::tObject::@332241055010322366143177356242222270031034105156 vfx
::std::unique_ptr< PassSimple > vfx_ps