1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES Utilities
3 * ------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief OpenGL State Reset.
22 *//*--------------------------------------------------------------------*/
23
24 #include "gluStateReset.hpp"
25 #include "gluContextInfo.hpp"
26 #include "gluRenderContext.hpp"
27 #include "tcuRenderTarget.hpp"
28 #include "glwFunctions.hpp"
29 #include "glwEnums.hpp"
30 #include "deUniquePtr.hpp"
31
32 namespace glu
33 {
34 namespace
35 {
36 enum
37 {
38 MAX_ERROR_COUNT = 10
39 };
40
resetErrors(const glw::Functions & gl)41 void resetErrors (const glw::Functions& gl)
42 {
43 size_t errorNdx = 0;
44
45 for (errorNdx = 0; errorNdx < MAX_ERROR_COUNT; errorNdx++)
46 {
47 if (gl.getError() == GL_NONE)
48 break;
49 }
50
51 if (errorNdx == MAX_ERROR_COUNT)
52 TCU_FAIL("Couldn't reset error state");
53 }
54
55 }
56
resetStateES(const RenderContext & renderCtx,const ContextInfo & ctxInfo)57 void resetStateES (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
58 {
59 const glw::Functions& gl = renderCtx.getFunctions();
60 const ContextType type = renderCtx.getType();
61
62 // Reset error state
63 resetErrors(gl);
64
65 DE_ASSERT(isContextTypeES(type));
66
67 // Vertex attrib array state.
68 {
69 int numVertexAttribArrays = 0;
70 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
71
72 gl.bindBuffer (GL_ARRAY_BUFFER, 0);
73 gl.bindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
74
75 if (contextSupports(type, ApiType::es(3,0)))
76 {
77 gl.bindVertexArray (0);
78 gl.disable (GL_PRIMITIVE_RESTART_FIXED_INDEX);
79 }
80
81 if (contextSupports(type, ApiType::es(3,1)))
82 gl.bindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
83
84 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
85 {
86 gl.disableVertexAttribArray (ndx);
87 gl.vertexAttribPointer (ndx, 4, GL_FLOAT, GL_FALSE, 0, DE_NULL);
88
89 if (contextSupports(type, ApiType::es(3,0)))
90 gl.vertexAttribDivisor(ndx, 0);
91 }
92
93 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex attrib array state reset failed");
94 }
95
96 // Transformation state.
97 {
98 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
99
100 gl.viewport (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
101 gl.depthRangef (0.0f, 1.0f);
102
103 if (contextSupports(type, ApiType::es(3,0)))
104 gl.bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
105
106 GLU_EXPECT_NO_ERROR(gl.getError(), "Transformation state reset failed");
107 }
108
109 // Rasterization state
110 {
111 gl.lineWidth (1.0f);
112 gl.disable (GL_CULL_FACE);
113 gl.cullFace (GL_BACK);
114 gl.frontFace (GL_CCW);
115 gl.polygonOffset(0.0f, 0.0f);
116 gl.disable (GL_POLYGON_OFFSET_FILL);
117
118 if (contextSupports(type, ApiType::es(3,0)))
119 gl.disable(GL_RASTERIZER_DISCARD);
120
121 GLU_EXPECT_NO_ERROR(gl.getError(), "Rasterization state reset failed");
122 }
123
124 // Multisampling state
125 {
126 gl.disable (GL_SAMPLE_ALPHA_TO_COVERAGE);
127 gl.disable (GL_SAMPLE_COVERAGE);
128 gl.sampleCoverage (1.0f, GL_FALSE);
129
130 if (contextSupports(type, ApiType::es(3,1)))
131 {
132 int numSampleMaskWords = 0;
133 gl.getIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numSampleMaskWords);
134
135 gl.disable(GL_SAMPLE_MASK);
136
137 for (int ndx = 0; ndx < numSampleMaskWords; ndx++)
138 gl.sampleMaski(ndx, ~0u);
139 }
140
141 GLU_EXPECT_NO_ERROR(gl.getError(), "Multisampling state reset failed");
142 }
143
144 // Texture state.
145 // \todo [2013-04-08 pyry] Reset all levels?
146 {
147 const float borderColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
148 int numTexUnits = 0;
149 const bool supportsBorderClamp = ctxInfo.isExtensionSupported("GL_EXT_texture_border_clamp") || contextSupports(type, ApiType::es(3,2));
150
151 gl.getIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numTexUnits);
152
153 for (int ndx = 0; ndx < numTexUnits; ndx++)
154 {
155 gl.activeTexture(GL_TEXTURE0 + ndx);
156
157 // Reset 2D texture.
158 gl.bindTexture(GL_TEXTURE_2D, 0);
159 gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
160 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
161 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
162 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
163 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
164
165 if (contextSupports(type, ApiType::es(3,0)))
166 {
167 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
168 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
169 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
170 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
171 gl.texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1000.0f);
172 gl.texParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1000.0f);
173 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
174 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
175 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
176 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
177 }
178
179 if (contextSupports(type, ApiType::es(3,1)))
180 gl.texParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
181
182 if (supportsBorderClamp)
183 gl.texParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
184
185 // Reset cube map texture.
186 gl.bindTexture(GL_TEXTURE_CUBE_MAP, 0);
187 gl.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
188 gl.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
189 gl.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
190 gl.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
191 gl.texImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
192 gl.texImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
193 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
194 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
195 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
196 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
197
198 if (contextSupports(type, ApiType::es(3,0)))
199 {
200 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_R, GL_RED);
201 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
202 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
203 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
204 gl.texParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_LOD, -1000.0f);
205 gl.texParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LOD, 1000.0f);
206 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
207 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 1000);
208 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
209 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
210 }
211
212 if (contextSupports(type, ApiType::es(3,1)))
213 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
214
215 if (supportsBorderClamp)
216 gl.texParameterfv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
217
218 if (contextSupports(type, ApiType::es(3,0)))
219 {
220 // Reset 2D array texture.
221 gl.bindTexture(GL_TEXTURE_2D_ARRAY, 0);
222 gl.texImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
223 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
224 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
226 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
227 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
228 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
229 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
230 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
231 gl.texParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
232 gl.texParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
233 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
234 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
235 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
236 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
237
238 if (supportsBorderClamp)
239 gl.texParameterfv(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
240 }
241
242 if (contextSupports(type, ApiType::es(3,1)))
243 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
244
245 if (contextSupports(type, ApiType::es(3,0)))
246 {
247 // Reset 3D texture.
248 gl.bindTexture(GL_TEXTURE_3D, 0);
249 gl.texImage3D(GL_TEXTURE_3D, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
250 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
251 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
252 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
253 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
254 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
255 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
256 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
257 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
258 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
259 gl.texParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, -1000.0f);
260 gl.texParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 1000.0f);
261 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
262 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1000);
263 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
264 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
265
266 if (supportsBorderClamp)
267 gl.texParameterfv(GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
268 }
269
270 if (contextSupports(type, ApiType::es(3,1)))
271 gl.texParameteri(GL_TEXTURE_3D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
272
273 if (contextSupports(type, ApiType::es(3,1)))
274 {
275 // Reset multisample textures.
276 gl.bindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
277 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_SWIZZLE_R, GL_RED);
278 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
279 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
280 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
281 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_BASE_LEVEL, 0);
282 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAX_LEVEL, 1000);
283 }
284
285 if (ctxInfo.isExtensionSupported("GL_OES_texture_storage_multisample_2d_array"))
286 {
287 gl.bindTexture(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0);
288 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
289 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
290 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
291 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
292 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
293 gl.texParameteri(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
294 }
295
296 if (ctxInfo.isExtensionSupported("GL_EXT_texture_cube_map_array"))
297 {
298 // Reset cube array texture.
299 gl.bindTexture(GL_TEXTURE_CUBE_MAP_ARRAY, 0);
300 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
301 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
302 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
303 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
304 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
305 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
306 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
307 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
308 gl.texParameterf(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
309 gl.texParameterf(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
310 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
311 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
312 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
313 gl.texParameteri(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
314
315 if (supportsBorderClamp)
316 gl.texParameterfv(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
317 }
318 }
319
320 gl.activeTexture(GL_TEXTURE0);
321
322 if (contextSupports(type, ApiType::es(3,0)))
323 {
324 for (int ndx = 0; ndx < numTexUnits; ndx++)
325 gl.bindSampler(ndx, 0);
326 }
327
328 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state reset failed");
329 }
330
331 // Resetting state using non-indexed variants should be enough, but some
332 // implementations have bugs so we need to make sure indexed state gets
333 // set back to initial values.
334 if (ctxInfo.isExtensionSupported("GL_EXT_draw_buffers_indexed"))
335 {
336 int numDrawBuffers = 0;
337
338 gl.getIntegerv(GL_MAX_DRAW_BUFFERS, &numDrawBuffers);
339
340 for (int drawBufferNdx = 0; drawBufferNdx < numDrawBuffers; drawBufferNdx++)
341 {
342 gl.disablei (GL_BLEND, drawBufferNdx);
343 gl.blendFunci (drawBufferNdx, GL_ONE, GL_ZERO);
344 gl.blendEquationi (drawBufferNdx, GL_FUNC_ADD);
345 gl.colorMaski (drawBufferNdx, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
346 }
347
348 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to reset indexed draw buffer state");
349 }
350
351 // Pixel operations.
352 {
353 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
354
355 gl.disable (GL_SCISSOR_TEST);
356 gl.scissor (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
357
358 gl.disable (GL_STENCIL_TEST);
359 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
360 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
361
362 gl.disable (GL_DEPTH_TEST);
363 gl.depthFunc (GL_LESS);
364
365 gl.disable (GL_BLEND);
366 gl.blendFunc (GL_ONE, GL_ZERO);
367 gl.blendEquation(GL_FUNC_ADD);
368 gl.blendColor (0.0f, 0.0f, 0.0f, 0.0f);
369
370 gl.enable (GL_DITHER);
371
372 if (ctxInfo.isExtensionSupported("GL_EXT_sRGB_write_control"))
373 {
374 gl.enable (GL_FRAMEBUFFER_SRGB);
375 }
376
377 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
378 }
379
380 // Framebuffer control.
381 {
382 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
383 gl.depthMask (GL_TRUE);
384 gl.stencilMask (~0u);
385
386 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
387 gl.clearDepthf (1.0f);
388 gl.clearStencil (0);
389
390 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
391 }
392
393 // Framebuffer state.
394 {
395 // \note Actually spec explictly says 0 but on some platforms (iOS) no default framebuffer exists.
396 const deUint32 defaultFbo = renderCtx.getDefaultFramebuffer();
397 const deUint32 drawBuffer = defaultFbo != 0 ? GL_COLOR_ATTACHMENT0 : GL_BACK;
398 const deUint32 readBuffer = defaultFbo != 0 ? GL_COLOR_ATTACHMENT0 : GL_BACK;
399
400 gl.bindFramebuffer(GL_FRAMEBUFFER, defaultFbo);
401
402 if (contextSupports(type, ApiType::es(3,0)))
403 {
404 gl.drawBuffers (1, &drawBuffer);
405 gl.readBuffer (readBuffer);
406 }
407
408 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
409 }
410
411 // Renderbuffer state.
412 {
413 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
414 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
415 }
416
417 // Pixel transfer state.
418 {
419 gl.pixelStorei(GL_UNPACK_ALIGNMENT, 4);
420 gl.pixelStorei(GL_PACK_ALIGNMENT, 4);
421
422 if (contextSupports(type, ApiType::es(3,0)))
423 {
424 gl.pixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
425 gl.pixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
426 gl.pixelStorei(GL_UNPACK_ROW_LENGTH, 0);
427 gl.pixelStorei(GL_UNPACK_SKIP_ROWS, 0);
428 gl.pixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
429
430 gl.pixelStorei(GL_PACK_ROW_LENGTH, 0);
431 gl.pixelStorei(GL_PACK_SKIP_ROWS, 0);
432 gl.pixelStorei(GL_PACK_SKIP_PIXELS, 0);
433
434 gl.bindBuffer(GL_PIXEL_PACK_BUFFER, 0);
435 gl.bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
436 }
437
438 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
439 }
440
441 // Program object state.
442 {
443 gl.useProgram(0);
444
445 if (contextSupports(type, ApiType::es(3,0)))
446 {
447 int maxUniformBufferBindings = 0;
448 gl.getIntegerv (GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
449 gl.bindBuffer (GL_UNIFORM_BUFFER, 0);
450
451 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
452 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
453 }
454
455 if (contextSupports(type, ApiType::es(3,1)))
456 {
457 gl.bindProgramPipeline(0);
458
459 {
460 int maxAtomicCounterBufferBindings = 0;
461 gl.getIntegerv (GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS, &maxAtomicCounterBufferBindings);
462 gl.bindBuffer (GL_ATOMIC_COUNTER_BUFFER, 0);
463
464 for (int ndx = 0; ndx < maxAtomicCounterBufferBindings; ndx++)
465 gl.bindBufferBase(GL_ATOMIC_COUNTER_BUFFER, ndx, 0);
466 }
467
468 {
469 int maxShaderStorageBufferBindings = 0;
470 gl.getIntegerv (GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &maxShaderStorageBufferBindings);
471 gl.bindBuffer (GL_SHADER_STORAGE_BUFFER, 0);
472
473 for (int ndx = 0; ndx < maxShaderStorageBufferBindings; ndx++)
474 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, ndx, 0);
475 }
476 }
477
478 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
479 }
480
481 // Vertex shader state.
482 {
483 int numVertexAttribArrays = 0;
484 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
485
486 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
487 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
488
489 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
490 }
491
492 // Transform feedback state.
493 if (contextSupports(type, ApiType::es(3,0)))
494 {
495 int numTransformFeedbackSeparateAttribs = 0;
496 glw::GLboolean transformFeedbackActive = 0;
497 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
498 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
499
500 if (transformFeedbackActive)
501 gl.endTransformFeedback();
502
503 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
504
505 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
506 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
507
508 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
509 }
510
511 // Asynchronous query state.
512 if (contextSupports(type, ApiType::es(3,0)))
513 {
514 static const deUint32 targets[] = { GL_ANY_SAMPLES_PASSED, GL_ANY_SAMPLES_PASSED_CONSERVATIVE, GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
515
516 for (int i = 0; i < DE_LENGTH_OF_ARRAY(targets); i++)
517 {
518 int queryActive = 0;
519 gl.getQueryiv(targets[i], GL_CURRENT_QUERY, &queryActive);
520
521 if (queryActive != 0)
522 gl.endQuery(targets[i]);
523 }
524
525 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
526 }
527
528 // Hints.
529 {
530 gl.hint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
531
532 if (contextSupports(type, ApiType::es(3,0)))
533 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
534
535 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
536 }
537
538 // Compute.
539 if (contextSupports(type, ApiType::es(3,1)))
540 {
541 gl.bindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
542 GLU_EXPECT_NO_ERROR(gl.getError(), "Compute dispatch state reset failed");
543 }
544
545 // Buffer copy state.
546 if (contextSupports(type, ApiType::es(3,0)))
547 {
548 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
549 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
550
551 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
552 }
553
554 // Images.
555 if (contextSupports(type, ApiType::es(3,1)))
556 {
557 int numImageUnits = 0;
558 gl.getIntegerv(GL_MAX_IMAGE_UNITS, &numImageUnits);
559
560 for (int ndx = 0; ndx < numImageUnits; ndx++)
561 gl.bindImageTexture(ndx, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
562
563 GLU_EXPECT_NO_ERROR(gl.getError(), "Image state reset failed");
564 }
565
566 // Sample shading state.
567 if (contextSupports(type, ApiType::es(3,1)) && ctxInfo.isExtensionSupported("GL_OES_sample_shading"))
568 {
569 gl.minSampleShading(0.0f);
570 gl.disable(GL_SAMPLE_SHADING);
571
572 GLU_EXPECT_NO_ERROR(gl.getError(), "Sample shading state reset failed");
573 }
574
575 // Debug state
576 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
577 {
578 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
579 gl.debugMessageCallback != DE_NULL &&
580 gl.popDebugGroup != DE_NULL;
581
582 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
583 if (entrypointsPresent)
584 {
585 int stackDepth = 0;
586 gl.getIntegerv(GL_DEBUG_GROUP_STACK_DEPTH, &stackDepth);
587 for (int ndx = 1; ndx < stackDepth; ++ndx)
588 gl.popDebugGroup();
589
590 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
591 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
592 gl.debugMessageCallback(DE_NULL, DE_NULL);
593
594 if (type.getFlags() & glu::CONTEXT_DEBUG)
595 gl.enable(GL_DEBUG_OUTPUT);
596 else
597 gl.disable(GL_DEBUG_OUTPUT);
598 gl.disable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
599
600 GLU_EXPECT_NO_ERROR(gl.getError(), "Debug state reset failed");
601 }
602 }
603
604 // Primitive bounding box state.
605 if (ctxInfo.isExtensionSupported("GL_EXT_primitive_bounding_box"))
606 {
607 gl.primitiveBoundingBox(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
608 GLU_EXPECT_NO_ERROR(gl.getError(), "Primitive bounding box state reset failed");
609 }
610
611 // Tessellation state
612 if (ctxInfo.isExtensionSupported("GL_EXT_tessellation_shader"))
613 {
614 gl.patchParameteri(GL_PATCH_VERTICES, 3);
615 GLU_EXPECT_NO_ERROR(gl.getError(), "Tessellation patch vertices state reset failed");
616 }
617
618 // Advanced coherent blending
619 if (ctxInfo.isExtensionSupported("GL_KHR_blend_equation_advanced_coherent"))
620 {
621 gl.enable(GL_BLEND_ADVANCED_COHERENT_KHR);
622 GLU_EXPECT_NO_ERROR(gl.getError(), "Blend equation advanced coherent state reset failed");
623 }
624
625 // Texture buffer
626 if (ctxInfo.isExtensionSupported("GL_EXT_texture_buffer"))
627 {
628 gl.bindTexture(GL_TEXTURE_BUFFER, 0);
629 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
630 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture buffer state reset failed");
631 }
632 }
633
resetStateGLCore(const RenderContext & renderCtx,const ContextInfo & ctxInfo)634 void resetStateGLCore (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
635 {
636 const glw::Functions& gl = renderCtx.getFunctions();
637 const ContextType type = renderCtx.getType();
638
639 // Reset error state
640 resetErrors(gl);
641
642 // Vertex attrib array state.
643 {
644 gl.bindVertexArray (0);
645 gl.bindBuffer (GL_ARRAY_BUFFER, 0);
646 gl.bindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
647
648 if (contextSupports(type, ApiType::core(3,1)))
649 {
650 gl.disable (GL_PRIMITIVE_RESTART);
651 gl.primitiveRestartIndex(0);
652 }
653
654 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex attrib array state reset failed");
655 }
656
657 // Transformation state.
658 {
659 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
660 int numUserClipPlanes = 0;
661
662 gl.getIntegerv(GL_MAX_CLIP_DISTANCES, &numUserClipPlanes);
663
664 gl.viewport (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
665 gl.depthRange (0.0, 1.0);
666
667 for (int ndx = 0; ndx < numUserClipPlanes; ndx++)
668 gl.disable(GL_CLIP_DISTANCE0+ndx);
669
670 if (contextSupports(type, ApiType::core(3,2)))
671 gl.disable(GL_DEPTH_CLAMP);
672
673 //gl.bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
674
675 GLU_EXPECT_NO_ERROR(gl.getError(), "Transformation state reset failed");
676 }
677
678 // Coloring
679 {
680 gl.clampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
681
682 if (contextSupports(type, ApiType::core(3,2)))
683 gl.provokingVertex(GL_LAST_VERTEX_CONVENTION);
684
685 GLU_EXPECT_NO_ERROR(gl.getError(), "Coloring state reset failed");
686 }
687
688 // Rasterization state
689 {
690 gl.disable (GL_RASTERIZER_DISCARD);
691 gl.pointSize (1.0f);
692 gl.pointParameterf (GL_POINT_FADE_THRESHOLD_SIZE, 1.0f);
693 gl.pointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT);
694 gl.lineWidth (1.0f);
695 gl.disable (GL_LINE_SMOOTH);
696 gl.disable (GL_CULL_FACE);
697 gl.cullFace (GL_BACK);
698 gl.frontFace (GL_CCW);
699 gl.disable (GL_POLYGON_SMOOTH);
700 gl.polygonOffset (0.0f, 0.0f);
701 gl.disable (GL_POLYGON_OFFSET_POINT);
702 gl.disable (GL_POLYGON_OFFSET_LINE);
703 gl.disable (GL_POLYGON_OFFSET_FILL);
704
705 GLU_EXPECT_NO_ERROR(gl.getError(), "Rasterization state reset failed");
706 }
707
708 // Multisampling state
709 {
710 gl.enable (GL_MULTISAMPLE);
711 gl.disable (GL_SAMPLE_ALPHA_TO_COVERAGE);
712 gl.disable (GL_SAMPLE_ALPHA_TO_ONE);
713 gl.disable (GL_SAMPLE_COVERAGE);
714 gl.sampleCoverage (1.0f, GL_FALSE);
715
716 if (contextSupports(type, ApiType::core(3,2)))
717 {
718 int numSampleMaskWords = 0;
719 gl.getIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numSampleMaskWords);
720
721 gl.disable(GL_SAMPLE_MASK);
722
723 for (int ndx = 0; ndx < numSampleMaskWords; ndx++)
724 gl.sampleMaski(ndx, ~0u);
725 }
726
727 GLU_EXPECT_NO_ERROR(gl.getError(), "Multisampling state reset failed");
728 }
729
730 // Texture state.
731 // \todo [2013-04-08 pyry] Reset all levels?
732 {
733 const float borderColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
734 int numTexUnits = 0;
735 gl.getIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numTexUnits);
736
737 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
738
739 for (int ndx = 0; ndx < numTexUnits; ndx++)
740 {
741 gl.activeTexture(GL_TEXTURE0 + ndx);
742
743 // Reset 1D texture.
744 gl.bindTexture (GL_TEXTURE_1D, 0);
745 gl.texImage1D (GL_TEXTURE_1D, 0, GL_RGBA, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
746 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
747 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
748 gl.texParameterfv (GL_TEXTURE_1D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
749 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
750 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MIN_LOD, -1000.0f);
751 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MAX_LOD, 1000.0f);
752 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
753 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 1000);
754 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_LOD_BIAS, 0.0f);
755 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
756 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
757
758 if (contextSupports(type, ApiType::core(3,3)))
759 {
760 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_R, GL_RED);
761 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
762 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
763 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
764 }
765
766 // Reset 2D texture.
767 gl.bindTexture (GL_TEXTURE_2D, 0);
768 gl.texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
769 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
770 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
771 gl.texParameterfv (GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
772 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
773 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
774 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1000.0f);
775 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1000.0f);
776 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
777 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
778 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f);
779 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
780 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
781
782 if (contextSupports(type, ApiType::core(3,3)))
783 {
784 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
785 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
786 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
787 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
788 }
789
790 // Reset cube map texture.
791 gl.bindTexture (GL_TEXTURE_CUBE_MAP, 0);
792 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
793 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
794 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
795 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
796 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
797 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
798 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
799 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
800 gl.texParameterfv (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
801 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
802 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
803 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_LOD, -1000.0f);
804 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LOD, 1000.0f);
805 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
806 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 1000);
807 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_LOD_BIAS, 0.0f);
808 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
809 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
810
811 if (contextSupports(type, ApiType::core(3,3)))
812 {
813 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_R, GL_RED);
814 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
815 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
816 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
817 }
818
819 // Reset 1D array texture.
820 gl.bindTexture (GL_TEXTURE_1D_ARRAY, 0);
821 gl.texImage2D (GL_TEXTURE_1D_ARRAY, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
822 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
823 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
824 gl.texParameterfv (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
825 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
826 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
827 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
828 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
829 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
830 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
831 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
832 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
833
834 if (contextSupports(type, ApiType::core(3,3)))
835 {
836 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
837 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
838 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
839 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
840 }
841
842 // Reset 2D array texture.
843 gl.bindTexture (GL_TEXTURE_2D_ARRAY, 0);
844 gl.texImage3D (GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
845 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
846 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
847 gl.texParameterfv (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
848 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
849 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
850 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
851 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
852 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
853 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
854 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
855 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
856 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
857
858 if (contextSupports(type, ApiType::core(3,3)))
859 {
860 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
861 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
862 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
863 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
864 }
865
866 // Reset 3D texture.
867 gl.bindTexture (GL_TEXTURE_3D, 0);
868 gl.texImage3D (GL_TEXTURE_3D, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
869 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
870 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
871 gl.texParameterfv (GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
872 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
873 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
874 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
875 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, -1000.0f);
876 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 1000.0f);
877 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
878 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1000);
879 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_LOD_BIAS, 0.0f);
880 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
881 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
882
883 if (contextSupports(type, ApiType::core(3,3)))
884 {
885 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
886 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
887 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
888 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
889 }
890
891 if (contextSupports(type, ApiType::core(3,1)))
892 {
893 // Reset rectangle texture.
894 gl.bindTexture (GL_TEXTURE_RECTANGLE, 0);
895 gl.texImage2D (GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
896 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
897 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
898 gl.texParameterfv (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
899 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
900 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
901 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BASE_LEVEL, 0);
902 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAX_LEVEL, 1000);
903 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_MODE, GL_NONE);
904 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
905 // \todo [2013-06-17 pyry] Drivers don't accept GL_MIN_LOD, GL_MAX_LOD for rectangle textures. Is that okay?
906
907 if (contextSupports(type, ApiType::core(3,3)))
908 {
909 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_R, GL_RED);
910 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
911 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
912 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
913 }
914
915 // Reset buffer texture.
916 gl.bindTexture (GL_TEXTURE_BUFFER, 0);
917 gl.texBuffer (GL_TEXTURE_BUFFER, GL_R8, 0);
918 // \todo [2013-05-04 pyry] Which parameters apply to buffer textures?
919 }
920
921 if (contextSupports(type, ApiType::core(3,2)))
922 {
923 // Reset 2D multisample texture.
924 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE, 0);
925 gl.texImage2DMultisample (GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 0, 0, GL_TRUE);
926
927 // Reset 2D multisample array texture.
928 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0);
929 gl.texImage3DMultisample (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 0, 0, 0, GL_TRUE);
930 }
931 }
932
933 gl.activeTexture(GL_TEXTURE0);
934
935 if (contextSupports(type, ApiType::core(3,3)))
936 {
937 for (int ndx = 0; ndx < numTexUnits; ndx++)
938 gl.bindSampler(ndx, 0);
939
940 gl.disable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
941 }
942
943 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state reset failed");
944 }
945
946 // Pixel operations.
947 {
948 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
949
950 gl.disable (GL_SCISSOR_TEST);
951 gl.scissor (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
952
953 gl.disable (GL_STENCIL_TEST);
954 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
955 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
956
957 gl.disable (GL_DEPTH_TEST);
958 gl.depthFunc (GL_LESS);
959
960 gl.disable (GL_BLEND);
961 gl.blendFunc (GL_ONE, GL_ZERO);
962 gl.blendEquation(GL_FUNC_ADD);
963 gl.blendColor (0.0f, 0.0f, 0.0f, 0.0f);
964
965 gl.disable (GL_FRAMEBUFFER_SRGB);
966 gl.enable (GL_DITHER);
967
968 gl.disable (GL_COLOR_LOGIC_OP);
969 gl.logicOp (GL_COPY);
970
971 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
972 }
973
974 // Framebuffer control.
975 {
976 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
977 gl.depthMask (GL_TRUE);
978 gl.stencilMask (~0u);
979
980 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
981 gl.clearDepth (1.0);
982 gl.clearStencil (0);
983
984 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
985 }
986
987 // Framebuffer state.
988 {
989 // \todo [2013-04-05 pyry] Single-buffered rendering: use GL_FRONT
990 deUint32 framebuffer = renderCtx.getDefaultFramebuffer();
991 deUint32 drawReadBuffer = framebuffer == 0 ? GL_BACK : GL_COLOR_ATTACHMENT0;
992
993 gl.bindFramebuffer (GL_FRAMEBUFFER, renderCtx.getDefaultFramebuffer());
994 gl.drawBuffer (drawReadBuffer);
995 gl.readBuffer (drawReadBuffer);
996
997 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
998 }
999
1000 // Renderbuffer state.
1001 {
1002 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
1003 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
1004 }
1005
1006 // Pixel transfer state.
1007 {
1008 gl.pixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
1009 gl.pixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
1010 gl.pixelStorei (GL_UNPACK_IMAGE_HEIGHT, 0);
1011 gl.pixelStorei (GL_UNPACK_SKIP_IMAGES, 0);
1012 gl.pixelStorei (GL_UNPACK_ROW_LENGTH, 0);
1013 gl.pixelStorei (GL_UNPACK_SKIP_ROWS, 0);
1014 gl.pixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
1015 gl.pixelStorei (GL_UNPACK_ALIGNMENT, 4);
1016
1017 gl.pixelStorei (GL_PACK_SWAP_BYTES, GL_FALSE);
1018 gl.pixelStorei (GL_PACK_LSB_FIRST, GL_FALSE);
1019 gl.pixelStorei (GL_PACK_IMAGE_HEIGHT, 0);
1020 gl.pixelStorei (GL_PACK_SKIP_IMAGES, 0);
1021 gl.pixelStorei (GL_PACK_ROW_LENGTH, 0);
1022 gl.pixelStorei (GL_PACK_SKIP_ROWS, 0);
1023 gl.pixelStorei (GL_PACK_SKIP_PIXELS, 0);
1024 gl.pixelStorei (GL_PACK_ALIGNMENT, 4);
1025
1026 gl.bindBuffer (GL_PIXEL_PACK_BUFFER, 0);
1027 gl.bindBuffer (GL_PIXEL_UNPACK_BUFFER, 0);
1028
1029 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
1030 }
1031
1032 // Program object state.
1033 {
1034 gl.useProgram(0);
1035
1036 if (contextSupports(type, ApiType::core(3,1)))
1037 {
1038 int maxUniformBufferBindings = 0;
1039 gl.getIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
1040
1041 gl.bindBuffer(GL_UNIFORM_BUFFER, 0);
1042
1043 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
1044 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
1045 }
1046
1047 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
1048 }
1049
1050 // Vertex shader state.
1051 {
1052 int numVertexAttribArrays = 0;
1053 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
1054
1055 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
1056 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
1057
1058 gl.disable(GL_VERTEX_PROGRAM_POINT_SIZE);
1059
1060 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
1061 }
1062
1063 // Transform feedback state.
1064 {
1065 int numTransformFeedbackSeparateAttribs = 0;
1066 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
1067
1068 if (contextSupports(type, ApiType::core(4,0)))
1069 {
1070 glw::GLboolean transformFeedbackActive = 0;
1071 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
1072
1073 if (transformFeedbackActive)
1074 gl.endTransformFeedback();
1075 }
1076
1077 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
1078
1079 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
1080 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
1081
1082 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
1083 }
1084
1085 // Asynchronous query state.
1086 {
1087 deUint32 queryTargets[8];
1088 int numTargets = 0;
1089
1090 queryTargets[numTargets++] = GL_PRIMITIVES_GENERATED;
1091 queryTargets[numTargets++] = GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN;
1092 queryTargets[numTargets++] = GL_SAMPLES_PASSED;
1093
1094 DE_ASSERT(numTargets <= DE_LENGTH_OF_ARRAY(queryTargets));
1095
1096 for (int i = 0; i < numTargets; i++)
1097 {
1098 int queryActive = 0;
1099 gl.getQueryiv(queryTargets[i], GL_CURRENT_QUERY, &queryActive);
1100
1101 if (queryActive != 0)
1102 gl.endQuery(queryTargets[i]);
1103 }
1104
1105 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
1106 }
1107
1108 // Hints.
1109 {
1110 gl.hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
1111 gl.hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
1112 gl.hint(GL_TEXTURE_COMPRESSION_HINT, GL_DONT_CARE);
1113 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
1114
1115 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
1116 }
1117
1118 // Buffer copy state.
1119 if (contextSupports(type, ApiType::core(3,1)))
1120 {
1121 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
1122 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
1123
1124 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
1125 }
1126
1127 // Debug state
1128 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
1129 {
1130 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
1131 gl.debugMessageCallback != DE_NULL;
1132
1133 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
1134 if (entrypointsPresent)
1135 {
1136 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
1137 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
1138 gl.debugMessageCallback(DE_NULL, DE_NULL);
1139
1140 if (type.getFlags() & glu::CONTEXT_DEBUG)
1141 gl.enable(GL_DEBUG_OUTPUT);
1142 else
1143 gl.disable(GL_DEBUG_OUTPUT);
1144 }
1145 }
1146 }
1147
resetState(const RenderContext & renderCtx,const ContextInfo & ctxInfo)1148 void resetState (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
1149 {
1150 if (isContextTypeES(renderCtx.getType()))
1151 resetStateES(renderCtx, ctxInfo);
1152 else if (isContextTypeGLCore(renderCtx.getType()))
1153 resetStateGLCore(renderCtx, ctxInfo);
1154 else
1155 throw tcu::InternalError("State reset requested for unsupported context type");
1156 }
1157
1158 } // glu
1159