2021-01-16 15:43:04 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file opengl.h OpenGL video driver support. */
|
|
|
|
|
|
|
|
#ifndef VIDEO_OPENGL_H
|
|
|
|
#define VIDEO_OPENGL_H
|
|
|
|
|
|
|
|
#include "../core/alloc_type.hpp"
|
2021-02-20 23:35:35 +00:00
|
|
|
#include "../core/geometry_type.hpp"
|
2021-01-16 15:43:25 +00:00
|
|
|
#include "../gfx_type.h"
|
2021-01-16 15:43:38 +00:00
|
|
|
#include "../spriteloader/spriteloader.hpp"
|
|
|
|
#include "../misc/lrucache.hpp"
|
2021-01-16 15:43:04 +00:00
|
|
|
|
2021-01-16 15:43:05 +00:00
|
|
|
typedef void (*OGLProc)();
|
|
|
|
typedef OGLProc (*GetOGLProcAddressProc)(const char *proc);
|
|
|
|
|
|
|
|
bool IsOpenGLVersionAtLeast(byte major, byte minor);
|
2021-01-16 15:43:17 +00:00
|
|
|
const char *FindStringInExtensionList(const char *string, const char *substring);
|
2021-01-16 15:43:05 +00:00
|
|
|
|
2021-01-16 15:43:38 +00:00
|
|
|
class OpenGLSprite;
|
|
|
|
|
2021-01-16 15:43:05 +00:00
|
|
|
/** Platform-independent back-end class for OpenGL video drivers. */
|
2021-01-16 15:43:38 +00:00
|
|
|
class OpenGLBackend : public ZeroedMemoryAllocator, SpriteEncoder {
|
2021-01-16 15:43:04 +00:00
|
|
|
private:
|
|
|
|
static OpenGLBackend *instance; ///< Singleton instance pointer.
|
|
|
|
|
2021-01-16 15:43:43 +00:00
|
|
|
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.
|
2021-01-16 15:43:16 +00:00
|
|
|
GLuint vid_pbo; ///< Pixel buffer object storing the memory used for the video driver to draw to.
|
2021-01-16 15:43:04 +00:00
|
|
|
GLuint vid_texture; ///< Texture handle for the video buffer texture.
|
2021-01-16 15:43:25 +00:00
|
|
|
GLuint vid_program; ///< Shader program for rendering a RGBA video buffer.
|
|
|
|
GLuint pal_program; ///< Shader program for rendering a paletted video buffer.
|
2021-01-16 15:43:14 +00:00
|
|
|
GLuint vao_quad; ///< Vertex array object storing the rendering state for the fullscreen quad.
|
2021-01-16 15:43:13 +00:00
|
|
|
GLuint vbo_quad; ///< Vertex buffer with a fullscreen quad.
|
2021-01-16 15:43:25 +00:00
|
|
|
GLuint pal_texture; ///< Palette lookup texture.
|
2021-01-16 15:43:04 +00:00
|
|
|
|
2021-01-16 15:43:43 +00:00
|
|
|
void *anim_buffer; ///< Pointer to the mapped animation buffer.
|
2021-01-16 15:43:42 +00:00
|
|
|
GLuint anim_pbo; ///< Pixel buffer object storing the memory used for the animation buffer.
|
|
|
|
GLuint anim_texture; ///< Texture handle for the animation buffer texture.
|
|
|
|
|
2021-01-16 15:43:38 +00:00
|
|
|
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;
|
|
|
|
|
2021-02-24 21:10:25 +00:00
|
|
|
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, Sprite> cursor_cache; ///< Cache of encoded cursor sprites.
|
|
|
|
PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette.
|
2021-03-08 20:16:41 +00:00
|
|
|
bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending.
|
2021-01-16 15:43:38 +00:00
|
|
|
|
2021-04-05 22:22:55 +00:00
|
|
|
Point cursor_pos; ///< Cursor position
|
|
|
|
bool cursor_in_window; ///< Cursor inside this window
|
|
|
|
PalSpriteID cursor_sprite_seq[16]; ///< Current image of cursor
|
|
|
|
Point cursor_sprite_pos[16]; ///< Relative position of individual cursor sprites
|
|
|
|
uint cursor_sprite_count; ///< Number of cursor sprites to draw
|
|
|
|
|
2021-01-16 15:43:04 +00:00
|
|
|
OpenGLBackend();
|
|
|
|
~OpenGLBackend();
|
|
|
|
|
2021-04-21 20:06:04 +00:00
|
|
|
const char *Init(const Dimension &screen_res);
|
2021-01-16 15:43:20 +00:00
|
|
|
bool InitShaders();
|
2021-01-16 15:43:04 +00:00
|
|
|
|
2021-04-12 19:44:32 +00:00
|
|
|
void InternalClearCursorCache();
|
|
|
|
|
2021-02-28 09:58:56 +00:00
|
|
|
void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
|
2021-01-16 15:43:38 +00:00
|
|
|
|
2021-01-16 15:43:04 +00:00
|
|
|
public:
|
|
|
|
/** Get singleton instance of this class. */
|
|
|
|
static inline OpenGLBackend *Get()
|
|
|
|
{
|
|
|
|
return OpenGLBackend::instance;
|
|
|
|
}
|
2021-04-21 20:06:04 +00:00
|
|
|
static const char *Create(GetOGLProcAddressProc get_proc, const Dimension &screen_res);
|
2021-01-16 15:43:04 +00:00
|
|
|
static void Destroy();
|
|
|
|
|
2021-02-21 21:17:11 +00:00
|
|
|
void PrepareContext();
|
|
|
|
|
2022-04-30 12:52:39 +00:00
|
|
|
std::string GetDriverName();
|
|
|
|
|
2021-01-16 15:43:25 +00:00
|
|
|
void UpdatePalette(const Colour *pal, uint first, uint length);
|
2021-01-16 15:43:04 +00:00
|
|
|
bool Resize(int w, int h, bool force = false);
|
2021-01-16 15:43:16 +00:00
|
|
|
void Paint();
|
2021-01-16 15:43:04 +00:00
|
|
|
|
2021-01-16 15:43:38 +00:00
|
|
|
void DrawMouseCursor();
|
2021-02-24 14:22:23 +00:00
|
|
|
void PopulateCursorCache();
|
2021-01-16 15:43:38 +00:00
|
|
|
void ClearCursorCache();
|
|
|
|
|
2021-01-16 15:43:16 +00:00
|
|
|
void *GetVideoBuffer();
|
2021-01-16 15:43:42 +00:00
|
|
|
uint8 *GetAnimBuffer();
|
2021-01-16 15:43:16 +00:00
|
|
|
void ReleaseVideoBuffer(const Rect &update_rect);
|
2021-01-16 15:43:42 +00:00
|
|
|
void ReleaseAnimBuffer(const Rect &update_rect);
|
2021-01-16 15:43:38 +00:00
|
|
|
|
|
|
|
/* SpriteEncoder */
|
|
|
|
|
|
|
|
bool Is32BppSupported() override { return true; }
|
|
|
|
uint GetSpriteAlignment() override { return 1u << (ZOOM_LVL_COUNT - 1); }
|
|
|
|
Sprite *Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Class that encapsulates a RGBA texture together with a paletted remap texture. */
|
|
|
|
class OpenGLSprite {
|
|
|
|
private:
|
|
|
|
/** Enum of all used OpenGL texture objects. */
|
|
|
|
enum Texture {
|
|
|
|
TEX_RGBA, ///< RGBA texture part.
|
|
|
|
TEX_REMAP, ///< Remap texture part.
|
|
|
|
NUM_TEX
|
|
|
|
};
|
|
|
|
|
|
|
|
Dimension dim;
|
|
|
|
GLuint tex[NUM_TEX]; ///< The texture objects.
|
|
|
|
|
|
|
|
static GLuint dummy_tex[NUM_TEX]; ///< 1x1 dummy textures to substitute for unused sprite components.
|
|
|
|
|
2021-02-24 21:10:25 +00:00
|
|
|
static GLuint pal_identity; ///< Identity texture mapping.
|
|
|
|
static GLuint pal_tex; ///< Texture for palette remap.
|
|
|
|
static GLuint pal_pbo; ///< Pixel buffer object for remap upload.
|
|
|
|
|
2021-01-16 15:43:38 +00:00
|
|
|
static bool Create();
|
|
|
|
static void Destroy();
|
|
|
|
|
|
|
|
bool BindTextures();
|
|
|
|
|
|
|
|
public:
|
|
|
|
OpenGLSprite(uint width, uint height, uint levels, SpriteColourComponent components);
|
|
|
|
~OpenGLSprite();
|
|
|
|
|
|
|
|
void Update(uint width, uint height, uint level, const SpriteLoader::CommonPixel *data);
|
|
|
|
Dimension GetSize(ZoomLevel level) const;
|
|
|
|
|
|
|
|
friend class OpenGLBackend;
|
2021-01-16 15:43:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* VIDEO_OPENGL_H */
|