@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
111 lines (108 loc) • 4.21 kB
JavaScript
import { i as TU } from './index-By0tcgYN.esm.js';
const WGSL = `override f:bool=false;@group(0)@binding(0)var t:texture_2d<f32>;@group(0)@binding(1)var o:texture_storage_2d<rgba16float,write>;@compute @workgroup_size(8,8)fn main(@builtin(global_invocation_id)g:vec3u){let d=textureDimensions(t);if(any(g.xy>=d)){return;}let c=textureLoad(t,vec2u(g.x,select(g.y,d.y-1u-g.y,f)),0);textureStore(o,g.xy,vec4f(pow(c.rgb,vec3f(2.2))/max(c.a,1.0/255.0),1));}`;
let _device = null;
let _module = null;
let _noFlip = null;
let _flip = null;
function getPipeline(device, flipY) {
if (device !== _device) {
_device = device;
_module = device.createShaderModule({ code: WGSL });
_noFlip = null;
_flip = null;
}
const slot = flipY ? _flip : _noFlip;
if (slot) {
return slot;
}
const p = device.createComputePipeline({
layout: "auto",
compute: { module: _module, entryPoint: "main", constants: { f: flipY ? 1 : 0 } }
});
if (flipY) {
_flip = p;
} else {
_noFlip = p;
}
return p;
}
function makeBindGroup(device, pipeline, inView, outView) {
return device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: inView },
{ binding: 1, resource: outView }
]
});
}
function encodeDispatch(encoder, pipeline, bg, w, h) {
const pass = encoder.beginComputePass();
pass.setPipeline(pipeline);
pass.setBindGroup(0, bg);
pass.dispatchWorkgroups(Math.ceil(w / 8), Math.ceil(h / 8));
pass.end();
}
function decodeBrdfPng(engine, image) {
const device = engine._device;
const pipeline = getPipeline(device, false);
const w = image.width;
const h = image.height;
const inputTex = device.createTexture({
size: { width: w, height: h },
format: "rgba8unorm",
usage: TU.TEXTURE_BINDING | TU.COPY_DST | TU.RENDER_ATTACHMENT
});
device.queue.copyExternalImageToTexture({ source: image, flipY: false }, { texture: inputTex, premultipliedAlpha: false }, { width: w, height: h });
const texture = device.createTexture({
size: { width: w, height: h },
format: "rgba16float",
usage: TU.TEXTURE_BINDING | TU.STORAGE_BINDING
});
const bg = makeBindGroup(device, pipeline, inputTex.createView(), texture.createView());
const enc = device.createCommandEncoder();
encodeDispatch(enc, pipeline, bg, w, h);
device.queue.submit([enc.finish()]);
inputTex.destroy();
return texture;
}
function uploadCubemapRGBD(engine, images, width, mipCount) {
const device = engine._device;
const pipeline = getPipeline(device, true);
const texture = device.createTexture({
size: { width, height: width, depthOrArrayLayers: 6 },
format: "rgba16float",
mipLevelCount: mipCount,
usage: TU.TEXTURE_BINDING | TU.COPY_DST | TU.COPY_SRC | TU.RENDER_ATTACHMENT,
dimension: "2d"
});
for (let mip = 0; mip < mipCount; mip++) {
const mipSize = Math.max(1, width >> mip);
const inputTex = device.createTexture({
size: { width: mipSize, height: mipSize },
format: "rgba8unorm",
usage: TU.TEXTURE_BINDING | TU.COPY_DST | TU.RENDER_ATTACHMENT
});
const outputTex = device.createTexture({
size: { width: mipSize, height: mipSize },
format: "rgba16float",
usage: TU.STORAGE_BINDING | TU.COPY_SRC
});
const bindGroup = makeBindGroup(device, pipeline, inputTex.createView(), outputTex.createView());
for (let face = 0; face < 6; face++) {
const idx = mip * 6 + face;
if (idx >= images.length) {
break;
}
device.queue.copyExternalImageToTexture({ source: images[idx], flipY: false }, { texture: inputTex, premultipliedAlpha: false }, { width: mipSize, height: mipSize });
const encoder = device.createCommandEncoder();
encodeDispatch(encoder, pipeline, bindGroup, mipSize, mipSize);
encoder.copyTextureToTexture({ texture: outputTex }, { texture, origin: { x: 0, y: 0, z: face }, mipLevel: mip }, { width: mipSize, height: mipSize });
device.queue.submit([encoder.finish()]);
}
inputTex.destroy();
outputTex.destroy();
}
return texture;
}
export { decodeBrdfPng, uploadCubemapRGBD };
//# sourceMappingURL=rgbd-decode-CLksmdlu.esm.js.map