draw.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. * @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. if (this.wboit?.supported) {
  96. this.wboit.setSize(width, height);
  97. }
  98. if (this.dpoit?.supported) {
  99. this.dpoit.setSize(width, height);
  100. }
  101. this.marking.setSize(width, height);
  102. this.postprocessing.setSize(width, height);
  103. this.antialiasing.setSize(width, height);
  104. }
  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.colorTarget.bind();
  109. renderer.clear(true);
  110. // render opaque primitives
  111. this.depthTextureOpaque.attachFramebuffer(this.colorTarget.framebuffer, 'depth');
  112. this.colorTarget.bind();
  113. renderer.clearDepth();
  114. renderer.renderDpoitOpaque(scene.primitives, camera, null);
  115. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  116. if (PostprocessingPass.isOutlineEnabled(postprocessingProps)) {
  117. this.depthTargetTransparent.bind();
  118. renderer.clearDepth(true);
  119. if (scene.opacityAverage < 1) {
  120. renderer.renderDepthTransparent(scene.primitives, camera, this.depthTextureOpaque);
  121. }
  122. }
  123. this.postprocessing.render(camera, false, transparentBackground, renderer.props.backgroundColor, postprocessingProps);
  124. }
  125. let dpoitTextures;
  126. // render transparent primitives and volumes
  127. if (scene.opacityAverage < 1 || scene.volumes.renderables.length > 0) {
  128. dpoitTextures = this.dpoit.bind();
  129. if (isTimingMode) this.webgl.timer.mark('DpoitPasses.render');
  130. if (scene.opacityAverage < 1) {
  131. renderer.renderDpoitTransparent(scene.primitives, camera, this.depthTextureOpaque, dpoitTextures);
  132. }
  133. if (scene.volumes.renderables.length > 0) {
  134. renderer.renderDpoitTransparent(scene.volumes, camera, this.depthTextureOpaque, dpoitTextures);
  135. }
  136. for (let i = 0; i < iterations; i++) {
  137. dpoitTextures = this.dpoit.bindDualDepthPeeling();
  138. if (scene.opacityAverage < 1) {
  139. renderer.renderDpoitTransparent(scene.primitives, camera, this.depthTextureOpaque, dpoitTextures);
  140. }
  141. if (scene.volumes.renderables.length > 0) {
  142. renderer.renderDpoitTransparent(scene.volumes, camera, this.depthTextureOpaque, dpoitTextures);
  143. }
  144. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  145. this.postprocessing.target.bind();
  146. } else {
  147. this.colorTarget.bind();
  148. }
  149. this.dpoit.renderBlendBack();
  150. }
  151. if (isTimingMode) this.webgl.timer.markEnd('DpoitPasses.render');
  152. // evaluate dpoit
  153. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  154. this.postprocessing.target.bind();
  155. } else {
  156. this.colorTarget.bind();
  157. }
  158. this.dpoit.render();
  159. }
  160. }
  161. private _renderWboit(renderer: Renderer, camera: ICamera, scene: Scene, transparentBackground: boolean, postprocessingProps: PostprocessingProps) {
  162. if (!this.wboit?.supported) throw new Error('expected wboit to be supported');
  163. this.depthTextureOpaque.attachFramebuffer(this.colorTarget.framebuffer, 'depth');
  164. renderer.clear(true);
  165. // render opaque primitives
  166. if (scene.hasOpaque) {
  167. renderer.renderWboitOpaque(scene.primitives, camera, null);
  168. }
  169. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  170. if (PostprocessingPass.isOutlineEnabled(postprocessingProps)) {
  171. this.depthTargetTransparent.bind();
  172. renderer.clearDepth(true);
  173. if (scene.opacityAverage < 1) {
  174. renderer.renderDepthTransparent(scene.primitives, camera, this.depthTextureOpaque);
  175. }
  176. }
  177. this.postprocessing.render(camera, false, transparentBackground, renderer.props.backgroundColor, postprocessingProps);
  178. }
  179. // render transparent primitives and volumes
  180. if (scene.opacityAverage < 1 || scene.volumes.renderables.length > 0) {
  181. this.wboit.bind();
  182. if (scene.opacityAverage < 1) {
  183. renderer.renderWboitTransparent(scene.primitives, camera, this.depthTextureOpaque);
  184. }
  185. if (scene.volumes.renderables.length > 0) {
  186. renderer.renderWboitTransparent(scene.volumes, camera, this.depthTextureOpaque);
  187. }
  188. // evaluate wboit
  189. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  190. this.postprocessing.target.bind();
  191. } else {
  192. this.colorTarget.bind();
  193. }
  194. this.wboit.render();
  195. }
  196. }
  197. private _renderBlended(renderer: Renderer, camera: ICamera, scene: Scene, toDrawingBuffer: boolean, transparentBackground: boolean, postprocessingProps: PostprocessingProps) {
  198. if (toDrawingBuffer) {
  199. this.drawTarget.bind();
  200. } else {
  201. if (!this.packedDepth) {
  202. this.depthTextureOpaque.attachFramebuffer(this.colorTarget.framebuffer, 'depth');
  203. } else {
  204. this.colorTarget.bind();
  205. }
  206. }
  207. renderer.clear(true);
  208. if (scene.hasOpaque) {
  209. renderer.renderBlendedOpaque(scene.primitives, camera, null);
  210. }
  211. if (!toDrawingBuffer) {
  212. // do a depth pass if not rendering to drawing buffer and
  213. // extensions.depthTexture is unsupported (i.e. depthTarget is set)
  214. if (this.depthTargetOpaque) {
  215. this.depthTargetOpaque.bind();
  216. renderer.clearDepth(true);
  217. renderer.renderDepthOpaque(scene.primitives, camera, null);
  218. this.colorTarget.bind();
  219. }
  220. if (PostprocessingPass.isEnabled(postprocessingProps)) {
  221. if (!this.packedDepth) {
  222. this.depthTextureOpaque.detachFramebuffer(this.postprocessing.target.framebuffer, 'depth');
  223. } else {
  224. this.colorTarget.depthRenderbuffer?.detachFramebuffer(this.postprocessing.target.framebuffer);
  225. }
  226. if (PostprocessingPass.isOutlineEnabled(postprocessingProps)) {
  227. this.depthTargetTransparent.bind();
  228. renderer.clearDepth(true);
  229. if (scene.opacityAverage < 1) {
  230. renderer.renderDepthTransparent(scene.primitives, camera, this.depthTextureOpaque);
  231. }
  232. }
  233. this.postprocessing.render(camera, false, transparentBackground, renderer.props.backgroundColor, postprocessingProps);
  234. if (!this.packedDepth) {
  235. this.depthTextureOpaque.attachFramebuffer(this.postprocessing.target.framebuffer, 'depth');
  236. } else {
  237. this.colorTarget.depthRenderbuffer?.attachFramebuffer(this.postprocessing.target.framebuffer);
  238. }
  239. }
  240. if (scene.volumes.renderables.length > 0) {
  241. const target = PostprocessingPass.isEnabled(postprocessingProps)
  242. ? this.postprocessing.target : this.colorTarget;
  243. if (!this.packedDepth) {
  244. this.depthTextureOpaque.detachFramebuffer(target.framebuffer, 'depth');
  245. } else {
  246. this.colorTarget.depthRenderbuffer?.detachFramebuffer(target.framebuffer);
  247. }
  248. target.bind();
  249. renderer.renderBlendedVolume(scene.volumes, camera, this.depthTextureOpaque);
  250. if (!this.packedDepth) {
  251. this.depthTextureOpaque.attachFramebuffer(target.framebuffer, 'depth');
  252. } else {
  253. this.colorTarget.depthRenderbuffer?.attachFramebuffer(target.framebuffer);
  254. }
  255. target.bind();
  256. }
  257. }
  258. if (scene.opacityAverage < 1) {
  259. renderer.renderBlendedTransparent(scene.primitives, camera, null);
  260. }
  261. }
  262. private _render(renderer: Renderer, camera: ICamera, scene: Scene, helper: Helper, toDrawingBuffer: boolean, transparentBackground: boolean, props: Props) {
  263. const volumeRendering = scene.volumes.renderables.length > 0;
  264. const postprocessingEnabled = PostprocessingPass.isEnabled(props.postprocessing);
  265. const antialiasingEnabled = AntialiasingPass.isEnabled(props.postprocessing);
  266. const markingEnabled = MarkingPass.isEnabled(props.marking);
  267. const { x, y, width, height } = camera.viewport;
  268. renderer.setViewport(x, y, width, height);
  269. renderer.update(camera);
  270. if (transparentBackground && !antialiasingEnabled && toDrawingBuffer) {
  271. this.drawTarget.bind();
  272. renderer.clear(false);
  273. }
  274. if (this.wboitEnabled) {
  275. this._renderWboit(renderer, camera, scene, transparentBackground, props.postprocessing);
  276. } else if (this.dpoitEnabled) {
  277. this._renderDpoit(renderer, camera, scene, props.dpoitIterations, transparentBackground, props.postprocessing);
  278. } else {
  279. this._renderBlended(renderer, camera, scene, !volumeRendering && !postprocessingEnabled && !antialiasingEnabled && toDrawingBuffer, transparentBackground, props.postprocessing);
  280. }
  281. const target = postprocessingEnabled
  282. ? this.postprocessing.target
  283. : !toDrawingBuffer || volumeRendering || this.wboitEnabled || this.dpoitEnabled
  284. ? this.colorTarget
  285. : this.drawTarget;
  286. if (markingEnabled && scene.markerAverage > 0) {
  287. const markingDepthTest = props.marking.ghostEdgeStrength < 1;
  288. if (markingDepthTest && scene.markerAverage !== 1) {
  289. this.marking.depthTarget.bind();
  290. renderer.clear(false, true);
  291. renderer.renderMarkingDepth(scene.primitives, camera, null);
  292. }
  293. this.marking.maskTarget.bind();
  294. renderer.clear(false, true);
  295. renderer.renderMarkingMask(scene.primitives, camera, markingDepthTest ? this.marking.depthTarget.texture : null);
  296. this.marking.update(props.marking);
  297. this.marking.render(camera.viewport, target);
  298. } else {
  299. target.bind();
  300. }
  301. if (helper.debug.isEnabled) {
  302. helper.debug.syncVisibility();
  303. renderer.renderBlended(helper.debug.scene, camera);
  304. }
  305. if (helper.handle.isEnabled) {
  306. renderer.renderBlended(helper.handle.scene, camera);
  307. }
  308. if (helper.camera.isEnabled) {
  309. helper.camera.update(camera);
  310. renderer.update(helper.camera.camera);
  311. renderer.renderBlended(helper.camera.scene, helper.camera.camera);
  312. }
  313. if (antialiasingEnabled) {
  314. this.antialiasing.render(camera, toDrawingBuffer, props.postprocessing);
  315. } else if (toDrawingBuffer) {
  316. this.drawTarget.bind();
  317. this.webgl.state.disable(this.webgl.gl.DEPTH_TEST);
  318. if (postprocessingEnabled) {
  319. this.copyFboPostprocessing.render();
  320. } else if (volumeRendering || this.wboitEnabled || this.dpoitEnabled) {
  321. this.copyFboTarget.render();
  322. }
  323. }
  324. this.webgl.gl.flush();
  325. }
  326. render(ctx: RenderContext, props: Props, toDrawingBuffer: boolean) {
  327. if (isTimingMode) this.webgl.timer.mark('DrawPass.render');
  328. const { renderer, camera, scene, helper } = ctx;
  329. this.postprocessing.setTransparentBackground(props.transparentBackground);
  330. const transparentBackground = props.transparentBackground || this.postprocessing.background.isEnabled(props.postprocessing.background);
  331. renderer.setTransparentBackground(transparentBackground);
  332. renderer.setDrawingBufferSize(this.colorTarget.getWidth(), this.colorTarget.getHeight());
  333. renderer.setPixelRatio(this.webgl.pixelRatio);
  334. if (StereoCamera.is(camera)) {
  335. this._render(renderer, camera.left, scene, helper, toDrawingBuffer, transparentBackground, props);
  336. this._render(renderer, camera.right, scene, helper, toDrawingBuffer, transparentBackground, props);
  337. } else {
  338. this._render(renderer, camera, scene, helper, toDrawingBuffer, transparentBackground, props);
  339. }
  340. if (isTimingMode) this.webgl.timer.markEnd('DrawPass.render');
  341. }
  342. getColorTarget(postprocessingProps: PostprocessingProps): RenderTarget {
  343. if (AntialiasingPass.isEnabled(postprocessingProps)) {
  344. return this.antialiasing.target;
  345. } else if (PostprocessingPass.isEnabled(postprocessingProps)) {
  346. return this.postprocessing.target;
  347. }
  348. return this.colorTarget;
  349. }
  350. }