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

219 lines
6.5 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
{
// 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;
float dimming;
bool convertBGRAToRGBA;
unsigned int blurPasses;
unsigned int shaders;
postProcessingParams()
: opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, blurPasses {1}
, shaders {0}
{
}
};
static std::vector<Shader*> sShaderProgramVector;
static GLuint shaderFBO1;
static GLuint shaderFBO2;
// This is simply to get rid of a GCC false positive -Wunused-variable compiler warning.
static GLuint shaderFBODummy1 {shaderFBO1};
static GLuint shaderFBODummy2 {shaderFBO2};
static constexpr glm::mat4 getIdentity() { return glm::mat4 {1.0f}; }
static inline glm::mat4 mTrans {getIdentity()};
#if !defined(NDEBUG)
#define GL_CHECK_ERROR(Function) (Function, _GLCheckError(#Function))
static inline 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) << "OpenGL ES 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 {
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
BGRA,
2021-08-19 19:39:01 +00:00
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 {
glm::vec2 position;
glm::vec2 texture;
unsigned int color;
float opacity;
float saturation;
float dimming;
bool convertBGRAToRGBA;
bool postProcessing;
unsigned int shaders;
Vertex()
: opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, postProcessing {false}
, shaders {0}
{
}
2021-08-19 19:39:01 +00:00
Vertex(const glm::vec2& position, const glm::vec2& textureCoord, const unsigned int color)
: position(position)
, texture(textureCoord)
, color(color)
, opacity {1.0f}
, saturation {1.0f}
, dimming {1.0f}
, convertBGRAToRGBA {false}
, postProcessing {false}
, 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 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);
SDL_Window* getSDLWindow();
const float getWindowWidth();
const float getWindowHeight();
const float getScreenWidth();
const float getScreenHeight();
const float getScreenOffsetX();
const float getScreenOffsetY();
const bool getScreenRotated();
const float getScreenWidthModifier();
const float getScreenHeightModifier();
const float getScreenAspectRatio();
Shader* getShaderProgram(unsigned int shaderID);
const glm::mat4& getProjectionMatrix();
const glm::mat4& getProjectionMatrixNormal();
void shaderPostprocessing(
const unsigned int shaders,
const Renderer::postProcessingParams& parameters = postProcessingParams(),
unsigned char* textureRGBA = nullptr);
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 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 drawTriangleStrips(const Vertex* vertices,
const unsigned int numVertices,
const Blend::Factor srcBlendFactor = Blend::SRC_ALPHA,
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);
void setSwapInterval();
void swapBuffers();
} // namespace Renderer
#endif // ES_CORE_RENDERER_RENDERER_H