comparison src/render/opengles/SDL_render_gles.c @ 5207:daa5463466c5

Fixed building on Android, added SDL_opengles2.h, removed unnecessary SDL_glesfuncs.h
author Sam Lantinga <slouken@libsdl.org>
date Sun, 06 Feb 2011 02:35:14 -0800
parents 25ffd4e5255c
children 115fff0641ee
comparison
equal deleted inserted replaced
5206:25ffd4e5255c 5207:daa5463466c5
86 { 86 {
87 SDL_GLContext context; 87 SDL_GLContext context;
88 SDL_bool updateSize; 88 SDL_bool updateSize;
89 int blendMode; 89 int blendMode;
90 90
91 #ifndef APIENTRY
92 #define APIENTRY
93 #endif
94
95 SDL_bool useDrawTexture; 91 SDL_bool useDrawTexture;
96 SDL_bool GL_OES_draw_texture_supported; 92 SDL_bool GL_OES_draw_texture_supported;
97
98 /* OpenGL ES functions */
99 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
100 #include "../../video/SDL_glesfuncs.h"
101 #undef SDL_PROC
102
103 } GLES_RenderData; 93 } GLES_RenderData;
104 94
105 typedef struct 95 typedef struct
106 { 96 {
107 GLuint texture; 97 GLuint texture;
144 default: 134 default:
145 error = "UNKNOWN"; 135 error = "UNKNOWN";
146 break; 136 break;
147 } 137 }
148 SDL_SetError("%s: %s", prefix, error); 138 SDL_SetError("%s: %s", prefix, error);
149 }
150
151 static int
152 GLES_LoadFunctions(GLES_RenderData * data)
153 {
154
155 #define SDL_PROC(ret,func,params) \
156 data->func = func;
157 #include "../../video/SDL_glesfuncs.h"
158 #undef SDL_PROC
159
160 return 0;
161 } 139 }
162 140
163 SDL_Renderer * 141 SDL_Renderer *
164 GLES_CreateRenderer(SDL_Window * window, Uint32 flags) 142 GLES_CreateRenderer(SDL_Window * window, Uint32 flags)
165 { 143 {
204 renderer->info = GLES_RenderDriver.info; 182 renderer->info = GLES_RenderDriver.info;
205 renderer->driverdata = data; 183 renderer->driverdata = data;
206 184
207 renderer->info.flags = SDL_RENDERER_ACCELERATED; 185 renderer->info.flags = SDL_RENDERER_ACCELERATED;
208 186
209 if (GLES_LoadFunctions(data) < 0) {
210 GLES_DestroyRenderer(renderer);
211 return NULL;
212 }
213
214 data->context = SDL_GL_CreateContext(window); 187 data->context = SDL_GL_CreateContext(window);
215 if (!data->context) { 188 if (!data->context) {
216 GLES_DestroyRenderer(renderer); 189 GLES_DestroyRenderer(renderer);
217 return NULL; 190 return NULL;
218 } 191 }
241 data->GL_OES_draw_texture_supported = SDL_FALSE; 214 data->GL_OES_draw_texture_supported = SDL_FALSE;
242 data->useDrawTexture = SDL_FALSE; 215 data->useDrawTexture = SDL_FALSE;
243 } 216 }
244 #endif 217 #endif
245 218
246 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); 219 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
247 renderer->info.max_texture_width = value; 220 renderer->info.max_texture_width = value;
248 data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value); 221 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
249 renderer->info.max_texture_height = value; 222 renderer->info.max_texture_height = value;
250 223
251 /* Set up parameters for rendering */ 224 /* Set up parameters for rendering */
252 data->blendMode = -1; 225 data->blendMode = -1;
253 data->glDisable(GL_DEPTH_TEST); 226 glDisable(GL_DEPTH_TEST);
254 data->glDisable(GL_CULL_FACE); 227 glDisable(GL_CULL_FACE);
255 data->updateSize = SDL_TRUE; 228 data->updateSize = SDL_TRUE;
256 229
257 data->glEnableClientState(GL_VERTEX_ARRAY); 230 glEnableClientState(GL_VERTEX_ARRAY);
258 data->glEnableClientState(GL_TEXTURE_COORD_ARRAY); 231 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
259 232
260 return renderer; 233 return renderer;
261 } 234 }
262 235
263 static SDL_GLContext SDL_CurrentContext = NULL; 236 static SDL_GLContext SDL_CurrentContext = NULL;
276 } 249 }
277 if (data->updateSize) { 250 if (data->updateSize) {
278 int w, h; 251 int w, h;
279 252
280 SDL_GetWindowSize(window, &w, &h); 253 SDL_GetWindowSize(window, &w, &h);
281 data->glMatrixMode(GL_PROJECTION); 254 glMatrixMode(GL_PROJECTION);
282 data->glLoadIdentity(); 255 glLoadIdentity();
283 data->glMatrixMode(GL_MODELVIEW); 256 glMatrixMode(GL_MODELVIEW);
284 data->glLoadIdentity(); 257 glLoadIdentity();
285 data->glViewport(0, 0, w, h); 258 glViewport(0, 0, w, h);
286 data->glOrthof(0.0, (GLfloat) w, (GLfloat) h, 0.0, 0.0, 1.0); 259 glOrthof(0.0, (GLfloat) w, (GLfloat) h, 0.0, 0.0, 1.0);
287 data->updateSize = SDL_FALSE; 260 data->updateSize = SDL_FALSE;
288 } 261 }
289 return 0; 262 return 0;
290 } 263 }
291 264
351 } 324 }
352 } 325 }
353 326
354 texture->driverdata = data; 327 texture->driverdata = data;
355 328
356 renderdata->glGetError(); 329 glGetError();
357 renderdata->glEnable(GL_TEXTURE_2D); 330 glEnable(GL_TEXTURE_2D);
358 renderdata->glGenTextures(1, &data->texture); 331 glGenTextures(1, &data->texture);
359 332
360 data->type = GL_TEXTURE_2D; 333 data->type = GL_TEXTURE_2D;
361 /* no NPOV textures allowed in OpenGL ES (yet) */ 334 /* no NPOV textures allowed in OpenGL ES (yet) */
362 texture_w = power_of_2(texture->w); 335 texture_w = power_of_2(texture->w);
363 texture_h = power_of_2(texture->h); 336 texture_h = power_of_2(texture->h);
364 data->texw = (GLfloat) texture->w / texture_w; 337 data->texw = (GLfloat) texture->w / texture_w;
365 data->texh = (GLfloat) texture->h / texture_h; 338 data->texh = (GLfloat) texture->h / texture_h;
366 339
367 data->format = format; 340 data->format = format;
368 data->formattype = type; 341 data->formattype = type;
369 renderdata->glBindTexture(data->type, data->texture); 342 glBindTexture(data->type, data->texture);
370 renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, 343 glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
371 GL_LINEAR); 344 GL_LINEAR);
372 renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, 345 glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
373 GL_LINEAR); 346 GL_LINEAR);
374 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_S, 347 glTexParameteri(data->type, GL_TEXTURE_WRAP_S,
375 GL_CLAMP_TO_EDGE); 348 GL_CLAMP_TO_EDGE);
376 renderdata->glTexParameteri(data->type, GL_TEXTURE_WRAP_T, 349 glTexParameteri(data->type, GL_TEXTURE_WRAP_T,
377 GL_CLAMP_TO_EDGE); 350 GL_CLAMP_TO_EDGE);
378 351
379 renderdata->glTexImage2D(data->type, 0, internalFormat, texture_w, 352 glTexImage2D(data->type, 0, internalFormat, texture_w,
380 texture_h, 0, format, type, NULL); 353 texture_h, 0, format, type, NULL);
381 renderdata->glDisable(GL_TEXTURE_2D); 354 glDisable(GL_TEXTURE_2D);
382 355
383 result = renderdata->glGetError(); 356 result = glGetError();
384 if (result != GL_NO_ERROR) { 357 if (result != GL_NO_ERROR) {
385 GLES_SetError("glTexImage2D()", result); 358 GLES_SetError("glTexImage2D()", result);
386 return -1; 359 return -1;
387 } 360 }
388 return 0; 361 return 0;
400 void * temp_ptr; 373 void * temp_ptr;
401 int i; 374 int i;
402 375
403 GLES_ActivateRenderer(renderer); 376 GLES_ActivateRenderer(renderer);
404 377
405 renderdata->glGetError(); 378 glGetError();
406 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 379 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
407 renderdata->glEnable(data->type); 380 glEnable(data->type);
408 renderdata->glBindTexture(data->type, data->texture); 381 glBindTexture(data->type, data->texture);
409 382
410 if( rect->w * bpp == pitch ) { 383 if( rect->w * bpp == pitch ) {
411 temp_buffer = (void *)pixels; /* No need to reformat */ 384 temp_buffer = (void *)pixels; /* No need to reformat */
412 } else { 385 } else {
413 /* Reformatting of mem area required */ 386 /* Reformatting of mem area required */
418 temp_ptr += rect->w * bpp; 391 temp_ptr += rect->w * bpp;
419 pixels += pitch; 392 pixels += pitch;
420 } 393 }
421 } 394 }
422 395
423 renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, 396 glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
424 rect->h, data->format, data->formattype, 397 rect->h, data->format, data->formattype,
425 temp_buffer); 398 temp_buffer);
426 399
427 if( temp_buffer != pixels ) { 400 if( temp_buffer != pixels ) {
428 SDL_free(temp_buffer); 401 SDL_free(temp_buffer);
429 } 402 }
430 403
431 renderdata->glDisable(data->type); 404 glDisable(data->type);
432 result = renderdata->glGetError(); 405 result = glGetError();
433 if (result != GL_NO_ERROR) { 406 if (result != GL_NO_ERROR) {
434 GLES_SetError("glTexSubImage2D()", result); 407 GLES_SetError("glTexSubImage2D()", result);
435 return -1; 408 return -1;
436 } 409 }
437 return 0; 410 return 0;
456 GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata; 429 GLES_RenderData *renderdata = (GLES_RenderData *) renderer->driverdata;
457 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata; 430 GLES_TextureData *data = (GLES_TextureData *) texture->driverdata;
458 431
459 GLES_ActivateRenderer(renderer); 432 GLES_ActivateRenderer(renderer);
460 433
461 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 434 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
462 renderdata->glEnable(data->type); 435 glEnable(data->type);
463 renderdata->glBindTexture(data->type, data->texture); 436 glBindTexture(data->type, data->texture);
464 renderdata->glTexSubImage2D(data->type, 0, 0, 0, texture->w, 437 glTexSubImage2D(data->type, 0, 0, 0, texture->w,
465 texture->h, data->format, data->formattype, 438 texture->h, data->format, data->formattype,
466 data->pixels); 439 data->pixels);
467 renderdata->glDisable(data->type); 440 glDisable(data->type);
468 } 441 }
469 442
470 static void 443 static void
471 GLES_SetBlendMode(GLES_RenderData * data, int blendMode) 444 GLES_SetBlendMode(GLES_RenderData * data, int blendMode)
472 { 445 {
473 if (blendMode != data->blendMode) { 446 if (blendMode != data->blendMode) {
474 switch (blendMode) { 447 switch (blendMode) {
475 case SDL_BLENDMODE_NONE: 448 case SDL_BLENDMODE_NONE:
476 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 449 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
477 data->glDisable(GL_BLEND); 450 glDisable(GL_BLEND);
478 break; 451 break;
479 case SDL_BLENDMODE_BLEND: 452 case SDL_BLENDMODE_BLEND:
480 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 453 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
481 data->glEnable(GL_BLEND); 454 glEnable(GL_BLEND);
482 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 455 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
483 break; 456 break;
484 case SDL_BLENDMODE_ADD: 457 case SDL_BLENDMODE_ADD:
485 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 458 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
486 data->glEnable(GL_BLEND); 459 glEnable(GL_BLEND);
487 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE); 460 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
488 break; 461 break;
489 case SDL_BLENDMODE_MOD: 462 case SDL_BLENDMODE_MOD:
490 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 463 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
491 data->glEnable(GL_BLEND); 464 glEnable(GL_BLEND);
492 data->glBlendFunc(GL_ZERO, GL_SRC_COLOR); 465 glBlendFunc(GL_ZERO, GL_SRC_COLOR);
493 break; 466 break;
494 } 467 }
495 data->blendMode = blendMode; 468 data->blendMode = blendMode;
496 } 469 }
497 } 470 }
506 479
507 GLES_ActivateRenderer(renderer); 480 GLES_ActivateRenderer(renderer);
508 481
509 GLES_SetBlendMode(data, renderer->blendMode); 482 GLES_SetBlendMode(data, renderer->blendMode);
510 483
511 data->glColor4f((GLfloat) renderer->r * inv255f, 484 glColor4f((GLfloat) renderer->r * inv255f,
512 (GLfloat) renderer->g * inv255f, 485 (GLfloat) renderer->g * inv255f,
513 (GLfloat) renderer->b * inv255f, 486 (GLfloat) renderer->b * inv255f,
514 (GLfloat) renderer->a * inv255f); 487 (GLfloat) renderer->a * inv255f);
515 488
516 vertices = SDL_stack_alloc(GLshort, count*2); 489 vertices = SDL_stack_alloc(GLshort, count*2);
517 for (i = 0; i < count; ++i) { 490 for (i = 0; i < count; ++i) {
518 vertices[2*i+0] = (GLshort)points[i].x; 491 vertices[2*i+0] = (GLshort)points[i].x;
519 vertices[2*i+1] = (GLshort)points[i].y; 492 vertices[2*i+1] = (GLshort)points[i].y;
520 } 493 }
521 data->glVertexPointer(2, GL_SHORT, 0, vertices); 494 glVertexPointer(2, GL_SHORT, 0, vertices);
522 data->glDrawArrays(GL_POINTS, 0, count); 495 glDrawArrays(GL_POINTS, 0, count);
523 SDL_stack_free(vertices); 496 SDL_stack_free(vertices);
524 497
525 return 0; 498 return 0;
526 } 499 }
527 500
535 508
536 GLES_ActivateRenderer(renderer); 509 GLES_ActivateRenderer(renderer);
537 510
538 GLES_SetBlendMode(data, renderer->blendMode); 511 GLES_SetBlendMode(data, renderer->blendMode);
539 512
540 data->glColor4f((GLfloat) renderer->r * inv255f, 513 glColor4f((GLfloat) renderer->r * inv255f,
541 (GLfloat) renderer->g * inv255f, 514 (GLfloat) renderer->g * inv255f,
542 (GLfloat) renderer->b * inv255f, 515 (GLfloat) renderer->b * inv255f,
543 (GLfloat) renderer->a * inv255f); 516 (GLfloat) renderer->a * inv255f);
544 517
545 vertices = SDL_stack_alloc(GLshort, count*2); 518 vertices = SDL_stack_alloc(GLshort, count*2);
546 for (i = 0; i < count; ++i) { 519 for (i = 0; i < count; ++i) {
547 vertices[2*i+0] = (GLshort)points[i].x; 520 vertices[2*i+0] = (GLshort)points[i].x;
548 vertices[2*i+1] = (GLshort)points[i].y; 521 vertices[2*i+1] = (GLshort)points[i].y;
549 } 522 }
550 data->glVertexPointer(2, GL_SHORT, 0, vertices); 523 glVertexPointer(2, GL_SHORT, 0, vertices);
551 if (count > 2 && 524 if (count > 2 &&
552 points[0].x == points[count-1].x && points[0].y == points[count-1].y) { 525 points[0].x == points[count-1].x && points[0].y == points[count-1].y) {
553 /* GL_LINE_LOOP takes care of the final segment */ 526 /* GL_LINE_LOOP takes care of the final segment */
554 --count; 527 --count;
555 data->glDrawArrays(GL_LINE_LOOP, 0, count); 528 glDrawArrays(GL_LINE_LOOP, 0, count);
556 } else { 529 } else {
557 data->glDrawArrays(GL_LINE_STRIP, 0, count); 530 glDrawArrays(GL_LINE_STRIP, 0, count);
558 } 531 }
559 SDL_stack_free(vertices); 532 SDL_stack_free(vertices);
560 533
561 return 0; 534 return 0;
562 } 535 }
570 543
571 GLES_ActivateRenderer(renderer); 544 GLES_ActivateRenderer(renderer);
572 545
573 GLES_SetBlendMode(data, renderer->blendMode); 546 GLES_SetBlendMode(data, renderer->blendMode);
574 547
575 data->glColor4f((GLfloat) renderer->r * inv255f, 548 glColor4f((GLfloat) renderer->r * inv255f,
576 (GLfloat) renderer->g * inv255f, 549 (GLfloat) renderer->g * inv255f,
577 (GLfloat) renderer->b * inv255f, 550 (GLfloat) renderer->b * inv255f,
578 (GLfloat) renderer->a * inv255f); 551 (GLfloat) renderer->a * inv255f);
579 552
580 for (i = 0; i < count; ++i) { 553 for (i = 0; i < count; ++i) {
591 vertices[4] = minx; 564 vertices[4] = minx;
592 vertices[5] = maxy; 565 vertices[5] = maxy;
593 vertices[6] = maxx; 566 vertices[6] = maxx;
594 vertices[7] = maxy; 567 vertices[7] = maxy;
595 568
596 data->glVertexPointer(2, GL_SHORT, 0, vertices); 569 glVertexPointer(2, GL_SHORT, 0, vertices);
597 data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 570 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
598 } 571 }
599 572
600 return 0; 573 return 0;
601 } 574 }
602 575
613 void *temp_buffer; /* used for reformatting dirty rect pixels */ 586 void *temp_buffer; /* used for reformatting dirty rect pixels */
614 void *temp_ptr; 587 void *temp_ptr;
615 588
616 GLES_ActivateRenderer(renderer); 589 GLES_ActivateRenderer(renderer);
617 590
618 data->glEnable(GL_TEXTURE_2D); 591 glEnable(GL_TEXTURE_2D);
619 592
620 data->glBindTexture(texturedata->type, texturedata->texture); 593 glBindTexture(texturedata->type, texturedata->texture);
621 594
622 if (texture->modMode) { 595 if (texture->modMode) {
623 data->glColor4f((GLfloat) texture->r * inv255f, 596 glColor4f((GLfloat) texture->r * inv255f,
624 (GLfloat) texture->g * inv255f, 597 (GLfloat) texture->g * inv255f,
625 (GLfloat) texture->b * inv255f, 598 (GLfloat) texture->b * inv255f,
626 (GLfloat) texture->a * inv255f); 599 (GLfloat) texture->a * inv255f);
627 } else { 600 } else {
628 data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 601 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
629 } 602 }
630 603
631 GLES_SetBlendMode(data, texture->blendMode); 604 GLES_SetBlendMode(data, texture->blendMode);
632 605
633 if (data->GL_OES_draw_texture_supported && data->useDrawTexture) { 606 if (data->GL_OES_draw_texture_supported && data->useDrawTexture) {
639 SDL_GetWindowSize(window, &w, &h); 612 SDL_GetWindowSize(window, &w, &h);
640 cropRect[0] = srcrect->x; 613 cropRect[0] = srcrect->x;
641 cropRect[1] = srcrect->y + srcrect->h; 614 cropRect[1] = srcrect->y + srcrect->h;
642 cropRect[2] = srcrect->w; 615 cropRect[2] = srcrect->w;
643 cropRect[3] = -srcrect->h; 616 cropRect[3] = -srcrect->h;
644 data->glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, 617 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES,
645 cropRect); 618 cropRect);
646 data->glDrawTexiOES(dstrect->x, h - dstrect->y - dstrect->h, 0, 619 glDrawTexiOES(dstrect->x, h - dstrect->y - dstrect->h, 0,
647 dstrect->w, dstrect->h); 620 dstrect->w, dstrect->h);
648 } else { 621 } else {
649 622
650 minx = dstrect->x; 623 minx = dstrect->x;
651 miny = dstrect->y; 624 miny = dstrect->y;
680 texCoords[4] = minu; 653 texCoords[4] = minu;
681 texCoords[5] = maxv; 654 texCoords[5] = maxv;
682 texCoords[6] = maxu; 655 texCoords[6] = maxu;
683 texCoords[7] = maxv; 656 texCoords[7] = maxv;
684 657
685 data->glVertexPointer(2, GL_SHORT, 0, vertices); 658 glVertexPointer(2, GL_SHORT, 0, vertices);
686 data->glTexCoordPointer(2, GL_FLOAT, 0, texCoords); 659 glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
687 data->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); 660 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
688 } 661 }
689 662
690 data->glDisable(GL_TEXTURE_2D); 663 glDisable(GL_TEXTURE_2D);
691 664
692 return 0; 665 return 0;
693 } 666 }
694 667
695 static void 668 static void