Skip to content

Commit

Permalink
wgpu shader fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hana-alice committed Jun 25, 2023
1 parent 58c03e7 commit 8362cca
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions cocos/gfx/webgpu/webgpu-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/

import { WEBGPU } from 'internal:constants';
import { gfx, webgpuAdapter, glslangWasmModule, promiseForWebGPUInstantiation, spirvOptModule } from '../../webgpu/instantiated';
import { gfx, webgpuAdapter, glslangWasmModule, promiseForWebGPUInstantiation, spirvOptModule, nagaModule } from '../../webgpu/instantiated';
import {
Texture, CommandBuffer, DescriptorSet, Device, InputAssembler, Buffer, Shader
} from './override';
Expand Down Expand Up @@ -243,7 +243,7 @@ WEBGPU && promiseForWebGPUInstantiation.then(() => {
return target;
}

let funcReg = /\s([\S]+)\s*\(([^)]+)\)[\s].?{/g;
let funcReg = /\s([\S]+)\s*\(([\w\s,]+)\)[\s|\\|n]*{/g;
let funcIter = funcReg.exec(code);
const funcSet = new Set<string>();
const paramTypeMap = new Map<string, string>();
Expand All @@ -254,23 +254,22 @@ WEBGPU && promiseForWebGPUInstantiation.then(() => {
let paramsRes = params.slice();
if (params.includes('sampler')) {
const paramIndexSet = new Set<number>();
let singleParamReg = new RegExp(`\\W?sampler(\\w+)\\s+\\b([^,)]+)\\b`);
// const paramReg = /[^,]+?sampler(\S+)\s+\b([^,)]+)\b/g;
let paramIter = singleParamReg.exec(paramsRes);
let index = 0;

while(paramIter) {
if(paramIter[0].includes('sampler')) {
const samplerType = paramIter[1];
const paramName = paramIter[2];
paramsRes = paramsRes.replace(singleParamReg,'texture$1 $2, sampler $2_sampler');
paramIndexSet.add(index);
const paramArr = params.split(',');

for (let i = 0; i < paramArr.length; ++i) {
const paramDecl = paramArr[i].split(' ');
// assert(paramDecl.length >= 2)
const typeDecl = paramDecl[paramDecl.length - 2];
if(typeDecl.includes('sampler') && typeDecl !== 'sampler') {
const samplerType = typeDecl.replace('sampler', '');
const paramName = paramDecl[paramDecl.length - 1];
paramsRes = paramsRes.replace(paramArr[i],` texture${samplerType} ${paramName}, sampler ${paramName}_sampler`);
paramIndexSet.add(i);
paramTypeMap.set(paramName, samplerType);
}
++index;

paramIter = singleParamReg.exec(paramsRes);
}
// let singleParamReg = new RegExp(`(\\W?)(\\w+)\\s+\\b([^,)]+)\\b`);

code = code.replace(params, paramsRes);

const funcName = funcIter[1];
Expand Down Expand Up @@ -379,13 +378,26 @@ WEBGPU && promiseForWebGPUInstantiation.then(() => {
const createShader = Device.prototype.createShader;
Device.prototype.createShader = function (shaderInfo: ShaderInfo) {
const spvDatas: any = [];
if(shaderInfo.name === 'legacy/standard|standard-vs|standard-fs|CC_RECEIVE_SHADOW1|CC_SUPPORT_CASCADED_SHADOW_MAP1|CC_USE_HDR1') {
console.log('222222222');
}
for (let i = 0; i < shaderInfo.stages.length; ++i) {
shaderInfo.stages[i].source = seperateCombinedSamplerTexture(shaderInfo.stages[i].source);
const stageStr = shaderInfo.stages[i].stage === ShaderStageFlagBit.VERTEX ? 'vertex'
: shaderInfo.stages[i].stage === ShaderStageFlagBit.FRAGMENT ? 'fragment' : 'compute';
const sourceCode = `#version 450\n${shaderInfo.stages[i].source}`;
const spv = glslangWasmModule.glslang.compileGLSL(sourceCode, stageStr, false, '1.3');
const spvOpted = spirvOptModule.spv.opt(spirvOptModule.spv.opt.SPV_ENV_WEBGPU_0/*unused */, spv);

// const naga = nagaModule.naga;
// const stringOffset = naga.string_offset();
// const len = nagaModule.naga.naga_convert_spirv_to_wgsl(nagaModule.naga_converter, spvOpted);
// const wgslBuffer = new Uint8Array(nagaModule.naga.memory, stringOffset, len);
// let str = '';
// for (let i=0; i<wgslBuffer.length; i++) {
// str += String.fromCharCode(wgslBuffer[i]);
// }

spvDatas.push(spvOpted);
}

Expand Down

0 comments on commit 8362cca

Please sign in to comment.