fs_bokeh_linear_depth.sc 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. $input v_texcoord0
  2. /*
  3. * Copyright 2021 elven cache. All rights reserved.
  4. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  5. */
  6. #include "../common/common.sh"
  7. #include "parameters.sh"
  8. SAMPLER2D(s_depth, 0);
  9. // from assao sample, cs_assao_prepare_depths.sc
  10. float ScreenSpaceToViewSpaceDepth( float screenDepth )
  11. {
  12. float depthLinearizeMul = u_depthUnpackConsts.x;
  13. float depthLinearizeAdd = u_depthUnpackConsts.y;
  14. // Optimised version of "-cameraClipNear / (cameraClipFar - projDepth * (cameraClipFar - cameraClipNear)) * cameraClipFar"
  15. // Set your depthLinearizeMul and depthLinearizeAdd to:
  16. // depthLinearizeMul = ( cameraClipFar * cameraClipNear) / ( cameraClipFar - cameraClipNear );
  17. // depthLinearizeAdd = cameraClipFar / ( cameraClipFar - cameraClipNear );
  18. return depthLinearizeMul / ( depthLinearizeAdd - screenDepth );
  19. }
  20. void main()
  21. {
  22. vec2 texCoord = v_texcoord0;
  23. float depth = texture2D(s_depth, texCoord).x;
  24. float linearDepth = ScreenSpaceToViewSpaceDepth(depth);
  25. gl_FragColor = vec4_splat(linearDepth);
  26. }