Skip to content

Commit 52164bd

Browse files
committed
ton more stuff
1 parent 44b7536 commit 52164bd

File tree

1 file changed

+146
-49
lines changed

1 file changed

+146
-49
lines changed

src/library_webgpu.js

Lines changed: 146 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ var LibraryWebGPU = {
5151
initManagers: function() {
5252
function makeManager() {
5353
return {
54-
objects: [null],
54+
objects: [undefined],
5555
create: function(object) {
5656
var id = this.objects.length;
5757
{{{ gpu.makeCheck("typeof this.objects[id] === 'undefined'") }}}
5858
this.objects[id] = { refcount: 1, object };
5959
return id;
6060
},
6161
get: function(id) {
62+
if (id === 0) return undefined;
6263
{{{ gpu.makeCheckDefined('this.objects[id]') }}}
6364
return this.objects[id].object;
6465
},
@@ -70,7 +71,7 @@ var LibraryWebGPU = {
7071
release: function(id) {
7172
var o = this.objects[id];
7273
{{{ gpu.makeCheckDefined('o') }}}
73-
{{{ gpu.makeCheck('o.refCount > 0') }}}
74+
{{{ gpu.makeCheck('o.refcount > 0') }}}
7475
o.refcount--;
7576
if (o.refcount <= 0) {
7677
delete this.objects[id];
@@ -95,13 +96,51 @@ var LibraryWebGPU = {
9596
{{{ gpu.makeInitManager('ShaderModule') }}}
9697
},
9798

98-
getColor: function(ptr) {
99-
return [
100-
{{{ makeGetValue('ptr', 0, 'float') }}},
101-
{{{ makeGetValue('ptr', 4, 'float') }}},
102-
{{{ makeGetValue('ptr', 8, 'float') }}},
103-
{{{ makeGetValue('ptr', 12, 'float') }}},
104-
];
99+
makeColor: function(ptr) {
100+
return {
101+
r: {{{ makeGetValue('ptr', 0, 'float') }}},
102+
g: {{{ makeGetValue('ptr', 4, 'float') }}},
103+
b: {{{ makeGetValue('ptr', 8, 'float') }}},
104+
a: {{{ makeGetValue('ptr', 12, 'float') }}},
105+
};
106+
},
107+
108+
makeExtent3D: function(ptr) {
109+
return {
110+
width: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnExtent3D.width) }}},
111+
height: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnExtent3D.height) }}},
112+
depth: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnExtent3D.depth) }}},
113+
};
114+
},
115+
116+
makeOrigin3D: function(ptr) {
117+
return {
118+
x: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnOrigin3D.x) }}},
119+
y: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnOrigin3D.y) }}},
120+
z: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnOrigin3D.z) }}},
121+
};
122+
},
123+
124+
makeTextureCopyView: function(ptr) {
125+
{{{ gpu.makeCheckDescriptor('ptr') }}}
126+
return {
127+
texture: this.mgrTexture.get(
128+
{{{ makeGetValue('ptr', C_STRUCTS.DawnTextureCopyView.texture, '*') }}}),
129+
mipLevel: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnTextureCopyView.mipLevel, '*') }}},
130+
arrayLayer: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnTextureCopyView.arrayLayer, '*') }}},
131+
origin: WebGPU.makeOrigin3D(ptr + {{{ C_STRUCTS.DawnTextureCopyView.origin }}}),
132+
};
133+
},
134+
135+
makeBufferCopyView: function(ptr) {
136+
{{{ gpu.makeCheckDescriptor('ptr') }}}
137+
return {
138+
buffer: this.mgrBuffer.get(
139+
{{{ makeGetValue('ptr', C_STRUCTS.DawnBufferCopyView.buffer, '*') }}}),
140+
offset: {{{ gpu.makeGetU64('ptr', C_STRUCTS.DawnBufferCopyView.offset) }}},
141+
rowPitch: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnBufferCopyView.rowPitch) }}},
142+
imageHeight: {{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnBufferCopyView.imageHeight) }}},
143+
};
105144
},
106145

107146
AddressMode: [ 'repeat', 'mirror-repeat', 'clamp-to-edge' ],
@@ -130,22 +169,22 @@ var LibraryWebGPU = {
130169
TextureComponentType: [ 'float', 'sint', 'uint' ],
131170
TextureDimension: [ '1d', '2d', '3d' ],
132171
TextureFormat: [
133-
'undefined', 'r8-unorm', 'r8-snorm', 'r8-uint', 'r8-sint', 'r16-uint', 'r16-sint',
134-
'r16-float', 'rg8-unorm', 'rg8-snorm', 'rg8-uint', 'rg8-sint', 'r32-float', 'r32-uint',
135-
'r32-sint', 'rg16-uint', 'rg16-sint', 'rg16-float', 'rgba8-unorm', 'rgba8-unorm-srgb',
136-
'rgba8-snorm', 'rgba8-uint', 'rgba8-sint', 'bgra8-unorm', 'bgra8-unorm-srgb',
137-
'rgb10-a2-unorm', 'rg11-b10-float', 'rg32-float', 'rg32-uint', 'rg32-sint', 'rgba16-uint',
138-
'rgba16-sint', 'rgba16-float', 'rgba32-float', 'rgba32-uint', 'rgba32-sint',
139-
'depth32-float', 'depth24-plus', 'depth24-plus-stencil8', 'bc1-rgba-unorm',
140-
'bc1-rgba-unorm-srgb', 'bc2-rgba-unorm', 'bc2-rgba-unorm-srgb', 'bc3-rgba-unorm',
141-
'bc3-rgba-unorm-srgb', 'bc4-r-unorm', 'bc4-r-snorm', 'bc5-rg-unorm', 'bc5-rg-snorm',
142-
'bc6h-rgb-ufloat', 'bc6h-rgb-sfloat', 'bc7-rgba-unorm', 'bc7-rgba-unorm-srgb',
172+
'undefined', 'r8unorm', 'r8snorm', 'r8uint', 'r8sint', 'r16uint', 'r16sint',
173+
'r16float', 'rg8unorm', 'rg8snorm', 'rg8uint', 'rg8sint', 'r32float', 'r32uint',
174+
'r32sint', 'rg16uint', 'rg16sint', 'rg16float', 'rgba8unorm', 'rgba8unorm-srgb',
175+
'rgba8snorm', 'rgba8uint', 'rgba8sint', 'bgra8unorm', 'bgra8unorm-srgb',
176+
'rgb10a2unorm', 'rg11b10float', 'rg32float', 'rg32uint', 'rg32sint', 'rgba16uint',
177+
'rgba16sint', 'rgba16float', 'rgba32float', 'rgba32uint', 'rgba32sint',
178+
'depth32float', 'depth24plus', 'depth24plusstencil8', 'bc1rgbaunorm',
179+
'bc1rgbaunorm-srgb', 'bc2rgbaunorm', 'bc2rgbaunorm-srgb', 'bc3rgbaunorm',
180+
'bc3rgbaunorm-srgb', 'bc4runorm', 'bc4rsnorm', 'bc5rgunorm', 'bc5rgsnorm',
181+
'bc6hrgbufloat', 'bc6hrgbsfloat', 'bc7rgbaunorm', 'bc7rgbaunorm-srgb',
143182
],
144-
TextureViewDimension: [ 'undefined', '1d', '2d', '2d-array', 'cube', 'cube-array', '3d' ],
183+
TextureViewDimension: [ 'undefined', '1d', '2d', '2darray', 'cube', 'cube-array', '3d' ],
145184
VertexFormat: [
146-
'uchar2', 'uchar4', 'char2', 'char4', 'uchar2-norm', 'uchar4-norm', 'char2-norm',
147-
'char4-norm', 'ushort2', 'ushort4', 'short2', 'short4', 'ushort2-norm', 'ushort4-norm',
148-
'short2-norm', 'short4-norm', 'half2', 'half4', 'float', 'float2', 'float3', 'float4',
185+
'uchar2', 'uchar4', 'char2', 'char4', 'uchar2norm', 'uchar4norm', 'char2norm',
186+
'char4norm', 'ushort2', 'ushort4', 'short2', 'short4', 'ushort2norm', 'ushort4norm',
187+
'short2norm', 'short4norm', 'half2', 'half4', 'float', 'float2', 'float3', 'float4',
149188
'uint', 'uint2', 'uint3', 'uint4', 'int', 'int2', 'int3', 'int4',
150189
],
151190
},
@@ -201,8 +240,23 @@ var LibraryWebGPU = {
201240
return WebGPU.mgrBuffer.create(device.createBuffer(desc));
202241
},
203242

204-
dawnDeviceCreateTexture: function() {
205-
console.warn('dawnDeviceCreateTexture: unimplemented');
243+
dawnDeviceCreateTexture: function(deviceId, descriptor) {
244+
{{{ gpu.makeCheckDescriptor('descriptor') }}}
245+
246+
var desc = {
247+
size: WebGPU.makeExtent3D(descriptor + {{{ C_STRUCTS.DawnTextureDescriptor.size }}}),
248+
arrayLayerCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.arrayLayerCount) }}},
249+
mipLevelCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.mipLevelCount) }}},
250+
sampleCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.sampleCount) }}},
251+
dimension: WebGPU.TextureDimension[
252+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.dimension) }}}],
253+
format: WebGPU.TextureFormat[
254+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.format) }}}],
255+
usage: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureDescriptor.usage) }}},
256+
};
257+
258+
var device = WebGPU.mgrDevice.get(deviceId);
259+
return WebGPU.mgrTexture.create(device.createTexture(desc));
206260
},
207261

208262
dawnDeviceCreatePipelineLayout: function(deviceId, descriptor) {
@@ -223,8 +277,18 @@ var LibraryWebGPU = {
223277
dawnDeviceCreateRenderPipeline: function(deviceId, descriptor) {
224278
{{{ gpu.makeCheckDescriptor('descriptor') }}}
225279

280+
function makeStage(ptr) {
281+
if (ptr === 0) return undefined;
282+
return {
283+
module: WebGPU.mgrShaderModule.get(
284+
{{{ gpu.makeGetU32('ptr', C_STRUCTS.DawnPipelineStageDescriptor.module) }}}),
285+
entryPoint: UTF8ToString(
286+
{{{ makeGetValue('ptr', C_STRUCTS.DawnPipelineStageDescriptor.entryPoint, '*') }}}),
287+
};
288+
}
289+
226290
function makeRasterizationState(rsPtr) {
227-
if (rsPtr === null) return null;
291+
if (rsPtr === 0) return undefined;
228292
{{{ gpu.makeCheckDescriptor('rsPtr') }}}
229293
return {
230294
frontFace: WebGPU.FrontFace[
@@ -235,6 +299,7 @@ var LibraryWebGPU = {
235299
}
236300

237301
function makeBlendDescriptor(bdPtr) {
302+
if (bdPtr === 0) return undefined;
238303
{{{ gpu.makeCheck('bdPtr !== 0') }}}
239304
return {
240305
operation: WebGPU.BlendOperation[
@@ -285,6 +350,8 @@ var LibraryWebGPU = {
285350
}
286351

287352
function makeDepthStencilState(dssPtr) {
353+
if (dssPtr === 0) return undefined;
354+
288355
{{{ gpu.makeCheck('dssPtr !== 0') }}}
289356
return {
290357
format: WebGPU.TextureFormat[
@@ -345,6 +412,7 @@ var LibraryWebGPU = {
345412
}
346413

347414
function makeVertexInput(viPtr) {
415+
if (viPtr === 0) return undefined;
348416
{{{ gpu.makeCheckDescriptor('viPtr') }}}
349417
return {
350418
indexFormat: WebGPU.IndexFormat[
@@ -358,8 +426,10 @@ var LibraryWebGPU = {
358426
var desc = {
359427
layout: WebGPU.mgrPipelineLayout.get(
360428
{{{ makeGetValue('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.layout, '*') }}}),
361-
vertexStage: null,
362-
fragmentStage: null,
429+
vertexStage: makeStage(
430+
descriptor + {{{ C_STRUCTS.DawnRenderPipelineDescriptor.vertexStage }}}),
431+
fragmentStage: makeStage(
432+
{{{ makeGetValue('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.fragmentStage, '*') }}}),
363433
primitiveTopology: WebGPU.PrimitiveTopology[
364434
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.primitiveTopology) }}}],
365435
rasterizationState: makeRasterizationState(
@@ -373,18 +443,19 @@ var LibraryWebGPU = {
373443
{{{ makeGetValue('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.vertexInput, '*') }}}),
374444
sampleCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.sampleCount) }}},
375445
sampleMask: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.sampleMask) }}},
376-
alphaToCoverageEnabled: {{{ gpu.makeGetBool('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.alphaToCoverageEnabled) }}},
446+
// TODO(kainino0x): Turn this on when it doesn't throw an error in Chrome.
447+
//alphaToCoverageEnabled: {{{ gpu.makeGetBool('descriptor', C_STRUCTS.DawnRenderPipelineDescriptor.alphaToCoverageEnabled) }}},
377448
};
378449

379450
var device = WebGPU.mgrDevice.get(deviceId);
380451
return WebGPU.mgrRenderPipeline.create(device.createRenderPipeline(desc));
381452
},
382453

383-
dawnDeviceCreateShaderModule: function(descriptor) {
454+
dawnDeviceCreateShaderModule: function(deviceId, descriptor) {
384455
{{{ gpu.makeCheckDescriptor('descriptor') }}}
385456
var count = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnShaderModuleDescriptor.codeSize) }}};
386457
var start = {{{ makeGetValue('descriptor', C_STRUCTS.DawnShaderModuleDescriptor.code, '*') }}};
387-
var desc = { code: HEAP32.subarray(start, start + count) };
458+
var desc = { code: HEAPU32.subarray(start >> 2, (start >> 2) + count) };
388459

389460
var device = WebGPU.mgrDevice.get(deviceId);
390461
return WebGPU.mgrShaderModule.create(device.createShaderModule(desc));
@@ -418,12 +489,12 @@ var LibraryWebGPU = {
418489

419490
// dawnCommandEncoder
420491

421-
dawnCommandEncoderFinish: function(commandEncoderId) {
422-
var commandEncoder = WebGPU.mgrCommandEncoder.get(commandEncoderId);
492+
dawnCommandEncoderFinish: function(encoderId) {
493+
var commandEncoder = WebGPU.mgrCommandEncoder.get(encoderId);
423494
return WebGPU.mgrCommandBuffer.create(commandEncoder.finish());
424495
},
425496

426-
dawnCommandEncoderBeginRenderPass: function(commandEncoderId, descriptor) {
497+
dawnCommandEncoderBeginRenderPass: function(encoderId, descriptor) {
427498
{{{ gpu.makeCheck('descriptor !== 0') }}}
428499

429500
function makeColorAttachment(caPtr) {
@@ -436,7 +507,7 @@ var LibraryWebGPU = {
436507
{{{ gpu.makeGetU32('caPtr', C_STRUCTS.DawnRenderPassColorAttachmentDescriptor.loadOp) }}}],
437508
storeOp: WebGPU.StoreOp[
438509
{{{ gpu.makeGetU32('caPtr', C_STRUCTS.DawnRenderPassColorAttachmentDescriptor.storeOp) }}}],
439-
clearColor: WebGPU.getColor(caPtr + {{{ C_STRUCTS.DawnRenderPassColorAttachmentDescriptor.clearColor }}}),
510+
loadValue: WebGPU.makeColor(caPtr + {{{ C_STRUCTS.DawnRenderPassColorAttachmentDescriptor.clearColor }}}),
440511
};
441512
}
442513

@@ -479,12 +550,12 @@ var LibraryWebGPU = {
479550
};
480551
}
481552

482-
var commandEncoder = WebGPU.mgrCommandEncoder.get(commandEncoderId);
483-
commandEncoder.beginRenderPass(makeRenderPassDescriptor(descriptor));
553+
var commandEncoder = WebGPU.mgrCommandEncoder.get(encoderId);
554+
return WebGPU.mgrRenderPassEncoder.create(commandEncoder.beginRenderPass(makeRenderPassDescriptor(descriptor)));
484555
},
485556

486-
dawnCommandEncoderCopyBufferToBuffer: function(commandEncoderId, srcId, srcOffset_l, srcOffset_h, dstId, dstOffset_l, dstOffset_h, size_l, size_h) {
487-
var commandEncoder = WebGPU.mgrCommandEncoder.get(commandEncoderId);
557+
dawnCommandEncoderCopyBufferToBuffer: function(encoderId, srcId, srcOffset_l, srcOffset_h, dstId, dstOffset_l, dstOffset_h, size_l, size_h) {
558+
var commandEncoder = WebGPU.mgrCommandEncoder.get(encoderId);
488559
var src = WebGPU.mgrBuffer.get(srcId);
489560
var dst = WebGPU.mgrBuffer.get(dstId);
490561
commandEncoder.copyBufferToBuffer(
@@ -493,8 +564,11 @@ var LibraryWebGPU = {
493564
{{{ makeU64ToNumber('size_l', 'size_h') }}});
494565
},
495566

496-
dawnCommandEncoderCopyTextureToBuffer: function() {
497-
console.warn('dawnCommandEncoderCopyTextureToBuffer: unimplemented');
567+
dawnCommandEncoderCopyTextureToBuffer: function(encoderId, srcPtr, dstPtr, copySizePtr) {
568+
var commandEncoder = WebGPU.mgrCommandEncoder.get(encoderId);
569+
var copySize = WebGPU.makeExtent3D(copySizePtr);
570+
commandEncoder.copyTextureToBuffer(
571+
WebGPU.makeTextureCopyView(srcPtr), WebGPU.makeBufferCopyView(dstPtr), copySize);
498572
},
499573

500574
// dawnBuffer
@@ -560,20 +634,43 @@ var LibraryWebGPU = {
560634

561635
// dawnTexture
562636

563-
dawnTextureCreateView: function() {
564-
console.warn('dawnTextureCreateView: unimplemented');
637+
dawnTextureCreateView: function(textureId, descriptor) {
638+
var desc;
639+
if (descriptor !== 0) {
640+
{{{ gpu.makeCheckDescriptor('descriptor') }}}
641+
642+
desc = {
643+
format: WebGPU.TextureFormat[
644+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.format) }}}],
645+
dimension: WebGPU.TextureViewDimension[
646+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.dimension) }}}],
647+
baseMipLevel: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.baseMipLevel) }}},
648+
mipLevelCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.mipLevelCount) }}},
649+
baseArrayLayer: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.baseArrayLayer) }}},
650+
arrayLayerCount: {{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.arrayLayerCount) }}},
651+
aspect: WebGPU.TextureAspect[
652+
{{{ gpu.makeGetU32('descriptor', C_STRUCTS.DawnTextureViewDescriptor.aspect) }}}],
653+
};
654+
}
655+
656+
var texture = WebGPU.mgrTexture.get(textureId);
657+
return WebGPU.mgrTextureView.create(texture.createView(desc));
565658
},
566659

567660
// dawnRenderPass
568661

569-
dawnRenderPassEncoderSetPipeline: function() {
570-
console.warn('dawnRenderPassEncoderSetPipeline: unimplemented');
662+
dawnRenderPassEncoderSetPipeline: function(passId, pipelineId) {
663+
var pass = WebGPU.mgrRenderPassEncoder.get(passId);
664+
var pipeline = WebGPU.mgrRenderPipeline.get(pipelineId);
665+
pass.setPipeline(pipeline);
571666
},
572-
dawnRenderPassEncoderDraw: function() {
573-
console.warn('dawnRenderPassEncoderDraw: unimplemented');
667+
dawnRenderPassEncoderDraw: function(passId, vertexCount, instanceCount, firstVertex, firstInstance) {
668+
var pass = WebGPU.mgrRenderPassEncoder.get(passId);
669+
pass.draw(vertexCount, instanceCount, firstVertex, firstInstance);
574670
},
575-
dawnRenderPassEncoderEndPass: function() {
576-
console.warn('dawnRenderPassEncoderEndPass: unimplemented');
671+
dawnRenderPassEncoderEndPass: function(passId) {
672+
var pass = WebGPU.mgrRenderPassEncoder.get(passId);
673+
pass.endPass();
577674
},
578675
};
579676

0 commit comments

Comments
 (0)