Merge pull request #1382 from ts-korhonen/opengl

OpenGL renderer a.k.a. shader support
This commit is contained in:
Miran Grča
2021-04-18 00:17:53 +02:00
committed by GitHub
15 changed files with 4537 additions and 12 deletions

View File

@@ -93,7 +93,7 @@ extern char *plat_get_basename(const char *path);
extern void plat_get_dirname(char *dest, const char *path);
extern char *plat_get_filename(char *s);
extern char *plat_get_extension(char *s);
extern void plat_append_filename(char *dest, char *s1, char *s2);
extern void plat_append_filename(char *dest, const char *s1, const char *s2);
extern void plat_put_backslash(char *s);
extern void plat_path_slash(char *path);
extern int plat_path_abs(char *path);

View File

@@ -306,8 +306,9 @@
#define IDM_VID_SDL_SW 40050
#define IDM_VID_SDL_HW 40051
#define IDM_VID_SDL_OPENGL 40052
#define IDM_VID_OPENGL_CORE 40053
#ifdef USE_VNC
#define IDM_VID_VNC 40053
#define IDM_VID_VNC 40054
#endif
#define IDM_VID_SCALE_1X 40055
#define IDM_VID_SCALE_2X 40056

View File

@@ -85,9 +85,9 @@ DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
#define WM_HAS_SHUTDOWN 0x8897
#ifdef USE_VNC
#define RENDERERS_NUM 4
#define RENDERERS_NUM 5
#else
#define RENDERERS_NUM 3
#define RENDERERS_NUM 4
#endif

View File

@@ -0,0 +1,27 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box distribution.
*
* Header file for OpenGL rendering module
*
* Authors: Teemu Korhonen
*
* Copyright 2021 Teemu Korhonen
*/
#ifndef WIN_OPENGL_H
#define WIN_OPENGL_H
#define UNICODE
#include <Windows.h>
extern int opengl_init(HWND hwnd);
extern int opengl_pause(void);
extern void opengl_close(void);
extern void opengl_set_fs(int fs);
extern void opengl_resize(int w, int h);
#endif /*!WIN_OPENGL_H*/

View File

@@ -0,0 +1,23 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box distribution.
*
* Header file for shader file parser.
*
* Authors: Teemu Korhonen
*
* Copyright 2021 Teemu Korhonen
*/
#ifndef WIN_OPENGL_GLSLP_H
#define WIN_OPENGL_GLSLP_H
#include <glad/glad.h>
GLuint load_custom_shaders();
GLuint load_default_shaders();
#endif /*!WIN_OPENGL_GLSLP_H*/

View File

@@ -0,0 +1,290 @@
#ifndef __khrplatform_h_
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are 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 Materials.
**
** THE MATERIALS ARE 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
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APICALL
*-------------------------------------------------------------------------
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#elif defined (__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIENTRY
*-------------------------------------------------------------------------
* This follows the return type of the function and precedes the function
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIATTRIBUTES
*-------------------------------------------------------------------------
* This follows the closing parenthesis of the function prototype arguments.
*/
#if defined (__ARMCC_2__)
#define KHRONOS_APIATTRIBUTES __softfp
#else
#define KHRONOS_APIATTRIBUTES
#endif
/*-------------------------------------------------------------------------
* basic type definitions
*-----------------------------------------------------------------------*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
/*
* Using <stdint.h>
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__VMS ) || defined(__sgi)
/*
* Using <inttypes.h>
*/
#include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__sun__) || defined(__digital__)
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
#else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif 0
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#define KHRONOS_SUPPORT_INT64 0
#define KHRONOS_SUPPORT_FLOAT 0
#else
/*
* Generic fallback
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#endif
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef _WIN64
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
* Dummy value used to pad enum types to 32 bits.
*/
#ifndef KHRONOS_MAX_ENUM
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
#endif
/*
* Enumerated boolean type
*
* Values other than zero should be considered to be true. Therefore
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum {
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */

2129
src/include/glad/glad.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -66,6 +66,9 @@ BEGIN
MENUITEM "&SDL (Software)", IDM_VID_SDL_SW
MENUITEM "SDL (&Hardware)", IDM_VID_SDL_HW
MENUITEM "SDL (&OpenGL)", IDM_VID_SDL_OPENGL
#ifdef DEV_BRANCH
MENUITEM "Open&GL (3.3 Core)", IDM_VID_OPENGL_CORE
#endif
#ifdef USE_VNC
MENUITEM "&VNC", IDM_VID_VNC
#endif

View File

@@ -20,7 +20,7 @@ add_library(plat OBJECT win.c win_dynld.c win_thread.c win_cdrom.c
add_library(ui OBJECT win_ui.c win_stbar.c win_sdl.c win_dialog.c win_about.c
win_settings.c win_devconf.c win_snd_gain.c win_specify_dim.c win_new_floppy.c
win_jsconf.c win_media_menu.c 86Box.rc)
win_jsconf.c win_media_menu.c 86Box.rc glad.c win_opengl.c win_opengl_glslp.c)
if(MSVC)
# MSVC complains when we include the manifest from 86Box.rc...

View File

@@ -451,7 +451,7 @@ ifeq ($(WX), y)
UIOBJ := wx_main.o wx_ui.o wx_stbar.o wx_render.o
else
UIOBJ := win_ui.o win_stbar.o \
win_sdl.o \
win_sdl.o win_opengl.o win_opengl_glslp.o glad.o \
win_dialog.o win_about.o \
win_settings.o win_devconf.o win_snd_gain.o win_specify_dim.o \
win_new_floppy.o win_jsconf.o win_media_menu.o

1140
src/win/glad.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -51,6 +51,7 @@
# include <86box/vnc.h>
#endif
#include <86box/win_sdl.h>
#include <86box/win_opengl.h>
#include <86box/win.h>
#include <86box/version.h>
#ifdef MTR_ENABLED
@@ -99,11 +100,16 @@ static const struct {
void (*enable)(int enable);
void (*set_fs)(int fs);
} vid_apis[RENDERERS_NUM] = {
{ "SDL_Software", 1, (int(*)(void*))sdl_inits, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs },
{ "SDL_Hardware", 1, (int(*)(void*))sdl_inith, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs },
{ "SDL_Software", 1, (int(*)(void*))sdl_inits, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs },
{ "SDL_Hardware", 1, (int(*)(void*))sdl_inith, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs },
{ "SDL_OpenGL", 1, (int(*)(void*))sdl_initho, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs }
#ifdef DEV_BRANCH
,{ "OpenGL_Core", 1, (int(*)(void*))opengl_init, opengl_close, opengl_resize, opengl_pause, NULL, opengl_set_fs}
#else
,{ "OpenGL_Core", 1, (int(*)(void*))sdl_initho, sdl_close, NULL, sdl_pause, sdl_enable, sdl_set_fs } /* fall back to SDL_OpenGL */
#endif
#ifdef USE_VNC
,{ "VNC", 0, vnc_init, vnc_close, vnc_resize, vnc_pause, NULL, NULL }
,{ "VNC", 0, vnc_init, vnc_close, vnc_resize, vnc_pause, NULL, NULL }
#endif
};
@@ -777,7 +783,7 @@ plat_get_extension(char *s)
void
plat_append_filename(char *dest, char *s1, char *s2)
plat_append_filename(char *dest, const char *s1, const char *s2)
{
strcpy(dest, s1);
plat_path_slash(dest);
@@ -922,9 +928,17 @@ plat_vidapi_name(int api)
case 2:
name = "sdl_opengl";
break;
#ifdef USE_VNC
#ifdef DEV_BRANCH
case 3:
name = "opengl_core";
break;
#else
case 3:
name = "sdl_opengl"; /* fall back to SDL_OpenGL */
break;
#endif
#ifdef USE_VNC
case 4:
name = "vnc";
break;
#endif

659
src/win/win_opengl.c Normal file
View File

@@ -0,0 +1,659 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box distribution.
*
* Rendering module for OpenGL
*
* TODO: More shader features
* - scaling
* - multipass
* - previous frames
* (UI) options
* More error handling
*
* Authors: Teemu Korhonen
*
* Copyright 2021 Teemu Korhonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#define UNICODE
#include <Windows.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <glad/glad.h>
#include <stdlib.h>
#include <stdint.h>
#include <86box/86box.h>
#include <86box/plat.h>
#include <86box/video.h>
#include <86box/win.h>
#include <86box/win_opengl.h>
#include <86box/win_opengl_glslp.h>
static const int INIT_WIDTH = 640;
static const int INIT_HEIGHT = 400;
/* Option; Target framerate: Sync with emulation / 25 fps / 30 fps / 50 fps / 60 fps / 75 fps */
static const int SYNC_WITH_BLITTER = 1;
static const int TARGET_FRAMETIME = 13;
/* Option; Vsync: Off / On */
static const int VSYNC = 0;
/**
* @brief A dedicated OpenGL thread.
* OpenGL context's don't handle multiple threads well.
*/
static thread_t* thread = NULL;
/**
* @brief A window usable with an OpenGL context
*/
static SDL_Window* window = NULL;
/**
* @brief SDL window handle
*/
static HWND window_hwnd = NULL;
/**
* @brief Parent window handle (hwndRender from win_ui)
*/
static HWND parent = NULL;
/**
* @brief Events listened in OpenGL thread.
*/
static union
{
struct
{
HANDLE closing;
HANDLE resize;
HANDLE blit_waiting;
};
HANDLE asArray[3];
} sync_objects = { 0 };
/**
* @brief Signal from OpenGL thread that it's done with video buffer.
*/
static HANDLE blit_done = NULL;
/**
* @brief Blit event parameters.
*/
static volatile struct
{
int x, y, y1, y2, w, h, resized;
} blit_info = { 0 };
/**
* @brief Resize event parameters.
*/
static volatile struct
{
int width, height, fullscreen, scaling_mode;
} resize_info = { 0 };
/**
* @brief Identifiers to OpenGL objects and uniforms.
*/
typedef struct
{
GLuint vertexArrayID;
GLuint vertexBufferID;
GLuint textureID;
GLuint shader_progID;
/* Uniforms */
GLint input_size;
GLint output_size;
GLint texture_size;
GLint frame_count;
} gl_identifiers;
/**
* @brief Userdata to pass onto windows message hook
*/
typedef struct
{
HWND window;
int* fullscreen;
} winmessage_data;
/**
* @brief Set or unset OpenGL context window as a child window.
*
* Modifies the window style and sets the parent window.
* WS_EX_NOACTIVATE keeps the window from stealing input focus.
*/
static void set_parent_binding(int enable)
{
long style = GetWindowLong(window_hwnd, GWL_STYLE);
long ex_style = GetWindowLong(window_hwnd, GWL_EXSTYLE);
if (enable)
{
style |= WS_CHILD;
ex_style |= WS_EX_NOACTIVATE;
}
else
{
style &= ~WS_CHILD;
ex_style &= ~WS_EX_NOACTIVATE;
}
SetWindowLong(window_hwnd, GWL_STYLE, style);
SetWindowLong(window_hwnd, GWL_EXSTYLE, ex_style);
SetParent(window_hwnd, enable ? parent : NULL);
}
/**
* @brief Windows message handler for SDL Windows.
* @param userdata winmessage_data
* @param hWnd
* @param message
* @param wParam
* @param lParam
*/
static void winmessage_hook(void* userdata, void* hWnd, unsigned int message, Uint64 wParam, Sint64 lParam)
{
winmessage_data* msg_data = (winmessage_data*)userdata;
/* Process only our window */
if (msg_data->window != hWnd || parent == NULL)
return;
switch (message)
{
case WM_LBUTTONUP:
case WM_LBUTTONDOWN:
case WM_MBUTTONUP:
case WM_MBUTTONDOWN:
if (!*msg_data->fullscreen)
{
/* Mouse events that enter and exit capture. */
PostMessage(parent, message, wParam, lParam);
}
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
if (*msg_data->fullscreen)
{
PostMessage(parent, message, wParam, lParam);
}
break;
case WM_INPUT:
if (*msg_data->fullscreen)
{
/* Raw input handler from win_ui.c : input_proc */
UINT size = 0;
PRAWINPUT raw = NULL;
/* Here we read the raw input data */
GetRawInputData((HRAWINPUT)(LPARAM)lParam, RID_INPUT, NULL, &size, sizeof(RAWINPUTHEADER));
raw = (PRAWINPUT)malloc(size);
if (GetRawInputData((HRAWINPUT)(LPARAM)lParam, RID_INPUT, raw, &size, sizeof(RAWINPUTHEADER)) == size) {
switch (raw->header.dwType)
{
case RIM_TYPEKEYBOARD:
keyboard_handle(raw);
break;
case RIM_TYPEMOUSE:
win_mouse_handle(raw);
break;
case RIM_TYPEHID:
win_joystick_handle(raw);
break;
}
}
free(raw);
}
break;
}
}
/**
* @brief Initialize OpenGL context
* @return Identifiers
*/
static gl_identifiers initialize_glcontext()
{
/* Vertex, texture 2d coordinates and color (white) making a quad as triangle strip */
static const GLfloat surface[] = {
-1.f, 1.f, 0.f, 0.f, 1.f, 1.f, 1.f, 1.f,
1.f, 1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f,
-1.f, -1.f, 0.f, 1.f, 1.f, 1.f, 1.f, 1.f,
1.f, -1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f
};
gl_identifiers gl = { 0 };
glGenVertexArrays(1, &gl.vertexArrayID);
glBindVertexArray(gl.vertexArrayID);
glGenBuffers(1, &gl.vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, gl.vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(surface), surface, GL_STATIC_DRAW);
glGenTextures(1, &gl.textureID);
glBindTexture(GL_TEXTURE_2D, gl.textureID);
GLfloat border_color[] = { 0.f, 0.f, 0.f, 1.f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
glClearColor(0.f, 0.f, 0.f, 1.f);
gl.shader_progID = load_custom_shaders();
if (gl.shader_progID == 0)
gl.shader_progID = load_default_shaders();
glUseProgram(gl.shader_progID);
GLint vertex_coord = glGetAttribLocation(gl.shader_progID, "VertexCoord");
if (vertex_coord != -1)
{
glEnableVertexAttribArray(vertex_coord);
glVertexAttribPointer(vertex_coord, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
}
GLint tex_coord = glGetAttribLocation(gl.shader_progID, "TexCoord");
if (tex_coord != -1)
{
glEnableVertexAttribArray(tex_coord);
glVertexAttribPointer(tex_coord, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
}
GLint color = glGetAttribLocation(gl.shader_progID, "Color");
if (color != -1)
{
glEnableVertexAttribArray(color);
glVertexAttribPointer(color, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(4 * sizeof(GLfloat)));
}
GLint mvp_matrix = glGetUniformLocation(gl.shader_progID, "MVPMatrix");
if (mvp_matrix != -1)
{
static const GLfloat mvp[] = {
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f
};
glUniformMatrix4fv(mvp_matrix, 1, GL_FALSE, mvp);
}
GLint frame_direction = glGetUniformLocation(gl.shader_progID, "FrameDirection");
if (frame_direction != -1)
glUniform1i(frame_direction, 1); /* always forward */
gl.input_size = glGetUniformLocation(gl.shader_progID, "InputSize");
gl.output_size = glGetUniformLocation(gl.shader_progID, "OutputSize");
gl.texture_size = glGetUniformLocation(gl.shader_progID, "TextureSize");
gl.frame_count = glGetUniformLocation(gl.shader_progID, "FrameCount");
return gl;
}
/**
* @brief Clean up OpenGL context
* @param Identifiers from initialize
*/
static void finalize_glcontext(gl_identifiers gl)
{
glDeleteProgram(gl.shader_progID);
glDeleteTextures(1, &gl.textureID);
glDeleteBuffers(1, &gl.vertexBufferID);
glDeleteVertexArrays(1, &gl.vertexArrayID);
}
/**
* @brief Renders a frame and swaps the buffer
* @param gl Identifiers from initialize
*/
static void render_and_swap(gl_identifiers gl)
{
static int frame_counter = 0;
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
SDL_GL_SwapWindow(window);
if (gl.frame_count != -1)
glUniform1i(gl.frame_count, frame_counter = (frame_counter + 1) & 1023);
}
/**
* @brief Main OpenGL thread proc.
*
* OpenGL context should be accessed only from this single thread.
* Events are used to synchronize communication.
*/
static void opengl_main(void* param)
{
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); /* Is this actually doing anything...? */
window = SDL_CreateWindow("86Box OpenGL Renderer", 0, 0, resize_info.width, resize_info.height, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
SDL_GL_SetSwapInterval(VSYNC);
/* Keep track of certain parameters, only changed in this thread to avoid race conditions */
int fullscreen = resize_info.fullscreen, video_width = INIT_WIDTH, video_height = INIT_HEIGHT;
SDL_SysWMinfo wmi = { 0 };
SDL_VERSION(&wmi.version);
SDL_GetWindowWMInfo(window, &wmi);
if (wmi.subsystem == SDL_SYSWM_WINDOWS)
window_hwnd = wmi.info.win.window;
/* Pass window handle and full screen mode to windows message hook */
winmessage_data msg_data = (winmessage_data){ window_hwnd, &fullscreen };
SDL_SetWindowsMessageHook(winmessage_hook, &msg_data);
if (!fullscreen)
set_parent_binding(1);
else
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_GLContext context = SDL_GL_CreateContext(window);
gladLoadGLLoader(SDL_GL_GetProcAddress);
gl_identifiers gl = initialize_glcontext();
if (gl.frame_count != -1)
glUniform1i(gl.frame_count, 0);
uint32_t last_swap = -TARGET_FRAMETIME;
/* Render loop */
int closing = 0;
while (!closing)
{
if (SYNC_WITH_BLITTER)
render_and_swap(gl);
DWORD wait_result = WAIT_TIMEOUT;
do
{
if (!SYNC_WITH_BLITTER)
{
uint32_t ticks = plat_get_ticks();
if (ticks - last_swap > TARGET_FRAMETIME)
{
render_and_swap(gl);
last_swap = ticks;
}
}
SDL_Event event;
/* Handle SDL_Window events */
while (SDL_PollEvent(&event)) { /* No need for actual handlers, but message queue must be processed. */ }
/* Keep cursor hidden in full screen and mouse capture */
int show_cursor = !(fullscreen || !!mouse_capture);
if (SDL_ShowCursor(-1) != show_cursor)
SDL_ShowCursor(show_cursor);
/* Wait for synchronized events for 1ms before going back to window events */
wait_result = WaitForMultipleObjects(sizeof(sync_objects) / sizeof(HANDLE), sync_objects.asArray, FALSE, 1);
} while (wait_result == WAIT_TIMEOUT);
HANDLE sync_event = sync_objects.asArray[wait_result - WAIT_OBJECT_0];
if (sync_event == sync_objects.closing)
{
closing = 1;
}
else if (sync_event == sync_objects.blit_waiting)
{
/* Resize the texture */
if (blit_info.resized)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, blit_info.w, blit_info.h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
video_width = blit_info.w;
video_height = blit_info.h;
if (fullscreen)
SetEvent(sync_objects.resize);
}
/* Transfer video buffer to texture */
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, blit_info.y1, blit_info.w, blit_info.y2 - blit_info.y1, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, &(render_buffer->dat)[blit_info.y1 * blit_info.w]);
/* Signal that we're done with the video buffer */
SetEvent(blit_done);
/* Update uniforms */
if (gl.input_size != -1)
glUniform2f(gl.input_size, video_width, video_height);
if (gl.texture_size != -1)
glUniform2f(gl.texture_size, video_width, video_height);
}
else if (sync_event == sync_objects.resize)
{
if (fullscreen != resize_info.fullscreen)
{
fullscreen = resize_info.fullscreen;
set_parent_binding(!fullscreen);
SDL_SetWindowFullscreen(window, fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
if (fullscreen)
{
SetForegroundWindow(window_hwnd);
SetFocus(window_hwnd);
}
}
if (fullscreen)
{
int width, height, pad_x = 0, pad_y = 0, px_size = 1;
float ratio = 0;
const float ratio43 = 4.f / 3.f;
SDL_GetWindowSize(window, &width, &height);
if (video_width > 0 && video_height > 0)
{
switch (resize_info.scaling_mode)
{
case FULLSCR_SCALE_INT:
px_size = max(min(width / video_width, height / video_height), 1);
pad_x = width - (video_width * px_size);
pad_y = height - (video_height * px_size);
break;
case FULLSCR_SCALE_KEEPRATIO:
ratio = (float)video_width / (float)video_height;
case FULLSCR_SCALE_43:
if (ratio == 0)
ratio = ratio43;
if (ratio < ((float)width / (float)height))
pad_x = width - (int)roundf((float)height * ratio);
else
pad_y = height - (int)roundf((float)width / ratio);
break;
case FULLSCR_SCALE_FULL:
default:
break;
}
}
glViewport(pad_x / 2, pad_y / 2, width - pad_x, height - pad_y);
if (gl.output_size != -1)
glUniform2f(gl.output_size, width - pad_x, height - pad_y);
}
else
{
SDL_SetWindowSize(window, resize_info.width, resize_info.height);
/* SWP_NOZORDER is needed for child window and SDL doesn't enable it. */
SetWindowPos(window_hwnd, parent, 0, 0, resize_info.width, resize_info.height, SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
glViewport(0, 0, resize_info.width, resize_info.height);
if (gl.output_size != -1)
glUniform2f(gl.output_size, resize_info.width, resize_info.height);
}
}
}
finalize_glcontext(gl);
SDL_GL_DeleteContext(context);
set_parent_binding(0);
SDL_SetWindowsMessageHook(NULL, NULL);
SDL_DestroyWindow(window);
window = NULL;
}
static void opengl_blit(int x, int y, int y1, int y2, int w, int h)
{
if (y1 == y2 || h <= 0 || render_buffer == NULL || thread == NULL)
{
video_blit_complete();
return;
}
blit_info.resized = (w != blit_info.w || h != blit_info.h);
blit_info.x = x;
blit_info.y = y;
blit_info.y1 = y1;
blit_info.y2 = y2;
blit_info.w = w;
blit_info.h = h;
SignalObjectAndWait(sync_objects.blit_waiting, blit_done, INFINITE, FALSE);
video_blit_complete();
}
int opengl_init(HWND hwnd)
{
for (int i = 0; i < sizeof(sync_objects) / sizeof(HANDLE); i++)
sync_objects.asArray[i] = CreateEvent(NULL, FALSE, FALSE, NULL);
blit_done = CreateEvent(NULL, FALSE, FALSE, NULL);
parent = hwnd;
RECT parent_size;
GetWindowRect(parent, &parent_size);
resize_info.width = parent_size.right - parent_size.left;
resize_info.height = parent_size.bottom - parent_size.top;
resize_info.fullscreen = video_fullscreen & 1;
resize_info.scaling_mode = video_fullscreen_scale;
thread = thread_create(opengl_main, (void*)NULL);
atexit(opengl_close);
video_setblit(opengl_blit);
return 1;
}
int opengl_pause(void)
{
return 0;
}
void opengl_close(void)
{
if (thread == NULL)
return;
SetEvent(sync_objects.closing);
thread_wait(thread, -1);
memset((void*)&blit_info, 0, sizeof(blit_info));
SetEvent(blit_done);
thread = NULL;
CloseHandle(blit_done);
for (int i = 0; i < sizeof(sync_objects) / sizeof(HANDLE); i++)
{
CloseHandle(sync_objects.asArray[i]);
sync_objects.asArray[i] = (HANDLE)NULL;
}
parent = NULL;
}
void opengl_set_fs(int fs)
{
if (thread == NULL)
return;
resize_info.fullscreen = fs;
resize_info.scaling_mode = video_fullscreen_scale;
SetEvent(sync_objects.resize);
}
void opengl_resize(int w, int h)
{
if (thread == NULL)
return;
resize_info.width = w;
resize_info.height = h;
resize_info.scaling_mode = video_fullscreen_scale;
SetEvent(sync_objects.resize);
}

233
src/win/win_opengl_glslp.c Normal file
View File

@@ -0,0 +1,233 @@
/*
* 86Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus.
*
* This file is part of the 86Box distribution.
*
* File parser for .glslp and .glsl shader files
* in the format of libretro.
*
* TODO: Read .glslp files for multipass shaders and settings.
*
* Authors: Teemu Korhonen
*
* Copyright 2021 Teemu Korhonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <glad/glad.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <86box/86box.h>
#include <86box/plat.h>
#include <86box/win_opengl_glslp.h>
/**
* @brief Default vertex shader.
*/
static const GLchar* vertex_shader = "#version 330 core\n\
in vec2 VertexCoord;\n\
in vec2 TexCoord;\n\
out vec2 tex;\n\
void main(){\n\
gl_Position = vec4(VertexCoord, 0.0, 1.0);\n\
tex = TexCoord;\n\
}\n";
/**
* @brief Default fragment shader.
*/
static const GLchar* fragment_shader = "#version 330 core\n\
in vec2 tex;\n\
uniform sampler2D texsampler;\n\
void main() {\n\
gl_FragColor = texture(texsampler, tex);\n\
}\n";
/**
* @brief Reads a whole file into a null terminated string.
* @param Path Path to the file relative to executable path.
* @return Pointer to the string or NULL on error. Remember to free() after use.
*/
static char* read_file_to_string(const char* path)
{
char* full_path = (char*)malloc(sizeof(char) * (strlen(path) + strlen(exe_path) + 1));
plat_append_filename(full_path, exe_path, path);
FILE* file_handle = plat_fopen(full_path, "rb");
free(full_path);
if (file_handle != NULL)
{
/* get file size */
fseek(file_handle, 0, SEEK_END);
size_t file_size = (size_t)ftell(file_handle);
fseek(file_handle, 0, SEEK_SET);
/* read to buffer and close */
char* content = (char*)malloc(sizeof(char) * (file_size + 1));
if (!content)
return NULL;
size_t length = fread(content, sizeof(char), file_size, file_handle);
fclose(file_handle);
content[length] = 0;
return content;
}
return NULL;
}
/**
* @brief Compile custom shaders into a program.
* @return Shader program identifier.
*/
GLuint load_custom_shaders()
{
GLint status = GL_FALSE;
int info_log_length;
/* TODO: get path from config */
char* shader = read_file_to_string("shaders/shader.glsl");
if (shader != NULL)
{
int success = 1;
const char* vertex_sources[2] = { "#define VERTEX\n", shader };
const char* fragment_sources[2] = { "#define FRAGMENT\n", shader };
GLuint vertex_id = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertex_id, 2, vertex_sources, NULL);
glCompileShader(vertex_id);
glGetShaderiv(vertex_id, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
glGetShaderiv(vertex_id, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* info_log_text = (GLchar*)malloc(sizeof(GLchar) * info_log_length);
glGetShaderInfoLog(vertex_id, info_log_length, NULL, info_log_text);
/* TODO: error logging */
free(info_log_text);
success = 0;
}
glShaderSource(fragment_id, 2, fragment_sources, NULL);
glCompileShader(fragment_id);
glGetShaderiv(fragment_id, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
glGetShaderiv(fragment_id, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* info_log_text = (GLchar*)malloc(sizeof(GLchar) * info_log_length);
glGetShaderInfoLog(fragment_id, info_log_length, NULL, info_log_text);
/* TODO: error logging */
free(info_log_text);
success = 0;
}
free(shader);
GLuint prog_id = 0;
if (success)
{
prog_id = glCreateProgram();
glAttachShader(prog_id, vertex_id);
glAttachShader(prog_id, fragment_id);
glLinkProgram(prog_id);
glGetProgramiv(prog_id, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
glGetProgramiv(prog_id, GL_INFO_LOG_LENGTH, &info_log_length);
GLchar* info_log_text = (GLchar*)malloc(sizeof(GLchar) * info_log_length);
glGetProgramInfoLog(prog_id, info_log_length, NULL, info_log_text);
/* TODO: error logging */
free(info_log_text);
}
glDetachShader(prog_id, vertex_id);
glDetachShader(prog_id, fragment_id);
}
glDeleteShader(vertex_id);
glDeleteShader(fragment_id);
return prog_id;
}
return 0;
}
/**
* @brief Compile default shaders into a program.
* @return Shader program identifier.
*/
GLuint load_default_shaders()
{
GLuint vertex_id = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertex_id, 1, &vertex_shader, NULL);
glCompileShader(vertex_id);
glShaderSource(fragment_id, 1, &fragment_shader, NULL);
glCompileShader(fragment_id);
GLuint prog_id = glCreateProgram();
glAttachShader(prog_id, vertex_id);
glAttachShader(prog_id, fragment_id);
glLinkProgram(prog_id);
glDetachShader(prog_id, vertex_id);
glDetachShader(prog_id, fragment_id);
glDeleteShader(vertex_id);
glDeleteShader(fragment_id);
return prog_id;
}

View File

@@ -223,6 +223,9 @@ ResetAllMenus(void)
CheckMenuItem(menuMain, IDM_VID_SDL_SW, MF_UNCHECKED);
CheckMenuItem(menuMain, IDM_VID_SDL_HW, MF_UNCHECKED);
CheckMenuItem(menuMain, IDM_VID_SDL_OPENGL, MF_UNCHECKED);
#ifdef DEV_BRANCH
CheckMenuItem(menuMain, IDM_VID_OPENGL_CORE, MF_UNCHECKED);
#endif
#ifdef USE_VNC
CheckMenuItem(menuMain, IDM_VID_VNC, MF_UNCHECKED);
#endif
@@ -669,6 +672,9 @@ MainWindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
case IDM_VID_SDL_SW:
case IDM_VID_SDL_HW:
case IDM_VID_SDL_OPENGL:
#ifdef DEV_BRANCH
case IDM_VID_OPENGL_CORE:
#endif
#ifdef USE_VNC
case IDM_VID_VNC:
#endif