Skip to content

Commit ffacd45

Browse files
committed
feat: Allow shader generators to report different language keys
1 parent a94d1fb commit ffacd45

5 files changed

Lines changed: 31 additions & 3 deletions

File tree

packages/typegpu-gl/src/glslGenerator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export class GlslGenerator extends WgslGenerator {
8484
#functionType: TgpuShaderStage | 'normal' | undefined;
8585
#entryFnState: EntryFnState | undefined;
8686

87+
static {
88+
GlslGenerator.prototype.languageKey = 'glsl';
89+
}
90+
8791
override typeAnnotation(data: d.BaseData): string {
8892
// For WGSL identity types (scalars, vectors, common matrices), map to GLSL directly.
8993
if (!d.isLooseData(data)) {

packages/typegpu-gl/tests/glslGenerator.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { tgpu, d } from 'typegpu';
2+
import { tgpu, d, std } from 'typegpu';
33
import { glOptions } from '@typegpu/gl';
44
import { translateWgslTypeToGlsl } from '../src/glslGenerator.ts';
55

@@ -47,6 +47,21 @@ describe('translateWgslTypeToGlsl', () => {
4747
});
4848
});
4949

50+
describe('GlslGenerator', () => {
51+
it('reports "glsl" as the language', () => {
52+
function foo() {
53+
'use gpu';
54+
return std.getTargetShaderLanguage() === 'glsl';
55+
}
56+
57+
expect(tgpu.resolve([foo], glOptions())).toMatchInlineSnapshot(`
58+
"bool foo() {
59+
return true;
60+
}"
61+
`);
62+
});
63+
});
64+
5065
describe('GlslGenerator - variable declarations', () => {
5166
it('generates GLSL-style variable declarations for JS function', () => {
5267
const main = () => {

packages/typegpu/src/std/environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ export const isBeingTranspiled = impl;
5050
* @note
5151
* Inside `lazy`, it always returns `wgsl`.
5252
* Inside `simulate`, it always returns `undefined`.
53-
* Inside `comptime`, it returns `wgsl` if called during the resolution process; otherwise, `undefined`.
53+
* Inside `comptime`, it returns the shader language that is ultimately being generated (usually `wgsl`) if called during the resolution process; otherwise, `undefined`.
5454
*/
5555
export const getTargetShaderLanguage = comptime((() => {
5656
const ctx = getResolutionCtx();
5757
if (!ctx) {
5858
return undefined;
5959
}
60-
return getExecMode().type !== 'simulate' ? 'wgsl' : undefined;
60+
return getExecMode().type !== 'simulate' ? ctx.gen.languageKey : undefined;
6161
}) as () => string | undefined);

packages/typegpu/src/tgsl/shaderGenerator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface VariableDefinitionOptions {
3737
* shader code in the target language (WGSL, GLSL, etc.).
3838
*/
3939
export interface ShaderGenerator {
40+
readonly languageKey: string;
41+
4042
initGenerator(ctx: GenerationCtx): void;
4143

4244
declareGlobalConst(options: ConstantDefinitionOptions): ResolvedSnippet;

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ export class WgslGenerator implements ShaderGenerator {
213213
// used to detect `continue` and `break` nodes in loop body
214214
#unrolling = false;
215215

216+
// prototype properties
217+
declare languageKey: string;
218+
219+
static {
220+
WgslGenerator.prototype.languageKey = 'wgsl';
221+
}
222+
216223
public initGenerator(ctx: GenerationCtx) {
217224
this.#ctx = ctx;
218225
}

0 commit comments

Comments
 (0)