OpenTTD Source 20241224-master-gee860a5c8e
OpenGLBackend Class Reference

Platform-independent back-end class for OpenGL video drivers. More...

#include <opengl.h>

Inheritance diagram for OpenGLBackend:
ZeroedMemoryAllocator SpriteEncoder

Public Member Functions

void PrepareContext ()
 
std::string GetDriverName ()
 
void UpdatePalette (const Colour *pal, uint first, uint length)
 Update the stored palette.
 
bool Resize (int w, int h, bool force=false)
 Change the size of the drawing window and allocate matching resources.
 
void Paint ()
 Render video buffer to the screen.
 
void DrawMouseCursor ()
 Draw mouse cursor on screen.
 
void PopulateCursorCache ()
 
void ClearCursorCache ()
 Queue a request for cursor cache clear.
 
void * GetVideoBuffer ()
 Get a pointer to the memory for the video driver to draw to.
 
uint8_t * GetAnimBuffer ()
 Get a pointer to the memory for the separate animation buffer.
 
void ReleaseVideoBuffer (const Rect &update_rect)
 Update video buffer texture after the video buffer was filled.
 
void ReleaseAnimBuffer (const Rect &update_rect)
 Update animation buffer texture after the animation buffer was filled.
 
bool Is32BppSupported () override
 Can the sprite encoder make use of RGBA sprites?
 
uint GetSpriteAlignment () override
 Get the value which the height and width on a sprite have to be aligned by.
 
SpriteEncode (const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
 Convert a sprite from the loader to our own format.
 
- Public Member Functions inherited from ZeroedMemoryAllocator
void * operator new (size_t size)
 Memory allocator for a single class instance.
 
void * operator new[] (size_t size)
 Memory allocator for an array of class instances.
 
void operator delete (void *ptr)
 Memory release for a single class instance.
 
void operator delete[] (void *ptr)
 Memory release for an array of class instances.
 

Static Public Member Functions

static OpenGLBackendGet ()
 Get singleton instance of this class.
 
static std::optional< std::string_view > Create (GetOGLProcAddressProc get_proc, const Dimension &screen_res)
 Create and initialize the singleton back-end class.
 
static void Destroy ()
 Free resources and destroy singleton back-end class.
 

Private Member Functions

 OpenGLBackend ()
 Construct OpenGL back-end class.
 
 ~OpenGLBackend ()
 Free allocated resources.
 
std::optional< std::string_view > Init (const Dimension &screen_res)
 Check for the needed OpenGL functionality and allocate all resources.
 
bool InitShaders ()
 Create all needed shader programs.
 
void InternalClearCursorCache ()
 Clear all cached cursor sprites.
 
void RenderOglSprite (OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
 Render a sprite to the back buffer.
 

Private Attributes

bool persistent_mapping_supported
 Persistent pixel buffer mapping supported.
 
GLsync sync_vid_mapping
 Sync object for the persistently mapped video buffer.
 
GLsync sync_anim_mapping
 Sync object for the persistently mapped animation buffer.
 
void * vid_buffer
 Pointer to the mapped video buffer.
 
GLuint vid_pbo
 Pixel buffer object storing the memory used for the video driver to draw to.
 
GLuint vid_texture
 Texture handle for the video buffer texture.
 
GLuint vid_program
 Shader program for rendering a RGBA video buffer.
 
GLuint pal_program
 Shader program for rendering a paletted video buffer.
 
GLuint vao_quad
 Vertex array object storing the rendering state for the fullscreen quad.
 
GLuint vbo_quad
 Vertex buffer with a fullscreen quad.
 
GLuint pal_texture
 Palette lookup texture.
 
void * anim_buffer
 Pointer to the mapped animation buffer.
 
GLuint anim_pbo
 Pixel buffer object storing the memory used for the animation buffer.
 
GLuint anim_texture
 Texture handle for the animation buffer texture.
 
GLuint remap_program
 Shader program for blending and rendering a RGBA + remap texture.
 
GLint remap_sprite_loc
 Uniform location for sprite parameters.
 
GLint remap_screen_loc
 Uniform location for screen size.
 
GLint remap_zoom_loc
 Uniform location for sprite zoom.
 
GLint remap_rgb_loc
 Uniform location for RGB mode flag.
 
GLuint sprite_program
 Shader program for blending and rendering a sprite to the video buffer.
 
GLint sprite_sprite_loc
 Uniform location for sprite parameters.
 
GLint sprite_screen_loc
 Uniform location for screen size.
 
GLint sprite_zoom_loc
 Uniform location for sprite zoom.
 
GLint sprite_rgb_loc
 Uniform location for RGB mode flag.
 
GLint sprite_crash_loc
 Uniform location for crash remap mode flag.
 
LRUCache< SpriteID, Spritecursor_cache
 Cache of encoded cursor sprites.
 
PaletteID last_sprite_pal = (PaletteID)-1
 Last uploaded remap palette.
 
bool clear_cursor_cache = false
 A clear of the cursor cache is pending.
 
Point cursor_pos
 Cursor position.
 
bool cursor_in_window
 Cursor inside this window.
 
std::vector< CursorSpritecursor_sprites
 Sprites comprising cursor.
 

Static Private Attributes

static OpenGLBackendinstance = nullptr
 Singleton instance pointer.
 

Detailed Description

Platform-independent back-end class for OpenGL video drivers.

Definition at line 28 of file opengl.h.

Constructor & Destructor Documentation

◆ OpenGLBackend()

OpenGLBackend::OpenGLBackend ( )
private

Construct OpenGL back-end class.

Definition at line 489 of file opengl.cpp.

Referenced by Create().

◆ ~OpenGLBackend()

OpenGLBackend::~OpenGLBackend ( )
private

Member Function Documentation

◆ ClearCursorCache()

void OpenGLBackend::ClearCursorCache ( )

Queue a request for cursor cache clear.

Definition at line 1135 of file opengl.cpp.

References clear_cursor_cache.

Referenced by VideoDriver_SDL_OpenGL::ClearSystemSprites().

◆ Create()

std::optional< std::string_view > OpenGLBackend::Create ( GetOGLProcAddressProc  get_proc,
const Dimension screen_res 
)
static

Create and initialize the singleton back-end class.

Parameters
get_procCallback to get an OpenGL function from the OS driver.
screen_resCurrent display resolution.
Returns
std::nullopt on success, error message otherwise.

Definition at line 467 of file opengl.cpp.

References Destroy(), Init(), instance, and OpenGLBackend().

◆ Destroy()

void OpenGLBackend::Destroy ( )
static

Free resources and destroy singleton back-end class.

Definition at line 480 of file opengl.cpp.

References instance.

Referenced by Create().

◆ DrawMouseCursor()

void OpenGLBackend::DrawMouseCursor ( )

◆ Encode()

Sprite * OpenGLBackend::Encode ( const SpriteLoader::SpriteCollection sprite,
SpriteAllocator allocator 
)
overridevirtual

Convert a sprite from the loader to our own format.

Implements SpriteEncoder.

Definition at line 1261 of file opengl.cpp.

References SpriteAllocator::Allocate(), Sprite::data, Font, Sprite::height, OpenGLSprite::Update(), Sprite::width, Sprite::x_offs, Sprite::y_offs, ZOOM_LVL_END, and ZOOM_LVL_MIN.

◆ Get()

◆ GetAnimBuffer()

uint8_t * OpenGLBackend::GetAnimBuffer ( )

Get a pointer to the memory for the separate animation buffer.

Returns
Pointer to draw on.

Definition at line 1170 of file opengl.cpp.

References anim_buffer, anim_pbo, persistent_mapping_supported, and sync_anim_mapping.

Referenced by VideoDriver_SDL_OpenGL::GetVideoPointer().

◆ GetDriverName()

std::string OpenGLBackend::GetDriverName ( )

Definition at line 744 of file opengl.cpp.

◆ GetSpriteAlignment()

uint OpenGLBackend::GetSpriteAlignment ( )
inlineoverridevirtual

Get the value which the height and width on a sprite have to be aligned by.

Returns
The needed alignment or 0 if any alignment is accepted.

Reimplemented from SpriteEncoder.

Definition at line 109 of file opengl.h.

References ZOOM_LVL_END.

◆ GetVideoBuffer()

void * OpenGLBackend::GetVideoBuffer ( )

Get a pointer to the memory for the video driver to draw to.

Returns
Pointer to draw on.

Definition at line 1148 of file opengl.cpp.

References BlitterFactory::GetCurrentBlitter(), Blitter::GetScreenDepth(), persistent_mapping_supported, sync_vid_mapping, vid_buffer, and vid_pbo.

Referenced by VideoDriver_SDL_OpenGL::GetVideoPointer().

◆ Init()

std::optional< std::string_view > OpenGLBackend::Init ( const Dimension screen_res)
private

Check for the needed OpenGL functionality and allocate all resources.

Parameters
screen_resCurrent display resolution.
Returns
Error string or std::nullopt if successful.

Definition at line 525 of file opengl.cpp.

References anim_pbo, anim_texture, OpenGLSprite::Create(), Debug, InitShaders(), IsOpenGLVersionAtLeast(), pal_program, pal_texture, persistent_mapping_supported, remap_program, remap_rgb_loc, remap_screen_loc, remap_sprite_loc, remap_zoom_loc, sprite_crash_loc, sprite_program, sprite_rgb_loc, sprite_screen_loc, sprite_sprite_loc, sprite_zoom_loc, vao_quad, vbo_quad, vid_pbo, vid_program, and vid_texture.

Referenced by Create().

◆ InitShaders()

◆ InternalClearCursorCache()

void OpenGLBackend::InternalClearCursorCache ( )
private

Clear all cached cursor sprites.

Definition at line 1122 of file opengl.cpp.

References cursor_cache, Sprite::data, free(), and LRUCache< Tkey, Tdata >::Pop().

Referenced by ~OpenGLBackend().

◆ Is32BppSupported()

bool OpenGLBackend::Is32BppSupported ( )
inlineoverridevirtual

Can the sprite encoder make use of RGBA sprites?

Implements SpriteEncoder.

Definition at line 108 of file opengl.h.

◆ Paint()

void OpenGLBackend::Paint ( )

◆ PopulateCursorCache()

void OpenGLBackend::PopulateCursorCache ( )

Definition at line 1090 of file opengl.cpp.

◆ PrepareContext()

void OpenGLBackend::PrepareContext ( )

Definition at line 735 of file opengl.cpp.

◆ ReleaseAnimBuffer()

void OpenGLBackend::ReleaseAnimBuffer ( const Rect update_rect)

Update animation buffer texture after the animation buffer was filled.

Parameters
update_rectRectangle encompassing the dirty region of the animation buffer.

Definition at line 1231 of file opengl.cpp.

References anim_buffer, anim_pbo, anim_texture, persistent_mapping_supported, and sync_anim_mapping.

Referenced by VideoDriver_SDL_OpenGL::ReleaseVideoPointer().

◆ ReleaseVideoBuffer()

void OpenGLBackend::ReleaseVideoBuffer ( const Rect update_rect)

Update video buffer texture after the video buffer was filled.

Parameters
update_rectRectangle encompassing the dirty region of the video buffer.

Definition at line 1193 of file opengl.cpp.

References BlitterFactory::GetCurrentBlitter(), IsEmptyRect(), persistent_mapping_supported, sync_vid_mapping, vid_buffer, vid_pbo, and vid_texture.

Referenced by VideoDriver_SDL_OpenGL::ReleaseVideoPointer().

◆ RenderOglSprite()

void OpenGLBackend::RenderOglSprite ( OpenGLSprite gl_sprite,
PaletteID  pal,
int  x,
int  y,
ZoomLevel  zoom 
)
private

Render a sprite to the back buffer.

Parameters
gl_spriteSprite to render.
xX position of the sprite.
yY position of the sprite.
zoomZoom level to use.

Definition at line 1289 of file opengl.cpp.

References OpenGLSprite::BindTextures(), GB(), OpenGLSprite::GetSize(), last_sprite_pal, OpenGLSprite::pal_identity, OpenGLSprite::pal_pbo, OpenGLSprite::pal_tex, pal_texture, PALETTE_CRASH, PALETTE_WIDTH, Recolour, sprite_crash_loc, sprite_program, sprite_rgb_loc, sprite_screen_loc, sprite_sprite_loc, sprite_zoom_loc, and vao_quad.

Referenced by DrawMouseCursor().

◆ Resize()

bool OpenGLBackend::Resize ( int  w,
int  h,
bool  force = false 
)

Change the size of the drawing window and allocate matching resources.

Parameters
wNew width of the window.
hNew height of the window.
forceRecreate resources even if size didn't change.
Falseif nothing had to be done, true otherwise.

Definition at line 913 of file opengl.cpp.

References Align(), anim_buffer, anim_pbo, anim_texture, Colour::data, BlitterFactory::GetCurrentBlitter(), Blitter::GetScreenDepth(), persistent_mapping_supported, remap_program, remap_screen_loc, vid_buffer, vid_pbo, and vid_texture.

Referenced by VideoDriver_SDL_OpenGL::AllocateBackingStore().

◆ UpdatePalette()

void OpenGLBackend::UpdatePalette ( const Colour pal,
uint  first,
uint  length 
)

Update the stored palette.

Parameters
palPalette array with at least 256 elements.
firstFirst entry to update.
lengthNumber of entries to update.

Definition at line 1025 of file opengl.cpp.

References pal_texture.

Referenced by VideoDriver_SDL_OpenGL::Paint().

Field Documentation

◆ anim_buffer

void* OpenGLBackend::anim_buffer
private

Pointer to the mapped animation buffer.

Definition at line 45 of file opengl.h.

Referenced by GetAnimBuffer(), ReleaseAnimBuffer(), and Resize().

◆ anim_pbo

GLuint OpenGLBackend::anim_pbo
private

Pixel buffer object storing the memory used for the animation buffer.

Definition at line 46 of file opengl.h.

Referenced by GetAnimBuffer(), Init(), ReleaseAnimBuffer(), Resize(), and ~OpenGLBackend().

◆ anim_texture

GLuint OpenGLBackend::anim_texture
private

Texture handle for the animation buffer texture.

Definition at line 47 of file opengl.h.

Referenced by Init(), Paint(), ReleaseAnimBuffer(), Resize(), and ~OpenGLBackend().

◆ clear_cursor_cache

bool OpenGLBackend::clear_cursor_cache = false
private

A clear of the cursor cache is pending.

Definition at line 64 of file opengl.h.

Referenced by ClearCursorCache().

◆ cursor_cache

LRUCache<SpriteID, Sprite> OpenGLBackend::cursor_cache
private

Cache of encoded cursor sprites.

Definition at line 62 of file opengl.h.

Referenced by DrawMouseCursor(), and InternalClearCursorCache().

◆ cursor_in_window

bool OpenGLBackend::cursor_in_window
private

Cursor inside this window.

Definition at line 67 of file opengl.h.

Referenced by DrawMouseCursor().

◆ cursor_pos

Point OpenGLBackend::cursor_pos
private

Cursor position.

Definition at line 66 of file opengl.h.

◆ cursor_sprites

std::vector<CursorSprite> OpenGLBackend::cursor_sprites
private

Sprites comprising cursor.

Definition at line 68 of file opengl.h.

Referenced by DrawMouseCursor().

◆ instance

OpenGLBackend * OpenGLBackend::instance = nullptr
staticprivate

Singleton instance pointer.

Definition at line 30 of file opengl.h.

Referenced by Create(), Destroy(), and Get().

◆ last_sprite_pal

PaletteID OpenGLBackend::last_sprite_pal = (PaletteID)-1
private

Last uploaded remap palette.

Definition at line 63 of file opengl.h.

Referenced by RenderOglSprite().

◆ pal_program

GLuint OpenGLBackend::pal_program
private

Shader program for rendering a paletted video buffer.

Definition at line 40 of file opengl.h.

Referenced by Init(), InitShaders(), Paint(), and ~OpenGLBackend().

◆ pal_texture

GLuint OpenGLBackend::pal_texture
private

Palette lookup texture.

Definition at line 43 of file opengl.h.

Referenced by Init(), Paint(), RenderOglSprite(), UpdatePalette(), and ~OpenGLBackend().

◆ persistent_mapping_supported

bool OpenGLBackend::persistent_mapping_supported
private

Persistent pixel buffer mapping supported.

Definition at line 32 of file opengl.h.

Referenced by GetAnimBuffer(), GetVideoBuffer(), Init(), ReleaseAnimBuffer(), ReleaseVideoBuffer(), and Resize().

◆ remap_program

GLuint OpenGLBackend::remap_program
private

Shader program for blending and rendering a RGBA + remap texture.

Definition at line 49 of file opengl.h.

Referenced by Init(), InitShaders(), Paint(), Resize(), and ~OpenGLBackend().

◆ remap_rgb_loc

GLint OpenGLBackend::remap_rgb_loc
private

Uniform location for RGB mode flag.

Definition at line 53 of file opengl.h.

Referenced by Init(), and Paint().

◆ remap_screen_loc

GLint OpenGLBackend::remap_screen_loc
private

Uniform location for screen size.

Definition at line 51 of file opengl.h.

Referenced by Init(), Paint(), and Resize().

◆ remap_sprite_loc

GLint OpenGLBackend::remap_sprite_loc
private

Uniform location for sprite parameters.

Definition at line 50 of file opengl.h.

Referenced by Init(), and Paint().

◆ remap_zoom_loc

GLint OpenGLBackend::remap_zoom_loc
private

Uniform location for sprite zoom.

Definition at line 52 of file opengl.h.

Referenced by Init(), and Paint().

◆ sprite_crash_loc

GLint OpenGLBackend::sprite_crash_loc
private

Uniform location for crash remap mode flag.

Definition at line 60 of file opengl.h.

Referenced by Init(), and RenderOglSprite().

◆ sprite_program

GLuint OpenGLBackend::sprite_program
private

Shader program for blending and rendering a sprite to the video buffer.

Definition at line 55 of file opengl.h.

Referenced by Init(), InitShaders(), RenderOglSprite(), and ~OpenGLBackend().

◆ sprite_rgb_loc

GLint OpenGLBackend::sprite_rgb_loc
private

Uniform location for RGB mode flag.

Definition at line 59 of file opengl.h.

Referenced by Init(), and RenderOglSprite().

◆ sprite_screen_loc

GLint OpenGLBackend::sprite_screen_loc
private

Uniform location for screen size.

Definition at line 57 of file opengl.h.

Referenced by Init(), and RenderOglSprite().

◆ sprite_sprite_loc

GLint OpenGLBackend::sprite_sprite_loc
private

Uniform location for sprite parameters.

Definition at line 56 of file opengl.h.

Referenced by Init(), and RenderOglSprite().

◆ sprite_zoom_loc

GLint OpenGLBackend::sprite_zoom_loc
private

Uniform location for sprite zoom.

Definition at line 58 of file opengl.h.

Referenced by Init(), and RenderOglSprite().

◆ sync_anim_mapping

GLsync OpenGLBackend::sync_anim_mapping
private

Sync object for the persistently mapped animation buffer.

Definition at line 34 of file opengl.h.

Referenced by GetAnimBuffer(), and ReleaseAnimBuffer().

◆ sync_vid_mapping

GLsync OpenGLBackend::sync_vid_mapping
private

Sync object for the persistently mapped video buffer.

Definition at line 33 of file opengl.h.

Referenced by GetVideoBuffer(), and ReleaseVideoBuffer().

◆ vao_quad

GLuint OpenGLBackend::vao_quad
private

Vertex array object storing the rendering state for the fullscreen quad.

Definition at line 41 of file opengl.h.

Referenced by Init(), Paint(), RenderOglSprite(), and ~OpenGLBackend().

◆ vbo_quad

GLuint OpenGLBackend::vbo_quad
private

Vertex buffer with a fullscreen quad.

Definition at line 42 of file opengl.h.

Referenced by Init(), and ~OpenGLBackend().

◆ vid_buffer

void* OpenGLBackend::vid_buffer
private

Pointer to the mapped video buffer.

Definition at line 36 of file opengl.h.

Referenced by GetVideoBuffer(), ReleaseVideoBuffer(), and Resize().

◆ vid_pbo

GLuint OpenGLBackend::vid_pbo
private

Pixel buffer object storing the memory used for the video driver to draw to.

Definition at line 37 of file opengl.h.

Referenced by GetVideoBuffer(), Init(), ReleaseVideoBuffer(), Resize(), and ~OpenGLBackend().

◆ vid_program

GLuint OpenGLBackend::vid_program
private

Shader program for rendering a RGBA video buffer.

Definition at line 39 of file opengl.h.

Referenced by Init(), InitShaders(), Paint(), and ~OpenGLBackend().

◆ vid_texture

GLuint OpenGLBackend::vid_texture
private

Texture handle for the video buffer texture.

Definition at line 38 of file opengl.h.

Referenced by Init(), Paint(), ReleaseVideoBuffer(), Resize(), and ~OpenGLBackend().


The documentation for this class was generated from the following files: