gl-bind-attrib-location-long-names-test.html (4238B)
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>WebGL BindAttribLocation Long Names Conformance Tests</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 <div id="description"></div> 18 <div id="console"></div> 19 <canvas style="border: 1px solid black;" id="canvas" width="50" height="50"></canvas> 20 <script id="vshader" type="text/something-not-javascript"> 21 attribute vec4 vPosition$(suffix); 22 attribute vec4 vColor$(suffix); 23 varying vec4 color; 24 void main() 25 { 26 gl_Position = vPosition$(suffix); 27 color = vColor$(suffix); 28 } 29 </script> 30 <script id="fshader" type="text/something-not-javascript"> 31 precision mediump float; 32 33 varying vec4 color; 34 void main() 35 { 36 gl_FragColor = color; 37 } 38 </script> 39 <script> 40 "use strict"; 41 description("This test checks using long names with bindAttribLocation work."); 42 43 debug(""); 44 debug("Canvas.getContext"); 45 46 var wtu = WebGLTestUtils; 47 var gl = wtu.create3DContext("canvas"); 48 shouldBeNonNull("gl"); 49 50 debug(""); 51 debug("Checking gl.bindAttribLocation with long names."); 52 53 var program = gl.createProgram(); 54 55 var suffix = "long"; 56 for (var ii = 0; ii < 5; ++ii) { 57 suffix = suffix + suffix; 58 } 59 var replacements = { 60 suffix: suffix 61 }; 62 63 var vsrc = wtu.replaceParams(wtu.getScript("vshader"), replacements); 64 var fsrc = wtu.replaceParams(wtu.getScript("fshader"), replacements); 65 66 var vs = wtu.loadShader(gl, vsrc, gl.VERTEX_SHADER); 67 var fs = wtu.loadShader(gl, fsrc, gl.FRAGMENT_SHADER); 68 69 var attribs = { 70 vPosition: "vPosition" + suffix, 71 vColor: "vColor" + suffix 72 }; 73 74 gl.attachShader(program, vs); 75 gl.attachShader(program, fs); 76 77 var positions = gl.createBuffer(); 78 gl.bindBuffer(gl.ARRAY_BUFFER, positions); 79 gl.bufferData( 80 gl.ARRAY_BUFFER, 81 new Float32Array( 82 [ 1.0, 1.0, 0.0, 83 -1.0, 1.0, 0.0, 84 -1.0, -1.0, 0.0, 85 1.0, 1.0, 0.0, 86 -1.0, -1.0, 0.0, 87 1.0, -1.0, 0.0]), 88 gl.STATIC_DRAW); 89 90 var colors = gl.createBuffer(); 91 gl.bindBuffer(gl.ARRAY_BUFFER, colors); 92 gl.bufferData( 93 gl.ARRAY_BUFFER, 94 new Float32Array( 95 [ 0,1,0,1, 96 0,1,0,1, 97 0,1,0,1, 98 0,1,0,1, 99 0,1,0,1, 100 0,1,0,1]), 101 gl.STATIC_DRAW); 102 103 function setBindLocations(colorLocation, positionLocation) { 104 gl.bindAttribLocation(program, positionLocation, attribs.vPosition); 105 gl.bindAttribLocation(program, colorLocation, attribs.vColor); 106 gl.linkProgram(program); 107 gl.useProgram(program); 108 var linked = (gl.getProgramParameter(program, gl.LINK_STATUS) != 0); 109 assertMsg(linked, "program linked successfully"); 110 111 debug("vPosition:" + gl.getAttribLocation(program, attribs.vPosition)) 112 debug("vColor :" + gl.getAttribLocation(program, attribs.vColor)) 113 assertMsg(gl.getAttribLocation(program, attribs.vPosition) == positionLocation, 114 "location of vPosition should be " + positionLocation); 115 assertMsg(gl.getAttribLocation(program, attribs.vColor) == colorLocation, 116 "location of vColor should be " + colorLocation); 117 118 var ploc = gl.getAttribLocation(program, attribs.vPosition); 119 var cloc = gl.getAttribLocation(program, attribs.vColor); 120 gl.bindBuffer(gl.ARRAY_BUFFER, positions); 121 gl.enableVertexAttribArray(positionLocation); 122 gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); 123 gl.bindBuffer(gl.ARRAY_BUFFER, colors); 124 gl.enableVertexAttribArray(colorLocation); 125 gl.vertexAttribPointer(colorLocation, 4, gl.FLOAT, false, 0, 0); 126 } 127 128 function checkDraw(colorLocation, positionLocation, r, g, b, a) { 129 wtu.clearAndDrawUnitQuad(gl); 130 wtu.checkCanvas(gl, [r, g, b, a], "should be green"); 131 132 gl.disableVertexAttribArray(positionLocation); 133 gl.disableVertexAttribArray(colorLocation); 134 } 135 136 setBindLocations(2, 3); 137 checkDraw(2, 3, 0, 255, 0, 255); 138 139 setBindLocations(0, 3); 140 gl.disableVertexAttribArray(0); 141 gl.vertexAttrib4f(0, 1, 0, 0, 1); 142 checkDraw(0, 3, 255, 0, 0, 255); 143 144 wtu.glErrorShouldBe(gl, gl.NO_ERROR); 145 146 debug(""); 147 var successfullyParsed = true; 148 149 </script> 150 <script src="../../js/js-test-post.js"></script> 151 152 </body> 153 </html>