Skip to content

TSdlGL

For working with OpenGL in SDL.

Definition

Unit: Neslib.Sdl3.Video

type TSdlGL = record ... end;

Properties

Name Description
Attributes OpenGL window attributes.
CurrentWindow The currently active OpenGL window.
SwapInterval The swap interval for the current OpenGL context.

Methods

Name Description
GetProcAddress Get an OpenGL function by name.
IsExtensionSupported Check if an OpenGL extension is supported for the current context.
LoadLibrary Dynamically load an OpenGL library.
ResetAttributes Reset all previously set OpenGL context attributes to their default values.
SwapWindow Update a window with OpenGL rendering.
UnloadLibrary Unload the OpenGL library previously loaded by LoadLibrary.

Property Descriptions

Attributes

OpenGL window attributes.

You should set these before window creation.

You shouls get these after creating the OpenGL context, since the values obtained can differ from the requested ones.

class property Attributes[const AAttr: TSdlGLAttr]: Integer read GetAttribute write SetAttribute

Type: Integer

Exceptions

ESdlError: Raised on failure.

See Also

Remarks

This property should only be used on the main thread.


CurrentWindow

The currently active OpenGL window.

class property CurrentWindow: TSdlWindow read GetCurrentWindow

Type: TSdlWindow

Exceptions

ESdlError: Raised on failure.

Remarks

This property should only be used on the main thread.


SwapInterval

The swap interval for the current OpenGL context.

Some systems allow specifying -1 for the interval, to enable adaptive vsync. Adaptive vsync works the same as vsync, but if you've already missed the vertical retrace for a given frame, it swaps buffers immediately, which might be less jarring for the user during occasional framerate drops. If an application requests adaptive vsync and the system does not support it, this function will fail and return false. In such a case, you should probably retry the call with 1 for the interval.

Adaptive vsync is implemented for some glX drivers with GLX_EXT_swap_control_tear, and for some Windows drivers with WGL_EXT_swap_control_tear.

If the system can't determine the swap interval, or there isn't a valid current context, this property will return 0 as a safe default.

Read more on the Khronos wiki.

class property SwapInterval: Integer read GetSwapInterval write SetSwapInterval

Type: Integer

Exceptions

ESdlError: Raised on failure.

Remarks

This property should only be used on the main thread.


Method Descriptions

GetProcAddress(String)

Get an OpenGL function by name.

If the GL library is loaded at runtime with LoadLibrary, then all GL functions must be retrieved this way. Usually this is used to retrieve function pointers to OpenGL extensions.

There are some quirks to looking up OpenGL functions that require some extra care from the application. If you code carefully, you can handle these quirks without any platform-specific code, though:

  • On Windows, function pointers are specific to the current GL context; this means you need to have created a GL context and made it current before calling GetProcAddress. If you recreate your context or create a second context, you should assume that any existing function pointers aren't valid to use with it. This is (currently) a Windows-specific limitation, and in practice lots of drivers don't suffer this limitation, but it is still the way the wgl API is documented to work and you should expect crashes if you don't respect it. Store a copy of the function pointers that comes and goes with context lifespan.
  • Some OpenGL drivers, on all platforms, will return nil if a function isn't supported, but you can't count on this behavior. Check for extensions you use, and if you get a nil anyway, act as if that extension wasn't available. This is probably a bug in the driver, but you can code defensively for this scenario anyhow.
  • OpenGL function pointers must be declared stdcall, even when not running on Windows. This will ensure the proper calling convention is followed on platforms where this matters (Win32) thereby avoiding stack corruption.

class function GetProcAddress(const AProc: String): Pointer; inline; static

Parameters

AProc: String : The name of an OpenGL function.

Returns

Pointer: A pointer to the named OpenGL function. The returned pointer should be cast to the appropriate function signature.

See Also

Remarks

This method should only be called on the main thread.


IsExtensionSupported(String)

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

This function operates on the current GL context; you must have created a context and it must be current before calling this function. Do not assume that all contexts you create will have the same set of extensions available, or that recreating an existing context will offer the same extensions again.

While it's probably not a massive overhead, this function is not an O(1) operation. Check the extensions you care about after creating the GL context and save that information somewhere instead of calling the function every time you need to know.

class function IsExtensionSupported(const AExtension: String): Boolean; inline; static

Parameters

AExtension: String : The name of the extension to check.

Returns

Boolean: True if the extension is supported, False otherwise.

Remarks

This method should only be called on the main thread.


LoadLibrary(String)

Dynamically load an OpenGL library.

This should be done after initializing the video driver, but before creating any OpenGL windows. If no OpenGL library is loaded, the default library will be loaded upon creation of the first OpenGL window.

If you do this, you need to retrieve all of the GL functions used in your program from the dynamic library using GetProcAddress.

class procedure LoadLibrary(const APath: String); static

Exceptions

ESdlError: Raised on failure.

Parameters

APath: String : The platform dependent OpenGL library name, or an empty string to open the default OpenGL library.

See Also

Remarks

This method should only be called on the main thread.


ResetAttributes

Reset all previously set OpenGL context attributes to their default values.

class procedure ResetAttributes; inline; static

See Also

Remarks

This method should only be called on the main thread.


SwapWindow(TSdlWindow)

Update a window with OpenGL rendering.

This is used with double-buffered OpenGL contexts, which are the default.

On macOS, make sure you bind 0 to the draw framebuffer before swapping the window, otherwise nothing will happen. If you aren't using glBindFramebuffer, this is the default and you won't have to do anything extra.

class procedure SwapWindow(const AWindow: TSdlWindow); inline; static

Exceptions

ESdlError: Raised on failure.

Parameters

AWindow: TSdlWindow : The window to change.

Remarks

This method should only be called on the main thread.


UnloadLibrary

Unload the OpenGL library previously loaded by LoadLibrary.

class procedure UnloadLibrary; inline; static

See Also

Remarks

This method should only be called on the main thread.