Skip to content

Commit 84161c8

Browse files
OpenGL Logical Device, Queue and Swapchain getNativeHandle (expose EGL context and surface)
1 parent 0261077 commit 84161c8

File tree

10 files changed

+57
-74
lines changed

10 files changed

+57
-74
lines changed

include/nbl/video/CEGL.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@
77
namespace nbl::video::egl
88
{
99

10+
// any EGL handles are not native EGL handles, they come from our EGL over WGL/GLX/EGL implementation!
1011
class CEGL
1112
{
1213
public:
14+
//
15+
struct Context
16+
{
17+
EGLContext ctx = EGL_NO_CONTEXT;
18+
EGLSurface surface = EGL_NO_SURFACE;
19+
20+
// to load function pointers, make EGL context current and use `egl->call.peglGetProcAddress("glFuncname")`
21+
};
22+
23+
//
1324
CEGL(const char* eglOptionalPath) : call(eglOptionalPath) {}
1425

1526
bool initialize()

include/nbl/video/COpenGL_Connection.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ template<E_API_TYPE API_TYPE>
1414
class COpenGL_Connection final : public IAPIConnection
1515
{
1616
public:
17-
//
18-
struct SStuff
19-
{
20-
// to load function pointers, make EGL context current and use `egl->call.peglGetProcAddress("glFuncname")`
21-
};
22-
2317
//
2418
static core::smart_refctd_ptr<COpenGL_Connection<API_TYPE>> create(core::smart_refctd_ptr<system::ISystem>&& sys, uint32_t appVer, const char* appName, COpenGLDebugCallback&& dbgCb);
2519

include/nbl/video/IGPUImageView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class IGPUImageView : public asset::IImageView<IGPUImage>, public IBackendObject
2020
const SCreationParams& getCreationParameters() const { return params; }
2121

2222
// OpenGL: const GLuint* handle of GL_TEXTURE_VIEW target
23-
// Vulkan: const VKImageView*
23+
// Vulkan: const VkImageView*
2424
virtual const void* getNativeHandle() const = 0;
2525

2626
protected:

include/nbl/video/IGPUQueue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class IGPUQueue : public core::Interface, public core::Unmovable
6363

6464
inline constexpr static float DEFAULT_QUEUE_PRIORITY = 1.f;
6565

66-
// OpenGL: GLsync_
66+
// OpenGL: const egl::CEGL::Context*
6767
// Vulkan: const VkQueue*
6868
virtual const void* getNativeHandle() const = 0;
6969

include/nbl/video/ILogicalDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ class ILogicalDevice : public core::IReferenceCounted
644644
return IGPUAccelerationStructure::BuildSizes{};
645645
}
646646

647-
// OpenGL: EGL
647+
// OpenGL: const egl::CEGL::Context*
648648
// Vulkan: const VkDevice*
649649
virtual const void* getNativeHandle() const = 0;
650650

include/nbl/video/ISwapchain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ISwapchain : public core::IReferenceCounted, public IBackendObject
8484
return m_params;
8585
}
8686

87-
// OpenGL: EGL
87+
// OpenGL: const egl::CEGL::Context*
8888
// Vulkan: const VkSwapchainKHR*
8989
virtual const void* getNativeHandle() const = 0;
9090

src/nbl/video/COpenGL_LogicalDevice.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
6868
m_config(config),
6969
m_gl_ver(major, minor)
7070
{
71-
EGLContext master_ctx = m_threadHandler.getContext();
72-
7371
uint32_t totalQCount = getTotalQueueCount(params);
7472
assert(totalQCount <= MaxQueueCount);
7573

@@ -84,16 +82,16 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
8482
{
8583
const float priority = qci.priorities[j];
8684

87-
SGLContext glctx = createWindowlessGLContext(FunctionTableType::EGL_API_TYPE, _egl, major, minor, config, master_ctx);
85+
auto glctx = createWindowlessGLContext(FunctionTableType::EGL_API_TYPE, _egl, major, minor, config, m_threadHandler.glctx.ctx);
8886

8987
const uint32_t ix = offset + j;
9088
const uint32_t ctxid = 1u + ix; // +1 because one ctx is here, in logical device (consider if it means we have to have another spec shader GL name for it, probably not) -- [TODO]
9189

9290
(*m_queues)[ix] = new CThreadSafeGPUQueueAdapter
9391
(
9492
this,
95-
(IGPUQueue*)new QueueType(this, rdoc, _egl, m_glfeatures, ctxid, glctx.ctx,
96-
glctx.pbuffer, famIx, flags, priority,
93+
(IGPUQueue*)new QueueType(this, rdoc, _egl, m_glfeatures, ctxid,
94+
glctx, famIx, flags, priority,
9795
static_cast<COpenGLDebugCallback*>(physicalDevice->getDebugCallback()))
9896
);
9997
}
@@ -195,13 +193,12 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
195193
return nullptr;
196194
}
197195

198-
EGLContext master_ctx = m_threadHandler.getContext();
199196
EGLConfig fbconfig = m_config;
200197
auto glver = m_gl_ver;
201198

202199
// master context must not be current while creating a context with whom it will be sharing
203200
unbindMasterContext();
204-
EGLContext ctx = createGLContext(FunctionTableType::EGL_API_TYPE, m_egl, glver.first, glver.second, fbconfig, master_ctx);
201+
EGLContext ctx = createGLContext(FunctionTableType::EGL_API_TYPE, m_egl, glver.first, glver.second, fbconfig, m_threadHandler.glctx.ctx);
205202
auto sc = SwapchainType::create(std::move(params),core::smart_refctd_ptr<IOpenGL_LogicalDevice>(this),m_egl,std::move(images),m_glfeatures,ctx,fbconfig,static_cast<COpenGLDebugCallback*>(m_physicalDevice->getDebugCallback()));
206203
if (!sc)
207204
return nullptr;
@@ -665,6 +662,9 @@ class COpenGL_LogicalDevice : public IOpenGL_LogicalDevice
665662
openglQueue->destroyQueries(queriesToDestroy);
666663
}
667664
}
665+
666+
const void* getNativeHandle() const override { return &m_threadHandler.glctx; }
667+
668668
protected:
669669
void bindMasterContext()
670670
{

src/nbl/video/COpenGL_Queue.h

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ class COpenGL_Queue final : public IGPUQueue
128128
using base_t = system::IAsyncQueueDispatcher<CThreadHandler, SRequest, 256u, ThreadInternalStateType>;
129129
friend base_t;
130130

131-
CThreadHandler(const egl::CEGL* _egl, renderdoc_api_t* rdoc, IOpenGL_LogicalDevice* dev, const FeaturesType* _features, EGLContext _ctx, EGLSurface _pbuf, uint32_t _ctxid, COpenGLDebugCallback* _dbgCb) :
131+
CThreadHandler(const egl::CEGL* _egl, renderdoc_api_t* rdoc, IOpenGL_LogicalDevice* dev, const FeaturesType* _features, const egl::CEGL::Context& _glctx, uint32_t _ctxid, COpenGLDebugCallback* _dbgCb) :
132132
m_rdoc_api(rdoc),
133133
egl(_egl),
134134
m_device(dev), m_masterContextCallsWaited(0),
135-
thisCtx(_ctx), pbuffer(_pbuf),
135+
glctx(_glctx),
136136
features(_features),
137137
m_ctxid(_ctxid),
138138
m_dbgCb(_dbgCb)
@@ -158,7 +158,7 @@ class COpenGL_Queue final : public IGPUQueue
158158
EGLBoolean mcres = EGL_FALSE;
159159
while (mcres!=EGL_TRUE)
160160
{
161-
mcres = egl->call.peglMakeCurrent(egl->display,pbuffer,pbuffer,thisCtx);
161+
mcres = egl->call.peglMakeCurrent(egl->display, glctx.surface, glctx.surface, glctx.ctx);
162162
/*
163163
* I think Queue context creation has a timing bug
164164
* Debug build breaks, context can never be made current
@@ -168,9 +168,9 @@ class COpenGL_Queue final : public IGPUQueue
168168
//_NBL_DEBUG_BREAK_IF(mcres!=EGL_TRUE);
169169
}
170170

171-
#ifndef _NBL_PLATFORM_ANDROID_
172-
egl->call.peglGetPlatformDependentHandles(&nativeHandles, egl->display, pbuffer, thisCtx);
173-
#endif
171+
#ifndef _NBL_PLATFORM_ANDROID_
172+
egl->call.peglGetPlatformDependentHandles(&nativeHandles, egl->display, glctx.surface, glctx.ctx);
173+
#endif
174174
new (state_ptr) ThreadInternalStateType(egl,features,core::smart_refctd_ptr<system::ILogger>(m_dbgCb->getLogger()));
175175
auto& gl = state_ptr->gl;
176176
auto& ctxlocal = state_ptr->ctxlocal;
@@ -323,18 +323,17 @@ class COpenGL_Queue final : public IGPUQueue
323323
state_ptr->~ThreadInternalStateType();
324324

325325
egl->call.peglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); // detach ctx from thread
326-
egl->call.peglDestroyContext(egl->display, thisCtx);
327-
egl->call.peglDestroySurface(egl->display, pbuffer);
326+
egl->call.peglDestroyContext(egl->display, glctx.ctx);
327+
egl->call.peglDestroySurface(egl->display, glctx.surface);
328328
}
329329

330330
renderdoc_api_t* m_rdoc_api;
331+
egl::CEGL::Context glctx;
331332
private:
332333
const egl::CEGL* egl;
333334
IOpenGL_LogicalDevice* m_device;
334335
uint64_t m_masterContextCallsWaited;
335336

336-
EGLContext thisCtx;
337-
EGLSurface pbuffer;
338337
const FeaturesType* features;
339338
uint32_t m_ctxid;
340339
COpenGLDebugCallback* m_dbgCb;
@@ -350,14 +349,13 @@ class COpenGL_Queue final : public IGPUQueue
350349
const egl::CEGL* _egl,
351350
const FeaturesType* _features,
352351
uint32_t _ctxid,
353-
EGLContext _ctx,
354-
EGLSurface _surface,
352+
const egl::CEGL::Context& _glctx,
355353
uint32_t _famIx,
356354
E_CREATE_FLAGS _flags,
357355
float _priority,
358356
COpenGLDebugCallback* _dbgCb
359357
) : IGPUQueue(gldev,_famIx,_flags,_priority),
360-
threadHandler(_egl,rdoc,gldev,_features,_ctx,_surface,_ctxid,_dbgCb),
358+
threadHandler(_egl,rdoc,gldev,_features,_glctx,_ctxid,_dbgCb),
361359
m_mempool(128u,1u,512u,sizeof(void*)),
362360
m_ctxid(_ctxid)
363361
{
@@ -556,7 +554,7 @@ class COpenGL_Queue final : public IGPUQueue
556554
return true;
557555
}
558556

559-
const void* getNativeHandle() const override {return nullptr;}
557+
const void* getNativeHandle() const override {return &threadHandler.glctx;}
560558

561559
protected:
562560
~COpenGL_Queue()

src/nbl/video/COpenGL_Swapchain.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class COpenGL_Swapchain final : public ISwapchain
110110
m_threadHandler.waitForInitComplete();
111111
}
112112

113-
virtual const void* getNativeHandle() const override {return nullptr;}
113+
virtual const void* getNativeHandle() const override {return &m_threadHandler.glctx;}
114114

115115
protected:
116116
// images will be created in COpenGLLogicalDevice::createSwapchain
@@ -149,7 +149,7 @@ class COpenGL_Swapchain final : public ISwapchain
149149
) : m_device(dev), m_masterContextCallsWaited(0),
150150
egl(_egl),
151151
m_presentMode(presentMode),
152-
thisCtx(_ctx), surface(EGL_NO_SURFACE),
152+
glctx{_ctx,EGL_NO_SURFACE},
153153
features(_features),
154154
images(_images),
155155
m_dbgCb(_dbgCb)
@@ -165,8 +165,8 @@ class COpenGL_Swapchain final : public ISwapchain
165165
EGL_NONE
166166
};
167167

168-
surface = _egl->call.peglCreateWindowSurface(_egl->display, _config, (EGLNativeWindowType)_window, surface_attributes);
169-
assert(surface != EGL_NO_SURFACE);
168+
glctx.surface = _egl->call.peglCreateWindowSurface(_egl->display, _config, (EGLNativeWindowType)_window, surface_attributes);
169+
assert(glctx.surface != EGL_NO_SURFACE);
170170

171171
base_t::start();
172172
}
@@ -195,13 +195,14 @@ class COpenGL_Swapchain final : public ISwapchain
195195
return syncs[imgix];
196196
}
197197

198+
egl::CEGL::Context glctx;
198199
protected:
199200

200201
void init(SThreadHandlerInternalState* state_ptr)
201202
{
202203
egl->call.peglBindAPI(FunctionTableType::EGL_API_TYPE);
203204

204-
EGLBoolean mcres = egl->call.peglMakeCurrent(egl->display, surface, surface, thisCtx);
205+
EGLBoolean mcres = egl->call.peglMakeCurrent(egl->display, glctx.surface, glctx.surface, glctx.ctx);
205206
assert(mcres == EGL_TRUE);
206207

207208
m_ctxCreatedCvar.notify_one();
@@ -285,7 +286,7 @@ class COpenGL_Swapchain final : public ISwapchain
285286
syncs[imgix]->init(m_device, &gl, false);
286287
// swap buffers performs an implicit flush before swapping
287288
// https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglSwapBuffers.xhtml
288-
egl->call.peglSwapBuffers(egl->display, surface);
289+
egl->call.peglSwapBuffers(egl->display, glctx.surface);
289290
}
290291

291292
void exit(SThreadHandlerInternalState* gl)
@@ -297,8 +298,8 @@ class COpenGL_Swapchain final : public ISwapchain
297298
gl->~SThreadHandlerInternalState();
298299

299300
egl->call.peglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
300-
egl->call.peglDestroyContext(egl->display, thisCtx);
301-
egl->call.peglDestroySurface(egl->display, surface);
301+
egl->call.peglDestroyContext(egl->display, glctx.ctx);
302+
egl->call.peglDestroySurface(egl->display, glctx.surface);
302303
}
303304

304305
bool wakeupPredicate() const { return needToBlit; }
@@ -309,16 +310,15 @@ class COpenGL_Swapchain final : public ISwapchain
309310
uint64_t m_masterContextCallsWaited;
310311

311312
const egl::CEGL* egl;
312-
EGLContext thisCtx;
313-
EGLSurface surface;
314313
ISurface::E_PRESENT_MODE m_presentMode;
315314
const COpenGLFeatureMap* features;
316315
core::SRange<core::smart_refctd_ptr<IGPUImage>> images;
317316
GLuint fbos[MaxImages]{};
318317
core::smart_refctd_ptr<COpenGLSync> syncs[MaxImages];
319318
COpenGLDebugCallback* m_dbgCb;
320-
std::array<GLuint, MaxImages> m_texViews;
321-
struct SRequest {
319+
std::array<GLuint,MaxImages> m_texViews;
320+
struct SRequest
321+
{
322322
SRequest() { sems.reserve(50); }
323323

324324
uint32_t imgIx = 0u;

0 commit comments

Comments
 (0)