sampler-uniforms.html (4469B)
1 <!-- 2 Copyright (c) 2019 The Khronos Group Inc. 3 Use of this source code is governed by an MIT-style license that can be 4 found in the LICENSE.txt file. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 10 <meta charset="utf-8"> 11 <title>WebGL2 getActiveUniform conformance test.</title> 12 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 13 <script src="../../js/js-test-pre.js"></script> 14 <script src="../../js/webgl-test-utils.js"> </script> 15 </head> 16 <body> 17 <canvas id="example" width="16" height="16"></canvas> 18 <div id="description"></div> 19 <div id="console"></div> 20 <script id="vshader" type="x-shader/x-vertex">#version 300 es 21 void main() 22 { 23 gl_Position = vec4(0, 0, 0, 1); 24 } 25 </script> 26 <script id="fshader" type="x-shader/x-fragment">#version 300 es 27 precision mediump float; 28 uniform mediump $type uniform0; 29 out vec4 myFragColor; 30 void main() 31 { 32 myFragColor = vec4(0,$access,0,1); 33 } 34 </script> 35 <script> 36 "use strict"; 37 description("Tests getActiveUniform for various types"); 38 39 var wtu = WebGLTestUtils; 40 var gl = wtu.create3DContext("example", undefined, 2); 41 42 var tests = [ 43 { glType: gl.SAMPLER_2D, size: 1, type: 'sampler2D', access: 'texture(uniform0, vec2(0,0)).x'}, 44 { glType: gl.SAMPLER_CUBE, size: 1, type: 'samplerCube', access: 'texture(uniform0, vec3(0,1,0)).x'}, 45 { glType: gl.SAMPLER_3D, size: 1, type: 'sampler3D', access: 'texture(uniform0, vec3(0,1,0)).x'}, 46 { glType: gl.SAMPLER_2D_ARRAY, size: 1, type: 'sampler2DArray', access: 'texture(uniform0, vec3(0,1,0)).x'}, 47 48 { glType: gl.SAMPLER_2D_SHADOW, size: 1, type: 'sampler2DShadow', access: 'texture(uniform0, vec3(0,1,0))'}, 49 { glType: gl.SAMPLER_CUBE_SHADOW, size: 1, type: 'samplerCubeShadow', access: 'texture(uniform0, vec4(0,1,0,0))'}, 50 { glType: gl.SAMPLER_2D_ARRAY_SHADOW, size: 1, type: 'sampler2DArrayShadow', access: 'texture(uniform0, vec4(0,1,0,0))'}, 51 52 { glType: gl.INT_SAMPLER_2D, size: 1, type: 'isampler2D', access: 'texture(uniform0, vec2(0,0)).x'}, 53 { glType: gl.INT_SAMPLER_CUBE, size: 1, type: 'isamplerCube', access: 'texture(uniform0, vec3(0,1,0)).x'}, 54 { glType: gl.INT_SAMPLER_3D, size: 1, type: 'isampler3D', access: 'texture(uniform0, vec3(0,1,0)).x'}, 55 { glType: gl.INT_SAMPLER_2D_ARRAY, size: 1, type: 'isampler2DArray', access: 'texture(uniform0, vec3(0,1,0)).x'}, 56 57 { glType: gl.UNSIGNED_INT_SAMPLER_2D, size: 1, type: 'usampler2D', access: 'texture(uniform0, vec2(0,0)).x'}, 58 { glType: gl.UNSIGNED_INT_SAMPLER_CUBE, size: 1, type: 'usamplerCube', access: 'texture(uniform0, vec3(0,1,0)).x'}, 59 { glType: gl.UNSIGNED_INT_SAMPLER_3D, size: 1, type: 'usampler3D', access: 'texture(uniform0, vec3(0,1,0)).x'}, 60 { glType: gl.UNSIGNED_INT_SAMPLER_2D_ARRAY, size: 1, type: 'usampler2DArray', access: 'texture(uniform0, vec3(0,1,0)).x'}, 61 ]; 62 63 var vs = wtu.loadShaderFromScript(gl, 'vshader', gl.VERTEX_SHADER); 64 var source = document.getElementById('fshader').text; 65 66 function createProgram(type, access) { 67 var fs = wtu.loadShader( 68 gl, 69 source.replace('$type', type).replace('$access', access), 70 gl.FRAGMENT_SHADER); 71 var program = wtu.setupProgram(gl, [vs, fs]); 72 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "no errors from setup"); 73 return program; 74 } 75 76 for (var tt = 0; tt < tests.length; ++tt) { 77 var t = tests[tt]; 78 debug(""); 79 debug("Testing uniform sampler type : " + t.type); 80 var program = createProgram(t.type, t.access); 81 var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS); 82 assertMsg(numUniforms >= 1, "at least 1 sampler uniform"); 83 var found = false; 84 for (var ii = 0; ii < numUniforms; ++ii) { 85 var info = gl.getActiveUniform(program, ii); 86 if (info.name == 'uniform0') { 87 found = true; 88 assertMsg(info.type == t.glType, 89 "type must be " + wtu.glEnumToString(gl, t.glType) + " was " + 90 wtu.glEnumToString(gl, info.type)); 91 assertMsg(info.size == t.size, 92 "size must be " + t.size + ' was ' + info.size); 93 } 94 } 95 assertMsg(found, "uniform 'uniform0' should exist"); 96 var loc = gl.getUniformLocation(program, 'uniform0'); 97 assertMsg(loc != null, "getUniformLocation must return non null"); 98 gl.uniform1i(loc, tt + 1); 99 var val = gl.getUniform(program, loc); 100 assertMsg(val == tt + 1, "getUniform must return set value"); 101 } 102 103 debug(""); 104 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "no errors running the tests"); 105 106 var successfullyParsed = true; 107 </script> 108 <script src="../../js/js-test-post.js"></script> 109 110 </body> 111 </html>