Cocoa: Allow sharing OpenGL context resources

This commit is contained in:
Michalis Kamburelis 2022-05-23 01:56:13 +01:00 committed by Maxim Ganetsky
parent 7995fdd441
commit 06215a7aa2

View File

@ -14,7 +14,6 @@
Mouse: Mouse:
the TLCLCommonCallback mouse handlers check Owner.isEnabled, which the TLCLCommonCallback mouse handlers check Owner.isEnabled, which
for a NSView always returns false. for a NSView always returns false.
SharedControl
} }
unit GLCocoaNSContext; unit GLCocoaNSContext;
@ -67,7 +66,6 @@ type
TCocoaOpenGLView = objcclass(NSOpenGLView) TCocoaOpenGLView = objcclass(NSOpenGLView)
public public
Owner: TWinControl; Owner: TWinControl;
//nsGL: NSOpenGLContext;
callback: TLCLCommonCallback; callback: TLCLCommonCallback;
backingScaleFactor: Single; backingScaleFactor: Single;
function acceptsFirstResponder: LCLObjCBoolean; override; function acceptsFirstResponder: LCLObjCBoolean; override;
@ -189,8 +187,7 @@ var
PixFmt: NSOpenGLPixelFormat; PixFmt: NSOpenGLPixelFormat;
p: NSView; p: NSView;
ns: NSRect; ns: NSRect;
aNSOpenGLContext: NSOpenGLContext; aNSOpenGLContext, SharedContext: NSOpenGLContext;
CGLContext: CGLContextObj;
begin begin
Result:=0; Result:=0;
p := nil; p := nil;
@ -204,7 +201,12 @@ begin
Attrs:=CreateOpenGLContextAttrList(DoubleBuffered,MajorVersion,MinorVersion, MultiSampling,AlphaBits,DepthBits,StencilBits,AUXBuffers); Attrs:=CreateOpenGLContextAttrList(DoubleBuffered,MajorVersion,MinorVersion, MultiSampling,AlphaBits,DepthBits,StencilBits,AUXBuffers);
try try
PixFmt:=NSOpenGLPixelFormat(NSOpenGLPixelFormat.alloc).initWithAttributes(Attrs); PixFmt:=NSOpenGLPixelFormat(NSOpenGLPixelFormat.alloc).initWithAttributes(Attrs);
aNSOpenGLContext:=NSOpenGLContext(NSOpenGLContext.alloc).initWithFormat_shareContext(PixFmt,nil); { Use SharedControl to share OpenGL resources with another TOpenGLControl instance }
if SharedControl <> nil then
SharedContext := TCocoaOpenGLView(SharedControl.Handle).openGLContext
else
SharedContext := nil;
aNSOpenGLContext:=NSOpenGLContext(NSOpenGLContext.alloc).initWithFormat_shareContext(PixFmt,SharedContext);
if aNSOpenGLContext = nil then if aNSOpenGLContext = nil then
debugln(['LOpenGLCreateContext Error']); debugln(['LOpenGLCreateContext Error']);
View := TCocoaOpenGLView(TCocoaOpenGLView.alloc).initWithFrame_pixelFormat(ns,PixFmt); View := TCocoaOpenGLView(TCocoaOpenGLView.alloc).initWithFrame_pixelFormat(ns,PixFmt);
@ -217,7 +219,9 @@ begin
p.addSubview(View); p.addSubview(View);
SetViewDefaults(View); SetViewDefaults(View);
View.Owner:=AWinControl; View.Owner:=AWinControl;
//View.nsGL := aNSOpenGLContext; { If we wouldn't set View.openGLContext, it would get automatically created.
But then aNSOpenGLContext is ignored, and so SharedContext and SharedControl don't work. }
View.setOpenGLContext(aNSOpenGLContext);
View.callback:=TLCLCommonCallback.Create(View, AWinControl); View.callback:=TLCLCommonCallback.Create(View, AWinControl);
LSetWantsBestResolutionOpenGLSurface(AMacRetinaMode, HWND(View)); LSetWantsBestResolutionOpenGLSurface(AMacRetinaMode, HWND(View));
//View.setPixelFormat(PixFmt); //View.setPixelFormat(PixFmt);