summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/js/tests/ovr_multiview2_util.js
blob: 5de4dc88d840420d7e1602e824d34b41bc0a81bd (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
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.
*/

"use strict";

function createTextureWithNearestFiltering(target)
{
    let texture = gl.createTexture();
    gl.bindTexture(target, texture);
    gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
    gl.texParameteri(target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
    gl.texParameteri(target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "texture parameter setup should succeed");
    return texture;
}

// Write a transformation matrix to elements of floatArray starting from index.
// The matrix transforms a unit square (-1 to 1) to a rectangle with the width scaleX and the left edge at offsetX.
function setupTranslateAndScaleXMatrix(floatArray, index, scaleX, offsetX)
{
    // x position is transformed according to this equation: scaleX * x0 + translateX = offsetX
    // By substituting x0 with -1 (unit square x value for the left edge), we get the following:
    let translateX = offsetX + scaleX;

    floatArray[index] = scaleX;
    floatArray[index + 1] = 0.0;
    floatArray[index + 2] = 0.0;
    floatArray[index + 3] = 0.0;

    floatArray[index + 4] = 0.0;
    floatArray[index + 5] = 1.0;
    floatArray[index + 6] = 0.0;
    floatArray[index + 7] = 0.0;

    floatArray[index + 8] = 0.0;
    floatArray[index + 9] = 0.0;
    floatArray[index + 10] = 1.0;
    floatArray[index + 11] = 0.0;

    floatArray[index + 12] = translateX;
    floatArray[index + 13] = 0.0;
    floatArray[index + 14] = 0.0;
    floatArray[index + 15] = 1.0;
}

// Check the currently bound read framebuffer with dimensions <width> x <height>.
// The framebuffer should be divided into <strips> equally wide vertical strips, with the one indicated by
// <coloredStripIndex> colored with <expectedStripColor>. The rest of the framebuffer should be colored transparent black.
// A two pixel wide region at each edge of the colored region is left unchecked to allow for some tolerance for rasterization.
function checkVerticalStrip(width, height, strips, coloredStripIndex, expectedStripColor, framebufferDescription)
{
    let colorRegionLeftEdge = (width / strips) * coloredStripIndex;
    let colorRegionRightEdge = (width / strips) * (coloredStripIndex + 1);
    if (coloredStripIndex > 0) {
        wtu.checkCanvasRect(gl, 0, 0, colorRegionLeftEdge - 1, height, [0, 0, 0, 0], 'the left edge of ' + framebufferDescription + ' should be untouched');
    }
    if (coloredStripIndex < strips - 1) {
        wtu.checkCanvasRect(gl, colorRegionRightEdge + 1, 0, width - colorRegionRightEdge - 1, height, [0, 0, 0, 0], 'the right edge of ' + framebufferDescription + ' should be untouched');
    }
    wtu.checkCanvasRect(gl, colorRegionLeftEdge + 1, 0, colorRegionRightEdge - colorRegionLeftEdge - 2, height, expectedStripColor, 'a thin strip in ' + framebufferDescription + ' should be colored ' + expectedStripColor);
}

function getMultiviewPassthroughVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'in vec4 a_position;',

    'void main() {',
    '    gl_Position = a_position;',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

// This shader splits the viewport into <views> equally sized vertical strips.
// The input quad defined by "a_position" is transformed to fill a different
// strip in each view.
function getMultiviewOffsetVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'in vec4 a_position;',

    'void main() {',
    '    vec4 pos = a_position;',
    "    // Transform the quad to a thin vertical strip that's offset along the x axis according to the view id.",
    '    pos.x = (pos.x * 0.5 + 0.5 + float(gl_ViewID_OVR)) * 2.0 / $(num_views).0 - 1.0;',
    '    gl_Position = pos;',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

// This shader transforms the incoming "a_position" with transforms for each
// view given in the uniform array "transform".
function getMultiviewRealisticUseCaseVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'uniform mat4 transform[$(num_views)];',
    'in vec4 a_position;',

    'void main() {',
    "    // Transform the quad with the transformation matrix chosen according to gl_ViewID_OVR.",
    '    vec4 pos = transform[gl_ViewID_OVR] * a_position;',
    '    gl_Position = pos;',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

function getMultiviewColorFragmentShader() {
    return ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',
    'precision highp float;',

    'out vec4 my_FragColor;',

    'void main() {',
    '    uint mask = gl_ViewID_OVR + 1u;',
    '    my_FragColor = vec4(((mask & 4u) != 0u) ? 1.0 : 0.0,',
    '                        ((mask & 2u) != 0u) ? 1.0 : 0.0,',
    '                        ((mask & 1u) != 0u) ? 1.0 : 0.0,',
    '                        1.0);',
    '}'].join('\n');
}

function getMultiviewColorFragmentShaderForDrawBuffers(drawBuffers) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',
    'precision highp float;',

    'out vec4 my_FragColor[$(drawBuffers)];',

    'void main() {',
    '    uint mask;'];

    for (let i = 0; i < drawBuffers; ++i) {
        shaderCode.push(wtu.replaceParams('    mask = gl_ViewID_OVR + $(i)u;', {'i': i + 1}));
        shaderCode.push(wtu.replaceParams('    my_FragColor[$(i)] = vec4(((mask & 4u) != 0u) ? 1.0 : 0.0,', {'i': i}));
        shaderCode.push('                           ((mask & 2u) != 0u) ? 1.0 : 0.0,');
        shaderCode.push('                           ((mask & 1u) != 0u) ? 1.0 : 0.0,');
        shaderCode.push('                           1.0);');
    }
    shaderCode.push('}');
    shaderCode = shaderCode.join('\n');
    return wtu.replaceParams(shaderCode, {'drawBuffers' : drawBuffers});
}

function getMultiviewVaryingVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'in vec4 a_position;',
    'out float testVarying;',

    'void main() {',
    '    gl_Position = a_position;',
    '    testVarying = float(gl_ViewID_OVR);',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

function getMultiviewVaryingFragmentShader() {
    return ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',
    'precision highp float;',

    'in float testVarying;',
    'out vec4 my_FragColor;',

    'void main() {',
    '    int mask = int(testVarying + 0.1) + 1;',
    '    my_FragColor = vec4(((mask & 4) != 0) ? 1.0 : 0.0,',
    '                        ((mask & 2) != 0) ? 1.0 : 0.0,',
    '                        ((mask & 1) != 0) ? 1.0 : 0.0,',
    '                        1.0);',
    '}'].join('\n');
}

function getMultiviewFlatVaryingVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'in vec4 a_position;',
    'flat out int testVarying;',

    'void main() {',
    '    gl_Position = a_position;',
    '    testVarying = int(gl_ViewID_OVR);',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

function getMultiviewFlatVaryingFragmentShader() {
    return ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',
    'precision highp float;',

    'flat in int testVarying;',
    'out vec4 my_FragColor;',

    'void main() {',
    '    int mask = testVarying + 1;',
    '    my_FragColor = vec4(((mask & 4) != 0) ? 1.0 : 0.0,',
    '                        ((mask & 2) != 0) ? 1.0 : 0.0,',
    '                        ((mask & 1) != 0) ? 1.0 : 0.0,',
    '                        1.0);',
    '}'].join('\n');
}

function getMultiviewInstancedVertexShader(views) {
    let shaderCode = ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',

    'layout(num_views = $(num_views)) in;',

    'in vec4 a_position;',
    'out vec4 color;',

    'void main() {',
    '    vec4 pos = a_position;',
    "    // Transform the quad to a thin vertical strip that's offset along the x axis according to the view id and instance id.",
    '    pos.x = (pos.x * 0.5 + 0.5 + float(gl_ViewID_OVR) + float(gl_InstanceID)) * 2.0 / ($(num_views).0 * 2.0) - 1.0;',
    '    int mask = gl_InstanceID + 1;',
    '    color = vec4(((mask & 4) != 0) ? 1.0 : 0.0,',
    '                 ((mask & 2) != 0) ? 1.0 : 0.0,',
    '                 ((mask & 1) != 0) ? 1.0 : 0.0,',
    '                 1.0);',
    '    gl_Position = pos;',
    '}'].join('\n');
    return wtu.replaceParams(shaderCode, {'num_views': views});
}

function getInstanceColorFragmentShader() {
    return ['#version 300 es',
    '#extension GL_OVR_multiview2 : require',
    'precision highp float;',

    'in vec4 color;',
    'out vec4 my_FragColor;',

    'void main() {',
    '    my_FragColor = color;',
    '}'].join('\n');
}

function getExpectedColor(view) {
    var mask = (view + 1);
    return [(mask & 4) ? 255 : 0, (mask & 2) ? 255 : 0, (mask & 1) ? 255 : 0, 255];
}