Differential D4455 Diff 14273 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
| /** | /** | ||||
| * 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 mat4 ProjectionMatrix; | uniform mat4 ProjectionMatrix; | ||||
| uniform vec2 invertedViewportSize; | uniform vec2 invertedViewportSize; | ||||
| uniform vec2 nearFar; | uniform vec2 nearFar; | ||||
| uniform vec3 dofParams; | uniform vec3 dofParams; | ||||
| uniform float noiseOffset; | uniform float noiseOffset; | ||||
| uniform sampler2D inputCocTex; | uniform sampler2D inputCocTex; | ||||
| uniform sampler2D maxCocTilesTex; | uniform sampler2D maxCocTilesTex; | ||||
| Show All 25 Lines | |||||
| const float MAX_COC_SIZE = 100.0; | const float MAX_COC_SIZE = 100.0; | ||||
| vec2 encode_coc(float near, float far) { return vec2(near, far) / MAX_COC_SIZE; } | vec2 encode_coc(float near, float far) { return vec2(near, far) / MAX_COC_SIZE; } | ||||
| float decode_coc(vec2 cocs) { return max(cocs.x, cocs.y) * MAX_COC_SIZE; } | float decode_coc(vec2 cocs) { return max(cocs.x, cocs.y) * MAX_COC_SIZE; } | ||||
| float decode_signed_coc(vec2 cocs) { return ((cocs.x > cocs.y) ? cocs.x : -cocs.y) * MAX_COC_SIZE; } | float decode_signed_coc(vec2 cocs) { return ((cocs.x > cocs.y) ? cocs.x : -cocs.y) * MAX_COC_SIZE; } | ||||
| /** | /** | ||||
| * ----------------- 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 = 0) out vec4 halfResColor; | ||||
| layout(location = 1) out vec2 normalizedCoc; | 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); | ||||
| Show All 30 Lines | void main() | ||||
| normalizedCoc = encode_coc(coc_near, coc_far); | normalizedCoc = encode_coc(coc_near, coc_far); | ||||
| } | } | ||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 0.5 ------------------ | * ----------------- STEP 0.5 ------------------ | ||||
| * Custom Coc aware downsampling. Quater res pass. | * Custom Coc aware downsampling. Quater res pass. | ||||
| **/ | */ | ||||
| #ifdef DOWNSAMPLE | #ifdef DOWNSAMPLE | ||||
| layout(location = 0) out vec4 outColor; | layout(location = 0) out vec4 outColor; | ||||
| layout(location = 1) out vec2 outCocs; | layout(location = 1) out vec2 outCocs; | ||||
| 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); | ||||
| Show All 27 Lines | void main() | ||||
| outCocs = encode_coc(coc_near, coc_far); | outCocs = encode_coc(coc_near, coc_far); | ||||
| } | } | ||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 1 ------------------ | * ----------------- STEP 1 ------------------ | ||||
| * Flatten COC buffer using max filter. | * Flatten COC buffer using max filter. | ||||
| **/ | */ | ||||
| #if defined(FLATTEN_VERTICAL) || defined(FLATTEN_HORIZONTAL) | #if defined(FLATTEN_VERTICAL) || defined(FLATTEN_HORIZONTAL) | ||||
| layout(location = 0) out vec2 flattenedCoc; | layout(location = 0) out vec2 flattenedCoc; | ||||
| void main() | void main() | ||||
| { | { | ||||
| #ifdef FLATTEN_HORIZONTAL | #ifdef FLATTEN_HORIZONTAL | ||||
| ivec2 texel = ivec2(gl_FragCoord.xy) * ivec2(8, 1); | ivec2 texel = ivec2(gl_FragCoord.xy) * ivec2(8, 1); | ||||
| Show All 18 Lines | |||||
| #endif | #endif | ||||
| flattenedCoc = max(max(max(cocs1, cocs2), max(cocs3, cocs4)), max(max(cocs5, cocs6), max(cocs7, cocs8))); | flattenedCoc = max(max(max(cocs1, cocs2), max(cocs3, cocs4)), max(max(cocs5, cocs6), max(cocs7, cocs8))); | ||||
| } | } | ||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 1.ax------------------ | * ----------------- STEP 1.ax------------------ | ||||
| * Dilate COC buffer using min filter. | * Dilate COC buffer using min filter. | ||||
| **/ | */ | ||||
| #if defined(DILATE_VERTICAL) || defined(DILATE_HORIZONTAL) | #if defined(DILATE_VERTICAL) || defined(DILATE_HORIZONTAL) | ||||
| layout(location = 0) out vec2 dilatedCoc; | layout(location = 0) out vec2 dilatedCoc; | ||||
| void main() | void main() | ||||
| { | { | ||||
| vec2 texel_size = 1.0 / vec2(textureSize(inputCocTex, 0)); | vec2 texel_size = 1.0 / vec2(textureSize(inputCocTex, 0)); | ||||
| vec2 uv = gl_FragCoord.xy * texel_size; | vec2 uv = gl_FragCoord.xy * texel_size; | ||||
| Show All 18 Lines | #endif | ||||
| dilatedCoc = max(max(max(cocs1, cocs2), max(cocs3, cocs4)), max(max(cocs5, cocs6), cocs7)); | dilatedCoc = max(max(max(cocs1, cocs2), max(cocs3, cocs4)), max(max(cocs5, cocs6), cocs7)); | ||||
| } | } | ||||
| #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; | layout(location = 0) out vec4 blurColor; | ||||
| #define NUM_SAMPLES 49 | #define NUM_SAMPLES 49 | ||||
| layout(std140) uniform dofSamplesBlock { | layout(std140) uniform dofSamplesBlock { | ||||
| vec4 samples[NUM_SAMPLES]; | vec4 samples[NUM_SAMPLES]; | ||||
| }; | }; | ||||
| ▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | |||||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||||
| * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||||
| * 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; | 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 | ||||
| * since this filter is not weighted by CoC | * since this filter is not weighted by CoC | ||||
| * and can bleed a bit. */ | * and can bleed a bit. */ | ||||
| float rad = clamp(coc - 9.0, 0.0, 1.0); | float rad = clamp(coc - 9.0, 0.0, 1.0); | ||||
| #define vec vec4 | #define vec vec4 | ||||
| #define toVec(x) x.rgba | #define toVec(x) x.rgba | ||||
| Show All 29 Lines | #define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges | ||||
| mnmx3(v[3], v[4], v[8]); | mnmx3(v[3], v[4], v[8]); | ||||
| toVec(finalColor) = v[4]; | toVec(finalColor) = v[4]; | ||||
| } | } | ||||
| #endif | #endif | ||||
| /** | /** | ||||
| * ----------------- STEP 4 ------------------ | * ----------------- STEP 4 ------------------ | ||||
| **/ | */ | ||||
| #ifdef RESOLVE | #ifdef RESOLVE | ||||
| out vec4 finalColor; | out vec4 finalColor; | ||||
| 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); | ||||
| finalColor = texture(halfResColorTex, uv); | finalColor = texture(halfResColorTex, uv); | ||||
| finalColor.a = smoothstep(1.0, 3.0, abs(coc)); | finalColor.a = smoothstep(1.0, 3.0, abs(coc)); | ||||
| } | } | ||||
| #endif | #endif | ||||
| No newline at end of file | |||||