OpenTTD Source  1.11.0-beta2
opengl.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #ifndef VIDEO_OPENGL_H
13 #define VIDEO_OPENGL_H
14 
15 #include "../core/alloc_type.hpp"
16 #include "../core/geometry_type.hpp"
17 #include "../gfx_type.h"
18 #include "../spriteloader/spriteloader.hpp"
19 #include "../misc/lrucache.hpp"
20 
21 typedef void (*OGLProc)();
22 typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
23 
24 bool IsOpenGLVersionAtLeast(byte major, byte minor);
25 const char *FindStringInExtensionList(const char *string, const char *substring);
26 
27 class OpenGLSprite;
28 
31 private:
33 
37 
38  void *vid_buffer;
39  GLuint vid_pbo;
40  GLuint vid_texture;
41  GLuint vid_program;
42  GLuint pal_program;
43  GLuint vao_quad;
44  GLuint vbo_quad;
45  GLuint pal_texture;
46 
47  void *anim_buffer;
48  GLuint anim_pbo;
49  GLuint anim_texture;
50 
51  GLuint remap_program;
55  GLint remap_rgb_loc;
56 
57  GLuint sprite_program;
63 
66 
67  OpenGLBackend();
69 
70  const char *Init();
71  bool InitShaders();
72 
73  void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
74 
75 public:
77  static inline OpenGLBackend *Get()
78  {
80  }
81  static const char *Create(GetOGLProcAddressProc get_proc);
82  static void Destroy();
83 
84  void PrepareContext();
85 
86  void UpdatePalette(const Colour *pal, uint first, uint length);
87  bool Resize(int w, int h, bool force = false);
88  void Paint();
89 
90  void DrawMouseCursor();
91  void ClearCursorCache();
92 
93  void *GetVideoBuffer();
94  uint8 *GetAnimBuffer();
95  void ReleaseVideoBuffer(const Rect &update_rect);
96  void ReleaseAnimBuffer(const Rect &update_rect);
97 
98  /* SpriteEncoder */
99 
100  bool Is32BppSupported() override { return true; }
101  uint GetSpriteAlignment() override { return 1u << (ZOOM_LVL_COUNT - 1); }
102  Sprite *Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override;
103 };
104 
105 
108 private:
110  enum Texture {
113  NUM_TEX
114  };
115 
116  Dimension dim;
117  GLuint tex[NUM_TEX];
118 
119  static GLuint dummy_tex[NUM_TEX];
120 
121  static GLuint pal_identity;
122  static GLuint pal_tex;
123  static GLuint pal_pbo;
124 
125  static bool Create();
126  static void Destroy();
127 
128  bool BindTextures();
129 
130 public:
131  OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components);
132  ~OpenGLSprite();
133 
134  void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
135  Dimension GetSize(ZoomLevel level) const;
136 
137  friend class OpenGLBackend;
138 };
139 
140 #endif /* VIDEO_OPENGL_H */
OpenGLBackend::sync_vid_mapping
GLsync sync_vid_mapping
Sync object for the persistently mapped video buffer.
Definition: opengl.h:35
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:1220
OpenGLBackend::GetSpriteAlignment
uint GetSpriteAlignment() override
Get the value which the height and width on a sprite have to be aligned by.
Definition: opengl.h:101
OpenGLSprite::pal_tex
static GLuint pal_tex
Texture for palette remap.
Definition: opengl.h:122
OpenGLBackend::pal_program
GLuint pal_program
Shader program for rendering a paletted video buffer.
Definition: opengl.h:42
OpenGLSprite::TEX_RGBA
@ TEX_RGBA
RGBA texture part.
Definition: opengl.h:111
OpenGLBackend::Is32BppSupported
bool Is32BppSupported() override
Can the sprite encoder make use of RGBA sprites?
Definition: opengl.h:100
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
OpenGLSprite::GetSize
Dimension GetSize(ZoomLevel level) const
Query the sprite size at a certain zoom level.
Definition: opengl.cpp:1446
OpenGLSprite::pal_identity
static GLuint pal_identity
Identity texture mapping.
Definition: opengl.h:121
OpenGLSprite::Destroy
static void Destroy()
Free all common resources for sprite rendering.
Definition: opengl.cpp:1331
OpenGLSprite::tex
GLuint tex[NUM_TEX]
The texture objects.
Definition: opengl.h:117
OpenGLSprite::OpenGLSprite
OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components)
Create an OpenGL sprite with a palette remap part.
Definition: opengl.cpp:1346
OpenGLSprite::TEX_REMAP
@ TEX_REMAP
Remap texture part.
Definition: opengl.h:112
ZOOM_LVL_COUNT
@ ZOOM_LVL_COUNT
Number of zoom levels.
Definition: zoom_type.h:32
OpenGLBackend::vid_buffer
void * vid_buffer
Pointer to the mapped video buffer.
Definition: opengl.h:38
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
OpenGLSprite::pal_pbo
static GLuint pal_pbo
Pixel buffer object for remap upload.
Definition: opengl.h:123
OpenGLBackend::~OpenGLBackend
~OpenGLBackend()
Free allocated resources.
Definition: opengl.cpp:500
OpenGLBackend::sprite_zoom_loc
GLint sprite_zoom_loc
Uniform location for sprite zoom;.
Definition: opengl.h:60
OpenGLBackend::Get
static OpenGLBackend * Get()
Get singleton instance of this class.
Definition: opengl.h:77
LRUCache< SpriteID, Sprite >
OpenGLBackend::anim_pbo
GLuint anim_pbo
Pixel buffer object storing the memory used for the animation buffer.
Definition: opengl.h:48
OpenGLBackend
Platform-independent back-end class for OpenGL video drivers.
Definition: opengl.h:30
OpenGLBackend::vid_texture
GLuint vid_texture
Texture handle for the video buffer texture.
Definition: opengl.h:40
OpenGLBackend::remap_rgb_loc
GLint remap_rgb_loc
Uniform location for RGB mode flag;.
Definition: opengl.h:55
OpenGLBackend::DrawMouseCursor
void DrawMouseCursor()
Draw mouse cursor on screen.
Definition: opengl.cpp:1033
OpenGLBackend::last_sprite_pal
PaletteID last_sprite_pal
Last uploaded remap palette.
Definition: opengl.h:65
OpenGLBackend::anim_buffer
void * anim_buffer
Pointer to the mapped animation buffer.
Definition: opengl.h:47
OpenGLBackend::ReleaseVideoBuffer
void ReleaseVideoBuffer(const Rect &update_rect)
Update video buffer texture after the video buffer was filled.
Definition: opengl.cpp:1120
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:44
OpenGLSprite::Create
static bool Create()
Create all common resources for sprite rendering.
Definition: opengl.cpp:1270
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:33
OpenGLBackend::GetVideoBuffer
void * GetVideoBuffer()
Get a pointer to the memory for the video driver to draw to.
Definition: opengl.cpp:1076
OpenGLBackend::instance
static OpenGLBackend * instance
Singleton instance pointer.
Definition: opengl.h:32
OpenGLBackend::anim_texture
GLuint anim_texture
Texture handle for the animation buffer texture.
Definition: opengl.h:49
OpenGLBackend::sprite_screen_loc
GLint sprite_screen_loc
Uniform location for screen size;.
Definition: opengl.h:59
OpenGLBackend::sync_anim_mapping
GLsync sync_anim_mapping
Sync object for the persistently mapped animation buffer.
Definition: opengl.h:36
OpenGLBackend::Destroy
static void Destroy()
Free resources and destroy singleton back-end class.
Definition: opengl.cpp:484
SpriteEncoder
Interface for something that can encode a sprite.
Definition: spriteloader.hpp:82
OpenGLBackend::persistent_mapping_supported
bool persistent_mapping_supported
Persistent pixel buffer mapping supported.
Definition: opengl.h:34
OpenGLBackend::cursor_cache
LRUCache< SpriteID, Sprite > cursor_cache
Cache of encoded cursor sprites.
Definition: opengl.h:64
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
OpenGLBackend::vao_quad
GLuint vao_quad
Vertex array object storing the rendering state for the fullscreen quad.
Definition: opengl.h:43
OpenGLSprite::dummy_tex
static GLuint dummy_tex[NUM_TEX]
1x1 dummy textures to substitute for unused sprite components.
Definition: opengl.h:119
OpenGLBackend::Init
const char * Init()
Check for the needed OpenGL functionality and allocate all resources.
Definition: opengl.cpp:528
OpenGLBackend::ClearCursorCache
void ClearCursorCache()
Clear all cached cursor sprites.
Definition: opengl.cpp:1060
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:1399
OpenGLBackend::pal_texture
GLuint pal_texture
Palette lookup texture.
Definition: opengl.h:45
FindStringInExtensionList
const char * FindStringInExtensionList(const char *string, const char *substring)
Find a substring in a string made of space delimited elements.
Definition: opengl.cpp:149
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
OpenGLBackend::sprite_sprite_loc
GLint sprite_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:58
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:47
OpenGLSprite::BindTextures
bool BindTextures()
Bind textures for rendering this sprite.
Definition: opengl.cpp:1456
OpenGLBackend::Paint
void Paint()
Render video buffer to the screen.
Definition: opengl.cpp:1001
OpenGLBackend::remap_sprite_loc
GLint remap_sprite_loc
Uniform location for sprite parameters.
Definition: opengl.h:52
OpenGLBackend::UpdatePalette
void UpdatePalette(const Colour *pal, uint first, uint length)
Update the stored palette.
Definition: opengl.cpp:987
OpenGLBackend::sprite_program
GLuint sprite_program
Shader program for blending and rendering a sprite to the video buffer.
Definition: opengl.h:57
OpenGLBackend::sprite_crash_loc
GLint sprite_crash_loc
Uniform location for crash remap mode flag;.
Definition: opengl.h:62
OpenGLSprite
Class that encapsulates a RGBA texture together with a paletted remap texture.
Definition: opengl.h:107
OpenGLBackend::OpenGLBackend
OpenGLBackend()
Construct OpenGL back-end class.
Definition: opengl.cpp:493
OpenGLBackend::sprite_rgb_loc
GLint sprite_rgb_loc
Uniform location for RGB mode flag;.
Definition: opengl.h:61
OpenGLBackend::remap_zoom_loc
GLint remap_zoom_loc
Uniform location for sprite zoom;.
Definition: opengl.h:54
OpenGLBackend::remap_screen_loc
GLint remap_screen_loc
Uniform location for screen size;.
Definition: opengl.h:53
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
OpenGLBackend::vid_pbo
GLuint vid_pbo
Pixel buffer object storing the memory used for the video driver to draw to.
Definition: opengl.h:39
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:1162
OpenGLBackend::remap_program
GLuint remap_program
Shader program for blending and rendering a RGBA + remap texture.
Definition: opengl.h:51
OpenGLBackend::vid_program
GLuint vid_program
Shader program for rendering a RGBA video buffer.
Definition: opengl.h:41
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:887
OpenGLBackend::GetAnimBuffer
uint8 * GetAnimBuffer()
Get a pointer to the memory for the separate animation buffer.
Definition: opengl.cpp:1097
OpenGLBackend::Encode
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: opengl.cpp:1192
IsOpenGLVersionAtLeast
bool IsOpenGLVersionAtLeast(byte major, byte minor)
Check if the current OpenGL version is equal or higher than a given one.
Definition: opengl.cpp:212
OpenGLBackend::Create
static const char * Create(GetOGLProcAddressProc get_proc)
Create and initialize the singleton back-end class.
Definition: opengl.cpp:471
OpenGLBackend::InitShaders
bool InitShaders()
Create all needed shader programs.
Definition: opengl.cpp:794
SpriteColourComponent
SpriteColourComponent
The different colour components a sprite can have.
Definition: spriteloader.hpp:21
OpenGLSprite::Texture
Texture
Enum of all used OpenGL texture objects.
Definition: opengl.h:110