Differential D13910 Diff 47464 source/blender/draw/engines/workbench/shaders/workbench_effect_dof_frag.glsl
Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/workbench/shaders/workbench_effect_dof_frag.glsl
| #pragma BLENDER_REQUIRE(common_view_lib.glsl) | #pragma BLENDER_REQUIRE(common_view_lib.glsl) | ||||
| #pragma BLENDER_REQUIRE(common_math_lib.glsl) | #pragma BLENDER_REQUIRE(common_math_lib.glsl) | ||||
| /** | /** | ||||
| * Separable Hexagonal Bokeh Blur by Colin Barré-Brisebois | * Separable Hexagonal Bokeh Blur by Colin Barré-Brisebois | ||||
| * https://colinbarrebrisebois.com/2017/04/18/hexagonal-bokeh-blur-revisited-part-1-basic-3-pass-version/ | * https://colinbarrebrisebois.com/2017/04/18/hexagonal-bokeh-blur-revisited-part-1-basic-3-pass-version/ | ||||
| * Converted and adapted from HLSL to GLSL by Clément Foucault | * Converted and adapted from HLSL to GLSL by Clément Foucault | ||||
| */ | */ | ||||
| uniform vec2 invertedViewportSize; | |||||
| uniform vec2 nearFar; | |||||
| uniform vec3 dofParams; | |||||
| uniform float noiseOffset; | |||||
| uniform sampler2D inputCocTex; | |||||
| uniform sampler2D maxCocTilesTex; | |||||
| uniform sampler2D sceneColorTex; | |||||
| uniform sampler2D sceneDepthTex; | |||||
| uniform sampler2D backgroundTex; | |||||
| uniform sampler2D halfResColorTex; | |||||
| uniform sampler2D blurTex; | |||||
| uniform sampler2D noiseTex; | |||||
| #define dof_aperturesize dofParams.x | #define dof_aperturesize dofParams.x | ||||
| #define dof_distance dofParams.y | #define dof_distance dofParams.y | ||||
| #define dof_invsensorsize dofParams.z | #define dof_invsensorsize dofParams.z | ||||
| /* divide by sensor size to get the normalized size */ | /* divide by sensor size to get the normalized size */ | ||||
| #define calculate_coc(zdepth) \ | #define calculate_coc(zdepth) \ | ||||
| (dof_aperturesize * (dof_distance / zdepth - 1.0) * dof_invsensorsize) | (dof_aperturesize * (dof_distance / zdepth - 1.0) * dof_invsensorsize) | ||||
| Show All 17 Lines | |||||
| } | } | ||||
| /** | /** | ||||
| * ----------------- STEP 0 ------------------ | * ----------------- STEP 0 ------------------ | ||||
| * Custom Coc aware downsampling. Half res pass. | * Custom Coc aware downsampling. Half res pass. | ||||
| */ | */ | ||||
| #ifdef PREPARE | #ifdef PREPARE | ||||
| layout(location = 0) out vec4 halfResColor; | |||||
| layout(location = 1) out vec2 normalizedCoc; | |||||
| void main() | void main() | ||||
| { | { | ||||
| ivec4 texel = ivec4(gl_FragCoord.xyxy) * 2 + ivec4(0, 0, 1, 1); | ivec4 texel = ivec4(gl_FragCoord.xyxy) * 2 + ivec4(0, 0, 1, 1); | ||||
| vec4 color1 = texelFetch(sceneColorTex, texel.xy, 0); | vec4 color1 = texelFetch(sceneColorTex, texel.xy, 0); | ||||
| vec4 color2 = texelFetch(sceneColorTex, texel.zw, 0); | vec4 color2 = texelFetch(sceneColorTex, texel.zw, 0); | ||||
| vec4 color3 = texelFetch(sceneColorTex, texel.zy, 0); | vec4 color3 = texelFetch(sceneColorTex, texel.zy, 0); | ||||
| vec4 color4 = texelFetch(sceneColorTex, texel.xw, 0); | vec4 color4 = texelFetch(sceneColorTex, texel.xw, 0); | ||||
| Show All 27 Lines | |||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 0.5 ------------------ | * ----------------- STEP 0.5 ------------------ | ||||
| * Custom Coc aware downsampling. Quarter res pass. | * Custom Coc aware downsampling. Quarter res pass. | ||||
| */ | */ | ||||
| #ifdef DOWNSAMPLE | #ifdef DOWNSAMPLE | ||||
| layout(location = 0) out vec4 outColor; | |||||
| layout(location = 1) out vec2 outCocs; | |||||
| void main() | void main() | ||||
| { | { | ||||
| vec4 texel = vec4(gl_FragCoord.xyxy) * 2.0 + vec4(0.0, 0.0, 1.0, 1.0); | vec4 texel = vec4(gl_FragCoord.xyxy) * 2.0 + vec4(0.0, 0.0, 1.0, 1.0); | ||||
| texel = (texel - 0.5) / vec4(textureSize(sceneColorTex, 0).xyxy); | texel = (texel - 0.5) / vec4(textureSize(sceneColorTex, 0).xyxy); | ||||
| /* Using texelFetch can bypass the mip range setting on some platform. | /* Using texelFetch can bypass the mip range setting on some platform. | ||||
| * Using texture Lod fix this issue. Note that we need to disable filtering to get the right | * Using texture Lod fix this issue. Note that we need to disable filtering to get the right | ||||
| * texel values. */ | * texel values. */ | ||||
| ▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | |||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 2 ------------------ | * ----------------- STEP 2 ------------------ | ||||
| * Blur vertically and diagonally. | * Blur vertically and diagonally. | ||||
| * Outputs vertical blur and combined blur in MRT | * Outputs vertical blur and combined blur in MRT | ||||
| */ | */ | ||||
| #ifdef BLUR1 | #ifdef BLUR1 | ||||
| layout(location = 0) out vec4 blurColor; | |||||
| # define NUM_SAMPLES 49 | |||||
| layout(std140) uniform dofSamplesBlock | |||||
| { | |||||
| vec4 samples[NUM_SAMPLES]; | |||||
| }; | |||||
| vec2 get_random_vector(float offset) | vec2 get_random_vector(float offset) | ||||
| { | { | ||||
| /* Interlieved gradient noise by Jorge Jimenez | /* Interlieved gradient noise by Jorge Jimenez | ||||
| * http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare */ | * http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare */ | ||||
| float ign = fract(offset + | float ign = fract(offset + | ||||
| 52.9829189 * fract(0.06711056 * gl_FragCoord.x + 0.00583715 * gl_FragCoord.y)); | 52.9829189 * fract(0.06711056 * gl_FragCoord.x + 0.00583715 * gl_FragCoord.y)); | ||||
| float bn = texelFetch(noiseTex, ivec2(gl_FragCoord.xy) % 64, 0).a; | float bn = texelFetch(noiseTex, ivec2(gl_FragCoord.xy) % 64, 0).a; | ||||
| ▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||
| */ | */ | ||||
| #ifdef BLUR2 | #ifdef BLUR2 | ||||
| out vec4 finalColor; | |||||
| void main() | void main() | ||||
| { | { | ||||
| /* Half Res pass */ | /* Half Res pass */ | ||||
| vec2 pixel_size = 1.0 / vec2(textureSize(blurTex, 0).xy); | vec2 pixel_size = 1.0 / vec2(textureSize(blurTex, 0).xy); | ||||
| vec2 uv = gl_FragCoord.xy * pixel_size.xy; | vec2 uv = gl_FragCoord.xy * pixel_size.xy; | ||||
| float coc = decode_coc(texture(inputCocTex, uv).rg); | float coc = decode_coc(texture(inputCocTex, uv).rg); | ||||
| /* Only use this filter if coc is > 9.0 | /* Only use this filter if coc is > 9.0 | ||||
| ▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | |||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 4 ------------------ | * ----------------- STEP 4 ------------------ | ||||
| */ | */ | ||||
| #ifdef RESOLVE | #ifdef RESOLVE | ||||
| layout(location = 0, index = 0) out vec4 finalColorAdd; | |||||
| layout(location = 0, index = 1) out vec4 finalColorMul; | |||||
| void main() | void main() | ||||
| { | { | ||||
| /* Fullscreen pass */ | /* Fullscreen pass */ | ||||
| vec2 pixel_size = 0.5 / vec2(textureSize(halfResColorTex, 0).xy); | vec2 pixel_size = 0.5 / vec2(textureSize(halfResColorTex, 0).xy); | ||||
| vec2 uv = gl_FragCoord.xy * pixel_size; | vec2 uv = gl_FragCoord.xy * pixel_size; | ||||
| /* TODO: MAKE SURE TO ALIGN SAMPLE POSITION TO AVOID OFFSET IN THE BOKEH. */ | /* TODO: MAKE SURE TO ALIGN SAMPLE POSITION TO AVOID OFFSET IN THE BOKEH. */ | ||||
| float depth = texelFetch(sceneDepthTex, ivec2(gl_FragCoord.xy), 0).r; | float depth = texelFetch(sceneDepthTex, ivec2(gl_FragCoord.xy), 0).r; | ||||
| float zdepth = linear_depth(depth); | float zdepth = linear_depth(depth); | ||||
| float coc = calculate_coc(zdepth); | float coc = calculate_coc(zdepth); | ||||
| float blend = smoothstep(1.0, 3.0, abs(coc)); | float blend = smoothstep(1.0, 3.0, abs(coc)); | ||||
| finalColorAdd = texture(halfResColorTex, uv) * blend; | finalColorAdd = texture(halfResColorTex, uv) * blend; | ||||
| finalColorMul = vec4(1.0 - blend); | finalColorMul = vec4(1.0 - blend); | ||||
| } | } | ||||
| #endif | #endif | ||||