diff --git a/include/bgfx.h b/include/bgfx.h index 3ac04888..f2fc0bcc 100644 --- a/include/bgfx.h +++ b/include/bgfx.h @@ -1098,6 +1098,15 @@ namespace bgfx /// view will use these matrices. void setViewTransform(uint8_t _id, const void* _view, const void* _projL, uint8_t _flags = BGFX_VIEW_STEREO, const void* _projR = NULL); + /// Post submit view reordering. + /// + /// @param _id First view id. + /// @param _num Number of views to remap. + /// @param _remap View remap id table. Passing `NULL` will reset view ids + /// to default state. + /// + void setViewRemap(uint8_t _id = 0, uint8_t _num = UINT8_MAX, const void* _remap = NULL); + /// Sets debug marker. void setMarker(const char* _marker); diff --git a/src/bgfx.cpp b/src/bgfx.cpp index c550500a..8ecb753d 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -797,6 +797,10 @@ namespace bgfx void Frame::sort() { + for (uint32_t ii = 0, num = m_num; ii < num; ++ii) + { + m_sortKeys[ii] = SortKey::remapView(m_sortKeys[ii], m_viewRemap); + } bx::radixSort64(m_sortKeys, s_ctx->m_tempKeys, m_sortValues, s_ctx->m_tempValues, m_num); } @@ -951,6 +955,11 @@ namespace bgfx BX_TRACE("Multithreaded renderer is disabled."); #endif // BGFX_CONFIG_MULTITHREADED + for (uint32_t ii = 0; ii < BX_COUNTOF(m_viewRemap); ++ii) + { + m_viewRemap[ii] = ii; + } + memset(m_fb, 0xff, sizeof(m_fb) ); memset(m_clear, 0, sizeof(m_clear) ); memset(m_rect, 0, sizeof(m_rect) ); @@ -1156,6 +1165,8 @@ namespace bgfx freeDynamicBuffers(); m_submit->m_resolution = m_resolution; m_submit->m_debug = m_debug; + + memcpy(m_submit->m_viewRemap, m_viewRemap, sizeof(m_viewRemap) ); memcpy(m_submit->m_fb, m_fb, sizeof(m_fb) ); memcpy(m_submit->m_clear, m_clear, sizeof(m_clear) ); memcpy(m_submit->m_rect, m_rect, sizeof(m_rect) ); @@ -2673,6 +2684,13 @@ again: s_ctx->setViewTransform(_id, _view, _projL, _flags, _projR); } + void setViewRemap(uint8_t _id, uint8_t _num, const void* _remap) + { + BGFX_CHECK_MAIN_THREAD(); + BX_CHECK(_id < BGFX_CONFIG_MAX_VIEWS, "Invalid view id: %d", _id); + s_ctx->setViewRemap(_id, _num, _remap); + } + void setMarker(const char* _marker) { BGFX_CHECK_MAIN_THREAD(); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index feced94a..cfc32cf8 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -679,8 +679,10 @@ namespace bgfx }; #define SORT_KEY_RENDER_DRAW (UINT64_C(1)<<0x2b) +#define SORT_KEY_VIEW_SHIFT UINT8_C(0x37) +#define SORT_KEY_VIEW_MASK ( (uint64_t(BGFX_CONFIG_MAX_VIEWS-1) )<>0x2c)& 0x7ff; - m_view = (_key>>0x37)&(BGFX_CONFIG_MAX_VIEWS-1); + m_view = uint8_t( (_key&SORT_KEY_VIEW_MASK)>>SORT_KEY_VIEW_SHIFT); if (_key & SORT_KEY_RENDER_DRAW) { m_depth = _key & 0xffffffff; @@ -738,6 +740,21 @@ namespace bgfx return true; // compute } + bool decode(uint64_t _key, uint8_t _viewRemap[BGFX_CONFIG_MAX_VIEWS]) + { + bool compute = decode(_key); + m_view = _viewRemap[m_view]; + return compute; + } + + static uint64_t remapView(uint64_t _key, uint8_t _viewRemap[BGFX_CONFIG_MAX_VIEWS]) + { + const uint8_t oldView = uint8_t( (_key & SORT_KEY_VIEW_MASK) >> SORT_KEY_VIEW_SHIFT); + const uint64_t view = uint64_t(_viewRemap[oldView]) << SORT_KEY_VIEW_SHIFT; + const uint64_t key = (_key & ~SORT_KEY_VIEW_MASK) | view; + return key; + } + void reset() { m_depth = 0; @@ -1564,6 +1581,7 @@ namespace bgfx SortKey m_key; + uint8_t m_viewRemap[BGFX_CONFIG_MAX_VIEWS]; FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS]; Clear m_clear[BGFX_CONFIG_MAX_VIEWS]; float m_clearColor[BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE][4]; @@ -3038,6 +3056,23 @@ namespace bgfx } } + BGFX_API_FUNC(void setViewRemap(uint8_t _id, uint8_t _num, const void* _remap) ) + { + const uint32_t num = bx::uint32_min( (BGFX_CONFIG_MAX_VIEWS - _id) + _num, BGFX_CONFIG_MAX_VIEWS) - _id; + if (NULL == _remap) + { + for (uint32_t ii = 0; ii < num; ++ii) + { + uint8_t id = uint8_t(ii+_id); + m_viewRemap[id] = id; + } + } + else + { + memcpy(&m_viewRemap[_id], _remap, num); + } + } + BGFX_API_FUNC(void setMarker(const char* _marker) ) { m_submit->setMarker(_marker); @@ -3358,6 +3393,7 @@ namespace bgfx FrameBufferRef m_frameBufferRef[BGFX_CONFIG_MAX_FRAME_BUFFERS]; VertexDeclRef m_declRef; + uint8_t m_viewRemap[BGFX_CONFIG_MAX_VIEWS]; FrameBufferHandle m_fb[BGFX_CONFIG_MAX_VIEWS]; Clear m_clear[BGFX_CONFIG_MAX_VIEWS]; diff --git a/src/config.h b/src/config.h index 1a069b4e..396e6506 100644 --- a/src/config.h +++ b/src/config.h @@ -168,7 +168,7 @@ #ifndef BGFX_CONFIG_MAX_VIEWS // Do not change. Must be power of 2. -# define BGFX_CONFIG_MAX_VIEWS 32 +# define BGFX_CONFIG_MAX_VIEWS 256 #endif // BGFX_CONFIG_MAX_VIEWS #define BGFX_CONFIG_MAX_VIEW_NAME_RESERVED 5 diff --git a/src/renderer_d3d11.cpp b/src/renderer_d3d11.cpp index 1cd7735e..e493932f 100644 --- a/src/renderer_d3d11.cpp +++ b/src/renderer_d3d11.cpp @@ -726,7 +726,7 @@ namespace bgfx } // Init reserved part of view name. - for (uint8_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) + for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) { char name[BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1]; bx::snprintf(name, sizeof(name), "%3d ", ii); @@ -3051,7 +3051,7 @@ namespace bgfx int32_t numItems = _render->m_num; for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;) { - const bool isCompute = key.decode(_render->m_sortKeys[item]); + const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap); const bool viewChanged = 0 || key.m_view != view || item == numItems diff --git a/src/renderer_d3d9.cpp b/src/renderer_d3d9.cpp index a9043284..63470406 100644 --- a/src/renderer_d3d9.cpp +++ b/src/renderer_d3d9.cpp @@ -570,7 +570,7 @@ namespace bgfx } // Init reserved part of view name. - for (uint8_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) + for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii) { char name[BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1]; bx::snprintf(name, sizeof(name), "%3d ", ii); @@ -2864,7 +2864,7 @@ namespace bgfx { for (uint32_t item = 0, numItems = _render->m_num; item < numItems; ++item) { - const bool isCompute = key.decode(_render->m_sortKeys[item]); + const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap); if (isCompute) { diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 1ad0d691..9d9f1131 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -4292,7 +4292,7 @@ namespace bgfx int32_t numItems = _render->m_num; for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;) { - const bool isCompute = key.decode(_render->m_sortKeys[item]); + const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap); const bool viewChanged = 0 || key.m_view != view || item == numItems