tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sglrShaderProgram.js (12152B)


      1 /*-------------------------------------------------------------------------
      2 * drawElements Quality Program OpenGL ES Utilities
      3 * ------------------------------------------------
      4 *
      5 * Copyright 2014 The Android Open Source Project
      6 *
      7 * Licensed under the Apache License, Version 2.0 (the "License");
      8 * you may not use this file except in compliance with the License.
      9 * You may obtain a copy of the License at
     10 *
     11 *      http://www.apache.org/licenses/LICENSE-2.0
     12 *
     13 * Unless required by applicable law or agreed to in writing, software
     14 * distributed under the License is distributed on an "AS IS" BASIS,
     15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 * See the License for the specific language governing permissions and
     17 * limitations under the License.
     18 *
     19 */
     20 
     21 'use strict';
     22 goog.provide('framework.opengl.simplereference.sglrShaderProgram');
     23 goog.require('framework.common.tcuTexture');
     24 goog.require('framework.common.tcuTextureUtil');
     25 goog.require('framework.delibs.debase.deMath');
     26 goog.require('framework.opengl.gluShaderUtil');
     27 goog.require('framework.opengl.gluTextureUtil');
     28 goog.require('framework.referencerenderer.rrDefs');
     29 goog.require('framework.referencerenderer.rrFragmentOperations');
     30 goog.require('framework.referencerenderer.rrGenericVector');
     31 goog.require('framework.referencerenderer.rrShaders');
     32 goog.require('framework.referencerenderer.rrShadingContext');
     33 goog.require('framework.referencerenderer.rrVertexAttrib');
     34 goog.require('framework.referencerenderer.rrVertexPacket');
     35 
     36 goog.scope(function() {
     37 
     38    var sglrShaderProgram = framework.opengl.simplereference.sglrShaderProgram;
     39    var rrShaders = framework.referencerenderer.rrShaders;
     40    var rrGenericVector = framework.referencerenderer.rrGenericVector;
     41    var tcuTexture = framework.common.tcuTexture;
     42    var deMath = framework.delibs.debase.deMath;
     43    var gluTextureUtil = framework.opengl.gluTextureUtil;
     44    var gluShaderUtil = framework.opengl.gluShaderUtil;
     45    var tcuTextureUtil = framework.common.tcuTextureUtil;
     46    var rrDefs = framework.referencerenderer.rrDefs;
     47    var rrFragmentOperations = framework.referencerenderer.rrFragmentOperations;
     48    var rrVertexAttrib = framework.referencerenderer.rrVertexAttrib;
     49    var rrVertexPacket = framework.referencerenderer.rrVertexPacket;
     50    var rrShadingContext = framework.referencerenderer.rrShadingContext;
     51 
     52    var DE_ASSERT = function(x) {
     53        if (!x)
     54            throw new Error('Assert failed');
     55    };
     56 
     57    /**
     58     * sglrShaderProgram.VaryingFlags
     59     * @constructor
     60     * @struct
     61     */
     62    sglrShaderProgram.VaryingFlags = function() {
     63        this.NONE = true; //TODO: is NONE necessary?
     64        this.FLATSHADE = false;
     65    };
     66 
     67    /**
     68     * sglrShaderProgram.VertexAttribute
     69     * @constructor
     70     * @param {string} name_
     71     * @param {rrGenericVector.GenericVecType} type_
     72     */
     73    sglrShaderProgram.VertexAttribute = function(name_, type_) {
     74        this.name = name_;
     75        this.type = type_;
     76    };
     77 
     78    /**
     79     * sglrShaderProgram.VertexToFragmentVarying
     80     * @constructor
     81     * @param {rrGenericVector.GenericVecType} type_
     82     * @param {sglrShaderProgram.VaryingFlags=} flags
     83     */
     84    sglrShaderProgram.VertexToFragmentVarying = function(type_, flags) {
     85        this.type = type_;
     86        this.flatshade = flags === undefined ? new sglrShaderProgram.VaryingFlags().FLATSHADE : flags.FLATSHADE;
     87    };
     88 
     89    /**
     90     * sglrShaderProgram.FragmentOutput
     91     * @constructor
     92     * @param {rrGenericVector.GenericVecType} type_
     93     */
     94    sglrShaderProgram.FragmentOutput = function(type_) {
     95        /** @type {rrGenericVector.GenericVecType} */ this.type = type_;
     96    };
     97 
     98    /**
     99     * sglrShaderProgram.Uniform
    100     * @constructor
    101     * @param {string} name_
    102     * @param {gluShaderUtil.DataType} type_
    103     */
    104    sglrShaderProgram.Uniform = function(name_, type_) {
    105        /** @type {string} */ this.name = name_;
    106        /** @type {gluShaderUtil.DataType} */ this.type = type_;
    107        /** @type {Array<number>} */ this.value;
    108        /** @type {?rrDefs.Sampler} */ this.sampler = null;
    109    };
    110 
    111    /**
    112     * sglrShaderProgram.VertexSource
    113     * @constructor
    114     * @param {string} str
    115     */
    116    sglrShaderProgram.VertexSource = function(str) {
    117        /** @type {string} */ this.source = str;
    118    };
    119 
    120    /**
    121     * sglrShaderProgram.FragmentSource
    122     * @constructor
    123     * @param {string} str
    124     */
    125    sglrShaderProgram.FragmentSource = function(str) {
    126        /** @type {string} */ this.source = str;
    127    };
    128 
    129    /**
    130     * sglrShaderProgram.ShaderProgramDeclaration
    131     * @constructor
    132     */
    133    sglrShaderProgram.ShaderProgramDeclaration = function() {
    134        /** @type {Array<sglrShaderProgram.VertexAttribute>} */ this.m_vertexAttributes = [];
    135        /** @type {Array<sglrShaderProgram.VertexToFragmentVarying>} */ this.m_vertexToFragmentVaryings = [];
    136        /** @type {Array<sglrShaderProgram.FragmentOutput>} */ this.m_fragmentOutputs = [];
    137        /** @type {Array<sglrShaderProgram.Uniform>} */ this.m_uniforms = [];
    138        /** @type {string} */ this.m_vertexSource;
    139        /** @type {string} */ this.m_fragmentSource;
    140 
    141        /** @type {boolean} */ this.m_vertexShaderSet = false;
    142        /** @type {boolean} */ this.m_fragmentShaderSet = false;
    143    };
    144 
    145    /**
    146     * Add a vertex attribute to the shader program declaration.
    147     * @param {sglrShaderProgram.VertexAttribute} v
    148     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    149     */
    150    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushVertexAttribute = function(v) {
    151        this.m_vertexAttributes.push(v);
    152        return this;
    153    };
    154 
    155    /**
    156     * Add a vertex to fragment varying to the shader program declaration.
    157     * @param {sglrShaderProgram.VertexToFragmentVarying} v
    158     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    159     */
    160    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushVertexToFragmentVarying = function(v) {
    161        this.m_vertexToFragmentVaryings.push(v);
    162        return this;
    163    };
    164 
    165    /**
    166     * Add a fragment output to the shader program declaration.
    167     * @param {sglrShaderProgram.FragmentOutput} v
    168     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    169     */
    170    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushFragmentOutput = function(v) {
    171        this.m_fragmentOutputs.push(v);
    172        return this;
    173    };
    174 
    175    /**
    176     * Add a uniform to the shader program declaration.
    177     * @param {sglrShaderProgram.Uniform} v
    178     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    179     */
    180    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushUniform = function(v) {
    181        this.m_uniforms.push(v);
    182        return this;
    183    };
    184 
    185    /**
    186     * @param {sglrShaderProgram.VertexSource} c
    187     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    188     */
    189    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushVertexSource = function(c) {
    190        DE_ASSERT(!this.m_vertexShaderSet);
    191        this.m_vertexSource = c.source;
    192        this.m_vertexShaderSet = true;
    193        return this;
    194    };
    195 
    196    /**
    197     * @param {sglrShaderProgram.FragmentSource} c
    198     * @return {sglrShaderProgram.ShaderProgramDeclaration}
    199     */
    200    sglrShaderProgram.ShaderProgramDeclaration.prototype.pushFragmentSource = function(c) {
    201        DE_ASSERT(!this.m_fragmentSource);
    202        /** @type {sglrShaderProgram.FragmentSource} */ this.m_fragmentSource = c.source;
    203        /** @type {boolean} */ this.m_fragmentShaderSet = true;
    204        return this;
    205    };
    206 
    207    /**
    208     * @return {boolean}
    209     */
    210    sglrShaderProgram.ShaderProgramDeclaration.prototype.valid = function() {
    211        if (!this.m_vertexShaderSet || !this.m_fragmentShaderSet)
    212            return false;
    213 
    214        if (this.m_fragmentOutputs.length == 0)
    215            return false;
    216 
    217        return true;
    218    };
    219 
    220    /**
    221     * @return {number}
    222     */
    223    sglrShaderProgram.ShaderProgramDeclaration.prototype.getVertexInputCount = function() {
    224        return this.m_vertexAttributes.length;
    225    };
    226 
    227    /**
    228     * @return {number}
    229     */
    230    sglrShaderProgram.ShaderProgramDeclaration.prototype.getVertexOutputCount = function() {
    231        return this.m_vertexToFragmentVaryings.length;
    232    };
    233 
    234    /**
    235     * @return {number}
    236     */
    237    sglrShaderProgram.ShaderProgramDeclaration.prototype.getFragmentInputCount = function() {
    238        return this.m_vertexToFragmentVaryings.length;
    239    };
    240 
    241    /**
    242     * @return {number}
    243     */
    244    sglrShaderProgram.ShaderProgramDeclaration.prototype.getFragmentOutputCount = function() {
    245        return this.m_fragmentOutputs.length;
    246    };
    247 
    248    /**
    249     * @constructor
    250     * @param {sglrShaderProgram.ShaderProgramDeclaration} decl
    251     */
    252    sglrShaderProgram.ShaderProgram = function(decl) {
    253        /** @type {rrShaders.VertexShader} */ this.vertexShader = new rrShaders.VertexShader(decl.getVertexInputCount(), decl.getVertexOutputCount());
    254        /** @type {rrShaders.FragmentShader} */ this.fragmentShader = new rrShaders.FragmentShader(decl.getFragmentInputCount(), decl.getFragmentOutputCount());
    255 
    256        /** @type {Array<string>} */ this.m_attributeNames = [];
    257        /** @type {Array<sglrShaderProgram.Uniform>} */ this.m_uniforms = [];
    258        /** @type {string} */ this.m_vertSrc = decl.m_vertexSource;
    259        /** @type {string} */ this.m_fragSrc = decl.m_fragmentSource;
    260 
    261        DE_ASSERT(decl.valid());
    262 
    263        // Set up shader IO
    264 
    265        for (var ndx = 0; ndx < decl.m_vertexAttributes.length; ++ndx) {
    266            this.vertexShader.m_inputs[ndx].type = decl.m_vertexAttributes[ndx].type;
    267            this.m_attributeNames[ndx] = decl.m_vertexAttributes[ndx].name;
    268        }
    269 
    270        for (var ndx = 0; ndx < decl.m_vertexToFragmentVaryings.length; ++ndx) {
    271            this.vertexShader.m_outputs[ndx].type = decl.m_vertexToFragmentVaryings[ndx].type;
    272            this.vertexShader.m_outputs[ndx].flatshade = decl.m_vertexToFragmentVaryings[ndx].flatshade;
    273 
    274            this.fragmentShader.m_inputs[ndx] = this.vertexShader.m_outputs[ndx];
    275        }
    276 
    277        for (var ndx = 0; ndx < decl.m_fragmentOutputs.length; ++ndx)
    278            this.fragmentShader.m_outputs[ndx].type = decl.m_fragmentOutputs[ndx].type;
    279 
    280        // Set up uniforms
    281 
    282        for (var ndx = 0; ndx < decl.m_uniforms.length; ++ndx)
    283            this.m_uniforms[ndx] = new sglrShaderProgram.Uniform(decl.m_uniforms[ndx].name, decl.m_uniforms[ndx].type);
    284    };
    285 
    286    /**
    287     * @return {rrShaders.VertexShader}
    288     */
    289    sglrShaderProgram.ShaderProgram.prototype.getVertexShader = function() {
    290        return this.vertexShader;
    291    };
    292 
    293    /**
    294     * @return {rrShaders.FragmentShader}
    295     */
    296    sglrShaderProgram.ShaderProgram.prototype.getFragmentShader = function() {
    297        return this.fragmentShader;
    298    };
    299 
    300    /**
    301     * @param {string} name
    302     * @return {sglrShaderProgram.Uniform}
    303     * @throws {Error}
    304     */
    305    sglrShaderProgram.ShaderProgram.prototype.getUniformByName = function(name) {
    306        DE_ASSERT(name);
    307 
    308        for (var ndx = 0; ndx < this.m_uniforms.length; ++ndx)
    309            if (this.m_uniforms[ndx].name == name)
    310                return this.m_uniforms[ndx];
    311 
    312        throw new Error('Invalid uniform name, uniform not found.');
    313    };
    314 
    315    /**
    316     * shadeFragments - abstract function, to be implemented by children classes
    317     * @param {Array<rrFragmentOperations.Fragment>} packets
    318     * @param {rrShadingContext.FragmentShadingContext} context
    319     * @throws {Error}
    320     */
    321    sglrShaderProgram.ShaderProgram.prototype.shadeFragments = function(packets, context) {
    322        throw new Error('This function needs to be overwritten in a child class.');
    323    };
    324 
    325    /**
    326     * shadeVertices - abstract function, to be implemented by children classes
    327     * @param {Array<rrVertexAttrib.VertexAttrib>} inputs
    328     * @param {Array<rrVertexPacket.VertexPacket>} packets
    329     * @param {number} numPackets
    330     * @throws {Error}
    331     */
    332     sglrShaderProgram.ShaderProgram.prototype.shadeVertices = function(inputs, packets, numPackets) {
    333        throw new Error('This function needs to be overwritten in a child class.');
    334     };
    335 
    336 });