@bigmistqke/view.gl 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/tag.test.ts CHANGED
@@ -914,84 +914,6 @@ describe('glsl composition', () => {
914
914
  })
915
915
  })
916
916
 
917
- describe('compile with symbols', () => {
918
- let gl: GL
919
- let mockProgram: WebGLProgram
920
-
921
- beforeEach(() => {
922
- gl = createMockGL()
923
- mockProgram = gl.createProgram()!
924
-
925
- // Mock shader creation and compilation
926
- gl.createShader = vi.fn(type => ({ type } as WebGLShader))
927
- gl.shaderSource = vi.fn()
928
- gl.compileShader = vi.fn()
929
- gl.getShaderParameter = vi.fn(() => true)
930
- gl.attachShader = vi.fn()
931
- gl.linkProgram = vi.fn()
932
- gl.getProgramParameter = vi.fn(() => true)
933
- gl.createProgram = vi.fn(() => mockProgram)
934
- })
935
-
936
- it('should compile shaders with symbol keys', () => {
937
- const u_time_symbol = Symbol('u_time')
938
- const a_position_symbol = Symbol('a_position')
939
-
940
- const vertex = glsl`
941
- ${attribute.vec2(a_position_symbol)}
942
- ${uniform.float(u_time_symbol)}
943
-
944
- void main() {
945
- gl_Position = vec4(${a_position_symbol}, 0.0, 1.0);
946
- }`
947
-
948
- const fragment = glsl`
949
- precision mediump float;
950
-
951
- ${uniform.float(u_time_symbol)}
952
-
953
- void main() {
954
- gl_FragColor = vec4(${u_time_symbol}, 0.0, 0.0, 1.0);
955
- }`
956
-
957
- const { program, schema } = compile(gl, vertex, fragment)
958
-
959
- expect(program).toBe(mockProgram)
960
- expect(schema.uniforms[u_time_symbol]).toEqual({ kind: 'float' })
961
- expect(schema.attributes[a_position_symbol]).toEqual({ kind: 'vec2' })
962
- })
963
-
964
- it('should merge symbols from vertex and fragment shaders', () => {
965
- const u_shared_symbol = Symbol('u_shared')
966
- const u_vertex_only = Symbol('u_vertex_only')
967
- const u_fragment_only = Symbol('u_fragment_only')
968
-
969
- const vertex = glsl`
970
- ${uniform.float(u_shared_symbol)}
971
- ${uniform.vec2(u_vertex_only)}
972
-
973
- void main() {
974
- gl_Position = vec4(${u_vertex_only}, ${u_shared_symbol}, 1.0);
975
- }`
976
-
977
- const fragment = glsl`
978
- precision mediump float;
979
-
980
- ${uniform.float(u_shared_symbol)}
981
- ${uniform.vec3(u_fragment_only)}
982
-
983
- void main() {
984
- gl_FragColor = vec4(${u_fragment_only}, ${u_shared_symbol});
985
- }`
986
-
987
- const { schema } = compile(gl, vertex, fragment)
988
-
989
- expect(schema.uniforms[u_shared_symbol]).toEqual({ kind: 'float' })
990
- expect(schema.uniforms[u_vertex_only]).toEqual({ kind: 'vec2' })
991
- expect(schema.uniforms[u_fragment_only]).toEqual({ kind: 'vec3' })
992
- })
993
- })
994
-
995
917
  describe('glsl array interpolation', () => {
996
918
  let gl: GL
997
919
 
@@ -1505,4 +1427,416 @@ describe('compile', () => {
1505
1427
  expect(gl.linkProgram).toHaveBeenCalled()
1506
1428
  expect(gl.getProgramParameter).toHaveBeenCalled()
1507
1429
  })
1430
+
1431
+ it('should compile shaders with symbol keys', () => {
1432
+ const u_time_symbol = Symbol('u_time')
1433
+ const a_position_symbol = Symbol('a_position')
1434
+
1435
+ const vertex = glsl`
1436
+ ${attribute.vec2(a_position_symbol)}
1437
+ ${uniform.float(u_time_symbol)}
1438
+
1439
+ void main() {
1440
+ gl_Position = vec4(${a_position_symbol}, 0.0, 1.0);
1441
+ }`
1442
+
1443
+ const fragment = glsl`
1444
+ precision mediump float;
1445
+
1446
+ ${uniform.float(u_time_symbol)}
1447
+
1448
+ void main() {
1449
+ gl_FragColor = vec4(${u_time_symbol}, 0.0, 0.0, 1.0);
1450
+ }`
1451
+
1452
+ const { program, schema } = compile(gl, vertex, fragment)
1453
+
1454
+ expect(program).toBe(mockProgram)
1455
+ expect(schema.uniforms[u_time_symbol]).toEqual({ kind: 'float' })
1456
+ expect(schema.attributes[a_position_symbol]).toEqual({ kind: 'vec2' })
1457
+ })
1458
+
1459
+ it('should merge symbols from vertex and fragment shaders', () => {
1460
+ const u_shared_symbol = Symbol('u_shared')
1461
+ const u_vertex_only = Symbol('u_vertex_only')
1462
+ const u_fragment_only = Symbol('u_fragment_only')
1463
+
1464
+ const vertex = glsl`
1465
+ ${uniform.float(u_shared_symbol)}
1466
+ ${uniform.vec2(u_vertex_only)}
1467
+
1468
+ void main() {
1469
+ gl_Position = vec4(${u_vertex_only}, ${u_shared_symbol}, 1.0);
1470
+ }`
1471
+
1472
+ const fragment = glsl`
1473
+ precision mediump float;
1474
+
1475
+ ${uniform.float(u_shared_symbol)}
1476
+ ${uniform.vec3(u_fragment_only)}
1477
+
1478
+ void main() {
1479
+ gl_FragColor = vec4(${u_fragment_only}, ${u_shared_symbol});
1480
+ }`
1481
+
1482
+ const { schema } = compile(gl, vertex, fragment)
1483
+
1484
+ expect(schema.uniforms[u_shared_symbol]).toEqual({ kind: 'float' })
1485
+ expect(schema.uniforms[u_vertex_only]).toEqual({ kind: 'vec2' })
1486
+ expect(schema.uniforms[u_fragment_only]).toEqual({ kind: 'vec3' })
1487
+ })
1488
+ })
1489
+
1490
+ describe('compile.toString', () => {
1491
+ it('should convert glsl tag to shader string', () => {
1492
+ const shader = glsl`
1493
+ ${uniform.float('u_time')}
1494
+ ${attribute.vec2('a_position')}
1495
+
1496
+ void main() {
1497
+ gl_Position = vec4(a_position, 0.0, 1.0);
1498
+ }`
1499
+
1500
+ const result = compile.toString(shader)
1501
+
1502
+ expect(result).toContain('uniform float u_time;')
1503
+ expect(result).toContain('attribute vec2 a_position;')
1504
+ expect(result).toContain('void main() {')
1505
+ expect(result).toContain('gl_Position = vec4(a_position, 0.0, 1.0);')
1506
+ })
1507
+
1508
+ it('should handle WebGL2 syntax', () => {
1509
+ const shader = glsl`#version 300 es
1510
+ ${uniform.float('u_time')}
1511
+ ${attribute.vec3('a_position')}
1512
+
1513
+ void main() {
1514
+ gl_Position = vec4(a_position, 1.0);
1515
+ }`
1516
+
1517
+ const result = compile.toString(shader)
1518
+
1519
+ expect(result).toContain('#version 300 es')
1520
+ expect(result).toContain('uniform float u_time;')
1521
+ expect(result).toContain('in vec3 a_position;')
1522
+ })
1523
+
1524
+ it('should handle arrays', () => {
1525
+ const shader = glsl`
1526
+ ${uniform.vec3('u_palette', { size: 5 })}
1527
+
1528
+ void main() {
1529
+ gl_FragColor = vec4(u_palette[0], 1.0);
1530
+ }`
1531
+
1532
+ const result = compile.toString(shader)
1533
+
1534
+ expect(result).toContain('uniform vec3 u_palette[5];')
1535
+ })
1536
+
1537
+ it('should handle symbols', () => {
1538
+ const u_time_symbol = Symbol('u_time')
1539
+ const shader = glsl`
1540
+ ${uniform.float(u_time_symbol)}
1541
+
1542
+ void main() {
1543
+ gl_FragColor = vec4(${u_time_symbol});
1544
+ }`
1545
+
1546
+ const result = compile.toString(shader)
1547
+
1548
+ expect(result).toMatch(/uniform float VIEW_GL_ALIAS_\d+;/)
1549
+ expect(result).toMatch(/vec4\(VIEW_GL_ALIAS_\d+\)/)
1550
+ })
1551
+
1552
+ it('should handle interleaved attributes', () => {
1553
+ const shader = glsl`
1554
+ ${interleave('vertexData', [
1555
+ attribute.vec2('a_position'),
1556
+ attribute.vec4('a_color')
1557
+ ])}
1558
+
1559
+ void main() {
1560
+ gl_Position = vec4(a_position, 0.0, 1.0);
1561
+ }`
1562
+
1563
+ const result = compile.toString(shader)
1564
+
1565
+ expect(result).toContain('attribute vec2 a_position;')
1566
+ expect(result).toContain('attribute vec4 a_color;')
1567
+ })
1568
+
1569
+ it('should handle nested glsl tags', () => {
1570
+ const uniforms = glsl`${uniform.float('u_time')}`
1571
+ const shader = glsl`
1572
+ ${uniforms}
1573
+ ${attribute.vec2('a_position')}
1574
+
1575
+ void main() {
1576
+ gl_Position = vec4(a_position, u_time, 0.0, 1.0);
1577
+ }`
1578
+
1579
+ const result = compile.toString(shader)
1580
+
1581
+ expect(result).toContain('uniform float u_time;')
1582
+ expect(result).toContain('attribute vec2 a_position;')
1583
+ })
1584
+
1585
+ it('should handle arrays of resources', () => {
1586
+ const shader = glsl`
1587
+ ${[uniform.float('u_time'), uniform.vec2('u_resolution')]}
1588
+
1589
+ void main() {
1590
+ gl_Position = vec4(u_resolution * u_time, 0.0, 1.0);
1591
+ }`
1592
+
1593
+ const result = compile.toString(shader)
1594
+
1595
+ expect(result).toContain('uniform float u_time;')
1596
+ expect(result).toContain('uniform vec2 u_resolution;')
1597
+ })
1598
+
1599
+ it('should handle string interpolations', () => {
1600
+ const maxLights = 4
1601
+ const shader = glsl`
1602
+ #define MAX_LIGHTS ${maxLights}
1603
+ ${uniform.float('u_time')}
1604
+
1605
+ void main() {
1606
+ gl_Position = vec4(1.0);
1607
+ }`
1608
+
1609
+ const result = compile.toString(shader)
1610
+
1611
+ expect(result).toContain('#define MAX_LIGHTS 4')
1612
+ expect(result).toContain('uniform float u_time;')
1613
+ })
1614
+ })
1615
+
1616
+ describe('compile.toSchema', () => {
1617
+ it('should extract schema from glsl tag', () => {
1618
+ const shader = glsl`
1619
+ ${uniform.float('u_time')}
1620
+ ${uniform.vec3('u_color')}
1621
+ ${attribute.vec2('a_position')}
1622
+
1623
+ void main() {
1624
+ gl_Position = vec4(a_position, 0.0, 1.0);
1625
+ }`
1626
+
1627
+ const schema = compile.toSchema(shader)
1628
+
1629
+ expect(schema).toEqual({
1630
+ uniforms: {
1631
+ u_time: { kind: 'float' },
1632
+ u_color: { kind: 'vec3' }
1633
+ },
1634
+ attributes: {
1635
+ a_position: { kind: 'vec2' }
1636
+ },
1637
+ interleavedAttributes: {}
1638
+ })
1639
+ })
1640
+
1641
+ it('should handle arrays', () => {
1642
+ const shader = glsl`
1643
+ ${uniform.vec3('u_palette', { size: 5 })}
1644
+ ${uniform.mat4('u_matrices', { size: 10 })}
1645
+
1646
+ void main() {}`
1647
+
1648
+ const schema = compile.toSchema(shader)
1649
+
1650
+ expect(schema).toEqual({
1651
+ uniforms: {
1652
+ u_palette: { kind: 'vec3', size: 5 },
1653
+ u_matrices: { kind: 'mat4', size: 10 }
1654
+ },
1655
+ attributes: {},
1656
+ interleavedAttributes: {}
1657
+ })
1658
+ })
1659
+
1660
+ it('should handle symbols', () => {
1661
+ const u_time_symbol = Symbol('u_time')
1662
+ const a_pos_symbol = Symbol('a_position')
1663
+
1664
+ const shader = glsl`
1665
+ ${uniform.float(u_time_symbol)}
1666
+ ${attribute.vec3(a_pos_symbol)}
1667
+
1668
+ void main() {}`
1669
+
1670
+ const schema = compile.toSchema(shader)
1671
+
1672
+ expect(schema).toEqual({
1673
+ uniforms: {
1674
+ [u_time_symbol]: { kind: 'float' }
1675
+ },
1676
+ attributes: {
1677
+ [a_pos_symbol]: { kind: 'vec3' }
1678
+ },
1679
+ interleavedAttributes: {}
1680
+ })
1681
+ })
1682
+
1683
+ it('should handle interleaved attributes', () => {
1684
+ const shader = glsl`
1685
+ ${interleave('vertexData', [
1686
+ attribute.vec2('a_position'),
1687
+ attribute.vec4('a_color')
1688
+ ])}
1689
+ ${interleave('instanceData', [
1690
+ attribute.vec3('a_instancePos'),
1691
+ attribute.float('a_instanceScale')
1692
+ ], { instanced: true })}
1693
+
1694
+ void main() {}`
1695
+
1696
+ const schema = compile.toSchema(shader)
1697
+
1698
+ expect(schema).toEqual({
1699
+ uniforms: {},
1700
+ attributes: {},
1701
+ interleavedAttributes: {
1702
+ vertexData: {
1703
+ layout: [
1704
+ { key: 'a_position', kind: 'vec2' },
1705
+ { key: 'a_color', kind: 'vec4' }
1706
+ ],
1707
+ instanced: false
1708
+ },
1709
+ instanceData: {
1710
+ layout: [
1711
+ { key: 'a_instancePos', kind: 'vec3' },
1712
+ { key: 'a_instanceScale', kind: 'float' }
1713
+ ],
1714
+ instanced: true
1715
+ }
1716
+ }
1717
+ })
1718
+ })
1719
+
1720
+ it('should handle nested glsl tags', () => {
1721
+ const uniforms = glsl`
1722
+ ${uniform.float('u_time')}
1723
+ ${uniform.vec2('u_resolution')}
1724
+ `
1725
+
1726
+ const lighting = glsl`
1727
+ ${uniform.vec3('u_lightPos')}
1728
+ ${uniform.vec3('u_lightColor')}
1729
+ `
1730
+
1731
+ const shader = glsl`
1732
+ ${uniforms}
1733
+ ${lighting}
1734
+ ${attribute.vec3('a_position')}
1735
+
1736
+ void main() {}`
1737
+
1738
+ const schema = compile.toSchema(shader)
1739
+
1740
+ expect(schema).toEqual({
1741
+ uniforms: {
1742
+ u_time: { kind: 'float' },
1743
+ u_resolution: { kind: 'vec2' },
1744
+ u_lightPos: { kind: 'vec3' },
1745
+ u_lightColor: { kind: 'vec3' }
1746
+ },
1747
+ attributes: {
1748
+ a_position: { kind: 'vec3' }
1749
+ },
1750
+ interleavedAttributes: {}
1751
+ })
1752
+ })
1753
+
1754
+ it('should handle arrays of resources', () => {
1755
+ const shader = glsl`
1756
+ ${[
1757
+ uniform.float('u_time'),
1758
+ uniform.vec2('u_resolution'),
1759
+ attribute.vec3('a_position')
1760
+ ]}
1761
+ ${[
1762
+ interleave('data', [
1763
+ attribute.vec2('a_uv')
1764
+ ])
1765
+ ]}
1766
+
1767
+ void main() {}`
1768
+
1769
+ const schema = compile.toSchema(shader)
1770
+
1771
+ expect(schema).toEqual({
1772
+ uniforms: {
1773
+ u_time: { kind: 'float' },
1774
+ u_resolution: { kind: 'vec2' }
1775
+ },
1776
+ attributes: {
1777
+ a_position: { kind: 'vec3' }
1778
+ },
1779
+ interleavedAttributes: {
1780
+ data: {
1781
+ layout: [
1782
+ { key: 'a_uv', kind: 'vec2' }
1783
+ ],
1784
+ instanced: false
1785
+ }
1786
+ }
1787
+ })
1788
+ })
1789
+
1790
+ it('should ignore non-resource interpolations', () => {
1791
+ const shader = glsl`
1792
+ ${'// Comment'}
1793
+ ${42}
1794
+ ${uniform.float('u_time')}
1795
+ ${'precision highp float;'}
1796
+
1797
+ void main() {}`
1798
+
1799
+ const schema = compile.toSchema(shader)
1800
+
1801
+ expect(schema).toEqual({
1802
+ uniforms: {
1803
+ u_time: { kind: 'float' }
1804
+ },
1805
+ attributes: {},
1806
+ interleavedAttributes: {}
1807
+ })
1808
+ })
1809
+
1810
+ it('should handle empty glsl tag', () => {
1811
+ const shader = glsl`void main() {}`
1812
+
1813
+ const schema = compile.toSchema(shader)
1814
+
1815
+ expect(schema).toEqual({
1816
+ uniforms: {},
1817
+ attributes: {},
1818
+ interleavedAttributes: {}
1819
+ })
1820
+ })
1821
+
1822
+ it('should handle instanced attributes', () => {
1823
+ const shader = glsl`
1824
+ ${attribute.vec3('a_position')}
1825
+ ${attribute.vec4('a_color', { instanced: true })}
1826
+ ${attribute.mat4('a_instanceMatrix', { instanced: true })}
1827
+
1828
+ void main() {}`
1829
+
1830
+ const schema = compile.toSchema(shader)
1831
+
1832
+ expect(schema).toEqual({
1833
+ uniforms: {},
1834
+ attributes: {
1835
+ a_position: { kind: 'vec3' },
1836
+ a_color: { kind: 'vec4', instanced: true },
1837
+ a_instanceMatrix: { kind: 'mat4', instanced: true }
1838
+ },
1839
+ interleavedAttributes: {}
1840
+ })
1841
+ })
1508
1842
  })