-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add WebGL2 support #6035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WebGL2 support #6035
Changes from all commits
38abe49
34a7f11
7a7cc1d
9d754c5
a8ea6dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,10 @@ const lightingShader = readFileSync( | |
join(__dirname, '/shaders/lighting.glsl'), | ||
'utf-8' | ||
); | ||
const webgl2CompatibilityShader = readFileSync( | ||
join(__dirname, '/shaders/webgl2Compatibility.glsl'), | ||
'utf-8' | ||
); | ||
|
||
const defaultShaders = { | ||
immediateVert: readFileSync( | ||
|
@@ -61,8 +65,10 @@ const defaultShaders = { | |
phongFrag: | ||
lightingShader + | ||
readFileSync(join(__dirname, '/shaders/phong.frag'), 'utf-8'), | ||
fontVert: readFileSync(join(__dirname, '/shaders/font.vert'), 'utf-8'), | ||
fontFrag: readFileSync(join(__dirname, '/shaders/font.frag'), 'utf-8'), | ||
fontVert: webgl2CompatibilityShader + | ||
readFileSync(join(__dirname, '/shaders/font.vert'), 'utf-8'), | ||
fontFrag: webgl2CompatibilityShader + | ||
readFileSync(join(__dirname, '/shaders/font.frag'), 'utf-8'), | ||
lineVert: | ||
lineDefs + readFileSync(join(__dirname, '/shaders/line.vert'), 'utf-8'), | ||
lineFrag: | ||
|
@@ -125,7 +131,11 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { | |
|
||
this.curBlendMode = constants.BLEND; | ||
this._cachedBlendMode = undefined; | ||
this.blendExt = this.GL.getExtension('EXT_blend_minmax'); | ||
if (this.webglVersion === constants.WEBGL2) { | ||
this.blendExt = this.GL; | ||
} else { | ||
this.blendExt = this.GL.getExtension('EXT_blend_minmax'); | ||
} | ||
this._isBlending = false; | ||
|
||
this._useSpecularMaterial = false; | ||
|
@@ -277,7 +287,8 @@ p5.RendererGL.prototype._setAttributeDefaults = function(pInst) { | |
antialias: applyAA, | ||
premultipliedAlpha: true, | ||
preserveDrawingBuffer: true, | ||
perPixelLighting: true | ||
perPixelLighting: true, | ||
version: 2 | ||
}; | ||
if (pInst._glAttributes === null) { | ||
pInst._glAttributes = defaults; | ||
|
@@ -288,9 +299,24 @@ p5.RendererGL.prototype._setAttributeDefaults = function(pInst) { | |
}; | ||
|
||
p5.RendererGL.prototype._initContext = function() { | ||
this.drawingContext = | ||
this.canvas.getContext('webgl', this._pInst._glAttributes) || | ||
this.canvas.getContext('experimental-webgl', this._pInst._glAttributes); | ||
if (this._pInst._glAttributes.version !== 1) { | ||
// Unless WebGL1 is explicitly asked for, try to create a WebGL2 context | ||
this.drawingContext = | ||
this.canvas.getContext('webgl2', this._pInst._glAttributes); | ||
} | ||
this.webglVersion = this.drawingContext ? constants.WEBGL2 : constants.WEBGL; | ||
if (this._isMainCanvas) { | ||
// If this is the main canvas, make sure the global `webglVersion` is set | ||
this._pInst._setProperty('webglVersion', this.webglVersion); | ||
} | ||
if (!this.drawingContext) { | ||
// If we were unable to create a WebGL2 context (either because it was | ||
// disabled via `setAttributes({ version: 1 })` or because the device | ||
// doesn't support it), fall back to a WebGL1 context | ||
this.drawingContext = | ||
this.canvas.getContext('webgl', this._pInst._glAttributes) || | ||
this.canvas.getContext('experimental-webgl', this._pInst._glAttributes); | ||
} | ||
if (this.drawingContext === null) { | ||
throw new Error('Error creating webgl context'); | ||
} else { | ||
|
@@ -402,6 +428,11 @@ p5.RendererGL.prototype._resetContext = function(options, callback) { | |
* lighting shader otherwise per-vertex lighting is used. | ||
* default is true. | ||
* | ||
* version - either 1 or 2, to specify which WebGL version to ask for. By | ||
* default, WebGL 2 will be requested. If WebGL2 is not available, it will | ||
* fall back to WebGL 1. You can check what version is used with by looking at | ||
* the global `webglVersion` property. | ||
* | ||
* @method setAttributes | ||
* @for p5 | ||
* @param {String} key Name of attribute | ||
|
@@ -1251,16 +1282,39 @@ p5.RendererGL.prototype._getLineShader = function() { | |
|
||
p5.RendererGL.prototype._getFontShader = function() { | ||
if (!this._defaultFontShader) { | ||
this.GL.getExtension('OES_standard_derivatives'); | ||
if (this.webglVersion === constants.WEBGL) { | ||
this.GL.getExtension('OES_standard_derivatives'); | ||
} | ||
this._defaultFontShader = new p5.Shader( | ||
this, | ||
defaultShaders.fontVert, | ||
defaultShaders.fontFrag | ||
this._webGL2CompatibilityPrefix('vert', 'mediump') + | ||
defaultShaders.fontVert, | ||
this._webGL2CompatibilityPrefix('frag', 'mediump') + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious if this float precision is always guaranteed to work well on a device that supports WebGL2. Not sure but it is irrelevant because this design will make adjustments to precision based on the device easier in the future. |
||
defaultShaders.fontFrag | ||
); | ||
} | ||
return this._defaultFontShader; | ||
}; | ||
|
||
p5.RendererGL.prototype._webGL2CompatibilityPrefix = function( | ||
shaderType, | ||
floatPrecision | ||
) { | ||
let code = ''; | ||
if (this.webglVersion === constants.WEBGL2) { | ||
code += '#version 300 es\n#define WEBGL2\n'; | ||
} | ||
if (shaderType === 'vert') { | ||
code += '#define VERTEX_SHADER\n'; | ||
} else if (shaderType === 'frag') { | ||
code += '#define FRAGMENT_SHADER\n'; | ||
} | ||
if (floatPrecision) { | ||
code += `precision ${floatPrecision} float;\n`; | ||
} | ||
return code; | ||
}; | ||
|
||
p5.RendererGL.prototype._getEmptyTexture = function() { | ||
if (!this._emptyTexture) { | ||
// a plain white texture RGBA, full alpha, single pixel. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,10 @@ p5.Texture = function(renderer, obj, settings) { | |
|
||
settings = settings || {}; | ||
|
||
if (settings.dataType === gl.FLOAT) { | ||
if ( | ||
settings.dataType === gl.FLOAT && | ||
this._renderer._pInst.webglVersion !== constants.WEBGL2 | ||
) { | ||
const ext = gl.getExtension('OES_texture_float'); | ||
if (!ext) { | ||
console.log( | ||
|
@@ -351,7 +354,10 @@ p5.Texture.prototype.setWrapMode = function(wrapX, wrapY) { | |
const heightPowerOfTwo = isPowerOfTwo(wrapHeight); | ||
|
||
if (wrapX === constants.REPEAT) { | ||
if (widthPowerOfTwo && heightPowerOfTwo) { | ||
if ( | ||
this._renderer.webglVersion === constants.WEBGL2 || | ||
(widthPowerOfTwo && heightPowerOfTwo) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth rolling this into a simple function that returns a boolean just to improve readability. I find the nested conditionals and multiline conditions to be a challenge to read in this section (and many other sections of the GL code). Not a new problem with this PR by any means but one worth keeping in mind. |
||
) { | ||
this.glWrapS = gl.REPEAT; | ||
} else { | ||
console.warn( | ||
|
@@ -360,7 +366,10 @@ p5.Texture.prototype.setWrapMode = function(wrapX, wrapY) { | |
this.glWrapS = gl.CLAMP_TO_EDGE; | ||
} | ||
} else if (wrapX === constants.MIRROR) { | ||
if (widthPowerOfTwo && heightPowerOfTwo) { | ||
if ( | ||
this._renderer.webglVersion === constants.WEBGL2 || | ||
(widthPowerOfTwo && heightPowerOfTwo) | ||
) { | ||
this.glWrapS = gl.MIRRORED_REPEAT; | ||
} else { | ||
console.warn( | ||
|
@@ -374,7 +383,10 @@ p5.Texture.prototype.setWrapMode = function(wrapX, wrapY) { | |
} | ||
|
||
if (wrapY === constants.REPEAT) { | ||
if (widthPowerOfTwo && heightPowerOfTwo) { | ||
if ( | ||
this._renderer.webglVersion === constants.WEBGL2 || | ||
(widthPowerOfTwo && heightPowerOfTwo) | ||
) { | ||
this.glWrapT = gl.REPEAT; | ||
} else { | ||
console.warn( | ||
|
@@ -383,7 +395,10 @@ p5.Texture.prototype.setWrapMode = function(wrapX, wrapY) { | |
this.glWrapT = gl.CLAMP_TO_EDGE; | ||
} | ||
} else if (wrapY === constants.MIRROR) { | ||
if (widthPowerOfTwo && heightPowerOfTwo) { | ||
if ( | ||
this._renderer.webglVersion === constants.WEBGL2 || | ||
(widthPowerOfTwo && heightPowerOfTwo) | ||
) { | ||
this.glWrapT = gl.MIRRORED_REPEAT; | ||
} else { | ||
console.warn( | ||
|
Uh oh!
There was an error while loading. Please reload this page.