| <!-- |
| /* |
| ** Copyright (c) 2014 The Khronos Group Inc. |
| ** |
| ** Permission is hereby granted, free of charge, to any person obtaining a |
| ** copy of this software and/or associated documentation files (the |
| ** "Materials"), to deal in the Materials without restriction, including |
| ** without limitation the rights to use, copy, modify, merge, publish, |
| ** distribute, sublicense, and/or sell copies of the Materials, and to |
| ** permit persons to whom the Materials are furnished to do so, subject to |
| ** the following conditions: |
| ** |
| ** The above copyright notice and this permission notice shall be included |
| ** in all copies or substantial portions of the Materials. |
| ** |
| ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| */ |
| --> |
| |
| |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <meta charset="utf-8"> |
| <title>Passing a struct containing a sampler to a function.</title> |
| <link rel="stylesheet" href="../../../resources/js-test-style.css"/> |
| <script src="../../../js/js-test-pre.js"></script> |
| <script src="../../../js/webgl-test-utils.js"></script> |
| </head> |
| |
| <body> |
| <canvas id="output" style="border: none;" width="64" height="64"></canvas> |
| <div id="description"></div> |
| <div id="console"></div> |
| |
| <script id="shader-fs" type="x-shader/x-fragment"> |
| precision mediump float; |
| |
| struct SomeStruct{ |
| sampler2D source; |
| }; |
| |
| vec4 fun(SomeStruct s){ |
| return texture2D(s.source, vec2(0.5)); |
| } |
| |
| uniform SomeStruct green; |
| void main(){ |
| gl_FragColor = fun(green); |
| } |
| </script> |
| |
| <script id="shader-fs-array" type="x-shader/x-fragment"> |
| precision mediump float; |
| |
| struct SomeStruct{ |
| sampler2D source; |
| }; |
| |
| vec4 fun(SomeStruct s[2]){ |
| return texture2D(s[0].source, vec2(0.5)); |
| } |
| |
| uniform SomeStruct green[2]; |
| void main(){ |
| gl_FragColor = fun(green); |
| } |
| </script> |
| |
| <script> |
| "use strict"; |
| |
| description(); |
| debug("If the test passes correctly the viewport will be green."); |
| |
| var wtu = WebGLTestUtils; |
| var canvas = document.getElementById("output"); |
| var gl = wtu.create3DContext(canvas); |
| |
| var textureGreen; |
| |
| var createGreenTexture = function() { |
| var texture = gl.createTexture(); |
| gl.bindTexture(gl.TEXTURE_2D, texture); |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| wtu.fillTexture(gl, texture, 1, 1, [0, 255, 0, 255]); |
| gl.bindTexture(gl.TEXTURE_2D, null); |
| return texture; |
| }; |
| |
| var test = function(fragShaderId, texUniformName) { |
| var program = wtu.setupProgram(gl, [wtu.simpleVertexShader, fragShaderId], ["a_position"], [0], true); |
| |
| if (!program) { |
| testFailed("Shader compilation/link failed"); |
| } else { |
| // Bind texture |
| var uniformMap = wtu.getUniformMap(gl, program); |
| gl.activeTexture(gl.TEXTURE0); |
| gl.bindTexture(gl.TEXTURE_2D, textureGreen); |
| gl.uniform1i(uniformMap[texUniformName].location, 0); |
| |
| // Draw |
| wtu.clearAndDrawUnitQuad(gl); |
| |
| // Verify output |
| wtu.checkCanvasRect(gl, 0, 128, 256, 128, [0, 255,0, 255], "should be green", 1); |
| } |
| }; |
| |
| if (!gl) { |
| testFailed("context does not exist"); |
| } else { |
| wtu.setupUnitQuad(gl, 0, 1); |
| textureGreen = createGreenTexture(); |
| test("shader-fs", "green.source"); |
| test("shader-fs-array", "green[0].source"); |
| } |
| var successfullyParsed = true; |
| </script> |
| <script src="../../../js/js-test-post.js"></script> |
| </body> |
| </html> |