summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/context/context-sharing-texture2darray-texture3d-data-bug.html
blob: 8ef6dd74b336e26d0ba9abbade1d6b93e2e28924 (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
<!--
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>Multiple WebGL2 Context sharing texture2darray/texture3d data bug test</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="canvas1" width="64" height="64"> </canvas>
<canvas id="canvas2" width="64" height="64"> </canvas>
<div id="console"></div>

<!-- WebGL 2 Shaders -->
<script id="vs" type="x-shader/x-vertex">#version 300 es
precision mediump float;
in vec4 a_position;
in vec2 a_coord;
out vec2 v_coord;
void main() {
    gl_Position = a_position;
    v_coord = a_coord;
}
</script>

<script id="fs_texture_3d" type="x-shader/x-fragment">#version 300 es
precision mediump float;
in vec2 v_coord;
uniform mediump sampler3D u_sampler;
out vec4 o_color;
void main () {
    o_color = texture(u_sampler, vec3(v_coord, 0.0));
}
</script>

<script id="fs_texture_2d_array" type="x-shader/x-fragment">#version 300 es
precision mediump float;
in vec2 v_coord;
uniform mediump sampler2DArray u_sampler;
out vec4 o_color;
void main () {
    o_color = texture(u_sampler, vec3(v_coord, 0.0));
}
</script>

<script>
"use strict";
description("This test verifies that 2 different contexts both using 2d array texture or 3d texture does not share the texture data among them due to context save/restore bug. https://bugs.chromium.org/p/chromium/issues/detail?id=788448");
debug("");

function render(gl, width, height, expectedColor, msg) {
    wtu.setupUnitQuad(gl, 0, 1);
    wtu.clearAndDrawUnitQuad(gl);
    wtu.checkCanvasRect(gl, 0, 0, width, height, expectedColor, msg);
}

function StateSetup(gl, texture_type, texture_color, width, height) {

    // create a buffer to hold texture data
    const depth = 4;
    var size = width * height * depth * 4;
    var buf = new Uint8Array(size);
    for (var i = 0; i < size; i += 4) {
        buf[i + 0] = texture_color[0];
        buf[i + 1] = texture_color[1];
        buf[i + 2] = texture_color[2];
        buf[i + 3] = texture_color[3];
    }
    gl.viewport(0, 0, width, height);

    // choose texture type and fragment shader type
    var tex_type = gl.TEXTURE_2D;
    var fragment_shader =  "", vertex_shader = "vs";
    if(texture_type === "3d") {
      tex_type = gl.TEXTURE_3D, fragment_shader = "fs_texture_3d";
    } else if(texture_type === "2d_array") {
      tex_type = gl.TEXTURE_2D_ARRAY, fragment_shader = "fs_texture_2d_array";
    } else {
      testFailed("Texture type must be 3d or 2darray");
    }

    var program = wtu.setupProgram(gl, [vertex_shader, fragment_shader], ['a_position', 'a_coord'], [0, 1]);

    // create a texture
    var texture = gl.createTexture();
    gl.activeTexture(gl.TEXTURE0);

    // program texture parameters
    gl.activeTexture(gl.TEXTURE0);
    gl.bindTexture(tex_type, texture);
    gl.texImage3D(tex_type, 0, gl.RGBA, width, height, depth, 0, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    gl.texParameteri(tex_type, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
    gl.texParameteri(tex_type, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

    // bind sampler to the texture
    var samplerLoc = gl.getUniformLocation(program, "u_sampler");
    gl.uniform1i(samplerLoc, 0);

    // flush all gl commands
    gl.flush();
}

var wtu = WebGLTestUtils;
var canvas1 = document.getElementById("canvas1");
var gl1 = wtu.create3DContext(canvas1, null, 2); //context1

var canvas2 = document.getElementById("canvas2");
var gl2 = wtu.create3DContext(canvas2, null, 2); // context2

if (gl1 && gl2)
{
  testPassed("Created 2 WebGL2 context successfully");
  var red = [255, 0, 0, 255], green = [0,255,0,255], blue = [0,0,255,255];
  var width = 64, height = 64;
  var texture_type = "3d", texture_color = green;
  StateSetup(gl1,texture_type, texture_color, width, height);// context1 state setup
  texture_color = red;
  StateSetup(gl2, texture_type, texture_color, width, height);// context2 state setup
  render(gl1, width, height, green, "Result pixels rendering from context1 with 3d texture should be green");// render context1

  texture_type = "2d_array", texture_color = blue;
  StateSetup(gl1, texture_type, texture_color, width, height);// context1 state setup
  texture_color = green;
  StateSetup(gl2, texture_type, texture_color, width, height);// context2 state setup
  render(gl1, width, height, blue, "Result pixels rendering from context1 with 2darray texture should be blue");//render context1
}
else if(!gl1)
{
  testFailed("Fail to get 1st WebGL2 context");
}
else
{
  testFailed("Fail to get 2nd WebGL2 context");
}

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

</body>
</html>