Cleanup: ImageEngine-Move Responsibility of Texture Creation.

It used to be a number of fixed full screen images where the
responsibility was at image engine. Now moved the responsibility to the
drawing mode to make sure we can allocate non-full region-size textures
in a future refactoring.
This commit is contained in:
Jeroen Bakker 2022-12-09 13:01:57 +01:00
parent 595b302231
commit bdd196661e
3 changed files with 52 additions and 48 deletions

View File

@ -31,6 +31,14 @@ struct FullScreenTextures {
{
}
/**
* \brief Ensure enough texture infos are allocated in `instance_data`.
*/
void ensure_texture_infos()
{
instance_data->texture_infos.resize(4);
}
/**
* \brief Update the uv and region bounds of all texture_infos of instance_data.
*/
@ -45,7 +53,7 @@ struct FullScreenTextures {
BLI_rctf_init(
&region_uv_bounds, region_uv_min.x, region_uv_max.x, region_uv_min.y, region_uv_max.y);
/* Calculate 9 coordinates that will be used as uv bounds of the 4 textures. */
/* Calculate 9 coordinates that will be used as uv bounds of the textures. */
float2 onscreen_multiple = (blender::math::floor(region_uv_min / region_uv_span) +
float2(1.0f)) *
region_uv_span;
@ -135,6 +143,15 @@ struct FullScreenTextures {
info.calc_region_bounds_from_uv_bounds(uv_to_screen);
}
}
void ensure_gpu_textures_allocation()
{
float2 viewport_size = DRW_viewport_size_get();
int2 texture_size(viewport_size.x, viewport_size.y);
for (TextureInfo &info : instance_data->texture_infos) {
info.ensure_gpu_texture(texture_size);
}
}
};
using namespace blender::bke::image::partial_update;
@ -171,8 +188,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
DRW_shgroup_uniform_texture(shgrp, "depth_texture", dtxl->depth);
float image_mat[4][4];
unit_m4(image_mat);
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
const TextureInfo &info = instance_data->texture_infos[i];
for (const TextureInfo &info : instance_data->texture_infos) {
DRWShadingGroup *shgrp_sub = DRW_shgroup_create_sub(shgrp);
DRW_shgroup_uniform_ivec2_copy(shgrp_sub, "offset", info.offset());
DRW_shgroup_uniform_texture_ex(shgrp_sub, "imageTexture", info.texture, GPU_SAMPLER_DEFAULT);
@ -200,8 +216,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
tile_user = *image_user;
}
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
const TextureInfo &info = instance_data.texture_infos[i];
for (const TextureInfo &info : instance_data.texture_infos) {
LISTBASE_FOREACH (ImageTile *, image_tile_ptr, &image->tiles) {
const ImageTileWrapper image_tile(image_tile_ptr);
const int tile_x = image_tile.get_tile_x_offset();
@ -305,8 +320,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
const float tile_width = float(iterator.tile_data.tile_buffer->x);
const float tile_height = float(iterator.tile_data.tile_buffer->y);
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
const TextureInfo &info = instance_data.texture_infos[i];
for (const TextureInfo &info : instance_data.texture_infos) {
/* Dirty images will receive a full update. No need to do a partial one now. */
if (info.need_full_update) {
continue;
@ -407,8 +421,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
void do_full_update_for_dirty_textures(IMAGE_InstanceData &instance_data,
const ImageUser *image_user) const
{
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
TextureInfo &info = instance_data.texture_infos[i];
for (TextureInfo &info : instance_data.texture_infos) {
if (!info.need_full_update) {
continue;
}
@ -518,6 +531,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
/* Step: Find out which screen space textures are needed to draw on the screen. Remove the
* screen space textures that aren't needed. */
method.ensure_texture_infos();
const ARegion *region = draw_ctx->region;
method.update_bounds(region);
@ -525,7 +539,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
instance_data->update_image_usage(iuser);
/* Step: Update the GPU textures based on the changes in the image. */
instance_data->update_gpu_texture_allocations();
method.ensure_gpu_textures_allocation();
update_textures(*instance_data, image, iuser);
/* Step: Add the GPU textures to the shgroup. */

View File

@ -21,15 +21,6 @@
namespace blender::draw::image_engine {
/**
* \brief max allowed textures to use by the ScreenSpaceDrawingMode.
*
* The image engine uses 4 full screen textures to draw the image. With 4 textures it is possible
* to pan the screen where only the texture needs to be updated when they are not visible on the
* screen.
*/
constexpr int SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN = 4;
struct IMAGE_InstanceData {
struct Image *image;
/** Usage data of the previous time, to identify changes that require a full update. */
@ -61,7 +52,8 @@ struct IMAGE_InstanceData {
/** \brief Transform matrix to convert a normalized screen space coordinates to texture space. */
float ss_to_texture[4][4];
TextureInfo texture_infos[SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN];
Vector<TextureInfo> texture_infos;
public:
virtual ~IMAGE_InstanceData() = default;
@ -75,33 +67,9 @@ struct IMAGE_InstanceData {
reset_need_full_update(true);
}
void update_gpu_texture_allocations()
{
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
TextureInfo &info = texture_infos[i];
const bool is_allocated = info.texture != nullptr;
const bool resolution_changed = assign_if_different(info.last_viewport_size,
float2(DRW_viewport_size_get()));
const bool should_be_freed = is_allocated && resolution_changed;
const bool should_be_created = !is_allocated || resolution_changed;
if (should_be_freed) {
GPU_texture_free(info.texture);
info.texture = nullptr;
}
if (should_be_created) {
DRW_texture_ensure_fullscreen_2d(
&info.texture, GPU_RGBA16F, static_cast<DRWTextureFlag>(0));
}
info.need_full_update |= should_be_created;
}
}
void update_batches()
{
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
TextureInfo &info = texture_infos[i];
for (TextureInfo &info : texture_infos) {
BatchUpdater batch_updater(info);
batch_updater.update_batch();
}
@ -121,8 +89,8 @@ struct IMAGE_InstanceData {
/** \brief Set dirty flag of all texture slots to the given value. */
void reset_need_full_update(bool new_value)
{
for (int i = 0; i < SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN; i++) {
texture_infos[i].need_full_update = new_value;
for (TextureInfo &info : texture_infos) {
info.need_full_update = new_value;
}
}
};

View File

@ -42,7 +42,7 @@ struct TextureInfo {
*/
GPUTexture *texture;
float2 last_viewport_size = float2(0.0f, 0.0f);
int2 last_texture_size = int2(0);
~TextureInfo()
{
@ -83,6 +83,28 @@ struct TextureInfo {
bottom_left_region.y,
top_right_region.y);
}
void ensure_gpu_texture(int2 texture_size)
{
const bool is_allocated = texture != nullptr;
const bool resolution_changed = assign_if_different(last_texture_size, texture_size);
const bool should_be_freed = is_allocated && resolution_changed;
const bool should_be_created = !is_allocated || resolution_changed;
if (should_be_freed) {
GPU_texture_free(texture);
texture = nullptr;
}
if (should_be_created) {
texture = DRW_texture_create_2d_ex(UNPACK2(texture_size),
GPU_RGBA16F,
GPU_TEXTURE_USAGE_GENERAL,
static_cast<DRWTextureFlag>(0),
nullptr);
}
need_full_update |= should_be_created;
}
};
} // namespace blender::draw::image_engine