tor-browser

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

glsSamplerObjectTest.js (52629B)


      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('modules.shared.glsSamplerObjectTest');
     23 goog.require('framework.common.tcuImageCompare');
     24 goog.require('framework.common.tcuSurface');
     25 goog.require('framework.common.tcuTestCase');
     26 goog.require('framework.common.tcuTexture');
     27 goog.require('framework.common.tcuTextureUtil');
     28 goog.require('framework.delibs.debase.deRandom');
     29 goog.require('framework.delibs.debase.deString');
     30 goog.require('framework.opengl.gluDrawUtil');
     31 goog.require('framework.opengl.gluShaderProgram');
     32 goog.require('framework.opengl.gluTextureUtil');
     33 
     34 goog.scope(function() {
     35 
     36 var glsSamplerObjectTest = modules.shared.glsSamplerObjectTest;
     37 var tcuTestCase = framework.common.tcuTestCase;
     38 var deRandom = framework.delibs.debase.deRandom;
     39 var gluShaderProgram = framework.opengl.gluShaderProgram;
     40 var tcuTexture = framework.common.tcuTexture;
     41 var tcuSurface = framework.common.tcuSurface;
     42 var tcuTextureUtil = framework.common.tcuTextureUtil;
     43 var tcuImageCompare = framework.common.tcuImageCompare;
     44 var gluDrawUtil = framework.opengl.gluDrawUtil;
     45 var gluTextureUtil = framework.opengl.gluTextureUtil;
     46 var deString = framework.delibs.debase.deString;
     47 
     48    var DE_ASSERT = function(expression) {
     49        if (!expression) throw new Error('Assert failed');
     50    };
     51 
     52    // glsSamplerObjectTest.TextureSamplerTest
     53 
     54    /** @const @type {number} */ glsSamplerObjectTest.VIEWPORT_WIDTH = 128;
     55    /** @const @type {number} */ glsSamplerObjectTest.VIEWPORT_HEIGHT = 128;
     56 
     57    /** @const @type {number} */ glsSamplerObjectTest.TEXTURE2D_WIDTH = 32;
     58    /** @const @type {number} */ glsSamplerObjectTest.TEXTURE2D_HEIGHT = 32;
     59 
     60    /** @const @type {number} */ glsSamplerObjectTest.TEXTURE3D_WIDTH = 32;
     61    /** @const @type {number} */ glsSamplerObjectTest.TEXTURE3D_HEIGHT = 32;
     62    /** @const @type {number} */ glsSamplerObjectTest.TEXTURE3D_DEPTH = 32;
     63 
     64    /** @const @type {number} */ glsSamplerObjectTest.CUBEMAP_SIZE = 32;
     65 
     66    /** @const @type {Array<number>} */ glsSamplerObjectTest.s_positions = [
     67        -1.0, -1.0,
     68         1.0, -1.0,
     69         1.0, 1.0,
     70         1.0, 1.0,
     71        -1.0, 1.0,
     72        -1.0, -1.0
     73    ];
     74 
     75    /** @const @type {Array<number>} */ glsSamplerObjectTest.s_positions3D = [
     76        -1.0, -1.0, -1.0,
     77         1.0, -1.0, 1.0,
     78         1.0, 1.0, -1.0,
     79         1.0, 1.0, -1.0,
     80        -1.0, 1.0, 1.0,
     81        -1.0, -1.0, -1.0
     82    ];
     83 
     84    /** @const @type {Array<number>} */ glsSamplerObjectTest.s_positionsCube = [
     85        -1.0, -1.0, -1.0, -0.5,
     86         1.0, -1.0, 1.0, -0.5,
     87         1.0, 1.0, 1.0, 0.5,
     88         1.0, 1.0, 1.0, 0.5,
     89        -1.0, 1.0, -1.0, 0.5,
     90        -1.0, -1.0, -1.0, -0.5
     91    ];
     92 
     93    /**
     94     * @struct
     95     * @constructor
     96     */
     97    glsSamplerObjectTest.SamplingState = function(minFilter, magFilter, wrapT, wrapS, wrapR, minLod, maxLod) {
     98        /** @type {number} */ this.minFilter = minFilter;
     99        /** @type {number} */ this.magFilter = magFilter;
    100        /** @type {number} */ this.wrapT = wrapT;
    101        /** @type {number} */ this.wrapS = wrapS;
    102        /** @type {number} */ this.wrapR = wrapR;
    103        /** @type {number} */ this.minLod = minLod;
    104        /** @type {number} */ this.maxLod = maxLod;
    105    };
    106 
    107    /**
    108     * @struct
    109     * @param {string} name
    110     * @param {string} desc
    111     * @param {number} target
    112     * @param {glsSamplerObjectTest.SamplingState} state1
    113     * @param {glsSamplerObjectTest.SamplingState} state2
    114     * @param {glsSamplerObjectTest.SamplingState=} state3
    115     * @constructor
    116     */
    117    glsSamplerObjectTest.TestSpec = function(name, desc, target, state1, state2, state3) {
    118        /** @type {string} */ this.name = name;
    119        /** @type {string} */ this.desc = desc;
    120        /** @type {number} */ this.target = target;
    121        /** @type {glsSamplerObjectTest.SamplingState} */ this.textureState = state1;
    122        /** @type {glsSamplerObjectTest.SamplingState} */ this.textureState2 = state3 !== undefined ? state2 : null; // merging TST and MTST structs
    123        /** @type {glsSamplerObjectTest.SamplingState} */ this.samplerState = state3 !== undefined ? state3 : state2;
    124    };
    125 
    126    /**
    127     * @constructor
    128     * @extends {tcuTestCase.DeqpTest}
    129     * @const @param {glsSamplerObjectTest.TestSpec} spec
    130     */
    131    glsSamplerObjectTest.TextureSamplerTest = function(spec) {
    132        tcuTestCase.DeqpTest.call(this, spec.name, spec.desc);
    133        /** @type {gluShaderProgram.ShaderProgram} */ this.m_program = null;
    134        /** @type {number} */ this.m_target = spec.target;
    135        /** @type {glsSamplerObjectTest.SamplingState} */ this.m_textureState = spec.textureState;
    136        /** @type {glsSamplerObjectTest.SamplingState} */ this.m_samplerState = spec.samplerState;
    137        /** @type {deRandom.Random} */ this.m_random = new deRandom.Random(deString.deStringHash(spec.name));
    138    };
    139 
    140    glsSamplerObjectTest.TextureSamplerTest.prototype = Object.create(tcuTestCase.DeqpTest.prototype);
    141    glsSamplerObjectTest.TextureSamplerTest.prototype.constructor = glsSamplerObjectTest.TextureSamplerTest;
    142 
    143    /**
    144     * @private
    145     * @param {tcuSurface.Surface} textureRef
    146     * @param {tcuSurface.Surface} samplerRef
    147     * @param {number} x
    148     * @param {number} y
    149     */
    150    glsSamplerObjectTest.TextureSamplerTest.prototype.renderReferences = function(textureRef, samplerRef, x, y) {
    151        /** @type {WebGLTexture} */ var texture = glsSamplerObjectTest.TextureSamplerTest.createTexture(this.m_target);
    152 
    153        gl.viewport(x, y, glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    154 
    155        gl.bindTexture(this.m_target, texture);
    156 
    157        glsSamplerObjectTest.TextureSamplerTest.setTextureState(this.m_target, this.m_textureState);
    158        this.render();
    159        var texRef = textureRef.getAccess();
    160        var texRefTransferFormat = gluTextureUtil.getTransferFormat(texRef.getFormat());
    161        gl.readPixels(x, y, texRef.m_width, texRef.m_height, texRefTransferFormat.format, texRefTransferFormat.dataType, textureRef.m_pixels);
    162 
    163        glsSamplerObjectTest.TextureSamplerTest.setTextureState(this.m_target, this.m_samplerState);
    164        this.render();
    165        var sampRef = samplerRef.getAccess();
    166        var sampRefTransferFormat = gluTextureUtil.getTransferFormat(sampRef.getFormat());
    167        gl.readPixels(x, y, sampRef.m_width, sampRef.m_height, sampRefTransferFormat.format, sampRefTransferFormat.dataType, samplerRef.m_pixels);
    168 
    169        gl.deleteTexture(texture);
    170    };
    171 
    172    /**
    173     * @private
    174     * @param {tcuSurface.Surface} textureResult
    175     * @param {tcuSurface.Surface} samplerResult
    176     * @param {number} x
    177     * @param {number} y
    178     */
    179    glsSamplerObjectTest.TextureSamplerTest.prototype.renderResults = function(textureResult, samplerResult, x, y) {
    180        /** @type {WebGLTexture} */ var texture = glsSamplerObjectTest.TextureSamplerTest.createTexture(this.m_target);
    181 
    182        gl.viewport(x, y, glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    183 
    184        var sampler = gl.createSampler();
    185        DE_ASSERT(sampler != -1);
    186 
    187        gl.bindSampler(0, sampler);
    188 
    189        // First set sampler state
    190        glsSamplerObjectTest.TextureSamplerTest.setSamplerState(this.m_samplerState, sampler);
    191 
    192        // Set texture state
    193        gl.bindTexture(this.m_target, texture);
    194 
    195        glsSamplerObjectTest.TextureSamplerTest.setTextureState(this.m_target, this.m_textureState);
    196        // Render using sampler
    197        this.render();
    198        var sampRes = samplerResult.getAccess();
    199        var sampResTransferFormat = gluTextureUtil.getTransferFormat(sampRes.getFormat());
    200        gl.readPixels(x, y, sampRes.m_width, sampRes.m_height, sampResTransferFormat.format, sampResTransferFormat.dataType, samplerResult.m_pixels);
    201 
    202        // Render without sampler
    203        gl.bindSampler(0, null);
    204        gl.deleteSampler(sampler);
    205 
    206        this.render();
    207        var texRes = textureResult.getAccess();
    208        var texResTransferFormat = gluTextureUtil.getTransferFormat(texRes.getFormat());
    209        gl.readPixels(x, y, texRes.m_width, texRes.m_height, texResTransferFormat.format, texResTransferFormat.dataType, textureResult.m_pixels);
    210 
    211        gl.deleteSampler(sampler);
    212        gl.deleteTexture(texture);
    213    };
    214 
    215    /**
    216     * @private
    217     */
    218    glsSamplerObjectTest.TextureSamplerTest.prototype.render = function() {
    219        /** @type {WebGLUniformLocation} */ var samplerLoc;
    220        /** @type {WebGLUniformLocation} */ var scaleLoc;
    221 
    222        gl.useProgram(this.m_program.getProgram());
    223 
    224        samplerLoc = gl.getUniformLocation(this.m_program.getProgram(), 'u_sampler');
    225        DE_ASSERT(samplerLoc != null);
    226 
    227        scaleLoc = gl.getUniformLocation(this.m_program.getProgram(), 'u_posScale');
    228        DE_ASSERT(scaleLoc != null);
    229 
    230        gl.clearColor(0.5, 0.5, 0.5, 1.0);
    231 
    232        gl.clear(gl.COLOR_BUFFER_BIT);
    233 
    234        gl.uniform1i(samplerLoc, 0);
    235 
    236        gl.uniform1f(scaleLoc, 1.0);
    237 
    238        /** @type {Array<gluDrawUtil.VertexArrayBinding>} */ var vertexArrays;
    239        switch (this.m_target) {
    240            case gl.TEXTURE_2D: {
    241                vertexArrays = [
    242                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    243                        gluDrawUtil.bindingPointFromName('a_position'),
    244                        new gluDrawUtil.VertexArrayPointer(
    245                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    246                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    247                            2,
    248                            6,
    249                            0,
    250                            glsSamplerObjectTest.s_positions))
    251                ];
    252 
    253                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    254 
    255                gl.uniform1f(scaleLoc, 0.25);
    256 
    257                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    258 
    259                break;
    260            }
    261 
    262            case gl.TEXTURE_3D: {
    263                vertexArrays = [
    264                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    265                        gluDrawUtil.bindingPointFromName('a_position'),
    266                        new gluDrawUtil.VertexArrayPointer(
    267                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    268                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    269                            3,
    270                            6,
    271                            0,
    272                            glsSamplerObjectTest.s_positions3D))
    273                ];
    274 
    275                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    276 
    277                gl.uniform1f(scaleLoc, 0.25);
    278 
    279                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    280 
    281                break;
    282            }
    283 
    284            case gl.TEXTURE_CUBE_MAP: {
    285                vertexArrays = [
    286                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    287                        gluDrawUtil.bindingPointFromName('a_position'),
    288                        new gluDrawUtil.VertexArrayPointer(
    289                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    290                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    291                            4,
    292                            6,
    293                            0,
    294                            glsSamplerObjectTest.s_positionsCube))
    295                ];
    296 
    297                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    298 
    299                gl.uniform1f(scaleLoc, 0.25);
    300 
    301                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    302 
    303                break;
    304            }
    305 
    306            default:
    307                DE_ASSERT(false);
    308        }
    309    };
    310 
    311    /**
    312     * @private
    313     * @param {number} target
    314     * @param {glsSamplerObjectTest.SamplingState} state
    315     */
    316    glsSamplerObjectTest.TextureSamplerTest.setTextureState = function(target, state) {
    317        gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, state.minFilter);
    318        gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, state.magFilter);
    319        gl.texParameteri(target, gl.TEXTURE_WRAP_S, state.wrapS);
    320        gl.texParameteri(target, gl.TEXTURE_WRAP_T, state.wrapT);
    321        gl.texParameteri(target, gl.TEXTURE_WRAP_R, state.wrapR);
    322        gl.texParameterf(target, gl.TEXTURE_MAX_LOD, state.maxLod);
    323        gl.texParameterf(target, gl.TEXTURE_MIN_LOD, state.minLod);
    324    };
    325 
    326    /**
    327     * @private
    328     * @param {glsSamplerObjectTest.SamplingState} state
    329     * @param {WebGLSampler} sampler
    330     */
    331    glsSamplerObjectTest.TextureSamplerTest.setSamplerState = function(state, sampler) {
    332        gl.samplerParameteri(sampler, gl.TEXTURE_MIN_FILTER, state.minFilter);
    333        gl.samplerParameteri(sampler, gl.TEXTURE_MAG_FILTER, state.magFilter);
    334        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_S, state.wrapS);
    335        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_T, state.wrapT);
    336        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_R, state.wrapR);
    337        gl.samplerParameterf(sampler, gl.TEXTURE_MAX_LOD, state.maxLod);
    338        gl.samplerParameterf(sampler, gl.TEXTURE_MIN_LOD, state.minLod);
    339    };
    340 
    341    /**
    342     * @private
    343     * @return {WebGLTexture}
    344     */
    345    glsSamplerObjectTest.TextureSamplerTest.createTexture2D = function() {
    346        /** @type {WebGLTexture} */ var texture = null;
    347        /** @type {tcuTexture.Texture2D} */ var refTexture = new tcuTexture.Texture2D(
    348                                                                new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    349                                                                                             tcuTexture.ChannelType.UNORM_INT8),
    350                                                                glsSamplerObjectTest.TEXTURE2D_WIDTH,
    351                                                                glsSamplerObjectTest.TEXTURE2D_HEIGHT);
    352 
    353        refTexture.allocLevel(0);
    354        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    355 
    356        texture = gl.createTexture();
    357 
    358        gl.bindTexture(gl.TEXTURE_2D, texture);
    359 
    360        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, refTexture.getWidth(), refTexture.getHeight(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
    361 
    362        gl.generateMipmap(gl.TEXTURE_2D);
    363 
    364        gl.bindTexture(gl.TEXTURE_2D, null);
    365 
    366        return texture;
    367    };
    368 
    369    /**
    370     * @private
    371     * @return {WebGLTexture}
    372     */
    373    glsSamplerObjectTest.TextureSamplerTest.createTexture3D = function() {
    374        /** @type {WebGLTexture} */ var texture = null;
    375        /** @type {tcuTexture.Texture3D} */ var refTexture = new tcuTexture.Texture3D(
    376                                                                 new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    377                                                                                              tcuTexture.ChannelType.UNORM_INT8),
    378                                                                 glsSamplerObjectTest.TEXTURE3D_WIDTH,
    379                                                                 glsSamplerObjectTest.TEXTURE3D_HEIGHT,
    380                                                                 glsSamplerObjectTest.TEXTURE3D_DEPTH);
    381 
    382        refTexture.allocLevel(0);
    383        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    384 
    385        texture = gl.createTexture();
    386 
    387        gl.bindTexture(gl.TEXTURE_3D, texture);
    388 
    389        gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
    390 
    391        gl.generateMipmap(gl.TEXTURE_3D);
    392 
    393        gl.bindTexture(gl.TEXTURE_3D, null);
    394 
    395        return texture;
    396    };
    397 
    398    /**
    399     * @private
    400     * @return {WebGLTexture}
    401     */
    402    glsSamplerObjectTest.TextureSamplerTest.createTextureCube = function() {
    403        /** @type {WebGLTexture} */ var texture = null;
    404        /** @type {tcuTexture.TextureCube} */ var refTexture = new tcuTexture.TextureCube(
    405                                                                  new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    406                                                                                               tcuTexture.ChannelType.UNORM_INT8),
    407                                                                  glsSamplerObjectTest.CUBEMAP_SIZE);
    408 
    409        texture = gl.createTexture();
    410 
    411        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_X, 0);
    412        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y, 0);
    413        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z, 0);
    414        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X, 0);
    415        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y, 0);
    416        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z, 0);
    417 
    418        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_X), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    419        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    420        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    421        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    422        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    423        tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]);
    424 
    425        gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
    426        // TODO: check internalFormat / format parameters in texImage2D (were RGBA8 and RGBA respectively)
    427        for (var face in tcuTexture.CubeFace) {
    428            /** @const @type {number} */ var target = gluTextureUtil.getGLCubeFace(tcuTexture.CubeFace[face]);
    429            gl.texImage2D(target, 0, gl.RGBA, refTexture.getSize(), refTexture.getSize(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevelFace(0, tcuTexture.CubeFace[face]).getDataPtr());
    430        }
    431 
    432        gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
    433        gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
    434 
    435        return texture;
    436    };
    437 
    438    /**
    439     * @private
    440     * @param {number} target
    441     * @return {WebGLTexture}
    442     */
    443    glsSamplerObjectTest.TextureSamplerTest.createTexture = function(target) {
    444        /** @type {WebGLTexture} */ var texture;
    445        switch (target) {
    446            case gl.TEXTURE_2D:
    447                texture = glsSamplerObjectTest.TextureSamplerTest.createTexture2D();
    448                break;
    449 
    450            case gl.TEXTURE_3D:
    451                texture = glsSamplerObjectTest.TextureSamplerTest.createTexture3D();
    452                break;
    453 
    454            case gl.TEXTURE_CUBE_MAP:
    455                texture = glsSamplerObjectTest.TextureSamplerTest.createTextureCube();
    456                break;
    457 
    458            default:
    459                throw new Error('Unsupported target: ' + WebGLTestUtils.glEnumToString(gl, target));
    460        }
    461 
    462        return texture;
    463    };
    464 
    465    /**
    466     * @private
    467     * @param {number} target
    468     * @return {string}
    469     */
    470     glsSamplerObjectTest.TextureSamplerTest.selectVertexShader = function(target) {
    471         switch (target) {
    472             case gl.TEXTURE_2D:
    473                 return '#version 300 es\n' +
    474                 'in highp vec2 a_position;\n' +
    475                 'uniform highp float u_posScale;\n' +
    476                 'out mediump vec2 v_texCoord;\n' +
    477                 'void main (void)\n' +
    478                 ' {\n' +
    479                 '\tv_texCoord = a_position;\n' +
    480                 '\tgl_Position = vec4(u_posScale * a_position, 0.0, 1.0);\n' +
    481                 '}';
    482 
    483             case gl.TEXTURE_3D:
    484                 return '#version 300 es\n' +
    485                 'in highp vec3 a_position;\n' +
    486                 'uniform highp float u_posScale;\n' +
    487                 'out mediump vec3 v_texCoord;\n' +
    488                 'void main (void)\n' +
    489                 ' {\n' +
    490                 '\tv_texCoord = a_position;\n' +
    491                 '\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n' +
    492                 '}';
    493 
    494             case gl.TEXTURE_CUBE_MAP:
    495                 return '#version 300 es\n' +
    496                 'in highp vec4 a_position;\n' +
    497                 'uniform highp float u_posScale;\n' +
    498                 'out mediump vec2 v_texCoord;\n' +
    499                 'void main (void)\n' +
    500                 ' {\n' +
    501                 '\tv_texCoord = a_position.zw;\n' +
    502                 '\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n' +
    503                 '}';
    504 
    505             default:
    506                 DE_ASSERT(false);
    507                 return '';
    508         }
    509     };
    510 
    511     /**
    512      * @private
    513      * @param {number} target
    514      * @return {string}
    515      */
    516     glsSamplerObjectTest.TextureSamplerTest.selectFragmentShader = function(target) {
    517         switch (target) {
    518             case gl.TEXTURE_2D:
    519                 return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
    520                 'uniform lowp sampler2D u_sampler;\n' +
    521                 'in mediump vec2 v_texCoord;\n' +
    522                 'void main (void)\n' +
    523                 ' {\n' +
    524                 '\to_color = texture(u_sampler, v_texCoord);\n' +
    525                 '}';
    526 
    527             case gl.TEXTURE_3D:
    528                 return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
    529                 'uniform lowp sampler3D u_sampler;\n' +
    530                 'in mediump vec3 v_texCoord;\n' +
    531                 'void main (void)\n' +
    532                 ' {\n' +
    533                 '\to_color = texture(u_sampler, v_texCoord);\n' +
    534                 '}';
    535 
    536             case gl.TEXTURE_CUBE_MAP:
    537                 return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
    538                 'uniform lowp samplerCube u_sampler;\n' +
    539                 'in mediump vec2 v_texCoord;\n' +
    540                 'void main (void)\n' +
    541                 ' {\n' +
    542                 '\to_color = texture(u_sampler, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x)));\n' +
    543                 '}';
    544 
    545             default:
    546                 DE_ASSERT(false);
    547                 return '';
    548         }
    549     };
    550 
    551    glsSamplerObjectTest.TextureSamplerTest.prototype.init = function() {
    552        /** @const @type {string} */ var vertexShaderTemplate = glsSamplerObjectTest.TextureSamplerTest.selectVertexShader(this.m_target);
    553        /** @const @type {string} */ var fragmentShaderTemplate = glsSamplerObjectTest.TextureSamplerTest.selectFragmentShader(this.m_target);
    554 
    555        DE_ASSERT(!this.m_program);
    556        this.m_program = new gluShaderProgram.ShaderProgram(gl,
    557                                                       gluShaderProgram.makeVtxFragSources(
    558                                                           vertexShaderTemplate,
    559                                                           fragmentShaderTemplate));
    560 
    561        if (!this.m_program.isOk()) {
    562            // tcu::TestLog& log = m_testCtx.getLog();
    563            // log << *m_program;
    564            throw new Error('Failed to compile shaders');
    565        }
    566    };
    567 
    568    glsSamplerObjectTest.TextureSamplerTest.prototype.iterate = function() {
    569        //tcu::TestLog& log = m_testCtx.getLog();
    570 
    571        /** @type {tcuSurface.Surface} */ var textureRef = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    572        /** @type {tcuSurface.Surface} */ var samplerRef = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    573 
    574        /** @type {tcuSurface.Surface} */ var textureResult = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    575        /** @type {tcuSurface.Surface} */ var samplerResult = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    576 
    577        /** @type {number} */ var x = this.m_random.getInt(0, gl.drawingBufferWidth - glsSamplerObjectTest.VIEWPORT_WIDTH);
    578        /** @type {number} */ var y = this.m_random.getInt(0, gl.drawingBufferHeight - glsSamplerObjectTest.VIEWPORT_HEIGHT);
    579 
    580        this.renderReferences(textureRef, samplerRef, x, y);
    581        this.renderResults(textureResult, samplerResult, x, y);
    582 
    583        /** @type {boolean} */ var isOk = tcuImageCompare.pixelThresholdCompare('Sampler render result', 'Result from rendering with sampler', samplerRef, samplerResult, [0, 0, 0, 0]);
    584 
    585        if (!tcuImageCompare.pixelThresholdCompare('Texture render result', 'Result from rendering with texture state', textureRef, textureResult, [0, 0, 0, 0]))
    586            isOk = false;
    587 
    588        assertMsgOptions(isOk, '', true, false);
    589 
    590        return tcuTestCase.IterateResult.STOP;
    591    };
    592 
    593    // glsSamplerObjectTest.MultiTextureSamplerTest
    594 
    595    /**
    596     * @constructor
    597     * @extends {tcuTestCase.DeqpTest}
    598     * @const @param {glsSamplerObjectTest.TestSpec} spec
    599     */
    600    glsSamplerObjectTest.MultiTextureSamplerTest = function(spec) {
    601        tcuTestCase.DeqpTest.call(this, spec.name, spec.desc);
    602        /** @type {gluShaderProgram.ShaderProgram} */ this.m_program = null;
    603        /** @type {number} */ this.m_target = spec.target;
    604        /** @type {glsSamplerObjectTest.SamplingState} */ this.m_textureState1 = spec.textureState;
    605        /** @type {glsSamplerObjectTest.SamplingState} */ this.m_textureState2 = spec.textureState2;
    606        /** @type {glsSamplerObjectTest.SamplingState} */ this.m_samplerState = spec.samplerState;
    607        /** @type {deRandom.Random} */ this.m_random = new deRandom.Random(deString.deStringHash(spec.name));
    608    };
    609 
    610    glsSamplerObjectTest.MultiTextureSamplerTest.prototype = Object.create(tcuTestCase.DeqpTest.prototype);
    611    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.constructor = glsSamplerObjectTest.MultiTextureSamplerTest;
    612 
    613    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.init = function() {
    614        /** @type {string} */ var vertexShaderTemplate = glsSamplerObjectTest.MultiTextureSamplerTest.selectVertexShader(this.m_target);
    615        /** @type {string} */ var fragmentShaderTemplate = glsSamplerObjectTest.MultiTextureSamplerTest.selectFragmentShader(this.m_target);
    616 
    617        DE_ASSERT(!this.m_program);
    618        this.m_program = new gluShaderProgram.ShaderProgram(gl,
    619                                                            gluShaderProgram.makeVtxFragSources(
    620                                                                vertexShaderTemplate,
    621                                                                fragmentShaderTemplate));
    622        if (!this.m_program.isOk()) {
    623            // tcu::TestLog& log = m_testCtx.getLog();
    624            //
    625            // log << *m_program;
    626            throw new Error('Failed to compile shaders');
    627        }
    628    };
    629 
    630    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.deinit = function() {
    631        gl.activeTexture(gl.TEXTURE0);
    632    }
    633 
    634    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.iterate = function() {
    635        //tcu::TestLog& log = m_testCtx.getLog();
    636 
    637        /** @type {tcuSurface.Surface} */ var textureRef = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    638        /** @type {tcuSurface.Surface} */ var samplerRef = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    639 
    640        /** @type {tcuSurface.Surface} */ var textureResult = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    641        /** @type {tcuSurface.Surface} */ var samplerResult = new tcuSurface.Surface(glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    642 
    643        /** @type {number} */ var x = this.m_random.getInt(0, gl.drawingBufferWidth - glsSamplerObjectTest.VIEWPORT_WIDTH);
    644        /** @type {number} */ var y = this.m_random.getInt(0, gl.drawingBufferHeight - glsSamplerObjectTest.VIEWPORT_HEIGHT);
    645 
    646        this.renderReferences(textureRef, samplerRef, x, y);
    647        this.renderResults(textureResult, samplerResult, x, y);
    648 
    649        /** @type {boolean} */ var isOk = tcuImageCompare.pixelThresholdCompare('Sampler render result', 'Result from rendering with sampler', samplerRef, samplerResult, [0, 0, 0, 0]);
    650 
    651        if (!tcuImageCompare.pixelThresholdCompare('Texture render result', 'Result from rendering with texture state', textureRef, textureResult, [0, 0, 0, 0]))
    652            isOk = false;
    653 
    654        assertMsgOptions(isOk, '', true, false);
    655 
    656        return tcuTestCase.IterateResult.STOP;
    657    };
    658 
    659    /**
    660     * @private
    661     * @param {tcuSurface.Surface} textureRef
    662     * @param {tcuSurface.Surface} samplerRef
    663     * @param {number} x
    664     * @param {number} y
    665     */
    666    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.renderReferences = function(textureRef, samplerRef, x, y) {
    667        /** @type {WebGLTexture} */ var texture1 = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture(this.m_target, 0);
    668        /** @type {WebGLTexture} */ var texture2 = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture(this.m_target, 1);
    669 
    670        gl.viewport(x, y, glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    671 
    672        // Generate texture rendering reference
    673        gl.activeTexture(gl.TEXTURE0);
    674        gl.bindTexture(this.m_target, texture1);
    675        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this.m_target, this.m_textureState1);
    676 
    677        gl.activeTexture(gl.TEXTURE1);
    678        gl.bindTexture(this.m_target, texture2);
    679        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this.m_target, this.m_textureState2);
    680 
    681        this.render();
    682        var texRef = textureRef.getAccess();
    683        var texRefTransferFormat = gluTextureUtil.getTransferFormat(texRef.getFormat());
    684        gl.readPixels(x, y, texRef.m_width, texRef.m_height, texRefTransferFormat.format, texRefTransferFormat.dataType, textureRef.m_pixels);
    685 
    686        // Generate sampler rendering reference
    687        gl.activeTexture(gl.TEXTURE0);
    688        gl.bindTexture(this.m_target, texture1);
    689        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this.m_target, this.m_samplerState);
    690 
    691        gl.activeTexture(gl.TEXTURE1);
    692        gl.bindTexture(this.m_target, texture2);
    693        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this.m_target, this.m_samplerState);
    694 
    695        this.render();
    696        var sampRef = samplerRef.getAccess();
    697        var sampRefTransferFormat = gluTextureUtil.getTransferFormat(sampRef.getFormat());
    698        gl.readPixels(x, y, sampRef.m_width, sampRef.m_height, sampRefTransferFormat.format, sampRefTransferFormat.dataType, samplerRef.m_pixels);
    699    };
    700 
    701    /**
    702     * @private
    703     * @param {tcuSurface.Surface} textureResult
    704     * @param {tcuSurface.Surface} samplerResult
    705     * @param {number} x
    706     * @param {number} y
    707     */
    708    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.renderResults = function(textureResult, samplerResult, x, y) {
    709        /** @type {WebGLTexture} */ var texture1 = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture(this.m_target, 0);
    710        /** @type {WebGLTexture} */ var texture2 = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture(this.m_target, 1);
    711 
    712        gl.viewport(x, y, glsSamplerObjectTest.VIEWPORT_WIDTH, glsSamplerObjectTest.VIEWPORT_HEIGHT);
    713 
    714        /** @type {WebGLSampler} */ var sampler = gl.createSampler();
    715        DE_ASSERT(sampler != -1);
    716 
    717        gl.bindSampler(0, sampler);
    718        gl.bindSampler(1, sampler);
    719 
    720        // First set sampler state
    721        glsSamplerObjectTest.MultiTextureSamplerTest.setSamplerState(this.m_samplerState, sampler);
    722 
    723        // Set texture state
    724        gl.bindTexture(this.m_target, texture1);
    725        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this.m_target, this.m_textureState1);
    726 
    727        gl.bindTexture(this.m_target, texture2);
    728        glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState(this. m_target, this.m_textureState2);
    729 
    730        gl.activeTexture(gl.TEXTURE0);
    731        gl.bindTexture(this.m_target, texture1);
    732 
    733        gl.activeTexture(gl.TEXTURE1);
    734        gl.bindTexture(this.m_target, texture2);
    735 
    736        // Render using sampler
    737        this.render();
    738        var sampRes = samplerResult.getAccess();
    739        var sampResTransferFormat = gluTextureUtil.getTransferFormat(sampRes.getFormat());
    740        gl.readPixels(x, y, sampRes.m_width, sampRes.m_height, sampResTransferFormat.format, sampResTransferFormat.dataType, samplerResult.m_pixels);
    741 
    742        gl.bindSampler(0, null);
    743        gl.bindSampler(1, null);
    744 
    745        this.render();
    746        var texRes = textureResult.getAccess();
    747        var texResTransferFormat = gluTextureUtil.getTransferFormat(texRes.getFormat());
    748        gl.readPixels(x, y, texRes.m_width, texRes.m_height, texResTransferFormat.format, texResTransferFormat.dataType, textureResult.m_pixels);
    749 
    750        gl.activeTexture(gl.TEXTURE0);
    751        gl.bindTexture(this.m_target, null);
    752 
    753        gl.activeTexture(gl.TEXTURE1);
    754        gl.bindTexture(this.m_target, null);
    755 
    756        gl.deleteSampler(sampler);
    757        gl.deleteTexture(texture1);
    758        gl.deleteTexture(texture2);
    759    };
    760 
    761    glsSamplerObjectTest.MultiTextureSamplerTest.prototype.render = function() {
    762 
    763        gl.useProgram(this.m_program.getProgram());
    764 
    765        /** @type {WebGLUniformLocation} */ var samplerLoc1 = gl.getUniformLocation(this.m_program.getProgram(), 'u_sampler1');
    766        DE_ASSERT(samplerLoc1 != -1);
    767 
    768        /** @type {WebGLUniformLocation} */ var samplerLoc2 = gl.getUniformLocation(this.m_program.getProgram(), 'u_sampler2');
    769        DE_ASSERT(samplerLoc2 != -1);
    770 
    771        /** @type {WebGLUniformLocation} */ var scaleLoc = gl.getUniformLocation(this.m_program.getProgram(), 'u_posScale');
    772        DE_ASSERT(scaleLoc != -1);
    773 
    774        gl.clearColor(0.5, 0.5, 0.5, 1.0);
    775 
    776        gl.clear(gl.COLOR_BUFFER_BIT);
    777 
    778        gl.uniform1i(samplerLoc1, 0);
    779 
    780        gl.uniform1i(samplerLoc2, 1);
    781 
    782        gl.uniform1f(scaleLoc, 1.0);
    783 
    784        /** @type {Array<gluDrawUtil.VertexArrayBinding>} */ var vertexArrays;
    785        switch (this.m_target) {
    786            case gl.TEXTURE_2D: {
    787                vertexArrays = [
    788                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    789                        gluDrawUtil.bindingPointFromName('a_position'),
    790                        new gluDrawUtil.VertexArrayPointer(
    791                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    792                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    793                            2,
    794                            6,
    795                            0,
    796                            glsSamplerObjectTest.s_positions))
    797                ];
    798 
    799                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    800 
    801                gl.uniform1f(scaleLoc, 0.25);
    802 
    803                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    804 
    805                break;
    806            }
    807 
    808            case gl.TEXTURE_3D: {
    809                vertexArrays = [
    810                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    811                        gluDrawUtil.bindingPointFromName('a_position'),
    812                        new gluDrawUtil.VertexArrayPointer(
    813                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    814                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    815                            3,
    816                            6,
    817                            0,
    818                            glsSamplerObjectTest.s_positions3D))
    819                ];
    820 
    821                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    822 
    823                gl.uniform1f(scaleLoc, 0.25);
    824 
    825                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    826 
    827                break;
    828            }
    829 
    830            case gl.TEXTURE_CUBE_MAP: {
    831                vertexArrays = [
    832                    gluDrawUtil.vabFromBindingPointAndArrayPointer(
    833                        gluDrawUtil.bindingPointFromName('a_position'),
    834                        new gluDrawUtil.VertexArrayPointer(
    835                            gluDrawUtil.VertexComponentType.VTX_COMP_FLOAT,
    836                            gluDrawUtil.VertexComponentConversion.VTX_COMP_CONVERT_NONE,
    837                            4,
    838                            6,
    839                            0,
    840                            glsSamplerObjectTest.s_positionsCube))
    841                ];
    842 
    843                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    844 
    845                gl.uniform1f(scaleLoc, 0.25);
    846 
    847                gluDrawUtil.draw(gl, this.m_program.getProgram(), vertexArrays, new gluDrawUtil.PrimitiveList(gluDrawUtil.primitiveType.TRIANGLES, 6));
    848 
    849                break;
    850            }
    851 
    852            default:
    853                DE_ASSERT(false);
    854        }
    855 
    856    };
    857 
    858    /**
    859     * @private
    860     * @param {number} target
    861     * @param {glsSamplerObjectTest.SamplingState} state
    862     */
    863    glsSamplerObjectTest.MultiTextureSamplerTest.setTextureState = function(target, state) {
    864        gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, state.minFilter);
    865        gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, state.magFilter);
    866        gl.texParameteri(target, gl.TEXTURE_WRAP_S, state.wrapS);
    867        gl.texParameteri(target, gl.TEXTURE_WRAP_T, state.wrapT);
    868        gl.texParameteri(target, gl.TEXTURE_WRAP_R, state.wrapR);
    869        gl.texParameterf(target, gl.TEXTURE_MAX_LOD, state.maxLod);
    870        gl.texParameterf(target, gl.TEXTURE_MIN_LOD, state.minLod);
    871    };
    872 
    873    /**
    874     * @private
    875     * @param {glsSamplerObjectTest.SamplingState} state
    876     * @param {WebGLSampler} sampler
    877     */
    878    glsSamplerObjectTest.MultiTextureSamplerTest.setSamplerState = function(state, sampler) {
    879        gl.samplerParameteri(sampler, gl.TEXTURE_MIN_FILTER, state.minFilter);
    880        gl.samplerParameteri(sampler, gl.TEXTURE_MAG_FILTER, state.magFilter);
    881        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_S, state.wrapS);
    882        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_T, state.wrapT);
    883        gl.samplerParameteri(sampler, gl.TEXTURE_WRAP_R, state.wrapR);
    884        gl.samplerParameterf(sampler, gl.TEXTURE_MAX_LOD, state.maxLod);
    885        gl.samplerParameterf(sampler, gl.TEXTURE_MIN_LOD, state.minLod);
    886    };
    887 
    888    /**
    889     * @private
    890     * @param {number} id
    891     * @return {WebGLTexture }
    892     */
    893    glsSamplerObjectTest.MultiTextureSamplerTest.createTexture2D = function(id) {
    894        /** @type {WebGLTexture} */ var texture = null;
    895        /** @type {tcuTexture.Texture2D} */ var refTexture = new tcuTexture.Texture2D(
    896                                                                 new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    897                                                                                              tcuTexture.ChannelType.UNORM_INT8),
    898                                                                 glsSamplerObjectTest.TEXTURE2D_WIDTH,
    899                                                                 glsSamplerObjectTest.TEXTURE2D_HEIGHT);
    900 
    901        refTexture.allocLevel(0);
    902 
    903        texture = gl.createTexture();
    904 
    905        switch (id) {
    906            case 0:
    907                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.5, 0.5]);
    908                break;
    909 
    910            case 1:
    911                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 1.0, 1.0]);
    912                break;
    913 
    914            default:
    915                DE_ASSERT(false);
    916        }
    917 
    918        gl.bindTexture(gl.TEXTURE_2D, texture);
    919 
    920        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, refTexture.getWidth(), refTexture.getHeight(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
    921 
    922        gl.generateMipmap(gl.TEXTURE_2D);
    923 
    924        gl.bindTexture(gl.TEXTURE_2D, null);
    925 
    926        return texture;
    927    };
    928 
    929    /**
    930     * @private
    931     * @param {number} id
    932     * @return {WebGLTexture}
    933     */
    934    glsSamplerObjectTest.MultiTextureSamplerTest.createTexture3D = function(id) {
    935        /** @type {WebGLTexture} */ var texture = null;
    936        /** @type {tcuTexture.Texture3D} */ var refTexture = new tcuTexture.Texture3D(
    937                                                                 new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    938                                                                                              tcuTexture.ChannelType.UNORM_INT8),
    939                                                                 glsSamplerObjectTest.TEXTURE3D_WIDTH,
    940                                                                 glsSamplerObjectTest.TEXTURE3D_HEIGHT,
    941                                                                 glsSamplerObjectTest.TEXTURE3D_DEPTH);
    942 
    943        refTexture.allocLevel(0);
    944 
    945        texture = gl.createTexture();
    946 
    947        switch (id) {
    948            case 0:
    949                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 0.5, 0.5]);
    950                break;
    951 
    952            case 1:
    953                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevel(0), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 1.0, 1.0]);
    954                break;
    955 
    956            default:
    957                DE_ASSERT(false);
    958        }
    959 
    960        gl.bindTexture(gl.TEXTURE_3D, texture);
    961        // TODO: check internalFormat and format in texImage3D
    962        gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA, refTexture.getWidth(), refTexture.getHeight(), refTexture.getDepth(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevel(0).getDataPtr());
    963 
    964        gl.generateMipmap(gl.TEXTURE_3D);
    965 
    966        gl.bindTexture(gl.TEXTURE_3D, null);
    967 
    968        return texture;
    969    };
    970 
    971    /**
    972     * @private
    973     * @param {number} id
    974     * @return {WebGLTexture}
    975     */
    976    glsSamplerObjectTest.MultiTextureSamplerTest.createTextureCube = function(id) {
    977        /** @type {WebGLTexture} */ var texture = null;
    978        /** @type {tcuTexture.TextureCube} */ var refTexture = new tcuTexture.TextureCube(
    979                                                                     new tcuTexture.TextureFormat(tcuTexture.ChannelOrder.RGBA,
    980                                                                                                  tcuTexture.ChannelType.UNORM_INT8),
    981                                                                     glsSamplerObjectTest.CUBEMAP_SIZE);
    982 
    983        texture = gl.createTexture();
    984 
    985        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_X, 0);
    986        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y, 0);
    987        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z, 0);
    988        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X, 0);
    989        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y, 0);
    990        refTexture.allocLevel(tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z, 0);
    991 
    992        switch (id) {
    993            case 0:
    994                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_X), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
    995                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
    996                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
    997                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
    998                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
    999                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z), [0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.5]);
   1000                break;
   1001 
   1002            case 1:
   1003                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_X), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1004                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Y), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1005                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_POSITIVE_Z), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1006                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_X), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1007                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Y), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1008                tcuTextureUtil.fillWithComponentGradients(refTexture.getLevelFace(0, tcuTexture.CubeFace.CUBEFACE_NEGATIVE_Z), [0.5, 0.5, 0.5, 0.5], [1.0, 1.0, 1.0, 1.0]);
   1009                break;
   1010 
   1011            default:
   1012                DE_ASSERT(false);
   1013        }
   1014 
   1015        gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
   1016 
   1017        for (var face in tcuTexture.CubeFace) {
   1018            /** @const @type {number} */ var target = gluTextureUtil.getGLCubeFace(tcuTexture.CubeFace[face]);
   1019            gl.texImage2D(target, 0, gl.RGBA, refTexture.getSize(), refTexture.getSize(), 0, gl.RGBA, gl.UNSIGNED_BYTE, refTexture.getLevelFace(0, tcuTexture.CubeFace[face]).getDataPtr());
   1020        }
   1021 
   1022        gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
   1023        gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
   1024 
   1025        return texture;
   1026    };
   1027 
   1028    /**
   1029     * @private
   1030     * @param {number} target
   1031     * @param {number} id
   1032     * @return {WebGLTexture}
   1033     */
   1034    glsSamplerObjectTest.MultiTextureSamplerTest.createTexture = function(target, id) {
   1035        /** @type {WebGLTexture} */ var texture;
   1036        switch (target) {
   1037            case gl.TEXTURE_2D:
   1038                texture = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture2D(id);
   1039                break;
   1040 
   1041            case gl.TEXTURE_3D:
   1042                texture = glsSamplerObjectTest.MultiTextureSamplerTest.createTexture3D(id);
   1043                break;
   1044 
   1045            case gl.TEXTURE_CUBE_MAP:
   1046                texture = glsSamplerObjectTest.MultiTextureSamplerTest.createTextureCube(id);
   1047                break;
   1048 
   1049            default:
   1050                DE_ASSERT(false);
   1051        }
   1052 
   1053        return texture;
   1054    };
   1055 
   1056    /**
   1057     * @private
   1058     * @param {number} target
   1059     * @return {string}
   1060     */
   1061    glsSamplerObjectTest.MultiTextureSamplerTest.selectVertexShader = function(target) {
   1062        switch (target) {
   1063                case gl.TEXTURE_2D:
   1064                    return '#version 300 es\n' +
   1065                    'in highp vec2 a_position;\n' +
   1066                    'uniform highp float u_posScale;\n' +
   1067                    'out mediump vec2 v_texCoord;\n' +
   1068                    'void main (void)\n' +
   1069                    ' {\n' +
   1070                    '\tv_texCoord = a_position;\n' +
   1071                    '\tgl_Position = vec4(u_posScale * a_position, 0.0, 1.0);\n' +
   1072                    '}';
   1073 
   1074                case gl.TEXTURE_3D:
   1075                    return '#version 300 es\n' +
   1076                    'in highp vec3 a_position;\n' +
   1077                    'uniform highp float u_posScale;\n' +
   1078                    'out mediump vec3 v_texCoord;\n' +
   1079                    'void main (void)\n' +
   1080                    ' {\n' +
   1081                    '\tv_texCoord = a_position;\n' +
   1082                    '\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n' +
   1083                    '}';
   1084 
   1085                case gl.TEXTURE_CUBE_MAP:
   1086                    return '#version 300 es\n' +
   1087                    'in highp vec4 a_position;\n' +
   1088                    'uniform highp float u_posScale;\n' +
   1089                    'out mediump vec2 v_texCoord;\n' +
   1090                    'void main (void)\n' +
   1091                    ' {\n' +
   1092                    '\tv_texCoord = a_position.zw;\n' +
   1093                    '\tgl_Position = vec4(u_posScale * a_position.xy, 0.0, 1.0);\n' +
   1094                    '}';
   1095 
   1096                default:
   1097                    DE_ASSERT(false);
   1098                    return '';
   1099            }
   1100    };
   1101 
   1102    /**
   1103     * @private
   1104     * @param {number} target
   1105     * @return {string}
   1106     */
   1107    glsSamplerObjectTest.MultiTextureSamplerTest.selectFragmentShader = function(target) {
   1108        switch (target) {
   1109                case gl.TEXTURE_2D:
   1110                    return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
   1111                    'uniform lowp sampler2D u_sampler1;\n' +
   1112                    'uniform lowp sampler2D u_sampler2;\n' +
   1113                    'in mediump vec2 v_texCoord;\n' +
   1114                    'void main (void)\n' +
   1115                    ' {\n' +
   1116                    '\to_color = vec4(0.75, 0.75, 0.75, 1.0) * (texture(u_sampler1, v_texCoord) + texture(u_sampler2, v_texCoord));\n' +
   1117                    '}';
   1118 
   1119                    break;
   1120 
   1121                case gl.TEXTURE_3D:
   1122                    return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
   1123                    'uniform lowp sampler3D u_sampler1;\n' +
   1124                    'uniform lowp sampler3D u_sampler2;\n' +
   1125                    'in mediump vec3 v_texCoord;\n' +
   1126                    'void main (void)\n' +
   1127                    ' {\n' +
   1128                    '\to_color = vec4(0.75, 0.75, 0.75, 1.0) * (texture(u_sampler1, v_texCoord) + texture(u_sampler2, v_texCoord));\n' +
   1129                    '}';
   1130 
   1131                case gl.TEXTURE_CUBE_MAP:
   1132                    return '#version 300 es\nlayout(location = 0) out mediump vec4 o_color;\n' +
   1133                    'uniform lowp samplerCube u_sampler1;\n' +
   1134                    'uniform lowp samplerCube u_sampler2;\n' +
   1135                    'in mediump vec2 v_texCoord;\n' +
   1136                    'void main (void)\n' +
   1137                    ' {\n' +
   1138                    '\to_color = vec4(0.5, 0.5, 0.5, 1.0) * (texture(u_sampler1, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x)))' +
   1139                    '+ texture(u_sampler2, vec3(cos(3.14 * v_texCoord.y) * sin(3.14 * v_texCoord.x), sin(3.14 * v_texCoord.y), cos(3.14 * v_texCoord.y) * cos(3.14 * v_texCoord.x))));\n' +
   1140                    '}';
   1141 
   1142                default:
   1143                    DE_ASSERT(false);
   1144                    return '';
   1145            }
   1146    };
   1147 
   1148 });