bool TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight) { BX_CHECK(m_font == NULL, "TrueTypeFont already initialized"); BX_CHECK( (_bufferSize > 256 && _bufferSize < 100000000), "TrueType buffer size is suspicious"); BX_CHECK( (_pixelHeight > 4 && _pixelHeight < 128), "TrueType buffer size is suspicious"); FTHolder* holder = new FTHolder; FT_Error error = FT_Init_FreeType(&holder->library); BX_WARN(!error, "FT_Init_FreeType failed."); if (error) { goto err0; } error = FT_New_Memory_Face(holder->library, _buffer, _bufferSize, _fontIndex, &holder->face); BX_WARN(!error, "FT_Init_FreeType failed."); if (error) { if (FT_Err_Unknown_File_Format == error) { goto err0; } goto err1; } error = FT_Select_Charmap(holder->face, FT_ENCODING_UNICODE); BX_WARN(!error, "FT_Init_FreeType failed."); if (error) { goto err2; } error = FT_Set_Pixel_Sizes(holder->face, 0, _pixelHeight); BX_WARN(!error, "FT_Init_FreeType failed."); if (error) { goto err2; } m_font = holder; return true; err2: FT_Done_Face(holder->face); err1: FT_Done_FreeType(holder->library); err0: delete holder; return false; }
bool NvApi::initAftermath(const ID3D12Device* _device, const ID3D12CommandList* _commandList) { if (loadAftermath() ) { int32_t result; result = nvAftermathDx12Initialize(0x13, 1, _device); if (1 == result) { result = nvAftermathDx12CreateContextHandle(_commandList, &m_aftermathHandle); BX_WARN(1 == result, "NV Aftermath: nvAftermathDx12CreateContextHandle failed %x", result); if (1 == result) { return true; } } else { switch (result) { case int32_t(0xbad0000a): BX_TRACE("NV Aftermath: Debug layer not compatible with Aftermath."); break; default: BX_TRACE("NV Aftermath: Failed to initialize."); break; } } shutdownAftermath(); } return false; }
bool OVR::postReset(void* _nwh, ovrRenderAPIConfig* _config, bool _debug) { if (_debug) { switch (_config->Header.API) { #if BGFX_CONFIG_RENDERER_DIRECT3D11 case ovrRenderAPI_D3D11: { ovrD3D11ConfigData* data = (ovrD3D11ConfigData*)_config; # if OVR_VERSION > OVR_VERSION_043 m_rtSize = data->Header.BackBufferSize; # else m_rtSize = data->Header.RTSize; # endif // OVR_VERSION > OVR_VERSION_043 } break; #endif // BGFX_CONFIG_RENDERER_DIRECT3D11 #if BGFX_CONFIG_RENDERER_OPENGL case ovrRenderAPI_OpenGL: { ovrGLConfigData* data = (ovrGLConfigData*)_config; # if OVR_VERSION > OVR_VERSION_043 m_rtSize = data->Header.BackBufferSize; # else m_rtSize = data->Header.RTSize; # endif // OVR_VERSION > OVR_VERSION_043 } break; #endif // BGFX_CONFIG_RENDERER_OPENGL case ovrRenderAPI_None: default: BX_CHECK(false, "You should not be here!"); break; } m_debug = true; return false; } if (!m_initialized) { return false; } if (!_debug) { m_hmd = ovrHmd_Create(0); } if (NULL == m_hmd) { m_hmd = ovrHmd_CreateDebug(ovrHmd_DK2); BX_WARN(NULL != m_hmd, "Unable to initialize OVR."); if (NULL == m_hmd) { return false; } } BX_TRACE("HMD: %s, %s, firmware: %d.%d" , m_hmd->ProductName , m_hmd->Manufacturer , m_hmd->FirmwareMajor , m_hmd->FirmwareMinor ); ovrBool result; result = ovrHmd_AttachToWindow(m_hmd, _nwh, NULL, NULL); if (!result) { goto ovrError; } ovrFovPort eyeFov[2] = { m_hmd->DefaultEyeFov[0], m_hmd->DefaultEyeFov[1] }; result = ovrHmd_ConfigureRendering(m_hmd , _config , 0 #if OVR_VERSION < OVR_VERSION_050 | ovrDistortionCap_Chromatic // permanently enabled >= v5.0 #endif | ovrDistortionCap_Vignette | ovrDistortionCap_TimeWarp | ovrDistortionCap_Overdrive | ovrDistortionCap_NoRestore | ovrDistortionCap_HqDistortion , eyeFov , m_erd ); if (!result) { goto ovrError; } ovrHmd_SetEnabledCaps(m_hmd , 0 | ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction ); result = ovrHmd_ConfigureTracking(m_hmd , 0 | ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position , 0 ); if (!result) { ovrError: BX_TRACE("Failed to initialize OVR."); ovrHmd_Destroy(m_hmd); m_hmd = NULL; return false; } ovrSizei sizeL = ovrHmd_GetFovTextureSize(m_hmd, ovrEye_Left, m_hmd->DefaultEyeFov[0], 1.0f); ovrSizei sizeR = ovrHmd_GetFovTextureSize(m_hmd, ovrEye_Right, m_hmd->DefaultEyeFov[1], 1.0f); m_rtSize.w = sizeL.w + sizeR.w; m_rtSize.h = bx::uint32_max(sizeL.h, sizeR.h); m_warning = true; return true; }
void render(uint8_t _view, const float* _mtxView, const float* _eye) { if (0 != m_num) { bgfx::TransientVertexBuffer tvb; bgfx::TransientIndexBuffer tib; const uint32_t numVertices = bgfx::getAvailTransientVertexBuffer(m_num*4, PosColorTexCoord0Vertex::ms_decl); const uint32_t numIndices = bgfx::getAvailTransientIndexBuffer(m_num*6); const uint32_t max = bx::uint32_min(numVertices/4, numIndices/6); BX_WARN(m_num == max , "Truncating transient buffer for particles to maximum available (requested %d, available %d)." , m_num , max ); if (0 < max) { bgfx::allocTransientBuffers(&tvb , PosColorTexCoord0Vertex::ms_decl , max*4 , &tib , max*6 ); PosColorTexCoord0Vertex* vertices = (PosColorTexCoord0Vertex*)tvb.data; ParticleSort* particleSort = (ParticleSort*)BX_ALLOC(m_allocator, max*sizeof(ParticleSort) ); uint32_t pos = 0; for (uint16_t ii = 0, numEmitters = m_emitterAlloc->getNumHandles(); ii < numEmitters; ++ii) { const uint16_t idx = m_emitterAlloc->getHandleAt(ii); Emitter& emitter = m_emitter[idx]; pos += emitter.render(_mtxView, _eye, pos, max, particleSort, vertices); } qsort(particleSort , max , sizeof(ParticleSort) , particleSortFn ); uint16_t* indices = (uint16_t*)tib.data; for (uint32_t ii = 0; ii < max; ++ii) { const ParticleSort& sort = particleSort[ii]; uint16_t* index = &indices[ii*6]; uint16_t idx = (uint16_t)sort.idx; index[0] = idx*4+0; index[1] = idx*4+1; index[2] = idx*4+2; index[3] = idx*4+2; index[4] = idx*4+3; index[5] = idx*4+0; } BX_FREE(m_allocator, particleSort); bgfx::setState(0 | BGFX_STATE_RGB_WRITE | BGFX_STATE_ALPHA_WRITE | BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_CULL_CW | BGFX_STATE_BLEND_NORMAL ); bgfx::setVertexBuffer(&tvb); bgfx::setIndexBuffer(&tib); bgfx::setTexture(0, s_texColor, m_particleTexture); bgfx::submit(_view, m_particleProgram); } } }
void GlContext::create(uint32_t /*_width*/, uint32_t /*_height*/) { m_opengl32dll = bx::dlopen("opengl32.dll"); BGFX_FATAL(NULL != m_opengl32dll, Fatal::UnableToInitialize, "Failed to load opengl32.dll."); wglGetProcAddress = (PFNWGLGETPROCADDRESSPROC)bx::dlsym(m_opengl32dll, "wglGetProcAddress"); BGFX_FATAL(NULL != wglGetProcAddress, Fatal::UnableToInitialize, "Failed get wglGetProcAddress."); // If g_bgfxHwnd is NULL, the assumption is that GL context was created // by user (for example, using SDL, GLFW, etc.) BX_WARN(NULL != g_bgfxHwnd , "bgfx::winSetHwnd with valid window is not called. This might " "be intentional when GL context is created by the user." ); if (NULL != g_bgfxHwnd) { wglMakeCurrent = (PFNWGLMAKECURRENTPROC)bx::dlsym(m_opengl32dll, "wglMakeCurrent"); BGFX_FATAL(NULL != wglMakeCurrent, Fatal::UnableToInitialize, "Failed get wglMakeCurrent."); wglCreateContext = (PFNWGLCREATECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglCreateContext"); BGFX_FATAL(NULL != wglCreateContext, Fatal::UnableToInitialize, "Failed get wglCreateContext."); wglDeleteContext = (PFNWGLDELETECONTEXTPROC)bx::dlsym(m_opengl32dll, "wglDeleteContext"); BGFX_FATAL(NULL != wglDeleteContext, Fatal::UnableToInitialize, "Failed get wglDeleteContext."); m_hdc = GetDC(g_bgfxHwnd); BGFX_FATAL(NULL != m_hdc, Fatal::UnableToInitialize, "GetDC failed!"); // Dummy window to peek into WGL functionality. // // An application can only set the pixel format of a window one time. // Once a window's pixel format is set, it cannot be changed. // MSDN: http://msdn.microsoft.com/en-us/library/windows/desktop/dd369049%28v=vs.85%29.aspx HWND hwnd = CreateWindowA("STATIC" , "" , WS_POPUP|WS_DISABLED , -32000 , -32000 , 0 , 0 , NULL , NULL , GetModuleHandle(NULL) , 0 ); HDC hdc = GetDC(hwnd); BGFX_FATAL(NULL != hdc, Fatal::UnableToInitialize, "GetDC failed!"); HGLRC context = createContext(hdc); wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB"); wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB"); wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB"); wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT"); if (NULL != wglGetExtensionsStringARB) { const char* extensions = (const char*)wglGetExtensionsStringARB(hdc); BX_TRACE("WGL extensions:"); dumpExtensions(extensions); } if (NULL != wglChoosePixelFormatARB && NULL != wglCreateContextAttribsARB) { int32_t attrs[] = { WGL_SAMPLE_BUFFERS_ARB, 0, WGL_SAMPLES_ARB, 0, WGL_SUPPORT_OPENGL_ARB, true, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_DRAW_TO_WINDOW_ARB, true, WGL_DOUBLE_BUFFER_ARB, true, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0 }; int result; uint32_t numFormats = 0; do { result = wglChoosePixelFormatARB(m_hdc, attrs, NULL, 1, &m_pixelFormat, &numFormats); if (0 == result || 0 == numFormats) { attrs[3] >>= 1; attrs[1] = attrs[3] == 0 ? 0 : 1; } } while (0 == numFormats); DescribePixelFormat(m_hdc, m_pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &m_pfd); BX_TRACE("Pixel format:\n" "\tiPixelType %d\n" "\tcColorBits %d\n" "\tcAlphaBits %d\n" "\tcDepthBits %d\n" "\tcStencilBits %d\n" , m_pfd.iPixelType , m_pfd.cColorBits , m_pfd.cAlphaBits , m_pfd.cDepthBits , m_pfd.cStencilBits ); result = SetPixelFormat(m_hdc, m_pixelFormat, &m_pfd); // When window is created by SDL and SDL_WINDOW_OPENGL is set SetPixelFormat // will fail. Just warn and continue. In case it failed for some other reason // create context will fail and it will error out there. BX_WARN(result, "SetPixelFormat failed (last err: 0x%08x)!", GetLastError() ); uint32_t flags = BGFX_CONFIG_DEBUG ? WGL_CONTEXT_DEBUG_BIT_ARB : 0; BX_UNUSED(flags); int32_t contextAttrs[9] = { #if BGFX_CONFIG_RENDERER_OPENGL >= 31 WGL_CONTEXT_MAJOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL / 10, WGL_CONTEXT_MINOR_VERSION_ARB, BGFX_CONFIG_RENDERER_OPENGL % 10, WGL_CONTEXT_FLAGS_ARB, flags, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, #else WGL_CONTEXT_MAJOR_VERSION_ARB, 2, WGL_CONTEXT_MINOR_VERSION_ARB, 1, 0, 0, 0, 0, #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31 0 }; m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs); if (NULL == m_context) { // nVidia doesn't like context profile mask for contexts below 3.2? contextAttrs[6] = WGL_CONTEXT_PROFILE_MASK_ARB == contextAttrs[6] ? 0 : contextAttrs[6]; m_context = wglCreateContextAttribsARB(m_hdc, 0, contextAttrs); } BGFX_FATAL(NULL != m_context, Fatal::UnableToInitialize, "Failed to create context 0x%08x.", GetLastError() ); BX_STATIC_ASSERT(sizeof(contextAttrs) == sizeof(m_contextAttrs) ); memcpy(m_contextAttrs, contextAttrs, sizeof(contextAttrs) ); }
int main(int _argc, char** _argv) { //DBG(BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME); if (BX_ENABLED(ENTRY_CONFIG_PROFILER) ) { rmtSettings* settings = rmt_Settings(); BX_WARN(NULL != settings, "Remotery is not enabled."); if (NULL != settings) { settings->malloc = rmtMalloc; settings->realloc = rmtRealloc; settings->free = rmtFree; rmtError err = rmt_CreateGlobalInstance(&s_rmt); BX_WARN(RMT_ERROR_NONE != err, "Remotery failed to create global instance."); if (RMT_ERROR_NONE == err) { rmt_SetCurrentThreadName("Main"); } else { s_rmt = NULL; } } } #if BX_CONFIG_CRT_FILE_READER_WRITER s_fileReader = new bx::CrtFileReader; s_fileWriter = new bx::CrtFileWriter; #endif // BX_CONFIG_CRT_FILE_READER_WRITER cmdInit(); cmdAdd("mouselock", cmdMouseLock); cmdAdd("graphics", cmdGraphics ); cmdAdd("exit", cmdExit ); inputInit(); inputAddBindings("bindings", s_bindings); entry::WindowHandle defaultWindow = { 0 }; entry::setWindowTitle(defaultWindow, bx::baseName(_argv[0]) ); setWindowSize(defaultWindow, ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT); int32_t result = ::_main_(_argc, _argv); inputRemoveBindings("bindings"); inputShutdown(); cmdShutdown(); #if BX_CONFIG_CRT_FILE_READER_WRITER delete s_fileReader; s_fileReader = NULL; delete s_fileWriter; s_fileWriter = NULL; #endif // BX_CONFIG_CRT_FILE_READER_WRITER if (BX_ENABLED(ENTRY_CONFIG_PROFILER) && NULL != s_rmt) { rmt_DestroyGlobalInstance(s_rmt); } return result; }