From 0df2e90edbaa03b5ff5f5535e274fcbdf6118153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Thu, 1 Apr 2021 20:09:16 -0700 Subject: [PATCH] Cleanup. --- examples/05-instancing/instancing.cpp | 57 ++++++++++++++++----------- examples/common/imgui/imgui.h | 14 +++++++ src/bgfx.cpp | 22 ++++++----- src/bgfx_p.h | 55 +++++++++++++------------- 4 files changed, 88 insertions(+), 60 deletions(-) diff --git a/examples/05-instancing/instancing.cpp b/examples/05-instancing/instancing.cpp index dbb05188d..ae80d11b3 100644 --- a/examples/05-instancing/instancing.cpp +++ b/examples/05-instancing/instancing.cpp @@ -75,9 +75,9 @@ public: m_height = _height; m_debug = BGFX_DEBUG_TEXT; m_reset = BGFX_RESET_VSYNC; - m_use_instancing = true; - m_last_frame_missing = 0; - m_side_size = 11; + m_useInstancing = true; + m_lastFrameMissing = 0; + m_sideSize = 11; bgfx::Init init; init.type = args.m_type; @@ -166,14 +166,27 @@ public: , 0 ); + // Get renderer capabilities info. + const bgfx::Caps* caps = bgfx::getCaps(); + + // Check if instancing is supported. + const bool instancingSupported = 0 != (BGFX_CAPS_INSTANCING & caps->supported); + m_useInstancing &= instancingSupported; + ImGui::Text("%d draw calls", bgfx::getStats()->numDraw); - ImGui::Checkbox("Use Instancing", &m_use_instancing); + + ImGui::PushEnabled(instancingSupported); + ImGui::Checkbox("Use Instancing", &m_useInstancing); + ImGui::PopEnabled(); + ImGui::Text("Grid Side Size:"); - ImGui::SliderInt("", (int*)&m_side_size, 1, 512); - if (m_last_frame_missing > 0) + ImGui::SliderInt("", (int*)&m_sideSize, 1, 512); + + if (m_lastFrameMissing > 0) { - ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Couldn't draw %d cubes last frame", m_last_frame_missing); + ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Couldn't draw %d cubes last frame", m_lastFrameMissing); } + if (bgfx::getStats()->numDraw >= bgfx::getCaps()->limits.maxDrawCalls) { ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Draw call limit reached!"); @@ -192,18 +205,14 @@ public: float time = (float)( (bx::getHPCounter() - m_timeOffset)/double(bx::getHPFrequency() ) ); - // Get renderer capabilities info. - const bgfx::Caps* caps = bgfx::getCaps(); - - // Check if instancing is supported. - if (0 == (BGFX_CAPS_INSTANCING & caps->supported) ) + if (!instancingSupported) { // When instancing is not supported by GPU, implement alternative // code path that doesn't use instancing. bool blink = uint32_t(time*3.0f)&1; bgfx::dbgTextPrintf(0, 0, blink ? 0x4f : 0x04, " Instancing is not supported by GPU. "); - m_use_instancing = false; + m_useInstancing = false; } const bx::Vec3 at = { 0.0f, 0.0f, 0.0f }; @@ -222,20 +231,20 @@ public: bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) ); } - m_last_frame_missing = 0; + m_lastFrameMissing = 0; - if (m_use_instancing) + if (m_useInstancing) { // 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color. const uint16_t instanceStride = 80; // to total number of instances to draw - uint32_t totalCubes = m_side_size * m_side_size; + uint32_t totalCubes = m_sideSize * m_sideSize; // figure out how big of a buffer is available uint32_t drawnCubes = bgfx::getAvailInstanceDataBuffer(totalCubes, instanceStride); // save how many we couldn't draw due to buffer room so we can display it - m_last_frame_missing = totalCubes - drawnCubes; + m_lastFrameMissing = totalCubes - drawnCubes; bgfx::InstanceDataBuffer idb; bgfx::allocInstanceDataBuffer(&idb, drawnCubes, instanceStride); @@ -244,8 +253,8 @@ public: for (uint32_t ii = 0; ii < drawnCubes; ++ii) { - uint32_t yy = ii / m_side_size; - uint32_t xx = ii % m_side_size; + uint32_t yy = ii / m_sideSize; + uint32_t xx = ii % m_sideSize; float* mtx = (float*)data; bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f); @@ -278,9 +287,9 @@ public: else { // non-instanced path - for (uint32_t yy = 0; yy < m_side_size; ++yy) + for (uint32_t yy = 0; yy < m_sideSize; ++yy) { - for (uint32_t xx = 0; xx < m_side_size; ++xx) + for (uint32_t xx = 0; xx < m_sideSize; ++xx) { float mtx[16]; bx::mtxRotateXY(mtx, time + xx * 0.21f, time + yy * 0.37f); @@ -320,9 +329,9 @@ public: uint32_t m_height; uint32_t m_debug; uint32_t m_reset; - bool m_use_instancing; - uint32_t m_last_frame_missing; - uint32_t m_side_size; + bool m_useInstancing; + uint32_t m_lastFrameMissing; + uint32_t m_sideSize; bgfx::VertexBufferHandle m_vbh; bgfx::IndexBufferHandle m_ibh; diff --git a/examples/common/imgui/imgui.h b/examples/common/imgui/imgui.h index ea18b3222..0106e5cc0 100644 --- a/examples/common/imgui/imgui.h +++ b/examples/common/imgui/imgui.h @@ -119,6 +119,20 @@ namespace ImGui ; } + inline void PushEnabled(bool _enabled) + { + extern void PushItemFlag(int option, bool enabled); + PushItemFlag(1<<2 /*ImGuiItemFlags_Disabled*/, !_enabled); + PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * (_enabled ? 1.0f : 0.5f) ); + } + + inline void PopEnabled() + { + extern void PopItemFlag(); + PopItemFlag(); + PopStyleVar(); + } + } // namespace ImGui #endif // IMGUI_H_HEADER_GUARD diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 1217eec94..073cb1dcb 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1406,31 +1406,33 @@ namespace bgfx viewRemap[m_viewRemap[ii] ] = ViewId(ii); View& view = s_ctx->m_view[ii]; - Rect fbRect(0, 0, uint16_t(m_resolution.width), uint16_t(m_resolution.height) ); + Rect rect(0, 0, uint16_t(m_resolution.width), uint16_t(m_resolution.height) ); + if (isValid(view.m_fbh) ) { - const FrameBufferRef& fb = s_ctx->m_frameBufferRef[view.m_fbh.idx]; - const BackbufferRatio::Enum bbRatio = fb.m_window + const FrameBufferRef& fbr = s_ctx->m_frameBufferRef[view.m_fbh.idx]; + const BackbufferRatio::Enum bbRatio = fbr.m_window ? BackbufferRatio::Count - : BackbufferRatio::Enum(s_ctx->m_textureRef[fb.un.m_th[0].idx].m_bbRatio) + : BackbufferRatio::Enum(s_ctx->m_textureRef[fbr.un.m_th[0].idx].m_bbRatio) ; + if (BackbufferRatio::Count != bbRatio) { - getTextureSizeFromRatio(bbRatio, fbRect.m_width, fbRect.m_height); + getTextureSizeFromRatio(bbRatio, rect.m_width, rect.m_height); } else { - fbRect.m_width = fb.m_width; - fbRect.m_height = fb.m_height; + rect.m_width = fbr.m_width; + rect.m_height = fbr.m_height; } } - view.m_rect.intersect(fbRect); + view.m_rect.intersect(rect); BX_ASSERT(!view.m_rect.isZeroArea(), "View %d: view rect outside of framebuffer extent", ii); if (!view.m_scissor.isZero() ) { - view.m_scissor.intersect(fbRect); + view.m_scissor.intersect(rect); } } @@ -1438,12 +1440,14 @@ namespace bgfx { m_sortKeys[ii] = SortKey::remapView(m_sortKeys[ii], viewRemap); } + bx::radixSort(m_sortKeys, s_ctx->m_tempKeys, m_sortValues, s_ctx->m_tempValues, m_numRenderItems); for (uint32_t ii = 0, num = m_numBlitItems; ii < num; ++ii) { m_blitKeys[ii] = BlitKey::remapView(m_blitKeys[ii], viewRemap); } + bx::radixSort(m_blitKeys, (uint32_t*)&s_ctx->m_tempKeys, m_numBlitItems); } diff --git a/src/bgfx_p.h b/src/bgfx_p.h index eab239454..2d2887164 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -4622,19 +4622,20 @@ namespace bgfx const TextureRef& firstTexture = m_textureRef[_attachment[0].handle.idx]; const BackbufferRatio::Enum bbRatio = BackbufferRatio::Enum(firstTexture.m_bbRatio); - FrameBufferRef& ref = m_frameBufferRef[handle.idx]; + FrameBufferRef& fbr = m_frameBufferRef[handle.idx]; if (BackbufferRatio::Count == bbRatio) { - ref.m_width = bx::max(firstTexture.m_width >> _attachment[0].mip, 1); - ref.m_height = bx::max(firstTexture.m_height >> _attachment[0].mip, 1); + fbr.m_width = bx::max(firstTexture.m_width >> _attachment[0].mip, 1); + fbr.m_height = bx::max(firstTexture.m_height >> _attachment[0].mip, 1); } - ref.m_window = false; - bx::memSet(ref.un.m_th, 0xff, sizeof(ref.un.m_th) ); - + + fbr.m_window = false; + bx::memSet(fbr.un.m_th, 0xff, sizeof(fbr.un.m_th) ); + for (uint32_t ii = 0; ii < _num; ++ii) { TextureHandle texHandle = _attachment[ii].handle; - ref.un.m_th[ii] = texHandle; + fbr.un.m_th[ii] = texHandle; textureIncRef(texHandle); } @@ -4670,11 +4671,11 @@ namespace bgfx cmdbuf.write(_format); cmdbuf.write(_depthFormat); - FrameBufferRef& ref = m_frameBufferRef[handle.idx]; - ref.m_width = _width; - ref.m_height = _height; - ref.m_window = true; - ref.un.m_nwh = _nwh; + FrameBufferRef& fbr = m_frameBufferRef[handle.idx]; + fbr.m_width = _width; + fbr.m_height = _height; + fbr.m_window = true; + fbr.un.m_nwh = _nwh; } return handle; @@ -4686,8 +4687,8 @@ namespace bgfx BGFX_CHECK_HANDLE("setName", m_frameBufferHandle, _handle); - FrameBufferRef& ref = m_frameBufferRef[_handle.idx]; - ref.m_name.set(_name); + FrameBufferRef& fbr = m_frameBufferRef[_handle.idx]; + fbr.m_name.set(_name); // setName(convert(_handle), _name); } @@ -4698,11 +4699,11 @@ namespace bgfx BGFX_CHECK_HANDLE("getTexture", m_frameBufferHandle, _handle); - const FrameBufferRef& ref = m_frameBufferRef[_handle.idx]; - if (!ref.m_window) + const FrameBufferRef& fbr = m_frameBufferRef[_handle.idx]; + if (!fbr.m_window) { const uint32_t attachment = bx::min(_attachment, BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS); - return ref.un.m_th[attachment]; + return fbr.un.m_th[attachment]; } return BGFX_INVALID_HANDLE; @@ -4719,14 +4720,14 @@ namespace bgfx CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::DestroyFrameBuffer); cmdbuf.write(_handle); - FrameBufferRef& ref = m_frameBufferRef[_handle.idx]; - ref.m_name.clear(); + FrameBufferRef& fbr = m_frameBufferRef[_handle.idx]; + fbr.m_name.clear(); - if (!ref.m_window) + if (!fbr.m_window) { - for (uint32_t ii = 0; ii < BX_COUNTOF(ref.un.m_th); ++ii) + for (uint32_t ii = 0; ii < BX_COUNTOF(fbr.un.m_th); ++ii) { - TextureHandle th = ref.un.m_th[ii]; + TextureHandle th = fbr.un.m_th[ii]; if (isValid(th) ) { textureDecRef(th); @@ -4899,8 +4900,8 @@ namespace bgfx if (isValid(_handle) ) { - FrameBufferRef& ref = m_frameBufferRef[_handle.idx]; - if (!ref.m_window) + const FrameBufferRef& fbr = m_frameBufferRef[_handle.idx]; + if (!fbr.m_window) { BX_TRACE("requestScreenShot can be done only for window frame buffer handles (handle: %d).", _handle.idx); return; @@ -5202,9 +5203,9 @@ namespace bgfx ProgramHashMap m_programHashMap; ProgramRef m_programRef[BGFX_CONFIG_MAX_PROGRAMS]; - TextureRef m_textureRef[BGFX_CONFIG_MAX_TEXTURES]; - FrameBufferRef m_frameBufferRef[BGFX_CONFIG_MAX_FRAME_BUFFERS]; - VertexLayoutRef m_vertexLayoutRef; + TextureRef m_textureRef[BGFX_CONFIG_MAX_TEXTURES]; + FrameBufferRef m_frameBufferRef[BGFX_CONFIG_MAX_FRAME_BUFFERS]; + VertexLayoutRef m_vertexLayoutRef; ViewId m_viewRemap[BGFX_CONFIG_MAX_VIEWS]; uint32_t m_seq[BGFX_CONFIG_MAX_VIEWS];