getNextRenderTarget method

RenderTarget getNextRenderTarget(
  1. Size size,
  2. bool enableMsaa
)

Implementation

gpu.RenderTarget getNextRenderTarget(Size size, bool enableMsaa) {
  if (size != _previousSize) {
    _cursor = 0;
    _renderTargets.clear();
    _previousSize = size;
  }
  if (_cursor == _renderTargets.length) {
    final gpu.Texture? colorTexture = gpu.gpuContext.createTexture(
      gpu.StorageMode.devicePrivate,
      size.width.toInt(),
      size.height.toInt(),
      enableRenderTargetUsage: true,
      enableShaderReadUsage: true,
      coordinateSystem: gpu.TextureCoordinateSystem.renderToTexture,
    );
    if (colorTexture == null) {
      throw Exception("Failed to create Surface color texture!");
    }
    final colorAttachment = gpu.ColorAttachment(texture: colorTexture);
    if (enableMsaa) {
      final gpu.Texture? msaaColorTexture = gpu.gpuContext.createTexture(
        gpu.StorageMode.deviceTransient,
        size.width.toInt(),
        size.height.toInt(),
        sampleCount: 4,
        enableRenderTargetUsage: true,
        coordinateSystem: gpu.TextureCoordinateSystem.renderToTexture,
      );
      if (msaaColorTexture == null) {
        throw Exception("Failed to create Surface color texture!");
      }
      colorAttachment.resolveTexture = colorAttachment.texture;
      colorAttachment.texture = msaaColorTexture;
      colorAttachment.storeAction = gpu.StoreAction.multisampleResolve;
    }
    final gpu.Texture? depthTexture = gpu.gpuContext.createTexture(
      gpu.StorageMode.deviceTransient,
      size.width.toInt(),
      size.height.toInt(),
      sampleCount: enableMsaa ? 4 : 1,
      format: gpu.gpuContext.defaultDepthStencilFormat,
      enableRenderTargetUsage: true,
      coordinateSystem: gpu.TextureCoordinateSystem.renderToTexture,
    );
    if (depthTexture == null) {
      throw Exception("Failed to create Surface depth texture!");
    }
    final renderTarget = gpu.RenderTarget.singleColor(
      colorAttachment,
      depthStencilAttachment: gpu.DepthStencilAttachment(
          texture: depthTexture, depthClearValue: 1.0),
    );
    _renderTargets.add(renderTarget);
  }
  gpu.RenderTarget result = _renderTargets[_cursor];
  _cursor = (_cursor + 1) % _maxFramesInFlight;
  return result;
}