From e3485957c8a746f2c1f783a6542a0b8aaa4cf5a4 Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 18 Dec 2024 15:17:18 +0800 Subject: [PATCH 1/2] feat: disable glsl compress for non mini package --- rollup.config.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 4ef87b8cc7..5be0009918 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -31,11 +31,14 @@ pkgs.push({ ...shaderLabPkg, verboseMode: true }); const extensions = [".js", ".jsx", ".ts", ".tsx"]; const mainFields = NODE_ENV === "development" ? ["debug", "module", "main"] : undefined; +const glslifyPlugin = glslify({ + include: [/\.(glsl|gs)$/], + compress: false +}); + const commonPlugins = [ resolve({ extensions, preferBuiltins: true, mainFields }), - glslify({ - include: [/\.(glsl|gs)$/] - }), + glslifyPlugin, swc( defineRollupSwcOption({ include: /\.[mc]?[jt]sx?$/, @@ -90,6 +93,15 @@ function config({ location, pkgJson, verboseMode }) { } } else { if (compress) { + const glslifyPluginIdx = curPlugins.findIndex((item) => item === glslifyPlugin); + curPlugins.splice( + glslifyPluginIdx, + 1, + glslify({ + include: [/\.(glsl|gs)$/], + compress: true + }) + ); curPlugins.push(minify({ sourceMap: true })); file = path.join(location, "dist", "browser.min.js"); } else { From 15eb742f6fadfbfe41e364a90f70c6651b9c246a Mon Sep 17 00:00:00 2001 From: Sway007 Date: Wed, 25 Dec 2024 19:46:38 +0800 Subject: [PATCH 2/2] fix: generic function type deduce --- packages/shader-lab/src/parser/AST.ts | 4 +- .../src/parser/builtin/functions.ts | 59 +++++++++++-------- pnpm-lock.yaml | 22 +++++++ tests/package.json | 3 +- tests/src/shader-lab/ShaderLab.test.ts | 30 ++++++++++ tests/src/shader-lab/ShaderValidate.ts | 5 ++ .../shaders/builtin-function.shader | 37 ++++++++++++ tests/vitest.config.ts | 3 +- 8 files changed, 133 insertions(+), 30 deletions(-) create mode 100644 tests/src/shader-lab/shaders/builtin-function.shader diff --git a/packages/shader-lab/src/parser/AST.ts b/packages/shader-lab/src/parser/AST.ts index ec968e6966..3721e3c517 100644 --- a/packages/shader-lab/src/parser/AST.ts +++ b/packages/shader-lab/src/parser/AST.ts @@ -701,9 +701,9 @@ export namespace ASTNode { } } // #if _VERBOSE - const builtinFn = BuiltinFunction.getFn(fnIdent, ...(paramSig ?? [])); + const builtinFn = BuiltinFunction.getFn(fnIdent, paramSig); if (builtinFn) { - this.type = BuiltinFunction.getReturnType(builtinFn.fun, builtinFn.genType); + this.type = builtinFn.realReturnType; return; } // #endif diff --git a/packages/shader-lab/src/parser/builtin/functions.ts b/packages/shader-lab/src/parser/builtin/functions.ts index c65b1883eb..079a5bd61f 100644 --- a/packages/shader-lab/src/parser/builtin/functions.ts +++ b/packages/shader-lab/src/parser/builtin/functions.ts @@ -32,6 +32,11 @@ export class BuiltinFunction { ident: string; readonly args: BuiltinType[]; readonly scope: EShaderStage; + signatures: NonGenericGalaceanType[] = []; + + get realReturnType(): NonGenericGalaceanType { + return this.signatures[0]; + } private constructor(ident: string, returnType: BuiltinType, scope: EShaderStage, ...args: BuiltinType[]) { this.ident = ident; @@ -59,33 +64,37 @@ export class BuiltinFunction { BuiltinFunctionTable.set(ident, list); } - static getFn( - ident: string, - ...args: BuiltinType[] - ): { fun: BuiltinFunction; genType: Exclude } | undefined { + static getFn(ident: string, parameterTypes: NonGenericGalaceanType[]): BuiltinFunction | undefined { const list = BuiltinFunctionTable.get(ident); - let realType = TypeAny; - if (list?.length) { - const fun = list.find((item) => { - if (item.args.length !== args.length) return false; - let genType = 0; - for (let i = 0; i < args.length; i++) { - if (args[i] === TypeAny) continue; - realType = args[i]; - if (isGenericType(item.args[i])) { - if (genType === 0) { - genType = args[i]; - continue; - } else { - realType = genType; + if (list) { + for (let length = list.length, i = 0; i < length; i++) { + const fn = list[i]; + const fnArgs = fn.args; + const argLength = fnArgs.length; + if (argLength !== parameterTypes.length) continue; + // Try to match generic parameter type. + let returnType = TypeAny; + let found = true; + for (let i = 0; i < argLength; i++) { + const curFnArg = fnArgs[i]; + if (isGenericType(curFnArg)) { + if (returnType === TypeAny) returnType = parameterTypes[i]; + } else { + if (curFnArg !== parameterTypes[i] && parameterTypes[i] !== TypeAny) { + found = false; + break; } } - if (args[i] === TypeAny) continue; - if (args[i] !== realType) return false; } - return true; - }); - if (fun) return { fun, genType: realType }; + if (found) { + fn.signatures.length = 0; + fn.signatures.push(returnType); + for (let i = 0; i < argLength; i++) { + fn.signatures.push(parameterTypes[i]); + } + return fn; + } + } } } } @@ -249,8 +258,8 @@ BuiltinFunction._create("textureSize", EKeyword.IVEC2, EKeyword.SAMPLER_CUBE_SHA BuiltinFunction._create("textureSize", EKeyword.IVEC3, EGenType.GSampler2DArray, EKeyword.INT); BuiltinFunction._create("textureSize", EKeyword.IVEC3, EKeyword.SAMPLER2D_ARRAY_SHADOW, EKeyword.INT); -BuiltinFunction._create("texture2D", EKeyword.SAMPLER2D, EKeyword.VEC2); -BuiltinFunction._create("texture2D", EKeyword.SAMPLER2D, EKeyword.VEC2, EKeyword.FLOAT); +BuiltinFunction._create("texture2D", EKeyword.VEC4, EKeyword.SAMPLER2D, EKeyword.VEC2); +BuiltinFunction._create("texture2D", EKeyword.VEC4, EKeyword.SAMPLER2D, EKeyword.VEC2, EKeyword.FLOAT); BuiltinFunction._create("texture", EGenType.GVec4, EGenType.GSampler2D, EKeyword.VEC2, EKeyword.FLOAT); BuiltinFunction._create("texture", EGenType.GVec4, EGenType.GSampler2D, EKeyword.VEC2); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 736f18b0ee..2160231c96 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -304,6 +304,9 @@ importers: '@galacean/engine-shader-lab': specifier: workspace:* version: link:../packages/shader-lab + '@galacean/engine-shader-shaderlab': + specifier: workspace:* + version: link:../packages/shader-shaderlab devDependencies: '@vitest/browser': specifier: 2.1.3 @@ -930,36 +933,42 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.0': resolution: {integrity: sha512-6uHywSIzz8+vi2lAzFeltnYbdHsDm3iIB57d4g5oaB9vKwjb6N6dRIgZMujw4nm5r6v9/BQH0noq6DzHrqr2pA==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.0': resolution: {integrity: sha512-BfNjXwZKxBy4WibDb/LDCriWSKLz+jJRL3cM/DllnHH5QUyoiUNEp3GmL80ZqxeumoADfCCP19+qiYiC8gUBjA==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.0': resolution: {integrity: sha512-S1qARKOphxfiBEkwLUbHjCY9BWPdWnW9j7f7Hb2jPplu8UZ3nes7zpPOW9bkLbHRvWM0WDTsjdOTUgW0xLBN1Q==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.0': resolution: {integrity: sha512-d9AOkusyXARkFD66S6zlGXyzx5RvY+chTP9Jp0ypSTC9d4lzyRs9ovGf/80VCxjKddcUvnsGwCHWuF2EoPgWjw==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.0': resolution: {integrity: sha512-iqOC+GoTDoFyk/VYSFHwjHhYrk8bljW6zOhPuhi5t9ulqiYq1togGJB5e3PwYVFFfeVgc6pbz3JdQyDoBszVaA==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [musl] '@parcel/watcher-win32-arm64@2.5.0': resolution: {integrity: sha512-twtft1d+JRNkM5YbmexfcH/N4znDtjgysFaV9zvZmmJezQsKpkfLYJ+JFV3uygugK6AtIM2oADPkB2AdhBrNig==} @@ -1065,46 +1074,55 @@ packages: resolution: {integrity: sha512-6npqOKEPRZkLrMcvyC/32OzJ2srdPzCylJjiTJT2c0bwwSGm7nz2F9mNQ1WrAqCBZROcQn91Fno+khFhVijmFA==} cpu: [arm] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.27.2': resolution: {integrity: sha512-V9Xg6eXtgBtHq2jnuQwM/jr2mwe2EycnopO8cbOvpzFuySCGtKlPCI3Hj9xup/pJK5Q0388qfZZy2DqV2J8ftw==} cpu: [arm] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.27.2': resolution: {integrity: sha512-uCFX9gtZJoQl2xDTpRdseYuNqyKkuMDtH6zSrBTA28yTfKyjN9hQ2B04N5ynR8ILCoSDOrG/Eg+J2TtJ1e/CSA==} cpu: [arm64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.27.2': resolution: {integrity: sha512-/PU9P+7Rkz8JFYDHIi+xzHabOu9qEWR07L5nWLIUsvserrxegZExKCi2jhMZRd0ATdboKylu/K5yAXbp7fYFvA==} cpu: [arm64] os: [linux] + libc: [musl] '@rollup/rollup-linux-powerpc64le-gnu@4.27.2': resolution: {integrity: sha512-eCHmol/dT5odMYi/N0R0HC8V8QE40rEpkyje/ZAXJYNNoSfrObOvG/Mn+s1F/FJyB7co7UQZZf6FuWnN6a7f4g==} cpu: [ppc64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.27.2': resolution: {integrity: sha512-DEP3Njr9/ADDln3kNi76PXonLMSSMiCir0VHXxmGSHxCxDfQ70oWjHcJGfiBugzaqmYdTC7Y+8Int6qbnxPBIQ==} cpu: [riscv64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.27.2': resolution: {integrity: sha512-NHGo5i6IE/PtEPh5m0yw5OmPMpesFnzMIS/lzvN5vknnC1sXM5Z/id5VgcNPgpD+wHmIcuYYgW+Q53v+9s96lQ==} cpu: [s390x] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.27.2': resolution: {integrity: sha512-PaW2DY5Tan+IFvNJGHDmUrORadbe/Ceh8tQxi8cmdQVCCYsLoQo2cuaSj+AU+YRX8M4ivS2vJ9UGaxfuNN7gmg==} cpu: [x64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-musl@4.27.2': resolution: {integrity: sha512-dOlWEMg2gI91Qx5I/HYqOD6iqlJspxLcS4Zlg3vjk1srE67z5T2Uz91yg/qA8sY0XcwQrFzWWiZhMNERylLrpQ==} cpu: [x64] os: [linux] + libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.27.2': resolution: {integrity: sha512-euMIv/4x5Y2/ImlbGl88mwKNXDsvzbWUlT7DFky76z2keajCtcbAsN9LUdmk31hAoVmJJYSThgdA0EsPeTr1+w==} @@ -1148,24 +1166,28 @@ packages: engines: {node: '>=10'} cpu: [arm64] os: [linux] + libc: [glibc] '@swc/core-linux-arm64-musl@1.9.2': resolution: {integrity: sha512-8xzrOmsyCC1zrx2Wzx/h8dVsdewO1oMCwBTLc1gSJ/YllZYTb04pNm6NsVbzUX2tKddJVRgSJXV10j/NECLwpA==} engines: {node: '>=10'} cpu: [arm64] os: [linux] + libc: [musl] '@swc/core-linux-x64-gnu@1.9.2': resolution: {integrity: sha512-kZrNz/PjRQKcchWF6W292jk3K44EoVu1ad5w+zbS4jekIAxsM8WwQ1kd+yjUlN9jFcF8XBat5NKIs9WphJCVXg==} engines: {node: '>=10'} cpu: [x64] os: [linux] + libc: [glibc] '@swc/core-linux-x64-musl@1.9.2': resolution: {integrity: sha512-TTIpR4rjMkhX1lnFR+PSXpaL83TrQzp9znRdp2TzYrODlUd/R20zOwSo9vFLCyH6ZoD47bccY7QeGZDYT3nlRg==} engines: {node: '>=10'} cpu: [x64] os: [linux] + libc: [musl] '@swc/core-win32-arm64-msvc@1.9.2': resolution: {integrity: sha512-+Eg2d4icItKC0PMjZxH7cSYFLWk0aIp94LNmOw6tPq0e69ax6oh10upeq0D1fjWsKLmOJAWEvnXlayZcijEXDw==} diff --git a/tests/package.json b/tests/package.json index fbbc9f27f4..3bc5ea85ed 100644 --- a/tests/package.json +++ b/tests/package.json @@ -23,7 +23,8 @@ "@galacean/engine-rhi-webgl": "workspace:*", "@galacean/engine-physics-lite": "workspace:*", "@galacean/engine-shader-lab": "workspace:*", - "@galacean/engine-physics-physx": "workspace:*" + "@galacean/engine-physics-physx": "workspace:*", + "@galacean/engine-shader-shaderlab": "workspace:*" }, "devDependencies": { "@vitest/browser": "2.1.3" diff --git a/tests/src/shader-lab/ShaderLab.test.ts b/tests/src/shader-lab/ShaderLab.test.ts index 3544e0f2fe..e3e7fa53dd 100644 --- a/tests/src/shader-lab/ShaderLab.test.ts +++ b/tests/src/shader-lab/ShaderLab.test.ts @@ -9,6 +9,7 @@ import { Color } from "@galacean/engine-math"; import { ShaderLab as ShaderLabVerbose, GSError } from "@galacean/engine-shader-lab/verbose"; import { ShaderLab as ShaderLabRelease } from "@galacean/engine-shader-lab"; import { glslValidate, shaderParse } from "./ShaderValidate"; +import { registerIncludes } from "@galacean/engine-shader-shaderlab"; import { IShaderContent } from "@galacean/engine-design"; import { describe, beforeAll, expect, assert, it } from "vitest"; @@ -17,6 +18,28 @@ const { readFile } = server.commands; const demoShader = await readFile("./shaders/demo.shader"); +const commonMacros = [ + { name: "RENDERER_IS_RECEIVE_SHADOWS" }, + { name: "MATERIAL_IS_TRANSPARENT" }, + { name: "RENDERER_HAS_UV" }, + { name: "RENDERER_HAS_NORMAL" }, + { name: "RENDERER_HAS_TANGENT" }, + { name: "SCENE_FOG_MODE", value: "0" }, + { name: "SCENE_SHADOW_CASCADED_COUNT", value: "1" }, + { name: "CAMERA_ORTHOGRAPHIC" }, + { name: "MATERIAL_NEED_WORLD_POS" }, + { name: "MATERIAL_NEED_TILING_OFFSET" }, + { name: "SCENE_DIRECT_LIGHT_COUNT", value: "1" }, + { name: "MATERIAL_ENABLE_SS_REFRACTION" }, + { name: "MATERIAL_HAS_TRANSMISSION" }, + { name: "MATERIAL_HAS_THICKNESS" }, + { name: "MATERIAL_HAS_ABSORPTION" }, + { name: "MATERIAL_HAS_TRANSMISSION_TEXTURE" }, + { name: "REFRACTION_SPHERE" } +] + .map((item) => `#define ${item.name} ${item.value ?? ""}`) + .join("\n"); + function toString(v: Color): string { return `Color(${v.r}, ${v.g}, ${v.b}, ${v.a})`; } @@ -124,6 +147,13 @@ describe("ShaderLab", () => { expect(passList[0].isUsePass).to.be.true; expect(passList[0].name).eq("pbr/Default/Forward"); pass1 = passList[1]; + registerIncludes(); + }); + + it("builtin-function", async () => { + let shaderSource = await readFile("./shaders/builtin-function.shader"); + shaderSource = shaderSource.replace("__$$insert_maros$$__", commonMacros); + glslValidate(shaderSource, shaderLabVerbose, {}); }); it("create shaderLab", async () => { diff --git a/tests/src/shader-lab/ShaderValidate.ts b/tests/src/shader-lab/ShaderValidate.ts index 1a0b0fb996..bed23cfe5f 100644 --- a/tests/src/shader-lab/ShaderValidate.ts +++ b/tests/src/shader-lab/ShaderValidate.ts @@ -90,6 +90,11 @@ export function glslValidate(shaderSource, _shaderLab?: ShaderLab, includeMap = // @ts-ignore ShaderPass._shaderRootPath ); + if (shaderLab.errors) { + for (const error of shaderLab.errors) { + console.error(error.toString()); + } + } validateShaderPass(pass, compiledPass.vertex, compiledPass.fragment); }); }); diff --git a/tests/src/shader-lab/shaders/builtin-function.shader b/tests/src/shader-lab/shaders/builtin-function.shader new file mode 100644 index 0000000000..fc63ec3597 --- /dev/null +++ b/tests/src/shader-lab/shaders/builtin-function.shader @@ -0,0 +1,37 @@ +Shader "/Folder1/test.gs" { + SubShader "Default" { + UsePass "pbr/Default/ShadowCaster" + + Pass "Forward Pass" { + Tags { pipelineStage = "Forward"} + + DepthState { + WriteEnabled = depthWriteEnabled; + } + + BlendState { + Enabled = blendEnabled; + SourceColorBlendFactor = sourceColorBlendFactor; + DestinationColorBlendFactor = destinationColorBlendFactor; + SourceAlphaBlendFactor = sourceAlphaBlendFactor; + DestinationAlphaBlendFactor = destinationAlphaBlendFactor; + } + + RasterState{ + CullMode = rasterStateCullMode; + } + + RenderQueueType = renderQueueType; + + #define IS_METALLIC_WORKFLOW + #define MATERIAL_ENABLE_IRIDESCENCE + + __$$insert_maros$$__ + + VertexShader = PBRVertex; + FragmentShader = PBRFragment; + + #include "ForwardPassPBR.glsl" + } + } +} diff --git a/tests/vitest.config.ts b/tests/vitest.config.ts index 6a87b0e5b7..7014792762 100644 --- a/tests/vitest.config.ts +++ b/tests/vitest.config.ts @@ -19,8 +19,7 @@ export default defineProject({ launch: { args: ["--use-gl=egl", "--ignore-gpu-blocklist", "--use-gl=angle"] } - }, - headless: true + } } } });