GPUShaderModule: getCompilationInfo() 方法
注意:此功能在 Web Workers 中可用。
GPUShaderModule 接口的 getCompilationInfo() 方法返回一个 Promise,该 Promise 会解析为一个 GPUCompilationInfo 对象,其中包含 GPUShaderModule 编译过程中生成的各种消息。
语法
js
getCompilationInfo()
参数
无。
返回值
一个 Promise,它解析为一个 GPUCompilationInfo 对象。
GPUCompilationInfo 包含一个 messages 属性,该属性是一个 GPUCompilationMessage 对象数组,每个对象都包含一条单独的编译消息的详细信息。
示例
在下面的示例中,我们在着色器代码的函数声明中故意遗漏了一个括号。
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;
}
`;
编译着色器模块时,我们使用 getCompilationInfo() 来获取有关由此产生的错误的某些信息。
js
async function init() {
// …
const shaderModule = device.createShaderModule({
code: shaders,
});
const shaderInfo = await shaderModule.getCompilationInfo();
const firstMessage = shaderInfo.messages[0];
console.log(firstMessage.lineNum); // 9
console.log(firstMessage.message); // "expected ')' for function declaration"
console.log(firstMessage.type); // "error"
// …
}
规范
| 规范 |
|---|
| WebGPU # dom-gpushadermodule-getcompilationinfo |
浏览器兼容性
加载中…