Workbench: Depth Of Field: Fix undefined behavior with using texelFetch

On MacOS + Intel Iris Graphics 6100 (may affect other config too), the
texelFetch operation bypass the base mip setting of the texture object.

Using textureLod with lod = 0.0 ensure the lowest (after clamping) mip will
always be selected.

Also disable the texture filtering for this sampler to avoid unecessary
fetches.

This should fix T78653 Blender 2.83 broken Depth of Field in Viewport
This commit is contained in:
Clément Foucault 2020-09-14 23:03:07 +02:00
parent f7829787da
commit 7710de26d0
Notes: blender-bot 2023-02-14 06:17:14 +01:00
Referenced by commit 26a827f254, Workbench: Depth Of Field: Fix regression in look and avoid implicit cast
Referenced by issue #78653, Blender 2.83 broken Depth of Field in Viewport (Mac OSX)
2 changed files with 14 additions and 10 deletions

View File

@ -108,18 +108,22 @@ layout(location = 1) out vec2 outCocs;
void main()
{
ivec4 texel = ivec4(gl_FragCoord.xyxy) * 2 + ivec4(0, 0, 1, 1);
vec4 texel = vec4(gl_FragCoord.xyxy) * 2 + vec4(0, 0, 1, 1);
texel = (texel + 0.5) / vec4(textureSize(sceneColorTex, 0).xyxy);
vec4 color1 = texelFetch(sceneColorTex, texel.xy, 0);
vec4 color2 = texelFetch(sceneColorTex, texel.zw, 0);
vec4 color3 = texelFetch(sceneColorTex, texel.zy, 0);
vec4 color4 = texelFetch(sceneColorTex, texel.xw, 0);
/* 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
* texel values. */
vec4 color1 = textureLod(sceneColorTex, texel.xy, 0.0);
vec4 color2 = textureLod(sceneColorTex, texel.zw, 0.0);
vec4 color3 = textureLod(sceneColorTex, texel.zy, 0.0);
vec4 color4 = textureLod(sceneColorTex, texel.xw, 0.0);
vec4 depths;
vec2 cocs1 = texelFetch(inputCocTex, texel.xy, 0).rg;
vec2 cocs2 = texelFetch(inputCocTex, texel.zw, 0).rg;
vec2 cocs3 = texelFetch(inputCocTex, texel.zy, 0).rg;
vec2 cocs4 = texelFetch(inputCocTex, texel.xw, 0).rg;
vec2 cocs1 = textureLod(inputCocTex, texel.xy, 0.0).rg;
vec2 cocs2 = textureLod(inputCocTex, texel.zw, 0.0).rg;
vec2 cocs3 = textureLod(inputCocTex, texel.zy, 0.0).rg;
vec2 cocs4 = textureLod(inputCocTex, texel.xw, 0.0).rg;
vec4 cocs_near = vec4(cocs1.r, cocs2.r, cocs3.r, cocs4.r) * MAX_COC_SIZE;
vec4 cocs_far = vec4(cocs1.g, cocs2.g, cocs3.g, cocs4.g) * MAX_COC_SIZE;

View File

@ -286,7 +286,7 @@ void workbench_dof_cache_init(WORKBENCH_Data *vedata)
psl->dof_down2_ps = DRW_pass_create("DoF DownSample", DRW_STATE_WRITE_COLOR);
DRWShadingGroup *grp = DRW_shgroup_create(downsample_sh, psl->dof_down2_ps);
DRW_shgroup_uniform_texture(grp, "sceneColorTex", txl->dof_source_tx);
DRW_shgroup_uniform_texture_ex(grp, "sceneColorTex", txl->dof_source_tx, GPU_SAMPLER_DEFAULT);
DRW_shgroup_uniform_texture(grp, "inputCocTex", txl->coc_halfres_tx);
DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
}