draw.ts 14 KB

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