2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
12 <script src=
"../../js/js-test-pre.js"></script>
13 <script src=
"../../js/webgl-test-utils.js"></script>
16 <canvas id=
"testbed" width=
"40" height=
"40" style=
"width: 40px; height: 40px;"></canvas>
17 <div id=
"description"></div>
18 <div id=
"console"></div>
21 var wtu
= WebGLTestUtils
;
22 description('Verify multisampled stencil renderbuffers are initialized to 0 before being read in WebGL');
24 var gl
= wtu
.create3DContext("testbed", null, 2);
27 testFailed('canvas.getContext() failed');
29 // Set the clear color to green. It should never show up.
30 gl
.clearColor(0, 1, 0, 1);
31 gl
.disable(gl
.DEPTH_TEST
);
34 var maxSamples
= gl
.getInternalformatParameter(
35 gl
.RENDERBUFFER
, gl
.RGBA8
, gl
.SAMPLES
)[0];
36 for (let i
= 0; i
< 2; ++i
) {
37 // Non-multisampled tests
38 runTest(gl
, {alloc1
: {w
: c
.width
, h
: c
.height
, s
: 0}, alloc2
: null});
39 runTest(gl
, {alloc1
: null, alloc2
: {w
: c
.width
, h
: c
.height
, s
: 0}});
41 runTest(gl
, {alloc1
: {w
: c
.width
, h
: c
.height
, s
: maxSamples
}, alloc2
: null});
42 runTest(gl
, {alloc1
: null, alloc2
: {w
: c
.width
, h
: c
.height
, s
: maxSamples
}});
44 // Tests for initially allocating at the wrong size.
45 // This is caused by a Qualcomm driver bug: http://crbug.com/696126
46 runTest(gl
, {alloc1
: {w
: 5, h
: 5, s
: maxSamples
}, alloc2
: {w
: c
.width
, h
: c
.height
, s
: maxSamples
}});
49 // Testing buffer clearing won't change the clear values.
50 var clearColor
= gl
.getParameter(gl
.COLOR_CLEAR_VALUE
);
51 shouldBe("clearColor", "[0, 1, 0, 1]");
52 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, 'should be no errors');
55 function runTest(gl
, params
) {
56 debug("Test for stencil buffer: " + JSON
.stringify(params
));
57 let resolve
= params
.alloc2
? params
.alloc2
: params
.alloc1
;
58 gl
.viewport(0, 0, resolve
.w
, resolve
.h
);
59 wtu
.checkCanvasRect(gl
, 0, 0, resolve
.w
, resolve
.h
,
61 "internal buffers have been initialized to 0");
63 gl
.disable(gl
.STENCIL_TEST
);
65 // fill the back buffer so we know that reading below happens from
68 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.STENCIL_BUFFER_BIT
);
70 // Set up (color+stencil) non-multisampled buffer to blit to and read back from.
71 var fbo
= gl
.createFramebuffer();
72 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo
);
73 var buffer
= gl
.createRenderbuffer();
74 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, buffer
);
75 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.RGBA8
, resolve
.w
, resolve
.h
);
76 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.RENDERBUFFER
, buffer
);
77 var stencilBuffer
= gl
.createRenderbuffer();
78 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, stencilBuffer
);
79 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.STENCIL_INDEX8
, resolve
.w
, resolve
.h
);
80 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, stencilBuffer
);
81 if (gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) != gl
.FRAMEBUFFER_COMPLETE
) {
82 debug("Skip: framebuffer is allowed to be incomplete.");
85 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, 'should be no errors');
87 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.STENCIL_BUFFER_BIT
);
88 wtu
.checkCanvasRect(gl
, 0, 0, resolve
.w
, resolve
.h
,
90 "user buffer has been cleared to green");
92 // Set up (stencil-only) multisampled buffer to test.
93 var fbo_m
= gl
.createFramebuffer();
94 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo_m
);
95 var buffer_m
= gl
.createRenderbuffer();
96 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, buffer_m
);
99 allocStorage(params
.alloc1
.w
, params
.alloc1
.h
, params
.alloc1
.s
);
101 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, buffer_m
);
104 if (gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) != gl
.FRAMEBUFFER_COMPLETE
) {
105 debug("Skip: framebuffer is allowed to be incomplete.");
108 // Clear the FBO in order to make sure framebufferRenderbuffer is
109 // committed. (In Firefox, framebufferRenderbuffer is deferred, so this
110 // is needed to trigger the bug.)
111 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.STENCIL_BUFFER_BIT
);
113 allocStorage(params
.alloc2
.w
, params
.alloc2
.h
, params
.alloc2
.s
);
116 function allocStorage(width
, height
, samples
) {
117 gl
.renderbufferStorageMultisample(
118 gl
.RENDERBUFFER
, samples
, gl
.STENCIL_INDEX8
, width
, height
);
119 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
,
120 "should be no error after renderbufferStorageMultisample(STENCIL_INDEX8).");
123 if (gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
) != gl
.FRAMEBUFFER_COMPLETE
) {
124 debug("Skip: framebuffer is allowed to be incomplete.");
127 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, 'should be no errors');
129 // Blit from multisampled buffer to non-multisampled buffer.
130 gl
.bindFramebuffer(gl
.READ_FRAMEBUFFER
, fbo_m
);
131 gl
.bindFramebuffer(gl
.DRAW_FRAMEBUFFER
, fbo
);
133 gl
.clear(gl
.STENCIL_BUFFER_BIT
);
134 // Blit stencil from fbo_m (should be default value of 0) to fbo (should be 2).
135 gl
.blitFramebuffer(0, 0, resolve
.w
, resolve
.h
,
136 0, 0, resolve
.w
, resolve
.h
,
137 gl
.STENCIL_BUFFER_BIT
, gl
.NEAREST
);
138 // fbo stencil should now be 0.
139 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, 'should be no errors');
141 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo
);
142 // Draw a blue quad (with stencil = 1) (should pass the stencil test: 1 > 0)
143 gl
.stencilFunc(gl
.GREATER
, 1, 0xffffffff);
144 gl
.enable(gl
.STENCIL_TEST
);
145 wtu
.setupColorQuad(gl
);
146 wtu
.setFloatDrawColor(gl
, [0, 0, 1, 1]);
147 wtu
.drawUnitQuad(gl
);
148 wtu
.checkCanvasRect(gl
, 0, 0, resolve
.w
, resolve
.h
,
151 gl
.deleteFramebuffer(fbo_m
);
152 gl
.deleteRenderbuffer(buffer_m
);
153 gl
.deleteFramebuffer(fbo
);
154 gl
.deleteRenderbuffer(buffer
);
156 gl
.canvas
.width
+= 1;
157 gl
.canvas
.height
+= 1;
159 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, 'should be no errors');
163 var successfullyParsed
= true;
165 <script src=
"../../js/js-test-post.js"></script>