draw.ts 17 KB

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