generateMipmaps method

dynamic generateMipmaps(
  1. dynamic textureGPU,
  2. dynamic textureGPUDescriptor
)

Implementation

generateMipmaps(GPUTexture textureGPU, textureGPUDescriptor) {
  var pipeline = this.getMipmapPipeline(textureGPUDescriptor.format);

  var commandEncoder = this.device.createCommandEncoder();
  var bindGroupLayout =
      pipeline.getBindGroupLayout(0); // @TODO: Consider making this static.

  var srcView = textureGPU.createView(
      GPUTextureViewDescriptor(baseMipLevel: 0, mipLevelCount: 1));

  for (var i = 1; i < textureGPUDescriptor.mipLevelCount; i++) {
    var dstView = textureGPU.createView(
        GPUTextureViewDescriptor(baseMipLevel: i, mipLevelCount: 1));

    var passEncoder = commandEncoder.beginRenderPass(GPURenderPassDescriptor(
        colorAttachments: GPURenderPassColorAttachment(
            view: dstView,
            clearColor: GPUColor(r: 0, g: 0, b: 0, a: 0),
            loadOp: GPULoadOp.Clear, // TODO Confirm
            storeOp: GPUStoreOp.Store // TODO Confirm
            ),
        depthStencilAttachment: GPURenderPassDepthStencilAttachment()));

    var bindGroup = this.device.createBindGroup(GPUBindGroupDescriptor(
        layout: bindGroupLayout,
        entries: [
          GPUBindGroupEntry(binding: 0, sampler: this.sampler),
          GPUBindGroupEntry(binding: 1, textureView: srcView)
        ],
        entryCount: 2));

    passEncoder.setPipeline(pipeline);
    passEncoder.setBindGroup(0, bindGroup);
    passEncoder.draw(4, 1, 0, 0);
    passEncoder.end();

    srcView = dstView;
  }

  this.device.queue.submit(commandEncoder.finish());
}