diff --git a/src/poly/main_win.cc b/src/poly/main_win.cc index b4bdad74f..c01705ba7 100644 --- a/src/poly/main_win.cc +++ b/src/poly/main_win.cc @@ -37,13 +37,13 @@ void AttachConsole() { auto con_handle = _open_osfhandle(std_handle, _O_TEXT); auto fp = _fdopen(con_handle, "w"); *stdout = *fp; - setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stdout, nullptr, _IONBF, 0); std_handle = (intptr_t)GetStdHandle(STD_ERROR_HANDLE); con_handle = _open_osfhandle(std_handle, _O_TEXT); fp = _fdopen(con_handle, "w"); *stderr = *fp; - setvbuf(stderr, NULL, _IONBF, 0); + setvbuf(stderr, nullptr, _IONBF, 0); } } // namespace poly @@ -62,7 +62,7 @@ int wmain(int argc, wchar_t* argv[]) { for (int n = 0; n < argca; n++) { size_t len = wcslen(argv[n]); argva[n] = (char*)alloca(len + 1); - wcstombs_s(NULL, argva[n], len + 1, argv[n], _TRUNCATE); + wcstombs_s(nullptr, argva[n], len + 1, argv[n], _TRUNCATE); } // Parse flags; this may delete some of them. @@ -74,6 +74,10 @@ int wmain(int argc, wchar_t* argv[]) { args.push_back(poly::to_wstring(argva[n])); } + // Setup COM on the main thread. + // NOTE: this may fail if COM has already been initialized - that's OK. + CoInitializeEx(nullptr, COINIT_MULTITHREADED); + // Call app-provided entry point. int result = entry_info.entry_point(args); diff --git a/src/xenia/core.h b/src/xenia/core.h index 7179804da..c89428f74 100644 --- a/src/xenia/core.h +++ b/src/xenia/core.h @@ -18,7 +18,6 @@ namespace xe { } // namespace xe #include -#include #include #include #include diff --git a/src/xenia/core/pal.h b/src/xenia/core/pal.h deleted file mode 100644 index 7ba8d232f..000000000 --- a/src/xenia/core/pal.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#ifndef XENIA_CORE_PAL_H_ -#define XENIA_CORE_PAL_H_ - -#include -#include - - -typedef struct { - int reserved; -} xe_pal_options_t; - - -int xe_pal_init(xe_pal_options_t options); - - -typedef struct { - struct { - uint32_t physical_count; - uint32_t logical_count; - } processors; -} xe_system_info; - -int xe_pal_get_system_info(xe_system_info* out_info); - -double xe_pal_now(); - - -#endif // XENIA_CORE_PAL_H_ diff --git a/src/xenia/core/pal_mac.cc b/src/xenia/core/pal_mac.cc deleted file mode 100644 index edcfef4aa..000000000 --- a/src/xenia/core/pal_mac.cc +++ /dev/null @@ -1,84 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - -#include -#include -#include -#include -#include -#include - - -namespace { -typedef struct xe_pal_mac { - double time_to_sec; -} xe_pal_mac_t; - -xe_pal_mac_t* pal; -} - - -void xe_pal_dealloc(); -int xe_pal_init(xe_pal_options_t options) { - pal = (xe_pal_mac_t*)xe_calloc(sizeof(pal)); - - mach_timebase_info_data_t info; - mach_timebase_info(&info); - pal->time_to_sec = (double)((info.numer / info.denom) / 1000000000.0); - - atexit(xe_pal_dealloc); - return 0; -} - -void xe_pal_dealloc() { - // - - xe_free(pal); -} - -int xe_pal_get_system_info(xe_system_info* out_info) { - xe_zero_struct(out_info, sizeof(xe_system_info)); - out_info->processors.physical_count = 1; - out_info->processors.logical_count = 1; - - // http://geekinfo.googlecode.com/svn-history/r74/trunk/src/macosxsystem.cpp - int count; - size_t size = sizeof(int); - if (!sysctlbyname("hw.physicalcpu", &count, &size, NULL, 0)) { - out_info->processors.physical_count = count; - } else if (!sysctlbyname("hw.packages", &count, &size, NULL, 0)) { - out_info->processors.physical_count = count; - } else { - out_info->processors.physical_count = 1; - } - if (!sysctlbyname("hw.logicalcpu", &count, &size, NULL, 0)) { - out_info->processors.logical_count = count; - } else if (!sysctlbyname("hw.ncpu", &count, &size, NULL, 0)) { - out_info->processors.logical_count = count; - } else { - out_info->processors.logical_count = 1; - } - - return 0; -} - -double xe_pal_now() { - // According to a bunch of random posts, CACurrentMediaTime is a better call: - // https://devforums.apple.com/message/118806#118806 - // CACurrentMediaTime is based on mach_absolute_time(), which doesn't have a - // dependency on QuartzCore: - // http://developer.apple.com/library/mac/#qa/qa2004/qa1398.html - - // return (double)CFAbsoluteTimeGetCurrent(); - // return (double)CACurrentMediaTime(); - - return (double)(mach_absolute_time() * pal->time_to_sec); -} diff --git a/src/xenia/core/pal_posix.cc b/src/xenia/core/pal_posix.cc deleted file mode 100644 index 93b2986f8..000000000 --- a/src/xenia/core/pal_posix.cc +++ /dev/null @@ -1,62 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - - -namespace { -typedef struct xe_pal_posix { - -} xe_pal_posix_t; - -xe_pal_posix_t* pal; -} - - -void xe_pal_dealloc(); -int xe_pal_init(xe_pal_options_t options) { - pal = (xe_pal_posix_t)xe_calloc(sizeof(pal)); - - // - - atexit(xe_pal_dealloc); - return 0; -} - -void xe_pal_dealloc() { - // - - xe_free(pal); -} - -int xe_pal_get_system_info(xe_system_info* out_info) { - xe_zero_struct(out_info, sizeof(xe_system_info)); - out_info->processors.physical_count = 1; - out_info->processors.logical_count = 1; - -#if defined(_SC_NPROCESSORS_ONLN) - int nproc = sysconf(_SC_NPROCESSORS_ONLN); - if (nproc >= 1) { - // Only able to get logical count. - sysInfo->processors.logicalCount = nproc; - } -#else -#warning no calls to get processor counts -#endif // _SC_NPROCESSORS_ONLN - - return 0; -} - -double xe_pal_now() { - // http://www.kernel.org/doc/man-pages/online/pages/man2/clock_gettime.2.html - // http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html - struct timespec ts; - CPIGNORE(clock_gettime(CLOCK_MONOTONIC, &ts)); - return (double)(ts.tv_sec + (ts.tv_nsec * 1000000000.0)); -} diff --git a/src/xenia/core/pal_win.cc b/src/xenia/core/pal_win.cc deleted file mode 100644 index c0b0a90a3..000000000 --- a/src/xenia/core/pal_win.cc +++ /dev/null @@ -1,146 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - -#include - -namespace { -typedef struct xe_pal_win { - bool use_high_perf_timer; - double inv_ticks_per_sec; -} xe_pal_win_t; - -xe_pal_win_t* pal; -} - - -void xe_pal_dealloc(); -int xe_pal_init(xe_pal_options_t options) { - pal = (xe_pal_win_t*)xe_calloc(sizeof(xe_pal_win_t)); - - // Get QPC timing frequency... hopefully stable over the life of the app, - // but likely not. - // TODO(benvanik): requery periodically/etc. - LARGE_INTEGER ticks_per_sec; - if (QueryPerformanceFrequency(&ticks_per_sec)) { - pal->use_high_perf_timer = true; - pal->inv_ticks_per_sec = 1.0 / (double)ticks_per_sec.QuadPart; - } else { - pal->use_high_perf_timer = false; - XELOGW("Falling back from high performance timer"); - } - - // Setup COM on the main thread. - // NOTE: this may fail if COM has already been initialized - that's OK. - CoInitializeEx(NULL, COINIT_MULTITHREADED); - - atexit(xe_pal_dealloc); - return 0; -} - -void xe_pal_dealloc() { - // - - xe_free(pal); -} - -// http://msdn.microsoft.com/en-us/library/ms683194.aspx -namespace { -DWORD CountSetBits(ULONG_PTR bitMask) { - DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1; - DWORD bitSetCount = 0; - ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT; - for (DWORD i = 0; i <= LSHIFT; ++i, bitTest /= 2) { - bitSetCount += ((bitMask & bitTest)?1:0); - } - return bitSetCount; -} -} -int xe_pal_get_system_info(xe_system_info* out_info) { - int result_code = 1; - xe_zero_struct(out_info, sizeof(xe_system_info)); - out_info->processors.physical_count = 1; - out_info->processors.logical_count = 1; - - typedef BOOL (WINAPI *LPFN_GLPI)( - PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); - - HMODULE kernel32 = NULL; - LPFN_GLPI glpi = NULL; - PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; - - kernel32 = GetModuleHandle(L"kernel32"); - XEEXPECTNOTNULL(kernel32); - glpi = (LPFN_GLPI)GetProcAddress(kernel32, "GetLogicalProcessorInformation"); - XEEXPECTNOTNULL(glpi); - - // Call GLPI once to get the buffer size, allocate it, then call again. - DWORD buffer_length = 0; - XEEXPECTFALSE(glpi(NULL, &buffer_length)); - XEEXPECT(GetLastError() == ERROR_INSUFFICIENT_BUFFER); - buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)xe_malloc(buffer_length); - XEEXPECTNOTNULL(buffer); - XEEXPECTTRUE(glpi(buffer, &buffer_length)); - XEEXPECTNOTZERO(buffer_length); - - out_info->processors.physical_count = 0; - out_info->processors.logical_count = 0; - - size_t info_count = buffer_length / - sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); - for (size_t n = 0; n < info_count; n++) { - switch (buffer[n].Relationship) { - case RelationProcessorPackage: - out_info->processors.physical_count++; - break; - case RelationProcessorCore: - if (buffer[n].ProcessorCore.Flags == 1) { - // Hyper-threaded. - // The number of processors is set as bits in ProcessorMask. - out_info->processors.logical_count += - CountSetBits(buffer[n].ProcessorMask); - } else { - // A real core - just count as one. - out_info->processors.logical_count++; - } - break; - } - } - - out_info->processors.physical_count = - std::max(1u, out_info->processors.physical_count); - out_info->processors.logical_count = - std::max(1u, out_info->processors.logical_count); - - result_code = 0; -XECLEANUP: - xe_free(buffer); - if (kernel32) { - FreeLibrary(kernel32); - } - return result_code; -} - -double xe_pal_now() { - if (pal->use_high_perf_timer) { - LARGE_INTEGER counter; - QueryPerformanceCounter(&counter); - return counter.QuadPart * pal->inv_ticks_per_sec; - } else { - // Using GetSystemTimeAsFileTime instead of GetSystemTime() as it has a - // 100ns resolution. - FILETIME ft; - GetSystemTimeAsFileTime(&ft); - ULARGE_INTEGER uli; - uli.LowPart = ft.dwLowDateTime; - uli.HighPart = ft.dwHighDateTime; - return uli.QuadPart / 10000000.0; - } -} diff --git a/src/xenia/core/sources.gypi b/src/xenia/core/sources.gypi index 206ae0e60..858a696bb 100644 --- a/src/xenia/core/sources.gypi +++ b/src/xenia/core/sources.gypi @@ -4,7 +4,6 @@ 'hash.cc', 'hash.h', 'mmap.h', - 'pal.h', 'ref.cc', 'ref.h', 'run_loop.h', @@ -20,18 +19,15 @@ }], ['OS == "linux"', { 'sources': [ - 'pal_posix.cc', ], }], ['OS == "mac"', { 'sources': [ - 'pal_mac.cc', ], }], ['OS == "win"', { 'sources': [ 'mmap_win.cc', - 'pal_win.cc', 'run_loop_win.cc', 'socket_win.cc', ], diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_system.cc b/src/xenia/gpu/d3d11/d3d11_graphics_system.cc index f88977bac..7f357334a 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_system.cc +++ b/src/xenia/gpu/d3d11/d3d11_graphics_system.cc @@ -9,6 +9,8 @@ #include +#include + #include #include #include @@ -24,8 +26,7 @@ using namespace xe::gpu::d3d11; D3D11GraphicsSystem::D3D11GraphicsSystem(Emulator* emulator) : GraphicsSystem(emulator), window_(nullptr), dxgi_factory_(nullptr), device_(nullptr), - timer_queue_(nullptr), vsync_timer_(nullptr), - last_swap_time_(0.0) { + timer_queue_(nullptr), vsync_timer_(nullptr) { } D3D11GraphicsSystem::~D3D11GraphicsSystem() { @@ -142,8 +143,9 @@ void D3D11GraphicsSystem::Initialize() { void D3D11GraphicsSystem::Pump() { SCOPE_profile_cpu_f("gpu"); - double time_since_last_swap = xe_pal_now() - last_swap_time_; - if (time_since_last_swap > 1.0) { + auto time_since_last_swap = std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - last_swap_time_); + if (time_since_last_swap.count() > 1) { // Force a swap when profiling. if (Profiler::is_enabled()) { window_->Swap(); @@ -159,7 +161,7 @@ void D3D11GraphicsSystem::Swap() { // If we are set to vsync this will block. window_->Swap(); - last_swap_time_ = xe_pal_now(); + last_swap_time_ = std::chrono::high_resolution_clock::now(); } void __stdcall D3D11GraphicsSystem::VsyncCallback(D3D11GraphicsSystem* gs, diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_system.h b/src/xenia/gpu/d3d11/d3d11_graphics_system.h index 487e21821..bdefc2c21 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_system.h +++ b/src/xenia/gpu/d3d11/d3d11_graphics_system.h @@ -49,7 +49,7 @@ private: HANDLE timer_queue_; HANDLE vsync_timer_; - double last_swap_time_; + std::chrono::high_resolution_clock::time_point last_swap_time_; }; diff --git a/src/xenia/gpu/graphics_system.cc b/src/xenia/gpu/graphics_system.cc index 93b69984e..c969e23d2 100644 --- a/src/xenia/gpu/graphics_system.cc +++ b/src/xenia/gpu/graphics_system.cc @@ -29,7 +29,7 @@ GraphicsSystem::GraphicsSystem(Emulator* emulator) : running_(false), driver_(nullptr), command_processor_(nullptr), interrupt_callback_(0), interrupt_callback_data_(0), - last_interrupt_time_(0), thread_wait_(nullptr) { + thread_wait_(nullptr) { // Create the run loop used for any windows/etc. // This must be done on the thread we create the driver. run_loop_ = xe_run_loop_create(); @@ -193,7 +193,6 @@ void GraphicsSystem::DispatchInterruptCallback( } // NOTE: we may be executing in some random thread. - last_interrupt_time_ = xe_pal_now(); if (!interrupt_callback_) { return; } diff --git a/src/xenia/gpu/graphics_system.h b/src/xenia/gpu/graphics_system.h index 84685db79..0a068ff71 100644 --- a/src/xenia/gpu/graphics_system.h +++ b/src/xenia/gpu/graphics_system.h @@ -81,7 +81,6 @@ protected: uint32_t interrupt_callback_; uint32_t interrupt_callback_data_; - double last_interrupt_time_; HANDLE thread_wait_; }; diff --git a/tools/xenia-compare/xenia-compare.cc b/tools/xenia-compare/xenia-compare.cc index e5f338d48..16200cb21 100644 --- a/tools/xenia-compare/xenia-compare.cc +++ b/tools/xenia-compare/xenia-compare.cc @@ -23,14 +23,6 @@ namespace xc { using xdb::PostmortemDebugTarget; int main(std::vector& args) { - // Create platform abstraction layer. - xe_pal_options_t pal_options; - xe_zero_struct(&pal_options, sizeof(pal_options)); - if (xe_pal_init(pal_options)) { - XEFATAL("Failed to initialize PAL"); - return 1; - } - auto left_target = std::make_unique(); if (!left_target->LoadTrace(poly::to_wstring(FLAGS_trace_file_left))) { XEFATAL("Unable to load left trace file: %s", diff --git a/tools/xenia-debug/xenia-debug.cc b/tools/xenia-debug/xenia-debug.cc index f16e2401f..ce8450d50 100644 --- a/tools/xenia-debug/xenia-debug.cc +++ b/tools/xenia-debug/xenia-debug.cc @@ -24,14 +24,6 @@ DEFINE_string(content_file, "", namespace xdb { int main(std::vector& args) { - // Create platform abstraction layer. - xe_pal_options_t pal_options; - xe_zero_struct(&pal_options, sizeof(pal_options)); - if (xe_pal_init(pal_options)) { - XEFATAL("Failed to initialize PAL"); - return 1; - } - wxInitializer init; if (!init.IsOk()) { XEFATAL("Failed to initialize wxWidgets"); diff --git a/tools/xenia-run/xenia-run.cc b/tools/xenia-run/xenia-run.cc index 4f95bbf2b..a238824df 100644 --- a/tools/xenia-run/xenia-run.cc +++ b/tools/xenia-run/xenia-run.cc @@ -38,14 +38,6 @@ int xenia_run(std::vector& args) { // Normalize the path and make absolute. std::wstring abs_path = poly::to_absolute_path(path); - // Create platform abstraction layer. - xe_pal_options_t pal_options; - xe_zero_struct(&pal_options, sizeof(pal_options)); - if (xe_pal_init(pal_options)) { - XELOGE("Failed to initialize PAL"); - return 1; - } - // Create the emulator. auto emulator = std::make_unique(L""); X_STATUS result = emulator->Setup(); diff --git a/tools/xenia-test/xenia-test.cc b/tools/xenia-test/xenia-test.cc index 955cc2eda..3a6f78b72 100644 --- a/tools/xenia-test/xenia-test.cc +++ b/tools/xenia-test/xenia-test.cc @@ -229,10 +229,6 @@ int run_tests(std::string& test_name) { vector test_files; - xe_pal_options_t pal_options; - xe_zero_struct(&pal_options, sizeof(pal_options)); - XEEXPECTZERO(xe_pal_init(pal_options)); - XEEXPECTZERO(discover_tests(FLAGS_test_path, test_files)); if (!test_files.size()) { printf("No tests discovered - invalid path?\n");