4 #if !defined(GLAPIENTRY) 5 #if defined(GL_APIENTRY) 6 #define GLAPIENTRY GL_APIENTRY 14 #include "SDL_platform.h" 20 #if __STDC_VERSION__ >= 199901L 26 #include "stb_image.h" 27 #include "stb_image_write.h" 30 #define M_PI 3.14159265358979323846 36 #define static_inline static 38 #define static_inline static inline 43 #define __func__ __FUNCTION__ 47 #if defined(_MSC_VER) && (_MSC_VER < 1900) 48 #define snprintf c99_snprintf 50 static_inline int c99_vsnprintf(
char* str,
size_t size,
const char* format, va_list ap)
55 count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
57 count = _vscprintf(format, ap);
62 static_inline int c99_snprintf(
char* str,
size_t size,
const char* format, ...)
68 count = c99_vsnprintf(str, size, format, ap);
79 #if defined(SDL_GPU_USE_BUFFER_PIPELINE) && !defined(SDL_GPU_USE_BUFFER_RESET) && !defined(SDL_GPU_USE_BUFFER_MAPPING) && !defined(SDL_GPU_USE_BUFFER_UPDATE) 80 #define SDL_GPU_USE_BUFFER_RESET 85 #define GPU_BLIT_BUFFER_VERTICES_PER_SPRITE 4 86 #define GPU_BLIT_BUFFER_INIT_MAX_NUM_VERTICES (GPU_BLIT_BUFFER_VERTICES_PER_SPRITE*1000) 90 #define GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES 60000 92 #define GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES 4000000000u 96 #define GPU_BLIT_BUFFER_FLOATS_PER_VERTEX 8 99 #define GPU_BLIT_BUFFER_STRIDE (sizeof(float)*GPU_BLIT_BUFFER_FLOATS_PER_VERTEX) 100 #define GPU_BLIT_BUFFER_VERTEX_OFFSET 0 101 #define GPU_BLIT_BUFFER_TEX_COORD_OFFSET 2 102 #define GPU_BLIT_BUFFER_COLOR_OFFSET 4 109 #ifdef SDL_GPU_USE_SDL2 111 #define GET_ALPHA(sdl_color) ((sdl_color).a) 115 return SDL_GetWindowFromID(windowID);
120 return SDL_GetWindowID(window);
125 SDL_GetWindowSize(window, w, h);
130 SDL_GL_GetDrawableSize(window, w, h);
135 SDL_SetWindowSize(SDL_GetWindowFromID(target->
context->
windowID), w, h);
140 return (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
145 return (SDL_GetColorKey(surface, NULL) == 0);
150 #define SDL_Window SDL_Surface 151 #define GET_ALPHA(sdl_color) ((sdl_color).unused) 155 return (windowID == 1? SDL_GetVideoSurface() : NULL);
160 return (SDL_GetVideoSurface() == window? 1 : 0);
178 SDL_Surface* screen = SDL_GetVideoSurface();
179 Uint32 flags = screen->flags;
181 screen = SDL_SetVideoMode(w, h, 0, flags);
183 screen = SDL_SetVideoMode(w, h, 0, flags);
188 return (window->flags & SDL_FULLSCREEN);
193 return (surface->flags & SDL_SRCCOLORKEY);
203 if(target == NULL || target->
context == NULL)
212 if(target == NULL || target->
context == NULL)
221 #ifndef GL_VERTEX_SHADER 222 #ifndef SDL_GPU_DISABLE_SHADERS 223 #define SDL_GPU_DISABLE_SHADERS 229 #ifdef SDL_GPU_USE_OPENGL 237 static SDL_PixelFormat* AllocFormat(GLenum glFormat);
238 static void FreeFormat(SDL_PixelFormat* format);
241 static char shader_message[256];
245 static GPU_bool isExtensionSupported(
const char* extension_str)
247 #ifdef SDL_GPU_USE_OPENGL 248 return glewIsExtensionSupported(extension_str);
251 char* p = (
char*)glGetString(GL_EXTENSIONS);
253 unsigned long extNameLen;
258 extNameLen = strlen(extension_str);
263 unsigned long n = strcspn(p,
" ");
264 if((extNameLen == n) && (strncmp(extension_str, p, n) == 0))
275 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
276 #if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2 277 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
280 glTexSubImage2D(GL_TEXTURE_2D, 0,
281 update_rect.
x, update_rect.
y, update_rect.
w, update_rect.
h,
282 format, GL_UNSIGNED_BYTE, pixels);
284 #if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2 285 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
287 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
290 static void row_upload_texture(
const unsigned char* pixels,
GPU_Rect update_rect, Uint32 format,
int alignment,
unsigned int pitch,
int bytes_per_pixel)
293 unsigned int h = update_rect.
h;
294 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
295 if(h > 0 && update_rect.
w > 0.0f)
298 for(i = 0; i < h; ++i)
300 glTexSubImage2D(GL_TEXTURE_2D, 0,
301 update_rect.
x, update_rect.
y + i, update_rect.
w, 1,
302 format, GL_UNSIGNED_BYTE, pixels);
306 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
309 static void copy_upload_texture(
const unsigned char* pixels,
GPU_Rect update_rect, Uint32 format,
int alignment,
unsigned int pitch,
int bytes_per_pixel)
312 unsigned int h = update_rect.
h;
313 unsigned int w = update_rect.
w*bytes_per_pixel;
320 unsigned char *copy = (
unsigned char*)SDL_malloc(update_rect.
h*w);
321 unsigned char *dst = copy;
323 for(i = 0; i < h; ++i)
325 memcpy(dst, pixels, w);
334 static void (*slow_upload_texture)(
const unsigned char* pixels,
GPU_Rect update_rect, Uint32 format,
int alignment,
unsigned int pitch,
int bytes_per_pixel) = NULL;
338 #if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2 341 if(row_length == update_rect.
w)
344 slow_upload_texture(pixels, update_rect, format, alignment, pitch, bytes_per_pixel);
351 #if defined(SDL_GPU_USE_OPENGL) || SDL_GPU_GLES_MAJOR_VERSION > 2 352 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
353 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
354 glTexImage2D(GL_TEXTURE_2D, 0, format, update_rect.
w, update_rect.
h, 0,
355 format, GL_UNSIGNED_BYTE, pixels);
356 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
357 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
359 glTexImage2D(GL_TEXTURE_2D, 0, format, update_rect.
w, update_rect.
h, 0,
360 format, GL_UNSIGNED_BYTE, NULL);
361 upload_texture(pixels, update_rect, format, alignment, row_length, row_length*bytes_per_pixel, bytes_per_pixel);
366 #if defined(SDL_GPU_ASSUME_CORE_FBO) 367 #define glBindFramebufferPROC glBindFramebuffer 368 #define glCheckFramebufferStatusPROC glCheckFramebufferStatus 369 #define glDeleteFramebuffersPROC glDeleteFramebuffers 370 #define glFramebufferTexture2DPROC glFramebufferTexture2D 371 #define glGenFramebuffersPROC glGenFramebuffers 372 #define glGenerateMipmapPROC glGenerateMipmap 378 static GLenum
GLAPIENTRY glCheckFramebufferStatusNOOP(GLenum target)
391 static void GLAPIENTRY glGenFramebuffersNOOP(GLsizei n, GLuint *
ids)
395 static void GLAPIENTRY glGenerateMipmapNOOP(GLenum target)
401 static GLenum (
GLAPIENTRY *glCheckFramebufferStatusPROC)(GLenum target) = glCheckFramebufferStatusNOOP;
404 static void (
GLAPIENTRY *glGenFramebuffersPROC)(GLsizei n, GLuint *
ids) = glGenFramebuffersNOOP;
405 static void (
GLAPIENTRY *glGenerateMipmapPROC)(GLenum target) = glGenerateMipmapNOOP;
414 #ifdef SDL_GPU_USE_OPENGL 415 #if SDL_GPU_GL_MAJOR_VERSION >= 2 419 if(isExtensionSupported(
"GL_ARB_texture_non_power_of_two"))
424 #elif defined(SDL_GPU_USE_GLES) 425 #if SDL_GPU_GLES_MAJOR_VERSION >= 3 429 if(isExtensionSupported(
"GL_OES_texture_npot") || isExtensionSupported(
"GL_IMG_texture_npot")
430 || isExtensionSupported(
"GL_APPLE_texture_2D_limited_npot") || isExtensionSupported(
"GL_ARB_texture_non_power_of_two"))
435 #if SDL_GPU_GLES_MAJOR_VERSION >= 2 443 #ifdef SDL_GPU_ASSUME_CORE_FBO 446 #elif defined(SDL_GPU_USE_OPENGL) 447 if(isExtensionSupported(
"GL_ARB_framebuffer_object"))
458 else if(isExtensionSupported(
"GL_EXT_framebuffer_object"))
461 glBindFramebufferPROC = glBindFramebufferEXT;
462 glCheckFramebufferStatusPROC = glCheckFramebufferStatusEXT;
463 glDeleteFramebuffersPROC = glDeleteFramebuffersEXT;
464 glFramebufferTexture2DPROC = glFramebufferTexture2DEXT;
465 glGenFramebuffersPROC = glGenFramebuffersEXT;
466 glGenerateMipmapPROC = glGenerateMipmapEXT;
470 #elif defined(SDL_GPU_USE_GLES) 471 if(isExtensionSupported(
"GL_OES_framebuffer_object"))
474 glBindFramebufferPROC = glBindFramebufferOES;
475 glCheckFramebufferStatusPROC = glCheckFramebufferStatusOES;
476 glDeleteFramebuffersPROC = glDeleteFramebuffersOES;
477 glFramebufferTexture2DPROC = glFramebufferTexture2DOES;
478 glGenFramebuffersPROC = glGenFramebuffersOES;
479 glGenerateMipmapPROC = glGenerateMipmapOES;
486 #ifdef SDL_GPU_USE_OPENGL 490 #if SDL_GPU_GL_MAJOR_VERSION >= 2 494 if(isExtensionSupported(
"GL_EXT_blend_equation_separate"))
500 #elif defined(SDL_GPU_USE_GLES) 502 #if SDL_GPU_GLES_MAJOR_VERSION >= 2 508 if(isExtensionSupported(
"GL_OES_blend_subtract"))
513 if(isExtensionSupported(
"GL_OES_blend_func_separate"))
518 if(isExtensionSupported(
"GL_OES_blend_equation_separate"))
526 #ifdef SDL_GPU_USE_OPENGL 527 #if SDL_GPU_GL_MAJOR_VERSION >= 2 530 if(isExtensionSupported(
"GL_ARB_texture_mirrored_repeat"))
535 #elif defined(SDL_GPU_USE_GLES) 536 #if SDL_GPU_GLES_MAJOR_VERSION >= 2 539 if(isExtensionSupported(
"GL_OES_texture_mirrored_repeat"))
547 if(isExtensionSupported(
"GL_EXT_bgr"))
549 if(isExtensionSupported(
"GL_EXT_bgra"))
551 if(isExtensionSupported(
"GL_EXT_abgr"))
556 #ifdef SDL_GPU_USE_GLES 563 #ifndef SDL_GPU_DISABLE_SHADERS 564 if(isExtensionSupported(
"GL_ARB_fragment_shader"))
566 if(isExtensionSupported(
"GL_ARB_vertex_shader"))
568 if(isExtensionSupported(
"GL_ARB_geometry_shader4"))
571 #ifdef SDL_GPU_ASSUME_SHADERS 576 static void extBindFramebuffer(
GPU_Renderer* renderer, GLuint handle)
585 return ((x != 0) && !(x & (x - 1)));
604 renderer->
impl->FlushBlitBuffer(renderer);
606 glBindTexture( GL_TEXTURE_2D, handle );
614 renderer->
impl->FlushBlitBuffer(renderer);
616 glBindTexture( GL_TEXTURE_2D, handle );
631 renderer->
impl->FlushBlitBuffer(renderer);
633 extBindFramebuffer(renderer, handle);
654 renderer->
impl->FlushBlitBuffer(renderer);
656 extBindFramebuffer(renderer, handle);
664 renderer->
impl->FlushBlitBuffer(renderer);
672 renderer->
impl->FlushBlitBuffer(renderer);
688 renderer->
impl->FlushBlitBuffer(renderer);
695 unsigned int new_max_num_vertices;
698 if(minimum_vertices_needed <= cdata->blit_buffer_max_num_vertices)
704 new_max_num_vertices = ((
unsigned int)cdata->blit_buffer_max_num_vertices) * 2;
705 while(new_max_num_vertices <= minimum_vertices_needed)
706 new_max_num_vertices *= 2;
714 memcpy(new_buffer, cdata->blit_buffer, cdata->blit_buffer_num_vertices * GPU_BLIT_BUFFER_STRIDE);
715 SDL_free(cdata->blit_buffer);
716 cdata->blit_buffer = new_buffer;
717 cdata->blit_buffer_max_num_vertices = new_max_num_vertices;
719 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 721 #if !defined(SDL_GPU_NO_VAO) 722 glBindVertexArray(cdata->blit_VAO);
730 #if !defined(SDL_GPU_NO_VAO) 731 glBindVertexArray(0);
740 unsigned int new_max_num_vertices;
741 unsigned short* new_indices;
743 if(minimum_vertices_needed <= cdata->index_buffer_max_num_vertices)
749 new_max_num_vertices = cdata->index_buffer_max_num_vertices * 2;
750 while(new_max_num_vertices <= minimum_vertices_needed)
751 new_max_num_vertices *= 2;
758 new_indices = (
unsigned short*)SDL_malloc(new_max_num_vertices *
sizeof(
unsigned short));
759 memcpy(new_indices, cdata->index_buffer, cdata->index_buffer_num_vertices *
sizeof(
unsigned short));
760 SDL_free(cdata->index_buffer);
761 cdata->index_buffer = new_indices;
762 cdata->index_buffer_max_num_vertices = new_max_num_vertices;
764 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 766 #if !defined(SDL_GPU_NO_VAO) 767 glBindVertexArray(cdata->blit_VAO);
771 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
unsigned short) * cdata->index_buffer_max_num_vertices, NULL, GL_DYNAMIC_DRAW);
773 #if !defined(SDL_GPU_NO_VAO) 774 glBindVertexArray(0);
788 renderer->
impl->FlushBlitBuffer(renderer);
790 #ifdef SDL_GPU_USE_SDL2 801 glEnable(GL_SCISSOR_TEST);
822 glDisable(GL_SCISSOR_TEST);
828 renderer->
impl->SetCamera(renderer, target, &target->
camera);
833 static void changeColor(
GPU_Renderer* renderer, SDL_Color color)
835 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 841 if(cdata->last_color.r != color.r
842 || cdata->last_color.g != color.g
843 || cdata->last_color.b != color.b
846 renderer->
impl->FlushBlitBuffer(renderer);
847 cdata->last_color = color;
848 glColor4f(color.r/255.01f, color.g/255.01f, color.b/255.01f,
GET_ALPHA(color)/255.01f);
856 if(cdata->last_use_blending == enable)
859 renderer->
impl->FlushBlitBuffer(renderer);
866 cdata->last_use_blending = enable;
873 renderer->
impl->FlushBlitBuffer(renderer);
875 cdata->last_blend_mode = mode;
910 if(cdata->last_blend_mode.source_color == mode.
source_color 911 && cdata->last_blend_mode.dest_color == mode.
dest_color 912 && cdata->last_blend_mode.source_alpha == mode.
source_alpha 913 && cdata->last_blend_mode.dest_alpha == mode.
dest_alpha 918 forceChangeBlendMode(renderer, mode);
923 static Uint32 get_proper_program_id(
GPU_Renderer* renderer, Uint32 program_object)
929 if(program_object == 0)
932 return program_object;
943 #ifndef SDL_GPU_SKIP_ENABLE_TEXTURE_2D 945 glEnable(GL_TEXTURE_2D);
947 glDisable(GL_TEXTURE_2D);
957 renderer->
impl->FlushBlitBuffer(renderer);
960 #ifndef SDL_GPU_SKIP_ENABLE_TEXTURE_2D 962 glEnable(GL_TEXTURE_2D);
964 glDisable(GL_TEXTURE_2D);
973 renderer->
impl->FlushBlitBuffer(renderer);
982 renderer->
impl->FlushBlitBuffer(renderer);
987 #define MIX_COLOR_COMPONENT_NORMALIZED_RESULT(a, b) ((a)/255.0f * (b)/255.0f) 988 #define MIX_COLOR_COMPONENT(a, b) (((a)/255.0f * (b)/255.0f)*255) 1003 return image->
color;
1010 enableTexturing(renderer);
1013 renderer->
impl->FlushBlitBuffer(renderer);
1018 changeColor(renderer, get_complete_mod_color(renderer, target, image));
1020 changeBlendMode(renderer, image->
blend_mode);
1027 static void prepareToRenderShapes(
GPU_Renderer* renderer,
unsigned int shape)
1031 disableTexturing(renderer);
1034 renderer->
impl->FlushBlitBuffer(renderer);
1055 cdata->last_viewport = viewport;
1061 if(target->
image != NULL)
1062 y = target->
image->
h - viewport.
h - viewport.
y;
1063 else if(target->
context != NULL)
1067 glViewport(viewport.
x, y, viewport.
w, viewport.
h);
1070 static void changeViewport(
GPU_Target* target)
1074 if(cdata->last_viewport.x == target->
viewport.
x && cdata->last_viewport.y == target->
viewport.
y && cdata->last_viewport.w == target->
viewport.
w && cdata->last_viewport.h == target->
viewport.
h)
1077 forceChangeViewport(target, target->
viewport);
1080 static void applyTargetCamera(
GPU_Target* target)
1084 cdata->last_camera = target->
camera;
1085 cdata->last_camera_inverted = (target->
image != NULL);
1099 applyTargetCamera(target);
1103 static void get_camera_matrix(
float* result,
GPU_Camera camera)
1107 GPU_bool invert = cdata->last_camera_inverted;
1108 float offsetX, offsetY;
1119 offsetX = target->
w/2.0f;
1120 offsetY = target->
h/2.0f;
1133 #ifdef SDL_GPU_APPLY_TRANSFORMS_TO_GL_STACK 1134 static void applyTransforms(
void)
1140 float cam_matrix[16];
1141 get_camera_matrix(cam_matrix, cdata->last_camera);
1145 glMatrixMode(GL_PROJECTION);
1147 glMatrixMode(GL_MODELVIEW);
1158 #ifdef SDL_GPU_USE_OPENGL 1159 const char* vendor_string;
1172 if(GPU_flags & GPU_INIT_DISABLE_DOUBLE_BUFFER)
1173 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
1175 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
1176 #ifdef SDL_GPU_USE_SDL2 1179 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
1180 #ifdef SDL_GPU_USE_GLES 1181 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
1185 #if SDL_GPU_GL_MAJOR_VERSION == 3 1188 if(GPU_flags & GPU_INIT_REQUEST_COMPATIBILITY_PROFILE)
1189 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
1192 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
1203 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, renderer_request.
major_version);
1204 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, renderer_request.
minor_version);
1207 if(!(GPU_flags & GPU_INIT_DISABLE_VSYNC))
1208 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
1211 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
1213 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
1214 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
1215 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
1216 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
1220 #ifdef SDL_GPU_USE_SDL2 1240 SDL_flags |= SDL_WINDOW_OPENGL;
1241 if(!(SDL_flags & SDL_WINDOW_HIDDEN))
1242 SDL_flags |= SDL_WINDOW_SHOWN;
1245 window = SDL_CreateWindow(
"",
1246 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1262 SDL_flags |= SDL_OPENGL;
1264 window = SDL_SetVideoMode(w, h, 0, SDL_flags);
1285 #ifdef SDL_GPU_USE_OPENGL 1286 vendor_string = (
const char*)glGetString(GL_VENDOR);
1287 if(strstr(vendor_string,
"Intel") != NULL)
1289 vendor_is_Intel = 1;
1290 apply_Intel_attrib_workaround = 1;
1303 static GPU_bool get_GL_version(
int* major,
int* minor)
1305 const char* version_string;
1306 #ifdef SDL_GPU_USE_OPENGL 1308 version_string = (
const char*)glGetString(GL_VERSION);
1309 if(version_string == NULL || sscanf(version_string,
"%d.%d", major, minor) <= 0)
1313 #if SDL_GPU_GL_MAJOR_VERSION != 3 1325 version_string = (
const char*)glGetString(GL_VERSION);
1327 if(version_string == NULL || sscanf(version_string,
"OpenGL ES %d.%d", major, minor) <= 0)
1330 if(version_string == NULL || sscanf(version_string,
"OpenGL ES-C%*c %d.%d", major, minor) <= 0)
1334 #if SDL_GPU_GLES_MAJOR_VERSION == 1 1348 static GPU_bool get_GLSL_version(
int* version)
1350 #ifndef SDL_GPU_DISABLE_SHADERS 1351 const char* version_string;
1353 #ifdef SDL_GPU_USE_OPENGL 1355 version_string = (
const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
1356 if(version_string == NULL || sscanf(version_string,
"%d.%d", &major, &minor) <= 0)
1363 *version = major*100 + minor;
1367 version_string = (
const char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
1368 if(version_string == NULL || sscanf(version_string,
"OpenGL ES GLSL ES %d.%d", &major, &minor) <= 0)
1375 *version = major*100 + minor;
1389 static void update_stored_dimensions(
GPU_Target* target)
1414 int framebuffer_handle;
1415 SDL_Color white = { 255, 255, 255, 255 };
1416 #ifdef SDL_GPU_USE_OPENGL 1423 int blit_buffer_storage_size;
1424 int index_buffer_storage_size;
1434 target->
image = NULL;
1444 cdata->last_image = NULL;
1445 cdata->last_target = NULL;
1448 cdata->blit_buffer_num_vertices = 0;
1450 cdata->blit_buffer = (
float*)SDL_malloc(blit_buffer_storage_size);
1452 cdata->index_buffer_num_vertices = 0;
1454 cdata->index_buffer = (
unsigned short*)SDL_malloc(index_buffer_storage_size);
1469 SDL_free(cdata->blit_buffer);
1470 SDL_free(cdata->index_buffer);
1473 SDL_free(target->
data);
1482 #ifdef SDL_GPU_USE_SDL2 1490 SDL_free(cdata->blit_buffer);
1491 SDL_free(cdata->index_buffer);
1494 SDL_free(target->
data);
1511 update_stored_dimensions(target);
1540 cdata->last_color = white;
1542 cdata->last_use_texturing =
GPU_TRUE;
1543 cdata->last_shape = GL_TRIANGLES;
1548 cdata->last_viewport = target->
viewport;
1549 cdata->last_camera = target->
camera;
1550 cdata->last_camera_inverted =
GPU_FALSE;
1552 #ifdef SDL_GPU_USE_OPENGL 1553 glewExperimental = GL_TRUE;
1566 framebuffer_handle = 0;
1572 if(!get_API_versions(renderer))
1584 init_features(renderer);
1586 if(!IsFeatureEnabled(renderer, required_features))
1593 #ifdef SDL_GPU_USE_SDL2 1595 if(!(renderer->
GPU_init_flags & (GPU_INIT_DISABLE_VSYNC | GPU_INIT_ENABLE_VSYNC)))
1598 if(SDL_GL_SetSwapInterval(-1) < 0)
1599 SDL_GL_SetSwapInterval(1);
1602 SDL_GL_SetSwapInterval(1);
1604 SDL_GL_SetSwapInterval(0);
1608 if(renderer->
GPU_init_flags & GPU_INIT_USE_COPY_TEXTURE_UPLOAD_FALLBACK)
1609 slow_upload_texture = copy_upload_texture;
1611 slow_upload_texture = row_upload_texture;
1625 #ifndef SDL_GPU_SKIP_ENABLE_TEXTURE_2D 1626 glEnable(GL_TEXTURE_2D);
1628 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1630 glDisable(GL_BLEND);
1631 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
1636 glClear( GL_COLOR_BUFFER_BIT );
1637 #if SDL_GPU_GL_TIER < 3 1638 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
1642 applyTargetCamera(target);
1644 renderer->
impl->SetLineThickness(renderer, 1.0f);
1647 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 1649 #if !defined(SDL_GPU_NO_VAO) 1650 glGenVertexArrays(1, &cdata->blit_VAO);
1651 glBindVertexArray(cdata->blit_VAO);
1659 #ifndef SDL_GPU_DISABLE_SHADERS 1670 #ifdef SDL_GPU_ENABLE_CORE_SHADERS 1700 p = renderer->
impl->CreateShaderProgram(renderer);
1701 renderer->
impl->AttachShader(renderer, p, v);
1702 renderer->
impl->AttachShader(renderer, p, f);
1703 renderer->
impl->LinkShaderProgram(renderer, p);
1737 p = renderer->
impl->CreateShaderProgram(renderer);
1738 renderer->
impl->AttachShader(renderer, p, v);
1739 renderer->
impl->AttachShader(renderer, p, f);
1740 renderer->
impl->LinkShaderProgram(renderer, p);
1760 snprintf(shader_message, 256,
"Shaders not supported by this hardware. Default shaders are disabled.\n");
1764 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 1776 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cdata->blit_IBO);
1777 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof(
unsigned short) * cdata->blit_buffer_max_num_vertices, NULL, GL_DYNAMIC_DRAW);
1804 if(target->
image != NULL)
1819 if(target == NULL || target->
context == NULL)
1822 if(target->
image != NULL)
1826 #ifdef SDL_GPU_USE_SDL2 1831 #ifdef SDL_GPU_USE_SDL2 1832 SDL_GL_MakeCurrent(SDL_GetWindowFromID(windowID), target->
context->
context);
1838 renderer->
impl->FlushBlitBuffer(renderer);
1888 #ifdef SDL_GPU_USE_SDL2 1893 #ifndef SDL_GPU_USE_BUFFER_PIPELINE 1894 glColor4f(cdata->last_color.r/255.01f, cdata->last_color.g/255.01f, cdata->last_color.b/255.01f,
GET_ALPHA(cdata->last_color)/255.01f);
1896 #ifndef SDL_GPU_SKIP_ENABLE_TEXTURE_2D 1897 if(cdata->last_use_texturing)
1898 glEnable(GL_TEXTURE_2D);
1900 glDisable(GL_TEXTURE_2D);
1903 if(cdata->last_use_blending)
1906 glDisable(GL_BLEND);
1908 forceChangeBlendMode(renderer, cdata->last_blend_mode);
1910 forceChangeViewport(target, target->
viewport);
1912 if(cdata->last_image != NULL)
1913 glBindTexture(GL_TEXTURE_2D, ((
GPU_IMAGE_DATA*)(cdata->last_image)->data)->handle);
1915 if(cdata->last_target != NULL)
1916 extBindFramebuffer(renderer, ((
GPU_TARGET_DATA*)cdata->last_target->data)->handle);
1927 renderer->
impl->FlushBlitBuffer(renderer);
1939 #ifdef SDL_GPU_USE_SDL1 1952 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
1953 glClear( GL_COLOR_BUFFER_BIT );
1957 update_stored_dimensions(target);
1970 changeViewport(target);
1975 applyTargetCamera(target);
1989 renderer->
impl->FlushBlitBuffer(renderer);
1996 applyTargetCamera(target);
2008 renderer->
impl->FlushBlitBuffer(renderer);
2016 applyTargetCamera(target);
2031 #ifdef SDL_GPU_USE_SDL2 2033 Uint32 old_flags = SDL_GetWindowFlags(window);
2034 GPU_bool was_fullscreen = (old_flags & SDL_WINDOW_FULLSCREEN);
2035 GPU_bool is_fullscreen = was_fullscreen;
2039 if(enable_fullscreen)
2041 if(use_desktop_resolution)
2042 flags = SDL_WINDOW_FULLSCREEN_DESKTOP;
2044 flags = SDL_WINDOW_FULLSCREEN;
2047 if(SDL_SetWindowFullscreen(window, flags) >= 0)
2049 flags = SDL_GetWindowFlags(window);
2050 is_fullscreen = (flags & SDL_WINDOW_FULLSCREEN);
2055 if(!was_fullscreen && is_fullscreen)
2067 SDL_Surface* surf = SDL_GetVideoSurface();
2068 GPU_bool was_fullscreen = (surf->flags & SDL_FULLSCREEN);
2069 GPU_bool is_fullscreen = was_fullscreen;
2071 if(was_fullscreen ^ enable_fullscreen)
2073 SDL_WM_ToggleFullScreen(surf);
2074 is_fullscreen = (surf->flags & SDL_FULLSCREEN);
2079 if(is_fullscreen != was_fullscreen)
2095 changeViewport(target);
2102 applyTargetCamera(target);
2108 return is_fullscreen;
2128 old_camera = target->
camera;
2130 if(!equal_cameras(new_camera, old_camera))
2133 renderer->
impl->FlushBlitBuffer(renderer);
2135 target->
camera = new_camera;
2141 static GLuint CreateUninitializedTexture(
GPU_Renderer* renderer)
2145 glGenTextures(1, &handle);
2152 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2154 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2155 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2156 #if defined(SDL_GPU_USE_GLES) && (SDL_GPU_GLES_TIER == 1) 2157 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
2159 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
2167 GLuint handle, num_layers, bytes_per_pixel;
2171 SDL_Color white = { 255, 255, 255, 255 };
2176 gl_format = GL_LUMINANCE;
2178 bytes_per_pixel = 1;
2181 gl_format = GL_LUMINANCE_ALPHA;
2183 bytes_per_pixel = 2;
2188 bytes_per_pixel = 3;
2191 gl_format = GL_RGBA;
2193 bytes_per_pixel = 4;
2199 bytes_per_pixel = 3;
2204 gl_format = GL_BGRA;
2206 bytes_per_pixel = 4;
2211 gl_format = GL_ABGR;
2213 bytes_per_pixel = 4;
2217 gl_format = GL_ALPHA;
2219 bytes_per_pixel = 1;
2221 #ifndef SDL_GPU_USE_GLES 2225 bytes_per_pixel = 2;
2229 gl_format = GL_LUMINANCE;
2231 bytes_per_pixel = 1;
2234 gl_format = GL_LUMINANCE;
2236 bytes_per_pixel = 1;
2243 if(bytes_per_pixel < 1 || bytes_per_pixel > 4)
2250 handle = CreateUninitializedTexture(renderer);
2273 result->
color = white;
2281 result->
data = data;
2283 data->handle = handle;
2285 data->format = gl_format;
2303 GLenum internal_format;
2304 static unsigned char* zero_buffer = NULL;
2305 static unsigned int zero_buffer_size = 0;
2313 result = CreateUninitializedImage(renderer, w, h, format);
2321 changeTexturing(renderer,
GPU_TRUE);
2322 bindTexture(renderer, result);
2338 SDL_free(zero_buffer);
2340 zero_buffer = (
unsigned char*)SDL_malloc(zero_buffer_size);
2341 memset(zero_buffer, 0, zero_buffer_size);
2359 #ifdef SDL_GPU_DISABLE_TEXTURE_GETS 2365 GLuint num_layers, bytes_per_pixel;
2367 GLint wrap_s, wrap_t;
2373 SDL_Color white = { 255, 255, 255, 255 };
2378 #ifdef SDL_GPU_USE_GLES 2388 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &gl_format);
2395 bytes_per_pixel = 1;
2397 case GL_LUMINANCE_ALPHA:
2400 bytes_per_pixel = 2;
2405 bytes_per_pixel = 3;
2410 bytes_per_pixel = 4;
2416 bytes_per_pixel = 3;
2423 bytes_per_pixel = 4;
2430 bytes_per_pixel = 4;
2436 bytes_per_pixel = 1;
2438 #ifndef SDL_GPU_USE_GLES 2442 bytes_per_pixel = 2;
2450 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
2451 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
2454 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &min_filter);
2464 case GL_LINEAR_MIPMAP_NEAREST:
2467 case GL_LINEAR_MIPMAP_LINEAR:
2477 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &wrap_s);
2478 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &wrap_t);
2483 case GL_CLAMP_TO_EDGE:
2500 case GL_CLAMP_TO_EDGE:
2519 data->handle = handle;
2520 data->owns_handle = take_ownership;
2521 data->format = gl_format;
2537 result->
color = white;
2545 result->
data = data;
2589 renderer->
impl->FlushBlitBuffer(renderer);
2591 if(bindFramebuffer(renderer, source))
2593 glReadPixels(0, 0, source->
base_w, source->
base_h, format, GL_UNSIGNED_BYTE, pixels);
2601 #ifdef SDL_GPU_USE_GLES 2610 #ifdef SDL_GPU_USE_GLES 2613 if(source->
target == NULL)
2621 result = readTargetPixels(renderer, source->
target, format, pixels);
2624 renderer->
impl->FreeTarget(renderer, source->
target);
2630 glGetTexImage(GL_TEXTURE_2D, 0, format, GL_UNSIGNED_BYTE, pixels);
2640 int bytes_per_pixel;
2641 unsigned char* data;
2643 unsigned char* copy;
2647 renderer->
impl->FlushBlitBuffer(renderer);
2649 bytes_per_pixel = 4;
2650 if(target->
image != NULL)
2652 data = (
unsigned char*)SDL_malloc(target->
base_w * target->
base_h * bytes_per_pixel);
2662 pitch = target->
base_w * bytes_per_pixel;
2663 copy = (
unsigned char*)SDL_malloc(pitch);
2665 for(y = 0; y < target->
base_h/2; y++)
2667 unsigned char* top = &data[target->
base_w * y * bytes_per_pixel];
2668 unsigned char* bottom = &data[target->
base_w * (target->
base_h - y - 1) * bytes_per_pixel];
2669 memcpy(copy, top, pitch);
2670 memcpy(top, bottom, pitch);
2671 memcpy(bottom, copy, pitch);
2680 unsigned char* data;
2683 renderer->
impl->FlushBlitBuffer(renderer);
2700 SDL_Surface* surface;
2702 if(image == NULL || filename == NULL ||
2715 SDL_FreeSurface(surface);
2721 unsigned char* data;
2722 SDL_Surface* result;
2723 SDL_PixelFormat* format;
2736 data = getRawTargetData(renderer, target);
2746 result = SDL_CreateRGBSurface(SDL_SWSURFACE, target->
base_w, target->
base_h, format->BitsPerPixel, format->Rmask, format->Gmask, format->Bmask, format->Amask);
2758 int source_pitch = target->
base_w*format->BytesPerPixel;
2759 for(i = 0; i < target->
base_h; ++i)
2761 memcpy((Uint8*)result->pixels + i*result->pitch, data + source_pitch*i, source_pitch);
2773 unsigned char* data;
2774 SDL_Surface* result;
2775 SDL_PixelFormat* format;
2783 if(image->
w < 1 || image->
h < 1)
2800 data = getRawImageData(renderer, image);
2810 result = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, format->BitsPerPixel, format->Rmask, format->Gmask, format->Bmask, format->Amask);
2822 int source_pitch = image->
texture_w*format->BytesPerPixel;
2823 for(i = 0; i < h; ++i)
2825 memcpy((Uint8*)result->pixels + i*result->pitch, data + source_pitch*i, result->pitch);
2851 #ifdef SDL_GPU_USE_GLES 2853 static int compareFormats(
GPU_Renderer* renderer, GLenum glFormat, SDL_Surface* surface, GLenum* surfaceFormatResult)
2855 SDL_PixelFormat* format = surface->format;
2860 if(format->BytesPerPixel != 3)
2863 if(format->Rmask == 0x0000FF && format->Gmask == 0x00FF00 && format->Bmask == 0xFF0000)
2865 if(surfaceFormatResult != NULL)
2866 *surfaceFormatResult = GL_RGB;
2870 if(format->Rmask == 0xFF0000 && format->Gmask == 0x00FF00 && format->Bmask == 0x0000FF)
2874 if(surfaceFormatResult != NULL)
2875 *surfaceFormatResult = GL_BGR;
2883 if(format->BytesPerPixel != 4)
2886 if (format->Rmask == 0x000000FF && format->Gmask == 0x0000FF00 && format->Bmask == 0x00FF0000)
2888 if(surfaceFormatResult != NULL)
2889 *surfaceFormatResult = GL_RGBA;
2893 if (format->Rmask == 0x00FF0000 && format->Gmask == 0x0000FF00 && format->Bmask == 0x000000FF)
2897 if(surfaceFormatResult != NULL)
2898 *surfaceFormatResult = GL_BGRA;
2904 if (format->Rmask == 0xFF000000 && format->Gmask == 0x00FF0000 && format->Bmask == 0x0000FF00)
2908 if(surfaceFormatResult != NULL)
2909 *surfaceFormatResult = GL_ABGR;
2922 static int compareFormats(
GPU_Renderer* renderer, GLenum glFormat, SDL_Surface* surface, GLenum* surfaceFormatResult)
2924 SDL_PixelFormat* format = surface->format;
2929 if(format->BytesPerPixel != 3)
2933 if(format->Rmask == 0x0000FF && format->Gmask == 0x00FF00 && format->Bmask == 0xFF0000)
2935 if(surfaceFormatResult != NULL)
2936 *surfaceFormatResult = GL_RGB;
2940 if(format->Rmask == 0xFF0000 && format->Gmask == 0x00FF00 && format->Bmask == 0x0000FF)
2945 if(surfaceFormatResult != NULL)
2946 *surfaceFormatResult = GL_BGR;
2956 if(format->BytesPerPixel != 4)
2960 if(format->Rmask == 0x000000FF && format->Gmask == 0x0000FF00 && format->Bmask == 0x00FF0000)
2962 if(surfaceFormatResult != NULL)
2963 *surfaceFormatResult = GL_RGBA;
2967 if(format->Rmask == 0xFF000000 && format->Gmask == 0x00FF0000 && format->Bmask == 0x0000FF00)
2972 if(surfaceFormatResult != NULL)
2973 *surfaceFormatResult = GL_ABGR;
2979 else if(format->Rmask == 0x00FF0000 && format->Gmask == 0x0000FF00 && format->Bmask == 0x000000FF)
2985 if(surfaceFormatResult != NULL)
2986 *surfaceFormatResult = GL_BGRA;
3001 static SDL_PixelFormat* AllocFormat(GLenum glFormat)
3005 Uint32 Rmask, Gmask, Bmask, Amask = 0, mask;
3006 SDL_PixelFormat* result;
3055 result = (SDL_PixelFormat*)SDL_malloc(
sizeof(SDL_PixelFormat));
3056 memset(result, 0,
sizeof(SDL_PixelFormat));
3058 result->BitsPerPixel = 8*channels;
3059 result->BytesPerPixel = channels;
3061 result->Rmask = Rmask;
3065 for (mask = Rmask; !(mask & 0x01); mask >>= 1)
3067 for (; (mask & 0x01); mask >>= 1)
3071 result->Gmask = Gmask;
3075 for (mask = Gmask; !(mask & 0x01); mask >>= 1)
3077 for (; (mask & 0x01); mask >>= 1)
3081 result->Bmask = Bmask;
3085 for (mask = Bmask; !(mask & 0x01); mask >>= 1)
3087 for (; (mask & 0x01); mask >>= 1)
3091 result->Amask = Amask;
3095 for (mask = Amask; !(mask & 0x01); mask >>= 1)
3097 for (; (mask & 0x01); mask >>= 1)
3104 static void FreeFormat(SDL_PixelFormat* format)
3111 static SDL_Surface* copySurfaceIfNeeded(
GPU_Renderer* renderer, GLenum glFormat, SDL_Surface* surface, GLenum* surfaceFormatResult)
3114 int format_compare = compareFormats(renderer, glFormat, surface, surfaceFormatResult);
3117 if(format_compare < 0)
3122 if(format_compare > 0)
3125 SDL_PixelFormat* dst_fmt = AllocFormat(glFormat);
3126 surface = SDL_ConvertSurface(surface, dst_fmt, 0);
3127 FreeFormat(dst_fmt);
3128 if(surfaceFormatResult != NULL && surface != NULL)
3129 *surfaceFormatResult = glFormat;
3175 SDL_Color color = image->
color;
3179 Uint16 w = 0, h = 0;
3190 renderer->
impl->Blit(renderer, image, NULL, target, image->
w / 2, image->
h / 2);
3209 GLenum internal_format;
3212 unsigned char* texture_data = getRawImageData(renderer, image);
3213 if(texture_data == NULL)
3222 SDL_free(texture_data);
3227 changeTexturing(renderer, 1);
3228 bindTexture(renderer, result);
3248 SDL_free(texture_data);
3266 result = gpu_copy_image_pixels_only(renderer, image);
3291 GLenum original_format;
3293 SDL_Surface* newSurface;
3299 if(image == NULL || surface == NULL)
3303 original_format = data->format;
3305 newSurface = copySurfaceIfNeeded(renderer, data->format, surface, &original_format);
3306 if(newSurface == NULL)
3312 if(image_rect != NULL)
3314 updateRect = *image_rect;
3315 if(updateRect.
x < 0)
3317 updateRect.
w += updateRect.
x;
3320 if(updateRect.
y < 0)
3322 updateRect.
h += updateRect.
y;
3325 if(updateRect.
x + updateRect.
w > image->
base_w)
3326 updateRect.
w += image->
base_w - (updateRect.
x + updateRect.
w);
3327 if(updateRect.
y + updateRect.
h > image->
base_h)
3328 updateRect.
h += image->
base_h - (updateRect.
y + updateRect.
h);
3330 if(updateRect.
w <= 0)
3332 if(updateRect.
h <= 0)
3341 if(updateRect.
w < 0.0f || updateRect.
h < 0.0f)
3348 if(surface_rect != NULL)
3350 sourceRect = *surface_rect;
3351 if(sourceRect.
x < 0)
3353 sourceRect.
w += sourceRect.
x;
3356 if(sourceRect.
y < 0)
3358 sourceRect.
h += sourceRect.
y;
3361 if(sourceRect.
x + sourceRect.
w > newSurface->w)
3362 sourceRect.
w += newSurface->w - (sourceRect.
x + sourceRect.
w);
3363 if(sourceRect.
y + sourceRect.
h > newSurface->h)
3364 sourceRect.
h += newSurface->h - (sourceRect.
y + sourceRect.
h);
3366 if(sourceRect.
w <= 0)
3368 if(sourceRect.
h <= 0)
3375 sourceRect.
w = newSurface->w;
3376 sourceRect.
h = newSurface->h;
3380 changeTexturing(renderer, 1);
3382 renderer->
impl->FlushBlitBuffer(renderer);
3383 bindTexture(renderer, image);
3385 while(newSurface->pitch % alignment)
3389 if(sourceRect.
w < updateRect.
w)
3390 updateRect.
w = sourceRect.
w;
3391 if(sourceRect.
h < updateRect.
h)
3392 updateRect.
h = sourceRect.
h;
3394 pixels = (Uint8*)newSurface->pixels;
3396 pixels += (
int)(newSurface->pitch * sourceRect.
y + (newSurface->format->BytesPerPixel)*sourceRect.
x);
3398 upload_texture(pixels, updateRect, original_format, alignment, newSurface->pitch/newSurface->format->BytesPerPixel, newSurface->pitch, newSurface->format->BytesPerPixel);
3401 if(surface != newSurface)
3402 SDL_FreeSurface(newSurface);
3407 static void UpdateImageBytes(
GPU_Renderer* renderer,
GPU_Image* image,
const GPU_Rect* image_rect,
const unsigned char* bytes,
int bytes_per_row)
3410 GLenum original_format;
3415 if(image == NULL || bytes == NULL)
3419 original_format = data->format;
3421 if(image_rect != NULL)
3423 updateRect = *image_rect;
3424 if(updateRect.
x < 0)
3426 updateRect.
w += updateRect.
x;
3429 if(updateRect.
y < 0)
3431 updateRect.
h += updateRect.
y;
3434 if(updateRect.
x + updateRect.
w > image->
base_w)
3435 updateRect.
w += image->
base_w - (updateRect.
x + updateRect.
w);
3436 if(updateRect.
y + updateRect.
h > image->
base_h)
3437 updateRect.
h += image->
base_h - (updateRect.
y + updateRect.
h);
3439 if(updateRect.
w <= 0)
3441 if(updateRect.
h <= 0)
3450 if(updateRect.
w < 0.0f || updateRect.
h < 0.0f)
3458 changeTexturing(renderer, 1);
3460 renderer->
impl->FlushBlitBuffer(renderer);
3461 bindTexture(renderer, image);
3463 while(bytes_per_row % alignment)
3475 SDL_Surface* newSurface;
3476 GLenum internal_format;
3494 internal_format = data->format;
3496 newSurface = copySurfaceIfNeeded(renderer, internal_format, surface, &internal_format);
3497 if(newSurface == NULL)
3509 if(tdata->handle != 0)
3510 glDeleteFramebuffersPROC(1, &tdata->handle);
3515 if(data->owns_handle)
3516 glDeleteTextures( 1, &data->handle);
3520 if(surface_rect == NULL)
3524 sourceRect.
w = surface->w;
3525 sourceRect.
h = surface->h;
3528 sourceRect = *surface_rect;
3531 if(sourceRect.
x < 0)
3533 sourceRect.
w += sourceRect.
x;
3536 if(sourceRect.
y < 0)
3538 sourceRect.
h += sourceRect.
y;
3541 if(sourceRect.
x >= surface->w)
3542 sourceRect.
x = surface->w - 1;
3543 if(sourceRect.
y >= surface->h)
3544 sourceRect.
y = surface->h - 1;
3546 if(sourceRect.
x + sourceRect.
w > surface->w)
3547 sourceRect.
w = surface->w - sourceRect.
x;
3548 if(sourceRect.
y + sourceRect.
h > surface->h)
3549 sourceRect.
h = surface->h - sourceRect.
y;
3551 if(sourceRect.
w <= 0 || sourceRect.
h <= 0)
3558 data->handle = CreateUninitializedTexture(renderer);
3559 data->owns_handle = 1;
3560 if(data->handle == 0)
3593 while(newSurface->pitch % alignment)
3596 pixels = (Uint8*)newSurface->pixels;
3598 pixels += (
int)(newSurface->pitch * sourceRect.
y + (newSurface->format->BytesPerPixel)*sourceRect.
x);
3600 upload_new_texture(pixels,
GPU_MakeRect(0, 0, w, h), internal_format, alignment, (newSurface->pitch / newSurface->format->BytesPerPixel), newSurface->format->BytesPerPixel);
3604 if(surface != newSurface)
3605 SDL_FreeSurface(newSurface);
3618 glGenFramebuffersPROC(1, &tdata->handle);
3619 if(tdata->handle == 0)
3658 if(x < 0 || x >= Surface->w)
3661 bpp = Surface->format->BytesPerPixel;
3662 bits = ((Uint8*)Surface->pixels) + y*Surface->pitch + x*bpp;
3667 return *((Uint8*)Surface->pixels + y * Surface->pitch + x);
3670 return *((Uint16*)Surface->pixels + y * Surface->pitch/2 + x);
3676 r = *((bits)+Surface->format->Rshift/8);
3677 g = *((bits)+Surface->format->Gshift/8);
3678 b = *((bits)+Surface->format->Bshift/8);
3679 return SDL_MapRGB(Surface->format, r, g, b);
3683 return *((Uint32*)Surface->pixels + y * Surface->pitch/4 + x);
3701 if(surface->w == 0 || surface->h == 0)
3708 if(surface->format->Amask == 0)
3721 image = renderer->
impl->
CreateImage(renderer, surface->w, surface->h, format);
3725 renderer->
impl->UpdateImage(renderer, image, NULL, surface, NULL);
3738 if(target->
image != NULL)
3740 result = gpu_copy_image_pixels_only(renderer, target->
image);
3746 SDL_FreeSurface(surface);
3767 if(image->
target != NULL)
3774 renderer->
impl->FreeTarget(renderer, target);
3781 if(data->refcount > 1)
3787 if(data->owns_handle)
3790 glDeleteTextures( 1, &data->handle);
3810 if(image->
target != NULL)
3817 glGenFramebuffersPROC(1, &handle);
3835 result->
data = data;
3836 data->handle = handle;
3842 result->
image = image;
3843 result->
w = image->
w;
3844 result->
h = image->
h;
3872 if(data->refcount > 1)
3882 glDeleteFramebuffersPROC(1, &data->handle);
3904 SDL_free(cdata->blit_buffer);
3905 SDL_free(cdata->index_buffer);
3909 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 3913 #if !defined(SDL_GPU_NO_VAO) 3914 glDeleteVertexArrays(1, &cdata->blit_VAO);
3919 #ifdef SDL_GPU_USE_SDL2 3921 SDL_GL_DeleteContext(context->
context);
3943 renderer->
impl->FlushBlitBuffer(renderer);
3975 #define SET_TEXTURED_VERTEX(x, y, s, t, r, g, b, a) \ 3976 blit_buffer[vert_index] = x; \ 3977 blit_buffer[vert_index+1] = y; \ 3978 blit_buffer[tex_index] = s; \ 3979 blit_buffer[tex_index+1] = t; \ 3980 blit_buffer[color_index] = r; \ 3981 blit_buffer[color_index+1] = g; \ 3982 blit_buffer[color_index+2] = b; \ 3983 blit_buffer[color_index+3] = a; \ 3984 index_buffer[cdata->index_buffer_num_vertices++] = cdata->blit_buffer_num_vertices++; \ 3985 vert_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 3986 tex_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 3987 color_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 3989 #define SET_TEXTURED_VERTEX_UNINDEXED(x, y, s, t, r, g, b, a) \ 3990 blit_buffer[vert_index] = x; \ 3991 blit_buffer[vert_index+1] = y; \ 3992 blit_buffer[tex_index] = s; \ 3993 blit_buffer[tex_index+1] = t; \ 3994 blit_buffer[color_index] = r; \ 3995 blit_buffer[color_index+1] = g; \ 3996 blit_buffer[color_index+2] = b; \ 3997 blit_buffer[color_index+3] = a; \ 3998 vert_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 3999 tex_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 4000 color_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4002 #define SET_UNTEXTURED_VERTEX(x, y, r, g, b, a) \ 4003 blit_buffer[vert_index] = x; \ 4004 blit_buffer[vert_index+1] = y; \ 4005 blit_buffer[color_index] = r; \ 4006 blit_buffer[color_index+1] = g; \ 4007 blit_buffer[color_index+2] = b; \ 4008 blit_buffer[color_index+3] = a; \ 4009 index_buffer[cdata->index_buffer_num_vertices++] = cdata->blit_buffer_num_vertices++; \ 4010 vert_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 4011 color_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4013 #define SET_UNTEXTURED_VERTEX_UNINDEXED(x, y, r, g, b, a) \ 4014 blit_buffer[vert_index] = x; \ 4015 blit_buffer[vert_index+1] = y; \ 4016 blit_buffer[color_index] = r; \ 4017 blit_buffer[color_index+1] = g; \ 4018 blit_buffer[color_index+2] = b; \ 4019 blit_buffer[color_index+3] = a; \ 4020 vert_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; \ 4021 color_index += GPU_BLIT_BUFFER_FLOATS_PER_VERTEX; 4023 #define SET_INDEXED_VERTEX(offset) \ 4024 index_buffer[cdata->index_buffer_num_vertices++] = blit_buffer_starting_index + (offset); 4026 #define SET_RELATIVE_INDEXED_VERTEX(offset) \ 4027 index_buffer[cdata->index_buffer_num_vertices++] = cdata->blit_buffer_num_vertices + (offset); 4031 #define BEGIN_UNTEXTURED_SEGMENTS(x1, y1, x2, y2, r, g, b, a) \ 4032 SET_UNTEXTURED_VERTEX(x1, y1, r, g, b, a); \ 4033 SET_UNTEXTURED_VERTEX(x2, y2, r, g, b, a); 4036 #define SET_UNTEXTURED_SEGMENTS(x1, y1, x2, y2, r, g, b, a) \ 4037 SET_UNTEXTURED_VERTEX(x1, y1, r, g, b, a); \ 4038 SET_RELATIVE_INDEXED_VERTEX(-2); \ 4039 SET_UNTEXTURED_VERTEX(x2, y2, r, g, b, a); \ 4040 SET_RELATIVE_INDEXED_VERTEX(-2); \ 4041 SET_RELATIVE_INDEXED_VERTEX(-2); \ 4042 SET_RELATIVE_INDEXED_VERTEX(-1); 4045 #define LOOP_UNTEXTURED_SEGMENTS() \ 4046 SET_INDEXED_VERTEX(0); \ 4047 SET_RELATIVE_INDEXED_VERTEX(-1); \ 4048 SET_INDEXED_VERTEX(1); \ 4049 SET_INDEXED_VERTEX(0); 4051 #define END_UNTEXTURED_SEGMENTS(x1, y1, x2, y2, r, g, b, a) \ 4052 SET_UNTEXTURED_VERTEX(x1, y1, r, g, b, a); \ 4053 SET_RELATIVE_INDEXED_VERTEX(-2); \ 4054 SET_UNTEXTURED_VERTEX(x2, y2, r, g, b, a); \ 4055 SET_RELATIVE_INDEXED_VERTEX(-2); 4061 Uint32 tex_w, tex_h;
4064 float x1, y1, x2, y2;
4065 float dx1, dy1, dx2, dy2;
4068 unsigned short* index_buffer;
4069 unsigned short blit_buffer_starting_index;
4091 makeContextCurrent(renderer, target);
4098 prepareToRenderToTarget(renderer, target);
4099 prepareToRenderImage(renderer, target, image);
4102 bindTexture(renderer, image);
4105 if(!bindFramebuffer(renderer, target))
4121 if(src_rect == NULL)
4126 x2 = ((float)image->
w)/tex_w;
4127 y2 = ((float)image->
h)/tex_h;
4134 x1 = src_rect->
x/(float)tex_w;
4135 y1 = src_rect->
y/(float)tex_h;
4136 x2 = (src_rect->
x + src_rect->
w)/(
float)tex_w;
4137 y2 = (src_rect->
y + src_rect->
h)/(
float)tex_h;
4145 x1 *= image->
base_w/(float)image->
w;
4146 y1 *= image->
base_h/(
float)image->
h;
4147 x2 *= image->
base_w/(float)image->
w;
4148 y2 *= image->
base_h/(
float)image->
h;
4154 dx2 = x + w * (1.0f - image->
anchor_x);
4155 dy2 = y + h * (1.0f - image->
anchor_y);
4160 fractional = w/2.0f - floorf(w/2.0f);
4163 fractional = h/2.0f - floorf(h/2.0f);
4177 if(cdata->blit_buffer_num_vertices + 4 >= cdata->blit_buffer_max_num_vertices)
4179 if(!growBlitBuffer(cdata, cdata->blit_buffer_num_vertices + 4))
4180 renderer->
impl->FlushBlitBuffer(renderer);
4182 if(cdata->index_buffer_num_vertices + 6 >= cdata->index_buffer_max_num_vertices)
4184 if(!growIndexBuffer(cdata, cdata->index_buffer_num_vertices + 6))
4185 renderer->
impl->FlushBlitBuffer(renderer);
4188 blit_buffer = cdata->blit_buffer;
4189 index_buffer = cdata->index_buffer;
4191 blit_buffer_starting_index = cdata->blit_buffer_num_vertices;
4205 r = image->
color.r/255.0f;
4206 g = image->
color.g/255.0f;
4207 b = image->
color.b/255.0f;
4244 w = (src_rect == NULL? image->
w : src_rect->
w);
4245 h = (src_rect == NULL? image->
h : src_rect->
h);
4246 renderer->
impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->
anchor_x, h*image->
anchor_y, degrees, 1.0f, 1.0f);
4263 w = (src_rect == NULL? image->
w : src_rect->
w);
4264 h = (src_rect == NULL? image->
h : src_rect->
h);
4265 renderer->
impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->
anchor_x, h*image->
anchor_y, 0.0f, scaleX, scaleY);
4282 w = (src_rect == NULL? image->
w : src_rect->
w);
4283 h = (src_rect == NULL? image->
h : src_rect->
h);
4284 renderer->
impl->BlitTransformX(renderer, image, src_rect, target, x, y, w*image->
anchor_x, h*image->
anchor_y, degrees, scaleX, scaleY);
4287 static void BlitTransformX(
GPU_Renderer* renderer,
GPU_Image* image,
GPU_Rect* src_rect,
GPU_Target* target,
float x,
float y,
float pivot_x,
float pivot_y,
float degrees,
float scaleX,
float scaleY)
4289 Uint32 tex_w, tex_h;
4290 float x1, y1, x2, y2;
4291 float dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4;
4295 unsigned short* index_buffer;
4296 unsigned short blit_buffer_starting_index;
4319 makeContextCurrent(renderer, target);
4321 prepareToRenderToTarget(renderer, target);
4322 prepareToRenderImage(renderer, target, image);
4325 bindTexture(renderer, image);
4328 if(!bindFramebuffer(renderer, target))
4350 if(src_rect == NULL)
4355 x2 = ((float)image->
w)/tex_w;
4356 y2 = ((float)image->
h)/tex_h;
4363 x1 = src_rect->
x/(float)tex_w;
4364 y1 = src_rect->
y/(float)tex_h;
4365 x2 = (src_rect->
x + src_rect->
w)/(
float)tex_w;
4366 y2 = (src_rect->
y + src_rect->
h)/(
float)tex_h;
4374 x1 *= image->
base_w/(float)image->
w;
4375 y1 *= image->
base_h/(
float)image->
h;
4376 x2 *= image->
base_w/(float)image->
w;
4377 y2 *= image->
base_h/(
float)image->
h;
4390 fractional = w/2.0f - floorf(w/2.0f);
4393 fractional = h/2.0f - floorf(h/2.0f);
4408 if(scaleX != 1.0f || scaleY != 1.0f)
4425 float cosA = cos(degrees*
M_PI/180);
4426 float sinA = sin(degrees*
M_PI/180);
4428 dx1 = dx1*cosA - dy1*sinA;
4429 dy1 = tempX*sinA + dy1*cosA;
4431 dx2 = dx2*cosA - dy2*sinA;
4432 dy2 = tempX*sinA + dy2*cosA;
4434 dx3 = dx3*cosA - dy3*sinA;
4435 dy3 = tempX*sinA + dy3*cosA;
4437 dx4 = dx4*cosA - dy4*sinA;
4438 dy4 = tempX*sinA + dy4*cosA;
4453 if(cdata->blit_buffer_num_vertices + 4 >= cdata->blit_buffer_max_num_vertices)
4455 if(!growBlitBuffer(cdata, cdata->blit_buffer_num_vertices + 4))
4456 renderer->
impl->FlushBlitBuffer(renderer);
4458 if(cdata->index_buffer_num_vertices + 6 >= cdata->index_buffer_max_num_vertices)
4460 if(!growIndexBuffer(cdata, cdata->index_buffer_num_vertices + 6))
4461 renderer->
impl->FlushBlitBuffer(renderer);
4464 blit_buffer = cdata->blit_buffer;
4465 index_buffer = cdata->index_buffer;
4467 blit_buffer_starting_index = cdata->blit_buffer_num_vertices;
4482 r = image->
color.r/255.0f;
4483 g = image->
color.g/255.0f;
4484 b = image->
color.b/255.0f;
4508 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4513 if(type == GPU_TYPE_DOUBLE)
return sizeof(double);
4514 if(type == GPU_TYPE_FLOAT)
return sizeof(float);
4515 if(type == GPU_TYPE_INT)
return sizeof(int);
4516 if(type == GPU_TYPE_UNSIGNED_INT)
return sizeof(
unsigned int);
4517 if(type == GPU_TYPE_SHORT)
return sizeof(short);
4518 if(type == GPU_TYPE_UNSIGNED_SHORT)
return sizeof(
unsigned short);
4519 if(type == GPU_TYPE_BYTE)
return sizeof(char);
4520 if(type == GPU_TYPE_UNSIGNED_BYTE)
return sizeof(
unsigned char);
4527 for(i = 0; i < 16; i++)
4539 memcpy(storage_ptr, values_ptr, value_size_bytes);
4541 memcpy(storage_ptr, values_ptr, value_size_bytes);
4543 memcpy(storage_ptr, values_ptr, value_size_bytes);
4545 memcpy(storage_ptr, values_ptr, value_size_bytes);
4554 static void upload_attribute_data(
GPU_CONTEXT_DATA* cdata,
int num_vertices)
4557 for(i = 0; i < 16; i++)
4562 int num_values_used = num_vertices;
4590 for(i = 0; i < 16; i++)
4603 static int get_lowest_attribute_num_values(
GPU_CONTEXT_DATA* cdata,
int cap)
4607 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4609 for(i = 0; i < 16; i++)
4625 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4626 #if defined(SDL_GPU_USE_BUFFER_RESET) 4629 glBufferData(GL_ELEMENT_ARRAY_BUFFER, bytes_indices, indices, GL_DYNAMIC_DRAW);
4630 #elif defined(SDL_GPU_USE_BUFFER_MAPPING) 4633 unsigned short* data_i = (indices == NULL? NULL : (
unsigned short*)
glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,
GL_WRITE_ONLY));
4636 memcpy(data, values, bytes);
4641 memcpy(data_i, indices, bytes_indices);
4644 #elif defined(SDL_GPU_USE_BUFFER_UPDATE) 4649 #error "SDL_gpu's VBO upload needs to choose SDL_GPU_USE_BUFFER_RESET, SDL_GPU_USE_BUFFER_MAPPING, or SDL_GPU_USE_BUFFER_UPDATE and none is defined!" 4655 static void SetAttributefv(
GPU_Renderer* renderer,
int location,
int num_elements,
float* value);
4662 intptr_t stride, offset_texcoords, offset_colors;
4663 int size_vertices, size_texcoords, size_colors;
4665 GPU_bool using_texture = (image != NULL);
4666 GPU_bool use_vertices = (flags & (GPU_BATCH_XY | GPU_BATCH_XYZ));
4667 GPU_bool use_texcoords = (flags & GPU_BATCH_ST);
4668 GPU_bool use_colors = (flags & (GPU_BATCH_RGB | GPU_BATCH_RGBA | GPU_BATCH_RGB8 | GPU_BATCH_RGBA8));
4669 GPU_bool use_byte_colors = (flags & (GPU_BATCH_RGB8 | GPU_BATCH_RGBA8));
4670 GPU_bool use_z = (flags & GPU_BATCH_XYZ);
4671 GPU_bool use_a = (flags & (GPU_BATCH_RGBA | GPU_BATCH_RGBA8));
4673 if(num_vertices == 0)
4681 if((image != NULL && renderer != image->
renderer) || renderer != target->
renderer)
4687 makeContextCurrent(renderer, target);
4691 bindTexture(renderer, image);
4694 if(!bindFramebuffer(renderer, target))
4700 prepareToRenderToTarget(renderer, target);
4702 prepareToRenderImage(renderer, target, image);
4704 prepareToRenderShapes(renderer, GL_TRIANGLES);
4705 changeViewport(target);
4706 changeCamera(target);
4709 changeTexturing(renderer,
GPU_TRUE);
4711 setClipRect(renderer, target);
4713 #ifdef SDL_GPU_APPLY_TRANSFORMS_TO_GL_STACK 4714 if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER))
4722 renderer->
impl->FlushBlitBuffer(renderer);
4724 if(cdata->index_buffer_num_vertices + num_indices >= cdata->index_buffer_max_num_vertices)
4726 growBlitBuffer(cdata, cdata->index_buffer_num_vertices + num_indices);
4728 if(cdata->blit_buffer_num_vertices + num_vertices >= cdata->blit_buffer_max_num_vertices)
4730 growBlitBuffer(cdata, cdata->blit_buffer_num_vertices + num_vertices);
4734 if(cdata->blit_buffer_num_vertices + num_vertices >= cdata->blit_buffer_max_num_vertices)
4736 if(!growBlitBuffer(cdata, cdata->blit_buffer_num_vertices + num_vertices))
4739 num_vertices = (cdata->blit_buffer_max_num_vertices - cdata->blit_buffer_num_vertices);
4742 if(cdata->index_buffer_num_vertices + num_indices >= cdata->index_buffer_max_num_vertices)
4744 if(!growIndexBuffer(cdata, cdata->index_buffer_num_vertices + num_indices))
4747 num_indices = (cdata->index_buffer_max_num_vertices - cdata->index_buffer_num_vertices);
4751 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4752 refresh_attribute_data(cdata);
4756 num_indices = num_vertices;
4759 (void)offset_texcoords;
4760 (void)offset_colors;
4761 (void)size_vertices;
4762 (void)size_texcoords;
4766 offset_texcoords = offset_colors = 0;
4767 size_vertices = size_texcoords = size_colors = 0;
4777 stride += size_vertices;
4779 offset_texcoords = stride;
4780 offset_colors = stride;
4787 stride += size_texcoords;
4789 offset_colors = stride;
4801 if(use_colors && !use_byte_colors)
4803 stride += size_colors;
4807 stride *=
sizeof(float);
4808 offset_texcoords *=
sizeof(float);
4809 offset_colors *=
sizeof(float);
4812 if(use_colors && use_byte_colors)
4814 stride += size_colors;
4817 #ifdef SDL_GPU_USE_ARRAY_PIPELINE 4822 glEnableClientState(GL_VERTEX_ARRAY);
4824 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
4826 glEnableClientState(GL_COLOR_ARRAY);
4830 glVertexPointer(size_vertices, GL_FLOAT, stride, values);
4832 glTexCoordPointer(size_texcoords, GL_FLOAT, stride, values + offset_texcoords);
4836 glColorPointer(size_colors, GL_UNSIGNED_BYTE, stride, values + offset_colors);
4838 glColorPointer(size_colors, GL_FLOAT, stride, values + offset_colors);
4843 glDrawArrays(GL_TRIANGLES, 0, num_indices);
4845 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, indices);
4849 glDisableClientState(GL_COLOR_ARRAY);
4851 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
4853 glDisableClientState(GL_VERTEX_ARRAY);
4859 #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 4860 if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER))
4862 #ifdef SDL_GPU_USE_FIXED_FUNCTION_PIPELINE 4868 float* vertex_pointer = (
float*)(values);
4869 float* texcoord_pointer = (
float*)((
char*)values + offset_texcoords);
4871 glBegin(GL_TRIANGLES);
4872 for(i = 0; i < num_indices; i++)
4882 Uint8* color_pointer = (Uint8*)((
char*)values + offset_colors);
4883 glColor4ub(color_pointer[index], color_pointer[index+1], color_pointer[index+2], (use_a? color_pointer[index+3] : 255));
4887 float* color_pointer = (
float*)((
char*)values + offset_colors);
4888 glColor4f(color_pointer[index], color_pointer[index+1], color_pointer[index+2], (use_a? color_pointer[index+3] : 1.0f));
4892 glTexCoord2f( texcoord_pointer[index], texcoord_pointer[index+1] );
4894 glVertex3f( vertex_pointer[index], vertex_pointer[index+1], (use_z? vertex_pointer[index+2] : 0.0f) );
4900 #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 4906 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 4917 #if !defined(SDL_GPU_NO_VAO) 4918 glBindVertexArray(cdata->blit_VAO);
4925 float cam_matrix[16];
4927 get_camera_matrix(cam_matrix, cdata->last_camera);
4938 cdata->blit_VBO_flop = !cdata->blit_VBO_flop;
4939 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cdata->blit_IBO);
4942 submit_buffer_data(stride * num_vertices, values,
sizeof(
unsigned short)*num_indices, indices);
4969 SDL_Color color = get_complete_mod_color(renderer, target, image);
4970 float default_color[4] = {color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f};
4975 upload_attribute_data(cdata, num_indices);
4978 glDrawArrays(GL_TRIANGLES, 0, num_indices);
4980 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, (
void*)0);
4990 disable_attribute_data(cdata);
4992 #if !defined(SDL_GPU_NO_VAO) 4993 glBindVertexArray(0);
4999 cdata->blit_buffer_num_vertices = 0;
5000 cdata->index_buffer_num_vertices = 0;
5002 unsetClipRect(renderer, target);
5007 #ifndef __IPHONEOS__ 5013 renderer->
impl->FlushBlitBuffer(renderer);
5014 bindTexture(renderer, image);
5015 glGenerateMipmapPROC(GL_TEXTURE_2D);
5018 glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &filter);
5019 if(filter == GL_LINEAR)
5020 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
5037 renderer->
impl->FlushBlitBuffer(renderer);
5056 renderer->
impl->FlushBlitBuffer(renderer);
5064 static void swizzle_for_format(SDL_Color* color, GLenum format,
unsigned char pixel[4])
5069 color->b = color->g = color->r = pixel[0];
5072 case GL_LUMINANCE_ALPHA:
5073 color->b = color->g = color->r = pixel[0];
5078 color->b = pixel[0];
5079 color->g = pixel[1];
5080 color->r = pixel[2];
5086 color->b = pixel[0];
5087 color->g = pixel[1];
5088 color->r = pixel[2];
5095 color->b = pixel[1];
5096 color->g = pixel[2];
5097 color->r = pixel[3];
5102 #ifndef SDL_GPU_USE_GLES 5104 color->r = pixel[0];
5105 color->g = pixel[1];
5111 color->r = pixel[0];
5112 color->g = pixel[1];
5113 color->b = pixel[2];
5117 color->r = pixel[0];
5118 color->g = pixel[1];
5119 color->b = pixel[2];
5129 SDL_Color result = {0,0,0,0};
5134 if(x < 0 || y < 0 || x >= target->
w || y >= target->
h)
5138 renderer->
impl->FlushBlitBuffer(renderer);
5139 if(bindFramebuffer(renderer, target))
5141 unsigned char pixels[4];
5143 glReadPixels(x, y, 1, 1, format, GL_UNSIGNED_BYTE, pixels);
5145 swizzle_for_format(&result, format, pixels);
5153 GLenum minFilter, magFilter;
5169 minFilter = GL_NEAREST;
5170 magFilter = GL_NEAREST;
5174 minFilter = GL_LINEAR_MIPMAP_NEAREST;
5176 minFilter = GL_LINEAR;
5178 magFilter = GL_LINEAR;
5182 minFilter = GL_LINEAR_MIPMAP_LINEAR;
5184 minFilter = GL_LINEAR;
5186 magFilter = GL_LINEAR;
5194 bindTexture(renderer, image);
5198 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
5199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
5204 GLenum wrap_x, wrap_y;
5220 wrap_x = GL_CLAMP_TO_EDGE;
5242 wrap_y = GL_CLAMP_TO_EDGE;
5262 bindTexture(renderer, image);
5267 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_x );
5268 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_y );
5282 makeContextCurrent(renderer, target);
5285 renderer->
impl->FlushBlitBuffer(renderer);
5286 if(bindFramebuffer(renderer, target))
5288 setClipRect(renderer, target);
5290 glClearColor(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
5291 glClear(GL_COLOR_BUFFER_BIT);
5293 unsetClipRect(renderer, target);
5297 static void DoPartialFlush(
GPU_Renderer* renderer,
GPU_Target* dest,
GPU_Context* context,
unsigned short num_vertices,
float* blit_buffer,
unsigned int num_indices,
unsigned short* index_buffer)
5301 #ifdef SDL_GPU_USE_ARRAY_PIPELINE 5302 glEnableClientState(GL_VERTEX_ARRAY);
5303 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
5304 glEnableClientState(GL_COLOR_ARRAY);
5310 glDrawElements(cdata->last_shape, num_indices, GL_UNSIGNED_SHORT, index_buffer);
5312 glDisableClientState(GL_COLOR_ARRAY);
5313 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
5314 glDisableClientState(GL_VERTEX_ARRAY);
5320 #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 5321 if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER))
5323 #ifdef SDL_GPU_USE_FIXED_FUNCTION_PIPELINE 5331 glBegin(cdata->last_shape);
5332 for(i = 0; i < num_indices; i++)
5335 glColor4f( color_pointer[index], color_pointer[index+1], color_pointer[index+2], color_pointer[index+3] );
5336 glTexCoord2f( texcoord_pointer[index], texcoord_pointer[index+1] );
5337 glVertex3f( vertex_pointer[index], vertex_pointer[index+1], 0.0f );
5348 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 5351 #if !defined(SDL_GPU_NO_VAO) 5352 glBindVertexArray(cdata->blit_VAO);
5367 float cam_matrix[16];
5368 get_camera_matrix(cam_matrix, cdata->last_camera);
5382 cdata->blit_VBO_flop = !cdata->blit_VBO_flop;
5383 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cdata->blit_IBO);
5405 upload_attribute_data(cdata, num_vertices);
5407 glDrawElements(cdata->last_shape, num_indices, GL_UNSIGNED_SHORT, (
void*)0);
5417 disable_attribute_data(cdata);
5419 #if !defined(SDL_GPU_NO_VAO) 5420 glBindVertexArray(0);
5426 static void DoUntexturedFlush(
GPU_Renderer* renderer,
GPU_Target* dest,
GPU_Context* context,
unsigned short num_vertices,
float* blit_buffer,
unsigned int num_indices,
unsigned short* index_buffer)
5431 #ifdef SDL_GPU_USE_ARRAY_PIPELINE 5432 glEnableClientState(GL_VERTEX_ARRAY);
5433 glEnableClientState(GL_COLOR_ARRAY);
5438 glDrawElements(cdata->last_shape, num_indices, GL_UNSIGNED_SHORT, index_buffer);
5440 glDisableClientState(GL_COLOR_ARRAY);
5441 glDisableClientState(GL_VERTEX_ARRAY);
5445 #ifdef SDL_GPU_USE_BUFFER_PIPELINE_FALLBACK 5446 if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER))
5448 #ifdef SDL_GPU_USE_FIXED_FUNCTION_PIPELINE 5455 glBegin(cdata->last_shape);
5456 for(i = 0; i < num_indices; i++)
5459 glColor4f( color_pointer[index], color_pointer[index+1], color_pointer[index+2], color_pointer[index+3] );
5460 glVertex3f( vertex_pointer[index], vertex_pointer[index+1], 0.0f );
5468 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 5471 #if !defined(SDL_GPU_NO_VAO) 5472 glBindVertexArray(cdata->blit_VAO);
5487 float cam_matrix[16];
5488 get_camera_matrix(cam_matrix, cdata->last_camera);
5502 cdata->blit_VBO_flop = !cdata->blit_VBO_flop;
5503 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cdata->blit_IBO);
5520 upload_attribute_data(cdata, num_vertices);
5522 glDrawElements(cdata->last_shape, num_indices, GL_UNSIGNED_SHORT, (
void*)0);
5530 disable_attribute_data(cdata);
5532 #if !defined(SDL_GPU_NO_VAO) 5533 glBindVertexArray(0);
5539 #define MAX(a, b) ((a) > (b)? (a) : (b)) 5550 if(cdata->blit_buffer_num_vertices > 0 && cdata->last_target != NULL)
5556 unsigned short* index_buffer;
5558 changeViewport(dest);
5561 applyTexturing(renderer);
5563 #ifdef SDL_GPU_APPLY_TRANSFORMS_TO_GL_STACK 5564 if(!IsFeatureEnabled(renderer, GPU_FEATURE_VERTEX_SHADER))
5568 setClipRect(renderer, dest);
5570 #ifdef SDL_GPU_USE_BUFFER_PIPELINE 5571 refresh_attribute_data(cdata);
5574 blit_buffer = cdata->blit_buffer;
5575 index_buffer = cdata->index_buffer;
5577 if(cdata->last_use_texturing)
5579 while(cdata->blit_buffer_num_vertices > 0)
5581 num_vertices =
MAX(cdata->blit_buffer_num_vertices, get_lowest_attribute_num_values(cdata, cdata->blit_buffer_num_vertices));
5582 num_indices = num_vertices * 3 / 2;
5584 DoPartialFlush(renderer, dest, context, num_vertices, blit_buffer, num_indices, index_buffer);
5586 cdata->blit_buffer_num_vertices -= num_vertices;
5589 index_buffer += num_indices;
5594 DoUntexturedFlush(renderer, dest, context, cdata->blit_buffer_num_vertices, blit_buffer, cdata->index_buffer_num_vertices, index_buffer);
5597 cdata->blit_buffer_num_vertices = 0;
5598 cdata->index_buffer_num_vertices = 0;
5600 unsetClipRect(renderer, dest);
5606 renderer->
impl->FlushBlitBuffer(renderer);
5608 if(target != NULL && target->
context != NULL)
5610 makeContextCurrent(renderer, target);
5612 #ifdef SDL_GPU_USE_SDL2 5615 SDL_GL_SwapBuffers();
5619 #ifdef SDL_GPU_USE_OPENGL 5621 apply_Intel_attrib_workaround =
GPU_TRUE;
5636 static Uint32 GetShaderSourceSize(
const char* filename);
5637 static Uint32 GetShaderSource(
const char* filename,
char* result);
5639 static void read_until_end_of_comment(SDL_RWops* rwops,
char multiline)
5642 while(SDL_RWread(rwops, &buffer, 1, 1) > 0)
5654 if(SDL_RWread(rwops, &buffer, 1, 1) <= 0 || buffer ==
'/')
5661 static Uint32 GetShaderSourceSize_RW(SDL_RWops* shader_source)
5668 if(shader_source == NULL)
5676 while((len = SDL_RWread(shader_source, &buffer, 1, 1)) > 0)
5679 if(buffer[0] ==
'#')
5683 unsigned long line_len;
5685 while((line_len = SDL_RWread(shader_source, buffer+line_size, 1, 1)) > 0)
5687 line_size += line_len;
5688 if(buffer[line_size - line_len] ==
'\n')
5691 buffer[line_size] =
'\0';
5694 token = strtok(buffer,
"# \t");
5696 if(token != NULL && strcmp(token,
"include") == 0)
5699 token = strtok(NULL,
"\"");
5703 size += GetShaderSourceSize(token) + 1;
5714 if(last_char ==
'/')
5716 if(buffer[0] ==
'/')
5718 read_until_end_of_comment(shader_source, 0);
5721 else if(buffer[0] ==
'*')
5723 read_until_end_of_comment(shader_source, 1);
5729 last_char = buffer[0];
5733 SDL_RWseek(shader_source, 0, SEEK_SET);
5738 static Uint32 GetShaderSource_RW(SDL_RWops* shader_source,
char* result)
5745 if(shader_source == NULL)
5756 while((len = SDL_RWread(shader_source, &buffer, 1, 1)) > 0)
5759 if(buffer[0] ==
'#')
5763 unsigned long line_len;
5764 char token_buffer[512];
5766 while((line_len = SDL_RWread(shader_source, buffer+line_size, 1, 1)) > 0)
5768 line_size += line_len;
5769 if(buffer[line_size - line_len] ==
'\n')
5774 memcpy(token_buffer, buffer, line_size+1);
5775 token_buffer[line_size] =
'\0';
5776 token = strtok(token_buffer,
"# \t");
5778 if(token != NULL && strcmp(token,
"include") == 0)
5781 token = strtok(NULL,
"\"");
5785 size += GetShaderSource(token, result + size);
5786 result[size] =
'\n';
5792 memcpy(result + size, buffer, line_size);
5799 memcpy(result + size, buffer, len);
5802 if(last_char ==
'/')
5804 if(buffer[0] ==
'/')
5806 read_until_end_of_comment(shader_source, 0);
5807 memcpy(result + size,
"\n", 1);
5810 else if(buffer[0] ==
'*')
5812 read_until_end_of_comment(shader_source, 1);
5813 memcpy(result + size,
"*/", 2);
5819 last_char = buffer[0];
5821 result[size] =
'\0';
5824 SDL_RWseek(shader_source, 0, SEEK_SET);
5828 static Uint32 GetShaderSource(
const char* filename,
char* result)
5833 if(filename == NULL)
5835 rwops = SDL_RWFromFile(filename,
"r");
5837 size = GetShaderSource_RW(rwops, result);
5843 static Uint32 GetShaderSourceSize(
const char* filename)
5848 if(filename == NULL)
5850 rwops = SDL_RWFromFile(filename,
"r");
5852 result = GetShaderSourceSize_RW(rwops);
5859 static Uint32 compile_shader_source(
GPU_ShaderEnum shader_type,
const char* shader_source)
5862 GLuint shader_object = 0;
5864 (void)shader_source;
5866 #ifndef SDL_GPU_DISABLE_SHADERS 5878 #ifdef GL_GEOMETRY_SHADER 5882 snprintf(shader_message, 256,
"Failed to create geometry shader object.\n");
5888 if(shader_object == 0)
5891 snprintf(shader_message, 256,
"Failed to create new shader object.\n");
5912 return shader_object;
5919 Uint32 size = GetShaderSourceSize_RW(shader_source);
5920 char* source_string = (
char*)SDL_malloc(size+1);
5921 int result = GetShaderSource_RW(shader_source, source_string);
5926 SDL_RWclose(shader_source);
5931 snprintf(shader_message, 256,
"Failed to read shader source.\n");
5932 SDL_free(source_string);
5936 result2 = compile_shader_source(shader_type, source_string);
5937 SDL_free(source_string);
5944 Uint32 size = (Uint32)strlen(shader_source);
5948 rwops = SDL_RWFromConstMem(shader_source, size);
5949 return renderer->
impl->CompileShader_RW(renderer, shader_type, rwops, 1);
5952 static Uint32 CreateShaderProgram(
GPU_Renderer* renderer)
5954 #ifndef SDL_GPU_DISABLE_SHADERS 5971 #ifndef SDL_GPU_DISABLE_SHADERS 5980 glBindAttribLocation(program_object, 0,
"gpu_Vertex");
5997 (void)program_object;
6003 static void FreeShader(
GPU_Renderer* renderer, Uint32 shader_object)
6006 (void)shader_object;
6007 #ifndef SDL_GPU_DISABLE_SHADERS 6013 static void FreeShaderProgram(
GPU_Renderer* renderer, Uint32 program_object)
6016 (void)program_object;
6017 #ifndef SDL_GPU_DISABLE_SHADERS 6023 static void AttachShader(
GPU_Renderer* renderer, Uint32 program_object, Uint32 shader_object)
6026 (void)program_object;
6027 (void)shader_object;
6028 #ifndef SDL_GPU_DISABLE_SHADERS 6034 static void DetachShader(
GPU_Renderer* renderer, Uint32 program_object, Uint32 shader_object)
6037 (void)program_object;
6038 (void)shader_object;
6039 #ifndef SDL_GPU_DISABLE_SHADERS 6041 glDetachShader(program_object, shader_object);
6049 #ifndef SDL_GPU_DISABLE_SHADERS 6052 if(program_object == 0)
6062 renderer->
impl->FlushBlitBuffer(renderer);
6092 static void DeactivateShaderProgram(
GPU_Renderer* renderer)
6094 renderer->
impl->ActivateShaderProgram(renderer, 0, NULL);
6097 static const char* GetShaderMessage(
GPU_Renderer* renderer)
6100 return shader_message;
6103 static int GetAttributeLocation(
GPU_Renderer* renderer, Uint32 program_object,
const char* attrib_name)
6105 #ifndef SDL_GPU_DISABLE_SHADERS 6108 program_object = get_proper_program_id(renderer, program_object);
6109 if(program_object == 0)
6114 (void)program_object;
6120 static int GetUniformLocation(
GPU_Renderer* renderer, Uint32 program_object,
const char* uniform_name)
6122 #ifndef SDL_GPU_DISABLE_SHADERS 6125 program_object = get_proper_program_id(renderer, program_object);
6126 if(program_object == 0)
6131 (void)program_object;
6137 static GPU_ShaderBlock LoadShaderBlock(
GPU_Renderer* renderer, Uint32 program_object,
const char* position_name,
const char* texcoord_name,
const char* color_name,
const char* modelViewMatrix_name)
6140 program_object = get_proper_program_id(renderer, program_object);
6150 if(position_name == NULL)
6153 b.
position_loc = renderer->
impl->GetAttributeLocation(renderer, program_object, position_name);
6155 if(texcoord_name == NULL)
6158 b.
texcoord_loc = renderer->
impl->GetAttributeLocation(renderer, program_object, texcoord_name);
6160 if(color_name == NULL)
6163 b.
color_loc = renderer->
impl->GetAttributeLocation(renderer, program_object, color_name);
6165 if(modelViewMatrix_name == NULL)
6176 #ifndef SDL_GPU_DISABLE_SHADERS 6182 renderer->
impl->FlushBlitBuffer(renderer);
6193 glBindTexture(GL_TEXTURE_2D, new_texture);
6207 static void GetUniformiv(
GPU_Renderer* renderer, Uint32 program_object,
int location,
int* values)
6210 (void)program_object;
6214 #ifndef SDL_GPU_DISABLE_SHADERS 6217 program_object = get_proper_program_id(renderer, program_object);
6218 if(program_object != 0)
6223 static void SetUniformi(
GPU_Renderer* renderer,
int location,
int value)
6229 #ifndef SDL_GPU_DISABLE_SHADERS 6232 renderer->
impl->FlushBlitBuffer(renderer);
6239 static void SetUniformiv(
GPU_Renderer* renderer,
int location,
int num_elements_per_value,
int num_values,
int* values)
6243 (void)num_elements_per_value;
6247 #ifndef SDL_GPU_DISABLE_SHADERS 6250 renderer->
impl->FlushBlitBuffer(renderer);
6253 switch(num_elements_per_value)
6272 static void GetUniformuiv(
GPU_Renderer* renderer, Uint32 program_object,
int location,
unsigned int* values)
6275 (void)program_object;
6279 #ifndef SDL_GPU_DISABLE_SHADERS 6282 program_object = get_proper_program_id(renderer, program_object);
6283 if(program_object != 0)
6284 #if defined(SDL_GPU_USE_GLES) && SDL_GPU_GLES_MAJOR_VERSION < 3 6287 glGetUniformuiv(program_object, location, values);
6292 static void SetUniformui(
GPU_Renderer* renderer,
int location,
unsigned int value)
6298 #ifndef SDL_GPU_DISABLE_SHADERS 6301 renderer->
impl->FlushBlitBuffer(renderer);
6304 #if defined(SDL_GPU_USE_GLES) && SDL_GPU_GLES_MAJOR_VERSION < 3 6307 glUniform1ui(location, value);
6312 static void SetUniformuiv(
GPU_Renderer* renderer,
int location,
int num_elements_per_value,
int num_values,
unsigned int* values)
6316 (void)num_elements_per_value;
6320 #ifndef SDL_GPU_DISABLE_SHADERS 6323 renderer->
impl->FlushBlitBuffer(renderer);
6326 #if defined(SDL_GPU_USE_GLES) && SDL_GPU_GLES_MAJOR_VERSION < 3 6327 switch(num_elements_per_value)
6343 switch(num_elements_per_value)
6346 glUniform1uiv(location, num_values, values);
6349 glUniform2uiv(location, num_values, values);
6352 glUniform3uiv(location, num_values, values);
6355 glUniform4uiv(location, num_values, values);
6363 static void GetUniformfv(
GPU_Renderer* renderer, Uint32 program_object,
int location,
float* values)
6366 (void)program_object;
6370 #ifndef SDL_GPU_DISABLE_SHADERS 6373 program_object = get_proper_program_id(renderer, program_object);
6374 if(program_object != 0)
6375 glGetUniformfv(program_object, location, values);
6379 static void SetUniformf(
GPU_Renderer* renderer,
int location,
float value)
6385 #ifndef SDL_GPU_DISABLE_SHADERS 6388 renderer->
impl->FlushBlitBuffer(renderer);
6395 static void SetUniformfv(
GPU_Renderer* renderer,
int location,
int num_elements_per_value,
int num_values,
float* values)
6399 (void)num_elements_per_value;
6403 #ifndef SDL_GPU_DISABLE_SHADERS 6406 renderer->
impl->FlushBlitBuffer(renderer);
6409 switch(num_elements_per_value)
6427 static void SetUniformMatrixfv(
GPU_Renderer* renderer,
int location,
int num_matrices,
int num_rows,
int num_columns,
GPU_bool transpose,
float* values)
6437 #ifndef SDL_GPU_DISABLE_SHADERS 6440 renderer->
impl->FlushBlitBuffer(renderer);
6443 if(num_rows < 2 || num_rows > 4 || num_columns < 2 || num_columns > 4)
6448 #if defined(SDL_GPU_USE_GLES) 6450 #define glUniformMatrix2x3fv glUniformMatrix2fv 6451 #define glUniformMatrix2x4fv glUniformMatrix2fv 6452 #define glUniformMatrix3x2fv glUniformMatrix2fv 6453 #define glUniformMatrix3x4fv glUniformMatrix2fv 6454 #define glUniformMatrix4x2fv glUniformMatrix2fv 6455 #define glUniformMatrix4x3fv glUniformMatrix2fv 6456 if(num_rows != num_columns)
6466 if(num_columns == 2)
6467 glUniformMatrix2fv(location, num_matrices, transpose, values);
6468 else if(num_columns == 3)
6469 glUniformMatrix2x3fv(location, num_matrices, transpose, values);
6470 else if(num_columns == 4)
6471 glUniformMatrix2x4fv(location, num_matrices, transpose, values);
6474 if(num_columns == 2)
6475 glUniformMatrix3x2fv(location, num_matrices, transpose, values);
6476 else if(num_columns == 3)
6477 glUniformMatrix3fv(location, num_matrices, transpose, values);
6478 else if(num_columns == 4)
6479 glUniformMatrix3x4fv(location, num_matrices, transpose, values);
6482 if(num_columns == 2)
6483 glUniformMatrix4x2fv(location, num_matrices, transpose, values);
6484 else if(num_columns == 3)
6485 glUniformMatrix4x3fv(location, num_matrices, transpose, values);
6486 else if(num_columns == 4)
6494 static void SetAttributef(
GPU_Renderer* renderer,
int location,
float value)
6500 #ifndef SDL_GPU_DISABLE_SHADERS 6503 renderer->
impl->FlushBlitBuffer(renderer);
6507 #ifdef SDL_GPU_USE_OPENGL 6508 if(apply_Intel_attrib_workaround && location == 0)
6510 apply_Intel_attrib_workaround =
GPU_FALSE;
6511 glBegin(GL_TRIANGLES);
6521 static void SetAttributei(
GPU_Renderer* renderer,
int location,
int value)
6527 #ifndef SDL_GPU_DISABLE_SHADERS 6530 renderer->
impl->FlushBlitBuffer(renderer);
6534 #ifdef SDL_GPU_USE_OPENGL 6535 if(apply_Intel_attrib_workaround && location == 0)
6537 apply_Intel_attrib_workaround =
GPU_FALSE;
6538 glBegin(GL_TRIANGLES);
6548 static void SetAttributeui(
GPU_Renderer* renderer,
int location,
unsigned int value)
6554 #ifndef SDL_GPU_DISABLE_SHADERS 6557 renderer->
impl->FlushBlitBuffer(renderer);
6561 #ifdef SDL_GPU_USE_OPENGL 6562 if(apply_Intel_attrib_workaround && location == 0)
6564 apply_Intel_attrib_workaround =
GPU_FALSE;
6565 glBegin(GL_TRIANGLES);
6576 static void SetAttributefv(
GPU_Renderer* renderer,
int location,
int num_elements,
float* value)
6583 #ifndef SDL_GPU_DISABLE_SHADERS 6586 renderer->
impl->FlushBlitBuffer(renderer);
6590 #ifdef SDL_GPU_USE_OPENGL 6591 if(apply_Intel_attrib_workaround && location == 0)
6593 apply_Intel_attrib_workaround =
GPU_FALSE;
6594 glBegin(GL_TRIANGLES);
6599 switch(num_elements)
6618 static void SetAttributeiv(
GPU_Renderer* renderer,
int location,
int num_elements,
int* value)
6624 #ifndef SDL_GPU_DISABLE_SHADERS 6627 renderer->
impl->FlushBlitBuffer(renderer);
6631 #ifdef SDL_GPU_USE_OPENGL 6632 if(apply_Intel_attrib_workaround && location == 0)
6634 apply_Intel_attrib_workaround =
GPU_FALSE;
6635 glBegin(GL_TRIANGLES);
6640 switch(num_elements)
6659 static void SetAttributeuiv(
GPU_Renderer* renderer,
int location,
int num_elements,
unsigned int* value)
6666 #ifndef SDL_GPU_DISABLE_SHADERS 6669 renderer->
impl->FlushBlitBuffer(renderer);
6673 #ifdef SDL_GPU_USE_OPENGL 6674 if(apply_Intel_attrib_workaround && location == 0)
6676 apply_Intel_attrib_workaround =
GPU_FALSE;
6677 glBegin(GL_TRIANGLES);
6682 switch(num_elements)
6703 #ifndef SDL_GPU_DISABLE_SHADERS 6712 FlushBlitBuffer(renderer);
6714 a = &cdata->shader_attributes[source.
location];
6761 #define SET_COMMON_FUNCTIONS(impl) \ 6762 impl->Init = &Init; \ 6763 impl->CreateTargetFromWindow = &CreateTargetFromWindow; \ 6764 impl->CreateAliasTarget = &CreateAliasTarget; \ 6765 impl->MakeCurrent = &MakeCurrent; \ 6766 impl->SetAsCurrent = &SetAsCurrent; \ 6767 impl->ResetRendererState = &ResetRendererState; \ 6768 impl->SetWindowResolution = &SetWindowResolution; \ 6769 impl->SetVirtualResolution = &SetVirtualResolution; \ 6770 impl->UnsetVirtualResolution = &UnsetVirtualResolution; \ 6771 impl->Quit = &Quit; \ 6773 impl->SetFullscreen = &SetFullscreen; \ 6774 impl->SetCamera = &SetCamera; \ 6776 impl->CreateImage = &CreateImage; \ 6777 impl->CreateImageUsingTexture = &CreateImageUsingTexture; \ 6778 impl->CreateAliasImage = &CreateAliasImage; \ 6779 impl->SaveImage = &SaveImage; \ 6780 impl->CopyImage = &CopyImage; \ 6781 impl->UpdateImage = &UpdateImage; \ 6782 impl->UpdateImageBytes = &UpdateImageBytes; \ 6783 impl->ReplaceImage = &ReplaceImage; \ 6784 impl->CopyImageFromSurface = &CopyImageFromSurface; \ 6785 impl->CopyImageFromTarget = &CopyImageFromTarget; \ 6786 impl->CopySurfaceFromTarget = &CopySurfaceFromTarget; \ 6787 impl->CopySurfaceFromImage = &CopySurfaceFromImage; \ 6788 impl->FreeImage = &FreeImage; \ 6790 impl->GetTarget = &GetTarget; \ 6791 impl->FreeTarget = &FreeTarget; \ 6793 impl->Blit = &Blit; \ 6794 impl->BlitRotate = &BlitRotate; \ 6795 impl->BlitScale = &BlitScale; \ 6796 impl->BlitTransform = &BlitTransform; \ 6797 impl->BlitTransformX = &BlitTransformX; \ 6798 impl->TriangleBatchX = &TriangleBatchX; \ 6800 impl->GenerateMipmaps = &GenerateMipmaps; \ 6802 impl->SetClip = &SetClip; \ 6803 impl->UnsetClip = &UnsetClip; \ 6805 impl->GetPixel = &GetPixel; \ 6806 impl->SetImageFilter = &SetImageFilter; \ 6807 impl->SetWrapMode = &SetWrapMode; \ 6809 impl->ClearRGBA = &ClearRGBA; \ 6810 impl->FlushBlitBuffer = &FlushBlitBuffer; \ 6811 impl->Flip = &Flip; \ 6813 impl->CompileShader_RW = &CompileShader_RW; \ 6814 impl->CompileShader = &CompileShader; \ 6815 impl->CreateShaderProgram = &CreateShaderProgram; \ 6816 impl->LinkShaderProgram = &LinkShaderProgram; \ 6817 impl->FreeShader = &FreeShader; \ 6818 impl->FreeShaderProgram = &FreeShaderProgram; \ 6819 impl->AttachShader = &AttachShader; \ 6820 impl->DetachShader = &DetachShader; \ 6821 impl->ActivateShaderProgram = &ActivateShaderProgram; \ 6822 impl->DeactivateShaderProgram = &DeactivateShaderProgram; \ 6823 impl->GetShaderMessage = &GetShaderMessage; \ 6824 impl->GetAttributeLocation = &GetAttributeLocation; \ 6825 impl->GetUniformLocation = &GetUniformLocation; \ 6826 impl->LoadShaderBlock = &LoadShaderBlock; \ 6827 impl->SetShaderImage = &SetShaderImage; \ 6828 impl->GetUniformiv = &GetUniformiv; \ 6829 impl->SetUniformi = &SetUniformi; \ 6830 impl->SetUniformiv = &SetUniformiv; \ 6831 impl->GetUniformuiv = &GetUniformuiv; \ 6832 impl->SetUniformui = &SetUniformui; \ 6833 impl->SetUniformuiv = &SetUniformuiv; \ 6834 impl->GetUniformfv = &GetUniformfv; \ 6835 impl->SetUniformf = &SetUniformf; \ 6836 impl->SetUniformfv = &SetUniformfv; \ 6837 impl->SetUniformMatrixfv = &SetUniformMatrixfv; \ 6838 impl->SetAttributef = &SetAttributef; \ 6839 impl->SetAttributei = &SetAttributei; \ 6840 impl->SetAttributeui = &SetAttributeui; \ 6841 impl->SetAttributefv = &SetAttributefv; \ 6842 impl->SetAttributeiv = &SetAttributeiv; \ 6843 impl->SetAttributeuiv = &SetAttributeuiv; \ 6844 impl->SetAttributeSource = &SetAttributeSource; \ 6848 impl->SetLineThickness = &SetLineThickness; \ 6849 impl->GetLineThickness = &GetLineThickness; \ 6850 impl->Pixel = &Pixel; \ 6851 impl->Line = &Line; \ 6853 impl->ArcFilled = &ArcFilled; \ 6854 impl->Circle = &Circle; \ 6855 impl->CircleFilled = &CircleFilled; \ 6856 impl->Ellipse = &Ellipse; \ 6857 impl->EllipseFilled = &EllipseFilled; \ 6858 impl->Sector = &Sector; \ 6859 impl->SectorFilled = &SectorFilled; \ 6861 impl->TriFilled = &TriFilled; \ 6862 impl->Rectangle = &Rectangle; \ 6863 impl->RectangleFilled = &RectangleFilled; \ 6864 impl->RectangleRound = &RectangleRound; \ 6865 impl->RectangleRoundFilled = &RectangleRoundFilled; \ 6866 impl->Polygon = &Polygon; \ 6867 impl->PolygonFilled = &PolygonFilled; GPU_Image *SDLCALL * CopyImageFromSurface(GPU_Renderer *renderer, SDL_Surface *surface)
#define glVertexAttribI1i
struct GPU_Renderer * renderer
GPU_Target * context_target
#define glGetProgramInfoLog
#define glVertexAttribI3ui
DECLSPEC Uint32 SDLCALL GPU_GetInitWindow(void)
GPU_Target *SDLCALL * CreateTargetFromWindow(GPU_Renderer *renderer, Uint32 windowID, GPU_Target *target)
GPU_BlendFuncEnum dest_alpha
DECLSPEC void SDLCALL GPU_MatrixOrtho(float *result, float left, float right, float bottom, float top, float near, float far)
DECLSPEC void SDLCALL GPU_MakeCurrent(GPU_Target *target, Uint32 windowID)
static_inline void flushAndClearBlitBufferIfCurrentFramebuffer(GPU_Renderer *renderer, GPU_Target *target)
#define SDL_GPU_GLSL_VERSION
static GLenum GLenum textarget
GPU_BlendEqEnum color_equation
static_inline void flushAndClearBlitBufferIfCurrentTexture(GPU_Renderer *renderer, GPU_Image *image)
DECLSPEC GPU_bool SDLCALL GPU_SaveSurface(SDL_Surface *surface, const char *filename, GPU_FileFormatEnum format)
#define GPU_BLIT_BUFFER_COLOR_OFFSET
DECLSPEC float *SDLCALL GPU_GetProjection(void)
GPU_RendererID requested_id
DECLSPEC const char *SDLCALL GPU_GetShaderMessage(void)
static_inline Uint32 get_window_id(SDL_Surface *window)
GPU_Image *SDLCALL * CreateImage(GPU_Renderer *renderer, Uint16 w, Uint16 h, GPU_FormatEnum format)
static_inline Uint32 getPixel(SDL_Surface *Surface, int x, int y)
DECLSPEC void SDLCALL GPU_SetColor(GPU_Image *image, SDL_Color color)
static_inline GPU_bool get_fullscreen_state(SDL_Window *window)
DECLSPEC GPU_InitFlagEnum SDLCALL GPU_GetPreInitFlags(void)
Uint32 current_shader_program
float matrix[GPU_MATRIX_STACK_MAX][16]
DECLSPEC void SDLCALL GPU_SetImageFilter(GPU_Image *image, GPU_FilterEnum filter)
int per_vertex_storage_size
GPU_ShaderBlock default_untextured_shader_block
#define glDeleteFramebuffers
#define GPU_DEFAULT_TEXTURED_VERTEX_SHADER_SOURCE
#define GL_MIRRORED_REPEAT
struct GPU_Renderer * renderer
#define glVertexAttribI2i
DECLSPEC float *SDLCALL GPU_GetModelView(void)
static_inline void upload_texture(const void *pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length, unsigned int pitch, int bytes_per_pixel)
static_inline void upload_new_texture(void *pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length, int bytes_per_pixel)
#define GPU_FEATURE_BASIC_SHADERS
int modelViewProjection_loc
GPU_MatrixStack modelview_matrix
#define SDL_GPU_GLES_MAJOR_VERSION
GPU_BlendMode shapes_blend_mode
DECLSPEC void SDLCALL GPU_SetShaderBlock(GPU_ShaderBlock block)
GPU_bool shapes_use_blending
DECLSPEC GPU_Target *SDLCALL GPU_GetContextTarget(void)
#define glGetAttribLocation
#define GL_FRAGMENT_SHADER
#define glGetShaderInfoLog
GPU_BlendFuncEnum source_color
DECLSPEC void SDLCALL GPU_SetBlending(GPU_Image *image, GPU_bool enable)
GPU_WindowFlagEnum SDL_init_flags
DECLSPEC void SDLCALL GPU_MatrixRotate(float *result, float degrees, float x, float y, float z)
DECLSPEC GPU_FeatureEnum SDLCALL GPU_GetRequiredFeatures(void)
GPU_bool using_virtual_resolution
int gpu_strcasecmp(const char *s1, const char *s2)
GPU_Target * current_context_target
#define GPU_BLIT_BUFFER_INIT_MAX_NUM_VERTICES
static_inline void flushAndBindTexture(GPU_Renderer *renderer, GLuint handle)
#define glFramebufferTexture2D
#define glUniformMatrix4fv
#define glVertexAttribI2ui
#define glVertexAttribI3i
#define GPU_BLIT_BUFFER_STRIDE
#define glGenFramebuffers
#define glEnableVertexAttribArray
#define SET_INDEXED_VERTEX(offset)
DECLSPEC void SDLCALL GPU_AddWindowMapping(GPU_Target *target)
DECLSPEC void SDLCALL GPU_MultiplyAndAssign(float *result, float *B)
DECLSPEC void SDLCALL GPU_UnsetImageVirtualResolution(GPU_Image *image)
#define glVertexAttribPointer
static_inline void get_target_window_dimensions(GPU_Target *target, int *w, int *h)
DECLSPEC void SDLCALL GPU_UnsetClip(GPU_Target *target)
DECLSPEC void SDLCALL GPU_FreeImage(GPU_Image *image)
static_inline void get_target_drawable_dimensions(GPU_Target *target, int *w, int *h)
#define GPU_DEFAULT_UNTEXTURED_VERTEX_SHADER_SOURCE
DECLSPEC void SDLCALL GPU_GenerateMipmaps(GPU_Image *image)
static GLenum GLenum GLuint GLint level
DECLSPEC void SDLCALL GPU_GetModelViewProjection(float *result)
GPU_AttributeFormat format
#define glVertexAttribI1ui
GPU_MatrixStack projection_matrix
#define glDisableVertexAttribArray
static GLenum GLenum GLuint texture
SDL_Surface *SDLCALL * CopySurfaceFromImage(GPU_Renderer *renderer, GPU_Image *image)
#define SDL_GPU_GLSL_VERSION_CORE
#define GL_FRAMEBUFFER_COMPLETE
#define GET_ALPHA(sdl_color)
#define SDL_GPU_DISABLE_SHADERS
#define GPU_DEFAULT_TEXTURED_VERTEX_SHADER_SOURCE_CORE
Uint32 GPU_WindowFlagEnum
GPU_ShaderBlock current_shader_block
#define GPU_DEFAULT_TEXTURED_FRAGMENT_SHADER_SOURCE
#define SET_TEXTURED_VERTEX_UNINDEXED(x, y, s, t, r, g, b, a)
DECLSPEC void SDLCALL GPU_MatrixTranslate(float *result, float x, float y, float z)
GPU_Target *SDLCALL * GetTarget(GPU_Renderer *renderer, GPU_Image *image)
static GLuint framebuffer
DECLSPEC void SDLCALL GPU_LogError(const char *format,...)
static_inline void submit_buffer_data(int bytes, float *values, int bytes_indices, unsigned short *indices)
GPU_FilterEnum filter_mode
void * per_vertex_storage
static_inline void fast_upload_texture(const void *pixels, GPU_Rect update_rect, Uint32 format, int alignment, int row_length)
DECLSPEC GPU_bool SDLCALL GPU_GetCoordinateMode(void)
GPU_InitFlagEnum GPU_init_flags
static_inline void get_drawable_dimensions(SDL_Window *window, int *w, int *h)
#define MIX_COLOR_COMPONENT(a, b)
Uint32 default_untextured_shader_program
#define GPU_BLIT_BUFFER_ABSOLUTE_MAX_VERTICES
#define GL_COLOR_ATTACHMENT0
float default_image_anchor_y
static const GLuint * framebuffers
static_inline void get_window_dimensions(SDL_Window *window, int *w, int *h)
static_inline unsigned int getNearestPowerOf2(unsigned int n)
#define glVertexAttribI4ui
static_inline GPU_bool isCurrentTarget(GPU_Renderer *renderer, GPU_Target *target)
DECLSPEC void SDLCALL GPU_RemoveWindowMappingByTarget(GPU_Target *target)
int per_vertex_storage_stride_bytes
GPU_bool using_virtual_resolution
DECLSPEC void SDLCALL GPU_UnsetColor(GPU_Image *image)
#define GPU_BLIT_BUFFER_TEX_COORD_OFFSET
GPU_Target * context_target
SDL_Surface *SDLCALL * CopySurfaceFromTarget(GPU_Renderer *renderer, GPU_Target *target)
#define GPU_BLIT_BUFFER_FLOATS_PER_VERTEX
#define glBlendFuncSeparate
static_inline void flushAndBindFramebuffer(GPU_Renderer *renderer, GLuint handle)
GPU_BlendEqEnum alpha_equation
#define glGetUniformLocation
DECLSPEC void SDLCALL GPU_MatrixCopy(float *result, const float *A)
static_inline void resize_window(GPU_Target *target, int w, int h)
#define GPU_DEFAULT_UNTEXTURED_FRAGMENT_SHADER_SOURCE_CORE
#define GPU_DEFAULT_UNTEXTURED_FRAGMENT_SHADER_SOURCE
#define glBindFramebuffer
#define glCheckFramebufferStatus
struct GPU_RendererImpl * impl
#define GPU_BLIT_BUFFER_VERTICES_PER_SPRITE
#define glVertexAttribI4i
GPU_BlendFuncEnum source_alpha
#define MIX_COLOR_COMPONENT_NORMALIZED_RESULT(a, b)
DECLSPEC void SDLCALL GPU_MatrixIdentity(float *result)
static_inline void flushBlitBufferIfCurrentTexture(GPU_Renderer *renderer, GPU_Image *image)
DECLSPEC void SDLCALL GPU_SetInitWindow(Uint32 windowID)
GPU_BlendFuncEnum dest_color
DECLSPEC GPU_Rect SDLCALL GPU_MakeRect(float x, float y, float w, float h)
float default_image_anchor_x
#define GPU_DEFAULT_TEXTURED_FRAGMENT_SHADER_SOURCE_CORE
DECLSPEC GPU_BlendMode SDLCALL GPU_GetBlendModeFromPreset(GPU_BlendPresetEnum preset)
DECLSPEC void SDLCALL GPU_SetWrapMode(GPU_Image *image, GPU_WrapEnum wrap_mode_x, GPU_WrapEnum wrap_mode_y)
DECLSPEC void SDLCALL GPU_PushErrorCode(const char *function, GPU_ErrorEnum error, const char *details,...)
DECLSPEC void SDLCALL GPU_SetSnapMode(GPU_Image *image, GPU_SnapEnum mode)
#define SDL_GPU_GL_MAJOR_VERSION
DECLSPEC void SDLCALL GPU_MatrixScale(float *result, float sx, float sy, float sz)
DECLSPEC GPU_Camera SDLCALL GPU_GetDefaultCamera(void)
#define glBlendEquationSeparate
#define GL_COMPILE_STATUS
GPU_FeatureEnum enabled_features
DECLSPEC GPU_Target *SDLCALL GPU_GetTarget(GPU_Image *image)
static_inline GPU_bool has_colorkey(SDL_Surface *surface)
DECLSPEC void SDLCALL GPU_Multiply4x4(float *result, float *A, float *B)
#define GPU_BLIT_BUFFER_VERTEX_OFFSET
DECLSPEC void SDLCALL GPU_RemoveWindowMapping(Uint32 windowID)
#define GL_FRAMEBUFFER_BINDING
GPU_ShaderBlock default_textured_shader_block
DECLSPEC GPU_ShaderBlock SDLCALL GPU_LoadShaderBlock(Uint32 program_object, const char *position_name, const char *texcoord_name, const char *color_name, const char *modelViewMatrix_name)
static_inline SDL_Window * get_window(Uint32 windowID)
Uint32 default_textured_shader_program
DECLSPEC void SDLCALL GPU_SetImageVirtualResolution(GPU_Image *image, Uint16 w, Uint16 h)
static_inline GPU_bool isPowerOfTwo(unsigned int x)
int per_vertex_storage_offset_bytes
#define GPU_DEFAULT_UNTEXTURED_VERTEX_SHADER_SOURCE_CORE
#define GPU_INDEX_BUFFER_ABSOLUTE_MAX_VERTICES