[Bf-blender-cvs] [e88a7153649] master: Cleanup: more refactoring of GPU material attributes and textures

Brecht Van Lommel noreply at git.blender.org
Thu Feb 27 16:28:08 CET 2020


Commit: e88a715364981af39353073c6d96b9056fafca6b
Author: Brecht Van Lommel
Date:   Thu Feb 27 13:55:29 2020 +0100
Branches: master
https://developer.blender.org/rBe88a715364981af39353073c6d96b9056fafca6b

Cleanup: more refactoring of GPU material attributes and textures

This further separates requested attributes and textures from the actual
node graph, that can be retained after the graph has been compiled and
freed. It makes it easier to add volume grids as a native concept, which
sits somewhere between an attribute and a texture.

It also adds explicit link types for UDIM tile mapping, rather than
relying on fairly hidden logic.

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

M	source/blender/draw/intern/draw_manager_data.c
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/intern/gpu_codegen.c
M	source/blender/gpu/intern/gpu_material.c
M	source/blender/gpu/intern/gpu_node_graph.c
M	source/blender/gpu/intern/gpu_node_graph.h
M	source/blender/nodes/shader/node_shader_util.c
M	source/blender/nodes/shader/nodes/node_shader_attribute.c
M	source/blender/nodes/shader/nodes/node_shader_geometry.c
M	source/blender/nodes/shader/nodes/node_shader_normal_map.c
M	source/blender/nodes/shader/nodes/node_shader_tangent.c
M	source/blender/nodes/shader/nodes/node_shader_tex_coord.c
M	source/blender/nodes/shader/nodes/node_shader_tex_environment.c
M	source/blender/nodes/shader/nodes/node_shader_tex_image.c
M	source/blender/nodes/shader/nodes/node_shader_tex_sky.c
M	source/blender/nodes/shader/nodes/node_shader_uvmap.c
M	source/blender/nodes/shader/nodes/node_shader_vector_displacement.c
M	source/blender/nodes/shader/nodes/node_shader_vertex_color.c

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

diff --git a/source/blender/draw/intern/draw_manager_data.c b/source/blender/draw/intern/draw_manager_data.c
index fbdabb44b15..43e8e2a4733 100644
--- a/source/blender/draw/intern/draw_manager_data.c
+++ b/source/blender/draw/intern/draw_manager_data.c
@@ -1203,6 +1203,19 @@ static DRWShadingGroup *drw_shgroup_material_create_ex(GPUPass *gpupass, DRWPass
   return grp;
 }
 
+static void drw_shgroup_material_texture(DRWShadingGroup *grp,
+                                         GPUMaterialTexture *tex,
+                                         const char *name,
+                                         int textarget)
+{
+  GPUTexture *gputex = GPU_texture_from_blender(tex->ima, tex->iuser, NULL, textarget);
+  DRW_shgroup_uniform_texture(grp, name, gputex);
+
+  GPUTexture **gputex_ref = BLI_memblock_alloc(DST.vmempool->images);
+  *gputex_ref = gputex;
+  GPU_texture_ref(gputex);
+}
+
 static DRWShadingGroup *drw_shgroup_material_inputs(DRWShadingGroup *grp,
                                                     struct GPUMaterial *material)
 {
@@ -1210,35 +1223,20 @@ static DRWShadingGroup *drw_shgroup_material_inputs(DRWShadingGroup *grp,
 
   /* Bind all textures needed by the material. */
   for (GPUMaterialTexture *tex = textures.first; tex; tex = tex->next) {
-    GPUTexture *gputex;
-
     if (tex->ima) {
       /* Image */
-      GPUTexture **gputex_ref = BLI_memblock_alloc(DST.vmempool->images);
-
-      int textarget;
-      if (tex->type == GPU_TEX2D_ARRAY) {
-        textarget = GL_TEXTURE_2D_ARRAY;
-      }
-      else if (tex->type == GPU_TEX1D_ARRAY) {
-        textarget = GL_TEXTURE_1D_ARRAY;
+      if (tex->tiled_mapping_name[0]) {
+        drw_shgroup_material_texture(grp, tex, tex->sampler_name, GL_TEXTURE_2D_ARRAY);
+        drw_shgroup_material_texture(grp, tex, tex->tiled_mapping_name, GL_TEXTURE_1D_ARRAY);
       }
       else {
-        textarget = GL_TEXTURE_2D;
+        drw_shgroup_material_texture(grp, tex, tex->sampler_name, GL_TEXTURE_2D);
       }
-      *gputex_ref = gputex = GPU_texture_from_blender(tex->ima, tex->iuser, NULL, textarget);
-
-      GPU_texture_ref(gputex);
     }
     else if (tex->colorband) {
       /* Color Ramp */
-      gputex = *tex->colorband;
+      DRW_shgroup_uniform_texture(grp, tex->sampler_name, *tex->colorband);
     }
-    else {
-      continue;
-    }
-
-    DRW_shgroup_uniform_texture(grp, tex->shadername, gputex);
   }
 
   GPUUniformBuffer *ubo = GPU_material_uniform_buffer_get(material);
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index 800843dd851..37fe30bc96b 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -64,6 +64,7 @@ typedef enum eGPUType {
   GPU_VEC4 = 4,
   GPU_MAT3 = 9,
   GPU_MAT4 = 16,
+  GPU_MAX_CONSTANT_DATA = GPU_MAT4,
 
   /* Values not in GPU_DATATYPE_STR */
   GPU_TEX1D_ARRAY = 1001,
@@ -137,10 +138,12 @@ typedef enum eGPUMaterialStatus {
   GPU_MAT_SUCCESS,
 } eGPUMaterialStatus;
 
-GPUNodeLink *GPU_attribute(CustomDataType type, const char *name);
-GPUNodeLink *GPU_constant(float *num);
-GPUNodeLink *GPU_uniform(float *num);
-GPUNodeLink *GPU_image(struct Image *ima, struct ImageUser *iuser);
+GPUNodeLink *GPU_constant(const float *num);
+GPUNodeLink *GPU_uniform(const float *num);
+GPUNodeLink *GPU_attribute(GPUMaterial *mat, CustomDataType type, const char *name);
+GPUNodeLink *GPU_image(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser);
+GPUNodeLink *GPU_image_tiled(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser);
+GPUNodeLink *GPU_image_tiled_mapping(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser);
 GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *layer);
 GPUNodeLink *GPU_builtin(eGPUBuiltin builtin);
 
@@ -212,17 +215,19 @@ typedef struct GPUMaterialAttribute {
   struct GPUMaterialAttribute *next, *prev;
   int type;      /* CustomDataType */
   char name[64]; /* MAX_CUSTOMDATA_LAYER_NAME */
+  eGPUType gputype;
   int id;
+  int users;
 } GPUMaterialAttribute;
 
 typedef struct GPUMaterialTexture {
   struct GPUMaterialTexture *next, *prev;
-  eGPUType type;
   struct Image *ima;
   struct ImageUser *iuser;
   struct GPUTexture **colorband;
-  char shadername[32]; /* Name of sampler in GLSL. */
-  int id;
+  char sampler_name[32];       /* Name of sampler in GLSL. */
+  char tiled_mapping_name[32]; /* Name of tile mapping sampler in GLSL. */
+  int users;
 } GPUMaterialTexture;
 
 ListBase GPU_material_attributes(GPUMaterial *material);
diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c
index 6c14d918816..553ecb65529 100644
--- a/source/blender/gpu/intern/gpu_codegen.c
+++ b/source/blender/gpu/intern/gpu_codegen.c
@@ -216,16 +216,6 @@ static void codegen_print_datatype(DynStr *ds, const eGPUType type, float *data)
   }
 }
 
-static int codegen_input_has_texture(GPUInput *input)
-{
-  if (input->link) {
-    return 0;
-  }
-  else {
-    return (input->source == GPU_SOURCE_TEX);
-  }
-}
-
 static const char *gpu_builtin_name(eGPUBuiltin builtin)
 {
   if (builtin == GPU_VIEW_MATRIX) {
@@ -299,14 +289,14 @@ static const char *gpu_builtin_name(eGPUBuiltin builtin)
   }
 }
 
-static void codegen_set_unique_ids(ListBase *nodes)
+static void codegen_set_unique_ids(GPUNodeGraph *graph)
 {
   GPUNode *node;
   GPUInput *input;
   GPUOutput *output;
   int id = 1;
 
-  for (node = nodes->first; node; node = node->next) {
+  for (node = graph->nodes.first; node; node = node->next) {
     for (input = node->inputs.first; input; input = input->next) {
       /* set id for unique names of uniform variables */
       input->id = id++;
@@ -322,7 +312,9 @@ static void codegen_set_unique_ids(ListBase *nodes)
 /**
  * It will create an UBO for GPUMaterial if there is any GPU_DYNAMIC_UBO.
  */
-static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, ListBase *nodes)
+static int codegen_process_uniforms_functions(GPUMaterial *material,
+                                              DynStr *ds,
+                                              GPUNodeGraph *graph)
 {
   GPUNode *node;
   GPUInput *input;
@@ -330,27 +322,29 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
   int builtins = 0;
   ListBase ubo_inputs = {NULL, NULL};
 
-  /* print uniforms */
-  for (node = nodes->first; node; node = node->next) {
+  /* Attributes */
+  for (GPUMaterialAttribute *attr = graph->attributes.first; attr; attr = attr->next) {
+    BLI_dynstr_appendf(ds, "in %s var%d;\n", gpu_data_type_to_string(attr->gputype), attr->id);
+  }
+
+  /* Textures */
+  for (GPUMaterialTexture *tex = graph->textures.first; tex; tex = tex->next) {
+    if (tex->colorband) {
+      BLI_dynstr_appendf(ds, "uniform sampler1DArray %s;\n", tex->sampler_name);
+    }
+    else if (tex->tiled_mapping_name[0]) {
+      BLI_dynstr_appendf(ds, "uniform sampler2DArray %s;\n", tex->sampler_name);
+      BLI_dynstr_appendf(ds, "uniform sampler1DArray %s;\n", tex->tiled_mapping_name);
+    }
+    else {
+      BLI_dynstr_appendf(ds, "uniform sampler2D %s;\n", tex->sampler_name);
+    }
+  }
+
+  /* Print other uniforms */
+  for (node = graph->nodes.first; node; node = node->next) {
     for (input = node->inputs.first; input; input = input->next) {
-      if (input->source == GPU_SOURCE_TEX) {
-        /* create exactly one sampler for each texture */
-        if (codegen_input_has_texture(input) && input->bindtex) {
-          const char *type;
-          if (input->colorband || input->type == GPU_TEX1D_ARRAY) {
-            type = "sampler1DArray";
-          }
-          else if (input->type == GPU_TEX2D_ARRAY) {
-            type = "sampler2DArray";
-          }
-          else {
-            BLI_assert(input->type == GPU_TEX2D);
-            type = "sampler2D";
-          }
-          BLI_dynstr_appendf(ds, "uniform %s samp%d;\n", type, input->texid);
-        }
-      }
-      else if (input->source == GPU_SOURCE_BUILTIN) {
+      if (input->source == GPU_SOURCE_BUILTIN) {
         /* only define each builtin uniform/varying once */
         if (!(builtins & input->builtin)) {
           builtins |= input->builtin;
@@ -385,10 +379,6 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
         codegen_print_datatype(ds, input->type, input->vec);
         BLI_dynstr_append(ds, ";\n");
       }
-      else if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
-        BLI_dynstr_appendf(
-            ds, "in %s var%d;\n", gpu_data_type_to_string(input->type), input->attr_id);
-      }
     }
   }
 
@@ -412,12 +402,12 @@ static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds,
   return builtins;
 }
 
-static void codegen_declare_tmps(DynStr *ds, ListBase *nodes)
+static void codegen_declare_tmps(DynStr *ds, GPUNodeGraph *graph)
 {
   GPUNode *node;
   GPUOutput *output;
 
-  for (node = nodes->first; node; node = node->next) {
+  for (node = graph->nodes.first; node; node = node->next) {
     /* declare temporary variables for node output storage */
     for (output = node->outputs.first; output; output = output->next) {
       if (output->type == GPU_CLOSURE) {
@@ -432,18 +422,21 @@ static void codegen_declare_tmps(DynStr *ds, ListBase *nodes)
   BLI_dynstr_append(ds, "\n");
 }
 
-static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *finaloutput)
+static void codegen_call_functions(DynStr *ds, GPUNodeGraph *graph, GPUOutput *finaloutput)
 {
   GPUNode *node;
   GPUInput *input;
   GPUOutput *output;
 
-  for (node = nodes->first; node; node = node->next) {
+  for (node = graph->nodes.first; node; node = node->next) {
     BLI_dynstr_appendf(ds, "\t%s(", node->name);
 
     for (input = node->inputs.first; input; input = input->next) {
       if (input->source == GPU_SOURCE_TEX) {
-        BLI_dynstr_appendf(ds, "samp%d", input->texid);
+        BLI_dynstr_append(ds, input->texture->sampler_name);
+      }
+      else if (input->source == GPU_SOURCE_TEX_TILED_MAPPING) {
+        BLI_dynstr_append(ds, input->texture->tiled_mapping_name);
       }
       else if (input->source == GPU_S

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list