Blender V5.3
image_instance.hh
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#pragma once
10
11#include <DRW_render.hh>
12
13#include "BKE_context.hh"
14#include "BKE_image_gpu.hh"
15
16#include "GPU_capabilities.hh"
17
18#include "DRW_engine.hh"
19#include "draw_view_data.hh"
20
23#include "image_private.hh"
24#include "image_space.hh"
25#include "image_space_image.hh"
26#include "image_space_node.hh"
27
28#include "BLI_math_matrix.hh"
29
30#include "DNA_space_types.h"
31
32namespace blender::image_engine {
33
34static inline std::unique_ptr<AbstractSpaceAccessor> space_accessor_from_space(
35 SpaceLink *space_link)
36{
37 if (space_link->spacetype == SPACE_IMAGE) {
38 return std::make_unique<SpaceImageAccessor>(
39 static_cast<SpaceImage *>(static_cast<void *>(space_link)));
40 }
41 if (space_link->spacetype == SPACE_NODE) {
42 return std::make_unique<SpaceNodeAccessor>(
43 static_cast<SpaceNode *>(static_cast<void *>(space_link)));
44 }
46 return nullptr;
47}
48
49class Instance : public DrawEngine {
50 private:
51 std::unique_ptr<AbstractSpaceAccessor> space_;
52 std::unique_ptr<AbstractDrawingMode> drawing_mode_;
53 Main *main_;
54
55 public:
58 Manager *manager = nullptr;
59
60 public:
62 {
63 return "UV/Image";
64 }
65
66 void init() final
67 {
68 const DRWContext *ctx_state = DRW_context_get();
69 main_ = CTX_data_main(ctx_state->evil_C);
70 region = ctx_state->region;
71 space_ = space_accessor_from_space(ctx_state->space_data);
73 }
74
75 /* Constructs either a screen space or an image space drawing mode depending on if the image can
76 * fit in a GPU texture. So we just need to retrieve the image buffer and check if its size is
77 * safe for GPU use. */
78 std::unique_ptr<AbstractDrawingMode> get_drawing_mode()
79 {
80 if (this->state.image->source != IMA_SRC_TILED) {
81 /* If the full image resolution can fit in a GPU texture, draw it directly.
82 * Otherwise fall back to slower tiled screen space drawing. */
83 const bool only_full_resolution = true;
85 this->state.image, space_->get_image_user(), only_full_resolution);
86 if (texture) {
87 const bool texture_owned = true;
88 return std::make_unique<ImageSpaceDrawingMode>(*this, texture, nullptr, texture_owned);
89 }
90
91 return std::make_unique<ScreenSpaceDrawingMode>(*this);
92 }
93
94 for (ImageTile &tile : this->state.image->tiles) {
95 ImageTileWrapper image_tile(&tile);
96 ImageUser tile_user = space_->get_image_user() ? *space_->get_image_user() :
97 ImageUser{.scene = nullptr};
98 tile_user.tile = image_tile.get_tile_number();
99 ImBuf *buffer = BKE_image_acquire_ibuf_gpu(this->state.image, &tile_user, nullptr);
100 BLI_SCOPED_DEFER([&]() { BKE_image_release_ibuf(this->state.image, buffer, nullptr); });
101 if (!buffer) {
102 continue;
103 }
104
105 /* Image will not fit in a GPU texture, use screen space drawing. */
106 if (!GPU_is_safe_texture_size(buffer->x, buffer->y)) {
107 return std::make_unique<ScreenSpaceDrawingMode>(*this);
108 }
109
110 /* GPU drawing will limit image resolution due to the GPU back-end having a lower maximum
111 * texture size or a resolution limit in the preferences, so use screen space drawing to get
112 * the full resolution. */
113 if (GPU_texture_size_with_limit(buffer->x) != buffer->x ||
114 GPU_texture_size_with_limit(buffer->y) != buffer->y)
115 {
116 return std::make_unique<ScreenSpaceDrawingMode>(*this);
117 }
118 }
119
120 /* Image can fit in a GPU texture, use image space drawing. */
122 this->state.image, space_->get_image_user(), true, false);
123 const bool texture_owned = true;
124 auto mode = std::make_unique<ImageSpaceDrawingMode>(
125 *this, gpu_tiles_textures.texture, gpu_tiles_textures.tile_mapping, texture_owned);
126 return mode;
127 }
128
130 {
131 /* Setup full screen view matrix. */
133 0.0f, float(region->winx), 0.0f, float(region->winy), 0.0f, 1.0f);
134 float4x4 winmat = float4x4::identity();
135 state.view.sync(viewmat, winmat);
136 state.flags.do_tile_drawing = false;
137
138 this->image_sync();
139 drawing_mode_.reset();
140 this->state.float_buffers.reset_usage_flags();
141 if (this->state.image) {
142 this->drawing_mode_ = this->get_drawing_mode();
143 drawing_mode_->begin_sync();
144 drawing_mode_->image_sync(state.image, space_->get_image_user());
145 }
146 }
147
148 /* Computes a transformation matrix from the normalized screen space coordinates with half pixel
149 * offsets into the image sampler space. */
151 const float2 image_offset,
152 const float2 image_size,
153 const float2 pan_offset,
154 const float zoom,
155 const float aspect_ratio) const
156 {
157 /* Transforms output normalized screen coordinates with half pixel offsets into integer data
158 * coordinates. */
159 const float3x3 output_screen_uv_to_output_texel = math::from_scale<float3x3, 2>(output_size);
160 const float3x3 output_texel_to_output_data = math::from_location<float3x3>(float2(-0.5f));
161
162 /* Transforms output data coordinates into the centered virtual space. */
163 const float2 output_center = float2(output_size) / 2.0f;
164 const float2 output_translation = -output_center;
165 const float3x3 output_data_to_virtual = math::from_location<float3x3>(output_translation);
166
167 /* Transform the image in pixel space based on the pan offset, zoom, and aspect ratio. */
168 const float3x3 image_transformation = math::from_loc_scale<float3x3>(
169 pan_offset, float2(zoom) * float2(1.0f, aspect_ratio));
170
171 /* Transforms image data coordinates into the centered virtual space with the image offset. We
172 * also bias translations to avoids the round-to-even behavior of some GPUs at pixel
173 * boundaries. */
174 const float2 image_center = image_size / 2.0f;
175 const float2 corrective_translation = float2(std::numeric_limits<float>::epsilon() * 10e3f);
176 const float2 image_translation = image_offset - image_center + corrective_translation;
177 const float3x3 image_data_to_virtual = math::translate(image_transformation,
178 image_translation);
179
180 /* Transforms the output data space to the image data space. */
181 const float3x3 virtual_to_image_data = math::invert(image_data_to_virtual);
182 const float3x3 output_data_to_image_data = virtual_to_image_data * output_data_to_virtual;
183
184 /* Transform from image data coordinates to image sampler normalized coordinates with half
185 * pixel offsets. */
186 const float3x3 image_data_to_image_texel = math::from_location<float3x3>(float2(0.5f));
187 const float3x3 image_texel_to_image_sampler = math::from_scale<float3x3, 2>(1.0f / image_size);
188
189 const float3x3 output_screen_uv_to_image_sampler = image_texel_to_image_sampler *
190 image_data_to_image_texel *
191 output_data_to_image_data *
192 output_texel_to_output_data *
193 output_screen_uv_to_output_texel;
194 return output_screen_uv_to_image_sampler;
195 }
196
198 {
199 state.image = space_->get_image(main_);
200 if (state.image == nullptr) {
201 /* Early exit, nothing to draw. */
202 return;
203 }
204 state.flags.do_tile_drawing = state.image->source != IMA_SRC_TILED &&
205 space_->use_tile_drawing();
206 void *lock;
207 ImBuf *image_buffer = space_->acquire_image_buffer(state.image, &lock);
208
209 const float2 image_size = float2(image_buffer ? image_buffer->x : 1024.0f,
210 image_buffer ? image_buffer->y : 1024.0f);
211 float2 offset = float2(0.0f);
212 if (image_buffer && space_->use_display_window() &&
213 flag_is_set(image_buffer->flags, ImBufFlags::HasDisplayWindow))
214 {
215 offset = float2(image_buffer->display_offset);
216 }
217
220 offset,
221 image_size,
222 space_->get_pan_offset(),
223 space_->get_zoom(),
224 space_->get_aspect_ratio());
225
226 const Scene *scene = DRW_context_get()->scene;
227 state.sh_params.update(space_.get(), scene, state.image, image_buffer);
228 space_->release_buffer(state.image, image_buffer, lock);
229
230 ImageUser *iuser = space_->get_image_user();
231 if (state.image->rr != nullptr) {
232 BKE_image_multilayer_index(state.image->rr, iuser);
233 }
234 else {
235 BKE_image_multiview_index(state.image, iuser);
236 }
237 }
238
239 void object_sync(ObjectRef & /*obref*/, Manager & /*manager*/) final {}
240
241 void end_sync() final {}
242
243 void draw(Manager & /*manager*/) final
244 {
246 if (drawing_mode_) {
247 drawing_mode_->draw_viewport();
248 }
249 else {
251 DRW_context_get()->viewport_framebuffer_list_get()->default_fb, double4(0.0), 1.0f);
252 }
254 state.image = nullptr;
255 drawing_mode_.reset();
257 }
258};
259} // namespace blender::image_engine
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define BLI_SCOPED_DEFER(function_to_defer)
volatile int lock
#define BLI_assert_unreachable()
A running instance of the engine.
void object_sync(ObjectRef &, Manager &) final
float3x3 compute_screen_space_to_sampler_space_transformation(const float2 output_size, const float2 image_offset, const float2 image_size, const float2 pan_offset, const float zoom, const float aspect_ratio) const
std::unique_ptr< AbstractDrawingMode > get_drawing_mode()
StringRefNull name_get() final
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
const ccl_global KernelWorkTile * tile
static std::unique_ptr< AbstractSpaceAccessor > space_accessor_from_space(SpaceLink *space_link)
MatBase< T, 4, 4 > orthographic(T left, T right, T bottom, T top, T near_clip, T far_clip)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
MatT from_loc_scale(const typename MatT::loc_type &location, const VecBase< typename MatT::base_type, ScaleDim > &scale)
CartesianBasis invert(const CartesianBasis &basis)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
MatBase< T, NumCol, NumRow > translate(const MatBase< T, NumCol, NumRow > &mat, const VectorT &translation)
MatT from_location(const typename MatT::loc_type &location)
void BKE_image_multiview_index(const Image *ima, ImageUser *iuser)
ImageGPUTextures BKE_image_acquire_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping, const bool try_only)
Definition image_gpu.cc:762
MatBase< float, 4, 4 > float4x4
ImBuf * BKE_image_acquire_ibuf_gpu(Image *ima, ImageUser *iuser, void **r_lock, bool *r_load_failed=nullptr, bool cached_only=false)
VecBase< float, 2 > float2
RenderPass * BKE_image_multilayer_index(RenderResult *rr, ImageUser *iuser)
gpu::Texture * BKE_image_acquire_gpu_viewer_texture(Image *image, ImageUser *iuser, bool only_full_resolution=false)
Definition image_gpu.cc:749
void DRW_submission_end()
const DRWContext * DRW_context_get()
MatBase< float, 3, 3 > float3x3
Main * CTX_data_main(const bContext *C)
bool GPU_is_safe_texture_size(int width, int height)
void DRW_submission_start()
draw::Manager * DRW_manager_get()
void GPU_framebuffer_clear_color_depth(gpu::FrameBuffer *fb, const double4 clear_col, float clear_depth)
VecBase< double, 4 > double4
int GPU_texture_size_with_limit(int res)
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
const bContext * evil_C
SpaceLink * space_data
FloatBufferCache float_buffers