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 <title>Verify copyTexImage2D follows format restictions
</title>
11 <meta charset=
"utf-8">
12 <link rel=
"stylesheet" href=
"../../../resources/js-test-style.css"/>
13 <script src=
"../../../js/js-test-pre.js"></script>
14 <script src=
"../../../js/webgl-test-utils.js"></script>
17 <div id=
"description"></div>
18 <canvas id=
"canvas" width=
"2" height=
"2"></canvas>
19 <canvas id=
"canvasNoAlpha" width=
"2" height=
"2"></canvas>
20 <div id=
"console"></div>
24 function getChannelsFromFormat(format
) {
31 case gl
.LUMINANCE_ALPHA
:
50 'LUMINANCE_ALPHA': false,
56 var wtu
= WebGLTestUtils
;
60 var canvas
= document
.getElementById("canvas");
61 var canvasNoAlpha
= document
.getElementById("canvasNoAlpha");
62 var gl
= wtu
.create3DContext(canvas
, {alpha
:true});
63 var glNoAlpha
= wtu
.create3DContext(canvasNoAlpha
, {alpha
:false});
65 debug("test with an RGBA backbuffer");
66 var program
= wtu
.setupTexturedQuad(gl
);
67 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "During Initialization");
70 testBackbufferFormats();
72 debug("test with an RGB backbuffer");
74 var program
= wtu
.setupTexturedQuad(gl
);
75 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "During Initialization");
78 function testBackbufferFormats() {
79 var fbo
= gl
.createFramebuffer();
80 gl
.bindFramebuffer(gl
.FRAMEBUFFER
, fbo
);
82 for (var ii
= 0; ii
< formats
.length
; ++ii
) {
83 var backFormat
= formats
[ii
];
85 var tex
= gl
.createTexture();
86 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
87 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
[backFormat
], 2, 2, 0, gl
[backFormat
], gl
.UNSIGNED_BYTE
, null);
88 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.NEAREST
);
89 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.NEAREST
);
91 gl
.framebufferTexture2D(gl
.FRAMEBUFFER
, gl
.COLOR_ATTACHMENT0
, gl
.TEXTURE_2D
, tex
, 0);
92 var status
= gl
.checkFramebufferStatus(gl
.FRAMEBUFFER
);
95 if (status
== gl
.FRAMEBUFFER_COMPLETE
) {
96 if (!isRenderable
[backFormat
]) {
97 testFailed('Creating framebuffer from ' + backFormat
+ ' texture succeeded even though it is not a renderable format');
99 debug('test with ' + backFormat
+ ' fbo');
100 testFormats(backFormat
);
103 debug(backFormat
+ ' not supported as a renderbuffer attachment');
108 function testFormats(backFormat
) {
109 for (var ii
= 0; ii
< formats
.length
; ++ii
) {
110 testCopyTexImage2D(backFormat
, formats
[ii
]);
114 function toChannels(value
) {
115 return ((value
& 0x1000) ? 'R' : '_') +
116 ((value
& 0x0100) ? 'G' : '_') +
117 ((value
& 0x0010) ? 'B' : '_') +
118 ((value
& 0x0001) ? 'A' : '_');
121 function testCopyTexImage2D(backFormat
, texFormat
) {
122 var need
= getChannelsFromFormat(texFormat
);
123 var have
= getChannelsFromFormat(backFormat
);
124 var shouldPass
= (need
& have
) == need
;
126 //debug("need: " + toChannels(need));
127 //debug("have: " + toChannels(have));
128 //debug("both: " + toChannels(have & need));
131 gl
.clearColor(0.25, 1, 0.75, 0.5);
132 gl
.clear(gl
.COLOR_BUFFER_BIT
);
134 var texture
= gl
.createTexture();
135 // Bind the texture to texture unit 0
136 gl
.bindTexture(gl
.TEXTURE_2D
, texture
);
137 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.NEAREST
);
138 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.NEAREST
);
140 gl
.copyTexImage2D(gl
.TEXTURE_2D
, 0, gl
[texFormat
], 0, 0, 2, 2, 0);
143 gl
, gl
.INVALID_OPERATION
,
144 "should not be able to copyTexImage2D " + texFormat
+ " from " + backFormat
);
150 "should be able to copyTexImage2D " + texFormat
+ " from " + backFormat
);
153 wtu
.clearAndDrawUnitQuad(gl
);
155 var expectedColors
= {
156 'ALPHA': [0, 0, 0, 127],
157 'LUMINANCE': [64, 64, 64, 255],
158 'LUMINANCE_ALPHA': [64, 64, 64, 127],
159 'RGB': [64, 255, 191, 255],
160 'RGBA': [64, 255, 191, 127]
163 var color
= expectedColors
[texFormat
];
165 wtu
.checkCanvas(gl
, color
, "should be " + color
, 16);
167 wtu
.glErrorShouldBe(gl
, gl
.NO_ERROR
, "should be no errors");
169 var successfullyParsed
= true;
171 <script src=
"../../../js/js-test-post.js"></script>