UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

81 lines (78 loc) 2.67 kB
import { TextureView } from '../texture-view.js'; class WebgpuBindGroup { update(bindGroup) { this.destroy(); const device = bindGroup.device; const desc = this.createDescriptor(device, bindGroup); this.bindGroup = device.wgpu.createBindGroup(desc); } destroy() { this.bindGroup = null; } createDescriptor(device, bindGroup) { const entries = []; const format = bindGroup.format; const uniformBufferFormats = bindGroup.format.uniformBufferFormats; bindGroup.uniformBuffers.forEach((ub, i)=>{ const slot = uniformBufferFormats[i].slot; const buffer = ub.persistent ? ub.impl.buffer : ub.allocation.gpuBuffer.buffer; entries.push({ binding: slot, resource: { buffer: buffer, offset: 0, size: ub.format.byteSize } }); }); const textureFormats = bindGroup.format.textureFormats; bindGroup.textures.forEach((value, textureIndex)=>{ const isTextureView = value instanceof TextureView; const texture = isTextureView ? value.texture : value; const wgpuTexture = texture.impl; const textureFormat = format.textureFormats[textureIndex]; const slot = textureFormats[textureIndex].slot; const view = wgpuTexture.getView(device, isTextureView ? value : undefined); entries.push({ binding: slot, resource: view }); if (textureFormat.hasSampler) { const sampler = wgpuTexture.getSampler(device, textureFormat.sampleType); entries.push({ binding: slot + 1, resource: sampler }); } }); const storageTextureFormats = bindGroup.format.storageTextureFormats; bindGroup.storageTextures.forEach((value, textureIndex)=>{ const isTextureView = value instanceof TextureView; const texture = isTextureView ? value.texture : value; const wgpuTexture = texture.impl; const slot = storageTextureFormats[textureIndex].slot; const view = wgpuTexture.getView(device, isTextureView ? value : undefined); entries.push({ binding: slot, resource: view }); }); const storageBufferFormats = bindGroup.format.storageBufferFormats; bindGroup.storageBuffers.forEach((buffer, bufferIndex)=>{ const wgpuBuffer = buffer.impl.buffer; const slot = storageBufferFormats[bufferIndex].slot; entries.push({ binding: slot, resource: { buffer: wgpuBuffer } }); }); const desc = { layout: bindGroup.format.impl.bindGroupLayout, entries: entries }; return desc; } } export { WebgpuBindGroup };