Add: Overlay cargo icon in vehicle/depot list when holding shift+ctrl. (#12938)
[openttd-github.git] / src / video / opengl.h
blob14633c44cd9313ca51e1ae08162c3962954335e3
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file opengl.h OpenGL video driver support. */
10 #ifndef VIDEO_OPENGL_H
11 #define VIDEO_OPENGL_H
13 #include "../core/alloc_type.hpp"
14 #include "../core/geometry_type.hpp"
15 #include "../gfx_type.h"
16 #include "../spriteloader/spriteloader.hpp"
17 #include "../misc/lrucache.hpp"
19 typedef void (*OGLProc)();
20 typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
22 bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor);
23 const char *FindStringInExtensionList(const char *string, const char *substring);
25 class OpenGLSprite;
27 /** Platform-independent back-end class for OpenGL video drivers. */
28 class OpenGLBackend : public ZeroedMemoryAllocator, SpriteEncoder {
29 private:
30 static OpenGLBackend *instance; ///< Singleton instance pointer.
32 bool persistent_mapping_supported; ///< Persistent pixel buffer mapping supported.
33 GLsync sync_vid_mapping; ///< Sync object for the persistently mapped video buffer.
34 GLsync sync_anim_mapping; ///< Sync object for the persistently mapped animation buffer.
36 void *vid_buffer; ///< Pointer to the mapped video buffer.
37 GLuint vid_pbo; ///< Pixel buffer object storing the memory used for the video driver to draw to.
38 GLuint vid_texture; ///< Texture handle for the video buffer texture.
39 GLuint vid_program; ///< Shader program for rendering a RGBA video buffer.
40 GLuint pal_program; ///< Shader program for rendering a paletted video buffer.
41 GLuint vao_quad; ///< Vertex array object storing the rendering state for the fullscreen quad.
42 GLuint vbo_quad; ///< Vertex buffer with a fullscreen quad.
43 GLuint pal_texture; ///< Palette lookup texture.
45 void *anim_buffer; ///< Pointer to the mapped animation buffer.
46 GLuint anim_pbo; ///< Pixel buffer object storing the memory used for the animation buffer.
47 GLuint anim_texture; ///< Texture handle for the animation buffer texture.
49 GLuint remap_program; ///< Shader program for blending and rendering a RGBA + remap texture.
50 GLint remap_sprite_loc; ///< Uniform location for sprite parameters.
51 GLint remap_screen_loc; ///< Uniform location for screen size.
52 GLint remap_zoom_loc; ///< Uniform location for sprite zoom.
53 GLint remap_rgb_loc; ///< Uniform location for RGB mode flag.
55 GLuint sprite_program; ///< Shader program for blending and rendering a sprite to the video buffer.
56 GLint sprite_sprite_loc; ///< Uniform location for sprite parameters.
57 GLint sprite_screen_loc; ///< Uniform location for screen size.
58 GLint sprite_zoom_loc; ///< Uniform location for sprite zoom.
59 GLint sprite_rgb_loc; ///< Uniform location for RGB mode flag.
60 GLint sprite_crash_loc; ///< Uniform location for crash remap mode flag.
62 LRUCache<SpriteID, Sprite> cursor_cache; ///< Cache of encoded cursor sprites.
63 PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette.
64 bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending.
66 Point cursor_pos; ///< Cursor position
67 bool cursor_in_window; ///< Cursor inside this window
68 std::vector<CursorSprite> cursor_sprites; ///< Sprites comprising cursor
70 OpenGLBackend();
71 ~OpenGLBackend();
73 std::optional<std::string_view> Init(const Dimension &screen_res);
74 bool InitShaders();
76 void InternalClearCursorCache();
78 void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
80 public:
81 /** Get singleton instance of this class. */
82 static inline OpenGLBackend *Get()
84 return OpenGLBackend::instance;
86 static std::optional<std::string_view> Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
87 static void Destroy();
89 void PrepareContext();
91 std::string GetDriverName();
93 void UpdatePalette(const Colour *pal, uint first, uint length);
94 bool Resize(int w, int h, bool force = false);
95 void Paint();
97 void DrawMouseCursor();
98 void PopulateCursorCache();
99 void ClearCursorCache();
101 void *GetVideoBuffer();
102 uint8_t *GetAnimBuffer();
103 void ReleaseVideoBuffer(const Rect &update_rect);
104 void ReleaseAnimBuffer(const Rect &update_rect);
106 /* SpriteEncoder */
108 bool Is32BppSupported() override { return true; }
109 uint GetSpriteAlignment() override { return 1u << (ZOOM_LVL_END - 1); }
110 Sprite *Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override;
114 /** Class that encapsulates a RGBA texture together with a paletted remap texture. */
115 class OpenGLSprite {
116 private:
117 /** Enum of all used OpenGL texture objects. */
118 enum Texture {
119 TEX_RGBA, ///< RGBA texture part.
120 TEX_REMAP, ///< Remap texture part.
121 NUM_TEX
124 Dimension dim;
125 GLuint tex[NUM_TEX]; ///< The texture objects.
127 static GLuint dummy_tex[NUM_TEX]; ///< 1x1 dummy textures to substitute for unused sprite components.
129 static GLuint pal_identity; ///< Identity texture mapping.
130 static GLuint pal_tex; ///< Texture for palette remap.
131 static GLuint pal_pbo; ///< Pixel buffer object for remap upload.
133 static bool Create();
134 static void Destroy();
136 bool BindTextures();
138 public:
139 OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components);
140 ~OpenGLSprite();
142 void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
143 Dimension GetSize(ZoomLevel level) const;
145 friend class OpenGLBackend;
148 #endif /* VIDEO_OPENGL_H */