feat(tests): improve bgfx initialization order tests with enhanced logging and refactored initialization logic

This commit is contained in:
2026-01-08 15:04:50 +00:00
parent e85d41995a
commit f0f42aa74f
3 changed files with 61 additions and 37 deletions

View File

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

View File

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

View File

@@ -3,6 +3,7 @@
#include <vector>
#include <memory>
#include <cstring>
#include <iostream>
// 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