Skip to content

Commit

Permalink
feat(shader): add ComputeShader auto binding (Orillusion#359)
Browse files Browse the repository at this point in the history
add ComputeShader auto binding
  • Loading branch information
Codeboy-cn authored Jan 24, 2024
1 parent bba64a1 commit b0319d9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
43 changes: 42 additions & 1 deletion src/gfx/graphics/webGpu/shader/util/Preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ export class Preprocessor {
public static parse(code: string, defineValue: { [name: string]: any }): string {
code = this.filterComment(code);
code = this.parsePreprocess(new PreprocessorContext(), code, defineValue);
code = this.parseAutoBindingForGroupX(code, 1);
code = this.parseAutoBindingForAllGroup(code);
return code;
}

public static parseComputeShader(code: string, defineValue: { [name: string]: any }): string {
code = this.filterComment(code);
code = this.parsePreprocess(new PreprocessorContext(), code, defineValue);
code = this.parseAutoBindingForAllGroup(code);
return code;
}

Expand All @@ -31,6 +32,46 @@ export class Preprocessor {
return header + this.parsePreprocessCommand(context, codeBlock, defineValue) + tail;
}

protected static parseAutoBindingForAllGroup(code: string): string {
let offset = 0;
let result = '';
let group = new Map<number, number>();
while (offset < code.length) {
let nLeftIndex = code.indexOf('@group(', offset);
if (nLeftIndex == -1) {
result += code.substring(offset);
break;
}
let nRightIndex = code.indexOf(')', nLeftIndex);
let groupID = Number.parseInt(code.substring(nLeftIndex + 7, nRightIndex));
nLeftIndex = code.indexOf('@binding(', nRightIndex);
nRightIndex = code.indexOf(')', nLeftIndex);

let bindingID = code.substring(nLeftIndex + 9, nRightIndex);

result += code.substring(offset, nLeftIndex);
if (bindingID.includes(`auto`)) {
if (group.has(groupID)) {
let lastBindingId = group.get(groupID) + 1;
result += `@binding(${lastBindingId})`;
group.set(groupID, lastBindingId);
} else {
result += '@binding(0)';
group.set(groupID, 0);
}
} else {
let nBindingID = Number.parseInt(bindingID);
if (!group.has(groupID) || group.get(groupID) < nBindingID) {
group.set(groupID, nBindingID);
}
result += `@binding(${bindingID})`;
}
offset = nRightIndex + 1;
}

return result;
}

protected static parseAutoBindingForGroupX(code: string, nGroup: number): string {
let offset = 0;
let result = '';
Expand Down
5 changes: 3 additions & 2 deletions src/gfx/graphics/webGpu/shader/value/ShaderReflectionInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ export class ShaderReflection {
}

public static getShaderReflection2(code: string, shaderBase: ShaderPassBase) {
if (shaderBase.shaderVariant != undefined) {
// Remove this if because need to parse auto
// if (shaderBase.shaderVariant != undefined) {
let preShader = Preprocessor.parse(code, shaderBase.defineValue);
ShaderReflection.parser2(preShader, shaderBase);
}
// }
}

/**
Expand Down

0 comments on commit b0319d9

Please sign in to comment.