fxaa.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { QuadSchema, QuadValues } from '../../mol-gl/compute/util';
  7. import { ComputeRenderable, createComputeRenderable } from '../../mol-gl/renderable';
  8. import { TextureSpec, UniformSpec, DefineSpec, Values } from '../../mol-gl/renderable/schema';
  9. import { ShaderCode } from '../../mol-gl/shader-code';
  10. import { WebGLContext } from '../../mol-gl/webgl/context';
  11. import { createComputeRenderItem } from '../../mol-gl/webgl/render-item';
  12. import { Texture } from '../../mol-gl/webgl/texture';
  13. import { Vec2 } from '../../mol-math/linear-algebra';
  14. import { ValueCell } from '../../mol-util';
  15. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  16. import { quad_vert } from '../../mol-gl/shader/quad.vert';
  17. import { fxaa_frag } from '../../mol-gl/shader/fxaa.frag';
  18. import { Viewport } from '../camera/util';
  19. import { RenderTarget } from '../../mol-gl/webgl/render-target';
  20. import { isTimingMode } from '../../mol-util/debug';
  21. export const FxaaParams = {
  22. edgeThresholdMin: PD.Numeric(0.0312, { min: 0.0312, max: 0.0833, step: 0.0001 }, { description: 'Trims the algorithm from processing darks.' }),
  23. edgeThresholdMax: PD.Numeric(0.063, { min: 0.063, max: 0.333, step: 0.001 }, { description: 'The minimum amount of local contrast required to apply algorithm.' }),
  24. iterations: PD.Numeric(12, { min: 0, max: 16, step: 1 }, { description: 'Number of edge exploration steps.' }),
  25. subpixelQuality: PD.Numeric(0.30, { min: 0.00, max: 1.00, step: 0.01 }, { description: 'Choose the amount of sub-pixel aliasing removal.' }),
  26. };
  27. export type FxaaProps = PD.Values<typeof FxaaParams>
  28. export class FxaaPass {
  29. private readonly renderable: FxaaRenderable;
  30. constructor(private webgl: WebGLContext, input: Texture) {
  31. this.renderable = getFxaaRenderable(webgl, input);
  32. }
  33. private updateState(viewport: Viewport) {
  34. const { gl, state } = this.webgl;
  35. state.enable(gl.SCISSOR_TEST);
  36. state.disable(gl.BLEND);
  37. state.disable(gl.DEPTH_TEST);
  38. state.depthMask(false);
  39. const { x, y, width, height } = viewport;
  40. state.viewport(x, y, width, height);
  41. state.scissor(x, y, width, height);
  42. state.clearColor(0, 0, 0, 1);
  43. gl.clear(gl.COLOR_BUFFER_BIT);
  44. }
  45. setSize(width: number, height: number) {
  46. ValueCell.update(this.renderable.values.uTexSizeInv, Vec2.set(this.renderable.values.uTexSizeInv.ref.value, 1 / width, 1 / height));
  47. }
  48. update(input: Texture, props: FxaaProps) {
  49. const { values } = this.renderable;
  50. const { edgeThresholdMin, edgeThresholdMax, iterations, subpixelQuality } = props;
  51. let needsUpdate = false;
  52. if (values.tColor.ref.value !== input) {
  53. ValueCell.update(this.renderable.values.tColor, input);
  54. needsUpdate = true;
  55. }
  56. if (values.dEdgeThresholdMin.ref.value !== edgeThresholdMin) needsUpdate = true;
  57. ValueCell.updateIfChanged(values.dEdgeThresholdMin, edgeThresholdMin);
  58. if (values.dEdgeThresholdMax.ref.value !== edgeThresholdMax) needsUpdate = true;
  59. ValueCell.updateIfChanged(values.dEdgeThresholdMax, edgeThresholdMax);
  60. if (values.dIterations.ref.value !== iterations) needsUpdate = true;
  61. ValueCell.updateIfChanged(values.dIterations, iterations);
  62. if (values.dSubpixelQuality.ref.value !== subpixelQuality) needsUpdate = true;
  63. ValueCell.updateIfChanged(values.dSubpixelQuality, subpixelQuality);
  64. if (needsUpdate) {
  65. this.renderable.update();
  66. }
  67. }
  68. render(viewport: Viewport, target: RenderTarget | undefined) {
  69. if (isTimingMode) this.webgl.timer.mark('FxaaPass.render');
  70. if (target) {
  71. target.bind();
  72. } else {
  73. this.webgl.unbindFramebuffer();
  74. }
  75. this.updateState(viewport);
  76. this.renderable.render();
  77. if (isTimingMode) this.webgl.timer.markEnd('FxaaPass.render');
  78. }
  79. }
  80. //
  81. const FxaaSchema = {
  82. ...QuadSchema,
  83. tColor: TextureSpec('texture', 'rgba', 'ubyte', 'linear'),
  84. uTexSizeInv: UniformSpec('v2'),
  85. dEdgeThresholdMin: DefineSpec('number'),
  86. dEdgeThresholdMax: DefineSpec('number'),
  87. dIterations: DefineSpec('number'),
  88. dSubpixelQuality: DefineSpec('number'),
  89. };
  90. const FxaaShaderCode = ShaderCode('fxaa', quad_vert, fxaa_frag);
  91. type FxaaRenderable = ComputeRenderable<Values<typeof FxaaSchema>>
  92. function getFxaaRenderable(ctx: WebGLContext, colorTexture: Texture): FxaaRenderable {
  93. const width = colorTexture.getWidth();
  94. const height = colorTexture.getHeight();
  95. const values: Values<typeof FxaaSchema> = {
  96. ...QuadValues,
  97. tColor: ValueCell.create(colorTexture),
  98. uTexSizeInv: ValueCell.create(Vec2.create(1 / width, 1 / height)),
  99. dEdgeThresholdMin: ValueCell.create(0.0312),
  100. dEdgeThresholdMax: ValueCell.create(0.125),
  101. dIterations: ValueCell.create(12),
  102. dSubpixelQuality: ValueCell.create(0.3),
  103. };
  104. const schema = { ...FxaaSchema };
  105. const renderItem = createComputeRenderItem(ctx, 'triangles', FxaaShaderCode, schema, values);
  106. return createComputeRenderable(renderItem, values);
  107. }