playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
242 lines (241 loc) • 8.66 kB
JavaScript
import { StringIds } from "../../../core/string-ids.js";
import { getMultisampledTextureCache } from "../multi-sampled-texture-cache.js";
const stringIds = new StringIds();
class ColorAttachment {
format;
multisampledBuffer;
destroy(device) {
device.deferDestroy(this.multisampledBuffer);
this.multisampledBuffer = null;
}
}
class DepthAttachment {
format;
hasStencil;
depthTexture = null;
depthTextureInternal = false;
multisampledDepthBuffer = null;
multisampledDepthBufferKey;
constructor(gpuFormat) {
this.format = gpuFormat;
this.hasStencil = gpuFormat === "depth24plus-stencil8";
}
destroy(device) {
if (this.depthTextureInternal) {
device.deferDestroy(this.depthTexture);
this.depthTexture = null;
}
if (this.multisampledDepthBuffer) {
this.multisampledDepthBuffer = null;
getMultisampledTextureCache(device).release(this.multisampledDepthBufferKey);
}
}
}
class WebgpuRenderTarget {
initialized = false;
key;
colorAttachments = [];
depthAttachment = null;
assignedColorTexture = null;
renderPassDescriptor = {};
isBackbuffer = false;
constructor(renderTarget) {
this.renderTarget = renderTarget;
}
destroy(device) {
this.initialized = false;
this.assignedColorTexture = null;
this.colorAttachments.forEach((colorAttachment) => {
colorAttachment.destroy(device);
});
this.colorAttachments.length = 0;
this.depthAttachment?.destroy(device);
this.depthAttachment = null;
}
updateKey() {
const rt = this.renderTarget;
let key = `${rt.samples}:${this.depthAttachment ? this.depthAttachment.format : "nodepth"}`;
this.colorAttachments.forEach((colorAttachment) => {
key += `:${colorAttachment.format}`;
});
this.key = stringIds.get(key);
}
assignColorTexture(gpuTexture, viewFormat) {
this.assignedColorTexture = gpuTexture;
const wgpuDevice = this.renderTarget.device;
const xrViewDesc = wgpuDevice?.xrColorTextureViewDescriptor;
const xrSlice = xrViewDesc && gpuTexture === wgpuDevice.xrColorTexture;
const view = gpuTexture.createView(
xrSlice ? xrViewDesc : { format: viewFormat }
);
const colorAttachment = this.renderPassDescriptor.colorAttachments[0];
const samples = this.renderTarget.samples;
if (samples > 1) {
colorAttachment.resolveTarget = view;
} else {
colorAttachment.view = view;
}
this.setColorAttachment(0, void 0, viewFormat);
this.updateKey();
}
setColorAttachment(index, multisampledBuffer, format) {
if (!this.colorAttachments[index]) {
this.colorAttachments[index] = new ColorAttachment();
}
if (multisampledBuffer) {
this.colorAttachments[index].multisampledBuffer = multisampledBuffer;
}
if (format) {
this.colorAttachments[index].format = format;
}
}
init(device, renderTarget) {
const wgpu = device.wgpu;
this.initDepthStencil(device, wgpu, renderTarget);
if (renderTarget._colorBuffers) {
renderTarget._colorBuffers.forEach((colorBuffer, index) => {
this.setColorAttachment(index, void 0, colorBuffer.impl.format);
});
}
this.renderPassDescriptor.colorAttachments = [];
const count = this.isBackbuffer ? 1 : renderTarget._colorBuffers?.length ?? 0;
for (let i = 0; i < count; ++i) {
const colorAttachment = this.initColor(device, wgpu, renderTarget, i);
const isDefaultFramebuffer = i === 0 && this.colorAttachments[0]?.format;
if (colorAttachment.view || isDefaultFramebuffer) {
this.renderPassDescriptor.colorAttachments.push(colorAttachment);
}
}
this.updateKey();
this.initialized = true;
}
initDepthStencil(device, wgpu, renderTarget) {
const { samples, width, height, depth, depthBuffer } = renderTarget;
if (depth || depthBuffer) {
let renderingView;
if (!depthBuffer) {
this.depthAttachment = new DepthAttachment("depth24plus-stencil8");
const depthTextureDesc = {
size: [width, height, 1],
dimension: "2d",
sampleCount: samples,
format: this.depthAttachment.format,
usage: GPUTextureUsage.RENDER_ATTACHMENT
};
if (samples > 1) {
depthTextureDesc.usage |= GPUTextureUsage.TEXTURE_BINDING;
} else {
depthTextureDesc.usage |= GPUTextureUsage.COPY_SRC;
}
const depthTexture = wgpu.createTexture(depthTextureDesc);
this.depthAttachment.depthTexture = depthTexture;
this.depthAttachment.depthTextureInternal = true;
renderingView = depthTexture.createView();
} else {
this.depthAttachment = new DepthAttachment(depthBuffer.impl.format);
if (samples > 1) {
const depthFormat = "depth24plus-stencil8";
this.depthAttachment.format = depthFormat;
this.depthAttachment.hasStencil = depthFormat === "depth24plus-stencil8";
const key = `${depthBuffer.id}:${width}:${height}:${samples}:${depthFormat}`;
const msTextures = getMultisampledTextureCache(device);
let msDepthTexture = msTextures.get(key);
if (!msDepthTexture) {
const multisampledDepthDesc = {
size: [width, height, 1],
dimension: "2d",
sampleCount: samples,
format: depthFormat,
usage: GPUTextureUsage.RENDER_ATTACHMENT | // if msaa and resolve targets are different formats, we need to be able to bind the msaa target as a texture for manual shader resolve
(depthFormat !== depthBuffer.impl.format ? GPUTextureUsage.TEXTURE_BINDING : 0)
};
msDepthTexture = wgpu.createTexture(multisampledDepthDesc);
msTextures.set(key, msDepthTexture);
}
this.depthAttachment.multisampledDepthBuffer = msDepthTexture;
this.depthAttachment.multisampledDepthBufferKey = key;
renderingView = msDepthTexture.createView();
} else {
const depthTexture = depthBuffer.impl.gpuTexture;
this.depthAttachment.depthTexture = depthTexture;
renderingView = depthTexture.createView();
}
}
this.renderPassDescriptor.depthStencilAttachment = {
view: renderingView
};
}
}
initColor(device, wgpu, renderTarget, index) {
const colorAttachment = {};
const { samples, width, height, mipLevel } = renderTarget;
const colorBuffer = renderTarget.getColorBuffer(index);
let colorView = null;
if (colorBuffer) {
const mipLevelCount = 1;
if (colorBuffer.cubemap) {
colorView = colorBuffer.impl.createView({
dimension: "2d",
baseArrayLayer: renderTarget.face,
arrayLayerCount: 1,
mipLevelCount,
baseMipLevel: mipLevel
});
} else {
colorView = colorBuffer.impl.createView({
mipLevelCount,
baseMipLevel: mipLevel
});
}
}
if (samples > 1) {
const format = this.isBackbuffer ? this.colorAttachments[index]?.format ?? device.backBufferViewFormat : colorBuffer.impl.format;
const multisampledTextureDesc = {
size: [width, height, 1],
dimension: "2d",
sampleCount: samples,
format,
usage: GPUTextureUsage.RENDER_ATTACHMENT
};
const multisampledColorBuffer = wgpu.createTexture(multisampledTextureDesc);
this.setColorAttachment(index, multisampledColorBuffer, multisampledTextureDesc.format);
colorAttachment.view = multisampledColorBuffer.createView();
colorAttachment.resolveTarget = colorView;
} else {
colorAttachment.view = colorView;
}
return colorAttachment;
}
setupForRenderPass(renderPass, renderTarget) {
const count = this.renderPassDescriptor.colorAttachments?.length ?? 0;
for (let i = 0; i < count; ++i) {
const colorAttachment = this.renderPassDescriptor.colorAttachments[i];
const colorOps = renderPass.colorArrayOps[i];
const srgb = renderTarget.isColorBufferSrgb(i);
colorAttachment.clearValue = srgb ? colorOps.clearValueLinear : colorOps.clearValue;
colorAttachment.loadOp = colorOps.clear ? "clear" : "load";
colorAttachment.storeOp = colorOps.store ? "store" : "discard";
}
const depthAttachment = this.renderPassDescriptor.depthStencilAttachment;
if (depthAttachment) {
depthAttachment.depthClearValue = renderPass.depthStencilOps.clearDepthValue;
depthAttachment.depthLoadOp = renderPass.depthStencilOps.clearDepth ? "clear" : "load";
depthAttachment.depthStoreOp = renderPass.depthStencilOps.storeDepth ? "store" : "discard";
depthAttachment.depthReadOnly = false;
if (this.depthAttachment.hasStencil) {
depthAttachment.stencilClearValue = renderPass.depthStencilOps.clearStencilValue;
depthAttachment.stencilLoadOp = renderPass.depthStencilOps.clearStencil ? "clear" : "load";
depthAttachment.stencilStoreOp = renderPass.depthStencilOps.storeStencil ? "store" : "discard";
depthAttachment.stencilReadOnly = false;
}
}
}
loseContext() {
this.initialized = false;
}
resolve(device, target, color, depth) {
}
}
export {
WebgpuRenderTarget
};