From 2d364b5bd17af09ef1bd1f3239e78b93e9f4c166 Mon Sep 17 00:00:00 2001 From: Matt Borgerson Date: Thu, 12 Mar 2020 02:28:44 -0700 Subject: [PATCH] nv2a: Add SDL-based offscreen GL context interface --- hw/xbox/nv2a/gl/gloffscreen_sdl.c | 104 ++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 hw/xbox/nv2a/gl/gloffscreen_sdl.c diff --git a/hw/xbox/nv2a/gl/gloffscreen_sdl.c b/hw/xbox/nv2a/gl/gloffscreen_sdl.c new file mode 100644 index 0000000000..0d45501735 --- /dev/null +++ b/hw/xbox/nv2a/gl/gloffscreen_sdl.c @@ -0,0 +1,104 @@ +/* + * Offscreen OpenGL abstraction layer -- SDL based + * + * Copyright (c) 2018 Matt Borgerson + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include +#include +#include +#include + +#include "gloffscreen.h" + +#include +#include + +struct _GloContext { + SDL_Window *window; + SDL_GLContext gl_context; +}; + +/* Create an OpenGL context */ +GloContext *glo_context_create(void) +{ + fprintf(stderr, "%s\n", __func__); + + GloContext *context = (GloContext *)malloc(sizeof(GloContext)); + assert(context != NULL); + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + + // Initialize rendering context + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute( + SDL_GL_CONTEXT_PROFILE_MASK, + SDL_GL_CONTEXT_PROFILE_CORE); + + // Create main window + context->window = SDL_CreateWindow( + "SDL Offscreen Window", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 640, 480, + SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN); + if (context->window == NULL) { + fprintf(stderr, "%s: Failed to create window\n", __func__); + SDL_Quit(); + exit(1); + } + + context->gl_context = SDL_GL_CreateContext(context->window); + if (context->gl_context == NULL) { + fprintf(stderr, "%s: Failed to create GL context\n", __func__); + SDL_DestroyWindow(context->window); + SDL_Quit(); + exit(1); + } + + glo_set_current(context); + + return context; +} + +/* Set current context */ +void glo_set_current(GloContext *context) +{ + if (context == NULL) { + SDL_GL_MakeCurrent(NULL, NULL); + } else { + SDL_GL_MakeCurrent(context->window, context->gl_context); + } +} + +/* Destroy a previously created OpenGL context */ +void glo_context_destroy(GloContext *context) +{ + if (!context) return; + glo_set_current(NULL); + free(context); +}