
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GLSL Array of Structs Uniform</title>
<link rel="stylesheet" href="../../../resources/js-test-style.css"/>
<link rel="stylesheet" href="../../../resources/glsl-feature-tests.css"/>
<script src="../../../js/js-test-pre.js"></script>
<script src="../../../js/webgl-test-utils.js"> </script>

<script id="vshader" type="x-shader/x-vertex">
attribute vec4 a_position;
void main(void) {
    gl_Position = a_position;
}
</script>
<script id="fshader0" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
  vec4 color;
};
uniform my_struct u_colors[2];
void main(void) {
    gl_FragColor = u_colors[0].color;
}
</script>
<script id="fshader1" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
  vec4 color;
};
uniform my_struct u_colors[2];
void main(void) {
    gl_FragColor = u_colors[1].color;
}
</script>
<script id="fshader2" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
  vec4 color1;
  vec4 color2;
};
uniform my_struct u_colors[2];
void main(void) {
    gl_FragColor = u_colors[0].color1 + u_colors[0].color2 + u_colors[1].color1 +u_colors[1].color2;
}
</script>
<script id="fshader3" type="x-shader/x-fragment">
precision mediump float;
struct my_struct {
  float r;
  float g;
  float b;
  float a;
};
uniform my_struct u_colors[2];
void main(void) {
    gl_FragColor = vec4(u_colors[0].r, u_colors[0].g, u_colors[1].b, u_colors[1].a);
}
</script>
</head>
<body>
<canvas id="canvas" width="50" height="50"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description();

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("canvas");
wtu.setupUnitQuad(gl);

for (var ii = 0; ii < 2; ++ii) {
  var program = wtu.setupProgram(gl, ["vshader", "fshader" + ii], ["a_position"]);
  var red_loc = gl.getUniformLocation(program, "u_colors[" + ii + "].color");
  var green_loc = gl.getUniformLocation(program, "u_colors[" + (1 - ii) + "].color");
  gl.uniform4fv(red_loc, [1, 0, 0, 1]);
  gl.uniform4fv(green_loc, [0, 1, 0, 1]);
  wtu.clearAndDrawUnitQuad(gl);
  wtu.checkCanvas(gl, [255, 0, 0, 255], "Should be red");
}

var program = wtu.setupProgram(gl, ["vshader", "fshader2"], ["a_position"]);
var numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
shouldBe("numUniforms", "4");
var uniforms = []
for (var ii = 0; ii < numUniforms; ++ii) {
  uniforms.push(gl.getActiveUniform(program, ii).name);
}
uniforms.sort();
shouldBe("uniforms[0]", '"u_colors[0].color1"');
shouldBe("uniforms[1]", '"u_colors[0].color2"');
shouldBe("uniforms[2]", '"u_colors[1].color1"');
shouldBe("uniforms[3]", '"u_colors[1].color2"');
var loc00 = gl.getUniformLocation(program, "u_colors[0].color1");
var loc01 = gl.getUniformLocation(program, "u_colors[0].color2");
var loc10 = gl.getUniformLocation(program, "u_colors[1].color1");
var loc11 = gl.getUniformLocation(program, "u_colors[1].color2");
shouldBeTrue("loc00 != undefined");
shouldBeTrue("loc01 != undefined");
shouldBeTrue("loc10 != undefined");
shouldBeTrue("loc11 != undefined");
gl.uniform4fv(loc00, [1, 0, 0, 0]);
gl.uniform4fv(loc01, [0, 1, 0, 0]);
gl.uniform4fv(loc10, [0, 0, 1, 0]);
gl.uniform4fv(loc11, [0, 0, 0, 1]);
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 0]);
wtu.checkCanvas(gl, [255, 255, 255, 255], "Should be white");

program = wtu.setupProgram(gl, ["vshader", "fshader3"], ["a_position"]);
var loc0r = gl.getUniformLocation(program, "u_colors[0].r");
var loc0g = gl.getUniformLocation(program, "u_colors[0].g");
var loc0b = gl.getUniformLocation(program, "u_colors[0].b");
var loc0a = gl.getUniformLocation(program, "u_colors[0].a");
var loc1r = gl.getUniformLocation(program, "u_colors[1].r");
var loc1g = gl.getUniformLocation(program, "u_colors[1].g");
var loc1b = gl.getUniformLocation(program, "u_colors[1].b");
var loc1a = gl.getUniformLocation(program, "u_colors[1].a");
shouldBeTrue("loc0r != undefined");
shouldBeTrue("loc0g != undefined");
shouldBeTrue("loc1b != undefined");
shouldBeTrue("loc1a != undefined");
gl.uniform1f(loc0r, 1);
gl.uniform1f(loc0g, 1);
gl.uniform1f(loc1b, 1);
gl.uniform1f(loc1a, 1);
wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 0]);
wtu.checkCanvas(gl, [255, 255, 255, 255], "Should be white");

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "there should be no errors");

var successfullyParsed = true;
</script>
<script src="../../../js/js-test-post.js"></script>
</body>
</html>

