diff --git a/source/blender/editors/interface/interface_icons.cc b/source/blender/editors/interface/interface_icons.cc index 7e0b17aa128..fcba1f677c4 100644 --- a/source/blender/editors/interface/interface_icons.cc +++ b/source/blender/editors/interface/interface_icons.cc @@ -1617,12 +1617,12 @@ static void icon_draw_cache_texture_flush_ex(GPUTexture *texture, return; } - GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR); + GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_ICON_MULTI); GPU_shader_bind(shader); - const int data_binding = GPU_shader_get_ubo_binding(shader, "multi_rect_data"); + const int data_binding = GPU_shader_get_ubo_binding(shader, "multi_icon_data"); GPUUniformBuf *ubo = GPU_uniformbuf_create_ex( - sizeof(MultiRectCallData), texture_draw_calls->drawcall_cache, __func__); + sizeof(MultiIconCallData), texture_draw_calls->drawcall_cache, __func__); GPU_uniformbuf_bind(ubo, data_binding); const int img_binding = GPU_shader_get_sampler_binding(shader, "image"); @@ -1798,7 +1798,7 @@ static void icon_draw_texture(float x, GPU_shader_bind(shader); const int img_binding = GPU_shader_get_sampler_binding(shader, "image"); - const int color_loc = GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR); + const int color_loc = GPU_shader_get_uniform(shader, "finalColor"); const int rect_tex_loc = GPU_shader_get_uniform(shader, "rect_icon"); const int rect_geom_loc = GPU_shader_get_uniform(shader, "rect_geom"); diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 29dd757b514..31e04ee5c02 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -348,7 +348,7 @@ set(GLSL_SRC shaders/gpu_shader_2D_line_dashed_frag.glsl shaders/gpu_shader_2D_image_vert.glsl shaders/gpu_shader_2D_image_rect_vert.glsl - shaders/gpu_shader_2D_image_multi_rect_vert.glsl + shaders/gpu_shader_icon_multi_vert.glsl shaders/gpu_shader_icon_frag.glsl shaders/gpu_shader_icon_vert.glsl shaders/gpu_shader_image_frag.glsl @@ -357,7 +357,6 @@ set(GLSL_SRC shaders/gpu_shader_image_overlays_stereo_merge_frag.glsl shaders/gpu_shader_image_shuffle_color_frag.glsl shaders/gpu_shader_image_color_frag.glsl - shaders/gpu_shader_image_varying_color_frag.glsl shaders/gpu_shader_3D_image_vert.glsl shaders/gpu_shader_3D_vert.glsl shaders/gpu_shader_3D_normal_vert.glsl @@ -639,7 +638,6 @@ set(SRC_SHADER_CREATE_INFOS shaders/infos/gpu_shader_2D_diag_stripes_info.hh shaders/infos/gpu_shader_2D_image_desaturate_color_info.hh shaders/infos/gpu_shader_2D_image_info.hh - shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh shaders/infos/gpu_shader_2D_image_overlays_merge_info.hh shaders/infos/gpu_shader_2D_image_overlays_stereo_merge_info.hh shaders/infos/gpu_shader_2D_image_rect_color_info.hh diff --git a/source/blender/gpu/GPU_shader_builtin.h b/source/blender/gpu/GPU_shader_builtin.h index bd9fa9eb232..3f08d64fa22 100644 --- a/source/blender/gpu/GPU_shader_builtin.h +++ b/source/blender/gpu/GPU_shader_builtin.h @@ -39,7 +39,7 @@ typedef enum eGPUBuiltinShader { /** Draw a texture with a desaturation factor. */ GPU_SHADER_2D_IMAGE_DESATURATE_COLOR, /** Draw a group of texture rectangle with an associated color multiplied. */ - GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR, + GPU_SHADER_ICON_MULTI, /** Draw a two color checker based on screen position (not UV coordinates). */ GPU_SHADER_2D_CHECKER, /** Draw diagonal stripes with two alternating colors. */ diff --git a/source/blender/gpu/GPU_shader_shared.h b/source/blender/gpu/GPU_shader_shared.h index 66face85b07..dd3636748d0 100644 --- a/source/blender/gpu/GPU_shader_shared.h +++ b/source/blender/gpu/GPU_shader_shared.h @@ -82,10 +82,10 @@ BLI_STATIC_ASSERT_ALIGN(struct SimpleLightingData, 16) #define MAX_CALLS 16 -struct MultiRectCallData { +struct MultiIconCallData { float4 calls_data[MAX_CALLS * 3]; }; -BLI_STATIC_ASSERT_ALIGN(struct MultiRectCallData, 16) +BLI_STATIC_ASSERT_ALIGN(struct MultiIconCallData, 16) enum TestStatus { TEST_STATUS_NONE = 0, diff --git a/source/blender/gpu/intern/gpu_shader_builtin.cc b/source/blender/gpu/intern/gpu_shader_builtin.cc index 7cf183bdf94..813fb4ff4e5 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.cc +++ b/source/blender/gpu/intern/gpu_shader_builtin.cc @@ -42,8 +42,8 @@ static const char *builtin_shader_create_info_name(eGPUBuiltinShader shader) return "gpu_shader_2D_image_shuffle_color"; case GPU_SHADER_2D_IMAGE_RECT_COLOR: return "gpu_shader_2D_image_rect_color"; - case GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR: - return "gpu_shader_2D_image_multi_rect_color"; + case GPU_SHADER_ICON_MULTI: + return "gpu_shader_icon_multi"; case GPU_SHADER_3D_UNIFORM_COLOR: return "gpu_shader_3D_uniform_color"; case GPU_SHADER_3D_FLAT_COLOR: diff --git a/source/blender/gpu/metal/mtl_context.hh b/source/blender/gpu/metal/mtl_context.hh index 5b37fefa2d8..3523cd47614 100644 --- a/source/blender/gpu/metal/mtl_context.hh +++ b/source/blender/gpu/metal/mtl_context.hh @@ -768,6 +768,7 @@ class MTLContext : public Context { void texture_unbind_all(); id get_sampler_from_state(MTLSamplerState state); id generate_sampler_from_state(MTLSamplerState state); + id generate_icon_sampler(); id get_default_sampler_state(); /* Metal Context pipeline state. */ diff --git a/source/blender/gpu/metal/mtl_context.mm b/source/blender/gpu/metal/mtl_context.mm index 14e53844722..3c1872d66f0 100644 --- a/source/blender/gpu/metal/mtl_context.mm +++ b/source/blender/gpu/metal/mtl_context.mm @@ -223,11 +223,13 @@ MTLContext::MTLContext(void *ghost_window, void *ghost_context) } /* Initialize samplers. */ - for (uint i = 0; i < GPU_SAMPLER_MAX; i++) { + for (uint i = 0; i < GPU_SAMPLER_ICON; i++) { MTLSamplerState state; state.state = static_cast(i); sampler_state_cache_[i] = this->generate_sampler_from_state(state); } + /* Special sampler for icons. */ + sampler_state_cache_[GPU_SAMPLER_ICON] = this->generate_icon_sampler(); } MTLContext::~MTLContext() @@ -2024,7 +2026,6 @@ id MTLContext::get_sampler_from_state(MTLSamplerState sampler_s id MTLContext::generate_sampler_from_state(MTLSamplerState sampler_state) { - /* Check if sampler already exists for given state. */ MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; descriptor.normalizedCoordinates = true; @@ -2067,6 +2068,21 @@ id MTLContext::generate_sampler_from_state(MTLSamplerState samp return state; } +id MTLContext::generate_icon_sampler() +{ + MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; + descriptor.minFilter = MTLSamplerMinMagFilterLinear; + descriptor.magFilter = MTLSamplerMinMagFilterLinear; + descriptor.mipFilter = MTLSamplerMipFilterNearest; + descriptor.lodMinClamp = 0; + descriptor.lodMaxClamp = 1; + + id icon_state = [this->device newSamplerStateWithDescriptor:descriptor]; + BLI_assert(icon_state != nil); + [descriptor autorelease]; + return icon_state; +} + id MTLContext::get_default_sampler_state() { if (default_sampler_state_ == nil) { diff --git a/source/blender/gpu/opengl/gl_texture.cc b/source/blender/gpu/opengl/gl_texture.cc index 424fbf702ea..35a25143209 100644 --- a/source/blender/gpu/opengl/gl_texture.cc +++ b/source/blender/gpu/opengl/gl_texture.cc @@ -594,11 +594,11 @@ void GLTexture::samplers_init() } samplers_update(); - /* Custom sampler for icons. */ + /* Custom sampler for icons. + * NOTE: The icon texture is sampled within the shader using a -0.5f lod bias. */ GLuint icon_sampler = samplers_[GPU_SAMPLER_ICON]; glSamplerParameteri(icon_sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glSamplerParameteri(icon_sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glSamplerParameterf(icon_sampler, GL_TEXTURE_LOD_BIAS, -0.5f); debug::object_label(GL_SAMPLER, icon_sampler, "icons"); } diff --git a/source/blender/gpu/shaders/gpu_shader_icon_frag.glsl b/source/blender/gpu/shaders/gpu_shader_icon_frag.glsl index 4452349f23c..61bbe6ecb2e 100644 --- a/source/blender/gpu/shaders/gpu_shader_icon_frag.glsl +++ b/source/blender/gpu/shaders/gpu_shader_icon_frag.glsl @@ -8,6 +8,10 @@ void main() { + /* Sample texture with LOD BIAS. Used instead of custom lod bias in GPU_SAMPLER_ICON. */ + fragColor = texture(image, texCoord_interp, -0.5) * finalColor; + +#ifdef DO_CORNER_MASKING /* Top-left rounded corner parameters. */ const float circle_radius_outer = 0.1; const float circle_radius_inner = 0.075; @@ -18,7 +22,6 @@ void main() const float mask_transparency = 0.25; vec2 circle_center = vec2(circle_radius_outer - text_width, 0.5); - fragColor = texture(image, texCoord_interp) * color; /* radius in icon space (1 is the icon width). */ float radius = length(mask_coord_interp - circle_center); @@ -39,4 +42,5 @@ void main() } fragColor = mix(vec4(0.0), fragColor, max(mask_transparency, mask)); +#endif } diff --git a/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl b/source/blender/gpu/shaders/gpu_shader_icon_multi_vert.glsl similarity index 78% rename from source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl rename to source/blender/gpu/shaders/gpu_shader_icon_multi_vert.glsl index 0309a98f965..c9a22f75ef0 100644 --- a/source/blender/gpu/shaders/gpu_shader_2D_image_multi_rect_vert.glsl +++ b/source/blender/gpu/shaders/gpu_shader_icon_multi_vert.glsl @@ -5,9 +5,9 @@ void main() { - 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]; + vec4 rect = multi_icon_data.calls_data[gl_InstanceID * 3]; + vec4 tex = multi_icon_data.calls_data[gl_InstanceID * 3 + 1]; + finalColor = multi_icon_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/gpu_shader_image_varying_color_frag.glsl b/source/blender/gpu/shaders/gpu_shader_image_varying_color_frag.glsl deleted file mode 100644 index f1f62832552..00000000000 --- a/source/blender/gpu/shaders/gpu_shader_image_varying_color_frag.glsl +++ /dev/null @@ -1,5 +0,0 @@ - -void main() -{ - fragColor = texture(image, texCoord_interp) * finalColor; -} 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 deleted file mode 100644 index b2776175fe3..00000000000 --- a/source/blender/gpu/shaders/infos/gpu_shader_2D_image_multi_rect_color_info.hh +++ /dev/null @@ -1,15 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ - -#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); diff --git a/source/blender/gpu/shaders/infos/gpu_shader_icon_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_icon_info.hh index 3d4077bdb09..0aba2189c53 100644 --- a/source/blender/gpu/shaders/infos/gpu_shader_icon_info.hh +++ b/source/blender/gpu/shaders/infos/gpu_shader_icon_info.hh @@ -9,10 +9,11 @@ #include "gpu_shader_create_info.hh" GPU_SHADER_CREATE_INFO(gpu_shader_icon) + .define("DO_CORNER_MASKING") .vertex_out(smooth_icon_interp_iface) .fragment_out(0, Type::VEC4, "fragColor") .push_constant(Type::MAT4, "ModelViewProjectionMatrix") - .push_constant(Type::VEC4, "color") + .push_constant(Type::VEC4, "finalColor") .push_constant(Type::VEC4, "rect_icon") .push_constant(Type::VEC4, "rect_geom") .push_constant(Type::FLOAT, "text_width") @@ -20,3 +21,14 @@ GPU_SHADER_CREATE_INFO(gpu_shader_icon) .vertex_source("gpu_shader_icon_vert.glsl") .fragment_source("gpu_shader_icon_frag.glsl") .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpu_shader_icon_multi) + .vertex_in(0, Type::VEC2, "pos") + .vertex_out(flat_color_smooth_tex_coord_interp_iface) + .fragment_out(0, Type::VEC4, "fragColor") + .uniform_buf(0, "MultiIconCallData", "multi_icon_data") + .sampler(0, ImageType::FLOAT_2D, "image") + .typedef_source("GPU_shader_shared.h") + .vertex_source("gpu_shader_icon_multi_vert.glsl") + .fragment_source("gpu_shader_icon_frag.glsl") + .do_static_compilation(true); diff --git a/source/blender/gpu/tests/gpu_shader_builtin_test.cc b/source/blender/gpu/tests/gpu_shader_builtin_test.cc index bb3f9f1a2ae..be6068ac084 100644 --- a/source/blender/gpu/tests/gpu_shader_builtin_test.cc +++ b/source/blender/gpu/tests/gpu_shader_builtin_test.cc @@ -36,7 +36,7 @@ static void test_shader_builtin() test_compile_builtin_shader(GPU_SHADER_3D_IMAGE_COLOR, GPU_SHADER_CFG_DEFAULT); test_compile_builtin_shader(GPU_SHADER_2D_IMAGE_DESATURATE_COLOR, GPU_SHADER_CFG_DEFAULT); test_compile_builtin_shader(GPU_SHADER_2D_IMAGE_RECT_COLOR, GPU_SHADER_CFG_DEFAULT); - test_compile_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR, GPU_SHADER_CFG_DEFAULT); + test_compile_builtin_shader(GPU_SHADER_ICON_MULTI, GPU_SHADER_CFG_DEFAULT); test_compile_builtin_shader(GPU_SHADER_2D_CHECKER, GPU_SHADER_CFG_DEFAULT); test_compile_builtin_shader(GPU_SHADER_2D_DIAG_STRIPES, GPU_SHADER_CFG_DEFAULT); test_compile_builtin_shader(GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR, GPU_SHADER_CFG_DEFAULT);