The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SDL_gpu_Renderer.c
Go to the documentation of this file.
1 #include "SDL_gpu.h"
2 #include "SDL_platform.h"
3 #include <string.h>
4 
5 #ifndef _MSC_VER
6  #include <strings.h>
7 #else
8  #define __func__ __FUNCTION__
9 #endif
10 
11 #define MAX_ACTIVE_RENDERERS 20
12 #define MAX_REGISTERED_RENDERERS 10
13 
14 typedef struct RendererRegistration
15 {
17  GPU_Renderer* (*createFn)(GPU_RendererID request);
20 
21 static Uint8 initialized = 0;
22 
25 
26 
27 
28 static GPU_RendererID makeRendererID(GPU_RendererEnum id, int major_version, int minor_version, int index)
29 {
30  GPU_RendererID r = {id, major_version, minor_version, index};
31  return r;
32 }
33 
34 
35 void GPU_InitRendererRegister(void);
36 
38 {
39  int count;
40  int i;
41 
43 
44  count = 0;
45  for(i = 0; i < MAX_ACTIVE_RENDERERS; i++)
46  {
47  if(rendererMap[i] != NULL)
48  count++;
49  }
50  return count;
51 }
52 
54 {
55  int count;
56  int i;
57 
59 
60  count = 0;
61  for(i = 0; i < MAX_ACTIVE_RENDERERS; i++)
62  {
63  if(rendererMap[i] != NULL)
64  {
65  renderers_array[count] = rendererMap[i]->id;
66  count++;
67  }
68  }
69 }
70 
71 
73 {
74  int count;
75  int i;
76 
78 
79  count = 0;
80  for(i = 0; i < MAX_REGISTERED_RENDERERS; i++)
81  {
82  if(rendererRegister[i].id.id != GPU_RENDERER_UNKNOWN)
83  count++;
84  }
85  return count;
86 }
87 
89 {
90  int count;
91  int i;
92 
94 
95  count = 0;
96  for(i = 0; i < MAX_REGISTERED_RENDERERS; i++)
97  {
98  if(rendererRegister[i].id.id != GPU_RENDERER_UNKNOWN)
99  {
100  renderers_array[count] = rendererRegister[i].id;
101  count++;
102  }
103  }
104 }
105 
106 
108 {
109  if(index >= MAX_REGISTERED_RENDERERS)
110  return makeRendererID(GPU_RENDERER_UNKNOWN, 0, 0, -1);
111 
112  return rendererRegister[index].id;
113 }
114 
124 void GPU_FreeRenderer_GLES_1(GPU_Renderer* renderer);
126 void GPU_FreeRenderer_GLES_2(GPU_Renderer* renderer);
127 
129 {
130  int i = 0;
131 
132  if(i >= MAX_REGISTERED_RENDERERS)
133  return;
134 
135  #ifndef SDL_GPU_DISABLE_OPENGL
136  #ifndef SDL_GPU_DISABLE_OPENGL_1_BASE
137  rendererRegister[i].id = makeRendererID(GPU_RENDERER_OPENGL_1_BASE, 1, 1, i);
138  rendererRegister[i].createFn = &GPU_CreateRenderer_OpenGL_1_BASE;
139  rendererRegister[i].freeFn = &GPU_FreeRenderer_OpenGL_1_BASE;
140 
141  i++;
142  if(i >= MAX_REGISTERED_RENDERERS)
143  return;
144  #endif
145 
146  #ifndef SDL_GPU_DISABLE_OPENGL_1
147  rendererRegister[i].id = makeRendererID(GPU_RENDERER_OPENGL_1, 1, 1, i);
148  rendererRegister[i].createFn = &GPU_CreateRenderer_OpenGL_1;
149  rendererRegister[i].freeFn = &GPU_FreeRenderer_OpenGL_1;
150 
151  i++;
152  if(i >= MAX_REGISTERED_RENDERERS)
153  return;
154  #endif
155 
156  #ifndef SDL_GPU_DISABLE_OPENGL_2
157  rendererRegister[i].id = makeRendererID(GPU_RENDERER_OPENGL_2, 2, 0, i);
158  rendererRegister[i].createFn = &GPU_CreateRenderer_OpenGL_2;
159  rendererRegister[i].freeFn = &GPU_FreeRenderer_OpenGL_2;
160 
161  i++;
162  if(i >= MAX_REGISTERED_RENDERERS)
163  return;
164  #endif
165 
166  #ifndef SDL_GPU_DISABLE_OPENGL_3
167  rendererRegister[i].id = makeRendererID(GPU_RENDERER_OPENGL_3, 3, 0, i);
168  rendererRegister[i].createFn = &GPU_CreateRenderer_OpenGL_3;
169  rendererRegister[i].freeFn = &GPU_FreeRenderer_OpenGL_3;
170 
171  i++;
172  if(i >= MAX_REGISTERED_RENDERERS)
173  return;
174  #endif
175  #endif
176 
177  #ifndef SDL_GPU_DISABLE_GLES
178  #ifndef SDL_GPU_DISABLE_GLES_1
179  rendererRegister[i].id = makeRendererID(GPU_RENDERER_GLES_1, 1, 1, i);
180  rendererRegister[i].createFn = &GPU_CreateRenderer_GLES_1;
181  rendererRegister[i].freeFn = &GPU_FreeRenderer_GLES_1;
182 
183  i++;
184  if(i >= MAX_REGISTERED_RENDERERS)
185  return;
186  #endif
187  #ifndef SDL_GPU_DISABLE_GLES_2
188  rendererRegister[i].id = makeRendererID(GPU_RENDERER_GLES_2, 2, 0, i);
189  rendererRegister[i].createFn = &GPU_CreateRenderer_GLES_2;
190  rendererRegister[i].freeFn = &GPU_FreeRenderer_GLES_2;
191 
192  i++;
193  if(i >= MAX_REGISTERED_RENDERERS)
194  return;
195  #endif
196  #endif
197 
198 }
199 
200 
201 static int renderer_order_size = 0;
203 
205 {
206  int i;
207 
208  if(initialized)
209  return;
210 
211  for(i = 0; i < MAX_REGISTERED_RENDERERS; i++)
212  {
213  rendererRegister[i].id.id = GPU_RENDERER_UNKNOWN;
214  rendererRegister[i].id.index = i;
215  rendererRegister[i].createFn = NULL;
216  rendererRegister[i].freeFn = NULL;
217  }
218  for(i = 0; i < MAX_ACTIVE_RENDERERS; i++)
219  {
220  rendererMap[i] = NULL;
221  }
222 
224 
225  initialized = 1;
226 
228 }
229 
230 
232 {
233  if(order_size != NULL)
234  *order_size = renderer_order_size;
235 
236  if(order != NULL && renderer_order_size > 0)
237  memcpy(order, renderer_order, renderer_order_size*sizeof(GPU_RendererID));
238 }
239 
241 {
242  if(order == NULL)
243  {
244  // Restore the default order
245  int count = 0;
246  GPU_RendererID default_order[GPU_RENDERER_ORDER_MAX];
247  GPU_GetDefaultRendererOrder(&count, default_order);
248  GPU_SetRendererOrder(count, default_order); // Call us again with the default order
249  return;
250  }
251 
252  if(order_size <= 0)
253  return;
254 
255  if(order_size > GPU_RENDERER_ORDER_MAX)
256  {
257  GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Given order_size (%d) is greater than GPU_RENDERER_ORDER_MAX (%d)", order_size, GPU_RENDERER_ORDER_MAX);
258  order_size = GPU_RENDERER_ORDER_MAX;
259  }
260 
261  memcpy(renderer_order, order, order_size*sizeof(GPU_RendererID));
262  renderer_order_size = order_size;
263 }
264 
265 
266 
268 {
269  int count = 0;
270  GPU_RendererID default_order[GPU_RENDERER_ORDER_MAX];
271 
272  #if defined(__ANDROID__) || defined(__IPHONEOS__)
273  default_order[count++] = GPU_MakeRendererID(GPU_RENDERER_GLES_2, 2, 0);
274  default_order[count++] = GPU_MakeRendererID(GPU_RENDERER_GLES_1, 1, 1);
275  #else
276  default_order[count++] = GPU_MakeRendererID(GPU_RENDERER_OPENGL_3, 3, 0);
277  default_order[count++] = GPU_MakeRendererID(GPU_RENDERER_OPENGL_2, 2, 0);
278  default_order[count++] = GPU_MakeRendererID(GPU_RENDERER_OPENGL_1, 1, 1);
279  #endif
280 
281  if(order_size != NULL)
282  *order_size = count;
283 
284  if(order != NULL && count > 0)
285  memcpy(order, default_order, count*sizeof(GPU_RendererID));
286 }
287 
289 {
291  return "OpenGL 1 BASE";
292  if(id == GPU_RENDERER_OPENGL_1)
293  return "OpenGL 1.x";
294  if(id == GPU_RENDERER_OPENGL_2)
295  return "OpenGL 2.x";
296  if(id == GPU_RENDERER_OPENGL_3)
297  return "OpenGL 3.x";
298  if(id == GPU_RENDERER_OPENGL_4)
299  return "OpenGL 4.x";
300  if(id == GPU_RENDERER_GLES_1)
301  return "OpenGLES 1.x";
302  if(id == GPU_RENDERER_GLES_2)
303  return "OpenGLES 2.x";
304  if(id == GPU_RENDERER_GLES_3)
305  return "OpenGLES 3.x";
306  if(id == GPU_RENDERER_D3D9)
307  return "Direct3D 9";
308  if(id == GPU_RENDERER_D3D10)
309  return "Direct3D 10";
310  if(id == GPU_RENDERER_D3D11)
311  return "Direct3D 11";
312 
313  return "Unknown";
314 }
315 
316 
318 {
319  GPU_Renderer* result = NULL;
320  int i;
321  for(i = 0; i < MAX_REGISTERED_RENDERERS; i++)
322  {
323  if(rendererRegister[i].id.id == GPU_RENDERER_UNKNOWN)
324  continue;
325 
326  if(id.id == rendererRegister[i].id.id)
327  {
328  if(rendererRegister[i].createFn != NULL)
329  {
330  result = rendererRegister[i].createFn(id);
331  }
332  break;
333  }
334  }
335 
336  if(result == NULL)
337  {
338  GPU_PushErrorCode(__func__, GPU_ERROR_DATA_ERROR, "Renderer was not found in the renderer registry.");
339  }
340  return result;
341 }
342 
343 
345 {
346  if(index >= MAX_ACTIVE_RENDERERS)
347  return NULL;
348 
349  return rendererMap[index];
350 }
351 
352 // Get a renderer from the map.
354 {
356 
357  if(id.index < 0)
358  return NULL;
359 
360  return GPU_GetRenderer(id.index);
361 }
362 
363 // Create a new renderer based on a registered id and store it in the map.
365 {
366  int i;
367  for(i = 0; i < MAX_ACTIVE_RENDERERS; i++)
368  {
369  if(rendererMap[i] == NULL)
370  {
371  // Create
372  GPU_Renderer* renderer = GPU_CreateRenderer(id);
373  if(renderer == NULL)
374  {
375  GPU_PushErrorCode(__func__, GPU_ERROR_BACKEND_ERROR, "Failed to create new renderer.");
376  return NULL;
377  }
378 
379  // Add
380  rendererMap[i] = renderer;
381  renderer->id.index = i;
382  // Return
383  return renderer;
384  }
385  }
386 
387  return NULL;
388 }
389 
391 {
392  int i;
393  for(i = 0; i < MAX_REGISTERED_RENDERERS; i++)
394  {
395  if(rendererRegister[i].id.id == GPU_RENDERER_UNKNOWN)
396  continue;
397 
398  if(renderer->id.id == rendererRegister[i].id.id)
399  {
400  rendererRegister[i].freeFn(renderer);
401  return;
402  }
403  }
404 }
405 
406 // Remove a renderer from the map and free it.
408 {
409  int i;
410  for(i = 0; i < MAX_ACTIVE_RENDERERS; i++)
411  {
412  if(rendererMap[i] == NULL)
413  continue;
414 
415  if(i == id.index && id.id == rendererMap[i]->id.id)
416  {
417  GPU_FreeRenderer(rendererMap[i]);
418  rendererMap[i] = NULL;
419  return;
420  }
421  }
422 }
static int renderer_order_size
void GPU_GetActiveRendererList(GPU_RendererID *renderers_array)
GPU_Renderer * GPU_CreateRenderer(GPU_RendererID id)
static const GPU_RendererEnum GPU_RENDERER_OPENGL_2
Definition: SDL_gpu.h:39
GLuint GLdouble GLdouble GLint GLint order
Definition: glew.h:2972
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
static const GPU_RendererEnum GPU_RENDERER_D3D9
Definition: SDL_gpu.h:45
Uint32 GPU_RendererEnum
Definition: SDL_gpu.h:35
void GPU_InitRendererRegister(void)
void GPU_RemoveRenderer(GPU_RendererID id)
GPU_RendererID GPU_MakeRendererID(GPU_RendererEnum id, int major_version, int minor_version)
Definition: SDL_gpu.c:619
static const GPU_RendererEnum GPU_RENDERER_D3D11
Definition: SDL_gpu.h:47
GPU_Renderer * GPU_GetRendererByID(GPU_RendererID id)
void GPU_PushErrorCode(const char *function, GPU_ErrorEnum error, const char *details,...)
Definition: SDL_gpu.c:503
void GPU_FreeRenderer_GLES_1(GPU_Renderer *renderer)
void GPU_FreeRenderer_OpenGL_2(GPU_Renderer *renderer)
void GPU_FreeRenderer_OpenGL_1_BASE(GPU_Renderer *renderer)
GLuint64EXT * result
Definition: glew.h:10727
GLuint id
Definition: glew.h:1647
static const GPU_RendererEnum GPU_RENDERER_GLES_3
Definition: SDL_gpu.h:44
void(* freeFn)(GPU_Renderer *)
void GPU_FreeRenderer_OpenGL_3(GPU_Renderer *renderer)
static const GPU_RendererEnum GPU_RENDERER_GLES_1
Definition: SDL_gpu.h:42
#define GPU_RENDERER_ORDER_MAX
Definition: SDL_gpu.h:33
static const GPU_RendererEnum GPU_RENDERER_OPENGL_4
Definition: SDL_gpu.h:41
GPU_RendererEnum id
Definition: SDL_gpu.h:55
GPU_Renderer * GPU_GetRenderer(unsigned int index)
struct RendererRegistration RendererRegistration
void GPU_RegisterRenderers()
GLuint GLuint GLsizei count
Definition: glew.h:1221
GPU_Renderer * GPU_CreateRenderer_GLES_1(GPU_RendererID request)
GPU_RendererID id
Definition: SDL_gpu.h:484
#define MAX_ACTIVE_RENDERERS
GPU_Renderer * GPU_CreateRenderer_OpenGL_1(GPU_RendererID request)
static const GPU_RendererEnum GPU_RENDERER_OPENGL_3
Definition: SDL_gpu.h:40
void GPU_GetRegisteredRendererList(GPU_RendererID *renderers_array)
static const GPU_RendererEnum GPU_RENDERER_OPENGL_1
Definition: SDL_gpu.h:38
GPU_RendererID GPU_GetRendererID(unsigned int index)
void GPU_FreeRenderer(GPU_Renderer *renderer)
static const GPU_RendererEnum GPU_RENDERER_UNKNOWN
Definition: SDL_gpu.h:36
GLuint index
Definition: glew.h:1782
int GPU_GetNumRegisteredRenderers(void)
#define MAX_REGISTERED_RENDERERS
size_t i
Definition: function.cpp:1057
void GPU_FreeRenderer_GLES_2(GPU_Renderer *renderer)
static const GPU_RendererEnum GPU_RENDERER_D3D10
Definition: SDL_gpu.h:46
static Uint8 initialized
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
static const GPU_RendererEnum GPU_RENDERER_GLES_2
Definition: SDL_gpu.h:43
static const GPU_RendererEnum GPU_RENDERER_OPENGL_1_BASE
Definition: SDL_gpu.h:37
static GPU_RendererID makeRendererID(GPU_RendererEnum id, int major_version, int minor_version, int index)
GPU_Renderer * GPU_CreateRenderer_OpenGL_1_BASE(GPU_RendererID request)
GPU_Renderer * GPU_CreateRenderer_GLES_2(GPU_RendererID request)
static GPU_Renderer * rendererMap[MAX_ACTIVE_RENDERERS]
GPU_Renderer * GPU_AddRenderer(GPU_RendererID id)
void GPU_GetDefaultRendererOrder(int *order_size, GPU_RendererID *order)
GPU_Renderer *(* createFn)(GPU_RendererID request)
void GPU_FreeRenderer_OpenGL_1(GPU_Renderer *renderer)
int GPU_GetNumActiveRenderers(void)
const char * GPU_GetRendererEnumString(GPU_RendererEnum id)
GPU_Renderer * GPU_CreateRenderer_OpenGL_3(GPU_RendererID request)
static GPU_RendererID renderer_order[GPU_RENDERER_ORDER_MAX]
void GPU_GetRendererOrder(int *order_size, GPU_RendererID *order)
static RendererRegistration rendererRegister[MAX_REGISTERED_RENDERERS]
GPU_Renderer * GPU_CreateRenderer_OpenGL_2(GPU_RendererID request)
void GPU_SetRendererOrder(int order_size, GPU_RendererID *order)