Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(cocos): macros the code only reads are baked into a converted shader, and effects written as GLSL main() programs convert: 17 of the 22 effects that were not converted now are

yhh960605e30e99
diff --git a/pipeline/src/import/cocos/convert.ts b/pipeline/src/import/cocos/convert.ts
index 107b84396..8ad8b3245 100644
--- a/pipeline/src/import/cocos/convert.ts
+++ b/pipeline/src/import/cocos/convert.ts
@@ -342,7 +342,12 @@ function modelInstanceOverrides(node: CocosNode, model: { rootId: string; name:
// where a skinned part finds its skeleton is the model's own: the engine's import binds the part to its joints
const skeleton = inst.overrides.filter((o) => o.path[0] === '_skinningRoot').length;
const inner = inst.overrides.length - roots.length - animation.used - materials - skeleton;
- if (inner > 0) ctx.report.note('model-instance-overrides', `${ctx.where}: ${node.name} (${inner} on the model's own parts)`);
+ if (inner > 0) {
+ // what is left, by the property it sets, so the note says what was not carried
+ const carried = new Set(['_defaultClip', '_clips', 'playOnLoad', '_skinningRoot']);
+ const left = new Set(inst.overrides.filter((o) => !carried.has(o.path[0] ?? '') && !(o.target.length === 1 && shape(o)) && !(o.path[0] === '_materials' && model.parts.has(o.target[0] ?? ''))).map((o) => o.path.join('.')));
+ ctx.report.note('model-instance-overrides', `${ctx.where}: ${node.name} (${inner} on the model's own parts: ${[...left].slice(0, 4).join(', ')})`);
+ }
if (animation.clip) {
const timeline = ctx.clipOn?.(animation.clip, model.paths, `${ctx.where}: ${node.name}`);
if (timeline) {
diff --git a/pipeline/src/import/cocos/effectConvert.ts b/pipeline/src/import/cocos/effectConvert.ts
index 1af2a5782..6c5373169 100644
--- a/pipeline/src/import/cocos/effectConvert.ts
+++ b/pipeline/src/import/cocos/effectConvert.ts
@@ -378,8 +378,15 @@ function emulateVertex(program: string, entry: string): Emulated | { error: stri
if ([...attributes.keys()].some((a) => /^a_(?:joints|weights)$/.test(a))) return { error: 'its vertex program skins the mesh, which the converted material does not' };
if ([...attributes.keys()].some((a) => /^a_(?:matWorld|instanced|dir|age)/.test(a))) return { error: 'its vertex program reads per-instance data, which the engine\'s own instancing does not give a material' };
const entryRe = new RegExp(`\\bvec4\\s+${entry}\\s*\\(\\s*\\)`);
- if (!entryRe.test(text)) return { error: `the vertex entry ${entry}() is not there` };
- text = text.replace(entryRe, 'vec4 cocos_vert()');
+ // A vertex program written as GLSL's own `void main()` writes `gl_Position` instead of returning it: the position is kept in a global, and the
+ // entry returns it, which is what the emulation calls.
+ const mainRe = /\bvoid\s+main\s*\(\s*\)/;
+ if (!entryRe.test(text) && mainRe.test(text)) {
+ text = `vec4 cocos_glPosition;\n#define gl_Position cocos_glPosition\n${text.replace(mainRe, 'void cocos_vert_main()')}\nvec4 cocos_vert() { cocos_vert_main(); return cocos_glPosition; }\n`;
+ } else {
+ if (!entryRe.test(text)) return { error: `the vertex entry ${entry}() is not there` };
+ text = text.replace(entryRe, 'vec4 cocos_vert()');
+ }
// A statement that assigns the position before the matrices are applied, from time or a periodic function: a displacement.
const displaces = /(?:\bIn\.position|\bposition|\ba_position)\b[^;]*\b(?:cc_time|sin|cos)\b|\b(?:cc_time|sin|cos)\b[^;]*(?:\bIn\.position|\bposition|\ba_position)\b/.test(text);
// A statement that takes components of the position and does not put it through the world matrix reads it in the mesh's own space (a terrain tiles its
@@ -603,8 +610,17 @@ export function convertPass(
// The entry: `vec4 frag()` is the fragment's colour.
const entry = new RegExp(`\\bvec4\\s+${fragEntry}\\s*\\(\\s*\\)`);
- if (!entry.test(body)) return fail(`the fragment entry ${fragEntry}() is not there`);
- body = body.replace(entry, 'vec4 cocos_frag()');
+ // A fragment program written as GLSL's own `void main()` writes the colour to an `out vec4` instead of returning it: the output is a global, and the
+ // entry returns it.
+ const outDeclaration = /^[ \t]*(?:layout\s*\([^)]*\)\s*)?out\s+(?:(?:highp|mediump|lowp)\s+)?vec4\s+(\w+)\s*;[ \t]*$/m.exec(body);
+ const mainFragment = /\bvoid\s+main\s*\(\s*\)/;
+ if (!entry.test(body) && outDeclaration && mainFragment.test(body)) {
+ const written = outDeclaration[1]!;
+ body = `${body.replace(outDeclaration[0], `vec4 ${written};`).replace(mainFragment, 'void cocos_frag_main()')}\nvec4 cocos_frag() { cocos_frag_main(); return ${written}; }\n`;
+ } else {
+ if (!entry.test(body)) return fail(`the fragment entry ${fragEntry}() is not there`);
+ body = body.replace(entry, 'vec4 cocos_frag()');
+ }
// A function nothing reaches (a variant of the entry this pass does not name) may read what the pass never declares, which a compiler refuses.
body = pruneUnreachable(body, ['cocos_frag']);
if (vertex) vertexText = pruneUnreachable(vertexText, ['cocos_vert']);
@@ -628,7 +644,7 @@ export function convertPass(
// What the code branches on, and what it merely reads (a channel name a material gives).
const text = `${vertexText}
${body}`;
- const used = Object.keys(value).filter((k) => new RegExp(`\b${k}\b`).test(text));
+ const used = Object.keys(value).filter((k) => new RegExp(`\\b${k}\\b`).test(text));
for (const m of new Set([...named, ...compared, ...used])) {
const v = value[m];
// Zero is the undefined of Cocos's `#if`, so a name that is not compared is left undefined for it.
@@ -667,7 +683,7 @@ ${body}`;
// The displacement, given to the engine as the material's vertex hook: the same program, run on the vertex's own position and normal. The engine's
// vertex stage owns the attribute names, so the program's are its own here.
if (vertex?.displaces) {
- const vertexReadsTexture = params.some((p) => p.type === 'texture' && new RegExp(`\b${p.name}\b`).test(vertexText));
+ const vertexReadsTexture = params.some((p) => p.type === 'texture' && new RegExp(`\\b${p.name}\\b`).test(vertexText));
if (vertexReadsTexture) {
approximations.push('a displacement of the vertex position that reads a texture is not applied');
} else {
diff --git a/pipeline/src/import/cocos/importProject.ts b/pipeline/src/import/cocos/importProject.ts
index 10128dac9..c07da729e 100644
--- a/pipeline/src/import/cocos/importProject.ts
+++ b/pipeline/src/import/cocos/importProject.ts
@@ -34,7 +34,7 @@ import { OUTLINE_SHADER } from './outlineShader';
* importer that made less (or differently), and is brought up to date when it is opened. Raise it when the converted output changes in a
* way an existing project needs to pick up: a new generated file, a field a document or a manifest now carries.
*/
-export const COCOS_IMPORT_REVISION = 27;
+export const COCOS_IMPORT_REVISION = 28;
/** Whether the project at @p projectRoot was written by an importer of this revision. A project that was not converted has nothing to bring up. */
export function cocosImportIsCurrent(projectRoot: string): boolean {
diff --git a/pipeline/tests/cocos-effect-convert.test.ts b/pipeline/tests/cocos-effect-convert.test.ts
index 82050b4fc..1165f9b16 100644
--- a/pipeline/tests/cocos-effect-convert.test.ts
+++ b/pipeline/tests/cocos-effect-convert.test.ts
@@ -150,6 +150,54 @@ describe('converting a pass', () => {
expect(source).not.toContain('#define USE_ALPHA_TEST');
});
+ it('bakes a macro the code only reads, such as the channel a material names for its alpha test', () => {
+ const channel = SPRITE_ADD.replace('vec4 o = texture(mainTexture, uv0) * color;', 'vec4 o = texture(mainTexture, uv0) * color;\n float edge = o.TEST_CHANNEL;');
+ const effect = parseEffect(channel);
+ const baked = convertPass(effect, effect.techniques[0]!.passes[0]!, 'channel', { chunk }, { TEST_CHANNEL: 'r' });
+ expect(baked.unsupported).toBeNull();
+ expect(baked.shader!.source).toContain('#define TEST_CHANNEL r');
+ });
+
+ it('converts a pass written as GLSL main() programs, which write gl_Position and an out colour instead of returning them', () => {
+ const raw = `
+CCEffect %{
+ techniques:
+ - passes:
+ - vert: raw-vs
+ frag: raw-fs
+ properties:
+ tint: { value: [1, 0.5, 0.25, 1], editor: { type: color } }
+}%
+CCProgram raw-vs %{
+ precision highp float;
+ #include <builtin/uniforms/cc-global>
+ in vec3 a_position;
+ in vec2 a_texCoord;
+ out vec2 v_uv;
+ void main () {
+ v_uv = a_texCoord;
+ gl_Position = cc_matViewProj * vec4(a_position, 1.0);
+ }
+}%
+CCProgram raw-fs %{
+ precision highp float;
+ in vec2 v_uv;
+ uniform Constant { vec4 tint; };
+ layout(location = 0) out vec4 outColor;
+ void main () {
+ outColor = tint * vec4(v_uv, 0.0, 1.0);
+ }
+}%
+`;
+ const effect = parseEffect(raw);
+ const converted = convertPass(effect, effect.techniques[0]!.passes[0]!, 'raw', { chunk }, {});
+ expect(converted.unsupported).toBeNull();
+ const source = converted.shader!.source;
+ expect(source).toContain('vec4 cocos_frag()');
+ expect(source).toContain('return outColor;');
+ expect(source).toContain('#define gl_Position cocos_glPosition');
+ });
+
it('maps the pass\'s blend, depth and cull state', () => {
expect(pass.state).toEqual({ blendMode: 1, depthTest: false, depthWrite: false, cull: 0 });
});