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 Render context implementation that does no rendering.
22 *//*--------------------------------------------------------------------*/
24 #include "tcuNullRenderContext.hpp"
25 #include "tcuTexture.hpp"
26 #include "tcuTextureUtil.hpp"
27 #include "deThreadLocal.hpp"
28 #include "gluRenderConfig.hpp"
29 #include "gluTextureUtil.hpp"
30 #include "glwEnums.hpp"
42 #include "tcuNullRenderContextFuncs.inl"
56 deUint32 allocate (void)
58 deUint32 object = ++m_lastObject;
60 object = ++m_lastObject; // Just ignore overflow.
64 void free (deUint32 object)
70 deUint32 m_lastObject;
76 Context (ContextType ctxType_);
80 Context (const Context&);
81 Context& operator= (const Context&);
83 void addExtension (const char* name);
86 // GL state exposed to implementation functions.
87 const ContextType ctxType;
92 string shadingLanguageVersion;
94 vector<string> extensionList;
95 vector<deUint32> compressedTextureList;
99 int pixelPackRowLength;
100 int pixelPackSkipRows;
101 int pixelPackSkipPixels;
102 int pixelPackAlignment;
104 GLuint pixelPackBufferBufferBinding;
106 ObjectManager shaders;
107 ObjectManager programs;
108 ObjectManager textures;
109 ObjectManager buffers;
110 ObjectManager renderbuffers;
111 ObjectManager framebuffers;
112 ObjectManager samplers;
113 ObjectManager vertexArrays;
114 ObjectManager queries;
115 ObjectManager transformFeedbacks;
116 ObjectManager programPipelines;
119 Context::Context (ContextType ctxType_)
121 , vendor ("drawElements")
123 , lastError (GL_NO_ERROR)
124 , pixelPackRowLength (0)
125 , pixelPackSkipRows (0)
126 , pixelPackSkipPixels (0)
127 , pixelPackAlignment (0)
128 , pixelPackBufferBufferBinding (0)
132 if (ctxType.getAPI() == ApiType::es(2, 0))
134 version = "OpenGL ES 2.0";
135 shadingLanguageVersion = "OpenGL ES GLSL ES 1.0";
137 else if (ctxType.getAPI() == ApiType::es(3, 0))
139 version = "OpenGL ES 3.0";
140 shadingLanguageVersion = "OpenGL ES GLSL ES 3.0";
142 else if (ctxType.getAPI() == ApiType::es(3, 1))
144 version = "OpenGL ES 3.1";
145 shadingLanguageVersion = "OpenGL ES GLSL ES 3.1";
146 addExtension("GL_OES_texture_stencil8");
147 addExtension("GL_OES_sample_shading");
148 addExtension("GL_OES_sample_variables");
149 addExtension("GL_OES_shader_multisample_interpolation");
150 addExtension("GL_OES_shader_image_atomic");
151 addExtension("GL_OES_texture_storage_multisample_2d_array");
152 addExtension("GL_KHR_blend_equation_advanced");
153 addExtension("GL_KHR_blend_equation_advanced_coherent");
154 addExtension("GL_EXT_shader_io_blocks");
155 addExtension("GL_EXT_geometry_shader");
156 addExtension("GL_EXT_geometry_point_size");
157 addExtension("GL_EXT_tessellation_shader");
158 addExtension("GL_EXT_tessellation_point_size");
159 addExtension("GL_EXT_gpu_shader5");
160 addExtension("GL_EXT_shader_implicit_conversions");
161 addExtension("GL_EXT_texture_buffer");
162 addExtension("GL_EXT_texture_cube_map_array");
163 addExtension("GL_EXT_draw_buffers_indexed");
164 addExtension("GL_EXT_texture_sRGB_decode");
165 addExtension("GL_EXT_texture_border_clamp");
166 addExtension("GL_KHR_debug");
167 addExtension("GL_EXT_primitive_bounding_box");
168 addExtension("GL_ANDROID_extension_pack_es31a");
169 addExtension("GL_EXT_copy_image");
171 else if (glu::isContextTypeGLCore(ctxType) && ctxType.getMajorVersion() == 3)
174 shadingLanguageVersion = "3.30";
176 else if (glu::isContextTypeGLCore(ctxType) && ctxType.getMajorVersion() == 4 && ctxType.getMinorVersion() <= 4)
179 shadingLanguageVersion = "4.40";
182 throw tcu::NotSupportedError("Unsupported GL version", "", __FILE__, __LINE__);
184 if (isContextTypeES(ctxType))
186 addExtension("GL_EXT_color_buffer_float");
187 addExtension("GL_EXT_color_buffer_half_float");
190 // support compressed formats
192 static deUint32 compressedFormats[] =
195 GL_COMPRESSED_R11_EAC,
196 GL_COMPRESSED_SIGNED_R11_EAC,
197 GL_COMPRESSED_RG11_EAC,
198 GL_COMPRESSED_SIGNED_RG11_EAC,
199 GL_COMPRESSED_RGB8_ETC2,
200 GL_COMPRESSED_SRGB8_ETC2,
201 GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
202 GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
203 GL_COMPRESSED_RGBA8_ETC2_EAC,
204 GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
205 GL_COMPRESSED_RGBA_ASTC_4x4_KHR,
206 GL_COMPRESSED_RGBA_ASTC_5x4_KHR,
207 GL_COMPRESSED_RGBA_ASTC_5x5_KHR,
208 GL_COMPRESSED_RGBA_ASTC_6x5_KHR,
209 GL_COMPRESSED_RGBA_ASTC_6x6_KHR,
210 GL_COMPRESSED_RGBA_ASTC_8x5_KHR,
211 GL_COMPRESSED_RGBA_ASTC_8x6_KHR,
212 GL_COMPRESSED_RGBA_ASTC_8x8_KHR,
213 GL_COMPRESSED_RGBA_ASTC_10x5_KHR,
214 GL_COMPRESSED_RGBA_ASTC_10x6_KHR,
215 GL_COMPRESSED_RGBA_ASTC_10x8_KHR,
216 GL_COMPRESSED_RGBA_ASTC_10x10_KHR,
217 GL_COMPRESSED_RGBA_ASTC_12x10_KHR,
218 GL_COMPRESSED_RGBA_ASTC_12x12_KHR,
219 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
220 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
221 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
222 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
223 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
224 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
225 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
226 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
227 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
228 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
229 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
230 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
231 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
232 GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
235 addExtension("GL_KHR_texture_compression_astc_hdr");
236 addExtension("GL_KHR_texture_compression_astc_ldr");
237 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(compressedFormats); ++ndx)
238 compressedTextureList.push_back(compressedFormats[ndx]);
242 Context::~Context (void)
246 void Context::addExtension (const char* name)
248 if (!extensions.empty())
252 extensionList.push_back(name);
255 static de::ThreadLocal s_currentCtx;
257 void setCurrentContext (Context* context)
259 s_currentCtx.set((void*)context);
262 Context* getCurrentContext (void)
264 return (Context*)s_currentCtx.get();
267 GLW_APICALL GLenum GLW_APIENTRY glGetError (void)
269 Context* const ctx = getCurrentContext();
270 const GLenum lastErr = ctx->lastError;
272 ctx->lastError = GL_NO_ERROR;
277 GLW_APICALL void GLW_APIENTRY glGetIntegerv (GLenum pname, GLint* params)
279 Context* const ctx = getCurrentContext();
283 case GL_NUM_EXTENSIONS:
284 *params = (int)ctx->extensionList.size();
287 case GL_MAX_VERTEX_ATTRIBS:
291 case GL_MAX_DRAW_BUFFERS:
292 case GL_MAX_COLOR_ATTACHMENTS:
296 case GL_MAX_TEXTURE_IMAGE_UNITS:
297 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
298 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
299 case GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS:
300 case GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS:
301 case GL_MAX_TESS_CONTROL_TEXTURE_IMAGE_UNITS:
305 case GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS:
306 case GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS:
307 case GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS:
308 case GL_MAX_TESS_CONTROL_SHADER_STORAGE_BLOCKS:
309 case GL_MAX_TESS_EVALUATION_SHADER_STORAGE_BLOCKS:
310 case GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS:
314 case GL_MAX_VERTEX_ATOMIC_COUNTER_BUFFERS:
315 case GL_MAX_FRAGMENT_ATOMIC_COUNTER_BUFFERS:
316 case GL_MAX_GEOMETRY_ATOMIC_COUNTER_BUFFERS:
317 case GL_MAX_TESS_CONTROL_ATOMIC_COUNTER_BUFFERS:
318 case GL_MAX_TESS_EVALUATION_ATOMIC_COUNTER_BUFFERS:
319 case GL_MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS:
323 case GL_MAX_SHADER_STORAGE_BLOCK_SIZE:
327 case GL_MAX_GEOMETRY_OUTPUT_VERTICES:
331 case GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
335 case GL_MAX_GEOMETRY_SHADER_INVOCATIONS:
339 case GL_MAX_COLOR_TEXTURE_SAMPLES:
343 case GL_MAX_TEXTURE_SIZE:
344 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
345 case GL_MAX_3D_TEXTURE_SIZE:
346 case GL_MAX_RENDERBUFFER_SIZE:
347 case GL_MAX_TEXTURE_BUFFER_SIZE:
351 case GL_MAX_ARRAY_TEXTURE_LAYERS:
355 case GL_NUM_SHADER_BINARY_FORMATS:
359 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
360 *params = (int)ctx->compressedTextureList.size();
363 case GL_COMPRESSED_TEXTURE_FORMATS:
364 deMemcpy(params, &ctx->compressedTextureList[0], (int)ctx->compressedTextureList.size());
367 case GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS:
371 case GL_MAX_UNIFORM_BUFFER_BINDINGS:
375 case GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT:
379 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
383 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
384 *params = GL_UNSIGNED_BYTE;
387 case GL_SAMPLE_BUFFERS:
396 GLW_APICALL void GLW_APIENTRY glGetBooleanv (GLenum pname, GLboolean* params)
400 case GL_SHADER_COMPILER:
409 GLW_APICALL void GLW_APIENTRY glGetFloatv (GLenum pname, GLfloat* params)
413 case GL_ALIASED_LINE_WIDTH_RANGE:
414 case GL_ALIASED_POINT_SIZE_RANGE:
424 GLW_APICALL void GLW_APIENTRY glGetInternalformativ (GLenum target, GLenum internalformat, GLenum pname, GLsizei bufSize, GLint* params)
426 static const int s_sampleCounts[] = { 16, 8, 4, 2, 1 };
428 DE_UNREF(internalformat);
433 case GL_NUM_SAMPLE_COUNTS:
435 *params = DE_LENGTH_OF_ARRAY(s_sampleCounts);
439 deMemcpy(params, s_sampleCounts, de::min(bufSize, DE_LENGTH_OF_ARRAY(s_sampleCounts)));
447 GLW_APICALL const glw::GLubyte* GLW_APIENTRY glGetString (GLenum name)
449 Context* const ctx = getCurrentContext();
453 case GL_VENDOR: return (const glw::GLubyte*)ctx->vendor.c_str();
454 case GL_VERSION: return (const glw::GLubyte*)ctx->version.c_str();
455 case GL_RENDERER: return (const glw::GLubyte*)ctx->renderer.c_str();
456 case GL_SHADING_LANGUAGE_VERSION: return (const glw::GLubyte*)ctx->shadingLanguageVersion.c_str();
457 case GL_EXTENSIONS: return (const glw::GLubyte*)ctx->extensions.c_str();
459 ctx->lastError = GL_INVALID_ENUM;
464 GLW_APICALL const glw::GLubyte* GLW_APIENTRY glGetStringi (GLenum name, GLuint index)
466 Context* const ctx = getCurrentContext();
468 if (name == GL_EXTENSIONS)
470 if ((size_t)index < ctx->extensionList.size())
471 return (const glw::GLubyte*)ctx->extensionList[index].c_str();
474 ctx->lastError = GL_INVALID_VALUE;
480 ctx->lastError = GL_INVALID_ENUM;
485 GLW_APICALL GLuint GLW_APIENTRY glCreateProgram ()
487 Context* const ctx = getCurrentContext();
488 return (GLuint)ctx->programs.allocate();
491 GLW_APICALL GLuint GLW_APIENTRY glCreateShader (GLenum type)
493 Context* const ctx = getCurrentContext();
495 return (GLuint)ctx->shaders.allocate();
498 GLW_APICALL void GLW_APIENTRY glGetShaderiv (GLuint shader, GLenum pname, GLint* params)
502 if (pname == GL_COMPILE_STATUS)
506 GLW_APICALL void GLW_APIENTRY glGetProgramiv (GLuint program, GLenum pname, GLint* params)
510 if (pname == GL_LINK_STATUS)
514 GLW_APICALL void GLW_APIENTRY glGenTextures (GLsizei n, GLuint* textures)
516 Context* const ctx = getCurrentContext();
520 for (int ndx = 0; ndx < n; ndx++)
521 textures[ndx] = ctx->textures.allocate();
525 GLW_APICALL void GLW_APIENTRY glGenQueries (GLsizei n, GLuint* ids)
527 Context* const ctx = getCurrentContext();
531 for (int ndx = 0; ndx < n; ndx++)
532 ids[ndx] = ctx->queries.allocate();
536 GLW_APICALL void GLW_APIENTRY glGenBuffers (GLsizei n, GLuint* buffers)
538 Context* const ctx = getCurrentContext();
542 for (int ndx = 0; ndx < n; ndx++)
543 buffers[ndx] = ctx->buffers.allocate();
547 GLW_APICALL void GLW_APIENTRY glGenRenderbuffers (GLsizei n, GLuint* renderbuffers)
549 Context* const ctx = getCurrentContext();
553 for (int ndx = 0; ndx < n; ndx++)
554 renderbuffers[ndx] = ctx->renderbuffers.allocate();
558 GLW_APICALL void GLW_APIENTRY glGenFramebuffers (GLsizei n, GLuint* framebuffers)
560 Context* const ctx = getCurrentContext();
564 for (int ndx = 0; ndx < n; ndx++)
565 framebuffers[ndx] = ctx->framebuffers.allocate();
569 GLW_APICALL void GLW_APIENTRY glGenVertexArrays (GLsizei n, GLuint* arrays)
571 Context* const ctx = getCurrentContext();
575 for (int ndx = 0; ndx < n; ndx++)
576 arrays[ndx] = ctx->vertexArrays.allocate();
580 GLW_APICALL void GLW_APIENTRY glGenSamplers (GLsizei count, GLuint* samplers)
582 Context* const ctx = getCurrentContext();
586 for (int ndx = 0; ndx < count; ndx++)
587 samplers[ndx] = ctx->samplers.allocate();
591 GLW_APICALL void GLW_APIENTRY glGenTransformFeedbacks (GLsizei n, GLuint* ids)
593 Context* const ctx = getCurrentContext();
597 for (int ndx = 0; ndx < n; ndx++)
598 ids[ndx] = ctx->transformFeedbacks.allocate();
602 GLW_APICALL void GLW_APIENTRY glGenProgramPipelines (GLsizei n, GLuint* pipelines)
604 Context* const ctx = getCurrentContext();
608 for (int ndx = 0; ndx < n; ndx++)
609 pipelines[ndx] = ctx->programPipelines.allocate();
613 GLW_APICALL GLvoid* GLW_APIENTRY glMapBufferRange (GLenum target, GLintptr offset, GLsizeiptr length, GLbitfield access)
615 Context* const ctx = getCurrentContext();
622 if (ctx->lastError == GL_NO_ERROR)
623 ctx->lastError = GL_INVALID_OPERATION;
628 GLW_APICALL GLenum GLW_APIENTRY glCheckFramebufferStatus (GLenum target)
631 return GL_FRAMEBUFFER_COMPLETE;
634 GLW_APICALL void GLW_APIENTRY glReadPixels (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels)
639 Context* const ctx = getCurrentContext();
640 const tcu::Vec4 clearColor (0.0f, 0.0f, 0.0f, 1.0f); // black
641 const tcu::TextureFormat transferFormat = glu::mapGLTransferFormat(format, type);
644 if (transferFormat.order == TextureFormat::CHANNELORDER_LAST || transferFormat.type == TextureFormat::CHANNELTYPE_LAST)
646 if (ctx->lastError == GL_NO_ERROR)
647 ctx->lastError = GL_INVALID_ENUM;
651 // unsupported formats
652 if (!(format == GL_RGBA && type == GL_UNSIGNED_BYTE) &&
653 !(format == GL_RGBA_INTEGER && type == GL_INT) &&
654 !(format == GL_RGBA_INTEGER && type == GL_UNSIGNED_INT) &&
655 !(format == GL_RGBA && type == GL_FLOAT))
657 if (ctx->lastError == GL_NO_ERROR)
658 ctx->lastError = GL_INVALID_ENUM;
663 if (width < 0 || height < 0)
665 if (ctx->lastError == GL_NO_ERROR)
666 ctx->lastError = GL_INVALID_OPERATION;
671 if (ctx->pixelPackBufferBufferBinding)
674 // read to use pointer
676 const int targetRowLength = (ctx->pixelPackRowLength != 0) ? (ctx->pixelPackRowLength) : (width);
677 const int targetSkipRows = ctx->pixelPackSkipRows;
678 const int targetSkipPixels = ctx->pixelPackSkipPixels;
679 const int infiniteHeight = targetSkipRows + height; // as much as needed
680 const int targetRowPitch = (ctx->pixelPackAlignment == 0) ? (targetRowLength * transferFormat.getPixelSize()) : (deAlign32(targetRowLength * transferFormat.getPixelSize(), ctx->pixelPackAlignment));
682 // Create access to the whole copy target
683 const tcu::PixelBufferAccess targetAccess (transferFormat, targetRowLength, infiniteHeight, 1, targetRowPitch, 0, pixels);
685 // Select (skip_pixels, skip_rows, width, height) subregion from it. Clip to horizontal boundaries
686 const tcu::PixelBufferAccess targetRectAccess = tcu::getSubregion(targetAccess,
687 de::clamp(targetSkipPixels, 0, targetAccess.getWidth()-1),
689 de::clamp(width, 0, de::max(0, targetAccess.getWidth() - targetSkipPixels)),
692 tcu::clear(targetRectAccess, clearColor);
696 GLW_APICALL void GLW_APIENTRY glBindBuffer (GLenum target, GLuint buffer)
698 Context* const ctx = getCurrentContext();
700 if (target == GL_PIXEL_PACK_BUFFER)
701 ctx->pixelPackBufferBufferBinding = buffer;
704 GLW_APICALL void GLW_APIENTRY glDeleteBuffers (GLsizei n, const GLuint* buffers)
706 Context* const ctx = getCurrentContext();
708 for (GLsizei ndx = 0; ndx < n; ++ndx)
709 if (buffers[ndx] && buffers[ndx] == ctx->pixelPackBufferBufferBinding)
710 ctx->pixelPackBufferBufferBinding = 0;
713 GLW_APICALL GLint GLW_APIENTRY glGetAttribLocation (GLuint program, const GLchar* name)
716 return (GLint)(deStringHash(name) & 0x7FFFFFFF);
719 void initFunctions (glw::Functions* gl)
721 # include "tcuNullRenderContextInitFuncs.inl"
724 static tcu::RenderTarget toRenderTarget (const RenderConfig& renderCfg)
726 const int width = getValueOrDefault(renderCfg, &RenderConfig::width, 256);
727 const int height = getValueOrDefault(renderCfg, &RenderConfig::height, 256);
728 const int redBits = getValueOrDefault(renderCfg, &RenderConfig::redBits, 8);
729 const int greenBits = getValueOrDefault(renderCfg, &RenderConfig::greenBits, 8);
730 const int blueBits = getValueOrDefault(renderCfg, &RenderConfig::blueBits, 8);
731 const int alphaBits = getValueOrDefault(renderCfg, &RenderConfig::alphaBits, 8);
732 const int depthBits = getValueOrDefault(renderCfg, &RenderConfig::depthBits, 24);
733 const int stencilBits = getValueOrDefault(renderCfg, &RenderConfig::stencilBits, 8);
734 const int numSamples = getValueOrDefault(renderCfg, &RenderConfig::numSamples, 0);
736 return tcu::RenderTarget(width, height, tcu::PixelFormat(redBits, greenBits, blueBits, alphaBits), depthBits, stencilBits, numSamples);
739 RenderContext::RenderContext (const RenderConfig& renderCfg)
740 : m_ctxType (renderCfg.type)
741 , m_renderTarget (toRenderTarget(renderCfg))
742 , m_context (DE_NULL)
744 m_context = new Context(m_ctxType);
746 initFunctions(&m_functions);
747 setCurrentContext(m_context);
750 RenderContext::~RenderContext (void)
752 setCurrentContext(DE_NULL);
756 void RenderContext::postIterate (void)