OpenTTD Source 20260109-master-g241b5fcdfe
opengl.cpp File Reference

OpenGL video driver support. More...

#include "../stdafx.h"
#include <GL/gl.h>
#include "../3rdparty/opengl/glext.h"
#include "opengl.h"
#include "../core/geometry_func.hpp"
#include "../core/math_func.hpp"
#include "../gfx_func.h"
#include "../debug.h"
#include "../blitter/factory.hpp"
#include "../zoom_func.h"
#include "../core/string_consumer.hpp"
#include "../table/opengl_shader.h"
#include "../table/sprites.h"
#include "../safeguards.h"

Go to the source code of this file.

Data Structures

struct  Simple2DVertex
 A simple 2D vertex with just position and texture. More...
 
class  OpenGLSpriteAllocator
 

Macros

#define GL_GLEXT_PROTOTYPES
 
#define GL(function)   static decltype(&function) _ ## function
 

Functions

 GL (glGetString)
 
 GL (glGetIntegerv)
 
 GL (glGetError)
 
 GL (glDebugMessageControl)
 
 GL (glDebugMessageCallback)
 
 GL (glDisable)
 
 GL (glEnable)
 
 GL (glViewport)
 
 GL (glClear)
 
 GL (glClearColor)
 
 GL (glBlendFunc)
 
 GL (glDrawArrays)
 
 GL (glTexImage1D)
 
 GL (glTexImage2D)
 
 GL (glTexParameteri)
 
 GL (glTexSubImage1D)
 
 GL (glTexSubImage2D)
 
 GL (glBindTexture)
 
 GL (glDeleteTextures)
 
 GL (glGenTextures)
 
 GL (glPixelStorei)
 
 GL (glActiveTexture)
 
 GL (glGenBuffers)
 
 GL (glDeleteBuffers)
 
 GL (glBindBuffer)
 
 GL (glBufferData)
 
 GL (glBufferSubData)
 
 GL (glMapBuffer)
 
 GL (glUnmapBuffer)
 
 GL (glClearBufferSubData)
 
 GL (glBufferStorage)
 
 GL (glMapBufferRange)
 
 GL (glClientWaitSync)
 
 GL (glFenceSync)
 
 GL (glDeleteSync)
 
 GL (glGenVertexArrays)
 
 GL (glDeleteVertexArrays)
 
 GL (glBindVertexArray)
 
 GL (glCreateProgram)
 
 GL (glDeleteProgram)
 
 GL (glLinkProgram)
 
 GL (glUseProgram)
 
 GL (glGetProgramiv)
 
 GL (glGetProgramInfoLog)
 
 GL (glCreateShader)
 
 GL (glDeleteShader)
 
 GL (glShaderSource)
 
 GL (glCompileShader)
 
 GL (glAttachShader)
 
 GL (glGetShaderiv)
 
 GL (glGetShaderInfoLog)
 
 GL (glGetUniformLocation)
 
 GL (glUniform1i)
 
 GL (glUniform1f)
 
 GL (glUniform2f)
 
 GL (glUniform4f)
 
 GL (glGetAttribLocation)
 
 GL (glEnableVertexAttribArray)
 
 GL (glDisableVertexAttribArray)
 
 GL (glVertexAttribPointer)
 
 GL (glBindFragDataLocation)
 
static std::optional< std::string_view > GlGetString (GLenum name)
 
bool HasStringInExtensionList (std::string_view string, std::string_view substring)
 Find a substring in a string made of space delimited elements.
 
static bool IsOpenGLExtensionSupported (std::string_view extension)
 Check if an OpenGL extension is supported by the current context.
 
bool IsOpenGLVersionAtLeast (uint8_t major, uint8_t minor)
 Check if the current OpenGL version is equal or higher than a given one.
 
template<typename F >
static bool BindGLProc (F &f, const char *name)
 Try loading an OpenGL function.
 
static bool BindBasicInfoProcs ()
 Bind basic information functions.
 
static bool BindBasicOpenGLProcs ()
 Bind OpenGL 1.0 and 1.1 functions.
 
static bool BindTextureExtensions ()
 Bind texture-related extension functions.
 
static bool BindVBOExtension ()
 Bind vertex buffer object extension functions.
 
static bool BindVBAExtension ()
 Bind vertex array object extension functions.
 
static bool BindShaderExtensions ()
 Bind extension functions for shader support.
 
static bool BindPersistentBufferExtensions ()
 Bind extension functions for persistent buffer mapping.
 
void APIENTRY DebugOutputCallback (GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
 Callback to receive OpenGL debug messages.
 
void SetupDebugOutput ()
 Enable OpenGL debug messages if supported.
 
static std::tuple< uint8_t, uint8_t > DecodeVersion (std::string_view ver)
 
static bool VerifyShader (GLuint shader)
 Check a shader for compilation errors and log them if necessary.
 
static bool VerifyProgram (GLuint program)
 Check a program for link errors and log them if necessary.
 
template<class T >
static void ClearPixelBuffer (size_t len, T data)
 Clear the bound pixel buffer to a specific value.
 

Variables

static const int MAX_CACHED_CURSORS = 48
 Maximum number of cursor sprites to cache.
 
GetOGLProcAddressProc GetOGLProcAddress
 
static uint8_t _gl_major_ver = 0
 Major OpenGL version.
 
static uint8_t _gl_minor_ver = 0
 Minor OpenGL version.
 

Detailed Description

OpenGL video driver support.

Definition in file opengl.cpp.

Macro Definition Documentation

◆ GL

#define GL (   function)    static decltype(&function) _ ## function

Definition at line 48 of file opengl.cpp.

◆ GL_GLEXT_PROTOTYPES

#define GL_GLEXT_PROTOTYPES

Definition at line 21 of file opengl.cpp.

Function Documentation

◆ BindBasicInfoProcs()

static bool BindBasicInfoProcs ( )
static

Bind basic information functions.

Definition at line 224 of file opengl.cpp.

References BindGLProc().

Referenced by OpenGLBackend::Init().

◆ BindBasicOpenGLProcs()

static bool BindBasicOpenGLProcs ( )
static

Bind OpenGL 1.0 and 1.1 functions.

Definition at line 234 of file opengl.cpp.

References BindGLProc().

Referenced by OpenGLBackend::Init().

◆ BindGLProc()

template<typename F >
static bool BindGLProc ( F &  f,
const char *  name 
)
static

Try loading an OpenGL function.

Template Parameters
FType of the function pointer.
Parameters
fReference where to store the function pointer in.
nameName of the function.
Returns
True if the function could be bound.

Definition at line 217 of file opengl.cpp.

Referenced by BindBasicInfoProcs(), BindBasicOpenGLProcs(), BindPersistentBufferExtensions(), BindShaderExtensions(), BindTextureExtensions(), BindVBAExtension(), BindVBOExtension(), and SetupDebugOutput().

◆ BindPersistentBufferExtensions()

static bool BindPersistentBufferExtensions ( )
static

Bind extension functions for persistent buffer mapping.

Definition at line 384 of file opengl.cpp.

References BindGLProc(), IsOpenGLExtensionSupported(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ BindShaderExtensions()

static bool BindShaderExtensions ( )
static

Bind extension functions for shader support.

Definition at line 318 of file opengl.cpp.

References BindGLProc(), IsOpenGLExtensionSupported(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ BindTextureExtensions()

static bool BindTextureExtensions ( )
static

Bind texture-related extension functions.

Definition at line 257 of file opengl.cpp.

References BindGLProc(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ BindVBAExtension()

static bool BindVBAExtension ( )
static

Bind vertex array object extension functions.

Definition at line 299 of file opengl.cpp.

References BindGLProc(), IsOpenGLExtensionSupported(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ BindVBOExtension()

static bool BindVBOExtension ( )
static

Bind vertex buffer object extension functions.

Definition at line 269 of file opengl.cpp.

References BindGLProc(), IsOpenGLExtensionSupported(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ ClearPixelBuffer()

template<class T >
static void ClearPixelBuffer ( size_t  len,
data 
)
static

Clear the bound pixel buffer to a specific value.

Parameters
lenLength of the buffer.
dataValue to set.
Template Parameters
TPixel type.

Definition at line 897 of file opengl.cpp.

◆ DebugOutputCallback()

void APIENTRY DebugOutputCallback ( GLenum  source,
GLenum  type,
GLuint  id,
GLenum  severity,
GLsizei  length,
const GLchar *  message,
const void *  userParam 
)

Callback to receive OpenGL debug messages.

Definition at line 405 of file opengl.cpp.

References Debug.

Referenced by SetupDebugOutput().

◆ DecodeVersion()

static std::tuple< uint8_t, uint8_t > DecodeVersion ( std::string_view  ver)
static

Definition at line 517 of file opengl.cpp.

◆ GlGetString()

static std::optional< std::string_view > GlGetString ( GLenum  name)
static

Definition at line 136 of file opengl.cpp.

◆ HasStringInExtensionList()

bool HasStringInExtensionList ( std::string_view  string,
std::string_view  substring 
)

Find a substring in a string made of space delimited elements.

The substring has to match the complete element, partial matches don't count.

Parameters
stringList of space delimited elements.
substringSubstring to find.
Returns
Whether the substring was found.

Definition at line 150 of file opengl.cpp.

References StringConsumer::SKIP_ALL_SEPARATORS.

Referenced by IsOpenGLExtensionSupported().

◆ IsOpenGLExtensionSupported()

static bool IsOpenGLExtensionSupported ( std::string_view  extension)
static

Check if an OpenGL extension is supported by the current context.

Parameters
extensionThe extension string to test.
Returns
True if the extension is supported, false if not.

Definition at line 165 of file opengl.cpp.

References HasStringInExtensionList(), and IsOpenGLVersionAtLeast().

Referenced by BindPersistentBufferExtensions(), BindShaderExtensions(), BindVBAExtension(), BindVBOExtension(), OpenGLBackend::Init(), and SetupDebugOutput().

◆ IsOpenGLVersionAtLeast()

bool IsOpenGLVersionAtLeast ( uint8_t  major,
uint8_t  minor 
)

Check if the current OpenGL version is equal or higher than a given one.

Parameters
majorMinimal major version.
minorMinimal minor version.
Precondition
OpenGL was initialized.
Returns
True if the OpenGL version is equal or higher than the requested one.

Definition at line 204 of file opengl.cpp.

References _gl_major_ver, and _gl_minor_ver.

Referenced by BindPersistentBufferExtensions(), BindShaderExtensions(), BindTextureExtensions(), BindVBAExtension(), BindVBOExtension(), OpenGLBackend::Init(), OpenGLBackend::InitShaders(), IsOpenGLExtensionSupported(), and SetupDebugOutput().

◆ SetupDebugOutput()

void SetupDebugOutput ( )

Enable OpenGL debug messages if supported.

Definition at line 429 of file opengl.cpp.

References BindGLProc(), DebugOutputCallback(), IsOpenGLExtensionSupported(), and IsOpenGLVersionAtLeast().

Referenced by OpenGLBackend::Init().

◆ VerifyProgram()

static bool VerifyProgram ( GLuint  program)
static

Check a program for link errors and log them if necessary.

Parameters
programProgram to check.
Returns
True if the program is valid.

Definition at line 783 of file opengl.cpp.

References ReusableBuffer< T >::Allocate(), Debug, and ReusableBuffer< T >::GetBuffer().

Referenced by OpenGLBackend::InitShaders().

◆ VerifyShader()

static bool VerifyShader ( GLuint  shader)
static

Check a shader for compilation errors and log them if necessary.

Parameters
shaderShader to check.
Returns
True if the shader is valid.

Definition at line 760 of file opengl.cpp.

References ReusableBuffer< T >::Allocate(), Debug, and ReusableBuffer< T >::GetBuffer().

Referenced by OpenGLBackend::InitShaders().

Variable Documentation

◆ _gl_major_ver

uint8_t _gl_major_ver = 0
static

Major OpenGL version.

Definition at line 194 of file opengl.cpp.

Referenced by OpenGLBackend::Init(), and IsOpenGLVersionAtLeast().

◆ _gl_minor_ver

uint8_t _gl_minor_ver = 0
static

Minor OpenGL version.

Definition at line 195 of file opengl.cpp.

Referenced by OpenGLBackend::Init(), and IsOpenGLVersionAtLeast().

◆ GetOGLProcAddress

GetOGLProcAddressProc GetOGLProcAddress

Definition at line 134 of file opengl.cpp.

◆ MAX_CACHED_CURSORS

const int MAX_CACHED_CURSORS = 48
static

Maximum number of cursor sprites to cache.

Definition at line 130 of file opengl.cpp.