Skip to content

Commit

Permalink
Shaderlab detect shader with duplicate name (#1897)
Browse files Browse the repository at this point in the history
* fix: detect shaders with duplicate name
  • Loading branch information
Sway007 authored Dec 8, 2023
1 parent 18a384f commit 34aee2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/shader/Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export class Shader {
}

const shaderInfo = Shader._shaderLab.parseShader(nameOrShaderSource);
if (shaderMap[shaderInfo.name]) {
throw `Shader named "${shaderInfo.name}" already exists.`;
}
const subShaderList = shaderInfo.subShaders.map((subShaderInfo) => {
const passList = subShaderInfo.passes.map((passInfo) => {
if (typeof passInfo === "string") {
Expand Down
8 changes: 8 additions & 0 deletions tests/src/shader-lab/ShaderLab.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IShaderPassInfo, ISubShaderInfo } from "@galacean/engine-design";
import { Color } from "@galacean/engine-math";
import { ShaderLab } from "@galacean/engine-shader-lab";
import { glslValidate } from "./ShaderValidate";
import { Shader } from "@galacean/engine-core";

import chai, { expect } from "chai";
import spies from "chai-spies";
Expand Down Expand Up @@ -215,4 +216,11 @@ describe("ShaderLab", () => {
const demoShader = fs.readFileSync(path.join(__dirname, "shaders/glass.shader")).toString();
glslValidate(demoShader, shaderLab);
});

it("shader with duplicate name", () => {
const demoShader = fs.readFileSync(path.join(__dirname, "shaders/glass.shader")).toString();
(Shader as any)._shaderLab = shaderLab;
expect(Shader.create(demoShader) instanceof Shader).to.be.true;
expect(Shader.create.bind(null, demoShader)).to.throw('Shader named "Gem" already exists.');
});
});

0 comments on commit 34aee2f

Please sign in to comment.