summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/buffers/one-large-uniform-buffer.html
blob: a2bddce04cfd368314270c8136763bf7396fa25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!--
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>WebGL Uniform Buffers Conformance Tests</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>
<div id="description"></div>
<canvas id="canvas" style="width: 50px; height: 50px;"> </canvas>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">#version 300 es
in vec4 position;
void main()
{
    gl_Position = position;
}
</script>
<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;
uniform uni {
    vec4 color;
};

out vec4 fragColor;

void main()
{
    fragColor = color;
}
</script>
<script>
"use strict";
description("This test covers ANGLE bugs when using a large uniform blocks. ANGLE would confuse an internal clipped uniform buffer size and produce an assert or error. Also there were issues with readback of large UBOs. See http://crbug.com/660670.");

debug("");

var wtu = WebGLTestUtils;
var canvas = document.getElementById("canvas");
var gl = wtu.create3DContext(canvas, null, 2);
var quadVB;

if (!gl) {
    testFailed("WebGL context does not exist");
} else {
    testPassed("WebGL context exists");

    debug("");
    debug("Testing uniform block with large data store");
    runTest();

    debug("");
    debug("Testing readback on uniform block with large data store");
    runReadbackTest();
}

function getQuadVerts(depth) {
  var quadVerts = new Float32Array(3 * 6);
  quadVerts[0] = -1.0; quadVerts[1] =   1.0; quadVerts[2] = depth;
  quadVerts[3] = -1.0; quadVerts[4] =  -1.0; quadVerts[5] = depth;
  quadVerts[6] =  1.0; quadVerts[7] =  -1.0; quadVerts[8] = depth;
  quadVerts[9] = -1.0; quadVerts[10] =  1.0; quadVerts[11] = depth;
  quadVerts[12] = 1.0; quadVerts[13] = -1.0; quadVerts[14] = depth;
  quadVerts[15] = 1.0; quadVerts[16] =  1.0; quadVerts[17] = depth;
  return quadVerts;
}

function drawQuad(depth) {
  if (!quadVB) {
    quadVB = gl.createBuffer()
  }

  var quadVerts = getQuadVerts(depth);

  gl.bindBuffer(gl.ARRAY_BUFFER, quadVB);
  gl.bufferData(gl.ARRAY_BUFFER, quadVerts, gl.STATIC_DRAW);
  gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 0, 0);
  gl.enableVertexAttribArray(0);
  gl.drawArrays(gl.TRIANGLES, 0, 6);
}

function runTest() {

    // Create the program
    var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["position"]);
    if (!program) {
        testFailed("Failed to set up the program");
        return;
    }

    // Init uniform buffer. To trigger the bug, it's necessary to use the
    // DYNAMIC_DRAW usage. This makes ANGLE attempt to map the buffer internally
    // with an incorrect copy size.
    var ubo = gl.createBuffer();
    var big_size = 4096 * 64;
    var data = new Float32Array([0.5, 0.75, 0.25, 1.0]);
    gl.bindBuffer(gl.UNIFORM_BUFFER, ubo);
    gl.bufferData(gl.UNIFORM_BUFFER, big_size, gl.DYNAMIC_DRAW);
    gl.bufferSubData(gl.UNIFORM_BUFFER, 0, data);

    gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, ubo);
    var buffer_index = gl.getUniformBlockIndex(program, "uni");
    if (buffer_index == -1) {
      testFailed("Failed to get uniform block index");
      return;
    }
    gl.uniformBlockBinding(program, buffer_index, 0);

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Setting up uniform block should succeed");

    // Draw the quad
    gl.useProgram(program);
    drawQuad(0.5);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Draw with uniform block should succeed");

    // Verify the output color
    var color = [127, 191, 64, 255];
    wtu.checkCanvas(gl, color, "canvas should be same as input uniform", 1);
}

function runReadbackTest() {

    // Create the program
    var program = wtu.setupProgram(gl, ["vshader", "fshader"], ["position"]);
    if (!program) {
        testFailed("Failed to set up the program");
        return;
    }

    // Init uniform buffer. To trigger the bug, it's necessary to use the
    // DYNAMIC_DRAW usage. This makes ANGLE attempt to map the buffer internally
    // with an incorrect copy size.
    var ubo = gl.createBuffer();
    var num_floats = 4096 * 16;
    var expected_data = new Float32Array(num_floats);
    for (var index = 0; index < num_floats; ++index) {
        expected_data[index] = index;
    }

    expected_data[0] = 0.5;
    expected_data[1] = 0.75;
    expected_data[2] = 0.25;
    expected_data[3] = 1.0;

    gl.bindBuffer(gl.UNIFORM_BUFFER, ubo);
    gl.bufferData(gl.UNIFORM_BUFFER, expected_data, gl.DYNAMIC_DRAW);
    gl.bufferSubData(gl.UNIFORM_BUFFER, 0, expected_data);

    gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, ubo);
    var buffer_index = gl.getUniformBlockIndex(program, "uni");
    if (buffer_index == -1) {
      testFailed("Failed to get uniform block index");
      return;
    }
    gl.uniformBlockBinding(program, buffer_index, 0);

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Setting up uniform block should succeed");

    // Draw the quad
    gl.useProgram(program);
    drawQuad(0.5);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Draw with uniform block should succeed");

    // Verify the output color
    var color = [127, 191, 64, 255];
    wtu.checkCanvas(gl, color, "canvas should be same as input uniform", 1);

    // Verify readback
    var actual_data = new Float32Array(num_floats);
    gl.getBufferSubData(gl.UNIFORM_BUFFER, 0, actual_data);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Readback from uniform block should succeed");

    for (var index = 0; index < num_floats; ++index) {
        if (actual_data[index] != expected_data[index]) {
            testFailed("Expected and actual buffer data do not match");
            return;
        }
    }
}

debug("");
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>