| 1 |
{ |
| 2 |
Copyright 2013-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Initializes OpenGL using Unix glX. } |
| 17 |
unit CastleInternalContextGlx; |
| 18 |
|
| 19 |
{$i castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
{$ifndef UNIX_WITH_X} |
| 24 |
{ To make it easier to organize Lazarus packages |
| 25 |
(to keep this unit in castle_base.lpk, since it doesn't depend on TCastleWindow, |
| 26 |
but at the same time this unit cannot have "<AddToUsesPkgSection Value="False"/>" |
| 27 |
in castle_base.lpk because castle_window.lpk expects to use this unit |
| 28 |
without recompiling...) -> |
| 29 |
this unit compiles even for platforms that don't have glX, |
| 30 |
the unit just doesn't define anything in this case. } |
| 31 |
implementation |
| 32 |
end. |
| 33 |
{$else} |
| 34 |
|
| 35 |
uses SysUtils, Classes, |
| 36 |
Glx, X, XUtil, Xlib, |
| 37 |
CastleInternalContextBase; |
| 38 |
|
| 39 |
type |
| 40 |
{ Initializes OpenGL using Unix glX. } |
| 41 |
TGLContextGlx = class(TGLContext) |
| 42 |
private |
| 43 |
Context: GLXContext; |
| 44 |
UseFBConfig: boolean; |
| 45 |
FBConfig: TGLXFBConfig; |
| 46 |
procedure GlxAvailableCheck; |
| 47 |
protected |
| 48 |
procedure InitializeCore(const Requirements: TGLContextRequirements); override; |
| 49 |
procedure FinalizeCore; override; |
| 50 |
procedure MakeCurrentCore; override; |
| 51 |
procedure SwapBuffersCore; override; |
| 52 |
public |
| 53 |
{ Set this before using Initialize and other methods. |
| 54 |
|
| 55 |
Note: screen of a window can change at runtime. |
| 56 |
But this class doesn't use XScreen after context initialization. } |
| 57 |
XDisplay: PDisplay; |
| 58 |
XScreen: Integer; |
| 59 |
|
| 60 |
// Initialized by InitializeEarly, freed by FinalizeLate |
| 61 |
XVisual: PXVisualInfo; |
| 62 |
|
| 63 |
// Set before SwapBuffer, MakeCurrent |
| 64 |
// TODO: simplify, require it before Initialize |
| 65 |
WindowXID: TWindow; |
| 66 |
|
| 67 |
{ Call before creating an X window (since window creation needs XVisual) |
| 68 |
to create XVisual. } |
| 69 |
procedure InitializeEarly(const Requirements: TGLContextRequirements); |
| 70 |
|
| 71 |
{ Call after window is destroyed to safely free XVisual.} |
| 72 |
procedure FinalizeLate; |
| 73 |
end; |
| 74 |
|
| 75 |
implementation |
| 76 |
|
| 77 |
uses CTypes, |
| 78 |
CastleLog, CastleUtils, CastleGLUtils; |
| 79 |
|
| 80 |
{ TGLContextGlx -------------------------------------------------------------- } |
| 81 |
|
| 82 |
procedure TGLContextGlx.GlxAvailableCheck; |
| 83 |
|
| 84 |
function GlxConfirmedVersion: String; |
| 85 |
begin |
| 86 |
if GLX_version_1_4(XDisplay) then |
| 87 |
GlxConfirmedVersion := '1.4' |
| 88 |
else |
| 89 |
if GLX_version_1_3(XDisplay) then |
| 90 |
GlxConfirmedVersion := '1.3' |
| 91 |
else |
| 92 |
if GLX_version_1_2(XDisplay) then |
| 93 |
GlxConfirmedVersion := '1.2' |
| 94 |
else |
| 95 |
if GLX_version_1_1(XDisplay) then |
| 96 |
GlxConfirmedVersion := '1.1' |
| 97 |
else |
| 98 |
if GLX_version_1_0(XDisplay) then |
| 99 |
GlxConfirmedVersion := '1.0' |
| 100 |
else |
| 101 |
GlxConfirmedVersion := 'None'; |
| 102 |
end; |
| 103 |
|
| 104 |
var |
| 105 |
GlxExtensions: String; |
| 106 |
begin |
| 107 |
if not GLX_version_1_0(XDisplay) then |
| 108 |
begin |
| 109 |
raise Exception.Create('glX extension (version at least 1.0) not found (necessary for OpenGL-based programs)'); |
| 110 |
end else |
| 111 |
begin |
| 112 |
GlxExtensions := glXQueryExtensionsString(XDisplay, XScreen); |
| 113 |
if LogGLInformationVerbose then |
| 114 |
begin |
| 115 |
WritelnLogMultiline('GLX', 'glX extension at least 1.0 found.' +NL+ |
| 116 |
'Versions (determined by checking both glXQueryExtension, glXQueryVersion and assigned entry points):' +NL+ |
| 117 |
Format(' Version 1.1: %s', [BoolToStr(GLX_version_1_1(XDisplay), true)]) +NL+ |
| 118 |
Format(' Version 1.2: %s', [BoolToStr(GLX_version_1_2(XDisplay), true)]) +NL+ |
| 119 |
Format(' Version 1.3: %s', [BoolToStr(GLX_version_1_3(XDisplay), true)]) +NL+ |
| 120 |
Format(' Version 1.4: %s', [BoolToStr(GLX_version_1_4(XDisplay), true)]) +NL+ |
| 121 |
NL+ |
| 122 |
'Important extensions (determined by checking glXQueryExtensionsString and assigned entry points):' +NL+ |
| 123 |
Format(' GLX_ARB_multisample: %s' , [BoolToStr(GLX_ARB_multisample(XDisplay, XScreen), true)]) + NL+ |
| 124 |
Format(' GLX_ARB_create_context: %s', [BoolToStr(GLX_ARB_create_context(XDisplay, XScreen), true)]) +NL+ |
| 125 |
NL+ |
| 126 |
'All extensions (according to glXQueryExtensionsString):' +NL+ |
| 127 |
GlxExtensions); |
| 128 |
end else |
| 129 |
begin |
| 130 |
WritelnLog('glX initialized (version >= %s). Multisample possible: %s, modern context creation: %s (more info: LogGLInformationVerbose:=true)', [ |
| 131 |
GlxConfirmedVersion, |
| 132 |
BoolToStr(GLX_ARB_multisample(XDisplay, XScreen), true), |
| 133 |
BoolToStr(GLX_ARB_create_context(XDisplay, XScreen), true) |
| 134 |
]); |
| 135 |
end; |
| 136 |
end; |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TGLContextGlx.InitializeEarly(const Requirements: TGLContextRequirements); |
| 140 |
var |
| 141 |
Attribs: TInt32List; |
| 142 |
FBConfigs: PGLXFBConfig; |
| 143 |
FBConfigsCount: Integer; |
| 144 |
begin |
| 145 |
GlxAvailableCheck; |
| 146 |
|
| 147 |
{ Use new glX 1.3 functions to create context, that take TGLXFBConfig? |
| 148 |
About modern GLX context selection, see |
| 149 |
http://www.opengl.org/wiki/Creating_an_OpenGL_Context and |
| 150 |
http://www.opengl.org/wiki/Tutorial:_OpenGL_3.0_Context_Creation_%28GLX%29 } |
| 151 |
UseFBConfig := GLX_version_1_3(XDisplay); |
| 152 |
|
| 153 |
Attribs := TInt32List.Create; |
| 154 |
try |
| 155 |
{ constant Attribs part } |
| 156 |
if UseFBConfig then |
| 157 |
Attribs.AddRange([GLX_X_RENDERABLE, 1 { true }]) else |
| 158 |
Attribs.Add(GLX_RGBA); |
| 159 |
|
| 160 |
if Requirements.DoubleBuffer then |
| 161 |
if UseFBConfig then |
| 162 |
Attribs.AddRange([GLX_DOUBLEBUFFER, 1]) else |
| 163 |
Attribs.Add(GLX_DOUBLEBUFFER); |
| 164 |
|
| 165 |
Attribs.AddRange([ |
| 166 |
{ Buffer sizes below are all in bits. } |
| 167 |
GLX_DEPTH_SIZE, Requirements.DepthBits, |
| 168 |
GLX_STENCIL_SIZE, Requirements.StencilBits, |
| 169 |
GLX_ALPHA_SIZE, Requirements.AlphaBits |
| 170 |
]); |
| 171 |
if Requirements.RedBits <> 0 then Attribs.AddRange([GLX_RED_SIZE , Requirements.RedBits ]); |
| 172 |
if Requirements.GreenBits <> 0 then Attribs.AddRange([GLX_GREEN_SIZE, Requirements.GreenBits]); |
| 173 |
if Requirements.BlueBits <> 0 then Attribs.AddRange([GLX_BLUE_SIZE , Requirements.BlueBits ]); |
| 174 |
|
| 175 |
if Requirements.MultiSampling > 1 then |
| 176 |
begin |
| 177 |
if GLX_ARB_multisample(XDisplay, XScreen) then |
| 178 |
begin |
| 179 |
Attribs.AddRange([ |
| 180 |
GLX_SAMPLE_BUFFERS_ARB, 1, |
| 181 |
GLX_SAMPLES_ARB, Requirements.MultiSampling ]); |
| 182 |
WritelnLog('MultiSampling', 'GLX_ARB_multisample supported, using multisampling'); |
| 183 |
end else |
| 184 |
raise EGLContextNotPossible.CreateFmt('Multisampling (%d samples) ' + |
| 185 |
'requested, but GLX_ARB_multisample not supported on this screen', |
| 186 |
[Requirements.MultiSampling]); |
| 187 |
end; |
| 188 |
|
| 189 |
{ end of Attribs array } |
| 190 |
Attribs.Add(None); |
| 191 |
|
| 192 |
if UseFBConfig then |
| 193 |
begin |
| 194 |
FBConfigs := glXChooseFBConfig(XDisplay, XScreen, |
| 195 |
Attribs.L, FBConfigsCount); |
| 196 |
if FBConfigsCount = 0 then |
| 197 |
raise EGLContextNotPossible.CreateFmt('No frame buffer configurations that match the specified attributes (%s)', |
| 198 |
[Requirements.RequestedBufferAttributes]); |
| 199 |
{ just choose the first FB config from the FBConfigs list. |
| 200 |
More involved selection possible. } |
| 201 |
FBConfig := FBConfigs^; |
| 202 |
XVisual := glXGetVisualFromFBConfig(XDisplay, FBConfig); |
| 203 |
end else |
| 204 |
XVisual := glXChooseVisual(XDisplay, XScreen, Attribs.L); |
| 205 |
finally FreeAndNil(Attribs) end; |
| 206 |
|
| 207 |
if XVisual = nil then |
| 208 |
raise EGLContextNotPossible.CreateFmt( |
| 209 |
'X visual with requested attributes (%s) not found', |
| 210 |
[ Requirements.RequestedBufferAttributes ]); |
| 211 |
{ XScreen should always be XVisual.screen now } |
| 212 |
end; |
| 213 |
|
| 214 |
procedure TGLContextGlx.InitializeCore(const Requirements: TGLContextRequirements); |
| 215 |
var |
| 216 |
ShareContextGlx: GLXContext; |
| 217 |
|
| 218 |
function UseCreateContextAttribsARB: GLXContext; |
| 219 |
var |
| 220 |
Attribs: TInt32List; |
| 221 |
begin |
| 222 |
{ TODO: |
| 223 |
|
| 224 |
We should catch X errors (see XSync and XSetErrorHandler inside |
| 225 |
http://www.opengl.org/wiki/Tutorial:_OpenGL_3.0_Context_Creation_%28GLX%29 ) |
| 226 |
and fallback on non-modern context, |
| 227 |
not using glXCreateContextAttribsARB, if it is impossible to get modern one. |
| 228 |
|
| 229 |
Right now, our XLibUtils catches X errors, but without XSync |
| 230 |
it's not known where they occur anyway. } |
| 231 |
|
| 232 |
Attribs := TInt32List.Create; |
| 233 |
try |
| 234 |
{$ifdef OpenGLES} |
| 235 |
{$fatal Cannot initialize OpenGLES context using glX. If you want to use OpenGLES, use EGL (see USE_EGL in CastleWindow unit). Or use regular desktop OpenGL, not OpenGLES, on regular Unix.} |
| 236 |
{$endif} |
| 237 |
|
| 238 |
{ Select 3.2 core context. |
| 239 |
See https://registry.khronos.org/OpenGL/extensions/ARB/GLX_ARB_create_context.txt } |
| 240 |
if TGLFeatures.RequestCapabilities = rcForceModern then |
| 241 |
begin |
| 242 |
Attribs.AddRange([ |
| 243 |
GLX_CONTEXT_MAJOR_VERSION_ARB, TGLFeatures.ModernVersionMajor, |
| 244 |
GLX_CONTEXT_MINOR_VERSION_ARB, TGLFeatures.ModernVersionMinor, |
| 245 |
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
| 246 |
{ Not used, following https://www.khronos.org/opengl/wiki/OpenGL_Context#Context_types |
| 247 |
forward-compatible really only makes sense on macOS. |
| 248 |
The "core" profile is what should be just used with new OpenGLs on sane |
| 249 |
(non-macOS) platforms. } |
| 250 |
//GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, |
| 251 |
]); |
| 252 |
end; |
| 253 |
{ Select debug context. |
| 254 |
Note that the implementation assumes nothing above passes GLX_CONTEXT_FLAGS_ARB. } |
| 255 |
if TGLFeatures.Debug then |
| 256 |
begin |
| 257 |
Attribs.AddRange([ |
| 258 |
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB |
| 259 |
]); |
| 260 |
end; |
| 261 |
Attribs.AddRange([None]); |
| 262 |
Assert(SizeOf(CInt) = SizeOf(Int32)); // make sure Attribs match PCInt type |
| 263 |
Result := glXCreateContextAttribsARB(XDisplay, FBConfig, ShareContextGlx, true, PCInt(Attribs.L)); |
| 264 |
finally FreeAndNil(Attribs) end; |
| 265 |
end; |
| 266 |
|
| 267 |
begin |
| 268 |
{ All OpenGL contexts should be shared } |
| 269 |
if SharedContext <> nil then |
| 270 |
ShareContextGlx := (SharedContext as TGLContextGlx).Context |
| 271 |
else |
| 272 |
ShareContextGlx := nil; |
| 273 |
|
| 274 |
if UseFBConfig then |
| 275 |
begin |
| 276 |
if (TGLFeatures.RequestCapabilities = rcForceModern) or |
| 277 |
TGLFeatures.Debug then |
| 278 |
begin |
| 279 |
if not GLX_ARB_create_context_profile(XDisplay, XScreen) then |
| 280 |
raise EGLContextNotPossible.Create('GLX_ARB_create_context_profile not available, cannot initialize context with "core" profile or debug bit'); |
| 281 |
Context := UseCreateContextAttribsARB; |
| 282 |
end else |
| 283 |
Context := glXCreateNewContext(XDisplay, FBConfig, GLX_RGBA_TYPE, ShareContextGlx, true); |
| 284 |
end else |
| 285 |
Context := glXCreateContext(XDisplay, XVisual, ShareContextGlx, true); |
| 286 |
|
| 287 |
Check(Context <> nil, 'Could not initialize rendering context'); |
| 288 |
end; |
| 289 |
|
| 290 |
procedure TGLContextGlx.FinalizeCore; |
| 291 |
begin |
| 292 |
if Context <> nil then |
| 293 |
begin |
| 294 |
glXDestroyContext(XDisplay, Context); |
| 295 |
Context := nil; |
| 296 |
end; |
| 297 |
end; |
| 298 |
|
| 299 |
procedure TGLContextGlx.FinalizeLate; |
| 300 |
begin |
| 301 |
if XVisual <> nil then |
| 302 |
begin |
| 303 |
XFree(XVisual); |
| 304 |
XVisual := nil; |
| 305 |
end; |
| 306 |
end; |
| 307 |
|
| 308 |
procedure TGLContextGlx.MakeCurrentCore; |
| 309 |
begin |
| 310 |
Check( glXMakeCurrent(XDisplay, WindowXID, Context), 'glXMakeCurrent'); |
| 311 |
end; |
| 312 |
|
| 313 |
procedure TGLContextGlx.SwapBuffersCore; |
| 314 |
begin |
| 315 |
glXSwapBuffers(XDisplay, WindowXID); |
| 316 |
end; |
| 317 |
|
| 318 |
end. |
| 319 |
|
| 320 |
{$endif} |