OpenTTD Source: src/video/opengl.h Source File

OpenTTD Source 15.3

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
10#ifndef VIDEO_OPENGL_H
11#define VIDEO_OPENGL_H
12
13#include "../core/geometry_type.hpp"
14#include "../gfx_type.h"
15#include "../spriteloader/spriteloader.hpp"
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
29 class OpenGLBackend : public SpriteEncoder {
30private:
31 static OpenGLBackend *instance;
32
33 bool persistent_mapping_supported = false;
34 GLsync sync_vid_mapping{};
35 GLsync sync_anim_mapping{};
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;
57 GLint sprite_sprite_loc = 0;
58 GLint sprite_screen_loc = 0;
59 GLint sprite_zoom_loc = 0;
60 GLint sprite_rgb_loc = 0;
61 GLint sprite_crash_loc = 0;
62
63 OpenGLSpriteLRUCache cursor_cache;
64 PaletteID last_sprite_pal = (PaletteID)-1;
65 bool clear_cursor_cache = false;
66
67 Point cursor_pos{};
68 bool cursor_in_window = false;
69 std::vector<CursorSprite> cursor_sprites{};
70
71 OpenGLBackend();
72 ~OpenGLBackend();
73
74 std::optional<std::string_view> Init(const Dimension &screen_res);
75 bool InitShaders();
76
77 void InternalClearCursorCache();
78
79 void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
80
81public:
83 static inline OpenGLBackend *Get()
84 {
85 return OpenGLBackend::instance;
86 }
87 static std::optional<std::string_view> Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
88 static void Destroy();
89
90 void PrepareContext();
91
92 std::string GetDriverName();
93
94 void UpdatePalette(const Colour *pal, uint first, uint length);
95 bool Resize(int w, int h, bool force = false);
96 void Paint();
97
98 void DrawMouseCursor();
99 void PopulateCursorCache();
100 void ClearCursorCache();
101
102 void *GetVideoBuffer();
103 uint8_t *GetAnimBuffer();
104 void ReleaseVideoBuffer(const Rect &update_rect);
105 void ReleaseAnimBuffer(const Rect &update_rect);
106
107 /* SpriteEncoder */
108
109 bool Is32BppSupported() override { return true; }
110 uint GetSpriteAlignment() override { return 1u << to_underlying(ZoomLevel::Max); }
111 Sprite *Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override;
112};
113
114
116 class OpenGLSprite {
117private:
119 enum Texture : uint8_t {
120 TEX_RGBA,
121 TEX_REMAP,
122 NUM_TEX
123 };
124
125 Dimension dim{};
126 std::array<GLuint, NUM_TEX> tex{};
127 int16_t x_offs = 0;
128 int16_t y_offs = 0;
129
130 static std::array<GLuint, NUM_TEX> dummy_tex;
131
132 static GLuint pal_identity;
133 static GLuint pal_tex;
134 static GLuint pal_pbo;
135
136 static bool Create();
137 static void Destroy();
138
139 bool BindTextures() const;
140
141public:
142 OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);
143
144 /* No support for moving/copying the textures is implemented. */
145 OpenGLSprite(const OpenGLSprite&) = delete;
146 OpenGLSprite(OpenGLSprite&&) = delete;
147 OpenGLSprite& operator=(const OpenGLSprite&) = delete;
148 OpenGLSprite& operator=(OpenGLSprite&&) = delete;
149 ~OpenGLSprite();
150
151 void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
152 Dimension GetSize(ZoomLevel level) const;
153
154 friend class OpenGLBackend;
155};
156
157#endif /* VIDEO_OPENGL_H */
LRUCache< SpriteID, std::unique_ptr< OpenGLSprite > >
OpenGLBackend
Platform-independent back-end class for OpenGL video drivers.
Definition opengl.h:29
OpenGLBackend::sprite_rgb_loc
GLint sprite_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:60
OpenGLBackend::anim_buffer
void * anim_buffer
Pointer to the mapped animation buffer.
Definition opengl.h:46
OpenGLBackend::remap_program
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition opengl.h:50
OpenGLBackend::cursor_in_window
bool cursor_in_window
Cursor inside this window.
Definition opengl.h:68
OpenGLBackend::GetSpriteAlignment
uint GetSpriteAlignment() override
Get the value which the height and width on a sprite have to be aligned by.
Definition opengl.h:110
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition opengl.cpp:1039
OpenGLBackend::pal_program
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition opengl.h:41
OpenGLBackend::cursor_cache
OpenGLSpriteLRUCache cursor_cache
Cache of encoded cursor sprites.
Definition opengl.h:63
OpenGLBackend::cursor_sprites
std::vector< CursorSprite > cursor_sprites
Sprites comprising cursor.
Definition opengl.h:69
OpenGLBackend::remap_screen_loc
GLint remap_screen_loc
Uniform location for screen size.
Definition opengl.h:52
OpenGLBackend::instance
static OpenGLBackend * instance
Singleton instance pointer.
Definition opengl.h:31
OpenGLBackend::GetAnimBuffer
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition opengl.cpp:1170
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition opengl.cpp:1148
OpenGLBackend::persistent_mapping_supported
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition opengl.h:33
OpenGLBackend::vid_texture
GLuint vid_texture
Texture handle for the video buffer texture.
Definition opengl.h:39
OpenGLBackend::vao_quad
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition opengl.h:42
OpenGLBackend::sprite_zoom_loc
GLint sprite_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:59
OpenGLBackend::remap_zoom_loc
GLint remap_zoom_loc
Uniform location for sprite zoom.
Definition opengl.h:53
OpenGLBackend::Resize
bool Resize(int w, int h, bool force=false)
Change the size of the drawing window and allocate matching resources.
Definition opengl.cpp:913
OpenGLBackend::OpenGLBackend
OpenGLBackend()
Construct OpenGL back-end class.
Definition opengl.cpp:486
OpenGLBackend::Create
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:464
OpenGLBackend::vid_buffer
void * vid_buffer
Pointer to the mapped video buffer.
Definition opengl.h:37
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition opengl.cpp:1025
OpenGLBackend::anim_texture
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition opengl.h:48
OpenGLBackend::InternalClearCursorCache
void InternalClearCursorCache()
Clear all cached cursor sprites.
Definition opengl.cpp:1127
OpenGLBackend::Encode
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:1261
OpenGLBackend::sprite_program
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition opengl.h:56
OpenGLBackend::ReleaseAnimBuffer
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition opengl.cpp:1231
OpenGLBackend::vid_pbo
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition opengl.h:38
OpenGLBackend::sync_anim_mapping
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition opengl.h:35
OpenGLBackend::~OpenGLBackend
~OpenGLBackend()
Free allocated resources.
Definition opengl.cpp:493
OpenGLBackend::vbo_quad
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
Definition opengl.h:43
OpenGLBackend::clear_cursor_cache
bool clear_cursor_cache
A clear of the cursor cache is pending.
Definition opengl.h:65
OpenGLBackend::remap_rgb_loc
GLint remap_rgb_loc
Uniform location for RGB mode flag.
Definition opengl.h:54
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition opengl.cpp:1135
OpenGLBackend::vid_program
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition opengl.h:40
OpenGLBackend::pal_texture
GLuint pal_texture
Palette lookup texture.
Definition opengl.h:44
OpenGLBackend::Is32BppSupported
bool Is32BppSupported() override
Can the sprite encoder make use of RGBA sprites?
Definition opengl.h:109
OpenGLBackend::remap_sprite_loc
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:51
OpenGLBackend::Get
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition opengl.h:83
OpenGLBackend::InitShaders
bool InitShaders()
Create all needed shader programs.
Definition opengl.cpp:805
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition opengl.cpp:1071
OpenGLBackend::cursor_pos
Point cursor_pos
Cursor position.
Definition opengl.h:67
OpenGLBackend::sprite_crash_loc
GLint sprite_crash_loc
Uniform location for crash remap mode flag.
Definition opengl.h:61
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition opengl.cpp:1193
OpenGLBackend::sprite_sprite_loc
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition opengl.h:57
OpenGLBackend::RenderOglSprite
void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
Definition opengl.cpp:1277
OpenGLBackend::last_sprite_pal
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition opengl.h:64
OpenGLBackend::anim_pbo
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition opengl.h:47
OpenGLBackend::sync_vid_mapping
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition opengl.h:34
OpenGLBackend::Init
std::optional< std::string_view > Init(const Dimension &screen_res)
Check for the needed OpenGL functionality and allocate all resources.
Definition opengl.cpp:530
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition opengl.cpp:477
OpenGLBackend::sprite_screen_loc
GLint sprite_screen_loc
Uniform location for screen size.
Definition opengl.h:58
OpenGLSprite
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition opengl.h:116
OpenGLSprite::tex
std::array< GLuint, NUM_TEX > tex
The texture objects.
Definition opengl.h:126
OpenGLSprite::BindTextures
bool BindTextures() const
Bind textures for rendering this sprite.
Definition opengl.cpp:1521
OpenGLSprite::Texture
Texture
Enum of all used OpenGL texture objects.
Definition opengl.h:119
OpenGLSprite::TEX_RGBA
@ TEX_RGBA
RGBA texture part.
Definition opengl.h:120
OpenGLSprite::TEX_REMAP
@ TEX_REMAP
Remap texture part.
Definition opengl.h:121
OpenGLSprite::GetSize
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition opengl.cpp:1511
OpenGLSprite::Create
static bool Create()
Create all common resources for sprite rendering.
Definition opengl.cpp:1327
OpenGLSprite::pal_pbo
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition opengl.h:134
OpenGLSprite::pal_identity
static GLuint pal_identity
Identity texture mapping.
Definition opengl.h:132
OpenGLSprite::pal_tex
static GLuint pal_tex
Texture for palette remap.
Definition opengl.h:133
OpenGLSprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition opengl.h:128
OpenGLSprite::dummy_tex
static std::array< GLuint, NUM_TEX > dummy_tex
1x1 dummy textures to substitute for unused sprite components.
Definition opengl.h:1318
OpenGLSprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition opengl.h:127
OpenGLSprite::Update
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:1463
OpenGLSprite::Destroy
static void Destroy()
Free all common resources for sprite rendering.
Definition opengl.cpp:1388
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
SpriteEncoder
Interface for something that can encode a sprite.
Definition spriteloader.hpp:130
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
IsOpenGLVersionAtLeast
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
HasStringInExtensionList
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
Coord2D< int >
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:48
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
ZoomLevel::Max
@ Max
Maximum zoom level.