Skip to content

Commit 3e0594f

Browse files
authored
feat: Support for WebGL 1 layout syntax (#31)
1 parent 511b394 commit 3e0594f

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

src/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export interface ForStatement extends Node {
274274

275275
export type ConstantQualifier = 'const'
276276
export type ParameterQualifier = 'in' | 'out' | 'inout'
277-
export type StorageQualifier = 'uniform' | 'in' | 'out'
277+
export type StorageQualifier = 'uniform' | 'in' | 'out' | 'attribute' | 'varying'
278278
export type InterfaceStorageQualifier = 'uniform' | 'buffer'
279279
export type MemoryQualifier = 'coherent' | 'volatile' | 'restrict' | 'readonly' | 'writeonly'
280280

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const INFIX_OPERATOR_PRECEDENCE_RIGHT: Record<string, Precedence> = {
120120
}
121121

122122
const QUALIFIER_REGEX =
123-
/^(const|buffer|uniform|in|out|inout|centroid|flat|smooth|invariant|lowp|mediump|highp|coherent|volatile|restrict|readonly|writeonly)$/
123+
/^(const|buffer|uniform|in|out|inout|centroid|flat|smooth|invariant|lowp|mediump|highp|coherent|volatile|restrict|readonly|writeonly|attribute|varying)$/
124124

125125
const SCOPE_DELTAS: Record<string, number> = {
126126
// Open

tests/generator.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,18 @@ describe('generator', () => {
260260
const output = generate(program, { target: 'GLSL' })
261261
expect(output).toBe(minify(compute))
262262
})
263+
264+
it('can generate attribute variable declarations', () => {
265+
const shader = 'attribute vec4 position;'
266+
const program = parse(shader)
267+
const output = generate(program, { target: 'GLSL' })
268+
expect(output).toBe('attribute vec4 position;')
269+
})
270+
271+
it('can generate varying variable declarations', () => {
272+
const shader = 'varying vec3 normal;'
273+
const program = parse(shader)
274+
const output = generate(program, { target: 'GLSL' })
275+
expect(output).toBe('varying vec3 normal;')
276+
})
263277
})

tests/parser.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,50 @@ describe('parser', () => {
318318
},
319319
])
320320

321+
expect(parse('attribute vec4 position;').body).toStrictEqual<[VariableDeclaration]>([
322+
{
323+
declarations: [
324+
{
325+
id: {
326+
name: 'position',
327+
type: 'Identifier',
328+
},
329+
init: null,
330+
layout: null,
331+
qualifiers: ['attribute'],
332+
type: 'VariableDeclarator',
333+
typeSpecifier: {
334+
name: 'vec4',
335+
type: 'Identifier',
336+
},
337+
},
338+
],
339+
type: 'VariableDeclaration',
340+
},
341+
])
342+
343+
expect(parse('varying vec3 normal;').body).toStrictEqual<[VariableDeclaration]>([
344+
{
345+
declarations: [
346+
{
347+
id: {
348+
name: 'normal',
349+
type: 'Identifier',
350+
},
351+
init: null,
352+
layout: null,
353+
qualifiers: ['varying'],
354+
type: 'VariableDeclarator',
355+
typeSpecifier: {
356+
name: 'vec3',
357+
type: 'Identifier',
358+
},
359+
},
360+
],
361+
type: 'VariableDeclaration',
362+
},
363+
])
364+
321365
expect(parse('const vec4 foo = vec4(0, 0, 0, 0);').body).toStrictEqual<[VariableDeclaration]>([
322366
{
323367
declarations: [

0 commit comments

Comments
 (0)