sampler-struct-function-arg.html (3022B)
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 8 <!DOCTYPE html> 9 <html> 10 <head> 11 <meta charset="utf-8"> 12 <title>Passing a struct containing a sampler to a function.</title> 13 <link rel="stylesheet" href="../../../resources/js-test-style.css"/> 14 <script src="../../../js/js-test-pre.js"></script> 15 <script src="../../../js/webgl-test-utils.js"></script> 16 </head> 17 18 <body> 19 <canvas id="output" style="border: none;" width="64" height="64"></canvas> 20 <div id="description"></div> 21 <div id="console"></div> 22 23 <script id="shader-fs" type="x-shader/x-fragment"> 24 precision mediump float; 25 26 struct SomeStruct{ 27 sampler2D source; 28 }; 29 30 vec4 fun(SomeStruct s){ 31 return texture2D(s.source, vec2(0.5)); 32 } 33 34 uniform SomeStruct green; 35 void main(){ 36 gl_FragColor = fun(green); 37 } 38 </script> 39 40 <script id="shader-fs-array" type="x-shader/x-fragment"> 41 precision mediump float; 42 43 struct SomeStruct{ 44 sampler2D source; 45 }; 46 47 vec4 fun(SomeStruct s[2]){ 48 return texture2D(s[0].source, vec2(0.5)); 49 } 50 51 uniform SomeStruct green[2]; 52 void main(){ 53 gl_FragColor = fun(green); 54 } 55 </script> 56 57 <script> 58 "use strict"; 59 60 description(); 61 debug("If the test passes correctly the viewport will be green."); 62 63 var wtu = WebGLTestUtils; 64 var canvas = document.getElementById("output"); 65 var gl = wtu.create3DContext(canvas); 66 67 var textureGreen; 68 69 var createGreenTexture = function() { 70 var texture = gl.createTexture(); 71 gl.bindTexture(gl.TEXTURE_2D, texture); 72 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); 73 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); 74 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); 75 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); 76 wtu.fillTexture(gl, texture, 1, 1, [0, 255, 0, 255]); 77 gl.bindTexture(gl.TEXTURE_2D, null); 78 return texture; 79 }; 80 81 var test = function(fragShaderId, texUniformName) { 82 var program = wtu.setupProgram(gl, [wtu.simpleVertexShader, fragShaderId], ["a_position"], [0], true); 83 84 if (!program) { 85 testFailed("Shader compilation/link failed"); 86 } else { 87 // Bind texture 88 var uniformMap = wtu.getUniformMap(gl, program); 89 gl.activeTexture(gl.TEXTURE0); 90 gl.bindTexture(gl.TEXTURE_2D, textureGreen); 91 gl.uniform1i(uniformMap[texUniformName].location, 0); 92 93 // Draw 94 wtu.clearAndDrawUnitQuad(gl); 95 96 // Verify output 97 wtu.checkCanvasRect(gl, 0, 128, 256, 128, [0, 255,0, 255], "should be green", 1); 98 } 99 }; 100 101 if (!gl) { 102 testFailed("context does not exist"); 103 } else { 104 wtu.setupUnitQuad(gl, 0, 1); 105 textureGreen = createGreenTexture(); 106 test("shader-fs", "green.source"); 107 test("shader-fs-array", "green[0].source"); 108 } 109 var successfullyParsed = true; 110 </script> 111 <script src="../../../js/js-test-post.js"></script> 112 </body> 113 </html>