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.
This commit is contained in:
Jeroen Bakker 2022-01-18 13:13:23 +01:00
parent 2486346f6f
commit 08d008a508
Notes: blender-bot 2023-02-14 10:21:11 +01:00
Referenced by issue #95009, GPU: Add CreateInfo Structs for builtin shaders
Referenced by issue #94975, Python API for GPUShaderCreateInfo
7 changed files with 38 additions and 13 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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)

View File

@ -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] =
{

View File

@ -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. */

View File

@ -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");

View File

@ -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);