-
-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dual-Stack WebGL Runtime with WebGL2 to WebGL1 Fallback (#5198)
* refactor: WebGL2 to WebGL1 fallback at runtime * update changelog * update changelog * replace isWebGL2 check * revert program.ts * implement shaders.transpileToWebGL1(), refactor shaders.prepare() * revert readme * add `shaders.test.ts` * more shader tests * update changelog * fix shader transpileToWebGL1 throw test * addressed code review feedback
- Loading branch information
Showing
5 changed files
with
136 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {transpileVertexShaderToWebGL1, transpileFragmentShaderToWebGL1} from '../../src/shaders/shaders'; | ||
import {describe, test, expect} from 'vitest'; | ||
import {globSync} from 'glob'; | ||
import fs from 'fs'; | ||
|
||
describe('Shaders', () => { | ||
test('webgl2 to webgl1 transpiled shaders should be identical', () => { | ||
const vertexSourceWebGL2 = ` | ||
in vec3 aPos; | ||
uniform mat4 u_matrix; | ||
void main() { | ||
gl_Position = u_matrix * vec4(aPos, 1.0); | ||
gl_PointSize = 20.0; | ||
}`; | ||
const fragmentSourceWebGL2 = ` | ||
out highp vec4 fragColor; | ||
void main() { | ||
fragColor = vec4(1.0, 0.0, 0.0, 1.0); | ||
}`; | ||
const vertexSourceWebGL1 = ` | ||
attribute vec3 aPos; | ||
uniform mat4 u_matrix; | ||
void main() { | ||
gl_Position = u_matrix * vec4(aPos, 1.0); | ||
gl_PointSize = 20.0; | ||
}`; | ||
const fragmentSourceWebGL1 = ` | ||
void main() { | ||
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); | ||
}`; | ||
const vertexSourceTranspiled = transpileVertexShaderToWebGL1(vertexSourceWebGL2); | ||
const fragmentSourceTranspiled = transpileFragmentShaderToWebGL1(fragmentSourceWebGL2); | ||
expect(vertexSourceTranspiled.trim()).equals(vertexSourceWebGL1.trim()); | ||
expect(fragmentSourceTranspiled.trim()).equals(fragmentSourceWebGL1.trim()); | ||
}); | ||
|
||
// reference: https://webgl2fundamentals.org/webgl/lessons/webgl1-to-webgl2.html | ||
test('built-in shaders should be written in WebGL2', () => { | ||
const shaderFiles = globSync('../../src/shaders/*.glsl'); | ||
for (const shaderFile of shaderFiles) { | ||
const shaderSource = fs.readFileSync(shaderFile, 'utf8'); | ||
expect(shaderSource.includes('attribute')).toBe(false); | ||
expect(shaderSource.includes('varying')).toBe(false); | ||
expect(shaderSource.includes('gl_FragColor')).toBe(false); | ||
expect(shaderSource.includes('texture2D')).toBe(false); | ||
} | ||
}); | ||
}); |