ES-DE/es-core/src/renderers/Renderer.h

206 lines
6.4 KiB
C
Raw Normal View History

// SPDX-License-Identifier: MIT
//
// EmulationStation Desktop Edition
// Renderer.h
//
// General rendering functions.
//
#ifndef ES_CORE_RENDERER_RENDERER_H
#define ES_CORE_RENDERER_RENDERER_H
#include "Log.h"
#include "Shader_GL21.h"
#include "utils/MathUtil.h"
#include <string>
#include <vector>
struct SDL_Window;
namespace Renderer
{
2021-08-19 19:39:01 +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};
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;
unsigned int blurPasses;
shaderParameters()
2021-08-19 19:39:01 +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}
{
}
};
static std::vector<Shader*> sShaderProgramVector;
static GLuint shaderFBO;
static glm::mat4 mProjectionMatrix;
static constexpr glm::mat4 getIdentity() { return glm::mat4{1.0f}; }
#if !defined(NDEBUG)
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
2020-12-16 22:59:00 +00:00
static void _GLCheckError(const std::string& _funcName)
{
const GLenum errorCode = glGetError();
if (errorCode != GL_NO_ERROR) {
#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
}
}
#else
#define GL_CHECK_ERROR(Function) (Function)
#endif
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
};
}
namespace Texture
{
enum Type {
2021-08-19 19:39:01 +00:00
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
ALPHA
};
}
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)
{
}
int x;
int y;
int w;
int h;
};
struct Vertex {
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)
{
}
glm::vec2 pos;
glm::vec2 tex;
unsigned int col;
2021-08-19 19:39:01 +00:00
float saturation{1.0};
float opacity{1.0};
unsigned int shaders{0};
};
bool init();
void deinit();
2021-08-19 19:39:01 +00:00
void pushClipRect(const glm::ivec2& pos, const glm::ivec2& size);
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,
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);
SDL_Window* getSDLWindow();
int getWindowWidth();
int getWindowHeight();
int getScreenWidth();
int getScreenHeight();
int getScreenOffsetX();
int getScreenOffsetY();
int getScreenRotate();
float getScreenWidthModifier();
float getScreenHeightModifier();
float getScreenAspectRatio();
unsigned int convertRGBAToABGR(unsigned int color);
unsigned int convertABGRToRGBA(unsigned int color);
Shader* getShaderProgram(unsigned int shaderID);
const glm::mat4 getProjectionMatrix();
void shaderPostprocessing(unsigned int shaders,
const Renderer::shaderParameters& parameters = shaderParameters(),
unsigned char* textureRGBA = nullptr);
static unsigned int getWindowFlags() { return SDL_WINDOW_OPENGL; }
void setupWindow();
bool createContext();
void destroyContext();
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);
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,
const unsigned y,
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);
void setSwapInterval();
void swapBuffers();
} // namespace Renderer
#endif // ES_CORE_RENDERER_RENDERER_H