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

OpenTTD Source 14.0-RC3

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef VIDEO_OPENGL_H
11 #define VIDEO_OPENGL_H
12
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"
18
19 typedef void (*OGLProc)();
20 typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
21
22 bool IsOpenGLVersionAtLeast(byte major, byte minor);
23 const char *FindStringInExtensionList(const char *string, const char *substring);
24
25 class OpenGLSprite;
26
28 class OpenGLBackend : public ZeroedMemoryAllocator, SpriteEncoder {
29 private:
30 static OpenGLBackend *instance;
31
32 bool persistent_mapping_supported;
33 GLsync sync_vid_mapping;
34 GLsync sync_anim_mapping;
35
36 void *vid_buffer;
37 GLuint vid_pbo;
38 GLuint vid_texture;
39 GLuint vid_program;
40 GLuint pal_program;
41 GLuint vao_quad;
42 GLuint vbo_quad;
43 GLuint pal_texture;
44
45 void *anim_buffer;
46 GLuint anim_pbo;
47 GLuint anim_texture;
48
49 GLuint remap_program;
50 GLint remap_sprite_loc;
51 GLint remap_screen_loc;
52 GLint remap_zoom_loc;
53 GLint remap_rgb_loc;
54
55 GLuint sprite_program;
56 GLint sprite_sprite_loc;
57 GLint sprite_screen_loc;
58 GLint sprite_zoom_loc;
59 GLint sprite_rgb_loc;
60 GLint sprite_crash_loc;
61
62 LRUCache<SpriteID, Sprite> cursor_cache;
63 PaletteID last_sprite_pal = (PaletteID)-1;
64 bool clear_cursor_cache = false;
65
66 Point cursor_pos;
67 bool cursor_in_window;
68 PalSpriteID cursor_sprite_seq[16];
69 Point cursor_sprite_pos[16];
70 uint cursor_sprite_count;
71
72 OpenGLBackend();
73 ~OpenGLBackend();
74
75 const char *Init(const Dimension &screen_res);
76 bool InitShaders();
77
78 void InternalClearCursorCache();
79
80 void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
81
82 public:
84 static inline OpenGLBackend *Get()
85 {
86 return OpenGLBackend::instance;
87 }
88 static const char *Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
89 static void Destroy();
90
91 void PrepareContext();
92
93 std::string GetDriverName();
94
95 void UpdatePalette(const Colour *pal, uint first, uint length);
96 bool Resize(int w, int h, bool force = false);
97 void Paint();
98
99 void DrawMouseCursor();
100 void PopulateCursorCache();
101 void ClearCursorCache();
102
103 void *GetVideoBuffer();
104 uint8_t *GetAnimBuffer();
105 void ReleaseVideoBuffer(const Rect &update_rect);
106 void ReleaseAnimBuffer(const Rect &update_rect);
107
108 /* SpriteEncoder */
109
110 bool Is32BppSupported() override { return true; }
111 uint GetSpriteAlignment() override { return 1u << (ZOOM_LVL_END - 1); }
112 Sprite *Encode(const SpriteLoader::SpriteCollection &sprite, AllocatorProc *allocator) override;
113 };
114
115
117 class OpenGLSprite {
118 private:
120 enum Texture {
121 TEX_RGBA,
122 TEX_REMAP,
123 NUM_TEX
124 };
125
126 Dimension dim;
127 GLuint tex[NUM_TEX];
128
129 static GLuint dummy_tex[NUM_TEX];
130
131 static GLuint pal_identity;
132 static GLuint pal_tex;
133 static GLuint pal_pbo;
134
135 static bool Create();
136 static void Destroy();
137
138 bool BindTextures();
139
140 public:
141 OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components);
142 ~OpenGLSprite();
143
144 void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
145 Dimension GetSize(ZoomLevel level) const;
146
147 friend class OpenGLBackend;
148 };
149
150 #endif /* VIDEO_OPENGL_H */
OpenGLBackend::cursor_pos
Point cursor_pos
Cursor position.
Definition: opengl.h:66
OpenGLBackend::sync_vid_mapping
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition: opengl.h:33
OpenGLBackend::RenderOglSprite
void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
Render a sprite to the back buffer.
Definition: opengl.cpp:1295
OpenGLBackend::GetSpriteAlignment
uint GetSpriteAlignment() override
Get the value which the height and width on a sprite have to be aligned by.
Definition: opengl.h:111
OpenGLSprite::pal_tex
static GLuint pal_tex
Texture for palette remap.
Definition: opengl.h:132
OpenGLBackend::pal_program
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition: opengl.h:40
OpenGLSprite::TEX_RGBA
@ TEX_RGBA
RGBA texture part.
Definition: opengl.h:121
OpenGLBackend::Is32BppSupported
bool Is32BppSupported() override
Can the sprite encoder make use of RGBA sprites?
Definition: opengl.h:110
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:30
OpenGLSprite::GetSize
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition: opengl.cpp:1522
OpenGLSprite::pal_identity
static GLuint pal_identity
Identity texture mapping.
Definition: opengl.h:131
OpenGLSprite::Destroy
static void Destroy()
Free all common resources for sprite rendering.
Definition: opengl.cpp:1406
ZOOM_LVL_END
@ ZOOM_LVL_END
End for iteration.
Definition: zoom_type.h:28
OpenGLSprite::tex
GLuint tex[NUM_TEX]
The texture objects.
Definition: opengl.h:127
OpenGLSprite::OpenGLSprite
OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
Create an OpenGL sprite with a palette remap part.
Definition: opengl.cpp:1421
OpenGLSprite::TEX_REMAP
@ TEX_REMAP
Remap texture part.
Definition: opengl.h:122
OpenGLBackend::GetAnimBuffer
uint8_t * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition: opengl.cpp:1176
OpenGLBackend::vid_buffer
void * vid_buffer
Pointer to the mapped video buffer.
Definition: opengl.h:36
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:19
OpenGLSprite::pal_pbo
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition: opengl.h:133
OpenGLBackend::~OpenGLBackend
~OpenGLBackend()
Free allocated resources.
Definition: opengl.cpp:496
PaletteID
uint32_t PaletteID
The number of the palette.
Definition: gfx_type.h:18
OpenGLBackend::sprite_zoom_loc
GLint sprite_zoom_loc
Uniform location for sprite zoom.
Definition: opengl.h:58
SpriteLoader::SpriteCollection
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
Definition: spriteloader.hpp:71
OpenGLBackend::Get
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition: opengl.h:84
LRUCache< SpriteID, Sprite >
OpenGLBackend::anim_pbo
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition: opengl.h:46
OpenGLBackend
Platform-independent back-end class for OpenGL video drivers.
Definition: opengl.h:28
OpenGLBackend::vid_texture
GLuint vid_texture
Texture handle for the video buffer texture.
Definition: opengl.h:38
OpenGLBackend::remap_rgb_loc
GLint remap_rgb_loc
Uniform location for RGB mode flag.
Definition: opengl.h:53
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition: opengl.cpp:1071
OpenGLBackend::last_sprite_pal
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition: opengl.h:63
OpenGLBackend::anim_buffer
void * anim_buffer
Pointer to the mapped animation buffer.
Definition: opengl.h:45
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition: opengl.cpp:1199
ZeroedMemoryAllocator
Base class that provides memory initialization on dynamically created objects.
Definition: alloc_type.hpp:85
OpenGLBackend::vbo_quad
GLuint vbo_quad
Vertex buffer with a fullscreen quad.
Definition: opengl.h:42
OpenGLSprite::Create
static bool Create()
Create all common resources for sprite rendering.
Definition: opengl.cpp:1345
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition: opengl.cpp:1154
OpenGLBackend::instance
static OpenGLBackend * instance
Singleton instance pointer.
Definition: opengl.h:30
OpenGLBackend::cursor_in_window
bool cursor_in_window
Cursor inside this window.
Definition: opengl.h:67
OpenGLBackend::anim_texture
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition: opengl.h:47
OpenGLBackend::sprite_screen_loc
GLint sprite_screen_loc
Uniform location for screen size.
Definition: opengl.h:57
OpenGLBackend::Create
static const char * Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res)
Create and initialize the singleton back-end class.
Definition: opengl.cpp:467
OpenGLBackend::Encode
Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: opengl.cpp:1267
OpenGLBackend::sync_anim_mapping
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition: opengl.h:34
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition: opengl.cpp:480
SpriteEncoder
Interface for something that can encode a sprite.
Definition: spriteloader.hpp:89
OpenGLBackend::persistent_mapping_supported
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition: opengl.h:32
OpenGLBackend::cursor_cache
LRUCache< SpriteID, Sprite > cursor_cache
Cache of encoded cursor sprites.
Definition: opengl.h:62
OpenGLBackend::cursor_sprite_seq
PalSpriteID cursor_sprite_seq[16]
Current image of cursor.
Definition: opengl.h:68
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:159
OpenGLBackend::vao_quad
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition: opengl.h:41
OpenGLSprite::dummy_tex
static GLuint dummy_tex[NUM_TEX]
1x1 dummy textures to substitute for unused sprite components.
Definition: opengl.h:129
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Queue a request for cursor cache clear.
Definition: opengl.cpp:1141
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:1474
OpenGLBackend::pal_texture
GLuint pal_texture
Palette lookup texture.
Definition: opengl.h:43
FindStringInExtensionList
const char * FindStringInExtensionList(const char *string, const char *substring)
Find a substring in a string made of space delimited elements.
Definition: opengl.cpp:144
OpenGLBackend::cursor_sprite_count
uint cursor_sprite_count
Number of cursor sprites to draw.
Definition: opengl.h:70
OpenGLBackend::InternalClearCursorCache
void InternalClearCursorCache()
Clear all cached cursor sprites.
Definition: opengl.cpp:1128
OpenGLBackend::sprite_sprite_loc
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:56
PalSpriteID
Combination of a palette sprite and a 'real' sprite.
Definition: gfx_type.h:22
OpenGLSprite::BindTextures
bool BindTextures()
Bind textures for rendering this sprite.
Definition: opengl.cpp:1532
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition: opengl.cpp:1039
OpenGLBackend::remap_sprite_loc
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:50
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition: opengl.cpp:1025
OpenGLBackend::sprite_program
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition: opengl.h:55
OpenGLBackend::sprite_crash_loc
GLint sprite_crash_loc
Uniform location for crash remap mode flag.
Definition: opengl.h:60
OpenGLSprite
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition: opengl.h:117
OpenGLBackend::clear_cursor_cache
bool clear_cursor_cache
A clear of the cursor cache is pending.
Definition: opengl.h:64
OpenGLBackend::OpenGLBackend
OpenGLBackend()
Construct OpenGL back-end class.
Definition: opengl.cpp:489
OpenGLBackend::sprite_rgb_loc
GLint sprite_rgb_loc
Uniform location for RGB mode flag.
Definition: opengl.h:59
OpenGLBackend::remap_zoom_loc
GLint remap_zoom_loc
Uniform location for sprite zoom.
Definition: opengl.h:52
OpenGLBackend::remap_screen_loc
GLint remap_screen_loc
Uniform location for screen size.
Definition: opengl.h:51
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:75
OpenGLBackend::vid_pbo
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition: opengl.h:37
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
OpenGLBackend::ReleaseAnimBuffer
void ReleaseAnimBuffer(const Rect &update_rect)
Update animation buffer texture after the animation buffer was filled.
Definition: opengl.cpp:1237
OpenGLBackend::remap_program
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition: opengl.h:49
OpenGLBackend::Init
const char * Init(const Dimension &screen_res)
Check for the needed OpenGL functionality and allocate all resources.
Definition: opengl.cpp:525
OpenGLBackend::vid_program
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition: opengl.h:39
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
IsOpenGLVersionAtLeast
bool IsOpenGLVersionAtLeast(byte major, byte minor)
Check if the current OpenGL version is equal or higher than a given one.
Definition: opengl.cpp:207
OpenGLBackend::cursor_sprite_pos
Point cursor_sprite_pos[16]
Relative position of individual cursor sprites.
Definition: opengl.h:69
OpenGLBackend::InitShaders
bool InitShaders()
Create all needed shader programs.
Definition: opengl.cpp:804
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition: spriteloader.hpp:22
OpenGLSprite::Texture
Texture
Enum of all used OpenGL texture objects.
Definition: opengl.h:120