kodi
RenderSystemGL.h
1 /*
2  * Copyright (C) 2005-2024 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "GLShader.h"
12 #include "rendering/RenderSystem.h"
13 #include "utils/ColorUtils.h"
14 #include "utils/Map.h"
15 
16 #include <map>
17 #include <memory>
18 
19 #include <fmt/format.h>
20 
21 #include "system_gl.h"
22 
23 enum class ShaderMethodGL
24 {
25  SM_DEFAULT = 0,
26  SM_TEXTURE,
27  SM_TEXTURE_LIM,
28  SM_MULTI,
29  SM_FONTS,
30  SM_FONTS_SHADER_CLIP,
31  SM_TEXTURE_NOBLEND,
32  SM_MULTI_BLENDCOLOR,
33  SM_MAX
34 };
35 
36 template<>
37 struct fmt::formatter<ShaderMethodGL> : fmt::formatter<std::string_view>
38 {
39  template<typename FormatContext>
40  constexpr auto format(const ShaderMethodGL& shaderMethod, FormatContext& ctx)
41  {
42  const auto it = ShaderMethodGLMap.find(shaderMethod);
43  if (it == ShaderMethodGLMap.cend())
44  throw std::range_error("no string mapping found for shader method");
45 
46  return fmt::formatter<string_view>::format(it->second, ctx);
47  }
48 
49 private:
50  static constexpr auto ShaderMethodGLMap = make_map<ShaderMethodGL, std::string_view>({
51  {ShaderMethodGL::SM_DEFAULT, "default"},
52  {ShaderMethodGL::SM_TEXTURE, "texture"},
53  {ShaderMethodGL::SM_TEXTURE_LIM, "texture limited"},
54  {ShaderMethodGL::SM_MULTI, "multi"},
55  {ShaderMethodGL::SM_FONTS, "fonts"},
56  {ShaderMethodGL::SM_FONTS_SHADER_CLIP, "fonts with vertex shader based clipping"},
57  {ShaderMethodGL::SM_TEXTURE_NOBLEND, "texture no blending"},
58  {ShaderMethodGL::SM_MULTI_BLENDCOLOR, "multi blend colour"},
59  });
60 
61  static_assert(static_cast<size_t>(ShaderMethodGL::SM_MAX) == ShaderMethodGLMap.size(),
62  "ShaderMethodGLMap doesn't match the size of ShaderMethodGL, did you forget to "
63  "add/remove a mapping?");
64 };
65 
67 {
68 public:
70  ~CRenderSystemGL() override;
71  bool InitRenderSystem() override;
72  bool DestroyRenderSystem() override;
73  bool ResetRenderSystem(int width, int height) override;
74 
75  bool BeginRender() override;
76  bool EndRender() override;
77  void PresentRender(bool rendered, bool videoLayer) override;
78  bool ClearBuffers(UTILS::COLOR::Color color) override;
79  bool IsExtSupported(const char* extension) const override;
80 
81  void SetVSync(bool vsync);
82  void ResetVSync() { m_bVsyncInit = false; }
83 
84  void SetViewPort(const CRect& viewPort) override;
85  void GetViewPort(CRect& viewPort) override;
86 
87  bool ScissorsCanEffectClipping() override;
88  CRect ClipRectToScissorRect(const CRect &rect) override;
89  void SetScissors(const CRect &rect) override;
90  void ResetScissors() override;
91 
92  void CaptureStateBlock() override;
93  void ApplyStateBlock() override;
94 
95  void SetCameraPosition(const CPoint &camera, int screenWidth, int screenHeight, float stereoFactor = 0.0f) override;
96 
97  void SetStereoMode(RENDER_STEREO_MODE mode, RENDER_STEREO_VIEW view) override;
98  bool SupportsStereo(RENDER_STEREO_MODE mode) const override;
99  bool SupportsNPOT(bool dxt) const override;
100 
101  void Project(float &x, float &y, float &z) override;
102 
103  std::string GetShaderPath(const std::string &filename) override;
104 
105  void GetGLVersion(int& major, int& minor);
106  void GetGLSLVersion(int& major, int& minor);
107 
108  void ResetGLErrors();
109 
110  // shaders
111  void EnableShader(ShaderMethodGL method);
112  void DisableShader();
113  GLint ShaderGetPos();
114  GLint ShaderGetCol();
115  GLint ShaderGetCoord0();
116  GLint ShaderGetCoord1();
117  GLint ShaderGetUniCol();
118  GLint ShaderGetModel();
119  GLint ShaderGetMatrix();
120  GLint ShaderGetClip();
121  GLint ShaderGetCoordStep();
122 
123 protected:
124  virtual void SetVSyncImpl(bool enable) = 0;
125  virtual void PresentRenderImpl(bool rendered) = 0;
126  void CalculateMaxTexturesize();
127  void InitialiseShaders();
128  void ReleaseShaders();
129 
130  bool m_bVsyncInit = false;
131  int m_width;
132  int m_height;
133 
134  std::string m_RenderExtensions;
135 
136  int m_glslMajor = 0;
137  int m_glslMinor = 0;
138 
139  GLint m_viewPort[4];
140 
141  std::map<ShaderMethodGL, std::unique_ptr<CGLShader>> m_pShader;
142  ShaderMethodGL m_method = ShaderMethodGL::SM_DEFAULT;
143  GLuint m_vertexArray = GL_NONE;
144 };
Definition: RenderSystemGL.h:66
Definition: RenderSystem.h:27