[Bf-blender-cvs] [c5edefe4961] temp-gpu-image-engine: Fix uvbounds calculation from the incorrect space.

Jeroen Bakker noreply at git.blender.org
Mon Dec 13 16:02:01 CET 2021


Commit: c5edefe4961e3df1d47bd365ed8791cb821342ee
Author: Jeroen Bakker
Date:   Mon Dec 13 16:01:53 2021 +0100
Branches: temp-gpu-image-engine
https://developer.blender.org/rBc5edefe4961e3df1d47bd365ed8791cb821342ee

Fix uvbounds calculation from the incorrect space.

===================================================================

M	source/blender/draw/engines/image/image_drawing_mode.hh
M	source/blender/draw/engines/image/image_engine.cc
M	source/blender/draw/engines/image/image_instance_data.hh
M	source/blender/draw/engines/image/image_space.hh
M	source/blender/draw/engines/image/image_space_image.hh
M	source/blender/draw/engines/image/image_space_node.hh
M	source/blender/draw/engines/image/image_texture_info.hh

===================================================================

diff --git a/source/blender/draw/engines/image/image_drawing_mode.hh b/source/blender/draw/engines/image/image_drawing_mode.hh
index 49a47b9bfd6..11802a4d8d5 100644
--- a/source/blender/draw/engines/image/image_drawing_mode.hh
+++ b/source/blender/draw/engines/image/image_drawing_mode.hh
@@ -44,6 +44,21 @@ struct OneTextureMethod {
   {
   }
 
+  void update_uv_to_texture_matrix(const ARegion *region)
+  {
+    // TODO: I remember that there was a function for this somewhere.
+    unit_m4(instance_data->uv_to_texture);
+    float scale_x = 1.0 / BLI_rctf_size_x(&region->v2d.cur);
+    float scale_y = 1.0 / BLI_rctf_size_y(&region->v2d.cur);
+    float translate_x = scale_x * -region->v2d.cur.xmin;
+    float translate_y = scale_y * -region->v2d.cur.ymin;
+
+    instance_data->uv_to_texture[0][0] = scale_x;
+    instance_data->uv_to_texture[1][1] = scale_y;
+    instance_data->uv_to_texture[3][0] = translate_x;
+    instance_data->uv_to_texture[3][1] = translate_y;
+  }
+
   /** \brief Update the texture slot uv and screen space bounds. */
   void update_screen_space_bounds(const ARegion *region)
   {
@@ -59,27 +74,12 @@ struct OneTextureMethod {
     }
   }
 
-  void update_uv_bounds()
+  void update_uv_bounds(const ARegion *region)
   {
-    /* Calculate the uv coordinates of the screen space visible corners. */
-    float inverse_mat[4][4];
-    DRW_view_viewmat_get(NULL, inverse_mat, true);
-
-    rctf new_uv_bounds;
-    float uv_min[3];
-    static const float screen_space_co1[3] = {0.0, 0.0, 0.0};
-    mul_v3_m4v3(uv_min, inverse_mat, screen_space_co1);
-
-    static const float screen_space_co2[3] = {1.0, 1.0, 0.0};
-    float uv_max[3];
-    mul_v3_m4v3(uv_max, inverse_mat, screen_space_co2);
-    BLI_rctf_init(&new_uv_bounds, uv_min[0], uv_max[0], uv_min[1], uv_max[1]);
-
-    if (!BLI_rctf_compare(
-            &instance_data->texture_infos[0].uv_bounds, &new_uv_bounds, EPSILON_UV_BOUNDS)) {
-      instance_data->texture_infos[0].uv_bounds = new_uv_bounds;
-      instance_data->texture_infos[0].dirty = true;
-      update_uv_to_texture_matrix(&instance_data->texture_infos[0]);
+    TextureInfo &info = instance_data->texture_infos[0];
+    if (!BLI_rctf_compare(&info.uv_bounds, &region->v2d.cur, EPSILON_UV_BOUNDS)) {
+      info.uv_bounds = region->v2d.cur;
+      info.dirty = true;
     }
 
     /* Mark the other textures as invalid. */
@@ -87,21 +87,6 @@ struct OneTextureMethod {
       BLI_rctf_init_minmax(&instance_data->texture_infos[i].clipping_bounds);
     }
   }
-
-  void update_uv_to_texture_matrix(TextureInfo *info)
-  {
-    // TODO: I remember that there was a function for this somewhere.
-    unit_m4(info->uv_to_texture);
-    float scale_x = 1.0 / BLI_rctf_size_x(&info->uv_bounds);
-    float scale_y = 1.0 / BLI_rctf_size_y(&info->uv_bounds);
-    float translate_x = scale_x * -info->uv_bounds.xmin;
-    float translate_y = scale_y * -info->uv_bounds.ymin;
-
-    info->uv_to_texture[0][0] = scale_x;
-    info->uv_to_texture[1][1] = scale_y;
-    info->uv_to_texture[3][0] = translate_x;
-    info->uv_to_texture[3][1] = translate_y;
-  }
 };
 
 using namespace blender::bke::image::partial_update;
@@ -367,7 +352,7 @@ template<typename TextureMethod> class ScreenSpaceDrawingMode : public AbstractD
      * Construct a variant of the info_uv_to_texture that adds the texel space
      * transformation.*/
     float uv_to_texel[4][4];
-    copy_m4_m4(uv_to_texel, texture_info.uv_to_texture);
+    copy_m4_m4(uv_to_texel, instance_data.uv_to_texture);
     float scale[3] = {static_cast<float>(texture_width) / static_cast<float>(tile_buffer.x),
                       static_cast<float>(texture_height) / static_cast<float>(tile_buffer.y),
                       1.0f};
@@ -418,8 +403,9 @@ 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.
     const ARegion *region = draw_ctx->region;
+    method.update_uv_to_texture_matrix(region);
     method.update_screen_space_bounds(region);
-    method.update_uv_bounds();
+    method.update_uv_bounds(region);
 
     // Step: Update the GPU textures based on the changes in the image.
     instance_data->update_gpu_texture_allocations();
diff --git a/source/blender/draw/engines/image/image_engine.cc b/source/blender/draw/engines/image/image_engine.cc
index ad02adaa98c..949044f83c2 100644
--- a/source/blender/draw/engines/image/image_engine.cc
+++ b/source/blender/draw/engines/image/image_engine.cc
@@ -89,7 +89,20 @@ class ImageEngine {
     IMAGE_InstanceData *instance_data = vedata->instance_data;
     drawing_mode.cache_init(vedata);
     const ARegion *region = draw_ctx->region;
-    instance_data->view = space->create_view_override(region);
+
+    /* Setup full screen view matrix. */
+    float winmat[4][4], viewmat[4][4];
+    orthographic_m4(viewmat, 0.0, region->winx, 0.0, region->winy, 0.0, 1.0);
+    unit_m4(winmat);
+    instance_data->view = DRW_view_create(viewmat, winmat, nullptr, nullptr, nullptr);
+
+    /*
+     * TODO: space can override the uv bounds of the visible region.
+     * for image space it would use the region->v2d.cur.
+     * for node space it would use the zoom and scaling of the backdrop.
+     *
+     * This should be stored in the root of the instance data.
+     */
   }
 
   void cache_populate()
diff --git a/source/blender/draw/engines/image/image_instance_data.hh b/source/blender/draw/engines/image/image_instance_data.hh
index 09a79b12a17..b48ac2b9fe8 100644
--- a/source/blender/draw/engines/image/image_instance_data.hh
+++ b/source/blender/draw/engines/image/image_instance_data.hh
@@ -59,6 +59,8 @@ struct IMAGE_InstanceData {
     DRWPass *image_pass;
   } passes;
 
+  /** \brief Transform matrix to convert a normalized uv coordinate to texture space. */
+  float uv_to_texture[4][4];
   TextureInfo texture_infos[SCREEN_SPACE_DRAWING_MODE_TEXTURE_LEN];
 
   /**
diff --git a/source/blender/draw/engines/image/image_space.hh b/source/blender/draw/engines/image/image_space.hh
index 1fd240b9a36..84841a76a7a 100644
--- a/source/blender/draw/engines/image/image_space.hh
+++ b/source/blender/draw/engines/image/image_space.hh
@@ -85,10 +85,6 @@ class AbstractSpaceAccessor {
                                 bool *r_owns_texture,
                                 GPUTexture **r_tex_tile_data) = 0;
 
-  /**
-   * Override the view for drawing.
-   */
-  virtual DRWView *create_view_override(const ARegion *UNUSED(region)) = 0;
 
   /**
    * Initialize the matrix that will be used to draw the image. The matrix will be send as object
diff --git a/source/blender/draw/engines/image/image_space_image.hh b/source/blender/draw/engines/image/image_space_image.hh
index 9dbd0648b74..6b7a31cb81b 100644
--- a/source/blender/draw/engines/image/image_space_image.hh
+++ b/source/blender/draw/engines/image/image_space_image.hh
@@ -101,15 +101,6 @@ class SpaceImageAccessor : public AbstractSpaceAccessor {
     }
   }
 
-  DRWView *create_view_override(const ARegion *region) override
-  {
-    /* Setup a screen pixel view. The backdrop of the node editor doesn't follow the region. */
-    float winmat[4][4], viewmat[4][4];
-    orthographic_m4(viewmat, 0.0, region->winx, 0.0, region->winy, 0.0, 1.0);
-    unit_m4(winmat);
-    return DRW_view_create(viewmat, winmat, nullptr, nullptr, nullptr);
-  }
-
   void get_gpu_textures(Image *image,
                         ImageUser *iuser,
                         ImBuf *image_buffer,
diff --git a/source/blender/draw/engines/image/image_space_node.hh b/source/blender/draw/engines/image/image_space_node.hh
index be2667879d4..bd48f912752 100644
--- a/source/blender/draw/engines/image/image_space_node.hh
+++ b/source/blender/draw/engines/image/image_space_node.hh
@@ -54,14 +54,6 @@ class SpaceNodeAccessor : public AbstractSpaceAccessor {
     BKE_image_release_ibuf(image, ibuf, lock);
   }
 
-  DRWView *create_view_override(const ARegion *region) override
-  {
-    /* Setup a screen pixel view. The backdrop of the node editor doesn't follow the region. */
-    float winmat[4][4], viewmat[4][4];
-    orthographic_m4(viewmat, 0.0, region->winx, 0.0, region->winy, 0.0, 1.0);
-    unit_m4(winmat);
-    return DRW_view_create(viewmat, winmat, nullptr, nullptr, nullptr);
-  }
 
   void get_shader_parameters(ShaderParameters &r_shader_parameters,
                              ImBuf *ibuf,
diff --git a/source/blender/draw/engines/image/image_texture_info.hh b/source/blender/draw/engines/image/image_texture_info.hh
index 759254c301e..b67778f18c3 100644
--- a/source/blender/draw/engines/image/image_texture_info.hh
+++ b/source/blender/draw/engines/image/image_texture_info.hh
@@ -46,8 +46,6 @@ struct TextureInfo {
   rctf clipping_bounds;
   /** \brief uv area of the texture. */
   rctf uv_bounds;
-  /** \brief Transform matrix to convert a normalized uv coordinate to texture space. */
-  float uv_to_texture[4][4];
 
   /**
    * \brief Batch to draw the associated texton the screen.



More information about the Bf-blender-cvs mailing list