stencil-renderbuffer-initialization.html (4795B)
1 <!-- 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. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 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> 14 </head> 15 <body> 16 <canvas id="testbed" width="40" height="40" style="width: 40px; height: 40px;"></canvas> 17 <div id="description"></div> 18 <div id="console"></div> 19 <script> 20 "use strict"; 21 var wtu = WebGLTestUtils; 22 description('Verify stencil renderbuffers are initialized to 0 before being read in WebGL'); 23 24 var gl = wtu.create3DContext("testbed"); 25 26 if (!gl) { 27 testFailed('canvas.getContext() failed'); 28 } else { 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); 32 gl.disable(gl.STENCIL_TEST); 33 34 let c = gl.canvas; 35 for (let i = 0; i < 2; ++i) { 36 runTest(gl, {alloc1: {w: c.width, h: c.height}, alloc2: null}); 37 runTest(gl, {alloc1: null, alloc2: {w: c.width, h: c.height}}); 38 // Tests for initially allocating at the wrong size. 39 runTest(gl, {alloc1: {w: 5, h: 5}, alloc2: {w: c.width, h: c.height}}); 40 } 41 42 // Testing buffer clearing won't change the clear values. 43 var clearColor = gl.getParameter(gl.COLOR_CLEAR_VALUE); 44 shouldBe("clearColor", "[0, 1, 0, 1]"); 45 wtu.glErrorShouldBe(gl, gl.NO_ERROR, 'should be no errors'); 46 } 47 48 function runTest(gl, params) { 49 debug("Test for stencil buffer: " + JSON.stringify(params)); 50 let final = params.alloc2 ? params.alloc2 : params.alloc1; 51 gl.viewport(0, 0, final.w, final.h); 52 wtu.checkCanvasRect(gl, 0, 0, final.w, final.h, 53 [0, 0, 0, 0], 54 "internal buffers have been initialized to 0"); 55 56 gl.disable(gl.STENCIL_TEST); 57 58 // fill the back buffer so we know that reading below happens from 59 // the renderbuffer. 60 gl.clearStencil(2); 61 gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); 62 63 // Set up (color+stencil) test buffer. 64 var fbo = gl.createFramebuffer(); 65 gl.bindFramebuffer(gl.FRAMEBUFFER, fbo); 66 var buffer = gl.createRenderbuffer(); 67 var stencilBuffer = gl.createRenderbuffer(); 68 69 if (params.alloc1) { 70 gl.bindRenderbuffer(gl.RENDERBUFFER, buffer); 71 allocStorage(gl.RGBA4, params.alloc1.w, params.alloc1.h); 72 gl.bindRenderbuffer(gl.RENDERBUFFER, stencilBuffer); 73 allocStorage(gl.STENCIL_INDEX8, params.alloc1.w, params.alloc1.h); 74 } 75 gl.bindRenderbuffer(gl.RENDERBUFFER, buffer); 76 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, buffer); 77 gl.bindRenderbuffer(gl.RENDERBUFFER, stencilBuffer); 78 gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, stencilBuffer); 79 if (params.alloc2) { 80 if (params.alloc1) { 81 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) { 82 debug("Skip: framebuffer is allowed to be incomplete."); 83 return; 84 } 85 // Clear the FBO in order to make sure framebufferRenderbuffer is 86 // committed. 87 gl.clear(gl.COLOR_BUFFER_BIT | gl.STENCIL_BUFFER_BIT); 88 } 89 gl.bindRenderbuffer(gl.RENDERBUFFER, buffer); 90 allocStorage(gl.RGBA4, params.alloc2.w, params.alloc2.h); 91 gl.bindRenderbuffer(gl.RENDERBUFFER, stencilBuffer); 92 allocStorage(gl.STENCIL_INDEX8, params.alloc2.w, params.alloc2.h); 93 } 94 95 function allocStorage(format, width, height) { 96 gl.renderbufferStorage(gl.RENDERBUFFER, format, width, height); 97 wtu.glErrorShouldBe(gl, gl.NO_ERROR, 98 "should be no error after renderbufferStorage."); 99 } 100 101 if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) { 102 debug("Skip: framebuffer is allowed to be incomplete."); 103 return; 104 } 105 wtu.glErrorShouldBe(gl, gl.NO_ERROR, 'should be no errors'); 106 107 // fbo depth should now be the default value of 0. 108 109 // Draw a blue quad (stencil = 1) (should pass the stencil test: 1 > 0) 110 gl.stencilFunc(gl.GREATER, 1, 0xffffffff); 111 gl.enable(gl.STENCIL_TEST); 112 wtu.setupColorQuad(gl); 113 wtu.setFloatDrawColor(gl, [0, 0, 1, 1]); 114 wtu.drawUnitQuad(gl); 115 wtu.checkCanvasRect(gl, 0, 0, final.w, final.h, 116 [0, 0, 255, 255]); 117 118 gl.deleteFramebuffer(fbo); 119 gl.deleteRenderbuffer(buffer); 120 121 gl.canvas.width += 1; 122 gl.canvas.height += 1; 123 124 wtu.glErrorShouldBe(gl, gl.NO_ERROR, 'should be no errors'); 125 debug(''); 126 } 127 128 var successfullyParsed = true; 129 </script> 130 <script src="../../js/js-test-post.js"></script> 131 </body> 132 </html>