The writeTexture()
method of the GPUQueue
interface writes a provided data source into a given GPUTexture
.
This is a convenience function, which provides an alternative to setting texture data via buffer mapping and buffer-to-texture copies. It lets the user agent determine the most efficient way to copy the data over.
writeTexture(destination, data, dataLayout, size)
The following criteria must be met when calling writeTexture()
, otherwise a GPUValidationError
is generated and the GPUQueue
becomes invalid:
In Efficiently rendering glTF models, a function is defined for creating a solid color texture:
function createSolidColorTexture(r, g, b, a) {
const data = new Uint8Array([r * 255, g * 255, b * 255, a * 255]);
const texture = device.createTexture({
size: { width: 1, height: 1 },
format: "rgba8unorm",
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST,
});
device.queue.writeTexture({ texture }, data, {}, { width: 1, height: 1 });
return texture;
}
This can be used to define standard textures for use in material libraries:
const opaqueWhiteTexture = createSolidColorTexture(1, 1, 1, 1);
const transparentBlackTexture = createSolidColorTexture(0, 0, 0, 0);
const defaultNormalTexture = createSolidColorTexture(0.5, 0.5, 1, 1);