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-08-17 20:11:16 +00:00
|
|
|
#include "utils/MathUtil.h"
|
2020-08-30 20:19:37 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
struct SDL_Window;
|
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
{
|
2022-01-16 11:09:55 +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};
|
|
|
|
const unsigned int SHADER_BGRA_TO_RGBA {64};
|
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()
|
2022-01-16 11:09:55 +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}
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
|
|
|
}
|
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;
|
2021-09-19 12:37:10 +00:00
|
|
|
// This is simply to get rid of a GCC false positive -Wunused-variable compiler warning.
|
|
|
|
static GLuint shaderFBODummy = shaderFBO;
|
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
static constexpr glm::mat4 getIdentity() { return glm::mat4 {1.0f}; }
|
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
|
|
|
|
2021-09-19 12:37:10 +00:00
|
|
|
static inline 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 {
|
2021-08-19 19:39:01 +00:00
|
|
|
ZERO,
|
|
|
|
ONE,
|
|
|
|
SRC_COLOR,
|
|
|
|
ONE_MINUS_SRC_COLOR,
|
|
|
|
SRC_ALPHA,
|
|
|
|
ONE_MINUS_SRC_ALPHA,
|
|
|
|
DST_COLOR,
|
|
|
|
ONE_MINUS_DST_COLOR,
|
|
|
|
DST_ALPHA,
|
|
|
|
ONE_MINUS_DST_ALPHA
|
2020-06-26 15:17:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace Texture
|
|
|
|
{
|
|
|
|
enum Type {
|
2021-08-19 19:39:01 +00:00
|
|
|
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
|
2022-01-07 17:54:52 +00:00
|
|
|
BGRA,
|
2021-08-19 19:39:01 +00:00
|
|
|
ALPHA
|
2020-06-26 15:17:35 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Rect {
|
2021-08-19 19:39:01 +00:00
|
|
|
Rect(const int xValue, const int yValue, const int wValue, const int hValue)
|
|
|
|
: x(xValue)
|
|
|
|
, y(yValue)
|
|
|
|
, w(wValue)
|
|
|
|
, h(hValue)
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
|
|
|
}
|
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-08-19 19:39:01 +00:00
|
|
|
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
|
|
|
|
: pos(position)
|
|
|
|
, tex(textureCoord)
|
|
|
|
, col(color)
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
|
|
|
}
|
2021-08-16 16:25:01 +00:00
|
|
|
glm::vec2 pos;
|
|
|
|
glm::vec2 tex;
|
2020-06-26 15:17:35 +00:00
|
|
|
unsigned int col;
|
2022-01-16 11:09:55 +00:00
|
|
|
float saturation {1.0};
|
|
|
|
float opacity {1.0};
|
|
|
|
unsigned int shaders {0};
|
2020-06-26 15:17:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool init();
|
|
|
|
void deinit();
|
2021-08-19 19:39:01 +00:00
|
|
|
void pushClipRect(const glm::ivec2& pos, const glm::ivec2& size);
|
2020-06-26 15:17:35 +00:00
|
|
|
void popClipRect();
|
2021-08-19 19:39:01 +00:00
|
|
|
void drawRect(const float x,
|
|
|
|
const float y,
|
|
|
|
const float w,
|
|
|
|
const float h,
|
|
|
|
const unsigned int color,
|
|
|
|
const unsigned int colorEnd,
|
2021-07-07 18:31:46 +00:00
|
|
|
bool horizontalGradient = false,
|
2021-08-19 19:39:01 +00:00
|
|
|
const float opacity = 1.0,
|
|
|
|
const glm::mat4& trans = getIdentity(),
|
|
|
|
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();
|
2021-11-09 21:40:08 +00:00
|
|
|
const int getWindowWidth();
|
|
|
|
const int getWindowHeight();
|
|
|
|
const int getScreenWidth();
|
|
|
|
const int getScreenHeight();
|
|
|
|
const int getScreenOffsetX();
|
|
|
|
const int getScreenOffsetY();
|
|
|
|
const int getScreenRotate();
|
|
|
|
const float getScreenWidthModifier();
|
|
|
|
const float getScreenHeightModifier();
|
|
|
|
const float getScreenAspectRatio();
|
|
|
|
|
|
|
|
const unsigned int convertRGBAToABGR(const unsigned int color);
|
|
|
|
const unsigned int convertABGRToRGBA(const unsigned int color);
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2020-09-04 16:59:19 +00:00
|
|
|
Shader* getShaderProgram(unsigned int shaderID);
|
2021-11-09 21:40:08 +00:00
|
|
|
const glm::mat4& getProjectionMatrix();
|
|
|
|
void shaderPostprocessing(const unsigned int shaders,
|
2021-07-07 18:31:46 +00:00
|
|
|
const Renderer::shaderParameters& parameters = shaderParameters(),
|
|
|
|
unsigned char* textureRGBA = nullptr);
|
|
|
|
|
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-08-19 18:16:42 +00:00
|
|
|
unsigned int createTexture(const Texture::Type type,
|
2022-01-07 17:54:52 +00:00
|
|
|
const Texture::Type format,
|
2021-08-19 18:16:42 +00:00
|
|
|
const bool linearMinify,
|
|
|
|
const bool linearMagnify,
|
|
|
|
const bool repeat,
|
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
|
|
|
void* data);
|
2021-08-19 19:39:01 +00:00
|
|
|
void destroyTexture(const unsigned int texture);
|
|
|
|
void updateTexture(const unsigned int texture,
|
|
|
|
const Texture::Type type,
|
|
|
|
const unsigned int x,
|
2021-11-09 21:40:08 +00:00
|
|
|
const unsigned int y,
|
2021-08-19 19:39:01 +00:00
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
|
|
|
void* data);
|
|
|
|
void bindTexture(const unsigned int texture);
|
|
|
|
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 glm::mat4& trans = getIdentity(),
|
|
|
|
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
|
|
|
|
const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA,
|
|
|
|
const shaderParameters& parameters = shaderParameters());
|
|
|
|
void setProjection(const glm::mat4& projection);
|
|
|
|
void setMatrix(const glm::mat4& matrix);
|
|
|
|
void setViewport(const Rect& viewport);
|
|
|
|
void setScissor(const Rect& scissor);
|
2020-06-26 15:17:35 +00:00
|
|
|
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
|