Skip to content

Commit

Permalink
Merge pull request #5623 from AnimatedRNG/webgl2-upgrade
Browse files Browse the repository at this point in the history
modernizeShader and other WebGL2 changes
  • Loading branch information
lilleyse authored Jul 19, 2017
2 parents 50728e3 + a05a976 commit c6d302f
Show file tree
Hide file tree
Showing 11 changed files with 679 additions and 28 deletions.
42 changes: 37 additions & 5 deletions Source/Renderer/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,15 @@ define([

// Query and initialize extensions
this._standardDerivatives = !!getExtension(gl, ['OES_standard_derivatives']);
this._blendMinmax = !!getExtension(gl, ['EXT_blend_minmax']);
this._elementIndexUint = !!getExtension(gl, ['OES_element_index_uint']);
this._depthTexture = !!getExtension(gl, ['WEBGL_depth_texture', 'WEBKIT_WEBGL_depth_texture']);
this._textureFloat = !!getExtension(gl, ['OES_texture_float']);
this._fragDepth = !!getExtension(gl, ['EXT_frag_depth']);
this._debugShaders = getExtension(gl, ['WEBGL_debug_shaders']);

this._colorBufferFloat = this._webgl2 && !!getExtension(gl, ['EXT_color_buffer_float']);

this._s3tc = !!getExtension(gl, ['WEBGL_compressed_texture_s3tc', 'MOZ_WEBGL_compressed_texture_s3tc', 'WEBKIT_WEBGL_compressed_texture_s3tc']);
this._pvrtc = !!getExtension(gl, ['WEBGL_compressed_texture_pvrtc', 'WEBKIT_WEBGL_compressed_texture_pvrtc']);
this._etc1 = !!getExtension(gl, ['WEBGL_compressed_texture_etc1']);
Expand Down Expand Up @@ -483,7 +486,21 @@ define([
*/
standardDerivatives : {
get : function() {
return this._standardDerivatives;
return this._standardDerivatives || this._webgl2;
}
},

/**
* <code>true</code> if the EXT_blend_minmax extension is supported. This
* extension extends blending capabilities by adding two new blend equations:
* the minimum or maximum color components of the source and destination colors.
* @memberof Context.prototype
* @type {Boolean}
* @see {@link https://www.khronos.org/registry/webgl/extensions/EXT_blend_minmax/}
*/
blendMinmax : {
get : function() {
return this._blendMinmax || this._webgl2;
}
},

Expand All @@ -510,7 +527,7 @@ define([
*/
depthTexture : {
get : function() {
return this._depthTexture;
return this._depthTexture || this._webgl2;
}
},

Expand All @@ -523,7 +540,7 @@ define([
*/
floatingPointTexture : {
get : function() {
return this._textureFloat;
return this._textureFloat || this._colorBufferFloat;
}
},

Expand Down Expand Up @@ -597,7 +614,7 @@ define([
*/
fragmentDepth : {
get : function() {
return this._fragDepth;
return this._fragDepth || this._webgl2;
}
},

Expand All @@ -614,6 +631,20 @@ define([
}
},

/**
* <code>true</code> if the EXT_color_buffer_float extension is supported. This
* extension makes the formats gl.R16F, gl.RG16F, gl.RGBA16F, gl.R32F, gl.RG32F,
* gl.RGBA32F, gl.R11F_G11F_B10F color renderable.
* @memberof Context.prototype
* @type {Boolean}
* @see {@link https://www.khronos.org/registry/webgl/extensions/EXT_color_buffer_float/}
*/
colorBufferFloat : {
get : function() {
return this._colorBufferFloat;
}
},

/**
* <code>true</code> if the WEBGL_draw_buffers extension is supported. This
* extensions provides support for multiple render targets. The framebuffer object can have mutiple
Expand Down Expand Up @@ -1076,7 +1107,8 @@ define([
}),
uniformMap : overrides.uniformMap,
owner : overrides.owner,
framebuffer : overrides.framebuffer
framebuffer : overrides.framebuffer,
pass : overrides.pass
});
};

Expand Down
4 changes: 3 additions & 1 deletion Source/Renderer/RenderState.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ define([
function validateBlendEquation(blendEquation) {
return ((blendEquation === WebGLConstants.FUNC_ADD) ||
(blendEquation === WebGLConstants.FUNC_SUBTRACT) ||
(blendEquation === WebGLConstants.FUNC_REVERSE_SUBTRACT));
(blendEquation === WebGLConstants.FUNC_REVERSE_SUBTRACT) ||
(blendEquation === WebGLConstants.MIN) ||
(blendEquation === WebGLConstants.MAX));
}

function validateBlendFunction(blendFunction) {
Expand Down
11 changes: 6 additions & 5 deletions Source/Renderer/ShaderCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ define([
});
}

var vertexShaderText = vertexShaderSource.createCombinedVertexShader();
var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader();
var vertexShaderText = vertexShaderSource.createCombinedVertexShader(this._context);
var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(this._context);

var keyword = vertexShaderText + fragmentShaderText + JSON.stringify(attributeLocations);
var cachedShader;
Expand Down Expand Up @@ -169,10 +169,11 @@ define([
});
}

var vertexShaderText = vertexShaderSource.createCombinedVertexShader();
var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader();

var context = this._context;

var vertexShaderText = vertexShaderSource.createCombinedVertexShader(context);
var fragmentShaderText = fragmentShaderSource.createCombinedFragmentShader(context);

var derivedShaderProgram = new ShaderProgram({
gl : context._gl,
logShaderCompilation : context.logShaderCompilation,
Expand Down
43 changes: 38 additions & 5 deletions Source/Renderer/ShaderSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ define([
'../Core/defaultValue',
'../Core/defined',
'../Core/DeveloperError',
'../Renderer/modernizeShader',
'../Shaders/Builtin/CzmBuiltins',
'./AutomaticUniforms'
], function(
defaultValue,
defined,
DeveloperError,
modernizeShader,
CzmBuiltins,
AutomaticUniforms) {
'use strict';
Expand Down Expand Up @@ -151,7 +153,7 @@ define([
return builtinsSource.replace(root.glslSource, '');
}

function combineShader(shaderSource, isFragmentShader) {
function combineShader(shaderSource, isFragmentShader, context) {
var i;
var length;

Expand Down Expand Up @@ -186,6 +188,17 @@ define([
return '\n';
});

// Extract shader extensions from sources
var extensions = [];
combinedSources = combinedSources.replace(/#extension.*\n/gm, function(match) {
// Extract extension to put at the top
extensions.push(match);

// Replace original #extension directive with a new line so the line numbers
// are not off by one.
return '\n';
});

// Remove precision qualifier
combinedSources = combinedSources.replace(/precision\s(lowp|mediump|highp)\s(float|int);/, '');

Expand All @@ -204,6 +217,11 @@ define([
result = '#version ' + version + '\n';
}

var extensionsLength = extensions.length;
for (i = 0; i < extensionsLength; i++) {
result += extensions[i];
}

if (isFragmentShader) {
result += '\
#ifdef GL_FRAGMENT_PRECISION_HIGH\n\
Expand All @@ -224,6 +242,12 @@ define([
}
}

// GLSLModernizer inserts its own layout qualifiers
// at this position in the source
if (context.webgl2) {
result += '#define OUTPUT_DECLARATION\n\n';
}

// append built-ins
if (shaderSource.includeBuiltIns) {
result += getBuiltinsAndAutomaticUniforms(combinedSources);
Expand All @@ -235,6 +259,11 @@ define([
// append actual source
result += combinedSources;

// modernize the source
if (context.webgl2) {
result = modernizeShader(result, isFragmentShader, true);
}

return result;
}

Expand Down Expand Up @@ -297,19 +326,23 @@ define([
/**
* Create a single string containing the full, combined vertex shader with all dependencies and defines.
*
* @param {Context} context The current rendering context
*
* @returns {String} The combined shader string.
*/
ShaderSource.prototype.createCombinedVertexShader = function() {
return combineShader(this, false);
ShaderSource.prototype.createCombinedVertexShader = function(context) {
return combineShader(this, false, context);
};

/**
* Create a single string containing the full, combined fragment shader with all dependencies and defines.
*
* @param {Context} context The current rendering context
*
* @returns {String} The combined shader string.
*/
ShaderSource.prototype.createCombinedFragmentShader = function() {
return combineShader(this, true);
ShaderSource.prototype.createCombinedFragmentShader = function(context) {
return combineShader(this, true, context);
};

/**
Expand Down
17 changes: 17 additions & 0 deletions Source/Renderer/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ define([
internalFormat = WebGLConstants.DEPTH_COMPONENT24;
}
}

if (pixelDatatype === PixelDatatype.FLOAT) {
switch (pixelFormat) {
case PixelFormat.RGBA:
internalFormat = WebGLConstants.RGBA32F;
break;
case PixelFormat.RGB:
internalFormat = WebGLConstants.RGB32F;
break;
case PixelFormat.RG:
internalFormat = WebGLConstants.RG32F;
break;
case PixelFormat.R:
internalFormat = WebGLConstants.R32F;
break;
}
}
}

//>>includeStart('debug', pragmas.debug);
Expand Down
Loading

0 comments on commit c6d302f

Please sign in to comment.