12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856 |
- /*
- Simple DirectMedia Layer
- Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
- */
- #include "SDL_internal.h"
- #if SDL_VIDEO_RENDER_OGL
- #include "../../video/SDL_sysvideo.h" /* For SDL_RecreateWindow */
- #include <SDL3/SDL_opengl.h>
- #include "../SDL_sysrender.h"
- #include "SDL_shaders_gl.h"
- #include "../../video/SDL_pixels_c.h"
- #ifdef SDL_PLATFORM_MACOS
- #include <OpenGL/OpenGL.h>
- #endif
- #ifdef SDL_VIDEO_VITA_PVR_OGL
- #include <GL/gl.h>
- #include <GL/glext.h>
- #endif
- /* To prevent unnecessary window recreation,
- * these should match the defaults selected in SDL_GL_ResetAttributes
- */
- #define RENDERER_CONTEXT_MAJOR 2
- #define RENDERER_CONTEXT_MINOR 1
- /* OpenGL renderer implementation */
- /* Details on optimizing the texture path on macOS:
- http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/opengl_texturedata/opengl_texturedata.html
- */
- typedef struct GL_FBOList GL_FBOList;
- struct GL_FBOList
- {
- Uint32 w, h;
- GLuint FBO;
- GL_FBOList *next;
- };
- typedef struct
- {
- SDL_bool viewport_dirty;
- SDL_Rect viewport;
- SDL_Texture *texture;
- SDL_Texture *target;
- int drawablew;
- int drawableh;
- SDL_BlendMode blend;
- GL_Shader shader;
- const float *shader_params;
- SDL_bool cliprect_enabled_dirty;
- SDL_bool cliprect_enabled;
- SDL_bool cliprect_dirty;
- SDL_Rect cliprect;
- SDL_bool texturing;
- SDL_bool texturing_dirty;
- SDL_bool vertex_array;
- SDL_bool color_array;
- SDL_bool texture_array;
- SDL_bool color_dirty;
- SDL_FColor color;
- SDL_bool clear_color_dirty;
- SDL_FColor clear_color;
- } GL_DrawStateCache;
- typedef struct
- {
- SDL_GLContext context;
- SDL_bool debug_enabled;
- SDL_bool GL_ARB_debug_output_supported;
- int errors;
- char **error_messages;
- GLDEBUGPROCARB next_error_callback;
- GLvoid *next_error_userparam;
- GLenum textype;
- SDL_bool GL_ARB_texture_non_power_of_two_supported;
- SDL_bool GL_ARB_texture_rectangle_supported;
- SDL_bool GL_EXT_framebuffer_object_supported;
- GL_FBOList *framebuffers;
- /* OpenGL functions */
- #define SDL_PROC(ret, func, params) ret (APIENTRY *func) params;
- #include "SDL_glfuncs.h"
- #undef SDL_PROC
- /* Multitexture support */
- SDL_bool GL_ARB_multitexture_supported;
- PFNGLACTIVETEXTUREARBPROC glActiveTextureARB;
- GLint num_texture_units;
- PFNGLGENFRAMEBUFFERSEXTPROC glGenFramebuffersEXT;
- PFNGLDELETEFRAMEBUFFERSEXTPROC glDeleteFramebuffersEXT;
- PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;
- PFNGLBINDFRAMEBUFFEREXTPROC glBindFramebufferEXT;
- PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC glCheckFramebufferStatusEXT;
- /* Shader support */
- GL_ShaderContext *shaders;
- GL_DrawStateCache drawstate;
- } GL_RenderData;
- typedef struct
- {
- GLuint texture;
- SDL_bool texture_external;
- GLfloat texw;
- GLfloat texh;
- GLenum format;
- GLenum formattype;
- GL_Shader shader;
- const float *shader_params;
- void *pixels;
- int pitch;
- SDL_Rect locked_rect;
- #if SDL_HAVE_YUV
- /* YUV texture support */
- SDL_bool yuv;
- SDL_bool nv12;
- GLuint utexture;
- SDL_bool utexture_external;
- GLuint vtexture;
- SDL_bool vtexture_external;
- #endif
- GL_FBOList *fbo;
- } GL_TextureData;
- static const char *GL_TranslateError(GLenum error)
- {
- #define GL_ERROR_TRANSLATE(e) \
- case e: \
- return #e;
- switch (error) {
- GL_ERROR_TRANSLATE(GL_INVALID_ENUM)
- GL_ERROR_TRANSLATE(GL_INVALID_VALUE)
- GL_ERROR_TRANSLATE(GL_INVALID_OPERATION)
- GL_ERROR_TRANSLATE(GL_OUT_OF_MEMORY)
- GL_ERROR_TRANSLATE(GL_NO_ERROR)
- GL_ERROR_TRANSLATE(GL_STACK_OVERFLOW)
- GL_ERROR_TRANSLATE(GL_STACK_UNDERFLOW)
- GL_ERROR_TRANSLATE(GL_TABLE_TOO_LARGE)
- default:
- return "UNKNOWN";
- }
- #undef GL_ERROR_TRANSLATE
- }
- static void GL_ClearErrors(SDL_Renderer *renderer)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- if (!data->debug_enabled) {
- return;
- }
- if (data->GL_ARB_debug_output_supported) {
- if (data->errors) {
- int i;
- for (i = 0; i < data->errors; ++i) {
- SDL_free(data->error_messages[i]);
- }
- SDL_free(data->error_messages);
- data->errors = 0;
- data->error_messages = NULL;
- }
- } else if (data->glGetError) {
- while (data->glGetError() != GL_NO_ERROR) {
- /* continue; */
- }
- }
- }
- static int GL_CheckAllErrors(const char *prefix, SDL_Renderer *renderer, const char *file, int line, const char *function)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- int ret = 0;
- if (!data->debug_enabled) {
- return 0;
- }
- if (data->GL_ARB_debug_output_supported) {
- if (data->errors) {
- int i;
- for (i = 0; i < data->errors; ++i) {
- SDL_SetError("%s: %s (%d): %s %s", prefix, file, line, function, data->error_messages[i]);
- ret = -1;
- }
- GL_ClearErrors(renderer);
- }
- } else {
- /* check gl errors (can return multiple errors) */
- for (;;) {
- GLenum error = data->glGetError();
- if (error != GL_NO_ERROR) {
- if (prefix == NULL || prefix[0] == '\0') {
- prefix = "generic";
- }
- SDL_SetError("%s: %s (%d): %s %s (0x%X)", prefix, file, line, function, GL_TranslateError(error), error);
- ret = -1;
- } else {
- break;
- }
- }
- }
- return ret;
- }
- #if 0
- #define GL_CheckError(prefix, renderer)
- #else
- #define GL_CheckError(prefix, renderer) GL_CheckAllErrors(prefix, renderer, SDL_FILE, SDL_LINE, SDL_FUNCTION)
- #endif
- static int GL_LoadFunctions(GL_RenderData *data)
- {
- #ifdef __SDL_NOGETPROCADDR__
- #define SDL_PROC(ret, func, params) data->func = func;
- #else
- int retval = 0;
- #define SDL_PROC(ret, func, params) \
- do { \
- data->func = (ret (APIENTRY *) params)SDL_GL_GetProcAddress(#func); \
- if (!data->func) { \
- retval = SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \
- } \
- } while (0);
- #endif /* __SDL_NOGETPROCADDR__ */
- #include "SDL_glfuncs.h"
- #undef SDL_PROC
- return retval;
- }
- static int GL_ActivateRenderer(SDL_Renderer *renderer)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- if (SDL_GL_GetCurrentContext() != data->context) {
- if (SDL_GL_MakeCurrent(renderer->window, data->context) < 0) {
- return -1;
- }
- }
- GL_ClearErrors(renderer);
- return 0;
- }
- static void APIENTRY GL_HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, const void *userParam)
- {
- SDL_Renderer *renderer = (SDL_Renderer *)userParam;
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- if (type == GL_DEBUG_TYPE_ERROR_ARB) {
- /* Record this error */
- int errors = data->errors + 1;
- char **error_messages = (char **)SDL_realloc(data->error_messages, errors * sizeof(*data->error_messages));
- if (error_messages) {
- data->errors = errors;
- data->error_messages = error_messages;
- data->error_messages[data->errors - 1] = SDL_strdup(message);
- }
- }
- /* If there's another error callback, pass it along, otherwise log it */
- if (data->next_error_callback) {
- data->next_error_callback(source, type, id, severity, length, message, data->next_error_userparam);
- } else {
- if (type == GL_DEBUG_TYPE_ERROR_ARB) {
- SDL_LogError(SDL_LOG_CATEGORY_RENDER, "%s", message);
- } else {
- SDL_LogDebug(SDL_LOG_CATEGORY_RENDER, "%s", message);
- }
- }
- }
- static GL_FBOList *GL_GetFBO(GL_RenderData *data, Uint32 w, Uint32 h)
- {
- GL_FBOList *result = data->framebuffers;
- while (result && ((result->w != w) || (result->h != h))) {
- result = result->next;
- }
- if (!result) {
- result = (GL_FBOList *)SDL_malloc(sizeof(GL_FBOList));
- if (result) {
- result->w = w;
- result->h = h;
- data->glGenFramebuffersEXT(1, &result->FBO);
- result->next = data->framebuffers;
- data->framebuffers = result;
- }
- }
- return result;
- }
- static void GL_WindowEvent(SDL_Renderer *renderer, const SDL_WindowEvent *event)
- {
- /* If the window x/y/w/h changed at all, assume the viewport has been
- * changed behind our backs. x/y changes might seem weird but viewport
- * resets have been observed on macOS at minimum!
- */
- if (event->type == SDL_EVENT_WINDOW_RESIZED ||
- event->type == SDL_EVENT_WINDOW_MOVED) {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- data->drawstate.viewport_dirty = SDL_TRUE;
- }
- }
- static GLenum GetBlendFunc(SDL_BlendFactor factor)
- {
- switch (factor) {
- case SDL_BLENDFACTOR_ZERO:
- return GL_ZERO;
- case SDL_BLENDFACTOR_ONE:
- return GL_ONE;
- case SDL_BLENDFACTOR_SRC_COLOR:
- return GL_SRC_COLOR;
- case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
- return GL_ONE_MINUS_SRC_COLOR;
- case SDL_BLENDFACTOR_SRC_ALPHA:
- return GL_SRC_ALPHA;
- case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
- return GL_ONE_MINUS_SRC_ALPHA;
- case SDL_BLENDFACTOR_DST_COLOR:
- return GL_DST_COLOR;
- case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
- return GL_ONE_MINUS_DST_COLOR;
- case SDL_BLENDFACTOR_DST_ALPHA:
- return GL_DST_ALPHA;
- case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
- return GL_ONE_MINUS_DST_ALPHA;
- default:
- return GL_INVALID_ENUM;
- }
- }
- static GLenum GetBlendEquation(SDL_BlendOperation operation)
- {
- switch (operation) {
- case SDL_BLENDOPERATION_ADD:
- return GL_FUNC_ADD;
- case SDL_BLENDOPERATION_SUBTRACT:
- return GL_FUNC_SUBTRACT;
- case SDL_BLENDOPERATION_REV_SUBTRACT:
- return GL_FUNC_REVERSE_SUBTRACT;
- case SDL_BLENDOPERATION_MINIMUM:
- return GL_MIN;
- case SDL_BLENDOPERATION_MAXIMUM:
- return GL_MAX;
- default:
- return GL_INVALID_ENUM;
- }
- }
- static SDL_bool GL_SupportsBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
- {
- SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
- SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
- SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
- SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
- SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
- SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
- if (GetBlendFunc(srcColorFactor) == GL_INVALID_ENUM ||
- GetBlendFunc(srcAlphaFactor) == GL_INVALID_ENUM ||
- GetBlendEquation(colorOperation) == GL_INVALID_ENUM ||
- GetBlendFunc(dstColorFactor) == GL_INVALID_ENUM ||
- GetBlendFunc(dstAlphaFactor) == GL_INVALID_ENUM ||
- GetBlendEquation(alphaOperation) == GL_INVALID_ENUM) {
- return SDL_FALSE;
- }
- if (colorOperation != alphaOperation) {
- return SDL_FALSE;
- }
- return SDL_TRUE;
- }
- static SDL_bool convert_format(Uint32 pixel_format, GLint *internalFormat, GLenum *format, GLenum *type)
- {
- switch (pixel_format) {
- case SDL_PIXELFORMAT_ARGB8888:
- case SDL_PIXELFORMAT_XRGB8888:
- *internalFormat = GL_RGBA8;
- *format = GL_BGRA;
- *type = GL_UNSIGNED_INT_8_8_8_8_REV;
- break;
- case SDL_PIXELFORMAT_ABGR8888:
- case SDL_PIXELFORMAT_XBGR8888:
- *internalFormat = GL_RGBA8;
- *format = GL_RGBA;
- *type = GL_UNSIGNED_INT_8_8_8_8_REV;
- break;
- case SDL_PIXELFORMAT_YV12:
- case SDL_PIXELFORMAT_IYUV:
- case SDL_PIXELFORMAT_NV12:
- case SDL_PIXELFORMAT_NV21:
- *internalFormat = GL_LUMINANCE;
- *format = GL_LUMINANCE;
- *type = GL_UNSIGNED_BYTE;
- break;
- #ifdef SDL_PLATFORM_MACOS
- case SDL_PIXELFORMAT_UYVY:
- *internalFormat = GL_RGB8;
- *format = GL_YCBCR_422_APPLE;
- *type = GL_UNSIGNED_SHORT_8_8_APPLE;
- break;
- #endif
- default:
- return SDL_FALSE;
- }
- return SDL_TRUE;
- }
- static int GL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_PropertiesID create_props)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- const GLenum textype = renderdata->textype;
- GL_TextureData *data;
- GLint internalFormat;
- GLenum format, type;
- int texture_w, texture_h;
- GLenum scaleMode;
- GL_ActivateRenderer(renderer);
- renderdata->drawstate.texture = NULL; /* we trash this state. */
- renderdata->drawstate.texturing_dirty = SDL_TRUE; /* we trash this state. */
- if (texture->access == SDL_TEXTUREACCESS_TARGET &&
- !renderdata->GL_EXT_framebuffer_object_supported) {
- return SDL_SetError("Render targets not supported by OpenGL");
- }
- if (!convert_format(texture->format, &internalFormat, &format, &type)) {
- return SDL_SetError("Texture format %s not supported by OpenGL",
- SDL_GetPixelFormatName(texture->format));
- }
- data = (GL_TextureData *)SDL_calloc(1, sizeof(*data));
- if (!data) {
- return -1;
- }
- if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
- size_t size;
- data->pitch = texture->w * SDL_BYTESPERPIXEL(texture->format);
- size = (size_t)texture->h * data->pitch;
- if (texture->format == SDL_PIXELFORMAT_YV12 ||
- texture->format == SDL_PIXELFORMAT_IYUV) {
- /* Need to add size for the U and V planes */
- size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
- }
- if (texture->format == SDL_PIXELFORMAT_NV12 ||
- texture->format == SDL_PIXELFORMAT_NV21) {
- /* Need to add size for the U/V plane */
- size += 2 * ((texture->h + 1) / 2) * ((data->pitch + 1) / 2);
- }
- data->pixels = SDL_calloc(1, size);
- if (!data->pixels) {
- SDL_free(data);
- return -1;
- }
- }
- if (texture->access == SDL_TEXTUREACCESS_TARGET) {
- data->fbo = GL_GetFBO(renderdata, texture->w, texture->h);
- } else {
- data->fbo = NULL;
- }
- data->texture = (GLuint)SDL_GetNumberProperty(create_props, SDL_PROP_TEXTURE_CREATE_OPENGL_TEXTURE_NUMBER, 0);
- if (data->texture) {
- data->texture_external = SDL_TRUE;
- } else {
- GL_CheckError("", renderer);
- renderdata->glGenTextures(1, &data->texture);
- if (GL_CheckError("glGenTextures()", renderer) < 0) {
- if (data->pixels) {
- SDL_free(data->pixels);
- }
- SDL_free(data);
- return -1;
- }
- }
- texture->driverdata = data;
- if (renderdata->GL_ARB_texture_non_power_of_two_supported) {
- texture_w = texture->w;
- texture_h = texture->h;
- data->texw = 1.0f;
- data->texh = 1.0f;
- } else if (renderdata->GL_ARB_texture_rectangle_supported) {
- texture_w = texture->w;
- texture_h = texture->h;
- data->texw = (GLfloat)texture_w;
- data->texh = (GLfloat)texture_h;
- } else {
- texture_w = SDL_powerof2(texture->w);
- texture_h = SDL_powerof2(texture->h);
- data->texw = (GLfloat)(texture->w) / texture_w;
- data->texh = (GLfloat)texture->h / texture_h;
- }
- SDL_PropertiesID props = SDL_GetTextureProperties(texture);
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER, data->texture);
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_TARGET_NUMBER, (Sint64) textype);
- SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_OPENGL_TEX_W_FLOAT, data->texw);
- SDL_SetFloatProperty(props, SDL_PROP_TEXTURE_OPENGL_TEX_H_FLOAT, data->texh);
- data->format = format;
- data->formattype = type;
- scaleMode = (texture->scaleMode == SDL_SCALEMODE_NEAREST) ? GL_NEAREST : GL_LINEAR;
- renderdata->glEnable(textype);
- renderdata->glBindTexture(textype, data->texture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, scaleMode);
- /* According to the spec, CLAMP_TO_EDGE is the default for TEXTURE_RECTANGLE
- and setting it causes an INVALID_ENUM error in the latest NVidia drivers.
- */
- if (textype != GL_TEXTURE_RECTANGLE_ARB) {
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
- GL_CLAMP_TO_EDGE);
- }
- #ifdef SDL_PLATFORM_MACOS
- #ifndef GL_TEXTURE_STORAGE_HINT_APPLE
- #define GL_TEXTURE_STORAGE_HINT_APPLE 0x85BC
- #endif
- #ifndef STORAGE_CACHED_APPLE
- #define STORAGE_CACHED_APPLE 0x85BE
- #endif
- #ifndef STORAGE_SHARED_APPLE
- #define STORAGE_SHARED_APPLE 0x85BF
- #endif
- if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
- renderdata->glTexParameteri(textype, GL_TEXTURE_STORAGE_HINT_APPLE,
- GL_STORAGE_SHARED_APPLE);
- } else {
- renderdata->glTexParameteri(textype, GL_TEXTURE_STORAGE_HINT_APPLE,
- GL_STORAGE_CACHED_APPLE);
- }
- if (texture->access == SDL_TEXTUREACCESS_STREAMING && texture->format == SDL_PIXELFORMAT_ARGB8888 && (texture->w % 8) == 0) {
- renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
- renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH,
- (data->pitch / SDL_BYTESPERPIXEL(texture->format)));
- renderdata->glTexImage2D(textype, 0, internalFormat, texture_w,
- texture_h, 0, format, type, data->pixels);
- renderdata->glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
- } else
- #endif
- {
- renderdata->glTexImage2D(textype, 0, internalFormat, texture_w,
- texture_h, 0, format, type, NULL);
- }
- renderdata->glDisable(textype);
- if (GL_CheckError("glTexImage2D()", renderer) < 0) {
- return -1;
- }
- #if SDL_HAVE_YUV
- if (texture->format == SDL_PIXELFORMAT_YV12 ||
- texture->format == SDL_PIXELFORMAT_IYUV) {
- data->yuv = SDL_TRUE;
- data->utexture = (GLuint)SDL_GetNumberProperty(create_props, SDL_PROP_TEXTURE_CREATE_OPENGL_TEXTURE_U_NUMBER, 0);
- if (data->utexture) {
- data->utexture_external = SDL_TRUE;
- } else {
- renderdata->glGenTextures(1, &data->utexture);
- }
- data->vtexture = (GLuint)SDL_GetNumberProperty(create_props, SDL_PROP_TEXTURE_CREATE_OPENGL_TEXTURE_V_NUMBER, 0);
- if (data->vtexture) {
- data->vtexture_external = SDL_TRUE;
- } else {
- renderdata->glGenTextures(1, &data->vtexture);
- }
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexImage2D(textype, 0, internalFormat, (texture_w + 1) / 2,
- (texture_h + 1) / 2, 0, format, type, NULL);
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_U_NUMBER, data->utexture);
- renderdata->glBindTexture(textype, data->vtexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexImage2D(textype, 0, internalFormat, (texture_w + 1) / 2,
- (texture_h + 1) / 2, 0, format, type, NULL);
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_V_NUMBER, data->vtexture);
- }
- if (texture->format == SDL_PIXELFORMAT_NV12 ||
- texture->format == SDL_PIXELFORMAT_NV21) {
- data->nv12 = SDL_TRUE;
- data->utexture = (GLuint)SDL_GetNumberProperty(create_props, SDL_PROP_TEXTURE_CREATE_OPENGL_TEXTURE_UV_NUMBER, 0);
- if (data->utexture) {
- data->utexture_external = SDL_TRUE;
- } else {
- renderdata->glGenTextures(1, &data->utexture);
- }
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER,
- scaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_S,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexParameteri(textype, GL_TEXTURE_WRAP_T,
- GL_CLAMP_TO_EDGE);
- renderdata->glTexImage2D(textype, 0, GL_LUMINANCE_ALPHA, (texture_w + 1) / 2,
- (texture_h + 1) / 2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, NULL);
- SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_OPENGL_TEXTURE_UV_NUMBER, data->utexture);
- }
- #endif
- if (texture->format == SDL_PIXELFORMAT_ABGR8888 || texture->format == SDL_PIXELFORMAT_ARGB8888) {
- data->shader = SHADER_RGBA;
- } else {
- data->shader = SHADER_RGB;
- }
- #if SDL_HAVE_YUV
- if (data->yuv || data->nv12) {
- if (data->yuv) {
- data->shader = SHADER_YUV;
- } else if (texture->format == SDL_PIXELFORMAT_NV12) {
- if (SDL_GetHintBoolean("SDL_RENDER_OPENGL_NV12_RG_SHADER", SDL_FALSE)) {
- data->shader = SHADER_NV12_RG;
- } else {
- data->shader = SHADER_NV12_RA;
- }
- } else {
- if (SDL_GetHintBoolean("SDL_RENDER_OPENGL_NV12_RG_SHADER", SDL_FALSE)) {
- data->shader = SHADER_NV21_RG;
- } else {
- data->shader = SHADER_NV21_RA;
- }
- }
- data->shader_params = SDL_GetYCbCRtoRGBConversionMatrix(texture->colorspace, texture->w, texture->h, 8);
- if (!data->shader_params) {
- return SDL_SetError("Unsupported YUV colorspace");
- }
- }
- #endif /* SDL_HAVE_YUV */
- return GL_CheckError("", renderer);
- }
- static int GL_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture,
- const SDL_Rect *rect, const void *pixels, int pitch)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- const GLenum textype = renderdata->textype;
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- const int texturebpp = SDL_BYTESPERPIXEL(texture->format);
- SDL_assert_release(texturebpp != 0); /* otherwise, division by zero later. */
- GL_ActivateRenderer(renderer);
- renderdata->drawstate.texture = NULL; /* we trash this state. */
- renderdata->glBindTexture(textype, data->texture);
- renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, (pitch / texturebpp));
- renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
- rect->h, data->format, data->formattype,
- pixels);
- #if SDL_HAVE_YUV
- if (data->yuv) {
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
- /* Skip to the correct offset into the next texture */
- pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
- if (texture->format == SDL_PIXELFORMAT_YV12) {
- renderdata->glBindTexture(textype, data->vtexture);
- } else {
- renderdata->glBindTexture(textype, data->utexture);
- }
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- data->format, data->formattype, pixels);
- /* Skip to the correct offset into the next texture */
- pixels = (const void *)((const Uint8 *)pixels + ((rect->h + 1) / 2) * ((pitch + 1) / 2));
- if (texture->format == SDL_PIXELFORMAT_YV12) {
- renderdata->glBindTexture(textype, data->utexture);
- } else {
- renderdata->glBindTexture(textype, data->vtexture);
- }
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- data->format, data->formattype, pixels);
- }
- if (data->nv12) {
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, ((pitch + 1) / 2));
- /* Skip to the correct offset into the next texture */
- pixels = (const void *)((const Uint8 *)pixels + rect->h * pitch);
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
- }
- #endif
- return GL_CheckError("glTexSubImage2D()", renderer);
- }
- #if SDL_HAVE_YUV
- static int GL_UpdateTextureYUV(SDL_Renderer *renderer, SDL_Texture *texture,
- const SDL_Rect *rect,
- const Uint8 *Yplane, int Ypitch,
- const Uint8 *Uplane, int Upitch,
- const Uint8 *Vplane, int Vpitch)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- const GLenum textype = renderdata->textype;
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- GL_ActivateRenderer(renderer);
- renderdata->drawstate.texture = NULL; /* we trash this state. */
- renderdata->glBindTexture(textype, data->texture);
- renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
- renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
- rect->h, data->format, data->formattype,
- Yplane);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Upitch);
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- data->format, data->formattype, Uplane);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Vpitch);
- renderdata->glBindTexture(textype, data->vtexture);
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- data->format, data->formattype, Vplane);
- return GL_CheckError("glTexSubImage2D()", renderer);
- }
- static int GL_UpdateTextureNV(SDL_Renderer *renderer, SDL_Texture *texture,
- const SDL_Rect *rect,
- const Uint8 *Yplane, int Ypitch,
- const Uint8 *UVplane, int UVpitch)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- const GLenum textype = renderdata->textype;
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- GL_ActivateRenderer(renderer);
- renderdata->drawstate.texture = NULL; /* we trash this state. */
- renderdata->glBindTexture(textype, data->texture);
- renderdata->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, Ypitch);
- renderdata->glTexSubImage2D(textype, 0, rect->x, rect->y, rect->w,
- rect->h, data->format, data->formattype,
- Yplane);
- renderdata->glPixelStorei(GL_UNPACK_ROW_LENGTH, UVpitch / 2);
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexSubImage2D(textype, 0, rect->x / 2, rect->y / 2,
- (rect->w + 1) / 2, (rect->h + 1) / 2,
- GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, UVplane);
- return GL_CheckError("glTexSubImage2D()", renderer);
- }
- #endif
- static int GL_LockTexture(SDL_Renderer *renderer, SDL_Texture *texture,
- const SDL_Rect *rect, void **pixels, int *pitch)
- {
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- data->locked_rect = *rect;
- *pixels =
- (void *)((Uint8 *)data->pixels + rect->y * data->pitch +
- rect->x * SDL_BYTESPERPIXEL(texture->format));
- *pitch = data->pitch;
- return 0;
- }
- static void GL_UnlockTexture(SDL_Renderer *renderer, SDL_Texture *texture)
- {
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- const SDL_Rect *rect;
- void *pixels;
- rect = &data->locked_rect;
- pixels =
- (void *)((Uint8 *)data->pixels + rect->y * data->pitch +
- rect->x * SDL_BYTESPERPIXEL(texture->format));
- GL_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
- }
- static void GL_SetTextureScaleMode(SDL_Renderer *renderer, SDL_Texture *texture, SDL_ScaleMode scaleMode)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- const GLenum textype = renderdata->textype;
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- GLenum glScaleMode = (scaleMode == SDL_SCALEMODE_NEAREST) ? GL_NEAREST : GL_LINEAR;
- renderdata->glBindTexture(textype, data->texture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, glScaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, glScaleMode);
- #if SDL_HAVE_YUV
- if (texture->format == SDL_PIXELFORMAT_YV12 ||
- texture->format == SDL_PIXELFORMAT_IYUV) {
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, glScaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, glScaleMode);
- renderdata->glBindTexture(textype, data->vtexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, glScaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, glScaleMode);
- }
- if (texture->format == SDL_PIXELFORMAT_NV12 ||
- texture->format == SDL_PIXELFORMAT_NV21) {
- renderdata->glBindTexture(textype, data->utexture);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, glScaleMode);
- renderdata->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, glScaleMode);
- }
- #endif
- }
- static int GL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- GL_TextureData *texturedata;
- GLenum status;
- GL_ActivateRenderer(renderer);
- if (!data->GL_EXT_framebuffer_object_supported) {
- return SDL_SetError("Render targets not supported by OpenGL");
- }
- data->drawstate.viewport_dirty = SDL_TRUE;
- if (!texture) {
- data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- return 0;
- }
- texturedata = (GL_TextureData *)texture->driverdata;
- data->glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, texturedata->fbo->FBO);
- /* TODO: check if texture pixel format allows this operation */
- data->glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, data->textype, texturedata->texture, 0);
- /* Check FBO status */
- status = data->glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
- if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
- return SDL_SetError("glFramebufferTexture2DEXT() failed");
- }
- return 0;
- }
- /* !!! FIXME: all these Queue* calls set up the vertex buffer the way the immediate mode
- !!! FIXME: renderer wants it, but this might want to operate differently if we move to
- !!! FIXME: VBOs at some point. */
- static int GL_QueueNoOp(SDL_Renderer *renderer, SDL_RenderCommand *cmd)
- {
- return 0; /* nothing to do in this backend. */
- }
- static int GL_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
- {
- GLfloat *verts = (GLfloat *)SDL_AllocateRenderVertices(renderer, count * 2 * sizeof(GLfloat), 0, &cmd->data.draw.first);
- int i;
- if (!verts) {
- return -1;
- }
- cmd->data.draw.count = count;
- for (i = 0; i < count; i++) {
- *(verts++) = 0.5f + points[i].x;
- *(verts++) = 0.5f + points[i].y;
- }
- return 0;
- }
- static int GL_QueueDrawLines(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
- {
- int i;
- GLfloat prevx, prevy;
- const size_t vertlen = (sizeof(GLfloat) * 2) * count;
- GLfloat *verts = (GLfloat *)SDL_AllocateRenderVertices(renderer, vertlen, 0, &cmd->data.draw.first);
- if (!verts) {
- return -1;
- }
- cmd->data.draw.count = count;
- /* 0.5f offset to hit the center of the pixel. */
- prevx = 0.5f + points->x;
- prevy = 0.5f + points->y;
- *(verts++) = prevx;
- *(verts++) = prevy;
- /* bump the end of each line segment out a quarter of a pixel, to provoke
- the diamond-exit rule. Without this, you won't just drop the last
- pixel of the last line segment, but you might also drop pixels at the
- edge of any given line segment along the way too. */
- for (i = 1; i < count; i++) {
- const GLfloat xstart = prevx;
- const GLfloat ystart = prevy;
- const GLfloat xend = points[i].x + 0.5f; /* 0.5f to hit pixel center. */
- const GLfloat yend = points[i].y + 0.5f;
- /* bump a little in the direction we are moving in. */
- const GLfloat deltax = xend - xstart;
- const GLfloat deltay = yend - ystart;
- const GLfloat angle = SDL_atan2f(deltay, deltax);
- prevx = xend + (SDL_cosf(angle) * 0.25f);
- prevy = yend + (SDL_sinf(angle) * 0.25f);
- *(verts++) = prevx;
- *(verts++) = prevy;
- }
- return 0;
- }
- static int GL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
- const float *xy, int xy_stride, const SDL_FColor *color, int color_stride, const float *uv, int uv_stride,
- int num_vertices, const void *indices, int num_indices, int size_indices,
- float scale_x, float scale_y)
- {
- GL_TextureData *texturedata = NULL;
- int i;
- int count = indices ? num_indices : num_vertices;
- GLfloat *verts;
- size_t sz = 2 * sizeof(GLfloat) + 4 * sizeof(GLfloat) + (texture ? 2 : 0) * sizeof(GLfloat);
- const float color_scale = cmd->data.draw.color_scale;
- verts = (GLfloat *)SDL_AllocateRenderVertices(renderer, count * sz, 0, &cmd->data.draw.first);
- if (!verts) {
- return -1;
- }
- if (texture) {
- texturedata = (GL_TextureData *)texture->driverdata;
- }
- cmd->data.draw.count = count;
- size_indices = indices ? size_indices : 0;
- for (i = 0; i < count; i++) {
- int j;
- float *xy_;
- SDL_FColor *col_;
- if (size_indices == 4) {
- j = ((const Uint32 *)indices)[i];
- } else if (size_indices == 2) {
- j = ((const Uint16 *)indices)[i];
- } else if (size_indices == 1) {
- j = ((const Uint8 *)indices)[i];
- } else {
- j = i;
- }
- xy_ = (float *)((char *)xy + j * xy_stride);
- *(verts++) = xy_[0] * scale_x;
- *(verts++) = xy_[1] * scale_y;
- col_ = (SDL_FColor *)((char *)color + j * color_stride);
- *(verts++) = col_->r * color_scale;
- *(verts++) = col_->g * color_scale;
- *(verts++) = col_->b * color_scale;
- *(verts++) = col_->a;
- if (texture) {
- float *uv_ = (float *)((char *)uv + j * uv_stride);
- *(verts++) = uv_[0] * texturedata->texw;
- *(verts++) = uv_[1] * texturedata->texh;
- }
- }
- return 0;
- }
- static int SetDrawState(GL_RenderData *data, const SDL_RenderCommand *cmd, const GL_Shader shader, const float *shader_params)
- {
- const SDL_BlendMode blend = cmd->data.draw.blend;
- SDL_bool vertex_array;
- SDL_bool color_array;
- SDL_bool texture_array;
- if (data->drawstate.viewport_dirty) {
- const SDL_bool istarget = data->drawstate.target != NULL;
- const SDL_Rect *viewport = &data->drawstate.viewport;
- data->glMatrixMode(GL_PROJECTION);
- data->glLoadIdentity();
- data->glViewport(viewport->x,
- istarget ? viewport->y : (data->drawstate.drawableh - viewport->y - viewport->h),
- viewport->w, viewport->h);
- if (viewport->w && viewport->h) {
- data->glOrtho((GLdouble)0, (GLdouble)viewport->w,
- (GLdouble)(istarget ? 0 : viewport->h),
- (GLdouble)(istarget ? viewport->h : 0),
- 0.0, 1.0);
- }
- data->glMatrixMode(GL_MODELVIEW);
- data->drawstate.viewport_dirty = SDL_FALSE;
- }
- if (data->drawstate.cliprect_enabled_dirty) {
- if (!data->drawstate.cliprect_enabled) {
- data->glDisable(GL_SCISSOR_TEST);
- } else {
- data->glEnable(GL_SCISSOR_TEST);
- }
- data->drawstate.cliprect_enabled_dirty = SDL_FALSE;
- }
- if (data->drawstate.cliprect_enabled && data->drawstate.cliprect_dirty) {
- const SDL_Rect *viewport = &data->drawstate.viewport;
- const SDL_Rect *rect = &data->drawstate.cliprect;
- data->glScissor(viewport->x + rect->x,
- data->drawstate.target ? viewport->y + rect->y : data->drawstate.drawableh - viewport->y - rect->y - rect->h,
- rect->w, rect->h);
- data->drawstate.cliprect_dirty = SDL_FALSE;
- }
- if (blend != data->drawstate.blend) {
- if (blend == SDL_BLENDMODE_NONE) {
- data->glDisable(GL_BLEND);
- } else {
- data->glEnable(GL_BLEND);
- data->glBlendFuncSeparate(GetBlendFunc(SDL_GetBlendModeSrcColorFactor(blend)),
- GetBlendFunc(SDL_GetBlendModeDstColorFactor(blend)),
- GetBlendFunc(SDL_GetBlendModeSrcAlphaFactor(blend)),
- GetBlendFunc(SDL_GetBlendModeDstAlphaFactor(blend)));
- data->glBlendEquation(GetBlendEquation(SDL_GetBlendModeColorOperation(blend)));
- }
- data->drawstate.blend = blend;
- }
- if (data->shaders &&
- (shader != data->drawstate.shader || shader_params != data->drawstate.shader_params)) {
- GL_SelectShader(data->shaders, shader, shader_params);
- data->drawstate.shader = shader;
- data->drawstate.shader_params = shader_params;
- }
- if (data->drawstate.texturing_dirty || ((cmd->data.draw.texture != NULL) != data->drawstate.texturing)) {
- if (!cmd->data.draw.texture) {
- data->glDisable(data->textype);
- data->drawstate.texturing = SDL_FALSE;
- } else {
- data->glEnable(data->textype);
- data->drawstate.texturing = SDL_TRUE;
- }
- data->drawstate.texturing_dirty = SDL_FALSE;
- }
- vertex_array = cmd->command == SDL_RENDERCMD_DRAW_POINTS || cmd->command == SDL_RENDERCMD_DRAW_LINES || cmd->command == SDL_RENDERCMD_GEOMETRY;
- color_array = cmd->command == SDL_RENDERCMD_GEOMETRY;
- texture_array = cmd->data.draw.texture != NULL;
- if (vertex_array != data->drawstate.vertex_array) {
- if (vertex_array) {
- data->glEnableClientState(GL_VERTEX_ARRAY);
- } else {
- data->glDisableClientState(GL_VERTEX_ARRAY);
- }
- data->drawstate.vertex_array = vertex_array;
- }
- if (color_array != data->drawstate.color_array) {
- if (color_array) {
- data->glEnableClientState(GL_COLOR_ARRAY);
- } else {
- data->glDisableClientState(GL_COLOR_ARRAY);
- }
- data->drawstate.color_array = color_array;
- }
- /* This is a little awkward but should avoid texcoord arrays getting into
- a bad state if the application is manually binding textures */
- if (texture_array != data->drawstate.texture_array) {
- if (texture_array) {
- data->glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- } else {
- data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- }
- data->drawstate.texture_array = texture_array;
- }
- return 0;
- }
- static int SetCopyState(GL_RenderData *data, const SDL_RenderCommand *cmd)
- {
- SDL_Texture *texture = cmd->data.draw.texture;
- const GL_TextureData *texturedata = (GL_TextureData *)texture->driverdata;
- SetDrawState(data, cmd, texturedata->shader, texturedata->shader_params);
- if (texture != data->drawstate.texture) {
- const GLenum textype = data->textype;
- #if SDL_HAVE_YUV
- if (texturedata->yuv) {
- if (data->GL_ARB_multitexture_supported) {
- data->glActiveTextureARB(GL_TEXTURE2_ARB);
- }
- data->glBindTexture(textype, texturedata->vtexture);
- if (data->GL_ARB_multitexture_supported) {
- data->glActiveTextureARB(GL_TEXTURE1_ARB);
- }
- data->glBindTexture(textype, texturedata->utexture);
- }
- if (texturedata->nv12) {
- if (data->GL_ARB_multitexture_supported) {
- data->glActiveTextureARB(GL_TEXTURE1_ARB);
- }
- data->glBindTexture(textype, texturedata->utexture);
- }
- #endif
- if (data->GL_ARB_multitexture_supported) {
- data->glActiveTextureARB(GL_TEXTURE0_ARB);
- }
- data->glBindTexture(textype, texturedata->texture);
- data->drawstate.texture = texture;
- }
- return 0;
- }
- static void GL_InvalidateCachedState(SDL_Renderer *renderer)
- {
- GL_DrawStateCache *cache = &((GL_RenderData *)renderer->driverdata)->drawstate;
- cache->viewport_dirty = SDL_TRUE;
- cache->texture = NULL;
- cache->drawablew = 0;
- cache->drawableh = 0;
- cache->blend = SDL_BLENDMODE_INVALID;
- cache->shader = SHADER_INVALID;
- cache->cliprect_enabled_dirty = SDL_TRUE;
- cache->cliprect_dirty = SDL_TRUE;
- cache->texturing_dirty = SDL_TRUE;
- cache->vertex_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
- cache->color_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
- cache->texture_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
- cache->color_dirty = SDL_TRUE;
- cache->clear_color_dirty = SDL_TRUE;
- }
- static int GL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
- {
- /* !!! FIXME: it'd be nice to use a vertex buffer instead of immediate mode... */
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- if (GL_ActivateRenderer(renderer) < 0) {
- return -1;
- }
- data->drawstate.target = renderer->target;
- if (!data->drawstate.target) {
- int w, h;
- SDL_GetWindowSizeInPixels(renderer->window, &w, &h);
- if ((w != data->drawstate.drawablew) || (h != data->drawstate.drawableh)) {
- data->drawstate.viewport_dirty = SDL_TRUE; // if the window dimensions changed, invalidate the current viewport, etc.
- data->drawstate.cliprect_dirty = SDL_TRUE;
- data->drawstate.drawablew = w;
- data->drawstate.drawableh = h;
- }
- }
- #ifdef SDL_PLATFORM_MACOS
- // On macOS on older systems, the OpenGL view change and resize events aren't
- // necessarily synchronized, so just always reset it.
- // Workaround for: https://discourse.libsdl.org/t/sdl-2-0-22-prerelease/35306/6
- data->drawstate.viewport_dirty = SDL_TRUE;
- #endif
- while (cmd) {
- switch (cmd->command) {
- case SDL_RENDERCMD_SETDRAWCOLOR:
- {
- const float r = cmd->data.color.color.r * cmd->data.color.color_scale;
- const float g = cmd->data.color.color.g * cmd->data.color.color_scale;
- const float b = cmd->data.color.color.b * cmd->data.color.color_scale;
- const float a = cmd->data.color.color.a;
- if (data->drawstate.clear_color_dirty ||
- (r != data->drawstate.color.r) ||
- (g != data->drawstate.color.g) ||
- (b != data->drawstate.color.b) ||
- (a != data->drawstate.color.a)) {
- data->glColor4f(r, g, b, a);
- data->drawstate.color.r = r;
- data->drawstate.color.g = g;
- data->drawstate.color.b = b;
- data->drawstate.color.a = a;
- data->drawstate.color_dirty = SDL_FALSE;
- }
- break;
- }
- case SDL_RENDERCMD_SETVIEWPORT:
- {
- SDL_Rect *viewport = &data->drawstate.viewport;
- if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof(cmd->data.viewport.rect)) != 0) {
- SDL_copyp(viewport, &cmd->data.viewport.rect);
- data->drawstate.viewport_dirty = SDL_TRUE;
- data->drawstate.cliprect_dirty = SDL_TRUE;
- }
- break;
- }
- case SDL_RENDERCMD_SETCLIPRECT:
- {
- const SDL_Rect *rect = &cmd->data.cliprect.rect;
- if (data->drawstate.cliprect_enabled != cmd->data.cliprect.enabled) {
- data->drawstate.cliprect_enabled = cmd->data.cliprect.enabled;
- data->drawstate.cliprect_enabled_dirty = SDL_TRUE;
- }
- if (SDL_memcmp(&data->drawstate.cliprect, rect, sizeof(*rect)) != 0) {
- SDL_copyp(&data->drawstate.cliprect, rect);
- data->drawstate.cliprect_dirty = SDL_TRUE;
- }
- break;
- }
- case SDL_RENDERCMD_CLEAR:
- {
- const float r = cmd->data.color.color.r * cmd->data.color.color_scale;
- const float g = cmd->data.color.color.g * cmd->data.color.color_scale;
- const float b = cmd->data.color.color.b * cmd->data.color.color_scale;
- const float a = cmd->data.color.color.a;
- if (data->drawstate.clear_color_dirty ||
- (r != data->drawstate.clear_color.r) ||
- (g != data->drawstate.clear_color.g) ||
- (b != data->drawstate.clear_color.b) ||
- (a != data->drawstate.clear_color.a)) {
- data->glClearColor(r, g, b, a);
- data->drawstate.clear_color.r = r;
- data->drawstate.clear_color.g = g;
- data->drawstate.clear_color.b = b;
- data->drawstate.clear_color.a = a;
- data->drawstate.clear_color_dirty = SDL_FALSE;
- }
- if (data->drawstate.cliprect_enabled || data->drawstate.cliprect_enabled_dirty) {
- data->glDisable(GL_SCISSOR_TEST);
- data->drawstate.cliprect_enabled_dirty = data->drawstate.cliprect_enabled;
- }
- data->glClear(GL_COLOR_BUFFER_BIT);
- break;
- }
- case SDL_RENDERCMD_FILL_RECTS: /* unused */
- break;
- case SDL_RENDERCMD_COPY: /* unused */
- break;
- case SDL_RENDERCMD_COPY_EX: /* unused */
- break;
- case SDL_RENDERCMD_DRAW_LINES:
- {
- if (SetDrawState(data, cmd, SHADER_SOLID, NULL) == 0) {
- size_t count = cmd->data.draw.count;
- const GLfloat *verts = (GLfloat *)(((Uint8 *)vertices) + cmd->data.draw.first);
- /* SetDrawState handles glEnableClientState. */
- data->glVertexPointer(2, GL_FLOAT, sizeof(float) * 2, verts);
- if (count > 2) {
- /* joined lines cannot be grouped */
- data->glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)count);
- } else {
- /* let's group non joined lines */
- SDL_RenderCommand *finalcmd = cmd;
- SDL_RenderCommand *nextcmd = cmd->next;
- SDL_BlendMode thisblend = cmd->data.draw.blend;
- while (nextcmd) {
- const SDL_RenderCommandType nextcmdtype = nextcmd->command;
- if (nextcmdtype != SDL_RENDERCMD_DRAW_LINES) {
- break; /* can't go any further on this draw call, different render command up next. */
- } else if (nextcmd->data.draw.count != 2) {
- break; /* can't go any further on this draw call, those are joined lines */
- } else if (nextcmd->data.draw.blend != thisblend) {
- break; /* can't go any further on this draw call, different blendmode copy up next. */
- } else {
- finalcmd = nextcmd; /* we can combine copy operations here. Mark this one as the furthest okay command. */
- count += nextcmd->data.draw.count;
- }
- nextcmd = nextcmd->next;
- }
- data->glDrawArrays(GL_LINES, 0, (GLsizei)count);
- cmd = finalcmd; /* skip any copy commands we just combined in here. */
- }
- }
- break;
- }
- case SDL_RENDERCMD_DRAW_POINTS:
- case SDL_RENDERCMD_GEOMETRY:
- {
- /* as long as we have the same copy command in a row, with the
- same texture, we can combine them all into a single draw call. */
- SDL_Texture *thistexture = cmd->data.draw.texture;
- SDL_BlendMode thisblend = cmd->data.draw.blend;
- const SDL_RenderCommandType thiscmdtype = cmd->command;
- SDL_RenderCommand *finalcmd = cmd;
- SDL_RenderCommand *nextcmd = cmd->next;
- size_t count = cmd->data.draw.count;
- int ret;
- while (nextcmd) {
- const SDL_RenderCommandType nextcmdtype = nextcmd->command;
- if (nextcmdtype != thiscmdtype) {
- break; /* can't go any further on this draw call, different render command up next. */
- } else if (nextcmd->data.draw.texture != thistexture || nextcmd->data.draw.blend != thisblend) {
- break; /* can't go any further on this draw call, different texture/blendmode copy up next. */
- } else {
- finalcmd = nextcmd; /* we can combine copy operations here. Mark this one as the furthest okay command. */
- count += nextcmd->data.draw.count;
- }
- nextcmd = nextcmd->next;
- }
- if (thistexture) {
- ret = SetCopyState(data, cmd);
- } else {
- ret = SetDrawState(data, cmd, SHADER_SOLID, NULL);
- }
- if (ret == 0) {
- const GLfloat *verts = (GLfloat *)(((Uint8 *)vertices) + cmd->data.draw.first);
- int op = GL_TRIANGLES; /* SDL_RENDERCMD_GEOMETRY */
- if (thiscmdtype == SDL_RENDERCMD_DRAW_POINTS) {
- op = GL_POINTS;
- }
- if (thiscmdtype == SDL_RENDERCMD_DRAW_POINTS) {
- /* SetDrawState handles glEnableClientState. */
- data->glVertexPointer(2, GL_FLOAT, sizeof(float) * 2, verts);
- } else {
- /* SetDrawState handles glEnableClientState. */
- if (thistexture) {
- data->glVertexPointer(2, GL_FLOAT, sizeof(float) * 8, verts + 0);
- data->glColorPointer(4, GL_FLOAT, sizeof(float) * 8, verts + 2);
- data->glTexCoordPointer(2, GL_FLOAT, sizeof(float) * 8, verts + 6);
- } else {
- data->glVertexPointer(2, GL_FLOAT, sizeof(float) * 6, verts + 0);
- data->glColorPointer(4, GL_FLOAT, sizeof(float) * 6, verts + 2);
- }
- }
- data->glDrawArrays(op, 0, (GLsizei)count);
- /* Restore previously set color when we're done. */
- if (thiscmdtype != SDL_RENDERCMD_DRAW_POINTS) {
- const float r = data->drawstate.color.r;
- const float g = data->drawstate.color.g;
- const float b = data->drawstate.color.b;
- const float a = data->drawstate.color.a;
- data->glColor4f(r, g, b, a);
- }
- }
- cmd = finalcmd; /* skip any copy commands we just combined in here. */
- break;
- }
- case SDL_RENDERCMD_NO_OP:
- break;
- }
- cmd = cmd->next;
- }
- /* Turn off vertex array state when we're done, in case external code
- relies on it being off. */
- if (data->drawstate.vertex_array) {
- data->glDisableClientState(GL_VERTEX_ARRAY);
- data->drawstate.vertex_array = SDL_FALSE;
- }
- if (data->drawstate.color_array) {
- data->glDisableClientState(GL_COLOR_ARRAY);
- data->drawstate.color_array = SDL_FALSE;
- }
- if (data->drawstate.texture_array) {
- data->glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- data->drawstate.texture_array = SDL_FALSE;
- }
- return GL_CheckError("", renderer);
- }
- static SDL_Surface *GL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- SDL_PixelFormatEnum format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ARGB8888;
- GLint internalFormat;
- GLenum targetFormat, type;
- int w, h;
- SDL_Surface *surface;
- GL_ActivateRenderer(renderer);
- if (!convert_format(format, &internalFormat, &targetFormat, &type)) {
- SDL_SetError("Texture format %s not supported by OpenGL", SDL_GetPixelFormatName(format));
- return NULL;
- }
- surface = SDL_CreateSurface(rect->w, rect->h, format);
- if (!surface) {
- return NULL;
- }
- SDL_GetCurrentRenderOutputSize(renderer, &w, &h);
- data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
- data->glPixelStorei(GL_PACK_ROW_LENGTH, (surface->pitch / SDL_BYTESPERPIXEL(format)));
- data->glReadPixels(rect->x, renderer->target ? rect->y : (h - rect->y) - rect->h,
- rect->w, rect->h, targetFormat, type, surface->pixels);
- if (GL_CheckError("glReadPixels()", renderer) < 0) {
- SDL_DestroySurface(surface);
- return NULL;
- }
- /* Flip the rows to be top-down if necessary */
- if (!renderer->target) {
- SDL_bool isstack;
- int length = rect->w * SDL_BYTESPERPIXEL(format);
- Uint8 *src = (Uint8 *)surface->pixels + (rect->h - 1) * surface->pitch;
- Uint8 *dst = (Uint8 *)surface->pixels;
- Uint8 *tmp = SDL_small_alloc(Uint8, length, &isstack);
- int rows = rect->h / 2;
- while (rows--) {
- SDL_memcpy(tmp, dst, length);
- SDL_memcpy(dst, src, length);
- SDL_memcpy(src, tmp, length);
- dst += surface->pitch;
- src -= surface->pitch;
- }
- SDL_small_free(tmp, isstack);
- }
- return surface;
- }
- static int GL_RenderPresent(SDL_Renderer *renderer)
- {
- GL_ActivateRenderer(renderer);
- return SDL_GL_SwapWindow(renderer->window);
- }
- static void GL_DestroyTexture(SDL_Renderer *renderer, SDL_Texture *texture)
- {
- GL_RenderData *renderdata = (GL_RenderData *)renderer->driverdata;
- GL_TextureData *data = (GL_TextureData *)texture->driverdata;
- GL_ActivateRenderer(renderer);
- if (renderdata->drawstate.texture == texture) {
- renderdata->drawstate.texture = NULL;
- }
- if (renderdata->drawstate.target == texture) {
- renderdata->drawstate.target = NULL;
- }
- if (!data) {
- return;
- }
- if (data->texture && !data->texture_external) {
- renderdata->glDeleteTextures(1, &data->texture);
- }
- #if SDL_HAVE_YUV
- if (data->yuv) {
- if (!data->utexture_external) {
- renderdata->glDeleteTextures(1, &data->utexture);
- }
- if (!data->vtexture_external) {
- renderdata->glDeleteTextures(1, &data->vtexture);
- }
- }
- if (data->nv12) {
- if (!data->utexture_external) {
- renderdata->glDeleteTextures(1, &data->utexture);
- }
- }
- #endif
- SDL_free(data->pixels);
- SDL_free(data);
- texture->driverdata = NULL;
- }
- static void GL_DestroyRenderer(SDL_Renderer *renderer)
- {
- GL_RenderData *data = (GL_RenderData *)renderer->driverdata;
- if (data) {
- if (data->context) {
- /* make sure we delete the right resources! */
- GL_ActivateRenderer(renderer);
- }
- GL_ClearErrors(renderer);
- if (data->GL_ARB_debug_output_supported) {
- PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC)SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
- /* Uh oh, we don't have a safe way of removing ourselves from the callback chain, if it changed after we set our callback. */
- /* For now, just always replace the callback with the original one */
- glDebugMessageCallbackARBFunc(data->next_error_callback, data->next_error_userparam);
- }
- if (data->shaders) {
- GL_DestroyShaderContext(data->shaders);
- }
- if (data->context) {
- while (data->framebuffers) {
- GL_FBOList *nextnode = data->framebuffers->next;
- /* delete the framebuffer object */
- data->glDeleteFramebuffersEXT(1, &data->framebuffers->FBO);
- GL_CheckError("", renderer);
- SDL_free(data->framebuffers);
- data->framebuffers = nextnode;
- }
- SDL_GL_DeleteContext(data->context);
- }
- SDL_free(data);
- }
- }
- static int GL_SetVSync(SDL_Renderer *renderer, const int vsync)
- {
- int retval;
- int interval = 0;
- retval = SDL_GL_SetSwapInterval(vsync);
- if (retval < 0) {
- return retval;
- }
- retval = SDL_GL_GetSwapInterval(&interval);
- if (retval < 0) {
- return retval;
- }
- if (interval != vsync) {
- return SDL_Unsupported();
- }
- return 0;
- }
- static int GL_CreateRenderer(SDL_Renderer *renderer, SDL_Window *window, SDL_PropertiesID create_props)
- {
- GL_RenderData *data = NULL;
- GLint value;
- SDL_WindowFlags window_flags;
- int profile_mask = 0, major = 0, minor = 0;
- SDL_bool changed_window = SDL_FALSE;
- const char *hint;
- SDL_bool non_power_of_two_supported = SDL_FALSE;
- SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile_mask);
- SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
- SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
- #ifndef SDL_VIDEO_VITA_PVR_OGL
- SDL_SyncWindow(window);
- window_flags = SDL_GetWindowFlags(window);
- if (!(window_flags & SDL_WINDOW_OPENGL) ||
- profile_mask == SDL_GL_CONTEXT_PROFILE_ES || major != RENDERER_CONTEXT_MAJOR || minor != RENDERER_CONTEXT_MINOR) {
- changed_window = SDL_TRUE;
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, 0);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, RENDERER_CONTEXT_MAJOR);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, RENDERER_CONTEXT_MINOR);
- if (SDL_RecreateWindow(window, (window_flags & ~(SDL_WINDOW_VULKAN | SDL_WINDOW_METAL)) | SDL_WINDOW_OPENGL) < 0) {
- goto error;
- }
- }
- #endif
- SDL_SetupRendererColorspace(renderer, create_props);
- if (renderer->output_colorspace != SDL_COLORSPACE_SRGB) {
- SDL_SetError("Unsupported output colorspace");
- goto error;
- }
- data = (GL_RenderData *)SDL_calloc(1, sizeof(*data));
- if (!data) {
- goto error;
- }
- renderer->WindowEvent = GL_WindowEvent;
- renderer->SupportsBlendMode = GL_SupportsBlendMode;
- renderer->CreateTexture = GL_CreateTexture;
- renderer->UpdateTexture = GL_UpdateTexture;
- #if SDL_HAVE_YUV
- renderer->UpdateTextureYUV = GL_UpdateTextureYUV;
- renderer->UpdateTextureNV = GL_UpdateTextureNV;
- #endif
- renderer->LockTexture = GL_LockTexture;
- renderer->UnlockTexture = GL_UnlockTexture;
- renderer->SetTextureScaleMode = GL_SetTextureScaleMode;
- renderer->SetRenderTarget = GL_SetRenderTarget;
- renderer->QueueSetViewport = GL_QueueNoOp;
- renderer->QueueSetDrawColor = GL_QueueNoOp;
- renderer->QueueDrawPoints = GL_QueueDrawPoints;
- renderer->QueueDrawLines = GL_QueueDrawLines;
- renderer->QueueGeometry = GL_QueueGeometry;
- renderer->InvalidateCachedState = GL_InvalidateCachedState;
- renderer->RunCommandQueue = GL_RunCommandQueue;
- renderer->RenderReadPixels = GL_RenderReadPixels;
- renderer->RenderPresent = GL_RenderPresent;
- renderer->DestroyTexture = GL_DestroyTexture;
- renderer->DestroyRenderer = GL_DestroyRenderer;
- renderer->SetVSync = GL_SetVSync;
- renderer->driverdata = data;
- GL_InvalidateCachedState(renderer);
- renderer->window = window;
- renderer->info.name = GL_RenderDriver.name;
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ARGB8888);
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_ABGR8888);
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_XRGB8888);
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_XBGR8888);
- data->context = SDL_GL_CreateContext(window);
- if (!data->context) {
- goto error;
- }
- if (SDL_GL_MakeCurrent(window, data->context) < 0) {
- SDL_GL_DeleteContext(data->context);
- goto error;
- }
- if (GL_LoadFunctions(data) < 0) {
- SDL_GL_DeleteContext(data->context);
- goto error;
- }
- #ifdef SDL_PLATFORM_MACOS
- /* Enable multi-threaded rendering */
- /* Disabled until Ryan finishes his VBO/PBO code...
- CGLEnable(CGLGetCurrentContext(), kCGLCEMPEngine);
- */
- #endif
- /* Check for debug output support */
- if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &value) == 0 &&
- (value & SDL_GL_CONTEXT_DEBUG_FLAG)) {
- data->debug_enabled = SDL_TRUE;
- }
- if (data->debug_enabled && SDL_GL_ExtensionSupported("GL_ARB_debug_output")) {
- PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackARBFunc = (PFNGLDEBUGMESSAGECALLBACKARBPROC)SDL_GL_GetProcAddress("glDebugMessageCallbackARB");
- data->GL_ARB_debug_output_supported = SDL_TRUE;
- data->glGetPointerv(GL_DEBUG_CALLBACK_FUNCTION_ARB, (GLvoid **)(char *)&data->next_error_callback);
- data->glGetPointerv(GL_DEBUG_CALLBACK_USER_PARAM_ARB, &data->next_error_userparam);
- glDebugMessageCallbackARBFunc(GL_HandleDebugMessage, renderer);
- /* Make sure our callback is called when errors actually happen */
- data->glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
- }
- hint = SDL_getenv("GL_ARB_texture_non_power_of_two");
- if (!hint || *hint != '0') {
- SDL_bool isGL2 = SDL_FALSE;
- const char *verstr = (const char *)data->glGetString(GL_VERSION);
- if (verstr) {
- char verbuf[16];
- char *ptr;
- SDL_strlcpy(verbuf, verstr, sizeof(verbuf));
- ptr = SDL_strchr(verbuf, '.');
- if (ptr) {
- *ptr = '\0';
- if (SDL_atoi(verbuf) >= 2) {
- isGL2 = SDL_TRUE;
- }
- }
- }
- if (isGL2 || SDL_GL_ExtensionSupported("GL_ARB_texture_non_power_of_two")) {
- non_power_of_two_supported = SDL_TRUE;
- }
- }
- data->textype = GL_TEXTURE_2D;
- if (non_power_of_two_supported) {
- data->GL_ARB_texture_non_power_of_two_supported = SDL_TRUE;
- data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
- SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, value);
- } else if (SDL_GL_ExtensionSupported("GL_ARB_texture_rectangle") ||
- SDL_GL_ExtensionSupported("GL_EXT_texture_rectangle")) {
- data->GL_ARB_texture_rectangle_supported = SDL_TRUE;
- data->textype = GL_TEXTURE_RECTANGLE_ARB;
- data->glGetIntegerv(GL_MAX_RECTANGLE_TEXTURE_SIZE_ARB, &value);
- SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, value);
- } else {
- data->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &value);
- SDL_SetNumberProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_MAX_TEXTURE_SIZE_NUMBER, value);
- }
- /* Check for multitexture support */
- if (SDL_GL_ExtensionSupported("GL_ARB_multitexture")) {
- data->glActiveTextureARB = (PFNGLACTIVETEXTUREARBPROC)SDL_GL_GetProcAddress("glActiveTextureARB");
- if (data->glActiveTextureARB) {
- data->GL_ARB_multitexture_supported = SDL_TRUE;
- data->glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &data->num_texture_units);
- }
- }
- /* Check for shader support */
- data->shaders = GL_CreateShaderContext();
- SDL_LogInfo(SDL_LOG_CATEGORY_RENDER, "OpenGL shaders: %s",
- data->shaders ? "ENABLED" : "DISABLED");
- #if SDL_HAVE_YUV
- /* We support YV12 textures using 3 textures and a shader */
- if (data->shaders && data->num_texture_units >= 3) {
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_YV12);
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_IYUV);
- }
- /* We support NV12 textures using 2 textures and a shader */
- if (data->shaders && data->num_texture_units >= 2) {
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV12);
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_NV21);
- }
- #endif
- #ifdef SDL_PLATFORM_MACOS
- SDL_AddSupportedTextureFormat(renderer, SDL_PIXELFORMAT_UYVY);
- #endif
- renderer->rect_index_order[0] = 0;
- renderer->rect_index_order[1] = 1;
- renderer->rect_index_order[2] = 3;
- renderer->rect_index_order[3] = 1;
- renderer->rect_index_order[4] = 3;
- renderer->rect_index_order[5] = 2;
- if (SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
- data->GL_EXT_framebuffer_object_supported = SDL_TRUE;
- data->glGenFramebuffersEXT = (PFNGLGENFRAMEBUFFERSEXTPROC)
- SDL_GL_GetProcAddress("glGenFramebuffersEXT");
- data->glDeleteFramebuffersEXT = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
- SDL_GL_GetProcAddress("glDeleteFramebuffersEXT");
- data->glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
- SDL_GL_GetProcAddress("glFramebufferTexture2DEXT");
- data->glBindFramebufferEXT = (PFNGLBINDFRAMEBUFFEREXTPROC)
- SDL_GL_GetProcAddress("glBindFramebufferEXT");
- data->glCheckFramebufferStatusEXT = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
- SDL_GL_GetProcAddress("glCheckFramebufferStatusEXT");
- } else {
- SDL_SetError("Can't create render targets, GL_EXT_framebuffer_object not available");
- SDL_GL_DeleteContext(data->context);
- goto error;
- }
- /* Set up parameters for rendering */
- data->glMatrixMode(GL_MODELVIEW);
- data->glLoadIdentity();
- data->glDisable(GL_DEPTH_TEST);
- data->glDisable(GL_CULL_FACE);
- data->glDisable(GL_SCISSOR_TEST);
- data->glDisable(data->textype);
- data->glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- data->glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
- /* This ended up causing video discrepancies between OpenGL and Direct3D */
- /* data->glEnable(GL_LINE_SMOOTH); */
- data->drawstate.color.r = 1.0f;
- data->drawstate.color.g = 1.0f;
- data->drawstate.color.b = 1.0f;
- data->drawstate.color.a = 1.0f;
- data->drawstate.clear_color.r = 1.0f;
- data->drawstate.clear_color.g = 1.0f;
- data->drawstate.clear_color.b = 1.0f;
- data->drawstate.clear_color.a = 1.0f;
- return 0;
- error:
- SDL_free(data);
- if (changed_window) {
- /* Uh oh, better try to put it back... */
- char *error = SDL_strdup(SDL_GetError());
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profile_mask);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
- SDL_RecreateWindow(window, window_flags);
- SDL_SetError("%s", error);
- SDL_free(error);
- }
- return -1;
- }
- SDL_RenderDriver GL_RenderDriver = {
- GL_CreateRenderer, "opengl"
- };
- #endif /* SDL_VIDEO_RENDER_OGL */
|