blob: 43d5e5d31b6d0d2467669f940dba42c3cd7cdde3 [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",
Peter Lohrmann0b5b75e2013-06-03 14:58:41 -070060 "GL_ARB_debug_output",
61 "GL_AMD_debug_output",
62 "GL_KHR_debug",
José Fonsecaa08d2752011-08-25 13:26:43 +010063};
José Fonsecaf028a8f2012-02-15 23:33:35 +000064
65static const char *
66extraExtension_stringsES[] = {
67 "GL_EXT_debug_marker",
68};
69
70// Description of additional extensions we want to advertise
71struct ExtensionsDesc
72{
73 unsigned numStrings;
74 const char **strings;
75};
76
77#define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0]))
78
79const struct ExtensionsDesc
80extraExtensionsFull = {
81 ARRAY_SIZE(extraExtension_stringsFull),
82 extraExtension_stringsFull
83};
84
85const struct ExtensionsDesc
86extraExtensionsES = {
87 ARRAY_SIZE(extraExtension_stringsES),
88 extraExtension_stringsES
89};
90
91
92const struct ExtensionsDesc *
93getExtraExtensions(void)
94{
95 Context *ctx = getContext();
96
97 switch (ctx->profile) {
98 case PROFILE_COMPAT:
99 return &extraExtensionsFull;
100 case PROFILE_ES1:
101 case PROFILE_ES2:
102 return &extraExtensionsES;
103 default:
104 assert(0);
105 return &extraExtensionsFull;
106 }
107}
José Fonseca1b3d3752011-07-15 10:15:19 +0100108
109
110/**
111 * Translate the GL extensions string, adding new extensions.
112 */
José Fonsecaa08d2752011-08-25 13:26:43 +0100113static const char *
114overrideExtensionsString(const char *extensions)
José Fonseca1b3d3752011-07-15 10:15:19 +0100115{
José Fonsecaf028a8f2012-02-15 23:33:35 +0000116 const ExtensionsDesc *desc = getExtraExtensions();
José Fonseca7525e6f2011-09-28 09:04:56 +0100117 size_t i;
José Fonsecaa08d2752011-08-25 13:26:43 +0100118
José Fonseca1b3d3752011-07-15 10:15:19 +0100119 ExtensionsMap::const_iterator it = extensionsMap.find(extensions);
120 if (it != extensionsMap.end()) {
121 return it->second;
122 }
123
José Fonsecaf028a8f2012-02-15 23:33:35 +0000124 size_t extensionsLen = strlen(extensions);
José Fonseca1b3d3752011-07-15 10:15:19 +0100125
José Fonsecaf028a8f2012-02-15 23:33:35 +0000126 size_t extraExtensionsLen = 0;
127 for (i = 0; i < desc->numStrings; ++i) {
128 const char * extraExtension = desc->strings[i];
129 size_t extraExtensionLen = strlen(extraExtension);
130 extraExtensionsLen += extraExtensionLen + 1;
José Fonsecaa08d2752011-08-25 13:26:43 +0100131 }
132
José Fonseca02873842011-10-27 13:23:17 +0100133 // We use malloc memory instead of a std::string because we need to ensure
134 // that extensions strings will not move in memory as the extensionsMap is
135 // updated.
José Fonsecaf028a8f2012-02-15 23:33:35 +0000136 size_t newExtensionsLen = extensionsLen + 1 + extraExtensionsLen + 1;
137 char *newExtensions = (char *)malloc(newExtensionsLen);
138 if (!newExtensions) {
José Fonseca1b3d3752011-07-15 10:15:19 +0100139 return extensions;
140 }
141
José Fonsecaf028a8f2012-02-15 23:33:35 +0000142 if (extensionsLen) {
143 memcpy(newExtensions, extensions, extensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100144
145 // Add space separator if necessary
José Fonsecaf028a8f2012-02-15 23:33:35 +0000146 if (newExtensions[extensionsLen - 1] != ' ') {
147 newExtensions[extensionsLen++] = ' ';
José Fonseca1b3d3752011-07-15 10:15:19 +0100148 }
149 }
150
José Fonsecaf028a8f2012-02-15 23:33:35 +0000151 for (i = 0; i < desc->numStrings; ++i) {
152 const char * extraExtension = desc->strings[i];
153 size_t extraExtensionLen = strlen(extraExtension);
154 memcpy(newExtensions + extensionsLen, extraExtension, extraExtensionLen);
155 extensionsLen += extraExtensionLen;
156 newExtensions[extensionsLen++] = ' ';
José Fonsecaa08d2752011-08-25 13:26:43 +0100157 }
José Fonsecaf028a8f2012-02-15 23:33:35 +0000158 newExtensions[extensionsLen++] = '\0';
159 assert(extensionsLen <= newExtensionsLen);
José Fonseca1b3d3752011-07-15 10:15:19 +0100160
José Fonsecaf028a8f2012-02-15 23:33:35 +0000161 extensionsMap[extensions] = newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100162
José Fonsecaf028a8f2012-02-15 23:33:35 +0000163 return newExtensions;
José Fonseca1b3d3752011-07-15 10:15:19 +0100164}
165
166
José Fonsecaa08d2752011-08-25 13:26:43 +0100167const GLubyte *
José Fonseca632a78d2012-04-19 07:18:59 +0100168_glGetString_override(GLenum name)
José Fonsecaa08d2752011-08-25 13:26:43 +0100169{
José Fonseca632a78d2012-04-19 07:18:59 +0100170 const GLubyte *result = _glGetString(name);
José Fonsecaa08d2752011-08-25 13:26:43 +0100171
172 if (result) {
173 switch (name) {
174 case GL_EXTENSIONS:
175 result = (const GLubyte *)overrideExtensionsString((const char *)result);
176 break;
177 default:
178 break;
179 }
180 }
181
182 return result;
183}
184
185
186void
José Fonseca632a78d2012-04-19 07:18:59 +0100187_glGetIntegerv_override(GLenum pname, GLint *params)
José Fonsecaa08d2752011-08-25 13:26:43 +0100188{
José Fonseca632a78d2012-04-19 07:18:59 +0100189 _glGetIntegerv(pname, params);
José Fonsecaa08d2752011-08-25 13:26:43 +0100190
191 if (params) {
192 switch (pname) {
193 case GL_NUM_EXTENSIONS:
José Fonsecaf028a8f2012-02-15 23:33:35 +0000194 {
195 const ExtensionsDesc *desc = getExtraExtensions();
196 *params += desc->numStrings;
197 }
José Fonsecaa08d2752011-08-25 13:26:43 +0100198 break;
199 default:
200 break;
201 }
202 }
203}
204
205
206const GLubyte *
José Fonseca632a78d2012-04-19 07:18:59 +0100207_glGetStringi_override(GLenum name, GLuint index)
José Fonsecaa08d2752011-08-25 13:26:43 +0100208{
209 switch (name) {
210 case GL_EXTENSIONS:
211 {
José Fonsecaf028a8f2012-02-15 23:33:35 +0000212 const ExtensionsDesc *desc = getExtraExtensions();
213 GLint numExtensions = 0;
José Fonseca632a78d2012-04-19 07:18:59 +0100214 _glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
José Fonsecaf028a8f2012-02-15 23:33:35 +0000215 if ((GLuint)numExtensions <= index && index < (GLuint)numExtensions + desc->numStrings) {
216 return (const GLubyte *)desc->strings[index - (GLuint)numExtensions];
José Fonsecaa08d2752011-08-25 13:26:43 +0100217 }
218 }
219 break;
220 default:
221 break;
222 }
223
José Fonseca632a78d2012-04-19 07:18:59 +0100224 return _glGetStringi(name, index);
José Fonsecaa08d2752011-08-25 13:26:43 +0100225}
226
227
José Fonseca1b3d3752011-07-15 10:15:19 +0100228} /* namespace gltrace */
229