uniform-blocks-with-arrays.html (3452B)
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 uniform blocks containing arrays 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 <script id="vshader" type="x-shader/x-vertex">#version 300 es 16 in vec4 vPosition; 17 void main() { 18 gl_Position = vPosition; 19 } 20 </script> 21 <script id="fshaderArraysOfStructs" type="x-shader/x-fragment">#version 300 es 22 precision highp float; 23 out vec4 my_FragColor; 24 struct light_t { 25 vec4 intensity; 26 }; 27 const int maxLights = 2; 28 layout(std140) uniform lightData { light_t lights[maxLights]; }; 29 vec4 processLight(vec4 lighting, light_t light) 30 { 31 return lighting + light.intensity; 32 } 33 void main() 34 { 35 vec4 lighting = vec4(0, 1, 0, 1); 36 for (int n = 0; n < maxLights; n++) 37 lighting = processLight(lighting, lights[n]); 38 my_FragColor = lighting; 39 } 40 </script> 41 <script id="fshaderArraysOfStructsContainingArrays" type="x-shader/x-fragment">#version 300 es 42 precision highp float; 43 out vec4 my_FragColor; 44 struct light_t { 45 vec4 intensity[3]; 46 }; 47 const int maxLights = 2; 48 layout(std140) uniform lightData { light_t lights[maxLights]; }; 49 vec4 processLight(vec4 lighting, light_t light) 50 { 51 return lighting + light.intensity[1]; 52 } 53 void main() 54 { 55 vec4 lighting = vec4(0, 1, 0, 1); 56 for (int n = 0; n < maxLights; n++) 57 lighting = processLight(lighting, lights[n]); 58 my_FragColor = lighting; 59 } 60 </script> 61 </head> 62 <body> 63 <div id="description"></div> 64 <div id="console"></div> 65 <script> 66 "use strict"; 67 description(); 68 69 // GLSL ES 3.00.6 section 4.3.7: 70 // "Otherwise, built-in types, previously declared structures, and arrays of these are allowed as the type of a declarator in the same manner they are allowed outside a block." 71 72 var wtu = WebGLTestUtils; 73 var gl = wtu.create3DContext(undefined, undefined, 2); 74 75 function runTest(fragShader, bufferFloatCount, shaderBlockDescription) { 76 debug(''); 77 debug('Testing fragment shader with an uniform block containing ' + shaderBlockDescription); 78 var program = wtu.setupProgram(gl, ['vshader', fragShader], ['vPosition'], undefined, true); 79 if (!program) { 80 testFailed("Loading program failed"); 81 return; 82 } 83 var blockIndex = gl.getUniformBlockIndex(program, "lightData"); 84 85 var uniformBuffer = gl.createBuffer(); 86 gl.bindBuffer(gl.UNIFORM_BUFFER, uniformBuffer); 87 gl.bufferData(gl.UNIFORM_BUFFER, new Float32Array(bufferFloatCount), gl.DYNAMIC_READ); 88 89 gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, uniformBuffer); 90 gl.uniformBlockBinding(program, blockIndex, 0); 91 92 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors from uniform buffer setup"); 93 94 wtu.clearAndDrawUnitQuad(gl); 95 96 wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors from draw"); 97 98 wtu.checkCanvas(gl, [0, 255, 0, 255], 'should be green', 1); 99 } 100 101 if (!gl) { 102 testFailed("WebGL context creation failed"); 103 } else { 104 wtu.setupUnitQuad(gl); 105 runTest("fshaderArraysOfStructs", 2 * 4, 'arrays of structs'); 106 runTest("fshaderArraysOfStructsContainingArrays", 2 * 3 * 4, 'arrays of structs containing arrays'); 107 } 108 109 debug(""); 110 var successfullyParsed = true; 111 </script> 112 <script src="../../js/js-test-post.js"></script> 113 114 </body> 115 </html>