p3nGu1nZz commited on
Commit
d48f607
·
1 Parent(s): 8e21624
Files changed (1) hide show
  1. index.html +13 -47
index.html CHANGED
@@ -18,51 +18,25 @@
18
  const canvas = document.querySelector('canvas');
19
  const context = canvas.getContext('webgpu');
20
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
21
- let device;
22
- let pipeline;
23
- let vertexBuffer;
24
- let indexBuffer;
25
- let uniformBuffer;
26
- let texture;
27
- let sampler;
28
- let bindGroup;
29
 
30
  async function main() {
31
  const adapter = await navigator.gpu?.requestAdapter();
32
  device = await adapter?.requestDevice();
33
- if (!device) {
34
- alert('need a browser that supports WebGPU');
35
- return;
36
- }
37
 
38
- context.configure({
39
- device,
40
- format: presentationFormat,
41
- });
42
 
43
  const shaderCode = await fetchShaderCode('shaders.wgsl');
44
- const module = device.createShaderModule({
45
- label: 'textured quad shaders',
46
- code: shaderCode,
47
- });
48
 
49
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, CONFIG);
50
- document.body.appendChild(glyphCanvas);
51
- glyphCanvas.style.backgroundColor = '#222';
52
 
53
  const vertexSize = CONFIG.floatsPerVertex * 4;
54
  const vertexBufferSize = CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * vertexSize;
55
- vertexBuffer = device.createBuffer({
56
- label: 'vertices',
57
- size: vertexBufferSize,
58
- usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
59
- });
60
-
61
- indexBuffer = device.createBuffer({
62
- label: 'indices',
63
- size: CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * 4,
64
- usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST,
65
- });
66
 
67
  const indices = Array.from({ length: CONFIG.maxGlyphs * 6 }, (_, i) => {
68
  const ndx = Math.floor(i / 6) * 4;
@@ -72,7 +46,7 @@
72
 
73
  const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, glyphCanvas);
74
  device.queue.writeBuffer(vertexBuffer, 0, vertexData);
75
-
76
  pipeline = device.createRenderPipeline({
77
  label: 'textured quad pipeline',
78
  layout: 'auto',
@@ -103,17 +77,9 @@
103
  },
104
  });
105
 
106
- texture = createTextureFromSource(device, glyphCanvas, { mips: true });
107
- sampler = device.createSampler({
108
- minFilter: 'linear',
109
- magFilter: 'linear'
110
- });
111
-
112
- uniformBuffer = device.createBuffer({
113
- label: 'uniforms for quad',
114
- size: CONFIG.uniformBufferSize,
115
- usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
116
- });
117
 
118
  const uniformValues = new Float32Array(CONFIG.uniformBufferSize / 4);
119
  const matrix = uniformValues.subarray(0, 16);
@@ -192,7 +158,7 @@
192
  return { vertexData, numGlyphs: offset / CONFIG.floatsPerVertex, width, height: y1 };
193
  }
194
 
195
- function createTextureFromSource(device, source, options = {}) {
196
  const texture = device.createTexture({
197
  format: 'rgba8unorm',
198
  size: [source.width, source.height],
@@ -200,7 +166,7 @@
200
  });
201
 
202
  device.queue.copyExternalImageToTexture(
203
- { source, flipY: options.flipY },
204
  { texture, premultipliedAlpha: true },
205
  { width: source.width, height: source.height }
206
  );
 
18
  const canvas = document.querySelector('canvas');
19
  const context = canvas.getContext('webgpu');
20
  const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
21
+ let device, pipeline, vertexBuffer, indexBuffer, uniformBuffer, texture, sampler, bindGroup;
 
 
 
 
 
 
 
22
 
23
  async function main() {
24
  const adapter = await navigator.gpu?.requestAdapter();
25
  device = await adapter?.requestDevice();
26
+ if (!device) return alert('WebGPU support required');
 
 
 
27
 
28
+ context.configure({ device, format: presentationFormat });
 
 
 
29
 
30
  const shaderCode = await fetchShaderCode('shaders.wgsl');
31
+ const module = device.createShaderModule({ label: 'textured quad shaders', code: shaderCode });
 
 
 
32
 
33
  const glyphCanvas = generateGlyphTextureAtlas(CANVAS, CTX, CONFIG);
34
+ document.body.appendChild(glyphCanvas).style.backgroundColor = '#222';
 
35
 
36
  const vertexSize = CONFIG.floatsPerVertex * 4;
37
  const vertexBufferSize = CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * vertexSize;
38
+ vertexBuffer = device.createBuffer({ label: 'vertices', size: vertexBufferSize, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST });
39
+ indexBuffer = device.createBuffer({ label: 'indices', size: CONFIG.maxGlyphs * CONFIG.vertsPerGlyph * 4, usage: GPUBufferUsage.INDEX | GPUBufferUsage.COPY_DST });
 
 
 
 
 
 
 
 
 
40
 
41
  const indices = Array.from({ length: CONFIG.maxGlyphs * 6 }, (_, i) => {
42
  const ndx = Math.floor(i / 6) * 4;
 
46
 
47
  const { vertexData, numGlyphs, width, height } = generateGlyphVerticesForText('Hello\nworld!\nText in\nWebGPU!', COLORS, glyphCanvas);
48
  device.queue.writeBuffer(vertexBuffer, 0, vertexData);
49
+
50
  pipeline = device.createRenderPipeline({
51
  label: 'textured quad pipeline',
52
  layout: 'auto',
 
77
  },
78
  });
79
 
80
+ texture = createTextureFromSource(device, glyphCanvas);
81
+ sampler = device.createSampler({ minFilter: 'linear', magFilter: 'linear' });
82
+ uniformBuffer = device.createBuffer({ label: 'uniforms for quad', size: CONFIG.uniformBufferSize, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
 
 
 
 
 
 
 
 
83
 
84
  const uniformValues = new Float32Array(CONFIG.uniformBufferSize / 4);
85
  const matrix = uniformValues.subarray(0, 16);
 
158
  return { vertexData, numGlyphs: offset / CONFIG.floatsPerVertex, width, height: y1 };
159
  }
160
 
161
+ function createTextureFromSource(device, source) {
162
  const texture = device.createTexture({
163
  format: 'rgba8unorm',
164
  size: [source.width, source.height],
 
166
  });
167
 
168
  device.queue.copyExternalImageToTexture(
169
+ { source, flipY: true },
170
  { texture, premultipliedAlpha: true },
171
  { width: source.width, height: source.height }
172
  );