GPUShaderModule
注意:此功能在 Web Workers 中可用。
GPUShaderModule 介面是 WebGPU API 的一部分,代表一個內部著色器模組物件,它是 WGSL 著色器程式碼的容器,可以提交給 GPU 以便透過管道執行。
GPUShaderModule 物件例項是使用 GPUDevice.createShaderModule() 建立的。
例項屬性
例項方法
getCompilationInfo()-
返回一個
Promise,該 Promise 會以一個GPUCompilationInfo物件解析,該物件包含在GPUShaderModule編譯期間生成的各種訊息。
示例
在我們 基本的渲染演示中,我們的著色器模組是使用以下程式碼建立的:
js
const shaders = `
struct VertexOut {
@builtin(position) position : vec4f,
@location(0) color : vec4f
}
@vertex
fn vertex_main(@location(0) position: vec4f,
@location(1) color: vec4f) -> VertexOut
{
var output : VertexOut;
output.position = position;
output.color = color;
return output;
}
@fragment
fn fragment_main(fragData: VertexOut) -> @location(0) vec4f
{
return fragData.color;
}
`;
async function init() {
if (!navigator.gpu) {
throw Error("WebGPU not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw Error("Couldn't request WebGPU adapter.");
}
const device = await adapter.requestDevice();
// …
// later on
const shaderModule = device.createShaderModule({
code: shaders,
});
// …
}
規範
| 規範 |
|---|
| WebGPU # gpushadermodule |
瀏覽器相容性
載入中…