fxaa.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. export const FxaaParams = {
  21. edgeThresholdMin: PD.Numeric(0.0312, { min: 0.0312, max: 0.0833, step: 0.0001 }, { description: 'Trims the algorithm from processing darks.' }),
  22. 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.' }),
  23. iterations: PD.Numeric(12, { min: 0, max: 16, step: 1 }, { description: 'Number of edge exploration steps.' }),
  24. subpixelQuality: PD.Numeric(0.30, { min: 0.00, max: 1.00, step: 0.01 }, { description: 'Choose the amount of sub-pixel aliasing removal.' }),
  25. };
  26. export type FxaaProps = PD.Values<typeof FxaaParams>
  27. export class FxaaPass {
  28. private readonly renderable: FxaaRenderable
  29. constructor(private webgl: WebGLContext, input: Texture) {
  30. this.renderable = getFxaaRenderable(webgl, input);
  31. }
  32. private updateState(viewport: Viewport) {
  33. const { gl, state } = this.webgl;
  34. state.enable(gl.SCISSOR_TEST);
  35. state.disable(gl.BLEND);
  36. state.disable(gl.DEPTH_TEST);
  37. state.depthMask(false);
  38. const { x, y, width, height } = viewport;
  39. gl.viewport(x, y, width, height);
  40. gl.scissor(x, y, width, height);
  41. state.clearColor(0, 0, 0, 1);
  42. gl.clear(gl.COLOR_BUFFER_BIT);
  43. }
  44. setSize(width: number, height: number) {
  45. ValueCell.update(this.renderable.values.uTexSizeInv, Vec2.set(this.renderable.values.uTexSizeInv.ref.value, 1 / width, 1 / height));
  46. }
  47. update(input: Texture, props: FxaaProps) {
  48. const { values } = this.renderable;
  49. const { edgeThresholdMin, edgeThresholdMax, iterations, subpixelQuality } = props;
  50. let needsUpdate = false;
  51. if (values.tColor.ref.value !== input) {
  52. ValueCell.update(this.renderable.values.tColor, input);
  53. needsUpdate = true;
  54. }
  55. if (values.dEdgeThresholdMin.ref.value !== edgeThresholdMin) needsUpdate = true;
  56. ValueCell.updateIfChanged(values.dEdgeThresholdMin, edgeThresholdMin);
  57. if (values.dEdgeThresholdMax.ref.value !== edgeThresholdMax) needsUpdate = true;
  58. ValueCell.updateIfChanged(values.dEdgeThresholdMax, edgeThresholdMax);
  59. if (values.dIterations.ref.value !== iterations) needsUpdate = true;
  60. ValueCell.updateIfChanged(values.dIterations, iterations);
  61. if (values.dSubpixelQuality.ref.value !== subpixelQuality) needsUpdate = true;
  62. ValueCell.updateIfChanged(values.dSubpixelQuality, subpixelQuality);
  63. if (needsUpdate) {
  64. this.renderable.update();
  65. }
  66. }
  67. render(viewport: Viewport, target: RenderTarget | undefined) {
  68. if (target) {
  69. target.bind();
  70. } else {
  71. this.webgl.unbindFramebuffer();
  72. }
  73. this.updateState(viewport);
  74. this.renderable.render();
  75. }
  76. }
  77. //
  78. const FxaaSchema = {
  79. ...QuadSchema,
  80. tColor: TextureSpec('texture', 'rgba', 'ubyte', 'linear'),
  81. uTexSizeInv: UniformSpec('v2'),
  82. dEdgeThresholdMin: DefineSpec('number'),
  83. dEdgeThresholdMax: DefineSpec('number'),
  84. dIterations: DefineSpec('number'),
  85. dSubpixelQuality: DefineSpec('number'),
  86. };
  87. const FxaaShaderCode = ShaderCode('fxaa', quad_vert, fxaa_frag);
  88. type FxaaRenderable = ComputeRenderable<Values<typeof FxaaSchema>>
  89. function getFxaaRenderable(ctx: WebGLContext, colorTexture: Texture): FxaaRenderable {
  90. const width = colorTexture.getWidth();
  91. const height = colorTexture.getHeight();
  92. const values: Values<typeof FxaaSchema> = {
  93. ...QuadValues,
  94. tColor: ValueCell.create(colorTexture),
  95. uTexSizeInv: ValueCell.create(Vec2.create(1 / width, 1 / height)),
  96. dEdgeThresholdMin: ValueCell.create(0.0312),
  97. dEdgeThresholdMax: ValueCell.create(0.125),
  98. dIterations: ValueCell.create(12),
  99. dSubpixelQuality: ValueCell.create(0.3),
  100. };
  101. const schema = { ...FxaaSchema };
  102. const renderItem = createComputeRenderItem(ctx, 'triangles', FxaaShaderCode, schema, values);
  103. return createComputeRenderable(renderItem, values);
  104. }