From 21c658b718b9bd0f79f435a6a8a8603c365264a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Tue, 14 Apr 2020 20:44:45 +0200 Subject: GPUShader: Implement workaround for gizmo drawing on sRGB framebuffer This solution involves adding a uniform to each fragment shader that is used by gizmo drawing and use the framebuffer state to set this uniform accordingly. This solution can also be carried to external shaders (addons). A single line of code would then be enough to fix the issue. The only trickery here is the dummy define: `#define srgb_to_framebuffer_space(a)` This is in order to avoid breaking other DRW shaders that use the same fragment shader code but do not need the tranformation. Related to T74139 Reviewed By: brecht, campbellbarton Differential Revision: https://developer.blender.org/D7261 --- source/blender/gpu/intern/gpu_shader.c | 56 +++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) (limited to 'source/blender/gpu/intern/gpu_shader.c') diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index 1a0daf4ac41..dc26a1a8e7b 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -134,21 +134,25 @@ extern char datatoc_gpu_shader_gpencil_fill_vert_glsl[]; extern char datatoc_gpu_shader_gpencil_fill_frag_glsl[]; extern char datatoc_gpu_shader_cfg_world_clip_lib_glsl[]; +extern char datatoc_gpu_shader_colorspace_lib_glsl[]; + const struct GPUShaderConfigData GPU_shader_cfg_data[GPU_SHADER_CFG_LEN] = { [GPU_SHADER_CFG_DEFAULT] = { .lib = "", - .def = "", + .def = "#define blender_srgb_to_framebuffer_space(a) a\n", }, [GPU_SHADER_CFG_CLIPPED] = { .lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl, - .def = "#define USE_WORLD_CLIP_PLANES\n", + .def = "#define USE_WORLD_CLIP_PLANES\n" + "#define blender_srgb_to_framebuffer_space(a) a\n", }, }; /* cache of built-in shaders (each is created on first use) */ static GPUShader *builtin_shaders[GPU_SHADER_CFG_LEN][GPU_SHADER_BUILTIN_LEN] = {{NULL}}; +static int g_shader_builtin_srgb_transform = 0; #ifndef NDEBUG static uint g_shaderid = 0; @@ -289,6 +293,28 @@ GPUShader *GPU_shader_create(const char *vertexcode, vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, shname); } +GPUShader *GPU_shader_create_from_python(const char *vertexcode, + const char *fragcode, + const char *geocode, + const char *libcode, + const char *defines) +{ + char *libcodecat = NULL; + + if (libcode == NULL) { + libcode = datatoc_gpu_shader_colorspace_lib_glsl; + } + else { + libcode = libcodecat = BLI_strdupcat(libcode, datatoc_gpu_shader_colorspace_lib_glsl); + } + + GPUShader *sh = GPU_shader_create_ex( + vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, NULL); + + MEM_SAFE_FREE(libcodecat); + return sh; +} + GPUShader *GPU_shader_load_from_binary(const char *binary, const int binary_format, const int binary_len, @@ -629,6 +655,7 @@ void GPU_shader_bind(GPUShader *shader) glUseProgram(shader->program); GPU_matrix_bind(shader->interface); + GPU_shader_set_srgb_uniform(shader->interface); } void GPU_shader_unbind(void) @@ -835,6 +862,15 @@ void GPU_shader_uniform_texture(GPUShader *UNUSED(shader), int location, GPUText glUniform1i(location, number); } +void GPU_shader_set_srgb_uniform(const GPUShaderInterface *interface) +{ + const GPUShaderInput *srgb_uniform = GPU_shaderinterface_uniform_builtin( + interface, GPU_UNIFORM_SRGB_TRANSFORM); + if (srgb_uniform) { + glUniform1i(srgb_uniform->location, g_shader_builtin_srgb_transform); + } +} + int GPU_shader_get_attribute(GPUShader *shader, const char *name) { BLI_assert(shader && shader->program); @@ -859,6 +895,11 @@ char *GPU_shader_get_binary(GPUShader *shader, uint *r_binary_format, int *r_bin return r_binary; } +void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear) +{ + g_shader_builtin_srgb_transform = use_srgb_to_linear; +} + static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { [GPU_SHADER_TEXT] = { @@ -983,6 +1024,7 @@ static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN] = { { .vert = datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl, .frag = datatoc_gpu_shader_uniform_color_frag_glsl, + .defs = datatoc_gpu_shader_colorspace_lib_glsl, }, [GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR] = @@ -1162,8 +1204,12 @@ GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, /* common case */ if (sh_cfg == GPU_SHADER_CFG_DEFAULT) { - *sh_p = GPU_shader_create( - stages->vert, stages->frag, stages->geom, NULL, stages->defs, __func__); + *sh_p = GPU_shader_create_from_arrays({ + .vert = (const char *[]){stages->vert, NULL}, + .geom = (const char *[]){stages->geom, NULL}, + .frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, + .defs = (const char *[]){stages->defs, NULL}, + }); } else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) { /* Remove eventually, for now ensure support for each shader has been added. */ @@ -1182,7 +1228,7 @@ GPUShader *GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, *sh_p = GPU_shader_create_from_arrays({ .vert = (const char *[]){world_clip_lib, stages->vert, NULL}, .geom = (const char *[]){stages->geom ? world_clip_lib : NULL, stages->geom, NULL}, - .frag = (const char *[]){stages->frag, NULL}, + .frag = (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL}, .defs = (const char *[]){world_clip_def, stages->defs, NULL}, }); } -- cgit v1.2.3