Skip to content
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

Refactor shader source classes, in order to work around a Firefox bug. #2214

Merged
merged 8 commits into from
Oct 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Source/Renderer/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,11 @@ define([
});

Context.prototype.replaceShaderProgram = function(shaderProgram, vertexShaderSource, fragmentShaderSource, attributeLocations) {
return this.shaderCache.replaceShaderProgram(shaderProgram, vertexShaderSource, fragmentShaderSource, attributeLocations);
return this._shaderCache.replaceShaderProgram(shaderProgram, vertexShaderSource, fragmentShaderSource, attributeLocations);
};

Context.prototype.createShaderProgram = function(vertexShaderSource, fragmentShaderSource, attributeLocations) {
return this.shaderCache.getShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
return this._shaderCache.getShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
};

function createBuffer(gl, bufferTarget, typedArrayOrSizeInBytes, usage) {
Expand Down
57 changes: 47 additions & 10 deletions Source/Renderer/ShaderCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
define([
'../Core/defined',
'../Core/destroyObject',
'./ShaderProgram'
'./ShaderProgram',
'./ShaderSource'
], function(
defined,
destroyObject,
ShaderProgram) {
ShaderProgram,
ShaderSource) {
"use strict";

/**
Expand All @@ -27,16 +29,15 @@ define([
* </p>
*
* @param {ShaderProgram} shaderProgram The shader program that is being reassigned. This can be <code>undefined</code>.
* @param {String} vertexShaderSource The GLSL source for the vertex shader.
* @param {String} fragmentShaderSource The GLSL source for the fragment shader.
* @param {String|ShaderSource} vertexShaderSource The GLSL source for the vertex shader.
* @param {String|ShaderSource} fragmentShaderSource The GLSL source for the fragment shader.
* @param {Object} attributeLocations Indices for the attribute inputs to the vertex shader.
* @returns {ShaderProgram} The cached or newly created shader program.
*
* @see ShaderCache#getShaderProgram
*
* @example
* this._shaderProgram = context.shaderCache.replaceShaderProgram(
* this._shaderProgram, vs, fs, attributeLocations);
* this._shaderProgram = context.shaderCache.replaceShaderProgram(this._shaderProgram, vs, fs, attributeLocations);
*/
ShaderCache.prototype.replaceShaderProgram = function(shaderProgram, vertexShaderSource, fragmentShaderSource, attributeLocations) {
if (defined(shaderProgram)) {
Expand All @@ -46,8 +47,36 @@ define([
return this.getShaderProgram(vertexShaderSource, fragmentShaderSource, attributeLocations);
};

/**
* Returns a shader program from the cache, or creates and caches a new shader program,
* given the GLSL vertex and fragment shader source and attribute locations.
*
* @param {String|ShaderSource} vertexShaderSource The GLSL source for the vertex shader.
* @param {String|ShaderSource} fragmentShaderSource The GLSL source for the fragment shader.
* @param {Object} attributeLocations Indices for the attribute inputs to the vertex shader.
*
* @returns {ShaderProgram} The cached or newly created shader program.
*/
ShaderCache.prototype.getShaderProgram = function(vertexShaderSource, fragmentShaderSource, attributeLocations) {
var keyword = vertexShaderSource + fragmentShaderSource + JSON.stringify(attributeLocations);
// convert shaders which are provided as strings into ShaderSource objects
// because ShaderSource handles all the automatic including of built-in functions, etc.

if (typeof vertexShaderSource === 'string') {
vertexShaderSource = new ShaderSource({
sources : [vertexShaderSource]
});
}

if (typeof fragmentShaderSource === 'string') {
fragmentShaderSource = new ShaderSource({
sources : [fragmentShaderSource]
});
}

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

var keyword = vertexShaderText + fragmentShaderText + JSON.stringify(attributeLocations);
var cachedShader;

if (this._shaders[keyword]) {
Expand All @@ -57,17 +86,25 @@ define([
delete this._shadersToRelease[keyword];
} else {
var context = this._context;
var sp = new ShaderProgram(context._gl, context.logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations);
var shaderProgram = new ShaderProgram({
gl : context._gl,
logShaderCompilation : context.logShaderCompilation,
vertexShaderSource : vertexShaderSource,
vertexShaderText : vertexShaderText,
fragmentShaderSource : fragmentShaderSource,
fragmentShaderText : fragmentShaderText,
attributeLocations : attributeLocations
});

cachedShader = {
cache : this,
shaderProgram : sp,
shaderProgram : shaderProgram,
keyword : keyword,
count : 0
};

// A shader can't be in more than one cache.
sp._cachedShader = cachedShader;
shaderProgram._cachedShader = cachedShader;
this._shaders[keyword] = cachedShader;
}

Expand Down
Loading