From fa1897bd1f76e2bb9373400992679e7e3312430f 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: Fri, 23 Jun 2023 19:16:32 -0700 Subject: [PATCH] Added headless validation for bgfx::init and bgfx::reset. --- src/bgfx.cpp | 38 ++++++++++++++++++++++++-------------- src/bgfx_p.h | 9 +++++++++ src/glcontext_egl.cpp | 4 ++-- src/renderer_gl.cpp | 28 ++++++++++++---------------- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 0a7972a1b..3443fb045 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -1889,27 +1889,37 @@ namespace bgfx bool Context::init(const Init& _init) { - BX_ASSERT(!m_rendererInitialized, "Already initialized?"); + if (m_rendererInitialized) + { + BX_TRACE("Already initialized!"); + return false; + } + + m_headless = true + && RendererType::Noop != _init.type + && NULL == _init.platformData.ndt + && NULL == _init.platformData.nwh + && NULL == _init.platformData.context + && NULL == _init.platformData.backBuffer + && NULL == _init.platformData.backBufferDS + ; + BX_WARN(!m_headless, "bgfx platform data like window handle or backbuffer is not set, creating headless device."); + + if (m_headless + && 0 != _init.resolution.width + && 0 != _init.resolution.height) + { + BX_TRACE("Initializing headless mode, resolution of non-existing backbuffer can't be larger than 0x0!"); + return false; + } m_init = _init; m_init.resolution.reset &= ~BGFX_RESET_INTERNAL_FORCE; m_init.resolution.numBackBuffers = bx::clamp(_init.resolution.numBackBuffers, 2, BGFX_CONFIG_MAX_BACK_BUFFERS); - m_init.resolution.maxFrameLatency = bx::min(_init.resolution.maxFrameLatency, BGFX_CONFIG_MAX_FRAME_LATENCY); + m_init.resolution.maxFrameLatency = bx::min(_init.resolution.maxFrameLatency, BGFX_CONFIG_MAX_FRAME_LATENCY); m_init.resolution.debugTextScale = bx::clamp(_init.resolution.debugTextScale, 1, BGFX_CONFIG_DEBUG_TEXT_MAX_SCALE); dump(m_init.resolution); - if (true - && RendererType::Noop != m_init.type - && NULL == m_init.platformData.ndt - && NULL == m_init.platformData.nwh - && NULL == m_init.platformData.context - && NULL == m_init.platformData.backBuffer - && NULL == m_init.platformData.backBufferDS - ) - { - BX_TRACE("bgfx platform data like window handle or backbuffer is not set, creating headless device."); - } - bx::memCopy(&g_platformData, &m_init.platformData, sizeof(PlatformData) ); m_exit = false; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 509312841..a6ae7ed09 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -3130,6 +3130,7 @@ namespace bgfx , m_rtMemoryUsed(0) , m_textureMemoryUsed(0) , m_renderCtx(NULL) + , m_headless(false) , m_rendererInitialized(false) , m_exit(false) , m_flipAfterRender(false) @@ -3168,6 +3169,13 @@ namespace bgfx { BGFX_MUTEX_SCOPE(m_resourceApiLock); + BX_ASSERT(false + || !m_headless + || 0 == _width + || 0 == _height + , "Running in headless mode, resolution of non-existing backbuffer can't be larger than 0x0!" + ); + const TextureFormat::Enum format = TextureFormat::Count != _format ? _format : m_init.resolution.format; if (!g_platformDataChangedSinceReset @@ -5363,6 +5371,7 @@ namespace bgfx RendererContextI* m_renderCtx; + bool m_headless; bool m_rendererInitialized; bool m_exit; bool m_flipAfterRender; diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 8e109aa78..b8f24873e 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -300,8 +300,8 @@ EGL_IMPORT { EGLint pbAttribs[] = { - EGL_WIDTH, EGLint(_width), - EGL_HEIGHT, EGLint(_height), + EGL_WIDTH, EGLint(1), + EGL_HEIGHT, EGLint(1), EGL_NONE }; diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index 020dceba6..c8ad44c33 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -4138,29 +4138,25 @@ namespace bgfx { namespace gl void setRenderContextSize(uint32_t _width, uint32_t _height, uint32_t _flags = 0) { - if (_width != 0 - || _height != 0) + if (!m_glctx.isValid() ) { - if (!m_glctx.isValid() ) - { - m_glctx.create(_width, _height, _flags); + m_glctx.create(_width, _height, _flags); #if BX_PLATFORM_IOS - // iOS: need to figure out how to deal with FBO created by context. - m_backBufferFbo = m_msaaBackBufferFbo = m_glctx.getFbo(); + // iOS: need to figure out how to deal with FBO created by context. + m_backBufferFbo = m_msaaBackBufferFbo = m_glctx.getFbo(); #endif // BX_PLATFORM_IOS - } - else - { - destroyMsaaFbo(); + } + else + { + destroyMsaaFbo(); - m_glctx.resize(_width, _height, _flags); + m_glctx.resize(_width, _height, _flags); - uint32_t msaa = (_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT; - msaa = bx::uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<>BGFX_RESET_MSAA_SHIFT; + msaa = bx::uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<