From 72be9be87d027fa2746bbc1b63c9beb1700cc371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 13 Jan 2016 21:18:59 -0800 Subject: [PATCH] Added ability to expose internal data (D3D device/GL context). --- include/bgfx/bgfxdefines.h | 2 +- include/bgfx/bgfxplatform.h | 22 +++++++++++++++++----- include/bgfx/c99/bgfx.h | 3 ++- include/bgfx/c99/bgfxplatform.h | 10 +++++++++- src/bgfx.cpp | 26 +++++++++++++++++++------- src/bgfx_p.h | 1 + src/glcontext_egl.cpp | 2 ++ src/glcontext_glx.cpp | 2 ++ src/glcontext_ppapi.cpp | 2 ++ src/glcontext_wgl.cpp | 2 ++ src/renderer_d3d11.cpp | 2 ++ src/renderer_d3d12.cpp | 2 ++ src/renderer_d3d9.cpp | 1 + 13 files changed, 62 insertions(+), 15 deletions(-) diff --git a/include/bgfx/bgfxdefines.h b/include/bgfx/bgfxdefines.h index 32a712af6..541fbc827 100644 --- a/include/bgfx/bgfxdefines.h +++ b/include/bgfx/bgfxdefines.h @@ -6,7 +6,7 @@ #ifndef BGFX_DEFINES_H_HEADER_GUARD #define BGFX_DEFINES_H_HEADER_GUARD -#define BGFX_API_VERSION UINT32_C(2) +#define BGFX_API_VERSION UINT32_C(3) /// #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001) //!< Enable RGB write. diff --git a/include/bgfx/bgfxplatform.h b/include/bgfx/bgfxplatform.h index b9107a26e..aa8088d2a 100644 --- a/include/bgfx/bgfxplatform.h +++ b/include/bgfx/bgfxplatform.h @@ -45,10 +45,10 @@ namespace bgfx /// struct PlatformData { - void* ndt; //!< Native display type - void* nwh; //!< Native window handle - void* context; //!< GL context, or D3D device - void* backBuffer; //!< GL backbuffer, or D3D render target view + void* ndt; //!< Native display type. + void* nwh; //!< Native window handle. + void* context; //!< GL context, or D3D device. + void* backBuffer; //!< GL backbuffer, or D3D render target view. void* backBufferDS; //!< Backbuffer depth/stencil. }; @@ -58,7 +58,19 @@ namespace bgfx /// /// @attention C99 equivalent is `bgfx_set_platform_data`. /// - void setPlatformData(const PlatformData& _hooks); + void setPlatformData(const PlatformData& _data); + + /// + struct InternalData + { + void* context; //!< GL context, or D3D device. + }; + + /// Get internal data for interop. + /// + /// @attention C99 equivalent is `bgfx_get_internal_data`. + /// + const InternalData* getInternalData(); } // namespace bgfx diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index a3b31ee9e..f35577dc3 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -433,7 +433,8 @@ typedef struct bgfx_allocator_vtbl typedef struct bgfx_interface_vtbl { bgfx_render_frame_t (*render_frame)(); - void (*set_platform_data)(bgfx_platform_data_t* _pd); + void (*set_platform_data)(const bgfx_platform_data_t* _data); + const bgfx_internal_data_t* (*get_platform_data)(); void (*vertex_decl_begin)(bgfx_vertex_decl_t* _decl, bgfx_renderer_type_t _renderer); void (*vertex_decl_add)(bgfx_vertex_decl_t* _decl, bgfx_attrib_t _attrib, uint8_t _num, bgfx_attrib_type_t _type, bool _normalized, bool _asInt); void (*vertex_decl_skip)(bgfx_vertex_decl_t* _decl, uint8_t _num); diff --git a/include/bgfx/c99/bgfxplatform.h b/include/bgfx/c99/bgfxplatform.h index ff2125f18..06a0f6ed4 100644 --- a/include/bgfx/c99/bgfxplatform.h +++ b/include/bgfx/c99/bgfxplatform.h @@ -41,6 +41,14 @@ typedef struct bgfx_platform_data } bgfx_platform_data_t; -BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd); +BGFX_C_API void bgfx_set_platform_data(const bgfx_platform_data_t* _data); + +typedef struct bgfx_internal_data +{ + void* context; + +} bgfx_internal_data_t; + +BGFX_C_API const bgfx_internal_data_t* bgfx_get_internal_data(); #endif // BGFX_PLATFORM_C99_H_HEADER_GUARD diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 2448b78df..abd00ee26 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -277,6 +277,7 @@ namespace bgfx static Context* s_ctx = NULL; static bool s_renderFrameCalled = false; + InternalData g_internalData; PlatformData g_platformData; void AllocatorStub::checkLeaks() @@ -292,19 +293,24 @@ namespace bgfx #endif // BGFX_CONFIG_MEMORY_TRACKING } - void setPlatformData(const PlatformData& _pd) + void setPlatformData(const PlatformData& _data) { if (NULL != s_ctx) { BGFX_FATAL(true - && g_platformData.ndt == _pd.ndt - && g_platformData.nwh == _pd.nwh - && g_platformData.context == _pd.context + && g_platformData.ndt == _data.ndt + && g_platformData.nwh == _data.nwh + && g_platformData.context == _data.context , Fatal::UnableToInitialize , "Only backbuffer pointer can be changed after initialization!" ); } - memcpy(&g_platformData, &_pd, sizeof(PlatformData) ); + memcpy(&g_platformData, &_data, sizeof(PlatformData) ); + } + + const InternalData* getInternalData() + { + return &g_internalData; } void setGraphicsDebuggerPresent(bool _present) @@ -4364,9 +4370,14 @@ BGFX_C_API bgfx_render_frame_t bgfx_render_frame() return bgfx_render_frame_t(bgfx::renderFrame() ); } -BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd) +BGFX_C_API void bgfx_set_platform_data(const bgfx_platform_data_t* _data) { - bgfx::setPlatformData(*(bgfx::PlatformData*)_pd); + bgfx::setPlatformData(*(const bgfx::PlatformData*)_data); +} + +BGFX_C_API const bgfx_internal_data_t* bgfx_get_internal_data() +{ + return (const bgfx_internal_data_t*)bgfx::getInternalData(); } BGFX_C_API bgfx_interface_vtbl_t* bgfx_get_interface(uint32_t _version) @@ -4376,6 +4387,7 @@ BGFX_C_API bgfx_interface_vtbl_t* bgfx_get_interface(uint32_t _version) #define BGFX_IMPORT \ BGFX_IMPORT_FUNC(render_frame) \ BGFX_IMPORT_FUNC(set_platform_data) \ + BGFX_IMPORT_FUNC(get_internal_data) \ BGFX_IMPORT_FUNC(vertex_decl_begin) \ BGFX_IMPORT_FUNC(vertex_decl_add) \ BGFX_IMPORT_FUNC(vertex_decl_skip) \ diff --git a/src/bgfx_p.h b/src/bgfx_p.h index b68e911ff..da57776f3 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -256,6 +256,7 @@ namespace stl namespace bgfx { + extern InternalData g_internalData; extern PlatformData g_platformData; #if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10) diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index d29a34227..960500ac1 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -314,6 +314,8 @@ EGL_IMPORT } import(); + + g_internalData.context = m_context; } void GlContext::destroy() diff --git a/src/glcontext_glx.cpp b/src/glcontext_glx.cpp index 4e7e38f3b..20de9d714 100644 --- a/src/glcontext_glx.cpp +++ b/src/glcontext_glx.cpp @@ -211,6 +211,8 @@ namespace bgfx { namespace gl glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glXSwapBuffers( (::Display*)g_platformData.ndt, (::Window)g_platformData.nwh); + + g_internalData.context = m_context; } void GlContext::destroy() diff --git a/src/glcontext_ppapi.cpp b/src/glcontext_ppapi.cpp index 977cd6c96..a4290cc30 100644 --- a/src/glcontext_ppapi.cpp +++ b/src/glcontext_ppapi.cpp @@ -191,6 +191,8 @@ namespace bgfx { namespace gl { BX_UNUSED(_width, _height); BX_TRACE("GlContext::create"); + + g_internalData.context = m_context; } void GlContext::destroy() diff --git a/src/glcontext_wgl.cpp b/src/glcontext_wgl.cpp index 3c52702a6..eafee4f94 100644 --- a/src/glcontext_wgl.cpp +++ b/src/glcontext_wgl.cpp @@ -268,6 +268,8 @@ namespace bgfx { namespace gl } import(); + + g_internalData.context = m_context; } void GlContext::destroy() diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 4296e1e96..6387f10c1 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -1465,6 +1465,8 @@ BX_PRAGMA_DIAGNOSTIC_POP(); } BGFX_GPU_PROFILER_BIND(m_device, m_deviceCtx); + + g_internalData.context = m_device; return true; error: diff --git a/src/renderer_d3d12.cpp b/src/renderer_d3d12.cpp index b9a5aa911..7149b2bbe 100644 --- a/src/renderer_d3d12.cpp +++ b/src/renderer_d3d12.cpp @@ -1042,6 +1042,8 @@ namespace bgfx { namespace d3d12 m_gpuTimer.init(); m_occlusionQuery.init(); } + + g_internalData.context = m_device; return true; error: diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index 9ae51d598..71ac00261 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -752,6 +752,7 @@ namespace bgfx { namespace d3d9 m_initialized = true; + g_internalData.context = m_device; return true; error: