diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 5 | ||||
-rw-r--r-- | src/common/bit_field.h | 65 | ||||
-rw-r--r-- | src/common/break_points.cpp | 1 | ||||
-rw-r--r-- | src/common/common_funcs.h | 2 | ||||
-rw-r--r-- | src/common/framebuffer_layout.cpp | 157 | ||||
-rw-r--r-- | src/common/framebuffer_layout.h | 55 | ||||
-rw-r--r-- | src/common/hash.cpp | 6 | ||||
-rw-r--r-- | src/common/x64/cpu_detect.cpp | 2 |
8 files changed, 49 insertions, 244 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 6905d2d50..7e83e64b0 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -27,7 +27,6 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scm_rev.cpp.in" "${CMAKE_CURRENT_SOU set(SRCS break_points.cpp file_util.cpp - framebuffer_layout.cpp hash.cpp logging/filter.cpp logging/text_formatter.cpp @@ -56,7 +55,6 @@ set(HEADERS common_paths.h common_types.h file_util.h - framebuffer_layout.h hash.h linear_disk_cache.h logging/text_formatter.h @@ -97,6 +95,7 @@ endif() create_directory_groups(${SRCS} ${HEADERS}) add_library(common STATIC ${SRCS} ${HEADERS}) +target_link_libraries(common PUBLIC Boost::boost microprofile) if (ARCHITECTURE_x86_64) - target_link_libraries(common xbyak) + target_link_libraries(common PRIVATE xbyak) endif() diff --git a/src/common/bit_field.h b/src/common/bit_field.h index 030f7caeb..0cc0a1be0 100644 --- a/src/common/bit_field.h +++ b/src/common/bit_field.h @@ -108,7 +108,7 @@ * symptoms. */ #pragma pack(1) -template <std::size_t position, std::size_t bits, typename T> +template <std::size_t Position, std::size_t Bits, typename T> struct BitField { private: // We hide the copy assigment operator here, because the default copy @@ -117,7 +117,45 @@ private: // We don't delete it because we want BitField to be trivially copyable. BitField& operator=(const BitField&) = default; + // StorageType is T for non-enum types and the underlying type of T if + // T is an enumeration. Note that T is wrapped within an enable_if in the + // former case to workaround compile errors which arise when using + // std::underlying_type<T>::type directly. + using StorageType = typename std::conditional_t<std::is_enum<T>::value, std::underlying_type<T>, + std::enable_if<true, T>>::type; + + // Unsigned version of StorageType + using StorageTypeU = std::make_unsigned_t<StorageType>; + public: + /// Constants to allow limited introspection of fields if needed + static constexpr size_t position = Position; + static constexpr size_t bits = Bits; + static constexpr StorageType mask = (((StorageTypeU)~0) >> (8 * sizeof(T) - bits)) << position; + + /** + * Formats a value by masking and shifting it according to the field parameters. A value + * containing several bitfields can be assembled by formatting each of their values and ORing + * the results together. + */ + static constexpr FORCE_INLINE StorageType FormatValue(const T& value) { + return ((StorageType)value << position) & mask; + } + + /** + * Extracts a value from the passed storage. In most situations prefer use the member functions + * (such as Value() or operator T), but this can be used to extract a value from a bitfield + * union in a constexpr context. + */ + static constexpr FORCE_INLINE T ExtractValue(const StorageType& storage) { + if (std::numeric_limits<T>::is_signed) { + std::size_t shift = 8 * sizeof(T) - bits; + return (T)((storage << (shift - position)) >> shift); + } else { + return (T)((storage & mask) >> position); + } + } + // This constructor and assignment operator might be considered ambiguous: // Would they initialize the storage or just the bitfield? // Hence, delete them. Use the Assign method to set bitfield values! @@ -126,23 +164,18 @@ public: // Force default constructor to be created // so that we can use this within unions - BitField() = default; + constexpr BitField() = default; FORCE_INLINE operator T() const { return Value(); } FORCE_INLINE void Assign(const T& value) { - storage = (storage & ~GetMask()) | (((StorageType)value << position) & GetMask()); + storage = (storage & ~mask) | FormatValue(value); } FORCE_INLINE T Value() const { - if (std::numeric_limits<T>::is_signed) { - std::size_t shift = 8 * sizeof(T) - bits; - return (T)((storage << (shift - position)) >> shift); - } else { - return (T)((storage & GetMask()) >> position); - } + return ExtractValue(storage); } // TODO: we may want to change this to explicit operator bool() if it's bug-free in VS2015 @@ -151,20 +184,6 @@ public: } private: - // StorageType is T for non-enum types and the underlying type of T if - // T is an enumeration. Note that T is wrapped within an enable_if in the - // former case to workaround compile errors which arise when using - // std::underlying_type<T>::type directly. - typedef typename std::conditional<std::is_enum<T>::value, std::underlying_type<T>, - std::enable_if<true, T>>::type::type StorageType; - - // Unsigned version of StorageType - typedef typename std::make_unsigned<StorageType>::type StorageTypeU; - - FORCE_INLINE StorageType GetMask() const { - return (((StorageTypeU)~0) >> (8 * sizeof(T) - bits)) << position; - } - StorageType storage; static_assert(bits + position <= 8 * sizeof(T), "Bitfield out of range"); diff --git a/src/common/break_points.cpp b/src/common/break_points.cpp index 03a19acba..fa367a4ca 100644 --- a/src/common/break_points.cpp +++ b/src/common/break_points.cpp @@ -5,7 +5,6 @@ #include <algorithm> #include <sstream> #include "common/break_points.h" -#include "common/logging/log.h" bool BreakPoints::IsAddressBreakPoint(u32 iAddress) const { auto cond = [&iAddress](const TBreakPoint& bp) { return bp.iAddress == iAddress; }; diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index b141e79ed..2e7877500 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -7,7 +7,7 @@ #if !defined(ARCHITECTURE_x86_64) && !defined(_M_ARM) #include <cstdlib> // for exit #endif -#include "common_types.h" +#include "common/common_types.h" #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) diff --git a/src/common/framebuffer_layout.cpp b/src/common/framebuffer_layout.cpp deleted file mode 100644 index a2a0e7dad..000000000 --- a/src/common/framebuffer_layout.cpp +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <cmath> - -#include "common/assert.h" -#include "common/framebuffer_layout.h" -#include "core/settings.h" -#include "video_core/video_core.h" - -namespace Layout { - -static const float TOP_SCREEN_ASPECT_RATIO = - static_cast<float>(VideoCore::kScreenTopHeight) / VideoCore::kScreenTopWidth; -static const float BOT_SCREEN_ASPECT_RATIO = - static_cast<float>(VideoCore::kScreenBottomHeight) / VideoCore::kScreenBottomWidth; - -// Finds the largest size subrectangle contained in window area that is confined to the aspect ratio -template <class T> -static MathUtil::Rectangle<T> maxRectangle(MathUtil::Rectangle<T> window_area, - float screen_aspect_ratio) { - float scale = std::min(static_cast<float>(window_area.GetWidth()), - window_area.GetHeight() / screen_aspect_ratio); - return MathUtil::Rectangle<T>{0, 0, static_cast<T>(std::round(scale)), - static_cast<T>(std::round(scale * screen_aspect_ratio))}; -} - -FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height, bool swapped) { - ASSERT(width > 0); - ASSERT(height > 0); - - FramebufferLayout res{width, height, true, true, {}, {}}; - // Default layout gives equal screen sizes to the top and bottom screen - MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height / 2}; - MathUtil::Rectangle<unsigned> top_screen = - maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO); - MathUtil::Rectangle<unsigned> bot_screen = - maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO); - - float window_aspect_ratio = static_cast<float>(height) / width; - // both screens height are taken into account by multiplying by 2 - float emulation_aspect_ratio = TOP_SCREEN_ASPECT_RATIO * 2; - - if (window_aspect_ratio < emulation_aspect_ratio) { - // Apply borders to the left and right sides of the window. - top_screen = - top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2); - bot_screen = - bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2); - } else { - // Window is narrower than the emulation content => apply borders to the top and bottom - // Recalculate the bottom screen to account for the width difference between top and bottom - screen_window_area = {0, 0, width, top_screen.GetHeight()}; - bot_screen = maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO); - bot_screen = bot_screen.TranslateX((top_screen.GetWidth() - bot_screen.GetWidth()) / 2); - if (swapped) { - bot_screen = bot_screen.TranslateY(height / 2 - bot_screen.GetHeight()); - } else { - top_screen = top_screen.TranslateY(height / 2 - top_screen.GetHeight()); - } - } - // Move the top screen to the bottom if we are swapped. - res.top_screen = swapped ? top_screen.TranslateY(height / 2) : top_screen; - res.bottom_screen = swapped ? bot_screen : bot_screen.TranslateY(height / 2); - return res; -} - -FramebufferLayout SingleFrameLayout(unsigned width, unsigned height, bool swapped) { - ASSERT(width > 0); - ASSERT(height > 0); - // The drawing code needs at least somewhat valid values for both screens - // so just calculate them both even if the other isn't showing. - FramebufferLayout res{width, height, !swapped, swapped, {}, {}}; - - MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height}; - MathUtil::Rectangle<unsigned> top_screen = - maxRectangle(screen_window_area, TOP_SCREEN_ASPECT_RATIO); - MathUtil::Rectangle<unsigned> bot_screen = - maxRectangle(screen_window_area, BOT_SCREEN_ASPECT_RATIO); - - float window_aspect_ratio = static_cast<float>(height) / width; - float emulation_aspect_ratio = (swapped) ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO; - - if (window_aspect_ratio < emulation_aspect_ratio) { - top_screen = - top_screen.TranslateX((screen_window_area.GetWidth() - top_screen.GetWidth()) / 2); - bot_screen = - bot_screen.TranslateX((screen_window_area.GetWidth() - bot_screen.GetWidth()) / 2); - } else { - top_screen = top_screen.TranslateY((height - top_screen.GetHeight()) / 2); - bot_screen = bot_screen.TranslateY((height - bot_screen.GetHeight()) / 2); - } - res.top_screen = top_screen; - res.bottom_screen = bot_screen; - return res; -} - -FramebufferLayout LargeFrameLayout(unsigned width, unsigned height, bool swapped) { - ASSERT(width > 0); - ASSERT(height > 0); - - FramebufferLayout res{width, height, true, true, {}, {}}; - // Split the window into two parts. Give 4x width to the main screen and 1x width to the small - // To do that, find the total emulation box and maximize that based on window size - float window_aspect_ratio = static_cast<float>(height) / width; - float emulation_aspect_ratio = - swapped - ? VideoCore::kScreenBottomHeight * 4 / - (VideoCore::kScreenBottomWidth * 4.0f + VideoCore::kScreenTopWidth) - : VideoCore::kScreenTopHeight * 4 / - (VideoCore::kScreenTopWidth * 4.0f + VideoCore::kScreenBottomWidth); - float large_screen_aspect_ratio = swapped ? BOT_SCREEN_ASPECT_RATIO : TOP_SCREEN_ASPECT_RATIO; - float small_screen_aspect_ratio = swapped ? TOP_SCREEN_ASPECT_RATIO : BOT_SCREEN_ASPECT_RATIO; - - MathUtil::Rectangle<unsigned> screen_window_area{0, 0, width, height}; - MathUtil::Rectangle<unsigned> total_rect = - maxRectangle(screen_window_area, emulation_aspect_ratio); - MathUtil::Rectangle<unsigned> large_screen = - maxRectangle(total_rect, large_screen_aspect_ratio); - MathUtil::Rectangle<unsigned> fourth_size_rect = total_rect.Scale(.25f); - MathUtil::Rectangle<unsigned> small_screen = - maxRectangle(fourth_size_rect, small_screen_aspect_ratio); - - if (window_aspect_ratio < emulation_aspect_ratio) { - large_screen = - large_screen.TranslateX((screen_window_area.GetWidth() - total_rect.GetWidth()) / 2); - } else { - large_screen = large_screen.TranslateY((height - total_rect.GetHeight()) / 2); - } - // Shift the small screen to the bottom right corner - small_screen = - small_screen.TranslateX(large_screen.right) - .TranslateY(large_screen.GetHeight() + large_screen.top - small_screen.GetHeight()); - res.top_screen = swapped ? small_screen : large_screen; - res.bottom_screen = swapped ? large_screen : small_screen; - return res; -} - -FramebufferLayout CustomFrameLayout(unsigned width, unsigned height) { - ASSERT(width > 0); - ASSERT(height > 0); - - FramebufferLayout res{width, height, true, true, {}, {}}; - - MathUtil::Rectangle<unsigned> top_screen{ - Settings::values.custom_top_left, Settings::values.custom_top_top, - Settings::values.custom_top_right, Settings::values.custom_top_bottom}; - MathUtil::Rectangle<unsigned> bot_screen{ - Settings::values.custom_bottom_left, Settings::values.custom_bottom_top, - Settings::values.custom_bottom_right, Settings::values.custom_bottom_bottom}; - - res.top_screen = top_screen; - res.bottom_screen = bot_screen; - return res; -} -} diff --git a/src/common/framebuffer_layout.h b/src/common/framebuffer_layout.h deleted file mode 100644 index f1df5c55a..000000000 --- a/src/common/framebuffer_layout.h +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include "common/math_util.h" -namespace Layout { -/// Describes the layout of the window framebuffer (size and top/bottom screen positions) -struct FramebufferLayout { - unsigned width; - unsigned height; - bool top_screen_enabled; - bool bottom_screen_enabled; - MathUtil::Rectangle<unsigned> top_screen; - MathUtil::Rectangle<unsigned> bottom_screen; -}; - -/** - * Factory method for constructing a default FramebufferLayout - * @param width Window framebuffer width in pixels - * @param height Window framebuffer height in pixels - * @param is_swapped if true, the bottom screen will be displayed above the top screen - * @return Newly created FramebufferLayout object with default screen regions initialized - */ -FramebufferLayout DefaultFrameLayout(unsigned width, unsigned height, bool is_swapped); - -/** - * Factory method for constructing a FramebufferLayout with only the top or bottom screen - * @param width Window framebuffer width in pixels - * @param height Window framebuffer height in pixels - * @param is_swapped if true, the bottom screen will be displayed (and the top won't be displayed) - * @return Newly created FramebufferLayout object with default screen regions initialized - */ -FramebufferLayout SingleFrameLayout(unsigned width, unsigned height, bool is_swapped); - -/** - * Factory method for constructing a Frame with the a 4x size Top screen with a 1x size bottom - * screen on the right - * This is useful in particular because it matches well with a 1920x1080 resolution monitor - * @param width Window framebuffer width in pixels - * @param height Window framebuffer height in pixels - * @param is_swapped if true, the bottom screen will be the large display - * @return Newly created FramebufferLayout object with default screen regions initialized - */ -FramebufferLayout LargeFrameLayout(unsigned width, unsigned height, bool is_swapped); - -/** - * Factory method for constructing a custom FramebufferLayout - * @param width Window framebuffer width in pixels - * @param height Window framebuffer height in pixels - * @return Newly created FramebufferLayout object with default screen regions initialized - */ -FramebufferLayout CustomFrameLayout(unsigned width, unsigned height); -} diff --git a/src/common/hash.cpp b/src/common/hash.cpp index f3d390dc5..a02e9e5b9 100644 --- a/src/common/hash.cpp +++ b/src/common/hash.cpp @@ -5,9 +5,9 @@ #if defined(_MSC_VER) #include <stdlib.h> #endif -#include "common_funcs.h" -#include "common_types.h" -#include "hash.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/hash.h" namespace Common { diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index 2cb3ab9cc..62f17fbb5 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp @@ -6,7 +6,7 @@ #include <string> #include <thread> #include "common/common_types.h" -#include "cpu_detect.h" +#include "common/x64/cpu_detect.h" #ifdef _MSC_VER #include <intrin.h> |