11import { describe , expect , it } from 'vitest' ;
22import tgpu , { d } from 'typegpu' ;
3+ import { GLOptions } from '@typegpu/gl' ;
34import glslGenerator , { translateWgslTypeToGlsl } from '../src/glslGenerator.ts' ;
45
56describe ( 'translateWgslTypeToGlsl' , ( ) => {
@@ -48,18 +49,14 @@ describe('translateWgslTypeToGlsl', () => {
4849
4950describe ( 'GlslGenerator - variable declarations' , ( ) => {
5051 it ( 'generates GLSL-style variable declarations for JS function' , ( ) => {
51- // 'use gpu' function - uses the TGSL code generation path, which calls functionDefinition
52- const fragFn = tgpu . fragmentFn ( {
53- in : { uv : d . vec2f } ,
54- out : d . vec4f ,
55- } ) ( ( input ) => {
52+ const main = ( ) => {
5653 'use gpu' ;
5754 // A variable that uses a vector type
58- const color = d . vec4f ( input . uv [ 0 ] , input . uv [ 1 ] , 0 , 1 ) ;
55+ const color = d . vec4f ( 1 , 0 , 0 , 1 ) ;
5956 return color ;
60- } ) ;
57+ } ;
6158
62- const result = tgpu . resolveWithContext ( [ fragFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
59+ const result = tgpu . resolveWithContext ( [ main ] , GLOptions ( ) ) ;
6360 // Should contain the resolved function code
6461 expect ( result . code ) . toBeDefined ( ) ;
6562 expect ( result . code . length ) . toBeGreaterThan ( 0 ) ;
@@ -78,28 +75,38 @@ describe('GlslGenerator - variable declarations', () => {
7875 return d . vec4f ( x , 0 , 0 , 1 ) ;
7976 } ) ;
8077
81- const result = tgpu . resolveWithContext ( [ fragFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
78+ const result = tgpu . resolveWithContext ( [ fragFn ] , GLOptions ( ) ) ;
8279 expect ( result . code ) . toBeDefined ( ) ;
8380 // Variable declaration for f32 should be `float`
8481 expect ( result . code ) . toContain ( 'float ' ) ;
8582 } ) ;
8683} ) ;
8784
88- describe ( 'GlslGenerator - functionDefinition post-processing' , ( ) => {
89- it ( 'translates WGSL type constructor calls in JS function body to GLSL' , ( ) => {
90- const fragFn = tgpu . fragmentFn ( {
91- in : { uv : d . vec2f } ,
92- out : d . vec4f ,
93- } ) ( ( input ) => {
85+ describe ( 'GlslGenerator - function definitions' , ( ) => {
86+ it ( 'generates proper function signatures' , ( ) => {
87+ function add ( a : number , b : number ) {
9488 'use gpu' ;
95- return d . vec4f ( input . uv [ 0 ] , 0.0 , 0.0 , 1.0 ) ;
96- } ) ;
89+ return a + b ;
90+ }
9791
98- const result = tgpu . resolveWithContext ( [ fragFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
99- // vec4f(...) in the body should become vec4(...)
100- expect ( result . code ) . toContain ( 'vec4(' ) ;
101- // Should not contain the WGSL-style constructor in the body
102- expect ( result . code ) . not . toMatch ( / \b v e c 4 f \s * \( / ) ;
92+ function main ( ) {
93+ 'use gpu' ;
94+ return add ( 1.5 , 1.2 ) ;
95+ }
96+
97+ const result = tgpu . resolveWithContext ( [ main ] , GLOptions ( ) ) ;
98+
99+ expect ( result . code ) . toMatchInlineSnapshot ( `
100+ "#version 300 es
101+
102+ float add(float a, float b) {
103+ return (a + b);
104+ }
105+
106+ float main() {
107+ return add(1.5f, 1.2f);
108+ }"
109+ ` ) ;
103110 } ) ;
104111
105112 it ( 'translates vec3f to vec3 in function body' , ( ) => {
@@ -111,7 +118,7 @@ describe('GlslGenerator - functionDefinition post-processing', () => {
111118 return d . vec4f ( color [ 0 ] , color [ 1 ] , color [ 2 ] , 1.0 ) ;
112119 } ) ;
113120
114- const result = tgpu . resolveWithContext ( [ fragFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
121+ const result = tgpu . resolveWithContext ( [ fragFn ] , GLOptions ( ) ) ;
115122 expect ( result . code ) . toContain ( 'vec3(' ) ;
116123 expect ( result . code ) . not . toMatch ( / \b v e c 3 f \s * \( / ) ;
117124 expect ( result . code ) . toContain ( 'vec4(' ) ;
@@ -127,41 +134,76 @@ describe('GlslGenerator - entry point generation with JS functions', () => {
127134 return Out ( { pos : d . vec4f ( 0.0 , 0.0 , 0.0 , 1.0 ) } ) ;
128135 } ) ;
129136
130- const result = tgpu . resolveWithContext ( [ vertFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
137+ const result = tgpu . resolveWithContext ( [ vertFn ] , GLOptions ( ) ) ;
131138 expect ( result . code ) . toBeDefined ( ) ;
132139 expect ( result . code . length ) . toBeGreaterThan ( 0 ) ;
133140 // The body should have translated type names
134141 expect ( result . code ) . toContain ( 'vec4(' ) ;
135142 expect ( result . code ) . not . toMatch ( / \b v e c 4 f \s * \( / ) ;
136143
137144 expect ( result . code ) . toMatchInlineSnapshot ( `
138- "struct vertFn_Output {
145+ "#version 300 es
146+
147+ struct vertFn_Output {
139148 @builtin(position) pos: vec4,
140149 }
141150
142- @vertex fn vertFn() -> vertFn_Output {
151+ void main() {
143152 return vertFn_Output(vec4(0, 0, 0, 1));
144153 }"
145154 ` ) ;
146155 } ) ;
147156
157+ // it('resolves a vertex function returning a position', () => {
158+ // const vertFn = tgpu.vertexFn({
159+ // out: { position: d.builtin.position },
160+ // })(() => {
161+ // 'use gpu';
162+ // return {
163+ // position: d.vec4f(1.0, 0.0, 0.0, 1.0),
164+ // };
165+ // });
166+
167+ // const result = tgpu.resolveWithContext([vertFn], GLOptions());
168+ // expect(result.code).toBeDefined();
169+ // expect(result.code).toContain('vec4(');
170+ // expect(result.code).not.toMatch(/\bvec4f\s*\(/);
171+
172+ // expect(result.code).toMatchInlineSnapshot(`
173+ // "#version 300 es
174+
175+ // void main() {
176+ // gl_Position = vec4(1, 0, 0, 1);
177+ // return;
178+ // }"
179+ // `);
180+ // });
181+
148182 it ( 'resolves a fragment function returning a color using GLSL generator' , ( ) => {
149183 const fragFn = tgpu . fragmentFn ( {
150184 out : d . vec4f ,
151185 } ) ( ( ) => {
152186 'use gpu' ;
187+ // This variable should get renamed to not conflict with
188+ // the global.
189+ const gl_Position = 1 ;
153190 return d . vec4f ( 1.0 , 0.0 , 0.0 , 1.0 ) ;
154191 } ) ;
155192
156- const result = tgpu . resolveWithContext ( [ fragFn ] , { unstable_shaderGenerator : glslGenerator } ) ;
193+ const result = tgpu . resolveWithContext ( [ fragFn ] , GLOptions ( ) ) ;
157194 expect ( result . code ) . toBeDefined ( ) ;
158- // The body should have translated vec4f → vec4
159195 expect ( result . code ) . toContain ( 'vec4(' ) ;
160196 expect ( result . code ) . not . toMatch ( / \b v e c 4 f \s * \( / ) ;
161197
162198 expect ( result . code ) . toMatchInlineSnapshot ( `
163- "@fragment fn fragFn() -> @location(0) vec4 {
164- return vec4(1, 0, 0, 1);
199+ "#version 300 es
200+
201+ void main() {
202+ int gl_Position_1 = 1;
203+ {
204+ gl_Position = vec4(1, 0, 0, 1);
205+ return;
206+ }
165207 }"
166208 ` ) ;
167209 } ) ;
0 commit comments