Blender V5.3
gpencil_render.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
9#include "BLI_math_geom_c.hh"
10#include "BLI_math_matrix_c.hh"
11#include "BLI_rect.hh"
12
13#include "BKE_colortools.hh"
14
15#include "DRW_render.hh"
16
17#include "BKE_object.hh"
18
20
21#include "RE_engine.h"
22#include "RE_pipeline.h"
23#include "render_types.h"
24
25#include "IMB_imbuf_types.hh"
26#include "IMB_partial_update.hh"
27
29
30namespace blender::draw::gpencil {
31
32/* Remap depth from views-pace to [0..1] to be able to use it with as GPU depth buffer. */
33static void remap_depth(const View &view, MutableSpan<float> pix_z)
34{
35 if (view.is_persp()) {
36 const float4x4 &winmat = view.winmat();
37 for (auto &pix : pix_z) {
38 pix = (-winmat[3][2] / -pix) - winmat[2][2];
39 pix = clamp_f(pix * 0.5f + 0.5f, 0.0f, 1.0f);
40 }
41 }
42 else {
43 /* Keep in mind, near and far distance are negatives. */
44 const float near = view.near_clip();
45 const float far = view.far_clip();
46 const float range_inv = 1.0f / fabsf(far - near);
47 for (auto &pix : pix_z) {
48 pix = (pix + near) * range_inv;
49 pix = clamp_f(pix, 0.0f, 1.0f);
50 }
51 }
52}
53
54static void render_set_view(RenderEngine *engine,
55 const Depsgraph *depsgraph,
56 const float2 aa_offset = float2{0.0f})
57{
58 Object *camera = DEG_get_evaluated(depsgraph, RE_GetCamera(engine->re));
59
60 float4x4 winmat, viewinv;
61 RE_GetCameraWindow(engine->re, camera, winmat.ptr());
62 RE_GetCameraModelMatrix(engine->re, camera, viewinv.ptr());
63
64 window_translate_m4(winmat.ptr(), winmat.ptr(), UNPACK2(aa_offset));
65
66 View::default_set(math::invert(viewinv), winmat);
67}
68
69static void render_init_buffers(const DRWContext *draw_ctx,
70 Instance &inst,
71 RenderEngine *engine,
72 RenderLayer *render_layer,
73 const rcti *rect,
74 const bool use_separated_pass)
75{
76 const int2 size = int2(draw_ctx->viewport_size_get());
77 View &view = View::default_get();
78
79 /* Create depth texture & color texture from render result. */
80 const char *viewname = RE_GetActiveRenderView(engine->re);
81 RenderPass *rpass_z_src = RE_pass_find_by_name(render_layer, RE_PASSNAME_DEPTH, viewname);
82 RenderPass *rpass_col_src = RE_pass_find_by_name(render_layer, RE_PASSNAME_COMBINED, viewname);
83
84 float *pix_z = (rpass_z_src) ? rpass_z_src->ibuf->float_data_for_write() : nullptr;
85 float *pix_col = (rpass_col_src) ? rpass_col_src->ibuf->float_data_for_write() : nullptr;
86
87 if (!pix_z || !pix_col) {
89 "Warning: To correctly render occluded Grease Pencil objects, "
90 "enable Combined and Depth passes.");
91 }
92
93 if (pix_z) {
94 /* Depth need to be remapped to [0..1] range. */
95 pix_z = MEM_dupalloc(pix_z);
96 remap_depth(view, {pix_z, rpass_z_src->rectx * rpass_z_src->recty});
97 }
98
99 const bool has_full_rect = (rect->xmin == 0 && rect->ymin == 0 && rect->xmax == size.x &&
100 rect->ymax == size.y);
101 const bool do_region = !use_separated_pass && !has_full_rect;
102 const bool do_clear_z = !pix_z || do_region;
103 const bool do_clear_col = use_separated_pass || (!pix_col) || do_region;
104
105 /* FIXME(fclem): we have a precision loss in the depth buffer because of this re-upload.
106 * Find where it comes from! */
107 /* In multi view render the textures can be reused. */
108 if (inst.render_depth_tx.is_valid() && !do_clear_z && has_full_rect) {
110 }
111 else {
115 gpu::TextureFormat::SFLOAT_32_DEPTH, int2(size), usage, do_region ? nullptr : pix_z);
116 }
117 if (inst.render_color_tx.is_valid() && !do_clear_col && has_full_rect) {
119 }
120 else {
124 gpu::TextureFormat::SFLOAT_16_16_16_16, int2(size), usage, do_region ? nullptr : pix_col);
125 }
126
129
130 if (do_clear_z || do_clear_col) {
131 /* To avoid unpredictable result, clear buffers that have not be initialized. */
133 if (do_clear_col) {
134 GPU_framebuffer_clear_color(inst.render_fb, {0.0, 0.0, 0.0, 0.0});
135 }
136 if (do_clear_z) {
138 }
139 }
140
141 if (do_region) {
142 int x = rect->xmin;
143 int y = rect->ymin;
144 int w = BLI_rcti_size_x(rect);
145 int h = BLI_rcti_size_y(rect);
146 if (pix_col) {
147 GPU_texture_update_sub(inst.render_color_tx, GPU_DATA_FLOAT, pix_col, x, y, 0, w, h, 0);
148 }
149 if (pix_z) {
150 GPU_texture_update_sub(inst.render_depth_tx, GPU_DATA_FLOAT, pix_z, x, y, 0, w, h, 0);
151 }
152 }
153
154 MEM_SAFE_DELETE(pix_z);
155}
156
157static void render_result_z(const DRWContext *draw_ctx,
158 RenderLayer *rl,
159 const char *viewname,
160 Instance &instance,
161 const rcti *rect)
162{
163 ViewLayer *view_layer = draw_ctx->view_layer;
164 if ((view_layer->passflag & SCE_PASS_DEPTH) == 0) {
165 return;
166 }
168 if (rp == nullptr) {
169 return;
170 }
171
172 float *ro_buffer_data = rp->ibuf->float_data_for_write();
173
175 rect->xmin,
176 rect->ymin,
177 BLI_rcti_size_x(rect),
178 BLI_rcti_size_y(rect),
180 ro_buffer_data);
181
182 float4x4 winmat = View::default_get().winmat();
183
184 int pix_num = BLI_rcti_size_x(rect) * BLI_rcti_size_y(rect);
185
186 /* Convert GPU depth [0..1] to view Z [near..far] */
187 if (View::default_get().is_persp()) {
188 for (int i = 0; i < pix_num; i++) {
189 if (ro_buffer_data[i] == 1.0f) {
190 ro_buffer_data[i] = 1e10f; /* Background */
191 }
192 else {
193 ro_buffer_data[i] = ro_buffer_data[i] * 2.0f - 1.0f;
194 ro_buffer_data[i] = winmat[3][2] / (ro_buffer_data[i] + winmat[2][2]);
195 }
196 }
197 }
198 else {
199 /* Keep in mind, near and far distance are negatives. */
200 float near = View::default_get().near_clip();
201 float far = View::default_get().far_clip();
202 float range = fabsf(far - near);
203
204 for (int i = 0; i < pix_num; i++) {
205 if (ro_buffer_data[i] == 1.0f) {
206 ro_buffer_data[i] = 1e10f; /* Background */
207 }
208 else {
209 ro_buffer_data[i] = ro_buffer_data[i] * range - near;
210 }
211 }
212 }
213
215}
216
217static void render_result_color(ImBuf *ibuf, Instance &instance, const rcti *rect)
218{
219 Framebuffer read_fb;
221 GPU_framebuffer_bind(read_fb);
223 rect->xmin,
224 rect->ymin,
225 BLI_rcti_size_x(rect),
226 BLI_rcti_size_y(rect),
227 4,
228 0,
230 ibuf->float_data_for_write());
231
233}
234
235/* This is taken from eevee::Sampling::cdf_from_curvemapping. */
236static void cdf_from_curvemapping(const CurveMapping &curve, Array<float> &cdf)
237{
238 BLI_assert(cdf.size() > 1);
239 cdf[0] = 0.0f;
240 /* Actual CDF evaluation. */
241 for (const int u : IndexRange(cdf.size() - 1)) {
242 const float x = float(u + 1) / float(cdf.size() - 1);
243 cdf[u + 1] = cdf[u] + BKE_curvemapping_evaluateF(&curve, 0, x);
244 }
245 /* Normalize the CDF. */
246 for (const int u : cdf.index_range()) {
247 cdf[u] /= cdf.last();
248 }
249 /* Just to make sure. */
250 cdf.last() = 1.0f;
251}
252
253/* This is taken from eevee::Sampling::cdf_invert. */
254static void cdf_invert(Array<float> &cdf, Array<float> &inverted_cdf)
255{
256 BLI_assert(cdf.first() == 0.0f && cdf.last() == 1.0f);
257 for (const int u : inverted_cdf.index_range()) {
258 const float x = clamp_f(u / float(inverted_cdf.size() - 1), 1e-5f, 1.0f - 1e-5f);
259 for (const int i : cdf.index_range().drop_front(1)) {
260 if (cdf[i] >= x) {
261 const float t = (x - cdf[i]) / (cdf[i] - cdf[i - 1]);
262 inverted_cdf[u] = (float(i) + t) / float(cdf.size() - 1);
263 break;
264 }
265 }
266 }
267}
268
269/* This is taken from eevee::MotionBlurModule::shutter_time_to_scene_time. */
270static float shutter_time_to_scene_time(const int shutter_position,
271 const float shutter_time,
272 const float frame_time,
273 float time)
274{
275 switch (shutter_position) {
276 case SCE_MB_START:
277 /* No offset. */
278 break;
279 case SCE_MB_CENTER:
280 time -= 0.5f;
281 break;
282 case SCE_MB_END:
283 time -= 1.0;
284 break;
285 default:
286 BLI_assert_msg(false, "Invalid motion blur position enum!");
287 break;
288 }
289 time *= shutter_time;
290 time += frame_time;
291 return time;
292}
293
294static void render_frame(RenderEngine *engine,
295 Depsgraph *depsgraph,
296 const DRWContext *draw_ctx,
297 RenderLayer *render_layer,
298 const rcti rect,
299 gpencil::Instance &inst,
300 Manager &manager,
301 const bool separated_pass)
302{
303 Scene *scene = draw_ctx->scene;
304
305 const float aa_radius = clamp_f(scene->r.gauss, 0.0f, 100.0f);
306
307 const bool motion_blur_enabled = (scene->r.mode & R_MBLUR) != 0 &&
308 (draw_ctx->view_layer->layflag & SCE_LAY_MOTION_BLUR) != 0 &&
310
311 const int motion_steps_count =
312 motion_blur_enabled ? max_ii(1, scene->grease_pencil_settings.motion_blur_steps) * 2 + 1 : 1;
313 const int total_step_count = ceil_to_multiple_u(scene->grease_pencil_settings.aa_samples,
314 motion_steps_count);
315 const int aa_per_step = total_step_count / motion_steps_count;
316
317 const int shutter_position = scene->r.motion_blur_position;
318 const float shutter_time = scene->r.motion_blur_shutter;
319
320 const int initial_frame = scene->r.cfra;
321 const float initial_subframe = scene->r.subframe;
322 const float frame_time = initial_frame + initial_subframe;
323
324 Array<float> time_steps(motion_steps_count);
325 if (motion_blur_enabled) {
327
330 cdf_invert(cdf, time_steps);
331
332 for (float &scene_time : time_steps) {
333 scene_time = shutter_time_to_scene_time(
334 shutter_position, shutter_time, frame_time, scene_time);
335 }
336 }
337 else {
338 BLI_assert(time_steps.size() == 1);
339 time_steps.first() = frame_time;
340 }
341
342 int sample_i = 0;
343 for (const float time : time_steps) {
344 inst.init();
345
346 if (motion_blur_enabled) {
347 DRW_render_set_time(engine, depsgraph, floorf(time), fractf(time));
348 }
349
350 inst.camera = DEG_get_evaluated(depsgraph, RE_GetCamera(engine->re));
351
352 manager.begin_sync();
353
354 /* Loop over all objects and create draw structure. */
355 inst.begin_sync();
356 DRW_render_object_iter(engine, depsgraph, [&](ObjectRef &ob_ref, RenderEngine *, Depsgraph *) {
357 if (!ELEM(ob_ref.object->type, OB_GREASE_PENCIL, OB_LAMP)) {
358 return;
359 }
361 return;
362 }
363 inst.object_sync(ob_ref, manager);
364 });
365 inst.end_sync();
366
367 manager.end_sync();
368
369 for ([[maybe_unused]] const int i : IndexRange(aa_per_step)) {
370 const float2 aa_sample = Instance::antialiasing_sample_get(sample_i, total_step_count) *
371 aa_radius;
372 const float2 aa_offset = 2.0f * aa_sample / float2(inst.render_color_tx.size());
373 render_set_view(engine, depsgraph, aa_offset);
374 render_init_buffers(draw_ctx, inst, engine, render_layer, &rect, separated_pass);
375
376 /* Render the gpencil object and merge the result to the underlying render. */
377 inst.draw(manager);
378
379 /* Weight of this render SSAA sample. The sum of previous samples is weighted by
380 * `1 - weight`. This diminishes after each new sample as we want all samples to be equally
381 * weighted inside the final result (inside the combined buffer). This weighting scheme
382 * allows to always store the resolved result making it ready for in-progress display or
383 * read-back. */
384 const float weight = 1.0f / (1.0f + sample_i);
385 inst.antialiasing_accumulate(manager, weight);
386
387 sample_i++;
388 }
389 }
390
391 if (motion_blur_enabled) {
392 /* Restore original frame number. This is because the render pipeline expects it. */
393 RE_engine_frame_set(engine, initial_frame, initial_subframe);
394 }
395}
396
397void Engine::render_to_image(RenderEngine *engine, RenderLayer *render_layer, const rcti rect)
398{
399 const char *viewname = RE_GetActiveRenderView(engine->re);
400
401 const DRWContext *draw_ctx = DRW_context_get();
402 Depsgraph *depsgraph = draw_ctx->depsgraph;
403
405 Render *re = engine->re;
407 re->result, RE_PASSNAME_GREASE_PENCIL, 4, "RGBA", render_layer->name, viewname, true);
408 }
409
411
412 Manager &manager = *DRW_manager_get();
413
414 render_set_view(engine, depsgraph);
415 render_init_buffers(draw_ctx, inst, engine, render_layer, &rect, false);
416
417 render_frame(engine, depsgraph, draw_ctx, render_layer, rect, inst, manager, false);
418 ImBuf *combined_ibuf = RE_RenderLayerGetPassImBuf(render_layer, RE_PASSNAME_COMBINED, viewname);
419 render_result_color(combined_ibuf, inst, &rect);
420
421 ImBuf *gpencil_ibuf = RE_RenderLayerGetPassImBuf(
422 render_layer, RE_PASSNAME_GREASE_PENCIL, viewname);
423 if (gpencil_ibuf) {
424 render_frame(engine, depsgraph, draw_ctx, render_layer, rect, inst, manager, true);
425 render_result_color(gpencil_ibuf, inst, &rect);
426 }
427
428 /* Transfer depth in the last step, because if we need to render separate pass, we need original
429 * untouched depth buffer. */
430 render_result_z(draw_ctx, render_layer, viewname, inst, &rect);
431}
432
433} // namespace blender::draw::gpencil
General operations, lookup, etc. for blender objects.
#define CM_TABLE
#define RE_PASSNAME_COMBINED
#define RE_PASSNAME_DEPTH
#define RE_PASSNAME_GREASE_PENCIL
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define UNPACK2(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
int64_t size() const
Definition BLI_array.hh:267
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
const T & first() const
Definition BLI_array.hh:292
constexpr IndexRange()=default
constexpr IndexRange drop_front(int64_t n) const
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)
void begin_sync(Object *object_active=nullptr)
bool ensure_2d(gpu::TextureFormat format, int2 extent, eGPUTextureUsage usage=GPU_TEXTURE_USAGE_GENERAL, const float *data=nullptr, int mip_len=1)
int3 size(int miplvl=0) const
static void default_set(const float4x4 &view_mat, const float4x4 &win_mat)
Definition draw_view.cc:322
bool is_persp(int view_id=0) const
Definition draw_view.hh:93
const float4x4 & winmat(int view_id=0) const
Definition draw_view.hh:148
static View & default_get()
Definition draw_view.cc:317
float near_clip(int view_id=0) const
Definition draw_view.hh:115
float far_clip(int view_id=0) const
Definition draw_view.hh:106
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
static void render_set_view(RenderEngine *engine, const Depsgraph *depsgraph, const float2 aa_offset=float2{0.0f})
static void cdf_invert(Array< float > &cdf, Array< float > &inverted_cdf)
static void render_frame(RenderEngine *engine, Depsgraph *depsgraph, const DRWContext *draw_ctx, RenderLayer *render_layer, const rcti rect, gpencil::Instance &inst, Manager &manager, const bool separated_pass)
static void render_result_color(ImBuf *ibuf, Instance &instance, const rcti *rect)
static void remap_depth(const View &view, MutableSpan< float > pix_z)
static void cdf_from_curvemapping(const CurveMapping &curve, Array< float > &cdf)
static float shutter_time_to_scene_time(const int shutter_position, const float shutter_time, const float frame_time, float time)
static void render_init_buffers(const DRWContext *draw_ctx, Instance &inst, RenderEngine *engine, RenderLayer *render_layer, const rcti *rect, const bool use_separated_pass)
static void render_result_z(const DRWContext *draw_ctx, RenderLayer *rl, const char *viewname, Instance &instance, const rcti *rect)
CartesianBasis invert(const CartesianBasis &basis)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_framebuffer_read_color(gpu::FrameBuffer *fb, int x, int y, int width, int height, int channels, int slot, eGPUDataFormat data_format, void *r_data)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
MINLINE float fractf(float a)
void window_translate_m4(float winmat[4][4], float perspmat[4][4], float x, float y)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
void RE_GetCameraWindow(Render *re, const Object *camera, float r_winmat[4][4])
Definition initrender.cc:62
ImBuf * RE_RenderLayerGetPassImBuf(RenderLayer *rl, const char *name, const char *viewname)
MINLINE int max_ii(int a, int b)
@ GREASE_PENCIL_AS_SEPARATE_PASS
MatBase< float, 4, 4 > float4x4
int DRW_object_visibility_in_active_context(const Object *ob)
void DRW_render_object_iter(RenderEngine *engine, Depsgraph *depsgraph, std::function< void(draw::ObjectRef &, RenderEngine *, Depsgraph *)> callback)
void DRW_render_set_time(RenderEngine *engine, Depsgraph *depsgraph, int frame, float subframe)
void GPU_texture_update_sub(gpu::Texture *texture, eGPUDataFormat data_format, const void *pixels, int offset_x, int offset_y, int offset_z, int width, int height, int depth, uint unpack_row_length=0)
const char * RE_GetActiveRenderView(Render *re)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void RE_GetCameraModelMatrix(const Render *re, const Object *camera, float r_modelmat[4][4])
Definition initrender.cc:74
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void BKE_curvemapping_changed(CurveMapping *cumap, bool rem_doubles)
void IMB_partial_update_mark_full(ImBuf *ibuf)
const DRWContext * DRW_context_get()
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void RE_engine_frame_set(RenderEngine *engine, int frame, float subframe)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
RenderPass * RE_pass_find_by_name(RenderLayer *rl, const char *name, const char *viewname)
void RE_engine_set_error_message(RenderEngine *engine, const char *msg)
void RE_create_render_pass(RenderResult *rr, const char *name, int channels, const char *chan_id, const char *layername, const char *viewname, const bool allocate)
draw::Manager * DRW_manager_get()
MINLINE uint ceil_to_multiple_u(uint a, uint b)
void GPU_framebuffer_read_depth(gpu::FrameBuffer *fb, int x, int y, int width, int height, eGPUDataFormat data_format, void *r_data)
float BKE_curvemapping_evaluateF(const CurveMapping *cumap, int cur, float value)
@ OB_VISIBLE_SELF
MINLINE float clamp_f(float value, float min, float max)
Object * RE_GetCamera(Render *re)
Definition initrender.cc:31
#define floorf
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
RenderResult * result
ViewLayer * view_layer
Depsgraph * depsgraph
float2 viewport_size_get() const
float * float_data_for_write()
Definition allocimbuf.cc:79
const c_style_mat & ptr() const
struct CurveMapping mblur_shutter_curve
struct Render * re
Definition RE_engine.h:148
char name[RE_MAXNAME]
Definition RE_pipeline.h:94
struct ImBuf * ibuf
Definition RE_pipeline.h:72
struct RenderData r
struct SceneGpencil grease_pencil_settings
eViewLayerGreasePencilFlags grease_pencil_flags
static void render_to_image(RenderEngine *engine, RenderLayer *render_layer, const rcti rect)
void draw(Manager &manager) final
static float2 antialiasing_sample_get(int sample_index, int sample_count)
void object_sync(ObjectRef &ob_ref, Manager &manager) final
void antialiasing_accumulate(Manager &manager, float alpha)