[Bf-blender-cvs] [08d008a5085] master: GPU: Create Info for GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR.

Jeroen Bakker noreply at git.blender.org
Tue Jan 18 13:17:07 CET 2022


Commit: 08d008a5085bf57ba43c7aa76d51ea60b732a11a
Author: Jeroen Bakker
Date:   Tue Jan 18 13:13:23 2022 +0100
Branches: master
https://developer.blender.org/rB08d008a5085bf57ba43c7aa76d51ea60b732a11a

GPU: Create Info for GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR.

This patch converts GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR shader to use
the GPUShaderCreateInfo pattern. It can be used as a reference when
converting other shaders.

In this special case the flat uniform vector cannot be used anymore as it
doesn't fit as push constants. To solve this a uniform buffer is used.

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

M	source/blender/editors/interface/interface_icons.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader_shared.h
M	source/blender/gpu/intern/gpu_shader_builtin.c
M	source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl
M	source/blender/gpu/shaders/infos/gpu_interface_info.hh
A	source/blender/gpu/shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh

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

diff --git a/source/blender/editors/interface/interface_icons.c b/source/blender/editors/interface/interface_icons.c
index 18018461ac2..0f5b4a1a0f1 100644
--- a/source/blender/editors/interface/interface_icons.c
+++ b/source/blender/editors/interface/interface_icons.c
@@ -31,6 +31,7 @@
 #include "GPU_batch_presets.h"
 #include "GPU_immediate.h"
 #include "GPU_matrix.h"
+#include "GPU_shader_shared.h"
 #include "GPU_state.h"
 #include "GPU_texture.h"
 
@@ -1582,18 +1583,21 @@ static void icon_draw_cache_texture_flush_ex(GPUTexture *texture,
   GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR);
   GPU_shader_bind(shader);
 
-  const int img_binding = GPU_shader_get_texture_binding(shader, "image");
-  const int data_loc = GPU_shader_get_uniform(shader, "calls_data");
+  const int data_loc = GPU_shader_get_uniform_block(shader, "multi_rect_data");
+  GPUUniformBuf *ubo = GPU_uniformbuf_create_ex(
+      sizeof(struct MultiRectCallData), texture_draw_calls->drawcall_cache, __func__);
+  GPU_uniformbuf_bind(ubo, data_loc);
 
+  const int img_binding = GPU_shader_get_texture_binding(shader, "image");
   GPU_texture_bind_ex(texture, GPU_SAMPLER_ICON, img_binding, false);
-  GPU_shader_uniform_vector(
-      shader, data_loc, 4, ICON_DRAW_CACHE_SIZE * 3, (float *)texture_draw_calls->drawcall_cache);
 
   GPUBatch *quad = GPU_batch_preset_quad();
   GPU_batch_set_shader(quad, shader);
   GPU_batch_draw_instanced(quad, texture_draw_calls->calls);
 
   GPU_texture_unbind(texture);
+  GPU_uniformbuf_unbind(ubo);
+  GPU_uniformbuf_free(ubo);
 
   texture_draw_calls->calls = 0;
 }
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 691c131b1c0..bb7e9e8b26f 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -432,6 +432,7 @@ shaders/infos/gpu_shader_2D_point_varying_size_varying_color_info.hh
 shaders/infos/gpu_shader_2D_point_uniform_size_uniform_color_aa_info.hh
 shaders/infos/gpu_shader_2D_point_uniform_size_uniform_color_outline_aa_info.hh
 shaders/infos/gpu_shader_2D_area_borders_info.hh
+shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh
 shaders/infos/gpu_shader_instance_varying_color_varying_size_info.hh
 shaders/infos/gpu_shader_3D_point_info.hh
 shaders/infos/gpu_shader_2D_nodelink_info.hh
diff --git a/source/blender/gpu/GPU_shader_shared.h b/source/blender/gpu/GPU_shader_shared.h
index 4d277921773..f400e151487 100644
--- a/source/blender/gpu/GPU_shader_shared.h
+++ b/source/blender/gpu/GPU_shader_shared.h
@@ -80,3 +80,10 @@ struct SimpleLightingData {
   float _pad;
 };
 BLI_STATIC_ASSERT_ALIGN(struct SimpleLightingData, 16)
+
+#define MAX_CALLS 16
+
+struct MultiRectCallData {
+  float4 calls_data[MAX_CALLS * 3];
+};
+BLI_STATIC_ASSERT_ALIGN(struct MultiRectCallData, 16)
diff --git a/source/blender/gpu/intern/gpu_shader_builtin.c b/source/blender/gpu/intern/gpu_shader_builtin.c
index cae7383f885..13f1774df03 100644
--- a/source/blender/gpu/intern/gpu_shader_builtin.c
+++ b/source/blender/gpu/intern/gpu_shader_builtin.c
@@ -210,12 +210,9 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = {
         },
     [GPU_SHADER_2D_IMAGE_RECT_COLOR] = {.name = "GPU_SHADER_2D_IMAGE_RECT_COLOR",
                                         .create_info = "gpu_shader_2D_image_rect_color"},
-    [GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] =
-        {
-            .name = "GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR",
-            .vert = datatoc_gpu_shader_2D_image_multi_rect_vert_glsl,
-            .frag = datatoc_gpu_shader_image_varying_color_frag_glsl,
-        },
+    [GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR] = {.name = "GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR",
+                                              .create_info =
+                                                  "gpu_shader_2D_image_multi_rect_color"},
 
     [GPU_SHADER_3D_UNIFORM_COLOR] =
         {
diff --git a/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl
index f4d8a941a6d..9851e08fe2e 100644
--- a/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl
@@ -17,9 +17,9 @@ in vec2 pos;
 
 void main()
 {
-  vec4 rect = calls_data[gl_InstanceID * 3];
-  vec4 tex = calls_data[gl_InstanceID * 3 + 1];
-  finalColor = calls_data[gl_InstanceID * 3 + 2];
+  vec4 rect = multi_rect_data.calls_data[gl_InstanceID * 3];
+  vec4 tex = multi_rect_data.calls_data[gl_InstanceID * 3 + 1];
+  finalColor = multi_rect_data.calls_data[gl_InstanceID * 3 + 2];
 
   /* Use pos to select the right swizzle (instead of gl_VertexID)
    * in order to workaround an OSX driver bug. */
diff --git a/source/blender/gpu/shaders/infos/gpu_interface_info.hh b/source/blender/gpu/shaders/infos/gpu_interface_info.hh
index b53b60fa587..d5ad333638f 100644
--- a/source/blender/gpu/shaders/infos/gpu_interface_info.hh
+++ b/source/blender/gpu/shaders/infos/gpu_interface_info.hh
@@ -31,3 +31,6 @@ GPU_SHADER_INTERFACE_INFO(smooth_color_iface, "").smooth(Type::VEC4, "finalColor
 GPU_SHADER_INTERFACE_INFO(smooth_tex_coord_interp_iface, "").smooth(Type::VEC2, "texCoord_interp");
 GPU_SHADER_INTERFACE_INFO(smooth_radii_iface, "").smooth(Type::VEC2, "radii");
 GPU_SHADER_INTERFACE_INFO(smooth_radii_outline_iface, "").smooth(Type::VEC4, "radii");
+GPU_SHADER_INTERFACE_INFO(flat_color_smooth_tex_coord_interp_iface, "")
+    .flat(Type::VEC4, "finalColor")
+    .smooth(Type::VEC2, "texCoord_interp");
diff --git a/source/blender/gpu/shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh
new file mode 100644
index 00000000000..4b154a045ee
--- /dev/null
+++ b/source/blender/gpu/shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh
@@ -0,0 +1,13 @@
+#include "gpu_interface_info.hh"
+#include "gpu_shader_create_info.hh"
+
+GPU_SHADER_CREATE_INFO(gpu_shader_2D_image_multi_rect_color)
+    .vertex_in(0, Type::VEC2, "pos")
+    .vertex_out(flat_color_smooth_tex_coord_interp_iface)
+    .fragment_out(0, Type::VEC4, "fragColor")
+    .uniform_buf(0, "MultiRectCallData", "multi_rect_data")
+    .sampler(0, ImageType::FLOAT_2D, "image")
+    .typedef_source("GPU_shader_shared.h")
+    .vertex_source("gpu_shader_2D_image_multi_rect_vert.glsl")
+    .fragment_source("gpu_shader_image_varying_color_frag.glsl")
+    .do_static_compilation(true);



More information about the Bf-blender-cvs mailing list