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.h
|
|
|
|
//
|
2020-06-28 16:39:18 +00:00
|
|
|
// General rendering functions.
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
#ifndef ES_CORE_RENDERER_RENDERER_H
|
|
|
|
#define ES_CORE_RENDERER_RENDERER_H
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Shader_GL21.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "math/Transform4x4f.h"
|
|
|
|
#include "math/Vector2f.h"
|
2020-08-30 20:19:37 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
struct SDL_Window;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
class Transform4x4f;
|
|
|
|
class Vector2i;
|
|
|
|
|
2019-08-08 20:16:11 +00:00
|
|
|
namespace Renderer
|
|
|
|
{
|
2021-07-07 18:31:46 +00:00
|
|
|
const unsigned int SHADER_DESATURATE = 1;
|
|
|
|
const unsigned int SHADER_OPACITY = 2;
|
|
|
|
const unsigned int SHADER_DIM = 4;
|
|
|
|
const unsigned int SHADER_BLUR_HORIZONTAL = 8;
|
|
|
|
const unsigned int SHADER_BLUR_VERTICAL = 16;
|
|
|
|
const unsigned int SHADER_SCANLINES = 32;
|
2020-09-04 16:59:19 +00:00
|
|
|
|
|
|
|
struct shaderParameters {
|
|
|
|
std::array<GLfloat, 2> textureSize;
|
|
|
|
std::array<GLfloat, 4> textureCoordinates;
|
|
|
|
float fragmentSaturation;
|
2020-09-12 10:14:48 +00:00
|
|
|
float fragmentDimValue;
|
2020-09-12 17:17:26 +00:00
|
|
|
float fragmentOpacity;
|
2021-03-17 19:29:43 +00:00
|
|
|
unsigned int blurPasses;
|
2020-09-04 16:59:19 +00:00
|
|
|
|
|
|
|
shaderParameters()
|
2021-07-07 18:31:46 +00:00
|
|
|
: textureSize({ 0.0f, 0.0f })
|
|
|
|
, textureCoordinates({ 0.0f, 0.0f, 0.0f, 0.0f })
|
|
|
|
, fragmentSaturation(1.0f)
|
|
|
|
, fragmentDimValue(0.4f)
|
|
|
|
, fragmentOpacity(1.0f)
|
|
|
|
, blurPasses(1)
|
|
|
|
{
|
|
|
|
}
|
2020-09-04 16:59:19 +00:00
|
|
|
};
|
|
|
|
|
2020-08-30 20:19:37 +00:00
|
|
|
static std::vector<Shader*> sShaderProgramVector;
|
2020-09-04 16:59:19 +00:00
|
|
|
static GLuint shaderFBO;
|
2020-09-12 10:14:48 +00:00
|
|
|
static Transform4x4f mProjectionMatrix;
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#if !defined(NDEBUG)
|
|
|
|
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-12-16 22:59:00 +00:00
|
|
|
static void _GLCheckError(const std::string& _funcName)
|
2020-08-30 20:19:37 +00:00
|
|
|
{
|
|
|
|
const GLenum errorCode = glGetError();
|
|
|
|
|
|
|
|
if (errorCode != GL_NO_ERROR) {
|
2021-07-07 18:31:46 +00:00
|
|
|
#if defined(USE_OPENGL_21)
|
|
|
|
LOG(LogError) << "OpenGL error: " << _funcName << " failed with error code: 0x"
|
|
|
|
<< std::hex << errorCode;
|
|
|
|
#else
|
|
|
|
LOG(LogError) << "OpenGLES error: " << _funcName << " failed with error code: 0x"
|
|
|
|
<< std::hex << errorCode;
|
|
|
|
#endif
|
2020-08-30 20:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-07 18:31:46 +00:00
|
|
|
#else
|
|
|
|
#define GL_CHECK_ERROR(Function) (Function)
|
|
|
|
#endif
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-06-26 15:17:35 +00:00
|
|
|
namespace Blend
|
|
|
|
{
|
|
|
|
enum Factor {
|
|
|
|
ZERO = 0,
|
|
|
|
ONE = 1,
|
|
|
|
SRC_COLOR = 2,
|
|
|
|
ONE_MINUS_SRC_COLOR = 3,
|
|
|
|
SRC_ALPHA = 4,
|
|
|
|
ONE_MINUS_SRC_ALPHA = 5,
|
|
|
|
DST_COLOR = 6,
|
|
|
|
ONE_MINUS_DST_COLOR = 7,
|
|
|
|
DST_ALPHA = 8,
|
|
|
|
ONE_MINUS_DST_ALPHA = 9
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Texture
|
|
|
|
{
|
|
|
|
enum Type {
|
2021-07-07 18:31:46 +00:00
|
|
|
RGBA = 0, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
|
2020-06-26 15:17:35 +00:00
|
|
|
ALPHA = 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Rect {
|
2021-07-07 18:31:46 +00:00
|
|
|
Rect(const int _x, const int _y, const int _w, const int _h)
|
|
|
|
: x(_x)
|
|
|
|
, y(_y)
|
|
|
|
, w(_w)
|
|
|
|
, h(_h)
|
|
|
|
{
|
|
|
|
}
|
2020-06-26 15:17:35 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
};
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
struct Vertex {
|
2020-06-26 15:17:35 +00:00
|
|
|
Vertex() {}
|
2021-07-07 18:31:46 +00:00
|
|
|
Vertex(const Vector2f& _pos, const Vector2f& _tex, const unsigned int _col)
|
|
|
|
: pos(_pos)
|
|
|
|
, tex(_tex)
|
|
|
|
, col(_col)
|
|
|
|
{
|
|
|
|
}
|
2020-06-26 15:17:35 +00:00
|
|
|
Vector2f pos;
|
|
|
|
Vector2f tex;
|
|
|
|
unsigned int col;
|
2020-08-30 20:19:37 +00:00
|
|
|
float saturation = 1.0;
|
2020-09-12 17:17:26 +00:00
|
|
|
float opacity = 1.0;
|
2020-09-04 16:59:19 +00:00
|
|
|
unsigned int shaders = 0;
|
2020-06-26 15:17:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool init();
|
|
|
|
void deinit();
|
|
|
|
void pushClipRect(const Vector2i& _pos, const Vector2i& _size);
|
|
|
|
void popClipRect();
|
2021-07-07 18:31:46 +00:00
|
|
|
void drawRect(const float _x,
|
|
|
|
const float _y,
|
|
|
|
const float _w,
|
|
|
|
const float _h,
|
|
|
|
const unsigned int _color,
|
|
|
|
const unsigned int _colorEnd,
|
|
|
|
bool horizontalGradient = false,
|
|
|
|
const float _opacity = 1.0,
|
|
|
|
const Transform4x4f& _trans = Transform4x4f::Identity(),
|
|
|
|
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
|
|
|
|
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
|
2020-06-26 15:17:35 +00:00
|
|
|
SDL_Window* getSDLWindow();
|
|
|
|
int getWindowWidth();
|
|
|
|
int getWindowHeight();
|
|
|
|
int getScreenWidth();
|
|
|
|
int getScreenHeight();
|
|
|
|
int getScreenOffsetX();
|
|
|
|
int getScreenOffsetY();
|
|
|
|
int getScreenRotate();
|
2021-01-13 18:42:06 +00:00
|
|
|
float getScreenWidthModifier();
|
|
|
|
float getScreenHeightModifier();
|
2021-03-09 16:17:33 +00:00
|
|
|
float getScreenAspectRatio();
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2020-12-18 15:49:11 +00:00
|
|
|
unsigned int convertRGBAToABGR(unsigned int color);
|
|
|
|
unsigned int convertABGRToRGBA(unsigned int color);
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-09-04 16:59:19 +00:00
|
|
|
Shader* getShaderProgram(unsigned int shaderID);
|
2020-09-12 10:14:48 +00:00
|
|
|
const Transform4x4f getProjectionMatrix();
|
2020-09-04 16:59:19 +00:00
|
|
|
void shaderPostprocessing(unsigned int shaders,
|
2021-07-07 18:31:46 +00:00
|
|
|
const Renderer::shaderParameters& parameters = shaderParameters(),
|
|
|
|
unsigned char* textureRGBA = nullptr);
|
|
|
|
|
|
|
|
static unsigned int getWindowFlags() { return SDL_WINDOW_OPENGL; }
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-06-26 15:17:35 +00:00
|
|
|
void setupWindow();
|
2020-08-30 20:19:37 +00:00
|
|
|
bool createContext();
|
2020-06-26 15:17:35 +00:00
|
|
|
void destroyContext();
|
2021-07-07 18:31:46 +00:00
|
|
|
unsigned int createTexture(const Texture::Type _type,
|
|
|
|
const bool _linear,
|
|
|
|
const bool _repeat,
|
|
|
|
const unsigned int _width,
|
|
|
|
const unsigned int _height,
|
|
|
|
void* _data);
|
2020-06-26 15:17:35 +00:00
|
|
|
void destroyTexture(const unsigned int _texture);
|
2021-07-07 18:31:46 +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
|
|
|
void bindTexture(const unsigned int _texture);
|
2021-07-07 18:31:46 +00:00
|
|
|
void drawLines(const Vertex* _vertices,
|
|
|
|
const unsigned int _numVertices,
|
|
|
|
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
|
|
|
|
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
|
|
|
|
void drawTriangleStrips(const Vertex* _vertices,
|
|
|
|
const unsigned int _numVertices,
|
|
|
|
const Transform4x4f& _trans = Transform4x4f::Identity(),
|
|
|
|
const Blend::Factor _srcBlendFactor = Blend::SRC_ALPHA,
|
|
|
|
const Blend::Factor _dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA,
|
|
|
|
const shaderParameters& _parameters = shaderParameters());
|
2020-06-26 15:17:35 +00:00
|
|
|
void setProjection(const Transform4x4f& _projection);
|
|
|
|
void setMatrix(const Transform4x4f& _matrix);
|
|
|
|
void setViewport(const Rect& _viewport);
|
|
|
|
void setScissor(const Rect& _scissor);
|
|
|
|
void setSwapInterval();
|
|
|
|
void swapBuffers();
|
2021-07-07 18:31:46 +00:00
|
|
|
|
|
|
|
} // namespace Renderer
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_RENDERER_RENDERER_H
|