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/>.
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(byte major
, byte minor
);
23 const char *FindStringInExtensionList(const char *string
, const char *substring
);
27 /** Platform-independent back-end class for OpenGL video drivers. */
28 class OpenGLBackend
: public ZeroedMemoryAllocator
, SpriteEncoder
{
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 PalSpriteID cursor_sprite_seq
[16]; ///< Current image of cursor
69 Point cursor_sprite_pos
[16]; ///< Relative position of individual cursor sprites
70 uint cursor_sprite_count
; ///< Number of cursor sprites to draw
75 const char *Init(const Dimension
&screen_res
);
78 void InternalClearCursorCache();
80 void RenderOglSprite(OpenGLSprite
*gl_sprite
, PaletteID pal
, int x
, int y
, ZoomLevel zoom
);
83 /** Get singleton instance of this class. */
84 static inline OpenGLBackend
*Get()
86 return OpenGLBackend::instance
;
88 static const char *Create(GetOGLProcAddressProc get_proc
, const Dimension
&screen_res
);
89 static void Destroy();
91 void PrepareContext();
93 std::string
GetDriverName();
95 void UpdatePalette(const Colour
*pal
, uint first
, uint length
);
96 bool Resize(int w
, int h
, bool force
= false);
99 void DrawMouseCursor();
100 void PopulateCursorCache();
101 void ClearCursorCache();
103 void *GetVideoBuffer();
104 uint8_t *GetAnimBuffer();
105 void ReleaseVideoBuffer(const Rect
&update_rect
);
106 void ReleaseAnimBuffer(const Rect
&update_rect
);
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
;
116 /** Class that encapsulates a RGBA texture together with a paletted remap texture. */
119 /** Enum of all used OpenGL texture objects. */
121 TEX_RGBA
, ///< RGBA texture part.
122 TEX_REMAP
, ///< Remap texture part.
127 GLuint tex
[NUM_TEX
]; ///< The texture objects.
129 static GLuint dummy_tex
[NUM_TEX
]; ///< 1x1 dummy textures to substitute for unused sprite components.
131 static GLuint pal_identity
; ///< Identity texture mapping.
132 static GLuint pal_tex
; ///< Texture for palette remap.
133 static GLuint pal_pbo
; ///< Pixel buffer object for remap upload.
135 static bool Create();
136 static void Destroy();
141 OpenGLSprite(uint width
, uint height
, uint levels
, SpriteColourComponent components
);
144 void Update(uint width
, uint height
, uint level
, const SpriteLoader::CommonPixel
*data
);
145 Dimension
GetSize(ZoomLevel level
) const;
147 friend class OpenGLBackend
;
150 #endif /* VIDEO_OPENGL_H */