summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance2/textures/misc/angle-stuck-depth-textures.html
blob: 17cb0ac91623a3d221b98228fe10b332357a6068 (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
<!--
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 Textures Misc Tests: "Stuck" Depth Textures</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="draw-vs" type="x-shader/x-vertex">#version 100
attribute vec3 vertex;
void main () {
  gl_Position = vec4(vertex.x, vertex.y, vertex.z * 2.0 - 1.0, 1);
}
</script>
<script id="draw-fs" type="x-shader/x-fragment">#version 100
void main () {
  gl_FragColor = vec4 (1.);
}
</script>

<script id="blit-vs" type="x-shader/x-vertex">#version 100
attribute vec2 vertex;
varying vec2 position;
void main () {
  position = vertex * .5 + .5;
  gl_Position = vec4(vertex, 0, 1);
}
</script>
<script id="blit-fs" type="x-shader/x-fragment">#version 100
precision mediump float;
uniform sampler2D texture;
varying vec2 position;
void main () {
  gl_FragColor = vec4 (texture2D (texture, position).rrr, 1.);
}
</script>

</head>
<body>
<canvas id="example" width="128" height="128"></canvas>
<div id="description"></div>
<div id="console"></div>

<script>
"use strict";

description("This test covers an ANGLE bug where an AMD workaround would cause depth textures to stick. See http://anglebug.com/1664.");

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

var WEBGL_depth_texture;

var drawProgram, blitProgram, textureLoc;

var quadVB;

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

  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;

  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);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after drawQuad");
}

// Test based on dEQP-GLES3.functional.blit.depth_stencil.depth_24_stencil8_stencil_only
function run_test() {

  var colorTex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, colorTex);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 128, 128, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  gl.bindTexture(gl.TEXTURE_2D, null);

  var depthTex = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, depthTex);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
  var levels = Math.log2(128);
  for (var mipLevel = 0; mipLevel <= levels; ++mipLevel)
  {
      var size = 128 >> mipLevel;
      gl.texImage2D(gl.TEXTURE_2D, mipLevel, gl.DEPTH24_STENCIL8, size, size, 0, gl.DEPTH_STENCIL,
                    gl.UNSIGNED_INT_24_8, null);
  }
  gl.bindTexture(gl.TEXTURE_2D, null);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after init textures");

  var framebuffer = gl.createFramebuffer();
  gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);

  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, colorTex, 0);
  gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthTex, 0);

  wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after init framebuffer");

  gl.depthRange(0.0, 1.0);
  gl.viewport(0, 0, 128, 128);
  gl.clearColor(0, 0, 0, 1);

  // Draw loop.
  for (var frame = 0; frame < 4; ++frame)
  {
      // draw into FBO
      gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
      gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);

      gl.enable(gl.DEPTH_TEST);

      gl.useProgram(drawProgram);
      if (frame % 2 != 0) {
        drawQuad(0.0);
      } else {
        drawQuad(1.0);
      }

      gl.bindFramebuffer(gl.FRAMEBUFFER, null);

      // blit FBO
      gl.disable(gl.DEPTH_TEST);

      gl.useProgram(blitProgram);
      gl.uniform1i(textureLoc, 0);
      gl.bindTexture(gl.TEXTURE_2D, depthTex);

      drawQuad(0.5);

      if (frame % 2 != 0) {
        wtu.checkCanvasRect(gl, 0, 0, 128, 128, [0, 0, 0, 255],
                            "depth texture should be black on odd iterations");

      } else {
        wtu.checkCanvasRect(gl, 0, 0, 128, 128, [255, 255, 255, 255],
                            "depth texture should be white on even iterations");
      }

      wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after draw");
  }

  gl.deleteTexture(colorTex);
  gl.deleteTexture(depthTex);
  gl.deleteFramebuffer(framebuffer);
}

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

    drawProgram = wtu.setupProgram(gl, ["draw-vs", "draw-fs"], ["vertex"]);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after draw program initialization");
    shouldBe('gl.getProgramParameter(drawProgram, gl.LINK_STATUS)', 'true');

    blitProgram = wtu.setupProgram(gl, ["blit-vs", "blit-fs"], ["vertex"]);
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "after blit program initialization");
    shouldBe('gl.getProgramParameter(blitProgram, gl.LINK_STATUS)', 'true');

    textureLoc = gl.getUniformLocation(blitProgram, "texture")
    wtu.glErrorShouldBe(gl, gl.NO_ERROR, "query texture location");
    shouldBeNonNull('textureLoc')

    run_test();
}

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

</body>
</html>