From 174524dde47b42ad29328759901e163086c616f7 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 13 Jan 2013 15:35:06 -0800 Subject: [PATCH] Moved x11 window creation into examples/common. --- examples/common/entry_linux.cpp | 89 ++++++++++++++++++++++- examples/common/entry_windows.cpp | 100 +++++++++++++------------- include/bgfxplatform.h | 70 ++++++++++-------- src/bgfx.cpp | 88 +++++++++++------------ src/renderer_gl.cpp | 114 +++++++++++------------------- 5 files changed, 260 insertions(+), 201 deletions(-) diff --git a/examples/common/entry_linux.cpp b/examples/common/entry_linux.cpp index 112e16f8..63e77341 100644 --- a/examples/common/entry_linux.cpp +++ b/examples/common/entry_linux.cpp @@ -7,10 +7,94 @@ #if BX_PLATFORM_LINUX +#include "bgfxplatform.h" +#include + +#include +#include + +#undef None #include "entry.h" +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 720 + +extern int _main_(int _argc, char** _argv); + namespace entry { + struct MainThreadEntry + { + int m_argc; + char** m_argv; + + static int32_t threadFunc(void* _userData) + { + MainThreadEntry* self = (MainThreadEntry*)_userData; + return _main_(self->m_argc, self->m_argv); + } + }; + + struct Context + { + int32_t run(int _argc, char** _argv) + { + m_display = XOpenDisplay(0); + + XLockDisplay(m_display); + + int32_t screen = DefaultScreen(m_display); + int32_t depth = DefaultDepth(m_display, screen); + Visual* visual = DefaultVisual(m_display, screen); + Window root = RootWindow(m_display, screen); + + XSetWindowAttributes windowAttrs; + windowAttrs.colormap = + XCreateColormap(m_display + , root + , visual + , AllocNone + ); + windowAttrs.background_pixmap = 0; + windowAttrs.border_pixel = 0; + + m_window = XCreateWindow(m_display + , root + , 0, 0 + , DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, depth + , InputOutput + , visual + , CWBorderPixel|CWColormap + , &windowAttrs + ); + + XMapRaised(m_display, m_window); + XFlush(m_display); + + XUnlockDisplay(m_display); + +// XResizeWindow(s_display, s_window, _width, _height); + + bgfx::x11SetDisplayWindow(m_display, m_window); + + MainThreadEntry mte; + mte.m_argc = _argc; + mte.m_argv = _argv; + + bx::Thread thread; + thread.init(mte.threadFunc, &mte); + + thread.shutdown(); + + return EXIT_SUCCESS; + } + + Display* m_display; + Window m_window; + }; + + static Context s_ctx; + Event::Enum poll() { return Event::Nop; @@ -18,11 +102,10 @@ namespace entry } // namespace entry -extern int _main_(int _argc, char** _argv); - int main(int _argc, char** _argv) { - return _main_(_argc, _argv); + using namespace entry; + return s_ctx.run(_argc, _argv); } #endif // BX_PLATFORM_LINUX diff --git a/examples/common/entry_windows.cpp b/examples/common/entry_windows.cpp index 1af9daf9..480975b9 100644 --- a/examples/common/entry_windows.cpp +++ b/examples/common/entry_windows.cpp @@ -1,38 +1,38 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#include - -#if BX_PLATFORM_WINDOWS - -#include -#include -#include - -#include "entry.h" -#include "dbg.h" - -#define DEFAULT_WIDTH 1280 -#define DEFAULT_HEIGHT 720 - -extern int _main_(int _argc, char** _argv); - -namespace entry -{ - struct MainThreadEntry - { - int m_argc; - char** m_argv; - - static int32_t threadFunc(void* _userData) - { - MainThreadEntry* self = (MainThreadEntry*)_userData; - return _main_(self->m_argc, self->m_argv); - } - }; - +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#include + +#if BX_PLATFORM_WINDOWS + +#include +#include +#include + +#include "entry.h" +#include "dbg.h" + +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 720 + +extern int _main_(int _argc, char** _argv); + +namespace entry +{ + struct MainThreadEntry + { + int m_argc; + char** m_argv; + + static int32_t threadFunc(void* _userData) + { + MainThreadEntry* self = (MainThreadEntry*)_userData; + return _main_(self->m_argc, self->m_argv); + } + }; + struct Context { Context() @@ -330,23 +330,23 @@ namespace entry }; static Context s_ctx; - + LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) { return s_ctx.process(_hwnd, _id, _wparam, _lparam); } - Event::Enum poll() - { - return Event::Nop; - } - -} // namespace entry - -int main(int _argc, char** _argv) -{ - using namespace entry; - return s_ctx.main(_argc, _argv); -} - -#endif // BX_PLATFORM_WINDOWS + Event::Enum poll() + { + return Event::Nop; + } + +} // namespace entry + +int main(int _argc, char** _argv) +{ + using namespace entry; + return s_ctx.main(_argc, _argv); +} + +#endif // BX_PLATFORM_WINDOWS diff --git a/include/bgfxplatform.h b/include/bgfxplatform.h index 56016a05..b00ab788 100644 --- a/include/bgfxplatform.h +++ b/include/bgfxplatform.h @@ -1,31 +1,39 @@ -/* - * Copyright 2011-2013 Branimir Karadzic. All rights reserved. - * License: http://www.opensource.org/licenses/BSD-2-Clause - */ - -#ifndef __BGFXPLATFORM_H__ -#define __BGFXPLATFORM_H__ - -#include - -#if BX_PLATFORM_NACL -# include -# include - -namespace bgfx -{ - typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); - void naclSetIntefraces(PP_Instance, const PPB_Instance*, const PPB_Graphics3D*, PostSwapBuffersFn); -} // namespace bgfx - -#elif BX_PLATFORM_WINDOWS -# include - -namespace bgfx -{ - void setHwnd(HWND _hwnd); -} // namespace bgfx - -#endif // BX_PLATFORM_ - -#endif // __BGFXPLATFORM_H__ +/* + * Copyright 2011-2013 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __BGFXPLATFORM_H__ +#define __BGFXPLATFORM_H__ + +#include + +#if BX_PLATFORM_LINUX +# include + +namespace bgfx +{ + void x11SetDisplayWindow(::Display* _display, ::Window _window); +} // namespace bgfx + +#elif BX_PLATFORM_NACL +# include +# include + +namespace bgfx +{ + typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); + void naclSetIntefraces(::PP_Instance, const ::PPB_Instance*, const ::PPB_Graphics3D*, PostSwapBuffersFn); +} // namespace bgfx + +#elif BX_PLATFORM_WINDOWS +# include + +namespace bgfx +{ + void setHwnd(::HWND _hwnd); +} // namespace bgfx + +#endif // BX_PLATFORM_ + +#endif // __BGFXPLATFORM_H__ diff --git a/src/bgfx.cpp b/src/bgfx.cpp index db27c176..db42e7fa 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -38,10 +38,10 @@ namespace bgfx #endif // BGFX_CONFIG_MULTITHREADED #if BX_PLATFORM_WINDOWS - void setHwnd(HWND _hwnd) - { - g_bgfxHwnd = _hwnd; - } + void setHwnd(::HWND _hwnd) + { + g_bgfxHwnd = _hwnd; + } #endif // BX_PLATFORM_WINDOWS struct CallbackStub : public CallbackI @@ -1035,7 +1035,7 @@ namespace bgfx return s_ctx.createTexture(mem, _flags, NULL); } - + TextureHandle createTextureCube(uint16_t _sides, uint16_t _width, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem) { BGFX_CHECK_MAIN_THREAD(); @@ -1081,52 +1081,52 @@ namespace bgfx s_ctx.destroyTexture(_handle); } - void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem) - { + void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem) + { BGFX_CHECK_MAIN_THREAD(); BX_CHECK(NULL != _mem, "_mem can't be NULL"); - if (_width == 0 - || _height == 0) - { - release(_mem); - } - else - { - s_ctx.updateTexture(_handle, 0, _mip, _x, _y, 0, _width, _height, 1, _mem); - } - } - - void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem) - { + if (_width == 0 + || _height == 0) + { + release(_mem); + } + else + { + s_ctx.updateTexture(_handle, 0, _mip, _x, _y, 0, _width, _height, 1, _mem); + } + } + + void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem) + { BGFX_CHECK_MAIN_THREAD(); BX_CHECK(NULL != _mem, "_mem can't be NULL"); - if (_width == 0 - || _height == 0 - || _depth == 0) - { - release(_mem); - } - else - { - s_ctx.updateTexture(_handle, 0, _mip, _x, _y, _z, _width, _height, _depth, _mem); - } - } - - void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem) - { + if (_width == 0 + || _height == 0 + || _depth == 0) + { + release(_mem); + } + else + { + s_ctx.updateTexture(_handle, 0, _mip, _x, _y, _z, _width, _height, _depth, _mem); + } + } + + void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem) + { BGFX_CHECK_MAIN_THREAD(); BX_CHECK(NULL != _mem, "_mem can't be NULL"); BX_CHECK(_side >= 0 && _side <= 5, "Invalid side %d.", _side); - if (_width == 0 - || _height == 0) - { - release(_mem); - } - else - { - s_ctx.updateTexture(_handle, _side, _mip, _x, _y, 0, _width, _height, 1, _mem); - } - } + if (_width == 0 + || _height == 0) + { + release(_mem); + } + else + { + s_ctx.updateTexture(_handle, _side, _mip, _x, _y, 0, _width, _height, 1, _mem); + } + } RenderTargetHandle createRenderTarget(uint16_t _width, uint16_t _height, uint32_t _flags, uint32_t _textureFlags) { diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index cbdc7b07..3b9351fd 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -12,6 +12,17 @@ namespace bgfx { +#if BX_PLATFORM_LINUX + static ::Display* s_display; + static ::Window s_window; + + void x11SetDisplayWindow(::Display* _display, ::Window _window) + { + s_display = _display; + s_window = _window; + } +#endif // BX_PLATFORM_LINUX + struct Extension { enum Enum @@ -196,8 +207,6 @@ namespace bgfx , m_surface(NULL) #elif BX_PLATFORM_LINUX , m_context(0) - , m_window(0) - , m_display(NULL) #endif // BX_PLATFORM_ { memset(&m_resolution, 0, sizeof(m_resolution) ); @@ -334,15 +343,12 @@ namespace bgfx } #elif BX_PLATFORM_LINUX - if (0 == m_display) + if (0 == m_context) { - Display* display = XOpenDisplay(0); - BGFX_FATAL(display, Fatal::UnableToInitialize, "Failed to open X display (0)."); - - XLockDisplay(display); + XLockDisplay(s_display); int major, minor; - bool version = glXQueryVersion(display, &major, &minor); + bool version = glXQueryVersion(s_display, &major, &minor); BGFX_FATAL(version, Fatal::UnableToInitialize, "Failed to query GLX version"); BGFX_FATAL( (major == 1 && minor >= 3) || major > 1 , Fatal::UnableToInitialize @@ -369,14 +375,14 @@ namespace bgfx GLXFBConfig bestConfig = NULL; int numConfigs; - GLXFBConfig* configs = glXChooseFBConfig(display, DefaultScreen(display), attrsGlx, &numConfigs); + GLXFBConfig* configs = glXChooseFBConfig(s_display, DefaultScreen(s_display), attrsGlx, &numConfigs); BX_TRACE("glX num configs %d", numConfigs); XVisualInfo* visualInfo = 0; for (int ii = 0; ii < numConfigs; ++ii) { - visualInfo = glXGetVisualFromFBConfig(display, configs[ii]); + visualInfo = glXGetVisualFromFBConfig(s_display, configs[ii]); if (NULL != visualInfo) { BX_TRACE("---"); @@ -384,7 +390,7 @@ namespace bgfx for (uint32_t attr = 6; attr < countof(attrsGlx)-1 && attrsGlx[attr] != None; attr += 2) { int value; - glXGetFBConfigAttrib(display, configs[ii], attrsGlx[attr], &value); + glXGetFBConfigAttrib(s_display, configs[ii], attrsGlx[attr], &value); BX_TRACE("glX %d/%d %2d: %4x, %8x (%8x%s)" , ii , numConfigs @@ -418,29 +424,8 @@ namespace bgfx XFree(configs); BGFX_FATAL(visualInfo, Fatal::UnableToInitialize, "Failed to find a suitable X11 display configuration."); - // Generate colormaps - XSetWindowAttributes windowAttrs; - windowAttrs.colormap = XCreateColormap(display, RootWindow(display, visualInfo->screen), visualInfo->visual, AllocNone); - windowAttrs.background_pixmap = None; - windowAttrs.border_pixel = 0; - - Window window = XCreateWindow( - display - , RootWindow(display, visualInfo->screen) - , 0, 0 - , _width, _height, 0, visualInfo->depth - , InputOutput - , visualInfo->visual - , CWBorderPixel|CWColormap - , &windowAttrs - ); - BGFX_FATAL(window, Fatal::UnableToInitialize, "Failed to create X11 window."); - - XMapRaised(display, window); - XFlush(display); - BX_TRACE("Create GL 2.1 context."); - m_context = glXCreateContext(display, visualInfo, 0, GL_TRUE); + m_context = glXCreateContext(s_display, visualInfo, 0, GL_TRUE); BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create GL 2.1 context."); XFree(visualInfo); @@ -457,16 +442,16 @@ namespace bgfx None, }; - GLXContext context = glXCreateContextAttribsARB(display, bestConfig, 0, true, contextAttrs); + GLXContext context = glXCreateContextAttribsARB(s_display, bestConfig, 0, true, contextAttrs); if (NULL != context) { - glXDestroyContext(display, m_context); + glXDestroyContext(s_display, m_context); m_context = context; } } - glXMakeCurrent(display, window, m_context); + glXMakeCurrent(s_display, s_window, m_context); # define GL_IMPORT(_optional, _proto, _func) \ { \ @@ -477,15 +462,12 @@ namespace bgfx # undef GL_IMPORT glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glXSwapBuffers(display, window); + glXSwapBuffers(s_display, s_window); - m_display = display; - m_window = window; - XUnlockDisplay(display); + XUnlockDisplay(s_display); } else { - XResizeWindow(m_display, m_window, _width, _height); } #elif BGFX_USE_EGL if (NULL == m_context) @@ -558,23 +540,6 @@ namespace bgfx #endif // BX_PLATFORM_ } -#if !BGFX_CONFIG_RENDERER_OPENGLES3 - if (NULL != glVertexAttribDivisor - && NULL != glDrawArraysInstanced - && NULL != glDrawElementsInstanced) - { - s_vertexAttribDivisor = glVertexAttribDivisor; - s_drawArraysInstanced = glDrawArraysInstanced; - s_drawElementsInstanced = glDrawElementsInstanced; - } - else - { - s_vertexAttribDivisor = stubVertexAttribDivisor; - s_drawArraysInstanced = stubDrawArraysInstanced; - s_drawElementsInstanced = stubDrawElementsInstanced; - } -#endif // !BGFX_CONFIG_RENDERER_OPENGLES3 - m_flip = true; } @@ -592,7 +557,7 @@ namespace bgfx eglMakeCurrent(m_display, m_surface, m_surface, m_context); eglSwapBuffers(m_display, m_surface); #elif BX_PLATFORM_LINUX - glXSwapBuffers(m_display, m_window); + glXSwapBuffers(s_display, s_window); #endif // BX_PLATFORM_ } @@ -760,8 +725,6 @@ namespace bgfx EGLSurface m_surface; #elif BX_PLATFORM_LINUX GLXContext m_context; - Window m_window; - Display* m_display; #endif // BX_PLATFORM_NACL }; @@ -804,18 +767,6 @@ namespace bgfx { renderFrame(); } -#elif BX_PLATFORM_LINUX - bool linuxGetDisplay(Display** _display, Window* _window) - { - if (!s_renderCtx.m_display) - { - return false; - } - - *_display = s_renderCtx.m_display; - *_window = s_renderCtx.m_window; - return true; - } #endif // BX_PLATFORM_ static const GLenum s_primType[] = @@ -2252,6 +2203,23 @@ namespace bgfx s_textureFormat[TextureFormat::BGRX8].m_fmt = GL_BGRA_EXT; s_textureFormat[TextureFormat::BGRA8].m_fmt = GL_BGRA_EXT; } + +#if !BGFX_CONFIG_RENDERER_OPENGLES3 + if (NULL != glVertexAttribDivisor + && NULL != glDrawArraysInstanced + && NULL != glDrawElementsInstanced) + { + s_vertexAttribDivisor = glVertexAttribDivisor; + s_drawArraysInstanced = glDrawArraysInstanced; + s_drawElementsInstanced = glDrawElementsInstanced; + } + else + { + s_vertexAttribDivisor = stubVertexAttribDivisor; + s_drawArraysInstanced = stubDrawArraysInstanced; + s_drawElementsInstanced = stubDrawElementsInstanced; + } +#endif // !BGFX_CONFIG_RENDERER_OPENGLES3 } }