-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
Expand file tree
/
Copy pathwebgpu_compute_texture_pingpong.html
More file actions
215 lines (138 loc) · 5.83 KB
/
webgpu_compute_texture_pingpong.html
File metadata and controls
215 lines (138 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<html lang="en">
<head>
<title>three.js webgpu - compute ping/pong texture</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="example.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
<div class="title-wrapper">
<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a>
<span>Compute Ping/Pong Texture</span>
</div>
<small>
Compute ping/pong texture using GPU (pure TSL).
</small>
</div>
<script type="importmap">
{
"imports": {
"three": "../build/three.webgpu.js",
"three/webgpu": "../build/three.webgpu.js",
"three/tsl": "../build/three.tsl.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three/webgpu';
import { storageTexture, textureStore, Fn, instanceIndex, uniform, float, vec2, vec4, uvec2, ivec2, int, NodeAccess } from 'three/tsl';
import WebGPU from 'three/addons/capabilities/WebGPU.js';
let camera, scene, renderer;
let computeInitNode, computeToPing, computeToPong;
let pingTexture, pongTexture;
let material;
let phase = true;
let lastUpdate = - 1;
const width = 512, height = 512;
const seed = uniform( new THREE.Vector2() );
init();
async function init() {
if ( WebGPU.isAvailable() === false ) {
document.body.appendChild( WebGPU.getErrorMessage() );
throw new Error( 'No WebGPU support' );
}
const aspect = window.innerWidth / window.innerHeight;
camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
camera.position.z = 1;
scene = new THREE.Scene();
// texture
const hdr = true;
pingTexture = new THREE.StorageTexture( width, height );
pongTexture = new THREE.StorageTexture( width, height );
if ( hdr ) {
pingTexture.type = THREE.HalfFloatType;
pongTexture.type = THREE.HalfFloatType;
}
const rand2 = Fn( ( [ n ] ) => {
return n.dot( vec2( 12.9898, 4.1414 ) ).sin().mul( 43758.5453 ).fract();
} );
// Create storage texture nodes with proper access
const writePing = storageTexture( pingTexture ).setAccess( NodeAccess.WRITE_ONLY );
const readPing = storageTexture( pingTexture ).setAccess( NodeAccess.READ_ONLY );
const writePong = storageTexture( pongTexture ).setAccess( NodeAccess.WRITE_ONLY );
const readPong = storageTexture( pongTexture ).setAccess( NodeAccess.READ_ONLY );
const computeInit = Fn( () => {
const posX = instanceIndex.mod( width );
const posY = instanceIndex.div( width );
const indexUV = uvec2( posX, posY );
const uv = vec2( float( posX ).div( width ), float( posY ).div( height ) );
const r = rand2( uv.add( seed.mul( 100 ) ) ).sub( rand2( uv.add( seed.mul( 300 ) ) ) );
const g = rand2( uv.add( seed.mul( 200 ) ) ).sub( rand2( uv.add( seed.mul( 300 ) ) ) );
const b = rand2( uv.add( seed.mul( 200 ) ) ).sub( rand2( uv.add( seed.mul( 100 ) ) ) );
textureStore( writePing, indexUV, vec4( r, g, b, 1 ) );
} );
computeInitNode = computeInit().compute( width * height );
// compute ping-pong: blur function using .load() for textureLoad
const blur = Fn( ( [ readTex, uv ] ) => {
const c0 = readTex.load( uv.add( ivec2( - 1, 1 ) ) );
const c1 = readTex.load( uv.add( ivec2( - 1, - 1 ) ) );
const c2 = readTex.load( uv.add( ivec2( 0, 0 ) ) );
const c3 = readTex.load( uv.add( ivec2( 1, - 1 ) ) );
const c4 = readTex.load( uv.add( ivec2( 1, 1 ) ) );
return c0.add( c1 ).add( c2 ).add( c3 ).add( c4 ).div( 5.0 );
} );
// compute loop: read from one texture, blur, write to another
const computePingPong = Fn( ( [ readTex, writeTex ] ) => {
const posX = instanceIndex.mod( width );
const posY = instanceIndex.div( width );
const indexUV = ivec2( int( posX ), int( posY ) );
const color = blur( readTex, indexUV );
textureStore( writeTex, indexUV, vec4( color.rgb.mul( 1.05 ), 1 ) );
} );
computeToPong = computePingPong( readPing, writePong ).compute( width * height );
computeToPing = computePingPong( readPong, writePing ).compute( width * height );
//
material = new THREE.MeshBasicMaterial( { color: 0xffffff, map: pongTexture } );
const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
scene.add( plane );
renderer = new THREE.WebGPURenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( render );
document.body.appendChild( renderer.domElement );
await renderer.init();
window.addEventListener( 'resize', onWindowResize );
// compute init
renderer.compute( computeInitNode );
}
function onWindowResize() {
renderer.setSize( window.innerWidth, window.innerHeight );
const aspect = window.innerWidth / window.innerHeight;
const frustumHeight = camera.top - camera.bottom;
camera.left = - frustumHeight * aspect / 2;
camera.right = frustumHeight * aspect / 2;
camera.updateProjectionMatrix();
}
function render() {
const time = performance.now();
const seconds = Math.floor( time / 1000 );
// reset every second
if ( phase && seconds !== lastUpdate ) {
seed.value.set( Math.random(), Math.random() );
renderer.compute( computeInitNode );
lastUpdate = seconds;
}
// compute step
renderer.compute( phase ? computeToPong : computeToPing );
material.map = phase ? pongTexture : pingTexture;
phase = ! phase;
// render step
// update material texture node
renderer.render( scene, camera );
}
</script>
</body>
</html>