[Bf-blender-cvs] [f2ea8e217b0] blender2.8: Object Mode : Add Outline FXAA

Clément Foucault noreply at git.blender.org
Fri Sep 22 17:35:51 CEST 2017


Commit: f2ea8e217b0b5bc02d1fbe92c33ee7fe05ae2c46
Author: Clément Foucault
Date:   Fri Sep 22 17:30:39 2017 +0200
Branches: blender2.8
https://developer.blender.org/rBf2ea8e217b0b5bc02d1fbe92c33ee7fe05ae2c46

Object Mode : Add Outline FXAA

Adds a FXAA for smoothing out the extracted outlines.
The Post Process Anti Aliasing is only done on the Alpha channel of the outlines.
Because of that we need to add bleed the outline color out of the silouhette so the AA'd alpha can blend the right color and not pick black when the alpha is smoothed out of the silhouette.

Also because of the AA needs to have clear contrast to work with, I decided to ditch the "bluring" or the occluded outlines.

The FXAA adds an overhead of 0.17ms but we gain back 0.22ms * 4 = 0.88ms by removing the blur.

The FXAA Implementation is from Corey Richardson (cmr) (D2717). I had to modify it a bit to only filter the alpha channel.

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/modes/object_mode.c
A	source/blender/draw/modes/shaders/common_fxaa_lib.glsl
M	source/blender/draw/modes/shaders/object_outline_expand_frag.glsl
M	source/blender/draw/modes/shaders/object_outline_resolve_frag.glsl

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index 8d05e61aaa6..a41ed73b143 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -169,6 +169,7 @@ data_to_c_simple(engines/eevee/shaders/ssr_lib.glsl SRC)
 data_to_c_simple(engines/eevee/shaders/volumetric_frag.glsl SRC)
 
 data_to_c_simple(modes/shaders/common_globals_lib.glsl SRC)
+data_to_c_simple(modes/shaders/common_fxaa_lib.glsl SRC)
 data_to_c_simple(modes/shaders/edit_mesh_overlay_frag.glsl SRC)
 data_to_c_simple(modes/shaders/edit_mesh_overlay_vert.glsl SRC)
 data_to_c_simple(modes/shaders/edit_mesh_overlay_geom_tri.glsl SRC)
diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c
index 22571808cbc..7fe7e5f0042 100644
--- a/source/blender/draw/modes/object_mode.c
+++ b/source/blender/draw/modes/object_mode.c
@@ -76,6 +76,8 @@ extern char datatoc_object_particle_prim_frag_glsl[];
 extern char datatoc_object_particle_dot_vert_glsl[];
 extern char datatoc_object_particle_dot_frag_glsl[];
 extern char datatoc_common_globals_lib_glsl[];
+extern char datatoc_common_fxaa_lib_glsl[];
+extern char datatoc_gpu_shader_fullscreen_vert_glsl[];
 
 /* *********** LISTS *********** */
 typedef struct OBJECT_PassList {
@@ -84,11 +86,7 @@ typedef struct OBJECT_PassList {
 	struct DRWPass *outlines;
 	struct DRWPass *outlines_search;
 	struct DRWPass *outlines_expand;
-	struct DRWPass *outlines_fade1;
-	struct DRWPass *outlines_fade2;
-	struct DRWPass *outlines_fade3;
-	struct DRWPass *outlines_fade4;
-	struct DRWPass *outlines_fade5;
+	struct DRWPass *outlines_bleed;
 	struct DRWPass *outlines_resolve;
 	struct DRWPass *grid;
 	struct DRWPass *bone_solid;
@@ -205,6 +203,7 @@ typedef struct OBJECT_PrivateData{
 static struct {
 	/* fullscreen shaders */
 	GPUShader *outline_resolve_sh;
+	GPUShader *outline_resolve_aa_sh;
 	GPUShader *outline_detect_sh;
 	GPUShader *outline_fade_sh;
 
@@ -226,6 +225,7 @@ static struct {
 	int zneg_flag;
 	float zplane_normal[3];
 	float zplane_axes[3];
+	float inv_viewport_size[2];
 	bool draw_grid;
 	/* Temp buffer textures */
 	struct GPUTexture *outlines_depth_tx;
@@ -276,6 +276,15 @@ static void OBJECT_engine_init(void *vedata)
 		e_data.outline_resolve_sh = DRW_shader_create_fullscreen(datatoc_object_outline_resolve_frag_glsl, NULL);
 	}
 
+	if (!e_data.outline_resolve_aa_sh) {
+		e_data.outline_resolve_aa_sh = DRW_shader_create_with_lib(
+		            datatoc_gpu_shader_fullscreen_vert_glsl, NULL,
+		            datatoc_object_outline_resolve_frag_glsl,
+		            datatoc_common_fxaa_lib_glsl,
+		            "#define FXAA_ALPHA\n"
+		            "#define USE_FXAA\n");
+	}
+
 	if (!e_data.outline_detect_sh) {
 		e_data.outline_detect_sh = DRW_shader_create_fullscreen(datatoc_object_outline_detect_frag_glsl, NULL);
 	}
@@ -499,11 +508,15 @@ static void OBJECT_engine_init(void *vedata)
 		e_data.grid_settings[3] = v3d->gridsubdiv; /* gridSubdiv */
 		e_data.grid_settings[4] = (v3d->gridsubdiv > 1) ? 1.0f / logf(v3d->gridsubdiv) : 0.0f; /* 1/log(gridSubdiv) */
 	}
+
+	copy_v2_v2(e_data.inv_viewport_size, DRW_viewport_size_get());
+	invert_v2(e_data.inv_viewport_size);
 }
 
 static void OBJECT_engine_free(void)
 {
 	DRW_SHADER_FREE_SAFE(e_data.outline_resolve_sh);
+	DRW_SHADER_FREE_SAFE(e_data.outline_resolve_aa_sh);
 	DRW_SHADER_FREE_SAFE(e_data.outline_detect_sh);
 	DRW_SHADER_FREE_SAFE(e_data.outline_fade_sh);
 	DRW_SHADER_FREE_SAFE(e_data.object_empty_image_sh);
@@ -717,12 +730,6 @@ static void OBJECT_cache_init(void *vedata)
 		DRWState state = DRW_STATE_WRITE_COLOR;
 		struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
 		static float alphaOcclu = 0.35f;
-		static float one = 1.0f;
-		static float alpha1 = 5.0f / 6.0f;
-		static float alpha2 = 4.0f / 5.0f;
-		static float alpha3 = 3.0f / 4.0f;
-		static float alpha4 = 2.0f / 3.0f;
-		static float alpha5 = 1.0f / 2.0f;
 		static bool bTrue = true;
 		static bool bFalse = false;
 
@@ -739,53 +746,13 @@ static void OBJECT_cache_init(void *vedata)
 
 		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_expand);
 		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_blur_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &one, 1);
 		DRW_shgroup_uniform_bool(grp, "doExpand", &bTrue, 1);
 		DRW_shgroup_call_add(grp, quad, NULL);
 
-		psl->outlines_fade1 = DRW_pass_create("Outlines Fade 1 Pass", state);
-
-		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_fade1);
-		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &alpha1, 1);
-		DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
-		DRW_shgroup_call_add(grp, quad, NULL);
-
-		psl->outlines_fade2 = DRW_pass_create("Outlines Fade 2 Pass", state);
-
-		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_fade2);
-		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_blur_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &alpha2, 1);
-		DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
-		DRW_shgroup_call_add(grp, quad, NULL);
-
-		psl->outlines_fade3 = DRW_pass_create("Outlines Fade 3 Pass", state);
-
-		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_fade3);
-		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &alpha3, 1);
-		DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
-		DRW_shgroup_call_add(grp, quad, NULL);
-
-		psl->outlines_fade4 = DRW_pass_create("Outlines Fade 4 Pass", state);
-
-		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_fade4);
-		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_blur_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &alpha4, 1);
-		DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
-		DRW_shgroup_call_add(grp, quad, NULL);
-
-		psl->outlines_fade5 = DRW_pass_create("Outlines Fade 5 Pass", state);
+		psl->outlines_bleed = DRW_pass_create("Outlines Bleed Pass", state);
 
-		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_fade5);
+		grp = DRW_shgroup_create(e_data.outline_fade_sh, psl->outlines_bleed);
 		DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx);
-		DRW_shgroup_uniform_buffer(grp, "outlineDepth", &e_data.outlines_depth_tx);
-		DRW_shgroup_uniform_float(grp, "alpha", &alpha5, 1);
 		DRW_shgroup_uniform_bool(grp, "doExpand", &bFalse, 1);
 		DRW_shgroup_call_add(grp, quad, NULL);
 	}
@@ -796,8 +763,9 @@ static void OBJECT_cache_init(void *vedata)
 
 		struct Gwn_Batch *quad = DRW_cache_fullscreen_quad_get();
 
-		DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_resolve_sh, psl->outlines_resolve);
+		DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_resolve_aa_sh, psl->outlines_resolve);
 		DRW_shgroup_uniform_buffer(grp, "outlineBluredColor", &e_data.outlines_blur_tx);
+		DRW_shgroup_uniform_vec2(grp, "rcpDimensions", e_data.inv_viewport_size, 1);
 		DRW_shgroup_call_add(grp, quad, NULL);
 	}
 
@@ -1841,24 +1809,13 @@ static void OBJECT_draw_scene(void *vedata)
 		DRW_framebuffer_bind(fbl->blur);
 		DRW_draw_pass(psl->outlines_search);
 
-		/* Expand and fade gradually */
+		/* Expand outline to form a 3px wide line */
 		DRW_framebuffer_bind(fbl->outlines);
 		DRW_draw_pass(psl->outlines_expand);
 
+		/* Bleed color so the AA can do it's stuff */
 		DRW_framebuffer_bind(fbl->blur);
-		DRW_draw_pass(psl->outlines_fade1);
-
-		DRW_framebuffer_bind(fbl->outlines);
-		DRW_draw_pass(psl->outlines_fade2);
-
-		DRW_framebuffer_bind(fbl->blur);
-		DRW_draw_pass(psl->outlines_fade3);
-
-		DRW_framebuffer_bind(fbl->outlines);
-		DRW_draw_pass(psl->outlines_fade4);
-
-		DRW_framebuffer_bind(fbl->blur);
-		DRW_draw_pass(psl->outlines_fade5);
+		DRW_draw_pass(psl->outlines_bleed);
 
 		/* detach temp textures */
 		DRW_framebuffer_texture_detach(e_data.outlines_color_tx);
diff --git a/source/blender/draw/modes/shaders/common_fxaa_lib.glsl b/source/blender/draw/modes/shaders/common_fxaa_lib.glsl
new file mode 100644
index 00000000000..8158437b943
--- /dev/null
+++ b/source/blender/draw/modes/shaders/common_fxaa_lib.glsl
@@ -0,0 +1,678 @@
+//----------------------------------------------------------------------------------
+// File:        es3-kepler\FXAA/FXAA3_11.h
+// SDK Version: v3.00
+// Email:       gameworks at nvidia.com
+// Site:        http://developer.nvidia.com/
+//
+// Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+//  * Redistributions of source code must retain the above copyright
+//    notice, this list of conditions and the following disclaimer.
+//  * Redistributions in binary form must reproduce the above copyright
+//    notice, this list of conditions and the following disclaimer in the
+//    documentation and/or other materials provided with the distribution.
+//  * Neither the name of NVIDIA CORPORATION nor the names of its
+//    contributors may be used to endorse or promote products derived
+//    from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+/

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list