summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/uniforms/large-uniform-buffers.html
blob: 5e67195ea8cfeda66986a3657a0e0f7cf95d5432 (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
<!--
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 large 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>
<script id="vshader" type="x-shader/x-vertex">#version 300 es
in vec4 a_vertex;
void main(void) {
  gl_Position = a_vertex;
}
</script>
<script id="fshader" type="x-shader/x-fragment">#version 300 es
precision mediump float;
layout (std140) uniform color_ubo {
  vec4 color;
};
out vec4 fragColor;
void main(void) {
  fragColor = color;
}
</script>
</head>
<body>
<canvas id="example" width="100", height="100"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
debug("");
description("Test for UBOs sized over 65536 - https://bugs.chromium.org/p/angleproject/issues/detail?id=3388");

var wtu = WebGLTestUtils;
var gl = wtu.create3DContext("example", undefined, 2);

function runTest() {
    var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['a_vertex']);
    var uboIndex = gl.INVALID_INDEX;
    if (program)
        uboIndex = gl.getUniformBlockIndex(program, "color_ubo");
    if (!program || uboIndex == gl.INVALID_INDEX) {
        testFailed("Loading program failed");
        return;
    }
    testPassed("Loading program succeeded");

    var vertices = new Float32Array([
        -1, -1, 0,
         1, -1, 0,
        -1,  1, 0,
         1,  1, 0
    ]);
    var vertexBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.enableVertexAttribArray(0);
    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

    var indexBuf = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Int16Array([ 0, 1, 2, 2, 1, 3 ]), gl.STATIC_DRAW);

    var uboDataSize = gl.getActiveUniformBlockParameter(
        program, uboIndex, gl.UNIFORM_BLOCK_DATA_SIZE);
    if (uboDataSize == 0) {
        testFailed("uniform block data size invalid");
        return;
    }

    var uboBuf = gl.createBuffer();
    var uboData = new Float32Array(0x20000);

    var offs0 = 0x00000;
    // Red
    uboData[offs0 + 0] = 1;
    uboData[offs0 + 1] = 0;
    uboData[offs0 + 2] = 0;
    uboData[offs0 + 3] = 1;

    var offs1 = 0x10000;
    const alignment = gl.getParameter(gl.UNIFORM_BUFFER_OFFSET_ALIGNMENT);
    if (offs1 % alignment != 0)
        testFailed("Platform has a strange uniform buffer offset alignment requirement: " + alignment);

    // Green
    uboData[offs1 + 0] = 0;
    uboData[offs1 + 1] = 1;
    uboData[offs1 + 2] = 0;
    uboData[offs1 + 3] = 1;

    gl.uniformBlockBinding(program, uboIndex, 0);
    gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, uboBuf);
    gl.bufferData(gl.UNIFORM_BUFFER, uboData, gl.STATIC_DRAW);

    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors from setup");

    debug("draw lower triangle - should be red");
    gl.bindBufferRange(gl.UNIFORM_BUFFER, 0, uboBuf, offs0 * 4, 0x10);
    gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 0);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors from draw");

    debug("draw upper triangle - should be green");
    var uboBuf2 = gl.createBuffer();
    gl.bindBufferRange(gl.UNIFORM_BUFFER, 0, uboBuf, offs1 * 4, 0x10);
    gl.drawElements(gl.TRIANGLES, 3, gl.UNSIGNED_SHORT, 6);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "No errors from draw");

    var width = 100, height = 100;
    wtu.checkCanvasRectColor(gl, 0, 0, width/2-5, height/2-5, [255, 0, 0, 255], 2,
        function() { testPassed("lower left should be red"); },
        function() { testFailed("lower left should be red"); });
    wtu.checkCanvasRectColor(gl, width/2+5, height/2+5, width/2-5, height/2-5, [0, 255, 0, 255], 2,
        function() { testPassed("top right should be green"); },
        function() { testFailed("top right should be green"); });
}

if (!gl) {
    testFailed("WebGL context creation failed");
} else {
    testPassed("WebGL context creation succeeded");
    runTest();
}

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

</body>
</html>