From f0f42aa74f05d5927af3160f206ca2c4cffcf56d Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Thu, 8 Jan 2026 15:04:50 +0000 Subject: [PATCH] feat(tests): improve bgfx initialization order tests with enhanced logging and refactored initialization logic --- src/services/impl/bgfx_graphics_backend.cpp | 36 +++++++++-- src/services/impl/bgfx_graphics_backend.hpp | 1 + ..._initialization_order_integration_test.cpp | 61 +++++++++---------- 3 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/services/impl/bgfx_graphics_backend.cpp b/src/services/impl/bgfx_graphics_backend.cpp index 0a136a1..3c482fe 100644 --- a/src/services/impl/bgfx_graphics_backend.cpp +++ b/src/services/impl/bgfx_graphics_backend.cpp @@ -308,7 +308,7 @@ BgfxGraphicsBackend::~BgfxGraphicsBackend() { if (logger_) { logger_->Trace("BgfxGraphicsBackend", "~BgfxGraphicsBackend"); } - if (initialized_) { + if (initialized_ || bgfxInitialized_) { Shutdown(); } } @@ -454,6 +454,12 @@ void BgfxGraphicsBackend::Initialize(void* window, const GraphicsConfig& config) if (initialized_) { return; } + if (bgfxInitialized_) { + if (logger_) { + logger_->Warn("BgfxGraphicsBackend::Initialize: bgfx already initialized; resetting before reinit"); + } + Shutdown(); + } frameCount_ = 0; (void)config; @@ -547,7 +553,7 @@ void BgfxGraphicsBackend::Initialize(void* window, const GraphicsConfig& config) "candidateRenderers=" + JoinRendererNames(candidates)); } - bool initialized = false; + bool bgfxInitSucceeded = false; bool requestedFailed = false; const bool requestedExplicit = requestedRenderer != bgfx::RendererType::Count; for (bgfx::RendererType::Enum renderer : candidates) { @@ -557,7 +563,12 @@ void BgfxGraphicsBackend::Initialize(void* window, const GraphicsConfig& config) "attemptingRenderer=" + RendererTypeName(renderer)); } if (bgfx::init(init)) { - initialized = true; + bgfxInitSucceeded = true; + bgfxInitialized_ = true; + if (logger_) { + logger_->Trace("BgfxGraphicsBackend", "Initialize", + "bgfxInitSucceeded renderer=" + RendererTypeName(renderer)); + } break; } if (logger_) { @@ -570,7 +581,7 @@ void BgfxGraphicsBackend::Initialize(void* window, const GraphicsConfig& config) } } - if (!initialized) { + if (!bgfxInitSucceeded) { if (platformService_ && !loggedInitFailureDiagnostics_) { platformService_->LogSystemInfo(); loggedInitFailureDiagnostics_ = true; @@ -606,13 +617,24 @@ void BgfxGraphicsBackend::Initialize(void* window, const GraphicsConfig& config) InitializeUniforms(); initialized_ = true; + if (logger_) { + logger_->Trace("BgfxGraphicsBackend", "Initialize", "backendInitialized=true"); + } } void BgfxGraphicsBackend::Shutdown() { if (logger_) { logger_->Trace("BgfxGraphicsBackend", "Shutdown"); } - if (!initialized_) { + if (!bgfxInitialized_) { + if (logger_) { + logger_->Trace("BgfxGraphicsBackend", "Shutdown", "bgfxInitialized=false"); + if (initialized_) { + logger_->Warn("BgfxGraphicsBackend::Shutdown: backend marked initialized without bgfx init"); + } + } + initialized_ = false; + frameCount_ = 0; return; } @@ -620,6 +642,10 @@ void BgfxGraphicsBackend::Shutdown() { DestroyBuffers(); DestroyUniforms(); bgfx::shutdown(); + if (logger_) { + logger_->Trace("BgfxGraphicsBackend", "Shutdown", "bgfxShutdown=true"); + } + bgfxInitialized_ = false; initialized_ = false; frameCount_ = 0; } diff --git a/src/services/impl/bgfx_graphics_backend.hpp b/src/services/impl/bgfx_graphics_backend.hpp index 9c6ec4f..ecf0b10 100644 --- a/src/services/impl/bgfx_graphics_backend.hpp +++ b/src/services/impl/bgfx_graphics_backend.hpp @@ -160,6 +160,7 @@ private: MaterialXUniforms materialXUniforms_{}; uint32_t viewportWidth_ = 0; uint32_t viewportHeight_ = 0; + bool bgfxInitialized_ = false; bool initialized_ = false; uint32_t frameCount_ = 0; bgfx::ViewId viewId_ = 0; diff --git a/tests/bgfx_initialization_order_integration_test.cpp b/tests/bgfx_initialization_order_integration_test.cpp index 6c978e1..2d3c5f8 100644 --- a/tests/bgfx_initialization_order_integration_test.cpp +++ b/tests/bgfx_initialization_order_integration_test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // Integration test for texture creation BEFORE first bgfx::frame() // Based on INITIALIZATION_ORDER_BUG.md @@ -42,20 +43,36 @@ const char* RendererTypeName(bgfx::RendererType::Enum type) { class BgfxInitializationOrderTest : public ::testing::Test { protected: + bool bgfxInitialized_ = false; + + bool InitializeNoopBgfx() { + bgfx::Init init; + init.type = bgfx::RendererType::Noop; + init.resolution.width = 1024; + init.resolution.height = 768; + init.resolution.reset = BGFX_RESET_NONE; + + bgfxInitialized_ = bgfx::init(init); + std::cout << "[TRACE] BgfxInitializationOrderTest::InitializeNoopBgfx initialized=" + << (bgfxInitialized_ ? "true" : "false") << "\n"; + + return bgfxInitialized_; + } + void TearDown() override { - // bgfx shutdown is safe to call even if not initialized - bgfx::shutdown(); + if (bgfxInitialized_) { + std::cout << "[TRACE] BgfxInitializationOrderTest::TearDown shutting down bgfx\n"; + bgfx::shutdown(); + bgfxInitialized_ = false; + } else { + std::cout << "[TRACE] BgfxInitializationOrderTest::TearDown skipped bgfx shutdown (not initialized)\n"; + } } }; TEST_F(BgfxInitializationOrderTest, CreateTexture_BeforeFrame_ProducesInvalidHandle) { // Initialize bgfx - bgfx::Init init; - init.type = bgfx::RendererType::Noop; - init.resolution.width = 1024; - init.resolution.height = 768; - init.resolution.reset = BGFX_RESET_NONE; - if (!bgfx::init(init)) { + if (!InitializeNoopBgfx()) { GTEST_SKIP() << "bgfx::init failed"; } const auto rendererType = bgfx::getRendererType(); @@ -93,12 +110,7 @@ TEST_F(BgfxInitializationOrderTest, CreateTexture_BeforeFrame_ProducesInvalidHan TEST_F(BgfxInitializationOrderTest, CreateTexture_AfterFrame_ProducesValidHandle) { // Initialize bgfx - bgfx::Init init; - init.type = bgfx::RendererType::Noop; - init.resolution.width = 1024; - init.resolution.height = 768; - init.resolution.reset = BGFX_RESET_NONE; - ASSERT_TRUE(bgfx::init(init)); + ASSERT_TRUE(InitializeNoopBgfx()); // ✅ CORRECT: Call frame FIRST to prime bgfx bgfx::frame(); @@ -127,12 +139,7 @@ TEST_F(BgfxInitializationOrderTest, CreateTexture_AfterFrame_ProducesValidHandle TEST_F(BgfxInitializationOrderTest, MultipleTextures_AfterFrame_AllValid) { // Initialize and prime bgfx - bgfx::Init init; - init.type = bgfx::RendererType::Noop; - init.resolution.width = 1024; - init.resolution.height = 768; - init.resolution.reset = BGFX_RESET_NONE; - ASSERT_TRUE(bgfx::init(init)); + ASSERT_TRUE(InitializeNoopBgfx()); bgfx::frame(); // Create multiple textures @@ -165,12 +172,7 @@ TEST_F(BgfxInitializationOrderTest, CorrectInitSequence_Documented) { // Documents the correct initialization sequence from INITIALIZATION_ORDER_BUG.md // Step 1: Initialize bgfx - bgfx::Init init; - init.type = bgfx::RendererType::Noop; - init.resolution.width = 1024; - init.resolution.height = 768; - init.resolution.reset = BGFX_RESET_NONE; - ASSERT_TRUE(bgfx::init(init)); + ASSERT_TRUE(InitializeNoopBgfx()); // Step 2: Call frame() FIRST - this primes bgfx bgfx::frame(); @@ -206,12 +208,7 @@ TEST_F(BgfxInitializationOrderTest, ErrorSymptom_InvalidHandleValue) { TEST_F(BgfxInitializationOrderTest, BufferCreation_AlsoRequiresFrame) { // Buffers (vertex, index) also need frame() to be called first - bgfx::Init init; - init.type = bgfx::RendererType::Noop; - init.resolution.width = 1024; - init.resolution.height = 768; - init.resolution.reset = BGFX_RESET_NONE; - ASSERT_TRUE(bgfx::init(init)); + ASSERT_TRUE(InitializeNoopBgfx()); bgfx::frame(); // Prime first // Create vertex buffer - simplified test without layout issues