get-uniform-indices.html (3225B)
1 <!-- 2 Copyright (c) 2022 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 <link rel="stylesheet" href="../../resources/js-test-style.css"/> 12 <script src="../../js/js-test-pre.js"></script> 13 <script src="../../js/webgl-test-utils.js"></script> 14 </head> 15 <body> 16 <div id="description"></div> 17 <div id="console"></div> 18 19 <script> 20 "use strict"; 21 description("This test verifies getUniformIndices behaviors."); 22 23 debug(""); 24 25 var wtu = WebGLTestUtils; 26 27 const e_canvas = document.createElement('canvas'); 28 const gl = e_canvas.getContext('webgl2'); 29 30 const VSRC = `\ 31 #version 300 es 32 precision mediump float; 33 uniform float u_vert_scalar; 34 uniform float u_vert_arr[3]; 35 uniform float u_both_scalar; 36 uniform float u_both_arr[3]; 37 void main() { 38 gl_Position = vec4(0, 0, 0, 1); 39 gl_Position.r += u_vert_scalar; 40 gl_Position.r += u_vert_arr[1]; 41 gl_Position.r += u_both_scalar; 42 gl_Position.r += u_both_arr[1]; 43 } 44 `; 45 46 const FSRC = `\ 47 #version 300 es 48 precision mediump float; 49 uniform float u_frag_scalar; 50 uniform float u_frag_arr[3]; 51 uniform float u_both_scalar; 52 uniform float u_both_arr[3]; 53 out vec4 o_frag_color; 54 void main() { 55 o_frag_color = vec4(0, 0, 0, 1); 56 o_frag_color.r += u_frag_scalar; 57 o_frag_color.r += u_frag_arr[1]; 58 o_frag_color.r += u_both_scalar; 59 o_frag_color.r += u_both_arr[1]; 60 } 61 `; 62 63 (() => { 64 if (!gl) { 65 testFailed("WebGL context does not exist"); 66 return; 67 } 68 69 window.prog = wtu.setupProgram(gl, [VSRC, FSRC]); 70 if (!prog) { 71 testFailed("Setting up program failed"); 72 return; 73 } 74 let err = gl.getError(); 75 if (err) throw err; 76 77 const IS_ACTIVE_BY_NAME = { 78 'u_vert_scalar' : true, 79 'u_vert_scalar[0]': false, 80 'u_vert_arr' : true, 81 'u_vert_arr[0]' : true, // Even if the [0] is unused, the name enumerated 82 // via getActiveUniforms for this array is 'u_vert_arr[0]'. 83 'u_vert_arr[1]' : false, 84 85 'u_frag_scalar' : true, 86 'u_frag_scalar[0]': false, 87 'u_frag_arr' : true, 88 'u_frag_arr[0]' : true, 89 'u_frag_arr[1]' : false, 90 91 'u_both_scalar' : true, 92 'u_both_scalar[0]': false, 93 'u_both_arr' : true, 94 'u_both_arr[0]' : true, 95 'u_both_arr[1]' : false, 96 }; 97 const NAMES = Object.keys(IS_ACTIVE_BY_NAME); 98 const active_ids = gl.getUniformIndices(prog, NAMES); 99 100 err = gl.getError(); 101 if (err) throw err; 102 103 NAMES.forEach((name, i) => { 104 const active_id_was = active_ids[i]; 105 const is_active_expected = IS_ACTIVE_BY_NAME[name]; 106 const is_active_was = active_id_was != gl.INVALID_INDEX; 107 expectTrue(is_active_was == is_active_expected, 108 `getUniformIndices([, '${name}' ,]) -> [, ${active_id_was} ,], should be [, ${is_active_expected ? '0<=N<INVALID_INDEX' : 'INVALID_INDEX'} ,]`); 109 if (is_active_was) { 110 const info = gl.getActiveUniform(prog, active_id_was); 111 expectTrue(info.name.startsWith(name), `'${info.name}'.startsWith('${name}')`); 112 } 113 }); 114 })(); 115 116 var successfullyParsed = true; 117 </script> 118 <script src="../../js/js-test-post.js"></script> 119 120 </body> 121 </html>