2 <title>WebGL test: Hidden depth/stencil passes without a depth/stencil buffer respectively
</title>
3 <script src='/tests/SimpleTest/SimpleTest.js'
></script>
4 <link rel='stylesheet' href='/tests/SimpleTest/test.css'
>
5 <script src='webgl-util.js'
></script>
7 <script id='vs' type='x-shader/x-vertex'
>
9 gl_PointSize =
1.0; // Note that this is undefined if we don't write to it!
10 gl_Position = vec4(vec3(
0),
1);
14 <script id='fs' type='x-shader/x-fragment'
>
15 precision mediump float;
18 gl_FragColor = vec4(
0,
1,
0,
1);
23 function ColorString(arr
) {
24 return '[' + arr
[0] + ', ' + arr
[1] + ', ' + arr
[2] + ', ' + arr
[3] + ']';
27 function DrawAndCheck(gl
, infoPrefix
, refColorStr
) {
28 gl
.viewport(0, 0, 1, 1);
30 gl
.clearColor(1, 0, 0, 1);
31 gl
.clear(gl
.COLOR_BUFFER_BIT
| gl
.DEPTH_BUFFER_BIT
| gl
.STENCIL_BUFFER_BIT
);
32 gl
.drawArrays(gl
.POINTS
, 0, 1);
34 var pixel
= new Uint8Array(4);
35 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixel
);
36 var pixelStr
= ColorString(pixel
);
38 ok(pixelStr
== refColorStr
, infoPrefix
+ pixelStr
+ ' should be ' + refColorStr
);
41 function TestCurrent(gl
, attribs
, infoPrefix
) {
42 infoPrefix
= infoPrefix
+ JSON
.stringify(attribs
) + ': ';
44 var CLEAR_COLOR
= ColorString([255, 0, 0, 255]);
45 var DRAW_COLOR
= ColorString([0, 255, 0, 255]);
47 gl
.disable(gl
.DEPTH_TEST
);
48 gl
.disable(gl
.STENCIL_TEST
);
50 DrawAndCheck(gl
, infoPrefix
+ 'initial: ', DRAW_COLOR
);
53 gl
.enable(gl
.DEPTH_TEST
);
54 gl
.depthFunc(gl
.NEVER
);
56 gl
.disable(gl
.STENCIL_TEST
);
58 // Depth test is enabled, and should pass NEVER.
59 // Since there is no depth buffer, the depth test is not run.
60 // Stencil test is disabled.
61 DrawAndCheck(gl
, infoPrefix
+ 'no-depth: ', DRAW_COLOR
);
64 if (!attribs
.stencil
) {
65 gl
.disable(gl
.DEPTH_TEST
);
67 gl
.enable(gl
.STENCIL_TEST
);
68 gl
.stencilFunc(gl
.NEVER
, 0, 0);
70 // Depth test is disabled.
71 // Stencil test is enabled, and should pass NEVER.
72 // Since there is no stencil buffer, the stencil test is not run.
73 DrawAndCheck(gl
, infoPrefix
+ 'no-stencil: ', DRAW_COLOR
);
77 function TestBackbuffer(requestedAttribs
) {
78 var canvas
= document
.createElement('canvas');
81 var gl
= canvas
.getContext('experimental-webgl', requestedAttribs
);
83 ok(true, 'WebGL doesn\'t work, skipping test.');
87 ok(gl
.drawingBufferWidth
== 1 && gl
.drawingBufferHeight
== 1,
88 'backbuffer should be 1x1');
90 var prog
= WebGLUtil
.createProgramByIds(gl
, 'vs', 'fs');
94 depth
: gl
.getContextAttributes().depth
,
95 stencil
: gl
.getContextAttributes().stencil
,
97 TestCurrent(gl
, attribs
, 'Backbuffer: ');
100 function TestUserFB() {
101 var canvas
= document
.createElement('canvas');
102 var gl
= canvas
.getContext('experimental-webgl');
104 ok(true, 'WebGL doesn\'t work, skipping test.');
108 var prog
= WebGLUtil
.createProgramByIds(gl
, 'vs', 'fs');
111 var rb
= gl
.createRenderbuffer();
112 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, rb
);
113 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.RGBA4
, 1, 1);
115 var fb
= gl
.createFramebuffer();
116 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fb
);
117 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.RENDERBUFFER
, rb
);
119 var depthRB
= gl
.createRenderbuffer();
120 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, depthRB
);
121 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.DEPTH_COMPONENT16
, 1, 1);
123 var stencilRB
= gl
.createRenderbuffer();
124 gl
.bindRenderbuffer(gl
.RENDERBUFFER
, stencilRB
);
125 gl
.renderbufferStorage(gl
.RENDERBUFFER
, gl
.STENCIL_INDEX8
, 1, 1);
128 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.DEPTH_ATTACHMENT
, gl
.RENDERBUFFER
, depthRB
);
129 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, null);
130 var status
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
);
131 if (status
!= gl
.FRAMEBUFFER_COMPLETE
) {
132 ok(true, 'Depth-only user FB is incomplete. This is allowed.');
136 TestCurrent(gl
, {depth
: true, stencil
: false}, 'Depth-only user FB');
140 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.DEPTH_ATTACHMENT
, gl
.RENDERBUFFER
, null);
141 gl
.framebufferRenderbuffer(gl
.FRAMEBUFFER
, gl
.STENCIL_ATTACHMENT
, gl
.RENDERBUFFER
, stencilRB
);
142 var status
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
);
143 if (status
!= gl
.FRAMEBUFFER_COMPLETE
) {
144 ok(true, 'Stencil-only user FB is incomplete. This is allowed.');
148 TestCurrent(gl
, {depth
: false, stencil
: true}, 'Stencil-only user FB');
153 TestBackbuffer({depth
: true, stencil
: false});
154 TestBackbuffer({depth
: false, stencil
: true});