draw.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
  6. */
  7. import { WebGLContext } from '../../mol-gl/webgl/context';
  8. import { createNullRenderTarget, RenderTarget } from '../../mol-gl/webgl/render-target';
  9. import { Renderer } from '../../mol-gl/renderer';
  10. import { Scene } from '../../mol-gl/scene';
  11. import { Texture } from '../../mol-gl/webgl/texture';
  12. import { Camera, ICamera } from '../camera';
  13. import { ValueCell } from '../../mol-util';
  14. import { Vec2 } from '../../mol-math/linear-algebra';
  15. import { Helper } from '../helper/helper';
  16. import { StereoCamera } from '../camera/stereo';
  17. import { WboitPass } from './wboit';
  18. import { AntialiasingPass, PostprocessingPass, PostprocessingProps } from './postprocessing';
  19. import { MarkingPass, MarkingProps } from './marking';
  20. import { CopyRenderable, createCopyRenderable } from '../../mol-gl/compute/util';
  21. type Props = {
  22. postprocessing: PostprocessingProps
  23. marking: MarkingProps
  24. transparentBackground: boolean;
  25. }
  26. type RenderContext = {
  27. renderer: Renderer;
  28. camera: Camera | StereoCamera;
  29. scene: Scene;
  30. helper: Helper;
  31. }
  32. export class DrawPass {
  33. private readonly drawTarget: RenderTarget;
  34. readonly colorTarget: RenderTarget;
  35. readonly depthTextureTransparent: Texture;
  36. readonly depthTextureOpaque: Texture;
  37. readonly packedDepth: boolean;
  38. private depthTargetTransparent: RenderTarget;
  39. private depthTargetOpaque: RenderTarget | null;
  40. private copyFboTarget: CopyRenderable;
  41. private copyFboPostprocessing: CopyRenderable;
  42. private wboit: WboitPass | undefined;
  43. private readonly marking: MarkingPass;
  44. readonly postprocessing: PostprocessingPass;
  45. private readonly antialiasing: AntialiasingPass;
  46. get wboitEnabled() {
  47. return !!this.wboit?.supported;
  48. }
  49. constructor(private webgl: WebGLContext, width: number, height: number, enableWboit: boolean) {
  50. const { extensions, resources, isWebGL2 } = webgl;
  51. this.drawTarget = createNullRenderTarget(webgl.gl);
  52. this.colorTarget = webgl.createRenderTarget(width, height, true, 'uint8', 'linear');
  53. this.packedDepth = !extensions.depthTexture;
  54. this.depthTargetTransparent = webgl.createRenderTarget(width, height);
  55. this.depthTextureTransparent = this.depthTargetTransparent.texture;
  56. this.depthTargetOpaque = this.packedDepth ? webgl.createRenderTarget(width, height) : null;
  57. this.depthTextureOpaque = this.depthTargetOpaque ? this.depthTargetOpaque.texture : resources.texture('image-depth', 'depth', isWebGL2 ? 'float' : 'ushort', 'nearest');
  58. if (!this.packedDepth) {
  59. this.depthTextureOpaque.define(width, height);
  60. }
  61. this.wboit = enableWboit ? new WboitPass(webgl, width, height) : undefined;
  62. this.marking = new MarkingPass(webgl, width, height);
  63. this.postprocessing = new PostprocessingPass(webgl, this);
  64. this.antialiasing = new AntialiasingPass(webgl, this);
  65. this.copyFboTarget = createCopyRenderable(webgl, this.colorTarget.texture);
  66. this.copyFboPostprocessing = createCopyRenderable(webgl, this.postprocessing.target.texture);
  67. }
  68. reset() {
  69. this.wboit?.reset();
  70. }
  71. setSize(width: number, height: number) {
  72. const w = this.colorTarget.getWidth();
  73. const h = this.colorTarget.getHeight();
  74. if (width !== w || height !== h) {
  75. this.colorTarget.setSize(width, height);
  76. this.depthTargetTransparent.setSize(width, height);
  77. if (this.depthTargetOpaque) {
  78. this.depthTargetOpaque.setSize(width, height);
  79. } else {
  80. this.depthTextureOpaque.define(width, height);
  81. }
  82. ValueCell.update(this.copyFboTarget.values.uTexSize, Vec2.set(this.copyFboTarget.values.uTexSize.ref.value, width, height));
  83. ValueCell.update(this.copyFboPostprocessing.values.uTexSize, Vec2.set(this.copyFboPostprocessing.values.uTexSize.ref.value, width, height));
  84. if (this.wboit?.supported) {
  85. this.wboit.setSize(width, height);
  86. }
  87. this.marking.setSize(width, height);
  88. this.postprocessing.setSize(width, height);
  89. this.antialiasing.setSize(width, height);
  90. }
  91. }
  92. private _renderWboit(renderer: Renderer, camera: ICamera, scene: Scene, transparentBackground: boolean, postprocessingProps: PostprocessingProps) {
  93. if (!this.wboit?.supported) throw new Error('expected wboit to be supported');
  94. this.colorTarget.bind();
  95. renderer.clear(true);
  96. // render opaque primitives
  97. this.depthTextureOpaque.attachFramebuffer(this.colorTarget.framebuffer, 'depth');
  98. this.colorTarget.bind();
  99. renderer.clearDepth();
  100. renderer.renderWboitOpaque(scene.primitives, camera, null);
  101. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  102. if (PostprocessingPass.isOutlineEnabled(postprocessingProps)) {
  103. this.depthTargetTransparent.bind();
  104. renderer.clearDepth(true);
  105. if (scene.opacityAverage < 1) {
  106. renderer.renderDepthTransparent(scene.primitives, camera, this.depthTextureOpaque);
  107. }
  108. }
  109. this.postprocessing.render(camera, false, transparentBackground, renderer.props.backgroundColor, postprocessingProps);
  110. }
  111. // render transparent primitives and volumes
  112. if (scene.opacityAverage < 1 || scene.volumes.renderables.length > 0) {
  113. this.wboit.bind();
  114. renderer.renderWboitTransparent(scene.primitives, camera, this.depthTextureOpaque);
  115. renderer.renderWboitTransparent(scene.volumes, camera, this.depthTextureOpaque);
  116. // evaluate wboit
  117. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  118. this.postprocessing.target.bind();
  119. } else {
  120. this.colorTarget.bind();
  121. }
  122. this.wboit.render();
  123. }
  124. }
  125. private _renderBlended(renderer: Renderer, camera: ICamera, scene: Scene, toDrawingBuffer: boolean, transparentBackground: boolean, postprocessingProps: PostprocessingProps) {
  126. if (toDrawingBuffer) {
  127. this.drawTarget.bind();
  128. } else {
  129. this.colorTarget.bind();
  130. if (!this.packedDepth) {
  131. this.depthTextureOpaque.attachFramebuffer(this.colorTarget.framebuffer, 'depth');
  132. }
  133. }
  134. renderer.clear(true);
  135. renderer.renderBlendedOpaque(scene.primitives, camera, null);
  136. if (!toDrawingBuffer) {
  137. // do a depth pass if not rendering to drawing buffer and
  138. // extensions.depthTexture is unsupported (i.e. depthTarget is set)
  139. if (this.depthTargetOpaque) {
  140. this.depthTargetOpaque.bind();
  141. renderer.clearDepth(true);
  142. renderer.renderDepthOpaque(scene.primitives, camera, null);
  143. this.colorTarget.bind();
  144. }
  145. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  146. if (!this.packedDepth) {
  147. this.depthTextureOpaque.detachFramebuffer(this.postprocessing.target.framebuffer, 'depth');
  148. } else {
  149. this.colorTarget.depthRenderbuffer?.detachFramebuffer(this.postprocessing.target.framebuffer);
  150. }
  151. if (PostprocessingPass.isOutlineEnabled(postprocessingProps)) {
  152. this.depthTargetTransparent.bind();
  153. renderer.clearDepth(true);
  154. if (scene.opacityAverage < 1) {
  155. renderer.renderDepthTransparent(scene.primitives, camera, this.depthTextureOpaque);
  156. }
  157. }
  158. this.postprocessing.render(camera, false, transparentBackground, renderer.props.backgroundColor, postprocessingProps);
  159. if (!this.packedDepth) {
  160. this.depthTextureOpaque.attachFramebuffer(this.postprocessing.target.framebuffer, 'depth');
  161. } else {
  162. this.colorTarget.depthRenderbuffer?.attachFramebuffer(this.postprocessing.target.framebuffer);
  163. }
  164. }
  165. renderer.renderBlendedVolume(scene.volumes, camera, this.depthTextureOpaque);
  166. }
  167. renderer.renderBlendedTransparent(scene.primitives, camera, null);
  168. }
  169. private _render(renderer: Renderer, camera: ICamera, scene: Scene, helper: Helper, toDrawingBuffer: boolean, props: Props) {
  170. const volumeRendering = scene.volumes.renderables.length > 0;
  171. const postprocessingEnabled = PostprocessingPass.isEnabled(props.postprocessing);
  172. const antialiasingEnabled = AntialiasingPass.isEnabled(props.postprocessing);
  173. const markingEnabled = MarkingPass.isEnabled(props.marking);
  174. const { x, y, width, height } = camera.viewport;
  175. renderer.setViewport(x, y, width, height);
  176. renderer.update(camera);
  177. if (props.transparentBackground && !antialiasingEnabled && toDrawingBuffer) {
  178. this.drawTarget.bind();
  179. renderer.clear(false);
  180. }
  181. if (this.wboitEnabled) {
  182. this._renderWboit(renderer, camera, scene, props.transparentBackground, props.postprocessing);
  183. } else {
  184. this._renderBlended(renderer, camera, scene, !volumeRendering && !postprocessingEnabled && !antialiasingEnabled && toDrawingBuffer, props.transparentBackground, props.postprocessing);
  185. }
  186. if (postprocessingEnabled) {
  187. this.postprocessing.target.bind();
  188. } else if (!toDrawingBuffer || volumeRendering || this.wboitEnabled) {
  189. this.colorTarget.bind();
  190. } else {
  191. this.drawTarget.bind();
  192. }
  193. if (markingEnabled) {
  194. if (scene.markerAverage > 0) {
  195. const markingDepthTest = props.marking.ghostEdgeStrength < 1;
  196. if (markingDepthTest && scene.markerAverage !== 1) {
  197. this.marking.depthTarget.bind();
  198. renderer.clear(false, true);
  199. renderer.renderMarkingDepth(scene.primitives, camera, null);
  200. }
  201. this.marking.maskTarget.bind();
  202. renderer.clear(false, true);
  203. renderer.renderMarkingMask(scene.primitives, camera, markingDepthTest ? this.marking.depthTarget.texture : null);
  204. this.marking.update(props.marking);
  205. this.marking.render(camera.viewport, postprocessingEnabled ? this.postprocessing.target : this.colorTarget);
  206. }
  207. }
  208. if (helper.debug.isEnabled) {
  209. helper.debug.syncVisibility();
  210. renderer.renderBlended(helper.debug.scene, camera, null);
  211. }
  212. if (helper.handle.isEnabled) {
  213. renderer.renderBlended(helper.handle.scene, camera, null);
  214. }
  215. if (helper.camera.isEnabled) {
  216. helper.camera.update(camera);
  217. renderer.update(helper.camera.camera);
  218. renderer.renderBlended(helper.camera.scene, helper.camera.camera, null);
  219. }
  220. if (antialiasingEnabled) {
  221. this.antialiasing.render(camera, toDrawingBuffer, props.postprocessing);
  222. } else if (toDrawingBuffer) {
  223. this.drawTarget.bind();
  224. this.webgl.state.disable(this.webgl.gl.DEPTH_TEST);
  225. if (postprocessingEnabled) {
  226. this.copyFboPostprocessing.render();
  227. } else if (volumeRendering || this.wboitEnabled) {
  228. this.copyFboTarget.render();
  229. }
  230. }
  231. this.webgl.gl.flush();
  232. }
  233. render(ctx: RenderContext, props: Props, toDrawingBuffer: boolean) {
  234. const { renderer, camera, scene, helper } = ctx;
  235. renderer.setTransparentBackground(props.transparentBackground);
  236. renderer.setDrawingBufferSize(this.colorTarget.getWidth(), this.colorTarget.getHeight());
  237. renderer.setPixelRatio(this.webgl.pixelRatio);
  238. if (StereoCamera.is(camera)) {
  239. this._render(renderer, camera.left, scene, helper, toDrawingBuffer, props);
  240. this._render(renderer, camera.right, scene, helper, toDrawingBuffer, props);
  241. } else {
  242. this._render(renderer, camera, scene, helper, toDrawingBuffer, props);
  243. }
  244. }
  245. getColorTarget(postprocessingProps: PostprocessingProps): RenderTarget {
  246. if (AntialiasingPass.isEnabled(postprocessingProps)) {
  247. return this.antialiasing.target;
  248. } else if (PostprocessingPass.isEnabled(postprocessingProps)) {
  249. return this.postprocessing.target;
  250. }
  251. return this.colorTarget;
  252. }
  253. }