[Bf-blender-cvs] [21c658b718b] master: GPUShader: Implement workaround for gizmo drawing on sRGB framebuffer

Clément Foucault noreply at git.blender.org
Tue Apr 14 20:44:57 CEST 2020


Commit: 21c658b718b9bd0f79f435a6a8a8603c365264a5
Author: Clément Foucault
Date:   Tue Apr 14 20:44:45 2020 +0200
Branches: master
https://developer.blender.org/rB21c658b718b9bd0f79f435a6a8a8603c365264a5

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

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

M	release/scripts/addons
M	source/blender/draw/intern/draw_manager.c
M	source/blender/editors/interface/interface_widgets.c
M	source/blender/editors/mesh/editmesh_knife.c
M	source/blender/editors/space_image/space_image.c
M	source/blender/editors/uvedit/uvedit_draw.c
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/GPU_shader_interface.h
M	source/blender/gpu/intern/gpu_batch.c
M	source/blender/gpu/intern/gpu_framebuffer.c
M	source/blender/gpu/intern/gpu_immediate.c
M	source/blender/gpu/intern/gpu_shader.c
M	source/blender/gpu/intern/gpu_shader_interface.c
M	source/blender/gpu/shaders/gpu_shader_2D_smooth_color_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_3D_smooth_color_frag.glsl
A	source/blender/gpu/shaders/gpu_shader_colorspace_lib.glsl
M	source/blender/gpu/shaders/gpu_shader_flat_color_frag.glsl
M	source/blender/gpu/shaders/gpu_shader_uniform_color_frag.glsl
M	source/blender/python/gpu/gpu_py_shader.c

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

diff --git a/release/scripts/addons b/release/scripts/addons
index d348bde0f96..47a32a5370d 160000
--- a/release/scripts/addons
+++ b/release/scripts/addons
@@ -1 +1 @@
-Subproject commit d348bde0f96809e289b0514c015cafb97f2dcf79
+Subproject commit 47a32a5370d36942674621e5a03e57e8dd4986d8
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index 1434cac5f97..09b9b63d07a 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -1296,9 +1296,6 @@ void DRW_draw_callbacks_post_scene(void)
     DRW_state_reset();
 
     GPU_framebuffer_bind(dfbl->overlay_fb);
-    /* Disable sRGB encoding from the fixed function pipeline since all the drawing in this
-     * function is done with sRGB color. Avoid double transform. */
-    glDisable(GL_FRAMEBUFFER_SRGB);
 
     GPU_matrix_projection_set(rv3d->winmat);
     GPU_matrix_set(rv3d->viewmat);
diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c
index fe2debd4344..361e5e76acc 100644
--- a/source/blender/editors/interface/interface_widgets.c
+++ b/source/blender/editors/interface/interface_widgets.c
@@ -1348,6 +1348,7 @@ void UI_widgetbase_draw_cache_flush(void)
                                 (float *)g_widget_base_batch.params);
     GPU_batch_uniform_3fv(batch, "checkerColorAndSize", checker_params);
     GPU_matrix_bind(batch->interface);
+    GPU_shader_set_srgb_uniform(batch->interface);
     GPU_batch_bind(batch);
     GPU_batch_draw_advanced(batch, 0, 0, 0, g_widget_base_batch.count);
 
diff --git a/source/blender/editors/mesh/editmesh_knife.c b/source/blender/editors/mesh/editmesh_knife.c
index fd92d864a28..f94cd778e13 100644
--- a/source/blender/editors/mesh/editmesh_knife.c
+++ b/source/blender/editors/mesh/editmesh_knife.c
@@ -1162,6 +1162,7 @@ static void knifetool_draw(const bContext *UNUSED(C), ARegion *UNUSED(region), v
     rgba_uchar_to_float(fcol, kcd->colors.point_a);
     GPU_batch_uniform_4fv(batch, "color", fcol);
     GPU_matrix_bind(batch->interface);
+    GPU_shader_set_srgb_uniform(batch->interface);
     GPU_point_size(11);
     if (snapped_verts_count > 0) {
       GPU_batch_draw_advanced(batch, 0, snapped_verts_count, 0, 0);
diff --git a/source/blender/editors/space_image/space_image.c b/source/blender/editors/space_image/space_image.c
index af94b5b6e21..1e1d4373fea 100644
--- a/source/blender/editors/space_image/space_image.c
+++ b/source/blender/editors/space_image/space_image.c
@@ -653,13 +653,13 @@ static void image_main_region_draw(const bContext *C, ARegion *region)
   GPU_clear(GPU_COLOR_BIT);
 
   GPU_framebuffer_bind(fbl->overlay_fb);
-  glDisable(GL_FRAMEBUFFER_SRGB);
 
   /* XXX not supported yet, disabling for now */
   scene->r.scemode &= ~R_COMP_CROP;
 
   /* clear and setup matrix */
   UI_GetThemeColor3fv(TH_BACK, col);
+  srgb_to_linearrgb_v3_v3(col, col);
   GPU_clear_color(col[0], col[1], col[2], 1.0f);
   GPU_clear(GPU_COLOR_BIT);
   GPU_depth_test(false);
diff --git a/source/blender/editors/uvedit/uvedit_draw.c b/source/blender/editors/uvedit/uvedit_draw.c
index 1e576f6fea4..6ea1bbbcc10 100644
--- a/source/blender/editors/uvedit/uvedit_draw.c
+++ b/source/blender/editors/uvedit/uvedit_draw.c
@@ -265,6 +265,7 @@ static void draw_uvs_texpaint(const Scene *scene, Object *ob, Depsgraph *depsgra
     bool prev_ma_match = (mpoly->mat_nr == (ob_eval->actcol - 1));
 
     GPU_matrix_bind(geom->interface);
+    GPU_shader_set_srgb_uniform(geom->interface);
     GPU_batch_bind(geom);
 
     /* TODO(fclem): If drawcall count becomes a problem in the future
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index a8a0288f895..85e81607225 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -314,6 +314,7 @@ data_to_c_simple(shaders/gpu_shader_gpencil_fill_vert.glsl SRC)
 data_to_c_simple(shaders/gpu_shader_gpencil_fill_frag.glsl SRC)
 
 data_to_c_simple(shaders/gpu_shader_cfg_world_clip_lib.glsl SRC)
+data_to_c_simple(shaders/gpu_shader_colorspace_lib.glsl SRC)
 
 data_to_c_simple(shaders/gpu_shader_common_obinfos_lib.glsl SRC)
 
diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h
index bb26f5d41a3..1fb0b7c1f6b 100644
--- a/source/blender/gpu/GPU_shader.h
+++ b/source/blender/gpu/GPU_shader.h
@@ -31,6 +31,7 @@ extern "C" {
 typedef struct GPUShader GPUShader;
 struct GPUTexture;
 struct GPUUniformBuffer;
+struct GPUShaderInterface;
 
 /* GPU Shader
  * - only for fragment shaders now
@@ -49,6 +50,11 @@ GPUShader *GPU_shader_create(const char *vertexcode,
                              const char *libcode,
                              const char *defines,
                              const char *shader_name);
+GPUShader *GPU_shader_create_from_python(const char *vertexcode,
+                                         const char *fragcode,
+                                         const char *geocode,
+                                         const char *libcode,
+                                         const char *defines);
 GPUShader *GPU_shader_create_ex(const char *vertexcode,
                                 const char *fragcode,
                                 const char *geocode,
@@ -83,6 +89,8 @@ int GPU_shader_get_program(GPUShader *shader);
 
 void *GPU_shader_get_interface(GPUShader *shader);
 
+void GPU_shader_set_srgb_uniform(const struct GPUShaderInterface *interface);
+
 int GPU_shader_get_uniform(GPUShader *shader, const char *name);
 int GPU_shader_get_uniform_ensure(GPUShader *shader, const char *name);
 int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin);
@@ -101,6 +109,8 @@ int GPU_shader_get_attribute(GPUShader *shader, const char *name);
 
 char *GPU_shader_get_binary(GPUShader *shader, uint *r_binary_format, int *r_binary_len);
 
+void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear);
+
 /* Builtin/Non-generated shaders */
 typedef enum eGPUBuiltinShader {
   /* specialized drawing */
diff --git a/source/blender/gpu/GPU_shader_interface.h b/source/blender/gpu/GPU_shader_interface.h
index f0c1c4c0b98..3e7bad409a3 100644
--- a/source/blender/gpu/GPU_shader_interface.h
+++ b/source/blender/gpu/GPU_shader_interface.h
@@ -56,6 +56,7 @@ typedef enum {
   GPU_UNIFORM_BASE_INSTANCE,  /* int baseInstance */
   GPU_UNIFORM_RESOURCE_CHUNK, /* int resourceChunk */
   GPU_UNIFORM_RESOURCE_ID,    /* int resourceId */
+  GPU_UNIFORM_SRGB_TRANSFORM, /* bool srgbTarget */
 
   GPU_UNIFORM_CUSTOM, /* custom uniform, not one of the above built-ins */
 
diff --git a/source/blender/gpu/intern/gpu_batch.c b/source/blender/gpu/intern/gpu_batch.c
index b0a24b1f2ff..3e0a1e57664 100644
--- a/source/blender/gpu/intern/gpu_batch.c
+++ b/source/blender/gpu/intern/gpu_batch.c
@@ -675,6 +675,7 @@ void GPU_batch_draw(GPUBatch *batch)
 #endif
   GPU_batch_program_use_begin(batch);
   GPU_matrix_bind(batch->interface);  // external call.
+  GPU_shader_set_srgb_uniform(batch->interface);
 
   GPU_batch_bind(batch);
   GPU_batch_draw_advanced(batch, 0, 0, 0, 0);
diff --git a/source/blender/gpu/intern/gpu_framebuffer.c b/source/blender/gpu/intern/gpu_framebuffer.c
index 59d0be2cefb..e6092b55fc4 100644
--- a/source/blender/gpu/intern/gpu_framebuffer.c
+++ b/source/blender/gpu/intern/gpu_framebuffer.c
@@ -514,6 +514,11 @@ void GPU_framebuffer_bind(GPUFrameBuffer *fb)
   if (GPU_framebuffer_active_get() != fb) {
     glBindFramebuffer(GL_FRAMEBUFFER, fb->object);
     glEnable(GL_FRAMEBUFFER_SRGB);
+
+    GPUTexture *first_target = fb->attachments[GPU_FB_COLOR_ATTACHMENT0].tex;
+    const bool is_srgb_target = (first_target &&
+                                 (GPU_texture_format(first_target) == GPU_SRGB8_A8));
+    GPU_shader_set_framebuffer_srgb_target(is_srgb_target);
   }
 
   gpu_framebuffer_current_set(fb);
@@ -549,6 +554,7 @@ void GPU_framebuffer_restore(void)
     glBindFramebuffer(GL_FRAMEBUFFER, GPU_framebuffer_default());
     gpu_framebuffer_current_set(NULL);
     glDisable(GL_FRAMEBUFFER_SRGB);
+    GPU_shader_set_framebuffer_srgb_target(false);
   }
 }
 
@@ -944,6 +950,7 @@ void GPU_offscreen_bind(GPUOffScreen *ofs, bool save)
   GPUFrameBuffer *ofs_fb = gpu_offscreen_fb_get(ofs);
   GPU_framebuffer_bind(ofs_fb);
   glDisable(GL_FRAMEBUFFER_SRGB);
+  GPU_shader_set_framebuffer_srgb_target(false);
 }
 
 void GPU_offscreen_unbind(GPUOffScreen *UNUSED(ofs), bool restore)
diff --git a/source/blender/gpu/intern/gpu_immediate.c b/source/blender/gpu/intern/gpu_immediate.c
index 4523df8be7c..b30fbd66670 100644
--- a/source/blender/gpu/intern/gpu_immediate.c
+++ b/source/blender/gpu/intern/gpu_immediate.c
@@ -151,6 +151,7 @@ void immBindProgram(GLuint program, const GPUShaderInterface *shaderface)
   glUseProgram(program);
   get_attr_locations(&imm.vertex_format, &imm.attr_binding, shaderface);
   GPU_matrix_bind(shaderface);
+  GPU_shader_set_srgb_uniform(shaderface);
 }
 
 void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
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}};

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list