blob: f6f70f32c03236c9a6b6a699b8ee6fa48b659e15 [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>
38
39#include <string>
40#include <map>
41
José Fonsecaa08d2752011-08-25 13:26:43 +010042#include "glproc.hpp"
José Fonseca1b3d3752011-07-15 10:15:19 +010043#include "gltrace.hpp"
44
45
46namespace gltrace {
47
48
49typedef std::map<std::string, const char *> ExtensionsMap;
50
51// Cache of the translated extensions strings
52static ExtensionsMap extensionsMap;
53
54
55// Additional extensions to be advertised
José Fonsecaf028a8f2012-02-15 23:33:35 +000056static const char *
57extraExtension_stringsFull[] = {
José Fonsecaa08d2752011-08-25 13:26:43 +010058 "GL_GREMEDY_string_marker",
59 "GL_GREMEDY_frame_terminator",
60};
José Fonsecaf028a8f2012-02-15 23:33:35 +000061
62static const char *
63extraExtension_stringsES[] = {
64 "GL_EXT_debug_marker",
65};
66
67// Description of additional extensions we want to advertise
68struct ExtensionsDesc
69{
70 unsigned numStrings;
71 const char **strings;
72};
73
74#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
75
76const struct ExtensionsDesc
77extraExtensionsFull = {
78 ARRAY_SIZE(extraExtension_stringsFull),
79 extraExtension_stringsFull
80};
81
82const struct ExtensionsDesc
83extraExtensionsES = {
84 ARRAY_SIZE(extraExtension_stringsES),
85 extraExtension_stringsES
86};
87
88
89const struct ExtensionsDesc *
90getExtraExtensions(void)
91{
92 Context *ctx = getContext();
93
94 switch (ctx->profile) {
95 case PROFILE_COMPAT:
96 return &extraExtensionsFull;
97 case PROFILE_ES1:
98 case PROFILE_ES2:
99 return &extraExtensionsES;
100 default:
101 assert(0);
102 return &extraExtensionsFull;
103 }
104}
José Fonseca1b3d3752011-07-15 10:15:19 +0100105
106
107/**
108 * Translate the GL extensions string, adding new extensions.
109 */
José Fonsecaa08d2752011-08-25 13:26:43 +0100110static const char *
111overrideExtensionsString(const char *extensions)
José Fonseca1b3d3752011-07-15 10:15:19 +0100112{
José Fonsecaf028a8f2012-02-15 23:33:35 +0000113 const ExtensionsDesc *desc = getExtraExtensions();
José Fonseca7525e6f2011-09-28 09:04:56 +0100114 size_t i;
José Fonsecaa08d2752011-08-25 13:26:43 +0100115
José Fonseca1b3d3752011-07-15 10:15:19 +0100116 ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
117 if (it != extensionsMap.end()) {
118 return it->second;
119 }
120
José Fonsecaf028a8f2012-02-15 23:33:35 +0000121 size_t extensionsLen = strlen(extensions);
José Fonseca1b3d3752011-07-15 10:15:19 +0100122
José Fonsecaf028a8f2012-02-15 23:33:35 +0000123 size_t extraExtensionsLen = 0;
124 for (i = 0; i < desc->numStrings; ++i) {
125 const char * extraExtension = desc->strings[i];
126 size_t extraExtensionLen = strlen(extraExtension);
127 extraExtensionsLen += extraExtensionLen + 1;
José Fonsecaa08d2752011-08-25 13:26:43 +0100128 }
129
José Fonseca02873842011-10-27 13:23:17 +0100130 // We use malloc memory instead of a std::string because we need to ensure
131 // that extensions strings will not move in memory as the extensionsMap is
132 // updated.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000133 size_t newExtensionsLen = extensionsLen + 1 + extraExtensionsLen + 1;
134 char *newExtensions = (char *)malloc(newExtensionsLen);
135 if (!newExtensions) {
José Fonseca1b3d3752011-07-15 10:15:19 +0100136 return extensions;
137 }
138
José Fonsecaf028a8f2012-02-15 23:33:35 +0000139 if (extensionsLen) {
140 memcpy(newExtensions, extensions, extensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100141
142 // Add space separator if necessary
José Fonsecaf028a8f2012-02-15 23:33:35 +0000143 if (newExtensions[extensionsLen - 1] != ' ') {
144 newExtensions[extensionsLen++] = ' ';
José Fonseca1b3d3752011-07-15 10:15:19 +0100145 }
146 }
147
José Fonsecaf028a8f2012-02-15 23:33:35 +0000148 for (i = 0; i < desc->numStrings; ++i) {
149 const char * extraExtension = desc->strings[i];
150 size_t extraExtensionLen = strlen(extraExtension);
151 memcpy(newExtensions + extensionsLen, extraExtension, extraExtensionLen);
152 extensionsLen += extraExtensionLen;
153 newExtensions[extensionsLen++] = ' ';
José Fonsecaa08d2752011-08-25 13:26:43 +0100154 }
José Fonsecaf028a8f2012-02-15 23:33:35 +0000155 newExtensions[extensionsLen++] = '\0';
156 assert(extensionsLen <= newExtensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100157
José Fonsecaf028a8f2012-02-15 23:33:35 +0000158 extensionsMap[extensions] = newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100159
José Fonsecaf028a8f2012-02-15 23:33:35 +0000160 return newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100161}
162
163
José Fonsecaa08d2752011-08-25 13:26:43 +0100164const GLubyte *
165__glGetString_override(GLenum name)
166{
167 const GLubyte *result = __glGetString(name);
168
169 if (result) {
170 switch (name) {
171 case GL_EXTENSIONS:
172 result = (const GLubyte *)overrideExtensionsString((const char *)result);
173 break;
174 default:
175 break;
176 }
177 }
178
179 return result;
180}
181
182
183void
184__glGetIntegerv_override(GLenum pname, GLint *params)
185{
186 __glGetIntegerv(pname, params);
187
188 if (params) {
189 switch (pname) {
190 case GL_NUM_EXTENSIONS:
José Fonsecaf028a8f2012-02-15 23:33:35 +0000191 {
192 const ExtensionsDesc *desc = getExtraExtensions();
193 *params += desc->numStrings;
194 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100195 break;
196 default:
197 break;
198 }
199 }
200}
201
202
203const GLubyte *
204__glGetStringi_override(GLenum name, GLuint index)
205{
206 switch (name) {
207 case GL_EXTENSIONS:
208 {
José Fonsecaf028a8f2012-02-15 23:33:35 +0000209 const ExtensionsDesc *desc = getExtraExtensions();
210 GLint numExtensions = 0;
211 __glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
212 if ((GLuint)numExtensions <= index && index < (GLuint)numExtensions + desc->numStrings) {
213 return (const GLubyte *)desc->strings[index - (GLuint)numExtensions];
José Fonsecaa08d2752011-08-25 13:26:43 +0100214 }
215 }
216 break;
217 default:
218 break;
219 }
220
221 return __glGetStringi(name, index);
222}
223
224
José Fonseca1b3d3752011-07-15 10:15:19 +0100225} /* namespace gltrace */
226