2020-09-16 20:14:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
2020-09-16 20:14:35 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-26 15:17:35 +00:00
|
|
|
// Renderer_GL21.cpp
|
|
|
|
//
|
|
|
|
// OpenGL 2.1 rendering functions.
|
|
|
|
//
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
#if defined(USE_OPENGL_21)
|
|
|
|
|
|
|
|
#include "Settings.h"
|
2020-08-30 20:19:37 +00:00
|
|
|
#include "Shader_GL21.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "renderers/Renderer.h"
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
{
|
2020-06-26 15:17:35 +00:00
|
|
|
static SDL_GLContext sdlContext = nullptr;
|
|
|
|
static GLuint whiteTexture = 0;
|
|
|
|
|
|
|
|
static GLenum convertBlendFactor(const Blend::Factor _blendFactor)
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
2020-06-26 15:17:35 +00:00
|
|
|
switch (_blendFactor) {
|
|
|
|
case Blend::ZERO: { return GL_ZERO; } break;
|
|
|
|
case Blend::ONE: { return GL_ONE; } break;
|
|
|
|
case Blend::SRC_COLOR: { return GL_SRC_COLOR; } break;
|
|
|
|
case Blend::ONE_MINUS_SRC_COLOR: { return GL_ONE_MINUS_SRC_COLOR; } break;
|
|
|
|
case Blend::SRC_ALPHA: { return GL_SRC_ALPHA; } break;
|
|
|
|
case Blend::ONE_MINUS_SRC_ALPHA: { return GL_ONE_MINUS_SRC_ALPHA; } break;
|
|
|
|
case Blend::DST_COLOR: { return GL_DST_COLOR; } break;
|
|
|
|
case Blend::ONE_MINUS_DST_COLOR: { return GL_ONE_MINUS_DST_COLOR; } break;
|
|
|
|
case Blend::DST_ALPHA: { return GL_DST_ALPHA; } break;
|
|
|
|
case Blend::ONE_MINUS_DST_ALPHA: { return GL_ONE_MINUS_DST_ALPHA; } break;
|
|
|
|
default: { return GL_ZERO; }
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GLenum convertTextureType(const Texture::Type _type)
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
2020-06-26 15:17:35 +00:00
|
|
|
switch (_type) {
|
|
|
|
case Texture::RGBA: { return GL_RGBA; } break;
|
|
|
|
case Texture::ALPHA: { return GL_ALPHA; } break;
|
|
|
|
default: { return GL_ZERO; }
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format on
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setupWindow()
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(__APPLE__)
|
2020-08-18 15:48:21 +00:00
|
|
|
// This is required on macOS, as the operating system will otherwise insist on using
|
|
|
|
// a newer OpenGL version which completely breaks the application.
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
|
|
|
|
|
|
|
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_DEPTH_SIZE, 24);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
}
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
bool createContext()
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2020-08-30 20:19:37 +00:00
|
|
|
bool missingExtension = false;
|
2020-06-26 15:17:35 +00:00
|
|
|
sdlContext = SDL_GL_CreateContext(getSDLWindow());
|
2020-08-18 15:48:21 +00:00
|
|
|
|
|
|
|
if (!sdlContext) {
|
|
|
|
LOG(LogError) << "Error creating OpenGL context. " << SDL_GetError();
|
2020-08-30 20:19:37 +00:00
|
|
|
return false;
|
2020-08-18 15:48:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-09-16 20:14:35 +00:00
|
|
|
glewInit();
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-09-16 20:14:35 +00:00
|
|
|
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_GL_MakeCurrent(getSDLWindow(), sdlContext);
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
std::string vendor =
|
|
|
|
glGetString(GL_VENDOR) ? reinterpret_cast<const char*>(glGetString(GL_VENDOR)) : "";
|
|
|
|
std::string renderer =
|
|
|
|
glGetString(GL_RENDERER) ? reinterpret_cast<const char*>(glGetString(GL_RENDERER)) : "";
|
|
|
|
std::string version =
|
|
|
|
glGetString(GL_VERSION) ? reinterpret_cast<const char*>(glGetString(GL_VERSION)) : "";
|
2020-06-26 15:17:35 +00:00
|
|
|
std::string extensions = glGetString(GL_EXTENSIONS) ?
|
2021-07-07 18:31:46 +00:00
|
|
|
reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)) :
|
|
|
|
"";
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2020-07-12 10:09:01 +00:00
|
|
|
LOG(LogInfo) << "GL vendor: " << vendor;
|
2020-06-26 15:17:35 +00:00
|
|
|
LOG(LogInfo) << "GL renderer: " << renderer;
|
2020-07-12 10:09:01 +00:00
|
|
|
LOG(LogInfo) << "GL version: " << version;
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(_WIN64)
|
2020-09-16 20:14:35 +00:00
|
|
|
LOG(LogInfo) << "EmulationStation renderer: OpenGL 2.1 with GLEW";
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
2020-07-12 10:09:01 +00:00
|
|
|
LOG(LogInfo) << "EmulationStation renderer: OpenGL 2.1";
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-06-26 15:17:35 +00:00
|
|
|
LOG(LogInfo) << "Checking available OpenGL extensions...";
|
|
|
|
std::string glExts = glGetString(GL_EXTENSIONS) ?
|
2021-07-07 18:31:46 +00:00
|
|
|
reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)) :
|
|
|
|
"";
|
2020-08-30 20:19:37 +00:00
|
|
|
if (extensions.find("GL_ARB_texture_non_power_of_two") == std::string::npos) {
|
|
|
|
LOG(LogError) << "GL_ARB_texture_non_power_of_two: MISSING";
|
|
|
|
missingExtension = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogInfo) << "GL_ARB_texture_non_power_of_two: OK";
|
|
|
|
}
|
|
|
|
if (extensions.find("GL_ARB_vertex_shader") == std::string::npos) {
|
|
|
|
LOG(LogError) << "GL_ARB_vertex_shader: MISSING";
|
|
|
|
missingExtension = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogInfo) << "GL_ARB_vertex_shader: OK";
|
|
|
|
}
|
|
|
|
if (extensions.find("GL_ARB_fragment_shader") == std::string::npos) {
|
|
|
|
LOG(LogError) << "GL_ARB_fragment_shader: MISSING";
|
|
|
|
missingExtension = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogInfo) << "GL_ARB_fragment_shader: OK";
|
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
if (extensions.find("GL_EXT_framebuffer_blit") == std::string::npos) {
|
|
|
|
LOG(LogError) << "GL_EXT_framebuffer_blit: MISSING";
|
|
|
|
missingExtension = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogInfo) << "GL_EXT_framebuffer_blit: OK";
|
|
|
|
}
|
2020-08-30 20:19:37 +00:00
|
|
|
if (missingExtension) {
|
|
|
|
LOG(LogError) << "Required OpenGL extensions missing.";
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
uint8_t data[4] = {255, 255, 255, 255};
|
2021-08-19 18:16:42 +00:00
|
|
|
whiteTexture = createTexture(Texture::RGBA, false, false, true, 1, 1, data);
|
2020-06-26 15:17:35 +00:00
|
|
|
|
|
|
|
GL_CHECK_ERROR(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
|
|
|
|
GL_CHECK_ERROR(glEnable(GL_TEXTURE_2D));
|
|
|
|
GL_CHECK_ERROR(glEnable(GL_BLEND));
|
|
|
|
GL_CHECK_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 1));
|
|
|
|
GL_CHECK_ERROR(glPixelStorei(GL_UNPACK_ALIGNMENT, 1));
|
|
|
|
GL_CHECK_ERROR(glEnableClientState(GL_VERTEX_ARRAY));
|
|
|
|
GL_CHECK_ERROR(glEnableClientState(GL_TEXTURE_COORD_ARRAY));
|
|
|
|
GL_CHECK_ERROR(glEnableClientState(GL_COLOR_ARRAY));
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-09-04 16:59:19 +00:00
|
|
|
// This is the framebuffer that will be used for shader rendering.
|
|
|
|
GL_CHECK_ERROR(glGenFramebuffers(1, &shaderFBO));
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
return true;
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroyContext()
|
|
|
|
{
|
2020-09-04 16:59:19 +00:00
|
|
|
GL_CHECK_ERROR(glDeleteFramebuffers(1, &shaderFBO));
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_GL_DeleteContext(sdlContext);
|
|
|
|
sdlContext = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-19 18:16:42 +00:00
|
|
|
unsigned int createTexture(const Texture::Type type,
|
|
|
|
const bool linearMinify,
|
|
|
|
const bool linearMagnify,
|
|
|
|
const bool repeat,
|
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
|
|
|
void* data)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 18:16:42 +00:00
|
|
|
const GLenum textureType = convertTextureType(type);
|
2020-06-26 15:17:35 +00:00
|
|
|
unsigned int texture;
|
|
|
|
|
|
|
|
GL_CHECK_ERROR(glGenTextures(1, &texture));
|
|
|
|
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
|
2021-08-19 18:16:42 +00:00
|
|
|
repeat ? static_cast<GLfloat>(GL_REPEAT) :
|
|
|
|
static_cast<GLfloat>(GL_CLAMP_TO_EDGE)));
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
|
2021-08-19 18:16:42 +00:00
|
|
|
repeat ? static_cast<GLfloat>(GL_REPEAT) :
|
|
|
|
static_cast<GLfloat>(GL_CLAMP_TO_EDGE)));
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
|
2021-08-19 18:16:42 +00:00
|
|
|
linearMinify ? static_cast<GLfloat>(GL_LINEAR) :
|
|
|
|
static_cast<GLfloat>(GL_NEAREST)));
|
|
|
|
GL_CHECK_ERROR(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
|
|
|
|
linearMagnify ? static_cast<GLfloat>(GL_LINEAR) :
|
|
|
|
static_cast<GLfloat>(GL_NEAREST)));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-08-19 18:16:42 +00:00
|
|
|
GL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, textureType, width, height, 0, textureType,
|
|
|
|
GL_UNSIGNED_BYTE, data));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void destroyTexture(const unsigned int texture)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDeleteTextures(1, &texture));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void updateTexture(const unsigned int texture,
|
|
|
|
const Texture::Type type,
|
|
|
|
const unsigned int x,
|
|
|
|
const unsigned y,
|
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
|
|
|
void* data)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
const GLenum textureType = convertTextureType(type);
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
|
|
|
|
GL_CHECK_ERROR(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, textureType,
|
|
|
|
GL_UNSIGNED_BYTE, data));
|
2020-06-26 15:17:35 +00:00
|
|
|
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
|
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void bindTexture(const unsigned int texture)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
if (texture == 0)
|
2020-06-26 15:17:35 +00:00
|
|
|
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, whiteTexture));
|
|
|
|
else
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void drawLines(const Vertex* vertices,
|
|
|
|
const unsigned int numVertices,
|
|
|
|
const Blend::Factor srcBlendFactor,
|
|
|
|
const Blend::Factor dstBlendFactor)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
|
|
|
|
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
|
|
|
|
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(
|
2021-08-19 19:39:01 +00:00
|
|
|
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_LINES, 0, numVertices));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void drawTriangleStrips(const Vertex* vertices,
|
|
|
|
const unsigned int numVertices,
|
|
|
|
const glm::mat4& trans,
|
|
|
|
const Blend::Factor srcBlendFactor,
|
|
|
|
const Blend::Factor dstBlendFactor,
|
|
|
|
const shaderParameters& parameters)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
float width = vertices[3].pos[0];
|
|
|
|
float height = vertices[3].pos[1];
|
2020-09-12 10:14:48 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].pos));
|
|
|
|
GL_CHECK_ERROR(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), &vertices[0].tex));
|
|
|
|
GL_CHECK_ERROR(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), &vertices[0].col));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(
|
2021-08-19 19:39:01 +00:00
|
|
|
glBlendFunc(convertBlendFactor(srcBlendFactor), convertBlendFactor(dstBlendFactor)));
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(USE_OPENGL_21)
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices[0].shaders == 0) {
|
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2020-09-12 10:14:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-03-17 19:29:43 +00:00
|
|
|
// If saturation is set below the maximum (default) value, run the
|
|
|
|
// desaturation shader.
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->saturation < 1.0f || parameters.fragmentSaturation < 1.0f) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_DESATURATE);
|
|
|
|
// Only try to use the shader if it has been loaded properly.
|
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
|
|
|
runShader->setSaturation(vertices->saturation);
|
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->shaders & SHADER_OPACITY) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_OPACITY);
|
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
|
|
|
vertices->opacity < 1.0f ? runShader->setOpacity(vertices->opacity) :
|
2021-08-19 19:51:42 +00:00
|
|
|
runShader->setOpacity(parameters.fragmentOpacity);
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-12 17:17:26 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
2020-09-12 17:17:26 +00:00
|
|
|
|
2021-03-17 19:29:43 +00:00
|
|
|
// Check if any other shaders are set to be used and if so, run them.
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->shaders & SHADER_DIM) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_DIM);
|
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
|
|
|
runShader->setDimValue(parameters.fragmentDimValue);
|
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->shaders & SHADER_BLUR_HORIZONTAL) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_BLUR_HORIZONTAL);
|
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
2021-08-17 16:41:45 +00:00
|
|
|
runShader->setTextureSize({width, height});
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-12 10:14:48 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
2020-09-12 10:14:48 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->shaders & SHADER_BLUR_VERTICAL) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_BLUR_VERTICAL);
|
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
2021-08-17 16:41:45 +00:00
|
|
|
runShader->setTextureSize({width, height});
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
if (vertices->shaders & SHADER_SCANLINES) {
|
2021-03-17 19:29:43 +00:00
|
|
|
Shader* runShader = getShaderProgram(SHADER_SCANLINES);
|
|
|
|
float shaderWidth = width * 1.2f;
|
2021-03-18 18:52:48 +00:00
|
|
|
// Scale the scanlines relative to screen resolution.
|
|
|
|
float screenHeightModifier = getScreenHeightModifier();
|
|
|
|
float relativeHeight = height / getScreenHeight();
|
|
|
|
float shaderHeight = 0.0f;
|
|
|
|
if (relativeHeight == 1.0f) {
|
|
|
|
// Full screen.
|
|
|
|
float modifier = 1.30f - (0.1f * screenHeightModifier);
|
|
|
|
shaderHeight = height * modifier;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Portion of screen, e.g. gamelist view.
|
|
|
|
// Average the relative width and height to avoid applying exaggerated
|
|
|
|
// scanlines to videos with non-standard aspect ratios.
|
|
|
|
float relativeWidth = width / getScreenWidth();
|
|
|
|
float relativeAdjustment = (relativeWidth + relativeHeight) / 2.0f;
|
2021-07-07 18:31:46 +00:00
|
|
|
float modifier =
|
|
|
|
1.41f + relativeAdjustment / 7.0f - (0.14f * screenHeightModifier);
|
2021-03-18 18:52:48 +00:00
|
|
|
shaderHeight = height * modifier;
|
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
if (runShader) {
|
|
|
|
runShader->activateShaders();
|
2021-08-19 19:39:01 +00:00
|
|
|
runShader->setModelViewProjectionMatrix(getProjectionMatrix() * trans);
|
2021-08-17 16:41:45 +00:00
|
|
|
runShader->setTextureSize({shaderWidth, shaderHeight});
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, numVertices));
|
2021-03-17 19:29:43 +00:00
|
|
|
runShader->deactivateShaders();
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
#endif
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void setProjection(const glm::mat4& projection)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
|
|
|
GL_CHECK_ERROR(glMatrixMode(GL_PROJECTION));
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&projection)));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void setMatrix(const glm::mat4& matrix)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
glm::mat4 newMatrix{matrix};
|
|
|
|
newMatrix[3] = glm::round(newMatrix[3]);
|
2020-06-26 15:17:35 +00:00
|
|
|
|
|
|
|
GL_CHECK_ERROR(glMatrixMode(GL_MODELVIEW));
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glLoadMatrixf(reinterpret_cast<const GLfloat*>(&newMatrix)));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void setViewport(const Rect& viewport)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
|
|
|
// glViewport starts at the bottom left of the window.
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glViewport(viewport.x, getWindowHeight() - viewport.y - viewport.h,
|
|
|
|
viewport.w, viewport.h));
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 19:39:01 +00:00
|
|
|
void setScissor(const Rect& scissor)
|
2020-06-26 15:17:35 +00:00
|
|
|
{
|
2021-08-19 19:39:01 +00:00
|
|
|
if ((scissor.x == 0) && (scissor.y == 0) && (scissor.w == 0) && (scissor.h == 0)) {
|
2020-06-26 15:17:35 +00:00
|
|
|
GL_CHECK_ERROR(glDisable(GL_SCISSOR_TEST));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// glScissor starts at the bottom left of the window.
|
2021-08-19 19:39:01 +00:00
|
|
|
GL_CHECK_ERROR(glScissor(scissor.x, getWindowHeight() - scissor.y - scissor.h,
|
|
|
|
scissor.w, scissor.h));
|
2020-06-26 15:17:35 +00:00
|
|
|
GL_CHECK_ERROR(glEnable(GL_SCISSOR_TEST));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSwapInterval()
|
|
|
|
{
|
|
|
|
if (Settings::getInstance()->getBool("VSync")) {
|
2021-01-06 23:17:59 +00:00
|
|
|
// Adaptive VSync seems to be nonfunctional or having issues on some hardware
|
|
|
|
// and drivers, so only attempt to apply regular VSync.
|
|
|
|
if (SDL_GL_SetSwapInterval(1) == 0) {
|
|
|
|
LOG(LogInfo) << "Enabling VSync...";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
LOG(LogWarning) << "Could not enable VSync: " << SDL_GetError();
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-06 23:17:59 +00:00
|
|
|
else {
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_GL_SetSwapInterval(0);
|
2021-01-06 23:17:59 +00:00
|
|
|
LOG(LogInfo) << "Disabling VSync...";
|
|
|
|
}
|
2020-06-26 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void swapBuffers()
|
|
|
|
{
|
|
|
|
SDL_GL_SwapWindow(getSDLWindow());
|
|
|
|
GL_CHECK_ERROR(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
|
|
|
|
}
|
2019-08-08 20:16:11 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
void shaderPostprocessing(unsigned int shaders,
|
|
|
|
const Renderer::shaderParameters& parameters,
|
|
|
|
unsigned char* textureRGBA)
|
2020-09-04 16:59:19 +00:00
|
|
|
{
|
|
|
|
Vertex vertices[4];
|
2021-07-07 18:31:46 +00:00
|
|
|
std::vector<unsigned int> shaderList;
|
2020-09-04 16:59:19 +00:00
|
|
|
GLuint width = getScreenWidth();
|
|
|
|
GLuint height = getScreenHeight();
|
|
|
|
float widthf = static_cast<float>(width);
|
|
|
|
float heightf = static_cast<float>(height);
|
|
|
|
|
|
|
|
// Set vertex positions and texture coordinates to full screen as all
|
|
|
|
// postprocessing is applied to the complete screen area.
|
2021-07-07 18:31:46 +00:00
|
|
|
// clang-format off
|
|
|
|
vertices[0] = { { 0.0f , 0.0f }, { 0.0f, 1.0f }, 0 };
|
|
|
|
vertices[1] = { { 0.0f , heightf }, { 0.0f, 0.0f }, 0 };
|
|
|
|
vertices[2] = { { widthf, 0.0f }, { 1.0f, 1.0f }, 0 };
|
|
|
|
vertices[3] = { { widthf, heightf }, { 1.0f, 0.0f }, 0 };
|
|
|
|
// clang-format on
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2021-03-17 19:29:43 +00:00
|
|
|
if (shaders & Renderer::SHADER_DESATURATE)
|
|
|
|
shaderList.push_back(Renderer::SHADER_DESATURATE);
|
|
|
|
if (shaders & Renderer::SHADER_OPACITY)
|
|
|
|
shaderList.push_back(Renderer::SHADER_OPACITY);
|
|
|
|
if (shaders & Renderer::SHADER_DIM)
|
|
|
|
shaderList.push_back(Renderer::SHADER_DIM);
|
|
|
|
if (shaders & Renderer::SHADER_BLUR_HORIZONTAL)
|
|
|
|
shaderList.push_back(Renderer::SHADER_BLUR_HORIZONTAL);
|
|
|
|
if (shaders & Renderer::SHADER_BLUR_VERTICAL)
|
|
|
|
shaderList.push_back(Renderer::SHADER_BLUR_VERTICAL);
|
|
|
|
if (shaders & Renderer::SHADER_SCANLINES)
|
|
|
|
shaderList.push_back(Renderer::SHADER_SCANLINES);
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2020-09-12 10:14:48 +00:00
|
|
|
if (parameters.fragmentSaturation < 1.0)
|
2020-09-04 16:59:19 +00:00
|
|
|
vertices[0].saturation = parameters.fragmentSaturation;
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
setMatrix(getIdentity());
|
2021-08-19 18:16:42 +00:00
|
|
|
GLuint screenTexture =
|
|
|
|
createTexture(Texture::RGBA, false, false, false, width, height, nullptr);
|
2020-09-04 16:59:19 +00:00
|
|
|
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0));
|
2021-03-17 19:29:43 +00:00
|
|
|
|
2021-09-19 12:37:10 +00:00
|
|
|
for (size_t i = 0; i < shaderList.size(); i++) {
|
2021-03-17 19:29:43 +00:00
|
|
|
vertices[0].shaders = shaderList[i];
|
|
|
|
int shaderPasses = 1;
|
|
|
|
// For the blur shaders there is an optional variable to set the number of passes
|
|
|
|
// to execute, which proportionally affects the blur amount.
|
|
|
|
if (shaderList[i] == Renderer::SHADER_BLUR_HORIZONTAL ||
|
2021-07-07 18:31:46 +00:00
|
|
|
shaderList[i] == Renderer::SHADER_BLUR_VERTICAL) {
|
2021-03-17 19:29:43 +00:00
|
|
|
shaderPasses = parameters.blurPasses;
|
2021-07-07 18:31:46 +00:00
|
|
|
}
|
2021-03-17 19:29:43 +00:00
|
|
|
|
|
|
|
for (int p = 0; p < shaderPasses; p++) {
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, shaderFBO));
|
|
|
|
|
|
|
|
// Attach the texture to the shader framebuffer.
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_TEXTURE_2D, screenTexture, 0));
|
2021-03-17 19:29:43 +00:00
|
|
|
|
|
|
|
// Blit the screen contents to screenTexture.
|
|
|
|
GL_CHECK_ERROR(glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST));
|
2021-03-17 19:29:43 +00:00
|
|
|
|
|
|
|
// Apply/render the shaders.
|
2021-08-15 17:30:31 +00:00
|
|
|
drawTriangleStrips(vertices, 4, getIdentity(), Blend::SRC_ALPHA,
|
2021-07-07 18:31:46 +00:00
|
|
|
Blend::ONE_MINUS_SRC_ALPHA, parameters);
|
2021-03-17 19:29:43 +00:00
|
|
|
|
|
|
|
// If textureRGBA has an address, it means that the output should go to this
|
|
|
|
// texture rather than to the screen. The glReadPixels() function is slow, but
|
|
|
|
// since this will typically only run every now and then to create a cached
|
|
|
|
// screen texture, it doesn't really matter.
|
|
|
|
if (textureRGBA) {
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_READ_FRAMEBUFFER, shaderFBO));
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_CHECK_ERROR(
|
|
|
|
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, textureRGBA));
|
2021-03-17 19:29:43 +00:00
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Blit the resulting postprocessed texture back to the primary framebuffer.
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_READ_FRAMEBUFFER, shaderFBO));
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
|
|
|
|
GL_CHECK_ERROR(glBlitFramebuffer(0, 0, width, height, 0, 0, width, height,
|
2021-07-07 18:31:46 +00:00
|
|
|
GL_COLOR_BUFFER_BIT, GL_NEAREST));
|
2021-03-17 19:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GL_CHECK_ERROR(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0));
|
|
|
|
destroyTexture(screenTexture);
|
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
} // namespace Renderer
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
#endif // USE_OPENGL_21
|