Added ability to expose internal data (D3D device/GL context).

This commit is contained in:
Branimir Karadžić 2016-01-13 21:18:59 -08:00
parent 066c4fb3e3
commit 72be9be87d
13 changed files with 62 additions and 15 deletions

View File

@ -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.

View File

@ -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

View File

@ -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);

View File

@ -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

View File

@ -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) \

View File

@ -256,6 +256,7 @@ namespace stl
namespace bgfx
{
extern InternalData g_internalData;
extern PlatformData g_platformData;
#if BGFX_CONFIG_MAX_DRAW_CALLS < (64<<10)

View File

@ -314,6 +314,8 @@ EGL_IMPORT
}
import();
g_internalData.context = m_context;
}
void GlContext::destroy()

View File

@ -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()

View File

@ -191,6 +191,8 @@ namespace bgfx { namespace gl
{
BX_UNUSED(_width, _height);
BX_TRACE("GlContext::create");
g_internalData.context = m_context;
}
void GlContext::destroy()

View File

@ -268,6 +268,8 @@ namespace bgfx { namespace gl
}
import();
g_internalData.context = m_context;
}
void GlContext::destroy()

View File

@ -1465,6 +1465,8 @@ BX_PRAGMA_DIAGNOSTIC_POP();
}
BGFX_GPU_PROFILER_BIND(m_device, m_deviceCtx);
g_internalData.context = m_device;
return true;
error:

View File

@ -1042,6 +1042,8 @@ namespace bgfx { namespace d3d12
m_gpuTimer.init();
m_occlusionQuery.init();
}
g_internalData.context = m_device;
return true;
error:

View File

@ -752,6 +752,7 @@ namespace bgfx { namespace d3d9
m_initialized = true;
g_internalData.context = m_device;
return true;
error: