Blender V5.3
image_drawing_mode_screen_space.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "draw_view_data.hh"
10
12#include "image_instance.hh"
13#include "image_shader.hh"
14
15#include "BKE_image.hh"
16
17#include "IMB_partial_update.hh"
18
19namespace blender::image_engine {
20
21void ScreenSpaceDrawingMode::add_shgroups() const
22{
23 PassSimple &pass = instance_.state.image_ps;
24 gpu::Shader *shader = ShaderModule::module_get().color.get();
25 const ShaderParameters &sh_params = instance_.state.sh_params;
26 DefaultTextureList *dtxl = DRW_context_get()->viewport_texture_list_get();
27
28 pass.shader_set(shader);
29 pass.push_constant("far_near_distances", sh_params.far_near);
30 pass.push_constant("shuffle", sh_params.shuffle);
31 pass.push_constant("draw_flags", int32_t(sh_params.flags));
32 pass.push_constant("is_image_premultiplied", sh_params.use_premul_alpha);
33 pass.bind_texture("depth_tx", dtxl->depth);
34
35 float4x4 image_mat = float4x4::identity();
36 ResourceHandleRange handle = instance_.manager->resource_handle(image_mat);
37 for (const TextureInfo &info : instance_.state.texture_infos) {
38 PassSimple::Sub &sub = pass.sub("Texture");
39 sub.push_constant("offset", info.offset());
40 sub.bind_texture("image_tx", info.texture);
41 sub.draw(info.batch, handle);
42 }
43}
44
45void ScreenSpaceDrawingMode::add_depth_shgroups(blender::Image *image, ImageUser *image_user) const
46{
47 PassSimple &pass = instance_.state.depth_ps;
48 gpu::Shader *shader = ShaderModule::module_get().depth.get();
49 pass.shader_set(shader);
50
51 float4x4 image_mat = float4x4::identity();
52 ResourceHandleRange handle = instance_.manager->resource_handle(image_mat);
53
54 ImageUser tile_user = {nullptr};
55 if (image_user) {
56 tile_user = *image_user;
57 }
58
59 for (const TextureInfo &info : instance_.state.texture_infos) {
60 for (ImageTile &image_tile_ptr : image->tiles) {
61 const ImageTileWrapper image_tile(&image_tile_ptr);
62 const int tile_x = image_tile.get_tile_x_offset();
63 const int tile_y = image_tile.get_tile_y_offset();
64 tile_user.tile = image_tile.get_tile_number();
65
66 /* NOTE: `BKE_image_has_ibuf` doesn't work as it fails for render results. That could be a
67 * bug or a feature. For now we just acquire to determine if there is a texture. */
68 void *lock;
69 ImBuf *tile_buffer = BKE_image_acquire_ibuf(image, &tile_user, &lock);
70 if (tile_buffer != nullptr) {
71 instance_.state.float_buffers.mark_used(tile_buffer);
72 PassSimple::Sub &sub = pass.sub("Tile");
73 float4 min_max_uv(tile_x, tile_y, tile_x + 1, tile_y + 1);
74 sub.push_constant("min_max_uv", min_max_uv);
75 sub.draw(info.batch, handle);
76 }
77 BKE_image_release_ibuf(image, tile_buffer, lock);
78 }
79 }
80}
81
82void ScreenSpaceDrawingMode::update_textures(blender::Image *image, ImageUser *image_user) const
83{
84 using namespace blender::imbuf::partial_update;
85 State &state = instance_.state;
86
87 ImageUser tile_user = {};
88 if (image_user) {
89 tile_user = *image_user;
90 }
91
92 /* Get changeset ID that we will update to, and last changeset ID. */
93 const imbuf::ChangesetID new_changeset_id = BKE_image_partial_update_flush(image, image_user);
94 const imbuf::ChangesetID last_changset_id = state.partial_update.last_changeset_id;
95
96 bool need_full_update = false;
97 for (ImageTile &image_tile_ptr : image->tiles) {
98 const ImageTileWrapper image_tile(&image_tile_ptr);
99 tile_user.tile = image_tile.get_tile_number();
100
101 void *lock;
102 ImBuf *tile_buffer = BKE_image_acquire_ibuf(image, &tile_user, &lock);
103 if (tile_buffer == nullptr) {
104 BKE_image_release_ibuf(image, tile_buffer, lock);
105 continue;
106 }
107
108 const Changes changes = IMB_partial_update_collect(tile_buffer, last_changset_id);
109 switch (changes.kind) {
110 case Changes::Kind::Full:
111 case Changes::Kind::Resized:
112 need_full_update = true;
113 break;
114 case Changes::Kind::Partial:
115 /* Partial update when wrap repeat is enabled is not supported. */
116 if (state.flags.do_tile_drawing) {
117 need_full_update = true;
118 }
119 else {
120 ImBuf *float_buffer = state.float_buffers.cached_float_buffer(tile_buffer);
121 for (const rcti &region : changes.modified_regions()) {
122 if (float_buffer != tile_buffer) {
123 do_partial_update_float_buffer(float_buffer, tile_buffer, region);
124 }
125 apply_partial_change(float_buffer, image_tile, region);
126 }
127 }
128 break;
129 case Changes::Kind::None:
130 break;
131 }
132 BKE_image_release_ibuf(image, tile_buffer, lock);
133 }
134
135 state.partial_update.last_changeset_id = new_changeset_id;
136
137 if (need_full_update) {
138 state.float_buffers.clear();
139 state.mark_all_texture_slots_dirty();
140 }
141 do_full_update_for_dirty_textures(image_user);
142}
143
144void ScreenSpaceDrawingMode::do_partial_update_float_buffer(ImBuf *float_buffer,
145 ImBuf *src,
146 const rcti &region) const
147{
148 BLI_assert(float_buffer->float_data() != nullptr);
149 BLI_assert(float_buffer->byte_data() == nullptr);
150 BLI_assert(src->float_data() == nullptr);
151 BLI_assert(src->byte_data() != nullptr);
152
153 /* Calculate the overlap between the updated region and the buffer size. A region is
154 * chunk-aligned (CHUNK_SIZE) and could lay partially outside the buffer when using different
155 * resolutions. */
156 rcti buffer_rect;
157 BLI_rcti_init(&buffer_rect, 0, float_buffer->x, 0, float_buffer->y);
158 rcti clipped_update_region;
159 const bool has_overlap = BLI_rcti_isect(&buffer_rect, &region, &clipped_update_region);
160 if (!has_overlap) {
161 return;
162 }
163
164 IMB_float_from_byte_ex(float_buffer, src, &clipped_update_region);
165}
166
167void ScreenSpaceDrawingMode::apply_partial_change(ImBuf *src_buffer,
168 const ImageTileWrapper &image_tile,
169 const rcti &region) const
170{
171 const float tile_width = float(src_buffer->x);
172 const float tile_height = float(src_buffer->y);
173 const float tile_offset_x = float(image_tile.get_tile_x_offset());
174 const float tile_offset_y = float(image_tile.get_tile_y_offset());
175
176 for (const TextureInfo &info : instance_.state.texture_infos) {
177 /* Dirty images will receive a full update. No need to do a partial one now. */
178 if (info.need_full_update) {
179 continue;
180 }
181 gpu::Texture *texture = info.texture;
182 const float texture_width = GPU_texture_width(texture);
183 const float texture_height = GPU_texture_height(texture);
184 /* TODO: early bound check. */
185 rctf changed_region_in_uv_space;
186 BLI_rctf_init(&changed_region_in_uv_space,
187 float(region.xmin) / tile_width + tile_offset_x,
188 float(region.xmax) / tile_width + tile_offset_x,
189 float(region.ymin) / tile_height + tile_offset_y,
190 float(region.ymax) / tile_height + tile_offset_y);
191 rctf changed_overlapping_region_in_uv_space;
192 const bool region_overlap = BLI_rctf_isect(&info.clipping_uv_bounds,
193 &changed_region_in_uv_space,
194 &changed_overlapping_region_in_uv_space);
195 if (!region_overlap) {
196 continue;
197 }
198 /* Convert the overlapping region to texel space and to ss_pixel space...
199 * TODO: first convert to ss_pixel space as integer based. and from there go back to texel
200 * space. But perhaps this isn't needed and we could use an extraction offset somehow. */
201 rcti gpu_texture_region_to_update;
203 &gpu_texture_region_to_update,
204 floor((changed_overlapping_region_in_uv_space.xmin - info.clipping_uv_bounds.xmin) *
205 texture_width / BLI_rctf_size_x(&info.clipping_uv_bounds)),
206 floor((changed_overlapping_region_in_uv_space.xmax - info.clipping_uv_bounds.xmin) *
207 texture_width / BLI_rctf_size_x(&info.clipping_uv_bounds)),
208 ceil((changed_overlapping_region_in_uv_space.ymin - info.clipping_uv_bounds.ymin) *
209 texture_height / BLI_rctf_size_y(&info.clipping_uv_bounds)),
210 ceil((changed_overlapping_region_in_uv_space.ymax - info.clipping_uv_bounds.ymin) *
211 texture_height / BLI_rctf_size_y(&info.clipping_uv_bounds)));
212 gpu_texture_region_to_update.xmax = min_ii(gpu_texture_region_to_update.xmax,
213 info.clipping_bounds.xmax);
214 gpu_texture_region_to_update.ymax = min_ii(gpu_texture_region_to_update.ymax,
215 info.clipping_bounds.ymax);
216
217 /* Create an image buffer with a size.
218 * Extract and scale into an imbuf. */
219 const int texture_region_width = BLI_rcti_size_x(&gpu_texture_region_to_update);
220 const int texture_region_height = BLI_rcti_size_y(&gpu_texture_region_to_update);
221
222 ImBuf extracted_buffer;
224 &extracted_buffer, texture_region_width, texture_region_height, ImBufFlags::FloatData);
225
226 int offset = 0;
227 float *float_data = extracted_buffer.float_data_for_write();
228 for (int y = gpu_texture_region_to_update.ymin; y < gpu_texture_region_to_update.ymax; y++) {
229 float yf = y / float(texture_height);
230 float v = info.clipping_uv_bounds.ymax * yf + info.clipping_uv_bounds.ymin * (1.0 - yf) -
231 tile_offset_y;
232 for (int x = gpu_texture_region_to_update.xmin; x < gpu_texture_region_to_update.xmax; x++) {
233 float xf = x / float(texture_width);
234 float u = info.clipping_uv_bounds.xmax * xf + info.clipping_uv_bounds.xmin * (1.0 - xf) -
235 tile_offset_x;
237 src_buffer, &float_data[offset * 4], u * src_buffer->x, v * src_buffer->y);
238 offset++;
239 }
240 }
241 IMB_gpu_clamp_half_float(&extracted_buffer);
242
245 float_data,
246 gpu_texture_region_to_update.xmin,
247 gpu_texture_region_to_update.ymin,
248 0,
249 extracted_buffer.x,
250 extracted_buffer.y,
251 0);
252 IMB_free_all_data(&extracted_buffer);
253 }
254}
255
256void ScreenSpaceDrawingMode::do_full_update_for_dirty_textures(const ImageUser *image_user) const
257{
258 for (TextureInfo &info : instance_.state.texture_infos) {
259 if (!info.need_full_update) {
260 continue;
261 }
262 do_full_update_gpu_texture(info, image_user);
263 }
264}
265
266void ScreenSpaceDrawingMode::do_full_update_gpu_texture(TextureInfo &info,
267 const ImageUser *image_user) const
268{
269 ImBuf texture_buffer;
270 const int texture_width = GPU_texture_width(info.texture);
271 const int texture_height = GPU_texture_height(info.texture);
272 IMB_initImBuf(&texture_buffer, texture_width, texture_height, ImBufFlags::FloatData);
273 ImageUser tile_user = {nullptr};
274 if (image_user) {
275 tile_user = *image_user;
276 }
277
278 void *lock;
279
280 blender::Image *image = instance_.state.image;
281 for (ImageTile &image_tile_ptr : image->tiles) {
282 const ImageTileWrapper image_tile(&image_tile_ptr);
283 tile_user.tile = image_tile.get_tile_number();
284
285 ImBuf *tile_buffer = BKE_image_acquire_ibuf(image, &tile_user, &lock);
286 if (tile_buffer != nullptr) {
287 do_full_update_texture_slot(info, texture_buffer, *tile_buffer, image_tile);
288 }
289 BKE_image_release_ibuf(image, tile_buffer, lock);
290 }
291 IMB_gpu_clamp_half_float(&texture_buffer);
292 GPU_texture_update(info.texture, GPU_DATA_FLOAT, texture_buffer.float_data());
293 IMB_free_all_data(&texture_buffer);
294}
295
296void ScreenSpaceDrawingMode::do_full_update_texture_slot(const TextureInfo &texture_info,
297 ImBuf &texture_buffer,
298 ImBuf &tile_buffer,
299 const ImageTileWrapper &image_tile) const
300{
301 const int texture_width = texture_buffer.x;
302 const int texture_height = texture_buffer.y;
303 ImBuf *float_tile_buffer = instance_.state.float_buffers.cached_float_buffer(&tile_buffer);
304
305 /* IMB_transform works in a non-consistent space. This should be documented or fixed!.
306 * Construct a variant of the info_uv_to_texture that adds the texel space
307 * transformation. */
308 float3x3 uv_to_texel;
309 rctf texture_area;
310 rctf tile_area;
311
312 BLI_rctf_init(&texture_area, 0.0, texture_width, 0.0, texture_height);
314 &tile_area,
315 tile_buffer.x * (texture_info.clipping_uv_bounds.xmin - image_tile.get_tile_x_offset()),
316 tile_buffer.x * (texture_info.clipping_uv_bounds.xmax - image_tile.get_tile_x_offset()),
317 tile_buffer.y * (texture_info.clipping_uv_bounds.ymin - image_tile.get_tile_y_offset()),
318 tile_buffer.y * (texture_info.clipping_uv_bounds.ymax - image_tile.get_tile_y_offset()));
319 BLI_rctf_transform_calc_m3_pivot_min(&tile_area, &texture_area, uv_to_texel.ptr());
320 uv_to_texel = math::invert(uv_to_texel);
321
322 rctf crop_rect;
323 const rctf *crop_rect_ptr = nullptr;
324 eIMBTransformMode transform_mode;
325 if (instance_.state.flags.do_tile_drawing) {
326 transform_mode = IMB_TRANSFORM_MODE_WRAP_REPEAT;
327 }
328 else {
329 BLI_rctf_init(&crop_rect, 0.0, tile_buffer.x, 0.0, tile_buffer.y);
330 crop_rect_ptr = &crop_rect;
331 transform_mode = IMB_TRANSFORM_MODE_CROP_SRC;
332 }
333
334 IMB_transform(float_tile_buffer,
335 &texture_buffer,
336 transform_mode,
338 uv_to_texel,
339 crop_rect_ptr);
340}
341
343{
344 {
346 instance_.state.depth_fb.ensure(GPU_ATTACHMENT_TEXTURE(dtxl->depth));
347 instance_.state.color_fb.ensure(GPU_ATTACHMENT_NONE, GPU_ATTACHMENT_TEXTURE(dtxl->color));
348 }
349 {
350 PassSimple &pass = instance_.state.image_ps;
351 pass.init();
353 }
354 {
355 PassSimple &pass = instance_.state.depth_ps;
356 pass.init();
358 }
359}
360
362{
363 State &state = instance_.state;
364
365 state.partial_update.ensure_image(image);
366 state.clear_need_full_update_flag();
367
368 /* Step: Find out which screen space textures are needed to draw on the screen. Recycle
369 * textures that are not on screen anymore. */
370 OneTexture method(&state);
371 method.ensure_texture_infos();
372 method.update_bounds(instance_.region);
373
374 /* Step: Check for changes in the image user compared to the last time. */
375 state.update_image_usage(iuser);
376
377 /* Step: Update the GPU textures based on the changes in the image. */
379 update_textures(image, iuser);
380
381 /* Step: Add the GPU textures to the shgroup. */
382 state.update_batches();
383 if (!state.flags.do_tile_drawing) {
384 add_depth_shgroups(image, iuser);
385 }
386 add_shgroups();
387}
388
390{
391 float clear_depth = instance_.state.flags.do_tile_drawing ? 0.75 : 1.0f;
392 GPU_framebuffer_bind(instance_.state.depth_fb);
393 instance_.state.depth_fb.clear_depth(clear_depth);
394 instance_.manager->submit(instance_.state.depth_ps, instance_.state.view);
395
396 GPU_framebuffer_bind(instance_.state.color_fb);
397 GPU_framebuffer_clear_color(instance_.state.color_fb, double4(0.0));
398 instance_.manager->submit(instance_.state.image_ps, instance_.state.view);
399}
400
401} // namespace blender::image_engine
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
volatile int lock
#define BLI_assert(a)
void state_set(DRWState state, int clip_plane_count=0)
detail::PassBase< command::DrawCommandBuf > Sub
Definition draw_pass.hh:509
void update_bounds(const ARegion *region) override
Update the uv and region bounds of all texture_infos of instance_data.
void ensure_texture_infos() override
Ensure enough texture infos are allocated in instance_data.
void image_sync(blender::Image *image, blender::ImageUser *iuser) const override
static ShaderModule & module_get()
#define image(slot, qualifiers, format)
#define floor
#define ceil
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const int state
detail::Pass< command::DrawCommandBuf > PassSimple
int tile_height(const AssetShelfSettings &settings)
int tile_width(const AssetShelfSettings &settings)
float4 interpolate_nearest_border_fl(const ImBuf *in, float u, float v)
Definition IMB_interp.hh:29
CartesianBasis invert(const CartesianBasis &basis)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void IMB_float_from_byte_ex(ImBuf *dst, const ImBuf *src, const rcti *region_to_update)
void GPU_texture_update(gpu::Texture *texture, eGPUDataFormat data_format, const void *data)
bool BLI_rcti_isect(const struct rcti *src1, const struct rcti *src2, struct rcti *dest)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MatBase< float, 4, 4 > float4x4
bool IMB_initImBuf(ImBuf *ibuf, unsigned int x, unsigned int y, ImBufFlags flags)
VecBase< float, 4 > float4
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)
void IMB_transform(const ImBuf *src, ImBuf *dst, eIMBTransformMode mode, eIMBInterpolationFilterMode filter, const float3x3 &transform_matrix, const rctf *src_crop)
Transform source image buffer onto destination image buffer using a transform matrix.
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void IMB_gpu_clamp_half_float(ImBuf *image_buffer)
Definition util_gpu.cc:866
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
int GPU_texture_width(const gpu::Texture *texture)
void BLI_rctf_transform_calc_m3_pivot_min(const rctf *dst, const rctf *src, float matrix[3][3])
Definition rct.cc:570
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_BLEND_ALPHA_PREMUL
Definition draw_state.hh:59
@ DRW_STATE_DEPTH_ALWAYS
Definition draw_state.hh:38
@ DRW_STATE_DEPTH_LESS_EQUAL
Definition draw_state.hh:40
@ IMB_FILTER_NEAREST
Definition IMB_imbuf.hh:338
const DRWContext * DRW_context_get()
MatBase< float, 3, 3 > float3x3
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
int64_t BKE_image_partial_update_flush(Image *ima, const ImageUser *iuser)
Definition image_gpu.cc:867
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.cc:428
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
int GPU_texture_height(const gpu::Texture *texture)
bool BLI_rctf_isect(const struct rctf *src1, const struct rctf *src2, struct rctf *dest)
void IMB_free_all_data(ImBuf *ibuf)
signed int int32_t
Definition BKE_text.h:78
imbuf::partial_update::Changes IMB_partial_update_collect(ImBuf *ibuf, imbuf::ChangesetID last_changeset_id)
MINLINE int min_ii(int a, int b)
VecBase< double, 4 > double4
eIMBTransformMode
Transform modes to use for IMB_transform function.
Definition IMB_imbuf.hh:576
@ IMB_TRANSFORM_MODE_WRAP_REPEAT
Wrap repeat the source buffer. Only supported in with nearest filtering.
Definition IMB_imbuf.hh:582
@ IMB_TRANSFORM_MODE_CROP_SRC
Crop the source buffer.
Definition IMB_imbuf.hh:580
void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax)
Definition rct.cc:418
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
DefaultTextureList * viewport_texture_list_get() const
const uint8_t * byte_data() const
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79