From 2ba11d72a2cfcdb7639de1e8a20a6ec587d38a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Wed, 9 Aug 2017 23:51:00 +0200 Subject: Object Mode Engine: Optimize outline passes. Group texture fetches to hide latency. 3.2ms -> 2.2ms (constant time improvement, not depending on scene complexity) Could optimize further with textureGather (require OpenGL 4.0). --- source/blender/draw/modes/object_mode.c | 4 +- .../modes/shaders/object_outline_detect_frag.glsl | 42 +++++++++++++++------ .../modes/shaders/object_outline_expand_frag.glsl | 43 +++++++++------------- 3 files changed, 50 insertions(+), 39 deletions(-) (limited to 'source/blender/draw') diff --git a/source/blender/draw/modes/object_mode.c b/source/blender/draw/modes/object_mode.c index 8176fbcda5c..1e70e942d07 100644 --- a/source/blender/draw/modes/object_mode.c +++ b/source/blender/draw/modes/object_mode.c @@ -696,7 +696,7 @@ static void OBJECT_cache_init(void *vedata) { DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_WIRE; - psl->outlines = DRW_pass_create("Outlines Pass", state); + psl->outlines = DRW_pass_create("Outlines Depth Pass", state); GPUShader *sh = GPU_shader_get_builtin_shader(GPU_SHADER_3D_UNIFORM_COLOR); @@ -725,7 +725,7 @@ static void OBJECT_cache_init(void *vedata) static bool bTrue = true; static bool bFalse = false; - psl->outlines_search = DRW_pass_create("Outlines Expand Pass", state); + psl->outlines_search = DRW_pass_create("Outlines Detect Pass", state); DRWShadingGroup *grp = DRW_shgroup_create(e_data.outline_detect_sh, psl->outlines_search); DRW_shgroup_uniform_buffer(grp, "outlineColor", &e_data.outlines_color_tx); diff --git a/source/blender/draw/modes/shaders/object_outline_detect_frag.glsl b/source/blender/draw/modes/shaders/object_outline_detect_frag.glsl index 5565a0f1e09..dc0ea938436 100644 --- a/source/blender/draw/modes/shaders/object_outline_detect_frag.glsl +++ b/source/blender/draw/modes/shaders/object_outline_detect_frag.glsl @@ -32,30 +32,48 @@ void search_outline(ivec2 uv, vec4 ref_col, inout bool ref_occlu, inout bool out void main() { ivec2 uv = ivec2(gl_FragCoord.xy); + + vec4 color[4]; + /* Idea : Use a 16bit ID to identify the color + * and store the colors in a UBO. And fetch all ids + * for discontinuity check with one textureGather \o/ */ vec4 ref_col = texelFetch(outlineColor, uv, 0).rgba; + color[0] = texelFetchOffset(outlineColor, uv, 0, ivec2( 1, 0)).rgba; + color[1] = texelFetchOffset(outlineColor, uv, 0, ivec2( 0, 1)).rgba; + color[2] = texelFetchOffset(outlineColor, uv, 0, ivec2(-1, 0)).rgba; + color[3] = texelFetchOffset(outlineColor, uv, 0, ivec2( 0, -1)).rgba; + /* TODO GATHER */ + vec4 depths; float depth = texelFetch(outlineDepth, uv, 0).r; - /* Modulate color if occluded */ + depths.x = texelFetchOffset(outlineDepth, uv, 0, ivec2( 1, 0)).r; + depths.y = texelFetchOffset(outlineDepth, uv, 0, ivec2( 0, 1)).r; + depths.z = texelFetchOffset(outlineDepth, uv, 0, ivec2(-1, 0)).r; + depths.w = texelFetchOffset(outlineDepth, uv, 0, ivec2( 0, -1)).r; + + vec4 scene_depths; float scene_depth = texelFetch(sceneDepth, uv, 0).r; + scene_depths.x = texelFetchOffset(sceneDepth, uv, 0, ivec2( 1, 0)).r; + scene_depths.y = texelFetchOffset(sceneDepth, uv, 0, ivec2( 0, 1)).r; + scene_depths.z = texelFetchOffset(sceneDepth, uv, 0, ivec2(-1, 0)).r; + scene_depths.w = texelFetchOffset(sceneDepth, uv, 0, ivec2( 0, -1)).r; bool ref_occlu = (depth > scene_depth); - bool outline = false; +#if 1 + bvec4 occlu = (!ref_occlu) ? notEqual(greaterThan(depths, scene_depths), bvec4(ref_occlu)) : bvec4(false); + outline = (!outline) ? (color[0] != ref_col) || occlu.x : true; + outline = (!outline) ? (color[1] != ref_col) || occlu.y : true; + outline = (!outline) ? (color[2] != ref_col) || occlu.z : true; + outline = (!outline) ? (color[3] != ref_col) || occlu.w : true; +#else search_outline(uv + ivec2( 1, 0), ref_col, ref_occlu, outline); search_outline(uv + ivec2( 0, 1), ref_col, ref_occlu, outline); search_outline(uv + ivec2(-1, 0), ref_col, ref_occlu, outline); search_outline(uv + ivec2( 0, -1), ref_col, ref_occlu, outline); +#endif FragColor = ref_col; - - /* We Hit something ! */ - if (outline) { - if (ref_occlu) { - FragColor.a *= alphaOcclu; - } - } - else { - FragColor.a = 0.0; - } + FragColor.a *= (outline) ? (ref_occlu) ? alphaOcclu : 1.0 : 0.0; } diff --git a/source/blender/draw/modes/shaders/object_outline_expand_frag.glsl b/source/blender/draw/modes/shaders/object_outline_expand_frag.glsl index e0568d1157a..c753e3ab39c 100644 --- a/source/blender/draw/modes/shaders/object_outline_expand_frag.glsl +++ b/source/blender/draw/modes/shaders/object_outline_expand_frag.glsl @@ -9,37 +9,30 @@ uniform sampler2D outlineDepth; uniform float alpha; uniform bool doExpand; -void search_outline(ivec2 uv, inout bool found_edge) -{ - if (!found_edge) { - vec4 color = texelFetch(outlineColor, uv, 0).rgba; - if (color.a != 0.0) { - if (doExpand || color.a != 1.0) { - FragColor = color; - found_edge = true; - } - } - } -} - void main() { ivec2 uv = ivec2(gl_FragCoord.xy); FragColor = texelFetch(outlineColor, uv, 0).rgba; float depth = texelFetch(outlineDepth, uv, 0).r; - if (FragColor.a != 0.0 || (depth == 1.0 && !doExpand)) - return; + vec4 color[4]; + color[0] = texelFetchOffset(outlineColor, uv, 0, ivec2( 1, 0)).rgba; + color[1] = texelFetchOffset(outlineColor, uv, 0, ivec2( 0, 1)).rgba; + color[2] = texelFetchOffset(outlineColor, uv, 0, ivec2(-1, 0)).rgba; + color[3] = texelFetchOffset(outlineColor, uv, 0, ivec2( 0, -1)).rgba; + + vec4 values = vec4(color[0].a, color[1].a, color[2].a, color[3].a); + + bool is_blank_pixel = !(FragColor.a != 0.0 || (depth == 1.0 && !doExpand)); + vec4 tests = vec4(is_blank_pixel); + tests *= step(vec4(1e-6), values); /* (color.a != 0.0) */ + tests *= (doExpand) ? vec4(1.0) : step(values, vec4(0.999)); /* (doExpand || color.a != 1.0) */ + bvec4 btests = equal(tests, vec4(1.0)); - bool found_edge = false; - search_outline(uv + ivec2( 1, 0), found_edge); - search_outline(uv + ivec2( 0, 1), found_edge); - search_outline(uv + ivec2(-1, 0), found_edge); - search_outline(uv + ivec2( 0, -1), found_edge); + FragColor = (btests.x) ? color[0] : FragColor; + FragColor = (btests.y) ? color[1] : FragColor; + FragColor = (btests.z) ? color[2] : FragColor; + FragColor = (btests.w) ? color[3] : FragColor; - /* We Hit something ! */ - if (found_edge) { - /* only change alpha */ - FragColor.a *= alpha; - } + FragColor *= (is_blank_pixel) ? alpha : 1.0; } -- cgit v1.2.3