comparison src/video/SDL_renderer_gl.c @ 2884:9dde605c7540

Date: Fri, 19 Dec 2008 20:17:35 +0100 From: Couriersud Subject: Re: Aw: Experience using SDL1.3 in sdlmame/Proposal for api additions > For consistency you'd probably want: > SDL_SetRenderDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a); > SDL_SetRenderDrawBlendMode(SDL_BlendMode blendMode); > SDL_RenderLine(int x1, int y1, int x2, int y2); > SDL_RenderFill(SDL_Rect *rect); > > You probably also want to add API functions query the current state. > I have implemented the above api for the opengl, x11, directfb and software renderers. I have also renamed *TEXTUREBLENDMODE* constants to BLENDMODE*. The unix build compiles. The windows renderer still needs to be updated, but I have no windows development machine at hand. Have a look at the x11 renderer for a sample. Vector games now run at 90% both on opengl and directfb in comparison to sdlmame's own opengl renderer. The same applies to raster games. The diff also includes a) Changed XDrawRect to XFillRect in x11 renderer b) A number of changes to fix blending and modulation issues in the directfb renderer.
author Sam Lantinga <slouken@libsdl.org>
date Sat, 20 Dec 2008 12:00:00 +0000
parents a38fcb093081
children e67f3f3bf221
comparison
equal deleted inserted replaced
2883:11626a53e7bc 2884:9dde605c7540
49 return SDL_BYTESPERPIXEL(format); 49 return SDL_BYTESPERPIXEL(format);
50 } 50 }
51 51
52 /* FOURCC format */ 52 /* FOURCC format */
53 switch (format) { 53 switch (format) {
54 case SDL_PIXELFORMAT_YV12: 54 case SDL_PIXELFORMAT_YV12:
55 case SDL_PIXELFORMAT_IYUV: 55 case SDL_PIXELFORMAT_IYUV:
56 case SDL_PIXELFORMAT_YUY2: 56 case SDL_PIXELFORMAT_YUY2:
57 case SDL_PIXELFORMAT_UYVY: 57 case SDL_PIXELFORMAT_UYVY:
58 case SDL_PIXELFORMAT_YVYU: 58 case SDL_PIXELFORMAT_YVYU:
59 return 2; 59 return 2;
60 default: 60 default:
61 return 1; /* shouldn't ever hit this. */ 61 return 1; /* shouldn't ever hit this. */
62 } 62 }
63 } 63 }
64 64
65 65
66 static const float inv255f = 1.0f / 255.0f; 66 static const float inv255f = 1.0f / 255.0f;
94 const SDL_Rect * rect, int markDirty, void **pixels, 94 const SDL_Rect * rect, int markDirty, void **pixels,
95 int *pitch); 95 int *pitch);
96 static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture); 96 static void GL_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
97 static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, 97 static void GL_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
98 int numrects, const SDL_Rect * rects); 98 int numrects, const SDL_Rect * rects);
99 static int GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, 99 static int GL_SetDrawColor(SDL_Renderer * renderer);
100 Uint8 a, const SDL_Rect * rect); 100 static int GL_SetDrawBlendMode(SDL_Renderer * renderer);
101 static int GL_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
102 int y2);
103 static int GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
101 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 104 static int GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
102 const SDL_Rect * srcrect, const SDL_Rect * dstrect); 105 const SDL_Rect * srcrect, const SDL_Rect * dstrect);
106
103 static void GL_RenderPresent(SDL_Renderer * renderer); 107 static void GL_RenderPresent(SDL_Renderer * renderer);
104 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture); 108 static void GL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
105 static void GL_DestroyRenderer(SDL_Renderer * renderer); 109 static void GL_DestroyRenderer(SDL_Renderer * renderer);
106 110
107 111
111 "opengl", 115 "opengl",
112 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD | 116 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD |
113 SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED), 117 SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
114 (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR | 118 (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
115 SDL_TEXTUREMODULATE_ALPHA), 119 SDL_TEXTUREMODULATE_ALPHA),
116 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | 120 (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
117 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD | 121 SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
118 SDL_TEXTUREBLENDMODE_MOD),
119 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST | 122 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
120 SDL_TEXTURESCALEMODE_SLOW), 123 SDL_TEXTURESCALEMODE_SLOW),
121 15, 124 15,
122 { 125 {
123 SDL_PIXELFORMAT_INDEX1LSB, 126 SDL_PIXELFORMAT_INDEX1LSB,
184 GLenum formattype; 187 GLenum formattype;
185 Uint8 *palette; 188 Uint8 *palette;
186 void *pixels; 189 void *pixels;
187 int pitch; 190 int pitch;
188 SDL_DirtyRectList dirty; 191 SDL_DirtyRectList dirty;
189 int HACK_RYAN_FIXME; 192 int HACK_RYAN_FIXME;
190 } GL_TextureData; 193 } GL_TextureData;
191 194
192 195
193 static void 196 static void
194 GL_SetError(const char *prefix, GLenum result) 197 GL_SetError(const char *prefix, GLenum result)
306 renderer->SetTextureScaleMode = GL_SetTextureScaleMode; 309 renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
307 renderer->UpdateTexture = GL_UpdateTexture; 310 renderer->UpdateTexture = GL_UpdateTexture;
308 renderer->LockTexture = GL_LockTexture; 311 renderer->LockTexture = GL_LockTexture;
309 renderer->UnlockTexture = GL_UnlockTexture; 312 renderer->UnlockTexture = GL_UnlockTexture;
310 renderer->DirtyTexture = GL_DirtyTexture; 313 renderer->DirtyTexture = GL_DirtyTexture;
314 renderer->SetDrawColor = GL_SetDrawColor;
315 renderer->SetDrawBlendMode = GL_SetDrawBlendMode;
316 renderer->RenderLine = GL_RenderLine;
311 renderer->RenderFill = GL_RenderFill; 317 renderer->RenderFill = GL_RenderFill;
312 renderer->RenderCopy = GL_RenderCopy; 318 renderer->RenderCopy = GL_RenderCopy;
313 renderer->RenderPresent = GL_RenderPresent; 319 renderer->RenderPresent = GL_RenderPresent;
314 renderer->DestroyTexture = GL_DestroyTexture; 320 renderer->DestroyTexture = GL_DestroyTexture;
315 renderer->DestroyRenderer = GL_DestroyRenderer; 321 renderer->DestroyRenderer = GL_DestroyRenderer;
427 data->updateSize = SDL_TRUE; 433 data->updateSize = SDL_TRUE;
428 434
429 return renderer; 435 return renderer;
430 } 436 }
431 437
438 static void
439 SetBlendMode(GL_RenderData * data, int blendMode)
440 {
441 if (blendMode != data->blendMode) {
442 switch (blendMode) {
443 case SDL_BLENDMODE_NONE:
444 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
445 data->glDisable(GL_BLEND);
446 break;
447 case SDL_BLENDMODE_MASK:
448 case SDL_BLENDMODE_BLEND:
449 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
450 data->glEnable(GL_BLEND);
451 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
452 break;
453 case SDL_BLENDMODE_ADD:
454 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
455 data->glEnable(GL_BLEND);
456 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
457 break;
458 case SDL_BLENDMODE_MOD:
459 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
460 data->glEnable(GL_BLEND);
461 data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
462 break;
463 }
464 data->blendMode = blendMode;
465 }
466 }
467
432 static int 468 static int
433 GL_ActivateRenderer(SDL_Renderer * renderer) 469 GL_ActivateRenderer(SDL_Renderer * renderer)
434 { 470 {
435 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; 471 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
436 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 472 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
474 510
475 511
476 //#define DEBUG_PROGRAM_COMPILE 1 512 //#define DEBUG_PROGRAM_COMPILE 1
477 513
478 static GLuint 514 static GLuint
479 compile_shader(GL_RenderData *data, GLenum shader_type, const char *_code) 515 compile_shader(GL_RenderData * data, GLenum shader_type, const char *_code)
480 { 516 {
481 const int have_texture_rects = data->GL_ARB_texture_rectangle_supported; 517 const int have_texture_rects = data->GL_ARB_texture_rectangle_supported;
482 const char *replacement = have_texture_rects ? "RECT" : "2D"; 518 const char *replacement = have_texture_rects ? "RECT" : "2D";
483 const size_t replacementlen = strlen(replacement); 519 const size_t replacementlen = strlen(replacement);
484 const char *token = "%TEXTURETARGET%"; 520 const char *token = "%TEXTURETARGET%";
494 */ 530 */
495 code = SDL_strdup(_code); 531 code = SDL_strdup(_code);
496 if (code == NULL) 532 if (code == NULL)
497 return 0; 533 return 0;
498 534
499 for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr+1, token)) { 535 for (ptr = SDL_strstr(code, token); ptr; ptr = SDL_strstr(ptr + 1, token)) {
500 memcpy(ptr, replacement, replacementlen); 536 memcpy(ptr, replacement, replacementlen);
501 memmove(ptr+replacementlen, ptr+tokenlen, strlen(ptr+tokenlen)+1); 537 memmove(ptr + replacementlen, ptr + tokenlen,
538 strlen(ptr + tokenlen) + 1);
502 } 539 }
503 540
504 #if DEBUG_PROGRAM_COMPILE 541 #if DEBUG_PROGRAM_COMPILE
505 printf("compiling shader:\n%s\n\n", code); 542 printf("compiling shader:\n%s\n\n", code);
506 #endif 543 #endif
507 544
508 data->glGetError(); /* flush any existing error state. */ 545 data->glGetError(); /* flush any existing error state. */
509 data->glGenProgramsARB(1, &program); 546 data->glGenProgramsARB(1, &program);
510 data->glBindProgramARB(shader_type, program); 547 data->glBindProgramARB(shader_type, program);
511 data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB, 548 data->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
512 SDL_strlen(code), code); 549 SDL_strlen(code), code);
513 550
514 SDL_free(code); 551 SDL_free(code);
515 552
516 if (data->glGetError() == GL_INVALID_OPERATION) 553 if (data->glGetError() == GL_INVALID_OPERATION) {
517 {
518 #if DEBUG_PROGRAM_COMPILE 554 #if DEBUG_PROGRAM_COMPILE
519 GLint pos = 0; 555 GLint pos = 0;
520 const GLubyte *errstr; 556 const GLubyte *errstr;
521 data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos); 557 data->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
522 errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB); 558 errstr = data->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
523 printf("program compile error at position %d: %s\n\n", 559 printf("program compile error at position %d: %s\n\n",
524 (int) pos, (const char *) errstr); 560 (int) pos, (const char *) errstr);
525 #endif 561 #endif
526 data->glBindProgramARB(shader_type, 0); 562 data->glBindProgramARB(shader_type, 0);
527 data->glDeleteProgramsARB(1, &program); 563 data->glDeleteProgramsARB(1, &program);
528 return 0; 564 return 0;
529 } 565 }
542 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr 578 * 4 bytes == 2 pixels: Y1/Cb/Cr, Y2/Cb/Cr
543 * 579 *
544 * !!! FIXME: this ignores blendmodes, etc. 580 * !!! FIXME: this ignores blendmodes, etc.
545 * !!! FIXME: this could be more efficient...use a dot product for green, etc. 581 * !!! FIXME: this could be more efficient...use a dot product for green, etc.
546 */ 582 */
547 static const char *fragment_program_UYVY_source_code = 583 static const char *fragment_program_UYVY_source_code = "!!ARBfp1.0\n"
548 "!!ARBfp1.0\n"
549
550 /* outputs... */ 584 /* outputs... */
551 "OUTPUT outcolor = result.color;\n" 585 "OUTPUT outcolor = result.color;\n"
552
553 /* scratch registers... */ 586 /* scratch registers... */
554 "TEMP uyvy;\n" 587 "TEMP uyvy;\n" "TEMP luminance;\n" "TEMP work;\n"
555 "TEMP luminance;\n"
556 "TEMP work;\n"
557
558 /* Halve the coordinates to grab the correct 32 bits for the fragment. */ 588 /* Halve the coordinates to grab the correct 32 bits for the fragment. */
559 "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n" 589 "MUL work, fragment.texcoord, { 0.5, 1.0, 1.0, 1.0 };\n"
560
561 /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */ 590 /* Sample the YUV texture. Cb, Y1, Cr, Y2, are stored in x, y, z, w. */
562 "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n" 591 "TEX uyvy, work, texture[0], %TEXTURETARGET%;\n"
563
564 /* Do subtractions (128/255, 16/255, 128/255, 16/255) */ 592 /* Do subtractions (128/255, 16/255, 128/255, 16/255) */
565 "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n" 593 "SUB uyvy, uyvy, { 0.501960784313726, 0.06274509803922, 0.501960784313726, 0.06274509803922 };\n"
566
567 /* Choose the luminance component by texcoord. */ 594 /* Choose the luminance component by texcoord. */
568 /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */ 595 /* !!! FIXME: laziness wins out for now... just average Y1 and Y2. */
569 "ADD luminance, uyvy.yyyy, uyvy.wwww;\n" 596 "ADD luminance, uyvy.yyyy, uyvy.wwww;\n"
570 "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n" 597 "MUL luminance, luminance, { 0.5, 0.5, 0.5, 0.5 };\n"
571
572 /* Multiply luminance by its magic value. */ 598 /* Multiply luminance by its magic value. */
573 "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n" 599 "MUL luminance, luminance, { 1.164, 1.164, 1.164, 1.164 };\n"
574
575 /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */ 600 /* uyvy.xyzw becomes Cr/Cr/Cb/Cb, with multiplications. */
576 "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n" 601 "MUL uyvy, uyvy.zzxx, { 1.596, -0.813, 2.018, -0.391 };\n"
577
578 /* Add luminance to Cr and Cb, store to RGB channels. */ 602 /* Add luminance to Cr and Cb, store to RGB channels. */
579 "ADD work.rgb, luminance, uyvy;\n" 603 "ADD work.rgb, luminance, uyvy;\n"
580
581 /* Do final addition for Green channel. (!!! FIXME: this should be a DPH?) */ 604 /* Do final addition for Green channel. (!!! FIXME: this should be a DPH?) */
582 "ADD work.g, work.g, uyvy.w;\n" 605 "ADD work.g, work.g, uyvy.w;\n"
583
584 /* Make sure alpha channel is fully opaque. (!!! FIXME: blend modes!) */ 606 /* Make sure alpha channel is fully opaque. (!!! FIXME: blend modes!) */
585 "MOV work.a, { 1.0 };\n" 607 "MOV work.a, { 1.0 };\n"
586
587 /* Store out the final fragment color... */ 608 /* Store out the final fragment color... */
588 "MOV outcolor, work;\n" 609 "MOV outcolor, work;\n"
589
590 /* ...and we're done! */ 610 /* ...and we're done! */
591 "END\n"; 611 "END\n";
592 612
593 613
594 static int 614 static int
799 data->texh = (GLfloat) texture->h / texture_h; 819 data->texh = (GLfloat) texture->h / texture_h;
800 } 820 }
801 821
802 /* YUV formats use RGBA but are really two bytes per pixel */ 822 /* YUV formats use RGBA but are really two bytes per pixel */
803 if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) { 823 if (internalFormat == GL_RGBA && bytes_per_pixel(texture->format) < 4) {
804 data->HACK_RYAN_FIXME = 2; 824 data->HACK_RYAN_FIXME = 2;
805 } else { 825 } else {
806 data->HACK_RYAN_FIXME = 1; 826 data->HACK_RYAN_FIXME = 1;
807 } 827 }
808 texture_w /= data->HACK_RYAN_FIXME; 828 texture_w /= data->HACK_RYAN_FIXME;
809 829
810 data->format = format; 830 data->format = format;
811 data->formattype = type; 831 data->formattype = type;
832 renderdata->glEnable(data->type);
812 renderdata->glBindTexture(data->type, data->texture); 833 renderdata->glBindTexture(data->type, data->texture);
813 renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER, 834 renderdata->glTexParameteri(data->type, GL_TEXTURE_MIN_FILTER,
814 GL_NEAREST); 835 GL_NEAREST);
815 renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER, 836 renderdata->glTexParameteri(data->type, GL_TEXTURE_MAG_FILTER,
816 GL_NEAREST); 837 GL_NEAREST);
892 *palette++ = colors->r; 913 *palette++ = colors->r;
893 *palette++ = colors->g; 914 *palette++ = colors->g;
894 *palette++ = colors->b; 915 *palette++ = colors->b;
895 ++colors; 916 ++colors;
896 } 917 }
918 renderdata->glEnable(data->type);
897 renderdata->glBindTexture(data->type, data->texture); 919 renderdata->glBindTexture(data->type, data->texture);
898 renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB, 920 renderdata->glColorTableEXT(data->type, GL_RGB8, 256, GL_RGB,
899 GL_UNSIGNED_BYTE, data->palette); 921 GL_UNSIGNED_BYTE, data->palette);
900 return 0; 922 return 0;
901 } 923 }
932 } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) { 954 } else if (texture->format == SDL_PIXELFORMAT_INDEX1MSB) {
933 renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0); 955 renderdata->glPixelStorei(GL_UNPACK_LSB_FIRST, 0);
934 } 956 }
935 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 957 renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
936 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, 958 renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
937 (pitch / bytes_per_pixel(texture->format)) / ((GL_TextureData *)texture->driverdata)->HACK_RYAN_FIXME); 959 (pitch / bytes_per_pixel(texture->format)) /
960 ((GL_TextureData *) texture->driverdata)->
961 HACK_RYAN_FIXME);
938 } 962 }
939 963
940 static int 964 static int
941 GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture) 965 GL_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
942 { 966 {
951 975
952 static int 976 static int
953 GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture) 977 GL_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
954 { 978 {
955 switch (texture->blendMode) { 979 switch (texture->blendMode) {
956 case SDL_TEXTUREBLENDMODE_NONE: 980 case SDL_BLENDMODE_NONE:
957 case SDL_TEXTUREBLENDMODE_MASK: 981 case SDL_BLENDMODE_MASK:
958 case SDL_TEXTUREBLENDMODE_BLEND: 982 case SDL_BLENDMODE_BLEND:
959 case SDL_TEXTUREBLENDMODE_ADD: 983 case SDL_BLENDMODE_ADD:
960 case SDL_TEXTUREBLENDMODE_MOD: 984 case SDL_BLENDMODE_MOD:
961 return 0; 985 return 0;
962 default: 986 default:
963 SDL_Unsupported(); 987 SDL_Unsupported();
964 texture->blendMode = SDL_TEXTUREBLENDMODE_NONE; 988 texture->blendMode = SDL_BLENDMODE_NONE;
965 return -1; 989 return -1;
966 } 990 }
967 } 991 }
968 992
969 static int 993 static int
993 GL_TextureData *data = (GL_TextureData *) texture->driverdata; 1017 GL_TextureData *data = (GL_TextureData *) texture->driverdata;
994 GLenum result; 1018 GLenum result;
995 1019
996 renderdata->glGetError(); 1020 renderdata->glGetError();
997 SetupTextureUpdate(renderdata, texture, pitch); 1021 SetupTextureUpdate(renderdata, texture, pitch);
1022 renderdata->glEnable(data->type);
998 renderdata->glBindTexture(data->type, data->texture); 1023 renderdata->glBindTexture(data->type, data->texture);
999 renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w, 1024 renderdata->glTexSubImage2D(data->type, 0, rect->x, rect->y, rect->w,
1000 rect->h, data->format, data->formattype, 1025 rect->h, data->format, data->formattype,
1001 pixels); 1026 pixels);
1002 result = renderdata->glGetError(); 1027 result = renderdata->glGetError();
1041 SDL_AddDirtyRect(&data->dirty, &rects[i]); 1066 SDL_AddDirtyRect(&data->dirty, &rects[i]);
1042 } 1067 }
1043 } 1068 }
1044 1069
1045 static int 1070 static int
1046 GL_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a, 1071 GL_SetDrawColor(SDL_Renderer * renderer)
1047 const SDL_Rect * rect) 1072 {
1073 return 0;
1074 }
1075
1076 static int
1077 GL_SetDrawBlendMode(SDL_Renderer * renderer)
1078 {
1079 return 0;
1080 }
1081
1082 static int
1083 GL_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
1048 { 1084 {
1049 GL_RenderData *data = (GL_RenderData *) renderer->driverdata; 1085 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
1050 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 1086 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
1051 1087
1052 data->glClearColor((GLclampf) r * inv255f, (GLclampf) g * inv255f, 1088 data->glColor4f((GLfloat) renderer->r * inv255f,
1053 (GLclampf) b * inv255f, (GLclampf) a * inv255f); 1089 (GLfloat) renderer->g * inv255f,
1054 data->glViewport(rect->x, window->h - rect->y, rect->w, rect->h); 1090 (GLfloat) renderer->b * inv255f,
1055 data->glClear(GL_COLOR_BUFFER_BIT); 1091 (GLfloat) renderer->a * inv255f);
1056 data->glViewport(0, 0, window->w, window->h); 1092 SetBlendMode(data, renderer->blendMode);
1093 data->glRecti(rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
1094
1095 return 0;
1096 }
1097
1098 static int
1099 GL_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
1100 {
1101 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
1102 //data->glLineWidth(1.0);
1103 //data->glPointSize(1.0);
1104
1105 SetBlendMode(data, renderer->blendMode);
1106
1107 data->glColor4f((GLfloat) renderer->r * inv255f,
1108 (GLfloat) renderer->g * inv255f,
1109 (GLfloat) renderer->b * inv255f,
1110 (GLfloat) renderer->a * inv255f);
1111
1112 if ((x1 == x2) && (y1 == y2)) {
1113 data->glBegin(GL_POINTS);
1114 data->glVertex2i(x1, y1);
1115 data->glEnd();
1116 } else {
1117 data->glBegin(GL_LINES);
1118 data->glVertex2i(x1, y1);
1119 data->glVertex2i(x2, y2);
1120 data->glEnd();
1121 }
1057 return 0; 1122 return 0;
1058 } 1123 }
1059 1124
1060 static int 1125 static int
1061 GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 1126 GL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
1071 void *pixels; 1136 void *pixels;
1072 int bpp = bytes_per_pixel(texture->format); 1137 int bpp = bytes_per_pixel(texture->format);
1073 int pitch = texturedata->pitch; 1138 int pitch = texturedata->pitch;
1074 1139
1075 SetupTextureUpdate(data, texture, pitch); 1140 SetupTextureUpdate(data, texture, pitch);
1141 data->glEnable(texturedata->type);
1076 data->glBindTexture(texturedata->type, texturedata->texture); 1142 data->glBindTexture(texturedata->type, texturedata->texture);
1077 for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) { 1143 for (dirty = texturedata->dirty.list; dirty; dirty = dirty->next) {
1078 SDL_Rect *rect = &dirty->rect; 1144 SDL_Rect *rect = &dirty->rect;
1079 pixels = 1145 pixels =
1080 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch + 1146 (void *) ((Uint8 *) texturedata->pixels + rect->y * pitch +
1081 rect->x * bpp); 1147 rect->x * bpp);
1082 data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y, 1148 data->glTexSubImage2D(texturedata->type, 0, rect->x, rect->y,
1083 rect->w / texturedata->HACK_RYAN_FIXME, rect->h, texturedata->format, 1149 rect->w / texturedata->HACK_RYAN_FIXME,
1150 rect->h, texturedata->format,
1084 texturedata->formattype, pixels); 1151 texturedata->formattype, pixels);
1085 } 1152 }
1086 SDL_ClearDirtyRects(&texturedata->dirty); 1153 SDL_ClearDirtyRects(&texturedata->dirty);
1087 } 1154 }
1088 1155
1098 minv = (GLfloat) srcrect->y / texture->h; 1165 minv = (GLfloat) srcrect->y / texture->h;
1099 minv *= texturedata->texh; 1166 minv *= texturedata->texh;
1100 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h; 1167 maxv = (GLfloat) (srcrect->y + srcrect->h) / texture->h;
1101 maxv *= texturedata->texh; 1168 maxv *= texturedata->texh;
1102 1169
1170 data->glEnable(texturedata->type);
1103 data->glBindTexture(texturedata->type, texturedata->texture); 1171 data->glBindTexture(texturedata->type, texturedata->texture);
1104 1172
1105 if (texture->modMode) { 1173 if (texture->modMode) {
1106 data->glColor4f((GLfloat) texture->r * inv255f, 1174 data->glColor4f((GLfloat) texture->r * inv255f,
1107 (GLfloat) texture->g * inv255f, 1175 (GLfloat) texture->g * inv255f,
1109 (GLfloat) texture->a * inv255f); 1177 (GLfloat) texture->a * inv255f);
1110 } else { 1178 } else {
1111 data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f); 1179 data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1112 } 1180 }
1113 1181
1114 if (texture->blendMode != data->blendMode) { 1182 SetBlendMode(data, texture->blendMode);
1115 switch (texture->blendMode) {
1116 case SDL_TEXTUREBLENDMODE_NONE:
1117 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
1118 data->glDisable(GL_BLEND);
1119 break;
1120 case SDL_TEXTUREBLENDMODE_MASK:
1121 case SDL_TEXTUREBLENDMODE_BLEND:
1122 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1123 data->glEnable(GL_BLEND);
1124 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1125 break;
1126 case SDL_TEXTUREBLENDMODE_ADD:
1127 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1128 data->glEnable(GL_BLEND);
1129 data->glBlendFunc(GL_SRC_ALPHA, GL_ONE);
1130 break;
1131 case SDL_TEXTUREBLENDMODE_MOD:
1132 data->glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1133 data->glEnable(GL_BLEND);
1134 data->glBlendFunc(GL_ZERO, GL_SRC_COLOR);
1135 break;
1136 }
1137 data->blendMode = texture->blendMode;
1138 }
1139 1183
1140 if (texture->scaleMode != data->scaleMode) { 1184 if (texture->scaleMode != data->scaleMode) {
1141 switch (texture->scaleMode) { 1185 switch (texture->scaleMode) {
1142 case SDL_TEXTURESCALEMODE_NONE: 1186 case SDL_TEXTURESCALEMODE_NONE:
1143 case SDL_TEXTURESCALEMODE_FAST: 1187 case SDL_TEXTURESCALEMODE_FAST:
1174 data->glEnd(); 1218 data->glEnd();
1175 1219
1176 if (texturedata->shader != 0) { 1220 if (texturedata->shader != 0) {
1177 data->glDisable(GL_FRAGMENT_PROGRAM_ARB); 1221 data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
1178 } 1222 }
1223
1224 data->glDisable(texturedata->type);
1225
1179 return 0; 1226 return 0;
1180 } 1227 }
1181 1228
1182 static void 1229 static void
1183 GL_RenderPresent(SDL_Renderer * renderer) 1230 GL_RenderPresent(SDL_Renderer * renderer)
1217 if (data->context) { 1264 if (data->context) {
1218 if (data->GL_ARB_fragment_program_supported) { 1265 if (data->GL_ARB_fragment_program_supported) {
1219 data->glDisable(GL_FRAGMENT_PROGRAM_ARB); 1266 data->glDisable(GL_FRAGMENT_PROGRAM_ARB);
1220 data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0); 1267 data->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
1221 if (data->fragment_program_UYVY != 0) { 1268 if (data->fragment_program_UYVY != 0) {
1222 data->glDeleteProgramsARB(1, &data->fragment_program_UYVY); 1269 data->glDeleteProgramsARB(1,
1270 &data->fragment_program_UYVY);
1223 } 1271 }
1224 } 1272 }
1225 1273
1226 /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */ 1274 /* SDL_GL_MakeCurrent(0, NULL); *//* doesn't do anything */
1227 SDL_GL_DeleteContext(data->context); 1275 SDL_GL_DeleteContext(data->context);