comparison src/video/SDL_renderer_gl.c @ 3433:ad845d9835aa

Hmm, this isn't going to work, is it?
author Sam Lantinga <slouken@libsdl.org>
date Sun, 15 Nov 2009 08:01:34 +0000
parents c8fbb27627d9
children 9f62f47d989b
comparison
equal deleted inserted replaced
3432:116f74112e8a 3433:ad845d9835aa
581 /* Store out the final fragment color... */ 581 /* Store out the final fragment color... */
582 "MOV outcolor, work;\n" 582 "MOV outcolor, work;\n"
583 /* ...and we're done! */ 583 /* ...and we're done! */
584 "END\n"; 584 "END\n";
585 585
586 static __inline__ SDL_bool
587 convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
588 GLint* internalFormat, GLenum* format, GLenum* type)
589 {
590 switch (pixel_format) {
591 case SDL_PIXELFORMAT_INDEX1LSB:
592 case SDL_PIXELFORMAT_INDEX1MSB:
593 *internalFormat = GL_RGB;
594 *format = GL_COLOR_INDEX;
595 *type = GL_BITMAP;
596 break;
597 case SDL_PIXELFORMAT_INDEX8:
598 if (!renderdata->GL_EXT_paletted_texture_supported) {
599 return SDL_FALSE;
600 }
601 *internalFormat = GL_COLOR_INDEX8_EXT;
602 *format = GL_COLOR_INDEX;
603 *type = GL_UNSIGNED_BYTE;
604 break;
605 case SDL_PIXELFORMAT_RGB332:
606 *internalFormat = GL_R3_G3_B2;
607 *format = GL_RGB;
608 *type = GL_UNSIGNED_BYTE_3_3_2;
609 break;
610 case SDL_PIXELFORMAT_RGB444:
611 *internalFormat = GL_RGB4;
612 *format = GL_RGB;
613 *type = GL_UNSIGNED_SHORT_4_4_4_4;
614 break;
615 case SDL_PIXELFORMAT_RGB555:
616 *internalFormat = GL_RGB5;
617 *format = GL_RGB;
618 *type = GL_UNSIGNED_SHORT_5_5_5_1;
619 break;
620 case SDL_PIXELFORMAT_ARGB4444:
621 *internalFormat = GL_RGBA4;
622 *format = GL_BGRA;
623 *type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
624 break;
625 case SDL_PIXELFORMAT_ARGB1555:
626 *internalFormat = GL_RGB5_A1;
627 *format = GL_BGRA;
628 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
629 break;
630 case SDL_PIXELFORMAT_RGB565:
631 *internalFormat = GL_RGB8;
632 *format = GL_RGB;
633 *type = GL_UNSIGNED_SHORT_5_6_5;
634 break;
635 case SDL_PIXELFORMAT_RGB24:
636 *internalFormat = GL_RGB8;
637 *format = GL_RGB;
638 *type = GL_UNSIGNED_BYTE;
639 break;
640 case SDL_PIXELFORMAT_RGB888:
641 *internalFormat = GL_RGB8;
642 *format = GL_BGRA;
643 *type = GL_UNSIGNED_BYTE;
644 break;
645 case SDL_PIXELFORMAT_BGR24:
646 *internalFormat = GL_RGB8;
647 *format = GL_BGR;
648 *type = GL_UNSIGNED_BYTE;
649 break;
650 case SDL_PIXELFORMAT_BGR888:
651 *internalFormat = GL_RGB8;
652 *format = GL_RGBA;
653 *type = GL_UNSIGNED_BYTE;
654 break;
655 case SDL_PIXELFORMAT_ARGB8888:
656 #ifdef __MACOSX__
657 *internalFormat = GL_RGBA;
658 *format = GL_BGRA;
659 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
660 #else
661 *internalFormat = GL_RGBA8;
662 *format = GL_BGRA;
663 *type = GL_UNSIGNED_BYTE;
664 #endif
665 break;
666 case SDL_PIXELFORMAT_ABGR8888:
667 *internalFormat = GL_RGBA8;
668 *format = GL_RGBA;
669 *type = GL_UNSIGNED_BYTE;
670 break;
671 case SDL_PIXELFORMAT_ARGB2101010:
672 *internalFormat = GL_RGB10_A2;
673 *format = GL_BGRA;
674 *type = GL_UNSIGNED_INT_2_10_10_10_REV;
675 break;
676 case SDL_PIXELFORMAT_UYVY:
677 if (renderdata->GL_APPLE_ycbcr_422_supported) {
678 *internalFormat = GL_RGB;
679 *format = GL_YCBCR_422_APPLE;
680 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
681 *type = GL_UNSIGNED_SHORT_8_8_APPLE;
682 #else
683 *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
684 #endif
685 } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
686 *internalFormat = GL_RGB;
687 *format = GL_YCBCR_MESA;
688 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
689 *type = GL_UNSIGNED_SHORT_8_8_MESA;
690 #else
691 *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
692 #endif
693 } else if (renderdata->GL_ARB_fragment_program_supported) {
694 *internalFormat = GL_RGBA;
695 *format = GL_RGBA;
696 *type = GL_UNSIGNED_BYTE;
697 } else {
698 return SDL_FALSE;
699 }
700 break;
701 case SDL_PIXELFORMAT_YUY2:
702 if (renderdata->GL_APPLE_ycbcr_422_supported) {
703 *internalFormat = GL_RGB;
704 *format = GL_YCBCR_422_APPLE;
705 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
706 *type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
707 #else
708 *type = GL_UNSIGNED_SHORT_8_8_APPLE;
709 #endif
710 } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
711 *internalFormat = GL_RGB;
712 *format = GL_YCBCR_MESA;
713 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
714 *type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
715 #else
716 *type = GL_UNSIGNED_SHORT_8_8_MESA;
717 #endif
718 } else {
719 return SDL_FALSE;
720 }
721 break;
722 default:
723 return SDL_FALSE;
724 }
725 return SDL_TRUE;
726 }
586 727
587 static int 728 static int
588 GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) 729 GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
589 { 730 {
590 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata; 731 GL_RenderData *renderdata = (GL_RenderData *) renderer->driverdata;
594 GLenum format, type; 735 GLenum format, type;
595 int texture_w, texture_h; 736 int texture_w, texture_h;
596 GLuint shader = 0; 737 GLuint shader = 0;
597 GLenum result; 738 GLenum result;
598 739
599 switch (texture->format) { 740 if (!convert_format(renderdata, texture->format, &internalFormat,
600 case SDL_PIXELFORMAT_INDEX1LSB: 741 &format, &type)) {
601 case SDL_PIXELFORMAT_INDEX1MSB:
602 internalFormat = GL_RGB;
603 format = GL_COLOR_INDEX;
604 type = GL_BITMAP;
605 break;
606 case SDL_PIXELFORMAT_INDEX8:
607 if (!renderdata->GL_EXT_paletted_texture_supported) {
608 SDL_SetError("Unsupported texture format");
609 return -1;
610 }
611 internalFormat = GL_COLOR_INDEX8_EXT;
612 format = GL_COLOR_INDEX;
613 type = GL_UNSIGNED_BYTE;
614 break;
615 case SDL_PIXELFORMAT_RGB332:
616 internalFormat = GL_R3_G3_B2;
617 format = GL_RGB;
618 type = GL_UNSIGNED_BYTE_3_3_2;
619 break;
620 case SDL_PIXELFORMAT_RGB444:
621 internalFormat = GL_RGB4;
622 format = GL_RGB;
623 type = GL_UNSIGNED_SHORT_4_4_4_4;
624 break;
625 case SDL_PIXELFORMAT_RGB555:
626 internalFormat = GL_RGB5;
627 format = GL_RGB;
628 type = GL_UNSIGNED_SHORT_5_5_5_1;
629 break;
630 case SDL_PIXELFORMAT_ARGB4444:
631 internalFormat = GL_RGBA4;
632 format = GL_BGRA;
633 type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
634 break;
635 case SDL_PIXELFORMAT_ARGB1555:
636 internalFormat = GL_RGB5_A1;
637 format = GL_BGRA;
638 type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
639 break;
640 case SDL_PIXELFORMAT_RGB565:
641 internalFormat = GL_RGB8;
642 format = GL_RGB;
643 type = GL_UNSIGNED_SHORT_5_6_5;
644 break;
645 case SDL_PIXELFORMAT_RGB24:
646 internalFormat = GL_RGB8;
647 format = GL_RGB;
648 type = GL_UNSIGNED_BYTE;
649 break;
650 case SDL_PIXELFORMAT_RGB888:
651 internalFormat = GL_RGB8;
652 format = GL_BGRA;
653 type = GL_UNSIGNED_BYTE;
654 break;
655 case SDL_PIXELFORMAT_BGR24:
656 internalFormat = GL_RGB8;
657 format = GL_BGR;
658 type = GL_UNSIGNED_BYTE;
659 break;
660 case SDL_PIXELFORMAT_BGR888:
661 internalFormat = GL_RGB8;
662 format = GL_RGBA;
663 type = GL_UNSIGNED_BYTE;
664 break;
665 case SDL_PIXELFORMAT_ARGB8888:
666 #ifdef __MACOSX__
667 internalFormat = GL_RGBA;
668 format = GL_BGRA;
669 type = GL_UNSIGNED_INT_8_8_8_8_REV;
670 #else
671 internalFormat = GL_RGBA8;
672 format = GL_BGRA;
673 type = GL_UNSIGNED_BYTE;
674 #endif
675 break;
676 case SDL_PIXELFORMAT_ABGR8888:
677 internalFormat = GL_RGBA8;
678 format = GL_RGBA;
679 type = GL_UNSIGNED_BYTE;
680 break;
681 case SDL_PIXELFORMAT_ARGB2101010:
682 internalFormat = GL_RGB10_A2;
683 format = GL_BGRA;
684 type = GL_UNSIGNED_INT_2_10_10_10_REV;
685 break;
686 case SDL_PIXELFORMAT_UYVY:
687 if (renderdata->GL_APPLE_ycbcr_422_supported) {
688 internalFormat = GL_RGB;
689 format = GL_YCBCR_422_APPLE;
690 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
691 type = GL_UNSIGNED_SHORT_8_8_APPLE;
692 #else
693 type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
694 #endif
695 } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
696 internalFormat = GL_RGB;
697 format = GL_YCBCR_MESA;
698 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
699 type = GL_UNSIGNED_SHORT_8_8_MESA;
700 #else
701 type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
702 #endif
703 } else if (renderdata->GL_ARB_fragment_program_supported) {
704 if (renderdata->fragment_program_UYVY == 0) {
705 renderdata->fragment_program_UYVY =
706 compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
707 fragment_program_UYVY_source_code);
708 if (renderdata->fragment_program_UYVY == 0) {
709 SDL_SetError("Fragment program compile error");
710 return -1;
711 }
712 }
713 shader = renderdata->fragment_program_UYVY;
714 internalFormat = GL_RGBA;
715 format = GL_RGBA;
716 type = GL_UNSIGNED_BYTE;
717 } else {
718 SDL_SetError("Unsupported texture format");
719 return -1;
720 }
721 break;
722 case SDL_PIXELFORMAT_YUY2:
723 if (renderdata->GL_APPLE_ycbcr_422_supported) {
724 internalFormat = GL_RGB;
725 format = GL_YCBCR_422_APPLE;
726 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
727 type = GL_UNSIGNED_SHORT_8_8_REV_APPLE;
728 #else
729 type = GL_UNSIGNED_SHORT_8_8_APPLE;
730 #endif
731 } else if (renderdata->GL_MESA_ycbcr_texture_supported) {
732 internalFormat = GL_RGB;
733 format = GL_YCBCR_MESA;
734 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
735 type = GL_UNSIGNED_SHORT_8_8_REV_MESA;
736 #else
737 type = GL_UNSIGNED_SHORT_8_8_MESA;
738 #endif
739 } else {
740 SDL_SetError("Unsupported texture format");
741 return -1;
742 }
743 break;
744 default:
745 SDL_SetError("Unsupported texture format"); 742 SDL_SetError("Unsupported texture format");
746 return -1; 743 return -1;
744 }
745 if (texture->format == SDL_PIXELFORMAT_UYVY &&
746 !renderdata->GL_APPLE_ycbcr_422_supported &&
747 !renderdata->GL_MESA_ycbcr_texture_supported &&
748 renderdata->GL_ARB_fragment_program_supported) {
749 if (renderdata->fragment_program_UYVY == 0) {
750 renderdata->fragment_program_UYVY =
751 compile_shader(renderdata, GL_FRAGMENT_PROGRAM_ARB,
752 fragment_program_UYVY_source_code);
753 if (renderdata->fragment_program_UYVY == 0) {
754 SDL_SetError("Fragment program compile error");
755 return -1;
756 }
757 }
758 shader = renderdata->fragment_program_UYVY;
747 } 759 }
748 760
749 data = (GL_TextureData *) SDL_calloc(1, sizeof(*data)); 761 data = (GL_TextureData *) SDL_calloc(1, sizeof(*data));
750 if (!data) { 762 if (!data) {
751 SDL_OutOfMemory(); 763 SDL_OutOfMemory();
1242 1254
1243 static int 1255 static int
1244 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, 1256 GL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
1245 void * pixels, int pitch) 1257 void * pixels, int pitch)
1246 { 1258 {
1247 //glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h, 1259 GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
1260 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
1261 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1262 Uint32 pixel_format = display->current_mode.format;
1263 GLint internalFormat;
1264 GLenum format, type;
1265
1266 if (!convert_format(data, pixel_format, &internalFormat, &format, &type)) {
1267 SDL_SetError("Unsupported pixel format");
1268 return -1;
1269 }
1270
1271 if (pixel_format == SDL_PIXELFORMAT_INDEX1LSB) {
1272 data->glPixelStorei(GL_PACK_LSB_FIRST, 1);
1273 } else if (pixel_format == SDL_PIXELFORMAT_INDEX1MSB) {
1274 data->glPixelStorei(GL_PACK_LSB_FIRST, 0);
1275 }
1276 data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
1277 data->glPixelStorei(GL_PACK_ROW_LENGTH,
1278 -2 * (pitch / bytes_per_pixel(pixel_format)));
1279
1280 data->glReadPixels(rect->x, rect->y+rect->h-1, rect->w, rect->h,
1281 format, type, pixels + (rect->h-1)*pitch);
1248 } 1282 }
1249 1283
1250 static int 1284 static int
1251 GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect, 1285 GL_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
1252 const void * pixels, int pitch) 1286 const void * pixels, int pitch)