webgpu_compute_nanite-style.html 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - nanite-style rasterizer</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js webgpu - nanite-style rasterizer">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_compute_nanite-style.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_compute_nanite-style.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>GPU-Driven Nanite-style Rasterizer</span>
  18. </div>
  19. <small>Rendering <span id="triangleCount"></span> triangles.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { Fn, If, Loop, vec4, vec2, uvec4, mat4, uint, float, int, min, max, atomicMax, atomicAdd, atomicStore, atomicLoad, floor, cos, sin, dot, bool, storage, uniform, uniformArray, uv, instanceIndex, vertexIndex, distance, screenSize, time, texture, varyingProperty, sqrt } from 'three/tsl';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  38. if ( WebGPU.isAvailable() === false ) {
  39. document.body.appendChild( WebGPU.getErrorMessage() );
  40. throw new Error( 'No WebGPU support' );
  41. }
  42. let camera, renderer, controls;
  43. let computeRasterize, computeClear, computeFrustum, computeDispatch, computeHWArgs;
  44. let quadMesh, hwScene, hwMesh;
  45. let cameraPos, projScreenMatrixUniform, frustumPlanesUniform;
  46. let cotHalfFovUniform, pixelErrorThresholdUniform, maxRasterSizeUniform;
  47. let workQueueCountAtomic, workQueueCountRead, dispatchBuffer, workQueueBuffer, chunkBoundsBuffer;
  48. let instanceWorldBuffer, instanceMvpBuffer;
  49. let instanceWorldAttr, instanceMvpAttr;
  50. let hwDrawBuffer;
  51. let timeScale;
  52. let screenTriAttr, screenTriAtomic, screenTriRead;
  53. let screenInstAttr, screenInstBuffer, screenInstRead;
  54. let maxPixels;
  55. const rows = 400;
  56. const cols = 400;
  57. const instanceCount = rows * cols;
  58. const MAX_RASTER_SIZE = 16;
  59. const options = { Mode: 'Meshlet Debug', Rasterizer: 'Both' };
  60. // Buffer visibility packaging configuration
  61. const TRIANGLE_INDEX_BITS = 14; // Bits allocated for triangle index (2^14 = 16384 max triangles)
  62. const TRIANGLE_INDEX_MASK = 0x3FFF; // Bitmask to extract triangle index (14 bits)
  63. const DEPTH_PRECISION_MAX = 4294967295.0; // Maximum value of the 32-bit depth (2^32 - 1)
  64. const background = new THREE.Color( .1, .1, .1 );
  65. init();
  66. async function init() {
  67. renderer = new THREE.WebGPURenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. renderer.setAnimationLoop( animate );
  71. renderer.inspector = new Inspector();
  72. document.body.appendChild( renderer.domElement );
  73. await renderer.init();
  74. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, .25, 1000000 );
  75. camera.position.set( 0, 15, 50 );
  76. controls = new OrbitControls( camera, renderer.domElement );
  77. controls.target.y = - 1.5;
  78. controls.enableDamping = true;
  79. controls.zoomSpeed = .5;
  80. controls.maxDistance = 1000;
  81. controls.maxPolarAngle = Math.PI / 2;
  82. // Generate LOD Geometries
  83. const lods = [
  84. { geometry: new TeapotGeometry( 1, 10 ), error: 0.0 },
  85. { geometry: new TeapotGeometry( 1, 8 ), error: 0.005 },
  86. { geometry: new TeapotGeometry( 1, 6 ), error: 0.015 },
  87. { geometry: new TeapotGeometry( 1, 5 ), error: 0.03 },
  88. { geometry: new TeapotGeometry( 1, 4 ), error: 0.06 },
  89. { geometry: new TeapotGeometry( 1, 3 ), error: 0.1 },
  90. { geometry: new TeapotGeometry( 1, 2 ), error: 0.2 }
  91. ];
  92. let totalVertices = 0;
  93. let totalIndices = 0;
  94. for ( const lod of lods ) {
  95. const geom = lod.geometry;
  96. const pos = geom.attributes.position;
  97. const uvs = geom.attributes.uv;
  98. const idx = geom.index ? Array.from( geom.index.array ) : Array.from( { length: pos.count }, ( _, i ) => i );
  99. lod.numVertices = pos.count;
  100. lod.numTriangles = idx.length / 3;
  101. lod.vertexOffset = totalVertices;
  102. lod.indexOffset = totalIndices;
  103. lod.positions = pos;
  104. lod.uvs = uvs;
  105. lod.indices = idx;
  106. totalVertices += pos.count;
  107. totalIndices += idx.length;
  108. }
  109. const maxTrianglesPerInstance = lods[ 0 ].numTriangles;
  110. const totalTriangles = rows * cols * maxTrianglesPerInstance;
  111. document.getElementById( 'triangleCount' ).innerText = new Intl.NumberFormat().format( totalTriangles );
  112. const vertexArray = new Float32Array( totalVertices * 4 ); // vec4 padded
  113. const uvArray = new Float32Array( totalVertices * 2 );
  114. const indexArray = new Uint32Array( totalIndices );
  115. const meshletTriangleArray = new Uint32Array( totalIndices / 3 ); // 1 meshlet ID per triangle
  116. let currentMeshletId = 1;
  117. for ( const lod of lods ) {
  118. for ( let i = 0; i < lod.numVertices; i ++ ) {
  119. const vIdx = lod.vertexOffset + i;
  120. vertexArray[ vIdx * 4 + 0 ] = lod.positions.getX( i );
  121. vertexArray[ vIdx * 4 + 1 ] = lod.positions.getY( i );
  122. vertexArray[ vIdx * 4 + 2 ] = lod.positions.getZ( i );
  123. vertexArray[ vIdx * 4 + 3 ] = 1.0;
  124. if ( lod.uvs ) {
  125. uvArray[ vIdx * 2 + 0 ] = lod.uvs.getX( i );
  126. uvArray[ vIdx * 2 + 1 ] = lod.uvs.getY( i );
  127. }
  128. }
  129. let currentTriCount = 0;
  130. for ( let i = 0; i < lod.numTriangles; i ++ ) {
  131. const triIdx = ( lod.indexOffset / 3 ) + i;
  132. indexArray[ triIdx * 3 + 0 ] = lod.vertexOffset + lod.indices[ i * 3 + 0 ];
  133. indexArray[ triIdx * 3 + 1 ] = lod.vertexOffset + lod.indices[ i * 3 + 1 ];
  134. indexArray[ triIdx * 3 + 2 ] = lod.vertexOffset + lod.indices[ i * 3 + 2 ];
  135. if ( currentTriCount >= 126 ) {
  136. currentMeshletId ++;
  137. currentTriCount = 0;
  138. }
  139. meshletTriangleArray[ triIdx ] = currentMeshletId;
  140. currentTriCount ++;
  141. }
  142. currentMeshletId ++;
  143. }
  144. // Precompute Bounding Spheres for each 64-triangle Chunk (Cluster)
  145. let totalChunks = 0;
  146. for ( const lod of lods ) {
  147. lod.numChunks = Math.ceil( lod.numTriangles / 64 );
  148. lod.chunkStart = totalChunks;
  149. totalChunks += lod.numChunks;
  150. }
  151. const chunkBoundsData = new Float32Array( totalChunks * 4 ); // vec4: cx, cy, cz, radius
  152. let currentChunkId = 0;
  153. for ( const lod of lods ) {
  154. const positions = lod.positions;
  155. const indices = lod.indices;
  156. for ( let c = 0; c < lod.numChunks; c ++ ) {
  157. const startTri = c * 64;
  158. const endTri = Math.min( startTri + 64, lod.numTriangles );
  159. // 1. Calculate Center
  160. let cx = 0, cy = 0, cz = 0;
  161. const vertCount = ( endTri - startTri ) * 3;
  162. for ( let t = startTri; t < endTri; t ++ ) {
  163. for ( let v = 0; v < 3; v ++ ) {
  164. const idx = indices[ t * 3 + v ];
  165. cx += positions.getX( idx );
  166. cy += positions.getY( idx );
  167. cz += positions.getZ( idx );
  168. }
  169. }
  170. cx /= vertCount;
  171. cy /= vertCount;
  172. cz /= vertCount;
  173. // 2. Calculate Radius
  174. let maxDistSq = 0;
  175. for ( let t = startTri; t < endTri; t ++ ) {
  176. for ( let v = 0; v < 3; v ++ ) {
  177. const idx = indices[ t * 3 + v ];
  178. const dx = positions.getX( idx ) - cx;
  179. const dy = positions.getY( idx ) - cy;
  180. const dz = positions.getZ( idx ) - cz;
  181. const distSq = dx * dx + dy * dy + dz * dz;
  182. if ( distSq > maxDistSq ) maxDistSq = distSq;
  183. }
  184. }
  185. const radius = Math.sqrt( maxDistSq );
  186. chunkBoundsData[ currentChunkId * 4 + 0 ] = cx;
  187. chunkBoundsData[ currentChunkId * 4 + 1 ] = cy;
  188. chunkBoundsData[ currentChunkId * 4 + 2 ] = cz;
  189. chunkBoundsData[ currentChunkId * 4 + 3 ] = radius;
  190. currentChunkId ++;
  191. }
  192. }
  193. // Upload LOD offsets to GPU (uvec4: triangleStart, numTriangles, chunkStart, 0)
  194. const lodOffsetsData = new Uint32Array( lods.length * 4 );
  195. for ( let i = 0; i < lods.length; i ++ ) {
  196. lodOffsetsData[ i * 4 + 0 ] = lods[ i ].indexOffset / 3;
  197. lodOffsetsData[ i * 4 + 1 ] = lods[ i ].numTriangles;
  198. lodOffsetsData[ i * 4 + 2 ] = lods[ i ].chunkStart;
  199. }
  200. const lodOffsetsBuffer = storage( new THREE.StorageBufferAttribute( lodOffsetsData, 4 ), 'uvec4', lods.length ).toReadOnly();
  201. chunkBoundsBuffer = storage( new THREE.StorageBufferAttribute( chunkBoundsData, 4 ), 'vec4', totalChunks ).toReadOnly();
  202. // Storage Buffers
  203. const vertexBuffer = storage( new THREE.StorageBufferAttribute( vertexArray, 4 ), 'vec4', totalVertices ).toReadOnly();
  204. const uvBuffer = storage( new THREE.StorageBufferAttribute( uvArray, 2 ), 'vec2', totalVertices ).toReadOnly();
  205. const indexBuffer = storage( new THREE.StorageBufferAttribute( indexArray, 1 ), 'uint', totalIndices ).toReadOnly();
  206. const meshletIdBuffer = storage( new THREE.StorageBufferAttribute( meshletTriangleArray, 1 ), 'uint', totalIndices / 3 ).toReadOnly();
  207. const materialModeUniform = uniform( 0, 'uint' );
  208. const textureMap = new THREE.TextureLoader().load( 'textures/uv_grid_directx.jpg' );
  209. textureMap.colorSpace = THREE.SRGBColorSpace;
  210. textureMap.wrapS = THREE.RepeatWrapping;
  211. textureMap.wrapT = THREE.RepeatWrapping;
  212. timeScale = uniform( 1.0 );
  213. const parameterGroup = renderer.inspector.createParameters( 'Parameters' );
  214. parameterGroup.add( options, 'Mode', { 'Meshlet Debug': 'Meshlet Debug', 'Texture': 'Texture' } ).addEventListener( 'change', ( e ) => {
  215. materialModeUniform.value = e.value === 'Texture' ? 1 : 0;
  216. } );
  217. parameterGroup.add( options, 'Rasterizer', { 'SW Only': 'SW Only', 'HW Only': 'HW Only', 'Both': 'Both' } );
  218. parameterGroup.add( timeScale, 'value', 0.0, 1.0 ).name( 'Animation Speed' );
  219. // Atomic visibility buffers — pack depth + payload into single u32 for race-free atomicMax
  220. // Buffer A: depth(18 high bits) | megaTriangleIndex(14 low bits) — triangle ID
  221. // Buffer B: depth(14 high bits) | instId(18 low bits) — instance ID
  222. // Since depth occupies the high bits, atomicMax picks the closest fragment AND its payload atomically
  223. createScreenBuffers();
  224. const staticInstanceData = new Float32Array( instanceCount * 4 );
  225. let dataIndex = 0;
  226. for ( let i = 0; i < rows; i ++ ) {
  227. for ( let j = 0; j < cols; j ++ ) {
  228. staticInstanceData[ dataIndex ++ ] = ( i - rows / 2 ) * 4.0;
  229. staticInstanceData[ dataIndex ++ ] = - 1;
  230. staticInstanceData[ dataIndex ++ ] = ( j - cols / 2 ) * 4.0;
  231. staticInstanceData[ dataIndex ++ ] = 1.0; // scale
  232. }
  233. }
  234. const instanceDataBuffer = storage( new THREE.StorageBufferAttribute( staticInstanceData, 4 ), 'vec4', instanceCount );
  235. const instanceWorldData = new Float32Array( instanceCount * 16 );
  236. const instanceMvpData = new Float32Array( instanceCount * 16 );
  237. instanceWorldAttr = new THREE.StorageBufferAttribute( instanceWorldData, 16 );
  238. instanceMvpAttr = new THREE.StorageBufferAttribute( instanceMvpData, 16 );
  239. instanceWorldBuffer = storage( instanceWorldAttr, 'mat4', instanceCount );
  240. instanceMvpBuffer = storage( instanceMvpAttr, 'mat4', instanceCount );
  241. const instanceWorldRead = storage( instanceWorldAttr, 'mat4', instanceCount ).toReadOnly();
  242. const workQueueCountData = new Uint32Array( 1 );
  243. const workQueueCountAttr = new THREE.StorageBufferAttribute( workQueueCountData, 1 );
  244. workQueueCountAtomic = storage( workQueueCountAttr, 'uint', 1 ).toAtomic();
  245. workQueueCountRead = storage( workQueueCountAttr, 'uint', 1 ).toReadOnly();
  246. const dispatchData = new Uint32Array( 3 );
  247. const dispatchAttr = new THREE.IndirectStorageBufferAttribute( dispatchData, 3 );
  248. dispatchBuffer = storage( dispatchAttr, 'uint', 3 );
  249. // Max work items = 60000 instances * 47 chunks = 2,820,000
  250. const MAX_WORK_ITEMS = 2820000;
  251. const workQueueData = new Uint32Array( MAX_WORK_ITEMS * 4 );
  252. workQueueBuffer = storage( new THREE.StorageBufferAttribute( workQueueData, 4 ), 'uvec4', MAX_WORK_ITEMS );
  253. // HW Rasterizer Buffers (for large triangles that exceed SW raster budget)
  254. const MAX_HW_TRIANGLES = 100000;
  255. // HW queue: index 0 is atomic counter, indices 1..MAX store payload32
  256. const hwQueueData = new Uint32Array( MAX_HW_TRIANGLES + 1 );
  257. const hwQueueAttr = new THREE.StorageBufferAttribute( hwQueueData, 1 );
  258. const hwQueueAtomic = storage( hwQueueAttr, 'uint', MAX_HW_TRIANGLES + 1 ).toAtomic();
  259. const hwQueueRead = storage( hwQueueAttr, 'uint', MAX_HW_TRIANGLES + 1 ).toReadOnly();
  260. // Draw indirect buffer: vertexCount, instanceCount, firstVertex, firstInstance
  261. const hwDrawData = new Uint32Array( 4 );
  262. const hwDrawAttr = new THREE.IndirectStorageBufferAttribute( hwDrawData, 4 );
  263. hwDrawBuffer = storage( hwDrawAttr, 'uint', 4 );
  264. projScreenMatrixUniform = uniform( new THREE.Matrix4() );
  265. frustumPlanesUniform = uniformArray( [
  266. new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4(),
  267. new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4()
  268. ], 'vec4' );
  269. cameraPos = uniform( new THREE.Vector3() );
  270. cotHalfFovUniform = uniform( 1.0 );
  271. pixelErrorThresholdUniform = uniform( 4.0 );
  272. maxRasterSizeUniform = uniform( MAX_RASTER_SIZE, 'int' ); // Max bounding box size in pixels for SW rasterizer
  273. // Compute Clear
  274. computeClear = Fn( () => {
  275. atomicStore( screenTriAtomic.element( instanceIndex ), uint( 0 ) );
  276. screenInstBuffer.element( instanceIndex ).assign( uint( 0 ) );
  277. If( instanceIndex.equal( 0 ), () => {
  278. atomicStore( workQueueCountAtomic.element( 0 ), uint( 0 ) );
  279. atomicStore( hwQueueAtomic.element( 0 ), uint( 0 ) );
  280. } );
  281. } )().compute( maxPixels, [ 256 ] ).setName( 'Compute Clear' );
  282. // Compute Frustum (GPU Culling, LOD & Work Allocation)
  283. computeFrustum = Fn( () => {
  284. const data = instanceDataBuffer.element( instanceIndex );
  285. const pos = data.xyz;
  286. const scale = data.w;
  287. const i = float( instanceIndex );
  288. // Rotation
  289. const rotY = time.mul( timeScale ).add( i );
  290. const c = cos( rotY );
  291. const s = sin( rotY );
  292. // Compose MatrixWorld
  293. const matrixWorld = mat4(
  294. vec4( c.mul( scale ), 0.0, s.mul( scale ), 0.0 ),
  295. vec4( 0.0, scale, 0.0, 0.0 ),
  296. vec4( s.negate().mul( scale ), 0.0, c.mul( scale ), 0.0 ),
  297. vec4( pos, 1.0 )
  298. );
  299. const visible = bool( true ).toVar();
  300. const radius = scale.mul( 2.0 ); // bounding sphere radius
  301. // Frustum culling using the 6 extracted world-space planes
  302. Loop( { start: 0, end: 6 }, ( { i: planeIndex } ) => {
  303. const plane = frustumPlanesUniform.element( planeIndex );
  304. const dist = dot( plane.xyz, pos ).add( plane.w );
  305. If( dist.lessThan( radius.negate() ), () => {
  306. visible.assign( false );
  307. } );
  308. } );
  309. If( visible, () => {
  310. const distToCamera = distance( cameraPos, pos );
  311. // Precompute projection factor once (Screen-Space Projected Error)
  312. // pixelError = cotHalfFov * errorWorld / dist * screenH / 2
  313. const pixelFactor = cotHalfFovUniform.div( max( 0.01, distToCamera ) ).mul( float( screenSize.y ) ).div( 2.0 );
  314. const lodLevel = uint( 0 ).toVar();
  315. let lodSelection = null;
  316. for ( let i = lods.length - 1; i > 0; i -- ) {
  317. const checkLod = float( lods[ i ].error ).mul( scale ).mul( pixelFactor ).lessThanEqual( pixelErrorThresholdUniform );
  318. if ( lodSelection === null ) {
  319. lodSelection = If( checkLod, () => {
  320. lodLevel.assign( i );
  321. } );
  322. } else {
  323. lodSelection = lodSelection.ElseIf( checkLod, () => {
  324. lodLevel.assign( i );
  325. } );
  326. }
  327. }
  328. const lodData = lodOffsetsBuffer.element( lodLevel );
  329. const lodTriStart = lodData.x;
  330. const lodNumTriangles = lodData.y;
  331. const lodChunkStart = lodData.z;
  332. // Calculate Work Items (64 triangles per item)
  333. const workItems = lodNumTriangles.add( 63 ).div( 64 );
  334. // Evaluate each Chunk (Cluster)
  335. Loop( { name: 'cIdx', type: 'uint', start: uint( 0 ), end: workItems, condition: '<' }, ( { cIdx: chunkIndex } ) => {
  336. const globalChunkId = lodChunkStart.add( uint( chunkIndex ) );
  337. const chunkBounds = chunkBoundsBuffer.element( globalChunkId );
  338. const chunkCenterLocal = chunkBounds.xyz;
  339. const chunkRadiusLocal = chunkBounds.w;
  340. // Transform chunk bounding sphere to world space and store as var to prevent inlining
  341. const chunkCenterWorld = matrixWorld.mul( vec4( chunkCenterLocal, 1.0 ) ).xyz.toVar();
  342. const chunkRadiusWorld = chunkRadiusLocal.mul( scale ).toVar();
  343. const chunkVisible = bool( true ).toVar();
  344. // Frustum cull the chunk
  345. Loop( { name: 'pIdx', start: 0, end: 6 }, ( { pIdx: planeIndex } ) => {
  346. const plane = frustumPlanesUniform.element( planeIndex );
  347. const chunkDist = dot( plane.xyz, chunkCenterWorld ).add( plane.w );
  348. If( chunkDist.lessThan( chunkRadiusWorld.negate() ), () => {
  349. chunkVisible.assign( false );
  350. } );
  351. } );
  352. If( chunkVisible, () => {
  353. const itemIndex = atomicAdd( workQueueCountAtomic.element( 0 ), 1 );
  354. // uvec4( instanceIndex, triangleStart, lodNumTriangles, chunkIndex )
  355. workQueueBuffer.element( itemIndex ).assign(
  356. uvec4( instanceIndex, lodTriStart, lodNumTriangles, uint( chunkIndex ) )
  357. );
  358. } );
  359. } );
  360. // Store transform for this instance
  361. instanceWorldBuffer.element( instanceIndex ).assign( matrixWorld );
  362. instanceMvpBuffer.element( instanceIndex ).assign( projScreenMatrixUniform.mul( matrixWorld ) );
  363. } );
  364. } )().compute( instanceCount ).setName( 'Compute Frustum' );
  365. // Compute Dispatch (Indirect arguments)
  366. computeDispatch = Fn( () => {
  367. const totalWorkgroups = workQueueCountRead.element( 0 );
  368. const maxDim = uint( 65535 );
  369. // Split totalWorkgroups into 2D dispatch if it exceeds 65535
  370. const dispatchX = min( totalWorkgroups, maxDim );
  371. const dispatchY = totalWorkgroups.add( maxDim ).sub( 1 ).div( maxDim );
  372. dispatchBuffer.element( 0 ).assign( dispatchX );
  373. dispatchBuffer.element( 1 ).assign( dispatchY );
  374. dispatchBuffer.element( 2 ).assign( 1 );
  375. } )().compute( 1 ).setName( 'Compute Dispatch' );
  376. // Edge function for barycentric coordinates
  377. const edgeFunction = Fn( ( [ a, b, c ] ) => {
  378. // (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)
  379. return c.y.sub( a.y ).mul( b.x.sub( a.x ) ).sub( c.x.sub( a.x ).mul( b.y.sub( a.y ) ) );
  380. } );
  381. // Compute Rasterizer
  382. computeRasterize = Fn( () => {
  383. const totalWorkgroups = workQueueCountRead.element( 0 );
  384. const totalThreads = totalWorkgroups.mul( 64 );
  385. If( instanceIndex.lessThan( totalThreads ), () => {
  386. const workItemId = instanceIndex.div( 64 );
  387. const localTriangleIndex = instanceIndex.mod( 64 );
  388. const workItem = workQueueBuffer.element( workItemId );
  389. const instId = workItem.x;
  390. const lodTriStart = workItem.y;
  391. const lodNumTriangles = workItem.z;
  392. const chunkIndex = workItem.w;
  393. const globalTriangleIndex = chunkIndex.mul( 64 ).add( localTriangleIndex );
  394. If( globalTriangleIndex.lessThan( lodNumTriangles ), () => {
  395. const megaTriangleIndex = lodTriStart.add( globalTriangleIndex );
  396. const indexOffset = megaTriangleIndex.mul( 3 );
  397. const i0 = indexBuffer.element( indexOffset );
  398. const i1 = indexBuffer.element( indexOffset.add( 1 ) );
  399. const i2 = indexBuffer.element( indexOffset.add( 2 ) );
  400. const v0 = vertexBuffer.element( i0 );
  401. const v1 = vertexBuffer.element( i1 );
  402. const v2 = vertexBuffer.element( i2 );
  403. const instMvpMatrix = instanceMvpBuffer.element( instId );
  404. // MVP
  405. const p0 = instMvpMatrix.mul( v0 );
  406. const p1 = instMvpMatrix.mul( v1 );
  407. const p2 = instMvpMatrix.mul( v2 );
  408. // Near plane clipping
  409. // If( p0.w.greaterThan( 0.5 ).and( p1.w.greaterThan( 0.5 ) ).and( p2.w.greaterThan( 0.5 ) ), () => {
  410. If( p0.w.greaterThan( 0.0 ).and( p1.w.greaterThan( 0.0 ) ).and( p2.w.greaterThan( 0.0 ) ), () => {
  411. const ndc0 = p0.xyz.div( p0.w );
  412. const ndc1 = p1.xyz.div( p1.w );
  413. const ndc2 = p2.xyz.div( p2.w );
  414. // Early Backface Culling in NDC
  415. const areaNdc = edgeFunction( ndc0, ndc1, ndc2 );
  416. If( areaNdc.greaterThan( 0.0 ), () => {
  417. // NDC guard: skip triangles entirely outside clip volume
  418. const ndcMinX = min( ndc0.x, min( ndc1.x, ndc2.x ) );
  419. const ndcMaxX = max( ndc0.x, max( ndc1.x, ndc2.x ) );
  420. const ndcMinY = min( ndc0.y, min( ndc1.y, ndc2.y ) );
  421. const ndcMaxY = max( ndc0.y, max( ndc1.y, ndc2.y ) );
  422. If( ndcMaxX.greaterThan( - 1.0 ).and( ndcMinX.lessThan( 1.0 ) ).and( ndcMaxY.greaterThan( - 1.0 ) ).and( ndcMinY.lessThan( 1.0 ) ), () => {
  423. // Map to screen coordinates
  424. const w = screenSize.x;
  425. const h = screenSize.y;
  426. const s0 = ndc0.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  427. const s1 = ndc1.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  428. const s2 = ndc2.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  429. // Bounding Box
  430. const minX = max( 0.0, min( s0.x, min( s1.x, s2.x ) ) );
  431. const maxX = min( w.sub( 1.0 ), max( s0.x, max( s1.x, s2.x ) ) );
  432. const minY = max( 0.0, min( s0.y, min( s1.y, s2.y ) ) );
  433. const maxY = min( h.sub( 1.0 ), max( s0.y, max( s1.y, s2.y ) ) );
  434. const startX = int( floor( minX ) );
  435. const endX = int( floor( maxX ) );
  436. const startY = int( floor( minY ) );
  437. const endY = int( floor( maxY ) );
  438. // Big triangle guard: skip triangles larger than maxRasterSize
  439. // This is the key performance safeguard — software rasterizers
  440. // should only handle small triangles. Large triangles cause O(n²)
  441. // pixel iteration per thread, which kills performance when close.
  442. const bbWidth = endX.sub( startX );
  443. const bbHeight = endY.sub( startY );
  444. // Compute payload32 for HW path (full precision)
  445. // payload32: instId (18 bits) | megaTriangleIndex (14 bits)
  446. const payload32 = instId.shiftLeft( TRIANGLE_INDEX_BITS ).bitOr( megaTriangleIndex.bitAnd( TRIANGLE_INDEX_MASK ) );
  447. // Sub-pixel / Valid bounds rejection + big triangle guard
  448. If( startX.lessThanEqual( endX ).and( startY.lessThanEqual( endY ) ).and( bbWidth.lessThanEqual( maxRasterSizeUniform ) ).and( bbHeight.lessThanEqual( maxRasterSizeUniform ) ), () => {
  449. const area = edgeFunction( s0, s1, s2 );
  450. const stepX_w0 = s1.y.sub( s2.y );
  451. const stepY_w0 = s2.x.sub( s1.x );
  452. const stepX_w1 = s2.y.sub( s0.y );
  453. const stepY_w1 = s0.x.sub( s2.x );
  454. const stepX_w2 = s0.y.sub( s1.y );
  455. const stepY_w2 = s1.x.sub( s0.x );
  456. // Top-Left rule check for each edge to guarantee watertightness
  457. const isTopLeft0 = stepX_w0.lessThan( 0.0 ).or( stepX_w0.equal( 0.0 ).and( stepY_w0.greaterThan( 0.0 ) ) );
  458. const isTopLeft1 = stepX_w1.lessThan( 0.0 ).or( stepX_w1.equal( 0.0 ).and( stepY_w1.greaterThan( 0.0 ) ) );
  459. const isTopLeft2 = stepX_w2.lessThan( 0.0 ).or( stepX_w2.equal( 0.0 ).and( stepY_w2.greaterThan( 0.0 ) ) );
  460. const bias0 = isTopLeft0.select( 0.0, - 1e-5 );
  461. const bias1 = isTopLeft1.select( 0.0, - 1e-5 );
  462. const bias2 = isTopLeft2.select( 0.0, - 1e-5 );
  463. const pStart = vec2( float( startX ).add( 0.5 ), float( startY ).add( 0.5 ) );
  464. const row_w0 = edgeFunction( s1, s2, pStart ).toVar();
  465. const row_w1 = edgeFunction( s2, s0, pStart ).toVar();
  466. const row_w2 = edgeFunction( s0, s1, pStart ).toVar();
  467. row_w0.addAssign( bias0 );
  468. row_w1.addAssign( bias1 );
  469. row_w2.addAssign( bias2 );
  470. // Incremental Z Math (ALU Optimization)
  471. const b0_start = row_w0.div( area );
  472. const b1_start = row_w1.div( area );
  473. const b2_start = row_w2.div( area );
  474. const row_z = b0_start.mul( ndc0.z ).add( b1_start.mul( ndc1.z ) ).add( b2_start.mul( ndc2.z ) ).toVar();
  475. const stepX_z = stepX_w0.div( area ).mul( ndc0.z ).add( stepX_w1.div( area ).mul( ndc1.z ) ).add( stepX_w2.div( area ).mul( ndc2.z ) );
  476. const stepY_z = stepY_w0.div( area ).mul( ndc0.z ).add( stepY_w1.div( area ).mul( ndc1.z ) ).add( stepY_w2.div( area ).mul( ndc2.z ) );
  477. Loop( { name: 'y', type: 'int', start: startY, end: endY, condition: '<=' }, ( { y } ) => {
  478. const w0 = row_w0.toVar();
  479. const w1 = row_w1.toVar();
  480. const w2 = row_w2.toVar();
  481. const z = row_z.toVar();
  482. Loop( { name: 'x', type: 'int', start: startX, end: endX, condition: '<=' }, ( { x } ) => {
  483. If( w0.greaterThanEqual( 0.0 ).and( w1.greaterThanEqual( 0.0 ) ).and( w2.greaterThanEqual( 0.0 ) ), () => {
  484. If( z.greaterThanEqual( 0.0 ).and( z.lessThanEqual( 1.0 ) ), () => {
  485. // Calculate 32-bit depth value (fourth-root distribution to maximize depth precision)
  486. const depth32 = uint( sqrt( sqrt( float( 1.0 ).sub( z ) ) ).mul( DEPTH_PRECISION_MAX ) );
  487. const pixelIndex = uint( y ).mul( uint( screenSize.x ) ).add( uint( x ) );
  488. // Early depth pre-check: skip atomicMax if pixel already has closer fragment
  489. const currentDepth = atomicLoad( screenTriAtomic.element( pixelIndex ) );
  490. If( depth32.greaterThan( currentDepth ), () => {
  491. // Atomic depth test
  492. const prevDepth = atomicMax( screenTriAtomic.element( pixelIndex ), depth32 );
  493. // If we successfully wrote the closest depth, write the payload
  494. If( depth32.greaterThan( prevDepth ), () => {
  495. screenInstBuffer.element( pixelIndex ).assign( payload32 );
  496. } );
  497. } );
  498. } );
  499. } );
  500. w0.addAssign( stepX_w0 );
  501. w1.addAssign( stepX_w1 );
  502. w2.addAssign( stepX_w2 );
  503. z.addAssign( stepX_z );
  504. } );
  505. row_w0.addAssign( stepY_w0 );
  506. row_w1.addAssign( stepY_w1 );
  507. row_w2.addAssign( stepY_w2 );
  508. row_z.addAssign( stepY_z );
  509. } );
  510. } ).Else( () => {
  511. // Big triangle → enqueue for HW rasterization
  512. If( startX.lessThanEqual( endX ).and( startY.lessThanEqual( endY ) ), () => {
  513. const hwCount = atomicAdd( hwQueueAtomic.element( 0 ), 1 );
  514. const hwSlot = hwCount.add( 1 );
  515. atomicStore( hwQueueAtomic.element( hwSlot ), payload32 );
  516. } );
  517. } );
  518. } );
  519. } ); // End Early Backface Culling
  520. } ); // End Near Plane Clipping
  521. } ); // End globalTriangleIndex bounds check
  522. } ); // End instanceIndex bounds check
  523. } )().compute( dispatchAttr ).setName( 'Compute Rasterize' );
  524. // Compute HW Draw Indirect Args
  525. computeHWArgs = Fn( () => {
  526. const hwCount = atomicLoad( hwQueueAtomic.element( 0 ) );
  527. // Non-indexed draw: vertexCount = hwCount * 3 (3 verts per triangle)
  528. hwDrawBuffer.element( 0 ).assign( hwCount.mul( 3 ) ); // vertexCount
  529. hwDrawBuffer.element( 1 ).assign( uint( 1 ) ); // instanceCount
  530. hwDrawBuffer.element( 2 ).assign( uint( 0 ) ); // firstVertex
  531. hwDrawBuffer.element( 3 ).assign( uint( 0 ) ); // firstInstance
  532. } )().compute( 1 ).setName( 'Compute HW Args' );
  533. // Hash function for meshlet colors (shared between HW mesh and fullscreen quad)
  534. const hashColor = Fn( ( [ id_in ] ) => {
  535. let id = uint( id_in ).toVar();
  536. id = id.mul( uint( 747796405 ) ).add( uint( 289559509 ) );
  537. id = id.shiftRight( 16 ).bitXor( id ).mul( uint( 277803737 ) );
  538. id = id.shiftRight( 16 ).bitXor( id );
  539. const r = float( id.bitAnd( uint( 255 ) ) ).div( 255.0 );
  540. const g = float( id.shiftRight( 8 ).bitAnd( uint( 255 ) ) ).div( 255.0 );
  541. const b = float( id.shiftRight( 16 ).bitAnd( uint( 255 ) ) ).div( 255.0 );
  542. return vec4( r.mul( 0.8 ).add( 0.2 ), g.mul( 0.8 ).add( 0.2 ), b.mul( 0.8 ).add( 0.2 ), 1.0 );
  543. } );
  544. // HW Rasterizer Mesh (renders big triangles via GPU hardware pipeline)
  545. // Unlike the SW rasterizer which writes to an atomic screen buffer,
  546. // the HW mesh renders directly with real colors and hardware depth testing.
  547. // It renders AFTER the fullscreen quad, overlaying HW-rasterized triangles.
  548. {
  549. // Geometry: dummy positions, vertex count driven by indirect draw
  550. const hwGeometry = new THREE.BufferGeometry();
  551. hwGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( new Float32Array( MAX_HW_TRIANGLES * 3 * 3 ), 3 ) );
  552. hwGeometry.setIndirect( hwDrawAttr );
  553. hwGeometry.boundingSphere = new THREE.Sphere().set( new THREE.Vector3(), Infinity );
  554. // Varying to pass payload and UVs from vertex to fragment
  555. const vPayload = varyingProperty( 'uint', 'vPayload' );
  556. const vUv = varyingProperty( 'vec2', 'vUv' );
  557. const hwMaterial = new THREE.NodeMaterial();
  558. hwMaterial.depthWrite = true;
  559. hwMaterial.depthTest = true;
  560. // Vertex shader: vertex pulling from HW queue
  561. hwMaterial.positionNode = Fn( () => {
  562. // vertexIndex: 0,1,2, 3,4,5, 6,7,8, ...
  563. const triIndex = vertexIndex.div( 3 ); // which triangle in HW queue
  564. const localVert = vertexIndex.mod( 3 ); // which vertex (0, 1, 2)
  565. const payload32 = hwQueueRead.element( triIndex.add( 1 ) );
  566. const instId = payload32.shiftRight( TRIANGLE_INDEX_BITS );
  567. const megaTriIdx = payload32.bitAnd( TRIANGLE_INDEX_MASK );
  568. // Fetch actual vertex index from the mega index buffer
  569. const vertGlobalIdx = indexBuffer.element( megaTriIdx.mul( 3 ).add( localVert ) );
  570. const v = vertexBuffer.element( vertGlobalIdx );
  571. // Transform to world space
  572. const worldPos = instanceWorldRead.element( instId ).mul( v );
  573. const uvVal = uvBuffer.element( vertGlobalIdx );
  574. vUv.assign( uvVal );
  575. vPayload.assign( payload32 );
  576. return worldPos.xyz;
  577. } )();
  578. // Fragment shader: directly output final color (no storage buffer writes)
  579. hwMaterial.fragmentNode = Fn( () => {
  580. const payload32 = vPayload;
  581. const instId = payload32.shiftRight( TRIANGLE_INDEX_BITS );
  582. const megaTriangleIndex = payload32.bitAnd( TRIANGLE_INDEX_MASK );
  583. const outColor = vec4( 0.0 ).toVar();
  584. If( materialModeUniform.equal( 0 ), () => {
  585. const meshletId = meshletIdBuffer.element( megaTriangleIndex ).add( instId.mul( 1000 ) );
  586. outColor.assign( hashColor( meshletId ) );
  587. } ).Else( () => {
  588. // Hardware interpolated UV!
  589. outColor.assign( texture( textureMap, vUv ) );
  590. } );
  591. return outColor;
  592. } )();
  593. hwMesh = new THREE.Mesh( hwGeometry, hwMaterial );
  594. hwMesh.frustumCulled = false;
  595. hwScene = new THREE.Scene();
  596. hwScene.add( hwMesh );
  597. }
  598. // Fullscreen Presentation Pass
  599. const material = new THREE.NodeMaterial();
  600. material.depthWrite = true;
  601. // Shared screen-coordinate helper
  602. const getPixelIndex = () => {
  603. const screenX = uint( floor( uv().x.mul( screenSize.x ) ) );
  604. const screenY = uint( floor( uv().y.oneMinus().mul( screenSize.y ) ) );
  605. return { screenX, screenY, pixelIndex: screenY.mul( uint( screenSize.x ) ).add( screenX ) };
  606. };
  607. // Output depth from the SW rasterizer so HW mesh can depth test against it
  608. material.depthNode = Fn( () => {
  609. const { pixelIndex } = getPixelIndex();
  610. // Read 32-bit depth from buffer
  611. const depth32 = screenTriRead.element( pixelIndex );
  612. // Reconstruct NDC Z from non-linear depth32 (fourth-root distribution)
  613. const y = float( depth32 ).div( DEPTH_PRECISION_MAX );
  614. const y2 = y.mul( y );
  615. const v = y2.mul( y2 ); // raise to the fourth power (y^4) to get original v
  616. return float( 1.0 ).sub( v );
  617. } )();
  618. material.colorNode = Fn( () => {
  619. const { pixelIndex } = getPixelIndex();
  620. // Single buffer read — check for background immediately (using 32-bit depth)
  621. const depth32 = screenTriRead.element( pixelIndex );
  622. // Background color for pixels with no geometry
  623. const outColor = vec4( background, 1.0 ).toVar();
  624. If( depth32.greaterThan( 0 ), () => {
  625. // Read the single packed payload
  626. const payload32 = screenInstRead.element( pixelIndex );
  627. const megaTriangleIndex = payload32.bitAnd( TRIANGLE_INDEX_MASK );
  628. const instId = payload32.shiftRight( TRIANGLE_INDEX_BITS );
  629. // Visibility Buffer: Fetch exact vertices and UVs
  630. const i0 = indexBuffer.element( megaTriangleIndex.mul( 3 ).add( 0 ) );
  631. const i1 = indexBuffer.element( megaTriangleIndex.mul( 3 ).add( 1 ) );
  632. const i2 = indexBuffer.element( megaTriangleIndex.mul( 3 ).add( 2 ) );
  633. const v0 = vertexBuffer.element( i0 );
  634. const v1 = vertexBuffer.element( i1 );
  635. const v2 = vertexBuffer.element( i2 );
  636. const t_uv0 = uvBuffer.element( i0 );
  637. const t_uv1 = uvBuffer.element( i1 );
  638. const t_uv2 = uvBuffer.element( i2 );
  639. // Project Vertices to Screen Space
  640. const matrixWorld = instanceWorldBuffer.element( instId );
  641. const mvpMatrix = projScreenMatrixUniform.mul( matrixWorld );
  642. const p0 = mvpMatrix.mul( v0 );
  643. const p1 = mvpMatrix.mul( v1 );
  644. const p2 = mvpMatrix.mul( v2 );
  645. const ndc0 = p0.xyz.div( p0.w );
  646. const ndc1 = p1.xyz.div( p1.w );
  647. const ndc2 = p2.xyz.div( p2.w );
  648. const w = screenSize.x;
  649. const h = screenSize.y;
  650. const s0 = ndc0.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  651. const s1 = ndc1.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  652. const s2 = ndc2.xy.add( 1.0 ).mul( 0.5 ).mul( vec2( w, h ) );
  653. const p = vec2( uv().x.mul( screenSize.x ), uv().y.oneMinus().mul( screenSize.y ) );
  654. // Compute Barycentrics
  655. const area = edgeFunction( s0, s1, s2 );
  656. const w0 = edgeFunction( s1, s2, p );
  657. const w1 = edgeFunction( s2, s0, p );
  658. const w2 = edgeFunction( s0, s1, p );
  659. // Guard against division by zero for safe execution
  660. const safeArea = area.equal( 0.0 ).select( 1.0, area );
  661. const b0 = w0.div( safeArea );
  662. const b1 = w1.div( safeArea );
  663. const b2 = w2.div( safeArea );
  664. // Perspective correct UV interpolation (32-bit floats!)
  665. const z_inv = b0.div( p0.w ).add( b1.div( p1.w ) ).add( b2.div( p2.w ) );
  666. const safeZInv = z_inv.equal( 0.0 ).select( 1.0, z_inv );
  667. const b0_p = b0.div( p0.w ).div( safeZInv );
  668. const b1_p = b1.div( p1.w ).div( safeZInv );
  669. const b2_p = b2.div( p2.w ).div( safeZInv );
  670. const uv_interp = t_uv0.mul( b0_p ).add( t_uv1.mul( b1_p ) ).add( t_uv2.mul( b2_p ) );
  671. // Compute screen-space derivatives analytically (extremely clean, no helper fragment issues)
  672. const dw0_dx = s2.y.sub( s1.y );
  673. const dw1_dx = s0.y.sub( s2.y );
  674. const dw2_dx = s1.y.sub( s0.y );
  675. const dw0_dy = s1.x.sub( s2.x );
  676. const dw1_dy = s2.x.sub( s0.x );
  677. const dw2_dy = s0.x.sub( s1.x );
  678. const q0 = float( 1.0 ).div( p0.w );
  679. const q1 = float( 1.0 ).div( p1.w );
  680. const q2 = float( 1.0 ).div( p2.w );
  681. const sum_w_q = w0.mul( q0 ).add( w1.mul( q1 ) ).add( w2.mul( q2 ) );
  682. const safe_sum_w_q = sum_w_q.equal( 0.0 ).select( 1.0, sum_w_q );
  683. const dUvDx = (
  684. dw0_dx.mul( q0 ).mul( t_uv0.sub( uv_interp ) )
  685. .add( dw1_dx.mul( q1 ).mul( t_uv1.sub( uv_interp ) ) )
  686. .add( dw2_dx.mul( q2 ).mul( t_uv2.sub( uv_interp ) ) )
  687. ).div( safe_sum_w_q );
  688. const dUvDy = (
  689. dw0_dy.mul( q0 ).mul( t_uv0.sub( uv_interp ) )
  690. .add( dw1_dy.mul( q1 ).mul( t_uv1.sub( uv_interp ) ) )
  691. .add( dw2_dy.mul( q2 ).mul( t_uv2.sub( uv_interp ) ) )
  692. ).div( safe_sum_w_q );
  693. If( materialModeUniform.equal( 0 ), () => {
  694. const meshletId = meshletIdBuffer.element( megaTriangleIndex ).add( instId.mul( 1000 ) );
  695. outColor.assign( hashColor( meshletId ) );
  696. } ).Else( () => {
  697. outColor.assign( texture( textureMap, uv_interp ).grad( dUvDx, dUvDy ) );
  698. } );
  699. } );
  700. return outColor;
  701. } )();
  702. quadMesh = new THREE.QuadMesh( material );
  703. window.addEventListener( 'resize', onWindowResize );
  704. }
  705. function createScreenBuffers() {
  706. const size = new THREE.Vector2();
  707. renderer.getDrawingBufferSize( size );
  708. const newMaxPixels = size.x * size.y;
  709. if ( newMaxPixels === maxPixels ) return;
  710. maxPixels = newMaxPixels;
  711. if ( screenTriAttr ) screenTriAttr.dispose();
  712. if ( screenInstAttr ) screenInstAttr.dispose();
  713. const screenTriData = new Uint32Array( maxPixels );
  714. screenTriAttr = new THREE.StorageBufferAttribute( screenTriData, 1 );
  715. const screenInstData = new Uint32Array( maxPixels );
  716. screenInstAttr = new THREE.StorageBufferAttribute( screenInstData, 1 );
  717. if ( screenTriAtomic === undefined ) {
  718. screenTriAtomic = storage( screenTriAttr, 'uint', maxPixels ).toAtomic();
  719. screenTriRead = storage( screenTriAttr, 'uint', maxPixels ).toReadOnly();
  720. screenInstBuffer = storage( screenInstAttr, 'uint', maxPixels );
  721. screenInstRead = storage( screenInstAttr, 'uint', maxPixels ).toReadOnly();
  722. } else {
  723. screenTriAtomic.value = screenTriAttr;
  724. screenTriAtomic.bufferCount = maxPixels;
  725. screenTriRead.value = screenTriAttr;
  726. screenTriRead.bufferCount = maxPixels;
  727. screenInstBuffer.value = screenInstAttr;
  728. screenInstBuffer.bufferCount = maxPixels;
  729. screenInstRead.value = screenInstAttr;
  730. screenInstRead.bufferCount = maxPixels;
  731. computeClear.count = maxPixels;
  732. computeClear.dispose();
  733. computeRasterize.dispose();
  734. computeFrustum.dispose();
  735. computeDispatch.dispose();
  736. computeHWArgs.dispose();
  737. quadMesh.material.dispose();
  738. hwMesh.material.dispose();
  739. }
  740. }
  741. function onWindowResize() {
  742. camera.aspect = window.innerWidth / window.innerHeight;
  743. camera.updateProjectionMatrix();
  744. renderer.setSize( window.innerWidth, window.innerHeight );
  745. createScreenBuffers();
  746. }
  747. const frustum = new THREE.Frustum();
  748. const projScreenMatrix = new THREE.Matrix4();
  749. const cameraInverse = new THREE.Matrix4();
  750. function animate() {
  751. controls.update();
  752. camera.updateMatrixWorld();
  753. cameraInverse.copy( camera.matrixWorld ).invert();
  754. projScreenMatrix.multiplyMatrices( camera.projectionMatrix, cameraInverse );
  755. frustum.setFromProjectionMatrix( projScreenMatrix );
  756. // Update GPU uniforms
  757. projScreenMatrixUniform.value.copy( projScreenMatrix );
  758. cameraPos.value.copy( camera.position );
  759. cotHalfFovUniform.value = camera.projectionMatrix.elements[ 5 ];
  760. // Pack frustum planes into the uniform array
  761. const planes = frustum.planes;
  762. const planesArray = frustumPlanesUniform.array;
  763. for ( let i = 0; i < 6; i ++ ) {
  764. const p = planes[ i ];
  765. planesArray[ i ].set( p.normal.x, p.normal.y, p.normal.z, p.constant );
  766. }
  767. // Compute & Render
  768. renderer.compute( computeClear );
  769. renderer.compute( computeFrustum );
  770. renderer.compute( computeDispatch );
  771. renderer.compute( computeRasterize );
  772. renderer.compute( computeHWArgs );
  773. const rasterMode = options.Rasterizer;
  774. // SW presentation (fullscreen quad reads atomic buffer)
  775. if ( rasterMode === 'SW Only' || rasterMode === 'Both' ) {
  776. quadMesh.render( renderer );
  777. }
  778. // HW mesh renders with real depth testing + colors
  779. if ( rasterMode === 'HW Only' || rasterMode === 'Both' ) {
  780. hwScene.background = ( rasterMode === 'HW Only' ) ? background : null;
  781. renderer.autoClear = ( rasterMode === 'HW Only' );
  782. renderer.render( hwScene, camera );
  783. renderer.autoClear = true;
  784. }
  785. }
  786. </script>
  787. </body>
  788. </html>
粤ICP备19079148号