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-03-11 22:17:04 +00:00
|
|
|
// clang-format off
|
|
|
|
const unsigned int SHADER_CORE {0x00000001};
|
|
|
|
const unsigned int SHADER_BLUR_HORIZONTAL {0x00000002};
|
|
|
|
const unsigned int SHADER_BLUR_VERTICAL {0x00000004};
|
|
|
|
const unsigned int SHADER_SCANLINES {0x00000008};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
struct postProcessingParams {
|
|
|
|
float opacity;
|
|
|
|
float saturation;
|
2022-03-11 22:51:41 +00:00
|
|
|
float dimming;
|
2022-03-11 22:17:04 +00:00
|
|
|
bool convertBGRAToRGBA;
|
2021-03-17 19:29:43 +00:00
|
|
|
unsigned int blurPasses;
|
2022-03-11 22:17:04 +00:00
|
|
|
unsigned int shaders;
|
2020-09-04 16:59:19 +00:00
|
|
|
|
2022-03-11 22:17:04 +00:00
|
|
|
postProcessingParams()
|
|
|
|
: opacity {1.0f}
|
|
|
|
, saturation {1.0f}
|
2022-03-11 22:51:41 +00:00
|
|
|
, dimming {1.0f}
|
2022-03-11 22:17:04 +00:00
|
|
|
, convertBGRAToRGBA {false}
|
2022-01-16 11:09:55 +00:00
|
|
|
, blurPasses {1}
|
2022-03-11 22:17:04 +00:00
|
|
|
, shaders {0}
|
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;
|
2022-03-11 22:17:04 +00:00
|
|
|
static GLuint shaderFBO1;
|
|
|
|
static GLuint shaderFBO2;
|
2021-09-19 12:37:10 +00:00
|
|
|
// This is simply to get rid of a GCC false positive -Wunused-variable compiler warning.
|
2022-03-11 22:17:04 +00:00
|
|
|
static GLuint shaderFBODummy1 {shaderFBO1};
|
|
|
|
static GLuint shaderFBODummy2 {shaderFBO2};
|
2021-09-19 12:37:10 +00:00
|
|
|
|
2022-01-16 11:09:55 +00:00
|
|
|
static constexpr glm::mat4 getIdentity() { return glm::mat4 {1.0f}; }
|
2022-03-11 22:17:04 +00:00
|
|
|
static inline glm::mat4 mTrans {getIdentity()};
|
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
|
2022-03-11 22:51:41 +00:00
|
|
|
LOG(LogError) << "OpenGL ES error: " << _funcName << " failed with error code: 0x"
|
2021-07-07 18:31:46 +00:00
|
|
|
<< 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 {
|
2022-03-11 22:51:41 +00:00
|
|
|
glm::vec2 position;
|
|
|
|
glm::vec2 texture;
|
|
|
|
unsigned int color;
|
2022-03-11 22:17:04 +00:00
|
|
|
float opacity;
|
|
|
|
float saturation;
|
2022-03-11 22:51:41 +00:00
|
|
|
float dimming;
|
2022-03-11 22:17:04 +00:00
|
|
|
bool convertBGRAToRGBA;
|
|
|
|
unsigned int shaders;
|
|
|
|
|
|
|
|
Vertex()
|
|
|
|
: opacity {1.0f}
|
|
|
|
, saturation {1.0f}
|
2022-03-11 22:51:41 +00:00
|
|
|
, dimming {1.0f}
|
2022-03-11 22:17:04 +00:00
|
|
|
, convertBGRAToRGBA {false}
|
|
|
|
, shaders {0}
|
|
|
|
{
|
|
|
|
}
|
2021-08-19 19:39:01 +00:00
|
|
|
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
|
2022-03-11 22:51:41 +00:00
|
|
|
: position(position)
|
|
|
|
, texture(textureCoord)
|
|
|
|
, color(color)
|
2022-03-11 22:17:04 +00:00
|
|
|
, opacity {1.0f}
|
|
|
|
, saturation {1.0f}
|
2022-03-11 22:51:41 +00:00
|
|
|
, dimming {1.0f}
|
2022-03-11 22:17:04 +00:00
|
|
|
, convertBGRAToRGBA {false}
|
|
|
|
, shaders {0}
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
|
|
|
}
|
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,
|
2022-03-11 22:51:41 +00:00
|
|
|
const float dimming = 1.0,
|
2021-08-19 19:39:01 +00:00
|
|
|
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();
|
2022-02-11 22:38:23 +00:00
|
|
|
const float getWindowWidth();
|
|
|
|
const float getWindowHeight();
|
|
|
|
const float getScreenWidth();
|
|
|
|
const float getScreenHeight();
|
|
|
|
const float getScreenOffsetX();
|
|
|
|
const float getScreenOffsetY();
|
2022-03-11 22:17:04 +00:00
|
|
|
const bool getScreenRotated();
|
2021-11-09 21:40:08 +00:00
|
|
|
const float getScreenWidthModifier();
|
|
|
|
const float getScreenHeightModifier();
|
|
|
|
const float getScreenAspectRatio();
|
|
|
|
|
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();
|
2022-03-11 22:17:04 +00:00
|
|
|
const glm::mat4& getProjectionMatrixNormal();
|
|
|
|
void shaderPostprocessing(
|
|
|
|
const unsigned int shaders,
|
|
|
|
const Renderer::postProcessingParams& parameters = postProcessingParams(),
|
|
|
|
unsigned char* textureRGBA = nullptr);
|
2021-07-07 18:31:46 +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-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 Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
|
2022-03-11 22:17:04 +00:00
|
|
|
const Blend::Factor dstBlendFactor = Blend::ONE_MINUS_SRC_ALPHA);
|
2021-08-19 19:39:01 +00:00
|
|
|
void setMatrix(const glm::mat4& matrix);
|
|
|
|
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
|