diff options
author | petersont@google.com <petersont@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-14 23:35:25 +0000 |
---|---|---|
committer | petersont@google.com <petersont@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-14 23:35:25 +0000 |
commit | 92e4874bf90039f08a262d6bc44c8cc6eba7c430 (patch) | |
tree | f810f224623ae2505ce35a38ae65c56634abc00c | |
parent | ba443b7a9aa9d0a3c44a3176384f7c7582a9604b (diff) | |
download | chromium_src-92e4874bf90039f08a262d6bc44c8cc6eba7c430.zip chromium_src-92e4874bf90039f08a262d6bc44c8cc6eba7c430.tar.gz chromium_src-92e4874bf90039f08a262d6bc44c8cc6eba7c430.tar.bz2 |
Refactored the SAS parameter code slightly to avoid redundant listing of parameter names.
Review URL: http://codereview.chromium.org/2122002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47337 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | o3d/samples/o3d-webgl/effect.js | 263 | ||||
-rw-r--r-- | o3d/samples/o3d-webgl/param.js | 88 |
2 files changed, 170 insertions, 181 deletions
diff --git a/o3d/samples/o3d-webgl/effect.js b/o3d/samples/o3d-webgl/effect.js index 0f241db..b0386d7 100644 --- a/o3d/samples/o3d-webgl/effect.js +++ b/o3d/samples/o3d-webgl/effect.js @@ -33,56 +33,54 @@ /** * EffectParameterInfo holds information about the Parameters an Effect needs. * o3d.Effect.getParameterInfo - */ -o3d.EffectParameterInfo = function() { }; + * @param {string} name Tne name of the parameter. + * @param {string} className The param class name. + * @param {number} numElements The number of Elements if the param is an array. + * @param {string} sasClassName The sas class name if the param is an sas type. + * @param {string} semantic The relevant semantic. + */ +o3d.EffectParameterInfo = + function(name, className, numElements, semantic, sasClassName) { + /** + * The name of the parameter. + * @type {string} + */ + this.name = name || ''; + + /** + * The type of the parameter. + * @type {string} + */ + this.className = className || ''; + + /** + * The number of elements. Non-zero for array types, zero for non-array + * types. + * @type {number} + */ + this.numElements = numElements || 0; + + /** + * The semantic of the parameter. This is always in UPPERCASE. + * @type {o3d.Stream.Semantic} + */ + this.semantic = semantic || o3d.Stream.UNKNOWN_SEMANTIC; + + /** + * If this is a standard parameter (SAS) this will be the name of the type + * of Param needed. Otherwise it will be the empty string. + * + * Standard Parameters are generally handled automatically by o3d but you + * can supply your own if you have a unique situation. + * + * @type {string} + */ + this.sasClassName = sasClassName || ''; +}; o3d.inherit('EffectParameterInfo', 'NamedObject'); /** - * The name of the parameter. - * @type {string} - */ -o3d.EffectParameterInfo.prototype.name = ''; - - - -/** - * The type of the parameter. - * @type {string} - */ -o3d.EffectParameterInfo.prototype.className = ''; - - - -/** - * The number of elements. Non-zero for array types, zero for non-array types. - * @type {number} - */ -o3d.EffectParameterInfo.prototype.numElements = 0; - - - -/** - * The semantic of the parameter. This is always in UPPERCASE. - * @type {o3d.Stream.Semantic} - */ -o3d.EffectParameterInfo.prototype.semantic = o3d.Stream.UNKNOWN_SEMANTIC; - - -/** - * If this is a standard parameter (SAS) this will be the name of the type - * of Param needed. Otherwise it will be the empty string. - * - * Standard Parameters are generally handled automatically by o3d but you - * can supply your own if you have a unique situation. - * - * @type {string} - */ -o3d.EffectParameterInfo.prototype.sas_class_name = ''; - - - -/** * EffectStreamInfo holds information about the Streams an Effect needs. * @param {o3d.Stream.Semantic} opt_semantic The semantic of the stream * @param {number} opt_semantic_index @@ -173,11 +171,10 @@ o3d.Effect.prototype.fragmentShaderLoaded_ = false; */ o3d.Effect.prototype.bindAttributesAndLinkIfReady = function() { if (this.vertexShaderLoaded_ && this.fragmentShaderLoaded_) { - var attributes = ['position', 'normal', 'tangent', 'binormal', 'color', - 'texCoord0', 'texCoord1', 'texCoord2', 'texCoord3', - 'texCoord4', 'texCoord5', 'texCoord6', 'texCoord7']; - for (var i = 0; i < attributes.length; ++i) { - this.gl.bindAttribLocation(this.program_, i, attributes[i]); + var semanticMap = o3d.Effect.semanticMap_; + for (var name in semanticMap) { + this.gl.bindAttribLocation( + this.program_, semanticMap[name].gl_index, name); } this.gl.linkProgram(this.program_); if (!this.gl.getProgramParameter(this.program_, this.gl.LINK_STATUS)) { @@ -281,8 +278,8 @@ o3d.Effect.prototype.getUniforms_ = this.program_, this.gl.ACTIVE_UNIFORMS); for (var i = 0; i < numUniforms; ++i) { var info = this.gl.getActiveUniform(this.program_, i); - this.uniforms_[info.name] = {info:info, - location:this.gl.getUniformLocation(this.program_, info.name)}; + this.uniforms_[info.name] = {info: info, + location: this.gl.getUniformLocation(this.program_, info.name)}; } }; @@ -307,6 +304,59 @@ o3d.Effect.prototype.getAttributes_ = /** + * A map linking the WebGL type of a uniform to the appropriate param type. + * This gets memoized in the function createUniformParameters. + * @private + */ +o3d.Effect.paramTypes_ = null; + +/** + * Accesses the paramTypes map, builds it if it isn't already built. + * @private; + */ +o3d.Effect.getParamTypes_ = function(gl) { + // Even though these constants should be the same for different webgl + // contexts, we can't create this table until the context is loaded, so + // we initialize it here rather than when the file loads. + if (!o3d.Effect.paramTypes_) { + o3d.Effect.paramTypes_ = {}; + o3d.Effect.paramTypes_[gl.FLOAT] = 'ParamFloat'; + o3d.Effect.paramTypes_[gl.FLOAT_VEC2] = 'ParamFloat2'; + o3d.Effect.paramTypes_[gl.FLOAT_VEC3] = 'ParamFloat3'; + o3d.Effect.paramTypes_[gl.FLOAT_VEC4] = 'ParamFloat4'; + o3d.Effect.paramTypes_[gl.INT] = 'ParamInteger'; + o3d.Effect.paramTypes_[gl.BOOL] = 'ParamBoolean'; + o3d.Effect.paramTypes_[gl.FLOAT_MAT4] = 'ParamMatrix4'; + o3d.Effect.paramTypes_[gl.SAMPLER_2D] = 'ParamSampler'; + o3d.Effect.paramTypes_[gl.SAMPLER_CUBE] = 'ParamSampler'; + } + + return o3d.Effect.paramTypes_; +} + +/** + * A map linking names of certain attributes in the shader to the corresponding + * semantic and semantic index. + * @private + */ +o3d.Effect.semanticMap_ = { + 'position': {semantic: o3d.Stream.POSITION, index: 0, gl_index: 0}, + 'normal': {semantic: o3d.Stream.NORMAL, index: 0, gl_index: 1}, + 'tangent': {semantic: o3d.Stream.TANGENT, index: 0, gl_index: 2}, + 'binormal': {semantic: o3d.Stream.BINORMAL, index: 0, gl_index: 3}, + 'color': {semantic: o3d.Stream.COLOR, index: 0, gl_index: 4}, + 'texCoord0': {semantic: o3d.Stream.TEXCOORD, index: 0, gl_index: 5}, + 'texCoord1': {semantic: o3d.Stream.TEXCOORD, index: 1, gl_index: 6}, + 'texCoord2': {semantic: o3d.Stream.TEXCOORD, index: 2, gl_index: 7}, + 'texCoord3': {semantic: o3d.Stream.TEXCOORD, index: 3, gl_index: 8}, + 'texCoord4': {semantic: o3d.Stream.TEXCOORD, index: 4, gl_index: 9}, + 'texCoord5': {semantic: o3d.Stream.TEXCOORD, index: 5, gl_index: 10}, + 'texCoord6': {semantic: o3d.Stream.TEXCOORD, index: 6, gl_index: 11}, + 'texCoord7': {semantic: o3d.Stream.TEXCOORD, index: 7, gl_index: 12} +}; + + +/** * For each of the effect's uniform parameters, creates corresponding * parameters on the given ParamObject. Skips SAS Parameters. * @@ -323,69 +373,14 @@ o3d.Effect.prototype.getAttributes_ = */ o3d.Effect.prototype.createUniformParameters = function(param_object) { - var sasNames = {'world': true, - 'view': true, - 'projection': true, - 'worldView': true, - 'worldProjection': true, - 'worldViewProjection': true, - 'worldInverse': true, - 'viewInverse': true, - 'projectionInverse': true, - 'worldViewInverse': true, - 'worldProjectionInverse': true, - 'worldViewProjectionInverse': true, - 'worldTranspose': true, - 'viewTranspose': true, - 'projectionTranspose': true, - 'worldViewTranspose': true, - 'worldProjectionTranspose': true, - 'worldViewProjectionTranspose': true, - 'worldInverseTranspose': true, - 'viewInverseTranspose': true, - 'projectionInverseTranspose': true, - 'worldViewInverseTranspose': true, - 'worldProjectionInverseTranspose': true, - 'worldViewProjectionInverseTranspose': true}; + var sasTypes = o3d.Param.sasTypes_; + var paramTypes = o3d.Effect.getParamTypes_(this.gl); for (var name in this.uniforms_) { var info = this.uniforms_[name].info; - - if (sasNames[name]) - continue; - - var paramType = ''; - switch (info.type) { - case this.gl.FLOAT: - paramType = 'ParamFloat'; - break; - case this.gl.FLOAT_VEC2: - paramType = 'ParamFloat2'; - break; - case this.gl.FLOAT_VEC3: - paramType = 'ParamFloat3'; - break; - case this.gl.FLOAT_VEC4: - paramType = 'ParamFloat4'; - break; - case this.gl.INT: - paramType = 'ParamInteger'; - break; - case this.gl.BOOL: - paramType = 'ParamBoolean'; - break; - case this.gl.FLOAT_MAT4: - paramType = 'ParamMatrix4'; - break; - case this.gl.SAMPLER_2D: - paramType = 'ParamSampler'; - break; - case this.gl.SAMPLER_CUBE: - paramType = 'ParamSampler'; - break; + if (!sasTypes[name]) { + param_object.createParam(info.name, paramTypes[info.type]); } - - param_object.createParam(info.name, paramType); } }; @@ -408,7 +403,14 @@ o3d.Effect.prototype.createUniformParameters = */ o3d.Effect.prototype.createSASParameters = function(param_object) { - o3d.notImplemented(); + var sasTypes = o3d.Param.sasTypes_; + for (var name in this.uniforms_) { + var info = this.uniforms_[name].info; + var sasType = sasTypes[name]; + if (sasType) { + param_object.createParam(info.name, sasType); + } + } }; @@ -418,8 +420,23 @@ o3d.Effect.prototype.createSASParameters = * EffectParameterInfo objects. */ o3d.Effect.prototype.getParameterInfo = function() { - o3d.notImplemented(); - return []; + var infoArray = []; + var sasTypes = o3d.Param.sasTypes_; + var paramTypes = o3d.Effect.getParamTypes_(this.gl); + var semanticMap = o3d.Effect.semanticMap_; + + for (var name in this.uniforms_) { + var info = this.uniforms_[name].info; + var sasTypeName = sasTypes[name] || ''; + var className = paramTypes[info.type] || ''; + var numElements = 0; // TODO(petersont): Add array support. + var semantic = semanticMap[name].semantic || o3d.Stream.UNKNOWN_SEMANTIC; + + infoArray.push(new EffectParameterInfo( + name, className, numElements, semantic, sasClassName)); + } + + return infoArray; }; @@ -432,21 +449,7 @@ o3d.Effect.prototype.getStreamInfo = function() { var infoList = []; for (var name in this.attributes_) { - var attributes = { - 'position': {semantic: o3d.Stream.POSITION, index: 0}, - 'normal': {semantic: o3d.Stream.NORMAL, index: 0}, - 'tangent': {semantic: o3d.Stream.TANGENT, index: 0}, - 'binormal': {semantic: o3d.Stream.BINORMAL, index: 0}, - 'color': {semantic: o3d.Stream.COLOR, index: 0}, - 'texCoord0': {semantic: o3d.Stream.TEXCOORD, index: 0}, - 'texCoord1': {semantic: o3d.Stream.TEXCOORD, index: 1}, - 'texCoord2': {semantic: o3d.Stream.TEXCOORD, index: 2}, - 'texCoord3': {semantic: o3d.Stream.TEXCOORD, index: 3}, - 'texCoord4': {semantic: o3d.Stream.TEXCOORD, index: 4}, - 'texCoord5': {semantic: o3d.Stream.TEXCOORD, index: 5}, - 'texCoord6': {semantic: o3d.Stream.TEXCOORD, index: 6}, - 'texCoord7': {semantic: o3d.Stream.TEXCOORD, index: 7}}; - var semantic_index_pair = attributes[name]; + var semantic_index_pair = o3d.Effect.semanticMap_[name]; infoList.push(new o3d.EffectStreamInfo( semantic_index_pair.semantic, semantic_index_pair.index)); } diff --git a/o3d/samples/o3d-webgl/param.js b/o3d/samples/o3d-webgl/param.js index 1792eb8..a7b3e6f 100644 --- a/o3d/samples/o3d-webgl/param.js +++ b/o3d/samples/o3d-webgl/param.js @@ -838,57 +838,43 @@ o3d.ParamSampler.prototype.applyToLocation = function(gl, location) { */ o3d.Param.SAS = new o3d.ParamObject; -o3d.Param.SAS.createParam('world', - 'WorldParamMatrix4'); -o3d.Param.SAS.createParam('view', - 'ViewParamMatrix4'); -o3d.Param.SAS.createParam('projection', - 'ProjectionParamMatrix4'); -o3d.Param.SAS.createParam('worldView', - 'WorldViewParamMatrix4'); -o3d.Param.SAS.createParam('viewProjection', - 'ViewProjectionParamMatrix4'); -o3d.Param.SAS.createParam('worldViewProjection', - 'WorldViewProjectionParamMatrix4'); - -o3d.Param.SAS.createParam('worldInverse', - 'WorldInverseParamMatrix4'); -o3d.Param.SAS.createParam('viewInverse', - 'ViewInverseParamMatrix4'); -o3d.Param.SAS.createParam('projectionInverse', - 'ProjectionInverseParamMatrix4'); -o3d.Param.SAS.createParam('worldViewInverse', - 'WorldViewInverseParamMatrix4'); -o3d.Param.SAS.createParam('viewProjectionInverse', - 'ViewProjectionInverseParamMatrix4'); -o3d.Param.SAS.createParam('worldViewProjectionInverse', - 'WorldViewProjectionInverseParamMatrix4'); - -o3d.Param.SAS.createParam('worldTranspose', - 'WorldInverseParamMatrix4'); -o3d.Param.SAS.createParam('viewTranspose', - 'ViewTransposeParamMatrix4'); -o3d.Param.SAS.createParam('projectionTranspose', - 'ProjectionTransposeParamMatrix4'); -o3d.Param.SAS.createParam('worldViewTranspose', - 'WorldViewTransposeParamMatrix4'); -o3d.Param.SAS.createParam('viewProjectionTranspose', - 'ViewProjectionTransposeParamMatrix4'); -o3d.Param.SAS.createParam('worldViewProjectionTranspose', - 'WorldViewProjectionTransposeParamMatrix4'); - -o3d.Param.SAS.createParam('worldInverseTranspose', - 'WorldInverseTransposeParamMatrix4'); -o3d.Param.SAS.createParam('viewInverseTranspose', - 'ViewInverseTransposeParamMatrix4'); -o3d.Param.SAS.createParam('projectionInverseTranspose', - 'ProjectionInverseTransposeParamMatrix4'); -o3d.Param.SAS.createParam('worldViewInverseTranspose', - 'WorldViewInverseTransposeParamMatrix4'); -o3d.Param.SAS.createParam('viewProjectionInverseTranspose', - 'ViewProjectionInverseTransposeParamMatrix4'); -o3d.Param.SAS.createParam('worldViewProjectionInverseTranspose', - 'WorldViewProjectionInverseTransposeParamMatrix4'); +/** + * A map linking the names of SAS parameters to their standard matrix parameter + * types. + * @private + */ +o3d.Param.sasTypes_ = { + 'world': 'WorldParamMatrix4', + 'view': 'ViewParamMatrix4', + 'projection': 'ProjectionParamMatrix4', + 'worldView': 'WorldViewParamMatrix4', + 'viewProjection': 'ViewProjectionParamMatrix4', + 'worldViewProjection': 'WorldViewProjectionParamMatrix4', + 'worldInverse': 'WorldInverseParamMatrix4', + 'viewInverse': 'ViewInverseParamMatrix4', + 'projectionInverse': 'ProjectionInverseParamMatrix4', + 'worldViewInverse': 'WorldViewInverseParamMatrix4', + 'viewProjectionInverse': 'ViewProjectionInverseParamMatrix4', + 'worldViewProjectionInverse': 'WorldViewProjectionInverseParamMatrix4', + 'worldTranspose': 'WorldTransposeParamMatrix4', + 'viewTranspose': 'ViewTransposeParamMatrix4', + 'projectionTranspose': 'ProjectionTransposeParamMatrix4', + 'worldViewTranspose': 'WorldViewTransposeParamMatrix4', + 'viewProjectionTranspose': 'ViewProjectionTransposeParamMatrix4', + 'worldViewProjectionTranspose': 'WorldViewProjectionTransposeParamMatrix4', + 'worldInverseTranspose': 'WorldInverseTransposeParamMatrix4', + 'viewInverseTranspose': 'ViewInverseTransposeParamMatrix4', + 'projectionInverseTranspose': 'ProjectionInverseTransposeParamMatrix4', + 'worldViewInverseTranspose': 'WorldViewInverseTransposeParamMatrix4', + 'viewProjectionInverseTranspose': + 'ViewProjectionInverseTransposeParamMatrix4', + 'worldViewProjectionInverseTranspose': + 'WorldViewProjectionInverseTransposeParamMatrix4' +}; + +for (name in o3d.Param.sasTypes_) { + o3d.Param.SAS.createParam(name, o3d.Param.sasTypes_[name]); +} /** * Sets the base world matrix that gets use to compute all other products for |