blob: 742e26a7b0a241f2097f282060dfa2e6fd73010c [file] [log] [blame]
José Fonseca1b3d3752011-07-15 10:15:19 +01001/**************************************************************************
2 *
3 * Copyright 2011 Jose Fonseca
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 **************************************************************************/
25
26
José Fonsecaa08d2752011-08-25 13:26:43 +010027/*
28 * Manipulation of GL extensions.
29 *
30 * So far we insert GREMEDY extensions, but in the future we could also clamp
31 * the GL extensions to core GL versions here.
32 */
33
34
José Fonseca02873842011-10-27 13:23:17 +010035#include <assert.h>
José Fonseca1b3d3752011-07-15 10:15:19 +010036#include <string.h>
37#include <stdlib.h>
Brian Paul5023f662015-01-29 17:31:29 -070038#include <stdio.h>
José Fonseca1b3d3752011-07-15 10:15:19 +010039
40#include <string>
41#include <map>
42
José Fonsecaa08d2752011-08-25 13:26:43 +010043#include "glproc.hpp"
José Fonseca1b3d3752011-07-15 10:15:19 +010044#include "gltrace.hpp"
Brian Paul5023f662015-01-29 17:31:29 -070045#include "config.hpp"
José Fonseca1b3d3752011-07-15 10:15:19 +010046
47
48namespace gltrace {
49
50
51typedef std::map<std::string, const char *> ExtensionsMap;
52
53// Cache of the translated extensions strings
54static ExtensionsMap extensionsMap;
55
56
57// Additional extensions to be advertised
José Fonsecaf028a8f2012-02-15 23:33:35 +000058static const char *
59extraExtension_stringsFull[] = {
José Fonsecaa08d2752011-08-25 13:26:43 +010060 "GL_GREMEDY_string_marker",
61 "GL_GREMEDY_frame_terminator",
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -070062 "GL_ARB_debug_output",
63 "GL_AMD_debug_output",
64 "GL_KHR_debug",
José Fonseca7ff82412014-02-04 19:01:22 +000065 "GL_EXT_debug_marker",
66 "GL_EXT_debug_label",
José Fonseca631dbd12014-12-15 16:34:45 +000067 "GL_VMWX_map_buffer_debug",
José Fonsecaa08d2752011-08-25 13:26:43 +010068};
José Fonsecaf028a8f2012-02-15 23:33:35 +000069
70static const char *
71extraExtension_stringsES[] = {
José Fonsecad7cd6d02014-12-15 09:58:03 +000072 "GL_KHR_debug",
José Fonsecaf028a8f2012-02-15 23:33:35 +000073 "GL_EXT_debug_marker",
José Fonseca7ff82412014-02-04 19:01:22 +000074 "GL_EXT_debug_label",
José Fonsecaf028a8f2012-02-15 23:33:35 +000075};
76
77// Description of additional extensions we want to advertise
78struct ExtensionsDesc
79{
80 unsigned numStrings;
81 const char **strings;
82};
83
84#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
85
86const struct ExtensionsDesc
87extraExtensionsFull = {
88 ARRAY_SIZE(extraExtension_stringsFull),
89 extraExtension_stringsFull
90};
91
92const struct ExtensionsDesc
93extraExtensionsES = {
94 ARRAY_SIZE(extraExtension_stringsES),
95 extraExtension_stringsES
96};
97
98
99const struct ExtensionsDesc *
José Fonsecaef5d8372015-01-08 14:07:54 +0000100getExtraExtensions(const Context *ctx)
José Fonsecaf028a8f2012-02-15 23:33:35 +0000101{
José Fonsecab0c59722015-01-05 20:45:41 +0000102 switch (ctx->profile.api) {
103 case glprofile::API_GL:
José Fonsecaf028a8f2012-02-15 23:33:35 +0000104 return &extraExtensionsFull;
José Fonsecab0c59722015-01-05 20:45:41 +0000105 case glprofile::API_GLES:
José Fonsecaf028a8f2012-02-15 23:33:35 +0000106 return &extraExtensionsES;
107 default:
108 assert(0);
109 return &extraExtensionsFull;
110 }
111}
José Fonseca1b3d3752011-07-15 10:15:19 +0100112
113
114/**
115 * Translate the GL extensions string, adding new extensions.
116 */
José Fonsecaa08d2752011-08-25 13:26:43 +0100117static const char *
118overrideExtensionsString(const char *extensions)
José Fonseca1b3d3752011-07-15 10:15:19 +0100119{
José Fonsecaef5d8372015-01-08 14:07:54 +0000120 const Context *ctx = getContext();
121 const ExtensionsDesc *desc = getExtraExtensions(ctx);
José Fonseca7525e6f2011-09-28 09:04:56 +0100122 size_t i;
José Fonsecaa08d2752011-08-25 13:26:43 +0100123
José Fonseca1b3d3752011-07-15 10:15:19 +0100124 ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
125 if (it != extensionsMap.end()) {
126 return it->second;
127 }
128
José Fonsecaf028a8f2012-02-15 23:33:35 +0000129 size_t extensionsLen = strlen(extensions);
José Fonseca1b3d3752011-07-15 10:15:19 +0100130
José Fonsecaf028a8f2012-02-15 23:33:35 +0000131 size_t extraExtensionsLen = 0;
132 for (i = 0; i < desc->numStrings; ++i) {
133 const char * extraExtension = desc->strings[i];
134 size_t extraExtensionLen = strlen(extraExtension);
135 extraExtensionsLen += extraExtensionLen + 1;
José Fonsecaa08d2752011-08-25 13:26:43 +0100136 }
137
José Fonseca02873842011-10-27 13:23:17 +0100138 // We use malloc memory instead of a std::string because we need to ensure
139 // that extensions strings will not move in memory as the extensionsMap is
140 // updated.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000141 size_t newExtensionsLen = extensionsLen + 1 + extraExtensionsLen + 1;
142 char *newExtensions = (char *)malloc(newExtensionsLen);
143 if (!newExtensions) {
José Fonseca1b3d3752011-07-15 10:15:19 +0100144 return extensions;
145 }
146
José Fonsecaf028a8f2012-02-15 23:33:35 +0000147 if (extensionsLen) {
148 memcpy(newExtensions, extensions, extensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100149
150 // Add space separator if necessary
José Fonsecaf028a8f2012-02-15 23:33:35 +0000151 if (newExtensions[extensionsLen - 1] != ' ') {
152 newExtensions[extensionsLen++] = ' ';
José Fonseca1b3d3752011-07-15 10:15:19 +0100153 }
154 }
155
José Fonsecaf028a8f2012-02-15 23:33:35 +0000156 for (i = 0; i < desc->numStrings; ++i) {
157 const char * extraExtension = desc->strings[i];
158 size_t extraExtensionLen = strlen(extraExtension);
159 memcpy(newExtensions + extensionsLen, extraExtension, extraExtensionLen);
160 extensionsLen += extraExtensionLen;
161 newExtensions[extensionsLen++] = ' ';
José Fonsecaa08d2752011-08-25 13:26:43 +0100162 }
José Fonsecaf028a8f2012-02-15 23:33:35 +0000163 newExtensions[extensionsLen++] = '\0';
164 assert(extensionsLen <= newExtensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100165
José Fonsecaf028a8f2012-02-15 23:33:35 +0000166 extensionsMap[extensions] = newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100167
José Fonsecaf028a8f2012-02-15 23:33:35 +0000168 return newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100169}
170
171
José Fonsecaa08d2752011-08-25 13:26:43 +0100172const GLubyte *
José Fonseca632a78d2012-04-19 07:18:59 +0100173_glGetString_override(GLenum name)
José Fonsecaa08d2752011-08-25 13:26:43 +0100174{
Brian Paul5023f662015-01-29 17:31:29 -0700175 const configuration *config = getConfig();
176 const GLubyte *result;
177
178 // Try getting the override string value first
179 result = getConfigString(config, name);
180 if (!result) {
181 // Ask the real GL library
182 result = _glGetString(name);
183 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100184
185 if (result) {
186 switch (name) {
187 case GL_EXTENSIONS:
188 result = (const GLubyte *)overrideExtensionsString((const char *)result);
189 break;
190 default:
191 break;
192 }
193 }
194
195 return result;
196}
197
198
199void
José Fonseca632a78d2012-04-19 07:18:59 +0100200_glGetIntegerv_override(GLenum pname, GLint *params)
José Fonsecaa08d2752011-08-25 13:26:43 +0100201{
Brian Paul5023f662015-01-29 17:31:29 -0700202 const configuration *config = getConfig();
203
204 *params = getConfigInteger(config, pname);
205 if (*params == 0) {
206 // Ask the real GL library
207 _glGetIntegerv(pname, params);
208 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100209
210 if (params) {
José Fonsecaef5d8372015-01-08 14:07:54 +0000211 const Context *ctx;
José Fonsecaa08d2752011-08-25 13:26:43 +0100212 switch (pname) {
213 case GL_NUM_EXTENSIONS:
José Fonsecaef5d8372015-01-08 14:07:54 +0000214 ctx = getContext();
215 if (ctx->profile.major >= 3) {
216 const ExtensionsDesc *desc = getExtraExtensions(ctx);
José Fonsecaf028a8f2012-02-15 23:33:35 +0000217 *params += desc->numStrings;
218 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100219 break;
José Fonsecad7730002013-10-24 18:55:19 +0100220 case GL_MAX_LABEL_LENGTH:
221 /* We provide our default implementation of KHR_debug when the
222 * driver does not. So return something sensible here.
223 */
224 if (params[0] == 0) {
225 params[0] = 256;
226 }
227 break;
José Fonsecaa08d2752011-08-25 13:26:43 +0100228 default:
229 break;
230 }
231 }
232}
233
234
235const GLubyte *
José Fonseca632a78d2012-04-19 07:18:59 +0100236_glGetStringi_override(GLenum name, GLuint index)
José Fonsecaa08d2752011-08-25 13:26:43 +0100237{
Brian Paul5023f662015-01-29 17:31:29 -0700238 const configuration *config = getConfig();
José Fonsecaef5d8372015-01-08 14:07:54 +0000239 const Context *ctx = getContext();
Brian Paul5023f662015-01-29 17:31:29 -0700240 const GLubyte *retVal;
José Fonsecaef5d8372015-01-08 14:07:54 +0000241
242 if (ctx->profile.major >= 3) {
243 switch (name) {
244 case GL_EXTENSIONS:
245 {
246 const ExtensionsDesc *desc = getExtraExtensions(ctx);
247 GLint numExtensions = 0;
Brian Paul5023f662015-01-29 17:31:29 -0700248 if (config) {
249 numExtensions = getConfigInteger(config, GL_NUM_EXTENSIONS);
250 }
251 if (numExtensions == 0) {
252 _glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
253 }
José Fonsecaef5d8372015-01-08 14:07:54 +0000254 if ((GLuint)numExtensions <= index && index < (GLuint)numExtensions + desc->numStrings) {
255 return (const GLubyte *)desc->strings[index - (GLuint)numExtensions];
256 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100257 }
José Fonsecaef5d8372015-01-08 14:07:54 +0000258 break;
259 default:
260 break;
José Fonsecaa08d2752011-08-25 13:26:43 +0100261 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100262 }
263
Brian Paul5023f662015-01-29 17:31:29 -0700264 retVal = getConfigStringi(config, name, index);
265 if (retVal)
266 return retVal;
267
José Fonseca632a78d2012-04-19 07:18:59 +0100268 return _glGetStringi(name, index);
José Fonsecaa08d2752011-08-25 13:26:43 +0100269}
270
271
José Fonseca1b3d3752011-07-15 10:15:19 +0100272} /* namespace gltrace */
273