OpenTTD Source 20260311-master-g511d3794ce
opengl.h
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef VIDEO_OPENGL_H
11#define VIDEO_OPENGL_H
12
14#include "../gfx_type.h"
16#include "../misc/lrucache.hpp"
17
18typedef void (*OGLProc)();
19typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
20
21bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor);
22bool HasStringInExtensionList(std::string_view string, std::string_view substring);
23
24class OpenGLSprite;
25
26using OpenGLSpriteLRUCache = LRUCache<SpriteID, std::unique_ptr<OpenGLSprite>>;
27
30private:
32
36
37 void *vid_buffer = nullptr;
38 GLuint vid_pbo = 0;
39 GLuint vid_texture = 0;
40 GLuint vid_program = 0;
41 GLuint pal_program = 0;
42 GLuint vao_quad = 0;
43 GLuint vbo_quad = 0;
44 GLuint pal_texture = 0;
45
46 void *anim_buffer = nullptr;
47 GLuint anim_pbo = 0;
48 GLuint anim_texture = 0;
49
50 GLuint remap_program = 0;
51 GLint remap_sprite_loc = 0;
52 GLint remap_screen_loc = 0;
53 GLint remap_zoom_loc = 0;
54 GLint remap_rgb_loc = 0;
55
56 GLuint sprite_program = 0;
59 GLint sprite_zoom_loc = 0;
60 GLint sprite_rgb_loc = 0;
61 GLint sprite_crash_loc = 0;
62
63 OpenGLSpriteLRUCache cursor_cache;
65 bool clear_cursor_cache = false;
66
68 bool cursor_in_window = false;
69 std::vector<CursorSprite> cursor_sprites{};
70
72 ~OpenGLBackend() override;
73
74 std::optional<std::string_view> Init(const Dimension &screen_res);
75 bool InitShaders();
76
78
79 void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
80
81public:
86 static inline OpenGLBackend *Get()
87 {
89 }
90
91 static std::optional<std::string_view> Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
92 static void Destroy();
93
94 void PrepareContext();
95
96 std::string GetDriverName();
97
98 void UpdatePalette(const Colour *pal, uint first, uint length);
99 bool Resize(int w, int h, bool force = false);
100 void Paint();
101
102 void DrawMouseCursor();
103 void PopulateCursorCache();
104 void ClearCursorCache();
105
106 void *GetVideoBuffer();
107 uint8_t *GetAnimBuffer();
108 void ReleaseVideoBuffer(const Rect &update_rect);
109 void ReleaseAnimBuffer(const Rect &update_rect);
110
111 /* SpriteEncoder */
112
113 bool Is32BppSupported() override { return true; }
114 uint GetSpriteAlignment() override { return 1u << to_underlying(ZoomLevel::Max); }
115 Sprite *Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override;
116};
117
118
121private:
123 enum Texture : uint8_t {
126 NUM_TEX
127 };
128
129 Dimension dim{};
130 std::array<GLuint, NUM_TEX> tex{};
131 int16_t x_offs = 0;
132 int16_t y_offs = 0;
133
134 static std::array<GLuint, NUM_TEX> dummy_tex;
135
136 static GLuint pal_identity;
137 static GLuint pal_tex;
138 static GLuint pal_pbo;
139
140 static bool Create();
141 static void Destroy();
142
143 bool BindTextures() const;
144
145public:
146 OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);
147
148 /* No support for moving/copying the textures is implemented. */
149 OpenGLSprite(const OpenGLSprite&) = delete;
150 OpenGLSprite(OpenGLSprite&&) = delete;
151 OpenGLSprite& operator=(const OpenGLSprite&) = delete;
152 OpenGLSprite& operator=(OpenGLSprite&&) = delete;
154
155 void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
156 Dimension GetSize(ZoomLevel level) const;
157
158 friend class OpenGLBackend;
159};
160
161#endif /* VIDEO_OPENGL_H */
Size limited cache with a least recently used eviction strategy.
Definition lrucache.hpp:23
GLint sprite_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:60
void * anim_buffer
Pointer to the mapped animation buffer.
Definition opengl.h:46
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition opengl.h:50
bool cursor_in_window
Cursor inside this window.
Definition opengl.h:68
uint GetSpriteAlignment() override
Get the value which the height and width on a sprite have to be aligned by.
Definition opengl.h:114
void Paint()
Render video buffer to the screen.
Definition opengl.cpp:1065
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition opengl.h:41
OpenGLSpriteLRUCache cursor_cache
Cache of encoded cursor sprites.
Definition opengl.h:63
std::vector< CursorSprite > cursor_sprites
Sprites comprising cursor.
Definition opengl.h:69
GLint remap_screen_loc
Uniform location for screen size.
Definition opengl.h:52
~OpenGLBackend() override
Free allocated resources.
Definition opengl.cpp:519
static OpenGLBackend * instance
Singleton instance pointer.
Definition opengl.h:31
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1196
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1174
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition opengl.h:33
GLuint vid_texture
Texture handle for the video buffer texture.
Definition opengl.h:39
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition opengl.h:42
GLint sprite_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:59
GLint remap_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:53
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition opengl.cpp:939
OpenGLBackend()
Construct OpenGL back-end class.
Definition opengl.cpp:512
static std::optional< std::string_view > Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition opengl.cpp:490
void * vid_buffer
Pointer to the mapped video buffer.
Definition opengl.h:37
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1051
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition opengl.h:48
void InternalClearCursorCache()
Clear all cached cursor sprites.
Definition opengl.cpp:1153
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
Definition opengl.cpp:1287
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition opengl.h:56
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1257
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition opengl.h:38
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition opengl.h:35
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
Definition opengl.h:43
bool clear_cursor_cache
A clear of the cursor cache is pending.
Definition opengl.h:65
GLint remap_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:54
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1161
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition opengl.h:40
GLuint pal_texture
Palette lookup texture.
Definition opengl.h:44
bool Is32BppSupported() override
Can the sprite encoder make use of RGBA sprites?
Definition opengl.h:113
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:51
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition opengl.h:86
bool InitShaders()
Create all needed shader programs.
Definition opengl.cpp:831
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1097
Point cursor_pos
Cursor position.
Definition opengl.h:67
GLint sprite_crash_loc
Uniform location for crash remap mode flag.
Definition opengl.h:61
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1219
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:57
void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
Definition opengl.cpp:1304
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition opengl.h:64
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition opengl.h:47
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition opengl.h:34
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:503
GLint sprite_screen_loc
Uniform location for screen size.
Definition opengl.h:58
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition opengl.h:120
std::array< GLuint, NUM_TEX > tex
The texture objects.
Definition opengl.h:130
bool BindTextures() const
Bind textures for rendering this sprite.
Definition opengl.cpp:1550
OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite)
Create an OpenGL sprite with a palette remap part.
Definition opengl.cpp:1428
Texture
Enum of all used OpenGL texture objects.
Definition opengl.h:123
@ TEX_RGBA
RGBA texture part.
Definition opengl.h:124
@ TEX_REMAP
Remap texture part.
Definition opengl.h:125
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition opengl.cpp:1540
static bool Create()
Create all common resources for sprite rendering.
Definition opengl.cpp:1354
~OpenGLSprite()
Delete the textures we allocated.
Definition opengl.cpp:1480
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition opengl.h:138
static GLuint pal_identity
Identity texture mapping.
Definition opengl.h:136
static GLuint pal_tex
Texture for palette remap.
Definition opengl.h:137
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition opengl.h:132
static std::array< GLuint, NUM_TEX > dummy_tex
1x1 dummy textures to substitute for unused sprite components.
Definition opengl.h:1345
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition opengl.h:131
void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data)
Update a single mip-map level with new pixel data.
Definition opengl.cpp:1492
static void Destroy()
Free all common resources for sprite rendering.
Definition opengl.cpp:1415
Interface for something that can allocate memory for a sprite.
Interface for something that can encode a sprite.
SpriteCollMap< Sprite > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
All geometry types in OpenTTD.
Types related to the graphics and/or input devices.
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Size limited cache map with a least recently used eviction strategy.
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
@ Init
Second step of NewGRF loading; load all actions into memory.
Definition newgrf.h:51
bool IsOpenGLVersionAtLeast(uint8_t major, uint8_t minor)
Check if the current OpenGL version is equal or higher than a given one.
Definition opengl.cpp:204
bool HasStringInExtensionList(std::string_view string, std::string_view substring)
Find a substring in a string made of space delimited elements.
Definition opengl.cpp:150
Base for loading sprites.
Dimensions (a width and height) of a rectangle in 2D.
Definition of a common pixel in OpenTTD's realm.
Data structure describing a sprite.
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
@ Max
Maximum zoom level.
Definition zoom_type.h:30