[Bf-blender-cvs] [28df0107d4a] master: Fix T94362: GPUMaterialTexture references freed ImageUser

Jacques Lucke noreply at git.blender.org
Sat Dec 25 11:15:24 CET 2021


Commit: 28df0107d4a83dd7ce62781bef821092db1e0835
Author: Jacques Lucke
Date:   Sat Dec 25 11:14:02 2021 +0100
Branches: master
https://developer.blender.org/rB28df0107d4a83dd7ce62781bef821092db1e0835

Fix T94362: GPUMaterialTexture references freed ImageUser

The issue was caused by rB7e712b2d6a0d257d272ed35622b41d06274af8df
and the fact that `GPUMaterialTexture` contains an `ImageUser *` which
references the `ImageUser` on e.g. `NodeTexImage`.

Since the node tree update refactor, it is possible that the node tree changes
without changing the actual material. Therefore, either the renderer should
check if the node tree has changed or it should not store pointers to data in
node storage. The latter approach is implemented in this patch.

Differential Revision: https://developer.blender.org/D13663

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

M	source/blender/draw/intern/draw_manager_data.c
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/intern/gpu_node_graph.c

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

diff --git a/source/blender/draw/intern/draw_manager_data.c b/source/blender/draw/intern/draw_manager_data.c
index e71a1298812..f1c13bc039a 100644
--- a/source/blender/draw/intern/draw_manager_data.c
+++ b/source/blender/draw/intern/draw_manager_data.c
@@ -1364,14 +1364,15 @@ void DRW_shgroup_add_material_resources(DRWShadingGroup *grp, struct GPUMaterial
     if (tex->ima) {
       /* Image */
       GPUTexture *gputex;
+      ImageUser *iuser = tex->iuser_available ? &tex->iuser : NULL;
       if (tex->tiled_mapping_name[0]) {
-        gputex = BKE_image_get_gpu_tiles(tex->ima, tex->iuser, NULL);
+        gputex = BKE_image_get_gpu_tiles(tex->ima, iuser, NULL);
         drw_shgroup_material_texture(grp, gputex, tex->sampler_name, tex->sampler_state);
-        gputex = BKE_image_get_gpu_tilemap(tex->ima, tex->iuser, NULL);
+        gputex = BKE_image_get_gpu_tilemap(tex->ima, iuser, NULL);
         drw_shgroup_material_texture(grp, gputex, tex->tiled_mapping_name, tex->sampler_state);
       }
       else {
-        gputex = BKE_image_get_gpu_texture(tex->ima, tex->iuser, NULL);
+        gputex = BKE_image_get_gpu_texture(tex->ima, iuser, NULL);
         drw_shgroup_material_texture(grp, gputex, tex->sampler_name, tex->sampler_state);
       }
     }
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index ea3028e539b..f7a01fadadc 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -24,6 +24,7 @@
 #pragma once
 
 #include "DNA_customdata_types.h" /* for CustomDataType */
+#include "DNA_image_types.h"
 #include "DNA_listBase.h"
 
 #include "BLI_sys_types.h" /* for bool */
@@ -256,7 +257,8 @@ typedef struct GPUMaterialAttribute {
 typedef struct GPUMaterialTexture {
   struct GPUMaterialTexture *next, *prev;
   struct Image *ima;
-  struct ImageUser *iuser;
+  struct ImageUser iuser;
+  bool iuser_available;
   struct GPUTexture **colorband;
   char sampler_name[32];       /* Name of sampler in GLSL. */
   char tiled_mapping_name[32]; /* Name of tile mapping sampler in GLSL. */
diff --git a/source/blender/gpu/intern/gpu_node_graph.c b/source/blender/gpu/intern/gpu_node_graph.c
index 50ff450ac79..e0d60aa5fda 100644
--- a/source/blender/gpu/intern/gpu_node_graph.c
+++ b/source/blender/gpu/intern/gpu_node_graph.c
@@ -439,7 +439,10 @@ static GPUMaterialTexture *gpu_node_graph_add_texture(GPUNodeGraph *graph,
   if (tex == NULL) {
     tex = MEM_callocN(sizeof(*tex), __func__);
     tex->ima = ima;
-    tex->iuser = iuser;
+    if (iuser != NULL) {
+      tex->iuser = *iuser;
+      tex->iuser_available = true;
+    }
     tex->colorband = colorband;
     tex->sampler_state = sampler_state;
     BLI_snprintf(tex->sampler_name, sizeof(tex->sampler_name), "samp%d", num_textures);



More information about the Bf-blender-cvs mailing list