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
|
|
|
|
//
|
2022-03-14 18:51:48 +00:00
|
|
|
// Generic 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"
|
2021-08-17 20:11:16 +00:00
|
|
|
#include "utils/MathUtil.h"
|
2020-08-30 20:19:37 +00:00
|
|
|
|
2022-03-14 18:51:48 +00:00
|
|
|
#include <stack>
|
2020-08-30 20:19:37 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
struct SDL_Window;
|
|
|
|
|
2022-03-14 18:51:48 +00:00
|
|
|
class Renderer
|
2019-08-08 20:16:11 +00:00
|
|
|
{
|
2022-03-14 18:51:48 +00:00
|
|
|
public:
|
2022-03-14 21:30:24 +00:00
|
|
|
enum class TextureType {
|
|
|
|
RGBA, // Replace with AllowShortEnumsOnASingleLine: false (clang-format >=11.0).
|
|
|
|
BGRA,
|
|
|
|
RED
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class BlendFactor {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
enum Shader {
|
|
|
|
CORE = 0x00000001,
|
|
|
|
BLUR_HORIZONTAL = 0x00000002,
|
|
|
|
BLUR_VERTICAL = 0x00000004,
|
|
|
|
SCANLINES = 0x00000008
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ShaderFlags {
|
|
|
|
BGRA_TO_RGBA = 0x00000001,
|
|
|
|
FONT_TEXTURE = 0x00000002,
|
|
|
|
POST_PROCESSING = 0x00000004
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
struct Vertex {
|
2022-03-11 22:51:41 +00:00
|
|
|
glm::vec2 position;
|
2022-03-14 23:14:06 +00:00
|
|
|
glm::vec2 texcoord;
|
2022-03-11 22:51:41 +00:00
|
|
|
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-04-18 19:37:58 +00:00
|
|
|
float reflectionsFalloff;
|
2022-03-11 22:17:04 +00:00
|
|
|
unsigned int shaders;
|
2022-03-14 21:30:24 +00:00
|
|
|
unsigned int shaderFlags;
|
2022-03-11 22:17:04 +00:00
|
|
|
|
|
|
|
Vertex()
|
|
|
|
: opacity {1.0f}
|
|
|
|
, saturation {1.0f}
|
2022-03-11 22:51:41 +00:00
|
|
|
, dimming {1.0f}
|
2022-04-18 19:37:58 +00:00
|
|
|
, reflectionsFalloff {0.0f}
|
2022-03-11 22:17:04 +00:00
|
|
|
, shaders {0}
|
2022-03-14 21:30:24 +00:00
|
|
|
, shaderFlags {0}
|
2022-03-11 22:17:04 +00:00
|
|
|
{
|
|
|
|
}
|
2022-03-14 18:51:48 +00:00
|
|
|
|
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)
|
2022-03-14 23:14:06 +00:00
|
|
|
, texcoord(textureCoord)
|
2022-03-11 22:51:41 +00:00
|
|
|
, 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-04-18 19:37:58 +00:00
|
|
|
, reflectionsFalloff {0.0f}
|
2022-03-11 22:17:04 +00:00
|
|
|
, shaders {0}
|
2022-03-14 21:30:24 +00:00
|
|
|
, shaderFlags {0}
|
2021-07-07 18:31:46 +00:00
|
|
|
{
|
|
|
|
}
|
2020-06-26 15:17:35 +00:00
|
|
|
};
|
|
|
|
|
2022-03-14 18:51:48 +00:00
|
|
|
struct postProcessingParams {
|
|
|
|
float opacity;
|
|
|
|
float saturation;
|
|
|
|
float dimming;
|
|
|
|
unsigned int blurPasses;
|
|
|
|
unsigned int shaders;
|
|
|
|
|
|
|
|
postProcessingParams()
|
|
|
|
: opacity {1.0f}
|
|
|
|
, saturation {1.0f}
|
|
|
|
, dimming {1.0f}
|
|
|
|
, blurPasses {1}
|
|
|
|
, shaders {0}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Rect {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
static Renderer* getInstance();
|
|
|
|
|
|
|
|
void setIcon();
|
|
|
|
bool createWindow();
|
|
|
|
void destroyWindow();
|
2020-06-26 15:17:35 +00:00
|
|
|
bool init();
|
|
|
|
void deinit();
|
2022-03-14 18:51:48 +00:00
|
|
|
|
|
|
|
virtual bool loadShaders() = 0;
|
|
|
|
|
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();
|
2022-03-14 18:51:48 +00:00
|
|
|
|
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,
|
2022-03-14 18:51:48 +00:00
|
|
|
const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA,
|
|
|
|
const BlendFactor dstBlendFactor = BlendFactor::ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
const glm::mat4& getProjectionMatrix()
|
|
|
|
{
|
|
|
|
if (mScreenRotated)
|
|
|
|
return mProjectionMatrixRotated;
|
|
|
|
else
|
|
|
|
return mProjectionMatrix;
|
|
|
|
}
|
|
|
|
const glm::mat4& getProjectionMatrixNormal() { return mProjectionMatrix; }
|
|
|
|
SDL_Window* getSDLWindow() { return mSDLWindow; }
|
|
|
|
const bool getScreenRotated() { return mScreenRotated; }
|
|
|
|
const float getWindowWidth() { return static_cast<float>(mWindowWidth); }
|
|
|
|
const float getWindowHeight() { return static_cast<float>(mWindowHeight); }
|
|
|
|
static const float getScreenWidth() { return static_cast<float>(sScreenWidth); }
|
|
|
|
static const float getScreenHeight() { return static_cast<float>(sScreenHeight); }
|
|
|
|
static const float getScreenWidthModifier() { return sScreenWidthModifier; }
|
|
|
|
static const float getScreenHeightModifier() { return sScreenHeightModifier; }
|
|
|
|
static const float getScreenAspectRatio() { return sScreenAspectRatio; }
|
|
|
|
|
|
|
|
static constexpr glm::mat4 getIdentity() { return glm::mat4 {1.0f}; }
|
|
|
|
glm::mat4 mTrans {getIdentity()};
|
|
|
|
|
|
|
|
virtual void shaderPostprocessing(
|
2022-03-11 22:17:04 +00:00
|
|
|
const unsigned int shaders,
|
|
|
|
const Renderer::postProcessingParams& parameters = postProcessingParams(),
|
2022-03-14 18:51:48 +00:00
|
|
|
unsigned char* textureRGBA = nullptr) = 0;
|
|
|
|
|
|
|
|
virtual void setup() = 0;
|
|
|
|
virtual bool createContext() = 0;
|
|
|
|
virtual void destroyContext() = 0;
|
|
|
|
virtual unsigned int createTexture(const TextureType type,
|
|
|
|
const bool linearMinify,
|
|
|
|
const bool linearMagnify,
|
|
|
|
const bool repeat,
|
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
|
|
|
void* data) = 0;
|
|
|
|
virtual void destroyTexture(const unsigned int texture) = 0;
|
|
|
|
virtual void updateTexture(const unsigned int texture,
|
|
|
|
const TextureType type,
|
|
|
|
const unsigned int x,
|
|
|
|
const unsigned int y,
|
2021-08-19 18:16:42 +00:00
|
|
|
const unsigned int width,
|
|
|
|
const unsigned int height,
|
2022-03-14 18:51:48 +00:00
|
|
|
void* data) = 0;
|
|
|
|
virtual void bindTexture(const unsigned int texture) = 0;
|
|
|
|
virtual void drawTriangleStrips(
|
|
|
|
const Vertex* vertices,
|
|
|
|
const unsigned int numVertices,
|
|
|
|
const BlendFactor srcBlendFactor = BlendFactor::SRC_ALPHA,
|
|
|
|
const BlendFactor dstBlendFactor = BlendFactor::ONE_MINUS_SRC_ALPHA) = 0;
|
|
|
|
virtual void setMatrix(const glm::mat4& matrix) = 0;
|
|
|
|
virtual void setScissor(const Rect& scissor) = 0;
|
|
|
|
virtual void setSwapInterval() = 0;
|
|
|
|
virtual void swapBuffers() = 0;
|
|
|
|
|
2022-03-14 21:30:24 +00:00
|
|
|
private:
|
2022-03-14 18:51:48 +00:00
|
|
|
std::stack<Rect> mClipStack;
|
|
|
|
SDL_Window* mSDLWindow {nullptr};
|
|
|
|
glm::mat4 mProjectionMatrix {};
|
|
|
|
glm::mat4 mProjectionMatrixRotated {};
|
|
|
|
int mWindowWidth {0};
|
|
|
|
int mWindowHeight {0};
|
|
|
|
static inline int sScreenWidth {0};
|
|
|
|
static inline int sScreenHeight {0};
|
|
|
|
int mScreenOffsetX {0};
|
|
|
|
int mScreenOffsetY {0};
|
|
|
|
bool mScreenRotated {0};
|
|
|
|
bool mInitialCursorState {1};
|
|
|
|
// Screen resolution modifiers relative to the 1920x1080 reference.
|
|
|
|
static inline float sScreenHeightModifier {0.0f};
|
|
|
|
static inline float sScreenWidthModifier {0.0f};
|
|
|
|
static inline float sScreenAspectRatio {0.0f};
|
|
|
|
};
|
2019-08-08 20:16:11 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_RENDERER_RENDERER_H
|