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 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
375 // Framebuffer control.
377 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
378 gl.depthMask (GL_TRUE);
379 gl.stencilMask (~0u);
381 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
382 gl.clearDepthf (1.0f);
385 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
388 // Framebuffer state.
390 // \note Actually spec explictly says 0 but on some platforms (iOS) no default framebuffer exists.
391 const deUint32 defaultFbo = renderCtx.getDefaultFramebuffer();
392 const deUint32 drawBuffer = defaultFbo != 0 ? GL_COLOR_ATTACHMENT0 : GL_BACK;
393 const deUint32 readBuffer = defaultFbo != 0 ? GL_COLOR_ATTACHMENT0 : GL_BACK;
395 gl.bindFramebuffer(GL_FRAMEBUFFER, defaultFbo);
397 if (contextSupports(type, ApiType::es(3,0)))
399 gl.drawBuffers (1, &drawBuffer);
400 gl.readBuffer (readBuffer);
403 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
406 // Renderbuffer state.
408 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
409 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
412 // Pixel transfer state.
414 gl.pixelStorei(GL_UNPACK_ALIGNMENT, 4);
415 gl.pixelStorei(GL_PACK_ALIGNMENT, 4);
417 if (contextSupports(type, ApiType::es(3,0)))
419 gl.pixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
420 gl.pixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
421 gl.pixelStorei(GL_UNPACK_ROW_LENGTH, 0);
422 gl.pixelStorei(GL_UNPACK_SKIP_ROWS, 0);
423 gl.pixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
425 gl.pixelStorei(GL_PACK_ROW_LENGTH, 0);
426 gl.pixelStorei(GL_PACK_SKIP_ROWS, 0);
427 gl.pixelStorei(GL_PACK_SKIP_PIXELS, 0);
429 gl.bindBuffer(GL_PIXEL_PACK_BUFFER, 0);
430 gl.bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
433 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
436 // Program object state.
440 if (contextSupports(type, ApiType::es(3,0)))
442 int maxUniformBufferBindings = 0;
443 gl.getIntegerv (GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
444 gl.bindBuffer (GL_UNIFORM_BUFFER, 0);
446 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
447 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
450 if (contextSupports(type, ApiType::es(3,1)))
452 gl.bindProgramPipeline(0);
455 int maxAtomicCounterBufferBindings = 0;
456 gl.getIntegerv (GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS, &maxAtomicCounterBufferBindings);
457 gl.bindBuffer (GL_ATOMIC_COUNTER_BUFFER, 0);
459 for (int ndx = 0; ndx < maxAtomicCounterBufferBindings; ndx++)
460 gl.bindBufferBase(GL_ATOMIC_COUNTER_BUFFER, ndx, 0);
464 int maxShaderStorageBufferBindings = 0;
465 gl.getIntegerv (GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS, &maxShaderStorageBufferBindings);
466 gl.bindBuffer (GL_SHADER_STORAGE_BUFFER, 0);
468 for (int ndx = 0; ndx < maxShaderStorageBufferBindings; ndx++)
469 gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, ndx, 0);
473 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
476 // Vertex shader state.
478 int numVertexAttribArrays = 0;
479 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
481 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
482 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
484 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
487 // Transform feedback state.
488 if (contextSupports(type, ApiType::es(3,0)))
490 int numTransformFeedbackSeparateAttribs = 0;
491 glw::GLboolean transformFeedbackActive = 0;
492 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
493 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
495 if (transformFeedbackActive)
496 gl.endTransformFeedback();
498 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
500 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
501 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
503 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
506 // Asynchronous query state.
507 if (contextSupports(type, ApiType::es(3,0)))
509 static const deUint32 targets[] = { GL_ANY_SAMPLES_PASSED, GL_ANY_SAMPLES_PASSED_CONSERVATIVE, GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN };
511 for (int i = 0; i < DE_LENGTH_OF_ARRAY(targets); i++)
514 gl.getQueryiv(targets[i], GL_CURRENT_QUERY, &queryActive);
516 if (queryActive != 0)
517 gl.endQuery(targets[i]);
520 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
525 gl.hint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE);
527 if (contextSupports(type, ApiType::es(3,0)))
528 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
530 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
534 if (contextSupports(type, ApiType::es(3,1)))
536 gl.bindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0);
537 GLU_EXPECT_NO_ERROR(gl.getError(), "Compute dispatch state reset failed");
540 // Buffer copy state.
541 if (contextSupports(type, ApiType::es(3,0)))
543 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
544 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
546 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
550 if (contextSupports(type, ApiType::es(3,1)))
552 int numImageUnits = 0;
553 gl.getIntegerv(GL_MAX_IMAGE_UNITS, &numImageUnits);
555 for (int ndx = 0; ndx < numImageUnits; ndx++)
556 gl.bindImageTexture(ndx, 0, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32UI);
558 GLU_EXPECT_NO_ERROR(gl.getError(), "Image state reset failed");
561 // Sample shading state.
562 if (contextSupports(type, ApiType::es(3,1)) && ctxInfo.isExtensionSupported("GL_OES_sample_shading"))
564 gl.minSampleShading(0.0f);
565 gl.disable(GL_SAMPLE_SHADING);
567 GLU_EXPECT_NO_ERROR(gl.getError(), "Sample shading state reset failed");
571 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
573 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
574 gl.debugMessageCallback != DE_NULL &&
575 gl.popDebugGroup != DE_NULL;
577 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
578 if (entrypointsPresent)
581 gl.getIntegerv(GL_DEBUG_GROUP_STACK_DEPTH, &stackDepth);
582 for (int ndx = 1; ndx < stackDepth; ++ndx)
585 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
586 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
587 gl.debugMessageCallback(DE_NULL, DE_NULL);
589 if (type.getFlags() & glu::CONTEXT_DEBUG)
590 gl.enable(GL_DEBUG_OUTPUT);
592 gl.disable(GL_DEBUG_OUTPUT);
593 gl.disable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
595 GLU_EXPECT_NO_ERROR(gl.getError(), "Debug state reset failed");
599 // Primitive bounding box state.
600 if (ctxInfo.isExtensionSupported("GL_EXT_primitive_bounding_box"))
602 gl.primitiveBoundingBox(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f);
603 GLU_EXPECT_NO_ERROR(gl.getError(), "Primitive bounding box state reset failed");
606 // Tessellation state
607 if (ctxInfo.isExtensionSupported("GL_EXT_tessellation_shader"))
609 gl.patchParameteri(GL_PATCH_VERTICES, 3);
610 GLU_EXPECT_NO_ERROR(gl.getError(), "Tessellation patch vertices state reset failed");
613 // Advanced coherent blending
614 if (ctxInfo.isExtensionSupported("GL_KHR_blend_equation_advanced_coherent"))
616 gl.enable(GL_BLEND_ADVANCED_COHERENT_KHR);
617 GLU_EXPECT_NO_ERROR(gl.getError(), "Blend equation advanced coherent state reset failed");
621 if (ctxInfo.isExtensionSupported("GL_EXT_texture_buffer"))
623 gl.bindTexture(GL_TEXTURE_BUFFER, 0);
624 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
625 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture buffer state reset failed");
629 void resetStateGLCore (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
631 const glw::Functions& gl = renderCtx.getFunctions();
632 const ContextType type = renderCtx.getType();
637 // Vertex attrib array state.
639 gl.bindVertexArray (0);
640 gl.bindBuffer (GL_ARRAY_BUFFER, 0);
641 gl.bindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
643 if (contextSupports(type, ApiType::core(3,1)))
645 gl.disable (GL_PRIMITIVE_RESTART);
646 gl.primitiveRestartIndex(0);
649 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex attrib array state reset failed");
652 // Transformation state.
654 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
655 int numUserClipPlanes = 0;
657 gl.getIntegerv(GL_MAX_CLIP_DISTANCES, &numUserClipPlanes);
659 gl.viewport (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
660 gl.depthRange (0.0, 1.0);
662 for (int ndx = 0; ndx < numUserClipPlanes; ndx++)
663 gl.disable(GL_CLIP_DISTANCE0+ndx);
665 if (contextSupports(type, ApiType::core(3,2)))
666 gl.disable(GL_DEPTH_CLAMP);
668 //gl.bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
670 GLU_EXPECT_NO_ERROR(gl.getError(), "Transformation state reset failed");
675 gl.clampColor(GL_CLAMP_READ_COLOR, GL_FIXED_ONLY);
677 if (contextSupports(type, ApiType::core(3,2)))
678 gl.provokingVertex(GL_LAST_VERTEX_CONVENTION);
680 GLU_EXPECT_NO_ERROR(gl.getError(), "Coloring state reset failed");
683 // Rasterization state
685 gl.disable (GL_RASTERIZER_DISCARD);
687 gl.pointParameterf (GL_POINT_FADE_THRESHOLD_SIZE, 1.0f);
688 gl.pointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_UPPER_LEFT);
690 gl.disable (GL_LINE_SMOOTH);
691 gl.disable (GL_CULL_FACE);
692 gl.cullFace (GL_BACK);
693 gl.frontFace (GL_CCW);
694 gl.disable (GL_POLYGON_SMOOTH);
695 gl.polygonOffset (0.0f, 0.0f);
696 gl.disable (GL_POLYGON_OFFSET_POINT);
697 gl.disable (GL_POLYGON_OFFSET_LINE);
698 gl.disable (GL_POLYGON_OFFSET_FILL);
700 GLU_EXPECT_NO_ERROR(gl.getError(), "Rasterization state reset failed");
703 // Multisampling state
705 gl.enable (GL_MULTISAMPLE);
706 gl.disable (GL_SAMPLE_ALPHA_TO_COVERAGE);
707 gl.disable (GL_SAMPLE_ALPHA_TO_ONE);
708 gl.disable (GL_SAMPLE_COVERAGE);
709 gl.sampleCoverage (1.0f, GL_FALSE);
711 if (contextSupports(type, ApiType::core(3,2)))
713 int numSampleMaskWords = 0;
714 gl.getIntegerv(GL_MAX_SAMPLE_MASK_WORDS, &numSampleMaskWords);
716 gl.disable(GL_SAMPLE_MASK);
718 for (int ndx = 0; ndx < numSampleMaskWords; ndx++)
719 gl.sampleMaski(ndx, ~0u);
722 GLU_EXPECT_NO_ERROR(gl.getError(), "Multisampling state reset failed");
726 // \todo [2013-04-08 pyry] Reset all levels?
728 const float borderColor[] = { 0.0f, 0.0f, 0.0f, 0.0f };
730 gl.getIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numTexUnits);
732 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
734 for (int ndx = 0; ndx < numTexUnits; ndx++)
736 gl.activeTexture(GL_TEXTURE0 + ndx);
739 gl.bindTexture (GL_TEXTURE_1D, 0);
740 gl.texImage1D (GL_TEXTURE_1D, 0, GL_RGBA, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
741 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
742 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
743 gl.texParameterfv (GL_TEXTURE_1D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
744 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
745 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MIN_LOD, -1000.0f);
746 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_MAX_LOD, 1000.0f);
747 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);
748 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 1000);
749 gl.texParameterf (GL_TEXTURE_1D, GL_TEXTURE_LOD_BIAS, 0.0f);
750 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
751 gl.texParameteri (GL_TEXTURE_1D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
753 if (contextSupports(type, ApiType::core(3,3)))
755 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_R, GL_RED);
756 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
757 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
758 gl.texParameteri(GL_TEXTURE_1D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
762 gl.bindTexture (GL_TEXTURE_2D, 0);
763 gl.texImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
764 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
765 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
766 gl.texParameterfv (GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
767 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
768 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
769 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, -1000.0f);
770 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 1000.0f);
771 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
772 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);
773 gl.texParameterf (GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f);
774 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
775 gl.texParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
777 if (contextSupports(type, ApiType::core(3,3)))
779 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
780 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
781 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
782 gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
785 // Reset cube map texture.
786 gl.bindTexture (GL_TEXTURE_CUBE_MAP, 0);
787 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
788 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
789 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
790 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
791 gl.texImage2D (GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
792 gl.texImage2D (GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
793 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
794 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
795 gl.texParameterfv (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
796 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
797 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
798 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_LOD, -1000.0f);
799 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LOD, 1000.0f);
800 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
801 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 1000);
802 gl.texParameterf (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_LOD_BIAS, 0.0f);
803 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_MODE, GL_NONE);
804 gl.texParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
806 if (contextSupports(type, ApiType::core(3,3)))
808 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_R, GL_RED);
809 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
810 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
811 gl.texParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
814 // Reset 1D array texture.
815 gl.bindTexture (GL_TEXTURE_1D_ARRAY, 0);
816 gl.texImage2D (GL_TEXTURE_1D_ARRAY, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
817 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
818 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
819 gl.texParameterfv (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
820 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
821 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
822 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
823 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
824 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
825 gl.texParameterf (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
826 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
827 gl.texParameteri (GL_TEXTURE_1D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
829 if (contextSupports(type, ApiType::core(3,3)))
831 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
832 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
833 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
834 gl.texParameteri(GL_TEXTURE_1D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
837 // Reset 2D array texture.
838 gl.bindTexture (GL_TEXTURE_2D_ARRAY, 0);
839 gl.texImage3D (GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
840 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
841 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
842 gl.texParameterfv (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
843 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
844 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
845 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_LOD, -1000.0f);
846 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LOD, 1000.0f);
847 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BASE_LEVEL, 0);
848 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, 1000);
849 gl.texParameterf (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_LOD_BIAS, 0.0f);
850 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_NONE);
851 gl.texParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
853 if (contextSupports(type, ApiType::core(3,3)))
855 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_R, GL_RED);
856 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
857 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
858 gl.texParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
862 gl.bindTexture (GL_TEXTURE_3D, 0);
863 gl.texImage3D (GL_TEXTURE_3D, 0, GL_RGBA, 0, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
864 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
865 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
866 gl.texParameterfv (GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
867 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
868 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
869 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
870 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MIN_LOD, -1000.0f);
871 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_MAX_LOD, 1000.0f);
872 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
873 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 1000);
874 gl.texParameterf (GL_TEXTURE_3D, GL_TEXTURE_LOD_BIAS, 0.0f);
875 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
876 gl.texParameteri (GL_TEXTURE_3D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
878 if (contextSupports(type, ApiType::core(3,3)))
880 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_R, GL_RED);
881 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
882 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
883 gl.texParameteri(GL_TEXTURE_3D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
886 if (contextSupports(type, ApiType::core(3,1)))
888 // Reset rectangle texture.
889 gl.bindTexture (GL_TEXTURE_RECTANGLE, 0);
890 gl.texImage2D (GL_TEXTURE_RECTANGLE, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, DE_NULL);
891 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
892 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
893 gl.texParameterfv (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BORDER_COLOR, &borderColor[0]);
894 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
895 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
896 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_BASE_LEVEL, 0);
897 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAX_LEVEL, 1000);
898 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_MODE, GL_NONE);
899 gl.texParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
900 // \todo [2013-06-17 pyry] Drivers don't accept GL_MIN_LOD, GL_MAX_LOD for rectangle textures. Is that okay?
902 if (contextSupports(type, ApiType::core(3,3)))
904 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_R, GL_RED);
905 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
906 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
907 gl.texParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
910 // Reset buffer texture.
911 gl.bindTexture (GL_TEXTURE_BUFFER, 0);
912 gl.texBuffer (GL_TEXTURE_BUFFER, GL_R8, 0);
913 // \todo [2013-05-04 pyry] Which parameters apply to buffer textures?
916 if (contextSupports(type, ApiType::core(3,2)))
918 // Reset 2D multisample texture.
919 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE, 0);
920 gl.texImage2DMultisample (GL_TEXTURE_2D_MULTISAMPLE, 1, GL_RGBA8, 0, 0, GL_TRUE);
922 // Reset 2D multisample array texture.
923 gl.bindTexture (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0);
924 gl.texImage3DMultisample (GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 1, GL_RGBA8, 0, 0, 0, GL_TRUE);
928 gl.activeTexture(GL_TEXTURE0);
930 if (contextSupports(type, ApiType::core(3,3)))
932 for (int ndx = 0; ndx < numTexUnits; ndx++)
933 gl.bindSampler(ndx, 0);
935 gl.disable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
938 GLU_EXPECT_NO_ERROR(gl.getError(), "Texture state reset failed");
943 const tcu::RenderTarget& renderTarget = renderCtx.getRenderTarget();
945 gl.disable (GL_SCISSOR_TEST);
946 gl.scissor (0, 0, renderTarget.getWidth(), renderTarget.getHeight());
948 gl.disable (GL_STENCIL_TEST);
949 gl.stencilFunc (GL_ALWAYS, 0, ~0u);
950 gl.stencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
952 gl.disable (GL_DEPTH_TEST);
953 gl.depthFunc (GL_LESS);
955 gl.disable (GL_BLEND);
956 gl.blendFunc (GL_ONE, GL_ZERO);
957 gl.blendEquation(GL_FUNC_ADD);
958 gl.blendColor (0.0f, 0.0f, 0.0f, 0.0f);
960 gl.disable (GL_FRAMEBUFFER_SRGB);
961 gl.enable (GL_DITHER);
963 gl.disable (GL_COLOR_LOGIC_OP);
964 gl.logicOp (GL_COPY);
966 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel operation state reset failed");
969 // Framebuffer control.
971 gl.colorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
972 gl.depthMask (GL_TRUE);
973 gl.stencilMask (~0u);
975 gl.clearColor (0.0f, 0.0f, 0.0f, 0.0f);
979 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer control state reset failed");
982 // Framebuffer state.
984 // \todo [2013-04-05 pyry] Single-buffered rendering: use GL_FRONT
985 deUint32 framebuffer = renderCtx.getDefaultFramebuffer();
986 deUint32 drawReadBuffer = framebuffer == 0 ? GL_BACK : GL_COLOR_ATTACHMENT0;
988 gl.bindFramebuffer (GL_FRAMEBUFFER, renderCtx.getDefaultFramebuffer());
989 gl.drawBuffer (drawReadBuffer);
990 gl.readBuffer (drawReadBuffer);
992 GLU_EXPECT_NO_ERROR(gl.getError(), "Framebuffer state reset failed");
995 // Renderbuffer state.
997 gl.bindRenderbuffer(GL_RENDERBUFFER, 0);
998 GLU_EXPECT_NO_ERROR(gl.getError(), "Renderbuffer state reset failed");
1001 // Pixel transfer state.
1003 gl.pixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
1004 gl.pixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
1005 gl.pixelStorei (GL_UNPACK_IMAGE_HEIGHT, 0);
1006 gl.pixelStorei (GL_UNPACK_SKIP_IMAGES, 0);
1007 gl.pixelStorei (GL_UNPACK_ROW_LENGTH, 0);
1008 gl.pixelStorei (GL_UNPACK_SKIP_ROWS, 0);
1009 gl.pixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
1010 gl.pixelStorei (GL_UNPACK_ALIGNMENT, 4);
1012 gl.pixelStorei (GL_PACK_SWAP_BYTES, GL_FALSE);
1013 gl.pixelStorei (GL_PACK_LSB_FIRST, GL_FALSE);
1014 gl.pixelStorei (GL_PACK_IMAGE_HEIGHT, 0);
1015 gl.pixelStorei (GL_PACK_SKIP_IMAGES, 0);
1016 gl.pixelStorei (GL_PACK_ROW_LENGTH, 0);
1017 gl.pixelStorei (GL_PACK_SKIP_ROWS, 0);
1018 gl.pixelStorei (GL_PACK_SKIP_PIXELS, 0);
1019 gl.pixelStorei (GL_PACK_ALIGNMENT, 4);
1021 gl.bindBuffer (GL_PIXEL_PACK_BUFFER, 0);
1022 gl.bindBuffer (GL_PIXEL_UNPACK_BUFFER, 0);
1024 GLU_EXPECT_NO_ERROR(gl.getError(), "Pixel transfer state reset failed");
1027 // Program object state.
1031 if (contextSupports(type, ApiType::core(3,1)))
1033 int maxUniformBufferBindings = 0;
1034 gl.getIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
1036 gl.bindBuffer(GL_UNIFORM_BUFFER, 0);
1038 for (int ndx = 0; ndx < maxUniformBufferBindings; ndx++)
1039 gl.bindBufferBase(GL_UNIFORM_BUFFER, ndx, 0);
1042 GLU_EXPECT_NO_ERROR(gl.getError(), "Program object state reset failed");
1045 // Vertex shader state.
1047 int numVertexAttribArrays = 0;
1048 gl.getIntegerv(GL_MAX_VERTEX_ATTRIBS, &numVertexAttribArrays);
1050 for (int ndx = 0; ndx < numVertexAttribArrays; ndx++)
1051 gl.vertexAttrib4f(ndx, 0.0f, 0.0f, 0.0f, 1.0f);
1053 gl.disable(GL_VERTEX_PROGRAM_POINT_SIZE);
1055 GLU_EXPECT_NO_ERROR(gl.getError(), "Vertex shader state reset failed");
1058 // Transform feedback state.
1060 int numTransformFeedbackSeparateAttribs = 0;
1061 gl.getIntegerv(GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS, &numTransformFeedbackSeparateAttribs);
1063 if (contextSupports(type, ApiType::core(4,0)))
1065 glw::GLboolean transformFeedbackActive = 0;
1066 gl.getBooleanv(GL_TRANSFORM_FEEDBACK_ACTIVE, &transformFeedbackActive);
1068 if (transformFeedbackActive)
1069 gl.endTransformFeedback();
1072 gl.bindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
1074 for (int ndx = 0; ndx < numTransformFeedbackSeparateAttribs; ndx++)
1075 gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, ndx, 0);
1077 GLU_EXPECT_NO_ERROR(gl.getError(), "Transform feedback state reset failed");
1080 // Asynchronous query state.
1082 deUint32 queryTargets[8];
1085 queryTargets[numTargets++] = GL_PRIMITIVES_GENERATED;
1086 queryTargets[numTargets++] = GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN;
1087 queryTargets[numTargets++] = GL_SAMPLES_PASSED;
1089 DE_ASSERT(numTargets <= DE_LENGTH_OF_ARRAY(queryTargets));
1091 for (int i = 0; i < numTargets; i++)
1093 int queryActive = 0;
1094 gl.getQueryiv(queryTargets[i], GL_CURRENT_QUERY, &queryActive);
1096 if (queryActive != 0)
1097 gl.endQuery(queryTargets[i]);
1100 GLU_EXPECT_NO_ERROR(gl.getError(), "Asynchronous query state reset failed");
1105 gl.hint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
1106 gl.hint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
1107 gl.hint(GL_TEXTURE_COMPRESSION_HINT, GL_DONT_CARE);
1108 gl.hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_DONT_CARE);
1110 GLU_EXPECT_NO_ERROR(gl.getError(), "Hints reset failed");
1113 // Buffer copy state.
1114 if (contextSupports(type, ApiType::core(3,1)))
1116 gl.bindBuffer(GL_COPY_READ_BUFFER, 0);
1117 gl.bindBuffer(GL_COPY_WRITE_BUFFER, 0);
1119 GLU_EXPECT_NO_ERROR(gl.getError(), "Buffer copy state reset failed");
1123 if (ctxInfo.isExtensionSupported("GL_KHR_debug"))
1125 const bool entrypointsPresent = gl.debugMessageControl != DE_NULL &&
1126 gl.debugMessageCallback != DE_NULL;
1128 // some drivers advertise GL_KHR_debug but give out null pointers. Silently ignore.
1129 if (entrypointsPresent)
1131 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, DE_NULL, true);
1132 gl.debugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, DE_NULL, false);
1133 gl.debugMessageCallback(DE_NULL, DE_NULL);
1135 if (type.getFlags() & glu::CONTEXT_DEBUG)
1136 gl.enable(GL_DEBUG_OUTPUT);
1138 gl.disable(GL_DEBUG_OUTPUT);
1143 void resetState (const RenderContext& renderCtx, const ContextInfo& ctxInfo)
1145 if (isContextTypeES(renderCtx.getType()))
1146 resetStateES(renderCtx, ctxInfo);
1147 else if (isContextTypeGLCore(renderCtx.getType()))
1148 resetStateGLCore(renderCtx, ctxInfo);
1150 throw tcu::InternalError("State reset requested for unsupported context type");