1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES Utilities
3 * ------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief OpenGL State Reset.
22 *//*--------------------------------------------------------------------*/
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"
41 void resetErrors (const glw::Functions& gl)
45 for (errorNdx = 0; errorNdx < MAX_ERROR_COUNT; errorNdx++)
47 if (gl.getError() == GL_NONE)
51 if (errorNdx == MAX_ERROR_COUNT)
52 TCU_FAIL("Couldn't reset error state");
57 void resetStateES (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
59 const glw::Functions& gl = renderCtx.getFunctions();
60 const ContextType type = renderCtx.getType();
65 DE_ASSERT(isContextTypeES(type));
67 // Vertex attrib array state.
69 int numVertexAttribArrays = 0;
70 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
72 gl.bindBuffer (GL_ARRAY_BUFFER, 0);
73 gl.bindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
75 if (contextSupports(type, ApiType::es(3,0)))
77 gl.bindVertexArray (0);
78 gl.disable (GL_PRIMITIVE_RESTART_FIXED_INDEX);
81 if (contextSupports(type, ApiType::es(3,1)))
82 gl.bindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
84 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
86 gl.disableVertexAttribArray (ndx);
87 gl.vertexAttribPointer (ndx, 4, GL_FLOAT, GL_FALSE, 0, DE_NULL);
89 if (contextSupports(type, ApiType::es(3,0)))
90 gl.vertexAttribDivisor(ndx, 0);
93 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex attrib array state reset failed");
96 // Transformation state.
98 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
100 gl.viewport (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
101 gl.depthRangef (0.0f, 1.0f);
103 if (contextSupports(type, ApiType::es(3,0)))
104 gl.bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
106 GLU_EXPECT_NO_ERROR(gl.getError(), "Transformation state reset failed");
109 // Rasterization state
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);
118 if (contextSupports(type, ApiType::es(3,0)))
119 gl.disable(GL_RASTERIZER_DISCARD);
121 GLU_EXPECT_NO_ERROR(gl.getError(), "Rasterization state reset failed");
124 // Multisampling state
126 gl.disable (GL_SAMPLE_ALPHA_TO_COVERAGE);
127 gl.disable (GL_SAMPLE_COVERAGE);
128 gl.sampleCoverage (1.0f, GL_FALSE);
130 if (contextSupports(type, ApiType::es(3,1)))
132 int numSampleMaskWords = 0;
133 gl.getIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numSampleMaskWords);
135 gl.disable(GL_SAMPLE_MASK);
137 for (int ndx = 0; ndx < numSampleMaskWords; ndx++)
138 gl.sampleMaski(ndx, ~0u);
141 GLU_EXPECT_NO_ERROR(gl.getError(), "Multisampling state reset failed");
145 // \todo [2013-04-08 pyry] Reset all levels?
147 const float borderColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
149 const bool supportsBorderClamp = ctxInfo.isExtensionSupported("GL_EXT_texture_border_clamp") || contextSupports(type, ApiType::es(3,2));
151 gl.getIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numTexUnits);
153 for (int ndx = 0; ndx < numTexUnits; ndx++)
155 gl.activeTexture(GL_TEXTURE0 + ndx);
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);
165 if (contextSupports(type, ApiType::es(3,0)))
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);
179 if (contextSupports(type, ApiType::es(3,1)))
180 gl.texParameteri(GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
182 if (supportsBorderClamp)
183 gl.texParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
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);
198 if (contextSupports(type, ApiType::es(3,0)))
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);
212 if (contextSupports(type, ApiType::es(3,1)))
213 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
215 if (supportsBorderClamp)
216 gl.texParameterfv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
218 if (contextSupports(type, ApiType::es(3,0)))
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);
238 if (supportsBorderClamp)
239 gl.texParameterfv(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
242 if (contextSupports(type, ApiType::es(3,1)))
243 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
245 if (contextSupports(type, ApiType::es(3,0)))
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);
266 if (supportsBorderClamp)
267 gl.texParameterfv(GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
270 if (contextSupports(type, ApiType::es(3,1)))
271 gl.texParameteri(GL_TEXTURE_3D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT);
273 if (contextSupports(type, ApiType::es(3,1)))
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);
285 if (ctxInfo.isExtensionSupported("GL_OES_texture_storage_multisample_2d_array"))
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);
296 if (ctxInfo.isExtensionSupported("GL_EXT_texture_cube_map_array"))
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);
315 if (supportsBorderClamp)
316 gl.texParameterfv(GL_TEXTURE_CUBE_MAP_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
320 gl.activeTexture(GL_TEXTURE0);
322 if (contextSupports(type, ApiType::es(3,0)))
324 for (int ndx = 0; ndx < numTexUnits; ndx++)
325 gl.bindSampler(ndx, 0);
328 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state reset failed");
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"))
336 int numDrawBuffers = 0;
338 gl.getIntegerv(GL_MAX_DRAW_BUFFERS, &numDrawBuffers);
340 for (int drawBufferNdx = 0; drawBufferNdx < numDrawBuffers; drawBufferNdx++)
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);
348 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to reset indexed draw buffer state");
353 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
355 gl.disable (GL_SCISSOR_TEST);
356 gl.scissor (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
358 gl.disable (GL_STENCIL_TEST);
359 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
360 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
362 gl.disable (GL_DEPTH_TEST);
363 gl.depthFunc (GL_LESS);
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);
370 gl.enable (GL_DITHER);
372 if (ctxInfo.isExtensionSupported("GL_EXT_sRGB_write_control"))
374 gl.enable (GL_FRAMEBUFFER_SRGB);
377 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
380 // Framebuffer control.
382 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
383 gl.depthMask (GL_TRUE);
384 gl.stencilMask (~0u);
386 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
387 gl.clearDepthf (1.0f);
390 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
393 // Framebuffer state.
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;
400 gl.bindFramebuffer(GL_FRAMEBUFFER, defaultFbo);
402 if (contextSupports(type, ApiType::es(3,0)))
404 gl.drawBuffers (1, &drawBuffer);
405 gl.readBuffer (readBuffer);
408 if (contextSupports(type, ApiType::es(3, 1)) && defaultFbo != 0)
410 gl.framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH, 0);
411 gl.framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT, 0);
412 gl.framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_SAMPLES, 0);
413 gl.framebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, GL_FALSE);
414 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer default state reset failed");
417 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
420 // Renderbuffer state.
422 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
423 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
426 // Pixel transfer state.
428 gl.pixelStorei(GL_UNPACK_ALIGNMENT, 4);
429 gl.pixelStorei(GL_PACK_ALIGNMENT, 4);
431 if (contextSupports(type, ApiType::es(3,0)))
433 gl.pixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
434 gl.pixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
435 gl.pixelStorei(GL_UNPACK_ROW_LENGTH, 0);
436 gl.pixelStorei(GL_UNPACK_SKIP_ROWS, 0);
437 gl.pixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
439 gl.pixelStorei(GL_PACK_ROW_LENGTH, 0);
440 gl.pixelStorei(GL_PACK_SKIP_ROWS, 0);
441 gl.pixelStorei(GL_PACK_SKIP_PIXELS, 0);
443 gl.bindBuffer(GL_PIXEL_PACK_BUFFER, 0);
444 gl.bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
447 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
450 // Program object state.
454 if (contextSupports(type, ApiType::es(3,0)))
456 int maxUniformBufferBindings = 0;
457 gl.getIntegerv (GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
458 gl.bindBuffer (GL_UNIFORM_BUFFER, 0);
460 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
461 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
464 if (contextSupports(type, ApiType::es(3,1)))
466 gl.bindProgramPipeline(0);
469 int maxAtomicCounterBufferBindings = 0;
470 gl.getIntegerv (GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS, &maxAtomicCounterBufferBindings);
471 gl.bindBuffer (GL_ATOMIC_COUNTER_BUFFER, 0);
473 for (int ndx = 0; ndx < maxAtomicCounterBufferBindings; ndx++)
474 gl.bindBufferBase(GL_ATOMIC_COUNTER_BUFFER, ndx, 0);
478 int maxShaderStorageBufferBindings = 0;
479 gl.getIntegerv (GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &maxShaderStorageBufferBindings);
480 gl.bindBuffer (GL_SHADER_STORAGE_BUFFER, 0);
482 for (int ndx = 0; ndx < maxShaderStorageBufferBindings; ndx++)
483 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, ndx, 0);
487 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
490 // Vertex shader state.
492 int numVertexAttribArrays = 0;
493 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
495 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
496 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
498 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
501 // Transform feedback state.
502 if (contextSupports(type, ApiType::es(3,0)))
504 int numTransformFeedbackSeparateAttribs = 0;
505 glw::GLboolean transformFeedbackActive = 0;
506 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
507 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
509 if (transformFeedbackActive)
510 gl.endTransformFeedback();
512 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
514 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
515 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
517 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
520 // Asynchronous query state.
521 if (contextSupports(type, ApiType::es(3,0)))
523 static const deUint32 targets[] = { GL_ANY_SAMPLES_PASSED, GL_ANY_SAMPLES_PASSED_CONSERVATIVE, GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
525 for (int i = 0; i < DE_LENGTH_OF_ARRAY(targets); i++)
528 gl.getQueryiv(targets[i], GL_CURRENT_QUERY, &queryActive);
530 if (queryActive != 0)
531 gl.endQuery(targets[i]);
534 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
539 gl.hint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
541 if (contextSupports(type, ApiType::es(3,0)))
542 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
544 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
548 if (contextSupports(type, ApiType::es(3,1)))
550 gl.bindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
551 GLU_EXPECT_NO_ERROR(gl.getError(), "Compute dispatch state reset failed");
554 // Buffer copy state.
555 if (contextSupports(type, ApiType::es(3,0)))
557 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
558 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
560 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
564 if (contextSupports(type, ApiType::es(3,1)))
566 int numImageUnits = 0;
567 gl.getIntegerv(GL_MAX_IMAGE_UNITS, &numImageUnits);
569 for (int ndx = 0; ndx < numImageUnits; ndx++)
570 gl.bindImageTexture(ndx, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
572 GLU_EXPECT_NO_ERROR(gl.getError(), "Image state reset failed");
575 // Sample shading state.
576 if (contextSupports(type, ApiType::es(3,1)) && ctxInfo.isExtensionSupported("GL_OES_sample_shading"))
578 gl.minSampleShading(0.0f);
579 gl.disable(GL_SAMPLE_SHADING);
581 GLU_EXPECT_NO_ERROR(gl.getError(), "Sample shading state reset failed");
585 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
587 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
588 gl.debugMessageCallback != DE_NULL &&
589 gl.popDebugGroup != DE_NULL;
591 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
592 if (entrypointsPresent)
595 gl.getIntegerv(GL_DEBUG_GROUP_STACK_DEPTH, &stackDepth);
596 for (int ndx = 1; ndx < stackDepth; ++ndx)
599 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
600 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
601 gl.debugMessageCallback(DE_NULL, DE_NULL);
603 if (type.getFlags() & glu::CONTEXT_DEBUG)
604 gl.enable(GL_DEBUG_OUTPUT);
606 gl.disable(GL_DEBUG_OUTPUT);
607 gl.disable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
609 GLU_EXPECT_NO_ERROR(gl.getError(), "Debug state reset failed");
613 // Primitive bounding box state.
614 if (ctxInfo.isExtensionSupported("GL_EXT_primitive_bounding_box"))
616 gl.primitiveBoundingBox(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
617 GLU_EXPECT_NO_ERROR(gl.getError(), "Primitive bounding box state reset failed");
620 // Tessellation state
621 if (ctxInfo.isExtensionSupported("GL_EXT_tessellation_shader"))
623 gl.patchParameteri(GL_PATCH_VERTICES, 3);
624 GLU_EXPECT_NO_ERROR(gl.getError(), "Tessellation patch vertices state reset failed");
627 // Advanced coherent blending
628 if (ctxInfo.isExtensionSupported("GL_KHR_blend_equation_advanced_coherent"))
630 gl.enable(GL_BLEND_ADVANCED_COHERENT_KHR);
631 GLU_EXPECT_NO_ERROR(gl.getError(), "Blend equation advanced coherent state reset failed");
635 if (ctxInfo.isExtensionSupported("GL_EXT_texture_buffer"))
637 gl.bindTexture(GL_TEXTURE_BUFFER, 0);
638 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
639 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture buffer state reset failed");
643 void resetStateGLCore (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
645 const glw::Functions& gl = renderCtx.getFunctions();
646 const ContextType type = renderCtx.getType();
651 // Primitives and vertices state
653 if (contextSupports(type, glu::ApiType::core(4, 0)))
655 const float defaultTessLevels[] = { 1.0f, 1.0f, 1.0f, 1.0f };
656 gl.patchParameteri(GL_PATCH_VERTICES_EXT, 3);
657 gl.patchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL, defaultTessLevels);
658 gl.patchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL, defaultTessLevels);
661 GLU_EXPECT_NO_ERROR(gl.getError(), "Primitives and vertices state reset failed");
664 // Vertex attrib array state.
666 gl.bindVertexArray (0);
667 gl.bindBuffer (GL_ARRAY_BUFFER, 0);
668 gl.bindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
670 if (contextSupports(type, ApiType::core(3,1)))
672 gl.disable (GL_PRIMITIVE_RESTART);
673 gl.primitiveRestartIndex(0);
676 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex attrib array state reset failed");
679 // Transformation state.
681 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
682 int numUserClipPlanes = 0;
684 gl.getIntegerv(GL_MAX_CLIP_DISTANCES, &numUserClipPlanes);
686 gl.viewport (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
687 gl.depthRange (0.0, 1.0);
689 for (int ndx = 0; ndx < numUserClipPlanes; ndx++)
690 gl.disable(GL_CLIP_DISTANCE0+ndx);
692 if (contextSupports(type, ApiType::core(3,2)))
693 gl.disable(GL_DEPTH_CLAMP);
695 //gl.bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
697 GLU_EXPECT_NO_ERROR(gl.getError(), "Transformation state reset failed");
702 gl.clampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
704 if (contextSupports(type, ApiType::core(3,2)))
705 gl.provokingVertex(GL_LAST_VERTEX_CONVENTION);
707 GLU_EXPECT_NO_ERROR(gl.getError(), "Coloring state reset failed");
710 // Rasterization state
712 gl.disable (GL_RASTERIZER_DISCARD);
714 gl.pointParameterf (GL_POINT_FADE_THRESHOLD_SIZE, 1.0f);
715 gl.pointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT);
717 gl.disable (GL_LINE_SMOOTH);
718 gl.disable (GL_CULL_FACE);
719 gl.cullFace (GL_BACK);
720 gl.frontFace (GL_CCW);
721 gl.disable (GL_POLYGON_SMOOTH);
722 gl.polygonOffset (0.0f, 0.0f);
723 gl.disable (GL_POLYGON_OFFSET_POINT);
724 gl.disable (GL_POLYGON_OFFSET_LINE);
725 gl.disable (GL_POLYGON_OFFSET_FILL);
727 GLU_EXPECT_NO_ERROR(gl.getError(), "Rasterization state reset failed");
730 // Multisampling state
732 gl.enable (GL_MULTISAMPLE);
733 gl.disable (GL_SAMPLE_ALPHA_TO_COVERAGE);
734 gl.disable (GL_SAMPLE_ALPHA_TO_ONE);
735 gl.disable (GL_SAMPLE_COVERAGE);
736 gl.sampleCoverage (1.0f, GL_FALSE);
738 if (contextSupports(type, ApiType::core(3,2)))
740 int numSampleMaskWords = 0;
741 gl.getIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numSampleMaskWords);
743 gl.disable(GL_SAMPLE_MASK);
745 for (int ndx = 0; ndx < numSampleMaskWords; ndx++)
746 gl.sampleMaski(ndx, ~0u);
749 GLU_EXPECT_NO_ERROR(gl.getError(), "Multisampling state reset failed");
753 // \todo [2013-04-08 pyry] Reset all levels?
755 const float borderColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
757 gl.getIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numTexUnits);
759 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
761 for (int ndx = 0; ndx < numTexUnits; ndx++)
763 gl.activeTexture(GL_TEXTURE0 + ndx);
766 gl.bindTexture (GL_TEXTURE_1D, 0);
767 gl.texImage1D (GL_TEXTURE_1D, 0, GL_RGBA, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
768 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
769 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
770 gl.texParameterfv (GL_TEXTURE_1D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
771 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
772 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MIN_LOD, -1000.0f);
773 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MAX_LOD, 1000.0f);
774 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
775 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 1000);
776 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_LOD_BIAS, 0.0f);
777 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
778 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
780 if (contextSupports(type, ApiType::core(3,3)))
782 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_R, GL_RED);
783 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
784 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
785 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
789 gl.bindTexture (GL_TEXTURE_2D, 0);
790 gl.texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
791 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
792 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
793 gl.texParameterfv (GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
794 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
795 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
796 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1000.0f);
797 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1000.0f);
798 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
799 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
800 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f);
801 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
802 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
804 if (contextSupports(type, ApiType::core(3,3)))
806 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
807 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
808 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
809 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
812 // Reset cube map texture.
813 gl.bindTexture (GL_TEXTURE_CUBE_MAP, 0);
814 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
815 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
816 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
817 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
818 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
819 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
820 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
821 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
822 gl.texParameterfv (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
823 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
824 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
825 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_LOD, -1000.0f);
826 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LOD, 1000.0f);
827 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
828 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 1000);
829 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_LOD_BIAS, 0.0f);
830 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
831 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
833 if (contextSupports(type, ApiType::core(3,3)))
835 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_R, GL_RED);
836 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
837 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
838 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
841 // Reset 1D array texture.
842 gl.bindTexture (GL_TEXTURE_1D_ARRAY, 0);
843 gl.texImage2D (GL_TEXTURE_1D_ARRAY, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
844 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
845 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
846 gl.texParameterfv (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
847 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
848 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
849 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
850 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
851 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
852 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
853 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
854 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
856 if (contextSupports(type, ApiType::core(3,3)))
858 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
859 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
860 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
861 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
864 // Reset 2D array texture.
865 gl.bindTexture (GL_TEXTURE_2D_ARRAY, 0);
866 gl.texImage3D (GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
867 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
868 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
869 gl.texParameterfv (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
870 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
871 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
872 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
873 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
874 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
875 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
876 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
877 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
878 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
880 if (contextSupports(type, ApiType::core(3,3)))
882 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
883 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
884 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
885 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
889 gl.bindTexture (GL_TEXTURE_3D, 0);
890 gl.texImage3D (GL_TEXTURE_3D, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
891 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
892 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
893 gl.texParameterfv (GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
894 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
895 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
896 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
897 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, -1000.0f);
898 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 1000.0f);
899 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
900 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1000);
901 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_LOD_BIAS, 0.0f);
902 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
903 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
905 if (contextSupports(type, ApiType::core(3,3)))
907 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
908 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
909 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
910 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
913 if (contextSupports(type, ApiType::core(3,1)))
915 // Reset rectangle texture.
916 gl.bindTexture (GL_TEXTURE_RECTANGLE, 0);
917 gl.texImage2D (GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
918 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
919 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
920 gl.texParameterfv (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
921 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
922 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
923 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BASE_LEVEL, 0);
924 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAX_LEVEL, 1000);
925 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_MODE, GL_NONE);
926 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
927 // \todo [2013-06-17 pyry] Drivers don't accept GL_MIN_LOD, GL_MAX_LOD for rectangle textures. Is that okay?
929 if (contextSupports(type, ApiType::core(3,3)))
931 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_R, GL_RED);
932 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
933 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
934 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
937 // Reset buffer texture.
938 gl.bindTexture (GL_TEXTURE_BUFFER, 0);
939 gl.texBuffer (GL_TEXTURE_BUFFER, GL_R8, 0);
940 // \todo [2013-05-04 pyry] Which parameters apply to buffer textures?
943 if (contextSupports(type, ApiType::core(3,2)))
945 // Reset 2D multisample texture.
946 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE, 0);
947 gl.texImage2DMultisample (GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 0, 0, GL_TRUE);
949 // Reset 2D multisample array texture.
950 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0);
951 gl.texImage3DMultisample (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 0, 0, 0, GL_TRUE);
955 gl.activeTexture(GL_TEXTURE0);
957 if (contextSupports(type, ApiType::core(3,3)))
959 for (int ndx = 0; ndx < numTexUnits; ndx++)
960 gl.bindSampler(ndx, 0);
962 gl.disable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
965 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state reset failed");
970 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
972 gl.disable (GL_SCISSOR_TEST);
973 gl.scissor (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
975 gl.disable (GL_STENCIL_TEST);
976 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
977 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
979 gl.disable (GL_DEPTH_TEST);
980 gl.depthFunc (GL_LESS);
982 gl.disable (GL_BLEND);
983 gl.blendFunc (GL_ONE, GL_ZERO);
984 gl.blendEquation(GL_FUNC_ADD);
985 gl.blendColor (0.0f, 0.0f, 0.0f, 0.0f);
987 gl.disable (GL_FRAMEBUFFER_SRGB);
988 gl.enable (GL_DITHER);
990 gl.disable (GL_COLOR_LOGIC_OP);
991 gl.logicOp (GL_COPY);
993 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
996 // Framebuffer control.
998 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
999 gl.depthMask (GL_TRUE);
1000 gl.stencilMask (~0u);
1002 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
1003 gl.clearDepth (1.0);
1004 gl.clearStencil (0);
1006 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
1009 // Framebuffer state.
1011 const deUint32 framebuffer = renderCtx.getDefaultFramebuffer();
1013 gl.bindFramebuffer(GL_FRAMEBUFFER, framebuffer);
1015 if (framebuffer == 0)
1017 gl.drawBuffer(GL_BACK);
1018 gl.readBuffer(GL_BACK);
1020 // This is a workaround for supporting single-buffered configurations.
1021 // Since there is no other place where we need to know if we are dealing
1022 // with single-buffered config, it is not worthwhile to add additional
1023 // state into RenderContext for that.
1024 if (gl.getError() != GL_NO_ERROR)
1026 gl.drawBuffer(GL_FRONT);
1027 gl.readBuffer(GL_FRONT);
1032 gl.drawBuffer(GL_COLOR_ATTACHMENT0);
1033 gl.readBuffer(GL_COLOR_ATTACHMENT0);
1036 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
1039 // Renderbuffer state.
1041 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
1042 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
1045 // Pixel transfer state.
1047 gl.pixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
1048 gl.pixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
1049 gl.pixelStorei (GL_UNPACK_IMAGE_HEIGHT, 0);
1050 gl.pixelStorei (GL_UNPACK_SKIP_IMAGES, 0);
1051 gl.pixelStorei (GL_UNPACK_ROW_LENGTH, 0);
1052 gl.pixelStorei (GL_UNPACK_SKIP_ROWS, 0);
1053 gl.pixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
1054 gl.pixelStorei (GL_UNPACK_ALIGNMENT, 4);
1056 gl.pixelStorei (GL_PACK_SWAP_BYTES, GL_FALSE);
1057 gl.pixelStorei (GL_PACK_LSB_FIRST, GL_FALSE);
1058 gl.pixelStorei (GL_PACK_IMAGE_HEIGHT, 0);
1059 gl.pixelStorei (GL_PACK_SKIP_IMAGES, 0);
1060 gl.pixelStorei (GL_PACK_ROW_LENGTH, 0);
1061 gl.pixelStorei (GL_PACK_SKIP_ROWS, 0);
1062 gl.pixelStorei (GL_PACK_SKIP_PIXELS, 0);
1063 gl.pixelStorei (GL_PACK_ALIGNMENT, 4);
1065 gl.bindBuffer (GL_PIXEL_PACK_BUFFER, 0);
1066 gl.bindBuffer (GL_PIXEL_UNPACK_BUFFER, 0);
1068 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
1071 // Program object state.
1075 if (contextSupports(type, ApiType::core(3,1)))
1077 int maxUniformBufferBindings = 0;
1078 gl.getIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
1080 gl.bindBuffer(GL_UNIFORM_BUFFER, 0);
1082 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
1083 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
1086 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
1089 // Vertex shader state.
1091 int numVertexAttribArrays = 0;
1092 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
1094 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
1095 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
1097 gl.disable(GL_VERTEX_PROGRAM_POINT_SIZE);
1099 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
1102 // Transform feedback state.
1104 int numTransformFeedbackSeparateAttribs = 0;
1105 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
1107 if (contextSupports(type, ApiType::core(4,0)))
1109 glw::GLboolean transformFeedbackActive = 0;
1110 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
1112 if (transformFeedbackActive)
1113 gl.endTransformFeedback();
1116 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
1118 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
1119 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
1121 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
1124 // Asynchronous query state.
1126 deUint32 queryTargets[8];
1129 queryTargets[numTargets++] = GL_PRIMITIVES_GENERATED;
1130 queryTargets[numTargets++] = GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN;
1131 queryTargets[numTargets++] = GL_SAMPLES_PASSED;
1133 DE_ASSERT(numTargets <= DE_LENGTH_OF_ARRAY(queryTargets));
1135 for (int i = 0; i < numTargets; i++)
1137 int queryActive = 0;
1138 gl.getQueryiv(queryTargets[i], GL_CURRENT_QUERY, &queryActive);
1140 if (queryActive != 0)
1141 gl.endQuery(queryTargets[i]);
1144 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
1149 gl.hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
1150 gl.hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
1151 gl.hint(GL_TEXTURE_COMPRESSION_HINT, GL_DONT_CARE);
1152 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
1154 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
1157 // Buffer copy state.
1158 if (contextSupports(type, ApiType::core(3,1)))
1160 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
1161 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
1163 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
1167 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
1169 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
1170 gl.debugMessageCallback != DE_NULL;
1172 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
1173 if (entrypointsPresent)
1175 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
1176 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
1177 gl.debugMessageCallback(DE_NULL, DE_NULL);
1179 if (type.getFlags() & glu::CONTEXT_DEBUG)
1180 gl.enable(GL_DEBUG_OUTPUT);
1182 gl.disable(GL_DEBUG_OUTPUT);
1187 void resetState (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
1189 if (isContextTypeES(renderCtx.getType()))
1190 resetStateES(renderCtx, ctxInfo);
1191 else if (isContextTypeGLCore(renderCtx.getType()))
1192 resetStateGLCore(renderCtx, ctxInfo);
1194 throw tcu::InternalError("State reset requested for unsupported context type");