From 43ce33b6cced1d049f1cef3a9b1fddcfad8aef7c Mon Sep 17 00:00:00 2001 From: M&M Date: Wed, 29 Jul 2020 10:25:37 -0700 Subject: logging/settings: Increase maximum log size to 100 MB and add extended logging option The extended logging option is automatically disabled on boot but can be enabled afterwards, allowing the log file to go up to 1 GB during that session. This commit also fixes a few errors that are present in the general debug menu. --- src/common/logging/backend.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 62cfde397..fdb2e52fa 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -23,6 +23,7 @@ #include "common/logging/text_formatter.h" #include "common/string_util.h" #include "common/threadsafe_queue.h" +#include "core/settings.h" namespace Log { @@ -152,10 +153,19 @@ FileBackend::FileBackend(const std::string& filename) void FileBackend::Write(const Entry& entry) { // prevent logs from going over the maximum size (in case its spamming and the user doesn't // know) - constexpr std::size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L; - if (!file.IsOpen() || bytes_written > MAX_BYTES_WRITTEN) { + constexpr std::size_t MAX_BYTES_WRITTEN = 100 * 1024 * 1024; + constexpr std::size_t MAX_BYTES_WRITTEN_EXTENDED = 1024 * 1024 * 1024; + + if (!file.IsOpen()) { + return; + } + + if (Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN_EXTENDED) { + return; + } else if (!Settings::values.extended_logging && bytes_written > MAX_BYTES_WRITTEN) { return; } + bytes_written += file.WriteString(FormatLogMessage(entry).append(1, '\n')); if (entry.log_level >= Level::Error) { file.Flush(); -- cgit v1.2.3 From 9d665cb8dbc560e5f5492d34aeab4bf41b9353d3 Mon Sep 17 00:00:00 2001 From: lat9nq Date: Mon, 28 Sep 2020 20:28:47 -0400 Subject: CMakeLists: fix for finding zstd on linux-mingw --- src/common/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5d54516eb..ea0191c4b 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -192,4 +192,4 @@ create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) -target_link_libraries(common PRIVATE lz4::lz4 zstd::zstd xbyak) +target_link_libraries(common PRIVATE lz4::lz4 zstd xbyak) -- cgit v1.2.3 From 2cbce77b925c4320d90d5845563928a838c3372c Mon Sep 17 00:00:00 2001 From: lat9nq Date: Mon, 28 Sep 2020 21:11:39 -0400 Subject: CMakeLists: use system zstd on Linux From what I understand, this tells CMake to use the system, not conan, version of zstd. Required to build on the coming MinGW Docker container. --- src/common/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ea0191c4b..0fb5d9708 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -192,4 +192,9 @@ create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) -target_link_libraries(common PRIVATE lz4::lz4 zstd xbyak) +target_link_libraries(common PRIVATE lz4::lz4 xbyak) +if (MSVC) + target_link_libraries(common PRIVATE zstd::zstd) +else() + target_link_libraries(common PRIVATE zstd) +endif() -- cgit v1.2.3 From 771a9c21cc2f401cb9fd653cefcfe9da78b8f1a7 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Sep 2020 16:19:37 -0300 Subject: common/wall_clock: Add virtual destructors From -fsanitize=address, this code wasn't calling the proper destructor. Adding virtual destructors for each inherited class and the base class fixes this bug. While we are at it, mark the functions as final. --- src/common/wall_clock.cpp | 2 +- src/common/wall_clock.h | 2 ++ src/common/x64/native_clock.h | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 3afbdb898..7a20e95b7 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -15,7 +15,7 @@ namespace Common { using base_timer = std::chrono::steady_clock; using base_time_point = std::chrono::time_point; -class StandardWallClock : public WallClock { +class StandardWallClock final : public WallClock { public: StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index 5db30083d..bc7adfbf8 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h @@ -13,6 +13,8 @@ namespace Common { class WallClock { public: + virtual ~WallClock() = default; + /// Returns current wall time in nanoseconds [[nodiscard]] virtual std::chrono::nanoseconds GetTimeNS() = 0; diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 891a3bbfd..7c503df26 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -12,7 +12,7 @@ namespace Common { namespace X64 { -class NativeClock : public WallClock { +class NativeClock final : public WallClock { public: NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency); -- cgit v1.2.3 From 39c8d18feba8eafcd43fbb55e73ae150a1947aad Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 13 Oct 2020 08:10:50 -0400 Subject: core/CMakeLists: Make some warnings errors Makes our error coverage a little more consistent across the board by applying it to Linux side of things as well. This also makes it more consistent with the warning settings in other libraries in the project. This also updates httplib to 0.7.9, as there are several warning cleanups made that allow us to enable several warnings as errors. --- externals/CMakeLists.txt | 3 + externals/httplib/httplib.h | 4469 ++++++++++++++------ src/common/hex_util.h | 10 +- src/core/CMakeLists.txt | 11 + src/core/crypto/key_manager.cpp | 2 +- src/core/file_sys/fsmitm_romfsbuild.cpp | 10 +- src/core/file_sys/ips_layer.cpp | 2 +- src/core/file_sys/nca_metadata.cpp | 2 +- src/core/file_sys/patch_manager.cpp | 2 +- src/core/frontend/emu_window.cpp | 10 +- src/core/frontend/framebuffer_layout.cpp | 6 +- src/core/gdbstub/gdbstub.cpp | 30 +- src/core/hle/ipc_helpers.h | 19 +- src/core/hle/kernel/handle_table.cpp | 2 +- src/core/hle/kernel/scheduler.cpp | 10 +- src/core/hle/service/bcat/backend/boxcat.cpp | 10 +- src/core/hle/service/hid/controllers/keyboard.cpp | 4 +- src/core/hle/service/hid/controllers/npad.cpp | 25 - src/core/hle/service/hid/hid.cpp | 2 +- src/core/hle/service/mii/manager.cpp | 4 +- src/core/hle/service/sockets/sockets_translate.cpp | 16 +- src/core/hle/service/sockets/sockets_translate.h | 4 +- src/core/hle/service/time/time_zone_manager.cpp | 21 +- src/core/hle/service/vi/vi.cpp | 2 +- src/core/loader/kip.cpp | 2 +- src/core/loader/nro.cpp | 2 +- src/core/loader/nso.cpp | 2 +- src/core/loader/nso.h | 2 +- src/core/memory.cpp | 12 +- src/core/memory/cheat_engine.cpp | 3 +- src/core/network/network.cpp | 48 +- src/web_service/web_backend.cpp | 15 +- 32 files changed, 3233 insertions(+), 1529 deletions(-) (limited to 'src/common') diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt index d1dcc403b..e01ff6930 100644 --- a/externals/CMakeLists.txt +++ b/externals/CMakeLists.txt @@ -90,6 +90,9 @@ if (ENABLE_WEB_SERVICE) target_include_directories(httplib INTERFACE ./httplib) target_compile_definitions(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT) target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES}) + if (WIN32) + target_link_libraries(httplib INTERFACE crypt32 cryptui ws2_32) + endif() endif() # Opus diff --git a/externals/httplib/httplib.h b/externals/httplib/httplib.h index e03842e6d..5139b7f05 100644 --- a/externals/httplib/httplib.h +++ b/externals/httplib/httplib.h @@ -16,14 +16,18 @@ #define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 #endif -#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND -#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0 -#endif - #ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT #define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 #endif +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300 +#endif + +#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND +#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0 +#endif + #ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND #define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 #endif @@ -32,6 +36,26 @@ #define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 #endif +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5 +#endif + +#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND +#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND +#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0 +#endif + +#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND +#ifdef _WIN32 +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000 +#else +#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0 +#endif +#endif + #ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH #define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 #endif @@ -41,16 +65,26 @@ #endif #ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH -#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits::max()) +#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits::max)()) +#endif + +#ifndef CPPHTTPLIB_TCP_NODELAY +#define CPPHTTPLIB_TCP_NODELAY false #endif #ifndef CPPHTTPLIB_RECV_BUFSIZ #define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) #endif +#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ +#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u) +#endif + #ifndef CPPHTTPLIB_THREAD_POOL_COUNT #define CPPHTTPLIB_THREAD_POOL_COUNT \ - (std::max(1u, std::thread::hardware_concurrency() - 1)) + ((std::max)(8u, std::thread::hardware_concurrency() > 0 \ + ? std::thread::hardware_concurrency() - 1 \ + : 0)) #endif /* @@ -92,6 +126,8 @@ using ssize_t = int; #include #include + +#include #include #ifndef WSA_FLAG_NO_HANDLE_INHERIT @@ -100,6 +136,8 @@ using ssize_t = int; #ifdef _MSC_VER #pragma comment(lib, "ws2_32.lib") +#pragma comment(lib, "crypt32.lib") +#pragma comment(lib, "cryptui.lib") #endif #ifndef strcasecmp @@ -118,6 +156,10 @@ using socket_t = SOCKET; #include #include #include +#ifdef __linux__ +#include +#endif +#include #ifdef CPPHTTPLIB_USE_POLL #include #endif @@ -134,17 +176,21 @@ using socket_t = int; #include #include #include +#include +#include #include #include #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -155,12 +201,17 @@ using socket_t = int; #include #include +#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK) +#include +#endif + #include +#include #include -// #if OPENSSL_VERSION_NUMBER < 0x1010100fL -// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported -// #endif +#if OPENSSL_VERSION_NUMBER < 0x1010100fL +#error Sorry, OpenSSL versions prior to 1.1.1 are not supported +#endif #if OPENSSL_VERSION_NUMBER < 0x10100000L #include @@ -174,6 +225,11 @@ inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) { #include #endif +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +#include +#include +#endif + /* * Declaration */ @@ -212,7 +268,8 @@ using MultipartFormDataMap = std::multimap; class DataSink { public: - DataSink() = default; + DataSink() : os(&sb_), sb_(*this) {} + DataSink(const DataSink &) = delete; DataSink &operator=(const DataSink &) = delete; DataSink(DataSink &&) = delete; @@ -221,10 +278,31 @@ public: std::function write; std::function done; std::function is_writable; + std::ostream os; + +private: + class data_sink_streambuf : public std::streambuf { + public: + explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {} + + protected: + std::streamsize xsputn(const char *s, std::streamsize n) { + sink_.write(s, static_cast(n)); + return n; + } + + private: + DataSink &sink_; + }; + + data_sink_streambuf sb_; }; using ContentProvider = - std::function; + std::function; + +using ContentProviderWithoutLength = + std::function; using ContentReceiver = std::function; @@ -238,18 +316,18 @@ public: using MultipartReader = std::function; - ContentReader(Reader reader, MultipartReader muitlpart_reader) - : reader_(reader), muitlpart_reader_(muitlpart_reader) {} + ContentReader(Reader reader, MultipartReader multipart_reader) + : reader_(reader), multipart_reader_(multipart_reader) {} bool operator()(MultipartContentHeader header, ContentReceiver receiver) const { - return muitlpart_reader_(header, receiver); + return multipart_reader_(header, receiver); } bool operator()(ContentReceiver receiver) const { return reader_(receiver); } Reader reader_; - MultipartReader muitlpart_reader_; + MultipartReader multipart_reader_; }; using Range = std::pair; @@ -261,6 +339,9 @@ struct Request { Headers headers; std::string body; + std::string remote_addr; + int remote_port = -1; + // for server std::string version; std::string target; @@ -273,6 +354,8 @@ struct Request { size_t redirect_count = CPPHTTPLIB_REDIRECT_MAX_COUNT; ResponseHandler response_handler; ContentReceiver content_receiver; + size_t content_length = 0; + ContentProvider content_provider; Progress progress; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -281,6 +364,8 @@ struct Request { bool has_header(const char *key) const; std::string get_header_value(const char *key, size_t id = 0) const; + template + T get_header_value(const char *key, size_t id = 0) const; size_t get_header_value_count(const char *key) const; void set_header(const char *key, const char *val); void set_header(const char *key, const std::string &val); @@ -295,35 +380,40 @@ struct Request { MultipartFormData get_file_value(const char *key) const; // private members... - size_t content_length; - ContentProvider content_provider; + size_t authorization_count_ = 0; }; struct Response { std::string version; int status = -1; + std::string reason; Headers headers; std::string body; bool has_header(const char *key) const; std::string get_header_value(const char *key, size_t id = 0) const; + template + T get_header_value(const char *key, size_t id = 0) const; size_t get_header_value_count(const char *key) const; void set_header(const char *key, const char *val); void set_header(const char *key, const std::string &val); - void set_redirect(const char *url); + void set_redirect(const char *url, int status = 302); + void set_redirect(const std::string &url, int status = 302); void set_content(const char *s, size_t n, const char *content_type); - void set_content(const std::string &s, const char *content_type); + void set_content(std::string s, const char *content_type); + + void set_content_provider( + size_t length, const char *content_type, ContentProvider provider, + const std::function &resource_releaser = nullptr); void set_content_provider( - size_t length, - std::function - provider, - std::function resource_releaser = [] {}); + const char *content_type, ContentProviderWithoutLength provider, + const std::function &resource_releaser = nullptr); void set_chunked_content_provider( - std::function provider, - std::function resource_releaser = [] {}); + const char *content_type, ContentProviderWithoutLength provider, + const std::function &resource_releaser = nullptr); Response() = default; Response(const Response &) = default; @@ -331,15 +421,16 @@ struct Response { Response(Response &&) = default; Response &operator=(Response &&) = default; ~Response() { - if (content_provider_resource_releaser) { - content_provider_resource_releaser(); + if (content_provider_resource_releaser_) { + content_provider_resource_releaser_(); } } // private members... - size_t content_length = 0; - ContentProvider content_provider; - std::function content_provider_resource_releaser; + size_t content_length_ = 0; + ContentProvider content_provider_; + std::function content_provider_resource_releaser_; + bool is_chunked_content_provider = false; }; class Stream { @@ -349,22 +440,25 @@ public: virtual bool is_readable() const = 0; virtual bool is_writable() const = 0; - virtual int read(char *ptr, size_t size) = 0; - virtual int write(const char *ptr, size_t size) = 0; - virtual std::string get_remote_addr() const = 0; + virtual ssize_t read(char *ptr, size_t size) = 0; + virtual ssize_t write(const char *ptr, size_t size) = 0; + virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0; template - int write_format(const char *fmt, const Args &... args); - int write(const char *ptr); - int write(const std::string &s); + ssize_t write_format(const char *fmt, const Args &... args); + ssize_t write(const char *ptr); + ssize_t write(const std::string &s); }; class TaskQueue { public: TaskQueue() = default; virtual ~TaskQueue() = default; + virtual void enqueue(std::function fn) = 0; virtual void shutdown() = 0; + + virtual void on_idle(){}; }; class ThreadPool : public TaskQueue { @@ -439,6 +533,26 @@ private: using Logger = std::function; +using SocketOptions = std::function; + +inline void default_socket_options(socket_t sock) { + int yes = 1; +#ifdef _WIN32 + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); + setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, + reinterpret_cast(&yes), sizeof(yes)); +#else +#ifdef SO_REUSEPORT + setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), + sizeof(yes)); +#else + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), + sizeof(yes)); +#endif +#endif +} + class Server { public: using Handler = std::function; @@ -461,23 +575,30 @@ public: Server &Patch(const char *pattern, Handler handler); Server &Patch(const char *pattern, HandlerWithContentReader handler); Server &Delete(const char *pattern, Handler handler); + Server &Delete(const char *pattern, HandlerWithContentReader handler); Server &Options(const char *pattern, Handler handler); - [[deprecated]] bool set_base_dir(const char *dir, - const char *mount_point = nullptr); - bool set_mount_point(const char *mount_point, const char *dir); + bool set_base_dir(const char *dir, const char *mount_point = nullptr); + bool set_mount_point(const char *mount_point, const char *dir, + Headers headers = Headers()); bool remove_mount_point(const char *mount_point); void set_file_extension_and_mimetype_mapping(const char *ext, const char *mime); void set_file_request_handler(Handler handler); void set_error_handler(Handler handler); + void set_expect_100_continue_handler(Expect100ContinueHandler handler); void set_logger(Logger logger); - void set_expect_100_continue_handler(Expect100ContinueHandler handler); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); void set_keep_alive_max_count(size_t count); - void set_read_timeout(time_t sec, time_t usec); + void set_keep_alive_timeout(time_t sec); + void set_read_timeout(time_t sec, time_t usec = 0); + void set_write_timeout(time_t sec, time_t usec = 0); + void set_idle_interval(time_t sec, time_t usec = 0); + void set_payload_max_length(size_t length); bool bind_to_port(const char *host, int port, int socket_flags = 0); @@ -492,54 +613,66 @@ public: std::function new_task_queue; protected: - bool process_request(Stream &strm, bool last_connection, - bool &connection_close, + bool process_request(Stream &strm, bool close_connection, + bool &connection_closed, const std::function &setup_request); - size_t keep_alive_max_count_; - time_t read_timeout_sec_; - time_t read_timeout_usec_; - size_t payload_max_length_; + std::atomic svr_sock_; + size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND; + time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; + time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; + time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND; + time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND; + size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH; private: using Handlers = std::vector>; using HandlersForContentReader = std::vector>; - socket_t create_server_socket(const char *host, int port, - int socket_flags) const; + socket_t create_server_socket(const char *host, int port, int socket_flags, + SocketOptions socket_options) const; int bind_internal(const char *host, int port, int socket_flags); bool listen_internal(); - bool routing(Request &req, Response &res, Stream &strm, bool last_connection); + bool routing(Request &req, Response &res, Stream &strm); bool handle_file_request(Request &req, Response &res, bool head = false); - bool dispatch_request(Request &req, Response &res, Handlers &handlers); - bool dispatch_request_for_content_reader(Request &req, Response &res, - ContentReader content_reader, - HandlersForContentReader &handlers); + bool dispatch_request(Request &req, Response &res, const Handlers &handlers); + bool + dispatch_request_for_content_reader(Request &req, Response &res, + ContentReader content_reader, + const HandlersForContentReader &handlers); bool parse_request_line(const char *s, Request &req); - bool write_response(Stream &strm, bool last_connection, const Request &req, + bool write_response(Stream &strm, bool close_connection, const Request &req, Response &res); bool write_content_with_provider(Stream &strm, const Request &req, Response &res, const std::string &boundary, const std::string &content_type); - bool read_content(Stream &strm, bool last_connection, Request &req, - Response &res); - bool read_content_with_content_receiver( - Stream &strm, bool last_connection, Request &req, Response &res, - ContentReceiver receiver, MultipartContentHeader multipart_header, - ContentReceiver multipart_receiver); - bool read_content_core(Stream &strm, bool last_connection, Request &req, - Response &res, ContentReceiver receiver, + bool read_content(Stream &strm, Request &req, Response &res); + bool + read_content_with_content_receiver(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, + MultipartContentHeader multipart_header, + ContentReceiver multipart_receiver); + bool read_content_core(Stream &strm, Request &req, Response &res, + ContentReceiver receiver, MultipartContentHeader mulitpart_header, ContentReceiver multipart_receiver); virtual bool process_and_close_socket(socket_t sock); + + struct MountPointEntry { + std::string mount_point; + std::string base_dir; + Headers headers; + }; + std::vector base_dirs_; std::atomic is_running_; - std::atomic svr_sock_; - std::vector> base_dirs_; std::map file_extension_and_mimetype_map_; Handler file_request_handler_; Handlers get_handlers_; @@ -550,293 +683,427 @@ private: Handlers patch_handlers_; HandlersForContentReader patch_handlers_for_content_reader_; Handlers delete_handlers_; + HandlersForContentReader delete_handlers_for_content_reader_; Handlers options_handlers_; Handler error_handler_; Logger logger_; Expect100ContinueHandler expect_100_continue_handler_; -}; - -class Client { -public: - explicit Client(const std::string &host, int port = 80, - const std::string &client_cert_path = std::string(), - const std::string &client_key_path = std::string()); - - virtual ~Client(); - - virtual bool is_valid() const; - - std::shared_ptr Get(const char *path); - - std::shared_ptr Get(const char *path, const Headers &headers); - - std::shared_ptr Get(const char *path, Progress progress); - - std::shared_ptr Get(const char *path, const Headers &headers, - Progress progress); - - std::shared_ptr Get(const char *path, - ContentReceiver content_receiver); - - std::shared_ptr Get(const char *path, const Headers &headers, - ContentReceiver content_receiver); - - std::shared_ptr - Get(const char *path, ContentReceiver content_receiver, Progress progress); - - std::shared_ptr Get(const char *path, const Headers &headers, - ContentReceiver content_receiver, - Progress progress); - - std::shared_ptr Get(const char *path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver); - - std::shared_ptr Get(const char *path, const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress); - - std::shared_ptr Head(const char *path); - - std::shared_ptr Head(const char *path, const Headers &headers); - - std::shared_ptr Post(const char *path, const std::string &body, - const char *content_type); - - std::shared_ptr Post(const char *path, const Headers &headers, - const std::string &body, - const char *content_type); - - std::shared_ptr Post(const char *path, size_t content_length, - ContentProvider content_provider, - const char *content_type); - - std::shared_ptr Post(const char *path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const char *content_type); - - std::shared_ptr Post(const char *path, const Params ¶ms); - - std::shared_ptr Post(const char *path, const Headers &headers, - const Params ¶ms); - - std::shared_ptr Post(const char *path, - const MultipartFormDataItems &items); - std::shared_ptr Post(const char *path, const Headers &headers, - const MultipartFormDataItems &items); - - std::shared_ptr Put(const char *path, const std::string &body, - const char *content_type); - - std::shared_ptr Put(const char *path, const Headers &headers, - const std::string &body, - const char *content_type); - - std::shared_ptr Put(const char *path, size_t content_length, - ContentProvider content_provider, - const char *content_type); - - std::shared_ptr Put(const char *path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const char *content_type); - - std::shared_ptr Put(const char *path, const Params ¶ms); - - std::shared_ptr Put(const char *path, const Headers &headers, - const Params ¶ms); - - std::shared_ptr Patch(const char *path, const std::string &body, - const char *content_type); + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + SocketOptions socket_options_ = default_socket_options; +}; - std::shared_ptr Patch(const char *path, const Headers &headers, - const std::string &body, - const char *content_type); +enum Error { + Success = 0, + Unknown, + Connection, + BindIPAddress, + Read, + Write, + ExceedRedirectCount, + Canceled, + SSLConnection, + SSLLoadingCerts, + SSLServerVerification +}; - std::shared_ptr Patch(const char *path, size_t content_length, - ContentProvider content_provider, - const char *content_type); +class Result { +public: + Result(const std::shared_ptr &res, Error err) + : res_(res), err_(err) {} + operator bool() const { return res_ != nullptr; } + bool operator==(std::nullptr_t) const { return res_ == nullptr; } + bool operator!=(std::nullptr_t) const { return res_ != nullptr; } + const Response &value() const { return *res_; } + const Response &operator*() const { return *res_; } + const Response *operator->() const { return res_.get(); } + Error error() const { return err_; } - std::shared_ptr Patch(const char *path, const Headers &headers, - size_t content_length, - ContentProvider content_provider, - const char *content_type); +private: + std::shared_ptr res_; + Error err_; +}; - std::shared_ptr Delete(const char *path); +class ClientImpl { +public: + explicit ClientImpl(const std::string &host); - std::shared_ptr Delete(const char *path, const std::string &body, - const char *content_type); + explicit ClientImpl(const std::string &host, int port); - std::shared_ptr Delete(const char *path, const Headers &headers); + explicit ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); - std::shared_ptr Delete(const char *path, const Headers &headers, - const std::string &body, - const char *content_type); + virtual ~ClientImpl(); - std::shared_ptr Options(const char *path); + virtual bool is_valid() const; - std::shared_ptr Options(const char *path, const Headers &headers); + Result Get(const char *path); + Result Get(const char *path, const Headers &headers); + Result Get(const char *path, Progress progress); + Result Get(const char *path, const Headers &headers, Progress progress); + Result Get(const char *path, ContentReceiver content_receiver); + Result Get(const char *path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const char *path, ContentReceiver content_receiver, + Progress progress); + Result Get(const char *path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + Result Get(const char *path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + + Result Head(const char *path); + Result Head(const char *path, const Headers &headers); + + Result Post(const char *path); + Result Post(const char *path, const std::string &body, + const char *content_type); + Result Post(const char *path, const Headers &headers, const std::string &body, + const char *content_type); + Result Post(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Post(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Post(const char *path, const Params ¶ms); + Result Post(const char *path, const Headers &headers, const Params ¶ms); + Result Post(const char *path, const MultipartFormDataItems &items); + Result Post(const char *path, const Headers &headers, + const MultipartFormDataItems &items); + + Result Put(const char *path); + Result Put(const char *path, const std::string &body, + const char *content_type); + Result Put(const char *path, const Headers &headers, const std::string &body, + const char *content_type); + Result Put(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Put(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Put(const char *path, const Params ¶ms); + Result Put(const char *path, const Headers &headers, const Params ¶ms); + + Result Patch(const char *path, const std::string &body, + const char *content_type); + Result Patch(const char *path, const Headers &headers, + const std::string &body, const char *content_type); + Result Patch(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Patch(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + + Result Delete(const char *path); + Result Delete(const char *path, const std::string &body, + const char *content_type); + Result Delete(const char *path, const Headers &headers); + Result Delete(const char *path, const Headers &headers, + const std::string &body, const char *content_type); + + Result Options(const char *path); + Result Options(const char *path, const Headers &headers); bool send(const Request &req, Response &res); - bool send(const std::vector &requests, - std::vector &responses); + size_t is_socket_open() const; + + void stop(); - void set_timeout_sec(time_t timeout_sec); + void set_default_headers(Headers headers); - void set_read_timeout(time_t sec, time_t usec); + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); - void set_keep_alive_max_count(size_t count); + void set_connection_timeout(time_t sec, time_t usec = 0); + void set_read_timeout(time_t sec, time_t usec = 0); + void set_write_timeout(time_t sec, time_t usec = 0); void set_basic_auth(const char *username, const char *password); - + void set_bearer_token_auth(const char *token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void set_digest_auth(const char *username, const char *password); #endif + void set_keep_alive(bool on); void set_follow_location(bool on); void set_compress(bool on); + void set_decompress(bool on); + void set_interface(const char *intf); void set_proxy(const char *host, int port); - void set_proxy_basic_auth(const char *username, const char *password); - + void set_proxy_bearer_token_auth(const char *token); #ifdef CPPHTTPLIB_OPENSSL_SUPPORT void set_proxy_digest_auth(const char *username, const char *password); #endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + void set_logger(Logger logger); protected: + struct Socket { + socket_t sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + SSL *ssl = nullptr; +#endif + + bool is_open() const { return sock != INVALID_SOCKET; } + }; + + virtual bool create_and_connect_socket(Socket &socket); + virtual void close_socket(Socket &socket, bool process_socket_ret); + bool process_request(Stream &strm, const Request &req, Response &res, - bool last_connection, bool &connection_close); + bool close_connection); + Error get_last_error() const; + + void copy_settings(const ClientImpl &rhs); + + // Error state + mutable Error error_ = Error::Success; + + // Socket endoint information const std::string host_; const int port_; const std::string host_and_port_; + // Current open socket + Socket socket_; + mutable std::mutex socket_mutex_; + std::recursive_mutex request_mutex_; + + // Default headers + Headers default_headers_; + // Settings std::string client_cert_path_; std::string client_key_path_; - time_t timeout_sec_ = 300; + time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND; + time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND; time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; - - size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; + time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND; + time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND; std::string basic_auth_username_; std::string basic_auth_password_; + std::string bearer_token_auth_token_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT std::string digest_auth_username_; std::string digest_auth_password_; #endif + bool keep_alive_ = false; bool follow_location_ = false; + bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY; + SocketOptions socket_options_ = nullptr; + bool compress_ = false; + bool decompress_ = true; std::string interface_; std::string proxy_host_; - int proxy_port_; + int proxy_port_ = -1; std::string proxy_basic_auth_username_; std::string proxy_basic_auth_password_; + std::string proxy_bearer_token_auth_token_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT std::string proxy_digest_auth_username_; std::string proxy_digest_auth_password_; #endif - Logger logger_; - - void copy_settings(const Client &rhs) { - client_cert_path_ = rhs.client_cert_path_; - client_key_path_ = rhs.client_key_path_; - timeout_sec_ = rhs.timeout_sec_; - read_timeout_sec_ = rhs.read_timeout_sec_; - read_timeout_usec_ = rhs.read_timeout_usec_; - keep_alive_max_count_ = rhs.keep_alive_max_count_; - basic_auth_username_ = rhs.basic_auth_username_; - basic_auth_password_ = rhs.basic_auth_password_; -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - digest_auth_username_ = rhs.digest_auth_username_; - digest_auth_password_ = rhs.digest_auth_password_; -#endif - follow_location_ = rhs.follow_location_; - compress_ = rhs.compress_; - interface_ = rhs.interface_; - proxy_host_ = rhs.proxy_host_; - proxy_port_ = rhs.proxy_port_; - proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; - proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; - proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; + bool server_certificate_verification_ = true; #endif - logger_ = rhs.logger_; - } + + Logger logger_; private: socket_t create_client_socket() const; bool read_response_line(Stream &strm, Response &res); - bool write_request(Stream &strm, const Request &req, bool last_connection); + bool write_request(Stream &strm, const Request &req, bool close_connection); bool redirect(const Request &req, Response &res); bool handle_request(Stream &strm, const Request &req, Response &res, - bool last_connection, bool &connection_close); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - bool connect(socket_t sock, Response &res, bool &error); -#endif - + bool close_connection); + void stop_core(); std::shared_ptr send_with_content_provider( const char *method, const char *path, const Headers &headers, const std::string &body, size_t content_length, ContentProvider content_provider, const char *content_type); - virtual bool process_and_close_socket( - socket_t sock, size_t request_count, - std::function - callback); - + virtual bool process_socket(Socket &socket, + std::function callback); virtual bool is_ssl() const; }; -inline void Get(std::vector &requests, const char *path, - const Headers &headers) { - Request req; - req.method = "GET"; - req.path = path; - req.headers = headers; - requests.emplace_back(std::move(req)); -} +class Client { +public: + // Universal interface + explicit Client(const char *scheme_host_port); + + explicit Client(const char *scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path); + + // HTTP only interface + explicit Client(const std::string &host, int port); + + explicit Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + ~Client(); + + bool is_valid() const; + + Result Get(const char *path); + Result Get(const char *path, const Headers &headers); + Result Get(const char *path, Progress progress); + Result Get(const char *path, const Headers &headers, Progress progress); + Result Get(const char *path, ContentReceiver content_receiver); + Result Get(const char *path, const Headers &headers, + ContentReceiver content_receiver); + Result Get(const char *path, ContentReceiver content_receiver, + Progress progress); + Result Get(const char *path, const Headers &headers, + ContentReceiver content_receiver, Progress progress); + Result Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver); + Result Get(const char *path, const Headers &headers, + ResponseHandler response_handler, ContentReceiver content_receiver, + Progress progress); + Result Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress); + + Result Head(const char *path); + Result Head(const char *path, const Headers &headers); + + Result Post(const char *path); + Result Post(const char *path, const std::string &body, + const char *content_type); + Result Post(const char *path, const Headers &headers, const std::string &body, + const char *content_type); + Result Post(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Post(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Post(const char *path, const Params ¶ms); + Result Post(const char *path, const Headers &headers, const Params ¶ms); + Result Post(const char *path, const MultipartFormDataItems &items); + Result Post(const char *path, const Headers &headers, + const MultipartFormDataItems &items); + Result Put(const char *path); + Result Put(const char *path, const std::string &body, + const char *content_type); + Result Put(const char *path, const Headers &headers, const std::string &body, + const char *content_type); + Result Put(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Put(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Put(const char *path, const Params ¶ms); + Result Put(const char *path, const Headers &headers, const Params ¶ms); + Result Patch(const char *path, const std::string &body, + const char *content_type); + Result Patch(const char *path, const Headers &headers, + const std::string &body, const char *content_type); + Result Patch(const char *path, size_t content_length, + ContentProvider content_provider, const char *content_type); + Result Patch(const char *path, const Headers &headers, size_t content_length, + ContentProvider content_provider, const char *content_type); + + Result Delete(const char *path); + Result Delete(const char *path, const std::string &body, + const char *content_type); + Result Delete(const char *path, const Headers &headers); + Result Delete(const char *path, const Headers &headers, + const std::string &body, const char *content_type); + + Result Options(const char *path); + Result Options(const char *path, const Headers &headers); -inline void Get(std::vector &requests, const char *path) { - Get(requests, path, Headers()); -} + bool send(const Request &req, Response &res); -inline void Post(std::vector &requests, const char *path, - const Headers &headers, const std::string &body, - const char *content_type) { - Request req; - req.method = "POST"; - req.path = path; - req.headers = headers; - req.headers.emplace("Content-Type", content_type); - req.body = body; - requests.emplace_back(std::move(req)); -} + size_t is_socket_open() const; -inline void Post(std::vector &requests, const char *path, - const std::string &body, const char *content_type) { - Post(requests, path, Headers(), body, content_type); -} + void stop(); + + void set_default_headers(Headers headers); + + void set_tcp_nodelay(bool on); + void set_socket_options(SocketOptions socket_options); + + void set_connection_timeout(time_t sec, time_t usec = 0); + void set_read_timeout(time_t sec, time_t usec = 0); + void set_write_timeout(time_t sec, time_t usec = 0); + + void set_basic_auth(const char *username, const char *password); + void set_bearer_token_auth(const char *token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_digest_auth(const char *username, const char *password); +#endif + + void set_keep_alive(bool on); + void set_follow_location(bool on); + + void set_compress(bool on); + + void set_decompress(bool on); + + void set_interface(const char *intf); + + void set_proxy(const char *host, int port); + void set_proxy_basic_auth(const char *username, const char *password); + void set_proxy_bearer_token_auth(const char *token); +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_proxy_digest_auth(const char *username, const char *password); +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void enable_server_certificate_verification(bool enabled); +#endif + + void set_logger(Logger logger); + + // SSL +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + void set_ca_cert_path(const char *ca_cert_file_path, + const char *ca_cert_dir_path = nullptr); + + void set_ca_cert_store(X509_STORE *ca_cert_store); + + long get_openssl_verify_result() const; + + SSL_CTX *ssl_context() const; +#endif + +private: + std::shared_ptr cli_; + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + bool is_ssl_ = false; +#endif +}; // namespace httplib #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLServer : public Server { @@ -845,43 +1112,58 @@ public: const char *client_ca_cert_file_path = nullptr, const char *client_ca_cert_dir_path = nullptr); - virtual ~SSLServer(); + SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store = nullptr); - virtual bool is_valid() const; + ~SSLServer() override; + + bool is_valid() const override; private: - virtual bool process_and_close_socket(socket_t sock); + bool process_and_close_socket(socket_t sock) override; SSL_CTX *ctx_; std::mutex ctx_mutex_; }; -class SSLClient : public Client { +class SSLClient : public ClientImpl { public: - SSLClient(const std::string &host, int port = 443, - const std::string &client_cert_path = std::string(), - const std::string &client_key_path = std::string()); + explicit SSLClient(const std::string &host); - virtual ~SSLClient(); + explicit SSLClient(const std::string &host, int port); - virtual bool is_valid() const; + explicit SSLClient(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path); + + explicit SSLClient(const std::string &host, int port, X509 *client_cert, + EVP_PKEY *client_key); + + ~SSLClient() override; + + bool is_valid() const override; - void set_ca_cert_path(const char *ca_ceert_file_path, + void set_ca_cert_path(const char *ca_cert_file_path, const char *ca_cert_dir_path = nullptr); - void enable_server_certificate_verification(bool enabled); + void set_ca_cert_store(X509_STORE *ca_cert_store); long get_openssl_verify_result() const; - SSL_CTX *ssl_context() const noexcept; + SSL_CTX *ssl_context() const; private: - virtual bool process_and_close_socket( - socket_t sock, size_t request_count, - std::function - callback); - virtual bool is_ssl() const; + bool create_and_connect_socket(Socket &socket) override; + void close_socket(Socket &socket, bool process_socket_ret) override; + + bool process_socket(Socket &socket, + std::function callback) override; + bool is_ssl() const override; + + bool connect_with_proxy(Socket &sock, Response &res, bool &success); + bool initialize_ssl(Socket &socket); + + bool load_certs(); bool verify_host(X509 *server_cert) const; bool verify_host_with_subject_alt_name(X509 *server_cert) const; @@ -890,12 +1172,15 @@ private: SSL_CTX *ctx_; std::mutex ctx_mutex_; + std::once_flag initialize_cert_; + std::vector host_components_; std::string ca_cert_file_path_; std::string ca_cert_dir_path_; - bool server_certificate_verification_ = false; long verify_result_ = 0; + + friend class ClientImpl; }; #endif @@ -948,31 +1233,39 @@ inline std::string from_i_to_hex(size_t n) { return ret; } +inline bool start_with(const std::string &a, const std::string &b) { + if (a.size() < b.size()) { return false; } + for (size_t i = 0; i < b.size(); i++) { + if (::tolower(a[i]) != ::tolower(b[i])) { return false; } + } + return true; +} + inline size_t to_utf8(int code, char *buff) { if (code < 0x0080) { buff[0] = (code & 0x7F); return 1; } else if (code < 0x0800) { - buff[0] = (0xC0 | ((code >> 6) & 0x1F)); - buff[1] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xC0 | ((code >> 6) & 0x1F)); + buff[1] = static_cast(0x80 | (code & 0x3F)); return 2; } else if (code < 0xD800) { - buff[0] = (0xE0 | ((code >> 12) & 0xF)); - buff[1] = (0x80 | ((code >> 6) & 0x3F)); - buff[2] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); return 3; } else if (code < 0xE000) { // D800 - DFFF is invalid... return 0; } else if (code < 0x10000) { - buff[0] = (0xE0 | ((code >> 12) & 0xF)); - buff[1] = (0x80 | ((code >> 6) & 0x3F)); - buff[2] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xE0 | ((code >> 12) & 0xF)); + buff[1] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[2] = static_cast(0x80 | (code & 0x3F)); return 3; } else if (code < 0x110000) { - buff[0] = (0xF0 | ((code >> 18) & 0x7)); - buff[1] = (0x80 | ((code >> 12) & 0x3F)); - buff[2] = (0x80 | ((code >> 6) & 0x3F)); - buff[3] = (0x80 | (code & 0x3F)); + buff[0] = static_cast(0xF0 | ((code >> 18) & 0x7)); + buff[1] = static_cast(0x80 | ((code >> 12) & 0x3F)); + buff[2] = static_cast(0x80 | ((code >> 6) & 0x3F)); + buff[3] = static_cast(0x80 | (code & 0x3F)); return 4; } @@ -992,8 +1285,8 @@ inline std::string base64_encode(const std::string &in) { int val = 0; int valb = -6; - for (uint8_t c : in) { - val = (val << 8) + c; + for (auto c : in) { + val = (val << 8) + static_cast(c); valb += 8; while (valb >= 0) { out.push_back(lookup[(val >> valb) & 0x3F]); @@ -1057,35 +1350,125 @@ inline bool is_valid_path(const std::string &path) { return true; } -inline void read_file(const std::string &path, std::string &out) { - std::ifstream fs(path, std::ios_base::binary); - fs.seekg(0, std::ios_base::end); - auto size = fs.tellg(); - fs.seekg(0); - out.resize(static_cast(size)); - fs.read(&out[0], size); +inline std::string encode_url(const std::string &s) { + std::string result; + + for (size_t i = 0; s[i]; i++) { + switch (s[i]) { + case ' ': result += "%20"; break; + case '+': result += "%2B"; break; + case '\r': result += "%0D"; break; + case '\n': result += "%0A"; break; + case '\'': result += "%27"; break; + case ',': result += "%2C"; break; + // case ':': result += "%3A"; break; // ok? probably... + case ';': result += "%3B"; break; + default: + auto c = static_cast(s[i]); + if (c >= 0x80) { + result += '%'; + char hex[4]; + auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c); + assert(len == 2); + result.append(hex, static_cast(len)); + } else { + result += s[i]; + } + break; + } + } + + return result; } -inline std::string file_extension(const std::string &path) { +inline std::string decode_url(const std::string &s, + bool convert_plus_to_space) { + std::string result; + + for (size_t i = 0; i < s.size(); i++) { + if (s[i] == '%' && i + 1 < s.size()) { + if (s[i + 1] == 'u') { + int val = 0; + if (from_hex_to_i(s, i + 2, 4, val)) { + // 4 digits Unicode codes + char buff[4]; + size_t len = to_utf8(val, buff); + if (len > 0) { result.append(buff, len); } + i += 5; // 'u0000' + } else { + result += s[i]; + } + } else { + int val = 0; + if (from_hex_to_i(s, i + 1, 2, val)) { + // 2 digits hex codes + result += static_cast(val); + i += 2; // '00' + } else { + result += s[i]; + } + } + } else if (convert_plus_to_space && s[i] == '+') { + result += ' '; + } else { + result += s[i]; + } + } + + return result; +} + +inline void read_file(const std::string &path, std::string &out) { + std::ifstream fs(path, std::ios_base::binary); + fs.seekg(0, std::ios_base::end); + auto size = fs.tellg(); + fs.seekg(0); + out.resize(static_cast(size)); + fs.read(&out[0], static_cast(size)); +} + +inline std::string file_extension(const std::string &path) { std::smatch m; static auto re = std::regex("\\.([a-zA-Z0-9]+)$"); if (std::regex_search(path, m, re)) { return m[1].str(); } return std::string(); } +inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; } + +inline std::pair trim(const char *b, const char *e, size_t left, + size_t right) { + while (b + left < e && is_space_or_tab(b[left])) { + left++; + } + while (right > 0 && is_space_or_tab(b[right - 1])) { + right--; + } + return std::make_pair(left, right); +} + +inline std::string trim_copy(const std::string &s) { + auto r = trim(s.data(), s.data() + s.size(), 0, s.size()); + return s.substr(r.first, r.second - r.first); +} + template void split(const char *b, const char *e, char d, Fn fn) { - int i = 0; - int beg = 0; + size_t i = 0; + size_t beg = 0; - while (e ? (b + i != e) : (b[i] != '\0')) { + while (e ? (b + i < e) : (b[i] != '\0')) { if (b[i] == d) { - fn(&b[beg], &b[i]); + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } beg = i + 1; } i++; } - if (i) { fn(&b[beg], &b[i]); } + if (i) { + auto r = trim(b, e, beg, i); + if (r.first < r.second) { fn(&b[r.first], &b[r.second]); } + } } // NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` @@ -1172,7 +1555,17 @@ inline int close_socket(socket_t sock) { #endif } -inline int select_read(socket_t sock, time_t sec, time_t usec) { +template inline ssize_t handle_EINTR(T fn) { + ssize_t res = false; + while (true) { + res = fn(); + if (res < 0 && errno == EINTR) { continue; } + break; + } + return res; +} + +inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) { #ifdef CPPHTTPLIB_USE_POLL struct pollfd pfd_read; pfd_read.fd = sock; @@ -1180,7 +1573,7 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); - return poll(&pfd_read, 1, timeout); + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else fd_set fds; FD_ZERO(&fds); @@ -1188,13 +1581,15 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) { timeval tv; tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); + tv.tv_usec = static_cast(usec); - return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); + return handle_EINTR([&]() { + return select(static_cast(sock + 1), &fds, nullptr, nullptr, &tv); + }); #endif } -inline int select_write(socket_t sock, time_t sec, time_t usec) { +inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) { #ifdef CPPHTTPLIB_USE_POLL struct pollfd pfd_read; pfd_read.fd = sock; @@ -1202,7 +1597,7 @@ inline int select_write(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); - return poll(&pfd_read, 1, timeout); + return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); #else fd_set fds; FD_ZERO(&fds); @@ -1210,9 +1605,11 @@ inline int select_write(socket_t sock, time_t sec, time_t usec) { timeval tv; tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); + tv.tv_usec = static_cast(usec); - return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); + return handle_EINTR([&]() { + return select(static_cast(sock + 1), nullptr, &fds, nullptr, &tv); + }); #endif } @@ -1224,13 +1621,14 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { auto timeout = static_cast(sec * 1000 + usec / 1000); - if (poll(&pfd_read, 1, timeout) > 0 && - pfd_read.revents & (POLLIN | POLLOUT)) { + auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); }); + + if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) { int error = 0; socklen_t len = sizeof(error); - return getsockopt(sock, SOL_SOCKET, SO_ERROR, - reinterpret_cast(&error), &len) >= 0 && - !error; + auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR, + reinterpret_cast(&error), &len); + return res >= 0 && !error; } return false; #else @@ -1243,10 +1641,13 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { timeval tv; tv.tv_sec = static_cast(sec); - tv.tv_usec = static_cast(usec); + tv.tv_usec = static_cast(usec); + + auto ret = handle_EINTR([&]() { + return select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv); + }); - if (select(static_cast(sock + 1), &fdsr, &fdsw, &fdse, &tv) > 0 && - (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { + if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { int error = 0; socklen_t len = sizeof(error); return getsockopt(sock, SOL_SOCKET, SO_ERROR, @@ -1259,40 +1660,45 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { class SocketStream : public Stream { public: - SocketStream(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec); + SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec); ~SocketStream() override; bool is_readable() const override; bool is_writable() const override; - int read(char *ptr, size_t size) override; - int write(const char *ptr, size_t size) override; - std::string get_remote_addr() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; private: socket_t sock_; time_t read_timeout_sec_; time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT class SSLSocketStream : public Stream { public: SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec); - virtual ~SSLSocketStream(); + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec); + ~SSLSocketStream() override; bool is_readable() const override; bool is_writable() const override; - int read(char *ptr, size_t size) override; - int write(const char *ptr, size_t size) override; - std::string get_remote_addr() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; private: socket_t sock_; SSL *ssl_; time_t read_timeout_sec_; time_t read_timeout_usec_; + time_t write_timeout_sec_; + time_t write_timeout_usec_; }; #endif @@ -1303,58 +1709,76 @@ public: bool is_readable() const override; bool is_writable() const override; - int read(char *ptr, size_t size) override; - int write(const char *ptr, size_t size) override; - std::string get_remote_addr() const override; + ssize_t read(char *ptr, size_t size) override; + ssize_t write(const char *ptr, size_t size) override; + void get_remote_ip_and_port(std::string &ip, int &port) const override; const std::string &get_buffer() const; private: std::string buffer; - int position = 0; + size_t position = 0; }; +inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) { + using namespace std::chrono; + auto start = steady_clock::now(); + while (true) { + auto val = select_read(sock, 0, 10000); + if (val < 0) { + return false; + } else if (val == 0) { + auto current = steady_clock::now(); + auto duration = duration_cast(current - start); + auto timeout = keep_alive_timeout_sec * 1000; + if (duration.count() > timeout) { return false; } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } else { + return true; + } + } +} + template -inline bool process_socket(bool is_client_request, socket_t sock, - size_t keep_alive_max_count, time_t read_timeout_sec, - time_t read_timeout_usec, T callback) { +inline bool +process_server_socket_core(socket_t sock, size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, T callback) { assert(keep_alive_max_count > 0); - auto ret = false; - - if (keep_alive_max_count > 1) { - auto count = keep_alive_max_count; - while (count > 0 && - (is_client_request || - select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, - CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { - SocketStream strm(sock, read_timeout_sec, read_timeout_usec); - auto last_connection = count == 1; - auto connection_close = false; - - ret = callback(strm, last_connection, connection_close); - if (!ret || connection_close) { break; } - - count--; - } - } else { // keep_alive_max_count is 0 or 1 - SocketStream strm(sock, read_timeout_sec, read_timeout_usec); - auto dummy_connection_close = false; - ret = callback(strm, true, dummy_connection_close); + auto count = keep_alive_max_count; + while (count > 0 && keep_alive(sock, keep_alive_timeout_sec)) { + auto close_connection = count == 1; + auto connection_closed = false; + ret = callback(close_connection, connection_closed); + if (!ret || connection_closed) { break; } + count--; } - return ret; } template -inline bool process_and_close_socket(bool is_client_request, socket_t sock, - size_t keep_alive_max_count, - time_t read_timeout_sec, - time_t read_timeout_usec, T callback) { - auto ret = process_socket(is_client_request, sock, keep_alive_max_count, - read_timeout_sec, read_timeout_usec, callback); - close_socket(sock); - return ret; +inline bool +process_server_socket(socket_t sock, size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + return process_server_socket_core( + sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} + +template +inline bool process_client_socket(socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + SocketStream strm(sock, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); } inline int shutdown_socket(socket_t sock) { @@ -1365,18 +1789,10 @@ inline int shutdown_socket(socket_t sock) { #endif } -template -socket_t create_socket(const char *host, int port, Fn fn, - int socket_flags = 0) { -#ifdef _WIN32 -#define SO_SYNCHRONOUS_NONALERT 0x20 -#define SO_OPENTYPE 0x7008 - - int opt = SO_SYNCHRONOUS_NONALERT; - setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&opt, - sizeof(opt)); -#endif - +template +socket_t create_socket(const char *host, int port, int socket_flags, + bool tcp_nodelay, SocketOptions socket_options, + BindOrConnect bind_or_connect) { // Get address info struct addrinfo hints; struct addrinfo *result; @@ -1390,6 +1806,9 @@ socket_t create_socket(const char *host, int port, Fn fn, auto service = std::to_string(port); if (getaddrinfo(host, service.c_str(), &hints, &result)) { +#ifdef __linux__ + res_init(); +#endif return INVALID_SOCKET; } @@ -1424,17 +1843,22 @@ socket_t create_socket(const char *host, int port, Fn fn, if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; } #endif - // Make 'reuse address' option available - int yes = 1; - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&yes), - sizeof(yes)); -#ifdef SO_REUSEPORT - setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast(&yes), - sizeof(yes)); -#endif + if (tcp_nodelay) { + int yes = 1; + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&yes), + sizeof(yes)); + } + + if (socket_options) { socket_options(sock); } + + if (rp->ai_family == AF_INET6) { + int no = 0; + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast(&no), + sizeof(no)); + } // bind or connect - if (fn(sock, *rp)) { + if (bind_or_connect(sock, *rp)) { freeaddrinfo(result); return sock; } @@ -1479,7 +1903,7 @@ inline bool bind_ip_address(socket_t sock, const char *host) { auto ret = false; for (auto rp = result; rp; rp = rp->ai_next) { const auto &ai = *rp; - if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + if (!::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { ret = true; break; } @@ -1489,8 +1913,12 @@ inline bool bind_ip_address(socket_t sock, const char *host) { return ret; } +#if !defined _WIN32 && !defined ANDROID +#define USE_IF2IP +#endif + +#ifdef USE_IF2IP inline std::string if2ip(const std::string &ifn) { -#ifndef _WIN32 struct ifaddrs *ifap; getifaddrs(&ifap); for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { @@ -1506,51 +1934,83 @@ inline std::string if2ip(const std::string &ifn) { } } freeifaddrs(ifap); -#endif return std::string(); } +#endif inline socket_t create_client_socket(const char *host, int port, - time_t timeout_sec, - const std::string &intf) { - return create_socket( - host, port, [&](socket_t sock, struct addrinfo &ai) -> bool { + bool tcp_nodelay, + SocketOptions socket_options, + time_t timeout_sec, time_t timeout_usec, + const std::string &intf, Error &error) { + auto sock = create_socket( + host, port, 0, tcp_nodelay, socket_options, + [&](socket_t sock, struct addrinfo &ai) -> bool { if (!intf.empty()) { +#ifdef USE_IF2IP auto ip = if2ip(intf); if (ip.empty()) { ip = intf; } - if (!bind_ip_address(sock, ip.c_str())) { return false; } + if (!bind_ip_address(sock, ip.c_str())) { + error = Error::BindIPAddress; + return false; + } +#endif } set_nonblocking(sock, true); - auto ret = ::connect(sock, ai.ai_addr, static_cast(ai.ai_addrlen)); + auto ret = + ::connect(sock, ai.ai_addr, static_cast(ai.ai_addrlen)); + if (ret < 0) { if (is_connection_error() || - !wait_until_socket_is_ready(sock, timeout_sec, 0)) { + !wait_until_socket_is_ready(sock, timeout_sec, timeout_usec)) { close_socket(sock); + error = Error::Connection; return false; } } set_nonblocking(sock, false); + error = Error::Success; return true; }); -} -inline std::string get_remote_addr(socket_t sock) { - struct sockaddr_storage addr; - socklen_t len = sizeof(addr); + if (sock != INVALID_SOCKET) { + error = Error::Success; + } else { + if (error == Error::Success) { error = Error::Connection; } + } - if (!getpeername(sock, reinterpret_cast(&addr), &len)) { - std::array ipstr{}; + return sock; +} - if (!getnameinfo(reinterpret_cast(&addr), len, - ipstr.data(), static_cast(ipstr.size()), nullptr, 0, NI_NUMERICHOST)) { - return ipstr.data(); - } +inline void get_remote_ip_and_port(const struct sockaddr_storage &addr, + socklen_t addr_len, std::string &ip, + int &port) { + if (addr.ss_family == AF_INET) { + port = ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + port = + ntohs(reinterpret_cast(&addr)->sin6_port); } - return std::string(); + std::array ipstr{}; + if (!getnameinfo(reinterpret_cast(&addr), addr_len, + ipstr.data(), static_cast(ipstr.size()), nullptr, + 0, NI_NUMERICHOST)) { + ip = ipstr.data(); + } +} + +inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (!getpeername(sock, reinterpret_cast(&addr), + &addr_len)) { + get_remote_ip_and_port(addr, addr_len, ip, port); + } } inline const char * @@ -1596,121 +2056,336 @@ find_content_type(const std::string &path, inline const char *status_message(int status) { switch (status) { case 100: return "Continue"; + case 101: return "Switching Protocol"; + case 102: return "Processing"; + case 103: return "Early Hints"; case 200: return "OK"; + case 201: return "Created"; case 202: return "Accepted"; + case 203: return "Non-Authoritative Information"; case 204: return "No Content"; + case 205: return "Reset Content"; case 206: return "Partial Content"; + case 207: return "Multi-Status"; + case 208: return "Already Reported"; + case 226: return "IM Used"; + case 300: return "Multiple Choice"; case 301: return "Moved Permanently"; case 302: return "Found"; case 303: return "See Other"; case 304: return "Not Modified"; + case 305: return "Use Proxy"; + case 306: return "unused"; + case 307: return "Temporary Redirect"; + case 308: return "Permanent Redirect"; case 400: return "Bad Request"; case 401: return "Unauthorized"; + case 402: return "Payment Required"; case 403: return "Forbidden"; case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 406: return "Not Acceptable"; + case 407: return "Proxy Authentication Required"; + case 408: return "Request Timeout"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 411: return "Length Required"; + case 412: return "Precondition Failed"; case 413: return "Payload Too Large"; - case 414: return "Request-URI Too Long"; + case 414: return "URI Too Long"; case 415: return "Unsupported Media Type"; case 416: return "Range Not Satisfiable"; case 417: return "Expectation Failed"; + case 418: return "I'm a teapot"; + case 421: return "Misdirected Request"; + case 422: return "Unprocessable Entity"; + case 423: return "Locked"; + case 424: return "Failed Dependency"; + case 425: return "Too Early"; + case 426: return "Upgrade Required"; + case 428: return "Precondition Required"; + case 429: return "Too Many Requests"; + case 431: return "Request Header Fields Too Large"; + case 451: return "Unavailable For Legal Reasons"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; case 503: return "Service Unavailable"; + case 504: return "Gateway Timeout"; + case 505: return "HTTP Version Not Supported"; + case 506: return "Variant Also Negotiates"; + case 507: return "Insufficient Storage"; + case 508: return "Loop Detected"; + case 510: return "Not Extended"; + case 511: return "Network Authentication Required"; default: case 500: return "Internal Server Error"; } } -#ifdef CPPHTTPLIB_ZLIB_SUPPORT -inline bool can_compress(const std::string &content_type) { - return !content_type.find("text/") || content_type == "image/svg+xml" || +inline bool can_compress_content_type(const std::string &content_type) { + return (!content_type.find("text/") && content_type != "text/event-stream") || + content_type == "image/svg+xml" || content_type == "application/javascript" || content_type == "application/json" || content_type == "application/xml" || content_type == "application/xhtml+xml"; } -inline bool compress(std::string &content) { - z_stream strm; - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; +enum class EncodingType { None = 0, Gzip, Brotli }; - auto ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, - Z_DEFAULT_STRATEGY); - if (ret != Z_OK) { return false; } +inline EncodingType encoding_type(const Request &req, const Response &res) { + auto ret = + detail::can_compress_content_type(res.get_header_value("Content-Type")); + if (!ret) { return EncodingType::None; } - strm.avail_in = content.size(); - strm.next_in = - const_cast(reinterpret_cast(content.data())); + const auto &s = req.get_header_value("Accept-Encoding"); + (void)(s); - std::string compressed; +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + // TODO: 'Accept-Encoding' has br, not br;q=0 + ret = s.find("br") != std::string::npos; + if (ret) { return EncodingType::Brotli; } +#endif - std::array buff{}; - do { - strm.avail_out = buff.size(); - strm.next_out = reinterpret_cast(buff.data()); - ret = deflate(&strm, Z_FINISH); - assert(ret != Z_STREAM_ERROR); - compressed.append(buff.data(), buff.size() - strm.avail_out); - } while (strm.avail_out == 0); +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 + ret = s.find("gzip") != std::string::npos; + if (ret) { return EncodingType::Gzip; } +#endif - assert(ret == Z_STREAM_END); - assert(strm.avail_in == 0); + return EncodingType::None; +} - content.swap(compressed); +class compressor { +public: + virtual ~compressor(){}; - deflateEnd(&strm); - return true; -} + typedef std::function Callback; + virtual bool compress(const char *data, size_t data_length, bool last, + Callback callback) = 0; +}; class decompressor { public: - decompressor() { - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; + virtual ~decompressor() {} + + virtual bool is_valid() const = 0; + + typedef std::function Callback; + virtual bool decompress(const char *data, size_t data_length, + Callback callback) = 0; +}; + +class nocompressor : public compressor { +public: + ~nocompressor(){}; + + bool compress(const char *data, size_t data_length, bool /*last*/, + Callback callback) override { + if (!data_length) { return true; } + return callback(data, data_length); + } +}; + +#ifdef CPPHTTPLIB_ZLIB_SUPPORT +class gzip_compressor : public compressor { +public: + gzip_compressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; + + is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, + Z_DEFAULT_STRATEGY) == Z_OK; + } + + ~gzip_compressor() { deflateEnd(&strm_); } + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override { + assert(is_valid_); + + auto flush = last ? Z_FINISH : Z_NO_FLUSH; + + strm_.avail_in = static_cast(data_length); + strm_.next_in = const_cast(reinterpret_cast(data)); + + int ret = Z_OK; + + std::array buff{}; + do { + strm_.avail_out = buff.size(); + strm_.next_out = reinterpret_cast(buff.data()); + + ret = deflate(&strm_, flush); + assert(ret != Z_STREAM_ERROR); + + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { + return false; + } + } while (strm_.avail_out == 0); + + assert((last && ret == Z_STREAM_END) || (!last && ret == Z_OK)); + assert(strm_.avail_in == 0); + return true; + } + +private: + bool is_valid_ = false; + z_stream strm_; +}; + +class gzip_decompressor : public decompressor { +public: + gzip_decompressor() { + std::memset(&strm_, 0, sizeof(strm_)); + strm_.zalloc = Z_NULL; + strm_.zfree = Z_NULL; + strm_.opaque = Z_NULL; // 15 is the value of wbits, which should be at the maximum possible value - // to ensure that any gzip stream can be decoded. The offset of 16 specifies - // that the stream to decompress will be formatted with a gzip wrapper. - is_valid_ = inflateInit2(&strm, 16 + 15) == Z_OK; + // to ensure that any gzip stream can be decoded. The offset of 32 specifies + // that the stream type should be automatically detected either gzip or + // deflate. + is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK; } - ~decompressor() { inflateEnd(&strm); } + ~gzip_decompressor() { inflateEnd(&strm_); } - bool is_valid() const { return is_valid_; } + bool is_valid() const override { return is_valid_; } + + bool decompress(const char *data, size_t data_length, + Callback callback) override { + assert(is_valid_); - template - bool decompress(const char *data, size_t data_length, T callback) { int ret = Z_OK; - strm.avail_in = data_length; - strm.next_in = const_cast(reinterpret_cast(data)); + strm_.avail_in = static_cast(data_length); + strm_.next_in = const_cast(reinterpret_cast(data)); - std::array buff{}; - do { - strm.avail_out = buff.size(); - strm.next_out = reinterpret_cast(buff.data()); + std::array buff{}; + while (strm_.avail_in > 0) { + strm_.avail_out = buff.size(); + strm_.next_out = reinterpret_cast(buff.data()); - ret = inflate(&strm, Z_NO_FLUSH); + ret = inflate(&strm_, Z_NO_FLUSH); assert(ret != Z_STREAM_ERROR); switch (ret) { case Z_NEED_DICT: case Z_DATA_ERROR: - case Z_MEM_ERROR: inflateEnd(&strm); return false; + case Z_MEM_ERROR: inflateEnd(&strm_); return false; } - if (!callback(buff.data(), buff.size() - strm.avail_out)) { + if (!callback(buff.data(), buff.size() - strm_.avail_out)) { return false; } - } while (strm.avail_out == 0); + } return ret == Z_OK || ret == Z_STREAM_END; } private: - bool is_valid_; - z_stream strm; + bool is_valid_ = false; + z_stream strm_; +}; +#endif + +#ifdef CPPHTTPLIB_BROTLI_SUPPORT +class brotli_compressor : public compressor { +public: + brotli_compressor() { + state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr); + } + + ~brotli_compressor() { BrotliEncoderDestroyInstance(state_); } + + bool compress(const char *data, size_t data_length, bool last, + Callback callback) override { + std::array buff{}; + + auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS; + auto available_in = data_length; + auto next_in = reinterpret_cast(data); + + for (;;) { + if (last) { + if (BrotliEncoderIsFinished(state_)) { break; } + } else { + if (!available_in) { break; } + } + + auto available_out = buff.size(); + auto next_out = buff.data(); + + if (!BrotliEncoderCompressStream(state_, operation, &available_in, + &next_in, &available_out, &next_out, + nullptr)) { + return false; + } + + auto output_bytes = buff.size() - available_out; + if (output_bytes) { + callback(reinterpret_cast(buff.data()), output_bytes); + } + } + + return true; + } + +private: + BrotliEncoderState *state_ = nullptr; +}; + +class brotli_decompressor : public decompressor { +public: + brotli_decompressor() { + decoder_s = BrotliDecoderCreateInstance(0, 0, 0); + decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT + : BROTLI_DECODER_RESULT_ERROR; + } + + ~brotli_decompressor() { + if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); } + } + + bool is_valid() const override { return decoder_s; } + + bool decompress(const char *data, size_t data_length, + Callback callback) override { + if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_ERROR) { + return 0; + } + + const uint8_t *next_in = (const uint8_t *)data; + size_t avail_in = data_length; + size_t total_out; + + decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT; + + std::array buff{}; + while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) { + char *next_out = buff.data(); + size_t avail_out = buff.size(); + + decoder_r = BrotliDecoderDecompressStream( + decoder_s, &avail_in, &next_in, &avail_out, + reinterpret_cast(&next_out), &total_out); + + if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; } + + if (!callback(buff.data(), buff.size() - avail_out)) { return false; } + } + + return decoder_r == BROTLI_DECODER_RESULT_SUCCESS || + decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT; + } + +private: + BrotliDecoderResult decoder_r; + BrotliDecoderState *decoder_s = nullptr; }; #endif @@ -1720,21 +2395,60 @@ inline bool has_header(const Headers &headers, const char *key) { inline const char *get_header_value(const Headers &headers, const char *key, size_t id = 0, const char *def = nullptr) { - auto it = headers.find(key); - std::advance(it, id); - if (it != headers.end()) { return it->second.c_str(); } + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second.c_str(); } return def; } -inline uint64_t get_header_value_uint64(const Headers &headers, const char *key, - int def = 0) { - auto it = headers.find(key); - if (it != headers.end()) { +template +inline T get_header_value(const Headers & /*headers*/, const char * /*key*/, + size_t /*id*/ = 0, uint64_t /*def*/ = 0) {} + +template <> +inline uint64_t get_header_value(const Headers &headers, + const char *key, size_t id, + uint64_t def) { + auto rng = headers.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return std::strtoull(it->second.data(), nullptr, 10); } return def; } +template +inline bool parse_header(const char *beg, const char *end, T fn) { + // Skip trailing spaces and tabs. + while (beg < end && is_space_or_tab(end[-1])) { + end--; + } + + auto p = beg; + while (p < end && *p != ':') { + p++; + } + + if (p == end) { return false; } + + auto key_end = p; + + if (*p++ != ':') { return false; } + + while (p < end && is_space_or_tab(*p)) { + p++; + } + + if (p < end) { + fn(std::string(beg, key_end), decode_url(std::string(p, end), false)); + return true; + } + + return false; +} + inline bool read_headers(Stream &strm, Headers &headers) { const auto bufsiz = 2048; char buf[bufsiz]; @@ -1751,24 +2465,13 @@ inline bool read_headers(Stream &strm, Headers &headers) { continue; // Skip invalid line. } - // Skip trailing spaces and tabs. + // Exclude CRLF auto end = line_reader.ptr() + line_reader.size() - 2; - while (line_reader.ptr() < end && (end[-1] == ' ' || end[-1] == '\t')) { - end--; - } - // Horizontal tab and ' ' are considered whitespace and are ignored when on - // the left or right side of the header value: - // - https://stackoverflow.com/questions/50179659/ - // - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html - static const std::regex re(R"((.+?):[\t ]*(.+))"); - - std::cmatch m; - if (std::regex_match(line_reader.ptr(), end, m, re)) { - auto key = std::string(m[1]); - auto val = std::string(m[2]); - headers.emplace(key, val); - } + parse_header(line_reader.ptr(), end, + [&](std::string &&key, std::string &&val) { + headers.emplace(std::move(key), std::move(val)); + }); } return true; @@ -1781,12 +2484,12 @@ inline bool read_content_with_length(Stream &strm, uint64_t len, uint64_t r = 0; while (r < len) { auto read_len = static_cast(len - r); - auto n = strm.read(buf, std::min(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); if (n <= 0) { return false; } - if (!out(buf, n)) { return false; } + if (!out(buf, static_cast(n))) { return false; } - r += n; + r += static_cast(n); if (progress) { if (!progress(r, len)) { return false; } @@ -1801,9 +2504,9 @@ inline void skip_content_with_length(Stream &strm, uint64_t len) { uint64_t r = 0; while (r < len) { auto read_len = static_cast(len - r); - auto n = strm.read(buf, std::min(read_len, CPPHTTPLIB_RECV_BUFSIZ)); + auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ)); if (n <= 0) { return; } - r += n; + r += static_cast(n); } } @@ -1816,7 +2519,7 @@ inline bool read_content_without_length(Stream &strm, ContentReceiver out) { } else if (n == 0) { return true; } - if (!out(buf, n)) { return false; } + if (!out(buf, static_cast(n))) { return false; } } return true; @@ -1830,9 +2533,17 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) { if (!line_reader.getline()) { return false; } - auto chunk_len = std::stoi(line_reader.ptr(), 0, 16); + unsigned long chunk_len; + while (true) { + char *end_ptr; + + chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16); + + if (end_ptr == line_reader.ptr()) { return false; } + if (chunk_len == ULONG_MAX) { return false; } + + if (chunk_len == 0) { break; } - while (chunk_len > 0) { if (!read_content_with_length(strm, chunk_len, nullptr, out)) { return false; } @@ -1842,8 +2553,6 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) { if (strcmp(line_reader.ptr(), "\r\n")) { break; } if (!line_reader.getline()) { return false; } - - chunk_len = std::stoi(line_reader.ptr(), 0, 16); } if (chunk_len == 0) { @@ -1860,62 +2569,86 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) { "chunked"); } -template -bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, - Progress progress, ContentReceiver receiver) { - - ContentReceiver out = [&](const char *buf, size_t n) { - return receiver(buf, n); - }; +template +bool prepare_content_receiver(T &x, int &status, ContentReceiver receiver, + bool decompress, U callback) { + if (decompress) { + std::string encoding = x.get_header_value("Content-Encoding"); + std::shared_ptr decompressor; + if (encoding.find("gzip") != std::string::npos || + encoding.find("deflate") != std::string::npos) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT - decompressor decompressor; - - if (!decompressor.is_valid()) { - status = 500; - return false; - } - - if (x.get_header_value("Content-Encoding") == "gzip") { - out = [&](const char *buf, size_t n) { - return decompressor.decompress( - buf, n, [&](const char *buf, size_t n) { return receiver(buf, n); }); - }; - } + decompressor = std::make_shared(); #else - if (x.get_header_value("Content-Encoding") == "gzip") { - status = 415; - return false; - } + status = 415; + return false; +#endif + } else if (encoding.find("br") != std::string::npos) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + decompressor = std::make_shared(); +#else + status = 415; + return false; #endif - - auto ret = true; - auto exceed_payload_max_length = false; - - if (is_chunked_transfer_encoding(x.headers)) { - ret = read_content_chunked(strm, out); - } else if (!has_header(x.headers, "Content-Length")) { - ret = read_content_without_length(strm, out); - } else { - auto len = get_header_value_uint64(x.headers, "Content-Length", 0); - if (len > payload_max_length) { - exceed_payload_max_length = true; - skip_content_with_length(strm, len); - ret = false; - } else if (len > 0) { - ret = read_content_with_length(strm, len, progress, out); } - } - if (!ret) { status = exceed_payload_max_length ? 413 : 400; } + if (decompressor) { + if (decompressor->is_valid()) { + ContentReceiver out = [&](const char *buf, size_t n) { + return decompressor->decompress( + buf, n, + [&](const char *buf, size_t n) { return receiver(buf, n); }); + }; + return callback(out); + } else { + status = 500; + return false; + } + } + } - return ret; + ContentReceiver out = [&](const char *buf, size_t n) { + return receiver(buf, n); + }; + return callback(out); +} + +template +bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, + Progress progress, ContentReceiver receiver, + bool decompress) { + return prepare_content_receiver( + x, status, receiver, decompress, [&](const ContentReceiver &out) { + auto ret = true; + auto exceed_payload_max_length = false; + + if (is_chunked_transfer_encoding(x.headers)) { + ret = read_content_chunked(strm, out); + } else if (!has_header(x.headers, "Content-Length")) { + ret = read_content_without_length(strm, out); + } else { + auto len = get_header_value(x.headers, "Content-Length"); + if (len > payload_max_length) { + exceed_payload_max_length = true; + skip_content_with_length(strm, len); + ret = false; + } else if (len > 0) { + ret = read_content_with_length(strm, len, progress, out); + } + } + + if (!ret) { status = exceed_payload_max_length ? 413 : 400; } + return ret; + }); } template -inline int write_headers(Stream &strm, const T &info, const Headers &headers) { - auto write_len = 0; +inline ssize_t write_headers(Stream &strm, const T &info, + const Headers &headers) { + ssize_t write_len = 0; for (const auto &x : info.headers) { + if (x.first == "EXCEPTION_WHAT") { continue; } auto len = strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); if (len < 0) { return len; } @@ -1933,57 +2666,150 @@ inline int write_headers(Stream &strm, const T &info, const Headers &headers) { return write_len; } +inline bool write_data(Stream &strm, const char *d, size_t l) { + size_t offset = 0; + while (offset < l) { + auto length = strm.write(d + offset, l - offset); + if (length < 0) { return false; } + offset += static_cast(length); + } + return true; +} + +template inline ssize_t write_content(Stream &strm, ContentProvider content_provider, - size_t offset, size_t length) { + size_t offset, size_t length, T is_shutting_down) { size_t begin_offset = offset; size_t end_offset = offset + length; - while (offset < end_offset) { - ssize_t written_length = 0; + auto ok = true; + DataSink data_sink; - DataSink data_sink; - data_sink.write = [&](const char *d, size_t l) { + data_sink.write = [&](const char *d, size_t l) { + if (ok) { offset += l; - written_length = strm.write(d, l); - }; - data_sink.done = [&](void) { written_length = -1; }; - data_sink.is_writable = [&](void) { return strm.is_writable(); }; + if (!write_data(strm, d, l)) { ok = false; } + } + }; + + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; - content_provider(offset, end_offset - offset, data_sink); - if (written_length < 0) { return written_length; } + while (offset < end_offset && !is_shutting_down()) { + if (!content_provider(offset, end_offset - offset, data_sink)) { + return -1; + } + if (!ok) { return -1; } } + return static_cast(offset - begin_offset); } template +inline ssize_t write_content_without_length(Stream &strm, + ContentProvider content_provider, + T is_shutting_down) { + size_t offset = 0; + auto data_available = true; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) { + if (ok) { + offset += l; + if (!write_data(strm, d, l)) { ok = false; } + } + }; + + data_sink.done = [&](void) { data_available = false; }; + + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; + + while (data_available && !is_shutting_down()) { + if (!content_provider(offset, 0, data_sink)) { return -1; } + if (!ok) { return -1; } + } + + return static_cast(offset); +} + +template inline ssize_t write_content_chunked(Stream &strm, ContentProvider content_provider, - T is_shutting_down) { + T is_shutting_down, U &compressor) { size_t offset = 0; auto data_available = true; ssize_t total_written_length = 0; - while (data_available && !is_shutting_down()) { - ssize_t written_length = 0; + auto ok = true; + DataSink data_sink; + + data_sink.write = [&](const char *d, size_t l) { + if (!ok) { return; } + + data_available = l > 0; + offset += l; + + std::string payload; + if (!compressor.compress(d, l, false, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } - DataSink data_sink; - data_sink.write = [&](const char *d, size_t l) { - data_available = l > 0; - offset += l; + if (!payload.empty()) { + // Emit chunked response header and footer for each chunk + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (write_data(strm, chunk.data(), chunk.size())) { + total_written_length += chunk.size(); + } else { + ok = false; + return; + } + } + }; + + data_sink.done = [&](void) { + if (!ok) { return; } + + data_available = false; + + std::string payload; + if (!compressor.compress(nullptr, 0, true, + [&](const char *data, size_t data_len) { + payload.append(data, data_len); + return true; + })) { + ok = false; + return; + } + if (!payload.empty()) { // Emit chunked response header and footer for each chunk - auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n"; - written_length = strm.write(chunk); - }; - data_sink.done = [&](void) { - data_available = false; - written_length = strm.write("0\r\n\r\n"); - }; - data_sink.is_writable = [&](void) { return strm.is_writable(); }; + auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n"; + if (write_data(strm, chunk.data(), chunk.size())) { + total_written_length += chunk.size(); + } else { + ok = false; + return; + } + } + + static const std::string done_marker("0\r\n\r\n"); + if (write_data(strm, done_marker.data(), done_marker.size())) { + total_written_length += done_marker.size(); + } else { + ok = false; + } + }; - content_provider(offset, 0, data_sink); + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; - if (written_length < 0) { return written_length; } - total_written_length += written_length; + while (data_available && !is_shutting_down()) { + if (!content_provider(offset, 0, data_sink)) { return -1; } + if (!ok) { return -1; } } + return total_written_length; } @@ -1994,6 +2820,12 @@ inline bool redirect(T &cli, const Request &req, Response &res, new_req.path = path; new_req.redirect_count -= 1; + if (res.status == 303 && (req.method != "GET" && req.method != "HEAD")) { + new_req.method = "GET"; + new_req.body.clear(); + new_req.headers.clear(); + } + Response new_res; auto ret = cli.send(new_req, new_res); @@ -2001,75 +2833,20 @@ inline bool redirect(T &cli, const Request &req, Response &res, return ret; } -inline std::string encode_url(const std::string &s) { - std::string result; - - for (auto i = 0; s[i]; i++) { - switch (s[i]) { - case ' ': result += "%20"; break; - case '+': result += "%2B"; break; - case '\r': result += "%0D"; break; - case '\n': result += "%0A"; break; - case '\'': result += "%27"; break; - case ',': result += "%2C"; break; - // case ':': result += "%3A"; break; // ok? probably... - case ';': result += "%3B"; break; - default: - auto c = static_cast(s[i]); - if (c >= 0x80) { - result += '%'; - char hex[4]; - size_t len = snprintf(hex, sizeof(hex) - 1, "%02X", c); - assert(len == 2); - result.append(hex, len); - } else { - result += s[i]; - } - break; - } - } - - return result; -} - -inline std::string decode_url(const std::string &s) { - std::string result; +inline std::string params_to_query_str(const Params ¶ms) { + std::string query; - for (size_t i = 0; i < s.size(); i++) { - if (s[i] == '%' && i + 1 < s.size()) { - if (s[i + 1] == 'u') { - int val = 0; - if (from_hex_to_i(s, i + 2, 4, val)) { - // 4 digits Unicode codes - char buff[4]; - size_t len = to_utf8(val, buff); - if (len > 0) { result.append(buff, len); } - i += 5; // 'u0000' - } else { - result += s[i]; - } - } else { - int val = 0; - if (from_hex_to_i(s, i + 1, 2, val)) { - // 2 digits hex codes - result += static_cast(val); - i += 2; // '00' - } else { - result += s[i]; - } - } - } else if (s[i] == '+') { - result += ' '; - } else { - result += s[i]; - } + for (auto it = params.begin(); it != params.end(); ++it) { + if (it != params.begin()) { query += "&"; } + query += it->first; + query += "="; + query += encode_url(it->second); } - - return result; + return query; } inline void parse_query_text(const std::string &s, Params ¶ms) { - split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) { + split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) { std::string key; std::string val; split(b, e, '=', [&](const char *b2, const char *e2) { @@ -2079,7 +2856,10 @@ inline void parse_query_text(const std::string &s, Params ¶ms) { val.assign(b2, e2); } }); - params.emplace(key, decode_url(val)); + + if (!key.empty()) { + params.emplace(decode_url(key, true), decode_url(val, true)); + } }); } @@ -2087,17 +2867,20 @@ inline bool parse_multipart_boundary(const std::string &content_type, std::string &boundary) { auto pos = content_type.find("boundary="); if (pos == std::string::npos) { return false; } - boundary = content_type.substr(pos + 9); - return true; + if (boundary.length() >= 2 && boundary.front() == '"' && + boundary.back() == '"') { + boundary = boundary.substr(1, boundary.size() - 2); + } + return !boundary.empty(); } inline bool parse_range_header(const std::string &s, Ranges &ranges) { static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); std::smatch m; if (std::regex_match(s, m, re_first_range)) { - auto pos = m.position(1); - auto len = m.length(1); + auto pos = static_cast(m.position(1)); + auto len = static_cast(m.length(1)); bool all_valid_ranges = true; split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { if (!all_valid_ranges) return; @@ -2128,21 +2911,21 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) { class MultipartFormDataParser { public: - MultipartFormDataParser() {} + MultipartFormDataParser() = default; - void set_boundary(const std::string &boundary) { boundary_ = boundary; } + void set_boundary(std::string &&boundary) { boundary_ = boundary; } bool is_valid() const { return is_valid_; } template bool parse(const char *buf, size_t n, T content_callback, U header_callback) { - static const std::regex re_content_type(R"(^Content-Type:\s*(.*?)\s*$)", - std::regex_constants::icase); static const std::regex re_content_disposition( "^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename=" "\"(.*?)\")?\\s*$", std::regex_constants::icase); + static const std::string dash_ = "--"; + static const std::string crlf_ = "\r\n"; buf_.append(buf, n); // TODO: performance improvement @@ -2152,10 +2935,7 @@ public: auto pattern = dash_ + boundary_ + crlf_; if (pattern.size() > buf_.size()) { return true; } auto pos = buf_.find(pattern); - if (pos != 0) { - is_done_ = true; - return false; - } + if (pos != 0) { return false; } buf_.erase(0, pattern.size()); off_ += pattern.size(); state_ = 1; @@ -2173,7 +2953,6 @@ public: if (pos == 0) { if (!header_callback(file_)) { is_valid_ = false; - is_done_ = false; return false; } buf_.erase(0, crlf_.size()); @@ -2182,12 +2961,13 @@ public: break; } - auto header = buf_.substr(0, pos); - { + static const std::string header_name = "content-type:"; + const auto header = buf_.substr(0, pos); + if (start_with(header, header_name)) { + file_.content_type = trim_copy(header.substr(header_name.size())); + } else { std::smatch m; - if (std::regex_match(header, m, re_content_type)) { - file_.content_type = m[1]; - } else if (std::regex_match(header, m, re_content_disposition)) { + if (std::regex_match(header, m, re_content_disposition)) { file_.name = m[1]; file_.filename = m[2]; } @@ -2197,6 +2977,7 @@ public: off_ += pos + crlf_.size(); pos = buf_.find(crlf_); } + if (state_ != 3) { return true; } break; } case 3: { // Body @@ -2205,10 +2986,17 @@ public: if (pattern.size() > buf_.size()) { return true; } auto pos = buf_.find(pattern); - if (pos == std::string::npos) { pos = buf_.size(); } + if (pos == std::string::npos) { + pos = buf_.size(); + while (pos > 0) { + auto c = buf_[pos - 1]; + if (c != '\r' && c != '\n' && c != '-') { break; } + pos--; + } + } + if (!content_callback(buf_.data(), pos)) { is_valid_ = false; - is_done_ = false; return false; } @@ -2224,7 +3012,6 @@ public: if (pos != std::string::npos) { if (!content_callback(buf_.data(), pos)) { is_valid_ = false; - is_done_ = false; return false; } @@ -2234,7 +3021,6 @@ public: } else { if (!content_callback(buf_.data(), pattern.size())) { is_valid_ = false; - is_done_ = false; return false; } @@ -2246,20 +3032,19 @@ public: } case 4: { // Boundary if (crlf_.size() > buf_.size()) { return true; } - if (buf_.find(crlf_) == 0) { + if (buf_.compare(0, crlf_.size(), crlf_) == 0) { buf_.erase(0, crlf_.size()); off_ += crlf_.size(); state_ = 1; } else { auto pattern = dash_ + crlf_; if (pattern.size() > buf_.size()) { return true; } - if (buf_.find(pattern) == 0) { + if (buf_.compare(0, pattern.size(), pattern) == 0) { buf_.erase(0, pattern.size()); off_ += pattern.size(); is_valid_ = true; state_ = 5; } else { - is_done_ = true; return true; } } @@ -2282,14 +3067,11 @@ private: file_.content_type.clear(); } - const std::string dash_ = "--"; - const std::string crlf_ = "\r\n"; std::string boundary_; std::string buf_; size_t state_ = 0; - size_t is_valid_ = false; - size_t is_done_ = false; + bool is_valid_ = false; size_t off_ = 0; MultipartFormData file_; }; @@ -2329,12 +3111,14 @@ get_range_offset_and_length(const Request &req, size_t content_length, return std::make_pair(0, content_length); } + auto slen = static_cast(content_length); + if (r.first == -1) { - r.first = content_length - r.second; - r.second = content_length - 1; + r.first = slen - r.second; + r.second = slen - 1; } - if (r.second == -1) { r.second = content_length - 1; } + if (r.second == -1) { r.second = slen - 1; } return std::make_pair(r.first, r.second - r.first + 1); } @@ -2420,16 +3204,19 @@ get_multipart_ranges_data_length(const Request &req, Response &res, return data_length; } +template inline bool write_multipart_ranges_data(Stream &strm, const Request &req, Response &res, const std::string &boundary, - const std::string &content_type) { + const std::string &content_type, + T is_shutting_down) { return process_multipart_ranges_data( req, res, boundary, content_type, [&](const std::string &token) { strm.write(token); }, [&](const char *token) { strm.write(token); }, [&](size_t offset, size_t length) { - return write_content(strm, res.content_provider, offset, length) >= 0; + return write_content(strm, res.content_provider_, offset, length, + is_shutting_down) >= 0; }); } @@ -2438,20 +3225,31 @@ get_range_offset_and_length(const Request &req, const Response &res, size_t index) { auto r = req.ranges[index]; - if (r.second == -1) { r.second = res.content_length - 1; } + if (r.second == -1) { + r.second = static_cast(res.content_length_) - 1; + } return std::make_pair(r.first, r.second - r.first + 1); } inline bool expect_content(const Request &req) { if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || - req.method == "PRI") { + req.method == "PRI" || req.method == "DELETE") { return true; } // TODO: check if Content-Length is set return false; } +inline bool has_crlf(const char *s) { + auto p = s; + while (*p) { + if (*p == '\r' || *p == '\n') { return true; } + p++; + } + return false; +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT template inline std::string message_digest(const std::string &s, Init init, @@ -2489,6 +3287,33 @@ inline std::string SHA_512(const std::string &s) { #endif #ifdef _WIN32 +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +// NOTE: This code came up with the following stackoverflow post: +// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store +inline bool load_system_certs_on_windows(X509_STORE *store) { + auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT"); + + if (!hStore) { return false; } + + PCCERT_CONTEXT pContext = NULL; + while (pContext = CertEnumCertificatesInStore(hStore, pContext)) { + auto encoded_cert = + static_cast(pContext->pbCertEncoded); + + auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded); + if (x509) { + X509_STORE_add_cert(store, x509); + X509_free(x509); + } + } + + CertFreeCertificateContext(pContext); + CertCloseStore(hStore, 0); + + return true; +} +#endif + class WSInit { public: WSInit() { @@ -2502,31 +3327,6 @@ public: static WSInit wsinit_; #endif -} // namespace detail - -// Header utilities -inline std::pair make_range_header(Ranges ranges) { - std::string field = "bytes="; - auto i = 0; - for (auto r : ranges) { - if (i != 0) { field += ", "; } - if (r.first != -1) { field += std::to_string(r.first); } - field += '-'; - if (r.second != -1) { field += std::to_string(r.second); } - i++; - } - return std::make_pair("Range", field); -} - -inline std::pair -make_basic_authentication_header(const std::string &username, - const std::string &password, - bool is_proxy = false) { - auto field = "Basic " + detail::base64_encode(username + ":" + password); - auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; - return std::make_pair(key, field); -} - #ifdef CPPHTTPLIB_OPENSSL_SUPPORT inline std::pair make_digest_authentication_header( const Request &req, const std::map &auth, @@ -2566,17 +3366,18 @@ inline std::pair make_digest_authentication_header( ":" + qop + ":" + H(A2)); } - auto field = "Digest username=\"hello\", realm=\"" + auth.at("realm") + - "\", nonce=\"" + auth.at("nonce") + "\", uri=\"" + req.path + - "\", algorithm=" + algo + ", qop=" + qop + ", nc=\"" + nc + - "\", cnonce=\"" + cnonce + "\", response=\"" + response + "\""; + auto field = "Digest username=\"" + username + "\", realm=\"" + + auth.at("realm") + "\", nonce=\"" + auth.at("nonce") + + "\", uri=\"" + req.path + "\", algorithm=" + algo + + ", qop=" + qop + ", nc=\"" + nc + "\", cnonce=\"" + cnonce + + "\", response=\"" + response + "\""; auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; return std::make_pair(key, field); } #endif -inline bool parse_www_authenticate(const httplib::Response &res, +inline bool parse_www_authenticate(const Response &res, std::map &auth, bool is_proxy) { auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; @@ -2593,9 +3394,13 @@ inline bool parse_www_authenticate(const httplib::Response &res, auto beg = std::sregex_iterator(s.begin(), s.end(), re); for (auto i = beg; i != std::sregex_iterator(); ++i) { auto m = *i; - auto key = s.substr(m.position(1), m.length(1)); - auto val = m.length(2) > 0 ? s.substr(m.position(2), m.length(2)) - : s.substr(m.position(3), m.length(3)); + auto key = s.substr(static_cast(m.position(1)), + static_cast(m.length(1))); + auto val = m.length(2) > 0 + ? s.substr(static_cast(m.position(2)), + static_cast(m.length(2))) + : s.substr(static_cast(m.position(3)), + static_cast(m.length(3))); auth[key] = val; } return true; @@ -2612,13 +3417,60 @@ inline std::string random_string(size_t length) { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"; const size_t max_index = (sizeof(charset) - 1); - return charset[rand() % max_index]; + return charset[static_cast(rand()) % max_index]; }; std::string str(length, 0); std::generate_n(str.begin(), length, randchar); return str; } +class ContentProviderAdapter { +public: + explicit ContentProviderAdapter( + ContentProviderWithoutLength &&content_provider) + : content_provider_(content_provider) {} + + bool operator()(size_t offset, size_t, DataSink &sink) { + return content_provider_(offset, sink); + } + +private: + ContentProviderWithoutLength content_provider_; +}; + +} // namespace detail + +// Header utilities +inline std::pair make_range_header(Ranges ranges) { + std::string field = "bytes="; + auto i = 0; + for (auto r : ranges) { + if (i != 0) { field += ", "; } + if (r.first != -1) { field += std::to_string(r.first); } + field += '-'; + if (r.second != -1) { field += std::to_string(r.second); } + i++; + } + return std::make_pair("Range", field); +} + +inline std::pair +make_basic_authentication_header(const std::string &username, + const std::string &password, + bool is_proxy = false) { + auto field = "Basic " + detail::base64_encode(username + ":" + password); + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} + +inline std::pair +make_bearer_token_authentication_header(const std::string &token, + bool is_proxy = false) { + auto field = "Bearer " + token; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; + return std::make_pair(key, field); +} + // Request implementation inline bool Request::has_header(const char *key) const { return detail::has_header(headers, key); @@ -2628,17 +3480,26 @@ inline std::string Request::get_header_value(const char *key, size_t id) const { return detail::get_header_value(headers, key, id, ""); } +template +inline T Request::get_header_value(const char *key, size_t id) const { + return detail::get_header_value(headers, key, id, 0); +} + inline size_t Request::get_header_value_count(const char *key) const { auto r = headers.equal_range(key); - return std::distance(r.first, r.second); + return static_cast(std::distance(r.first, r.second)); } inline void Request::set_header(const char *key, const char *val) { - headers.emplace(key, val); + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } } inline void Request::set_header(const char *key, const std::string &val) { - headers.emplace(key, val); + if (!detail::has_crlf(key) && !detail::has_crlf(val.c_str())) { + headers.emplace(key, val); + } } inline bool Request::has_param(const char *key) const { @@ -2646,15 +3507,16 @@ inline bool Request::has_param(const char *key) const { } inline std::string Request::get_param_value(const char *key, size_t id) const { - auto it = params.find(key); - std::advance(it, id); - if (it != params.end()) { return it->second; } + auto rng = params.equal_range(key); + auto it = rng.first; + std::advance(it, static_cast(id)); + if (it != rng.second) { return it->second; } return std::string(); } inline size_t Request::get_param_value_count(const char *key) const { auto r = params.equal_range(key); - return std::distance(r.first, r.second); + return static_cast(std::distance(r.first, r.second)); } inline bool Request::is_multipart_form_data() const { @@ -2682,22 +3544,41 @@ inline std::string Response::get_header_value(const char *key, return detail::get_header_value(headers, key, id, ""); } +template +inline T Response::get_header_value(const char *key, size_t id) const { + return detail::get_header_value(headers, key, id, 0); +} + inline size_t Response::get_header_value_count(const char *key) const { auto r = headers.equal_range(key); - return std::distance(r.first, r.second); + return static_cast(std::distance(r.first, r.second)); } inline void Response::set_header(const char *key, const char *val) { - headers.emplace(key, val); + if (!detail::has_crlf(key) && !detail::has_crlf(val)) { + headers.emplace(key, val); + } } inline void Response::set_header(const char *key, const std::string &val) { - headers.emplace(key, val); + if (!detail::has_crlf(key) && !detail::has_crlf(val.c_str())) { + headers.emplace(key, val); + } +} + +inline void Response::set_redirect(const char *url, int stat) { + if (!detail::has_crlf(url)) { + set_header("Location", url); + if (300 <= stat && stat < 400) { + this->status = stat; + } else { + this->status = 302; + } + } } -inline void Response::set_redirect(const char *url) { - set_header("Location", url); - status = 302; +inline void Response::set_redirect(const std::string &url, int stat) { + set_redirect(url.c_str(), stat); } inline void Response::set_content(const char *s, size_t n, @@ -2706,62 +3587,79 @@ inline void Response::set_content(const char *s, size_t n, set_header("Content-Type", content_type); } -inline void Response::set_content(const std::string &s, - const char *content_type) { - body = s; +inline void Response::set_content(std::string s, const char *content_type) { + body = std::move(s); set_header("Content-Type", content_type); } -inline void Response::set_content_provider( - size_t in_length, - std::function provider, - std::function resource_releaser) { +inline void +Response::set_content_provider(size_t in_length, const char *content_type, + ContentProvider provider, + const std::function &resource_releaser) { assert(in_length > 0); - content_length = in_length; - content_provider = [provider](size_t offset, size_t length, DataSink &sink) { - provider(offset, length, sink); - }; - content_provider_resource_releaser = resource_releaser; + set_header("Content-Type", content_type); + content_length_ = in_length; + content_provider_ = std::move(provider); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider = false; +} + +inline void +Response::set_content_provider(const char *content_type, + ContentProviderWithoutLength provider, + const std::function &resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider = false; } inline void Response::set_chunked_content_provider( - std::function provider, - std::function resource_releaser) { - content_length = 0; - content_provider = [provider](size_t offset, size_t, DataSink &sink) { - provider(offset, sink); - }; - content_provider_resource_releaser = resource_releaser; + const char *content_type, ContentProviderWithoutLength provider, + const std::function &resource_releaser) { + set_header("Content-Type", content_type); + content_length_ = 0; + content_provider_ = detail::ContentProviderAdapter(std::move(provider)); + content_provider_resource_releaser_ = resource_releaser; + is_chunked_content_provider = true; } // Rstream implementation -inline int Stream::write(const char *ptr) { return write(ptr, strlen(ptr)); } +inline ssize_t Stream::write(const char *ptr) { + return write(ptr, strlen(ptr)); +} -inline int Stream::write(const std::string &s) { +inline ssize_t Stream::write(const std::string &s) { return write(s.data(), s.size()); } template -inline int Stream::write_format(const char *fmt, const Args &... args) { - std::array buf; +inline ssize_t Stream::write_format(const char *fmt, const Args &... args) { + const auto bufsiz = 2048; + std::array buf; #if defined(_MSC_VER) && _MSC_VER < 1900 - auto n = _snprintf_s(buf, bufsiz, buf.size() - 1, fmt, args...); + auto sn = _snprintf_s(buf.data(), bufsiz - 1, buf.size() - 1, fmt, args...); #else - auto n = snprintf(buf.data(), buf.size() - 1, fmt, args...); + auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...); #endif - if (n <= 0) { return n; } + if (sn <= 0) { return sn; } - if (n >= static_cast(buf.size()) - 1) { + auto n = static_cast(sn); + + if (n >= buf.size() - 1) { std::vector glowable_buf(buf.size()); - while (n >= static_cast(glowable_buf.size() - 1)) { + while (n >= glowable_buf.size() - 1) { glowable_buf.resize(glowable_buf.size() * 2); #if defined(_MSC_VER) && _MSC_VER < 1900 - n = _snprintf_s(&glowable_buf[0], glowable_buf.size(), - glowable_buf.size() - 1, fmt, args...); + n = static_cast(_snprintf_s(&glowable_buf[0], glowable_buf.size(), + glowable_buf.size() - 1, fmt, + args...)); #else - n = snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...); + n = static_cast( + snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...)); #endif } return write(&glowable_buf[0], n); @@ -2774,32 +3672,53 @@ namespace detail { // Socket stream implementation inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, - time_t read_timeout_usec) + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) : sock_(sock), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec) {} + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec) {} inline SocketStream::~SocketStream() {} inline bool SocketStream::is_readable() const { - return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; + return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; } inline bool SocketStream::is_writable() const { - return detail::select_write(sock_, 0, 0) > 0; + return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0; } -inline int SocketStream::read(char *ptr, size_t size) { - if (is_readable()) { return recv(sock_, ptr, static_cast(size), 0); } - return -1; +inline ssize_t SocketStream::read(char *ptr, size_t size) { + if (!is_readable()) { return -1; } + +#ifdef _WIN32 + if (size > static_cast((std::numeric_limits::max)())) { + return -1; + } + return recv(sock_, ptr, static_cast(size), 0); +#else + return handle_EINTR([&]() { return recv(sock_, ptr, size, 0); }); +#endif } -inline int SocketStream::write(const char *ptr, size_t size) { - if (is_writable()) { return send(sock_, ptr, static_cast(size), 0); } - return -1; +inline ssize_t SocketStream::write(const char *ptr, size_t size) { + if (!is_writable()) { return -1; } + +#ifdef _WIN32 + if (size > static_cast((std::numeric_limits::max)())) { + return -1; + } + return send(sock_, ptr, static_cast(size), 0); +#else + return handle_EINTR([&]() { return send(sock_, ptr, size, 0); }); +#endif } -inline std::string SocketStream::get_remote_addr() const { - return detail::get_remote_addr(sock_); +inline void SocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + return detail::get_remote_ip_and_port(sock_, ip, port); } // Buffer stream implementation @@ -2807,22 +3726,23 @@ inline bool BufferStream::is_readable() const { return true; } inline bool BufferStream::is_writable() const { return true; } -inline int BufferStream::read(char *ptr, size_t size) { -#if defined(_MSC_VER) && _MSC_VER < 1900 - int len_read = static_cast(buffer._Copy_s(ptr, size, size, position)); +inline ssize_t BufferStream::read(char *ptr, size_t size) { +#if defined(_MSC_VER) && _MSC_VER <= 1900 + auto len_read = buffer._Copy_s(ptr, size, size, position); #else - int len_read = static_cast(buffer.copy(ptr, size, position)); + auto len_read = buffer.copy(ptr, size, position); #endif - position += len_read; - return len_read; + position += static_cast(len_read); + return static_cast(len_read); } -inline int BufferStream::write(const char *ptr, size_t size) { +inline ssize_t BufferStream::write(const char *ptr, size_t size) { buffer.append(ptr, size); - return static_cast(size); + return static_cast(size); } -inline std::string BufferStream::get_remote_addr() const { return ""; } +inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/, + int & /*port*/) const {} inline const std::string &BufferStream::get_buffer() const { return buffer; } @@ -2830,15 +3750,12 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; } // HTTP server implementation inline Server::Server() - : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), - read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND), - read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND), - payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false), - svr_sock_(INVALID_SOCKET) { + : new_task_queue( + [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }), + svr_sock_(INVALID_SOCKET), is_running_(false) { #ifndef _WIN32 signal(SIGPIPE, SIG_IGN); #endif - new_task_queue = [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }; } inline Server::~Server() {} @@ -2889,6 +3806,13 @@ inline Server &Server::Delete(const char *pattern, Handler handler) { return *this; } +inline Server &Server::Delete(const char *pattern, + HandlerWithContentReader handler) { + delete_handlers_for_content_reader_.push_back( + std::make_pair(std::regex(pattern), handler)); + return *this; +} + inline Server &Server::Options(const char *pattern, Handler handler) { options_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); return *this; @@ -2898,11 +3822,12 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) { return set_mount_point(mount_point, dir); } -inline bool Server::set_mount_point(const char *mount_point, const char *dir) { +inline bool Server::set_mount_point(const char *mount_point, const char *dir, + Headers headers) { if (detail::is_dir(dir)) { std::string mnt = mount_point ? mount_point : "/"; if (!mnt.empty() && mnt[0] == '/') { - base_dirs_.emplace_back(mnt, dir); + base_dirs_.push_back({mnt, dir, std::move(headers)}); return true; } } @@ -2911,7 +3836,7 @@ inline bool Server::set_mount_point(const char *mount_point, const char *dir) { inline bool Server::remove_mount_point(const char *mount_point) { for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { - if (it->first == mount_point) { + if (it->mount_point == mount_point) { base_dirs_.erase(it); return true; } @@ -2932,6 +3857,12 @@ inline void Server::set_error_handler(Handler handler) { error_handler_ = std::move(handler); } +inline void Server::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void Server::set_socket_options(SocketOptions socket_options) { + socket_options_ = socket_options; +} + inline void Server::set_logger(Logger logger) { logger_ = std::move(logger); } inline void @@ -2943,11 +3874,25 @@ inline void Server::set_keep_alive_max_count(size_t count) { keep_alive_max_count_ = count; } +inline void Server::set_keep_alive_timeout(time_t sec) { + keep_alive_timeout_sec_ = sec; +} + inline void Server::set_read_timeout(time_t sec, time_t usec) { read_timeout_sec_ = sec; read_timeout_usec_ = usec; } +inline void Server::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; +} + +inline void Server::set_idle_interval(time_t sec, time_t usec) { + idle_interval_sec_ = sec; + idle_interval_usec_ = usec; +} + inline void Server::set_payload_max_length(size_t length) { payload_max_length_ = length; } @@ -2987,7 +3932,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) { req.version = std::string(m[5]); req.method = std::string(m[1]); req.target = std::string(m[2]); - req.path = detail::decode_url(m[3]); + req.path = detail::decode_url(m[3], false); // Parse query text auto len = std::distance(m[4].first, m[4].second); @@ -2999,7 +3944,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) { return false; } -inline bool Server::write_response(Stream &strm, bool last_connection, +inline bool Server::write_response(Stream &strm, bool close_connection, const Request &req, Response &res) { assert(res.status != -1); @@ -3014,16 +3959,17 @@ inline bool Server::write_response(Stream &strm, bool last_connection, } // Headers - if (last_connection || req.get_header_value("Connection") == "close") { + if (close_connection || req.get_header_value("Connection") == "close") { res.set_header("Connection", "close"); - } - - if (!last_connection && req.get_header_value("Connection") == "Keep-Alive") { - res.set_header("Connection", "Keep-Alive"); + } else { + std::stringstream ss; + ss << "timeout=" << keep_alive_timeout_sec_ + << ", max=" << keep_alive_max_count_; + res.set_header("Keep-Alive", ss.str()); } if (!res.has_header("Content-Type") && - (!res.body.empty() || res.content_length > 0)) { + (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) { res.set_header("Content-Type", "text/plain"); } @@ -3047,18 +3993,20 @@ inline bool Server::write_response(Stream &strm, bool last_connection, "multipart/byteranges; boundary=" + boundary); } + auto type = detail::encoding_type(req, res); + if (res.body.empty()) { - if (res.content_length > 0) { + if (res.content_length_ > 0) { size_t length = 0; if (req.ranges.empty()) { - length = res.content_length; + length = res.content_length_; } else if (req.ranges.size() == 1) { auto offsets = - detail::get_range_offset_and_length(req, res.content_length, 0); + detail::get_range_offset_and_length(req, res.content_length_, 0); auto offset = offsets.first; length = offsets.second; auto content_range = detail::make_content_range_header_field( - offset, length, res.content_length); + offset, length, res.content_length_); res.set_header("Content-Range", content_range); } else { length = detail::get_multipart_ranges_data_length(req, res, boundary, @@ -3066,8 +4014,15 @@ inline bool Server::write_response(Stream &strm, bool last_connection, } res.set_header("Content-Length", std::to_string(length)); } else { - if (res.content_provider) { - res.set_header("Transfer-Encoding", "chunked"); + if (res.content_provider_) { + if (res.is_chunked_content_provider) { + res.set_header("Transfer-Encoding", "chunked"); + if (type == detail::EncodingType::Gzip) { + res.set_header("Content-Encoding", "gzip"); + } else if (type == detail::EncodingType::Brotli) { + res.set_header("Content-Encoding", "br"); + } + } } else { res.set_header("Content-Length", "0"); } @@ -3089,16 +4044,35 @@ inline bool Server::write_response(Stream &strm, bool last_connection, detail::make_multipart_ranges_data(req, res, boundary, content_type); } + if (type != detail::EncodingType::None) { + std::shared_ptr compressor; + + if (type == detail::EncodingType::Gzip) { #ifdef CPPHTTPLIB_ZLIB_SUPPORT - // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 - const auto &encodings = req.get_header_value("Accept-Encoding"); - if (encodings.find("gzip") != std::string::npos && - detail::can_compress(res.get_header_value("Content-Type"))) { - if (detail::compress(res.body)) { + compressor = std::make_shared(); res.set_header("Content-Encoding", "gzip"); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = std::make_shared(); + res.set_header("Content-Encoding", "brotli"); +#endif + } + + if (compressor) { + std::string compressed; + + if (!compressor->compress(res.body.data(), res.body.size(), true, + [&](const char *data, size_t data_len) { + compressed.append(data, data_len); + return true; + })) { + return false; + } + + res.body.swap(compressed); } } -#endif auto length = std::to_string(res.body.size()); res.set_header("Content-Length", length); @@ -3111,13 +4085,14 @@ inline bool Server::write_response(Stream &strm, bool last_connection, strm.write(data.data(), data.size()); // Body + auto ret = true; if (req.method != "HEAD") { if (!res.body.empty()) { - if (!strm.write(res.body)) { return false; } - } else if (res.content_provider) { + if (!strm.write(res.body)) { ret = false; } + } else if (res.content_provider_) { if (!write_content_with_provider(strm, req, res, boundary, content_type)) { - return false; + ret = false; } } } @@ -3125,87 +4100,109 @@ inline bool Server::write_response(Stream &strm, bool last_connection, // Log if (logger_) { logger_(req, res); } - return true; + return ret; } inline bool Server::write_content_with_provider(Stream &strm, const Request &req, Response &res, const std::string &boundary, const std::string &content_type) { - if (res.content_length) { + auto is_shutting_down = [this]() { + return this->svr_sock_ == INVALID_SOCKET; + }; + + if (res.content_length_ > 0) { if (req.ranges.empty()) { - if (detail::write_content(strm, res.content_provider, 0, - res.content_length) < 0) { + if (detail::write_content(strm, res.content_provider_, 0, + res.content_length_, is_shutting_down) < 0) { return false; } } else if (req.ranges.size() == 1) { auto offsets = - detail::get_range_offset_and_length(req, res.content_length, 0); + detail::get_range_offset_and_length(req, res.content_length_, 0); auto offset = offsets.first; auto length = offsets.second; - if (detail::write_content(strm, res.content_provider, offset, length) < - 0) { + if (detail::write_content(strm, res.content_provider_, offset, length, + is_shutting_down) < 0) { return false; } } else { - if (!detail::write_multipart_ranges_data(strm, req, res, boundary, - content_type)) { + if (!detail::write_multipart_ranges_data( + strm, req, res, boundary, content_type, is_shutting_down)) { return false; } } } else { - auto is_shutting_down = [this]() { - return this->svr_sock_ == INVALID_SOCKET; - }; - if (detail::write_content_chunked(strm, res.content_provider, - is_shutting_down) < 0) { - return false; + if (res.is_chunked_content_provider) { + auto type = detail::encoding_type(req, res); + + std::shared_ptr compressor; + if (type == detail::EncodingType::Gzip) { +#ifdef CPPHTTPLIB_ZLIB_SUPPORT + compressor = std::make_shared(); +#endif + } else if (type == detail::EncodingType::Brotli) { +#ifdef CPPHTTPLIB_BROTLI_SUPPORT + compressor = std::make_shared(); +#endif + } else { + compressor = std::make_shared(); + } + assert(compressor != nullptr); + + if (detail::write_content_chunked(strm, res.content_provider_, + is_shutting_down, *compressor) < 0) { + return false; + } + } else { + if (detail::write_content_without_length(strm, res.content_provider_, + is_shutting_down) < 0) { + return false; + } } } return true; } -inline bool Server::read_content(Stream &strm, bool last_connection, - Request &req, Response &res) { +inline bool Server::read_content(Stream &strm, Request &req, Response &res) { MultipartFormDataMap::iterator cur; - auto ret = read_content_core( - strm, last_connection, req, res, - // Regular - [&](const char *buf, size_t n) { - if (req.body.size() + n > req.body.max_size()) { return false; } - req.body.append(buf, n); - return true; - }, - // Multipart - [&](const MultipartFormData &file) { - cur = req.files.emplace(file.name, file); - return true; - }, - [&](const char *buf, size_t n) { - auto &content = cur->second.content; - if (content.size() + n > content.max_size()) { return false; } - content.append(buf, n); - return true; - }); - - const auto &content_type = req.get_header_value("Content-Type"); - if (!content_type.find("application/x-www-form-urlencoded")) { - detail::parse_query_text(req.body, req.params); + if (read_content_core( + strm, req, res, + // Regular + [&](const char *buf, size_t n) { + if (req.body.size() + n > req.body.max_size()) { return false; } + req.body.append(buf, n); + return true; + }, + // Multipart + [&](const MultipartFormData &file) { + cur = req.files.emplace(file.name, file); + return true; + }, + [&](const char *buf, size_t n) { + auto &content = cur->second.content; + if (content.size() + n > content.max_size()) { return false; } + content.append(buf, n); + return true; + })) { + const auto &content_type = req.get_header_value("Content-Type"); + if (!content_type.find("application/x-www-form-urlencoded")) { + detail::parse_query_text(req.body, req.params); + } + return true; } - - return ret; + return false; } inline bool Server::read_content_with_content_receiver( - Stream &strm, bool last_connection, Request &req, Response &res, - ContentReceiver receiver, MultipartContentHeader multipart_header, + Stream &strm, Request &req, Response &res, ContentReceiver receiver, + MultipartContentHeader multipart_header, ContentReceiver multipart_receiver) { - return read_content_core(strm, last_connection, req, res, receiver, - multipart_header, multipart_receiver); + return read_content_core(strm, req, res, receiver, multipart_header, + multipart_receiver); } -inline bool Server::read_content_core(Stream &strm, bool last_connection, - Request &req, Response &res, +inline bool Server::read_content_core(Stream &strm, Request &req, Response &res, ContentReceiver receiver, MultipartContentHeader mulitpart_header, ContentReceiver multipart_receiver) { @@ -3217,11 +4214,22 @@ inline bool Server::read_content_core(Stream &strm, bool last_connection, std::string boundary; if (!detail::parse_multipart_boundary(content_type, boundary)) { res.status = 400; - return write_response(strm, last_connection, req, res); + return false; } - multipart_form_data_parser.set_boundary(boundary); + multipart_form_data_parser.set_boundary(std::move(boundary)); out = [&](const char *buf, size_t n) { + /* For debug + size_t pos = 0; + while (pos < n) { + auto read_size = std::min(1, n - pos); + auto ret = multipart_form_data_parser.parse( + buf + pos, read_size, multipart_receiver, mulitpart_header); + if (!ret) { return false; } + pos += read_size; + } + return true; + */ return multipart_form_data_parser.parse(buf, n, multipart_receiver, mulitpart_header); }; @@ -3229,15 +4237,19 @@ inline bool Server::read_content_core(Stream &strm, bool last_connection, out = receiver; } - if (!detail::read_content(strm, req, payload_max_length_, res.status, - Progress(), out)) { - return write_response(strm, last_connection, req, res); + if (req.method == "DELETE" && !req.has_header("Content-Length")) { + return true; + } + + if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr, + out, true)) { + return false; } if (req.is_multipart_form_data()) { if (!multipart_form_data_parser.is_valid()) { res.status = 400; - return write_response(strm, last_connection, req, res); + return false; } } @@ -3246,15 +4258,12 @@ inline bool Server::read_content_core(Stream &strm, bool last_connection, inline bool Server::handle_file_request(Request &req, Response &res, bool head) { - for (const auto &kv : base_dirs_) { - const auto &mount_point = kv.first; - const auto &base_dir = kv.second; - + for (const auto &entry : base_dirs_) { // Prefix match - if (!req.path.find(mount_point)) { - std::string sub_path = "/" + req.path.substr(mount_point.size()); + if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) { + std::string sub_path = "/" + req.path.substr(entry.mount_point.size()); if (detail::is_valid_path(sub_path)) { - auto path = base_dir + sub_path; + auto path = entry.base_dir + sub_path; if (path.back() == '/') { path += "index.html"; } if (detail::is_file(path)) { @@ -3262,6 +4271,9 @@ inline bool Server::handle_file_request(Request &req, Response &res, auto type = detail::find_content_type(path, file_extension_and_mimetype_map_); if (type) { res.set_header("Content-Type", type); } + for (const auto& kv : entry.headers) { + res.set_header(kv.first.c_str(), kv.second); + } res.status = 200; if (!head && file_request_handler_) { file_request_handler_(req, res); @@ -3274,40 +4286,39 @@ inline bool Server::handle_file_request(Request &req, Response &res, return false; } -inline socket_t Server::create_server_socket(const char *host, int port, - int socket_flags) const { +inline socket_t +Server::create_server_socket(const char *host, int port, int socket_flags, + SocketOptions socket_options) const { return detail::create_socket( - host, port, + host, port, socket_flags, tcp_nodelay_, socket_options, [](socket_t sock, struct addrinfo &ai) -> bool { - if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { + if (::bind(sock, ai.ai_addr, static_cast(ai.ai_addrlen))) { return false; } if (::listen(sock, 5)) { // Listen through 5 channels return false; } return true; - }, - socket_flags); + }); } inline int Server::bind_internal(const char *host, int port, int socket_flags) { if (!is_valid()) { return -1; } - svr_sock_ = create_server_socket(host, port, socket_flags); + svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_); if (svr_sock_ == INVALID_SOCKET) { return -1; } if (port == 0) { - struct sockaddr_storage address; - socklen_t len = sizeof(address); - if (getsockname(svr_sock_, reinterpret_cast(&address), - &len) == -1) { + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + if (getsockname(svr_sock_, reinterpret_cast(&addr), + &addr_len) == -1) { return -1; } - if (address.ss_family == AF_INET) { - return ntohs(reinterpret_cast(&address)->sin_port); - } else if (address.ss_family == AF_INET6) { - return ntohs( - reinterpret_cast(&address)->sin6_port); + if (addr.ss_family == AF_INET) { + return ntohs(reinterpret_cast(&addr)->sin_port); + } else if (addr.ss_family == AF_INET6) { + return ntohs(reinterpret_cast(&addr)->sin6_port); } else { return -1; } @@ -3323,18 +4334,19 @@ inline bool Server::listen_internal() { { std::unique_ptr task_queue(new_task_queue()); - for (;;) { - if (svr_sock_ == INVALID_SOCKET) { - // The server socket was closed by 'stop' method. - break; - } - - auto val = detail::select_read(svr_sock_, 0, 100000); - - if (val == 0) { // Timeout - continue; + while (svr_sock_ != INVALID_SOCKET) { +#ifndef _WIN32 + if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) { +#endif + auto val = detail::select_read(svr_sock_, idle_interval_sec_, + idle_interval_usec_); + if (val == 0) { // Timeout + task_queue->on_idle(); + continue; + } +#ifndef _WIN32 } - +#endif socket_t sock = accept(svr_sock_, nullptr, nullptr); if (sock == INVALID_SOCKET) { @@ -3353,7 +4365,11 @@ inline bool Server::listen_internal() { break; } +#if __cplusplus > 201703L + task_queue->enqueue([=, this]() { process_and_close_socket(sock); }); +#else task_queue->enqueue([=]() { process_and_close_socket(sock); }); +#endif } task_queue->shutdown(); @@ -3363,8 +4379,7 @@ inline bool Server::listen_internal() { return ret; } -inline bool Server::routing(Request &req, Response &res, Stream &strm, - bool last_connection) { +inline bool Server::routing(Request &req, Response &res, Stream &strm) { // File handler bool is_head_request = req.method == "HEAD"; if ((req.method == "GET" || is_head_request) && @@ -3377,12 +4392,12 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm, { ContentReader reader( [&](ContentReceiver receiver) { - return read_content_with_content_receiver( - strm, last_connection, req, res, receiver, nullptr, nullptr); + return read_content_with_content_receiver(strm, req, res, receiver, + nullptr, nullptr); }, [&](MultipartContentHeader header, ContentReceiver receiver) { - return read_content_with_content_receiver( - strm, last_connection, req, res, nullptr, header, receiver); + return read_content_with_content_receiver(strm, req, res, nullptr, + header, receiver); }); if (req.method == "POST") { @@ -3400,11 +4415,16 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm, req, res, reader, patch_handlers_for_content_reader_)) { return true; } + } else if (req.method == "DELETE") { + if (dispatch_request_for_content_reader( + req, res, reader, delete_handlers_for_content_reader_)) { + return true; + } } } // Read content into `req.body` - if (!read_content(strm, last_connection, req, res)) { return false; } + if (!read_content(strm, req, res)) { return false; } } // Regular handler @@ -3427,22 +4447,31 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm, } inline bool Server::dispatch_request(Request &req, Response &res, - Handlers &handlers) { - for (const auto &x : handlers) { - const auto &pattern = x.first; - const auto &handler = x.second; + const Handlers &handlers) { - if (std::regex_match(req.path, req.matches, pattern)) { - handler(req, res); - return true; + try { + for (const auto &x : handlers) { + const auto &pattern = x.first; + const auto &handler = x.second; + + if (std::regex_match(req.path, req.matches, pattern)) { + handler(req, res); + return true; + } } + } catch (const std::exception &ex) { + res.status = 500; + res.set_header("EXCEPTION_WHAT", ex.what()); + } catch (...) { + res.status = 500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); } return false; } inline bool Server::dispatch_request_for_content_reader( Request &req, Response &res, ContentReader content_reader, - HandlersForContentReader &handlers) { + const HandlersForContentReader &handlers) { for (const auto &x : handlers) { const auto &pattern = x.first; const auto &handler = x.second; @@ -3456,8 +4485,8 @@ inline bool Server::dispatch_request_for_content_reader( } inline bool -Server::process_request(Stream &strm, bool last_connection, - bool &connection_close, +Server::process_request(Stream &strm, bool close_connection, + bool &connection_closed, const std::function &setup_request) { std::array buf{}; @@ -3476,26 +4505,28 @@ Server::process_request(Stream &strm, bool last_connection, Headers dummy; detail::read_headers(strm, dummy); res.status = 414; - return write_response(strm, last_connection, req, res); + return write_response(strm, close_connection, req, res); } // Request line and headers if (!parse_request_line(line_reader.ptr(), req) || !detail::read_headers(strm, req.headers)) { res.status = 400; - return write_response(strm, last_connection, req, res); + return write_response(strm, close_connection, req, res); } if (req.get_header_value("Connection") == "close") { - connection_close = true; + connection_closed = true; } if (req.version == "HTTP/1.0" && req.get_header_value("Connection") != "Keep-Alive") { - connection_close = true; + connection_closed = true; } - req.set_header("REMOTE_ADDR", strm.get_remote_addr()); + strm.get_remote_ip_and_port(req.remote_addr, req.remote_port); + req.set_header("REMOTE_ADDR", req.remote_addr); + req.set_header("REMOTE_PORT", std::to_string(req.remote_port)); if (req.has_header("Range")) { const auto &range_header_value = req.get_header_value("Range"); @@ -3517,136 +4548,211 @@ Server::process_request(Stream &strm, bool last_connection, strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, detail::status_message(status)); break; - default: return write_response(strm, last_connection, req, res); + default: return write_response(strm, close_connection, req, res); } } // Rounting - if (routing(req, res, strm, last_connection)) { + if (routing(req, res, strm)) { if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } } else { if (res.status == -1) { res.status = 404; } } - return write_response(strm, last_connection, req, res); + return write_response(strm, close_connection, req, res); } inline bool Server::is_valid() const { return true; } inline bool Server::process_and_close_socket(socket_t sock) { - return detail::process_and_close_socket( - false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_, - [this](Stream &strm, bool last_connection, bool &connection_close) { - return process_request(strm, last_connection, connection_close, + auto ret = detail::process_server_socket( + sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_, + read_timeout_usec_, write_timeout_sec_, write_timeout_usec_, + [this](Stream &strm, bool close_connection, bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, nullptr); }); + + detail::shutdown_socket(sock); + detail::close_socket(sock); + return ret; } // HTTP client implementation -inline Client::Client(const std::string &host, int port, - const std::string &client_cert_path, - const std::string &client_key_path) +inline ClientImpl::ClientImpl(const std::string &host) + : ClientImpl(host, 80, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port) + : ClientImpl(host, port, std::string(), std::string()) {} + +inline ClientImpl::ClientImpl(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) : host_(host), port_(port), host_and_port_(host_ + ":" + std::to_string(port_)), client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} -inline Client::~Client() {} +inline ClientImpl::~ClientImpl() { stop_core(); } + +inline bool ClientImpl::is_valid() const { return true; } -inline bool Client::is_valid() const { return true; } +inline Error ClientImpl::get_last_error() const { return error_; } -inline socket_t Client::create_client_socket() const { - if (!proxy_host_.empty()) { - return detail::create_client_socket(proxy_host_.c_str(), proxy_port_, - timeout_sec_, interface_); +inline void ClientImpl::copy_settings(const ClientImpl &rhs) { + client_cert_path_ = rhs.client_cert_path_; + client_key_path_ = rhs.client_key_path_; + connection_timeout_sec_ = rhs.connection_timeout_sec_; + read_timeout_sec_ = rhs.read_timeout_sec_; + read_timeout_usec_ = rhs.read_timeout_usec_; + write_timeout_sec_ = rhs.write_timeout_sec_; + write_timeout_usec_ = rhs.write_timeout_usec_; + basic_auth_username_ = rhs.basic_auth_username_; + basic_auth_password_ = rhs.basic_auth_password_; + bearer_token_auth_token_ = rhs.bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + digest_auth_username_ = rhs.digest_auth_username_; + digest_auth_password_ = rhs.digest_auth_password_; +#endif + keep_alive_ = rhs.keep_alive_; + follow_location_ = rhs.follow_location_; + tcp_nodelay_ = rhs.tcp_nodelay_; + socket_options_ = rhs.socket_options_; + compress_ = rhs.compress_; + decompress_ = rhs.decompress_; + interface_ = rhs.interface_; + proxy_host_ = rhs.proxy_host_; + proxy_port_ = rhs.proxy_port_; + proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_; + proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_; + proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; + proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_; +#endif +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + server_certificate_verification_ = rhs.server_certificate_verification_; +#endif + logger_ = rhs.logger_; +} + +inline socket_t ClientImpl::create_client_socket() const { + if (!proxy_host_.empty() && proxy_port_ != -1) { + return detail::create_client_socket( + proxy_host_.c_str(), proxy_port_, tcp_nodelay_, socket_options_, + connection_timeout_sec_, connection_timeout_usec_, interface_, error_); } - return detail::create_client_socket(host_.c_str(), port_, timeout_sec_, - interface_); + return detail::create_client_socket( + host_.c_str(), port_, tcp_nodelay_, socket_options_, + connection_timeout_sec_, connection_timeout_usec_, interface_, error_); +} + +inline bool ClientImpl::create_and_connect_socket(Socket &socket) { + auto sock = create_client_socket(); + if (sock == INVALID_SOCKET) { return false; } + socket.sock = sock; + return true; +} + +inline void ClientImpl::close_socket(Socket &socket, + bool /*process_socket_ret*/) { + detail::close_socket(socket.sock); + socket_.sock = INVALID_SOCKET; +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + socket_.ssl = nullptr; +#endif } -inline bool Client::read_response_line(Stream &strm, Response &res) { +inline bool ClientImpl::read_response_line(Stream &strm, Response &res) { std::array buf; detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); if (!line_reader.getline()) { return false; } - const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n"); + const static std::regex re("(HTTP/1\\.[01]) (\\d+) (.*?)\r\n"); std::cmatch m; - if (std::regex_match(line_reader.ptr(), m, re)) { + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } + res.version = std::string(m[1]); + res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); + + // Ignore '100 Continue' + while (res.status == 100) { + if (!line_reader.getline()) { return false; } // CRLF + if (!line_reader.getline()) { return false; } // next response line + + if (!std::regex_match(line_reader.ptr(), m, re)) { return false; } res.version = std::string(m[1]); res.status = std::stoi(std::string(m[2])); + res.reason = std::string(m[3]); } return true; } -inline bool Client::send(const Request &req, Response &res) { - auto sock = create_client_socket(); - if (sock == INVALID_SOCKET) { return false; } +inline bool ClientImpl::send(const Request &req, Response &res) { + std::lock_guard request_mutex_guard(request_mutex_); -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl() && !proxy_host_.empty()) { - bool error; - if (!connect(sock, res, error)) { return error; } - } -#endif + { + std::lock_guard guard(socket_mutex_); - return process_and_close_socket( - sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) { - return handle_request(strm, req, res, last_connection, - connection_close); - }); -} + auto is_alive = false; + if (socket_.is_open()) { + is_alive = detail::select_write(socket_.sock, 0, 0) > 0; + if (!is_alive) { close_socket(socket_, false); } + } -inline bool Client::send(const std::vector &requests, - std::vector &responses) { - size_t i = 0; - while (i < requests.size()) { - auto sock = create_client_socket(); - if (sock == INVALID_SOCKET) { return false; } + if (!is_alive) { + if (!create_and_connect_socket(socket_)) { return false; } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (is_ssl() && !proxy_host_.empty()) { - Response res; - bool error; - if (!connect(sock, res, error)) { return false; } - } -#endif + // TODO: refactoring + if (is_ssl()) { + auto &scli = static_cast(*this); + if (!proxy_host_.empty() && proxy_port_ != -1) { + bool success = false; + if (!scli.connect_with_proxy(socket_, res, success)) { + return success; + } + } - if (!process_and_close_socket(sock, requests.size() - i, - [&](Stream &strm, bool last_connection, - bool &connection_close) -> bool { - auto &req = requests[i++]; - auto res = Response(); - auto ret = handle_request(strm, req, res, - last_connection, - connection_close); - if (ret) { - responses.emplace_back(std::move(res)); - } - return ret; - })) { - return false; + if (!scli.initialize_ssl(socket_)) { return false; } + } +#endif } } - return true; + auto close_connection = !keep_alive_; + + auto ret = process_socket(socket_, [&](Stream &strm) { + return handle_request(strm, req, res, close_connection); + }); + + if (close_connection || !ret) { stop_core(); } + + if (!ret) { + if (error_ == Error::Success) { error_ = Error::Unknown; } + } + + return ret; } -inline bool Client::handle_request(Stream &strm, const Request &req, - Response &res, bool last_connection, - bool &connection_close) { - if (req.path.empty()) { return false; } +inline bool ClientImpl::handle_request(Stream &strm, const Request &req, + Response &res, bool close_connection) { + if (req.path.empty()) { + error_ = Error::Connection; + return false; + } bool ret; - if (!is_ssl() && !proxy_host_.empty()) { + if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) { auto req2 = req; req2.path = "http://" + host_and_port_ + req.path; - ret = process_request(strm, req2, res, last_connection, connection_close); + ret = process_request(strm, req2, res, close_connection); } else { - ret = process_request(strm, req, res, last_connection, connection_close); + ret = process_request(strm, req, res, close_connection); } if (!ret) { return false; } @@ -3656,7 +4762,8 @@ inline bool Client::handle_request(Stream &strm, const Request &req, } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - if (res.status == 401 || res.status == 407) { + if ((res.status == 401 || res.status == 407) && + req.authorization_count_ < 5) { auto is_proxy = res.status == 407; const auto &username = is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; @@ -3665,12 +4772,14 @@ inline bool Client::handle_request(Stream &strm, const Request &req, if (!username.empty() && !password.empty()) { std::map auth; - if (parse_www_authenticate(res, auth, is_proxy)) { + if (detail::parse_www_authenticate(res, auth, is_proxy)) { Request new_req = req; - auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization"; + new_req.authorization_count_ += 1; + auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; new_req.headers.erase(key); - new_req.headers.insert(make_digest_authentication_header( - req, auth, 1, random_string(10), username, password, is_proxy)); + new_req.headers.insert(detail::make_digest_authentication_header( + req, auth, new_req.authorization_count_, detail::random_string(10), + username, password, is_proxy)); Response new_res; @@ -3684,102 +4793,64 @@ inline bool Client::handle_request(Stream &strm, const Request &req, return ret; } -#ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline bool Client::connect(socket_t sock, Response &res, bool &error) { - error = true; - Response res2; - - if (!detail::process_socket( - true, sock, 1, read_timeout_sec_, read_timeout_usec_, - [&](Stream &strm, bool /*last_connection*/, bool &connection_close) { - Request req2; - req2.method = "CONNECT"; - req2.path = host_and_port_; - return process_request(strm, req2, res2, false, connection_close); - })) { - detail::close_socket(sock); - error = false; +inline bool ClientImpl::redirect(const Request &req, Response &res) { + if (req.redirect_count == 0) { + error_ = Error::ExceedRedirectCount; return false; } - if (res2.status == 407) { - if (!proxy_digest_auth_username_.empty() && - !proxy_digest_auth_password_.empty()) { - std::map auth; - if (parse_www_authenticate(res2, auth, true)) { - Response res3; - if (!detail::process_socket( - true, sock, 1, read_timeout_sec_, read_timeout_usec_, - [&](Stream &strm, bool /*last_connection*/, - bool &connection_close) { - Request req3; - req3.method = "CONNECT"; - req3.path = host_and_port_; - req3.headers.insert(make_digest_authentication_header( - req3, auth, 1, random_string(10), - proxy_digest_auth_username_, proxy_digest_auth_password_, - true)); - return process_request(strm, req3, res3, false, - connection_close); - })) { - detail::close_socket(sock); - error = false; - return false; - } - } - } else { - res = res2; - return false; - } - } - - return true; -} -#endif - -inline bool Client::redirect(const Request &req, Response &res) { - if (req.redirect_count == 0) { return false; } - - auto location = res.get_header_value("location"); + auto location = detail::decode_url(res.get_header_value("location"), true); if (location.empty()) { return false; } const static std::regex re( - R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); + R"(^(?:(https?):)?(?://([^:/?#]*)(?::(\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); std::smatch m; - if (!regex_match(location, m, re)) { return false; } + if (!std::regex_match(location, m, re)) { return false; } auto scheme = is_ssl() ? "https" : "http"; auto next_scheme = m[1].str(); auto next_host = m[2].str(); - auto next_path = m[3].str(); - if (next_scheme.empty()) { next_scheme = scheme; } + auto port_str = m[3].str(); + auto next_path = m[4].str(); + + auto next_port = port_; + if (!port_str.empty()) { + next_port = std::stoi(port_str); + } else if (!next_scheme.empty()) { + next_port = next_scheme == "https" ? 443 : 80; + } + if (next_scheme.empty()) { next_scheme = scheme; } if (next_host.empty()) { next_host = host_; } if (next_path.empty()) { next_path = "/"; } - if (next_scheme == scheme && next_host == host_) { + if (next_scheme == scheme && next_host == host_ && next_port == port_) { return detail::redirect(*this, req, res, next_path); } else { if (next_scheme == "https") { #ifdef CPPHTTPLIB_OPENSSL_SUPPORT - SSLClient cli(next_host.c_str()); + SSLClient cli(next_host.c_str(), next_port); cli.copy_settings(*this); - return detail::redirect(cli, req, res, next_path); + auto ret = detail::redirect(cli, req, res, next_path); + if (!ret) { error_ = cli.get_last_error(); } + return ret; #else return false; #endif } else { - Client cli(next_host.c_str()); + ClientImpl cli(next_host.c_str(), next_port); cli.copy_settings(*this); - return detail::redirect(cli, req, res, next_path); + auto ret = detail::redirect(cli, req, res, next_path); + if (!ret) { error_ = cli.get_last_error(); } + return ret; } } } -inline bool Client::write_request(Stream &strm, const Request &req, - bool last_connection) { +inline bool ClientImpl::write_request(Stream &strm, const Request &req, + bool close_connection) { detail::BufferStream bstrm; // Request line @@ -3789,7 +4860,7 @@ inline bool Client::write_request(Stream &strm, const Request &req, // Additonal headers Headers headers; - if (last_connection) { headers.emplace("Connection", "close"); } + if (close_connection) { headers.emplace("Connection", "close"); } if (!req.has_header("Host")) { if (is_ssl()) { @@ -3810,7 +4881,7 @@ inline bool Client::write_request(Stream &strm, const Request &req, if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); } if (!req.has_header("User-Agent")) { - headers.emplace("User-Agent", "cpp-httplib/0.5"); + headers.emplace("User-Agent", "cpp-httplib/0.7"); } if (req.body.empty()) { @@ -3818,7 +4889,10 @@ inline bool Client::write_request(Stream &strm, const Request &req, auto length = std::to_string(req.content_length); headers.emplace("Content-Length", length); } else { - headers.emplace("Content-Length", "0"); + if (req.method == "POST" || req.method == "PUT" || + req.method == "PATCH") { + headers.emplace("Content-Length", "0"); + } } } else { if (!req.has_header("Content-Type")) { @@ -3831,7 +4905,7 @@ inline bool Client::write_request(Stream &strm, const Request &req, } } - if (!basic_auth_username_.empty() && !basic_auth_password_.empty()) { + if (!basic_auth_password_.empty()) { headers.insert(make_basic_authentication_header( basic_auth_username_, basic_auth_password_, false)); } @@ -3842,11 +4916,24 @@ inline bool Client::write_request(Stream &strm, const Request &req, proxy_basic_auth_username_, proxy_basic_auth_password_, true)); } + if (!bearer_token_auth_token_.empty()) { + headers.insert(make_bearer_token_authentication_header( + bearer_token_auth_token_, false)); + } + + if (!proxy_bearer_token_auth_token_.empty()) { + headers.insert(make_bearer_token_authentication_header( + proxy_bearer_token_auth_token_, true)); + } + detail::write_headers(bstrm, req, headers); // Flush buffer auto &data = bstrm.get_buffer(); - strm.write(data.data(), data.size()); + if (!detail::write_data(strm, data.data(), data.size())) { + error_ = Error::Write; + return false; + } // Body if (req.body.empty()) { @@ -3854,55 +4941,95 @@ inline bool Client::write_request(Stream &strm, const Request &req, size_t offset = 0; size_t end_offset = req.content_length; + bool ok = true; + DataSink data_sink; data_sink.write = [&](const char *d, size_t l) { - auto written_length = strm.write(d, l); - offset += written_length; + if (ok) { + if (detail::write_data(strm, d, l)) { + offset += l; + } else { + ok = false; + } + } }; - data_sink.is_writable = [&](void) { return strm.is_writable(); }; + data_sink.is_writable = [&](void) { return ok && strm.is_writable(); }; while (offset < end_offset) { - req.content_provider(offset, end_offset - offset, data_sink); + if (!req.content_provider(offset, end_offset - offset, data_sink)) { + error_ = Error::Canceled; + return false; + } + if (!ok) { + error_ = Error::Write; + return false; + } } } } else { - strm.write(req.body); + return detail::write_data(strm, req.body.data(), req.body.size()); } return true; } -inline std::shared_ptr Client::send_with_content_provider( +inline std::shared_ptr ClientImpl::send_with_content_provider( const char *method, const char *path, const Headers &headers, const std::string &body, size_t content_length, ContentProvider content_provider, const char *content_type) { + Request req; req.method = method; - req.headers = headers; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.path = path; - req.headers.emplace("Content-Type", content_type); + if (content_type) { req.headers.emplace("Content-Type", content_type); } #ifdef CPPHTTPLIB_ZLIB_SUPPORT if (compress_) { + detail::gzip_compressor compressor; + if (content_provider) { + auto ok = true; size_t offset = 0; DataSink data_sink; data_sink.write = [&](const char *data, size_t data_len) { - req.body.append(data, data_len); - offset += data_len; + if (ok) { + auto last = offset + data_len == content_length; + + auto ret = compressor.compress( + data, data_len, last, [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + }); + + if (ret) { + offset += data_len; + } else { + ok = false; + } + } }; - data_sink.is_writable = [&](void) { return true; }; + data_sink.is_writable = [&](void) { return ok && true; }; - while (offset < content_length) { - content_provider(offset, content_length - offset, data_sink); + while (ok && offset < content_length) { + if (!content_provider(offset, content_length - offset, data_sink)) { + error_ = Error::Canceled; + return nullptr; + } } } else { - req.body = body; + if (!compressor.compress(body.data(), body.size(), true, + [&](const char *data, size_t data_len) { + req.body.append(data, data_len); + return true; + })) { + return nullptr; + } } - if (!detail::compress(req.body)) { return nullptr; } req.headers.emplace("Content-Encoding", "gzip"); } else #endif @@ -3920,215 +5047,232 @@ inline std::shared_ptr Client::send_with_content_provider( return send(req, *res) ? res : nullptr; } -inline bool Client::process_request(Stream &strm, const Request &req, - Response &res, bool last_connection, - bool &connection_close) { +inline bool ClientImpl::process_request(Stream &strm, const Request &req, + Response &res, bool close_connection) { // Send request - if (!write_request(strm, req, last_connection)) { return false; } + if (!write_request(strm, req, close_connection)) { return false; } // Receive response and headers if (!read_response_line(strm, res) || !detail::read_headers(strm, res.headers)) { + error_ = Error::Read; return false; } - if (res.get_header_value("Connection") == "close" || - res.version == "HTTP/1.0") { - connection_close = true; - } - if (req.response_handler) { - if (!req.response_handler(res)) { return false; } + if (!req.response_handler(res)) { + error_ = Error::Canceled; + return false; + } } // Body if (req.method != "HEAD" && req.method != "CONNECT") { - ContentReceiver out = [&](const char *buf, size_t n) { - if (res.body.size() + n > res.body.max_size()) { return false; } - res.body.append(buf, n); - return true; + auto out = + req.content_receiver + ? static_cast([&](const char *buf, size_t n) { + auto ret = req.content_receiver(buf, n); + if (!ret) { error_ = Error::Canceled; } + return ret; + }) + : static_cast([&](const char *buf, size_t n) { + if (res.body.size() + n > res.body.max_size()) { return false; } + res.body.append(buf, n); + return true; + }); + + auto progress = [&](uint64_t current, uint64_t total) { + if (!req.progress) { return true; } + auto ret = req.progress(current, total); + if (!ret) { error_ = Error::Canceled; } + return ret; }; - if (req.content_receiver) { - out = [&](const char *buf, size_t n) { - return req.content_receiver(buf, n); - }; - } - int dummy_status; - if (!detail::read_content(strm, res, std::numeric_limits::max(), - dummy_status, req.progress, out)) { + if (!detail::read_content(strm, res, (std::numeric_limits::max)(), + dummy_status, progress, out, decompress_)) { + if (error_ != Error::Canceled) { error_ = Error::Read; } return false; } } + if (res.get_header_value("Connection") == "close" || + (res.version == "HTTP/1.0" && res.reason != "Connection established")) { + stop_core(); + } + // Log if (logger_) { logger_(req, res); } return true; } -inline bool Client::process_and_close_socket( - socket_t sock, size_t request_count, - std::function - callback) { - request_count = std::min(request_count, keep_alive_max_count_); - return detail::process_and_close_socket(true, sock, request_count, - read_timeout_sec_, read_timeout_usec_, - callback); +inline bool +ClientImpl::process_socket(Socket &socket, + std::function callback) { + return detail::process_client_socket(socket.sock, read_timeout_sec_, + read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, callback); } -inline bool Client::is_ssl() const { return false; } +inline bool ClientImpl::is_ssl() const { return false; } -inline std::shared_ptr Client::Get(const char *path) { +inline Result ClientImpl::Get(const char *path) { return Get(path, Headers(), Progress()); } -inline std::shared_ptr Client::Get(const char *path, - Progress progress) { +inline Result ClientImpl::Get(const char *path, Progress progress) { return Get(path, Headers(), std::move(progress)); } -inline std::shared_ptr Client::Get(const char *path, - const Headers &headers) { +inline Result ClientImpl::Get(const char *path, const Headers &headers) { return Get(path, headers, Progress()); } -inline std::shared_ptr -Client::Get(const char *path, const Headers &headers, Progress progress) { +inline Result ClientImpl::Get(const char *path, const Headers &headers, + Progress progress) { Request req; req.method = "GET"; req.path = path; - req.headers = headers; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.progress = std::move(progress); auto res = std::make_shared(); - return send(req, *res) ? res : nullptr; + auto ret = send(req, *res); + return Result{ret ? res : nullptr, get_last_error()}; } -inline std::shared_ptr Client::Get(const char *path, - ContentReceiver content_receiver) { - return Get(path, Headers(), nullptr, std::move(content_receiver), Progress()); +inline Result ClientImpl::Get(const char *path, + ContentReceiver content_receiver) { + return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr); } -inline std::shared_ptr Client::Get(const char *path, - ContentReceiver content_receiver, - Progress progress) { +inline Result ClientImpl::Get(const char *path, + ContentReceiver content_receiver, + Progress progress) { return Get(path, Headers(), nullptr, std::move(content_receiver), std::move(progress)); } -inline std::shared_ptr Client::Get(const char *path, - const Headers &headers, - ContentReceiver content_receiver) { - return Get(path, headers, nullptr, std::move(content_receiver), Progress()); +inline Result ClientImpl::Get(const char *path, const Headers &headers, + ContentReceiver content_receiver) { + return Get(path, headers, nullptr, std::move(content_receiver), nullptr); } -inline std::shared_ptr Client::Get(const char *path, - const Headers &headers, - ContentReceiver content_receiver, - Progress progress) { +inline Result ClientImpl::Get(const char *path, const Headers &headers, + ContentReceiver content_receiver, + Progress progress) { return Get(path, headers, nullptr, std::move(content_receiver), std::move(progress)); } -inline std::shared_ptr Client::Get(const char *path, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver) { +inline Result ClientImpl::Get(const char *path, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return Get(path, Headers(), std::move(response_handler), content_receiver, + nullptr); +} + +inline Result ClientImpl::Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { return Get(path, headers, std::move(response_handler), content_receiver, - Progress()); + nullptr); } -inline std::shared_ptr Client::Get(const char *path, - const Headers &headers, - ResponseHandler response_handler, - ContentReceiver content_receiver, - Progress progress) { +inline Result ClientImpl::Get(const char *path, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { + return Get(path, Headers(), std::move(response_handler), content_receiver, + progress); +} + +inline Result ClientImpl::Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, + Progress progress) { Request req; req.method = "GET"; req.path = path; - req.headers = headers; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.response_handler = std::move(response_handler); req.content_receiver = std::move(content_receiver); req.progress = std::move(progress); auto res = std::make_shared(); - return send(req, *res) ? res : nullptr; + auto ret = send(req, *res); + return Result{ret ? res : nullptr, get_last_error()}; } -inline std::shared_ptr Client::Head(const char *path) { +inline Result ClientImpl::Head(const char *path) { return Head(path, Headers()); } -inline std::shared_ptr Client::Head(const char *path, - const Headers &headers) { +inline Result ClientImpl::Head(const char *path, const Headers &headers) { Request req; req.method = "HEAD"; - req.headers = headers; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.path = path; auto res = std::make_shared(); + auto ret = send(req, *res); + return Result{ret ? res : nullptr, get_last_error()}; +} - return send(req, *res) ? res : nullptr; +inline Result ClientImpl::Post(const char *path) { + return Post(path, std::string(), nullptr); } -inline std::shared_ptr Client::Post(const char *path, - const std::string &body, - const char *content_type) { +inline Result ClientImpl::Post(const char *path, const std::string &body, + const char *content_type) { return Post(path, Headers(), body, content_type); } -inline std::shared_ptr Client::Post(const char *path, - const Headers &headers, - const std::string &body, - const char *content_type) { - return send_with_content_provider("POST", path, headers, body, 0, nullptr, - content_type); +inline Result ClientImpl::Post(const char *path, const Headers &headers, + const std::string &body, + const char *content_type) { + auto ret = send_with_content_provider("POST", path, headers, body, 0, nullptr, + content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr Client::Post(const char *path, - const Params ¶ms) { +inline Result ClientImpl::Post(const char *path, const Params ¶ms) { return Post(path, Headers(), params); } -inline std::shared_ptr Client::Post(const char *path, - size_t content_length, - ContentProvider content_provider, - const char *content_type) { +inline Result ClientImpl::Post(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { return Post(path, Headers(), content_length, content_provider, content_type); } -inline std::shared_ptr -Client::Post(const char *path, const Headers &headers, size_t content_length, - ContentProvider content_provider, const char *content_type) { - return send_with_content_provider("POST", path, headers, std::string(), - content_length, content_provider, - content_type); +inline Result ClientImpl::Post(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + auto ret = send_with_content_provider("POST", path, headers, std::string(), + content_length, content_provider, + content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr -Client::Post(const char *path, const Headers &headers, const Params ¶ms) { - std::string query; - for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } - query += it->first; - query += "="; - query += detail::encode_url(it->second); - } - +inline Result ClientImpl::Post(const char *path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); return Post(path, headers, query, "application/x-www-form-urlencoded"); } -inline std::shared_ptr -Client::Post(const char *path, const MultipartFormDataItems &items) { +inline Result ClientImpl::Post(const char *path, + const MultipartFormDataItems &items) { return Post(path, Headers(), items); } -inline std::shared_ptr -Client::Post(const char *path, const Headers &headers, - const MultipartFormDataItems &items) { +inline Result ClientImpl::Post(const char *path, const Headers &headers, + const MultipartFormDataItems &items) { auto boundary = detail::make_multipart_data_boundary(); std::string body; @@ -4153,182 +5297,229 @@ Client::Post(const char *path, const Headers &headers, return Post(path, headers, body, content_type.c_str()); } -inline std::shared_ptr Client::Put(const char *path, - const std::string &body, - const char *content_type) { +inline Result ClientImpl::Put(const char *path) { + return Put(path, std::string(), nullptr); +} + +inline Result ClientImpl::Put(const char *path, const std::string &body, + const char *content_type) { return Put(path, Headers(), body, content_type); } -inline std::shared_ptr Client::Put(const char *path, - const Headers &headers, - const std::string &body, - const char *content_type) { - return send_with_content_provider("PUT", path, headers, body, 0, nullptr, - content_type); +inline Result ClientImpl::Put(const char *path, const Headers &headers, + const std::string &body, + const char *content_type) { + auto ret = send_with_content_provider("PUT", path, headers, body, 0, nullptr, + content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr Client::Put(const char *path, - size_t content_length, - ContentProvider content_provider, - const char *content_type) { +inline Result ClientImpl::Put(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { return Put(path, Headers(), content_length, content_provider, content_type); } -inline std::shared_ptr -Client::Put(const char *path, const Headers &headers, size_t content_length, - ContentProvider content_provider, const char *content_type) { - return send_with_content_provider("PUT", path, headers, std::string(), - content_length, content_provider, - content_type); +inline Result ClientImpl::Put(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + auto ret = send_with_content_provider("PUT", path, headers, std::string(), + content_length, content_provider, + content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr Client::Put(const char *path, - const Params ¶ms) { +inline Result ClientImpl::Put(const char *path, const Params ¶ms) { return Put(path, Headers(), params); } -inline std::shared_ptr -Client::Put(const char *path, const Headers &headers, const Params ¶ms) { - std::string query; - for (auto it = params.begin(); it != params.end(); ++it) { - if (it != params.begin()) { query += "&"; } - query += it->first; - query += "="; - query += detail::encode_url(it->second); - } - +inline Result ClientImpl::Put(const char *path, const Headers &headers, + const Params ¶ms) { + auto query = detail::params_to_query_str(params); return Put(path, headers, query, "application/x-www-form-urlencoded"); } -inline std::shared_ptr Client::Patch(const char *path, - const std::string &body, - const char *content_type) { +inline Result ClientImpl::Patch(const char *path, const std::string &body, + const char *content_type) { return Patch(path, Headers(), body, content_type); } -inline std::shared_ptr Client::Patch(const char *path, - const Headers &headers, - const std::string &body, - const char *content_type) { - return send_with_content_provider("PATCH", path, headers, body, 0, nullptr, - content_type); +inline Result ClientImpl::Patch(const char *path, const Headers &headers, + const std::string &body, + const char *content_type) { + auto ret = send_with_content_provider("PATCH", path, headers, body, 0, + nullptr, content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr Client::Patch(const char *path, - size_t content_length, - ContentProvider content_provider, - const char *content_type) { +inline Result ClientImpl::Patch(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { return Patch(path, Headers(), content_length, content_provider, content_type); } -inline std::shared_ptr -Client::Patch(const char *path, const Headers &headers, size_t content_length, - ContentProvider content_provider, const char *content_type) { - return send_with_content_provider("PATCH", path, headers, std::string(), - content_length, content_provider, - content_type); +inline Result ClientImpl::Patch(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + auto ret = send_with_content_provider("PATCH", path, headers, std::string(), + content_length, content_provider, + content_type); + return Result{ret, get_last_error()}; } -inline std::shared_ptr Client::Delete(const char *path) { +inline Result ClientImpl::Delete(const char *path) { return Delete(path, Headers(), std::string(), nullptr); } -inline std::shared_ptr Client::Delete(const char *path, - const std::string &body, - const char *content_type) { +inline Result ClientImpl::Delete(const char *path, const std::string &body, + const char *content_type) { return Delete(path, Headers(), body, content_type); } -inline std::shared_ptr Client::Delete(const char *path, - const Headers &headers) { +inline Result ClientImpl::Delete(const char *path, const Headers &headers) { return Delete(path, headers, std::string(), nullptr); } -inline std::shared_ptr Client::Delete(const char *path, - const Headers &headers, - const std::string &body, - const char *content_type) { +inline Result ClientImpl::Delete(const char *path, const Headers &headers, + const std::string &body, + const char *content_type) { Request req; req.method = "DELETE"; - req.headers = headers; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.path = path; if (content_type) { req.headers.emplace("Content-Type", content_type); } req.body = body; auto res = std::make_shared(); - - return send(req, *res) ? res : nullptr; + auto ret = send(req, *res); + return Result{ret ? res : nullptr, get_last_error()}; } -inline std::shared_ptr Client::Options(const char *path) { +inline Result ClientImpl::Options(const char *path) { return Options(path, Headers()); } -inline std::shared_ptr Client::Options(const char *path, - const Headers &headers) { +inline Result ClientImpl::Options(const char *path, const Headers &headers) { Request req; req.method = "OPTIONS"; + req.headers = default_headers_; + req.headers.insert(headers.begin(), headers.end()); req.path = path; - req.headers = headers; auto res = std::make_shared(); + auto ret = send(req, *res); + return Result{ret ? res : nullptr, get_last_error()}; +} - return send(req, *res) ? res : nullptr; +inline size_t ClientImpl::is_socket_open() const { + std::lock_guard guard(socket_mutex_); + return socket_.is_open(); } -inline void Client::set_timeout_sec(time_t timeout_sec) { - timeout_sec_ = timeout_sec; +inline void ClientImpl::stop() { + stop_core(); + error_ = Error::Canceled; } -inline void Client::set_read_timeout(time_t sec, time_t usec) { +inline void ClientImpl::stop_core() { + std::lock_guard guard(socket_mutex_); + if (socket_.is_open()) { + detail::shutdown_socket(socket_.sock); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + close_socket(socket_, true); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } +} + +inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { + connection_timeout_sec_ = sec; + connection_timeout_usec_ = usec; +} + +inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) { read_timeout_sec_ = sec; read_timeout_usec_ = usec; } -inline void Client::set_keep_alive_max_count(size_t count) { - keep_alive_max_count_ = count; +inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) { + write_timeout_sec_ = sec; + write_timeout_usec_ = usec; } -inline void Client::set_basic_auth(const char *username, const char *password) { +inline void ClientImpl::set_basic_auth(const char *username, + const char *password) { basic_auth_username_ = username; basic_auth_password_ = password; } +inline void ClientImpl::set_bearer_token_auth(const char *token) { + bearer_token_auth_token_ = token; +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_digest_auth(const char *username, - const char *password) { +inline void ClientImpl::set_digest_auth(const char *username, + const char *password) { digest_auth_username_ = username; digest_auth_password_ = password; } #endif -inline void Client::set_follow_location(bool on) { follow_location_ = on; } +inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; } -inline void Client::set_compress(bool on) { compress_ = on; } +inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; } -inline void Client::set_interface(const char *intf) { interface_ = intf; } +inline void ClientImpl::set_default_headers(Headers headers) { + default_headers_ = std::move(headers); +} -inline void Client::set_proxy(const char *host, int port) { +inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; } + +inline void ClientImpl::set_socket_options(SocketOptions socket_options) { + socket_options_ = socket_options; +} + +inline void ClientImpl::set_compress(bool on) { compress_ = on; } + +inline void ClientImpl::set_decompress(bool on) { decompress_ = on; } + +inline void ClientImpl::set_interface(const char *intf) { interface_ = intf; } + +inline void ClientImpl::set_proxy(const char *host, int port) { proxy_host_ = host; proxy_port_ = port; } -inline void Client::set_proxy_basic_auth(const char *username, - const char *password) { +inline void ClientImpl::set_proxy_basic_auth(const char *username, + const char *password) { proxy_basic_auth_username_ = username; proxy_basic_auth_password_ = password; } +inline void ClientImpl::set_proxy_bearer_token_auth(const char *token) { + proxy_bearer_token_auth_token_ = token; +} + #ifdef CPPHTTPLIB_OPENSSL_SUPPORT -inline void Client::set_proxy_digest_auth(const char *username, - const char *password) { +inline void ClientImpl::set_proxy_digest_auth(const char *username, + const char *password) { proxy_digest_auth_username_ = username; proxy_digest_auth_password_ = password; } #endif -inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void ClientImpl::enable_server_certificate_verification(bool enabled) { + server_certificate_verification_ = enabled; +} +#endif + +inline void ClientImpl::set_logger(Logger logger) { + logger_ = std::move(logger); +} /* * SSL Implementation @@ -4336,72 +5527,66 @@ inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); } #ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { -template -inline bool process_and_close_socket_ssl( - bool is_client_request, socket_t sock, size_t keep_alive_max_count, - time_t read_timeout_sec, time_t read_timeout_usec, SSL_CTX *ctx, - std::mutex &ctx_mutex, U SSL_connect_or_accept, V setup, T callback) { - assert(keep_alive_max_count > 0); - +template +inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex, + U SSL_connect_or_accept, V setup) { SSL *ssl = nullptr; { std::lock_guard guard(ctx_mutex); ssl = SSL_new(ctx); } - if (!ssl) { - close_socket(sock); - return false; - } - - auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); - SSL_set_bio(ssl, bio, bio); + if (ssl) { + auto bio = BIO_new_socket(static_cast(sock), BIO_NOCLOSE); + SSL_set_bio(ssl, bio, bio); - if (!setup(ssl)) { - SSL_shutdown(ssl); - { - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); + if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) { + SSL_shutdown(ssl); + { + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); + } + return nullptr; } - - close_socket(sock); - return false; } - auto ret = false; + return ssl; +} - if (SSL_connect_or_accept(ssl) == 1) { - if (keep_alive_max_count > 1) { - auto count = keep_alive_max_count; - while (count > 0 && - (is_client_request || - detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, - CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec); - auto last_connection = count == 1; - auto connection_close = false; - - ret = callback(ssl, strm, last_connection, connection_close); - if (!ret || connection_close) { break; } - - count--; - } - } else { - SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec); - auto dummy_connection_close = false; - ret = callback(ssl, strm, true, dummy_connection_close); - } +inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, + bool process_socket_ret) { + if (process_socket_ret) { + SSL_shutdown(ssl); // shutdown only if not already closed by remote } - SSL_shutdown(ssl); - { - std::lock_guard guard(ctx_mutex); - SSL_free(ssl); - } + std::lock_guard guard(ctx_mutex); + SSL_free(ssl); +} - close_socket(sock); +template +inline bool +process_server_socket_ssl(SSL *ssl, socket_t sock, size_t keep_alive_max_count, + time_t keep_alive_timeout_sec, + time_t read_timeout_sec, time_t read_timeout_usec, + time_t write_timeout_sec, time_t write_timeout_usec, + T callback) { + return process_server_socket_core( + sock, keep_alive_max_count, keep_alive_timeout_sec, + [&](bool close_connection, bool &connection_closed) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm, close_connection, connection_closed); + }); +} - return ret; +template +inline bool +process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec, + time_t read_timeout_usec, time_t write_timeout_sec, + time_t write_timeout_usec, T callback) { + SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec, + write_timeout_sec, write_timeout_usec); + return callback(strm); } #if OPENSSL_VERSION_NUMBER < 0x10100000L @@ -4420,11 +5605,11 @@ public: private: static void locking_callback(int mode, int type, const char * /*file*/, int /*line*/) { - auto &locks = *openSSL_locks_; + auto &lk = (*openSSL_locks_)[static_cast(type)]; if (mode & CRYPTO_LOCK) { - locks[type].lock(); + lk.lock(); } else { - locks[type].unlock(); + lk.unlock(); } } }; @@ -4458,9 +5643,30 @@ private: // SSL socket stream implementation inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, - time_t read_timeout_usec) + time_t read_timeout_usec, + time_t write_timeout_sec, + time_t write_timeout_usec) : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), - read_timeout_usec_(read_timeout_usec) {} + read_timeout_usec_(read_timeout_usec), + write_timeout_sec_(write_timeout_sec), + write_timeout_usec_(write_timeout_usec) { + { + timeval tv; + tv.tv_sec = static_cast(read_timeout_sec); + tv.tv_usec = static_cast(read_timeout_usec); + + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast(&tv), + sizeof(tv)); + } + { + timeval tv; + tv.tv_sec = static_cast(write_timeout_sec); + tv.tv_usec = static_cast(write_timeout_usec); + + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast(&tv), + sizeof(tv)); + } +} inline SSLSocketStream::~SSLSocketStream() {} @@ -4469,24 +5675,25 @@ inline bool SSLSocketStream::is_readable() const { } inline bool SSLSocketStream::is_writable() const { - return detail::select_write(sock_, 0, 0) > 0; + return detail::select_write(sock_, write_timeout_sec_, write_timeout_usec_) > + 0; } -inline int SSLSocketStream::read(char *ptr, size_t size) { - if (SSL_pending(ssl_) > 0 || - select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0) { +inline ssize_t SSLSocketStream::read(char *ptr, size_t size) { + if (SSL_pending(ssl_) > 0 || is_readable()) { return SSL_read(ssl_, ptr, static_cast(size)); } return -1; } -inline int SSLSocketStream::write(const char *ptr, size_t size) { +inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) { if (is_writable()) { return SSL_write(ssl_, ptr, static_cast(size)); } return -1; } -inline std::string SSLSocketStream::get_remote_addr() const { - return detail::get_remote_addr(sock_); +inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip, + int &port) const { + detail::get_remote_ip_and_port(sock_, ip, port); } static SSLInit sslinit_; @@ -4532,6 +5739,33 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, } } +inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key, + X509_STORE *client_ca_cert_store) { + ctx_ = SSL_CTX_new(SSLv23_server_method()); + + if (ctx_) { + SSL_CTX_set_options(ctx_, + SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | + SSL_OP_NO_COMPRESSION | + SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION); + + if (SSL_CTX_use_certificate(ctx_, cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } else if (client_ca_cert_store) { + + SSL_CTX_set_cert_store(ctx_, client_ca_cert_store); + + SSL_CTX_set_verify( + ctx_, + SSL_VERIFY_PEER | + SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE, + nullptr); + } + } +} + inline SSLServer::~SSLServer() { if (ctx_) { SSL_CTX_free(ctx_); } } @@ -4539,21 +5773,39 @@ inline SSLServer::~SSLServer() { inline bool SSLServer::is_valid() const { return ctx_; } inline bool SSLServer::process_and_close_socket(socket_t sock) { - return detail::process_and_close_socket_ssl( - false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_, - ctx_, ctx_mutex_, SSL_accept, [](SSL * /*ssl*/) { return true; }, - [this](SSL *ssl, Stream &strm, bool last_connection, - bool &connection_close) { - return process_request(strm, last_connection, connection_close, - [&](Request &req) { req.ssl = ssl; }); - }); + auto ssl = detail::ssl_new(sock, ctx_, ctx_mutex_, SSL_accept, + [](SSL * /*ssl*/) { return true; }); + + if (ssl) { + auto ret = detail::process_server_socket_ssl( + ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_, + read_timeout_sec_, read_timeout_usec_, write_timeout_sec_, + write_timeout_usec_, + [this, ssl](Stream &strm, bool close_connection, + bool &connection_closed) { + return process_request(strm, close_connection, connection_closed, + [&](Request &req) { req.ssl = ssl; }); + }); + + detail::ssl_delete(ctx_mutex_, ssl, ret); + return ret; + } + + detail::close_socket(sock); + return false; } // SSL HTTP client implementation +inline SSLClient::SSLClient(const std::string &host) + : SSLClient(host, 443, std::string(), std::string()) {} + +inline SSLClient::SSLClient(const std::string &host, int port) + : SSLClient(host, port, std::string(), std::string()) {} + inline SSLClient::SSLClient(const std::string &host, int port, const std::string &client_cert_path, const std::string &client_key_path) - : Client(host, port, client_cert_path, client_key_path) { + : ClientImpl(host, port, client_cert_path, client_key_path) { ctx_ = SSL_CTX_new(SSLv23_client_method()); detail::split(&host_[0], &host_[host_.size()], '.', @@ -4571,6 +5823,24 @@ inline SSLClient::SSLClient(const std::string &host, int port, } } +inline SSLClient::SSLClient(const std::string &host, int port, + X509 *client_cert, EVP_PKEY *client_key) + : ClientImpl(host, port) { + ctx_ = SSL_CTX_new(SSLv23_client_method()); + + detail::split(&host_[0], &host_[host_.size()], '.', + [&](const char *b, const char *e) { + host_components_.emplace_back(std::string(b, e)); + }); + if (client_cert != nullptr && client_key != nullptr) { + if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 || + SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) { + SSL_CTX_free(ctx_); + ctx_ = nullptr; + } + } +} + inline SSLClient::~SSLClient() { if (ctx_) { SSL_CTX_free(ctx_); } } @@ -4583,67 +5853,178 @@ inline void SSLClient::set_ca_cert_path(const char *ca_cert_file_path, if (ca_cert_dir_path) { ca_cert_dir_path_ = ca_cert_dir_path; } } -inline void SSLClient::enable_server_certificate_verification(bool enabled) { - server_certificate_verification_ = enabled; +inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (ca_cert_store) { + if (ctx_) { + if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) { + // Free memory allocated for old cert and use new store `ca_cert_store` + SSL_CTX_set_cert_store(ctx_, ca_cert_store); + } + } else { + X509_STORE_free(ca_cert_store); + } + } } inline long SSLClient::get_openssl_verify_result() const { return verify_result_; } -inline SSL_CTX *SSLClient::ssl_context() const noexcept { return ctx_; } +inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; } -inline bool SSLClient::process_and_close_socket( - socket_t sock, size_t request_count, - std::function - callback) { +inline bool SSLClient::create_and_connect_socket(Socket &socket) { + return is_valid() && ClientImpl::create_and_connect_socket(socket); +} - request_count = std::min(request_count, keep_alive_max_count_); +inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res, + bool &success) { + success = true; + Response res2; - return is_valid() && - detail::process_and_close_socket_ssl( - true, sock, request_count, read_timeout_sec_, read_timeout_usec_, - ctx_, ctx_mutex_, - [&](SSL *ssl) { - if (ca_cert_file_path_.empty()) { - SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr); - } else { - if (!SSL_CTX_load_verify_locations( - ctx_, ca_cert_file_path_.c_str(), nullptr)) { - return false; - } - SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER, nullptr); - } + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req2; + req2.method = "CONNECT"; + req2.path = host_and_port_; + return process_request(strm, req2, res2, false); + })) { + close_socket(socket, true); + success = false; + return false; + } - if (SSL_connect(ssl) != 1) { return false; } + if (res2.status == 407) { + if (!proxy_digest_auth_username_.empty() && + !proxy_digest_auth_password_.empty()) { + std::map auth; + if (detail::parse_www_authenticate(res2, auth, true)) { + Response res3; + if (!detail::process_client_socket( + socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) { + Request req3; + req3.method = "CONNECT"; + req3.path = host_and_port_; + req3.headers.insert(detail::make_digest_authentication_header( + req3, auth, 1, detail::random_string(10), + proxy_digest_auth_username_, proxy_digest_auth_password_, + true)); + return process_request(strm, req3, res3, false); + })) { + close_socket(socket, true); + success = false; + return false; + } + } + } else { + res = res2; + return false; + } + } + + return true; +} + +inline bool SSLClient::load_certs() { + bool ret = true; + + std::call_once(initialize_cert_, [&]() { + std::lock_guard guard(ctx_mutex_); + if (!ca_cert_file_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(), + nullptr)) { + ret = false; + } + } else if (!ca_cert_dir_path_.empty()) { + if (!SSL_CTX_load_verify_locations(ctx_, nullptr, + ca_cert_dir_path_.c_str())) { + ret = false; + } + } else { +#ifdef _WIN32 + detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_)); +#else + SSL_CTX_set_default_verify_paths(ctx_); +#endif + } + }); + + return ret; +} + +inline bool SSLClient::initialize_ssl(Socket &socket) { + auto ssl = detail::ssl_new( + socket.sock, ctx_, ctx_mutex_, + [&](SSL *ssl) { + if (server_certificate_verification_) { + if (!load_certs()) { + error_ = Error::SSLLoadingCerts; + return false; + } + SSL_set_verify(ssl, SSL_VERIFY_NONE, nullptr); + } - if (server_certificate_verification_) { - verify_result_ = SSL_get_verify_result(ssl); + if (SSL_connect(ssl) != 1) { + error_ = Error::SSLConnection; + return false; + } - if (verify_result_ != X509_V_OK) { return false; } + if (server_certificate_verification_) { + verify_result_ = SSL_get_verify_result(ssl); - auto server_cert = SSL_get_peer_certificate(ssl); + if (verify_result_ != X509_V_OK) { + error_ = Error::SSLServerVerification; + return false; + } - if (server_cert == nullptr) { return false; } + auto server_cert = SSL_get_peer_certificate(ssl); - if (!verify_host(server_cert)) { - X509_free(server_cert); - return false; - } - X509_free(server_cert); - } + if (server_cert == nullptr) { + error_ = Error::SSLServerVerification; + return false; + } + + if (!verify_host(server_cert)) { + X509_free(server_cert); + error_ = Error::SSLServerVerification; + return false; + } + X509_free(server_cert); + } + + return true; + }, + [&](SSL *ssl) { + SSL_set_tlsext_host_name(ssl, host_.c_str()); + return true; + }); + + if (ssl) { + socket.ssl = ssl; + return true; + } - return true; - }, - [&](SSL *ssl) { - SSL_set_tlsext_host_name(ssl, host_.c_str()); - return true; - }, - [&](SSL * /*ssl*/, Stream &strm, bool last_connection, - bool &connection_close) { - return callback(strm, last_connection, connection_close); - }); + close_socket(socket, false); + return false; +} + +inline void SSLClient::close_socket(Socket &socket, bool process_socket_ret) { + detail::close_socket(socket.sock); + socket_.sock = INVALID_SOCKET; + if (socket.ssl) { + detail::ssl_delete(ctx_mutex_, socket.ssl, process_socket_ret); + socket_.ssl = nullptr; + } +} + +inline bool +SSLClient::process_socket(Socket &socket, + std::function callback) { + assert(socket.ssl); + return detail::process_client_socket_ssl( + socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_, + write_timeout_sec_, write_timeout_usec_, callback); } inline bool SSLClient::is_ssl() const { return true; } @@ -4703,7 +6084,7 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { auto count = sk_GENERAL_NAME_num(alt_names); - for (auto i = 0; i < count && !dsn_matched; i++) { + for (decltype(count) i = 0; i < count && !dsn_matched; i++) { auto val = sk_GENERAL_NAME_value(alt_names, i); if (val->type == type) { auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5); @@ -4728,7 +6109,6 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const { } GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); - return ret; } @@ -4740,7 +6120,9 @@ inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const { auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, name, sizeof(name)); - if (name_len != -1) { return check_host_name(name, name_len); } + if (name_len != -1) { + return check_host_name(name, static_cast(name_len)); + } } return false; @@ -4775,6 +6157,327 @@ inline bool SSLClient::check_host_name(const char *pattern, } #endif +// Universal client implementation +inline Client::Client(const char *scheme_host_port) + : Client(scheme_host_port, std::string(), std::string()) {} + +inline Client::Client(const char *scheme_host_port, + const std::string &client_cert_path, + const std::string &client_key_path) { + const static std::regex re(R"(^(?:([a-z]+)://)?([^:/?#]+)(?::(\d+))?)"); + + std::cmatch m; + if (std::regex_match(scheme_host_port, m, re)) { + auto scheme = m[1].str(); + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + if (!scheme.empty() && (scheme != "http" && scheme != "https")) { +#else + if (!scheme.empty() && scheme != "http") { +#endif + return; + } + + auto is_ssl = scheme == "https"; + + auto host = m[2].str(); + + auto port_str = m[3].str(); + auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80); + + if (is_ssl) { +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT + cli_ = std::make_shared(host.c_str(), port, client_cert_path, + client_key_path); + is_ssl_ = is_ssl; +#endif + } else { + cli_ = std::make_shared(host.c_str(), port, client_cert_path, + client_key_path); + } + } else { + cli_ = std::make_shared(scheme_host_port, 80, client_cert_path, + client_key_path); + } +} + +inline Client::Client(const std::string &host, int port) + : cli_(std::make_shared(host, port)) {} + +inline Client::Client(const std::string &host, int port, + const std::string &client_cert_path, + const std::string &client_key_path) + : cli_(std::make_shared(host, port, client_cert_path, + client_key_path)) {} + +inline Client::~Client() {} + +inline bool Client::is_valid() const { + return cli_ != nullptr && cli_->is_valid(); +} + +inline Result Client::Get(const char *path) { return cli_->Get(path); } +inline Result Client::Get(const char *path, const Headers &headers) { + return cli_->Get(path, headers); +} +inline Result Client::Get(const char *path, Progress progress) { + return cli_->Get(path, progress); +} +inline Result Client::Get(const char *path, const Headers &headers, + Progress progress) { + return cli_->Get(path, headers, progress); +} +inline Result Client::Get(const char *path, ContentReceiver content_receiver) { + return cli_->Get(path, std::move(content_receiver)); +} +inline Result Client::Get(const char *path, const Headers &headers, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(content_receiver)); +} +inline Result Client::Get(const char *path, ContentReceiver content_receiver, + Progress progress) { + return cli_->Get(path, std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const char *path, const Headers &headers, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, std::move(content_receiver), + std::move(progress)); +} +inline Result Client::Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver) { + return cli_->Get(path, headers, std::move(response_handler), + std::move(content_receiver)); +} +inline Result Client::Get(const char *path, ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, std::move(response_handler), + std::move(content_receiver), std::move(progress)); +} +inline Result Client::Get(const char *path, const Headers &headers, + ResponseHandler response_handler, + ContentReceiver content_receiver, Progress progress) { + return cli_->Get(path, headers, response_handler, content_receiver, progress); +} + +inline Result Client::Head(const char *path) { return cli_->Head(path); } +inline Result Client::Head(const char *path, const Headers &headers) { + return cli_->Head(path, headers); +} + +inline Result Client::Post(const char *path) { return cli_->Post(path); } +inline Result Client::Post(const char *path, const std::string &body, + const char *content_type) { + return cli_->Post(path, body, content_type); +} +inline Result Client::Post(const char *path, const Headers &headers, + const std::string &body, const char *content_type) { + return cli_->Post(path, headers, body, content_type); +} +inline Result Client::Post(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Post(path, content_length, content_provider, content_type); +} +inline Result Client::Post(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Post(path, headers, content_length, content_provider, + content_type); +} +inline Result Client::Post(const char *path, const Params ¶ms) { + return cli_->Post(path, params); +} +inline Result Client::Post(const char *path, const Headers &headers, + const Params ¶ms) { + return cli_->Post(path, headers, params); +} +inline Result Client::Post(const char *path, + const MultipartFormDataItems &items) { + return cli_->Post(path, items); +} +inline Result Client::Post(const char *path, const Headers &headers, + const MultipartFormDataItems &items) { + return cli_->Post(path, headers, items); +} +inline Result Client::Put(const char *path) { return cli_->Put(path); } +inline Result Client::Put(const char *path, const std::string &body, + const char *content_type) { + return cli_->Put(path, body, content_type); +} +inline Result Client::Put(const char *path, const Headers &headers, + const std::string &body, const char *content_type) { + return cli_->Put(path, headers, body, content_type); +} +inline Result Client::Put(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Put(path, content_length, content_provider, content_type); +} +inline Result Client::Put(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Put(path, headers, content_length, content_provider, + content_type); +} +inline Result Client::Put(const char *path, const Params ¶ms) { + return cli_->Put(path, params); +} +inline Result Client::Put(const char *path, const Headers &headers, + const Params ¶ms) { + return cli_->Put(path, headers, params); +} +inline Result Client::Patch(const char *path, const std::string &body, + const char *content_type) { + return cli_->Patch(path, body, content_type); +} +inline Result Client::Patch(const char *path, const Headers &headers, + const std::string &body, const char *content_type) { + return cli_->Patch(path, headers, body, content_type); +} +inline Result Client::Patch(const char *path, size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Patch(path, content_length, content_provider, content_type); +} +inline Result Client::Patch(const char *path, const Headers &headers, + size_t content_length, + ContentProvider content_provider, + const char *content_type) { + return cli_->Patch(path, headers, content_length, content_provider, + content_type); +} +inline Result Client::Delete(const char *path) { return cli_->Delete(path); } +inline Result Client::Delete(const char *path, const std::string &body, + const char *content_type) { + return cli_->Delete(path, body, content_type); +} +inline Result Client::Delete(const char *path, const Headers &headers) { + return cli_->Delete(path, headers); +} +inline Result Client::Delete(const char *path, const Headers &headers, + const std::string &body, + const char *content_type) { + return cli_->Delete(path, headers, body, content_type); +} +inline Result Client::Options(const char *path) { return cli_->Options(path); } +inline Result Client::Options(const char *path, const Headers &headers) { + return cli_->Options(path, headers); +} + +inline bool Client::send(const Request &req, Response &res) { + return cli_->send(req, res); +} + +inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } + +inline void Client::stop() { cli_->stop(); } + +inline void Client::set_default_headers(Headers headers) { + cli_->set_default_headers(std::move(headers)); +} + +inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); } +inline void Client::set_socket_options(SocketOptions socket_options) { + cli_->set_socket_options(socket_options); +} + +inline void Client::set_connection_timeout(time_t sec, time_t usec) { + cli_->set_connection_timeout(sec, usec); +} +inline void Client::set_read_timeout(time_t sec, time_t usec) { + cli_->set_read_timeout(sec, usec); +} +inline void Client::set_write_timeout(time_t sec, time_t usec) { + cli_->set_write_timeout(sec, usec); +} + +inline void Client::set_basic_auth(const char *username, const char *password) { + cli_->set_basic_auth(username, password); +} +inline void Client::set_bearer_token_auth(const char *token) { + cli_->set_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_digest_auth(const char *username, + const char *password) { + cli_->set_digest_auth(username, password); +} +#endif + +inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); } +inline void Client::set_follow_location(bool on) { + cli_->set_follow_location(on); +} + +inline void Client::set_compress(bool on) { cli_->set_compress(on); } + +inline void Client::set_decompress(bool on) { cli_->set_decompress(on); } + +inline void Client::set_interface(const char *intf) { + cli_->set_interface(intf); +} + +inline void Client::set_proxy(const char *host, int port) { + cli_->set_proxy(host, port); +} +inline void Client::set_proxy_basic_auth(const char *username, + const char *password) { + cli_->set_proxy_basic_auth(username, password); +} +inline void Client::set_proxy_bearer_token_auth(const char *token) { + cli_->set_proxy_bearer_token_auth(token); +} +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_proxy_digest_auth(const char *username, + const char *password) { + cli_->set_proxy_digest_auth(username, password); +} +#endif + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::enable_server_certificate_verification(bool enabled) { + cli_->enable_server_certificate_verification(enabled); +} +#endif + +inline void Client::set_logger(Logger logger) { cli_->set_logger(logger); } + +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +inline void Client::set_ca_cert_path(const char *ca_cert_file_path, + const char *ca_cert_dir_path) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_path(ca_cert_file_path, + ca_cert_dir_path); + } +} + +inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) { + if (is_ssl_) { + static_cast(*cli_).set_ca_cert_store(ca_cert_store); + } +} + +inline long Client::get_openssl_verify_result() const { + if (is_ssl_) { + return static_cast(*cli_).get_openssl_verify_result(); + } + return -1; // NOTE: -1 doesn't match any of X509_V_ERR_??? +} + +inline SSL_CTX *Client::ssl_context() const { + if (is_ssl_) { return static_cast(*cli_).ssl_context(); } + return nullptr; +} +#endif + // ---------------------------------------------------------------------------- } // namespace httplib diff --git a/src/common/hex_util.h b/src/common/hex_util.h index 120f1a5e6..a8d414fb8 100644 --- a/src/common/hex_util.h +++ b/src/common/hex_util.h @@ -16,14 +16,14 @@ namespace Common { [[nodiscard]] constexpr u8 ToHexNibble(char c) { if (c >= 65 && c <= 70) { - return c - 55; + return static_cast(c - 55); } if (c >= 97 && c <= 102) { - return c - 87; + return static_cast(c - 87); } - return c - 48; + return static_cast(c - 48); } [[nodiscard]] std::vector HexStringToVector(std::string_view str, bool little_endian); @@ -33,11 +33,11 @@ template std::array out{}; if constexpr (le) { for (std::size_t i = 2 * Size - 2; i <= 2 * Size; i -= 2) { - out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); } } else { for (std::size_t i = 0; i < 2 * Size; i += 2) { - out[i / 2] = (ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1]); + out[i / 2] = static_cast((ToHexNibble(str[i]) << 4) | ToHexNibble(str[i + 1])); } } return out; diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d0c405ec7..9760be4e4 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -623,6 +623,17 @@ if (MSVC) # 'context' : truncation from 'type1' to 'type2' /we4305 ) +else() + target_compile_options(core PRIVATE + -Werror=conversion + -Werror=ignored-qualifiers + -Werror=implicit-fallthrough + -Werror=reorder + -Werror=sign-compare + -Werror=unused-but-set-parameter + -Werror=unused-but-set-variable + -Werror=unused-variable + ) endif() create_target_directory_groups(core) diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 65d246050..da15f764a 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -411,7 +411,7 @@ Loader::ResultStatus DeriveSDKeys(std::array& sd_keys, KeyManager& ke // Combine sources and seed for (auto& source : sd_key_sources) { for (std::size_t i = 0; i < source.size(); ++i) { - source[i] ^= sd_seed[i & 0xF]; + source[i] = static_cast(source[i] ^ sd_seed[i & 0xF]); } } diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp index 2aff2708a..c52fafb6f 100644 --- a/src/core/file_sys/fsmitm_romfsbuild.cpp +++ b/src/core/file_sys/fsmitm_romfsbuild.cpp @@ -266,8 +266,9 @@ std::multimap RomFSBuildContext::Build() { cur_file->offset = file_partition_size; file_partition_size += cur_file->size; cur_file->entry_offset = entry_offset; - entry_offset += sizeof(RomFSFileEntry) + - Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4); + entry_offset += + static_cast(sizeof(RomFSFileEntry) + + Common::AlignUp(cur_file->path_len - cur_file->cur_path_ofs, 4)); prev_file = cur_file; } // Assign deferred parent/sibling ownership. @@ -284,8 +285,9 @@ std::multimap RomFSBuildContext::Build() { for (const auto& it : directories) { cur_dir = it.second; cur_dir->entry_offset = entry_offset; - entry_offset += sizeof(RomFSDirectoryEntry) + - Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4); + entry_offset += + static_cast(sizeof(RomFSDirectoryEntry) + + Common::AlignUp(cur_dir->path_len - cur_dir->cur_path_ofs, 4)); } // Assign deferred parent/sibling ownership. for (auto it = directories.rbegin(); it->second != root; ++it) { diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index dd779310f..a6101f1c0 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -299,7 +299,7 @@ void IPSwitchCompiler::Parse() { patch_text->GetName(), offset, Common::HexToString(replace)); } - patch.records.insert_or_assign(offset, std::move(replace)); + patch.records.insert_or_assign(static_cast(offset), std::move(replace)); } patches.push_back(std::move(patch)); diff --git a/src/core/file_sys/nca_metadata.cpp b/src/core/file_sys/nca_metadata.cpp index 2d1476e3a..3596541b2 100644 --- a/src/core/file_sys/nca_metadata.cpp +++ b/src/core/file_sys/nca_metadata.cpp @@ -108,7 +108,7 @@ std::vector CNMT::Serialize() const { memcpy(out.data() + sizeof(CNMTHeader), &opt_header, sizeof(OptionalHeader)); } - auto offset = header.table_offset; + u64_le offset = header.table_offset; for (const auto& rec : content_records) { memcpy(out.data() + offset + sizeof(CNMTHeader), &rec, sizeof(ContentRecord)); diff --git a/src/core/file_sys/patch_manager.cpp b/src/core/file_sys/patch_manager.cpp index b9c09b456..807b05821 100644 --- a/src/core/file_sys/patch_manager.cpp +++ b/src/core/file_sys/patch_manager.cpp @@ -29,7 +29,7 @@ namespace FileSys { namespace { -constexpr u64 SINGLE_BYTE_MODULUS = 0x100; +constexpr u32 SINGLE_BYTE_MODULUS = 0x100; constexpr u64 DLC_BASE_TITLE_ID_MASK = 0xFFFFFFFFFFFFE000; constexpr std::array EXEFS_FILE_NAMES{ diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp index 9a081fbd4..8c1193894 100644 --- a/src/core/frontend/emu_window.cpp +++ b/src/core/frontend/emu_window.cpp @@ -84,10 +84,12 @@ void EmuWindow::TouchPressed(unsigned framebuffer_x, unsigned framebuffer_y) { return; std::lock_guard guard{touch_state->mutex}; - touch_state->touch_x = static_cast(framebuffer_x - framebuffer_layout.screen.left) / - (framebuffer_layout.screen.right - framebuffer_layout.screen.left); - touch_state->touch_y = static_cast(framebuffer_y - framebuffer_layout.screen.top) / - (framebuffer_layout.screen.bottom - framebuffer_layout.screen.top); + touch_state->touch_x = + static_cast(framebuffer_x - framebuffer_layout.screen.left) / + static_cast(framebuffer_layout.screen.right - framebuffer_layout.screen.left); + touch_state->touch_y = + static_cast(framebuffer_y - framebuffer_layout.screen.top) / + static_cast(framebuffer_layout.screen.bottom - framebuffer_layout.screen.top); touch_state->touch_pressed = true; } diff --git a/src/core/frontend/framebuffer_layout.cpp b/src/core/frontend/framebuffer_layout.cpp index c1fbc235b..1acc82497 100644 --- a/src/core/frontend/framebuffer_layout.cpp +++ b/src/core/frontend/framebuffer_layout.cpp @@ -14,8 +14,8 @@ namespace Layout { template static Common::Rectangle MaxRectangle(Common::Rectangle window_area, float screen_aspect_ratio) { - float scale = std::min(static_cast(window_area.GetWidth()), - window_area.GetHeight() / screen_aspect_ratio); + const float scale = std::min(static_cast(window_area.GetWidth()), + static_cast(window_area.GetHeight()) / screen_aspect_ratio); return Common::Rectangle{0, 0, static_cast(std::round(scale)), static_cast(std::round(scale * screen_aspect_ratio))}; } @@ -27,7 +27,7 @@ FramebufferLayout DefaultFrameLayout(u32 width, u32 height) { // so just calculate them both even if the other isn't showing. FramebufferLayout res{width, height, false, {}}; - const float window_aspect_ratio = static_cast(height) / width; + const float window_aspect_ratio = static_cast(height) / static_cast(width); const float emulation_aspect_ratio = EmulationAspectRatio( static_cast(Settings::values.aspect_ratio.GetValue()), window_aspect_ratio); diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp index 79f22a403..97ee65464 100644 --- a/src/core/gdbstub/gdbstub.cpp +++ b/src/core/gdbstub/gdbstub.cpp @@ -291,11 +291,11 @@ static void FpuWrite(std::size_t id, u128 val, Kernel::Thread* thread = nullptr) */ static u8 HexCharToValue(u8 hex) { if (hex >= '0' && hex <= '9') { - return hex - '0'; + return static_cast(hex - '0'); } else if (hex >= 'a' && hex <= 'f') { - return hex - 'a' + 0xA; + return static_cast(hex - 'a' + 0xA); } else if (hex >= 'A' && hex <= 'F') { - return hex - 'A' + 0xA; + return static_cast(hex - 'A' + 0xA); } LOG_ERROR(Debug_GDBStub, "Invalid nibble: {} ({:02X})", hex, hex); @@ -310,9 +310,9 @@ static u8 HexCharToValue(u8 hex) { static u8 NibbleToHex(u8 n) { n &= 0xF; if (n < 0xA) { - return '0' + n; + return static_cast('0' + n); } else { - return 'a' + n - 0xA; + return static_cast('a' + n - 0xA); } } @@ -355,8 +355,8 @@ static u64 HexToLong(const u8* src, std::size_t len) { */ static void MemToGdbHex(u8* dest, const u8* src, std::size_t len) { while (len-- > 0) { - u8 tmp = *src++; - *dest++ = NibbleToHex(tmp >> 4); + const u8 tmp = *src++; + *dest++ = NibbleToHex(static_cast(tmp >> 4)); *dest++ = NibbleToHex(tmp); } } @@ -370,7 +370,7 @@ static void MemToGdbHex(u8* dest, const u8* src, std::size_t len) { */ static void GdbHexToMem(u8* dest, const u8* src, std::size_t len) { while (len-- > 0) { - *dest++ = (HexCharToValue(src[0]) << 4) | HexCharToValue(src[1]); + *dest++ = static_cast((HexCharToValue(src[0]) << 4) | HexCharToValue(src[1])); src += 2; } } @@ -602,22 +602,22 @@ static void SendReply(const char* reply) { memcpy(command_buffer + 1, reply, command_length); - u8 checksum = CalculateChecksum(command_buffer, command_length + 1); + const u8 checksum = CalculateChecksum(command_buffer, command_length + 1); command_buffer[0] = GDB_STUB_START; command_buffer[command_length + 1] = GDB_STUB_END; - command_buffer[command_length + 2] = NibbleToHex(checksum >> 4); + command_buffer[command_length + 2] = NibbleToHex(static_cast(checksum >> 4)); command_buffer[command_length + 3] = NibbleToHex(checksum); u8* ptr = command_buffer; u32 left = command_length + 4; while (left > 0) { - int sent_size = send(gdbserver_socket, reinterpret_cast(ptr), left, 0); + const auto sent_size = send(gdbserver_socket, reinterpret_cast(ptr), left, 0); if (sent_size < 0) { LOG_ERROR(Debug_GDBStub, "gdb: send failed"); return Shutdown(); } - left -= sent_size; + left -= static_cast(sent_size); ptr += sent_size; } } @@ -777,10 +777,10 @@ static void ReadCommand() { command_buffer[command_length++] = c; } - u8 checksum_received = HexCharToValue(ReadByte()) << 4; - checksum_received |= HexCharToValue(ReadByte()); + auto checksum_received = static_cast(HexCharToValue(ReadByte()) << 4); + checksum_received |= static_cast(HexCharToValue(ReadByte())); - u8 checksum_calculated = CalculateChecksum(command_buffer, command_length); + const u32 checksum_calculated = CalculateChecksum(command_buffer, command_length); if (checksum_received != checksum_calculated) { LOG_ERROR(Debug_GDBStub, diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 1b503331f..1c354037d 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -38,10 +38,11 @@ public: explicit RequestHelperBase(Kernel::HLERequestContext& context) : context(&context), cmdbuf(context.CommandBuffer()) {} - void Skip(unsigned size_in_words, bool set_to_null) { - if (set_to_null) + void Skip(u32 size_in_words, bool set_to_null) { + if (set_to_null) { memset(cmdbuf + index, 0, size_in_words * sizeof(u32)); - index += size_in_words; + } + index += static_cast(size_in_words); } /** @@ -49,15 +50,15 @@ public: */ void AlignWithPadding() { if (index & 3) { - Skip(4 - (index & 3), true); + Skip(static_cast(4 - (index & 3)), true); } } - unsigned GetCurrentOffset() const { - return static_cast(index); + u32 GetCurrentOffset() const { + return static_cast(index); } - void SetCurrentOffset(unsigned offset) { + void SetCurrentOffset(u32 offset) { index = static_cast(offset); } }; @@ -89,7 +90,7 @@ public: // The entire size of the raw data section in u32 units, including the 16 bytes of mandatory // padding. - u32 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size; + u64 raw_data_size = sizeof(IPC::DataPayloadHeader) / 4 + 4 + normal_params_size; u32 num_handles_to_move{}; u32 num_domain_objects{}; @@ -105,7 +106,7 @@ public: raw_data_size += sizeof(DomainMessageHeader) / 4 + num_domain_objects; } - header.data_size.Assign(raw_data_size); + header.data_size.Assign(static_cast(raw_data_size)); if (num_handles_to_copy || num_handles_to_move) { header.enable_handle_descriptor.Assign(1); } diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index fb30b6f8b..3e745c18b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -118,7 +118,7 @@ std::shared_ptr HandleTable::GetGeneric(Handle handle) const { void HandleTable::Clear() { for (u16 i = 0; i < table_size; ++i) { - generations[i] = i + 1; + generations[i] = static_cast(i + 1); objects[i] = nullptr; } next_free_slot = 0; diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp index 5cbd3b912..6b7db5372 100644 --- a/src/core/hle/kernel/scheduler.cpp +++ b/src/core/hle/kernel/scheduler.cpp @@ -72,7 +72,7 @@ u32 GlobalScheduler::SelectThreads() { if (top_thread != nullptr) { // TODO(Blinkhawk): Implement Thread Pinning } else { - idle_cores |= (1ul << core); + idle_cores |= (1U << core); } top_threads[core] = top_thread; } @@ -126,7 +126,7 @@ u32 GlobalScheduler::SelectThreads() { top_threads[core_id] = suggested; } - idle_cores &= ~(1ul << core_id); + idle_cores &= ~(1U << core_id); } u32 cores_needing_context_switch{}; for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) { @@ -134,7 +134,7 @@ u32 GlobalScheduler::SelectThreads() { ASSERT(top_threads[core] == nullptr || static_cast(top_threads[core]->GetProcessorID()) == core); if (update_thread(top_threads[core], sched)) { - cores_needing_context_switch |= (1ul << core); + cores_needing_context_switch |= (1U << core); } } return cores_needing_context_switch; @@ -364,7 +364,7 @@ void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule, } else { must_context_switch = true; } - cores_pending_reschedule &= ~(1ul << core); + cores_pending_reschedule &= ~(1U << core); } if (must_context_switch) { auto& core_scheduler = kernel.CurrentScheduler(); @@ -767,7 +767,7 @@ void Scheduler::SwitchToCurrent() { current_thread->context_guard.unlock(); break; } - if (current_thread->GetProcessorID() != core_id) { + if (static_cast(current_thread->GetProcessorID()) != core_id) { current_thread->context_guard.unlock(); break; } diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp index ca021a99f..589e288df 100644 --- a/src/core/hle/service/bcat/backend/boxcat.cpp +++ b/src/core/hle/service/bcat/backend/boxcat.cpp @@ -196,7 +196,9 @@ private: const std::string& content_type_name) { if (client == nullptr) { client = std::make_unique(BOXCAT_HOSTNAME, PORT); - client->set_timeout_sec(timeout_seconds); + client->set_connection_timeout(timeout_seconds); + client->set_read_timeout(timeout_seconds); + client->set_write_timeout(timeout_seconds); } httplib::Headers headers{ @@ -255,7 +257,7 @@ private: return out; } - std::unique_ptr client; + std::unique_ptr client; std::string path; u64 title_id; u64 build_id; @@ -443,7 +445,9 @@ std::optional> Boxcat::GetLaunchParameter(TitleIDVersion title) Boxcat::StatusResult Boxcat::GetStatus(std::optional& global, std::map& games) { httplib::SSLClient client{BOXCAT_HOSTNAME, static_cast(PORT)}; - client.set_timeout_sec(static_cast(TIMEOUT_SECONDS)); + client.set_connection_timeout(static_cast(TIMEOUT_SECONDS)); + client.set_read_timeout(static_cast(TIMEOUT_SECONDS)); + client.set_write_timeout(static_cast(TIMEOUT_SECONDS)); httplib::Headers headers{ {std::string("Game-Assets-API-Version"), std::string(BOXCAT_API_VERSION)}, diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index 0b896d5ad..59b694cd4 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp @@ -42,8 +42,8 @@ void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, cur_entry.modifier = 0; if (Settings::values.keyboard_enabled) { for (std::size_t i = 0; i < keyboard_keys.size(); ++i) { - cur_entry.key[i / KEYS_PER_BYTE] |= - (keyboard_keys[i]->GetStatus() << (i % KEYS_PER_BYTE)); + auto& entry = cur_entry.key[i / KEYS_PER_BYTE]; + entry = static_cast(entry | (keyboard_keys[i]->GetStatus() << (i % KEYS_PER_BYTE))); } for (std::size_t i = 0; i < keyboard_mods.size(); ++i) { diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 2de4ed348..e311bc18c 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -269,7 +269,6 @@ void Controller_NPad::RequestPadStateUpdate(u32 npad_id) { auto& rstick_entry = npad_pad_states[controller_idx].r_stick; const auto& button_state = buttons[controller_idx]; const auto& analog_state = sticks[controller_idx]; - const auto& motion_state = motions[controller_idx]; const auto [stick_l_x_f, stick_l_y_f] = analog_state[static_cast(JoystickId::Joystick_Left)]->GetStatus(); const auto [stick_r_x_f, stick_r_y_f] = @@ -391,18 +390,6 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* libnx_entry.connection_status.raw = 0; libnx_entry.connection_status.IsConnected.Assign(1); - auto& full_sixaxis_entry = - npad.sixaxis_full.sixaxis[npad.sixaxis_full.common.last_entry_index]; - auto& handheld_sixaxis_entry = - npad.sixaxis_handheld.sixaxis[npad.sixaxis_handheld.common.last_entry_index]; - auto& dual_left_sixaxis_entry = - npad.sixaxis_dual_left.sixaxis[npad.sixaxis_dual_left.common.last_entry_index]; - auto& dual_right_sixaxis_entry = - npad.sixaxis_dual_right.sixaxis[npad.sixaxis_dual_right.common.last_entry_index]; - auto& left_sixaxis_entry = - npad.sixaxis_left.sixaxis[npad.sixaxis_left.common.last_entry_index]; - auto& right_sixaxis_entry = - npad.sixaxis_right.sixaxis[npad.sixaxis_right.common.last_entry_index]; switch (controller_type) { case NPadControllerType::None: @@ -541,18 +528,6 @@ void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing } } - auto& main_controller = - npad.main_controller_states.npad[npad.main_controller_states.common.last_entry_index]; - auto& handheld_entry = - npad.handheld_states.npad[npad.handheld_states.common.last_entry_index]; - auto& dual_entry = npad.dual_states.npad[npad.dual_states.common.last_entry_index]; - auto& left_entry = npad.left_joy_states.npad[npad.left_joy_states.common.last_entry_index]; - auto& right_entry = - npad.right_joy_states.npad[npad.right_joy_states.common.last_entry_index]; - auto& pokeball_entry = - npad.pokeball_states.npad[npad.pokeball_states.common.last_entry_index]; - auto& libnx_entry = npad.libnx.npad[npad.libnx.common.last_entry_index]; - auto& full_sixaxis_entry = npad.sixaxis_full.sixaxis[npad.sixaxis_full.common.last_entry_index]; auto& handheld_sixaxis_entry = diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 71dbaba7f..8918946a1 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -475,7 +475,7 @@ void Hid::StopSixAxisSensor(Kernel::HLERequestContext& ctx) { void Hid::EnableSixAxisSensorFusion(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto enable{rp.Pop()}; + [[maybe_unused]] const auto enable{rp.Pop()}; const auto handle{rp.Pop()}; const auto applet_resource_user_id{rp.Pop()}; diff --git a/src/core/hle/service/mii/manager.cpp b/src/core/hle/service/mii/manager.cpp index 4730070cb..8e433eb41 100644 --- a/src/core/hle/service/mii/manager.cpp +++ b/src/core/hle/service/mii/manager.cpp @@ -428,7 +428,7 @@ bool MiiManager::IsFullDatabase() const { } u32 MiiManager::GetCount(SourceFlag source_flag) const { - u32 count{}; + std::size_t count{}; if ((source_flag & SourceFlag::Database) != SourceFlag::None) { // TODO(bunnei): We don't implement the Mii database, but when we do, update this count += 0; @@ -436,7 +436,7 @@ u32 MiiManager::GetCount(SourceFlag source_flag) const { if ((source_flag & SourceFlag::Default) != SourceFlag::None) { count += DefaultMiiCount; } - return count; + return static_cast(count); } ResultVal MiiManager::UpdateLatest([[maybe_unused]] const MiiInfo& info, diff --git a/src/core/hle/service/sockets/sockets_translate.cpp b/src/core/hle/service/sockets/sockets_translate.cpp index 139743e1d..2e626fd86 100644 --- a/src/core/hle/service/sockets/sockets_translate.cpp +++ b/src/core/hle/service/sockets/sockets_translate.cpp @@ -89,9 +89,9 @@ Network::Protocol Translate(Type type, Protocol protocol) { } } -u16 TranslatePollEventsToHost(u16 flags) { - u16 result = 0; - const auto translate = [&result, &flags](u16 from, u16 to) { +u16 TranslatePollEventsToHost(u32 flags) { + u32 result = 0; + const auto translate = [&result, &flags](u32 from, u32 to) { if ((flags & from) != 0) { flags &= ~from; result |= to; @@ -105,12 +105,12 @@ u16 TranslatePollEventsToHost(u16 flags) { translate(POLL_NVAL, Network::POLL_NVAL); UNIMPLEMENTED_IF_MSG(flags != 0, "Unimplemented flags={}", flags); - return result; + return static_cast(result); } -u16 TranslatePollEventsToGuest(u16 flags) { - u16 result = 0; - const auto translate = [&result, &flags](u16 from, u16 to) { +u16 TranslatePollEventsToGuest(u32 flags) { + u32 result = 0; + const auto translate = [&result, &flags](u32 from, u32 to) { if ((flags & from) != 0) { flags &= ~from; result |= to; @@ -125,7 +125,7 @@ u16 TranslatePollEventsToGuest(u16 flags) { translate(Network::POLL_NVAL, POLL_NVAL); UNIMPLEMENTED_IF_MSG(flags != 0, "Unimplemented flags={}", flags); - return result; + return static_cast(result); } Network::SockAddrIn Translate(SockAddrIn value) { diff --git a/src/core/hle/service/sockets/sockets_translate.h b/src/core/hle/service/sockets/sockets_translate.h index 8ed041e31..e498913d4 100644 --- a/src/core/hle/service/sockets/sockets_translate.h +++ b/src/core/hle/service/sockets/sockets_translate.h @@ -31,10 +31,10 @@ Network::Type Translate(Type type); Network::Protocol Translate(Type type, Protocol protocol); /// Translate abstract poll event flags to guest poll event flags -u16 TranslatePollEventsToHost(u16 flags); +u16 TranslatePollEventsToHost(u32 flags); /// Translate guest poll event flags to abstract poll event flags -u16 TranslatePollEventsToGuest(u16 flags); +u16 TranslatePollEventsToGuest(u32 flags); /// Translate guest socket address structure to abstract socket address structure Network::SockAddrIn Translate(SockAddrIn value); diff --git a/src/core/hle/service/time/time_zone_manager.cpp b/src/core/hle/service/time/time_zone_manager.cpp index 69152d0ac..bdf0439f2 100644 --- a/src/core/hle/service/time/time_zone_manager.cpp +++ b/src/core/hle/service/time/time_zone_manager.cpp @@ -820,7 +820,10 @@ static ResultCode ToCalendarTimeImpl(const TimeZoneRule& rules, s64 time, Calend const ResultCode result{ ToCalendarTimeInternal(rules, time, calendar_time, calendar.additiona_info)}; calendar.time.year = static_cast(calendar_time.year); - calendar.time.month = calendar_time.month + 1; // Internal impl. uses 0-indexed month + + // Internal impl. uses 0-indexed month + calendar.time.month = static_cast(calendar_time.month + 1); + calendar.time.day = calendar_time.day; calendar.time.hour = calendar_time.hour; calendar.time.minute = calendar_time.minute; @@ -872,13 +875,15 @@ ResultCode TimeZoneManager::ToPosixTime(const TimeZoneRule& rules, const CalendarTime& calendar_time, s64& posix_time) const { posix_time = 0; - CalendarTimeInternal internal_time{}; - internal_time.year = calendar_time.year; - internal_time.month = calendar_time.month - 1; // Internal impl. uses 0-indexed month - internal_time.day = calendar_time.day; - internal_time.hour = calendar_time.hour; - internal_time.minute = calendar_time.minute; - internal_time.second = calendar_time.second; + CalendarTimeInternal internal_time{ + .year = calendar_time.year, + // Internal impl. uses 0-indexed month + .month = static_cast(calendar_time.month - 1), + .day = calendar_time.day, + .hour = calendar_time.hour, + .minute = calendar_time.minute, + .second = calendar_time.second, + }; s32 hour{internal_time.hour}; s32 minute{internal_time.minute}; diff --git a/src/core/hle/service/vi/vi.cpp b/src/core/hle/service/vi/vi.cpp index 480d34725..d380c60fb 100644 --- a/src/core/hle/service/vi/vi.cpp +++ b/src/core/hle/service/vi/vi.cpp @@ -159,7 +159,7 @@ public: header.data_size = static_cast(write_index - sizeof(Header)); header.data_offset = sizeof(Header); header.objects_size = 4; - header.objects_offset = sizeof(Header) + header.data_size; + header.objects_offset = static_cast(sizeof(Header) + header.data_size); std::memcpy(buffer.data(), &header, sizeof(Header)); return buffer; diff --git a/src/core/loader/kip.cpp b/src/core/loader/kip.cpp index 5981bcd21..2a905d3e4 100644 --- a/src/core/loader/kip.cpp +++ b/src/core/loader/kip.cpp @@ -16,7 +16,7 @@ namespace Loader { namespace { constexpr u32 PageAlignSize(u32 size) { - return (size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK; + return static_cast((size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK); } } // Anonymous namespace diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 9fb5eddad..5f4b3104b 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -127,7 +127,7 @@ FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& file) { } static constexpr u32 PageAlignSize(u32 size) { - return (size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK; + return static_cast((size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK); } static bool LoadNroImpl(Kernel::Process& process, const std::vector& data, diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 1e70f6e11..497f438a1 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -47,7 +47,7 @@ std::vector DecompressSegment(const std::vector& compressed_data, } constexpr u32 PageAlignSize(u32 size) { - return (size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK; + return static_cast((size + Core::Memory::PAGE_MASK) & ~Core::Memory::PAGE_MASK); } } // Anonymous namespace diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 4bd47787d..d331096ae 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -59,7 +59,7 @@ struct NSOHeader { static_assert(sizeof(NSOHeader) == 0x100, "NSOHeader has incorrect size."); static_assert(std::is_trivially_copyable_v, "NSOHeader must be trivially copyable."); -constexpr u64 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000; +constexpr u32 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000; struct NSOArgumentHeader { u32_le allocated_size; diff --git a/src/core/memory.cpp b/src/core/memory.cpp index c3f4829d7..b88aa5c40 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -120,9 +120,9 @@ struct Memory::Impl { if ((addr & 1) == 0) { return Read(addr); } else { - const u8 a{Read(addr)}; - const u8 b{Read(addr + sizeof(u8))}; - return (static_cast(b) << 8) | a; + const u32 a{Read(addr)}; + const u32 b{Read(addr + sizeof(u8))}; + return static_cast((b << 8) | a); } } @@ -130,9 +130,9 @@ struct Memory::Impl { if ((addr & 3) == 0) { return Read(addr); } else { - const u16 a{Read16(addr)}; - const u16 b{Read16(addr + sizeof(u16))}; - return (static_cast(b) << 16) | a; + const u32 a{Read16(addr)}; + const u32 b{Read16(addr + sizeof(u16))}; + return (b << 16) | a; } } diff --git a/src/core/memory/cheat_engine.cpp b/src/core/memory/cheat_engine.cpp index 29284a42d..2dd0eb0f8 100644 --- a/src/core/memory/cheat_engine.cpp +++ b/src/core/memory/cheat_engine.cpp @@ -153,8 +153,9 @@ std::vector TextCheatParser::Parse(std::string_view data) const { return {}; } + const auto value = static_cast(std::stoul(hex, nullptr, 0x10)); out[*current_entry].definition.opcodes[out[*current_entry].definition.num_opcodes++] = - std::stoul(hex, nullptr, 0x10); + value; i += 8; } else { diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 56d173b5e..4b3bb4366 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -238,14 +238,14 @@ SockAddrIn TranslateToSockAddrIn(sockaddr input_) { return result; } -u16 TranslatePollEvents(u16 events) { - u16 result = 0; +u16 TranslatePollEvents(u32 events) { + u32 result = 0; - if (events & POLL_IN) { + if ((events & POLL_IN) != 0) { events &= ~POLL_IN; result |= POLLIN; } - if (events & POLL_PRI) { + if ((events & POLL_PRI) != 0) { events &= ~POLL_PRI; #ifdef _WIN32 LOG_WARNING(Service, "Winsock doesn't support POLLPRI"); @@ -253,20 +253,20 @@ u16 TranslatePollEvents(u16 events) { result |= POLL_PRI; #endif } - if (events & POLL_OUT) { + if ((events & POLL_OUT) != 0) { events &= ~POLL_OUT; result |= POLLOUT; } UNIMPLEMENTED_IF_MSG(events != 0, "Unhandled guest events=0x{:x}", events); - return result; + return static_cast(result); } -u16 TranslatePollRevents(u16 revents) { - u16 result = 0; - const auto translate = [&result, &revents](int host, unsigned guest) { - if (revents & host) { +u16 TranslatePollRevents(u32 revents) { + u32 result = 0; + const auto translate = [&result, &revents](u32 host, u32 guest) { + if ((revents & host) != 0) { revents &= ~host; result |= guest; } @@ -280,7 +280,7 @@ u16 TranslatePollRevents(u16 revents) { UNIMPLEMENTED_IF_MSG(revents != 0, "Unhandled host revents=0x{:x}", revents); - return result; + return static_cast(result); } template @@ -350,7 +350,7 @@ std::pair Poll(std::vector& pollfds, s32 timeout) { } for (size_t i = 0; i < num; ++i) { - pollfds[i].revents = TranslatePollRevents(host_pollfds[i].revents); + pollfds[i].revents = TranslatePollRevents(static_cast(host_pollfds[i].revents)); } if (result > 0) { @@ -408,7 +408,7 @@ std::pair Socket::Accept() { Errno Socket::Connect(SockAddrIn addr_in) { const sockaddr host_addr_in = TranslateFromSockAddrIn(addr_in); - if (connect(fd, &host_addr_in, sizeof(host_addr_in)) != INVALID_SOCKET) { + if (connect(fd, &host_addr_in, sizeof(host_addr_in)) != SOCKET_ERROR) { return Errno::SUCCESS; } @@ -503,10 +503,10 @@ std::pair Socket::Recv(int flags, std::vector& message) { ASSERT(flags == 0); ASSERT(message.size() < static_cast(std::numeric_limits::max())); - const int result = + const auto result = recv(fd, reinterpret_cast(message.data()), static_cast(message.size()), 0); if (result != SOCKET_ERROR) { - return {result, Errno::SUCCESS}; + return {static_cast(result), Errno::SUCCESS}; } switch (const int ec = LastError()) { @@ -531,14 +531,14 @@ std::pair Socket::RecvFrom(int flags, std::vector& message, Sock socklen_t* const p_addrlen = addr ? &addrlen : nullptr; sockaddr* const p_addr_in = addr ? &addr_in : nullptr; - const int result = recvfrom(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, p_addr_in, p_addrlen); + const auto result = recvfrom(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0, p_addr_in, p_addrlen); if (result != SOCKET_ERROR) { if (addr) { ASSERT(addrlen == sizeof(addr_in)); *addr = TranslateToSockAddrIn(addr_in); } - return {result, Errno::SUCCESS}; + return {static_cast(result), Errno::SUCCESS}; } switch (const int ec = LastError()) { @@ -558,10 +558,10 @@ std::pair Socket::Send(const std::vector& message, int flags) { ASSERT(message.size() < static_cast(std::numeric_limits::max())); ASSERT(flags == 0); - const int result = send(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0); + const auto result = send(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0); if (result != SOCKET_ERROR) { - return {result, Errno::SUCCESS}; + return {static_cast(result), Errno::SUCCESS}; } const int ec = LastError(); @@ -591,10 +591,10 @@ std::pair Socket::SendTo(u32 flags, const std::vector& message, to = &host_addr_in; } - const int result = sendto(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, to, tolen); + const auto result = sendto(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0, to, tolen); if (result != SOCKET_ERROR) { - return {result, Errno::SUCCESS}; + return {static_cast(result), Errno::SUCCESS}; } const int ec = LastError(); diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp index 74e287045..534960d09 100644 --- a/src/web_service/web_backend.cpp +++ b/src/web_service/web_backend.cpp @@ -67,28 +67,25 @@ struct Client::Impl { const std::string& jwt = "", const std::string& username = "", const std::string& token = "") { if (cli == nullptr) { - auto parsedUrl = LUrlParser::clParseURL::ParseURL(host); - int port; + const auto parsedUrl = LUrlParser::clParseURL::ParseURL(host); + int port{}; if (parsedUrl.m_Scheme == "http") { if (!parsedUrl.GetPort(&port)) { port = HTTP_PORT; } - cli = std::make_unique(parsedUrl.m_Host.c_str(), port); } else if (parsedUrl.m_Scheme == "https") { if (!parsedUrl.GetPort(&port)) { port = HTTPS_PORT; } - cli = std::make_unique(parsedUrl.m_Host.c_str(), port); } else { LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme); return WebResult{WebResult::Code::InvalidURL, "Bad URL scheme", ""}; } + cli = std::make_unique(parsedUrl.m_Host.c_str(), port); } - if (cli == nullptr) { - LOG_ERROR(WebService, "Invalid URL {}", host + path); - return WebResult{WebResult::Code::InvalidURL, "Invalid URL", ""}; - } - cli->set_timeout_sec(TIMEOUT_SECONDS); + cli->set_connection_timeout(TIMEOUT_SECONDS); + cli->set_read_timeout(TIMEOUT_SECONDS); + cli->set_write_timeout(TIMEOUT_SECONDS); httplib::Headers params; if (!jwt.empty()) { -- cgit v1.2.3 From 046c0c91a3ed665531f20955e7cfb86fe5b73213 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 14 Oct 2020 02:51:14 -0400 Subject: input_common/CMakeLists: Make some warnings errors Makes the input_common code warnings consistent with the rest of the codebase. --- src/common/math_util.h | 4 +- src/common/vector_math.h | 75 +++++++++++++++++++--- src/input_common/CMakeLists.txt | 29 +++++++++ src/input_common/analog_from_button.cpp | 18 +++--- src/input_common/gcadapter/gc_adapter.cpp | 35 ++++++----- src/input_common/gcadapter/gc_adapter.h | 6 +- src/input_common/gcadapter/gc_poller.cpp | 48 +++++++------- src/input_common/keyboard.cpp | 5 +- src/input_common/main.cpp | 4 ++ src/input_common/motion_emu.cpp | 41 ++++++------ src/input_common/motion_from_button.cpp | 2 +- src/input_common/motion_input.cpp | 38 +++++------ src/input_common/motion_input.h | 10 +-- src/input_common/sdl/sdl_impl.cpp | 77 ++++++++++++----------- src/input_common/touch_from_button.cpp | 8 ++- src/input_common/udp/client.cpp | 65 ++++++++++--------- src/input_common/udp/client.h | 14 ++--- src/input_common/udp/udp.cpp | 28 ++++----- src/yuzu/configuration/configure_motion_touch.cpp | 2 +- 19 files changed, 306 insertions(+), 203 deletions(-) (limited to 'src/common') diff --git a/src/common/math_util.h b/src/common/math_util.h index b35ad8507..7cec80d57 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -20,8 +20,8 @@ struct Rectangle { constexpr Rectangle() = default; - constexpr Rectangle(T left, T top, T right, T bottom) - : left(left), top(top), right(right), bottom(bottom) {} + constexpr Rectangle(T left_, T top_, T right_, T bottom_) + : left(left_), top(top_), right(right_), bottom(bottom_) {} [[nodiscard]] T GetWidth() const { if constexpr (std::is_floating_point_v) { diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 2a0fcf541..22dba3c2d 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -87,7 +87,13 @@ public: template [[nodiscard]] constexpr Vec2 operator*(const V& f) const { - return {x * f, y * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + }; } template @@ -98,7 +104,13 @@ public: template [[nodiscard]] constexpr Vec2 operator/(const V& f) const { - return {x / f, y / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + }; } template @@ -168,7 +180,10 @@ public: template [[nodiscard]] constexpr Vec2 operator*(const V& f, const Vec2& vec) { - return Vec2(f * vec.x, f * vec.y); + using C = std::common_type_t; + + return Vec2(static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y))); } using Vec2f = Vec2; @@ -237,7 +252,14 @@ public: template [[nodiscard]] constexpr Vec3 operator*(const V& f) const { - return {x * f, y * f, z * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + static_cast(static_cast(z) * static_cast(f)), + }; } template @@ -247,7 +269,14 @@ public: } template [[nodiscard]] constexpr Vec3 operator/(const V& f) const { - return {x / f, y / f, z / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + static_cast(static_cast(z) / static_cast(f)), + }; } template @@ -367,7 +396,11 @@ public: template [[nodiscard]] constexpr Vec3 operator*(const V& f, const Vec3& vec) { - return Vec3(f * vec.x, f * vec.y, f * vec.z); + using C = std::common_type_t; + + return Vec3(static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y)), + static_cast(static_cast(f) * static_cast(vec.z))); } template <> @@ -446,7 +479,15 @@ public: template [[nodiscard]] constexpr Vec4 operator*(const V& f) const { - return {x * f, y * f, z * f, w * f}; + using TV = decltype(T{} * V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) * static_cast(f)), + static_cast(static_cast(y) * static_cast(f)), + static_cast(static_cast(z) * static_cast(f)), + static_cast(static_cast(w) * static_cast(f)), + }; } template @@ -457,7 +498,15 @@ public: template [[nodiscard]] constexpr Vec4 operator/(const V& f) const { - return {x / f, y / f, z / f, w / f}; + using TV = decltype(T{} / V{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(x) / static_cast(f)), + static_cast(static_cast(y) / static_cast(f)), + static_cast(static_cast(z) / static_cast(f)), + static_cast(static_cast(w) / static_cast(f)), + }; } template @@ -582,7 +631,15 @@ public: template [[nodiscard]] constexpr Vec4 operator*(const V& f, const Vec4& vec) { - return {f * vec.x, f * vec.y, f * vec.z, f * vec.w}; + using TV = decltype(V{} * T{}); + using C = std::common_type_t; + + return { + static_cast(static_cast(f) * static_cast(vec.x)), + static_cast(static_cast(f) * static_cast(vec.y)), + static_cast(static_cast(f) * static_cast(vec.z)), + static_cast(static_cast(f) * static_cast(vec.w)), + }; } using Vec4f = Vec4; diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt index c84685214..7b39a38c1 100644 --- a/src/input_common/CMakeLists.txt +++ b/src/input_common/CMakeLists.txt @@ -29,6 +29,35 @@ add_library(input_common STATIC udp/udp.h ) +if (MSVC) + target_compile_options(input_common PRIVATE + # 'expression' : signed/unsigned mismatch + /we4018 + # 'argument' : conversion from 'type1' to 'type2', possible loss of data (floating-point) + /we4244 + # 'conversion' : conversion from 'type1' to 'type2', signed/unsigned mismatch + /we4245 + # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data + /we4254 + # 'var' : conversion from 'size_t' to 'type', possible loss of data + /we4267 + # 'context' : truncation from 'type1' to 'type2' + /we4305 + ) +else() + target_compile_options(input_common PRIVATE + -Werror=conversion + -Werror=ignored-qualifiers + -Werror=implicit-fallthrough + -Werror=reorder + -Werror=shadow + -Werror=sign-compare + -Werror=unused-but-set-parameter + -Werror=unused-but-set-variable + -Werror=unused-variable + ) +endif() + if(SDL2_FOUND) target_sources(input_common PRIVATE sdl/sdl_impl.cpp diff --git a/src/input_common/analog_from_button.cpp b/src/input_common/analog_from_button.cpp index 6cabdaa3c..74744d7f3 100755 --- a/src/input_common/analog_from_button.cpp +++ b/src/input_common/analog_from_button.cpp @@ -20,18 +20,22 @@ public: constexpr float SQRT_HALF = 0.707106781f; int x = 0, y = 0; - if (right->GetStatus()) + if (right->GetStatus()) { ++x; - if (left->GetStatus()) + } + if (left->GetStatus()) { --x; - if (up->GetStatus()) + } + if (up->GetStatus()) { ++y; - if (down->GetStatus()) + } + if (down->GetStatus()) { --y; + } - float coef = modifier->GetStatus() ? modifier_scale : 1.0f; - return std::make_tuple(x * coef * (y == 0 ? 1.0f : SQRT_HALF), - y * coef * (x == 0 ? 1.0f : SQRT_HALF)); + const float coef = modifier->GetStatus() ? modifier_scale : 1.0f; + return std::make_tuple(static_cast(x) * coef * (y == 0 ? 1.0f : SQRT_HALF), + static_cast(y) * coef * (x == 0 ? 1.0f : SQRT_HALF)); } bool GetAnalogDirectionStatus(Input::AnalogDirection direction) const override { diff --git a/src/input_common/gcadapter/gc_adapter.cpp b/src/input_common/gcadapter/gc_adapter.cpp index 89c148aba..c95feb0d7 100644 --- a/src/input_common/gcadapter/gc_adapter.cpp +++ b/src/input_common/gcadapter/gc_adapter.cpp @@ -21,7 +21,7 @@ namespace GCAdapter { -/// Used to loop through and assign button in poller +// Used to loop through and assign button in poller constexpr std::array PadButtonArray{ PadButton::PAD_BUTTON_LEFT, PadButton::PAD_BUTTON_RIGHT, PadButton::PAD_BUTTON_DOWN, PadButton::PAD_BUTTON_UP, PadButton::PAD_TRIGGER_Z, PadButton::PAD_TRIGGER_R, @@ -29,6 +29,18 @@ constexpr std::array PadButtonArray{ PadButton::PAD_BUTTON_X, PadButton::PAD_BUTTON_Y, PadButton::PAD_BUTTON_START, }; +static void PadToState(const GCPadStatus& pad, GCState& out_state) { + for (const auto& button : PadButtonArray) { + const auto button_key = static_cast(button); + const auto button_value = (pad.button & button_key) != 0; + out_state.buttons.insert_or_assign(static_cast(button_key), button_value); + } + + for (std::size_t i = 0; i < pad.axis_values.size(); ++i) { + out_state.axes.insert_or_assign(static_cast(i), pad.axis_values[i]); + } +} + Adapter::Adapter() { if (usb_adapter_handle != nullptr) { return; @@ -78,17 +90,17 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array& ad for (std::size_t i = 0; i < b1_buttons.size(); ++i) { if ((b1 & (1U << i)) != 0) { - pad.button |= static_cast(b1_buttons[i]); + pad.button = static_cast(pad.button | static_cast(b1_buttons[i])); } } for (std::size_t j = 0; j < b2_buttons.size(); ++j) { if ((b2 & (1U << j)) != 0) { - pad.button |= static_cast(b2_buttons[j]); + pad.button = static_cast(pad.button | static_cast(b2_buttons[j])); } } for (PadAxes axis : axes) { - const std::size_t index = static_cast(axis); + const auto index = static_cast(axis); pad.axis_values[index] = adapter_payload[offset + 3 + index]; } @@ -100,17 +112,6 @@ GCPadStatus Adapter::GetPadStatus(std::size_t port, const std::array& ad return pad; } -void Adapter::PadToState(const GCPadStatus& pad, GCState& state) { - for (const auto& button : PadButtonArray) { - const u16 button_value = static_cast(button); - state.buttons.insert_or_assign(button_value, pad.button & button_value); - } - - for (size_t i = 0; i < pad.axis_values.size(); ++i) { - state.axes.insert_or_assign(static_cast(i), pad.axis_values[i]); - } -} - void Adapter::Read() { LOG_DEBUG(Input, "GC Adapter Read() thread started"); @@ -250,7 +251,7 @@ void Adapter::GetGCEndpoint(libusb_device* device) { const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i]; for (u8 e = 0; e < interface->bNumEndpoints; e++) { const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e]; - if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) { + if ((endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN) != 0) { input_endpoint = endpoint->bEndpointAddress; } else { output_endpoint = endpoint->bEndpointAddress; @@ -419,7 +420,7 @@ const std::array& Adapter::GetPadState() const { return state; } -int Adapter::GetOriginValue(int port, int axis) const { +int Adapter::GetOriginValue(u32 port, u32 axis) const { return origin_status[port].axis_values[axis]; } diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index 75bf9fe74..4f5f3de8e 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -60,7 +60,7 @@ struct GCPadStatus { struct GCState { std::unordered_map buttons; - std::unordered_map axes; + std::unordered_map axes; }; enum class ControllerTypes { None, Wired, Wireless }; @@ -89,13 +89,11 @@ public: std::array& GetPadState(); const std::array& GetPadState() const; - int GetOriginValue(int port, int axis) const; + int GetOriginValue(u32 port, u32 axis) const; private: GCPadStatus GetPadStatus(std::size_t port, const std::array& adapter_payload); - void PadToState(const GCPadStatus& pad, GCState& state); - void Read(); /// Resets status of device connected to port diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 92e9e8e89..893556916 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -15,7 +15,7 @@ namespace InputCommon { class GCButton final : public Input::ButtonDevice { public: - explicit GCButton(int port_, int button_, const GCAdapter::Adapter* adapter) + explicit GCButton(u32 port_, int button_, const GCAdapter::Adapter* adapter) : port(port_), button(button_), gcadapter(adapter) {} ~GCButton() override; @@ -28,14 +28,14 @@ public: } private: - const int port; + const u32 port; const int button; const GCAdapter::Adapter* gcadapter; }; class GCAxisButton final : public Input::ButtonDevice { public: - explicit GCAxisButton(int port_, int axis_, float threshold_, bool trigger_if_greater_, + explicit GCAxisButton(u32 port_, u32 axis_, float threshold_, bool trigger_if_greater_, const GCAdapter::Adapter* adapter) : port(port_), axis(axis_), threshold(threshold_), trigger_if_greater(trigger_if_greater_), gcadapter(adapter), @@ -56,8 +56,8 @@ public: } private: - const int port; - const int axis; + const u32 port; + const u32 axis; float threshold; bool trigger_if_greater; const GCAdapter::Adapter* gcadapter; @@ -70,8 +70,8 @@ GCButtonFactory::GCButtonFactory(std::shared_ptr adapter_) GCButton::~GCButton() = default; std::unique_ptr GCButtonFactory::Create(const Common::ParamPackage& params) { - const int button_id = params.Get("button", 0); - const int port = params.Get("port", 0); + const auto button_id = params.Get("button", 0); + const auto port = static_cast(params.Get("port", 0)); constexpr int PAD_STICK_ID = static_cast(GCAdapter::PadButton::PAD_STICK); @@ -149,25 +149,27 @@ void GCButtonFactory::EndConfiguration() { class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, - const GCAdapter::Adapter* adapter, float range_) + explicit GCAnalog(u32 port_, u32 axis_x_, u32 axis_y_, float deadzone_, + const GCAdapter::Adapter* adapter, float range_) : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), origin_value_x(static_cast(adapter->GetOriginValue(port_, axis_x_))), origin_value_y(static_cast(adapter->GetOriginValue(port_, axis_y_))), range(range_) {} - float GetAxis(int axis) const { + float GetAxis(u32 axis) const { if (gcadapter->DeviceConnected(port)) { std::lock_guard lock{mutex}; const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; - return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / (100.0f * range); + const auto axis_value = + static_cast(gcadapter->GetPadState()[port].axes.at(axis)); + return (axis_value - origin_value) / (100.0f * range); } return 0.0f; } - std::pair GetAnalog(int axis_x, int axis_y) const { - float x = GetAxis(axis_x); - float y = GetAxis(axis_y); + std::pair GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { + float x = GetAxis(analog_axis_x); + float y = GetAxis(analog_axis_y); // Make sure the coordinates are in the unit circle, // otherwise normalize it. @@ -208,9 +210,9 @@ public: } private: - const int port; - const int axis_x; - const int axis_y; + const u32 port; + const u32 axis_x; + const u32 axis_y; const float deadzone; const GCAdapter::Adapter* gcadapter; const float origin_value_x; @@ -231,11 +233,11 @@ GCAnalogFactory::GCAnalogFactory(std::shared_ptr adapter_) * - "axis_y": the index of the axis to be bind as y-axis */ std::unique_ptr GCAnalogFactory::Create(const Common::ParamPackage& params) { - const int port = params.Get("port", 0); - const int axis_x = params.Get("axis_x", 0); - const int axis_y = params.Get("axis_y", 1); - const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); - const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); + const auto port = static_cast(params.Get("port", 0)); + const auto axis_x = static_cast(params.Get("axis_x", 0)); + const auto axis_y = static_cast(params.Get("axis_y", 1)); + const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); + const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); return std::make_unique(port, axis_x, axis_y, deadzone, adapter.get(), range); } @@ -256,7 +258,7 @@ Common::ParamPackage GCAnalogFactory::GetNextInput() { for (std::size_t port = 0; port < queue.size(); ++port) { while (queue[port].Pop(pad)) { if (pad.axis == GCAdapter::PadAxes::Undefined || - std::abs((pad.axis_value - 128.0f) / 128.0f) < 0.1) { + std::abs((static_cast(pad.axis_value) - 128.0f) / 128.0f) < 0.1f) { continue; } // An analog device needs two axes, so we need to store the axis for later and wait for diff --git a/src/input_common/keyboard.cpp b/src/input_common/keyboard.cpp index afb8e6612..24a6f7a33 100644 --- a/src/input_common/keyboard.cpp +++ b/src/input_common/keyboard.cpp @@ -49,8 +49,9 @@ public: void ChangeKeyStatus(int key_code, bool pressed) { std::lock_guard guard{mutex}; for (const KeyButtonPair& pair : list) { - if (pair.key_code == key_code) + if (pair.key_code == key_code) { pair.key_button->status.store(pressed); + } } } @@ -73,7 +74,7 @@ KeyButton::~KeyButton() { } std::unique_ptr Keyboard::Create(const Common::ParamPackage& params) { - int key_code = params.Get("code", 0); + const int key_code = params.Get("code", 0); std::unique_ptr button = std::make_unique(key_button_list); key_button_list->AddKeyButton(key_code, button.get()); return button; diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp index 3d97d95f7..d32fd8b81 100644 --- a/src/input_common/main.cpp +++ b/src/input_common/main.cpp @@ -196,6 +196,10 @@ ButtonMapping InputSubsystem::GetButtonMappingForDevice(const Common::ParamPacka return impl->GetButtonMappingForDevice(device); } +MotionMapping InputSubsystem::GetMotionMappingForDevice(const Common::ParamPackage& device) const { + return impl->GetMotionMappingForDevice(device); +} + GCAnalogFactory* InputSubsystem::GetGCAnalogs() { return impl->gcanalog.get(); } diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp index 69fd3c1d2..d4da5596b 100644 --- a/src/input_common/motion_emu.cpp +++ b/src/input_common/motion_emu.cpp @@ -18,11 +18,11 @@ namespace InputCommon { // Implementation class of the motion emulation device class MotionEmuDevice { public: - MotionEmuDevice(int update_millisecond, float sensitivity) - : update_millisecond(update_millisecond), + explicit MotionEmuDevice(int update_millisecond_, float sensitivity_) + : update_millisecond(update_millisecond_), update_duration(std::chrono::duration_cast( std::chrono::milliseconds(update_millisecond))), - sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {} + sensitivity(sensitivity_), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {} ~MotionEmuDevice() { if (motion_emu_thread.joinable()) { @@ -37,16 +37,18 @@ public: } void Tilt(int x, int y) { - auto mouse_move = Common::MakeVec(x, y) - mouse_origin; - if (is_tilting) { - std::lock_guard guard{tilt_mutex}; - if (mouse_move.x == 0 && mouse_move.y == 0) { - tilt_angle = 0; - } else { - tilt_direction = mouse_move.Cast(); - tilt_angle = - std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f); - } + if (!is_tilting) { + return; + } + + std::lock_guard guard{tilt_mutex}; + const auto mouse_move = Common::MakeVec(x, y) - mouse_origin; + if (mouse_move.x == 0 && mouse_move.y == 0) { + tilt_angle = 0; + } else { + tilt_direction = mouse_move.Cast(); + tilt_angle = + std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f); } } @@ -86,11 +88,10 @@ private: void MotionEmuThread() { auto update_time = std::chrono::steady_clock::now(); Common::Quaternion q = Common::MakeQuaternion(Common::Vec3(), 0); - Common::Quaternion old_q; while (!shutdown_event.WaitUntil(update_time)) { update_time += update_duration; - old_q = q; + const Common::Quaternion old_q = q; { std::lock_guard guard{tilt_mutex}; @@ -100,14 +101,14 @@ private: Common::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), tilt_angle); } - auto inv_q = q.Inverse(); + const auto inv_q = q.Inverse(); // Set the gravity vector in world space auto gravity = Common::MakeVec(0.0f, -1.0f, 0.0f); // Find the angular rate vector in world space auto angular_rate = ((q - old_q) * inv_q).xyz * 2; - angular_rate *= 1000 / update_millisecond / Common::PI * 180; + angular_rate *= static_cast(1000 / update_millisecond) / Common::PI * 180.0f; // Transform the two vectors from world space to 3DS space gravity = QuaternionRotate(inv_q, gravity); @@ -136,7 +137,7 @@ private: // can forward all the inputs to the implementation only when it is valid. class MotionEmuDeviceWrapper : public Input::MotionDevice { public: - MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { + explicit MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { device = std::make_shared(update_millisecond, sensitivity); } @@ -148,8 +149,8 @@ public: }; std::unique_ptr MotionEmu::Create(const Common::ParamPackage& params) { - int update_period = params.Get("update_period", 100); - float sensitivity = params.Get("sensitivity", 0.01f); + const int update_period = params.Get("update_period", 100); + const float sensitivity = params.Get("sensitivity", 0.01f); auto device_wrapper = std::make_unique(update_period, sensitivity); // Previously created device is disconnected here. Having two motion devices for 3DS is not // expected. diff --git a/src/input_common/motion_from_button.cpp b/src/input_common/motion_from_button.cpp index 9d459f963..29045a673 100644 --- a/src/input_common/motion_from_button.cpp +++ b/src/input_common/motion_from_button.cpp @@ -11,7 +11,7 @@ class MotionKey final : public Input::MotionDevice { public: using Button = std::unique_ptr; - MotionKey(Button key_) : key(std::move(key_)) {} + explicit MotionKey(Button key_) : key(std::move(key_)) {} Input::MotionStatus GetStatus() const override { diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index e89019723..f77ba535d 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -8,8 +8,7 @@ namespace InputCommon { -MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) - : kp(new_kp), ki(new_ki), kd(new_kd), quat{{0, 0, -1}, 0} {} +MotionInput::MotionInput(f32 new_kp, f32 new_ki, f32 new_kd) : kp(new_kp), ki(new_ki), kd(new_kd) {} void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) { accel = acceleration; @@ -59,7 +58,7 @@ bool MotionInput::IsCalibrated(f32 sensitivity) const { } void MotionInput::UpdateRotation(u64 elapsed_time) { - const f32 sample_period = elapsed_time / 1000000.0f; + const auto sample_period = static_cast(elapsed_time) / 1000000.0f; if (sample_period > 0.1f) { return; } @@ -75,7 +74,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { f32 q2 = quat.xyz[0]; f32 q3 = quat.xyz[1]; f32 q4 = quat.xyz[2]; - const f32 sample_period = elapsed_time / 1000000.0f; + const auto sample_period = static_cast(elapsed_time) / 1000000.0f; // Ignore invalid elapsed time if (sample_period > 0.1f) { @@ -203,21 +202,21 @@ Input::MotionStatus MotionInput::GetRandomMotion(int accel_magnitude, int gyro_m std::random_device device; std::mt19937 gen(device()); std::uniform_int_distribution distribution(-1000, 1000); - const Common::Vec3f gyroscope = { - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, + const Common::Vec3f gyroscope{ + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, }; - const Common::Vec3f accelerometer = { - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, - distribution(gen) * 0.001f, + const Common::Vec3f accelerometer{ + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, + static_cast(distribution(gen)) * 0.001f, }; - const Common::Vec3f rotation = {}; - const std::array orientation = { - Common::Vec3f{1.0f, 0, 0}, - Common::Vec3f{0, 1.0f, 0}, - Common::Vec3f{0, 0, 1.0f}, + constexpr Common::Vec3f rotation; + constexpr std::array orientation{ + Common::Vec3f{1.0f, 0.0f, 0.0f}, + Common::Vec3f{0.0f, 1.0f, 0.0f}, + Common::Vec3f{0.0f, 0.0f, 1.0f}, }; return {accelerometer * accel_magnitude, gyroscope * gyro_magnitude, rotation, orientation}; } @@ -247,9 +246,6 @@ void MotionInput::SetOrientationFromAccelerometer() { const f32 sample_period = 0.015f; const auto normal_accel = accel.Normalized(); - const f32 ax = -normal_accel.x; - const f32 ay = normal_accel.y; - const f32 az = -normal_accel.z; while (!IsCalibrated(0.01f) && ++iterations < 100) { // Short name local variable for readability @@ -258,7 +254,7 @@ void MotionInput::SetOrientationFromAccelerometer() { f32 q3 = quat.xyz[1]; f32 q4 = quat.xyz[2]; - Common::Vec3f rad_gyro = {}; + Common::Vec3f rad_gyro; const f32 ax = -normal_accel.x; const f32 ay = normal_accel.y; const f32 az = -normal_accel.z; diff --git a/src/input_common/motion_input.h b/src/input_common/motion_input.h index 6342d0318..abb957f04 100644 --- a/src/input_common/motion_input.h +++ b/src/input_common/motion_input.h @@ -22,7 +22,7 @@ public: MotionInput& operator=(MotionInput&&) = default; void SetAcceleration(const Common::Vec3f& acceleration); - void SetGyroscope(const Common::Vec3f& acceleration); + void SetGyroscope(const Common::Vec3f& gyroscope); void SetQuaternion(const Common::Quaternion& quaternion); void SetGyroDrift(const Common::Vec3f& drift); void SetGyroThreshold(f32 threshold); @@ -49,16 +49,16 @@ private: void SetOrientationFromAccelerometer(); // PID constants - const f32 kp; - const f32 ki; - const f32 kd; + f32 kp; + f32 ki; + f32 kd; // PID errors Common::Vec3f real_error; Common::Vec3f integral_error; Common::Vec3f derivative_error; - Common::Quaternion quat; + Common::Quaternion quat{{0.0f, 0.0f, -1.0f}, 0.0f}; Common::Vec3f rotations; Common::Vec3f accel; Common::Vec3f gyro; diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index bd480570a..8c2cef35d 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -56,9 +56,9 @@ static int SDLEventWatcher(void* user_data, SDL_Event* event) { class SDLJoystick { public: SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, - SDL_GameController* gamecontroller) + SDL_GameController* game_controller) : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose}, - sdl_controller{gamecontroller, &SDL_GameControllerClose} {} + sdl_controller{game_controller, &SDL_GameControllerClose} {} void SetButton(int button, bool value) { std::lock_guard lock{mutex}; @@ -77,10 +77,10 @@ public: float GetAxis(int axis, float range) const { std::lock_guard lock{mutex}; - return state.axes.at(axis) / (32767.0f * range); + return static_cast(state.axes.at(axis)) / (32767.0f * range); } - bool RumblePlay(f32 amp_low, f32 amp_high, int time) { + bool RumblePlay(f32 amp_low, f32 amp_high, u32 time) { const u16 raw_amp_low = static_cast(amp_low * 0xFFFF); const u16 raw_amp_high = static_cast(amp_high * 0xFFFF); // Lower drastically the number of state changes @@ -124,7 +124,7 @@ public: return std::make_tuple(x, y); } - const InputCommon::MotionInput& GetMotion() const { + const MotionInput& GetMotion() const { return motion; } @@ -172,15 +172,15 @@ private: } state; std::string guid; int port; - u16 last_state_rumble_high; - u16 last_state_rumble_low; + u16 last_state_rumble_high = 0; + u16 last_state_rumble_low = 0; std::chrono::time_point last_vibration; std::unique_ptr sdl_joystick; std::unique_ptr sdl_controller; mutable std::mutex mutex; - // motion is initalized without PID values as motion input is not aviable for SDL2 - InputCommon::MotionInput motion{0.0f, 0.0f, 0.0f}; + // Motion is initialized without PID values as motion input is not aviable for SDL2 + MotionInput motion{0.0f, 0.0f, 0.0f}; }; std::shared_ptr SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { @@ -192,7 +192,7 @@ std::shared_ptr SDLState::GetSDLJoystickByGUID(const std::string& g nullptr, nullptr); it->second.emplace_back(std::move(joystick)); } - return it->second[port]; + return it->second[static_cast(port)]; } auto joystick = std::make_shared(guid, 0, nullptr, nullptr); return joystick_map[guid].emplace_back(std::move(joystick)); @@ -212,7 +212,7 @@ std::shared_ptr SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_ return sdl_joystick == joystick->GetSDLJoystick(); }); if (vec_it != map_it->second.end()) { - // This is the common case: There is already an existing SDL_Joystick maped to a + // This is the common case: There is already an existing SDL_Joystick mapped to a // SDLJoystick. return the SDLJoystick return *vec_it; } @@ -220,7 +220,7 @@ std::shared_ptr SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_ // Search for a SDLJoystick without a mapped SDL_Joystick... const auto nullptr_it = std::find_if(map_it->second.begin(), map_it->second.end(), [](const std::shared_ptr& joystick) { - return !joystick->GetSDLJoystick(); + return joystick->GetSDLJoystick() == nullptr; }); if (nullptr_it != map_it->second.end()) { // ... and map it @@ -273,22 +273,21 @@ void SDLState::InitJoystick(int joystick_index) { void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { const std::string guid = GetGUID(sdl_joystick); - std::shared_ptr joystick; + std::shared_ptr found_joystick; { std::lock_guard lock{joystick_map_mutex}; // This call to guid is safe since the joystick is guaranteed to be in the map const auto& joystick_guid_list = joystick_map[guid]; - const auto joystick_it = - std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), - [&sdl_joystick](const std::shared_ptr& joystick) { - return joystick->GetSDLJoystick() == sdl_joystick; - }); - joystick = *joystick_it; + const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), + [&sdl_joystick](const auto& joystick) { + return joystick->GetSDLJoystick() == sdl_joystick; + }); + found_joystick = *joystick_it; } // Destruct SDL_Joystick outside the lock guard because SDL can internally call the // event callback which locks the mutex again. - joystick->SetSDLJoystick(nullptr, nullptr); + found_joystick->SetSDLJoystick(nullptr, nullptr); } void SDLState::HandleGameControllerEvent(const SDL_Event& event) { @@ -392,8 +391,8 @@ private: class SDLAnalog final : public Input::AnalogDevice { public: - SDLAnalog(std::shared_ptr joystick_, int axis_x_, int axis_y_, float deadzone_, - float range_) + explicit SDLAnalog(std::shared_ptr joystick_, int axis_x_, int axis_y_, + float deadzone_, float range_) : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), range(range_) {} @@ -672,13 +671,13 @@ SDLState::SDLState() { RegisterFactory("sdl", button_factory); RegisterFactory("sdl", motion_factory); - // If the frontend is going to manage the event loop, then we dont start one here - start_thread = !SDL_WasInit(SDL_INIT_JOYSTICK); + // If the frontend is going to manage the event loop, then we don't start one here + start_thread = SDL_WasInit(SDL_INIT_JOYSTICK) == 0; if (start_thread && SDL_Init(SDL_INIT_JOYSTICK) < 0) { LOG_CRITICAL(Input, "SDL_Init(SDL_INIT_JOYSTICK) failed with: {}", SDL_GetError()); return; } - has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); + has_gamecontroller = SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0; if (SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1") == SDL_FALSE) { LOG_ERROR(Input, "Failed to set hint for background events with: {}", SDL_GetError()); } @@ -723,8 +722,8 @@ std::vector SDLState::GetInputDevices() { std::vector devices; for (const auto& [key, value] : joystick_map) { for (const auto& joystick : value) { - auto joy = joystick->GetSDLJoystick(); - if (auto controller = joystick->GetSDLGameController()) { + auto* joy = joystick->GetSDLJoystick(); + if (auto* controller = joystick->GetSDLGameController()) { std::string name = fmt::format("{} {}", SDL_GameControllerName(controller), joystick->GetPort()); devices.emplace_back(Common::ParamPackage{ @@ -748,7 +747,7 @@ std::vector SDLState::GetInputDevices() { } namespace { -Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, u8 axis, +Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis, float value = 0.1f) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); @@ -764,7 +763,7 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid return params; } -Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, u8 button) { +Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid, s32 button) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); params.Set("guid", std::move(guid)); @@ -772,7 +771,7 @@ Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid return params; } -Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, u8 hat, u8 value) { +Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat, s32 value) { Common::ParamPackage params({{"engine", "sdl"}}); params.Set("port", port); @@ -802,17 +801,19 @@ Common::ParamPackage SDLEventToButtonParamPackage(SDLState& state, const SDL_Eve case SDL_JOYAXISMOTION: { const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jaxis.axis, event.jaxis.value); + static_cast(event.jaxis.axis), + event.jaxis.value); } case SDL_JOYBUTTONUP: { const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jbutton.button); + static_cast(event.jbutton.button)); } case SDL_JOYHATMOTION: { const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jhat.hat, event.jhat.value); + static_cast(event.jhat.hat), + static_cast(event.jhat.value)); } } return {}; @@ -823,17 +824,19 @@ Common::ParamPackage SDLEventToMotionParamPackage(SDLState& state, const SDL_Eve case SDL_JOYAXISMOTION: { const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); return BuildAnalogParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jaxis.axis, event.jaxis.value); + static_cast(event.jaxis.axis), + event.jaxis.value); } case SDL_JOYBUTTONUP: { const auto joystick = state.GetSDLJoystickBySDLID(event.jbutton.which); return BuildButtonParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jbutton.button); + static_cast(event.jbutton.button)); } case SDL_JOYHATMOTION: { const auto joystick = state.GetSDLJoystickBySDLID(event.jhat.which); return BuildHatParamPackageForButton(joystick->GetPort(), joystick->GetGUID(), - event.jhat.hat, event.jhat.value); + static_cast(event.jhat.hat), + static_cast(event.jhat.value)); } } return {}; @@ -1062,7 +1065,7 @@ public: if (event.type == SDL_JOYAXISMOTION) { const auto axis = event.jaxis.axis; const auto joystick = state.GetSDLJoystickBySDLID(event.jaxis.which); - const auto controller = joystick->GetSDLGameController(); + auto* const controller = joystick->GetSDLGameController(); if (controller) { const auto axis_left_x = SDL_GameControllerGetBindForAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) diff --git a/src/input_common/touch_from_button.cpp b/src/input_common/touch_from_button.cpp index 98da0ef1a..c37716aae 100644 --- a/src/input_common/touch_from_button.cpp +++ b/src/input_common/touch_from_button.cpp @@ -11,9 +11,11 @@ namespace InputCommon { class TouchFromButtonDevice final : public Input::TouchDevice { public: TouchFromButtonDevice() { - for (const auto& config_entry : - Settings::values.touch_from_button_maps[Settings::values.touch_from_button_map_index] - .buttons) { + const auto button_index = + static_cast(Settings::values.touch_from_button_map_index); + const auto& buttons = Settings::values.touch_from_button_maps[button_index].buttons; + + for (const auto& config_entry : buttons) { const Common::ParamPackage package{config_entry}; map.emplace_back( Input::CreateDevice(config_entry), diff --git a/src/input_common/udp/client.cpp b/src/input_common/udp/client.cpp index 9d0b9f31d..bb109562c 100644 --- a/src/input_common/udp/client.cpp +++ b/src/input_common/udp/client.cpp @@ -26,11 +26,11 @@ class Socket { public: using clock = std::chrono::system_clock; - explicit Socket(const std::string& host, u16 port, u8 pad_index, u32 client_id, - SocketCallback callback) - : callback(std::move(callback)), timer(io_service), - socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id), - pad_index(pad_index) { + explicit Socket(const std::string& host, u16 port, std::size_t pad_index_, u32 client_id_, + SocketCallback callback_) + : callback(std::move(callback_)), timer(io_service), + socket(io_service, udp::endpoint(udp::v4(), 0)), client_id(client_id_), + pad_index(pad_index_) { boost::system::error_code ec{}; auto ipv4 = boost::asio::ip::make_address_v4(host, ec); if (ec.value() != boost::system::errc::success) { @@ -93,13 +93,17 @@ private: void HandleSend(const boost::system::error_code& error) { boost::system::error_code _ignored{}; // Send a request for getting port info for the pad - Request::PortInfo port_info{1, {pad_index, 0, 0, 0}}; + const Request::PortInfo port_info{1, {static_cast(pad_index), 0, 0, 0}}; const auto port_message = Request::Create(port_info, client_id); std::memcpy(&send_buffer1, &port_message, PORT_INFO_SIZE); socket.send_to(boost::asio::buffer(send_buffer1), send_endpoint, {}, _ignored); // Send a request for getting pad data for the pad - Request::PadData pad_data{Request::PadData::Flags::Id, pad_index, EMPTY_MAC_ADDRESS}; + const Request::PadData pad_data{ + Request::PadData::Flags::Id, + static_cast(pad_index), + EMPTY_MAC_ADDRESS, + }; const auto pad_message = Request::Create(pad_data, client_id); std::memcpy(send_buffer2.data(), &pad_message, PAD_DATA_SIZE); socket.send_to(boost::asio::buffer(send_buffer2), send_endpoint, {}, _ignored); @@ -112,7 +116,7 @@ private: udp::socket socket; u32 client_id{}; - u8 pad_index{}; + std::size_t pad_index{}; static constexpr std::size_t PORT_INFO_SIZE = sizeof(Message); static constexpr std::size_t PAD_DATA_SIZE = sizeof(Message); @@ -133,7 +137,7 @@ static void SocketLoop(Socket* socket) { Client::Client() { LOG_INFO(Input, "Udp Initialization started"); for (std::size_t client = 0; client < clients.size(); client++) { - u8 pad = client % 4; + const auto pad = client % 4; StartCommunication(client, Settings::values.udp_input_address, Settings::values.udp_input_port, pad, 24872); // Set motion parameters @@ -166,9 +170,9 @@ std::vector Client::GetInputDevices() const { bool Client::DeviceConnected(std::size_t pad) const { // Use last timestamp to detect if the socket has stopped sending data const auto now = std::chrono::system_clock::now(); - u64 time_difference = + const auto time_difference = static_cast( std::chrono::duration_cast(now - clients[pad].last_motion_update) - .count(); + .count()); return time_difference < 1000 && clients[pad].active == 1; } @@ -177,9 +181,9 @@ void Client::ReloadUDPClient() { ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port, client); } } -void Client::ReloadSocket(const std::string& host, u16 port, u8 pad_index, u32 client_id) { +void Client::ReloadSocket(const std::string& host, u16 port, std::size_t pad_index, u32 client_id) { // client number must be determined from host / port and pad index - std::size_t client = pad_index; + const std::size_t client = pad_index; clients[client].socket->Stop(); clients[client].thread.join(); StartCommunication(client, host, port, pad_index, client_id); @@ -194,8 +198,8 @@ void Client::OnPortInfo(Response::PortInfo data) { } void Client::OnPadData(Response::PadData data) { - // client number must be determined from host / port and pad index - std::size_t client = data.info.id; + // Client number must be determined from host / port and pad index + const std::size_t client = data.info.id; LOG_TRACE(Input, "PadData packet received"); if (data.packet_counter == clients[client].packet_sequence) { LOG_WARNING( @@ -207,11 +211,12 @@ void Client::OnPadData(Response::PadData data) { clients[client].active = data.info.is_pad_active; clients[client].packet_sequence = data.packet_counter; const auto now = std::chrono::system_clock::now(); - u64 time_difference = std::chrono::duration_cast( - now - clients[client].last_motion_update) - .count(); + const auto time_difference = + static_cast(std::chrono::duration_cast( + now - clients[client].last_motion_update) + .count()); clients[client].last_motion_update = now; - Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; + const Common::Vec3f raw_gyroscope = {data.gyro.pitch, data.gyro.roll, -data.gyro.yaw}; clients[client].motion.SetAcceleration({data.accel.x, -data.accel.z, data.accel.y}); // Gyroscope values are not it the correct scale from better joy. // Dividing by 312 allows us to make one full turn = 1 turn @@ -237,9 +242,11 @@ void Client::OnPadData(Response::PadData data) { const u16 min_y = clients[client].status.touch_calibration->min_y; const u16 max_y = clients[client].status.touch_calibration->max_y; - x = (std::clamp(static_cast(data.touch_1.x), min_x, max_x) - min_x) / + x = static_cast(std::clamp(static_cast(data.touch_1.x), min_x, max_x) - + min_x) / static_cast(max_x - min_x); - y = (std::clamp(static_cast(data.touch_1.y), min_y, max_y) - min_y) / + y = static_cast(std::clamp(static_cast(data.touch_1.y), min_y, max_y) - + min_y) / static_cast(max_y - min_y); } @@ -253,8 +260,8 @@ void Client::OnPadData(Response::PadData data) { } } -void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, - u32 client_id) { +void Client::StartCommunication(std::size_t client, const std::string& host, u16 port, + std::size_t pad_index, u32 client_id) { SocketCallback callback{[this](Response::Version version) { OnVersion(version); }, [this](Response::PortInfo info) { OnPortInfo(info); }, [this](Response::PadData data) { OnPadData(data); }}; @@ -264,9 +271,9 @@ void Client::StartCommunication(std::size_t client, const std::string& host, u16 } void Client::Reset() { - for (std::size_t client = 0; client < clients.size(); client++) { - clients[client].socket->Stop(); - clients[client].thread.join(); + for (auto& client : clients) { + client.socket->Stop(); + client.thread.join(); } } @@ -325,7 +332,7 @@ const std::array, 4>& Client::GetPadQueue() cons return pad_queue; } -void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function success_callback, std::function failure_callback) { std::thread([=] { @@ -346,7 +353,7 @@ void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 clie } CalibrationConfigurationJob::CalibrationConfigurationJob( - const std::string& host, u16 port, u8 pad_index, u32 client_id, + const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function status_callback, std::function data_callback) { @@ -366,7 +373,7 @@ CalibrationConfigurationJob::CalibrationConfigurationJob( current_status = Status::Ready; status_callback(current_status); } - if (!data.touch_1.is_active) { + if (data.touch_1.is_active == 0) { return; } LOG_DEBUG(Input, "Current touch: {} {}", data.touch_1.x, diff --git a/src/input_common/udp/client.h b/src/input_common/udp/client.h index 523dc6a7a..2491a03a2 100644 --- a/src/input_common/udp/client.h +++ b/src/input_common/udp/client.h @@ -84,8 +84,8 @@ public: bool DeviceConnected(std::size_t pad) const; void ReloadUDPClient(); - void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, u8 pad_index = 0, - u32 client_id = 24872); + void ReloadSocket(const std::string& host = "127.0.0.1", u16 port = 26760, + std::size_t pad_index = 0, u32 client_id = 24872); std::array, 4>& GetPadQueue(); const std::array, 4>& GetPadQueue() const; @@ -99,7 +99,7 @@ private: DeviceStatus status; std::thread thread; u64 packet_sequence = 0; - u8 active; + u8 active = 0; // Realtime values // motion is initalized with PID values for drift correction on joycons @@ -113,8 +113,8 @@ private: void OnVersion(Response::Version); void OnPortInfo(Response::PortInfo); void OnPadData(Response::PadData); - void StartCommunication(std::size_t client, const std::string& host, u16 port, u8 pad_index, - u32 client_id); + void StartCommunication(std::size_t client, const std::string& host, u16 port, + std::size_t pad_index, u32 client_id); void UpdateYuzuSettings(std::size_t client, const Common::Vec3& acc, const Common::Vec3& gyro, bool touch); @@ -139,7 +139,7 @@ public: * @param status_callback Callback for job status updates * @param data_callback Called when calibration data is ready */ - explicit CalibrationConfigurationJob(const std::string& host, u16 port, u8 pad_index, + explicit CalibrationConfigurationJob(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function status_callback, std::function data_callback); ~CalibrationConfigurationJob(); @@ -149,7 +149,7 @@ private: Common::Event complete_event; }; -void TestCommunication(const std::string& host, u16 port, u8 pad_index, u32 client_id, +void TestCommunication(const std::string& host, u16 port, std::size_t pad_index, u32 client_id, std::function success_callback, std::function failure_callback); diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index eba077a36..71a76a7aa 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp @@ -2,8 +2,6 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include -#include #include #include #include "common/assert.h" @@ -15,8 +13,8 @@ namespace InputCommon { class UDPMotion final : public Input::MotionDevice { public: - UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) - : ip(ip_), port(port_), pad(pad_), client(client_) {} + explicit UDPMotion(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) + : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} Input::MotionStatus GetStatus() const override { return client->GetPadState(pad).motion_status; @@ -25,7 +23,7 @@ public: private: const std::string ip; const int port; - const int pad; + const u32 pad; CemuhookUDP::Client* client; mutable std::mutex mutex; }; @@ -40,11 +38,11 @@ UDPMotionFactory::UDPMotionFactory(std::shared_ptr client_) * - "port": the nth jcpad on the adapter */ std::unique_ptr UDPMotionFactory::Create(const Common::ParamPackage& params) { - const std::string ip = params.Get("ip", "127.0.0.1"); - const int port = params.Get("port", 26760); - const int pad = params.Get("pad_index", 0); + auto ip = params.Get("ip", "127.0.0.1"); + const auto port = params.Get("port", 26760); + const auto pad = static_cast(params.Get("pad_index", 0)); - return std::make_unique(ip, port, pad, client.get()); + return std::make_unique(std::move(ip), port, pad, client.get()); } void UDPMotionFactory::BeginConfiguration() { @@ -79,7 +77,7 @@ Common::ParamPackage UDPMotionFactory::GetNextInput() { class UDPTouch final : public Input::TouchDevice { public: - UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_) + explicit UDPTouch(std::string ip_, int port_, u32 pad_, CemuhookUDP::Client* client_) : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {} std::tuple GetStatus() const override { @@ -89,7 +87,7 @@ public: private: const std::string ip; const int port; - const int pad; + const u32 pad; CemuhookUDP::Client* client; mutable std::mutex mutex; }; @@ -104,11 +102,11 @@ UDPTouchFactory::UDPTouchFactory(std::shared_ptr client_) * - "port": the nth jcpad on the adapter */ std::unique_ptr UDPTouchFactory::Create(const Common::ParamPackage& params) { - const std::string ip = params.Get("ip", "127.0.0.1"); - const int port = params.Get("port", 26760); - const int pad = params.Get("pad_index", 0); + auto ip = params.Get("ip", "127.0.0.1"); + const auto port = params.Get("port", 26760); + const auto pad = static_cast(params.Get("pad_index", 0)); - return std::make_unique(ip, port, pad, client.get()); + return std::make_unique(std::move(ip), port, pad, client.get()); } void UDPTouchFactory::BeginConfiguration() { diff --git a/src/yuzu/configuration/configure_motion_touch.cpp b/src/yuzu/configuration/configure_motion_touch.cpp index c7d085151..170574d9b 100644 --- a/src/yuzu/configuration/configure_motion_touch.cpp +++ b/src/yuzu/configuration/configure_motion_touch.cpp @@ -193,7 +193,7 @@ void ConfigureMotionTouch::OnCemuhookUDPTest() { udp_test_in_progress = true; InputCommon::CemuhookUDP::TestCommunication( ui->udp_server->text().toStdString(), static_cast(ui->udp_port->text().toInt()), - static_cast(ui->udp_pad_index->currentIndex()), 24872, + static_cast(ui->udp_pad_index->currentIndex()), 24872, [this] { LOG_INFO(Frontend, "UDP input test success"); QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true)); -- cgit v1.2.3 From be1954e04cb5a0c3a526f78ed5490a5e65310280 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 15 Oct 2020 14:49:45 -0400 Subject: core: Fix clang build Recent changes to the build system that made more warnings be flagged as errors caused building via clang to break. Fixes #4795 --- externals/microprofile/microprofile.h | 4 +- src/audio_core/CMakeLists.txt | 5 +- src/audio_core/algorithm/interpolate.cpp | 6 +- src/audio_core/audio_renderer.cpp | 4 +- src/audio_core/codec.cpp | 6 +- src/audio_core/command_generator.cpp | 212 ++++++++++++--------- src/audio_core/command_generator.h | 4 +- src/audio_core/cubeb_sink.cpp | 2 +- src/audio_core/cubeb_sink.h | 2 +- src/audio_core/info_updater.cpp | 4 +- src/audio_core/mix_context.cpp | 17 +- src/audio_core/sink_context.cpp | 5 +- src/audio_core/splitter_context.cpp | 60 +++--- src/audio_core/time_stretch.h | 10 +- src/audio_core/voice_context.cpp | 47 +++-- src/common/fiber.h | 4 +- src/common/file_util.h | 3 +- src/common/math_util.h | 4 +- src/common/multi_level_queue.h | 2 +- src/common/spin_lock.h | 8 + src/common/swap.h | 10 +- src/common/thread_queue_list.h | 4 +- src/core/CMakeLists.txt | 5 +- src/core/arm/arm_interface.cpp | 24 ++- src/core/arm/arm_interface.h | 8 +- src/core/arm/cpu_interrupt_handler.h | 4 +- src/core/arm/dynarmic/arm_dynarmic_32.cpp | 10 +- src/core/arm/dynarmic/arm_dynarmic_32.h | 8 +- src/core/arm/dynarmic/arm_dynarmic_64.cpp | 10 +- src/core/arm/dynarmic/arm_dynarmic_64.h | 8 +- src/core/arm/unicorn/arm_unicorn.cpp | 42 ++-- src/core/arm/unicorn/arm_unicorn.h | 8 +- src/core/core_timing.cpp | 13 +- src/core/core_timing_util.cpp | 42 ++-- src/core/core_timing_util.h | 4 +- src/core/crypto/key_manager.cpp | 7 +- src/core/crypto/partition_data_manager.cpp | 2 +- src/core/file_sys/content_archive.cpp | 4 +- src/core/file_sys/fsmitm_romfsbuild.cpp | 2 +- src/core/file_sys/ips_layer.cpp | 41 ++-- src/core/file_sys/kernel_executable.cpp | 6 +- src/core/file_sys/nca_patch.cpp | 7 +- src/core/frontend/applets/controller.cpp | 2 +- src/core/frontend/applets/profile_select.cpp | 3 +- src/core/gdbstub/gdbstub.cpp | 47 ++++- src/core/gdbstub/gdbstub.h | 2 +- src/core/hle/ipc_helpers.h | 4 +- src/core/hle/kernel/address_arbiter.cpp | 4 +- src/core/hle/kernel/handle_table.cpp | 2 +- src/core/hle/kernel/hle_ipc.cpp | 2 +- src/core/hle/kernel/kernel.cpp | 2 +- src/core/hle/kernel/memory/address_space_info.cpp | 2 + src/core/hle/kernel/memory/memory_manager.cpp | 4 +- src/core/hle/kernel/memory/page_heap.cpp | 17 +- src/core/hle/kernel/memory/page_heap.h | 18 +- src/core/hle/kernel/memory/page_table.cpp | 6 +- src/core/hle/kernel/physical_core.h | 2 +- src/core/hle/kernel/process.cpp | 17 +- src/core/hle/kernel/resource_limit.cpp | 4 +- src/core/hle/kernel/scheduler.cpp | 72 +++++-- src/core/hle/kernel/svc.cpp | 7 +- src/core/hle/kernel/svc_wrap.h | 17 +- src/core/hle/kernel/synchronization.cpp | 4 +- src/core/hle/kernel/thread.cpp | 2 +- src/core/hle/kernel/thread.h | 6 +- src/core/hle/service/acc/profile_manager.cpp | 13 +- src/core/hle/service/am/am.cpp | 2 +- src/core/hle/service/am/applets/controller.cpp | 26 +-- src/core/hle/service/audio/audout_u.cpp | 7 +- src/core/hle/service/audio/hwopus.cpp | 29 +-- src/core/hle/service/bcat/backend/backend.h | 8 +- src/core/hle/service/bcat/backend/boxcat.cpp | 9 + src/core/hle/service/bcat/module.cpp | 3 +- src/core/hle/service/filesystem/fsp_srv.cpp | 8 +- src/core/hle/service/hid/controllers/debug_pad.cpp | 8 +- src/core/hle/service/hid/controllers/gesture.cpp | 8 +- src/core/hle/service/hid/controllers/keyboard.cpp | 8 +- src/core/hle/service/hid/controllers/mouse.cpp | 8 +- src/core/hle/service/hid/controllers/npad.cpp | 69 ++++--- src/core/hle/service/hid/controllers/stubbed.cpp | 12 +- .../hle/service/hid/controllers/touchscreen.cpp | 9 +- src/core/hle/service/hid/controllers/touchscreen.h | 2 +- src/core/hle/service/hid/controllers/xpad.cpp | 8 +- src/core/hle/service/ldr/ldr.cpp | 4 +- src/core/hle/service/mii/manager.cpp | 20 +- src/core/hle/service/nfp/nfp.cpp | 2 +- src/core/hle/service/ns/ns.cpp | 2 +- src/core/hle/service/ns/pl_u.cpp | 12 +- .../hle/service/nvdrv/devices/nvhost_as_gpu.cpp | 48 +++-- src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp | 12 +- src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp | 8 +- src/core/hle/service/nvdrv/interface.cpp | 8 +- src/core/hle/service/nvflinger/nvflinger.cpp | 2 +- src/core/hle/service/service.cpp | 4 +- src/core/hle/service/set/set.cpp | 6 +- src/core/hle/service/sockets/bsd.cpp | 72 ++++--- src/core/hle/service/sockets/bsd.h | 3 + src/core/hle/service/sockets/sockets_translate.cpp | 1 + src/core/hle/service/time/time_zone_manager.cpp | 118 ++++++------ src/core/hle/service/time/time_zone_service.cpp | 4 +- src/core/loader/elf.cpp | 35 ++-- src/core/memory.cpp | 2 +- src/core/network/network.cpp | 31 +-- src/core/network/network.h | 12 +- src/core/network/sockets.h | 6 +- 105 files changed, 906 insertions(+), 667 deletions(-) (limited to 'src/common') diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h index 85d5bd5de..d22f92868 100644 --- a/externals/microprofile/microprofile.h +++ b/externals/microprofile/microprofile.h @@ -857,7 +857,7 @@ inline int64_t MicroProfileLogTickDifference(MicroProfileLogEntry Start, MicroPr { uint64_t nStart = Start; uint64_t nEnd = End; - int64_t nDifference = ((nEnd<<16) - (nStart<<16)); + auto nDifference = static_cast((nEnd << 16) - (nStart << 16)); return nDifference >> 16; } @@ -868,7 +868,7 @@ inline int64_t MicroProfileLogGetTick(MicroProfileLogEntry e) inline int64_t MicroProfileLogSetTick(MicroProfileLogEntry e, int64_t nTick) { - return (MP_LOG_TICK_MASK & nTick) | (e & ~MP_LOG_TICK_MASK); + return static_cast((MP_LOG_TICK_MASK & static_cast(nTick)) | (e & static_cast(~MP_LOG_TICK_MASK))); } template diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index 54940a034..74c1453aa 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt @@ -51,8 +51,9 @@ if (NOT MSVC) -Werror=implicit-fallthrough -Werror=reorder -Werror=sign-compare - -Werror=unused-but-set-parameter - -Werror=unused-but-set-variable + -Werror=sign-conversion + $<$:-Werror=unused-but-set-parameter> + $<$:-Werror=unused-but-set-variable> -Werror=unused-variable ) endif() diff --git a/src/audio_core/algorithm/interpolate.cpp b/src/audio_core/algorithm/interpolate.cpp index 699fcb84c..587ee5b7b 100644 --- a/src/audio_core/algorithm/interpolate.cpp +++ b/src/audio_core/algorithm/interpolate.cpp @@ -167,8 +167,8 @@ std::vector Interpolate(InterpolationState& state, std::vector input, output.reserve(static_cast(static_cast(input.size()) / ratio + InterpolationState::taps)); - for (std::size_t frame{}; frame < num_frames; ++frame) { - const std::size_t lut_index{(state.fraction >> 8) * InterpolationState::taps}; + for (std::size_t frame = 0; frame < num_frames; ++frame) { + const auto lut_index{static_cast(state.fraction >> 8) * InterpolationState::taps}; std::rotate(state.history.begin(), state.history.end() - 1, state.history.end()); state.history[0][0] = input[frame * 2 + 0]; @@ -225,7 +225,7 @@ void Resample(s32* output, const s32* input, s32 pitch, s32& fraction, std::size output[i] = (l0 * s0 + l1 * s1 + l2 * s2 + l3 * s3) >> 15; fraction += pitch; - index += (fraction >> 15); + index += static_cast(fraction >> 15); fraction &= 0x7fff; } } diff --git a/src/audio_core/audio_renderer.cpp b/src/audio_core/audio_renderer.cpp index a7e851bb8..094bace9c 100644 --- a/src/audio_core/audio_renderer.cpp +++ b/src/audio_core/audio_renderer.cpp @@ -187,8 +187,8 @@ void AudioRenderer::QueueMixedBuffer(Buffer::Tag tag) { const auto& in_params = final_mix.GetInParams(); std::vector mix_buffers(channel_count); for (std::size_t i = 0; i < channel_count; i++) { - mix_buffers[i] = - command_generator.GetMixBuffer(in_params.buffer_offset + buffer_offsets[i]); + mix_buffers[i] = command_generator.GetMixBuffer( + static_cast(in_params.buffer_offset) + buffer_offsets[i]); } for (std::size_t i = 0; i < BUFFER_SIZE; i++) { diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp index 2fb91c13a..d89f94ea2 100644 --- a/src/audio_core/codec.cpp +++ b/src/audio_core/codec.cpp @@ -32,7 +32,7 @@ std::vector DecodeADPCM(const u8* const data, std::size_t size, const ADPCM for (std::size_t framei = 0; framei < NUM_FRAMES; framei++) { const int frame_header = data[framei * FRAME_LEN]; const int scale = 1 << (frame_header & 0xF); - const int idx = (frame_header >> 4) & 0x7; + const auto idx = static_cast((frame_header >> 4) & 0x7); // Coefficients are fixed point with 11 bits fractional part. const int coef1 = coeff[idx * 2 + 0]; @@ -57,11 +57,11 @@ std::vector DecodeADPCM(const u8* const data, std::size_t size, const ADPCM std::size_t outputi = framei * SAMPLES_PER_FRAME; std::size_t datai = framei * FRAME_LEN + 1; for (std::size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) { - const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]); + const s16 sample1 = decode_sample(SIGNED_NIBBLES[static_cast(data[datai] >> 4)]); ret[outputi] = sample1; outputi++; - const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]); + const s16 sample2 = decode_sample(SIGNED_NIBBLES[static_cast(data[datai] & 0xF)]); ret[outputi] = sample2; outputi++; diff --git a/src/audio_core/command_generator.cpp b/src/audio_core/command_generator.cpp index fb8700ccf..c0edb625d 100644 --- a/src/audio_core/command_generator.cpp +++ b/src/audio_core/command_generator.cpp @@ -15,8 +15,8 @@ constexpr std::size_t MIX_BUFFER_SIZE = 0x3f00; constexpr std::size_t SCALED_MIX_BUFFER_SIZE = MIX_BUFFER_SIZE << 15ULL; template -void ApplyMix(s32* output, const s32* input, s32 gain, s32 sample_count) { - for (std::size_t i = 0; i < static_cast(sample_count); i += N) { +void ApplyMix(s32* output, const s32* input, s32 gain, std::size_t sample_count) { + for (std::size_t i = 0; i < sample_count; i += N) { for (std::size_t j = 0; j < N; j++) { output[i + j] += static_cast((static_cast(input[i + j]) * gain + 0x4000) >> 15); @@ -111,7 +111,8 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { const auto channel_count = in_params.channel_count; for (s32 channel = 0; channel < channel_count; channel++) { - const auto resource_id = in_params.voice_channel_resource_id[channel]; + const auto resource_id = + static_cast(in_params.voice_channel_resource_id[static_cast(channel)]); auto& dsp_state = voice_context.GetDspSharedState(resource_id); auto& channel_resource = voice_context.GetChannelResource(resource_id); @@ -132,14 +133,15 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { if (in_params.mix_id != AudioCommon::NO_MIX) { // If we're using a mix id - auto& mix_info = mix_context.GetInfo(in_params.mix_id); + auto& mix_info = mix_context.GetInfo(static_cast(in_params.mix_id)); const auto& dest_mix_params = mix_info.GetInParams(); // Voice Mixing GenerateVoiceMixCommand( channel_resource.GetCurrentMixVolume(), channel_resource.GetLastMixVolume(), - dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, - worker_params.mix_buffer_count + channel, in_params.node_id); + dsp_state, static_cast(dest_mix_params.buffer_offset), + static_cast(dest_mix_params.buffer_count), + worker_params.mix_buffer_count + static_cast(channel), in_params.node_id); // Update last mix volumes channel_resource.UpdateLastMixVolumes(); @@ -156,12 +158,15 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { continue; } - const auto& mix_info = mix_context.GetInfo(destination_data->GetMixId()); + const auto& mix_info = + mix_context.GetInfo(static_cast(destination_data->GetMixId())); const auto& dest_mix_params = mix_info.GetInParams(); GenerateVoiceMixCommand( destination_data->CurrentMixVolumes(), destination_data->LastMixVolumes(), - dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, - worker_params.mix_buffer_count + channel, in_params.node_id); + dsp_state, static_cast(dest_mix_params.buffer_offset), + static_cast(dest_mix_params.buffer_count), + worker_params.mix_buffer_count + static_cast(channel), + in_params.node_id); destination_data->MarkDirty(); } } @@ -219,9 +224,10 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo if (depop) { if (in_params.mix_id != AudioCommon::NO_MIX) { - auto& mix_info = mix_context.GetInfo(in_params.mix_id); + auto& mix_info = mix_context.GetInfo(static_cast(in_params.mix_id)); const auto& mix_in = mix_info.GetInParams(); - GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); + GenerateDepopPrepareCommand(dsp_state, static_cast(mix_in.buffer_count), + static_cast(mix_in.buffer_offset)); } else if (in_params.splitter_info_id != AudioCommon::NO_SPLITTER) { s32 index{}; while (const auto* destination = @@ -229,23 +235,24 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo if (!destination->IsConfigured()) { continue; } - auto& mix_info = mix_context.GetInfo(destination->GetMixId()); + auto& mix_info = mix_context.GetInfo(static_cast(destination->GetMixId())); const auto& mix_in = mix_info.GetInParams(); - GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); + GenerateDepopPrepareCommand(dsp_state, static_cast(mix_in.buffer_count), + static_cast(mix_in.buffer_offset)); } } } else { switch (in_params.sample_format) { case SampleFormat::Pcm16: DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(channel), dsp_state, channel, - worker_params.sample_rate, worker_params.sample_count, - in_params.node_id); + static_cast(worker_params.sample_rate), + static_cast(worker_params.sample_count), in_params.node_id); break; case SampleFormat::Adpcm: ASSERT(channel == 0 && in_params.channel_count == 1); DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(0), dsp_state, 0, - worker_params.sample_rate, worker_params.sample_count, - in_params.node_id); + static_cast(worker_params.sample_rate), + static_cast(worker_params.sample_count), in_params.node_id); break; default: UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); @@ -255,7 +262,7 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - s32 mix_buffer_count, s32 channel) { + u32 mix_buffer_count, s32 channel) { for (std::size_t i = 0; i < AudioCommon::MAX_BIQUAD_FILTERS; i++) { const auto& in_params = voice_info.GetInParams(); auto& biquad_filter = in_params.biquad_filter[i]; @@ -335,8 +342,8 @@ void CommandGenerator::GenerateDepopForMixBuffersCommand(std::size_t mix_buffer_ continue; } - depop_buffer[i] = - ApplyMixDepop(GetMixBuffer(i), depop_buffer[i], delta, worker_params.sample_count); + depop_buffer[i] = ApplyMixDepop(GetMixBuffer(i), depop_buffer[i], delta, + static_cast(worker_params.sample_count)); } } @@ -348,7 +355,7 @@ void CommandGenerator::GenerateEffectCommand(ServerMixInfo& mix_info) { if (index == AudioCommon::NO_EFFECT_ORDER) { break; } - auto* info = effect_context.GetInfo(index); + auto* info = effect_context.GetInfo(static_cast(index)); const auto type = info->GetType(); // TODO(ogniK): Finish remaining effects @@ -377,11 +384,11 @@ void CommandGenerator::GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, E } const auto& params = dynamic_cast(info)->GetParams(); const auto channel_count = params.channel_count; - for (s32 i = 0; i < channel_count; i++) { + for (size_t i = 0; i < channel_count; i++) { // TODO(ogniK): Actually implement reverb if (params.input[i] != params.output[i]) { - const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); - auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); + const auto* input = GetMixBuffer(static_cast(mix_buffer_offset + params.input[i])); + auto* output = GetMixBuffer(static_cast(mix_buffer_offset + params.output[i])); ApplyMix<1>(output, input, 32768, worker_params.sample_count); } } @@ -392,13 +399,14 @@ void CommandGenerator::GenerateBiquadFilterEffectCommand(s32 mix_buffer_offset, if (!enabled) { return; } + const auto& params = dynamic_cast(info)->GetParams(); - const auto channel_count = params.channel_count; - for (s32 i = 0; i < channel_count; i++) { + const auto channel_count = static_cast(params.channel_count); + for (size_t i = 0; i < channel_count; i++) { // TODO(ogniK): Actually implement biquad filter if (params.input[i] != params.output[i]) { - const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); - auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); + const auto* input = GetMixBuffer(static_cast(mix_buffer_offset + params.input[i])); + auto* output = GetMixBuffer(static_cast(mix_buffer_offset + params.output[i])); ApplyMix<1>(output, input, 32768, worker_params.sample_count); } } @@ -425,26 +433,30 @@ void CommandGenerator::GenerateAuxCommand(s32 mix_buffer_offset, EffectBase* inf memory.ReadBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); memory.ReadBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); - WriteAuxBuffer(send_info, aux->GetSendBuffer(), params.sample_count, - GetMixBuffer(input_index), worker_params.sample_count, offset, - write_count); + WriteAuxBuffer(send_info, aux->GetSendBuffer(), + static_cast(params.sample_count), + GetMixBuffer(static_cast(input_index)), + worker_params.sample_count, offset, write_count); memory.WriteBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); const auto samples_read = ReadAuxBuffer( - recv_info, aux->GetRecvBuffer(), params.sample_count, - GetMixBuffer(output_index), worker_params.sample_count, offset, write_count); + recv_info, aux->GetRecvBuffer(), static_cast(params.sample_count), + GetMixBuffer(static_cast(output_index)), worker_params.sample_count, + offset, write_count); memory.WriteBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); if (samples_read != static_cast(worker_params.sample_count) && samples_read <= params.sample_count) { - std::memset(GetMixBuffer(output_index), 0, params.sample_count - samples_read); + std::memset(GetMixBuffer(static_cast(output_index)), 0, + static_cast(params.sample_count - samples_read)); } } else { AuxInfoDSP empty{}; memory.WriteBlock(aux->GetSendInfo(), &empty, sizeof(AuxInfoDSP)); memory.WriteBlock(aux->GetRecvInfo(), &empty, sizeof(AuxInfoDSP)); if (output_index != input_index) { - std::memcpy(GetMixBuffer(output_index), GetMixBuffer(input_index), + std::memcpy(GetMixBuffer(static_cast(output_index)), + GetMixBuffer(static_cast(input_index)), worker_params.sample_count * sizeof(s32)); } } @@ -458,7 +470,8 @@ ServerSplitterDestinationData* CommandGenerator::GetDestinationData(s32 splitter if (splitter_id == AudioCommon::NO_SPLITTER) { return nullptr; } - return splitter_context.GetDestinationData(splitter_id, index); + return splitter_context.GetDestinationData(static_cast(splitter_id), + static_cast(index)); } s32 CommandGenerator::WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u32 max_samples, @@ -488,7 +501,7 @@ s32 CommandGenerator::WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u3 if (write_count != 0) { dsp_info.write_offset = (dsp_info.write_offset + write_count) % max_samples; } - return sample_count; + return static_cast(sample_count); } s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, @@ -518,7 +531,7 @@ s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u3 if (read_count != 0) { recv_info.read_offset = (recv_info.read_offset + read_count) % max_samples; } - return sample_count; + return static_cast(sample_count); } void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float current_volume, @@ -537,15 +550,15 @@ void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float curren } // Apply generic gain on samples ApplyGain(GetChannelMixBuffer(channel), GetChannelMixBuffer(channel), last, delta, - worker_params.sample_count); + static_cast(worker_params.sample_count)); } void CommandGenerator::GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, const MixVolumeBuffer& last_mix_volumes, - VoiceState& dsp_state, s32 mix_buffer_offset, - s32 mix_buffer_count, s32 voice_index, s32 node_id) { + VoiceState& dsp_state, u32 mix_buffer_offset, + u32 mix_buffer_count, u32 voice_index, s32 node_id) { // Loop all our mix buffers - for (s32 i = 0; i < mix_buffer_count; i++) { + for (size_t i = 0; i < mix_buffer_count; i++) { if (last_mix_volumes[i] != 0.0f || mix_volumes[i] != 0.0f) { const auto delta = static_cast((mix_volumes[i] - last_mix_volumes[i])) / static_cast(worker_params.sample_count); @@ -558,9 +571,9 @@ void CommandGenerator::GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volume mix_volumes[i]); } - dsp_state.previous_samples[i] = - ApplyMixRamp(GetMixBuffer(mix_buffer_offset + i), GetMixBuffer(voice_index), - last_mix_volumes[i], delta, worker_params.sample_count); + dsp_state.previous_samples[i] = ApplyMixRamp( + GetMixBuffer(mix_buffer_offset + i), GetMixBuffer(voice_index), last_mix_volumes[i], + delta, static_cast(worker_params.sample_count)); } else { dsp_state.previous_samples[i] = 0; } @@ -572,7 +585,8 @@ void CommandGenerator::GenerateSubMixCommand(ServerMixInfo& mix_info) { LOG_DEBUG(Audio, "(DSP_TRACE) GenerateSubMixCommand"); } const auto& in_params = mix_info.GetInParams(); - GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, + GenerateDepopForMixBuffersCommand(static_cast(in_params.buffer_count), + static_cast(in_params.buffer_offset), in_params.sample_rate); GenerateEffectCommand(mix_info); @@ -586,18 +600,18 @@ void CommandGenerator::GenerateMixCommands(ServerMixInfo& mix_info) { } const auto& in_params = mix_info.GetInParams(); if (in_params.dest_mix_id != AudioCommon::NO_MIX) { - const auto& dest_mix = mix_context.GetInfo(in_params.dest_mix_id); + const auto& dest_mix = mix_context.GetInfo(static_cast(in_params.dest_mix_id)); const auto& dest_in_params = dest_mix.GetInParams(); - const auto buffer_count = in_params.buffer_count; + const auto buffer_count = static_cast(in_params.buffer_count); - for (s32 i = 0; i < buffer_count; i++) { - for (s32 j = 0; j < dest_in_params.buffer_count; j++) { + for (u32 i = 0; i < buffer_count; i++) { + for (u32 j = 0; j < static_cast(dest_in_params.buffer_count); j++) { const auto mixed_volume = in_params.volume * in_params.mix_volume[i][j]; if (mixed_volume != 0.0f) { - GenerateMixCommand(dest_in_params.buffer_offset + j, - in_params.buffer_offset + i, mixed_volume, - in_params.node_id); + GenerateMixCommand(static_cast(dest_in_params.buffer_offset) + j, + static_cast(in_params.buffer_offset) + i, + mixed_volume, static_cast(in_params.node_id)); } } } @@ -608,15 +622,17 @@ void CommandGenerator::GenerateMixCommands(ServerMixInfo& mix_info) { continue; } - const auto& dest_mix = mix_context.GetInfo(destination_data->GetMixId()); + const auto& dest_mix = + mix_context.GetInfo(static_cast(destination_data->GetMixId())); const auto& dest_in_params = dest_mix.GetInParams(); const auto mix_index = (base - 1) % in_params.buffer_count + in_params.buffer_offset; for (std::size_t i = 0; i < static_cast(dest_in_params.buffer_count); i++) { const auto mixed_volume = in_params.volume * destination_data->GetMixVolume(i); if (mixed_volume != 0.0f) { - GenerateMixCommand(dest_in_params.buffer_offset + i, mix_index, mixed_volume, - in_params.node_id); + GenerateMixCommand(static_cast(dest_in_params.buffer_offset) + i, + static_cast(mix_index), mixed_volume, + static_cast(in_params.node_id)); } } } @@ -635,7 +651,8 @@ void CommandGenerator::GenerateMixCommand(std::size_t output_offset, std::size_t auto* output = GetMixBuffer(output_offset); const auto* input = GetMixBuffer(input_offset); - const s32 gain = static_cast(volume * 32768.0f); + const auto gain = static_cast(volume * 32768.0f); + // Mix with loop unrolling if (worker_params.sample_count % 4 == 0) { ApplyMix<4>(output, input, gain, worker_params.sample_count); @@ -653,7 +670,8 @@ void CommandGenerator::GenerateFinalMixCommand() { auto& mix_info = mix_context.GetFinalMixInfo(); const auto& in_params = mix_info.GetInParams(); - GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, + GenerateDepopForMixBuffersCommand(static_cast(in_params.buffer_count), + static_cast(in_params.buffer_offset), in_params.sample_rate); GenerateEffectCommand(mix_info); @@ -667,16 +685,16 @@ void CommandGenerator::GenerateFinalMixCommand() { in_params.node_id, in_params.buffer_offset + i, in_params.buffer_offset + i, in_params.volume); } - ApplyGainWithoutDelta(GetMixBuffer(in_params.buffer_offset + i), - GetMixBuffer(in_params.buffer_offset + i), gain, - worker_params.sample_count); + ApplyGainWithoutDelta(GetMixBuffer(static_cast(in_params.buffer_offset + i)), + GetMixBuffer(static_cast(in_params.buffer_offset + i)), gain, + static_cast(worker_params.sample_count)); } } s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, s32 channel, std::size_t mix_offset) { const auto& in_params = voice_info.GetInParams(); - const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + const auto& wave_buffer = in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; if (wave_buffer.buffer_address == 0) { return 0; } @@ -689,24 +707,26 @@ s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_s const auto samples_remaining = (wave_buffer.end_sample_offset - wave_buffer.start_sample_offset) - dsp_state.offset; const auto start_offset = - ((wave_buffer.start_sample_offset + dsp_state.offset) * in_params.channel_count) * + static_cast((wave_buffer.start_sample_offset + dsp_state.offset) * + in_params.channel_count) * sizeof(s16); const auto buffer_pos = wave_buffer.buffer_address + start_offset; const auto samples_processed = std::min(sample_count, samples_remaining); if (in_params.channel_count == 1) { - std::vector buffer(samples_processed); + std::vector buffer(static_cast(samples_processed)); memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); for (std::size_t i = 0; i < buffer.size(); i++) { sample_buffer[mix_offset + i] = buffer[i]; } } else { const auto channel_count = in_params.channel_count; - std::vector buffer(samples_processed * channel_count); + std::vector buffer(static_cast(samples_processed * channel_count)); memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); for (std::size_t i = 0; i < static_cast(samples_processed); i++) { - sample_buffer[mix_offset + i] = buffer[i * channel_count + channel]; + sample_buffer[mix_offset + i] = + buffer[i * static_cast(channel_count) + static_cast(channel)]; } } @@ -716,7 +736,7 @@ s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_s s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, s32 channel, std::size_t mix_offset) { const auto& in_params = voice_info.GetInParams(); - const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + const auto& wave_buffer = in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; if (wave_buffer.buffer_address == 0) { return 0; } @@ -736,7 +756,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s constexpr std::size_t SAMPLES_PER_FRAME = 14; auto frame_header = dsp_state.context.header; - s32 idx = (frame_header >> 4) & 0xf; + auto idx = static_cast((frame_header >> 4) & 0xf); s32 scale = frame_header & 0xf; s16 yn1 = dsp_state.context.yn1; s16 yn2 = dsp_state.context.yn2; @@ -753,9 +773,10 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s const auto samples_processed = std::min(sample_count, samples_remaining); const auto sample_pos = wave_buffer.start_sample_offset + dsp_state.offset; - const auto samples_remaining_in_frame = sample_pos % SAMPLES_PER_FRAME; - auto position_in_frame = ((sample_pos / SAMPLES_PER_FRAME) * NIBBLES_PER_SAMPLE) + - samples_remaining_in_frame + (samples_remaining_in_frame != 0 ? 2 : 0); + const auto samples_remaining_in_frame = static_cast(sample_pos) % SAMPLES_PER_FRAME; + auto position_in_frame = + ((static_cast(sample_pos) / SAMPLES_PER_FRAME) * NIBBLES_PER_SAMPLE) + + samples_remaining_in_frame + (samples_remaining_in_frame != 0 ? 2 : 0); const auto decode_sample = [&](const int nibble) -> s16 { const int xn = nibble * (1 << scale); @@ -774,7 +795,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s std::size_t buffer_offset{}; std::vector buffer( - std::max((samples_processed / FRAME_LEN) * SAMPLES_PER_FRAME, FRAME_LEN)); + std::max((static_cast(samples_processed) / FRAME_LEN) * SAMPLES_PER_FRAME, FRAME_LEN)); memory.ReadBlock(wave_buffer.buffer_address + (position_in_frame / 2), buffer.data(), buffer.size()); std::size_t cur_mix_offset = mix_offset; @@ -784,7 +805,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s if (position_in_frame % NIBBLES_PER_SAMPLE == 0) { // Read header frame_header = buffer[buffer_offset++]; - idx = (frame_header >> 4) & 0xf; + idx = static_cast((frame_header >> 4) & 0xf); scale = frame_header & 0xf; coef1 = coeffs[idx * 2]; coef2 = coeffs[idx * 2 + 1]; @@ -794,8 +815,8 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s if (remaining_samples >= static_cast(SAMPLES_PER_FRAME)) { for (std::size_t i = 0; i < SAMPLES_PER_FRAME / 2; i++) { // Sample 1 - const s32 s0 = SIGNED_NIBBLES[buffer[buffer_offset] >> 4]; - const s32 s1 = SIGNED_NIBBLES[buffer[buffer_offset++] & 0xf]; + const s32 s0 = SIGNED_NIBBLES[static_cast(buffer[buffer_offset] >> 4)]; + const s32 s1 = SIGNED_NIBBLES[static_cast(buffer[buffer_offset++] & 0xf)]; const s16 sample_1 = decode_sample(s0); const s16 sample_2 = decode_sample(s1); sample_buffer[cur_mix_offset++] = sample_1; @@ -807,14 +828,14 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s } } // Decode mid frame - s32 current_nibble = buffer[buffer_offset]; - if (position_in_frame++ & 0x1) { + auto current_nibble = static_cast(buffer[buffer_offset]); + if ((position_in_frame++ & 1) != 0) { current_nibble &= 0xf; buffer_offset++; } else { current_nibble >>= 4; } - const s16 sample = decode_sample(SIGNED_NIBBLES[current_nibble]); + const s16 sample = decode_sample(SIGNED_NIBBLES[static_cast(current_nibble)]); sample_buffer[cur_mix_offset++] = sample; remaining_samples--; } @@ -835,7 +856,7 @@ const s32* CommandGenerator::GetMixBuffer(std::size_t index) const { } std::size_t CommandGenerator::GetMixChannelBufferOffset(s32 channel) const { - return worker_params.mix_buffer_count + channel; + return worker_params.mix_buffer_count + static_cast(channel); } std::size_t CommandGenerator::GetTotalMixBufferCount() const { @@ -843,11 +864,11 @@ std::size_t CommandGenerator::GetTotalMixBufferCount() const { } s32* CommandGenerator::GetChannelMixBuffer(s32 channel) { - return GetMixBuffer(worker_params.mix_buffer_count + channel); + return GetMixBuffer(worker_params.mix_buffer_count + static_cast(channel)); } const s32* CommandGenerator::GetChannelMixBuffer(s32 channel) const { - return GetMixBuffer(worker_params.mix_buffer_count + channel); + return GetMixBuffer(worker_params.mix_buffer_count + static_cast(channel)); } void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* output, @@ -895,9 +916,10 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o s32 samples_read{}; while (samples_read < samples_to_read) { - const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; + const auto& wave_buffer = + in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; // No more data can be read - if (!dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index]) { + if (!dsp_state.is_wave_buffer_valid[static_cast(dsp_state.wave_buffer_index)]) { is_buffer_completed = true; break; } @@ -921,7 +943,7 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); } - temp_mix_offset += samples_decoded; + temp_mix_offset += static_cast(samples_decoded); samples_read += samples_decoded; dsp_state.offset += samples_decoded; dsp_state.played_sample_count += samples_decoded; @@ -944,10 +966,12 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o } else { // Update our wave buffer states - dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index] = false; + dsp_state.is_wave_buffer_valid[static_cast(dsp_state.wave_buffer_index)] = + false; dsp_state.wave_buffer_consumed++; dsp_state.wave_buffer_index = - (dsp_state.wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + static_cast(dsp_state.wave_buffer_index + 1) % + AudioCommon::MAX_WAVE_BUFFERS; if (wave_buffer.end_of_stream) { dsp_state.played_sample_count = 0; } @@ -957,16 +981,20 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o if (in_params.behavior_flags.is_pitch_and_src_skipped.Value()) { // No need to resample - std::memcpy(output, sample_buffer.data(), samples_read * sizeof(s32)); + std::memcpy(output, sample_buffer.data(), + static_cast(samples_read) * sizeof(s32)); } else { - std::fill(sample_buffer.begin() + temp_mix_offset, - sample_buffer.begin() + temp_mix_offset + (samples_to_read - samples_read), - 0); + { + const auto begin = sample_buffer.begin() + static_cast(temp_mix_offset); + const auto end = begin + (samples_to_read - samples_read); + std::fill(begin, end, 0); + } AudioCore::Resample(output, sample_buffer.data(), resample_rate, dsp_state.fraction, - samples_to_output); + static_cast(samples_to_output)); // Resample for (std::size_t i = 0; i < AudioCommon::MAX_SAMPLE_HISTORY; i++) { - dsp_state.sample_history[i] = sample_buffer[samples_to_read + i]; + dsp_state.sample_history[i] = + sample_buffer[static_cast(samples_to_read) + i]; } } output += samples_to_output; diff --git a/src/audio_core/command_generator.h b/src/audio_core/command_generator.h index 53e57748b..6cba70ae3 100644 --- a/src/audio_core/command_generator.h +++ b/src/audio_core/command_generator.h @@ -50,12 +50,12 @@ public: private: void GenerateDataSourceCommand(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 channel); void GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - s32 mix_buffer_count, s32 channel); + u32 mix_buffer_count, s32 channel); void GenerateVolumeRampCommand(float last_volume, float current_volume, s32 channel, s32 node_id); void GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, const MixVolumeBuffer& last_mix_volumes, VoiceState& dsp_state, - s32 mix_buffer_offset, s32 mix_buffer_count, s32 voice_index, + u32 mix_buffer_offset, u32 mix_buffer_count, u32 voice_index, s32 node_id); void GenerateSubMixCommand(ServerMixInfo& mix_info); void GenerateMixCommands(ServerMixInfo& mix_info); diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index 6eaa60815..a20b6ad5f 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp @@ -202,7 +202,7 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const } const std::size_t num_channels = impl->GetNumChannels(); - const std::size_t samples_to_write = num_channels * num_frames; + const std::size_t samples_to_write = num_channels * static_cast(num_frames); std::size_t samples_written; /* diff --git a/src/audio_core/cubeb_sink.h b/src/audio_core/cubeb_sink.h index 7ce850f47..c50d0b7bd 100644 --- a/src/audio_core/cubeb_sink.h +++ b/src/audio_core/cubeb_sink.h @@ -27,7 +27,7 @@ private: std::vector sink_streams; #ifdef _WIN32 - u32 com_init_result = 0; + s32 com_init_result = 0; #endif }; diff --git a/src/audio_core/info_updater.cpp b/src/audio_core/info_updater.cpp index 2940e53a9..f999a8b17 100644 --- a/src/audio_core/info_updater.cpp +++ b/src/audio_core/info_updater.cpp @@ -350,7 +350,7 @@ ResultCode InfoUpdater::UpdateMixes(MixContext& mix_context, std::size_t mix_buf std::size_t total_buffer_count{}; for (std::size_t i = 0; i < mix_count; i++) { const auto& in = mix_in_params[i]; - total_buffer_count += in.buffer_count; + total_buffer_count += static_cast(in.buffer_count); if (static_cast(in.dest_mix_id) > mix_count && in.dest_mix_id != AudioCommon::NO_MIX && in.mix_id != AudioCommon::FINAL_MIX) { LOG_ERROR( @@ -379,7 +379,7 @@ ResultCode InfoUpdater::UpdateMixes(MixContext& mix_context, std::size_t mix_buf const auto& mix_in = mix_in_params[i]; std::size_t target_mix{}; if (behavior_info.IsMixInParameterDirtyOnlyUpdateSupported()) { - target_mix = mix_in.mix_id; + target_mix = static_cast(mix_in.mix_id); } else { // Non dirty supported games just use i instead of the actual mix_id target_mix = i; diff --git a/src/audio_core/mix_context.cpp b/src/audio_core/mix_context.cpp index 4bca72eb0..c28bee453 100644 --- a/src/audio_core/mix_context.cpp +++ b/src/audio_core/mix_context.cpp @@ -62,7 +62,7 @@ void MixContext::UpdateDistancesFromFinalMix() { distance_to_final_mix = AudioCommon::NO_FINAL_MIX; break; } else { - const auto& dest_mix = GetInfo(mix_id); + const auto& dest_mix = GetInfo(static_cast(mix_id)); const auto dest_mix_distance = dest_mix.GetInParams().final_mix_distance; if (dest_mix_distance == AudioCommon::NO_FINAL_MIX) { @@ -129,7 +129,7 @@ bool MixContext::TsortInfo(SplitterContext& splitter_context) { std::size_t info_id{}; for (auto itr = sorted_list.rbegin(); itr != sorted_list.rend(); ++itr) { // Set our sorted info - sorted_info[info_id++] = &GetInfo(*itr); + sorted_info[info_id++] = &GetInfo(static_cast(*itr)); } // Calculate the mix buffer offset @@ -218,7 +218,8 @@ bool ServerMixInfo::Update(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix for (std::size_t i = 0; i < effect_count; i++) { auto* effect_info = effect_context.GetInfo(i); if (effect_info->GetMixID() == in_params.mix_id) { - effect_processing_order[effect_info->GetProcessingOrder()] = static_cast(i); + const auto processing_order = static_cast(effect_info->GetProcessingOrder()); + effect_processing_order[processing_order] = static_cast(i); } } @@ -265,7 +266,7 @@ bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InP if (in_params.dest_mix_id == mix_in.dest_mix_id && in_params.splitter_id == mix_in.splitter_id && ((in_params.splitter_id == AudioCommon::NO_SPLITTER) || - !splitter_context.GetInfo(in_params.splitter_id).HasNewConnection())) { + !splitter_context.GetInfo(static_cast(in_params.splitter_id)).HasNewConnection())) { return false; } // Remove current edges for mix id @@ -275,11 +276,11 @@ bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InP edge_matrix.Connect(in_params.mix_id, mix_in.dest_mix_id); } else if (mix_in.splitter_id != AudioCommon::NO_SPLITTER) { // Recurse our splitter linked and set our edges - auto& splitter_info = splitter_context.GetInfo(mix_in.splitter_id); - const auto length = splitter_info.GetLength(); - for (s32 i = 0; i < length; i++) { + auto& splitter_info = splitter_context.GetInfo(static_cast(mix_in.splitter_id)); + const auto length = static_cast(splitter_info.GetLength()); + for (size_t i = 0; i < length; i++) { const auto* splitter_destination = - splitter_context.GetDestinationData(mix_in.splitter_id, i); + splitter_context.GetDestinationData(static_cast(mix_in.splitter_id), i); if (splitter_destination == nullptr) { continue; } diff --git a/src/audio_core/sink_context.cpp b/src/audio_core/sink_context.cpp index 0882b411a..3d713814a 100644 --- a/src/audio_core/sink_context.cpp +++ b/src/audio_core/sink_context.cpp @@ -23,8 +23,9 @@ bool SinkContext::InUse() const { } std::vector SinkContext::OutputBuffers() const { - std::vector buffer_ret(use_count); - std::memcpy(buffer_ret.data(), buffers.data(), use_count); + const auto output_use_count = static_cast(use_count); + std::vector buffer_ret(output_use_count); + std::memcpy(buffer_ret.data(), buffers.data(), output_use_count); return buffer_ret; } diff --git a/src/audio_core/splitter_context.cpp b/src/audio_core/splitter_context.cpp index f21b53147..f3e870648 100644 --- a/src/audio_core/splitter_context.cpp +++ b/src/audio_core/splitter_context.cpp @@ -109,7 +109,7 @@ std::size_t ServerSplitterInfo::Update(SplitterInfo::InInfoPrams& header) { new_connection = true; // We need to update the size here due to the splitter bug being present and providing an // incorrect size. We're suppose to also update the header here but we just ignore and continue - return (sizeof(s32_le) * (header.length - 1)) + (sizeof(s32_le) * 3); + return (sizeof(s32_le) * static_cast(header.length - 1)) + (sizeof(s32_le) * 3); } ServerSplitterDestinationData* ServerSplitterInfo::GetHead() { @@ -306,13 +306,14 @@ bool SplitterContext::UpdateInfo(const std::vector& input, std::size_t& inpu break; } - if (header.send_id < 0 || static_cast(header.send_id) > info_count) { + const auto send_id = static_cast(header.send_id); + if (header.send_id < 0 || send_id > info_count) { LOG_ERROR(Audio, "Bad splitter data id"); break; } UpdateOffsets(sizeof(SplitterInfo::InInfoPrams)); - auto& info = GetInfo(header.send_id); + auto& info = GetInfo(send_id); if (!RecomposeDestination(info, header, input, input_offset)) { LOG_ERROR(Audio, "Failed to recompose destination for splitter!"); return false; @@ -348,11 +349,12 @@ bool SplitterContext::UpdateData(const std::vector& input, std::size_t& inpu break; } - if (header.splitter_id < 0 || static_cast(header.splitter_id) > data_count) { + const auto splitter_id = static_cast(header.splitter_id); + if (header.splitter_id < 0 || splitter_id > data_count) { LOG_ERROR(Audio, "Bad splitter data id"); break; } - GetData(header.splitter_id).Update(header); + GetData(splitter_id).Update(header); } return true; } @@ -386,9 +388,9 @@ bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, return true; } - auto* start_head = &GetData(header.resource_id_base); + auto* start_head = &GetData(static_cast(header.resource_id_base)); current_head = start_head; - std::vector resource_ids(size - 1); + std::vector resource_ids(static_cast(size - 1)); if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, resource_ids.size() * sizeof(s32_le))) { LOG_ERROR(Audio, "Buffer is an invalid size!"); @@ -397,8 +399,8 @@ bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, std::memcpy(resource_ids.data(), input.data() + input_offset, resource_ids.size() * sizeof(s32_le)); - for (auto resource_id : resource_ids) { - auto* head = &GetData(resource_id); + for (const auto resource_id : resource_ids) { + auto* head = &GetData(static_cast(resource_id)); current_head->SetNextDestination(head); current_head = head; } @@ -444,7 +446,7 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { const auto node_id = static_cast(i); // If we don't have a state, send to our index stack for work - if (GetState(i) == NodeStates::State::NoState) { + if (GetState(i) == State::NoState) { index_stack.push(node_id); } @@ -453,19 +455,19 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { // Get the current node const auto current_stack_index = index_stack.top(); // Check if we've seen the node yet - const auto index_state = GetState(current_stack_index); - if (index_state == NodeStates::State::NoState) { + const auto index_state = GetState(static_cast(current_stack_index)); + if (index_state == State::NoState) { // Mark the node as seen - UpdateState(NodeStates::State::InFound, current_stack_index); - } else if (index_state == NodeStates::State::InFound) { + UpdateState(State::InFound, static_cast(current_stack_index)); + } else if (index_state == State::InFound) { // We've seen this node before, mark it as completed - UpdateState(NodeStates::State::InCompleted, current_stack_index); + UpdateState(State::InCompleted, static_cast(current_stack_index)); // Update our index list PushTsortResult(current_stack_index); // Pop the stack index_stack.pop(); continue; - } else if (index_state == NodeStates::State::InCompleted) { + } else if (index_state == State::InCompleted) { // If our node is already sorted, clear it index_stack.pop(); continue; @@ -479,11 +481,11 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { } // Check if our node exists - const auto node_state = GetState(j); - if (node_state == NodeStates::State::NoState) { + const auto node_state = GetState(static_cast(j)); + if (node_state == State::NoState) { // Add more work index_stack.push(j); - } else if (node_state == NodeStates::State::InFound) { + } else if (node_state == State::InFound) { UNREACHABLE_MSG("Node start marked as found"); ResetState(); return false; @@ -507,17 +509,17 @@ void NodeStates::ResetState() { } } -void NodeStates::UpdateState(NodeStates::State state, std::size_t i) { +void NodeStates::UpdateState(State state, std::size_t i) { switch (state) { - case NodeStates::State::NoState: + case State::NoState: was_node_found[i] = false; was_node_completed[i] = false; break; - case NodeStates::State::InFound: + case State::InFound: was_node_found[i] = true; was_node_completed[i] = false; break; - case NodeStates::State::InCompleted: + case State::InCompleted: was_node_found[i] = false; was_node_completed[i] = true; break; @@ -528,13 +530,13 @@ NodeStates::State NodeStates::GetState(std::size_t i) { ASSERT(i < node_count); if (was_node_found[i]) { // If our node exists in our found list - return NodeStates::State::InFound; + return State::InFound; } else if (was_node_completed[i]) { // If node is in the completed list - return NodeStates::State::InCompleted; + return State::InCompleted; } else { // If in neither - return NodeStates::State::NoState; + return State::NoState; } } @@ -601,16 +603,16 @@ std::size_t EdgeMatrix::GetNodeCount() const { void EdgeMatrix::SetState(s32 a, s32 b, bool state) { ASSERT(InRange(a, b)); - edge_matrix.at(a * node_count + b) = state; + edge_matrix.at(static_cast(a) * node_count + static_cast(b)) = state; } bool EdgeMatrix::GetState(s32 a, s32 b) { ASSERT(InRange(a, b)); - return edge_matrix.at(a * node_count + b); + return edge_matrix.at(static_cast(a) * node_count + static_cast(b)); } bool EdgeMatrix::InRange(s32 a, s32 b) const { - const std::size_t pos = a * node_count + b; + const std::size_t pos = static_cast(a) * node_count + static_cast(b); return pos < (node_count * node_count); } diff --git a/src/audio_core/time_stretch.h b/src/audio_core/time_stretch.h index bb2270b96..3808e554d 100644 --- a/src/audio_core/time_stretch.h +++ b/src/audio_core/time_stretch.h @@ -5,9 +5,17 @@ #pragma once #include -#include #include "common/common_types.h" +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-conversion" +#endif +#include +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + namespace AudioCore { class TimeStretcher { diff --git a/src/audio_core/voice_context.cpp b/src/audio_core/voice_context.cpp index c46ee55f1..276b96ca4 100644 --- a/src/audio_core/voice_context.cpp +++ b/src/audio_core/voice_context.cpp @@ -98,7 +98,7 @@ void ServerVoiceInfo::UpdateParameters(const VoiceInfo::InParams& voice_in, BehaviorInfo& behavior_info) { in_params.in_use = voice_in.is_in_use; in_params.id = voice_in.id; - in_params.node_id = voice_in.node_id; + in_params.node_id = static_cast(voice_in.node_id); in_params.last_playstate = in_params.current_playstate; switch (voice_in.play_state) { case PlayState::Paused: @@ -220,8 +220,10 @@ void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, if (sample_format == SampleFormat::Pcm16) { const auto buffer_size = in_wave_buffer.buffer_size; if (in_wave_buffer.start_sample_offset < 0 || in_wave_buffer.end_sample_offset < 0 || - (buffer_size < (sizeof(s16) * in_wave_buffer.start_sample_offset)) || - (buffer_size < (sizeof(s16) * in_wave_buffer.end_sample_offset))) { + (buffer_size < + (sizeof(s16) * static_cast(in_wave_buffer.start_sample_offset))) || + (buffer_size < + (sizeof(s16) * static_cast(in_wave_buffer.end_sample_offset)))) { // TODO(ogniK): Write error info return; } @@ -254,8 +256,8 @@ void ServerVoiceInfo::WriteOutStatus( voice_out.played_sample_count = 0; voice_out.voice_dropped = false; } else if (!in_params.is_new) { - voice_out.wave_buffer_consumed = voice_states[0]->wave_buffer_consumed; - voice_out.played_sample_count = voice_states[0]->played_sample_count; + voice_out.wave_buffer_consumed = static_cast(voice_states[0]->wave_buffer_consumed); + voice_out.played_sample_count = static_cast(voice_states[0]->played_sample_count); voice_out.voice_dropped = in_params.voice_drop_flag; } else { voice_out.wave_buffer_consumed = 0; @@ -293,8 +295,8 @@ bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) { in_params.is_new = false; } - const s32 channel_count = in_params.channel_count; - for (s32 i = 0; i < channel_count; i++) { + const auto channel_count = static_cast(in_params.channel_count); + for (size_t i = 0; i < channel_count; i++) { const auto channel_resource = in_params.voice_channel_resource_id[i]; dsp_voice_states[i] = &voice_context.GetDspSharedState(static_cast(channel_resource)); @@ -303,8 +305,9 @@ bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) { } void ServerVoiceInfo::ResetResources(VoiceContext& voice_context) { - const s32 channel_count = in_params.channel_count; - for (s32 i = 0; i < channel_count; i++) { + const auto channel_count = static_cast(in_params.channel_count); + + for (size_t i = 0; i < channel_count; i++) { const auto channel_resource = in_params.voice_channel_resource_id[i]; auto& dsp_state = voice_context.GetDspSharedState(static_cast(channel_resource)); @@ -325,9 +328,9 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( switch (in_params.current_playstate) { case ServerPlayState::Play: { - for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { + for (size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { if (!in_params.wave_buffer[i].sent_to_dsp) { - for (s32 channel = 0; channel < channel_count; channel++) { + for (size_t channel = 0; channel < static_cast(channel_count); channel++) { dsp_voice_states[channel]->is_wave_buffer_valid[i] = true; } in_params.wave_buffer[i].sent_to_dsp = true; @@ -344,12 +347,13 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( case ServerPlayState::RequestStop: { for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { in_params.wave_buffer[i].sent_to_dsp = true; - for (s32 channel = 0; channel < channel_count; channel++) { + for (std::size_t channel = 0; channel < static_cast(channel_count); channel++) { auto* dsp_state = dsp_voice_states[channel]; if (dsp_state->is_wave_buffer_valid[i]) { dsp_state->wave_buffer_index = - (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + static_cast(static_cast(dsp_state->wave_buffer_index + 1) % + AudioCommon::MAX_WAVE_BUFFERS); dsp_state->wave_buffer_consumed++; } @@ -357,7 +361,7 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( } } - for (s32 channel = 0; channel < channel_count; channel++) { + for (size_t channel = 0; channel < static_cast(channel_count); channel++) { auto* dsp_state = dsp_voice_states[channel]; dsp_state->offset = 0; dsp_state->played_sample_count = 0; @@ -383,15 +387,16 @@ void ServerVoiceInfo::FlushWaveBuffers( auto wave_head = in_params.wave_bufffer_head; for (u8 i = 0; i < flush_count; i++) { - in_params.wave_buffer[wave_head].sent_to_dsp = true; - for (s32 channel = 0; channel < channel_count; channel++) { + in_params.wave_buffer[static_cast(wave_head)].sent_to_dsp = true; + for (size_t channel = 0; channel < static_cast(channel_count); channel++) { auto* dsp_state = dsp_voice_states[channel]; dsp_state->wave_buffer_consumed++; - dsp_state->is_wave_buffer_valid[wave_head] = false; - dsp_state->wave_buffer_index = - (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; + dsp_state->is_wave_buffer_valid[static_cast(wave_head)] = false; + dsp_state->wave_buffer_index = static_cast( + static_cast(dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS); } - wave_head = (wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS; + wave_head = + static_cast(static_cast(wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS); } } @@ -483,7 +488,7 @@ s32 VoiceContext::DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, const auto samples_remaining = (wave_buffer->end_sample_offset - wave_buffer->start_sample_offset) - buffer_offset; const auto start_offset = (wave_buffer->start_sample_offset + buffer_offset) * channel_count; - const auto buffer_pos = wave_buffer->buffer_address + start_offset; + const auto buffer_pos = wave_buffer->buffer_address + static_cast(start_offset); s16* buffer_data = reinterpret_cast(memory.GetPointer(buffer_pos)); diff --git a/src/common/fiber.h b/src/common/fiber.h index 89dde5e36..bc1db1582 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = default; - Fiber& operator=(Fiber&&) = default; + Fiber(Fiber&&) = delete; + Fiber& operator=(Fiber&&) = delete; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/file_util.h b/src/common/file_util.h index 8b587320f..508b7a10a 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -189,7 +189,8 @@ template return {}; } last = std::min(last, vector.size()); - return std::vector(vector.begin() + first, vector.begin() + first + last); + return std::vector(vector.begin() + static_cast(first), + vector.begin() + static_cast(first + last)); } enum class DirectorySeparator { diff --git a/src/common/math_util.h b/src/common/math_util.h index b35ad8507..661c056ca 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return std::abs(static_cast>(right - left)); + return static_cast(std::abs(static_cast>(right - left))); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return std::abs(static_cast>(bottom - top)); + return static_cast(std::abs(static_cast>(bottom - top))); } } diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h index 4b305bf40..71613f18b 100644 --- a/src/common/multi_level_queue.h +++ b/src/common/multi_level_queue.h @@ -320,7 +320,7 @@ private: } const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, shift); + const auto end_range = std::next(begin_range, static_cast(shift)); list.splice(list.end(), list, begin_range, end_range); } diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 4f946a258..06ac2f5bb 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,6 +15,14 @@ namespace Common { */ class SpinLock { public: + SpinLock() = default; + + SpinLock(const SpinLock&) = delete; + SpinLock& operator=(const SpinLock&) = delete; + + SpinLock(SpinLock&&) = delete; + SpinLock& operator=(SpinLock&&) = delete; + void lock(); void unlock(); [[nodiscard]] bool try_lock(); diff --git a/src/common/swap.h b/src/common/swap.h index 7665942a2..8c68c1f26 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -504,35 +504,35 @@ bool operator==(const S& p, const swap_struct_t v) { template struct swap_64_t { static T swap(T x) { - return static_cast(Common::swap64(x)); + return static_cast(Common::swap64(static_cast(x))); } }; template struct swap_32_t { static T swap(T x) { - return static_cast(Common::swap32(x)); + return static_cast(Common::swap32(static_cast(x))); } }; template struct swap_16_t { static T swap(T x) { - return static_cast(Common::swap16(x)); + return static_cast(Common::swap16(static_cast(x))); } }; template struct swap_float_t { static T swap(T x) { - return static_cast(Common::swapf(x)); + return static_cast(Common::swapf(static_cast(x))); } }; template struct swap_double_t { static T swap(T x) { - return static_cast(Common::swapd(x)); + return static_cast(Common::swapd(static_cast(x))); } }; diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h index def9e5d8d..69c9193da 100644 --- a/src/common/thread_queue_list.h +++ b/src/common/thread_queue_list.h @@ -33,7 +33,7 @@ struct ThreadQueueList { } } - return -1; + return static_cast(-1); } [[nodiscard]] T get_first() const { @@ -156,7 +156,7 @@ private: void link(Priority priority) { Queue* cur = &queues[priority]; - for (int i = priority - 1; i >= 0; --i) { + for (auto i = static_cast(priority - 1); i >= 0; --i) { if (queues[i].next_nonempty != UnlinkedTag()) { cur->next_nonempty = queues[i].next_nonempty; queues[i].next_nonempty = cur; diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 9760be4e4..f910e438f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -630,8 +630,9 @@ else() -Werror=implicit-fallthrough -Werror=reorder -Werror=sign-compare - -Werror=unused-but-set-parameter - -Werror=unused-but-set-variable + -Werror=sign-conversion + $<$:-Werror=unused-but-set-parameter> + $<$:-Werror=unused-but-set-variable> -Werror=unused-variable ) endif() diff --git a/src/core/arm/arm_interface.cpp b/src/core/arm/arm_interface.cpp index d2295ed90..adc6aa5c5 100644 --- a/src/core/arm/arm_interface.cpp +++ b/src/core/arm/arm_interface.cpp @@ -147,10 +147,18 @@ std::vector ARM_Interface::GetBacktraceFromContex auto fp = ctx.cpu_registers[29]; auto lr = ctx.cpu_registers[30]; while (true) { - out.push_back({"", 0, lr, 0}); - if (!fp) { + out.push_back({ + .module = "", + .address = 0, + .original_address = lr, + .offset = 0, + .name = "", + }); + + if (fp == 0) { break; } + lr = memory.Read64(fp + 8) - 4; fp = memory.Read64(fp); } @@ -203,10 +211,18 @@ std::vector ARM_Interface::GetBacktrace() const { auto fp = GetReg(29); auto lr = GetReg(30); while (true) { - out.push_back({"", 0, lr, 0, ""}); - if (!fp) { + out.push_back({ + .module = "", + .address = 0, + .original_address = lr, + .offset = 0, + .name = "", + }); + + if (fp == 0) { break; } + lr = memory.Read64(fp + 8) - 4; fp = memory.Read64(fp); } diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h index 1f24051e4..9b86247e2 100644 --- a/src/core/arm/arm_interface.h +++ b/src/core/arm/arm_interface.h @@ -93,14 +93,14 @@ public: * @param index Register index * @return Returns the value in the register */ - virtual u64 GetReg(int index) const = 0; + virtual u64 GetReg(std::size_t index) const = 0; /** * Set an ARM register * @param index Register index * @param value Value to set register to */ - virtual void SetReg(int index, u64 value) = 0; + virtual void SetReg(std::size_t index, u64 value) = 0; /** * Gets the value of a specified vector register. @@ -108,7 +108,7 @@ public: * @param index The index of the vector register. * @return the value within the vector register. */ - virtual u128 GetVectorReg(int index) const = 0; + virtual u128 GetVectorReg(std::size_t index) const = 0; /** * Sets a given value into a vector register. @@ -116,7 +116,7 @@ public: * @param index The index of the vector register. * @param value The new value to place in the register. */ - virtual void SetVectorReg(int index, u128 value) = 0; + virtual void SetVectorReg(std::size_t index, u128 value) = 0; /** * Get the current PSTATE register diff --git a/src/core/arm/cpu_interrupt_handler.h b/src/core/arm/cpu_interrupt_handler.h index 71e582f79..c20c280f1 100644 --- a/src/core/arm/cpu_interrupt_handler.h +++ b/src/core/arm/cpu_interrupt_handler.h @@ -21,8 +21,8 @@ public: CPUInterruptHandler(const CPUInterruptHandler&) = delete; CPUInterruptHandler& operator=(const CPUInterruptHandler&) = delete; - CPUInterruptHandler(CPUInterruptHandler&&) = default; - CPUInterruptHandler& operator=(CPUInterruptHandler&&) = default; + CPUInterruptHandler(CPUInterruptHandler&&) = delete; + CPUInterruptHandler& operator=(CPUInterruptHandler&&) = delete; bool IsInterrupted() const { return is_interrupted; diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.cpp b/src/core/arm/dynarmic/arm_dynarmic_32.cpp index b5f28a86e..fab694fc2 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_32.cpp @@ -111,7 +111,7 @@ public: } return 0U; } - return std::max(parent.system.CoreTiming().GetDowncount(), 0); + return static_cast(std::max(parent.system.CoreTiming().GetDowncount(), 0)); } ARM_Dynarmic_32& parent; @@ -210,19 +210,19 @@ u64 ARM_Dynarmic_32::GetPC() const { return jit->Regs()[15]; } -u64 ARM_Dynarmic_32::GetReg(int index) const { +u64 ARM_Dynarmic_32::GetReg(std::size_t index) const { return jit->Regs()[index]; } -void ARM_Dynarmic_32::SetReg(int index, u64 value) { +void ARM_Dynarmic_32::SetReg(std::size_t index, u64 value) { jit->Regs()[index] = static_cast(value); } -u128 ARM_Dynarmic_32::GetVectorReg(int index) const { +u128 ARM_Dynarmic_32::GetVectorReg(std::size_t index) const { return {}; } -void ARM_Dynarmic_32::SetVectorReg(int index, u128 value) {} +void ARM_Dynarmic_32::SetVectorReg(std::size_t index, u128 value) {} u32 ARM_Dynarmic_32::GetPSTATE() const { return jit->Cpsr(); diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.h b/src/core/arm/dynarmic/arm_dynarmic_32.h index 2bab31b92..ba646c623 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.h +++ b/src/core/arm/dynarmic/arm_dynarmic_32.h @@ -35,10 +35,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(int index) const override; - void SetReg(int index, u64 value) override; - u128 GetVectorReg(int index) const override; - void SetVectorReg(int index, u128 value) override; + u64 GetReg(std::size_t index) const override; + void SetReg(std::size_t index, u64 value) override; + u128 GetVectorReg(std::size_t index) const override; + void SetVectorReg(std::size_t index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; void Run() override; diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.cpp b/src/core/arm/dynarmic/arm_dynarmic_64.cpp index ce9968724..a2c4c2f30 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_64.cpp @@ -148,7 +148,7 @@ public: } return 0U; } - return std::max(parent.system.CoreTiming().GetDowncount(), 0); + return static_cast(std::max(parent.system.CoreTiming().GetDowncount(), 0)); } u64 GetCNTPCT() override { @@ -265,19 +265,19 @@ u64 ARM_Dynarmic_64::GetPC() const { return jit->GetPC(); } -u64 ARM_Dynarmic_64::GetReg(int index) const { +u64 ARM_Dynarmic_64::GetReg(std::size_t index) const { return jit->GetRegister(index); } -void ARM_Dynarmic_64::SetReg(int index, u64 value) { +void ARM_Dynarmic_64::SetReg(std::size_t index, u64 value) { jit->SetRegister(index, value); } -u128 ARM_Dynarmic_64::GetVectorReg(int index) const { +u128 ARM_Dynarmic_64::GetVectorReg(std::size_t index) const { return jit->GetVector(index); } -void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) { +void ARM_Dynarmic_64::SetVectorReg(std::size_t index, u128 value) { jit->SetVector(index, value); } diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.h b/src/core/arm/dynarmic/arm_dynarmic_64.h index 403c55961..2afb7e7a4 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.h +++ b/src/core/arm/dynarmic/arm_dynarmic_64.h @@ -33,10 +33,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(int index) const override; - void SetReg(int index, u64 value) override; - u128 GetVectorReg(int index) const override; - void SetVectorReg(int index, u128 value) override; + u64 GetReg(std::size_t index) const override; + void SetReg(std::size_t index, u64 value) override; + u128 GetVectorReg(std::size_t index) const override; + void SetVectorReg(std::size_t index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; void Run() override; diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp index 1df3f3ed1..c1612d626 100644 --- a/src/core/arm/unicorn/arm_unicorn.cpp +++ b/src/core/arm/unicorn/arm_unicorn.cpp @@ -96,35 +96,35 @@ u64 ARM_Unicorn::GetPC() const { return val; } -u64 ARM_Unicorn::GetReg(int regn) const { +u64 ARM_Unicorn::GetReg(std::size_t index) const { u64 val{}; auto treg = UC_ARM64_REG_SP; - if (regn <= 28) { - treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); - } else if (regn < 31) { - treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); + if (index <= 28) { + treg = static_cast(UC_ARM64_REG_X0 + static_cast(index)); + } else if (index < 31) { + treg = static_cast(UC_ARM64_REG_X29 + static_cast(index) - 29); } CHECKED(uc_reg_read(uc, treg, &val)); return val; } -void ARM_Unicorn::SetReg(int regn, u64 val) { +void ARM_Unicorn::SetReg(std::size_t index, u64 value) { auto treg = UC_ARM64_REG_SP; - if (regn <= 28) { - treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); - } else if (regn < 31) { - treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); + if (index <= 28) { + treg = static_cast(UC_ARM64_REG_X0 + static_cast(index)); + } else if (index < 31) { + treg = static_cast(UC_ARM64_REG_X29 + static_cast(index) - 29); } - CHECKED(uc_reg_write(uc, treg, &val)); + CHECKED(uc_reg_write(uc, treg, &value)); } -u128 ARM_Unicorn::GetVectorReg(int /*index*/) const { +u128 ARM_Unicorn::GetVectorReg(std::size_t /*index*/) const { UNIMPLEMENTED(); static constexpr u128 res{}; return res; } -void ARM_Unicorn::SetVectorReg(int /*index*/, u128 /*value*/) { +void ARM_Unicorn::SetVectorReg(std::size_t /*index*/, u128 /*value*/) { UNIMPLEMENTED(); } @@ -217,8 +217,8 @@ void ARM_Unicorn::SaveContext(ThreadContext64& ctx) { CHECKED(uc_reg_read(uc, UC_ARM64_REG_PC, &ctx.pc)); CHECKED(uc_reg_read(uc, UC_ARM64_REG_NZCV, &ctx.pstate)); - for (auto i = 0; i < 29; ++i) { - uregs[i] = UC_ARM64_REG_X0 + i; + for (std::size_t i = 0; i < 29; ++i) { + uregs[i] = UC_ARM64_REG_X0 + static_cast(i); tregs[i] = &ctx.cpu_registers[i]; } uregs[29] = UC_ARM64_REG_X29; @@ -228,8 +228,8 @@ void ARM_Unicorn::SaveContext(ThreadContext64& ctx) { CHECKED(uc_reg_read_batch(uc, uregs, tregs, 31)); - for (int i = 0; i < 32; ++i) { - uregs[i] = UC_ARM64_REG_Q0 + i; + for (std::size_t i = 0; i < 32; ++i) { + uregs[i] = UC_ARM64_REG_Q0 + static_cast(i); tregs[i] = &ctx.vector_registers[i]; } @@ -244,8 +244,8 @@ void ARM_Unicorn::LoadContext(const ThreadContext64& ctx) { CHECKED(uc_reg_write(uc, UC_ARM64_REG_PC, &ctx.pc)); CHECKED(uc_reg_write(uc, UC_ARM64_REG_NZCV, &ctx.pstate)); - for (int i = 0; i < 29; ++i) { - uregs[i] = UC_ARM64_REG_X0 + i; + for (std::size_t i = 0; i < 29; ++i) { + uregs[i] = UC_ARM64_REG_X0 + static_cast(i); tregs[i] = (void*)&ctx.cpu_registers[i]; } uregs[29] = UC_ARM64_REG_X29; @@ -255,8 +255,8 @@ void ARM_Unicorn::LoadContext(const ThreadContext64& ctx) { CHECKED(uc_reg_write_batch(uc, uregs, tregs, 31)); - for (auto i = 0; i < 32; ++i) { - uregs[i] = UC_ARM64_REG_Q0 + i; + for (std::size_t i = 0; i < 32; ++i) { + uregs[i] = UC_ARM64_REG_Q0 + static_cast(i); tregs[i] = (void*)&ctx.vector_registers[i]; } diff --git a/src/core/arm/unicorn/arm_unicorn.h b/src/core/arm/unicorn/arm_unicorn.h index 810aff311..1183e9541 100644 --- a/src/core/arm/unicorn/arm_unicorn.h +++ b/src/core/arm/unicorn/arm_unicorn.h @@ -26,10 +26,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(int index) const override; - void SetReg(int index, u64 value) override; - u128 GetVectorReg(int index) const override; - void SetVectorReg(int index, u128 value) override; + u64 GetReg(std::size_t index) const override; + void SetReg(std::size_t index, u64 value) override; + u128 GetVectorReg(std::size_t index) const override; + void SetVectorReg(std::size_t index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; VAddr GetTlsAddress() const override; diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index e6c8461a5..9b01f6293 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -140,7 +140,8 @@ void CoreTiming::AddTicks(u64 ticks) { void CoreTiming::Idle() { if (!event_queue.empty()) { const u64 next_event_time = event_queue.front().time; - const u64 next_ticks = nsToCycles(std::chrono::nanoseconds(next_event_time)) + 10U; + const u64 next_ticks = + static_cast(nsToCycles(std::chrono::nanoseconds(next_event_time))) + 10; if (next_ticks > ticks) { ticks = next_ticks; } @@ -187,7 +188,7 @@ void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { std::optional CoreTiming::Advance() { std::scoped_lock lock{advance_lock, basic_lock}; - global_timer = GetGlobalTimeNs().count(); + global_timer = static_cast(GetGlobalTimeNs().count()); while (!event_queue.empty() && event_queue.front().time <= global_timer) { Event evt = std::move(event_queue.front()); @@ -201,11 +202,11 @@ std::optional CoreTiming::Advance() { } basic_lock.lock(); - global_timer = GetGlobalTimeNs().count(); + global_timer = static_cast(GetGlobalTimeNs().count()); } if (!event_queue.empty()) { - const s64 next_time = event_queue.front().time - global_timer; + const auto next_time = static_cast(event_queue.front().time - global_timer); return next_time; } else { return std::nullopt; @@ -240,14 +241,14 @@ std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { if (is_multicore) { return clock->GetTimeNS(); } - return CyclesToNs(ticks); + return CyclesToNs(static_cast(ticks)); } std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { if (is_multicore) { return clock->GetTimeUS(); } - return CyclesToUs(ticks); + return CyclesToUs(static_cast(ticks)); } } // namespace Core::Timing diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index 8ce8e602e..5cd450714 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -21,9 +21,9 @@ s64 msToCycles(std::chrono::milliseconds ms) { } if (static_cast(ms.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return Hardware::BASE_CLOCK_RATE * (ms.count() / 1000); + return static_cast(Hardware::BASE_CLOCK_RATE * static_cast(ms.count() / 1000)); } - return (Hardware::BASE_CLOCK_RATE * ms.count()) / 1000; + return static_cast((Hardware::BASE_CLOCK_RATE * static_cast(ms.count())) / 1000); } s64 usToCycles(std::chrono::microseconds us) { @@ -33,51 +33,55 @@ s64 usToCycles(std::chrono::microseconds us) { } if (static_cast(us.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return Hardware::BASE_CLOCK_RATE * (us.count() / 1000000); + return static_cast(Hardware::BASE_CLOCK_RATE * static_cast(us.count() / 1000000)); } - return (Hardware::BASE_CLOCK_RATE * us.count()) / 1000000; + return static_cast((Hardware::BASE_CLOCK_RATE * static_cast(us.count())) / 1000000); } s64 nsToCycles(std::chrono::nanoseconds ns) { - const u128 temporal = Common::Multiply64Into128(ns.count(), Hardware::BASE_CLOCK_RATE); - return Common::Divide128On32(temporal, static_cast(1000000000)).first; + const u128 temp = + Common::Multiply64Into128(static_cast(ns.count()), Hardware::BASE_CLOCK_RATE); + return static_cast(Common::Divide128On32(temp, static_cast(1000000000)).first); } -u64 msToClockCycles(std::chrono::milliseconds ns) { - const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); +u64 msToClockCycles(std::chrono::milliseconds ms) { + const auto count = static_cast(ms.count()); + const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000).first; } -u64 usToClockCycles(std::chrono::microseconds ns) { - const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); +u64 usToClockCycles(std::chrono::microseconds us) { + const auto count = static_cast(us.count()); + const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000000).first; } u64 nsToClockCycles(std::chrono::nanoseconds ns) { - const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); + const auto count = static_cast(ns.count()); + const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000000000).first; } u64 CpuCyclesToClockCycles(u64 ticks) { - const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); - return Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temp = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); + return Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; } std::chrono::milliseconds CyclesToMs(s64 cycles) { - const u128 temporal = Common::Multiply64Into128(cycles, 1000); - u64 ms = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000); + const u64 ms = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::milliseconds(ms); } std::chrono::nanoseconds CyclesToNs(s64 cycles) { - const u128 temporal = Common::Multiply64Into128(cycles, 1000000000); - u64 ns = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000000000); + const u64 ns = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::nanoseconds(ns); } std::chrono::microseconds CyclesToUs(s64 cycles) { - const u128 temporal = Common::Multiply64Into128(cycles, 1000000); - u64 us = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000000); + const u64 us = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::microseconds(us); } diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index e4a046bf9..3be55e267 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -12,8 +12,8 @@ namespace Core::Timing { s64 msToCycles(std::chrono::milliseconds ms); s64 usToCycles(std::chrono::microseconds us); s64 nsToCycles(std::chrono::nanoseconds ns); -u64 msToClockCycles(std::chrono::milliseconds ns); -u64 usToClockCycles(std::chrono::microseconds ns); +u64 msToClockCycles(std::chrono::milliseconds ms); +u64 usToClockCycles(std::chrono::microseconds us); u64 nsToClockCycles(std::chrono::nanoseconds ns); std::chrono::milliseconds CyclesToMs(s64 cycles); std::chrono::nanoseconds CyclesToNs(s64 cycles); diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index da15f764a..1f0d3170b 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -143,6 +143,7 @@ u64 GetSignatureTypeDataSize(SignatureType type) { return 0x3C; } UNREACHABLE(); + return 0; } u64 GetSignatureTypePaddingSize(SignatureType type) { @@ -157,6 +158,7 @@ u64 GetSignatureTypePaddingSize(SignatureType type) { return 0x40; } UNREACHABLE(); + return 0; } SignatureType Ticket::GetSignatureType() const { @@ -171,6 +173,7 @@ SignatureType Ticket::GetSignatureType() const { } UNREACHABLE(); + return {}; } TicketData& Ticket::GetData() { @@ -348,7 +351,7 @@ std::optional DeriveSDSeed() { std::array buffer{}; std::size_t offset = 0; for (; offset + 0x10 < save_43.GetSize(); ++offset) { - if (!save_43.Seek(offset, SEEK_SET)) { + if (!save_43.Seek(static_cast(offset), SEEK_SET)) { return std::nullopt; } @@ -358,7 +361,7 @@ std::optional DeriveSDSeed() { } } - if (!save_43.Seek(offset + 0x10, SEEK_SET)) { + if (!save_43.Seek(static_cast(offset + 0x10), SEEK_SET)) { return std::nullopt; } diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp index 5f1c86a09..db54f71f4 100644 --- a/src/core/crypto/partition_data_manager.cpp +++ b/src/core/crypto/partition_data_manager.cpp @@ -161,7 +161,7 @@ static constexpr u8 CalculateMaxKeyblobSourceHash() { return true; }; - for (s8 i = 0x1F; i >= 0; --i) { + for (std::size_t i = 0x1F; i <= 0x1F; --i) { if (!is_zero(keyblob_source_hashes[i])) { return static_cast(i + 1); } diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index 76af47ff9..0917f6ebf 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp @@ -201,9 +201,9 @@ bool NCA::HandlePotentialHeaderDecryption() { } std::vector NCA::ReadSectionHeaders() const { - const std::ptrdiff_t number_sections = + const auto number_sections = static_cast( std::count_if(std::begin(header.section_tables), std::end(header.section_tables), - [](NCASectionTableEntry entry) { return entry.media_offset > 0; }); + [](NCASectionTableEntry entry) { return entry.media_offset > 0; })); std::vector sections(number_sections); const auto length_sections = SECTION_HEADER_SIZE * number_sections; diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp index c52fafb6f..b2d38f01e 100644 --- a/src/core/file_sys/fsmitm_romfsbuild.cpp +++ b/src/core/file_sys/fsmitm_romfsbuild.cpp @@ -103,7 +103,7 @@ static u32 romfs_calc_path_hash(u32 parent, std::string_view path, u32 start, u32 hash = parent ^ 123456789; for (u32 i = 0; i < path_len; i++) { hash = (hash >> 5) | (hash << 27); - hash ^= path[start + i]; + hash ^= static_cast(path[start + i]); } return hash; diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index a6101f1c0..91dc69373 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -66,12 +66,14 @@ static bool IsEOF(IPSFileType type, const std::vector& data) { } VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { - if (in == nullptr || ips == nullptr) + if (in == nullptr || ips == nullptr) { return nullptr; + } const auto type = IdentifyMagic(ips->ReadBytes(0x5)); - if (type == IPSFileType::Error) + if (type == IPSFileType::Error) { return nullptr; + } auto in_data = in->ReadAllBytes(); @@ -84,37 +86,46 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { } u32 real_offset{}; - if (type == IPSFileType::IPS32) - real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3]; - else - real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2]; + if (type == IPSFileType::IPS32) { + real_offset = static_cast(temp[0] << 24) | static_cast(temp[1] << 16) | + static_cast(temp[2] << 8) | temp[3]; + } else { + real_offset = + static_cast(temp[0] << 16) | static_cast(temp[1] << 8) | temp[2]; + } u16 data_size{}; - if (ips->ReadObject(&data_size, offset) != sizeof(u16)) + if (ips->ReadObject(&data_size, offset) != sizeof(u16)) { return nullptr; + } data_size = Common::swap16(data_size); offset += sizeof(u16); if (data_size == 0) { // RLE u16 rle_size{}; - if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) + if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) { return nullptr; + } rle_size = Common::swap16(rle_size); offset += sizeof(u16); const auto data = ips->ReadByte(offset++); - if (!data) + if (!data) { return nullptr; + } - if (real_offset + rle_size > in_data.size()) + if (real_offset + rle_size > in_data.size()) { rle_size = static_cast(in_data.size() - real_offset); + } std::memset(in_data.data() + real_offset, *data, rle_size); } else { // Standard Patch auto read = data_size; - if (real_offset + read > in_data.size()) + if (real_offset + read > in_data.size()) { read = static_cast(in_data.size() - real_offset); - if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) + } + if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) { return nullptr; + } offset += data_size; } } @@ -182,14 +193,16 @@ void IPSwitchCompiler::ParseFlag(const std::string& line) { void IPSwitchCompiler::Parse() { const auto bytes = patch_text->ReadAllBytes(); std::stringstream s; - s.write(reinterpret_cast(bytes.data()), bytes.size()); + s.write(reinterpret_cast(bytes.data()), + static_cast(bytes.size())); std::vector lines; std::string stream_line; while (std::getline(s, stream_line)) { // Remove a trailing \r - if (!stream_line.empty() && stream_line.back() == '\r') + if (!stream_line.empty() && stream_line.back() == '\r') { stream_line.pop_back(); + } lines.push_back(std::move(stream_line)); } diff --git a/src/core/file_sys/kernel_executable.cpp b/src/core/file_sys/kernel_executable.cpp index ef93ef3ed..fa758b777 100644 --- a/src/core/file_sys/kernel_executable.cpp +++ b/src/core/file_sys/kernel_executable.cpp @@ -36,14 +36,14 @@ bool DecompressBLZ(std::vector& data) { while (out_index > 0) { --index; auto control = data[index + start_offset]; - for (size_t i = 0; i < 8; ++i) { + for (std::size_t i = 0; i < 8; ++i) { if (((control << i) & 0x80) > 0) { if (index < 2) { return false; } index -= 2; - std::size_t segment_offset = - data[index + start_offset] | data[index + start_offset + 1] << 8; + std::size_t segment_offset = static_cast(data[index + start_offset]) | + static_cast(data[index + start_offset + 1] << 8); std::size_t segment_size = ((segment_offset >> 12) & 0xF) + 3; segment_offset &= 0xFFF; segment_offset += 3; diff --git a/src/core/file_sys/nca_patch.cpp b/src/core/file_sys/nca_patch.cpp index 5990a2fd5..6d3472447 100644 --- a/src/core/file_sys/nca_patch.cpp +++ b/src/core/file_sys/nca_patch.cpp @@ -25,9 +25,9 @@ std::pair SearchBucketEntry(u64 offset, const BlockTyp ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block."); } - std::size_t bucket_id = std::count_if( + const auto bucket_id = static_cast(std::count_if( block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets, - [&offset](u64 base_offset) { return base_offset <= offset; }); + [&offset](u64 base_offset) { return base_offset <= offset; })); const auto& bucket = buckets[bucket_id]; @@ -53,6 +53,7 @@ std::pair SearchBucketEntry(u64 offset, const BlockTyp } UNREACHABLE_MSG("Offset could not be found in BKTR block."); + return {}; } } // Anonymous namespace @@ -136,7 +137,7 @@ std::size_t BKTR::Read(u8* data, std::size_t length, std::size_t offset) const { const auto block_offset = section_offset & 0xF; if (block_offset != 0) { - auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF); + auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xFU); cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt); if (length + block_offset < 0x10) { std::memcpy(data, block.data() + block_offset, std::min(length, block.size())); diff --git a/src/core/frontend/applets/controller.cpp b/src/core/frontend/applets/controller.cpp index c5d65f2d0..fdc97d692 100644 --- a/src/core/frontend/applets/controller.cpp +++ b/src/core/frontend/applets/controller.cpp @@ -30,7 +30,7 @@ void DefaultControllerApplet::ReconfigureControllers(std::function callb auto& players = Settings::values.players; const std::size_t min_supported_players = - parameters.enable_single_mode ? 1 : parameters.min_players; + parameters.enable_single_mode ? 1 : static_cast(parameters.min_players); // Disconnect Handheld first. npad.DisconnectNPadAtIndex(8); diff --git a/src/core/frontend/applets/profile_select.cpp b/src/core/frontend/applets/profile_select.cpp index 4df3574d2..a17420823 100644 --- a/src/core/frontend/applets/profile_select.cpp +++ b/src/core/frontend/applets/profile_select.cpp @@ -12,8 +12,9 @@ ProfileSelectApplet::~ProfileSelectApplet() = default; void DefaultProfileSelectApplet::SelectProfile( std::function)> callback) const { + const auto user_index = static_cast(Settings::values.current_user); Service::Account::ProfileManager manager; - callback(manager.GetUser(Settings::values.current_user).value_or(Common::UUID{})); + callback(manager.GetUser(user_index).value_or(Common::UUID{})); LOG_INFO(Service_ACC, "called, selecting current user instead of prompting..."); } diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp index 97ee65464..28a8a0f49 100644 --- a/src/core/gdbstub/gdbstub.cpp +++ b/src/core/gdbstub/gdbstub.cpp @@ -205,7 +205,7 @@ static Kernel::Thread* FindThreadById(s64 id) { const auto& threads = Core::System::GetInstance().GlobalScheduler().GetThreadList(); for (auto& thread : threads) { if (thread->GetThreadID() == static_cast(id)) { - current_core = thread->GetProcessorID(); + current_core = static_cast(thread->GetProcessorID()); return thread.get(); } } @@ -457,7 +457,14 @@ static u128 GdbHexToU128(const u8* src) { /// Read a byte from the gdb client. static u8 ReadByte() { u8 c; - std::size_t received_size = recv(gdbserver_socket, reinterpret_cast(&c), 1, MSG_WAITALL); + +#ifdef WIN32 + const auto socket_id = static_cast(gdbserver_socket); +#else + const auto socket_id = gdbserver_socket; +#endif + + const auto received_size = recv(socket_id, reinterpret_cast(&c), 1, MSG_WAITALL); if (received_size != 1) { LOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size); Shutdown(); @@ -574,7 +581,13 @@ bool CheckBreakpoint(VAddr addr, BreakpointType type) { * @param packet Packet to be sent to client. */ static void SendPacket(const char packet) { - std::size_t sent_size = send(gdbserver_socket, &packet, 1, 0); +#ifdef WIN32 + const auto socket_id = static_cast(gdbserver_socket); +#else + const auto socket_id = gdbserver_socket; +#endif + + const auto sent_size = send(socket_id, &packet, 1, 0); if (sent_size != 1) { LOG_ERROR(Debug_GDBStub, "send failed"); } @@ -611,7 +624,13 @@ static void SendReply(const char* reply) { u8* ptr = command_buffer; u32 left = command_length + 4; while (left > 0) { - const auto sent_size = send(gdbserver_socket, reinterpret_cast(ptr), left, 0); +#ifdef WIN32 + const auto socket_id = static_cast(gdbserver_socket); +#else + const auto socket_id = gdbserver_socket; +#endif + const auto sent_size = + send(socket_id, reinterpret_cast(ptr), static_cast(left), 0); if (sent_size < 0) { LOG_ERROR(Debug_GDBStub, "gdb: send failed"); return Shutdown(); @@ -1294,8 +1313,13 @@ static void Init(u16 port) { WSAStartup(MAKEWORD(2, 2), &InitData); #endif - int tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); - if (tmpsock == -1) { +#ifdef WIN32 + using socket_type = SOCKET; +#else + using socket_type = int; +#endif + const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); + if (tmpsock == static_cast(-1)) { LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket"); } @@ -1335,7 +1359,7 @@ static void Init(u16 port) { } // Clean up temporary socket if it's still alive at this point. - if (tmpsock != -1) { + if (tmpsock != static_cast(-1)) { shutdown(tmpsock, SHUT_RDWR); } } @@ -1352,7 +1376,12 @@ void Shutdown() { LOG_INFO(Debug_GDBStub, "Stopping GDB ..."); if (gdbserver_socket != -1) { - shutdown(gdbserver_socket, SHUT_RDWR); +#ifdef WIN32 + const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); +#else + const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); +#endif + shutdown(tmpsock, SHUT_RDWR); gdbserver_socket = -1; } @@ -1383,7 +1412,7 @@ void SetCpuStepFlag(bool is_step) { step_loop = is_step; } -void SendTrap(Kernel::Thread* thread, int trap) { +void SendTrap(Kernel::Thread* thread, u32 trap) { if (!send_trap) { return; } diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h index 8fe3c320b..23d80f367 100644 --- a/src/core/gdbstub/gdbstub.h +++ b/src/core/gdbstub/gdbstub.h @@ -110,5 +110,5 @@ void SetCpuStepFlag(bool is_step); * @param thread Sending thread. * @param trap Trap no. */ -void SendTrap(Kernel::Thread* thread, int trap); +void SendTrap(Kernel::Thread* thread, u32 trap); } // namespace GDBStub diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index 1c354037d..fcb86c822 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -233,7 +233,7 @@ void ResponseBuilder::PushRaw(const T& value) { static_assert(std::is_trivially_copyable_v, "It's undefined behavior to use memcpy with non-trivially copyable objects"); std::memcpy(cmdbuf + index, &value, sizeof(T)); - index += (sizeof(T) + 3) / 4; // round up to word length + index += static_cast((sizeof(T) + 3) / 4); // round up to word length } template <> @@ -390,7 +390,7 @@ void RequestParser::PopRaw(T& value) { static_assert(std::is_trivially_copyable_v, "It's undefined behavior to use memcpy with non-trivially copyable objects"); std::memcpy(&value, cmdbuf + index, sizeof(T)); - index += (sizeof(T) + 3) / 4; // round up to word length + index += static_cast((sizeof(T) + 3) / 4); // round up to word length } template diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index b882eaa0f..b6ebc5329 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -108,7 +108,7 @@ ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr a auto& monitor = system.Monitor(); s32 updated_value; do { - updated_value = monitor.ExclusiveRead32(current_core, address); + updated_value = static_cast(monitor.ExclusiveRead32(current_core, address)); if (updated_value != value) { return ERR_INVALID_STATE; @@ -129,7 +129,7 @@ ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr a updated_value = value; } } - } while (!monitor.ExclusiveWrite32(current_core, address, updated_value)); + } while (!monitor.ExclusiveWrite32(current_core, address, static_cast(updated_value))); WakeThreads(waiting_threads, num_to_wake); return RESULT_SUCCESS; diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index 3e745c18b..fe4988f84 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -68,7 +68,7 @@ ResultVal HandleTable::Create(std::shared_ptr obj) { generations[slot] = generation; objects[slot] = std::move(obj); - Handle handle = generation | (slot << 15); + const auto handle = static_cast(generation | static_cast(slot << 15)); return MakeResult(handle); } diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 81f85643b..0a2de4270 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -58,7 +58,7 @@ std::shared_ptr HLERequestContext::SleepClientThread( { Handle event_handle = InvalidHandle; - SchedulerLockAndSleep lock(kernel, event_handle, thread.get(), timeout); + SchedulerLockAndSleep lock(kernel, event_handle, thread.get(), static_cast(timeout)); thread->SetHLECallback( [context = *this, callback](std::shared_ptr thread) mutable -> bool { ThreadWakeupReason reason = thread->GetSignalingResult() == RESULT_TIMEOUT diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index f2b0fe2fd..c04b23eff 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -168,7 +168,7 @@ struct KernelCore::Impl { const auto type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_SUSPEND); auto thread_res = - Thread::Create(system, type, std::move(name), 0, 0, 0, static_cast(i), 0, + Thread::Create(system, type, std::move(name), 0, 0, 0, static_cast(i), 0, nullptr, std::move(init_func), init_func_parameter); suspend_threads[i] = std::move(thread_res).Unwrap(); diff --git a/src/core/hle/kernel/memory/address_space_info.cpp b/src/core/hle/kernel/memory/address_space_info.cpp index e4288cab4..6cf43ba24 100644 --- a/src/core/hle/kernel/memory/address_space_info.cpp +++ b/src/core/hle/kernel/memory/address_space_info.cpp @@ -96,6 +96,7 @@ u64 AddressSpaceInfo::GetAddressSpaceStart(std::size_t width, Type type) { return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].address; } UNREACHABLE(); + return 0; } std::size_t AddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) { @@ -112,6 +113,7 @@ std::size_t AddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].size; } UNREACHABLE(); + return 0; } } // namespace Kernel::Memory diff --git a/src/core/hle/kernel/memory/memory_manager.cpp b/src/core/hle/kernel/memory/memory_manager.cpp index acf13585c..a96157c37 100644 --- a/src/core/hle/kernel/memory/memory_manager.cpp +++ b/src/core/hle/kernel/memory/memory_manager.cpp @@ -71,7 +71,7 @@ VAddr MemoryManager::AllocateContinuous(std::size_t num_pages, std::size_t align } // If we allocated more than we need, free some - const auto allocated_pages{PageHeap::GetBlockNumPages(heap_index)}; + const auto allocated_pages{PageHeap::GetBlockNumPages(static_cast(heap_index))}; if (allocated_pages > num_pages) { chosen_manager.Free(allocated_block + num_pages * PageSize, allocated_pages - num_pages); } @@ -112,7 +112,7 @@ ResultCode MemoryManager::Allocate(PageLinkedList& page_list, std::size_t num_pa // Keep allocating until we've allocated all our pages for (s32 index{heap_index}; index >= 0 && num_pages > 0; index--) { - const auto pages_per_alloc{PageHeap::GetBlockNumPages(index)}; + const auto pages_per_alloc{PageHeap::GetBlockNumPages(static_cast(index))}; while (num_pages >= pages_per_alloc) { // Allocate a block diff --git a/src/core/hle/kernel/memory/page_heap.cpp b/src/core/hle/kernel/memory/page_heap.cpp index 0ab1f7205..7890b8c1a 100644 --- a/src/core/hle/kernel/memory/page_heap.cpp +++ b/src/core/hle/kernel/memory/page_heap.cpp @@ -33,11 +33,12 @@ void PageHeap::Initialize(VAddr address, std::size_t size, std::size_t metadata_ } VAddr PageHeap::AllocateBlock(s32 index) { - const std::size_t needed_size{blocks[index].GetSize()}; + const auto u_index = static_cast(index); + const auto needed_size{blocks[u_index].GetSize()}; - for (s32 i{index}; i < static_cast(MemoryBlockPageShifts.size()); i++) { - if (const VAddr addr{blocks[i].PopBlock()}; addr) { - if (const std::size_t allocated_size{blocks[i].GetSize()}; + for (auto i = u_index; i < MemoryBlockPageShifts.size(); i++) { + if (const VAddr addr = blocks[i].PopBlock(); addr != 0) { + if (const std::size_t allocated_size = blocks[i].GetSize(); allocated_size > needed_size) { Free(addr + needed_size, (allocated_size - needed_size) / PageSize); } @@ -50,7 +51,7 @@ VAddr PageHeap::AllocateBlock(s32 index) { void PageHeap::FreeBlock(VAddr block, s32 index) { do { - block = blocks[index++].PushBlock(block); + block = blocks[static_cast(index++)].PushBlock(block); } while (block != 0); } @@ -69,7 +70,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { VAddr after_start{end}; VAddr after_end{end}; while (big_index >= 0) { - const std::size_t block_size{blocks[big_index].GetSize()}; + const std::size_t block_size{blocks[static_cast(big_index)].GetSize()}; const VAddr big_start{Common::AlignUp((start), block_size)}; const VAddr big_end{Common::AlignDown((end), block_size)}; if (big_start < big_end) { @@ -87,7 +88,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { // Free space before the big blocks for (s32 i{big_index - 1}; i >= 0; i--) { - const std::size_t block_size{blocks[i].GetSize()}; + const std::size_t block_size{blocks[static_cast(i)].GetSize()}; while (before_start + block_size <= before_end) { before_end -= block_size; FreeBlock(before_end, i); @@ -96,7 +97,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { // Free space after the big blocks for (s32 i{big_index - 1}; i >= 0; i--) { - const std::size_t block_size{blocks[i].GetSize()}; + const std::size_t block_size{blocks[static_cast(i)].GetSize()}; while (after_start + block_size <= after_end) { FreeBlock(after_start, i); after_start += block_size; diff --git a/src/core/hle/kernel/memory/page_heap.h b/src/core/hle/kernel/memory/page_heap.h index 22b0de860..92a2bce04 100644 --- a/src/core/hle/kernel/memory/page_heap.h +++ b/src/core/hle/kernel/memory/page_heap.h @@ -34,7 +34,9 @@ public: static constexpr s32 GetBlockIndex(std::size_t num_pages) { for (s32 i{static_cast(NumMemoryBlockPageShifts) - 1}; i >= 0; i--) { - if (num_pages >= (static_cast(1) << MemoryBlockPageShifts[i]) / PageSize) { + const auto shift_index = static_cast(i); + if (num_pages >= + (static_cast(1) << MemoryBlockPageShifts[shift_index]) / PageSize) { return i; } } @@ -86,7 +88,7 @@ private: // Set the bitmap pointers for (s32 depth{GetHighestDepthIndex()}; depth >= 0; depth--) { - bit_storages[depth] = storage; + bit_storages[static_cast(depth)] = storage; size = Common::AlignUp(size, 64) / 64; storage += size; } @@ -99,7 +101,7 @@ private: s32 depth{}; do { - const u64 v{bit_storages[depth][offset]}; + const u64 v{bit_storages[static_cast(depth)][offset]}; if (v == 0) { // Non-zero depth indicates that a previous level had a free block ASSERT(depth == 0); @@ -125,7 +127,7 @@ private: constexpr bool ClearRange(std::size_t offset, std::size_t count) { const s32 depth{GetHighestDepthIndex()}; const auto bit_ind{offset / 64}; - u64* bits{bit_storages[depth]}; + u64* bits{bit_storages[static_cast(depth)]}; if (count < 64) { const auto shift{offset % 64}; ASSERT(shift + count <= 64); @@ -177,11 +179,11 @@ private: const auto which{offset % 64}; const u64 mask{1ULL << which}; - u64* bit{std::addressof(bit_storages[depth][ind])}; + u64* bit{std::addressof(bit_storages[static_cast(depth)][ind])}; const u64 v{*bit}; ASSERT((v & mask) == 0); *bit = v | mask; - if (v) { + if (v != 0) { break; } offset = ind; @@ -195,12 +197,12 @@ private: const auto which{offset % 64}; const u64 mask{1ULL << which}; - u64* bit{std::addressof(bit_storages[depth][ind])}; + u64* bit{std::addressof(bit_storages[static_cast(depth)][ind])}; u64 v{*bit}; ASSERT((v & mask) != 0); v &= ~mask; *bit = v; - if (v) { + if (v != 0) { break; } offset = ind; diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index a3fadb533..4f759d078 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -414,7 +414,8 @@ ResultCode PageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { const std::size_t remaining_pages{remaining_size / PageSize}; if (process->GetResourceLimit() && - !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, remaining_size)) { + !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, + static_cast(remaining_size))) { return ERR_RESOURCE_LIMIT_EXCEEDED; } @@ -778,7 +779,8 @@ ResultVal PageTable::SetHeapSize(std::size_t size) { auto process{system.Kernel().CurrentProcess()}; if (process->GetResourceLimit() && delta != 0 && - !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, delta)) { + !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, + static_cast(delta))) { return ERR_RESOURCE_LIMIT_EXCEEDED; } diff --git a/src/core/hle/kernel/physical_core.h b/src/core/hle/kernel/physical_core.h index d7a7a951c..6cb59d0fc 100644 --- a/src/core/hle/kernel/physical_core.h +++ b/src/core/hle/kernel/physical_core.h @@ -34,7 +34,7 @@ public: PhysicalCore& operator=(const PhysicalCore&) = delete; PhysicalCore(PhysicalCore&&) = default; - PhysicalCore& operator=(PhysicalCore&&) = default; + PhysicalCore& operator=(PhysicalCore&&) = delete; void Idle(); /// Interrupt this physical core. diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index ff9d9248b..0b39f2955 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -137,9 +137,10 @@ std::shared_ptr Process::GetResourceLimit() const { } u64 Process::GetTotalPhysicalMemoryAvailable() const { - const u64 capacity{resource_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory) + - page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + - main_thread_stack_size}; + const u64 capacity{ + static_cast(resource_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory)) + + page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + + main_thread_stack_size}; if (capacity < memory_usage_capacity) { return capacity; @@ -279,12 +280,12 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, // Set initial resource limits resource_limit->SetLimitValue( ResourceType::PhysicalMemory, - kernel.MemoryManager().GetSize(Memory::MemoryManager::Pool::Application)); + static_cast(kernel.MemoryManager().GetSize(Memory::MemoryManager::Pool::Application))); resource_limit->SetLimitValue(ResourceType::Threads, 608); resource_limit->SetLimitValue(ResourceType::Events, 700); resource_limit->SetLimitValue(ResourceType::TransferMemory, 128); resource_limit->SetLimitValue(ResourceType::Sessions, 894); - ASSERT(resource_limit->Reserve(ResourceType::PhysicalMemory, code_size)); + ASSERT(resource_limit->Reserve(ResourceType::PhysicalMemory, static_cast(code_size))); // Create TLS region tls_region_address = CreateTLSRegion(); @@ -300,9 +301,9 @@ void Process::Run(s32 main_thread_priority, u64 stack_size) { ChangeStatus(ProcessStatus::Running); - SetupMainThread(system, *this, main_thread_priority, main_thread_stack_top); + SetupMainThread(system, *this, static_cast(main_thread_priority), main_thread_stack_top); resource_limit->Reserve(ResourceType::Threads, 1); - resource_limit->Reserve(ResourceType::PhysicalMemory, main_thread_stack_size); + resource_limit->Reserve(ResourceType::PhysicalMemory, static_cast(main_thread_stack_size)); } void Process::PrepareForTermination() { @@ -363,7 +364,7 @@ VAddr Process::CreateTLSRegion() { ->AllocateAndMapMemory(1, Memory::PageSize, true, start, size / Memory::PageSize, Memory::MemoryState::ThreadLocal, Memory::MemoryPermission::ReadAndWrite, tls_map_addr) - .ValueOr(0)}; + .ValueOr(0U)}; ASSERT(tls_page_addr); diff --git a/src/core/hle/kernel/resource_limit.cpp b/src/core/hle/kernel/resource_limit.cpp index 212e442f4..e94093f24 100644 --- a/src/core/hle/kernel/resource_limit.cpp +++ b/src/core/hle/kernel/resource_limit.cpp @@ -43,8 +43,8 @@ void ResourceLimit::Release(ResourceType resource, u64 amount) { void ResourceLimit::Release(ResourceType resource, u64 used_amount, u64 available_amount) { const std::size_t index{ResourceTypeToIndex(resource)}; - current[index] -= used_amount; - available[index] -= available_amount; + current[index] -= static_cast(used_amount); + available[index] -= static_cast(available_amount); } std::shared_ptr ResourceLimit::Create(KernelCore& kernel) { diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp index 6b7db5372..4a9a762f3 100644 --- a/src/core/hle/kernel/scheduler.cpp +++ b/src/core/hle/kernel/scheduler.cpp @@ -89,9 +89,11 @@ u32 GlobalScheduler::SelectThreads() { while (iter != suggested_queue[core_id].end()) { suggested = *iter; iter++; - s32 suggested_core_id = suggested->GetProcessorID(); - Thread* top_thread = - suggested_core_id >= 0 ? top_threads[suggested_core_id] : nullptr; + const s32 suggested_core_id = suggested->GetProcessorID(); + Thread* top_thread = suggested_core_id >= 0 + ? top_threads[static_cast(suggested_core_id)] + : nullptr; + if (top_thread != suggested) { if (top_thread != nullptr && top_thread->GetPriority() < THREADPRIO_MAX_CORE_MIGRATION) { @@ -102,16 +104,19 @@ u32 GlobalScheduler::SelectThreads() { TransferToCore(suggested->GetPriority(), static_cast(core_id), suggested); break; } + suggested = nullptr; migration_candidates[num_candidates++] = suggested_core_id; } + // Step 3: Select a suggested thread from another core if (suggested == nullptr) { for (std::size_t i = 0; i < num_candidates; i++) { - s32 candidate_core = migration_candidates[i]; + const auto candidate_core = static_cast(migration_candidates[i]); suggested = top_threads[candidate_core]; auto it = scheduled_queue[candidate_core].begin(); - it++; + ++it; + Thread* next = it != scheduled_queue[candidate_core].end() ? *it : nullptr; if (next != nullptr) { TransferToCore(suggested->GetPriority(), static_cast(core_id), @@ -128,7 +133,8 @@ u32 GlobalScheduler::SelectThreads() { idle_cores &= ~(1U << core_id); } - u32 cores_needing_context_switch{}; + + u32 cores_needing_context_switch = 0; for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) { Scheduler& sched = kernel.Scheduler(core); ASSERT(top_threads[core] == nullptr || @@ -186,13 +192,16 @@ bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) { for (auto& thread : suggested_queue[core_id]) { const s32 source_core = thread->GetProcessorID(); if (source_core >= 0) { - if (current_threads[source_core] != nullptr) { - if (thread == current_threads[source_core] || - current_threads[source_core]->GetPriority() < min_regular_priority) { + const auto sanitized_source_core = static_cast(source_core); + + if (current_threads[sanitized_source_core] != nullptr) { + if (thread == current_threads[sanitized_source_core] || + current_threads[sanitized_source_core]->GetPriority() < min_regular_priority) { continue; } } } + if (next_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks() || next_thread->GetPriority() < thread->GetPriority()) { if (thread->GetPriority() <= priority) { @@ -240,17 +249,25 @@ bool GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread for (std::size_t i = 0; i < current_threads.size(); i++) { current_threads[i] = scheduled_queue[i].empty() ? nullptr : scheduled_queue[i].front(); } + for (auto& thread : suggested_queue[core_id]) { const s32 source_core = thread->GetProcessorID(); - if (source_core < 0 || thread == current_threads[source_core]) { + if (source_core < 0) { + continue; + } + + const auto sanitized_source_core = static_cast(source_core); + if (thread == current_threads[sanitized_source_core]) { continue; } - if (current_threads[source_core] == nullptr || - current_threads[source_core]->GetPriority() >= min_regular_priority) { + + if (current_threads[sanitized_source_core] == nullptr || + current_threads[sanitized_source_core]->GetPriority() >= min_regular_priority) { winner = thread; } break; } + if (winner != nullptr) { if (winner != yielding_thread) { TransferToCore(winner->GetPriority(), static_cast(core_id), winner); @@ -292,17 +309,22 @@ void GlobalScheduler::PreemptThreads() { if (thread->GetPriority() != priority) { continue; } + if (source_core >= 0) { - Thread* next_thread = scheduled_queue[source_core].empty() + const auto sanitized_source_core = static_cast(source_core); + Thread* next_thread = scheduled_queue[sanitized_source_core].empty() ? nullptr - : scheduled_queue[source_core].front(); + : scheduled_queue[sanitized_source_core].front(); + if (next_thread != nullptr && next_thread->GetPriority() < 2) { break; } + if (next_thread == thread) { continue; } } + if (current_thread != nullptr && current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) { winner = thread; @@ -322,17 +344,22 @@ void GlobalScheduler::PreemptThreads() { if (thread->GetPriority() < priority) { continue; } + if (source_core >= 0) { - Thread* next_thread = scheduled_queue[source_core].empty() + const auto sanitized_source_core = static_cast(source_core); + Thread* next_thread = scheduled_queue[sanitized_source_core].empty() ? nullptr - : scheduled_queue[source_core].front(); + : scheduled_queue[sanitized_source_core].front(); + if (next_thread != nullptr && next_thread->GetPriority() < 2) { break; } + if (next_thread == thread) { continue; } } + if (current_thread != nullptr && current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) { winner = thread; @@ -352,11 +379,11 @@ void GlobalScheduler::PreemptThreads() { void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule, Core::EmuThreadHandle global_thread) { - u32 current_core = global_thread.host_handle; + const u32 current_core = global_thread.host_handle; bool must_context_switch = global_thread.guest_handle != InvalidHandle && (current_core < Core::Hardware::NUM_CPU_CORES); while (cores_pending_reschedule != 0) { - u32 core = Common::CountTrailingZeroes32(cores_pending_reschedule); + const u32 core = Common::CountTrailingZeroes32(cores_pending_reschedule); ASSERT(core < Core::Hardware::NUM_CPU_CORES); if (!must_context_switch || core != current_core) { auto& phys_core = kernel.PhysicalCore(core); @@ -366,6 +393,7 @@ void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule, } cores_pending_reschedule &= ~(1U << core); } + if (must_context_switch) { auto& core_scheduler = kernel.CurrentScheduler(); kernel.ExitSVCProfile(); @@ -803,9 +831,11 @@ void Scheduler::Initialize() { std::string name = "Idle Thread Id:" + std::to_string(core_id); std::function init_func = Core::CpuManager::GetIdleThreadStartFunc(); void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - ThreadType type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_IDLE); - auto thread_res = Thread::Create(system, type, name, 0, 64, 0, static_cast(core_id), 0, - nullptr, std::move(init_func), init_func_parameter); + const auto type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_IDLE); + auto thread_res = + Thread::Create(system, type, std::move(name), 0, 64, 0, static_cast(core_id), 0, + nullptr, std::move(init_func), init_func_parameter); + idle_thread = std::move(thread_res).Unwrap(); } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index bafd1ced7..b8623e831 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -482,7 +482,8 @@ static ResultCode WaitSynchronization(Core::System& system, Handle* index, VAddr static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u32 handles_address, s32 handle_count, u32 timeout_high, Handle* index) { const s64 nano_seconds{(static_cast(timeout_high) << 32) | static_cast(timeout_low)}; - return WaitSynchronization(system, index, handles_address, handle_count, nano_seconds); + return WaitSynchronization(system, index, handles_address, static_cast(handle_count), + nano_seconds); } /// Resumes a thread waiting on WaitSynchronization @@ -2002,7 +2003,7 @@ static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, return ERR_INVALID_HANDLE; } - *core = thread->GetIdealCore(); + *core = static_cast(thread->GetIdealCore()); *mask = thread->GetAffinityMask(); return RESULT_SUCCESS; @@ -2070,7 +2071,7 @@ static ResultCode SetThreadCoreMask(Core::System& system, Handle thread_handle, return ERR_INVALID_HANDLE; } - return thread->SetCoreAndAffinityMask(core, affinity_mask); + return thread->SetCoreAndAffinityMask(static_cast(core), affinity_mask); } static ResultCode SetThreadCoreMask32(Core::System& system, Handle thread_handle, u32 core, diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 0b6dd9df0..9284a4c84 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -11,11 +11,11 @@ namespace Kernel { -static inline u64 Param(const Core::System& system, int n) { +static inline u64 Param(const Core::System& system, std::size_t n) { return system.CurrentArmInterface().GetReg(n); } -static inline u32 Param32(const Core::System& system, int n) { +static inline u32 Param32(const Core::System& system, std::size_t n) { return static_cast(system.CurrentArmInterface().GetReg(n)); } @@ -29,7 +29,7 @@ static inline void FuncReturn(Core::System& system, u64 result) { } static inline void FuncReturn32(Core::System& system, u32 result) { - system.CurrentArmInterface().SetReg(0, (u64)result); + system.CurrentArmInterface().SetReg(0, static_cast(result)); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -386,9 +386,10 @@ template void SvcWrap32(Core::System& system) { Handle param_1 = 0; - const u32 retval = func(system, ¶m_1, Param32(system, 0), Param32(system, 1), - Param32(system, 2), Param32(system, 3), Param32(system, 4)) - .raw; + const u32 retval = + func(system, ¶m_1, Param32(system, 0), Param32(system, 1), Param32(system, 2), + Param32(system, 3), static_cast(Param32(system, 4))) + .raw; system.CurrentArmInterface().SetReg(1, param_1); FuncReturn(system, retval); @@ -542,8 +543,8 @@ void SvcWrap32(Core::System& system) { template void SvcWrap32(Core::System& system) { u32 param_1 = 0; - const u32 retval = func(system, Param32(system, 0), Param32(system, 1), Param32(system, 2), - Param32(system, 3), ¶m_1) + const u32 retval = func(system, Param32(system, 0), Param32(system, 1), + static_cast(Param32(system, 2)), Param32(system, 3), ¶m_1) .raw; system.CurrentArmInterface().SetReg(1, param_1); FuncReturn(system, retval); diff --git a/src/core/hle/kernel/synchronization.cpp b/src/core/hle/kernel/synchronization.cpp index 8b875d853..653f722b3 100644 --- a/src/core/hle/kernel/synchronization.cpp +++ b/src/core/hle/kernel/synchronization.cpp @@ -51,7 +51,7 @@ std::pair Synchronization::WaitFor( // We found a ready object, acquire it and set the result value SynchronizationObject* object = itr->get(); object->Acquire(thread); - const u32 index = static_cast(std::distance(sync_objects.begin(), itr)); + const auto index = static_cast(std::distance(sync_objects.begin(), itr)); lock.CancelSleep(); return {RESULT_SUCCESS, index}; } @@ -105,7 +105,7 @@ std::pair Synchronization::WaitFor( }); ASSERT(itr != sync_objects.end()); signaling_object->Acquire(thread); - const u32 index = static_cast(std::distance(sync_objects.begin(), itr)); + const auto index = static_cast(std::distance(sync_objects.begin(), itr)); return {signaling_result, index}; } return {signaling_result, -1}; diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index d132aba34..323e740e9 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -525,7 +525,7 @@ ResultCode Thread::SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask) { if (old_affinity_mask != new_affinity_mask) { const s32 old_core = processor_id; if (processor_id >= 0 && ((affinity_mask >> processor_id) & 1) == 0) { - if (static_cast(ideal_core) < 0) { + if (ideal_core < 0) { processor_id = HighestSetCore(affinity_mask, Core::Hardware::NUM_CPU_CORES); } else { processor_id = ideal_core; diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 8daf79fac..21b22ca45 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -470,7 +470,7 @@ public: bool InvokeHLECallback(std::shared_ptr thread); - u32 GetIdealCore() const { + s32 GetIdealCore() const { return ideal_core; } @@ -654,8 +654,8 @@ private: Scheduler* scheduler = nullptr; - u32 ideal_core{0xFFFFFFFF}; - u64 affinity_mask{0x1}; + s32 ideal_core = -1; + u64 affinity_mask = 1; s32 ideal_core_override = -1; u64 affinity_mask_override = 0x1; diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 9b829e957..9c302043a 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -41,12 +41,15 @@ constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "/system/save/8000000000000010/su/ ProfileManager::ProfileManager() { ParseUserSaveFile(); - if (user_count == 0) + if (user_count == 0) { CreateNewUser(UUID::Generate(), "yuzu"); + } - auto current = std::clamp(Settings::values.current_user, 0, MAX_USERS - 1); - if (UserExistsIndex(current)) + auto current = static_cast( + std::clamp(Settings::values.current_user, 0, static_cast(MAX_USERS - 1))); + if (UserExistsIndex(current)) { current = 0; + } OpenUser(*GetUser(current)); } @@ -189,8 +192,8 @@ std::size_t ProfileManager::GetUserCount() const { /// booting std::size_t ProfileManager::GetOpenUserCount() const { - return std::count_if(profiles.begin(), profiles.end(), - [](const ProfileInfo& p) { return p.is_open; }); + return static_cast(std::count_if(profiles.begin(), profiles.end(), + [](const ProfileInfo& p) { return p.is_open; })); } /// Checks if a user id exists in our profile manager diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index d7a81f64a..995b7e5c6 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1311,7 +1311,7 @@ void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { params.is_account_selected = 1; Account::ProfileManager profile_manager{}; - const auto uuid = profile_manager.GetUser(Settings::values.current_user); + const auto uuid = profile_manager.GetUser(static_cast(Settings::values.current_user)); ASSERT(uuid); params.current_user = uuid->uuid; diff --git a/src/core/hle/service/am/applets/controller.cpp b/src/core/hle/service/am/applets/controller.cpp index 2151da783..17788d7a5 100644 --- a/src/core/hle/service/am/applets/controller.cpp +++ b/src/core/hle/service/am/applets/controller.cpp @@ -178,23 +178,23 @@ void Controller::Execute() { } void Controller::ConfigurationComplete() { - ControllerSupportResultInfo result_info{}; - const auto& players = Settings::values.players; - // If enable_single_mode is enabled, player_count is 1 regardless of any other parameters. - // Otherwise, only count connected players from P1-P8. - result_info.player_count = - is_single_mode ? 1 - : static_cast(std::count_if( - players.begin(), players.end() - 2, - [](Settings::PlayerInput player) { return player.connected; })); + const s8 player_count = + is_single_mode + ? 1 + : static_cast(std::count_if(players.begin(), players.end() - 2, + [](const auto& player) { return player.connected; })); - result_info.selected_id = HID::Controller_NPad::IndexToNPad( - std::distance(players.begin(), - std::find_if(players.begin(), players.end(), - [](Settings::PlayerInput player) { return player.connected; }))); + const auto index = static_cast(std::distance( + players.begin(), std::find_if(players.begin(), players.end(), + [](const auto& player) { return player.connected; }))); + // If enable_single_mode is enabled, player_count is 1 regardless of any other parameters. + // Otherwise, only count connected players from P1-P8. + ControllerSupportResultInfo result_info{}; + result_info.player_count = player_count; + result_info.selected_id = HID::Controller_NPad::IndexToNPad(index); result_info.result = 0; LOG_DEBUG(Service_HID, "Result Info: player_count={}, selected_id={}, result={}", diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index 9b4910e53..a345a68e6 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -69,9 +69,10 @@ public: buffer_event = Kernel::WritableEvent::CreateEventPair(system.Kernel(), "IAudioOutBufferReleased"); - stream = audio_core.OpenStream(system.CoreTiming(), audio_params.sample_rate, - audio_params.channel_count, std::move(unique_name), - [this] { buffer_event.writable->Signal(); }); + stream = + audio_core.OpenStream(system.CoreTiming(), static_cast(audio_params.sample_rate), + audio_params.channel_count, std::move(unique_name), + [this] { buffer_event.writable->Signal(); }); } private: diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index f1d81602c..16a6deb7e 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -50,8 +50,8 @@ public: Enabled, }; - explicit OpusDecoderState(OpusDecoderPtr decoder, u32 sample_rate, u32 channel_count) - : decoder{std::move(decoder)}, sample_rate{sample_rate}, channel_count{channel_count} {} + explicit OpusDecoderState(OpusDecoderPtr decoder_, s32 sample_rate_, u32 channel_count_) + : decoder{std::move(decoder_)}, sample_rate{sample_rate_}, channel_count{channel_count_} {} // Decodes interleaved Opus packets. Optionally allows reporting time taken to // perform the decoding, as well as any relevant extra behavior. @@ -113,15 +113,16 @@ private: return false; } - const auto frame = input.data() + sizeof(OpusPacketHeader); + const auto* const frame = input.data() + sizeof(OpusPacketHeader); const auto decoded_sample_count = opus_packet_get_nb_samples( - frame, static_cast(input.size() - sizeof(OpusPacketHeader)), - static_cast(sample_rate)); - if (decoded_sample_count * channel_count * sizeof(u16) > raw_output_sz) { + frame, static_cast(input.size() - sizeof(OpusPacketHeader)), sample_rate); + const auto decoded_size = + static_cast(decoded_sample_count) * channel_count * sizeof(u16); + if (decoded_size > raw_output_sz) { LOG_ERROR( Audio, "Decoded data does not fit into the output data, decoded_sz={}, raw_output_sz={}", - decoded_sample_count * channel_count * sizeof(u16), raw_output_sz); + decoded_size, raw_output_sz); return false; } @@ -137,11 +138,11 @@ private: } const auto end_time = std::chrono::high_resolution_clock::now() - start_time; - sample_count = out_sample_count; + sample_count = static_cast(out_sample_count); consumed = static_cast(sizeof(OpusPacketHeader) + hdr.size); if (out_performance_time != nullptr) { - *out_performance_time = - std::chrono::duration_cast(end_time).count(); + *out_performance_time = static_cast( + std::chrono::duration_cast(end_time).count()); } return true; @@ -154,7 +155,7 @@ private: } OpusDecoderPtr decoder; - u32 sample_rate; + s32 sample_rate; u32 channel_count; }; @@ -212,7 +213,7 @@ std::size_t WorkerBufferSize(u32 channel_count) { ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count"); constexpr int num_streams = 1; const int num_stereo_streams = channel_count == 2 ? 1 : 0; - return opus_multistream_decoder_get_size(num_streams, num_stereo_streams); + return static_cast(opus_multistream_decoder_get_size(num_streams, num_stereo_streams)); } // Creates the mapping table that maps the input channels to the particular @@ -244,7 +245,7 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { "Invalid sample rate"); ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count"); - const u32 worker_buffer_sz = static_cast(WorkerBufferSize(channel_count)); + const auto worker_buffer_sz = static_cast(WorkerBufferSize(channel_count)); LOG_DEBUG(Audio, "worker_buffer_sz={}", worker_buffer_sz); IPC::ResponseBuilder rb{ctx, 3}; @@ -254,7 +255,7 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { void HwOpus::OpenOpusDecoder(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto sample_rate = rp.Pop(); + const auto sample_rate = rp.Pop(); const auto channel_count = rp.Pop(); const auto buffer_sz = rp.Pop(); diff --git a/src/core/hle/service/bcat/backend/backend.h b/src/core/hle/service/bcat/backend/backend.h index 48bbbe66f..1e5e93290 100644 --- a/src/core/hle/service/bcat/backend/backend.h +++ b/src/core/hle/service/bcat/backend/backend.h @@ -53,10 +53,10 @@ struct DeliveryCacheProgressImpl { ResultCode result = RESULT_SUCCESS; DirectoryName current_directory; FileName current_file; - s64 current_downloaded_bytes; ///< Bytes downloaded on current file. - s64 current_total_bytes; ///< Bytes total on current file. - s64 total_downloaded_bytes; ///< Bytes downloaded on overall download. - s64 total_bytes; ///< Bytes total on overall download. + u64 current_downloaded_bytes; ///< Bytes downloaded on current file. + u64 current_total_bytes; ///< Bytes total on current file. + u64 total_downloaded_bytes; ///< Bytes downloaded on overall download. + u64 total_bytes; ///< Bytes total on overall download. INSERT_PADDING_BYTES( 0x198); ///< Appears to be unused in official code, possibly reserved for future use. }; diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp index 589e288df..bd7ea75c2 100644 --- a/src/core/hle/service/bcat/backend/boxcat.cpp +++ b/src/core/hle/service/bcat/backend/boxcat.cpp @@ -3,7 +3,16 @@ // Refer to the license.txt file included. #include + +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-conversion" +#endif #include +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + #include #include #include "common/hex_util.h" diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index db0e06ca1..5a7e9f930 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -454,7 +454,8 @@ private: write_size = std::min(write_size, files.size()); std::vector entries(write_size); std::transform( - files.begin(), files.begin() + write_size, entries.begin(), [](const auto& file) { + files.begin(), files.begin() + static_cast(write_size), entries.begin(), + [](const auto& file) { FileName name{}; std::memcpy(name.data(), file->GetName().data(), std::min(file->GetName().size(), name.size())); diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp index 649128be4..993686f1d 100644 --- a/src/core/hle/service/filesystem/fsp_srv.cpp +++ b/src/core/hle/service/filesystem/fsp_srv.cpp @@ -94,7 +94,8 @@ private: } // Read the data from the Storage backend - std::vector output = backend->ReadBytes(length, offset); + const auto output = backend->ReadBytes(static_cast(length), static_cast(offset)); + // Write the data to memory ctx.WriteBuffer(output); @@ -151,7 +152,7 @@ private: } // Read the data from the Storage backend - std::vector output = backend->ReadBytes(length, offset); + const auto output = backend->ReadBytes(static_cast(length), static_cast(offset)); // Write the data to memory ctx.WriteBuffer(output); @@ -194,7 +195,8 @@ private: // Write the data to the Storage backend const auto write_size = static_cast(std::distance(data.begin(), data.begin() + length)); - const std::size_t written = backend->Write(data.data(), write_size, offset); + const std::size_t written = + backend->Write(data.data(), write_size, static_cast(offset)); ASSERT_MSG(static_cast(written) == length, "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length, diff --git a/src/core/hle/service/hid/controllers/debug_pad.cpp b/src/core/hle/service/hid/controllers/debug_pad.cpp index ad251ed4a..c2c1470a5 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.cpp +++ b/src/core/hle/service/hid/controllers/debug_pad.cpp @@ -23,7 +23,7 @@ void Controller_DebugPad::OnRelease() {} void Controller_DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = core_timing.GetCPUTicks(); + shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -33,9 +33,11 @@ void Controller_DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, } shared_memory.header.entry_count = 16; - const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + const auto& last_entry = + shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + auto& cur_entry = + shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index b7b7bfeae..0618b2a05 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -19,7 +19,7 @@ void Controller_Gesture::OnRelease() {} void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = core_timing.GetCPUTicks(); + shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -29,9 +29,11 @@ void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u } shared_memory.header.entry_count = 16; - const auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; + const auto& last_entry = + shared_memory.gesture_states[static_cast(shared_memory.header.last_entry_index)]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; + auto& cur_entry = + shared_memory.gesture_states[static_cast(shared_memory.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index 59b694cd4..0624be316 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp @@ -21,7 +21,7 @@ void Controller_Keyboard::OnRelease() {} void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = core_timing.GetCPUTicks(); + shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -31,9 +31,11 @@ void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, } shared_memory.header.entry_count = 16; - const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + const auto& last_entry = + shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + auto& cur_entry = + shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp index ac40989c5..10e2373bc 100644 --- a/src/core/hle/service/hid/controllers/mouse.cpp +++ b/src/core/hle/service/hid/controllers/mouse.cpp @@ -19,7 +19,7 @@ void Controller_Mouse::OnRelease() {} void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = core_timing.GetCPUTicks(); + shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -29,9 +29,11 @@ void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } shared_memory.header.entry_count = 16; - auto& last_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; + auto& last_entry = + shared_memory.mouse_states[static_cast(shared_memory.header.last_entry_index)]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; + auto& cur_entry = + shared_memory.mouse_states[static_cast(shared_memory.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index e311bc18c..2422c0190 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -341,26 +341,29 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } for (std::size_t i = 0; i < shared_memory_entries.size(); i++) { auto& npad = shared_memory_entries[i]; - const std::array controller_npads{&npad.main_controller_states, - &npad.handheld_states, - &npad.dual_states, - &npad.left_joy_states, - &npad.right_joy_states, - &npad.pokeball_states, - &npad.libnx}; + const std::array controller_npads{ + &npad.main_controller_states, + &npad.handheld_states, + &npad.dual_states, + &npad.left_joy_states, + &npad.right_joy_states, + &npad.pokeball_states, + &npad.libnx, + }; for (auto* main_controller : controller_npads) { main_controller->common.entry_count = 16; main_controller->common.total_entry_count = 17; const auto& last_entry = - main_controller->npad[main_controller->common.last_entry_index]; + main_controller->npad[static_cast(main_controller->common.last_entry_index)]; - main_controller->common.timestamp = core_timing.GetCPUTicks(); + main_controller->common.timestamp = static_cast(core_timing.GetCPUTicks()); main_controller->common.last_entry_index = (main_controller->common.last_entry_index + 1) % 17; - auto& cur_entry = main_controller->npad[main_controller->common.last_entry_index]; + auto& cur_entry = + main_controller->npad[static_cast(main_controller->common.last_entry_index)]; cur_entry.timestamp = last_entry.timestamp + 1; cur_entry.timestamp2 = cur_entry.timestamp; @@ -371,22 +374,29 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* if (controller_type == NPadControllerType::None || !connected_controllers[i].is_connected) { continue; } - const u32 npad_index = static_cast(i); + const auto npad_index = static_cast(i); RequestPadStateUpdate(npad_index); auto& pad_state = npad_pad_states[npad_index]; auto& main_controller = - npad.main_controller_states.npad[npad.main_controller_states.common.last_entry_index]; + npad.main_controller_states + .npad[static_cast(npad.main_controller_states.common.last_entry_index)]; auto& handheld_entry = - npad.handheld_states.npad[npad.handheld_states.common.last_entry_index]; - auto& dual_entry = npad.dual_states.npad[npad.dual_states.common.last_entry_index]; - auto& left_entry = npad.left_joy_states.npad[npad.left_joy_states.common.last_entry_index]; + npad.handheld_states + .npad[static_cast(npad.handheld_states.common.last_entry_index)]; + auto& dual_entry = + npad.dual_states.npad[static_cast(npad.dual_states.common.last_entry_index)]; + auto& left_entry = + npad.left_joy_states + .npad[static_cast(npad.left_joy_states.common.last_entry_index)]; auto& right_entry = - npad.right_joy_states.npad[npad.right_joy_states.common.last_entry_index]; + npad.right_joy_states + .npad[static_cast(npad.right_joy_states.common.last_entry_index)]; auto& pokeball_entry = - npad.pokeball_states.npad[npad.pokeball_states.common.last_entry_index]; - auto& libnx_entry = npad.libnx.npad[npad.libnx.common.last_entry_index]; + npad.pokeball_states + .npad[static_cast(npad.pokeball_states.common.last_entry_index)]; + auto& libnx_entry = npad.libnx.npad[static_cast(npad.libnx.common.last_entry_index)]; libnx_entry.connection_status.raw = 0; libnx_entry.connection_status.IsConnected.Assign(1); @@ -500,13 +510,14 @@ void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing sixaxis_sensor->common.total_entry_count = 17; const auto& last_entry = - sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; + sixaxis_sensor->sixaxis[static_cast(sixaxis_sensor->common.last_entry_index)]; - sixaxis_sensor->common.timestamp = core_timing.GetCPUTicks(); + sixaxis_sensor->common.timestamp = static_cast(core_timing.GetCPUTicks()); sixaxis_sensor->common.last_entry_index = (sixaxis_sensor->common.last_entry_index + 1) % 17; - auto& cur_entry = sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; + auto& cur_entry = + sixaxis_sensor->sixaxis[static_cast(sixaxis_sensor->common.last_entry_index)]; cur_entry.timestamp = last_entry.timestamp + 1; cur_entry.timestamp2 = cur_entry.timestamp; @@ -529,17 +540,21 @@ void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing } auto& full_sixaxis_entry = - npad.sixaxis_full.sixaxis[npad.sixaxis_full.common.last_entry_index]; + npad.sixaxis_full.sixaxis[static_cast(npad.sixaxis_full.common.last_entry_index)]; auto& handheld_sixaxis_entry = - npad.sixaxis_handheld.sixaxis[npad.sixaxis_handheld.common.last_entry_index]; + npad.sixaxis_handheld + .sixaxis[static_cast(npad.sixaxis_handheld.common.last_entry_index)]; auto& dual_left_sixaxis_entry = - npad.sixaxis_dual_left.sixaxis[npad.sixaxis_dual_left.common.last_entry_index]; + npad.sixaxis_dual_left + .sixaxis[static_cast(npad.sixaxis_dual_left.common.last_entry_index)]; auto& dual_right_sixaxis_entry = - npad.sixaxis_dual_right.sixaxis[npad.sixaxis_dual_right.common.last_entry_index]; + npad.sixaxis_dual_right + .sixaxis[static_cast(npad.sixaxis_dual_right.common.last_entry_index)]; auto& left_sixaxis_entry = - npad.sixaxis_left.sixaxis[npad.sixaxis_left.common.last_entry_index]; + npad.sixaxis_left.sixaxis[static_cast(npad.sixaxis_left.common.last_entry_index)]; auto& right_sixaxis_entry = - npad.sixaxis_right.sixaxis[npad.sixaxis_right.common.last_entry_index]; + npad.sixaxis_right + .sixaxis[static_cast(npad.sixaxis_right.common.last_entry_index)]; switch (controller_type) { case NPadControllerType::None: diff --git a/src/core/hle/service/hid/controllers/stubbed.cpp b/src/core/hle/service/hid/controllers/stubbed.cpp index e7483bfa2..f9cb61667 100644 --- a/src/core/hle/service/hid/controllers/stubbed.cpp +++ b/src/core/hle/service/hid/controllers/stubbed.cpp @@ -22,12 +22,12 @@ void Controller_Stubbed::OnUpdate(const Core::Timing::CoreTiming& core_timing, u return; } - CommonHeader header{}; - header.timestamp = core_timing.GetCPUTicks(); - header.total_entry_count = 17; - header.entry_count = 0; - header.last_entry_index = 0; - + const CommonHeader header{ + .timestamp = static_cast(core_timing.GetCPUTicks()), + .total_entry_count = 17, + .last_entry_index = 0, + .entry_count = 0, + }; std::memcpy(data + common_offset, &header, sizeof(CommonHeader)); } diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index 0df395e85..06f4134a2 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -22,7 +22,7 @@ void Controller_Touchscreen::OnRelease() {} void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = core_timing.GetCPUTicks(); + shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -33,9 +33,12 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin shared_memory.header.entry_count = 16; const auto& last_entry = - shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; + shared_memory + .shared_memory_entries[static_cast(shared_memory.header.last_entry_index)]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; + auto& cur_entry = + shared_memory + .shared_memory_entries[static_cast(shared_memory.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 4d9042adc..746acbd1c 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -69,6 +69,6 @@ private: TouchScreenSharedMemory shared_memory{}; std::unique_ptr touch_device; std::unique_ptr touch_btn_device; - s64_le last_touch{}; + u64_le last_touch{}; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/xpad.cpp b/src/core/hle/service/hid/controllers/xpad.cpp index 2503ef241..60417abb8 100644 --- a/src/core/hle/service/hid/controllers/xpad.cpp +++ b/src/core/hle/service/hid/controllers/xpad.cpp @@ -20,7 +20,7 @@ void Controller_XPad::OnRelease() {} void Controller_XPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { for (auto& xpad_entry : shared_memory.shared_memory_entries) { - xpad_entry.header.timestamp = core_timing.GetCPUTicks(); + xpad_entry.header.timestamp = static_cast(core_timing.GetCPUTicks()); xpad_entry.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -30,9 +30,11 @@ void Controller_XPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } xpad_entry.header.entry_count = 16; - const auto& last_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; + const auto& last_entry = + xpad_entry.pad_states[static_cast(xpad_entry.header.last_entry_index)]; xpad_entry.header.last_entry_index = (xpad_entry.header.last_entry_index + 1) % 17; - auto& cur_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; + auto& cur_entry = + xpad_entry.pad_states[static_cast(xpad_entry.header.last_entry_index)]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index d8cd10e31..9ad5bbf0d 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -23,7 +23,7 @@ namespace Service::LDR { constexpr ResultCode ERROR_INSUFFICIENT_ADDRESS_SPACE{ErrorModule::RO, 2}; -constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; +[[maybe_unused]] constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; constexpr ResultCode ERROR_INVALID_NRO{ErrorModule::Loader, 52}; constexpr ResultCode ERROR_INVALID_NRR{ErrorModule::Loader, 53}; constexpr ResultCode ERROR_MISSING_NRR_HASH{ErrorModule::Loader, 54}; @@ -33,7 +33,7 @@ constexpr ResultCode ERROR_ALREADY_LOADED{ErrorModule::Loader, 57}; constexpr ResultCode ERROR_INVALID_ALIGNMENT{ErrorModule::Loader, 81}; constexpr ResultCode ERROR_INVALID_SIZE{ErrorModule::Loader, 82}; constexpr ResultCode ERROR_INVALID_NRO_ADDRESS{ErrorModule::Loader, 84}; -constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; +[[maybe_unused]] constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; constexpr ResultCode ERROR_NOT_INITIALIZED{ErrorModule::Loader, 87}; constexpr std::size_t MAXIMUM_LOADED_RO{0x40}; diff --git a/src/core/hle/service/mii/manager.cpp b/src/core/hle/service/mii/manager.cpp index 8e433eb41..5930765bc 100644 --- a/src/core/hle/service/mii/manager.cpp +++ b/src/core/hle/service/mii/manager.cpp @@ -240,10 +240,10 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eye_type.Assign( eye_type_info.values[GetRandomValue(eye_type_info.values_count)]); - const auto eye_rotate_1{gender != Gender::Male ? 4 : 2}; - const auto eye_rotate_2{gender != Gender::Male ? 3 : 4}; - const auto eye_rotate_offset{32 - EyeRotateLookup[eye_rotate_1] + eye_rotate_2}; - const auto eye_rotate{32 - EyeRotateLookup[bf.eye_type]}; + const auto eye_rotate_1{gender != Gender::Male ? 4U : 2U}; + const auto eye_rotate_2{gender != Gender::Male ? 3U : 4U}; + const auto eye_rotate_offset{32U - EyeRotateLookup[eye_rotate_1] + eye_rotate_2}; + const auto eye_rotate{32U - EyeRotateLookup[bf.eye_type]}; bf.eye_color.Assign( EyeColorLookup[eye_color_info @@ -257,11 +257,11 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eyebrow_type.Assign( eyebrow_type_info.values[GetRandomValue(eyebrow_type_info.values_count)]); - const auto eyebrow_rotate_1{race == Race::Asian ? 6 : 0}; - const auto eyebrow_y{race == Race::Asian ? 9 : 10}; - const auto eyebrow_rotate_offset{32 - EyebrowRotateLookup[eyebrow_rotate_1] + 6}; + const auto eyebrow_rotate_1{race == Race::Asian ? 6U : 0U}; + const auto eyebrow_y{race == Race::Asian ? 9U : 10U}; + const auto eyebrow_rotate_offset{32U - EyebrowRotateLookup[eyebrow_rotate_1] + 6}; const auto eyebrow_rotate{ - 32 - EyebrowRotateLookup[static_cast(bf.eyebrow_type.Value())]}; + 32U - EyebrowRotateLookup[static_cast(bf.eyebrow_type.Value())]}; bf.eyebrow_color.Assign(bf.hair_color); bf.eyebrow_scale.Assign(4); @@ -270,14 +270,14 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eyebrow_x.Assign(2); bf.eyebrow_y.Assign(axis_y + eyebrow_y); - const auto nose_scale{gender == Gender::Female ? 3 : 4}; + const auto nose_scale{gender == Gender::Female ? 3U : 4U}; bf.nose_type.Assign( nose_type_info.values[GetRandomValue(nose_type_info.values_count)]); bf.nose_scale.Assign(nose_scale); bf.nose_y.Assign(axis_y + 9); - const auto mouth_color{gender == Gender::Female ? GetRandomValue(4) : 0}; + const auto mouth_color{gender == Gender::Female ? GetRandomValue(4) : 0U}; bf.mouth_type.Assign( mouth_type_info.values[GetRandomValue(mouth_type_info.values_count)]); diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index a0469ffbd..0dd23ec9e 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -217,7 +217,7 @@ private: const auto& amiibo = nfp_interface.GetAmiiboBuffer(); const TagInfo tag_info{ .uuid = amiibo.uuid, - .uuid_length = static_cast(tag_info.uuid.size()), + .uuid_length = static_cast(amiibo.uuid.size()), .padding_1 = {}, .protocol = 1, // TODO(ogniK): Figure out actual values .tag_type = 2, diff --git a/src/core/hle/service/ns/ns.cpp b/src/core/hle/service/ns/ns.cpp index 58ee1f712..3edee6303 100644 --- a/src/core/hle/service/ns/ns.cpp +++ b/src/core/hle/service/ns/ns.cpp @@ -368,7 +368,7 @@ ResultVal IApplicationManagerInterface::GetApplicationDesiredLanguage( // Get language code from settings const auto language_code = - Set::GetLanguageCodeFromIndex(Settings::values.language_index.GetValue()); + Set::GetLanguageCodeFromIndex(static_cast(Settings::values.language_index.GetValue())); // Convert to application language, get priority list const auto application_language = ConvertToApplicationLanguage(language_code); diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 40838a225..5ccec2637 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -50,19 +50,9 @@ constexpr std::array, 7> SHARED_FONTS{ std::make_pair(FontArchives::Extension, "nintendo_ext2_003.bfttf"), }; -constexpr std::array SHARED_FONTS_TTF{ - "FontStandard.ttf", - "FontChineseSimplified.ttf", - "FontExtendedChineseSimplified.ttf", - "FontChineseTraditional.ttf", - "FontKorean.ttf", - "FontNintendoExtended.ttf", - "FontNintendoExtended2.ttf", -}; - // The below data is specific to shared font data dumped from Switch on f/w 2.2 // Virtual address and offsets/sizes likely will vary by dump -constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; +[[maybe_unused]] constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; constexpr u32 EXPECTED_RESULT{0x7f9a0218}; // What we expect the decrypted bfttf first 4 bytes to be constexpr u32 EXPECTED_MAGIC{0x36f81a1e}; // What we expect the encrypted bfttf first 4 bytes to be constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000}; diff --git a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp index 39bd2a45b..85e921ceb 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp @@ -139,7 +139,7 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou const auto object{nvmap_dev->GetObject(params.nvmap_handle)}; if (!object) { - LOG_CRITICAL(Service_NVDRV, "invalid nvmap_handle={:X}", params.nvmap_handle); + LOG_ERROR(Service_NVDRV, "invalid nvmap_handle={:X}", params.nvmap_handle); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; } @@ -151,21 +151,24 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou auto& gpu = system.GPU(); u64 page_size{params.page_size}; - if (!page_size) { + if (page_size == 0) { page_size = object->align; } if ((params.flags & AddressSpaceFlags::Remap) != AddressSpaceFlags::None) { - if (const auto buffer_map{FindBufferMap(params.offset)}; buffer_map) { - const auto cpu_addr{static_cast(buffer_map->CpuAddr() + params.buffer_offset)}; + const auto buffer_map = FindBufferMap(static_cast(params.offset)); + + if (buffer_map) { + const auto cpu_addr{ + static_cast(buffer_map->CpuAddr() + static_cast(params.buffer_offset))}; const auto gpu_addr{static_cast(params.offset + params.buffer_offset)}; if (!gpu.MemoryManager().Map(cpu_addr, gpu_addr, params.mapping_size)) { - LOG_CRITICAL(Service_NVDRV, - "remap failed, flags={:X}, nvmap_handle={:X}, buffer_offset={}, " - "mapping_size = {}, offset={}", - params.flags, params.nvmap_handle, params.buffer_offset, - params.mapping_size, params.offset); + LOG_ERROR(Service_NVDRV, + "Remap failed, flags={:X}, nvmap_handle={:X}, buffer_offset={}, " + "mapping_size = {}, offset={}", + params.flags, params.nvmap_handle, params.buffer_offset, + params.mapping_size, params.offset); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; @@ -174,7 +177,7 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::Success; } else { - LOG_CRITICAL(Service_NVDRV, "address not mapped offset={}", params.offset); + LOG_ERROR(Service_NVDRV, "Address not mapped. offset={}", params.offset); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; @@ -184,25 +187,27 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou // We can only map objects that have already been assigned a CPU address. ASSERT(object->status == nvmap::Object::Status::Allocated); - const auto physical_address{object->addr + params.buffer_offset}; + const auto physical_address{object->addr + static_cast(params.buffer_offset)}; u64 size{params.mapping_size}; - if (!size) { + if (size == 0) { size = object->size; } const bool is_alloc{(params.flags & AddressSpaceFlags::FixedOffset) == AddressSpaceFlags::None}; if (is_alloc) { - params.offset = gpu.MemoryManager().MapAllocate(physical_address, size, page_size); + params.offset = + static_cast(gpu.MemoryManager().MapAllocate(physical_address, size, page_size)); } else { - params.offset = gpu.MemoryManager().Map(physical_address, params.offset, size); + params.offset = static_cast( + gpu.MemoryManager().Map(physical_address, static_cast(params.offset), size)); } auto result{NvErrCodes::Success}; - if (!params.offset) { - LOG_CRITICAL(Service_NVDRV, "failed to map size={}", size); + if (params.offset == 0) { + LOG_ERROR(Service_NVDRV, "Failed to map size={}", size); result = NvErrCodes::InvalidInput; } else { - AddBufferMap(params.offset, size, physical_address, is_alloc); + AddBufferMap(static_cast(params.offset), size, physical_address, is_alloc); } std::memcpy(output.data(), ¶ms, output.size()); @@ -213,12 +218,13 @@ u32 nvhost_as_gpu::UnmapBuffer(const std::vector& input, std::vector& ou IoctlUnmapBuffer params{}; std::memcpy(¶ms, input.data(), input.size()); - LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", params.offset); + const auto offset = static_cast(params.offset); + LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", offset); - if (const auto size{RemoveBufferMap(params.offset)}; size) { - system.GPU().MemoryManager().Unmap(params.offset, *size); + if (const auto size{RemoveBufferMap(offset)}; size) { + system.GPU().MemoryManager().Unmap(offset, *size); } else { - LOG_ERROR(Service_NVDRV, "invalid offset=0x{:X}", params.offset); + LOG_ERROR(Service_NVDRV, "invalid offset=0x{:X}", offset); } std::memcpy(output.data(), ¶ms, output.size()); diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp index b27ee0502..07d851d0e 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp @@ -63,8 +63,7 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& return NvResult::BadParameter; } - u32 event_id = params.value & 0x00FF; - + const u32 event_id = params.value & 0x00FF; if (event_id >= MaxNvEvents) { std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::BadParameter; @@ -78,16 +77,17 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& event.writable->Signal(); return NvResult::Success; } + auto lock = gpu.LockSync(); const u32 current_syncpoint_value = gpu.GetSyncpointValue(params.syncpt_id); - const s32 diff = current_syncpoint_value - params.threshold; + const s32 diff = static_cast(current_syncpoint_value - params.threshold); if (diff >= 0) { event.writable->Signal(); params.value = current_syncpoint_value; std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::Success; } - const u32 target_value = current_syncpoint_value - diff; + const u32 target_value = current_syncpoint_value - static_cast(diff); if (!is_async) { params.value = 0; @@ -98,7 +98,7 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& return NvResult::Timeout; } - EventState status = events_interface.status[event_id]; + const EventState status = events_interface.status[event_id]; if (event_id < MaxNvEvents || status == EventState::Free || status == EventState::Registered) { events_interface.SetEventStatus(event_id, EventState::Waiting); events_interface.assigned_syncpt[event_id] = params.syncpt_id; @@ -114,7 +114,7 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& if (!is_async && ctrl.fresh_call) { ctrl.must_delay = true; ctrl.timeout = params.timeout; - ctrl.event_id = event_id; + ctrl.event_id = static_cast(event_id); return NvResult::Timeout; } std::memcpy(output.data(), ¶ms, sizeof(params)); diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp index f1966ac0e..5e51b37be 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp @@ -127,7 +127,7 @@ u32 nvhost_gpu::AllocGPFIFOEx2(const std::vector& input, std::vector& ou params.unk3); auto& gpu = system.GPU(); - params.fence_out.id = assigned_syncpoints; + params.fence_out.id = static_cast(assigned_syncpoints); params.fence_out.value = gpu.GetSyncpointValue(assigned_syncpoints); assigned_syncpoints++; std::memcpy(output.data(), ¶ms, output.size()); @@ -166,7 +166,8 @@ u32 nvhost_gpu::SubmitGPFIFO(const std::vector& input, std::vector& outp UNIMPLEMENTED_IF(params.flags.add_increment.Value() != 0); auto& gpu = system.GPU(); - u32 current_syncpoint_value = gpu.GetSyncpointValue(params.fence_out.id); + const u32 current_syncpoint_value = + gpu.GetSyncpointValue(static_cast(params.fence_out.id)); if (params.flags.increment.Value()) { params.fence_out.value += current_syncpoint_value; } else { @@ -200,7 +201,8 @@ u32 nvhost_gpu::KickoffPB(const std::vector& input, std::vector& output, UNIMPLEMENTED_IF(params.flags.add_increment.Value() != 0); auto& gpu = system.GPU(); - u32 current_syncpoint_value = gpu.GetSyncpointValue(params.fence_out.id); + const u32 current_syncpoint_value = + gpu.GetSyncpointValue(static_cast(params.fence_out.id)); if (params.flags.increment.Value()) { params.fence_out.value += current_syncpoint_value; } else { diff --git a/src/core/hle/service/nvdrv/interface.cpp b/src/core/hle/service/nvdrv/interface.cpp index 88fbfa9b0..2f4f73487 100644 --- a/src/core/hle/service/nvdrv/interface.cpp +++ b/src/core/hle/service/nvdrv/interface.cpp @@ -61,9 +61,9 @@ void NVDRV::IoctlBase(Kernel::HLERequestContext& ctx, IoctlVersion version) { if (ctrl.must_delay) { ctrl.fresh_call = false; ctx.SleepClientThread( - "NVServices::DelayedResponse", ctrl.timeout, - [=, this](std::shared_ptr thread, Kernel::HLERequestContext& ctx_, - Kernel::ThreadWakeupReason reason) { + "NVServices::DelayedResponse", static_cast(ctrl.timeout), + [=, this](std::shared_ptr, Kernel::HLERequestContext& ctx_, + Kernel::ThreadWakeupReason) { IoctlCtrl ctrl2{ctrl}; std::vector tmp_output = output; std::vector tmp_output2 = output2; @@ -77,7 +77,7 @@ void NVDRV::IoctlBase(Kernel::HLERequestContext& ctx, IoctlVersion version) { rb.Push(RESULT_SUCCESS); rb.Push(ioctl_result); }, - nvdrv->GetEventWriteable(ctrl.event_id)); + nvdrv->GetEventWriteable(static_cast(ctrl.event_id))); } else { ctx.WriteBuffer(output); if (version == IoctlVersion::Version3) { diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index c64673dba..621a429bc 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -247,7 +247,7 @@ void NVFlinger::Compose() { guard->unlock(); for (u32 fence_id = 0; fence_id < multi_fence.num_fences; fence_id++) { const auto& fence = multi_fence.fences[fence_id]; - gpu.WaitFence(fence.id, fence.value); + gpu.WaitFence(static_cast(fence.id), fence.value); } guard->lock(); diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index ba9159ee0..bc7476a5b 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -80,10 +80,10 @@ namespace Service { std::string_view port_name, const u32* cmd_buff) { // Number of params == bits 0-5 + bits 6-11 - int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); + const u32 num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); std::string function_string = fmt::format("function '{}': port={}", name, port_name); - for (int i = 1; i <= num_params; ++i) { + for (u32 i = 1; i <= num_params; ++i) { function_string += fmt::format(", cmd_buff[{}]=0x{:X}", i, cmd_buff[i]); } return function_string; diff --git a/src/core/hle/service/set/set.cpp b/src/core/hle/service/set/set.cpp index e64777668..82a7aecc7 100644 --- a/src/core/hle/service/set/set.cpp +++ b/src/core/hle/service/set/set.cpp @@ -91,7 +91,8 @@ void GetAvailableLanguageCodesImpl(Kernel::HLERequestContext& ctx, std::size_t m } void GetKeyCodeMapImpl(Kernel::HLERequestContext& ctx) { - const auto language_code = available_language_codes[Settings::values.language_index.GetValue()]; + const auto language_code = + available_language_codes[static_cast(Settings::values.language_index.GetValue())]; const auto key_code = std::find_if(language_to_layout.cbegin(), language_to_layout.cend(), [=](const auto& element) { return element.first == language_code; }); @@ -167,7 +168,8 @@ void SET::GetLanguageCode(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); - rb.PushEnum(available_language_codes[Settings::values.language_index.GetValue()]); + rb.PushEnum( + available_language_codes[static_cast(Settings::values.language_index.GetValue())]); } void SET::GetRegionCode(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index a74be9370..7cb70064c 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -437,9 +437,9 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco UNIMPLEMENTED_MSG("SOCK_RAW errno management"); } - [[maybe_unused]] const bool unk_flag = (static_cast(type) & 0x20000000) != 0; + [[maybe_unused]] const bool unk_flag = (static_cast(type) & 0x20000000U) != 0; UNIMPLEMENTED_IF_MSG(unk_flag, "Unknown flag in type"); - type = static_cast(static_cast(type) & ~0x20000000); + type = static_cast(static_cast(type) & ~0x20000000U); const s32 fd = FindFreeFileDescriptorHandle(); if (fd < 0) { @@ -447,7 +447,7 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco return {-1, Errno::MFILE}; } - FileDescriptor& descriptor = file_descriptors[fd].emplace(); + FileDescriptor& descriptor = GetFileDescriptor(fd).emplace(); // ENONMEM might be thrown here LOG_INFO(Service, "New socket fd={}", fd); @@ -461,7 +461,7 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco std::pair BSD::PollImpl(std::vector& write_buffer, std::vector read_buffer, s32 nfds, s32 timeout) { - if (write_buffer.size() < nfds * sizeof(PollFD)) { + if (write_buffer.size() < static_cast(nfds) * sizeof(PollFD)) { return {-1, Errno::INVAL}; } @@ -471,7 +471,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector fds(nfds); + std::vector fds(static_cast(nfds)); std::memcpy(fds.data(), read_buffer.data(), length); if (timeout >= 0) { @@ -497,7 +497,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector& descriptor = file_descriptors[pollfd.fd]; + const std::optional& descriptor = GetFileDescriptor(pollfd.fd); if (!descriptor) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", pollfd.fd); pollfd.revents = POLL_NVAL; @@ -508,7 +508,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector host_pollfds(fds.size()); std::transform(fds.begin(), fds.end(), host_pollfds.begin(), [this](PollFD pollfd) { Network::PollFD result; - result.socket = file_descriptors[pollfd.fd]->socket.get(); + result.socket = GetFileDescriptor(pollfd.fd)->socket.get(); result.events = TranslatePollEventsToHost(pollfd.events); result.revents = 0; return result; @@ -536,13 +536,13 @@ std::pair BSD::AcceptImpl(s32 fd, std::vector& write_buffer) { return {-1, Errno::MFILE}; } - FileDescriptor& descriptor = *file_descriptors[fd]; + FileDescriptor& descriptor = *GetFileDescriptor(fd); auto [result, bsd_errno] = descriptor.socket->Accept(); if (bsd_errno != Network::Errno::SUCCESS) { return {-1, Translate(bsd_errno)}; } - FileDescriptor& new_descriptor = file_descriptors[new_fd].emplace(); + FileDescriptor& new_descriptor = GetFileDescriptor(new_fd).emplace(); new_descriptor.socket = std::move(result.socket); new_descriptor.is_connection_based = descriptor.is_connection_based; @@ -561,7 +561,7 @@ Errno BSD::BindImpl(s32 fd, const std::vector& addr) { SockAddrIn addr_in; std::memcpy(&addr_in, addr.data(), sizeof(addr_in)); - return Translate(file_descriptors[fd]->socket->Bind(Translate(addr_in))); + return Translate(GetFileDescriptor(fd)->socket->Bind(Translate(addr_in))); } Errno BSD::ConnectImpl(s32 fd, const std::vector& addr) { @@ -573,7 +573,7 @@ Errno BSD::ConnectImpl(s32 fd, const std::vector& addr) { SockAddrIn addr_in; std::memcpy(&addr_in, addr.data(), sizeof(addr_in)); - return Translate(file_descriptors[fd]->socket->Connect(Translate(addr_in))); + return Translate(GetFileDescriptor(fd)->socket->Connect(Translate(addr_in))); } Errno BSD::GetPeerNameImpl(s32 fd, std::vector& write_buffer) { @@ -581,7 +581,7 @@ Errno BSD::GetPeerNameImpl(s32 fd, std::vector& write_buffer) { return Errno::BADF; } - const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetPeerName(); + const auto [addr_in, bsd_errno] = GetFileDescriptor(fd)->socket->GetPeerName(); if (bsd_errno != Network::Errno::SUCCESS) { return Translate(bsd_errno); } @@ -597,7 +597,7 @@ Errno BSD::GetSockNameImpl(s32 fd, std::vector& write_buffer) { return Errno::BADF; } - const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetSockName(); + const auto [addr_in, bsd_errno] = GetFileDescriptor(fd)->socket->GetSockName(); if (bsd_errno != Network::Errno::SUCCESS) { return Translate(bsd_errno); } @@ -612,7 +612,7 @@ Errno BSD::ListenImpl(s32 fd, s32 backlog) { if (!IsFileDescriptorValid(fd)) { return Errno::BADF; } - return Translate(file_descriptors[fd]->socket->Listen(backlog)); + return Translate(GetFileDescriptor(fd)->socket->Listen(backlog)); } std::pair BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) { @@ -620,14 +620,14 @@ std::pair BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) { return {-1, Errno::BADF}; } - FileDescriptor& descriptor = *file_descriptors[fd]; + FileDescriptor& descriptor = *GetFileDescriptor(fd); switch (cmd) { case FcntlCmd::GETFL: ASSERT(arg == 0); return {descriptor.flags, Errno::SUCCESS}; case FcntlCmd::SETFL: { - const bool enable = (arg & FLAG_O_NONBLOCK) != 0; + const bool enable = (static_cast(arg) & FLAG_O_NONBLOCK) != 0; const Errno bsd_errno = Translate(descriptor.socket->SetNonBlock(enable)); if (bsd_errno != Errno::SUCCESS) { return {-1, bsd_errno}; @@ -648,7 +648,7 @@ Errno BSD::SetSockOptImpl(s32 fd, u32 level, OptName optname, size_t optlen, con return Errno::BADF; } - Network::Socket* const socket = file_descriptors[fd]->socket.get(); + Network::Socket* const socket = GetFileDescriptor(fd)->socket.get(); if (optname == OptName::LINGER) { ASSERT(optlen == sizeof(Linger)); @@ -689,14 +689,14 @@ Errno BSD::ShutdownImpl(s32 fd, s32 how) { return Errno::BADF; } const Network::ShutdownHow host_how = Translate(static_cast(how)); - return Translate(file_descriptors[fd]->socket->Shutdown(host_how)); + return Translate(GetFileDescriptor(fd)->socket->Shutdown(host_how)); } std::pair BSD::RecvImpl(s32 fd, u32 flags, std::vector& message) { if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } - return Translate(file_descriptors[fd]->socket->Recv(flags, message)); + return Translate(GetFileDescriptor(fd)->socket->Recv(flags, message)); } std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& message, @@ -705,7 +705,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess return {-1, Errno::BADF}; } - FileDescriptor& descriptor = *file_descriptors[fd]; + FileDescriptor& descriptor = *GetFileDescriptor(fd); Network::SockAddrIn addr_in{}; Network::SockAddrIn* p_addr_in = nullptr; @@ -719,7 +719,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess // Apply flags if ((flags & FLAG_MSG_DONTWAIT) != 0) { flags &= ~FLAG_MSG_DONTWAIT; - if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { + if ((static_cast(descriptor.flags) & FLAG_O_NONBLOCK) == 0) { descriptor.socket->SetNonBlock(true); } } @@ -727,7 +727,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess const auto [ret, bsd_errno] = Translate(descriptor.socket->RecvFrom(flags, message, p_addr_in)); // Restore original state - if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { + if ((static_cast(descriptor.flags) & FLAG_O_NONBLOCK) == 0) { descriptor.socket->SetNonBlock(false); } @@ -748,7 +748,7 @@ std::pair BSD::SendImpl(s32 fd, u32 flags, const std::vector& me if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } - return Translate(file_descriptors[fd]->socket->Send(message, flags)); + return Translate(GetFileDescriptor(fd)->socket->Send(message, flags)); } std::pair BSD::SendToImpl(s32 fd, u32 flags, const std::vector& message, @@ -767,7 +767,8 @@ std::pair BSD::SendToImpl(s32 fd, u32 flags, const std::vector& p_addr_in = &addr_in; } - return Translate(file_descriptors[fd]->socket->SendTo(flags, message, p_addr_in)); + const auto& descriptor = GetFileDescriptor(fd); + return Translate(descriptor->socket->SendTo(flags, message, p_addr_in)); } Errno BSD::CloseImpl(s32 fd) { @@ -775,20 +776,21 @@ Errno BSD::CloseImpl(s32 fd) { return Errno::BADF; } - const Errno bsd_errno = Translate(file_descriptors[fd]->socket->Close()); + auto& descriptor = GetFileDescriptor(fd); + const Errno bsd_errno = Translate(descriptor->socket->Close()); if (bsd_errno != Errno::SUCCESS) { return bsd_errno; } LOG_INFO(Service, "Close socket fd={}", fd); - file_descriptors[fd].reset(); + descriptor.reset(); return bsd_errno; } s32 BSD::FindFreeFileDescriptorHandle() noexcept { for (s32 fd = 0; fd < static_cast(file_descriptors.size()); ++fd) { - if (!file_descriptors[fd]) { + if (!GetFileDescriptor(fd)) { return fd; } } @@ -800,7 +802,7 @@ bool BSD::IsFileDescriptorValid(s32 fd) const noexcept { LOG_ERROR(Service, "Invalid file descriptor handle={}", fd); return false; } - if (!file_descriptors[fd]) { + if (!GetFileDescriptor(fd)) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", fd); return false; } @@ -813,10 +815,12 @@ bool BSD::IsBlockingSocket(s32 fd) const noexcept { if (fd > static_cast(MAX_FD) || fd < 0) { return false; } - if (!file_descriptors[fd]) { + + const auto& descriptor = GetFileDescriptor(fd); + if (!descriptor) { return false; } - return (file_descriptors[fd]->flags & FLAG_O_NONBLOCK) != 0; + return (static_cast(descriptor->flags) & FLAG_O_NONBLOCK) != 0; } void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept { @@ -827,6 +831,14 @@ void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) co rb.PushEnum(bsd_errno); } +std::optional& BSD::GetFileDescriptor(s32 fd) { + return file_descriptors[static_cast(fd)]; +} + +const std::optional& BSD::GetFileDescriptor(s32 fd) const { + return file_descriptors[static_cast(fd)]; +} + BSD::BSD(Core::System& system, const char* name) : ServiceFramework(name), worker_pool{system, this} { // clang-format off diff --git a/src/core/hle/service/sockets/bsd.h b/src/core/hle/service/sockets/bsd.h index 357531951..ac9523d83 100644 --- a/src/core/hle/service/sockets/bsd.h +++ b/src/core/hle/service/sockets/bsd.h @@ -167,6 +167,9 @@ private: void BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept; + std::optional& GetFileDescriptor(s32 fd); + const std::optional& GetFileDescriptor(s32 fd) const; + std::array, MAX_FD> file_descriptors; BlockingWorkerPool(type)); + return {}; } } diff --git a/src/core/hle/service/time/time_zone_manager.cpp b/src/core/hle/service/time/time_zone_manager.cpp index bdf0439f2..df0ed924d 100644 --- a/src/core/hle/service/time/time_zone_manager.cpp +++ b/src/core/hle/service/time/time_zone_manager.cpp @@ -117,7 +117,8 @@ static constexpr int GetMonthLength(bool is_leap_year, int month) { constexpr std::array month_lengths{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; constexpr std::array month_lengths_leap{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - return is_leap_year ? month_lengths_leap[month] : month_lengths[month]; + const auto month_index = static_cast(month); + return is_leap_year ? month_lengths_leap[month_index] : month_lengths[month_index]; } static constexpr bool IsDigit(char value) { @@ -320,7 +321,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { int dest_len{}; int dest_offset{}; const char* dest_name{name + offset}; - if (rule.chars.size() < std::size_t(char_count)) { + if (rule.chars.size() < static_cast(char_count)) { return {}; } @@ -343,7 +344,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { return {}; } char_count += dest_len + 1; - if (rule.chars.size() < std::size_t(char_count)) { + if (rule.chars.size() < static_cast(char_count)) { return {}; } if (name[offset] != '\0' && name[offset] != ',' && name[offset] != ';') { @@ -386,7 +387,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { rule.default_type = 0; s64 jan_first{}; - int time_count{}; + u32 time_count{}; int jan_offset{}; int year_beginning{epoch_year}; do { @@ -414,7 +415,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { if (is_reversed || (start_time < end_time && (end_time - start_time < (year_seconds + (std_offset - dest_offset))))) { - if (rule.ats.size() - 2 < std::size_t(time_count)) { + if (rule.ats.size() - 2 < time_count) { break; } @@ -438,7 +439,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } jan_offset = 0; } - rule.time_count = time_count; + rule.time_count = static_cast(time_count); if (time_count == 0) { rule.type_count = 1; } else if (years_per_repeat < year - year_beginning) { @@ -451,26 +452,30 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } s64 their_std_offset{}; - for (int index{}; index < rule.time_count; ++index) { + for (u32 index = 0; index < static_cast(rule.time_count); ++index) { const s8 type{rule.types[index]}; - if (rule.ttis[type].is_standard_time_daylight) { - their_std_offset = -rule.ttis[type].gmt_offset; + const auto& tti = rule.ttis[static_cast(type)]; + + if (tti.is_standard_time_daylight) { + their_std_offset = -tti.gmt_offset; } } s64 their_offset{their_std_offset}; - for (int index{}; index < rule.time_count; ++index) { + for (u32 index = 0; index < static_cast(rule.time_count); ++index) { const s8 type{rule.types[index]}; - rule.types[index] = rule.ttis[type].is_dst ? 1 : 0; - if (!rule.ttis[type].is_gmt) { - if (!rule.ttis[type].is_standard_time_daylight) { + const auto& tti = rule.ttis[static_cast(type)]; + + rule.types[index] = tti.is_dst ? 1 : 0; + if (!tti.is_gmt) { + if (!tti.is_standard_time_daylight) { rule.ats[index] += dest_offset - their_std_offset; } else { rule.ats[index] += std_offset - their_std_offset; } } - their_offset = -rule.ttis[type].gmt_offset; - if (!rule.ttis[type].is_dst) { + their_offset = -tti.gmt_offset; + if (!tti.is_dst) { their_std_offset = their_offset; } } @@ -494,16 +499,16 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } rule.char_count = char_count; - for (int index{}; index < std_len; ++index) { + for (std::size_t index = 0; index < static_cast(std_len); ++index) { rule.chars[index] = std_name[index]; } - rule.chars[std_len++] = '\0'; + rule.chars[static_cast(std_len++)] = '\0'; if (dest_len != 0) { - for (int index{}; index < dest_len; ++index) { - rule.chars[std_len + index] = dest_name[index]; + for (int index = 0; index < dest_len; ++index) { + rule.chars[static_cast(std_len + index)] = dest_name[index]; } - rule.chars[std_len + dest_len] = '\0'; + rule.chars[static_cast(std_len + dest_len)] = '\0'; } return true; @@ -531,33 +536,33 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi int time_count{}; u64 read_offset = sizeof(TzifHeader); - for (int index{}; index < time_zone_rule.time_count; ++index) { + for (size_t index = 0; index < static_cast(time_zone_rule.time_count); ++index) { s64_be at{}; vfs_file->ReadObject(&at, read_offset); time_zone_rule.types[index] = 1; - if (time_count != 0 && at <= time_zone_rule.ats[time_count - 1]) { - if (at < time_zone_rule.ats[time_count - 1]) { + if (time_count != 0 && at <= time_zone_rule.ats[static_cast(time_count) - 1]) { + if (at < time_zone_rule.ats[static_cast(time_count) - 1]) { return {}; } time_zone_rule.types[index - 1] = 0; time_count--; } - time_zone_rule.ats[time_count++] = at; + time_zone_rule.ats[static_cast(time_count++)] = at; read_offset += sizeof(s64_be); } time_count = 0; - for (int index{}; index < time_zone_rule.time_count; ++index) { - const u8 type{*vfs_file->ReadByte(read_offset)}; - read_offset += sizeof(u8); + for (size_t index = 0; index < static_cast(time_zone_rule.time_count); ++index) { + const auto type{static_cast(*vfs_file->ReadByte(read_offset))}; + read_offset += sizeof(s8); if (time_zone_rule.time_count <= type) { return {}; } if (time_zone_rule.types[index] != 0) { - time_zone_rule.types[time_count++] = type; + time_zone_rule.types[static_cast(time_count++)] = type; } } time_zone_rule.time_count = time_count; - for (int index{}; index < time_zone_rule.type_count; ++index) { + for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { TimeTypeInfo& ttis{time_zone_rule.ttis[index]}; u32_be gmt_offset{}; vfs_file->ReadObject(&gmt_offset, read_offset); @@ -579,10 +584,11 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi ttis.abbreviation_list_index = abbreviation_list_index; } - vfs_file->ReadArray(time_zone_rule.chars.data(), time_zone_rule.char_count, read_offset); - time_zone_rule.chars[time_zone_rule.char_count] = '\0'; - read_offset += time_zone_rule.char_count; - for (int index{}; index < time_zone_rule.type_count; ++index) { + vfs_file->ReadArray(time_zone_rule.chars.data(), static_cast(time_zone_rule.char_count), + read_offset); + time_zone_rule.chars[static_cast(time_zone_rule.char_count)] = '\0'; + read_offset += static_cast(time_zone_rule.char_count); + for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { if (header.ttis_std_count == 0) { time_zone_rule.ttis[index].is_standard_time_daylight = false; } else { @@ -595,7 +601,7 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi } } - for (int index{}; index < time_zone_rule.type_count; ++index) { + for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { if (header.ttis_std_count == 0) { time_zone_rule.ttis[index].is_gmt = false; } else { @@ -619,13 +625,14 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi } std::array temp_name{}; - vfs_file->ReadArray(temp_name.data(), bytes_read, read_offset); - if (bytes_read > 2 && temp_name[0] == '\n' && temp_name[bytes_read - 1] == '\n' && - std::size_t(time_zone_rule.type_count) + 2 <= time_zone_rule.ttis.size()) { - temp_name[bytes_read - 1] = '\0'; + vfs_file->ReadArray(temp_name.data(), static_cast(bytes_read), read_offset); + if (bytes_read > 2 && temp_name[0] == '\n' && + temp_name[static_cast(bytes_read - 1)] == '\n' && + static_cast(time_zone_rule.type_count) + 2 <= time_zone_rule.ttis.size()) { + temp_name[static_cast(bytes_read - 1)] = '\0'; std::array name{}; - std::memcpy(name.data(), temp_name.data() + 1, std::size_t(bytes_read - 1)); + std::memcpy(name.data(), temp_name.data() + 1, static_cast(bytes_read - 1)); TimeZoneRule temp_rule; if (ParsePosixName(name.data(), temp_rule)) { @@ -642,24 +649,24 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi s32 default_type{}; for (default_type = 0; default_type < time_zone_rule.time_count; default_type++) { - if (time_zone_rule.types[default_type] == 0) { + if (time_zone_rule.types[static_cast(default_type)] == 0) { break; } } default_type = default_type < time_zone_rule.time_count ? -1 : 0; if (default_type < 0 && time_zone_rule.time_count > 0 && - time_zone_rule.ttis[time_zone_rule.types[0]].is_dst) { + time_zone_rule.ttis[static_cast(time_zone_rule.types[0])].is_dst) { default_type = time_zone_rule.types[0]; while (--default_type >= 0) { - if (!time_zone_rule.ttis[default_type].is_dst) { + if (!time_zone_rule.ttis[static_cast(default_type)].is_dst) { break; } } } if (default_type < 0) { default_type = 0; - while (time_zone_rule.ttis[default_type].is_dst) { + while (time_zone_rule.ttis[static_cast(default_type)].is_dst) { if (++default_type >= time_zone_rule.type_count) { default_type = 0; break; @@ -749,12 +756,12 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, CalendarTimeInternal& calendar_time, CalendarAdditionalInfo& calendar_additional_info) { if ((rules.go_ahead && time < rules.ats[0]) || - (rules.go_back && time > rules.ats[rules.time_count - 1])) { + (rules.go_back && time > rules.ats[static_cast(rules.time_count - 1)])) { s64 seconds{}; if (time < rules.ats[0]) { seconds = rules.ats[0] - time; } else { - seconds = time - rules.ats[rules.time_count - 1]; + seconds = time - rules.ats[static_cast(rules.time_count - 1)]; } seconds--; @@ -767,7 +774,8 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, } else { new_time -= seconds; } - if (new_time < rules.ats[0] && new_time > rules.ats[rules.time_count - 1]) { + if (new_time < rules.ats[0] && + new_time > rules.ats[static_cast(rules.time_count - 1)]) { return ERROR_TIME_NOT_FOUND; } if (const ResultCode result{ @@ -791,25 +799,27 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, s32 low{1}; s32 high{rules.time_count}; while (low < high) { - s32 mid{(low + high) >> 1}; - if (time < rules.ats[mid]) { + const s32 mid{(low + high) >> 1}; + if (time < rules.ats[static_cast(mid)]) { high = mid; } else { low = mid + 1; } } - tti_index = rules.types[low - 1]; + tti_index = rules.types[static_cast(low - 1)]; } - if (const ResultCode result{CreateCalendarTime(time, rules.ttis[tti_index].gmt_offset, - calendar_time, calendar_additional_info)}; + if (const ResultCode result{ + CreateCalendarTime(time, rules.ttis[static_cast(tti_index)].gmt_offset, + calendar_time, calendar_additional_info)}; result != RESULT_SUCCESS) { return result; } - calendar_additional_info.is_dst = rules.ttis[tti_index].is_dst; - const char* time_zone{&rules.chars[rules.ttis[tti_index].abbreviation_list_index]}; - for (int index{}; time_zone[index] != '\0'; ++index) { + const auto& tti = rules.ttis[static_cast(tti_index)]; + calendar_additional_info.is_dst = tti.is_dst; + const char* time_zone{&rules.chars[static_cast(tti.abbreviation_list_index)]}; + for (size_t index = 0; time_zone[index] != '\0'; ++index) { calendar_additional_info.timezone_name[index] = time_zone[index]; } return RESULT_SUCCESS; diff --git a/src/core/hle/service/time/time_zone_service.cpp b/src/core/hle/service/time/time_zone_service.cpp index ff3a10b3e..8a0227021 100644 --- a/src/core/hle/service/time/time_zone_service.cpp +++ b/src/core/hle/service/time/time_zone_service.cpp @@ -49,12 +49,12 @@ void ITimeZoneService::LoadTimeZoneRule(Kernel::HLERequestContext& ctx) { const auto raw_location_name{rp.PopRaw>()}; std::string location_name; - for (const auto& byte : raw_location_name) { + for (const auto byte : raw_location_name) { // Strip extra bytes if (byte == '\0') { break; } - location_name.push_back(byte); + location_name.push_back(static_cast(byte)); } LOG_DEBUG(Service_Time, "called, location_name={}", location_name); diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index dca1fcb18..86d0527fc 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -220,18 +220,19 @@ public: } const char* GetSectionName(int section) const; const u8* GetSectionDataPtr(int section) const { - if (section < 0 || section >= header->e_shnum) - return nullptr; - if (sections[section].sh_type != SHT_NOBITS) - return GetPtr(sections[section].sh_offset); - else + if (section < 0 || section >= header->e_shnum) { return nullptr; + } + if (sections[section].sh_type != SHT_NOBITS) { + return GetPtr(static_cast(sections[section].sh_offset)); + } + return nullptr; } bool IsCodeSection(int section) const { return sections[section].sh_type == SHT_PROGBITS; } const u8* GetSegmentPtr(int segment) { - return GetPtr(segments[segment].p_offset); + return GetPtr(static_cast(segments[segment].p_offset)); } u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; @@ -258,14 +259,14 @@ ElfReader::ElfReader(void* ptr) { } const char* ElfReader::GetSectionName(int section) const { - if (sections[section].sh_type == SHT_NULL) + if (sections[section].sh_type == SHT_NULL) { return nullptr; + } - int name_offset = sections[section].sh_name; - const char* ptr = reinterpret_cast(GetSectionDataPtr(header->e_shstrndx)); - - if (ptr) + const auto name_offset = sections[section].sh_name; + if (const auto* ptr = reinterpret_cast(GetSectionDataPtr(header->e_shstrndx))) { return ptr + name_offset; + } return nullptr; } @@ -291,7 +292,7 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { for (unsigned int i = 0; i < header->e_phnum; ++i) { const Elf32_Phdr* p = &segments[i]; if (p->p_type == PT_LOAD) { - total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; + total_image_size += (p->p_memsz + 0xFFF) & ~0xFFFU; } } @@ -300,14 +301,14 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { Kernel::CodeSet codeset; - for (unsigned int i = 0; i < header->e_phnum; ++i) { + for (u32 i = 0; i < header->e_phnum; ++i) { const Elf32_Phdr* p = &segments[i]; LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz); if (p->p_type == PT_LOAD) { Kernel::CodeSet::Segment* codeset_segment; - u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); + const u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); if (permission_flags == (PF_R | PF_X)) { codeset_segment = &codeset.CodeSegment(); } else if (permission_flags == (PF_R)) { @@ -329,14 +330,14 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { } const VAddr segment_addr = base_addr + p->p_vaddr; - const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF; + const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFFU; codeset_segment->offset = current_image_position; codeset_segment->addr = segment_addr; codeset_segment->size = aligned_size; - std::memcpy(program_image.data() + current_image_position, GetSegmentPtr(i), - p->p_filesz); + std::memcpy(program_image.data() + current_image_position, + GetSegmentPtr(static_cast(i)), p->p_filesz); current_image_position += aligned_size; } } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index b88aa5c40..2ce12df88 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -197,7 +197,7 @@ struct Memory::Impl { std::string string; string.reserve(max_length); for (std::size_t i = 0; i < max_length; ++i) { - const char c = Read8(vaddr); + const auto c = static_cast(Read8(vaddr)); if (c == '\0') { break; } diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 4b3bb4366..d280f7a28 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -96,7 +96,7 @@ int LastError() { bool EnableNonBlock(SOCKET fd, bool enable) { u_long value = enable ? 1 : 0; - return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR; + return ioctlsocket(fd, static_cast(FIONBIO), &value) != SOCKET_ERROR; } #elif __unix__ // ^ _WIN32 v __unix__ @@ -140,7 +140,9 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { result.sin_port = htons(input.portno); - result.sin_addr.s_addr = input.ip[0] | input.ip[1] << 8 | input.ip[2] << 16 | input.ip[3] << 24; + result.sin_addr.s_addr = static_cast( + input.ip[0] | static_cast(input.ip[1] << 8) | static_cast(input.ip[2] << 16) | + static_cast(input.ip[3] << 24)); sockaddr addr; std::memcpy(&addr, &result, sizeof(addr)); @@ -148,7 +150,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { } int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) { - return poll(fds, nfds, timeout); + return poll(fds, static_cast(nfds), timeout); } int closesocket(SOCKET fd) { @@ -158,7 +160,7 @@ int closesocket(SOCKET fd) { linger MakeLinger(bool enable, u32 linger_value) { linger value; value.l_onoff = enable ? 1 : 0; - value.l_linger = linger_value; + value.l_linger = static_cast(linger_value); return value; } @@ -337,7 +339,7 @@ std::pair Poll(std::vector& pollfds, s32 timeout) { std::transform(pollfds.begin(), pollfds.end(), host_pollfds.begin(), [](PollFD fd) { WSAPOLLFD result; result.fd = fd.socket->fd; - result.events = TranslatePollEvents(fd.events); + result.events = static_cast(TranslatePollEvents(fd.events)); result.revents = 0; return result; }); @@ -499,12 +501,12 @@ Errno Socket::Shutdown(ShutdownHow how) { } } -std::pair Socket::Recv(int flags, std::vector& message) { +std::pair Socket::Recv(u32 flags, std::vector& message) { ASSERT(flags == 0); ASSERT(message.size() < static_cast(std::numeric_limits::max())); - const auto result = - recv(fd, reinterpret_cast(message.data()), static_cast(message.size()), 0); + const auto result = recv(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } @@ -522,7 +524,7 @@ std::pair Socket::Recv(int flags, std::vector& message) { } } -std::pair Socket::RecvFrom(int flags, std::vector& message, SockAddrIn* addr) { +std::pair Socket::RecvFrom(u32 flags, std::vector& message, SockAddrIn* addr) { ASSERT(flags == 0); ASSERT(message.size() < static_cast(std::numeric_limits::max())); @@ -532,7 +534,7 @@ std::pair Socket::RecvFrom(int flags, std::vector& message, Sock sockaddr* const p_addr_in = addr ? &addr_in : nullptr; const auto result = recvfrom(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, p_addr_in, p_addrlen); + static_cast(message.size()), 0, p_addr_in, p_addrlen); if (result != SOCKET_ERROR) { if (addr) { ASSERT(addrlen == sizeof(addr_in)); @@ -554,12 +556,12 @@ std::pair Socket::RecvFrom(int flags, std::vector& message, Sock } } -std::pair Socket::Send(const std::vector& message, int flags) { +std::pair Socket::Send(const std::vector& message, u32 flags) { ASSERT(message.size() < static_cast(std::numeric_limits::max())); ASSERT(flags == 0); const auto result = send(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0); + static_cast(message.size()), 0); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } @@ -591,8 +593,9 @@ std::pair Socket::SendTo(u32 flags, const std::vector& message, to = &host_addr_in; } - const auto result = sendto(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, to, tolen); + const auto result = + sendto(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0, to, static_cast(tolen)); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } diff --git a/src/core/network/network.h b/src/core/network/network.h index 0622e4593..b95bf68f8 100644 --- a/src/core/network/network.h +++ b/src/core/network/network.h @@ -67,12 +67,12 @@ struct PollFD { u16 revents; }; -constexpr u16 POLL_IN = 1 << 0; -constexpr u16 POLL_PRI = 1 << 1; -constexpr u16 POLL_OUT = 1 << 2; -constexpr u16 POLL_ERR = 1 << 3; -constexpr u16 POLL_HUP = 1 << 4; -constexpr u16 POLL_NVAL = 1 << 5; +constexpr u32 POLL_IN = 1 << 0; +constexpr u32 POLL_PRI = 1 << 1; +constexpr u32 POLL_OUT = 1 << 2; +constexpr u32 POLL_ERR = 1 << 3; +constexpr u32 POLL_HUP = 1 << 4; +constexpr u32 POLL_NVAL = 1 << 5; class NetworkInstance { public: diff --git a/src/core/network/sockets.h b/src/core/network/sockets.h index 7bdff0fe4..1682cbd4e 100644 --- a/src/core/network/sockets.h +++ b/src/core/network/sockets.h @@ -56,11 +56,11 @@ public: Errno Shutdown(ShutdownHow how); - std::pair Recv(int flags, std::vector& message); + std::pair Recv(u32 flags, std::vector& message); - std::pair RecvFrom(int flags, std::vector& message, SockAddrIn* addr); + std::pair RecvFrom(u32 flags, std::vector& message, SockAddrIn* addr); - std::pair Send(const std::vector& message, int flags); + std::pair Send(const std::vector& message, u32 flags); std::pair SendTo(u32 flags, const std::vector& message, const SockAddrIn* addr); -- cgit v1.2.3 From 3d592972dc3fd61cc88771b889eff237e4e03e0f Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 Oct 2020 19:07:39 -0700 Subject: Revert "core: Fix clang build" --- externals/microprofile/microprofile.h | 4 +- src/audio_core/CMakeLists.txt | 5 +- src/audio_core/algorithm/interpolate.cpp | 6 +- src/audio_core/audio_renderer.cpp | 4 +- src/audio_core/codec.cpp | 6 +- src/audio_core/command_generator.cpp | 212 +++++++++------------ src/audio_core/command_generator.h | 4 +- src/audio_core/cubeb_sink.cpp | 2 +- src/audio_core/cubeb_sink.h | 2 +- src/audio_core/info_updater.cpp | 4 +- src/audio_core/mix_context.cpp | 17 +- src/audio_core/sink_context.cpp | 5 +- src/audio_core/splitter_context.cpp | 60 +++--- src/audio_core/time_stretch.h | 10 +- src/audio_core/voice_context.cpp | 47 ++--- src/common/fiber.h | 4 +- src/common/file_util.h | 3 +- src/common/math_util.h | 4 +- src/common/multi_level_queue.h | 2 +- src/common/spin_lock.h | 8 - src/common/swap.h | 10 +- src/common/thread_queue_list.h | 4 +- src/core/CMakeLists.txt | 5 +- src/core/arm/arm_interface.cpp | 24 +-- src/core/arm/arm_interface.h | 8 +- src/core/arm/cpu_interrupt_handler.h | 4 +- src/core/arm/dynarmic/arm_dynarmic_32.cpp | 10 +- src/core/arm/dynarmic/arm_dynarmic_32.h | 8 +- src/core/arm/dynarmic/arm_dynarmic_64.cpp | 10 +- src/core/arm/dynarmic/arm_dynarmic_64.h | 8 +- src/core/arm/unicorn/arm_unicorn.cpp | 42 ++-- src/core/arm/unicorn/arm_unicorn.h | 8 +- src/core/core_timing.cpp | 13 +- src/core/core_timing_util.cpp | 42 ++-- src/core/core_timing_util.h | 4 +- src/core/crypto/key_manager.cpp | 7 +- src/core/crypto/partition_data_manager.cpp | 2 +- src/core/file_sys/content_archive.cpp | 4 +- src/core/file_sys/fsmitm_romfsbuild.cpp | 2 +- src/core/file_sys/ips_layer.cpp | 41 ++-- src/core/file_sys/kernel_executable.cpp | 6 +- src/core/file_sys/nca_patch.cpp | 7 +- src/core/frontend/applets/controller.cpp | 2 +- src/core/frontend/applets/profile_select.cpp | 3 +- src/core/gdbstub/gdbstub.cpp | 47 +---- src/core/gdbstub/gdbstub.h | 2 +- src/core/hle/ipc_helpers.h | 4 +- src/core/hle/kernel/address_arbiter.cpp | 4 +- src/core/hle/kernel/handle_table.cpp | 2 +- src/core/hle/kernel/hle_ipc.cpp | 2 +- src/core/hle/kernel/kernel.cpp | 2 +- src/core/hle/kernel/memory/address_space_info.cpp | 2 - src/core/hle/kernel/memory/memory_manager.cpp | 4 +- src/core/hle/kernel/memory/page_heap.cpp | 17 +- src/core/hle/kernel/memory/page_heap.h | 18 +- src/core/hle/kernel/memory/page_table.cpp | 6 +- src/core/hle/kernel/physical_core.h | 2 +- src/core/hle/kernel/process.cpp | 17 +- src/core/hle/kernel/resource_limit.cpp | 4 +- src/core/hle/kernel/scheduler.cpp | 72 ++----- src/core/hle/kernel/svc.cpp | 7 +- src/core/hle/kernel/svc_wrap.h | 17 +- src/core/hle/kernel/synchronization.cpp | 4 +- src/core/hle/kernel/thread.cpp | 2 +- src/core/hle/kernel/thread.h | 6 +- src/core/hle/service/acc/profile_manager.cpp | 13 +- src/core/hle/service/am/am.cpp | 2 +- src/core/hle/service/am/applets/controller.cpp | 26 +-- src/core/hle/service/audio/audout_u.cpp | 7 +- src/core/hle/service/audio/hwopus.cpp | 29 ++- src/core/hle/service/bcat/backend/backend.h | 8 +- src/core/hle/service/bcat/backend/boxcat.cpp | 9 - src/core/hle/service/bcat/module.cpp | 3 +- src/core/hle/service/filesystem/fsp_srv.cpp | 8 +- src/core/hle/service/hid/controllers/debug_pad.cpp | 8 +- src/core/hle/service/hid/controllers/gesture.cpp | 8 +- src/core/hle/service/hid/controllers/keyboard.cpp | 8 +- src/core/hle/service/hid/controllers/mouse.cpp | 8 +- src/core/hle/service/hid/controllers/npad.cpp | 69 +++---- src/core/hle/service/hid/controllers/stubbed.cpp | 12 +- .../hle/service/hid/controllers/touchscreen.cpp | 9 +- src/core/hle/service/hid/controllers/touchscreen.h | 2 +- src/core/hle/service/hid/controllers/xpad.cpp | 8 +- src/core/hle/service/ldr/ldr.cpp | 4 +- src/core/hle/service/mii/manager.cpp | 20 +- src/core/hle/service/nfp/nfp.cpp | 2 +- src/core/hle/service/ns/ns.cpp | 2 +- src/core/hle/service/ns/pl_u.cpp | 12 +- .../hle/service/nvdrv/devices/nvhost_as_gpu.cpp | 48 ++--- src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp | 12 +- src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp | 8 +- src/core/hle/service/nvdrv/interface.cpp | 8 +- src/core/hle/service/nvflinger/nvflinger.cpp | 2 +- src/core/hle/service/service.cpp | 4 +- src/core/hle/service/set/set.cpp | 6 +- src/core/hle/service/sockets/bsd.cpp | 72 +++---- src/core/hle/service/sockets/bsd.h | 3 - src/core/hle/service/sockets/sockets_translate.cpp | 1 - src/core/hle/service/time/time_zone_manager.cpp | 118 ++++++------ src/core/hle/service/time/time_zone_service.cpp | 4 +- src/core/loader/elf.cpp | 35 ++-- src/core/memory.cpp | 2 +- src/core/network/network.cpp | 31 ++- src/core/network/network.h | 12 +- src/core/network/sockets.h | 6 +- 105 files changed, 667 insertions(+), 906 deletions(-) (limited to 'src/common') diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h index d22f92868..85d5bd5de 100644 --- a/externals/microprofile/microprofile.h +++ b/externals/microprofile/microprofile.h @@ -857,7 +857,7 @@ inline int64_t MicroProfileLogTickDifference(MicroProfileLogEntry Start, MicroPr { uint64_t nStart = Start; uint64_t nEnd = End; - auto nDifference = static_cast((nEnd << 16) - (nStart << 16)); + int64_t nDifference = ((nEnd<<16) - (nStart<<16)); return nDifference >> 16; } @@ -868,7 +868,7 @@ inline int64_t MicroProfileLogGetTick(MicroProfileLogEntry e) inline int64_t MicroProfileLogSetTick(MicroProfileLogEntry e, int64_t nTick) { - return static_cast((MP_LOG_TICK_MASK & static_cast(nTick)) | (e & static_cast(~MP_LOG_TICK_MASK))); + return (MP_LOG_TICK_MASK & nTick) | (e & ~MP_LOG_TICK_MASK); } template diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index 74c1453aa..54940a034 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt @@ -51,9 +51,8 @@ if (NOT MSVC) -Werror=implicit-fallthrough -Werror=reorder -Werror=sign-compare - -Werror=sign-conversion - $<$:-Werror=unused-but-set-parameter> - $<$:-Werror=unused-but-set-variable> + -Werror=unused-but-set-parameter + -Werror=unused-but-set-variable -Werror=unused-variable ) endif() diff --git a/src/audio_core/algorithm/interpolate.cpp b/src/audio_core/algorithm/interpolate.cpp index 587ee5b7b..699fcb84c 100644 --- a/src/audio_core/algorithm/interpolate.cpp +++ b/src/audio_core/algorithm/interpolate.cpp @@ -167,8 +167,8 @@ std::vector Interpolate(InterpolationState& state, std::vector input, output.reserve(static_cast(static_cast(input.size()) / ratio + InterpolationState::taps)); - for (std::size_t frame = 0; frame < num_frames; ++frame) { - const auto lut_index{static_cast(state.fraction >> 8) * InterpolationState::taps}; + for (std::size_t frame{}; frame < num_frames; ++frame) { + const std::size_t lut_index{(state.fraction >> 8) * InterpolationState::taps}; std::rotate(state.history.begin(), state.history.end() - 1, state.history.end()); state.history[0][0] = input[frame * 2 + 0]; @@ -225,7 +225,7 @@ void Resample(s32* output, const s32* input, s32 pitch, s32& fraction, std::size output[i] = (l0 * s0 + l1 * s1 + l2 * s2 + l3 * s3) >> 15; fraction += pitch; - index += static_cast(fraction >> 15); + index += (fraction >> 15); fraction &= 0x7fff; } } diff --git a/src/audio_core/audio_renderer.cpp b/src/audio_core/audio_renderer.cpp index 094bace9c..a7e851bb8 100644 --- a/src/audio_core/audio_renderer.cpp +++ b/src/audio_core/audio_renderer.cpp @@ -187,8 +187,8 @@ void AudioRenderer::QueueMixedBuffer(Buffer::Tag tag) { const auto& in_params = final_mix.GetInParams(); std::vector mix_buffers(channel_count); for (std::size_t i = 0; i < channel_count; i++) { - mix_buffers[i] = command_generator.GetMixBuffer( - static_cast(in_params.buffer_offset) + buffer_offsets[i]); + mix_buffers[i] = + command_generator.GetMixBuffer(in_params.buffer_offset + buffer_offsets[i]); } for (std::size_t i = 0; i < BUFFER_SIZE; i++) { diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp index d89f94ea2..2fb91c13a 100644 --- a/src/audio_core/codec.cpp +++ b/src/audio_core/codec.cpp @@ -32,7 +32,7 @@ std::vector DecodeADPCM(const u8* const data, std::size_t size, const ADPCM for (std::size_t framei = 0; framei < NUM_FRAMES; framei++) { const int frame_header = data[framei * FRAME_LEN]; const int scale = 1 << (frame_header & 0xF); - const auto idx = static_cast((frame_header >> 4) & 0x7); + const int idx = (frame_header >> 4) & 0x7; // Coefficients are fixed point with 11 bits fractional part. const int coef1 = coeff[idx * 2 + 0]; @@ -57,11 +57,11 @@ std::vector DecodeADPCM(const u8* const data, std::size_t size, const ADPCM std::size_t outputi = framei * SAMPLES_PER_FRAME; std::size_t datai = framei * FRAME_LEN + 1; for (std::size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) { - const s16 sample1 = decode_sample(SIGNED_NIBBLES[static_cast(data[datai] >> 4)]); + const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]); ret[outputi] = sample1; outputi++; - const s16 sample2 = decode_sample(SIGNED_NIBBLES[static_cast(data[datai] & 0xF)]); + const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]); ret[outputi] = sample2; outputi++; diff --git a/src/audio_core/command_generator.cpp b/src/audio_core/command_generator.cpp index c0edb625d..fb8700ccf 100644 --- a/src/audio_core/command_generator.cpp +++ b/src/audio_core/command_generator.cpp @@ -15,8 +15,8 @@ constexpr std::size_t MIX_BUFFER_SIZE = 0x3f00; constexpr std::size_t SCALED_MIX_BUFFER_SIZE = MIX_BUFFER_SIZE << 15ULL; template -void ApplyMix(s32* output, const s32* input, s32 gain, std::size_t sample_count) { - for (std::size_t i = 0; i < sample_count; i += N) { +void ApplyMix(s32* output, const s32* input, s32 gain, s32 sample_count) { + for (std::size_t i = 0; i < static_cast(sample_count); i += N) { for (std::size_t j = 0; j < N; j++) { output[i + j] += static_cast((static_cast(input[i + j]) * gain + 0x4000) >> 15); @@ -111,8 +111,7 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { const auto channel_count = in_params.channel_count; for (s32 channel = 0; channel < channel_count; channel++) { - const auto resource_id = - static_cast(in_params.voice_channel_resource_id[static_cast(channel)]); + const auto resource_id = in_params.voice_channel_resource_id[channel]; auto& dsp_state = voice_context.GetDspSharedState(resource_id); auto& channel_resource = voice_context.GetChannelResource(resource_id); @@ -133,15 +132,14 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { if (in_params.mix_id != AudioCommon::NO_MIX) { // If we're using a mix id - auto& mix_info = mix_context.GetInfo(static_cast(in_params.mix_id)); + auto& mix_info = mix_context.GetInfo(in_params.mix_id); const auto& dest_mix_params = mix_info.GetInParams(); // Voice Mixing GenerateVoiceMixCommand( channel_resource.GetCurrentMixVolume(), channel_resource.GetLastMixVolume(), - dsp_state, static_cast(dest_mix_params.buffer_offset), - static_cast(dest_mix_params.buffer_count), - worker_params.mix_buffer_count + static_cast(channel), in_params.node_id); + dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, + worker_params.mix_buffer_count + channel, in_params.node_id); // Update last mix volumes channel_resource.UpdateLastMixVolumes(); @@ -158,15 +156,12 @@ void CommandGenerator::GenerateVoiceCommand(ServerVoiceInfo& voice_info) { continue; } - const auto& mix_info = - mix_context.GetInfo(static_cast(destination_data->GetMixId())); + const auto& mix_info = mix_context.GetInfo(destination_data->GetMixId()); const auto& dest_mix_params = mix_info.GetInParams(); GenerateVoiceMixCommand( destination_data->CurrentMixVolumes(), destination_data->LastMixVolumes(), - dsp_state, static_cast(dest_mix_params.buffer_offset), - static_cast(dest_mix_params.buffer_count), - worker_params.mix_buffer_count + static_cast(channel), - in_params.node_id); + dsp_state, dest_mix_params.buffer_offset, dest_mix_params.buffer_count, + worker_params.mix_buffer_count + channel, in_params.node_id); destination_data->MarkDirty(); } } @@ -224,10 +219,9 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo if (depop) { if (in_params.mix_id != AudioCommon::NO_MIX) { - auto& mix_info = mix_context.GetInfo(static_cast(in_params.mix_id)); + auto& mix_info = mix_context.GetInfo(in_params.mix_id); const auto& mix_in = mix_info.GetInParams(); - GenerateDepopPrepareCommand(dsp_state, static_cast(mix_in.buffer_count), - static_cast(mix_in.buffer_offset)); + GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); } else if (in_params.splitter_info_id != AudioCommon::NO_SPLITTER) { s32 index{}; while (const auto* destination = @@ -235,24 +229,23 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo if (!destination->IsConfigured()) { continue; } - auto& mix_info = mix_context.GetInfo(static_cast(destination->GetMixId())); + auto& mix_info = mix_context.GetInfo(destination->GetMixId()); const auto& mix_in = mix_info.GetInParams(); - GenerateDepopPrepareCommand(dsp_state, static_cast(mix_in.buffer_count), - static_cast(mix_in.buffer_offset)); + GenerateDepopPrepareCommand(dsp_state, mix_in.buffer_count, mix_in.buffer_offset); } } } else { switch (in_params.sample_format) { case SampleFormat::Pcm16: DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(channel), dsp_state, channel, - static_cast(worker_params.sample_rate), - static_cast(worker_params.sample_count), in_params.node_id); + worker_params.sample_rate, worker_params.sample_count, + in_params.node_id); break; case SampleFormat::Adpcm: ASSERT(channel == 0 && in_params.channel_count == 1); DecodeFromWaveBuffers(voice_info, GetChannelMixBuffer(0), dsp_state, 0, - static_cast(worker_params.sample_rate), - static_cast(worker_params.sample_count), in_params.node_id); + worker_params.sample_rate, worker_params.sample_count, + in_params.node_id); break; default: UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); @@ -262,7 +255,7 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - u32 mix_buffer_count, s32 channel) { + s32 mix_buffer_count, s32 channel) { for (std::size_t i = 0; i < AudioCommon::MAX_BIQUAD_FILTERS; i++) { const auto& in_params = voice_info.GetInParams(); auto& biquad_filter = in_params.biquad_filter[i]; @@ -342,8 +335,8 @@ void CommandGenerator::GenerateDepopForMixBuffersCommand(std::size_t mix_buffer_ continue; } - depop_buffer[i] = ApplyMixDepop(GetMixBuffer(i), depop_buffer[i], delta, - static_cast(worker_params.sample_count)); + depop_buffer[i] = + ApplyMixDepop(GetMixBuffer(i), depop_buffer[i], delta, worker_params.sample_count); } } @@ -355,7 +348,7 @@ void CommandGenerator::GenerateEffectCommand(ServerMixInfo& mix_info) { if (index == AudioCommon::NO_EFFECT_ORDER) { break; } - auto* info = effect_context.GetInfo(static_cast(index)); + auto* info = effect_context.GetInfo(index); const auto type = info->GetType(); // TODO(ogniK): Finish remaining effects @@ -384,11 +377,11 @@ void CommandGenerator::GenerateI3dl2ReverbEffectCommand(s32 mix_buffer_offset, E } const auto& params = dynamic_cast(info)->GetParams(); const auto channel_count = params.channel_count; - for (size_t i = 0; i < channel_count; i++) { + for (s32 i = 0; i < channel_count; i++) { // TODO(ogniK): Actually implement reverb if (params.input[i] != params.output[i]) { - const auto* input = GetMixBuffer(static_cast(mix_buffer_offset + params.input[i])); - auto* output = GetMixBuffer(static_cast(mix_buffer_offset + params.output[i])); + const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); + auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); ApplyMix<1>(output, input, 32768, worker_params.sample_count); } } @@ -399,14 +392,13 @@ void CommandGenerator::GenerateBiquadFilterEffectCommand(s32 mix_buffer_offset, if (!enabled) { return; } - const auto& params = dynamic_cast(info)->GetParams(); - const auto channel_count = static_cast(params.channel_count); - for (size_t i = 0; i < channel_count; i++) { + const auto channel_count = params.channel_count; + for (s32 i = 0; i < channel_count; i++) { // TODO(ogniK): Actually implement biquad filter if (params.input[i] != params.output[i]) { - const auto* input = GetMixBuffer(static_cast(mix_buffer_offset + params.input[i])); - auto* output = GetMixBuffer(static_cast(mix_buffer_offset + params.output[i])); + const auto* input = GetMixBuffer(mix_buffer_offset + params.input[i]); + auto* output = GetMixBuffer(mix_buffer_offset + params.output[i]); ApplyMix<1>(output, input, 32768, worker_params.sample_count); } } @@ -433,30 +425,26 @@ void CommandGenerator::GenerateAuxCommand(s32 mix_buffer_offset, EffectBase* inf memory.ReadBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); memory.ReadBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); - WriteAuxBuffer(send_info, aux->GetSendBuffer(), - static_cast(params.sample_count), - GetMixBuffer(static_cast(input_index)), - worker_params.sample_count, offset, write_count); + WriteAuxBuffer(send_info, aux->GetSendBuffer(), params.sample_count, + GetMixBuffer(input_index), worker_params.sample_count, offset, + write_count); memory.WriteBlock(aux->GetSendInfo(), &send_info, sizeof(AuxInfoDSP)); const auto samples_read = ReadAuxBuffer( - recv_info, aux->GetRecvBuffer(), static_cast(params.sample_count), - GetMixBuffer(static_cast(output_index)), worker_params.sample_count, - offset, write_count); + recv_info, aux->GetRecvBuffer(), params.sample_count, + GetMixBuffer(output_index), worker_params.sample_count, offset, write_count); memory.WriteBlock(aux->GetRecvInfo(), &recv_info, sizeof(AuxInfoDSP)); if (samples_read != static_cast(worker_params.sample_count) && samples_read <= params.sample_count) { - std::memset(GetMixBuffer(static_cast(output_index)), 0, - static_cast(params.sample_count - samples_read)); + std::memset(GetMixBuffer(output_index), 0, params.sample_count - samples_read); } } else { AuxInfoDSP empty{}; memory.WriteBlock(aux->GetSendInfo(), &empty, sizeof(AuxInfoDSP)); memory.WriteBlock(aux->GetRecvInfo(), &empty, sizeof(AuxInfoDSP)); if (output_index != input_index) { - std::memcpy(GetMixBuffer(static_cast(output_index)), - GetMixBuffer(static_cast(input_index)), + std::memcpy(GetMixBuffer(output_index), GetMixBuffer(input_index), worker_params.sample_count * sizeof(s32)); } } @@ -470,8 +458,7 @@ ServerSplitterDestinationData* CommandGenerator::GetDestinationData(s32 splitter if (splitter_id == AudioCommon::NO_SPLITTER) { return nullptr; } - return splitter_context.GetDestinationData(static_cast(splitter_id), - static_cast(index)); + return splitter_context.GetDestinationData(splitter_id, index); } s32 CommandGenerator::WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u32 max_samples, @@ -501,7 +488,7 @@ s32 CommandGenerator::WriteAuxBuffer(AuxInfoDSP& dsp_info, VAddr send_buffer, u3 if (write_count != 0) { dsp_info.write_offset = (dsp_info.write_offset + write_count) % max_samples; } - return static_cast(sample_count); + return sample_count; } s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u32 max_samples, @@ -531,7 +518,7 @@ s32 CommandGenerator::ReadAuxBuffer(AuxInfoDSP& recv_info, VAddr recv_buffer, u3 if (read_count != 0) { recv_info.read_offset = (recv_info.read_offset + read_count) % max_samples; } - return static_cast(sample_count); + return sample_count; } void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float current_volume, @@ -550,15 +537,15 @@ void CommandGenerator::GenerateVolumeRampCommand(float last_volume, float curren } // Apply generic gain on samples ApplyGain(GetChannelMixBuffer(channel), GetChannelMixBuffer(channel), last, delta, - static_cast(worker_params.sample_count)); + worker_params.sample_count); } void CommandGenerator::GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, const MixVolumeBuffer& last_mix_volumes, - VoiceState& dsp_state, u32 mix_buffer_offset, - u32 mix_buffer_count, u32 voice_index, s32 node_id) { + VoiceState& dsp_state, s32 mix_buffer_offset, + s32 mix_buffer_count, s32 voice_index, s32 node_id) { // Loop all our mix buffers - for (size_t i = 0; i < mix_buffer_count; i++) { + for (s32 i = 0; i < mix_buffer_count; i++) { if (last_mix_volumes[i] != 0.0f || mix_volumes[i] != 0.0f) { const auto delta = static_cast((mix_volumes[i] - last_mix_volumes[i])) / static_cast(worker_params.sample_count); @@ -571,9 +558,9 @@ void CommandGenerator::GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volume mix_volumes[i]); } - dsp_state.previous_samples[i] = ApplyMixRamp( - GetMixBuffer(mix_buffer_offset + i), GetMixBuffer(voice_index), last_mix_volumes[i], - delta, static_cast(worker_params.sample_count)); + dsp_state.previous_samples[i] = + ApplyMixRamp(GetMixBuffer(mix_buffer_offset + i), GetMixBuffer(voice_index), + last_mix_volumes[i], delta, worker_params.sample_count); } else { dsp_state.previous_samples[i] = 0; } @@ -585,8 +572,7 @@ void CommandGenerator::GenerateSubMixCommand(ServerMixInfo& mix_info) { LOG_DEBUG(Audio, "(DSP_TRACE) GenerateSubMixCommand"); } const auto& in_params = mix_info.GetInParams(); - GenerateDepopForMixBuffersCommand(static_cast(in_params.buffer_count), - static_cast(in_params.buffer_offset), + GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, in_params.sample_rate); GenerateEffectCommand(mix_info); @@ -600,18 +586,18 @@ void CommandGenerator::GenerateMixCommands(ServerMixInfo& mix_info) { } const auto& in_params = mix_info.GetInParams(); if (in_params.dest_mix_id != AudioCommon::NO_MIX) { - const auto& dest_mix = mix_context.GetInfo(static_cast(in_params.dest_mix_id)); + const auto& dest_mix = mix_context.GetInfo(in_params.dest_mix_id); const auto& dest_in_params = dest_mix.GetInParams(); - const auto buffer_count = static_cast(in_params.buffer_count); + const auto buffer_count = in_params.buffer_count; - for (u32 i = 0; i < buffer_count; i++) { - for (u32 j = 0; j < static_cast(dest_in_params.buffer_count); j++) { + for (s32 i = 0; i < buffer_count; i++) { + for (s32 j = 0; j < dest_in_params.buffer_count; j++) { const auto mixed_volume = in_params.volume * in_params.mix_volume[i][j]; if (mixed_volume != 0.0f) { - GenerateMixCommand(static_cast(dest_in_params.buffer_offset) + j, - static_cast(in_params.buffer_offset) + i, - mixed_volume, static_cast(in_params.node_id)); + GenerateMixCommand(dest_in_params.buffer_offset + j, + in_params.buffer_offset + i, mixed_volume, + in_params.node_id); } } } @@ -622,17 +608,15 @@ void CommandGenerator::GenerateMixCommands(ServerMixInfo& mix_info) { continue; } - const auto& dest_mix = - mix_context.GetInfo(static_cast(destination_data->GetMixId())); + const auto& dest_mix = mix_context.GetInfo(destination_data->GetMixId()); const auto& dest_in_params = dest_mix.GetInParams(); const auto mix_index = (base - 1) % in_params.buffer_count + in_params.buffer_offset; for (std::size_t i = 0; i < static_cast(dest_in_params.buffer_count); i++) { const auto mixed_volume = in_params.volume * destination_data->GetMixVolume(i); if (mixed_volume != 0.0f) { - GenerateMixCommand(static_cast(dest_in_params.buffer_offset) + i, - static_cast(mix_index), mixed_volume, - static_cast(in_params.node_id)); + GenerateMixCommand(dest_in_params.buffer_offset + i, mix_index, mixed_volume, + in_params.node_id); } } } @@ -651,8 +635,7 @@ void CommandGenerator::GenerateMixCommand(std::size_t output_offset, std::size_t auto* output = GetMixBuffer(output_offset); const auto* input = GetMixBuffer(input_offset); - const auto gain = static_cast(volume * 32768.0f); - + const s32 gain = static_cast(volume * 32768.0f); // Mix with loop unrolling if (worker_params.sample_count % 4 == 0) { ApplyMix<4>(output, input, gain, worker_params.sample_count); @@ -670,8 +653,7 @@ void CommandGenerator::GenerateFinalMixCommand() { auto& mix_info = mix_context.GetFinalMixInfo(); const auto& in_params = mix_info.GetInParams(); - GenerateDepopForMixBuffersCommand(static_cast(in_params.buffer_count), - static_cast(in_params.buffer_offset), + GenerateDepopForMixBuffersCommand(in_params.buffer_count, in_params.buffer_offset, in_params.sample_rate); GenerateEffectCommand(mix_info); @@ -685,16 +667,16 @@ void CommandGenerator::GenerateFinalMixCommand() { in_params.node_id, in_params.buffer_offset + i, in_params.buffer_offset + i, in_params.volume); } - ApplyGainWithoutDelta(GetMixBuffer(static_cast(in_params.buffer_offset + i)), - GetMixBuffer(static_cast(in_params.buffer_offset + i)), gain, - static_cast(worker_params.sample_count)); + ApplyGainWithoutDelta(GetMixBuffer(in_params.buffer_offset + i), + GetMixBuffer(in_params.buffer_offset + i), gain, + worker_params.sample_count); } } s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, s32 channel, std::size_t mix_offset) { const auto& in_params = voice_info.GetInParams(); - const auto& wave_buffer = in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; if (wave_buffer.buffer_address == 0) { return 0; } @@ -707,26 +689,24 @@ s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_s const auto samples_remaining = (wave_buffer.end_sample_offset - wave_buffer.start_sample_offset) - dsp_state.offset; const auto start_offset = - static_cast((wave_buffer.start_sample_offset + dsp_state.offset) * - in_params.channel_count) * + ((wave_buffer.start_sample_offset + dsp_state.offset) * in_params.channel_count) * sizeof(s16); const auto buffer_pos = wave_buffer.buffer_address + start_offset; const auto samples_processed = std::min(sample_count, samples_remaining); if (in_params.channel_count == 1) { - std::vector buffer(static_cast(samples_processed)); + std::vector buffer(samples_processed); memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); for (std::size_t i = 0; i < buffer.size(); i++) { sample_buffer[mix_offset + i] = buffer[i]; } } else { const auto channel_count = in_params.channel_count; - std::vector buffer(static_cast(samples_processed * channel_count)); + std::vector buffer(samples_processed * channel_count); memory.ReadBlock(buffer_pos, buffer.data(), buffer.size() * sizeof(s16)); for (std::size_t i = 0; i < static_cast(samples_processed); i++) { - sample_buffer[mix_offset + i] = - buffer[i * static_cast(channel_count) + static_cast(channel)]; + sample_buffer[mix_offset + i] = buffer[i * channel_count + channel]; } } @@ -736,7 +716,7 @@ s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_s s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 sample_count, s32 channel, std::size_t mix_offset) { const auto& in_params = voice_info.GetInParams(); - const auto& wave_buffer = in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; if (wave_buffer.buffer_address == 0) { return 0; } @@ -756,7 +736,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s constexpr std::size_t SAMPLES_PER_FRAME = 14; auto frame_header = dsp_state.context.header; - auto idx = static_cast((frame_header >> 4) & 0xf); + s32 idx = (frame_header >> 4) & 0xf; s32 scale = frame_header & 0xf; s16 yn1 = dsp_state.context.yn1; s16 yn2 = dsp_state.context.yn2; @@ -773,10 +753,9 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s const auto samples_processed = std::min(sample_count, samples_remaining); const auto sample_pos = wave_buffer.start_sample_offset + dsp_state.offset; - const auto samples_remaining_in_frame = static_cast(sample_pos) % SAMPLES_PER_FRAME; - auto position_in_frame = - ((static_cast(sample_pos) / SAMPLES_PER_FRAME) * NIBBLES_PER_SAMPLE) + - samples_remaining_in_frame + (samples_remaining_in_frame != 0 ? 2 : 0); + const auto samples_remaining_in_frame = sample_pos % SAMPLES_PER_FRAME; + auto position_in_frame = ((sample_pos / SAMPLES_PER_FRAME) * NIBBLES_PER_SAMPLE) + + samples_remaining_in_frame + (samples_remaining_in_frame != 0 ? 2 : 0); const auto decode_sample = [&](const int nibble) -> s16 { const int xn = nibble * (1 << scale); @@ -795,7 +774,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s std::size_t buffer_offset{}; std::vector buffer( - std::max((static_cast(samples_processed) / FRAME_LEN) * SAMPLES_PER_FRAME, FRAME_LEN)); + std::max((samples_processed / FRAME_LEN) * SAMPLES_PER_FRAME, FRAME_LEN)); memory.ReadBlock(wave_buffer.buffer_address + (position_in_frame / 2), buffer.data(), buffer.size()); std::size_t cur_mix_offset = mix_offset; @@ -805,7 +784,7 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s if (position_in_frame % NIBBLES_PER_SAMPLE == 0) { // Read header frame_header = buffer[buffer_offset++]; - idx = static_cast((frame_header >> 4) & 0xf); + idx = (frame_header >> 4) & 0xf; scale = frame_header & 0xf; coef1 = coeffs[idx * 2]; coef2 = coeffs[idx * 2 + 1]; @@ -815,8 +794,8 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s if (remaining_samples >= static_cast(SAMPLES_PER_FRAME)) { for (std::size_t i = 0; i < SAMPLES_PER_FRAME / 2; i++) { // Sample 1 - const s32 s0 = SIGNED_NIBBLES[static_cast(buffer[buffer_offset] >> 4)]; - const s32 s1 = SIGNED_NIBBLES[static_cast(buffer[buffer_offset++] & 0xf)]; + const s32 s0 = SIGNED_NIBBLES[buffer[buffer_offset] >> 4]; + const s32 s1 = SIGNED_NIBBLES[buffer[buffer_offset++] & 0xf]; const s16 sample_1 = decode_sample(s0); const s16 sample_2 = decode_sample(s1); sample_buffer[cur_mix_offset++] = sample_1; @@ -828,14 +807,14 @@ s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_s } } // Decode mid frame - auto current_nibble = static_cast(buffer[buffer_offset]); - if ((position_in_frame++ & 1) != 0) { + s32 current_nibble = buffer[buffer_offset]; + if (position_in_frame++ & 0x1) { current_nibble &= 0xf; buffer_offset++; } else { current_nibble >>= 4; } - const s16 sample = decode_sample(SIGNED_NIBBLES[static_cast(current_nibble)]); + const s16 sample = decode_sample(SIGNED_NIBBLES[current_nibble]); sample_buffer[cur_mix_offset++] = sample; remaining_samples--; } @@ -856,7 +835,7 @@ const s32* CommandGenerator::GetMixBuffer(std::size_t index) const { } std::size_t CommandGenerator::GetMixChannelBufferOffset(s32 channel) const { - return worker_params.mix_buffer_count + static_cast(channel); + return worker_params.mix_buffer_count + channel; } std::size_t CommandGenerator::GetTotalMixBufferCount() const { @@ -864,11 +843,11 @@ std::size_t CommandGenerator::GetTotalMixBufferCount() const { } s32* CommandGenerator::GetChannelMixBuffer(s32 channel) { - return GetMixBuffer(worker_params.mix_buffer_count + static_cast(channel)); + return GetMixBuffer(worker_params.mix_buffer_count + channel); } const s32* CommandGenerator::GetChannelMixBuffer(s32 channel) const { - return GetMixBuffer(worker_params.mix_buffer_count + static_cast(channel)); + return GetMixBuffer(worker_params.mix_buffer_count + channel); } void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* output, @@ -916,10 +895,9 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o s32 samples_read{}; while (samples_read < samples_to_read) { - const auto& wave_buffer = - in_params.wave_buffer[static_cast(dsp_state.wave_buffer_index)]; + const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; // No more data can be read - if (!dsp_state.is_wave_buffer_valid[static_cast(dsp_state.wave_buffer_index)]) { + if (!dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index]) { is_buffer_completed = true; break; } @@ -943,7 +921,7 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o UNREACHABLE_MSG("Unimplemented sample format={}", in_params.sample_format); } - temp_mix_offset += static_cast(samples_decoded); + temp_mix_offset += samples_decoded; samples_read += samples_decoded; dsp_state.offset += samples_decoded; dsp_state.played_sample_count += samples_decoded; @@ -966,12 +944,10 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o } else { // Update our wave buffer states - dsp_state.is_wave_buffer_valid[static_cast(dsp_state.wave_buffer_index)] = - false; + dsp_state.is_wave_buffer_valid[dsp_state.wave_buffer_index] = false; dsp_state.wave_buffer_consumed++; dsp_state.wave_buffer_index = - static_cast(dsp_state.wave_buffer_index + 1) % - AudioCommon::MAX_WAVE_BUFFERS; + (dsp_state.wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; if (wave_buffer.end_of_stream) { dsp_state.played_sample_count = 0; } @@ -981,20 +957,16 @@ void CommandGenerator::DecodeFromWaveBuffers(ServerVoiceInfo& voice_info, s32* o if (in_params.behavior_flags.is_pitch_and_src_skipped.Value()) { // No need to resample - std::memcpy(output, sample_buffer.data(), - static_cast(samples_read) * sizeof(s32)); + std::memcpy(output, sample_buffer.data(), samples_read * sizeof(s32)); } else { - { - const auto begin = sample_buffer.begin() + static_cast(temp_mix_offset); - const auto end = begin + (samples_to_read - samples_read); - std::fill(begin, end, 0); - } + std::fill(sample_buffer.begin() + temp_mix_offset, + sample_buffer.begin() + temp_mix_offset + (samples_to_read - samples_read), + 0); AudioCore::Resample(output, sample_buffer.data(), resample_rate, dsp_state.fraction, - static_cast(samples_to_output)); + samples_to_output); // Resample for (std::size_t i = 0; i < AudioCommon::MAX_SAMPLE_HISTORY; i++) { - dsp_state.sample_history[i] = - sample_buffer[static_cast(samples_to_read) + i]; + dsp_state.sample_history[i] = sample_buffer[samples_to_read + i]; } } output += samples_to_output; diff --git a/src/audio_core/command_generator.h b/src/audio_core/command_generator.h index 6cba70ae3..53e57748b 100644 --- a/src/audio_core/command_generator.h +++ b/src/audio_core/command_generator.h @@ -50,12 +50,12 @@ public: private: void GenerateDataSourceCommand(ServerVoiceInfo& voice_info, VoiceState& dsp_state, s32 channel); void GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - u32 mix_buffer_count, s32 channel); + s32 mix_buffer_count, s32 channel); void GenerateVolumeRampCommand(float last_volume, float current_volume, s32 channel, s32 node_id); void GenerateVoiceMixCommand(const MixVolumeBuffer& mix_volumes, const MixVolumeBuffer& last_mix_volumes, VoiceState& dsp_state, - u32 mix_buffer_offset, u32 mix_buffer_count, u32 voice_index, + s32 mix_buffer_offset, s32 mix_buffer_count, s32 voice_index, s32 node_id); void GenerateSubMixCommand(ServerMixInfo& mix_info); void GenerateMixCommands(ServerMixInfo& mix_info); diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index a20b6ad5f..6eaa60815 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp @@ -202,7 +202,7 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const } const std::size_t num_channels = impl->GetNumChannels(); - const std::size_t samples_to_write = num_channels * static_cast(num_frames); + const std::size_t samples_to_write = num_channels * num_frames; std::size_t samples_written; /* diff --git a/src/audio_core/cubeb_sink.h b/src/audio_core/cubeb_sink.h index c50d0b7bd..7ce850f47 100644 --- a/src/audio_core/cubeb_sink.h +++ b/src/audio_core/cubeb_sink.h @@ -27,7 +27,7 @@ private: std::vector sink_streams; #ifdef _WIN32 - s32 com_init_result = 0; + u32 com_init_result = 0; #endif }; diff --git a/src/audio_core/info_updater.cpp b/src/audio_core/info_updater.cpp index f999a8b17..2940e53a9 100644 --- a/src/audio_core/info_updater.cpp +++ b/src/audio_core/info_updater.cpp @@ -350,7 +350,7 @@ ResultCode InfoUpdater::UpdateMixes(MixContext& mix_context, std::size_t mix_buf std::size_t total_buffer_count{}; for (std::size_t i = 0; i < mix_count; i++) { const auto& in = mix_in_params[i]; - total_buffer_count += static_cast(in.buffer_count); + total_buffer_count += in.buffer_count; if (static_cast(in.dest_mix_id) > mix_count && in.dest_mix_id != AudioCommon::NO_MIX && in.mix_id != AudioCommon::FINAL_MIX) { LOG_ERROR( @@ -379,7 +379,7 @@ ResultCode InfoUpdater::UpdateMixes(MixContext& mix_context, std::size_t mix_buf const auto& mix_in = mix_in_params[i]; std::size_t target_mix{}; if (behavior_info.IsMixInParameterDirtyOnlyUpdateSupported()) { - target_mix = static_cast(mix_in.mix_id); + target_mix = mix_in.mix_id; } else { // Non dirty supported games just use i instead of the actual mix_id target_mix = i; diff --git a/src/audio_core/mix_context.cpp b/src/audio_core/mix_context.cpp index c28bee453..4bca72eb0 100644 --- a/src/audio_core/mix_context.cpp +++ b/src/audio_core/mix_context.cpp @@ -62,7 +62,7 @@ void MixContext::UpdateDistancesFromFinalMix() { distance_to_final_mix = AudioCommon::NO_FINAL_MIX; break; } else { - const auto& dest_mix = GetInfo(static_cast(mix_id)); + const auto& dest_mix = GetInfo(mix_id); const auto dest_mix_distance = dest_mix.GetInParams().final_mix_distance; if (dest_mix_distance == AudioCommon::NO_FINAL_MIX) { @@ -129,7 +129,7 @@ bool MixContext::TsortInfo(SplitterContext& splitter_context) { std::size_t info_id{}; for (auto itr = sorted_list.rbegin(); itr != sorted_list.rend(); ++itr) { // Set our sorted info - sorted_info[info_id++] = &GetInfo(static_cast(*itr)); + sorted_info[info_id++] = &GetInfo(*itr); } // Calculate the mix buffer offset @@ -218,8 +218,7 @@ bool ServerMixInfo::Update(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix for (std::size_t i = 0; i < effect_count; i++) { auto* effect_info = effect_context.GetInfo(i); if (effect_info->GetMixID() == in_params.mix_id) { - const auto processing_order = static_cast(effect_info->GetProcessingOrder()); - effect_processing_order[processing_order] = static_cast(i); + effect_processing_order[effect_info->GetProcessingOrder()] = static_cast(i); } } @@ -266,7 +265,7 @@ bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InP if (in_params.dest_mix_id == mix_in.dest_mix_id && in_params.splitter_id == mix_in.splitter_id && ((in_params.splitter_id == AudioCommon::NO_SPLITTER) || - !splitter_context.GetInfo(static_cast(in_params.splitter_id)).HasNewConnection())) { + !splitter_context.GetInfo(in_params.splitter_id).HasNewConnection())) { return false; } // Remove current edges for mix id @@ -276,11 +275,11 @@ bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InP edge_matrix.Connect(in_params.mix_id, mix_in.dest_mix_id); } else if (mix_in.splitter_id != AudioCommon::NO_SPLITTER) { // Recurse our splitter linked and set our edges - auto& splitter_info = splitter_context.GetInfo(static_cast(mix_in.splitter_id)); - const auto length = static_cast(splitter_info.GetLength()); - for (size_t i = 0; i < length; i++) { + auto& splitter_info = splitter_context.GetInfo(mix_in.splitter_id); + const auto length = splitter_info.GetLength(); + for (s32 i = 0; i < length; i++) { const auto* splitter_destination = - splitter_context.GetDestinationData(static_cast(mix_in.splitter_id), i); + splitter_context.GetDestinationData(mix_in.splitter_id, i); if (splitter_destination == nullptr) { continue; } diff --git a/src/audio_core/sink_context.cpp b/src/audio_core/sink_context.cpp index 3d713814a..0882b411a 100644 --- a/src/audio_core/sink_context.cpp +++ b/src/audio_core/sink_context.cpp @@ -23,9 +23,8 @@ bool SinkContext::InUse() const { } std::vector SinkContext::OutputBuffers() const { - const auto output_use_count = static_cast(use_count); - std::vector buffer_ret(output_use_count); - std::memcpy(buffer_ret.data(), buffers.data(), output_use_count); + std::vector buffer_ret(use_count); + std::memcpy(buffer_ret.data(), buffers.data(), use_count); return buffer_ret; } diff --git a/src/audio_core/splitter_context.cpp b/src/audio_core/splitter_context.cpp index f3e870648..f21b53147 100644 --- a/src/audio_core/splitter_context.cpp +++ b/src/audio_core/splitter_context.cpp @@ -109,7 +109,7 @@ std::size_t ServerSplitterInfo::Update(SplitterInfo::InInfoPrams& header) { new_connection = true; // We need to update the size here due to the splitter bug being present and providing an // incorrect size. We're suppose to also update the header here but we just ignore and continue - return (sizeof(s32_le) * static_cast(header.length - 1)) + (sizeof(s32_le) * 3); + return (sizeof(s32_le) * (header.length - 1)) + (sizeof(s32_le) * 3); } ServerSplitterDestinationData* ServerSplitterInfo::GetHead() { @@ -306,14 +306,13 @@ bool SplitterContext::UpdateInfo(const std::vector& input, std::size_t& inpu break; } - const auto send_id = static_cast(header.send_id); - if (header.send_id < 0 || send_id > info_count) { + if (header.send_id < 0 || static_cast(header.send_id) > info_count) { LOG_ERROR(Audio, "Bad splitter data id"); break; } UpdateOffsets(sizeof(SplitterInfo::InInfoPrams)); - auto& info = GetInfo(send_id); + auto& info = GetInfo(header.send_id); if (!RecomposeDestination(info, header, input, input_offset)) { LOG_ERROR(Audio, "Failed to recompose destination for splitter!"); return false; @@ -349,12 +348,11 @@ bool SplitterContext::UpdateData(const std::vector& input, std::size_t& inpu break; } - const auto splitter_id = static_cast(header.splitter_id); - if (header.splitter_id < 0 || splitter_id > data_count) { + if (header.splitter_id < 0 || static_cast(header.splitter_id) > data_count) { LOG_ERROR(Audio, "Bad splitter data id"); break; } - GetData(splitter_id).Update(header); + GetData(header.splitter_id).Update(header); } return true; } @@ -388,9 +386,9 @@ bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, return true; } - auto* start_head = &GetData(static_cast(header.resource_id_base)); + auto* start_head = &GetData(header.resource_id_base); current_head = start_head; - std::vector resource_ids(static_cast(size - 1)); + std::vector resource_ids(size - 1); if (!AudioCommon::CanConsumeBuffer(input.size(), input_offset, resource_ids.size() * sizeof(s32_le))) { LOG_ERROR(Audio, "Buffer is an invalid size!"); @@ -399,8 +397,8 @@ bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, std::memcpy(resource_ids.data(), input.data() + input_offset, resource_ids.size() * sizeof(s32_le)); - for (const auto resource_id : resource_ids) { - auto* head = &GetData(static_cast(resource_id)); + for (auto resource_id : resource_ids) { + auto* head = &GetData(resource_id); current_head->SetNextDestination(head); current_head = head; } @@ -446,7 +444,7 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { const auto node_id = static_cast(i); // If we don't have a state, send to our index stack for work - if (GetState(i) == State::NoState) { + if (GetState(i) == NodeStates::State::NoState) { index_stack.push(node_id); } @@ -455,19 +453,19 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { // Get the current node const auto current_stack_index = index_stack.top(); // Check if we've seen the node yet - const auto index_state = GetState(static_cast(current_stack_index)); - if (index_state == State::NoState) { + const auto index_state = GetState(current_stack_index); + if (index_state == NodeStates::State::NoState) { // Mark the node as seen - UpdateState(State::InFound, static_cast(current_stack_index)); - } else if (index_state == State::InFound) { + UpdateState(NodeStates::State::InFound, current_stack_index); + } else if (index_state == NodeStates::State::InFound) { // We've seen this node before, mark it as completed - UpdateState(State::InCompleted, static_cast(current_stack_index)); + UpdateState(NodeStates::State::InCompleted, current_stack_index); // Update our index list PushTsortResult(current_stack_index); // Pop the stack index_stack.pop(); continue; - } else if (index_state == State::InCompleted) { + } else if (index_state == NodeStates::State::InCompleted) { // If our node is already sorted, clear it index_stack.pop(); continue; @@ -481,11 +479,11 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { } // Check if our node exists - const auto node_state = GetState(static_cast(j)); - if (node_state == State::NoState) { + const auto node_state = GetState(j); + if (node_state == NodeStates::State::NoState) { // Add more work index_stack.push(j); - } else if (node_state == State::InFound) { + } else if (node_state == NodeStates::State::InFound) { UNREACHABLE_MSG("Node start marked as found"); ResetState(); return false; @@ -509,17 +507,17 @@ void NodeStates::ResetState() { } } -void NodeStates::UpdateState(State state, std::size_t i) { +void NodeStates::UpdateState(NodeStates::State state, std::size_t i) { switch (state) { - case State::NoState: + case NodeStates::State::NoState: was_node_found[i] = false; was_node_completed[i] = false; break; - case State::InFound: + case NodeStates::State::InFound: was_node_found[i] = true; was_node_completed[i] = false; break; - case State::InCompleted: + case NodeStates::State::InCompleted: was_node_found[i] = false; was_node_completed[i] = true; break; @@ -530,13 +528,13 @@ NodeStates::State NodeStates::GetState(std::size_t i) { ASSERT(i < node_count); if (was_node_found[i]) { // If our node exists in our found list - return State::InFound; + return NodeStates::State::InFound; } else if (was_node_completed[i]) { // If node is in the completed list - return State::InCompleted; + return NodeStates::State::InCompleted; } else { // If in neither - return State::NoState; + return NodeStates::State::NoState; } } @@ -603,16 +601,16 @@ std::size_t EdgeMatrix::GetNodeCount() const { void EdgeMatrix::SetState(s32 a, s32 b, bool state) { ASSERT(InRange(a, b)); - edge_matrix.at(static_cast(a) * node_count + static_cast(b)) = state; + edge_matrix.at(a * node_count + b) = state; } bool EdgeMatrix::GetState(s32 a, s32 b) { ASSERT(InRange(a, b)); - return edge_matrix.at(static_cast(a) * node_count + static_cast(b)); + return edge_matrix.at(a * node_count + b); } bool EdgeMatrix::InRange(s32 a, s32 b) const { - const std::size_t pos = static_cast(a) * node_count + static_cast(b); + const std::size_t pos = a * node_count + b; return pos < (node_count * node_count); } diff --git a/src/audio_core/time_stretch.h b/src/audio_core/time_stretch.h index 3808e554d..bb2270b96 100644 --- a/src/audio_core/time_stretch.h +++ b/src/audio_core/time_stretch.h @@ -5,16 +5,8 @@ #pragma once #include -#include "common/common_types.h" - -#if defined(__GNUC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wsign-conversion" -#endif #include -#if defined(__GNUC__) -#pragma GCC diagnostic pop -#endif +#include "common/common_types.h" namespace AudioCore { diff --git a/src/audio_core/voice_context.cpp b/src/audio_core/voice_context.cpp index 276b96ca4..c46ee55f1 100644 --- a/src/audio_core/voice_context.cpp +++ b/src/audio_core/voice_context.cpp @@ -98,7 +98,7 @@ void ServerVoiceInfo::UpdateParameters(const VoiceInfo::InParams& voice_in, BehaviorInfo& behavior_info) { in_params.in_use = voice_in.is_in_use; in_params.id = voice_in.id; - in_params.node_id = static_cast(voice_in.node_id); + in_params.node_id = voice_in.node_id; in_params.last_playstate = in_params.current_playstate; switch (voice_in.play_state) { case PlayState::Paused: @@ -220,10 +220,8 @@ void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, if (sample_format == SampleFormat::Pcm16) { const auto buffer_size = in_wave_buffer.buffer_size; if (in_wave_buffer.start_sample_offset < 0 || in_wave_buffer.end_sample_offset < 0 || - (buffer_size < - (sizeof(s16) * static_cast(in_wave_buffer.start_sample_offset))) || - (buffer_size < - (sizeof(s16) * static_cast(in_wave_buffer.end_sample_offset)))) { + (buffer_size < (sizeof(s16) * in_wave_buffer.start_sample_offset)) || + (buffer_size < (sizeof(s16) * in_wave_buffer.end_sample_offset))) { // TODO(ogniK): Write error info return; } @@ -256,8 +254,8 @@ void ServerVoiceInfo::WriteOutStatus( voice_out.played_sample_count = 0; voice_out.voice_dropped = false; } else if (!in_params.is_new) { - voice_out.wave_buffer_consumed = static_cast(voice_states[0]->wave_buffer_consumed); - voice_out.played_sample_count = static_cast(voice_states[0]->played_sample_count); + voice_out.wave_buffer_consumed = voice_states[0]->wave_buffer_consumed; + voice_out.played_sample_count = voice_states[0]->played_sample_count; voice_out.voice_dropped = in_params.voice_drop_flag; } else { voice_out.wave_buffer_consumed = 0; @@ -295,8 +293,8 @@ bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) { in_params.is_new = false; } - const auto channel_count = static_cast(in_params.channel_count); - for (size_t i = 0; i < channel_count; i++) { + const s32 channel_count = in_params.channel_count; + for (s32 i = 0; i < channel_count; i++) { const auto channel_resource = in_params.voice_channel_resource_id[i]; dsp_voice_states[i] = &voice_context.GetDspSharedState(static_cast(channel_resource)); @@ -305,9 +303,8 @@ bool ServerVoiceInfo::UpdateForCommandGeneration(VoiceContext& voice_context) { } void ServerVoiceInfo::ResetResources(VoiceContext& voice_context) { - const auto channel_count = static_cast(in_params.channel_count); - - for (size_t i = 0; i < channel_count; i++) { + const s32 channel_count = in_params.channel_count; + for (s32 i = 0; i < channel_count; i++) { const auto channel_resource = in_params.voice_channel_resource_id[i]; auto& dsp_state = voice_context.GetDspSharedState(static_cast(channel_resource)); @@ -328,9 +325,9 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( switch (in_params.current_playstate) { case ServerPlayState::Play: { - for (size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { + for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { if (!in_params.wave_buffer[i].sent_to_dsp) { - for (size_t channel = 0; channel < static_cast(channel_count); channel++) { + for (s32 channel = 0; channel < channel_count; channel++) { dsp_voice_states[channel]->is_wave_buffer_valid[i] = true; } in_params.wave_buffer[i].sent_to_dsp = true; @@ -347,13 +344,12 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( case ServerPlayState::RequestStop: { for (std::size_t i = 0; i < AudioCommon::MAX_WAVE_BUFFERS; i++) { in_params.wave_buffer[i].sent_to_dsp = true; - for (std::size_t channel = 0; channel < static_cast(channel_count); channel++) { + for (s32 channel = 0; channel < channel_count; channel++) { auto* dsp_state = dsp_voice_states[channel]; if (dsp_state->is_wave_buffer_valid[i]) { dsp_state->wave_buffer_index = - static_cast(static_cast(dsp_state->wave_buffer_index + 1) % - AudioCommon::MAX_WAVE_BUFFERS); + (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; dsp_state->wave_buffer_consumed++; } @@ -361,7 +357,7 @@ bool ServerVoiceInfo::UpdateParametersForCommandGeneration( } } - for (size_t channel = 0; channel < static_cast(channel_count); channel++) { + for (s32 channel = 0; channel < channel_count; channel++) { auto* dsp_state = dsp_voice_states[channel]; dsp_state->offset = 0; dsp_state->played_sample_count = 0; @@ -387,16 +383,15 @@ void ServerVoiceInfo::FlushWaveBuffers( auto wave_head = in_params.wave_bufffer_head; for (u8 i = 0; i < flush_count; i++) { - in_params.wave_buffer[static_cast(wave_head)].sent_to_dsp = true; - for (size_t channel = 0; channel < static_cast(channel_count); channel++) { + in_params.wave_buffer[wave_head].sent_to_dsp = true; + for (s32 channel = 0; channel < channel_count; channel++) { auto* dsp_state = dsp_voice_states[channel]; dsp_state->wave_buffer_consumed++; - dsp_state->is_wave_buffer_valid[static_cast(wave_head)] = false; - dsp_state->wave_buffer_index = static_cast( - static_cast(dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS); + dsp_state->is_wave_buffer_valid[wave_head] = false; + dsp_state->wave_buffer_index = + (dsp_state->wave_buffer_index + 1) % AudioCommon::MAX_WAVE_BUFFERS; } - wave_head = - static_cast(static_cast(wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS); + wave_head = (wave_head + 1) % AudioCommon::MAX_WAVE_BUFFERS; } } @@ -488,7 +483,7 @@ s32 VoiceContext::DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, const auto samples_remaining = (wave_buffer->end_sample_offset - wave_buffer->start_sample_offset) - buffer_offset; const auto start_offset = (wave_buffer->start_sample_offset + buffer_offset) * channel_count; - const auto buffer_pos = wave_buffer->buffer_address + static_cast(start_offset); + const auto buffer_pos = wave_buffer->buffer_address + start_offset; s16* buffer_data = reinterpret_cast(memory.GetPointer(buffer_pos)); diff --git a/src/common/fiber.h b/src/common/fiber.h index bc1db1582..89dde5e36 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = delete; - Fiber& operator=(Fiber&&) = delete; + Fiber(Fiber&&) = default; + Fiber& operator=(Fiber&&) = default; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/file_util.h b/src/common/file_util.h index 508b7a10a..8b587320f 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -189,8 +189,7 @@ template return {}; } last = std::min(last, vector.size()); - return std::vector(vector.begin() + static_cast(first), - vector.begin() + static_cast(first + last)); + return std::vector(vector.begin() + first, vector.begin() + first + last); } enum class DirectorySeparator { diff --git a/src/common/math_util.h b/src/common/math_util.h index 4c38d8040..7cec80d57 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return static_cast(std::abs(static_cast>(right - left))); + return std::abs(static_cast>(right - left)); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return static_cast(std::abs(static_cast>(bottom - top))); + return std::abs(static_cast>(bottom - top)); } } diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h index 71613f18b..4b305bf40 100644 --- a/src/common/multi_level_queue.h +++ b/src/common/multi_level_queue.h @@ -320,7 +320,7 @@ private: } const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, static_cast(shift)); + const auto end_range = std::next(begin_range, shift); list.splice(list.end(), list, begin_range, end_range); } diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 06ac2f5bb..4f946a258 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,14 +15,6 @@ namespace Common { */ class SpinLock { public: - SpinLock() = default; - - SpinLock(const SpinLock&) = delete; - SpinLock& operator=(const SpinLock&) = delete; - - SpinLock(SpinLock&&) = delete; - SpinLock& operator=(SpinLock&&) = delete; - void lock(); void unlock(); [[nodiscard]] bool try_lock(); diff --git a/src/common/swap.h b/src/common/swap.h index 8c68c1f26..7665942a2 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -504,35 +504,35 @@ bool operator==(const S& p, const swap_struct_t v) { template struct swap_64_t { static T swap(T x) { - return static_cast(Common::swap64(static_cast(x))); + return static_cast(Common::swap64(x)); } }; template struct swap_32_t { static T swap(T x) { - return static_cast(Common::swap32(static_cast(x))); + return static_cast(Common::swap32(x)); } }; template struct swap_16_t { static T swap(T x) { - return static_cast(Common::swap16(static_cast(x))); + return static_cast(Common::swap16(x)); } }; template struct swap_float_t { static T swap(T x) { - return static_cast(Common::swapf(static_cast(x))); + return static_cast(Common::swapf(x)); } }; template struct swap_double_t { static T swap(T x) { - return static_cast(Common::swapd(static_cast(x))); + return static_cast(Common::swapd(x)); } }; diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h index 69c9193da..def9e5d8d 100644 --- a/src/common/thread_queue_list.h +++ b/src/common/thread_queue_list.h @@ -33,7 +33,7 @@ struct ThreadQueueList { } } - return static_cast(-1); + return -1; } [[nodiscard]] T get_first() const { @@ -156,7 +156,7 @@ private: void link(Priority priority) { Queue* cur = &queues[priority]; - for (auto i = static_cast(priority - 1); i >= 0; --i) { + for (int i = priority - 1; i >= 0; --i) { if (queues[i].next_nonempty != UnlinkedTag()) { cur->next_nonempty = queues[i].next_nonempty; queues[i].next_nonempty = cur; diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 9dc320f53..b6dc25f6b 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -630,9 +630,8 @@ else() -Werror=implicit-fallthrough -Werror=reorder -Werror=sign-compare - -Werror=sign-conversion - $<$:-Werror=unused-but-set-parameter> - $<$:-Werror=unused-but-set-variable> + -Werror=unused-but-set-parameter + -Werror=unused-but-set-variable -Werror=unused-variable ) endif() diff --git a/src/core/arm/arm_interface.cpp b/src/core/arm/arm_interface.cpp index adc6aa5c5..d2295ed90 100644 --- a/src/core/arm/arm_interface.cpp +++ b/src/core/arm/arm_interface.cpp @@ -147,18 +147,10 @@ std::vector ARM_Interface::GetBacktraceFromContex auto fp = ctx.cpu_registers[29]; auto lr = ctx.cpu_registers[30]; while (true) { - out.push_back({ - .module = "", - .address = 0, - .original_address = lr, - .offset = 0, - .name = "", - }); - - if (fp == 0) { + out.push_back({"", 0, lr, 0}); + if (!fp) { break; } - lr = memory.Read64(fp + 8) - 4; fp = memory.Read64(fp); } @@ -211,18 +203,10 @@ std::vector ARM_Interface::GetBacktrace() const { auto fp = GetReg(29); auto lr = GetReg(30); while (true) { - out.push_back({ - .module = "", - .address = 0, - .original_address = lr, - .offset = 0, - .name = "", - }); - - if (fp == 0) { + out.push_back({"", 0, lr, 0, ""}); + if (!fp) { break; } - lr = memory.Read64(fp + 8) - 4; fp = memory.Read64(fp); } diff --git a/src/core/arm/arm_interface.h b/src/core/arm/arm_interface.h index 9b86247e2..1f24051e4 100644 --- a/src/core/arm/arm_interface.h +++ b/src/core/arm/arm_interface.h @@ -93,14 +93,14 @@ public: * @param index Register index * @return Returns the value in the register */ - virtual u64 GetReg(std::size_t index) const = 0; + virtual u64 GetReg(int index) const = 0; /** * Set an ARM register * @param index Register index * @param value Value to set register to */ - virtual void SetReg(std::size_t index, u64 value) = 0; + virtual void SetReg(int index, u64 value) = 0; /** * Gets the value of a specified vector register. @@ -108,7 +108,7 @@ public: * @param index The index of the vector register. * @return the value within the vector register. */ - virtual u128 GetVectorReg(std::size_t index) const = 0; + virtual u128 GetVectorReg(int index) const = 0; /** * Sets a given value into a vector register. @@ -116,7 +116,7 @@ public: * @param index The index of the vector register. * @param value The new value to place in the register. */ - virtual void SetVectorReg(std::size_t index, u128 value) = 0; + virtual void SetVectorReg(int index, u128 value) = 0; /** * Get the current PSTATE register diff --git a/src/core/arm/cpu_interrupt_handler.h b/src/core/arm/cpu_interrupt_handler.h index c20c280f1..71e582f79 100644 --- a/src/core/arm/cpu_interrupt_handler.h +++ b/src/core/arm/cpu_interrupt_handler.h @@ -21,8 +21,8 @@ public: CPUInterruptHandler(const CPUInterruptHandler&) = delete; CPUInterruptHandler& operator=(const CPUInterruptHandler&) = delete; - CPUInterruptHandler(CPUInterruptHandler&&) = delete; - CPUInterruptHandler& operator=(CPUInterruptHandler&&) = delete; + CPUInterruptHandler(CPUInterruptHandler&&) = default; + CPUInterruptHandler& operator=(CPUInterruptHandler&&) = default; bool IsInterrupted() const { return is_interrupted; diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.cpp b/src/core/arm/dynarmic/arm_dynarmic_32.cpp index fab694fc2..b5f28a86e 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_32.cpp @@ -111,7 +111,7 @@ public: } return 0U; } - return static_cast(std::max(parent.system.CoreTiming().GetDowncount(), 0)); + return std::max(parent.system.CoreTiming().GetDowncount(), 0); } ARM_Dynarmic_32& parent; @@ -210,19 +210,19 @@ u64 ARM_Dynarmic_32::GetPC() const { return jit->Regs()[15]; } -u64 ARM_Dynarmic_32::GetReg(std::size_t index) const { +u64 ARM_Dynarmic_32::GetReg(int index) const { return jit->Regs()[index]; } -void ARM_Dynarmic_32::SetReg(std::size_t index, u64 value) { +void ARM_Dynarmic_32::SetReg(int index, u64 value) { jit->Regs()[index] = static_cast(value); } -u128 ARM_Dynarmic_32::GetVectorReg(std::size_t index) const { +u128 ARM_Dynarmic_32::GetVectorReg(int index) const { return {}; } -void ARM_Dynarmic_32::SetVectorReg(std::size_t index, u128 value) {} +void ARM_Dynarmic_32::SetVectorReg(int index, u128 value) {} u32 ARM_Dynarmic_32::GetPSTATE() const { return jit->Cpsr(); diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.h b/src/core/arm/dynarmic/arm_dynarmic_32.h index ba646c623..2bab31b92 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.h +++ b/src/core/arm/dynarmic/arm_dynarmic_32.h @@ -35,10 +35,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(std::size_t index) const override; - void SetReg(std::size_t index, u64 value) override; - u128 GetVectorReg(std::size_t index) const override; - void SetVectorReg(std::size_t index, u128 value) override; + u64 GetReg(int index) const override; + void SetReg(int index, u64 value) override; + u128 GetVectorReg(int index) const override; + void SetVectorReg(int index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; void Run() override; diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.cpp b/src/core/arm/dynarmic/arm_dynarmic_64.cpp index a2c4c2f30..ce9968724 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_64.cpp @@ -148,7 +148,7 @@ public: } return 0U; } - return static_cast(std::max(parent.system.CoreTiming().GetDowncount(), 0)); + return std::max(parent.system.CoreTiming().GetDowncount(), 0); } u64 GetCNTPCT() override { @@ -265,19 +265,19 @@ u64 ARM_Dynarmic_64::GetPC() const { return jit->GetPC(); } -u64 ARM_Dynarmic_64::GetReg(std::size_t index) const { +u64 ARM_Dynarmic_64::GetReg(int index) const { return jit->GetRegister(index); } -void ARM_Dynarmic_64::SetReg(std::size_t index, u64 value) { +void ARM_Dynarmic_64::SetReg(int index, u64 value) { jit->SetRegister(index, value); } -u128 ARM_Dynarmic_64::GetVectorReg(std::size_t index) const { +u128 ARM_Dynarmic_64::GetVectorReg(int index) const { return jit->GetVector(index); } -void ARM_Dynarmic_64::SetVectorReg(std::size_t index, u128 value) { +void ARM_Dynarmic_64::SetVectorReg(int index, u128 value) { jit->SetVector(index, value); } diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.h b/src/core/arm/dynarmic/arm_dynarmic_64.h index 2afb7e7a4..403c55961 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.h +++ b/src/core/arm/dynarmic/arm_dynarmic_64.h @@ -33,10 +33,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(std::size_t index) const override; - void SetReg(std::size_t index, u64 value) override; - u128 GetVectorReg(std::size_t index) const override; - void SetVectorReg(std::size_t index, u128 value) override; + u64 GetReg(int index) const override; + void SetReg(int index, u64 value) override; + u128 GetVectorReg(int index) const override; + void SetVectorReg(int index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; void Run() override; diff --git a/src/core/arm/unicorn/arm_unicorn.cpp b/src/core/arm/unicorn/arm_unicorn.cpp index c1612d626..1df3f3ed1 100644 --- a/src/core/arm/unicorn/arm_unicorn.cpp +++ b/src/core/arm/unicorn/arm_unicorn.cpp @@ -96,35 +96,35 @@ u64 ARM_Unicorn::GetPC() const { return val; } -u64 ARM_Unicorn::GetReg(std::size_t index) const { +u64 ARM_Unicorn::GetReg(int regn) const { u64 val{}; auto treg = UC_ARM64_REG_SP; - if (index <= 28) { - treg = static_cast(UC_ARM64_REG_X0 + static_cast(index)); - } else if (index < 31) { - treg = static_cast(UC_ARM64_REG_X29 + static_cast(index) - 29); + if (regn <= 28) { + treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); + } else if (regn < 31) { + treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); } CHECKED(uc_reg_read(uc, treg, &val)); return val; } -void ARM_Unicorn::SetReg(std::size_t index, u64 value) { +void ARM_Unicorn::SetReg(int regn, u64 val) { auto treg = UC_ARM64_REG_SP; - if (index <= 28) { - treg = static_cast(UC_ARM64_REG_X0 + static_cast(index)); - } else if (index < 31) { - treg = static_cast(UC_ARM64_REG_X29 + static_cast(index) - 29); + if (regn <= 28) { + treg = (uc_arm64_reg)(UC_ARM64_REG_X0 + regn); + } else if (regn < 31) { + treg = (uc_arm64_reg)(UC_ARM64_REG_X29 + regn - 29); } - CHECKED(uc_reg_write(uc, treg, &value)); + CHECKED(uc_reg_write(uc, treg, &val)); } -u128 ARM_Unicorn::GetVectorReg(std::size_t /*index*/) const { +u128 ARM_Unicorn::GetVectorReg(int /*index*/) const { UNIMPLEMENTED(); static constexpr u128 res{}; return res; } -void ARM_Unicorn::SetVectorReg(std::size_t /*index*/, u128 /*value*/) { +void ARM_Unicorn::SetVectorReg(int /*index*/, u128 /*value*/) { UNIMPLEMENTED(); } @@ -217,8 +217,8 @@ void ARM_Unicorn::SaveContext(ThreadContext64& ctx) { CHECKED(uc_reg_read(uc, UC_ARM64_REG_PC, &ctx.pc)); CHECKED(uc_reg_read(uc, UC_ARM64_REG_NZCV, &ctx.pstate)); - for (std::size_t i = 0; i < 29; ++i) { - uregs[i] = UC_ARM64_REG_X0 + static_cast(i); + for (auto i = 0; i < 29; ++i) { + uregs[i] = UC_ARM64_REG_X0 + i; tregs[i] = &ctx.cpu_registers[i]; } uregs[29] = UC_ARM64_REG_X29; @@ -228,8 +228,8 @@ void ARM_Unicorn::SaveContext(ThreadContext64& ctx) { CHECKED(uc_reg_read_batch(uc, uregs, tregs, 31)); - for (std::size_t i = 0; i < 32; ++i) { - uregs[i] = UC_ARM64_REG_Q0 + static_cast(i); + for (int i = 0; i < 32; ++i) { + uregs[i] = UC_ARM64_REG_Q0 + i; tregs[i] = &ctx.vector_registers[i]; } @@ -244,8 +244,8 @@ void ARM_Unicorn::LoadContext(const ThreadContext64& ctx) { CHECKED(uc_reg_write(uc, UC_ARM64_REG_PC, &ctx.pc)); CHECKED(uc_reg_write(uc, UC_ARM64_REG_NZCV, &ctx.pstate)); - for (std::size_t i = 0; i < 29; ++i) { - uregs[i] = UC_ARM64_REG_X0 + static_cast(i); + for (int i = 0; i < 29; ++i) { + uregs[i] = UC_ARM64_REG_X0 + i; tregs[i] = (void*)&ctx.cpu_registers[i]; } uregs[29] = UC_ARM64_REG_X29; @@ -255,8 +255,8 @@ void ARM_Unicorn::LoadContext(const ThreadContext64& ctx) { CHECKED(uc_reg_write_batch(uc, uregs, tregs, 31)); - for (std::size_t i = 0; i < 32; ++i) { - uregs[i] = UC_ARM64_REG_Q0 + static_cast(i); + for (auto i = 0; i < 32; ++i) { + uregs[i] = UC_ARM64_REG_Q0 + i; tregs[i] = (void*)&ctx.vector_registers[i]; } diff --git a/src/core/arm/unicorn/arm_unicorn.h b/src/core/arm/unicorn/arm_unicorn.h index 1183e9541..810aff311 100644 --- a/src/core/arm/unicorn/arm_unicorn.h +++ b/src/core/arm/unicorn/arm_unicorn.h @@ -26,10 +26,10 @@ public: void SetPC(u64 pc) override; u64 GetPC() const override; - u64 GetReg(std::size_t index) const override; - void SetReg(std::size_t index, u64 value) override; - u128 GetVectorReg(std::size_t index) const override; - void SetVectorReg(std::size_t index, u128 value) override; + u64 GetReg(int index) const override; + void SetReg(int index, u64 value) override; + u128 GetVectorReg(int index) const override; + void SetVectorReg(int index, u128 value) override; u32 GetPSTATE() const override; void SetPSTATE(u32 pstate) override; VAddr GetTlsAddress() const override; diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 9b01f6293..e6c8461a5 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -140,8 +140,7 @@ void CoreTiming::AddTicks(u64 ticks) { void CoreTiming::Idle() { if (!event_queue.empty()) { const u64 next_event_time = event_queue.front().time; - const u64 next_ticks = - static_cast(nsToCycles(std::chrono::nanoseconds(next_event_time))) + 10; + const u64 next_ticks = nsToCycles(std::chrono::nanoseconds(next_event_time)) + 10U; if (next_ticks > ticks) { ticks = next_ticks; } @@ -188,7 +187,7 @@ void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { std::optional CoreTiming::Advance() { std::scoped_lock lock{advance_lock, basic_lock}; - global_timer = static_cast(GetGlobalTimeNs().count()); + global_timer = GetGlobalTimeNs().count(); while (!event_queue.empty() && event_queue.front().time <= global_timer) { Event evt = std::move(event_queue.front()); @@ -202,11 +201,11 @@ std::optional CoreTiming::Advance() { } basic_lock.lock(); - global_timer = static_cast(GetGlobalTimeNs().count()); + global_timer = GetGlobalTimeNs().count(); } if (!event_queue.empty()) { - const auto next_time = static_cast(event_queue.front().time - global_timer); + const s64 next_time = event_queue.front().time - global_timer; return next_time; } else { return std::nullopt; @@ -241,14 +240,14 @@ std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { if (is_multicore) { return clock->GetTimeNS(); } - return CyclesToNs(static_cast(ticks)); + return CyclesToNs(ticks); } std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { if (is_multicore) { return clock->GetTimeUS(); } - return CyclesToUs(static_cast(ticks)); + return CyclesToUs(ticks); } } // namespace Core::Timing diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index 5cd450714..8ce8e602e 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -21,9 +21,9 @@ s64 msToCycles(std::chrono::milliseconds ms) { } if (static_cast(ms.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return static_cast(Hardware::BASE_CLOCK_RATE * static_cast(ms.count() / 1000)); + return Hardware::BASE_CLOCK_RATE * (ms.count() / 1000); } - return static_cast((Hardware::BASE_CLOCK_RATE * static_cast(ms.count())) / 1000); + return (Hardware::BASE_CLOCK_RATE * ms.count()) / 1000; } s64 usToCycles(std::chrono::microseconds us) { @@ -33,55 +33,51 @@ s64 usToCycles(std::chrono::microseconds us) { } if (static_cast(us.count()) > MAX_VALUE_TO_MULTIPLY) { LOG_DEBUG(Core_Timing, "Time very big, do rounding"); - return static_cast(Hardware::BASE_CLOCK_RATE * static_cast(us.count() / 1000000)); + return Hardware::BASE_CLOCK_RATE * (us.count() / 1000000); } - return static_cast((Hardware::BASE_CLOCK_RATE * static_cast(us.count())) / 1000000); + return (Hardware::BASE_CLOCK_RATE * us.count()) / 1000000; } s64 nsToCycles(std::chrono::nanoseconds ns) { - const u128 temp = - Common::Multiply64Into128(static_cast(ns.count()), Hardware::BASE_CLOCK_RATE); - return static_cast(Common::Divide128On32(temp, static_cast(1000000000)).first); + const u128 temporal = Common::Multiply64Into128(ns.count(), Hardware::BASE_CLOCK_RATE); + return Common::Divide128On32(temporal, static_cast(1000000000)).first; } -u64 msToClockCycles(std::chrono::milliseconds ms) { - const auto count = static_cast(ms.count()); - const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); +u64 msToClockCycles(std::chrono::milliseconds ns) { + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000).first; } -u64 usToClockCycles(std::chrono::microseconds us) { - const auto count = static_cast(us.count()); - const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); +u64 usToClockCycles(std::chrono::microseconds ns) { + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000000).first; } u64 nsToClockCycles(std::chrono::nanoseconds ns) { - const auto count = static_cast(ns.count()); - const u128 temp = Common::Multiply64Into128(count, Hardware::CNTFREQ); + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); return Common::Divide128On32(temp, 1000000000).first; } u64 CpuCyclesToClockCycles(u64 ticks) { - const u128 temp = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); - return Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); + return Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; } std::chrono::milliseconds CyclesToMs(s64 cycles) { - const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000); - const u64 ms = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temporal = Common::Multiply64Into128(cycles, 1000); + u64 ms = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::milliseconds(ms); } std::chrono::nanoseconds CyclesToNs(s64 cycles) { - const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000000000); - const u64 ns = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temporal = Common::Multiply64Into128(cycles, 1000000000); + u64 ns = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::nanoseconds(ns); } std::chrono::microseconds CyclesToUs(s64 cycles) { - const u128 temp = Common::Multiply64Into128(static_cast(cycles), 1000000); - const u64 us = Common::Divide128On32(temp, static_cast(Hardware::BASE_CLOCK_RATE)).first; + const u128 temporal = Common::Multiply64Into128(cycles, 1000000); + u64 us = Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; return std::chrono::microseconds(us); } diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index 3be55e267..e4a046bf9 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -12,8 +12,8 @@ namespace Core::Timing { s64 msToCycles(std::chrono::milliseconds ms); s64 usToCycles(std::chrono::microseconds us); s64 nsToCycles(std::chrono::nanoseconds ns); -u64 msToClockCycles(std::chrono::milliseconds ms); -u64 usToClockCycles(std::chrono::microseconds us); +u64 msToClockCycles(std::chrono::milliseconds ns); +u64 usToClockCycles(std::chrono::microseconds ns); u64 nsToClockCycles(std::chrono::nanoseconds ns); std::chrono::milliseconds CyclesToMs(s64 cycles); std::chrono::nanoseconds CyclesToNs(s64 cycles); diff --git a/src/core/crypto/key_manager.cpp b/src/core/crypto/key_manager.cpp index 1f0d3170b..da15f764a 100644 --- a/src/core/crypto/key_manager.cpp +++ b/src/core/crypto/key_manager.cpp @@ -143,7 +143,6 @@ u64 GetSignatureTypeDataSize(SignatureType type) { return 0x3C; } UNREACHABLE(); - return 0; } u64 GetSignatureTypePaddingSize(SignatureType type) { @@ -158,7 +157,6 @@ u64 GetSignatureTypePaddingSize(SignatureType type) { return 0x40; } UNREACHABLE(); - return 0; } SignatureType Ticket::GetSignatureType() const { @@ -173,7 +171,6 @@ SignatureType Ticket::GetSignatureType() const { } UNREACHABLE(); - return {}; } TicketData& Ticket::GetData() { @@ -351,7 +348,7 @@ std::optional DeriveSDSeed() { std::array buffer{}; std::size_t offset = 0; for (; offset + 0x10 < save_43.GetSize(); ++offset) { - if (!save_43.Seek(static_cast(offset), SEEK_SET)) { + if (!save_43.Seek(offset, SEEK_SET)) { return std::nullopt; } @@ -361,7 +358,7 @@ std::optional DeriveSDSeed() { } } - if (!save_43.Seek(static_cast(offset + 0x10), SEEK_SET)) { + if (!save_43.Seek(offset + 0x10, SEEK_SET)) { return std::nullopt; } diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp index db54f71f4..5f1c86a09 100644 --- a/src/core/crypto/partition_data_manager.cpp +++ b/src/core/crypto/partition_data_manager.cpp @@ -161,7 +161,7 @@ static constexpr u8 CalculateMaxKeyblobSourceHash() { return true; }; - for (std::size_t i = 0x1F; i <= 0x1F; --i) { + for (s8 i = 0x1F; i >= 0; --i) { if (!is_zero(keyblob_source_hashes[i])) { return static_cast(i + 1); } diff --git a/src/core/file_sys/content_archive.cpp b/src/core/file_sys/content_archive.cpp index 0917f6ebf..76af47ff9 100644 --- a/src/core/file_sys/content_archive.cpp +++ b/src/core/file_sys/content_archive.cpp @@ -201,9 +201,9 @@ bool NCA::HandlePotentialHeaderDecryption() { } std::vector NCA::ReadSectionHeaders() const { - const auto number_sections = static_cast( + const std::ptrdiff_t number_sections = std::count_if(std::begin(header.section_tables), std::end(header.section_tables), - [](NCASectionTableEntry entry) { return entry.media_offset > 0; })); + [](NCASectionTableEntry entry) { return entry.media_offset > 0; }); std::vector sections(number_sections); const auto length_sections = SECTION_HEADER_SIZE * number_sections; diff --git a/src/core/file_sys/fsmitm_romfsbuild.cpp b/src/core/file_sys/fsmitm_romfsbuild.cpp index b2d38f01e..c52fafb6f 100644 --- a/src/core/file_sys/fsmitm_romfsbuild.cpp +++ b/src/core/file_sys/fsmitm_romfsbuild.cpp @@ -103,7 +103,7 @@ static u32 romfs_calc_path_hash(u32 parent, std::string_view path, u32 start, u32 hash = parent ^ 123456789; for (u32 i = 0; i < path_len; i++) { hash = (hash >> 5) | (hash << 27); - hash ^= static_cast(path[start + i]); + hash ^= path[start + i]; } return hash; diff --git a/src/core/file_sys/ips_layer.cpp b/src/core/file_sys/ips_layer.cpp index 91dc69373..a6101f1c0 100644 --- a/src/core/file_sys/ips_layer.cpp +++ b/src/core/file_sys/ips_layer.cpp @@ -66,14 +66,12 @@ static bool IsEOF(IPSFileType type, const std::vector& data) { } VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { - if (in == nullptr || ips == nullptr) { + if (in == nullptr || ips == nullptr) return nullptr; - } const auto type = IdentifyMagic(ips->ReadBytes(0x5)); - if (type == IPSFileType::Error) { + if (type == IPSFileType::Error) return nullptr; - } auto in_data = in->ReadAllBytes(); @@ -86,46 +84,37 @@ VirtualFile PatchIPS(const VirtualFile& in, const VirtualFile& ips) { } u32 real_offset{}; - if (type == IPSFileType::IPS32) { - real_offset = static_cast(temp[0] << 24) | static_cast(temp[1] << 16) | - static_cast(temp[2] << 8) | temp[3]; - } else { - real_offset = - static_cast(temp[0] << 16) | static_cast(temp[1] << 8) | temp[2]; - } + if (type == IPSFileType::IPS32) + real_offset = (temp[0] << 24) | (temp[1] << 16) | (temp[2] << 8) | temp[3]; + else + real_offset = (temp[0] << 16) | (temp[1] << 8) | temp[2]; u16 data_size{}; - if (ips->ReadObject(&data_size, offset) != sizeof(u16)) { + if (ips->ReadObject(&data_size, offset) != sizeof(u16)) return nullptr; - } data_size = Common::swap16(data_size); offset += sizeof(u16); if (data_size == 0) { // RLE u16 rle_size{}; - if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) { + if (ips->ReadObject(&rle_size, offset) != sizeof(u16)) return nullptr; - } rle_size = Common::swap16(rle_size); offset += sizeof(u16); const auto data = ips->ReadByte(offset++); - if (!data) { + if (!data) return nullptr; - } - if (real_offset + rle_size > in_data.size()) { + if (real_offset + rle_size > in_data.size()) rle_size = static_cast(in_data.size() - real_offset); - } std::memset(in_data.data() + real_offset, *data, rle_size); } else { // Standard Patch auto read = data_size; - if (real_offset + read > in_data.size()) { + if (real_offset + read > in_data.size()) read = static_cast(in_data.size() - real_offset); - } - if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) { + if (ips->Read(in_data.data() + real_offset, read, offset) != data_size) return nullptr; - } offset += data_size; } } @@ -193,16 +182,14 @@ void IPSwitchCompiler::ParseFlag(const std::string& line) { void IPSwitchCompiler::Parse() { const auto bytes = patch_text->ReadAllBytes(); std::stringstream s; - s.write(reinterpret_cast(bytes.data()), - static_cast(bytes.size())); + s.write(reinterpret_cast(bytes.data()), bytes.size()); std::vector lines; std::string stream_line; while (std::getline(s, stream_line)) { // Remove a trailing \r - if (!stream_line.empty() && stream_line.back() == '\r') { + if (!stream_line.empty() && stream_line.back() == '\r') stream_line.pop_back(); - } lines.push_back(std::move(stream_line)); } diff --git a/src/core/file_sys/kernel_executable.cpp b/src/core/file_sys/kernel_executable.cpp index fa758b777..ef93ef3ed 100644 --- a/src/core/file_sys/kernel_executable.cpp +++ b/src/core/file_sys/kernel_executable.cpp @@ -36,14 +36,14 @@ bool DecompressBLZ(std::vector& data) { while (out_index > 0) { --index; auto control = data[index + start_offset]; - for (std::size_t i = 0; i < 8; ++i) { + for (size_t i = 0; i < 8; ++i) { if (((control << i) & 0x80) > 0) { if (index < 2) { return false; } index -= 2; - std::size_t segment_offset = static_cast(data[index + start_offset]) | - static_cast(data[index + start_offset + 1] << 8); + std::size_t segment_offset = + data[index + start_offset] | data[index + start_offset + 1] << 8; std::size_t segment_size = ((segment_offset >> 12) & 0xF) + 3; segment_offset &= 0xFFF; segment_offset += 3; diff --git a/src/core/file_sys/nca_patch.cpp b/src/core/file_sys/nca_patch.cpp index 6d3472447..5990a2fd5 100644 --- a/src/core/file_sys/nca_patch.cpp +++ b/src/core/file_sys/nca_patch.cpp @@ -25,9 +25,9 @@ std::pair SearchBucketEntry(u64 offset, const BlockTyp ASSERT_MSG(offset <= block.size, "Offset is out of bounds in BKTR relocation block."); } - const auto bucket_id = static_cast(std::count_if( + std::size_t bucket_id = std::count_if( block.base_offsets.begin() + 1, block.base_offsets.begin() + block.number_buckets, - [&offset](u64 base_offset) { return base_offset <= offset; })); + [&offset](u64 base_offset) { return base_offset <= offset; }); const auto& bucket = buckets[bucket_id]; @@ -53,7 +53,6 @@ std::pair SearchBucketEntry(u64 offset, const BlockTyp } UNREACHABLE_MSG("Offset could not be found in BKTR block."); - return {}; } } // Anonymous namespace @@ -137,7 +136,7 @@ std::size_t BKTR::Read(u8* data, std::size_t length, std::size_t offset) const { const auto block_offset = section_offset & 0xF; if (block_offset != 0) { - auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xFU); + auto block = bktr_romfs->ReadBytes(0x10, section_offset & ~0xF); cipher.Transcode(block.data(), block.size(), block.data(), Core::Crypto::Op::Decrypt); if (length + block_offset < 0x10) { std::memcpy(data, block.data() + block_offset, std::min(length, block.size())); diff --git a/src/core/frontend/applets/controller.cpp b/src/core/frontend/applets/controller.cpp index fdc97d692..c5d65f2d0 100644 --- a/src/core/frontend/applets/controller.cpp +++ b/src/core/frontend/applets/controller.cpp @@ -30,7 +30,7 @@ void DefaultControllerApplet::ReconfigureControllers(std::function callb auto& players = Settings::values.players; const std::size_t min_supported_players = - parameters.enable_single_mode ? 1 : static_cast(parameters.min_players); + parameters.enable_single_mode ? 1 : parameters.min_players; // Disconnect Handheld first. npad.DisconnectNPadAtIndex(8); diff --git a/src/core/frontend/applets/profile_select.cpp b/src/core/frontend/applets/profile_select.cpp index a17420823..4df3574d2 100644 --- a/src/core/frontend/applets/profile_select.cpp +++ b/src/core/frontend/applets/profile_select.cpp @@ -12,9 +12,8 @@ ProfileSelectApplet::~ProfileSelectApplet() = default; void DefaultProfileSelectApplet::SelectProfile( std::function)> callback) const { - const auto user_index = static_cast(Settings::values.current_user); Service::Account::ProfileManager manager; - callback(manager.GetUser(user_index).value_or(Common::UUID{})); + callback(manager.GetUser(Settings::values.current_user).value_or(Common::UUID{})); LOG_INFO(Service_ACC, "called, selecting current user instead of prompting..."); } diff --git a/src/core/gdbstub/gdbstub.cpp b/src/core/gdbstub/gdbstub.cpp index 28a8a0f49..97ee65464 100644 --- a/src/core/gdbstub/gdbstub.cpp +++ b/src/core/gdbstub/gdbstub.cpp @@ -205,7 +205,7 @@ static Kernel::Thread* FindThreadById(s64 id) { const auto& threads = Core::System::GetInstance().GlobalScheduler().GetThreadList(); for (auto& thread : threads) { if (thread->GetThreadID() == static_cast(id)) { - current_core = static_cast(thread->GetProcessorID()); + current_core = thread->GetProcessorID(); return thread.get(); } } @@ -457,14 +457,7 @@ static u128 GdbHexToU128(const u8* src) { /// Read a byte from the gdb client. static u8 ReadByte() { u8 c; - -#ifdef WIN32 - const auto socket_id = static_cast(gdbserver_socket); -#else - const auto socket_id = gdbserver_socket; -#endif - - const auto received_size = recv(socket_id, reinterpret_cast(&c), 1, MSG_WAITALL); + std::size_t received_size = recv(gdbserver_socket, reinterpret_cast(&c), 1, MSG_WAITALL); if (received_size != 1) { LOG_ERROR(Debug_GDBStub, "recv failed: {}", received_size); Shutdown(); @@ -581,13 +574,7 @@ bool CheckBreakpoint(VAddr addr, BreakpointType type) { * @param packet Packet to be sent to client. */ static void SendPacket(const char packet) { -#ifdef WIN32 - const auto socket_id = static_cast(gdbserver_socket); -#else - const auto socket_id = gdbserver_socket; -#endif - - const auto sent_size = send(socket_id, &packet, 1, 0); + std::size_t sent_size = send(gdbserver_socket, &packet, 1, 0); if (sent_size != 1) { LOG_ERROR(Debug_GDBStub, "send failed"); } @@ -624,13 +611,7 @@ static void SendReply(const char* reply) { u8* ptr = command_buffer; u32 left = command_length + 4; while (left > 0) { -#ifdef WIN32 - const auto socket_id = static_cast(gdbserver_socket); -#else - const auto socket_id = gdbserver_socket; -#endif - const auto sent_size = - send(socket_id, reinterpret_cast(ptr), static_cast(left), 0); + const auto sent_size = send(gdbserver_socket, reinterpret_cast(ptr), left, 0); if (sent_size < 0) { LOG_ERROR(Debug_GDBStub, "gdb: send failed"); return Shutdown(); @@ -1313,13 +1294,8 @@ static void Init(u16 port) { WSAStartup(MAKEWORD(2, 2), &InitData); #endif -#ifdef WIN32 - using socket_type = SOCKET; -#else - using socket_type = int; -#endif - const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); - if (tmpsock == static_cast(-1)) { + int tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); + if (tmpsock == -1) { LOG_ERROR(Debug_GDBStub, "Failed to create gdb socket"); } @@ -1359,7 +1335,7 @@ static void Init(u16 port) { } // Clean up temporary socket if it's still alive at this point. - if (tmpsock != static_cast(-1)) { + if (tmpsock != -1) { shutdown(tmpsock, SHUT_RDWR); } } @@ -1376,12 +1352,7 @@ void Shutdown() { LOG_INFO(Debug_GDBStub, "Stopping GDB ..."); if (gdbserver_socket != -1) { -#ifdef WIN32 - const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); -#else - const auto tmpsock = static_cast(socket(PF_INET, SOCK_STREAM, 0)); -#endif - shutdown(tmpsock, SHUT_RDWR); + shutdown(gdbserver_socket, SHUT_RDWR); gdbserver_socket = -1; } @@ -1412,7 +1383,7 @@ void SetCpuStepFlag(bool is_step) { step_loop = is_step; } -void SendTrap(Kernel::Thread* thread, u32 trap) { +void SendTrap(Kernel::Thread* thread, int trap) { if (!send_trap) { return; } diff --git a/src/core/gdbstub/gdbstub.h b/src/core/gdbstub/gdbstub.h index 23d80f367..8fe3c320b 100644 --- a/src/core/gdbstub/gdbstub.h +++ b/src/core/gdbstub/gdbstub.h @@ -110,5 +110,5 @@ void SetCpuStepFlag(bool is_step); * @param thread Sending thread. * @param trap Trap no. */ -void SendTrap(Kernel::Thread* thread, u32 trap); +void SendTrap(Kernel::Thread* thread, int trap); } // namespace GDBStub diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index fcb86c822..1c354037d 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -233,7 +233,7 @@ void ResponseBuilder::PushRaw(const T& value) { static_assert(std::is_trivially_copyable_v, "It's undefined behavior to use memcpy with non-trivially copyable objects"); std::memcpy(cmdbuf + index, &value, sizeof(T)); - index += static_cast((sizeof(T) + 3) / 4); // round up to word length + index += (sizeof(T) + 3) / 4; // round up to word length } template <> @@ -390,7 +390,7 @@ void RequestParser::PopRaw(T& value) { static_assert(std::is_trivially_copyable_v, "It's undefined behavior to use memcpy with non-trivially copyable objects"); std::memcpy(&value, cmdbuf + index, sizeof(T)); - index += static_cast((sizeof(T) + 3) / 4); // round up to word length + index += (sizeof(T) + 3) / 4; // round up to word length } template diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index b6ebc5329..b882eaa0f 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -108,7 +108,7 @@ ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr a auto& monitor = system.Monitor(); s32 updated_value; do { - updated_value = static_cast(monitor.ExclusiveRead32(current_core, address)); + updated_value = monitor.ExclusiveRead32(current_core, address); if (updated_value != value) { return ERR_INVALID_STATE; @@ -129,7 +129,7 @@ ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr a updated_value = value; } } - } while (!monitor.ExclusiveWrite32(current_core, address, static_cast(updated_value))); + } while (!monitor.ExclusiveWrite32(current_core, address, updated_value)); WakeThreads(waiting_threads, num_to_wake); return RESULT_SUCCESS; diff --git a/src/core/hle/kernel/handle_table.cpp b/src/core/hle/kernel/handle_table.cpp index fe4988f84..3e745c18b 100644 --- a/src/core/hle/kernel/handle_table.cpp +++ b/src/core/hle/kernel/handle_table.cpp @@ -68,7 +68,7 @@ ResultVal HandleTable::Create(std::shared_ptr obj) { generations[slot] = generation; objects[slot] = std::move(obj); - const auto handle = static_cast(generation | static_cast(slot << 15)); + Handle handle = generation | (slot << 15); return MakeResult(handle); } diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp index 0a2de4270..81f85643b 100644 --- a/src/core/hle/kernel/hle_ipc.cpp +++ b/src/core/hle/kernel/hle_ipc.cpp @@ -58,7 +58,7 @@ std::shared_ptr HLERequestContext::SleepClientThread( { Handle event_handle = InvalidHandle; - SchedulerLockAndSleep lock(kernel, event_handle, thread.get(), static_cast(timeout)); + SchedulerLockAndSleep lock(kernel, event_handle, thread.get(), timeout); thread->SetHLECallback( [context = *this, callback](std::shared_ptr thread) mutable -> bool { ThreadWakeupReason reason = thread->GetSignalingResult() == RESULT_TIMEOUT diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 56e14da6b..b2b5b8adf 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -171,7 +171,7 @@ struct KernelCore::Impl { const auto type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_SUSPEND); auto thread_res = - Thread::Create(system, type, std::move(name), 0, 0, 0, static_cast(i), 0, + Thread::Create(system, type, std::move(name), 0, 0, 0, static_cast(i), 0, nullptr, std::move(init_func), init_func_parameter); suspend_threads[i] = std::move(thread_res).Unwrap(); diff --git a/src/core/hle/kernel/memory/address_space_info.cpp b/src/core/hle/kernel/memory/address_space_info.cpp index 6cf43ba24..e4288cab4 100644 --- a/src/core/hle/kernel/memory/address_space_info.cpp +++ b/src/core/hle/kernel/memory/address_space_info.cpp @@ -96,7 +96,6 @@ u64 AddressSpaceInfo::GetAddressSpaceStart(std::size_t width, Type type) { return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].address; } UNREACHABLE(); - return 0; } std::size_t AddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) { @@ -113,7 +112,6 @@ std::size_t AddressSpaceInfo::GetAddressSpaceSize(std::size_t width, Type type) return AddressSpaceInfos[AddressSpaceIndices39Bit[index]].size; } UNREACHABLE(); - return 0; } } // namespace Kernel::Memory diff --git a/src/core/hle/kernel/memory/memory_manager.cpp b/src/core/hle/kernel/memory/memory_manager.cpp index a96157c37..acf13585c 100644 --- a/src/core/hle/kernel/memory/memory_manager.cpp +++ b/src/core/hle/kernel/memory/memory_manager.cpp @@ -71,7 +71,7 @@ VAddr MemoryManager::AllocateContinuous(std::size_t num_pages, std::size_t align } // If we allocated more than we need, free some - const auto allocated_pages{PageHeap::GetBlockNumPages(static_cast(heap_index))}; + const auto allocated_pages{PageHeap::GetBlockNumPages(heap_index)}; if (allocated_pages > num_pages) { chosen_manager.Free(allocated_block + num_pages * PageSize, allocated_pages - num_pages); } @@ -112,7 +112,7 @@ ResultCode MemoryManager::Allocate(PageLinkedList& page_list, std::size_t num_pa // Keep allocating until we've allocated all our pages for (s32 index{heap_index}; index >= 0 && num_pages > 0; index--) { - const auto pages_per_alloc{PageHeap::GetBlockNumPages(static_cast(index))}; + const auto pages_per_alloc{PageHeap::GetBlockNumPages(index)}; while (num_pages >= pages_per_alloc) { // Allocate a block diff --git a/src/core/hle/kernel/memory/page_heap.cpp b/src/core/hle/kernel/memory/page_heap.cpp index 7890b8c1a..0ab1f7205 100644 --- a/src/core/hle/kernel/memory/page_heap.cpp +++ b/src/core/hle/kernel/memory/page_heap.cpp @@ -33,12 +33,11 @@ void PageHeap::Initialize(VAddr address, std::size_t size, std::size_t metadata_ } VAddr PageHeap::AllocateBlock(s32 index) { - const auto u_index = static_cast(index); - const auto needed_size{blocks[u_index].GetSize()}; + const std::size_t needed_size{blocks[index].GetSize()}; - for (auto i = u_index; i < MemoryBlockPageShifts.size(); i++) { - if (const VAddr addr = blocks[i].PopBlock(); addr != 0) { - if (const std::size_t allocated_size = blocks[i].GetSize(); + for (s32 i{index}; i < static_cast(MemoryBlockPageShifts.size()); i++) { + if (const VAddr addr{blocks[i].PopBlock()}; addr) { + if (const std::size_t allocated_size{blocks[i].GetSize()}; allocated_size > needed_size) { Free(addr + needed_size, (allocated_size - needed_size) / PageSize); } @@ -51,7 +50,7 @@ VAddr PageHeap::AllocateBlock(s32 index) { void PageHeap::FreeBlock(VAddr block, s32 index) { do { - block = blocks[static_cast(index++)].PushBlock(block); + block = blocks[index++].PushBlock(block); } while (block != 0); } @@ -70,7 +69,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { VAddr after_start{end}; VAddr after_end{end}; while (big_index >= 0) { - const std::size_t block_size{blocks[static_cast(big_index)].GetSize()}; + const std::size_t block_size{blocks[big_index].GetSize()}; const VAddr big_start{Common::AlignUp((start), block_size)}; const VAddr big_end{Common::AlignDown((end), block_size)}; if (big_start < big_end) { @@ -88,7 +87,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { // Free space before the big blocks for (s32 i{big_index - 1}; i >= 0; i--) { - const std::size_t block_size{blocks[static_cast(i)].GetSize()}; + const std::size_t block_size{blocks[i].GetSize()}; while (before_start + block_size <= before_end) { before_end -= block_size; FreeBlock(before_end, i); @@ -97,7 +96,7 @@ void PageHeap::Free(VAddr addr, std::size_t num_pages) { // Free space after the big blocks for (s32 i{big_index - 1}; i >= 0; i--) { - const std::size_t block_size{blocks[static_cast(i)].GetSize()}; + const std::size_t block_size{blocks[i].GetSize()}; while (after_start + block_size <= after_end) { FreeBlock(after_start, i); after_start += block_size; diff --git a/src/core/hle/kernel/memory/page_heap.h b/src/core/hle/kernel/memory/page_heap.h index 92a2bce04..22b0de860 100644 --- a/src/core/hle/kernel/memory/page_heap.h +++ b/src/core/hle/kernel/memory/page_heap.h @@ -34,9 +34,7 @@ public: static constexpr s32 GetBlockIndex(std::size_t num_pages) { for (s32 i{static_cast(NumMemoryBlockPageShifts) - 1}; i >= 0; i--) { - const auto shift_index = static_cast(i); - if (num_pages >= - (static_cast(1) << MemoryBlockPageShifts[shift_index]) / PageSize) { + if (num_pages >= (static_cast(1) << MemoryBlockPageShifts[i]) / PageSize) { return i; } } @@ -88,7 +86,7 @@ private: // Set the bitmap pointers for (s32 depth{GetHighestDepthIndex()}; depth >= 0; depth--) { - bit_storages[static_cast(depth)] = storage; + bit_storages[depth] = storage; size = Common::AlignUp(size, 64) / 64; storage += size; } @@ -101,7 +99,7 @@ private: s32 depth{}; do { - const u64 v{bit_storages[static_cast(depth)][offset]}; + const u64 v{bit_storages[depth][offset]}; if (v == 0) { // Non-zero depth indicates that a previous level had a free block ASSERT(depth == 0); @@ -127,7 +125,7 @@ private: constexpr bool ClearRange(std::size_t offset, std::size_t count) { const s32 depth{GetHighestDepthIndex()}; const auto bit_ind{offset / 64}; - u64* bits{bit_storages[static_cast(depth)]}; + u64* bits{bit_storages[depth]}; if (count < 64) { const auto shift{offset % 64}; ASSERT(shift + count <= 64); @@ -179,11 +177,11 @@ private: const auto which{offset % 64}; const u64 mask{1ULL << which}; - u64* bit{std::addressof(bit_storages[static_cast(depth)][ind])}; + u64* bit{std::addressof(bit_storages[depth][ind])}; const u64 v{*bit}; ASSERT((v & mask) == 0); *bit = v | mask; - if (v != 0) { + if (v) { break; } offset = ind; @@ -197,12 +195,12 @@ private: const auto which{offset % 64}; const u64 mask{1ULL << which}; - u64* bit{std::addressof(bit_storages[static_cast(depth)][ind])}; + u64* bit{std::addressof(bit_storages[depth][ind])}; u64 v{*bit}; ASSERT((v & mask) != 0); v &= ~mask; *bit = v; - if (v != 0) { + if (v) { break; } offset = ind; diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index 4f759d078..a3fadb533 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -414,8 +414,7 @@ ResultCode PageTable::MapPhysicalMemory(VAddr addr, std::size_t size) { const std::size_t remaining_pages{remaining_size / PageSize}; if (process->GetResourceLimit() && - !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, - static_cast(remaining_size))) { + !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, remaining_size)) { return ERR_RESOURCE_LIMIT_EXCEEDED; } @@ -779,8 +778,7 @@ ResultVal PageTable::SetHeapSize(std::size_t size) { auto process{system.Kernel().CurrentProcess()}; if (process->GetResourceLimit() && delta != 0 && - !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, - static_cast(delta))) { + !process->GetResourceLimit()->Reserve(ResourceType::PhysicalMemory, delta)) { return ERR_RESOURCE_LIMIT_EXCEEDED; } diff --git a/src/core/hle/kernel/physical_core.h b/src/core/hle/kernel/physical_core.h index 6cb59d0fc..d7a7a951c 100644 --- a/src/core/hle/kernel/physical_core.h +++ b/src/core/hle/kernel/physical_core.h @@ -34,7 +34,7 @@ public: PhysicalCore& operator=(const PhysicalCore&) = delete; PhysicalCore(PhysicalCore&&) = default; - PhysicalCore& operator=(PhysicalCore&&) = delete; + PhysicalCore& operator=(PhysicalCore&&) = default; void Idle(); /// Interrupt this physical core. diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 0b39f2955..ff9d9248b 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -137,10 +137,9 @@ std::shared_ptr Process::GetResourceLimit() const { } u64 Process::GetTotalPhysicalMemoryAvailable() const { - const u64 capacity{ - static_cast(resource_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory)) + - page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + - main_thread_stack_size}; + const u64 capacity{resource_limit->GetCurrentResourceValue(ResourceType::PhysicalMemory) + + page_table->GetTotalHeapSize() + GetSystemResourceSize() + image_size + + main_thread_stack_size}; if (capacity < memory_usage_capacity) { return capacity; @@ -280,12 +279,12 @@ ResultCode Process::LoadFromMetadata(const FileSys::ProgramMetadata& metadata, // Set initial resource limits resource_limit->SetLimitValue( ResourceType::PhysicalMemory, - static_cast(kernel.MemoryManager().GetSize(Memory::MemoryManager::Pool::Application))); + kernel.MemoryManager().GetSize(Memory::MemoryManager::Pool::Application)); resource_limit->SetLimitValue(ResourceType::Threads, 608); resource_limit->SetLimitValue(ResourceType::Events, 700); resource_limit->SetLimitValue(ResourceType::TransferMemory, 128); resource_limit->SetLimitValue(ResourceType::Sessions, 894); - ASSERT(resource_limit->Reserve(ResourceType::PhysicalMemory, static_cast(code_size))); + ASSERT(resource_limit->Reserve(ResourceType::PhysicalMemory, code_size)); // Create TLS region tls_region_address = CreateTLSRegion(); @@ -301,9 +300,9 @@ void Process::Run(s32 main_thread_priority, u64 stack_size) { ChangeStatus(ProcessStatus::Running); - SetupMainThread(system, *this, static_cast(main_thread_priority), main_thread_stack_top); + SetupMainThread(system, *this, main_thread_priority, main_thread_stack_top); resource_limit->Reserve(ResourceType::Threads, 1); - resource_limit->Reserve(ResourceType::PhysicalMemory, static_cast(main_thread_stack_size)); + resource_limit->Reserve(ResourceType::PhysicalMemory, main_thread_stack_size); } void Process::PrepareForTermination() { @@ -364,7 +363,7 @@ VAddr Process::CreateTLSRegion() { ->AllocateAndMapMemory(1, Memory::PageSize, true, start, size / Memory::PageSize, Memory::MemoryState::ThreadLocal, Memory::MemoryPermission::ReadAndWrite, tls_map_addr) - .ValueOr(0U)}; + .ValueOr(0)}; ASSERT(tls_page_addr); diff --git a/src/core/hle/kernel/resource_limit.cpp b/src/core/hle/kernel/resource_limit.cpp index e94093f24..212e442f4 100644 --- a/src/core/hle/kernel/resource_limit.cpp +++ b/src/core/hle/kernel/resource_limit.cpp @@ -43,8 +43,8 @@ void ResourceLimit::Release(ResourceType resource, u64 amount) { void ResourceLimit::Release(ResourceType resource, u64 used_amount, u64 available_amount) { const std::size_t index{ResourceTypeToIndex(resource)}; - current[index] -= static_cast(used_amount); - available[index] -= static_cast(available_amount); + current[index] -= used_amount; + available[index] -= available_amount; } std::shared_ptr ResourceLimit::Create(KernelCore& kernel) { diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp index 4a9a762f3..6b7db5372 100644 --- a/src/core/hle/kernel/scheduler.cpp +++ b/src/core/hle/kernel/scheduler.cpp @@ -89,11 +89,9 @@ u32 GlobalScheduler::SelectThreads() { while (iter != suggested_queue[core_id].end()) { suggested = *iter; iter++; - const s32 suggested_core_id = suggested->GetProcessorID(); - Thread* top_thread = suggested_core_id >= 0 - ? top_threads[static_cast(suggested_core_id)] - : nullptr; - + s32 suggested_core_id = suggested->GetProcessorID(); + Thread* top_thread = + suggested_core_id >= 0 ? top_threads[suggested_core_id] : nullptr; if (top_thread != suggested) { if (top_thread != nullptr && top_thread->GetPriority() < THREADPRIO_MAX_CORE_MIGRATION) { @@ -104,19 +102,16 @@ u32 GlobalScheduler::SelectThreads() { TransferToCore(suggested->GetPriority(), static_cast(core_id), suggested); break; } - suggested = nullptr; migration_candidates[num_candidates++] = suggested_core_id; } - // Step 3: Select a suggested thread from another core if (suggested == nullptr) { for (std::size_t i = 0; i < num_candidates; i++) { - const auto candidate_core = static_cast(migration_candidates[i]); + s32 candidate_core = migration_candidates[i]; suggested = top_threads[candidate_core]; auto it = scheduled_queue[candidate_core].begin(); - ++it; - + it++; Thread* next = it != scheduled_queue[candidate_core].end() ? *it : nullptr; if (next != nullptr) { TransferToCore(suggested->GetPriority(), static_cast(core_id), @@ -133,8 +128,7 @@ u32 GlobalScheduler::SelectThreads() { idle_cores &= ~(1U << core_id); } - - u32 cores_needing_context_switch = 0; + u32 cores_needing_context_switch{}; for (u32 core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) { Scheduler& sched = kernel.Scheduler(core); ASSERT(top_threads[core] == nullptr || @@ -192,16 +186,13 @@ bool GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) { for (auto& thread : suggested_queue[core_id]) { const s32 source_core = thread->GetProcessorID(); if (source_core >= 0) { - const auto sanitized_source_core = static_cast(source_core); - - if (current_threads[sanitized_source_core] != nullptr) { - if (thread == current_threads[sanitized_source_core] || - current_threads[sanitized_source_core]->GetPriority() < min_regular_priority) { + if (current_threads[source_core] != nullptr) { + if (thread == current_threads[source_core] || + current_threads[source_core]->GetPriority() < min_regular_priority) { continue; } } } - if (next_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks() || next_thread->GetPriority() < thread->GetPriority()) { if (thread->GetPriority() <= priority) { @@ -249,25 +240,17 @@ bool GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread for (std::size_t i = 0; i < current_threads.size(); i++) { current_threads[i] = scheduled_queue[i].empty() ? nullptr : scheduled_queue[i].front(); } - for (auto& thread : suggested_queue[core_id]) { const s32 source_core = thread->GetProcessorID(); - if (source_core < 0) { - continue; - } - - const auto sanitized_source_core = static_cast(source_core); - if (thread == current_threads[sanitized_source_core]) { + if (source_core < 0 || thread == current_threads[source_core]) { continue; } - - if (current_threads[sanitized_source_core] == nullptr || - current_threads[sanitized_source_core]->GetPriority() >= min_regular_priority) { + if (current_threads[source_core] == nullptr || + current_threads[source_core]->GetPriority() >= min_regular_priority) { winner = thread; } break; } - if (winner != nullptr) { if (winner != yielding_thread) { TransferToCore(winner->GetPriority(), static_cast(core_id), winner); @@ -309,22 +292,17 @@ void GlobalScheduler::PreemptThreads() { if (thread->GetPriority() != priority) { continue; } - if (source_core >= 0) { - const auto sanitized_source_core = static_cast(source_core); - Thread* next_thread = scheduled_queue[sanitized_source_core].empty() + Thread* next_thread = scheduled_queue[source_core].empty() ? nullptr - : scheduled_queue[sanitized_source_core].front(); - + : scheduled_queue[source_core].front(); if (next_thread != nullptr && next_thread->GetPriority() < 2) { break; } - if (next_thread == thread) { continue; } } - if (current_thread != nullptr && current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) { winner = thread; @@ -344,22 +322,17 @@ void GlobalScheduler::PreemptThreads() { if (thread->GetPriority() < priority) { continue; } - if (source_core >= 0) { - const auto sanitized_source_core = static_cast(source_core); - Thread* next_thread = scheduled_queue[sanitized_source_core].empty() + Thread* next_thread = scheduled_queue[source_core].empty() ? nullptr - : scheduled_queue[sanitized_source_core].front(); - + : scheduled_queue[source_core].front(); if (next_thread != nullptr && next_thread->GetPriority() < 2) { break; } - if (next_thread == thread) { continue; } } - if (current_thread != nullptr && current_thread->GetLastRunningTicks() >= thread->GetLastRunningTicks()) { winner = thread; @@ -379,11 +352,11 @@ void GlobalScheduler::PreemptThreads() { void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule, Core::EmuThreadHandle global_thread) { - const u32 current_core = global_thread.host_handle; + u32 current_core = global_thread.host_handle; bool must_context_switch = global_thread.guest_handle != InvalidHandle && (current_core < Core::Hardware::NUM_CPU_CORES); while (cores_pending_reschedule != 0) { - const u32 core = Common::CountTrailingZeroes32(cores_pending_reschedule); + u32 core = Common::CountTrailingZeroes32(cores_pending_reschedule); ASSERT(core < Core::Hardware::NUM_CPU_CORES); if (!must_context_switch || core != current_core) { auto& phys_core = kernel.PhysicalCore(core); @@ -393,7 +366,6 @@ void GlobalScheduler::EnableInterruptAndSchedule(u32 cores_pending_reschedule, } cores_pending_reschedule &= ~(1U << core); } - if (must_context_switch) { auto& core_scheduler = kernel.CurrentScheduler(); kernel.ExitSVCProfile(); @@ -831,11 +803,9 @@ void Scheduler::Initialize() { std::string name = "Idle Thread Id:" + std::to_string(core_id); std::function init_func = Core::CpuManager::GetIdleThreadStartFunc(); void* init_func_parameter = system.GetCpuManager().GetStartFuncParamater(); - const auto type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_IDLE); - auto thread_res = - Thread::Create(system, type, std::move(name), 0, 64, 0, static_cast(core_id), 0, - nullptr, std::move(init_func), init_func_parameter); - + ThreadType type = static_cast(THREADTYPE_KERNEL | THREADTYPE_HLE | THREADTYPE_IDLE); + auto thread_res = Thread::Create(system, type, name, 0, 64, 0, static_cast(core_id), 0, + nullptr, std::move(init_func), init_func_parameter); idle_thread = std::move(thread_res).Unwrap(); } diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp index b8623e831..bafd1ced7 100644 --- a/src/core/hle/kernel/svc.cpp +++ b/src/core/hle/kernel/svc.cpp @@ -482,8 +482,7 @@ static ResultCode WaitSynchronization(Core::System& system, Handle* index, VAddr static ResultCode WaitSynchronization32(Core::System& system, u32 timeout_low, u32 handles_address, s32 handle_count, u32 timeout_high, Handle* index) { const s64 nano_seconds{(static_cast(timeout_high) << 32) | static_cast(timeout_low)}; - return WaitSynchronization(system, index, handles_address, static_cast(handle_count), - nano_seconds); + return WaitSynchronization(system, index, handles_address, handle_count, nano_seconds); } /// Resumes a thread waiting on WaitSynchronization @@ -2003,7 +2002,7 @@ static ResultCode GetThreadCoreMask(Core::System& system, Handle thread_handle, return ERR_INVALID_HANDLE; } - *core = static_cast(thread->GetIdealCore()); + *core = thread->GetIdealCore(); *mask = thread->GetAffinityMask(); return RESULT_SUCCESS; @@ -2071,7 +2070,7 @@ static ResultCode SetThreadCoreMask(Core::System& system, Handle thread_handle, return ERR_INVALID_HANDLE; } - return thread->SetCoreAndAffinityMask(static_cast(core), affinity_mask); + return thread->SetCoreAndAffinityMask(core, affinity_mask); } static ResultCode SetThreadCoreMask32(Core::System& system, Handle thread_handle, u32 core, diff --git a/src/core/hle/kernel/svc_wrap.h b/src/core/hle/kernel/svc_wrap.h index 9284a4c84..0b6dd9df0 100644 --- a/src/core/hle/kernel/svc_wrap.h +++ b/src/core/hle/kernel/svc_wrap.h @@ -11,11 +11,11 @@ namespace Kernel { -static inline u64 Param(const Core::System& system, std::size_t n) { +static inline u64 Param(const Core::System& system, int n) { return system.CurrentArmInterface().GetReg(n); } -static inline u32 Param32(const Core::System& system, std::size_t n) { +static inline u32 Param32(const Core::System& system, int n) { return static_cast(system.CurrentArmInterface().GetReg(n)); } @@ -29,7 +29,7 @@ static inline void FuncReturn(Core::System& system, u64 result) { } static inline void FuncReturn32(Core::System& system, u32 result) { - system.CurrentArmInterface().SetReg(0, static_cast(result)); + system.CurrentArmInterface().SetReg(0, (u64)result); } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -386,10 +386,9 @@ template void SvcWrap32(Core::System& system) { Handle param_1 = 0; - const u32 retval = - func(system, ¶m_1, Param32(system, 0), Param32(system, 1), Param32(system, 2), - Param32(system, 3), static_cast(Param32(system, 4))) - .raw; + const u32 retval = func(system, ¶m_1, Param32(system, 0), Param32(system, 1), + Param32(system, 2), Param32(system, 3), Param32(system, 4)) + .raw; system.CurrentArmInterface().SetReg(1, param_1); FuncReturn(system, retval); @@ -543,8 +542,8 @@ void SvcWrap32(Core::System& system) { template void SvcWrap32(Core::System& system) { u32 param_1 = 0; - const u32 retval = func(system, Param32(system, 0), Param32(system, 1), - static_cast(Param32(system, 2)), Param32(system, 3), ¶m_1) + const u32 retval = func(system, Param32(system, 0), Param32(system, 1), Param32(system, 2), + Param32(system, 3), ¶m_1) .raw; system.CurrentArmInterface().SetReg(1, param_1); FuncReturn(system, retval); diff --git a/src/core/hle/kernel/synchronization.cpp b/src/core/hle/kernel/synchronization.cpp index 653f722b3..8b875d853 100644 --- a/src/core/hle/kernel/synchronization.cpp +++ b/src/core/hle/kernel/synchronization.cpp @@ -51,7 +51,7 @@ std::pair Synchronization::WaitFor( // We found a ready object, acquire it and set the result value SynchronizationObject* object = itr->get(); object->Acquire(thread); - const auto index = static_cast(std::distance(sync_objects.begin(), itr)); + const u32 index = static_cast(std::distance(sync_objects.begin(), itr)); lock.CancelSleep(); return {RESULT_SUCCESS, index}; } @@ -105,7 +105,7 @@ std::pair Synchronization::WaitFor( }); ASSERT(itr != sync_objects.end()); signaling_object->Acquire(thread); - const auto index = static_cast(std::distance(sync_objects.begin(), itr)); + const u32 index = static_cast(std::distance(sync_objects.begin(), itr)); return {signaling_result, index}; } return {signaling_result, -1}; diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 323e740e9..d132aba34 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -525,7 +525,7 @@ ResultCode Thread::SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask) { if (old_affinity_mask != new_affinity_mask) { const s32 old_core = processor_id; if (processor_id >= 0 && ((affinity_mask >> processor_id) & 1) == 0) { - if (ideal_core < 0) { + if (static_cast(ideal_core) < 0) { processor_id = HighestSetCore(affinity_mask, Core::Hardware::NUM_CPU_CORES); } else { processor_id = ideal_core; diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h index 21b22ca45..8daf79fac 100644 --- a/src/core/hle/kernel/thread.h +++ b/src/core/hle/kernel/thread.h @@ -470,7 +470,7 @@ public: bool InvokeHLECallback(std::shared_ptr thread); - s32 GetIdealCore() const { + u32 GetIdealCore() const { return ideal_core; } @@ -654,8 +654,8 @@ private: Scheduler* scheduler = nullptr; - s32 ideal_core = -1; - u64 affinity_mask = 1; + u32 ideal_core{0xFFFFFFFF}; + u64 affinity_mask{0x1}; s32 ideal_core_override = -1; u64 affinity_mask_override = 0x1; diff --git a/src/core/hle/service/acc/profile_manager.cpp b/src/core/hle/service/acc/profile_manager.cpp index 9c302043a..9b829e957 100644 --- a/src/core/hle/service/acc/profile_manager.cpp +++ b/src/core/hle/service/acc/profile_manager.cpp @@ -41,15 +41,12 @@ constexpr char ACC_SAVE_AVATORS_BASE_PATH[] = "/system/save/8000000000000010/su/ ProfileManager::ProfileManager() { ParseUserSaveFile(); - if (user_count == 0) { + if (user_count == 0) CreateNewUser(UUID::Generate(), "yuzu"); - } - auto current = static_cast( - std::clamp(Settings::values.current_user, 0, static_cast(MAX_USERS - 1))); - if (UserExistsIndex(current)) { + auto current = std::clamp(Settings::values.current_user, 0, MAX_USERS - 1); + if (UserExistsIndex(current)) current = 0; - } OpenUser(*GetUser(current)); } @@ -192,8 +189,8 @@ std::size_t ProfileManager::GetUserCount() const { /// booting std::size_t ProfileManager::GetOpenUserCount() const { - return static_cast(std::count_if(profiles.begin(), profiles.end(), - [](const ProfileInfo& p) { return p.is_open; })); + return std::count_if(profiles.begin(), profiles.end(), + [](const ProfileInfo& p) { return p.is_open; }); } /// Checks if a user id exists in our profile manager diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 995b7e5c6..d7a81f64a 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -1311,7 +1311,7 @@ void IApplicationFunctions::PopLaunchParameter(Kernel::HLERequestContext& ctx) { params.is_account_selected = 1; Account::ProfileManager profile_manager{}; - const auto uuid = profile_manager.GetUser(static_cast(Settings::values.current_user)); + const auto uuid = profile_manager.GetUser(Settings::values.current_user); ASSERT(uuid); params.current_user = uuid->uuid; diff --git a/src/core/hle/service/am/applets/controller.cpp b/src/core/hle/service/am/applets/controller.cpp index 17788d7a5..2151da783 100644 --- a/src/core/hle/service/am/applets/controller.cpp +++ b/src/core/hle/service/am/applets/controller.cpp @@ -178,23 +178,23 @@ void Controller::Execute() { } void Controller::ConfigurationComplete() { - const auto& players = Settings::values.players; - - const s8 player_count = - is_single_mode - ? 1 - : static_cast(std::count_if(players.begin(), players.end() - 2, - [](const auto& player) { return player.connected; })); + ControllerSupportResultInfo result_info{}; - const auto index = static_cast(std::distance( - players.begin(), std::find_if(players.begin(), players.end(), - [](const auto& player) { return player.connected; }))); + const auto& players = Settings::values.players; // If enable_single_mode is enabled, player_count is 1 regardless of any other parameters. // Otherwise, only count connected players from P1-P8. - ControllerSupportResultInfo result_info{}; - result_info.player_count = player_count; - result_info.selected_id = HID::Controller_NPad::IndexToNPad(index); + result_info.player_count = + is_single_mode ? 1 + : static_cast(std::count_if( + players.begin(), players.end() - 2, + [](Settings::PlayerInput player) { return player.connected; })); + + result_info.selected_id = HID::Controller_NPad::IndexToNPad( + std::distance(players.begin(), + std::find_if(players.begin(), players.end(), + [](Settings::PlayerInput player) { return player.connected; }))); + result_info.result = 0; LOG_DEBUG(Service_HID, "Result Info: player_count={}, selected_id={}, result={}", diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index a345a68e6..9b4910e53 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -69,10 +69,9 @@ public: buffer_event = Kernel::WritableEvent::CreateEventPair(system.Kernel(), "IAudioOutBufferReleased"); - stream = - audio_core.OpenStream(system.CoreTiming(), static_cast(audio_params.sample_rate), - audio_params.channel_count, std::move(unique_name), - [this] { buffer_event.writable->Signal(); }); + stream = audio_core.OpenStream(system.CoreTiming(), audio_params.sample_rate, + audio_params.channel_count, std::move(unique_name), + [this] { buffer_event.writable->Signal(); }); } private: diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index 16a6deb7e..f1d81602c 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -50,8 +50,8 @@ public: Enabled, }; - explicit OpusDecoderState(OpusDecoderPtr decoder_, s32 sample_rate_, u32 channel_count_) - : decoder{std::move(decoder_)}, sample_rate{sample_rate_}, channel_count{channel_count_} {} + explicit OpusDecoderState(OpusDecoderPtr decoder, u32 sample_rate, u32 channel_count) + : decoder{std::move(decoder)}, sample_rate{sample_rate}, channel_count{channel_count} {} // Decodes interleaved Opus packets. Optionally allows reporting time taken to // perform the decoding, as well as any relevant extra behavior. @@ -113,16 +113,15 @@ private: return false; } - const auto* const frame = input.data() + sizeof(OpusPacketHeader); + const auto frame = input.data() + sizeof(OpusPacketHeader); const auto decoded_sample_count = opus_packet_get_nb_samples( - frame, static_cast(input.size() - sizeof(OpusPacketHeader)), sample_rate); - const auto decoded_size = - static_cast(decoded_sample_count) * channel_count * sizeof(u16); - if (decoded_size > raw_output_sz) { + frame, static_cast(input.size() - sizeof(OpusPacketHeader)), + static_cast(sample_rate)); + if (decoded_sample_count * channel_count * sizeof(u16) > raw_output_sz) { LOG_ERROR( Audio, "Decoded data does not fit into the output data, decoded_sz={}, raw_output_sz={}", - decoded_size, raw_output_sz); + decoded_sample_count * channel_count * sizeof(u16), raw_output_sz); return false; } @@ -138,11 +137,11 @@ private: } const auto end_time = std::chrono::high_resolution_clock::now() - start_time; - sample_count = static_cast(out_sample_count); + sample_count = out_sample_count; consumed = static_cast(sizeof(OpusPacketHeader) + hdr.size); if (out_performance_time != nullptr) { - *out_performance_time = static_cast( - std::chrono::duration_cast(end_time).count()); + *out_performance_time = + std::chrono::duration_cast(end_time).count(); } return true; @@ -155,7 +154,7 @@ private: } OpusDecoderPtr decoder; - s32 sample_rate; + u32 sample_rate; u32 channel_count; }; @@ -213,7 +212,7 @@ std::size_t WorkerBufferSize(u32 channel_count) { ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count"); constexpr int num_streams = 1; const int num_stereo_streams = channel_count == 2 ? 1 : 0; - return static_cast(opus_multistream_decoder_get_size(num_streams, num_stereo_streams)); + return opus_multistream_decoder_get_size(num_streams, num_stereo_streams); } // Creates the mapping table that maps the input channels to the particular @@ -245,7 +244,7 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { "Invalid sample rate"); ASSERT_MSG(channel_count == 1 || channel_count == 2, "Invalid channel count"); - const auto worker_buffer_sz = static_cast(WorkerBufferSize(channel_count)); + const u32 worker_buffer_sz = static_cast(WorkerBufferSize(channel_count)); LOG_DEBUG(Audio, "worker_buffer_sz={}", worker_buffer_sz); IPC::ResponseBuilder rb{ctx, 3}; @@ -255,7 +254,7 @@ void HwOpus::GetWorkBufferSize(Kernel::HLERequestContext& ctx) { void HwOpus::OpenOpusDecoder(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - const auto sample_rate = rp.Pop(); + const auto sample_rate = rp.Pop(); const auto channel_count = rp.Pop(); const auto buffer_sz = rp.Pop(); diff --git a/src/core/hle/service/bcat/backend/backend.h b/src/core/hle/service/bcat/backend/backend.h index 1e5e93290..48bbbe66f 100644 --- a/src/core/hle/service/bcat/backend/backend.h +++ b/src/core/hle/service/bcat/backend/backend.h @@ -53,10 +53,10 @@ struct DeliveryCacheProgressImpl { ResultCode result = RESULT_SUCCESS; DirectoryName current_directory; FileName current_file; - u64 current_downloaded_bytes; ///< Bytes downloaded on current file. - u64 current_total_bytes; ///< Bytes total on current file. - u64 total_downloaded_bytes; ///< Bytes downloaded on overall download. - u64 total_bytes; ///< Bytes total on overall download. + s64 current_downloaded_bytes; ///< Bytes downloaded on current file. + s64 current_total_bytes; ///< Bytes total on current file. + s64 total_downloaded_bytes; ///< Bytes downloaded on overall download. + s64 total_bytes; ///< Bytes total on overall download. INSERT_PADDING_BYTES( 0x198); ///< Appears to be unused in official code, possibly reserved for future use. }; diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp index e6cadf491..3b6f7498e 100644 --- a/src/core/hle/service/bcat/backend/boxcat.cpp +++ b/src/core/hle/service/bcat/backend/boxcat.cpp @@ -3,16 +3,7 @@ // Refer to the license.txt file included. #include - -#if defined(__GNUC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wsign-conversion" -#endif #include -#if defined(__GNUC__) -#pragma GCC diagnostic pop -#endif - #include #include #include "common/hex_util.h" diff --git a/src/core/hle/service/bcat/module.cpp b/src/core/hle/service/bcat/module.cpp index 5a7e9f930..db0e06ca1 100644 --- a/src/core/hle/service/bcat/module.cpp +++ b/src/core/hle/service/bcat/module.cpp @@ -454,8 +454,7 @@ private: write_size = std::min(write_size, files.size()); std::vector entries(write_size); std::transform( - files.begin(), files.begin() + static_cast(write_size), entries.begin(), - [](const auto& file) { + files.begin(), files.begin() + write_size, entries.begin(), [](const auto& file) { FileName name{}; std::memcpy(name.data(), file->GetName().data(), std::min(file->GetName().size(), name.size())); diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp index 993686f1d..649128be4 100644 --- a/src/core/hle/service/filesystem/fsp_srv.cpp +++ b/src/core/hle/service/filesystem/fsp_srv.cpp @@ -94,8 +94,7 @@ private: } // Read the data from the Storage backend - const auto output = backend->ReadBytes(static_cast(length), static_cast(offset)); - + std::vector output = backend->ReadBytes(length, offset); // Write the data to memory ctx.WriteBuffer(output); @@ -152,7 +151,7 @@ private: } // Read the data from the Storage backend - const auto output = backend->ReadBytes(static_cast(length), static_cast(offset)); + std::vector output = backend->ReadBytes(length, offset); // Write the data to memory ctx.WriteBuffer(output); @@ -195,8 +194,7 @@ private: // Write the data to the Storage backend const auto write_size = static_cast(std::distance(data.begin(), data.begin() + length)); - const std::size_t written = - backend->Write(data.data(), write_size, static_cast(offset)); + const std::size_t written = backend->Write(data.data(), write_size, offset); ASSERT_MSG(static_cast(written) == length, "Could not write all bytes to file (requested={:016X}, actual={:016X}).", length, diff --git a/src/core/hle/service/hid/controllers/debug_pad.cpp b/src/core/hle/service/hid/controllers/debug_pad.cpp index c2c1470a5..ad251ed4a 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.cpp +++ b/src/core/hle/service/hid/controllers/debug_pad.cpp @@ -23,7 +23,7 @@ void Controller_DebugPad::OnRelease() {} void Controller_DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); + shared_memory.header.timestamp = core_timing.GetCPUTicks(); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -33,11 +33,9 @@ void Controller_DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, } shared_memory.header.entry_count = 16; - const auto& last_entry = - shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; + const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = - shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; + auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index 0618b2a05..b7b7bfeae 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -19,7 +19,7 @@ void Controller_Gesture::OnRelease() {} void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); + shared_memory.header.timestamp = core_timing.GetCPUTicks(); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -29,11 +29,9 @@ void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u } shared_memory.header.entry_count = 16; - const auto& last_entry = - shared_memory.gesture_states[static_cast(shared_memory.header.last_entry_index)]; + const auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = - shared_memory.gesture_states[static_cast(shared_memory.header.last_entry_index)]; + auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index 0624be316..59b694cd4 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp @@ -21,7 +21,7 @@ void Controller_Keyboard::OnRelease() {} void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); + shared_memory.header.timestamp = core_timing.GetCPUTicks(); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -31,11 +31,9 @@ void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, } shared_memory.header.entry_count = 16; - const auto& last_entry = - shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; + const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = - shared_memory.pad_states[static_cast(shared_memory.header.last_entry_index)]; + auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp index 10e2373bc..ac40989c5 100644 --- a/src/core/hle/service/hid/controllers/mouse.cpp +++ b/src/core/hle/service/hid/controllers/mouse.cpp @@ -19,7 +19,7 @@ void Controller_Mouse::OnRelease() {} void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); + shared_memory.header.timestamp = core_timing.GetCPUTicks(); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -29,11 +29,9 @@ void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } shared_memory.header.entry_count = 16; - auto& last_entry = - shared_memory.mouse_states[static_cast(shared_memory.header.last_entry_index)]; + auto& last_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = - shared_memory.mouse_states[static_cast(shared_memory.header.last_entry_index)]; + auto& cur_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index 2422c0190..e311bc18c 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -341,29 +341,26 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } for (std::size_t i = 0; i < shared_memory_entries.size(); i++) { auto& npad = shared_memory_entries[i]; - const std::array controller_npads{ - &npad.main_controller_states, - &npad.handheld_states, - &npad.dual_states, - &npad.left_joy_states, - &npad.right_joy_states, - &npad.pokeball_states, - &npad.libnx, - }; + const std::array controller_npads{&npad.main_controller_states, + &npad.handheld_states, + &npad.dual_states, + &npad.left_joy_states, + &npad.right_joy_states, + &npad.pokeball_states, + &npad.libnx}; for (auto* main_controller : controller_npads) { main_controller->common.entry_count = 16; main_controller->common.total_entry_count = 17; const auto& last_entry = - main_controller->npad[static_cast(main_controller->common.last_entry_index)]; + main_controller->npad[main_controller->common.last_entry_index]; - main_controller->common.timestamp = static_cast(core_timing.GetCPUTicks()); + main_controller->common.timestamp = core_timing.GetCPUTicks(); main_controller->common.last_entry_index = (main_controller->common.last_entry_index + 1) % 17; - auto& cur_entry = - main_controller->npad[static_cast(main_controller->common.last_entry_index)]; + auto& cur_entry = main_controller->npad[main_controller->common.last_entry_index]; cur_entry.timestamp = last_entry.timestamp + 1; cur_entry.timestamp2 = cur_entry.timestamp; @@ -374,29 +371,22 @@ void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* if (controller_type == NPadControllerType::None || !connected_controllers[i].is_connected) { continue; } - const auto npad_index = static_cast(i); + const u32 npad_index = static_cast(i); RequestPadStateUpdate(npad_index); auto& pad_state = npad_pad_states[npad_index]; auto& main_controller = - npad.main_controller_states - .npad[static_cast(npad.main_controller_states.common.last_entry_index)]; + npad.main_controller_states.npad[npad.main_controller_states.common.last_entry_index]; auto& handheld_entry = - npad.handheld_states - .npad[static_cast(npad.handheld_states.common.last_entry_index)]; - auto& dual_entry = - npad.dual_states.npad[static_cast(npad.dual_states.common.last_entry_index)]; - auto& left_entry = - npad.left_joy_states - .npad[static_cast(npad.left_joy_states.common.last_entry_index)]; + npad.handheld_states.npad[npad.handheld_states.common.last_entry_index]; + auto& dual_entry = npad.dual_states.npad[npad.dual_states.common.last_entry_index]; + auto& left_entry = npad.left_joy_states.npad[npad.left_joy_states.common.last_entry_index]; auto& right_entry = - npad.right_joy_states - .npad[static_cast(npad.right_joy_states.common.last_entry_index)]; + npad.right_joy_states.npad[npad.right_joy_states.common.last_entry_index]; auto& pokeball_entry = - npad.pokeball_states - .npad[static_cast(npad.pokeball_states.common.last_entry_index)]; - auto& libnx_entry = npad.libnx.npad[static_cast(npad.libnx.common.last_entry_index)]; + npad.pokeball_states.npad[npad.pokeball_states.common.last_entry_index]; + auto& libnx_entry = npad.libnx.npad[npad.libnx.common.last_entry_index]; libnx_entry.connection_status.raw = 0; libnx_entry.connection_status.IsConnected.Assign(1); @@ -510,14 +500,13 @@ void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing sixaxis_sensor->common.total_entry_count = 17; const auto& last_entry = - sixaxis_sensor->sixaxis[static_cast(sixaxis_sensor->common.last_entry_index)]; + sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; - sixaxis_sensor->common.timestamp = static_cast(core_timing.GetCPUTicks()); + sixaxis_sensor->common.timestamp = core_timing.GetCPUTicks(); sixaxis_sensor->common.last_entry_index = (sixaxis_sensor->common.last_entry_index + 1) % 17; - auto& cur_entry = - sixaxis_sensor->sixaxis[static_cast(sixaxis_sensor->common.last_entry_index)]; + auto& cur_entry = sixaxis_sensor->sixaxis[sixaxis_sensor->common.last_entry_index]; cur_entry.timestamp = last_entry.timestamp + 1; cur_entry.timestamp2 = cur_entry.timestamp; @@ -540,21 +529,17 @@ void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing } auto& full_sixaxis_entry = - npad.sixaxis_full.sixaxis[static_cast(npad.sixaxis_full.common.last_entry_index)]; + npad.sixaxis_full.sixaxis[npad.sixaxis_full.common.last_entry_index]; auto& handheld_sixaxis_entry = - npad.sixaxis_handheld - .sixaxis[static_cast(npad.sixaxis_handheld.common.last_entry_index)]; + npad.sixaxis_handheld.sixaxis[npad.sixaxis_handheld.common.last_entry_index]; auto& dual_left_sixaxis_entry = - npad.sixaxis_dual_left - .sixaxis[static_cast(npad.sixaxis_dual_left.common.last_entry_index)]; + npad.sixaxis_dual_left.sixaxis[npad.sixaxis_dual_left.common.last_entry_index]; auto& dual_right_sixaxis_entry = - npad.sixaxis_dual_right - .sixaxis[static_cast(npad.sixaxis_dual_right.common.last_entry_index)]; + npad.sixaxis_dual_right.sixaxis[npad.sixaxis_dual_right.common.last_entry_index]; auto& left_sixaxis_entry = - npad.sixaxis_left.sixaxis[static_cast(npad.sixaxis_left.common.last_entry_index)]; + npad.sixaxis_left.sixaxis[npad.sixaxis_left.common.last_entry_index]; auto& right_sixaxis_entry = - npad.sixaxis_right - .sixaxis[static_cast(npad.sixaxis_right.common.last_entry_index)]; + npad.sixaxis_right.sixaxis[npad.sixaxis_right.common.last_entry_index]; switch (controller_type) { case NPadControllerType::None: diff --git a/src/core/hle/service/hid/controllers/stubbed.cpp b/src/core/hle/service/hid/controllers/stubbed.cpp index f9cb61667..e7483bfa2 100644 --- a/src/core/hle/service/hid/controllers/stubbed.cpp +++ b/src/core/hle/service/hid/controllers/stubbed.cpp @@ -22,12 +22,12 @@ void Controller_Stubbed::OnUpdate(const Core::Timing::CoreTiming& core_timing, u return; } - const CommonHeader header{ - .timestamp = static_cast(core_timing.GetCPUTicks()), - .total_entry_count = 17, - .last_entry_index = 0, - .entry_count = 0, - }; + CommonHeader header{}; + header.timestamp = core_timing.GetCPUTicks(); + header.total_entry_count = 17; + header.entry_count = 0; + header.last_entry_index = 0; + std::memcpy(data + common_offset, &header, sizeof(CommonHeader)); } diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index 06f4134a2..0df395e85 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -22,7 +22,7 @@ void Controller_Touchscreen::OnRelease() {} void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { - shared_memory.header.timestamp = static_cast(core_timing.GetCPUTicks()); + shared_memory.header.timestamp = core_timing.GetCPUTicks(); shared_memory.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -33,12 +33,9 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin shared_memory.header.entry_count = 16; const auto& last_entry = - shared_memory - .shared_memory_entries[static_cast(shared_memory.header.last_entry_index)]; + shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; - auto& cur_entry = - shared_memory - .shared_memory_entries[static_cast(shared_memory.header.last_entry_index)]; + auto& cur_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index 746acbd1c..4d9042adc 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -69,6 +69,6 @@ private: TouchScreenSharedMemory shared_memory{}; std::unique_ptr touch_device; std::unique_ptr touch_btn_device; - u64_le last_touch{}; + s64_le last_touch{}; }; } // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/xpad.cpp b/src/core/hle/service/hid/controllers/xpad.cpp index 60417abb8..2503ef241 100644 --- a/src/core/hle/service/hid/controllers/xpad.cpp +++ b/src/core/hle/service/hid/controllers/xpad.cpp @@ -20,7 +20,7 @@ void Controller_XPad::OnRelease() {} void Controller_XPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) { for (auto& xpad_entry : shared_memory.shared_memory_entries) { - xpad_entry.header.timestamp = static_cast(core_timing.GetCPUTicks()); + xpad_entry.header.timestamp = core_timing.GetCPUTicks(); xpad_entry.header.total_entry_count = 17; if (!IsControllerActivated()) { @@ -30,11 +30,9 @@ void Controller_XPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* } xpad_entry.header.entry_count = 16; - const auto& last_entry = - xpad_entry.pad_states[static_cast(xpad_entry.header.last_entry_index)]; + const auto& last_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; xpad_entry.header.last_entry_index = (xpad_entry.header.last_entry_index + 1) % 17; - auto& cur_entry = - xpad_entry.pad_states[static_cast(xpad_entry.header.last_entry_index)]; + auto& cur_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index 9ad5bbf0d..d8cd10e31 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -23,7 +23,7 @@ namespace Service::LDR { constexpr ResultCode ERROR_INSUFFICIENT_ADDRESS_SPACE{ErrorModule::RO, 2}; -[[maybe_unused]] constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; +constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; constexpr ResultCode ERROR_INVALID_NRO{ErrorModule::Loader, 52}; constexpr ResultCode ERROR_INVALID_NRR{ErrorModule::Loader, 53}; constexpr ResultCode ERROR_MISSING_NRR_HASH{ErrorModule::Loader, 54}; @@ -33,7 +33,7 @@ constexpr ResultCode ERROR_ALREADY_LOADED{ErrorModule::Loader, 57}; constexpr ResultCode ERROR_INVALID_ALIGNMENT{ErrorModule::Loader, 81}; constexpr ResultCode ERROR_INVALID_SIZE{ErrorModule::Loader, 82}; constexpr ResultCode ERROR_INVALID_NRO_ADDRESS{ErrorModule::Loader, 84}; -[[maybe_unused]] constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; +constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; constexpr ResultCode ERROR_NOT_INITIALIZED{ErrorModule::Loader, 87}; constexpr std::size_t MAXIMUM_LOADED_RO{0x40}; diff --git a/src/core/hle/service/mii/manager.cpp b/src/core/hle/service/mii/manager.cpp index 1b75c2ebe..d73b90015 100644 --- a/src/core/hle/service/mii/manager.cpp +++ b/src/core/hle/service/mii/manager.cpp @@ -240,10 +240,10 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eye_type.Assign( eye_type_info.values[GetRandomValue(eye_type_info.values_count)]); - const auto eye_rotate_1{gender != Gender::Male ? 4U : 2U}; - const auto eye_rotate_2{gender != Gender::Male ? 3U : 4U}; - const auto eye_rotate_offset{32U - EyeRotateLookup[eye_rotate_1] + eye_rotate_2}; - const auto eye_rotate{32U - EyeRotateLookup[bf.eye_type]}; + const auto eye_rotate_1{gender != Gender::Male ? 4 : 2}; + const auto eye_rotate_2{gender != Gender::Male ? 3 : 4}; + const auto eye_rotate_offset{32 - EyeRotateLookup[eye_rotate_1] + eye_rotate_2}; + const auto eye_rotate{32 - EyeRotateLookup[bf.eye_type]}; bf.eye_color.Assign( EyeColorLookup[eye_color_info @@ -257,11 +257,11 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eyebrow_type.Assign( eyebrow_type_info.values[GetRandomValue(eyebrow_type_info.values_count)]); - const auto eyebrow_rotate_1{race == Race::Asian ? 6U : 0U}; - const auto eyebrow_y{race == Race::Asian ? 9U : 10U}; - const auto eyebrow_rotate_offset{32U - EyebrowRotateLookup[eyebrow_rotate_1] + 6}; + const auto eyebrow_rotate_1{race == Race::Asian ? 6 : 0}; + const auto eyebrow_y{race == Race::Asian ? 9 : 10}; + const auto eyebrow_rotate_offset{32 - EyebrowRotateLookup[eyebrow_rotate_1] + 6}; const auto eyebrow_rotate{ - 32U - EyebrowRotateLookup[static_cast(bf.eyebrow_type.Value())]}; + 32 - EyebrowRotateLookup[static_cast(bf.eyebrow_type.Value())]}; bf.eyebrow_color.Assign(bf.hair_color); bf.eyebrow_scale.Assign(4); @@ -270,14 +270,14 @@ MiiStoreData BuildRandomStoreData(Age age, Gender gender, Race race, const Commo bf.eyebrow_x.Assign(2); bf.eyebrow_y.Assign(axis_y + eyebrow_y); - const auto nose_scale{gender == Gender::Female ? 3U : 4U}; + const auto nose_scale{gender == Gender::Female ? 3 : 4}; bf.nose_type.Assign( nose_type_info.values[GetRandomValue(nose_type_info.values_count)]); bf.nose_scale.Assign(nose_scale); bf.nose_y.Assign(axis_y + 9); - const auto mouth_color{gender == Gender::Female ? GetRandomValue(4) : 0U}; + const auto mouth_color{gender == Gender::Female ? GetRandomValue(4) : 0}; bf.mouth_type.Assign( mouth_type_info.values[GetRandomValue(mouth_type_info.values_count)]); diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp index 0dd23ec9e..a0469ffbd 100644 --- a/src/core/hle/service/nfp/nfp.cpp +++ b/src/core/hle/service/nfp/nfp.cpp @@ -217,7 +217,7 @@ private: const auto& amiibo = nfp_interface.GetAmiiboBuffer(); const TagInfo tag_info{ .uuid = amiibo.uuid, - .uuid_length = static_cast(amiibo.uuid.size()), + .uuid_length = static_cast(tag_info.uuid.size()), .padding_1 = {}, .protocol = 1, // TODO(ogniK): Figure out actual values .tag_type = 2, diff --git a/src/core/hle/service/ns/ns.cpp b/src/core/hle/service/ns/ns.cpp index 3edee6303..58ee1f712 100644 --- a/src/core/hle/service/ns/ns.cpp +++ b/src/core/hle/service/ns/ns.cpp @@ -368,7 +368,7 @@ ResultVal IApplicationManagerInterface::GetApplicationDesiredLanguage( // Get language code from settings const auto language_code = - Set::GetLanguageCodeFromIndex(static_cast(Settings::values.language_index.GetValue())); + Set::GetLanguageCodeFromIndex(Settings::values.language_index.GetValue()); // Convert to application language, get priority list const auto application_language = ConvertToApplicationLanguage(language_code); diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 5ccec2637..40838a225 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -50,9 +50,19 @@ constexpr std::array, 7> SHARED_FONTS{ std::make_pair(FontArchives::Extension, "nintendo_ext2_003.bfttf"), }; +constexpr std::array SHARED_FONTS_TTF{ + "FontStandard.ttf", + "FontChineseSimplified.ttf", + "FontExtendedChineseSimplified.ttf", + "FontChineseTraditional.ttf", + "FontKorean.ttf", + "FontNintendoExtended.ttf", + "FontNintendoExtended2.ttf", +}; + // The below data is specific to shared font data dumped from Switch on f/w 2.2 // Virtual address and offsets/sizes likely will vary by dump -[[maybe_unused]] constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; +constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; constexpr u32 EXPECTED_RESULT{0x7f9a0218}; // What we expect the decrypted bfttf first 4 bytes to be constexpr u32 EXPECTED_MAGIC{0x36f81a1e}; // What we expect the encrypted bfttf first 4 bytes to be constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000}; diff --git a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp index 3a5bebff3..f2529a12e 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_as_gpu.cpp @@ -155,7 +155,7 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou const auto object{nvmap_dev->GetObject(params.nvmap_handle)}; if (!object) { - LOG_ERROR(Service_NVDRV, "invalid nvmap_handle={:X}", params.nvmap_handle); + LOG_CRITICAL(Service_NVDRV, "invalid nvmap_handle={:X}", params.nvmap_handle); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; } @@ -167,24 +167,21 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou auto& gpu = system.GPU(); u64 page_size{params.page_size}; - if (page_size == 0) { + if (!page_size) { page_size = object->align; } if ((params.flags & AddressSpaceFlags::Remap) != AddressSpaceFlags::None) { - const auto buffer_map = FindBufferMap(static_cast(params.offset)); - - if (buffer_map) { - const auto cpu_addr{ - static_cast(buffer_map->CpuAddr() + static_cast(params.buffer_offset))}; + if (const auto buffer_map{FindBufferMap(params.offset)}; buffer_map) { + const auto cpu_addr{static_cast(buffer_map->CpuAddr() + params.buffer_offset)}; const auto gpu_addr{static_cast(params.offset + params.buffer_offset)}; if (!gpu.MemoryManager().Map(cpu_addr, gpu_addr, params.mapping_size)) { - LOG_ERROR(Service_NVDRV, - "Remap failed, flags={:X}, nvmap_handle={:X}, buffer_offset={}, " - "mapping_size = {}, offset={}", - params.flags, params.nvmap_handle, params.buffer_offset, - params.mapping_size, params.offset); + LOG_CRITICAL(Service_NVDRV, + "remap failed, flags={:X}, nvmap_handle={:X}, buffer_offset={}, " + "mapping_size = {}, offset={}", + params.flags, params.nvmap_handle, params.buffer_offset, + params.mapping_size, params.offset); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; @@ -193,7 +190,7 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::Success; } else { - LOG_ERROR(Service_NVDRV, "Address not mapped. offset={}", params.offset); + LOG_CRITICAL(Service_NVDRV, "address not mapped offset={}", params.offset); std::memcpy(output.data(), ¶ms, output.size()); return NvErrCodes::InvalidInput; @@ -203,27 +200,25 @@ u32 nvhost_as_gpu::MapBufferEx(const std::vector& input, std::vector& ou // We can only map objects that have already been assigned a CPU address. ASSERT(object->status == nvmap::Object::Status::Allocated); - const auto physical_address{object->addr + static_cast(params.buffer_offset)}; + const auto physical_address{object->addr + params.buffer_offset}; u64 size{params.mapping_size}; - if (size == 0) { + if (!size) { size = object->size; } const bool is_alloc{(params.flags & AddressSpaceFlags::FixedOffset) == AddressSpaceFlags::None}; if (is_alloc) { - params.offset = - static_cast(gpu.MemoryManager().MapAllocate(physical_address, size, page_size)); + params.offset = gpu.MemoryManager().MapAllocate(physical_address, size, page_size); } else { - params.offset = static_cast( - gpu.MemoryManager().Map(physical_address, static_cast(params.offset), size)); + params.offset = gpu.MemoryManager().Map(physical_address, params.offset, size); } auto result{NvErrCodes::Success}; - if (params.offset == 0) { - LOG_ERROR(Service_NVDRV, "Failed to map size={}", size); + if (!params.offset) { + LOG_CRITICAL(Service_NVDRV, "failed to map size={}", size); result = NvErrCodes::InvalidInput; } else { - AddBufferMap(static_cast(params.offset), size, physical_address, is_alloc); + AddBufferMap(params.offset, size, physical_address, is_alloc); } std::memcpy(output.data(), ¶ms, output.size()); @@ -234,13 +229,12 @@ u32 nvhost_as_gpu::UnmapBuffer(const std::vector& input, std::vector& ou IoctlUnmapBuffer params{}; std::memcpy(¶ms, input.data(), input.size()); - const auto offset = static_cast(params.offset); - LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", offset); + LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", params.offset); - if (const auto size{RemoveBufferMap(offset)}; size) { - system.GPU().MemoryManager().Unmap(offset, *size); + if (const auto size{RemoveBufferMap(params.offset)}; size) { + system.GPU().MemoryManager().Unmap(params.offset, *size); } else { - LOG_ERROR(Service_NVDRV, "invalid offset=0x{:X}", offset); + LOG_ERROR(Service_NVDRV, "invalid offset=0x{:X}", params.offset); } std::memcpy(output.data(), ¶ms, output.size()); diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp index 07d851d0e..b27ee0502 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp @@ -63,7 +63,8 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& return NvResult::BadParameter; } - const u32 event_id = params.value & 0x00FF; + u32 event_id = params.value & 0x00FF; + if (event_id >= MaxNvEvents) { std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::BadParameter; @@ -77,17 +78,16 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& event.writable->Signal(); return NvResult::Success; } - auto lock = gpu.LockSync(); const u32 current_syncpoint_value = gpu.GetSyncpointValue(params.syncpt_id); - const s32 diff = static_cast(current_syncpoint_value - params.threshold); + const s32 diff = current_syncpoint_value - params.threshold; if (diff >= 0) { event.writable->Signal(); params.value = current_syncpoint_value; std::memcpy(output.data(), ¶ms, sizeof(params)); return NvResult::Success; } - const u32 target_value = current_syncpoint_value - static_cast(diff); + const u32 target_value = current_syncpoint_value - diff; if (!is_async) { params.value = 0; @@ -98,7 +98,7 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& return NvResult::Timeout; } - const EventState status = events_interface.status[event_id]; + EventState status = events_interface.status[event_id]; if (event_id < MaxNvEvents || status == EventState::Free || status == EventState::Registered) { events_interface.SetEventStatus(event_id, EventState::Waiting); events_interface.assigned_syncpt[event_id] = params.syncpt_id; @@ -114,7 +114,7 @@ u32 nvhost_ctrl::IocCtrlEventWait(const std::vector& input, std::vector& if (!is_async && ctrl.fresh_call) { ctrl.must_delay = true; ctrl.timeout = params.timeout; - ctrl.event_id = static_cast(event_id); + ctrl.event_id = event_id; return NvResult::Timeout; } std::memcpy(output.data(), ¶ms, sizeof(params)); diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp index 5e51b37be..f1966ac0e 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp @@ -127,7 +127,7 @@ u32 nvhost_gpu::AllocGPFIFOEx2(const std::vector& input, std::vector& ou params.unk3); auto& gpu = system.GPU(); - params.fence_out.id = static_cast(assigned_syncpoints); + params.fence_out.id = assigned_syncpoints; params.fence_out.value = gpu.GetSyncpointValue(assigned_syncpoints); assigned_syncpoints++; std::memcpy(output.data(), ¶ms, output.size()); @@ -166,8 +166,7 @@ u32 nvhost_gpu::SubmitGPFIFO(const std::vector& input, std::vector& outp UNIMPLEMENTED_IF(params.flags.add_increment.Value() != 0); auto& gpu = system.GPU(); - const u32 current_syncpoint_value = - gpu.GetSyncpointValue(static_cast(params.fence_out.id)); + u32 current_syncpoint_value = gpu.GetSyncpointValue(params.fence_out.id); if (params.flags.increment.Value()) { params.fence_out.value += current_syncpoint_value; } else { @@ -201,8 +200,7 @@ u32 nvhost_gpu::KickoffPB(const std::vector& input, std::vector& output, UNIMPLEMENTED_IF(params.flags.add_increment.Value() != 0); auto& gpu = system.GPU(); - const u32 current_syncpoint_value = - gpu.GetSyncpointValue(static_cast(params.fence_out.id)); + u32 current_syncpoint_value = gpu.GetSyncpointValue(params.fence_out.id); if (params.flags.increment.Value()) { params.fence_out.value += current_syncpoint_value; } else { diff --git a/src/core/hle/service/nvdrv/interface.cpp b/src/core/hle/service/nvdrv/interface.cpp index 2f4f73487..88fbfa9b0 100644 --- a/src/core/hle/service/nvdrv/interface.cpp +++ b/src/core/hle/service/nvdrv/interface.cpp @@ -61,9 +61,9 @@ void NVDRV::IoctlBase(Kernel::HLERequestContext& ctx, IoctlVersion version) { if (ctrl.must_delay) { ctrl.fresh_call = false; ctx.SleepClientThread( - "NVServices::DelayedResponse", static_cast(ctrl.timeout), - [=, this](std::shared_ptr, Kernel::HLERequestContext& ctx_, - Kernel::ThreadWakeupReason) { + "NVServices::DelayedResponse", ctrl.timeout, + [=, this](std::shared_ptr thread, Kernel::HLERequestContext& ctx_, + Kernel::ThreadWakeupReason reason) { IoctlCtrl ctrl2{ctrl}; std::vector tmp_output = output; std::vector tmp_output2 = output2; @@ -77,7 +77,7 @@ void NVDRV::IoctlBase(Kernel::HLERequestContext& ctx, IoctlVersion version) { rb.Push(RESULT_SUCCESS); rb.Push(ioctl_result); }, - nvdrv->GetEventWriteable(static_cast(ctrl.event_id))); + nvdrv->GetEventWriteable(ctrl.event_id)); } else { ctx.WriteBuffer(output); if (version == IoctlVersion::Version3) { diff --git a/src/core/hle/service/nvflinger/nvflinger.cpp b/src/core/hle/service/nvflinger/nvflinger.cpp index 621a429bc..c64673dba 100644 --- a/src/core/hle/service/nvflinger/nvflinger.cpp +++ b/src/core/hle/service/nvflinger/nvflinger.cpp @@ -247,7 +247,7 @@ void NVFlinger::Compose() { guard->unlock(); for (u32 fence_id = 0; fence_id < multi_fence.num_fences; fence_id++) { const auto& fence = multi_fence.fences[fence_id]; - gpu.WaitFence(static_cast(fence.id), fence.value); + gpu.WaitFence(fence.id, fence.value); } guard->lock(); diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index bc7476a5b..ba9159ee0 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -80,10 +80,10 @@ namespace Service { std::string_view port_name, const u32* cmd_buff) { // Number of params == bits 0-5 + bits 6-11 - const u32 num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); + int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F); std::string function_string = fmt::format("function '{}': port={}", name, port_name); - for (u32 i = 1; i <= num_params; ++i) { + for (int i = 1; i <= num_params; ++i) { function_string += fmt::format(", cmd_buff[{}]=0x{:X}", i, cmd_buff[i]); } return function_string; diff --git a/src/core/hle/service/set/set.cpp b/src/core/hle/service/set/set.cpp index 82a7aecc7..e64777668 100644 --- a/src/core/hle/service/set/set.cpp +++ b/src/core/hle/service/set/set.cpp @@ -91,8 +91,7 @@ void GetAvailableLanguageCodesImpl(Kernel::HLERequestContext& ctx, std::size_t m } void GetKeyCodeMapImpl(Kernel::HLERequestContext& ctx) { - const auto language_code = - available_language_codes[static_cast(Settings::values.language_index.GetValue())]; + const auto language_code = available_language_codes[Settings::values.language_index.GetValue()]; const auto key_code = std::find_if(language_to_layout.cbegin(), language_to_layout.cend(), [=](const auto& element) { return element.first == language_code; }); @@ -168,8 +167,7 @@ void SET::GetLanguageCode(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); - rb.PushEnum( - available_language_codes[static_cast(Settings::values.language_index.GetValue())]); + rb.PushEnum(available_language_codes[Settings::values.language_index.GetValue()]); } void SET::GetRegionCode(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp index 7cb70064c..a74be9370 100644 --- a/src/core/hle/service/sockets/bsd.cpp +++ b/src/core/hle/service/sockets/bsd.cpp @@ -437,9 +437,9 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco UNIMPLEMENTED_MSG("SOCK_RAW errno management"); } - [[maybe_unused]] const bool unk_flag = (static_cast(type) & 0x20000000U) != 0; + [[maybe_unused]] const bool unk_flag = (static_cast(type) & 0x20000000) != 0; UNIMPLEMENTED_IF_MSG(unk_flag, "Unknown flag in type"); - type = static_cast(static_cast(type) & ~0x20000000U); + type = static_cast(static_cast(type) & ~0x20000000); const s32 fd = FindFreeFileDescriptorHandle(); if (fd < 0) { @@ -447,7 +447,7 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco return {-1, Errno::MFILE}; } - FileDescriptor& descriptor = GetFileDescriptor(fd).emplace(); + FileDescriptor& descriptor = file_descriptors[fd].emplace(); // ENONMEM might be thrown here LOG_INFO(Service, "New socket fd={}", fd); @@ -461,7 +461,7 @@ std::pair BSD::SocketImpl(Domain domain, Type type, Protocol protoco std::pair BSD::PollImpl(std::vector& write_buffer, std::vector read_buffer, s32 nfds, s32 timeout) { - if (write_buffer.size() < static_cast(nfds) * sizeof(PollFD)) { + if (write_buffer.size() < nfds * sizeof(PollFD)) { return {-1, Errno::INVAL}; } @@ -471,7 +471,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector fds(static_cast(nfds)); + std::vector fds(nfds); std::memcpy(fds.data(), read_buffer.data(), length); if (timeout >= 0) { @@ -497,7 +497,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector& descriptor = GetFileDescriptor(pollfd.fd); + const std::optional& descriptor = file_descriptors[pollfd.fd]; if (!descriptor) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", pollfd.fd); pollfd.revents = POLL_NVAL; @@ -508,7 +508,7 @@ std::pair BSD::PollImpl(std::vector& write_buffer, std::vector host_pollfds(fds.size()); std::transform(fds.begin(), fds.end(), host_pollfds.begin(), [this](PollFD pollfd) { Network::PollFD result; - result.socket = GetFileDescriptor(pollfd.fd)->socket.get(); + result.socket = file_descriptors[pollfd.fd]->socket.get(); result.events = TranslatePollEventsToHost(pollfd.events); result.revents = 0; return result; @@ -536,13 +536,13 @@ std::pair BSD::AcceptImpl(s32 fd, std::vector& write_buffer) { return {-1, Errno::MFILE}; } - FileDescriptor& descriptor = *GetFileDescriptor(fd); + FileDescriptor& descriptor = *file_descriptors[fd]; auto [result, bsd_errno] = descriptor.socket->Accept(); if (bsd_errno != Network::Errno::SUCCESS) { return {-1, Translate(bsd_errno)}; } - FileDescriptor& new_descriptor = GetFileDescriptor(new_fd).emplace(); + FileDescriptor& new_descriptor = file_descriptors[new_fd].emplace(); new_descriptor.socket = std::move(result.socket); new_descriptor.is_connection_based = descriptor.is_connection_based; @@ -561,7 +561,7 @@ Errno BSD::BindImpl(s32 fd, const std::vector& addr) { SockAddrIn addr_in; std::memcpy(&addr_in, addr.data(), sizeof(addr_in)); - return Translate(GetFileDescriptor(fd)->socket->Bind(Translate(addr_in))); + return Translate(file_descriptors[fd]->socket->Bind(Translate(addr_in))); } Errno BSD::ConnectImpl(s32 fd, const std::vector& addr) { @@ -573,7 +573,7 @@ Errno BSD::ConnectImpl(s32 fd, const std::vector& addr) { SockAddrIn addr_in; std::memcpy(&addr_in, addr.data(), sizeof(addr_in)); - return Translate(GetFileDescriptor(fd)->socket->Connect(Translate(addr_in))); + return Translate(file_descriptors[fd]->socket->Connect(Translate(addr_in))); } Errno BSD::GetPeerNameImpl(s32 fd, std::vector& write_buffer) { @@ -581,7 +581,7 @@ Errno BSD::GetPeerNameImpl(s32 fd, std::vector& write_buffer) { return Errno::BADF; } - const auto [addr_in, bsd_errno] = GetFileDescriptor(fd)->socket->GetPeerName(); + const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetPeerName(); if (bsd_errno != Network::Errno::SUCCESS) { return Translate(bsd_errno); } @@ -597,7 +597,7 @@ Errno BSD::GetSockNameImpl(s32 fd, std::vector& write_buffer) { return Errno::BADF; } - const auto [addr_in, bsd_errno] = GetFileDescriptor(fd)->socket->GetSockName(); + const auto [addr_in, bsd_errno] = file_descriptors[fd]->socket->GetSockName(); if (bsd_errno != Network::Errno::SUCCESS) { return Translate(bsd_errno); } @@ -612,7 +612,7 @@ Errno BSD::ListenImpl(s32 fd, s32 backlog) { if (!IsFileDescriptorValid(fd)) { return Errno::BADF; } - return Translate(GetFileDescriptor(fd)->socket->Listen(backlog)); + return Translate(file_descriptors[fd]->socket->Listen(backlog)); } std::pair BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) { @@ -620,14 +620,14 @@ std::pair BSD::FcntlImpl(s32 fd, FcntlCmd cmd, s32 arg) { return {-1, Errno::BADF}; } - FileDescriptor& descriptor = *GetFileDescriptor(fd); + FileDescriptor& descriptor = *file_descriptors[fd]; switch (cmd) { case FcntlCmd::GETFL: ASSERT(arg == 0); return {descriptor.flags, Errno::SUCCESS}; case FcntlCmd::SETFL: { - const bool enable = (static_cast(arg) & FLAG_O_NONBLOCK) != 0; + const bool enable = (arg & FLAG_O_NONBLOCK) != 0; const Errno bsd_errno = Translate(descriptor.socket->SetNonBlock(enable)); if (bsd_errno != Errno::SUCCESS) { return {-1, bsd_errno}; @@ -648,7 +648,7 @@ Errno BSD::SetSockOptImpl(s32 fd, u32 level, OptName optname, size_t optlen, con return Errno::BADF; } - Network::Socket* const socket = GetFileDescriptor(fd)->socket.get(); + Network::Socket* const socket = file_descriptors[fd]->socket.get(); if (optname == OptName::LINGER) { ASSERT(optlen == sizeof(Linger)); @@ -689,14 +689,14 @@ Errno BSD::ShutdownImpl(s32 fd, s32 how) { return Errno::BADF; } const Network::ShutdownHow host_how = Translate(static_cast(how)); - return Translate(GetFileDescriptor(fd)->socket->Shutdown(host_how)); + return Translate(file_descriptors[fd]->socket->Shutdown(host_how)); } std::pair BSD::RecvImpl(s32 fd, u32 flags, std::vector& message) { if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } - return Translate(GetFileDescriptor(fd)->socket->Recv(flags, message)); + return Translate(file_descriptors[fd]->socket->Recv(flags, message)); } std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& message, @@ -705,7 +705,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess return {-1, Errno::BADF}; } - FileDescriptor& descriptor = *GetFileDescriptor(fd); + FileDescriptor& descriptor = *file_descriptors[fd]; Network::SockAddrIn addr_in{}; Network::SockAddrIn* p_addr_in = nullptr; @@ -719,7 +719,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess // Apply flags if ((flags & FLAG_MSG_DONTWAIT) != 0) { flags &= ~FLAG_MSG_DONTWAIT; - if ((static_cast(descriptor.flags) & FLAG_O_NONBLOCK) == 0) { + if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { descriptor.socket->SetNonBlock(true); } } @@ -727,7 +727,7 @@ std::pair BSD::RecvFromImpl(s32 fd, u32 flags, std::vector& mess const auto [ret, bsd_errno] = Translate(descriptor.socket->RecvFrom(flags, message, p_addr_in)); // Restore original state - if ((static_cast(descriptor.flags) & FLAG_O_NONBLOCK) == 0) { + if ((descriptor.flags & FLAG_O_NONBLOCK) == 0) { descriptor.socket->SetNonBlock(false); } @@ -748,7 +748,7 @@ std::pair BSD::SendImpl(s32 fd, u32 flags, const std::vector& me if (!IsFileDescriptorValid(fd)) { return {-1, Errno::BADF}; } - return Translate(GetFileDescriptor(fd)->socket->Send(message, flags)); + return Translate(file_descriptors[fd]->socket->Send(message, flags)); } std::pair BSD::SendToImpl(s32 fd, u32 flags, const std::vector& message, @@ -767,8 +767,7 @@ std::pair BSD::SendToImpl(s32 fd, u32 flags, const std::vector& p_addr_in = &addr_in; } - const auto& descriptor = GetFileDescriptor(fd); - return Translate(descriptor->socket->SendTo(flags, message, p_addr_in)); + return Translate(file_descriptors[fd]->socket->SendTo(flags, message, p_addr_in)); } Errno BSD::CloseImpl(s32 fd) { @@ -776,21 +775,20 @@ Errno BSD::CloseImpl(s32 fd) { return Errno::BADF; } - auto& descriptor = GetFileDescriptor(fd); - const Errno bsd_errno = Translate(descriptor->socket->Close()); + const Errno bsd_errno = Translate(file_descriptors[fd]->socket->Close()); if (bsd_errno != Errno::SUCCESS) { return bsd_errno; } LOG_INFO(Service, "Close socket fd={}", fd); - descriptor.reset(); + file_descriptors[fd].reset(); return bsd_errno; } s32 BSD::FindFreeFileDescriptorHandle() noexcept { for (s32 fd = 0; fd < static_cast(file_descriptors.size()); ++fd) { - if (!GetFileDescriptor(fd)) { + if (!file_descriptors[fd]) { return fd; } } @@ -802,7 +800,7 @@ bool BSD::IsFileDescriptorValid(s32 fd) const noexcept { LOG_ERROR(Service, "Invalid file descriptor handle={}", fd); return false; } - if (!GetFileDescriptor(fd)) { + if (!file_descriptors[fd]) { LOG_ERROR(Service, "File descriptor handle={} is not allocated", fd); return false; } @@ -815,12 +813,10 @@ bool BSD::IsBlockingSocket(s32 fd) const noexcept { if (fd > static_cast(MAX_FD) || fd < 0) { return false; } - - const auto& descriptor = GetFileDescriptor(fd); - if (!descriptor) { + if (!file_descriptors[fd]) { return false; } - return (static_cast(descriptor->flags) & FLAG_O_NONBLOCK) != 0; + return (file_descriptors[fd]->flags & FLAG_O_NONBLOCK) != 0; } void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept { @@ -831,14 +827,6 @@ void BSD::BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) co rb.PushEnum(bsd_errno); } -std::optional& BSD::GetFileDescriptor(s32 fd) { - return file_descriptors[static_cast(fd)]; -} - -const std::optional& BSD::GetFileDescriptor(s32 fd) const { - return file_descriptors[static_cast(fd)]; -} - BSD::BSD(Core::System& system, const char* name) : ServiceFramework(name), worker_pool{system, this} { // clang-format off diff --git a/src/core/hle/service/sockets/bsd.h b/src/core/hle/service/sockets/bsd.h index ac9523d83..357531951 100644 --- a/src/core/hle/service/sockets/bsd.h +++ b/src/core/hle/service/sockets/bsd.h @@ -167,9 +167,6 @@ private: void BuildErrnoResponse(Kernel::HLERequestContext& ctx, Errno bsd_errno) const noexcept; - std::optional& GetFileDescriptor(s32 fd); - const std::optional& GetFileDescriptor(s32 fd) const; - std::array, MAX_FD> file_descriptors; BlockingWorkerPool(type)); - return {}; } } diff --git a/src/core/hle/service/time/time_zone_manager.cpp b/src/core/hle/service/time/time_zone_manager.cpp index df0ed924d..bdf0439f2 100644 --- a/src/core/hle/service/time/time_zone_manager.cpp +++ b/src/core/hle/service/time/time_zone_manager.cpp @@ -117,8 +117,7 @@ static constexpr int GetMonthLength(bool is_leap_year, int month) { constexpr std::array month_lengths{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; constexpr std::array month_lengths_leap{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; - const auto month_index = static_cast(month); - return is_leap_year ? month_lengths_leap[month_index] : month_lengths[month_index]; + return is_leap_year ? month_lengths_leap[month] : month_lengths[month]; } static constexpr bool IsDigit(char value) { @@ -321,7 +320,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { int dest_len{}; int dest_offset{}; const char* dest_name{name + offset}; - if (rule.chars.size() < static_cast(char_count)) { + if (rule.chars.size() < std::size_t(char_count)) { return {}; } @@ -344,7 +343,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { return {}; } char_count += dest_len + 1; - if (rule.chars.size() < static_cast(char_count)) { + if (rule.chars.size() < std::size_t(char_count)) { return {}; } if (name[offset] != '\0' && name[offset] != ',' && name[offset] != ';') { @@ -387,7 +386,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { rule.default_type = 0; s64 jan_first{}; - u32 time_count{}; + int time_count{}; int jan_offset{}; int year_beginning{epoch_year}; do { @@ -415,7 +414,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { if (is_reversed || (start_time < end_time && (end_time - start_time < (year_seconds + (std_offset - dest_offset))))) { - if (rule.ats.size() - 2 < time_count) { + if (rule.ats.size() - 2 < std::size_t(time_count)) { break; } @@ -439,7 +438,7 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } jan_offset = 0; } - rule.time_count = static_cast(time_count); + rule.time_count = time_count; if (time_count == 0) { rule.type_count = 1; } else if (years_per_repeat < year - year_beginning) { @@ -452,30 +451,26 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } s64 their_std_offset{}; - for (u32 index = 0; index < static_cast(rule.time_count); ++index) { + for (int index{}; index < rule.time_count; ++index) { const s8 type{rule.types[index]}; - const auto& tti = rule.ttis[static_cast(type)]; - - if (tti.is_standard_time_daylight) { - their_std_offset = -tti.gmt_offset; + if (rule.ttis[type].is_standard_time_daylight) { + their_std_offset = -rule.ttis[type].gmt_offset; } } s64 their_offset{their_std_offset}; - for (u32 index = 0; index < static_cast(rule.time_count); ++index) { + for (int index{}; index < rule.time_count; ++index) { const s8 type{rule.types[index]}; - const auto& tti = rule.ttis[static_cast(type)]; - - rule.types[index] = tti.is_dst ? 1 : 0; - if (!tti.is_gmt) { - if (!tti.is_standard_time_daylight) { + rule.types[index] = rule.ttis[type].is_dst ? 1 : 0; + if (!rule.ttis[type].is_gmt) { + if (!rule.ttis[type].is_standard_time_daylight) { rule.ats[index] += dest_offset - their_std_offset; } else { rule.ats[index] += std_offset - their_std_offset; } } - their_offset = -tti.gmt_offset; - if (!tti.is_dst) { + their_offset = -rule.ttis[type].gmt_offset; + if (!rule.ttis[type].is_dst) { their_std_offset = their_offset; } } @@ -499,16 +494,16 @@ static bool ParsePosixName(const char* name, TimeZoneRule& rule) { } rule.char_count = char_count; - for (std::size_t index = 0; index < static_cast(std_len); ++index) { + for (int index{}; index < std_len; ++index) { rule.chars[index] = std_name[index]; } - rule.chars[static_cast(std_len++)] = '\0'; + rule.chars[std_len++] = '\0'; if (dest_len != 0) { - for (int index = 0; index < dest_len; ++index) { - rule.chars[static_cast(std_len + index)] = dest_name[index]; + for (int index{}; index < dest_len; ++index) { + rule.chars[std_len + index] = dest_name[index]; } - rule.chars[static_cast(std_len + dest_len)] = '\0'; + rule.chars[std_len + dest_len] = '\0'; } return true; @@ -536,33 +531,33 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi int time_count{}; u64 read_offset = sizeof(TzifHeader); - for (size_t index = 0; index < static_cast(time_zone_rule.time_count); ++index) { + for (int index{}; index < time_zone_rule.time_count; ++index) { s64_be at{}; vfs_file->ReadObject(&at, read_offset); time_zone_rule.types[index] = 1; - if (time_count != 0 && at <= time_zone_rule.ats[static_cast(time_count) - 1]) { - if (at < time_zone_rule.ats[static_cast(time_count) - 1]) { + if (time_count != 0 && at <= time_zone_rule.ats[time_count - 1]) { + if (at < time_zone_rule.ats[time_count - 1]) { return {}; } time_zone_rule.types[index - 1] = 0; time_count--; } - time_zone_rule.ats[static_cast(time_count++)] = at; + time_zone_rule.ats[time_count++] = at; read_offset += sizeof(s64_be); } time_count = 0; - for (size_t index = 0; index < static_cast(time_zone_rule.time_count); ++index) { - const auto type{static_cast(*vfs_file->ReadByte(read_offset))}; - read_offset += sizeof(s8); + for (int index{}; index < time_zone_rule.time_count; ++index) { + const u8 type{*vfs_file->ReadByte(read_offset)}; + read_offset += sizeof(u8); if (time_zone_rule.time_count <= type) { return {}; } if (time_zone_rule.types[index] != 0) { - time_zone_rule.types[static_cast(time_count++)] = type; + time_zone_rule.types[time_count++] = type; } } time_zone_rule.time_count = time_count; - for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { + for (int index{}; index < time_zone_rule.type_count; ++index) { TimeTypeInfo& ttis{time_zone_rule.ttis[index]}; u32_be gmt_offset{}; vfs_file->ReadObject(&gmt_offset, read_offset); @@ -584,11 +579,10 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi ttis.abbreviation_list_index = abbreviation_list_index; } - vfs_file->ReadArray(time_zone_rule.chars.data(), static_cast(time_zone_rule.char_count), - read_offset); - time_zone_rule.chars[static_cast(time_zone_rule.char_count)] = '\0'; - read_offset += static_cast(time_zone_rule.char_count); - for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { + vfs_file->ReadArray(time_zone_rule.chars.data(), time_zone_rule.char_count, read_offset); + time_zone_rule.chars[time_zone_rule.char_count] = '\0'; + read_offset += time_zone_rule.char_count; + for (int index{}; index < time_zone_rule.type_count; ++index) { if (header.ttis_std_count == 0) { time_zone_rule.ttis[index].is_standard_time_daylight = false; } else { @@ -601,7 +595,7 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi } } - for (size_t index = 0; index < static_cast(time_zone_rule.type_count); ++index) { + for (int index{}; index < time_zone_rule.type_count; ++index) { if (header.ttis_std_count == 0) { time_zone_rule.ttis[index].is_gmt = false; } else { @@ -625,14 +619,13 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi } std::array temp_name{}; - vfs_file->ReadArray(temp_name.data(), static_cast(bytes_read), read_offset); - if (bytes_read > 2 && temp_name[0] == '\n' && - temp_name[static_cast(bytes_read - 1)] == '\n' && - static_cast(time_zone_rule.type_count) + 2 <= time_zone_rule.ttis.size()) { - temp_name[static_cast(bytes_read - 1)] = '\0'; + vfs_file->ReadArray(temp_name.data(), bytes_read, read_offset); + if (bytes_read > 2 && temp_name[0] == '\n' && temp_name[bytes_read - 1] == '\n' && + std::size_t(time_zone_rule.type_count) + 2 <= time_zone_rule.ttis.size()) { + temp_name[bytes_read - 1] = '\0'; std::array name{}; - std::memcpy(name.data(), temp_name.data() + 1, static_cast(bytes_read - 1)); + std::memcpy(name.data(), temp_name.data() + 1, std::size_t(bytes_read - 1)); TimeZoneRule temp_rule; if (ParsePosixName(name.data(), temp_rule)) { @@ -649,24 +642,24 @@ static bool ParseTimeZoneBinary(TimeZoneRule& time_zone_rule, FileSys::VirtualFi s32 default_type{}; for (default_type = 0; default_type < time_zone_rule.time_count; default_type++) { - if (time_zone_rule.types[static_cast(default_type)] == 0) { + if (time_zone_rule.types[default_type] == 0) { break; } } default_type = default_type < time_zone_rule.time_count ? -1 : 0; if (default_type < 0 && time_zone_rule.time_count > 0 && - time_zone_rule.ttis[static_cast(time_zone_rule.types[0])].is_dst) { + time_zone_rule.ttis[time_zone_rule.types[0]].is_dst) { default_type = time_zone_rule.types[0]; while (--default_type >= 0) { - if (!time_zone_rule.ttis[static_cast(default_type)].is_dst) { + if (!time_zone_rule.ttis[default_type].is_dst) { break; } } } if (default_type < 0) { default_type = 0; - while (time_zone_rule.ttis[static_cast(default_type)].is_dst) { + while (time_zone_rule.ttis[default_type].is_dst) { if (++default_type >= time_zone_rule.type_count) { default_type = 0; break; @@ -756,12 +749,12 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, CalendarTimeInternal& calendar_time, CalendarAdditionalInfo& calendar_additional_info) { if ((rules.go_ahead && time < rules.ats[0]) || - (rules.go_back && time > rules.ats[static_cast(rules.time_count - 1)])) { + (rules.go_back && time > rules.ats[rules.time_count - 1])) { s64 seconds{}; if (time < rules.ats[0]) { seconds = rules.ats[0] - time; } else { - seconds = time - rules.ats[static_cast(rules.time_count - 1)]; + seconds = time - rules.ats[rules.time_count - 1]; } seconds--; @@ -774,8 +767,7 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, } else { new_time -= seconds; } - if (new_time < rules.ats[0] && - new_time > rules.ats[static_cast(rules.time_count - 1)]) { + if (new_time < rules.ats[0] && new_time > rules.ats[rules.time_count - 1]) { return ERROR_TIME_NOT_FOUND; } if (const ResultCode result{ @@ -799,27 +791,25 @@ static ResultCode ToCalendarTimeInternal(const TimeZoneRule& rules, s64 time, s32 low{1}; s32 high{rules.time_count}; while (low < high) { - const s32 mid{(low + high) >> 1}; - if (time < rules.ats[static_cast(mid)]) { + s32 mid{(low + high) >> 1}; + if (time < rules.ats[mid]) { high = mid; } else { low = mid + 1; } } - tti_index = rules.types[static_cast(low - 1)]; + tti_index = rules.types[low - 1]; } - if (const ResultCode result{ - CreateCalendarTime(time, rules.ttis[static_cast(tti_index)].gmt_offset, - calendar_time, calendar_additional_info)}; + if (const ResultCode result{CreateCalendarTime(time, rules.ttis[tti_index].gmt_offset, + calendar_time, calendar_additional_info)}; result != RESULT_SUCCESS) { return result; } - const auto& tti = rules.ttis[static_cast(tti_index)]; - calendar_additional_info.is_dst = tti.is_dst; - const char* time_zone{&rules.chars[static_cast(tti.abbreviation_list_index)]}; - for (size_t index = 0; time_zone[index] != '\0'; ++index) { + calendar_additional_info.is_dst = rules.ttis[tti_index].is_dst; + const char* time_zone{&rules.chars[rules.ttis[tti_index].abbreviation_list_index]}; + for (int index{}; time_zone[index] != '\0'; ++index) { calendar_additional_info.timezone_name[index] = time_zone[index]; } return RESULT_SUCCESS; diff --git a/src/core/hle/service/time/time_zone_service.cpp b/src/core/hle/service/time/time_zone_service.cpp index 8a0227021..ff3a10b3e 100644 --- a/src/core/hle/service/time/time_zone_service.cpp +++ b/src/core/hle/service/time/time_zone_service.cpp @@ -49,12 +49,12 @@ void ITimeZoneService::LoadTimeZoneRule(Kernel::HLERequestContext& ctx) { const auto raw_location_name{rp.PopRaw>()}; std::string location_name; - for (const auto byte : raw_location_name) { + for (const auto& byte : raw_location_name) { // Strip extra bytes if (byte == '\0') { break; } - location_name.push_back(static_cast(byte)); + location_name.push_back(byte); } LOG_DEBUG(Service_Time, "called, location_name={}", location_name); diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 86d0527fc..dca1fcb18 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -220,19 +220,18 @@ public: } const char* GetSectionName(int section) const; const u8* GetSectionDataPtr(int section) const { - if (section < 0 || section >= header->e_shnum) { + if (section < 0 || section >= header->e_shnum) + return nullptr; + if (sections[section].sh_type != SHT_NOBITS) + return GetPtr(sections[section].sh_offset); + else return nullptr; - } - if (sections[section].sh_type != SHT_NOBITS) { - return GetPtr(static_cast(sections[section].sh_offset)); - } - return nullptr; } bool IsCodeSection(int section) const { return sections[section].sh_type == SHT_PROGBITS; } const u8* GetSegmentPtr(int segment) { - return GetPtr(static_cast(segments[segment].p_offset)); + return GetPtr(segments[segment].p_offset); } u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; @@ -259,14 +258,14 @@ ElfReader::ElfReader(void* ptr) { } const char* ElfReader::GetSectionName(int section) const { - if (sections[section].sh_type == SHT_NULL) { + if (sections[section].sh_type == SHT_NULL) return nullptr; - } - const auto name_offset = sections[section].sh_name; - if (const auto* ptr = reinterpret_cast(GetSectionDataPtr(header->e_shstrndx))) { + int name_offset = sections[section].sh_name; + const char* ptr = reinterpret_cast(GetSectionDataPtr(header->e_shstrndx)); + + if (ptr) return ptr + name_offset; - } return nullptr; } @@ -292,7 +291,7 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { for (unsigned int i = 0; i < header->e_phnum; ++i) { const Elf32_Phdr* p = &segments[i]; if (p->p_type == PT_LOAD) { - total_image_size += (p->p_memsz + 0xFFF) & ~0xFFFU; + total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; } } @@ -301,14 +300,14 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { Kernel::CodeSet codeset; - for (u32 i = 0; i < header->e_phnum; ++i) { + for (unsigned int i = 0; i < header->e_phnum; ++i) { const Elf32_Phdr* p = &segments[i]; LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz); if (p->p_type == PT_LOAD) { Kernel::CodeSet::Segment* codeset_segment; - const u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); + u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); if (permission_flags == (PF_R | PF_X)) { codeset_segment = &codeset.CodeSegment(); } else if (permission_flags == (PF_R)) { @@ -330,14 +329,14 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { } const VAddr segment_addr = base_addr + p->p_vaddr; - const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFFU; + const u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF; codeset_segment->offset = current_image_position; codeset_segment->addr = segment_addr; codeset_segment->size = aligned_size; - std::memcpy(program_image.data() + current_image_position, - GetSegmentPtr(static_cast(i)), p->p_filesz); + std::memcpy(program_image.data() + current_image_position, GetSegmentPtr(i), + p->p_filesz); current_image_position += aligned_size; } } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 2ce12df88..b88aa5c40 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -197,7 +197,7 @@ struct Memory::Impl { std::string string; string.reserve(max_length); for (std::size_t i = 0; i < max_length; ++i) { - const auto c = static_cast(Read8(vaddr)); + const char c = Read8(vaddr); if (c == '\0') { break; } diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index d280f7a28..4b3bb4366 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -96,7 +96,7 @@ int LastError() { bool EnableNonBlock(SOCKET fd, bool enable) { u_long value = enable ? 1 : 0; - return ioctlsocket(fd, static_cast(FIONBIO), &value) != SOCKET_ERROR; + return ioctlsocket(fd, FIONBIO, &value) != SOCKET_ERROR; } #elif __unix__ // ^ _WIN32 v __unix__ @@ -140,9 +140,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { result.sin_port = htons(input.portno); - result.sin_addr.s_addr = static_cast( - input.ip[0] | static_cast(input.ip[1] << 8) | static_cast(input.ip[2] << 16) | - static_cast(input.ip[3] << 24)); + result.sin_addr.s_addr = input.ip[0] | input.ip[1] << 8 | input.ip[2] << 16 | input.ip[3] << 24; sockaddr addr; std::memcpy(&addr, &result, sizeof(addr)); @@ -150,7 +148,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { } int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) { - return poll(fds, static_cast(nfds), timeout); + return poll(fds, nfds, timeout); } int closesocket(SOCKET fd) { @@ -160,7 +158,7 @@ int closesocket(SOCKET fd) { linger MakeLinger(bool enable, u32 linger_value) { linger value; value.l_onoff = enable ? 1 : 0; - value.l_linger = static_cast(linger_value); + value.l_linger = linger_value; return value; } @@ -339,7 +337,7 @@ std::pair Poll(std::vector& pollfds, s32 timeout) { std::transform(pollfds.begin(), pollfds.end(), host_pollfds.begin(), [](PollFD fd) { WSAPOLLFD result; result.fd = fd.socket->fd; - result.events = static_cast(TranslatePollEvents(fd.events)); + result.events = TranslatePollEvents(fd.events); result.revents = 0; return result; }); @@ -501,12 +499,12 @@ Errno Socket::Shutdown(ShutdownHow how) { } } -std::pair Socket::Recv(u32 flags, std::vector& message) { +std::pair Socket::Recv(int flags, std::vector& message) { ASSERT(flags == 0); ASSERT(message.size() < static_cast(std::numeric_limits::max())); - const auto result = recv(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0); + const auto result = + recv(fd, reinterpret_cast(message.data()), static_cast(message.size()), 0); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } @@ -524,7 +522,7 @@ std::pair Socket::Recv(u32 flags, std::vector& message) { } } -std::pair Socket::RecvFrom(u32 flags, std::vector& message, SockAddrIn* addr) { +std::pair Socket::RecvFrom(int flags, std::vector& message, SockAddrIn* addr) { ASSERT(flags == 0); ASSERT(message.size() < static_cast(std::numeric_limits::max())); @@ -534,7 +532,7 @@ std::pair Socket::RecvFrom(u32 flags, std::vector& message, Sock sockaddr* const p_addr_in = addr ? &addr_in : nullptr; const auto result = recvfrom(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, p_addr_in, p_addrlen); + static_cast(message.size()), 0, p_addr_in, p_addrlen); if (result != SOCKET_ERROR) { if (addr) { ASSERT(addrlen == sizeof(addr_in)); @@ -556,12 +554,12 @@ std::pair Socket::RecvFrom(u32 flags, std::vector& message, Sock } } -std::pair Socket::Send(const std::vector& message, u32 flags) { +std::pair Socket::Send(const std::vector& message, int flags) { ASSERT(message.size() < static_cast(std::numeric_limits::max())); ASSERT(flags == 0); const auto result = send(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0); + static_cast(message.size()), 0); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } @@ -593,9 +591,8 @@ std::pair Socket::SendTo(u32 flags, const std::vector& message, to = &host_addr_in; } - const auto result = - sendto(fd, reinterpret_cast(message.data()), - static_cast(message.size()), 0, to, static_cast(tolen)); + const auto result = sendto(fd, reinterpret_cast(message.data()), + static_cast(message.size()), 0, to, tolen); if (result != SOCKET_ERROR) { return {static_cast(result), Errno::SUCCESS}; } diff --git a/src/core/network/network.h b/src/core/network/network.h index b95bf68f8..0622e4593 100644 --- a/src/core/network/network.h +++ b/src/core/network/network.h @@ -67,12 +67,12 @@ struct PollFD { u16 revents; }; -constexpr u32 POLL_IN = 1 << 0; -constexpr u32 POLL_PRI = 1 << 1; -constexpr u32 POLL_OUT = 1 << 2; -constexpr u32 POLL_ERR = 1 << 3; -constexpr u32 POLL_HUP = 1 << 4; -constexpr u32 POLL_NVAL = 1 << 5; +constexpr u16 POLL_IN = 1 << 0; +constexpr u16 POLL_PRI = 1 << 1; +constexpr u16 POLL_OUT = 1 << 2; +constexpr u16 POLL_ERR = 1 << 3; +constexpr u16 POLL_HUP = 1 << 4; +constexpr u16 POLL_NVAL = 1 << 5; class NetworkInstance { public: diff --git a/src/core/network/sockets.h b/src/core/network/sockets.h index 1682cbd4e..7bdff0fe4 100644 --- a/src/core/network/sockets.h +++ b/src/core/network/sockets.h @@ -56,11 +56,11 @@ public: Errno Shutdown(ShutdownHow how); - std::pair Recv(u32 flags, std::vector& message); + std::pair Recv(int flags, std::vector& message); - std::pair RecvFrom(u32 flags, std::vector& message, SockAddrIn* addr); + std::pair RecvFrom(int flags, std::vector& message, SockAddrIn* addr); - std::pair Send(const std::vector& message, u32 flags); + std::pair Send(const std::vector& message, int flags); std::pair SendTo(u32 flags, const std::vector& message, const SockAddrIn* addr); -- cgit v1.2.3 From ea20b5c970cb261df93743803a227fafd5403d02 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 21 Oct 2020 22:14:21 -0400 Subject: core: Fix clang build pt.3 Should finally resolve building with clang. --- src/common/math_util.h | 4 ++-- src/core/hle/service/ldr/ldr.cpp | 4 ++-- src/core/hle/service/ns/pl_u.cpp | 12 +----------- src/core/network/network.cpp | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) (limited to 'src/common') diff --git a/src/common/math_util.h b/src/common/math_util.h index 7cec80d57..4c38d8040 100644 --- a/src/common/math_util.h +++ b/src/common/math_util.h @@ -27,7 +27,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(right - left); } else { - return std::abs(static_cast>(right - left)); + return static_cast(std::abs(static_cast>(right - left))); } } @@ -35,7 +35,7 @@ struct Rectangle { if constexpr (std::is_floating_point_v) { return std::abs(bottom - top); } else { - return std::abs(static_cast>(bottom - top)); + return static_cast(std::abs(static_cast>(bottom - top))); } } diff --git a/src/core/hle/service/ldr/ldr.cpp b/src/core/hle/service/ldr/ldr.cpp index d8cd10e31..9ad5bbf0d 100644 --- a/src/core/hle/service/ldr/ldr.cpp +++ b/src/core/hle/service/ldr/ldr.cpp @@ -23,7 +23,7 @@ namespace Service::LDR { constexpr ResultCode ERROR_INSUFFICIENT_ADDRESS_SPACE{ErrorModule::RO, 2}; -constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; +[[maybe_unused]] constexpr ResultCode ERROR_INVALID_MEMORY_STATE{ErrorModule::Loader, 51}; constexpr ResultCode ERROR_INVALID_NRO{ErrorModule::Loader, 52}; constexpr ResultCode ERROR_INVALID_NRR{ErrorModule::Loader, 53}; constexpr ResultCode ERROR_MISSING_NRR_HASH{ErrorModule::Loader, 54}; @@ -33,7 +33,7 @@ constexpr ResultCode ERROR_ALREADY_LOADED{ErrorModule::Loader, 57}; constexpr ResultCode ERROR_INVALID_ALIGNMENT{ErrorModule::Loader, 81}; constexpr ResultCode ERROR_INVALID_SIZE{ErrorModule::Loader, 82}; constexpr ResultCode ERROR_INVALID_NRO_ADDRESS{ErrorModule::Loader, 84}; -constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; +[[maybe_unused]] constexpr ResultCode ERROR_INVALID_NRR_ADDRESS{ErrorModule::Loader, 85}; constexpr ResultCode ERROR_NOT_INITIALIZED{ErrorModule::Loader, 87}; constexpr std::size_t MAXIMUM_LOADED_RO{0x40}; diff --git a/src/core/hle/service/ns/pl_u.cpp b/src/core/hle/service/ns/pl_u.cpp index 40838a225..5ccec2637 100644 --- a/src/core/hle/service/ns/pl_u.cpp +++ b/src/core/hle/service/ns/pl_u.cpp @@ -50,19 +50,9 @@ constexpr std::array, 7> SHARED_FONTS{ std::make_pair(FontArchives::Extension, "nintendo_ext2_003.bfttf"), }; -constexpr std::array SHARED_FONTS_TTF{ - "FontStandard.ttf", - "FontChineseSimplified.ttf", - "FontExtendedChineseSimplified.ttf", - "FontChineseTraditional.ttf", - "FontKorean.ttf", - "FontNintendoExtended.ttf", - "FontNintendoExtended2.ttf", -}; - // The below data is specific to shared font data dumped from Switch on f/w 2.2 // Virtual address and offsets/sizes likely will vary by dump -constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; +[[maybe_unused]] constexpr VAddr SHARED_FONT_MEM_VADDR{0x00000009d3016000ULL}; constexpr u32 EXPECTED_RESULT{0x7f9a0218}; // What we expect the decrypted bfttf first 4 bytes to be constexpr u32 EXPECTED_MAGIC{0x36f81a1e}; // What we expect the encrypted bfttf first 4 bytes to be constexpr u64 SHARED_FONT_MEM_SIZE{0x1100000}; diff --git a/src/core/network/network.cpp b/src/core/network/network.cpp index 4b3bb4366..5ef2e8511 100644 --- a/src/core/network/network.cpp +++ b/src/core/network/network.cpp @@ -148,7 +148,7 @@ sockaddr TranslateFromSockAddrIn(SockAddrIn input) { } int WSAPoll(WSAPOLLFD* fds, ULONG nfds, int timeout) { - return poll(fds, nfds, timeout); + return poll(fds, static_cast(nfds), timeout); } int closesocket(SOCKET fd) { -- cgit v1.2.3 From eb67a45ca82bc01ac843c853fd3c17f2a90e0250 Mon Sep 17 00:00:00 2001 From: ameerj Date: Mon, 26 Oct 2020 23:07:36 -0400 Subject: video_core: NVDEC Implementation This commit aims to implement the NVDEC (Nvidia Decoder) functionality, with video frame decoding being handled by the FFmpeg library. The process begins with Ioctl commands being sent to the NVDEC and VIC (Video Image Composer) emulated devices. These allocate the necessary GPU buffers for the frame data, along with providing information on the incoming video data. A Submit command then signals the GPU to process and decode the frame data. To decode the frame, the respective codec's header must be manually composed from the information provided by NVDEC, then sent with the raw frame data to the ffmpeg library. Currently, H264 and VP9 are supported, with VP9 having some minor artifacting issues related mainly to the reference frame composition in its uncompressed header. Async GPU is not properly implemented at the moment. Co-Authored-By: David <25727384+ogniK5377@users.noreply.github.com> --- CMakeLists.txt | 14 + CMakeModules/CopyYuzuFFmpegDeps.cmake | 10 + externals/find-modules/FindFFmpeg.cmake | 100 ++ src/common/CMakeLists.txt | 2 + src/common/stream.cpp | 47 + src/common/stream.h | 50 + src/core/CMakeLists.txt | 2 + .../hle/service/nvdrv/devices/nvhost_nvdec.cpp | 100 +- src/core/hle/service/nvdrv/devices/nvhost_nvdec.h | 71 +- .../service/nvdrv/devices/nvhost_nvdec_common.cpp | 234 +++++ .../service/nvdrv/devices/nvhost_nvdec_common.h | 168 ++++ src/core/hle/service/nvdrv/devices/nvhost_vic.cpp | 90 +- src/core/hle/service/nvdrv/devices/nvhost_vic.h | 88 +- src/core/hle/service/nvdrv/devices/nvmap.h | 1 + src/core/hle/service/nvdrv/nvdrv.cpp | 4 +- src/core/settings.cpp | 2 + src/core/settings.h | 1 + src/core/telemetry_session.cpp | 2 + src/video_core/CMakeLists.txt | 26 + src/video_core/cdma_pusher.cpp | 171 ++++ src/video_core/cdma_pusher.h | 138 +++ src/video_core/command_classes/codecs/codec.cpp | 114 +++ src/video_core/command_classes/codecs/codec.h | 68 ++ src/video_core/command_classes/codecs/h264.cpp | 276 ++++++ src/video_core/command_classes/codecs/h264.h | 130 +++ src/video_core/command_classes/codecs/vp9.cpp | 1010 ++++++++++++++++++++ src/video_core/command_classes/codecs/vp9.h | 216 +++++ src/video_core/command_classes/codecs/vp9_types.h | 369 +++++++ src/video_core/command_classes/host1x.cpp | 39 + src/video_core/command_classes/host1x.h | 78 ++ src/video_core/command_classes/nvdec.cpp | 56 ++ src/video_core/command_classes/nvdec.h | 39 + src/video_core/command_classes/nvdec_common.h | 48 + src/video_core/command_classes/sync_manager.cpp | 60 ++ src/video_core/command_classes/sync_manager.h | 64 ++ src/video_core/command_classes/vic.cpp | 180 ++++ src/video_core/command_classes/vic.h | 110 +++ src/video_core/gpu.cpp | 11 +- src/video_core/gpu.h | 23 +- src/video_core/gpu_asynch.cpp | 26 +- src/video_core/gpu_asynch.h | 3 +- src/video_core/gpu_synch.cpp | 18 +- src/video_core/gpu_synch.h | 3 +- src/video_core/gpu_thread.cpp | 16 +- src/video_core/gpu_thread.h | 19 +- src/video_core/memory_manager.cpp | 12 +- src/video_core/memory_manager.h | 5 +- src/video_core/video_core.cpp | 5 +- src/yuzu/CMakeLists.txt | 2 + src/yuzu/configuration/config.cpp | 4 + src/yuzu/configuration/configure_graphics.cpp | 10 + src/yuzu/configuration/configure_graphics.h | 1 + src/yuzu/configuration/configure_graphics.ui | 7 + 53 files changed, 4033 insertions(+), 310 deletions(-) create mode 100644 CMakeModules/CopyYuzuFFmpegDeps.cmake create mode 100644 externals/find-modules/FindFFmpeg.cmake create mode 100644 src/common/stream.cpp create mode 100644 src/common/stream.h create mode 100644 src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.cpp create mode 100644 src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.h create mode 100644 src/video_core/cdma_pusher.cpp create mode 100644 src/video_core/cdma_pusher.h create mode 100644 src/video_core/command_classes/codecs/codec.cpp create mode 100644 src/video_core/command_classes/codecs/codec.h create mode 100644 src/video_core/command_classes/codecs/h264.cpp create mode 100644 src/video_core/command_classes/codecs/h264.h create mode 100644 src/video_core/command_classes/codecs/vp9.cpp create mode 100644 src/video_core/command_classes/codecs/vp9.h create mode 100644 src/video_core/command_classes/codecs/vp9_types.h create mode 100644 src/video_core/command_classes/host1x.cpp create mode 100644 src/video_core/command_classes/host1x.h create mode 100644 src/video_core/command_classes/nvdec.cpp create mode 100644 src/video_core/command_classes/nvdec.h create mode 100644 src/video_core/command_classes/nvdec_common.h create mode 100644 src/video_core/command_classes/sync_manager.cpp create mode 100644 src/video_core/command_classes/sync_manager.h create mode 100644 src/video_core/command_classes/vic.cpp create mode 100644 src/video_core/command_classes/vic.h (limited to 'src/common') diff --git a/CMakeLists.txt b/CMakeLists.txt index 45bd03a65..8e9502a97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -263,6 +263,7 @@ if (CONAN_REQUIRED_LIBS) libzip:with_openssl=False libzip:enable_windows_crypto=False ) + conan_check(VERSION 1.24.0 REQUIRED) # Add the bincrafters remote conan_add_remote(NAME bincrafters @@ -354,6 +355,19 @@ if (NOT LIBUSB_FOUND) set(LIBUSB_LIBRARIES usb) endif() +# Use system installed ffmpeg. +if (NOT MSVC) + find_package(FFmpeg REQUIRED) +else() + set(FFMPEG_EXT_NAME "ffmpeg-4.2.1") + set(FFMPEG_PATH "${CMAKE_BINARY_DIR}/externals/${FFMPEG_EXT_NAME}") + download_bundled_external("ffmpeg/" ${FFMPEG_EXT_NAME} "") + set(FFMPEG_FOUND YES) + set(FFMPEG_INCLUDE_DIR "${FFMPEG_PATH}/include" CACHE PATH "Path to FFmpeg headers" FORCE) + set(FFMPEG_LIBRARY_DIR "${FFMPEG_PATH}/bin" CACHE PATH "Path to FFmpeg library" FORCE) + set(FFMPEG_DLL_DIR "${FFMPEG_PATH}/bin" CACHE PATH "Path to FFmpeg dll's" FORCE) +endif() + # Prefer the -pthread flag on Linux. set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) diff --git a/CMakeModules/CopyYuzuFFmpegDeps.cmake b/CMakeModules/CopyYuzuFFmpegDeps.cmake new file mode 100644 index 000000000..cca1eeeab --- /dev/null +++ b/CMakeModules/CopyYuzuFFmpegDeps.cmake @@ -0,0 +1,10 @@ +function(copy_yuzu_FFmpeg_deps target_dir) + include(WindowsCopyFiles) + set(DLL_DEST "${CMAKE_BINARY_DIR}/bin/$/") + windows_copy_files(${target_dir} ${FFMPEG_DLL_DIR} ${DLL_DEST} + avcodec-58.dll + avutil-56.dll + swresample-3.dll + swscale-5.dll + ) +endfunction(copy_yuzu_FFmpeg_deps) diff --git a/externals/find-modules/FindFFmpeg.cmake b/externals/find-modules/FindFFmpeg.cmake new file mode 100644 index 000000000..77b331e00 --- /dev/null +++ b/externals/find-modules/FindFFmpeg.cmake @@ -0,0 +1,100 @@ +# - Try to find ffmpeg libraries (libavcodec, libavformat and libavutil) +# Once done this will define +# +# FFMPEG_FOUND - system has ffmpeg or libav +# FFMPEG_INCLUDE_DIR - the ffmpeg include directory +# FFMPEG_LIBRARIES - Link these to use ffmpeg +# FFMPEG_LIBAVCODEC +# FFMPEG_LIBAVFORMAT +# FFMPEG_LIBAVUTIL +# +# Copyright (c) 2008 Andreas Schneider +# Modified for other libraries by Lasse Kärkkäinen +# Modified for Hedgewars by Stepik777 +# Modified for FFmpeg-example Tuukka Pasanen 2018 +# Modified for yuzu toastUnlimted 2020 +# +# Redistribution and use is allowed according to the terms of the New +# BSD license. +# + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(FFMPEG + FOUND_VAR FFMPEG_FOUND + REQUIRED_VARS + FFMPEG_LIBRARY + FFMPEG_INCLUDE_DIR + VERSION_VAR FFMPEG_VERSION +) + +if(FFMPEG_LIBRARIES AND FFMPEG_INCLUDE_DIR) + # in cache already + set(FFMPEG_FOUND TRUE) +else() + # use pkg-config to get the directories and then use these values + # in the FIND_PATH() and FIND_LIBRARY() calls + find_package(PkgConfig) + if(PKG_CONFIG_FOUND) + pkg_check_modules(_FFMPEG_AVCODEC libavcodec) + pkg_check_modules(_FFMPEG_AVUTIL libavutil) + pkg_check_modules(_FFMPEG_SWSCALE libswscale) + endif() + + find_path(FFMPEG_AVCODEC_INCLUDE_DIR + NAMES libavcodec/avcodec.h + PATHS ${_FFMPEG_AVCODEC_INCLUDE_DIRS} + /usr/include + /usr/local/include + /opt/local/include + /sw/include + PATH_SUFFIXES ffmpeg libav) + + find_library(FFMPEG_LIBAVCODEC + NAMES avcodec + PATHS ${_FFMPEG_AVCODEC_LIBRARY_DIRS} + /usr/lib + /usr/local/lib + /opt/local/lib + /sw/lib) + + find_library(FFMPEG_LIBAVUTIL + NAMES avutil + PATHS ${_FFMPEG_AVUTIL_LIBRARY_DIRS} + /usr/lib + /usr/local/lib + /opt/local/lib + /sw/lib) + + find_library(FFMPEG_LIBSWSCALE + NAMES swscale + PATHS ${_FFMPEG_SWSCALE_LIBRARY_DIRS} + /usr/lib + /usr/local/lib + /opt/local/lib + /sw/lib) + + if(FFMPEG_LIBAVCODEC AND FFMPEG_LIBAVUTIL AND FFMPEG_LIBSWSCALE) + set(FFMPEG_FOUND TRUE) + endif() + + if(FFMPEG_FOUND) + set(FFMPEG_INCLUDE_DIR ${FFMPEG_AVCODEC_INCLUDE_DIR}) + set(FFMPEG_LIBRARIES + ${FFMPEG_LIBAVCODEC} + ${FFMPEG_LIBAVUTIL} + ${FFMPEG_LIBSWSCALE}) + endif() + + if(FFMPEG_FOUND) + if(NOT FFMPEG_FIND_QUIETLY) + message(STATUS + "Found FFMPEG or Libav: ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIR}") + endif() + else() + if(FFMPEG_FIND_REQUIRED) + message(FATAL_ERROR + "Could not find libavcodec or libavutil or libswscale") + endif() + endif() +endif() diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0fb5d9708..e50ab2922 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -150,6 +150,8 @@ add_library(common STATIC scope_exit.h spin_lock.cpp spin_lock.h + stream.cpp + stream.h string_util.cpp string_util.h swap.h diff --git a/src/common/stream.cpp b/src/common/stream.cpp new file mode 100644 index 000000000..bf0496c26 --- /dev/null +++ b/src/common/stream.cpp @@ -0,0 +1,47 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "common/common_types.h" +#include "common/stream.h" + +namespace Common { + +Stream::Stream() = default; +Stream::~Stream() = default; + +void Stream::Seek(s32 offset, SeekOrigin origin) { + if (origin == SeekOrigin::SetOrigin) { + if (offset < 0) { + position = 0; + } else if (position >= buffer.size()) { + position = buffer.size(); + } else { + position = offset; + } + } else if (origin == SeekOrigin::FromCurrentPos) { + Seek(static_cast(position) + offset, SeekOrigin::SetOrigin); + } else if (origin == SeekOrigin::FromEnd) { + Seek(static_cast(buffer.size()) - offset, SeekOrigin::SetOrigin); + } +} + +u8 Stream::ReadByte() { + if (position < buffer.size()) { + return buffer[position++]; + } else { + throw std::out_of_range("Attempting to read a byte not within the buffer range"); + } +} + +void Stream::WriteByte(u8 byte) { + if (position == buffer.size()) { + buffer.push_back(byte); + position++; + } else { + buffer.insert(buffer.begin() + position, byte); + } +} + +} // namespace Common diff --git a/src/common/stream.h b/src/common/stream.h new file mode 100644 index 000000000..2585c16af --- /dev/null +++ b/src/common/stream.h @@ -0,0 +1,50 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "common/common_types.h" + +namespace Common { + +enum class SeekOrigin { + SetOrigin, + FromCurrentPos, + FromEnd, +}; + +class Stream { +public: + /// Stream creates a bitstream and provides common functionality on the stream. + explicit Stream(); + ~Stream(); + + /// Reposition bitstream "cursor" to the specified offset from origin + void Seek(s32 offset, SeekOrigin origin); + + /// Reads next byte in the stream buffer and increments position + u8 ReadByte(); + + /// Writes byte at current position + void WriteByte(u8 byte); + + std::size_t GetPosition() const { + return position; + } + + std::vector& GetBuffer() { + return buffer; + } + + const std::vector& GetBuffer() const { + return buffer; + } + +private: + std::vector buffer; + std::size_t position{0}; +}; + +} // namespace Common diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index db1c9fdef..e0f207f3e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -439,6 +439,8 @@ add_library(core STATIC hle/service/nvdrv/devices/nvhost_gpu.h hle/service/nvdrv/devices/nvhost_nvdec.cpp hle/service/nvdrv/devices/nvhost_nvdec.h + hle/service/nvdrv/devices/nvhost_nvdec_common.cpp + hle/service/nvdrv/devices/nvhost_nvdec_common.h hle/service/nvdrv/devices/nvhost_nvjpg.cpp hle/service/nvdrv/devices/nvhost_nvjpg.h hle/service/nvdrv/devices/nvhost_vic.cpp diff --git a/src/core/hle/service/nvdrv/devices/nvhost_nvdec.cpp b/src/core/hle/service/nvdrv/devices/nvhost_nvdec.cpp index fcb612864..b6df48360 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_nvdec.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_nvdec.cpp @@ -2,15 +2,17 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include - #include "common/assert.h" #include "common/logging/log.h" +#include "core/core.h" #include "core/hle/service/nvdrv/devices/nvhost_nvdec.h" +#include "video_core/memory_manager.h" +#include "video_core/renderer_base.h" namespace Service::Nvidia::Devices { -nvhost_nvdec::nvhost_nvdec(Core::System& system) : nvdevice(system) {} +nvhost_nvdec::nvhost_nvdec(Core::System& system, std::shared_ptr nvmap_dev) + : nvhost_nvdec_common(system, std::move(nvmap_dev)) {} nvhost_nvdec::~nvhost_nvdec() = default; u32 nvhost_nvdec::ioctl(Ioctl command, const std::vector& input, const std::vector& input2, @@ -21,7 +23,7 @@ u32 nvhost_nvdec::ioctl(Ioctl command, const std::vector& input, const std:: switch (static_cast(command.raw)) { case IoctlCommand::IocSetNVMAPfdCommand: - return SetNVMAPfd(input, output); + return SetNVMAPfd(input); case IoctlCommand::IocSubmit: return Submit(input, output); case IoctlCommand::IocGetSyncpoint: @@ -29,79 +31,29 @@ u32 nvhost_nvdec::ioctl(Ioctl command, const std::vector& input, const std:: case IoctlCommand::IocGetWaitbase: return GetWaitbase(input, output); case IoctlCommand::IocMapBuffer: - return MapBuffer(input, output); + case IoctlCommand::IocMapBuffer2: + case IoctlCommand::IocMapBuffer3: case IoctlCommand::IocMapBufferEx: - return MapBufferEx(input, output); - case IoctlCommand::IocUnmapBufferEx: - return UnmapBufferEx(input, output); + return MapBuffer(input, output); + case IoctlCommand::IocUnmapBufferEx: { + // This command is sent when the video stream has ended, flush all video contexts + // This is usually sent in the folowing order: vic, nvdec, vic. + // Inform the GPU to clear any remaining nvdec buffers when this is detected. + LOG_INFO(Service_NVDRV, "NVDEC video stream ended"); + Tegra::ChCommandHeaderList cmdlist(1); + cmdlist[0] = Tegra::ChCommandHeader{0xDEADB33F}; + system.GPU().PushCommandBuffer(cmdlist); + [[fallthrough]]; // fallthrough to unmap buffers + }; + case IoctlCommand::IocUnmapBuffer: + case IoctlCommand::IocUnmapBuffer2: + case IoctlCommand::IocUnmapBuffer3: + return UnmapBuffer(input, output); + case IoctlCommand::IocSetSubmitTimeout: + return SetSubmitTimeout(input, output); } - UNIMPLEMENTED_MSG("Unimplemented ioctl"); - return 0; -} - -u32 nvhost_nvdec::SetNVMAPfd(const std::vector& input, std::vector& output) { - IoctlSetNvmapFD params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlSetNvmapFD)); - LOG_DEBUG(Service_NVDRV, "called, fd={}", params.nvmap_fd); - - nvmap_fd = params.nvmap_fd; - return 0; -} - -u32 nvhost_nvdec::Submit(const std::vector& input, std::vector& output) { - IoctlSubmit params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlSubmit)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called"); - std::memcpy(output.data(), ¶ms, sizeof(IoctlSubmit)); - return 0; -} - -u32 nvhost_nvdec::GetSyncpoint(const std::vector& input, std::vector& output) { - IoctlGetSyncpoint params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlGetSyncpoint)); - LOG_INFO(Service_NVDRV, "called, unknown=0x{:X}", params.unknown); - params.value = 0; // Seems to be hard coded at 0 - std::memcpy(output.data(), ¶ms, sizeof(IoctlGetSyncpoint)); - return 0; -} - -u32 nvhost_nvdec::GetWaitbase(const std::vector& input, std::vector& output) { - IoctlGetWaitbase params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlGetWaitbase)); - LOG_INFO(Service_NVDRV, "called, unknown=0x{:X}", params.unknown); - params.value = 0; // Seems to be hard coded at 0 - std::memcpy(output.data(), ¶ms, sizeof(IoctlGetWaitbase)); - return 0; -} - -u32 nvhost_nvdec::MapBuffer(const std::vector& input, std::vector& output) { - IoctlMapBuffer params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlMapBuffer)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called with address={:08X}{:08X}", params.address_2, - params.address_1); - params.address_1 = 0; - params.address_2 = 0; - std::memcpy(output.data(), ¶ms, sizeof(IoctlMapBuffer)); - return 0; -} - -u32 nvhost_nvdec::MapBufferEx(const std::vector& input, std::vector& output) { - IoctlMapBufferEx params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlMapBufferEx)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called with address={:08X}{:08X}", params.address_2, - params.address_1); - params.address_1 = 0; - params.address_2 = 0; - std::memcpy(output.data(), ¶ms, sizeof(IoctlMapBufferEx)); - return 0; -} - -u32 nvhost_nvdec::UnmapBufferEx(const std::vector& input, std::vector& output) { - IoctlUnmapBufferEx params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlUnmapBufferEx)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called"); - std::memcpy(output.data(), ¶ms, sizeof(IoctlUnmapBufferEx)); + UNIMPLEMENTED_MSG("Unimplemented ioctl 0x{:X}", command.raw); return 0; } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_nvdec.h b/src/core/hle/service/nvdrv/devices/nvhost_nvdec.h index 4332db118..102777ddd 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_nvdec.h +++ b/src/core/hle/service/nvdrv/devices/nvhost_nvdec.h @@ -4,16 +4,14 @@ #pragma once -#include -#include "common/common_types.h" -#include "common/swap.h" -#include "core/hle/service/nvdrv/devices/nvdevice.h" +#include +#include "core/hle/service/nvdrv/devices/nvhost_nvdec_common.h" namespace Service::Nvidia::Devices { -class nvhost_nvdec final : public nvdevice { +class nvhost_nvdec final : public nvhost_nvdec_common { public: - explicit nvhost_nvdec(Core::System& system); + explicit nvhost_nvdec(Core::System& system, std::shared_ptr nvmap_dev); ~nvhost_nvdec() override; u32 ioctl(Ioctl command, const std::vector& input, const std::vector& input2, @@ -27,62 +25,15 @@ private: IocGetSyncpoint = 0xC0080002, IocGetWaitbase = 0xC0080003, IocMapBuffer = 0xC01C0009, + IocMapBuffer2 = 0xC16C0009, + IocMapBuffer3 = 0xC15C0009, IocMapBufferEx = 0xC0A40009, - IocUnmapBufferEx = 0xC0A4000A, + IocUnmapBuffer = 0xC0A4000A, + IocUnmapBuffer2 = 0xC16C000A, + IocUnmapBufferEx = 0xC01C000A, + IocUnmapBuffer3 = 0xC15C000A, + IocSetSubmitTimeout = 0x40040007, }; - - struct IoctlSetNvmapFD { - u32_le nvmap_fd; - }; - static_assert(sizeof(IoctlSetNvmapFD) == 0x4, "IoctlSetNvmapFD is incorrect size"); - - struct IoctlSubmit { - INSERT_PADDING_BYTES(0x40); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlSubmit) == 0x40, "IoctlSubmit has incorrect size"); - - struct IoctlGetSyncpoint { - u32 unknown; // seems to be ignored? Nintendo added this - u32 value; - }; - static_assert(sizeof(IoctlGetSyncpoint) == 0x08, "IoctlGetSyncpoint has incorrect size"); - - struct IoctlGetWaitbase { - u32 unknown; // seems to be ignored? Nintendo added this - u32 value; - }; - static_assert(sizeof(IoctlGetWaitbase) == 0x08, "IoctlGetWaitbase has incorrect size"); - - struct IoctlMapBuffer { - u32 unknown; - u32 address_1; - u32 address_2; - INSERT_PADDING_BYTES(0x10); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlMapBuffer) == 0x1C, "IoctlMapBuffer is incorrect size"); - - struct IoctlMapBufferEx { - u32 unknown; - u32 address_1; - u32 address_2; - INSERT_PADDING_BYTES(0x98); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlMapBufferEx) == 0xA4, "IoctlMapBufferEx has incorrect size"); - - struct IoctlUnmapBufferEx { - INSERT_PADDING_BYTES(0xA4); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlUnmapBufferEx) == 0xA4, "IoctlUnmapBufferEx has incorrect size"); - - u32_le nvmap_fd{}; - - u32 SetNVMAPfd(const std::vector& input, std::vector& output); - u32 Submit(const std::vector& input, std::vector& output); - u32 GetSyncpoint(const std::vector& input, std::vector& output); - u32 GetWaitbase(const std::vector& input, std::vector& output); - u32 MapBuffer(const std::vector& input, std::vector& output); - u32 MapBufferEx(const std::vector& input, std::vector& output); - u32 UnmapBufferEx(const std::vector& input, std::vector& output); }; } // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.cpp b/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.cpp new file mode 100644 index 000000000..85792495f --- /dev/null +++ b/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.cpp @@ -0,0 +1,234 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include + +#include "common/assert.h" +#include "common/common_types.h" +#include "common/logging/log.h" +#include "core/core.h" +#include "core/hle/service/nvdrv/devices/nvhost_nvdec_common.h" +#include "core/hle/service/nvdrv/devices/nvmap.h" +#include "core/memory.h" +#include "video_core/memory_manager.h" +#include "video_core/renderer_base.h" + +namespace Service::Nvidia::Devices { + +namespace { +// Splice vectors will copy count amount of type T from the input vector into the dst vector. +template +std::size_t SpliceVectors(const std::vector& input, std::vector& dst, std::size_t count, + std::size_t offset) { + std::memcpy(dst.data(), input.data() + offset, count * sizeof(T)); + offset += count * sizeof(T); + return offset; +} + +// Write vectors will write data to the output buffer +template +std::size_t WriteVectors(std::vector& dst, const std::vector& src, std::size_t offset) { + std::memcpy(dst.data() + offset, src.data(), src.size() * sizeof(T)); + offset += src.size() * sizeof(T); + return offset; +} +} // Anonymous namespace + +namespace NvErrCodes { +constexpr u32 Success{}; +constexpr u32 OutOfMemory{static_cast(-12)}; +constexpr u32 InvalidInput{static_cast(-22)}; +} // namespace NvErrCodes + +nvhost_nvdec_common::nvhost_nvdec_common(Core::System& system, std::shared_ptr nvmap_dev) + : nvdevice(system), nvmap_dev(std::move(nvmap_dev)) {} +nvhost_nvdec_common::~nvhost_nvdec_common() = default; + +u32 nvhost_nvdec_common::SetNVMAPfd(const std::vector& input) { + IoctlSetNvmapFD params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlSetNvmapFD)); + LOG_DEBUG(Service_NVDRV, "called, fd={}", params.nvmap_fd); + + nvmap_fd = params.nvmap_fd; + return 0; +} + +u32 nvhost_nvdec_common::Submit(const std::vector& input, std::vector& output) { + IoctlSubmit params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlSubmit)); + LOG_DEBUG(Service_NVDRV, "called NVDEC Submit, cmd_buffer_count={}", params.cmd_buffer_count); + + // Instantiate param buffers + std::size_t offset = sizeof(IoctlSubmit); + std::vector command_buffers(params.cmd_buffer_count); + std::vector relocs(params.relocation_count); + std::vector reloc_shifts(params.relocation_count); + std::vector syncpt_increments(params.syncpoint_count); + std::vector wait_checks(params.syncpoint_count); + std::vector fences(params.fence_count); + + // Splice input into their respective buffers + offset = SpliceVectors(input, command_buffers, params.cmd_buffer_count, offset); + offset = SpliceVectors(input, relocs, params.relocation_count, offset); + offset = SpliceVectors(input, reloc_shifts, params.relocation_count, offset); + offset = SpliceVectors(input, syncpt_increments, params.syncpoint_count, offset); + offset = SpliceVectors(input, wait_checks, params.syncpoint_count, offset); + offset = SpliceVectors(input, fences, params.fence_count, offset); + + // TODO(ameerj): For async gpu, utilize fences for syncpoint 'max' increment + + auto& gpu = system.GPU(); + + for (const auto& cmd_buffer : command_buffers) { + auto object = nvmap_dev->GetObject(cmd_buffer.memory_id); + ASSERT_OR_EXECUTE(object, return NvErrCodes::InvalidInput;); + const auto map = FindBufferMap(object->dma_map_addr); + if (!map) { + LOG_ERROR(Service_NVDRV, "Tried to submit an invalid offset 0x{:X} dma 0x{:X}", + object->addr, object->dma_map_addr); + return 0; + } + Tegra::ChCommandHeaderList cmdlist(cmd_buffer.word_count); + gpu.MemoryManager().ReadBlock(map->StartAddr() + cmd_buffer.offset, cmdlist.data(), + cmdlist.size() * sizeof(u32)); + gpu.PushCommandBuffer(cmdlist); + } + + std::memcpy(output.data(), ¶ms, sizeof(IoctlSubmit)); + // Some games expect command_buffers to be written back + offset = sizeof(IoctlSubmit); + offset = WriteVectors(output, command_buffers, offset); + offset = WriteVectors(output, relocs, offset); + offset = WriteVectors(output, reloc_shifts, offset); + offset = WriteVectors(output, syncpt_increments, offset); + offset = WriteVectors(output, wait_checks, offset); + + return NvErrCodes::Success; +} + +u32 nvhost_nvdec_common::GetSyncpoint(const std::vector& input, std::vector& output) { + IoctlGetSyncpoint params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlGetSyncpoint)); + LOG_DEBUG(Service_NVDRV, "called GetSyncpoint, id={}", params.param); + + // We found that implementing this causes deadlocks with async gpu, along with degraded + // performance. TODO: RE the nvdec async implementation + params.value = 0; + std::memcpy(output.data(), ¶ms, sizeof(IoctlGetSyncpoint)); + + return NvErrCodes::Success; +} + +u32 nvhost_nvdec_common::GetWaitbase(const std::vector& input, std::vector& output) { + IoctlGetWaitbase params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlGetWaitbase)); + params.value = 0; // Seems to be hard coded at 0 + std::memcpy(output.data(), ¶ms, sizeof(IoctlGetWaitbase)); + return 0; +} + +u32 nvhost_nvdec_common::MapBuffer(const std::vector& input, std::vector& output) { + IoctlMapBuffer params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlMapBuffer)); + std::vector cmd_buffer_handles(params.num_entries); + + SpliceVectors(input, cmd_buffer_handles, params.num_entries, sizeof(IoctlMapBuffer)); + + auto& gpu = system.GPU(); + + for (auto& cmf_buff : cmd_buffer_handles) { + auto object{nvmap_dev->GetObject(cmf_buff.map_handle)}; + if (!object) { + LOG_ERROR(Service_NVDRV, "invalid cmd_buffer nvmap_handle={:X}", cmf_buff.map_handle); + std::memcpy(output.data(), ¶ms, output.size()); + return NvErrCodes::InvalidInput; + } + if (object->dma_map_addr == 0) { + // NVDEC and VIC memory is in the 32-bit address space + // MapAllocate32 will attempt to map a lower 32-bit value in the shared gpu memory space + const GPUVAddr low_addr = gpu.MemoryManager().MapAllocate32(object->addr, object->size); + object->dma_map_addr = static_cast(low_addr); + // Ensure that the dma_map_addr is indeed in the lower 32-bit address space. + ASSERT(object->dma_map_addr == low_addr); + } + if (!object->dma_map_addr) { + LOG_ERROR(Service_NVDRV, "failed to map size={}", object->size); + } else { + cmf_buff.map_address = object->dma_map_addr; + AddBufferMap(object->dma_map_addr, object->size, object->addr, + object->status == nvmap::Object::Status::Allocated); + } + } + std::memcpy(output.data(), ¶ms, sizeof(IoctlMapBuffer)); + std::memcpy(output.data() + sizeof(IoctlMapBuffer), cmd_buffer_handles.data(), + cmd_buffer_handles.size() * sizeof(MapBufferEntry)); + + return NvErrCodes::Success; +} + +u32 nvhost_nvdec_common::UnmapBuffer(const std::vector& input, std::vector& output) { + IoctlMapBuffer params{}; + std::memcpy(¶ms, input.data(), sizeof(IoctlMapBuffer)); + std::vector cmd_buffer_handles(params.num_entries); + SpliceVectors(input, cmd_buffer_handles, params.num_entries, sizeof(IoctlMapBuffer)); + + auto& gpu = system.GPU(); + + for (auto& cmf_buff : cmd_buffer_handles) { + const auto object{nvmap_dev->GetObject(cmf_buff.map_handle)}; + if (!object) { + LOG_ERROR(Service_NVDRV, "invalid cmd_buffer nvmap_handle={:X}", cmf_buff.map_handle); + std::memcpy(output.data(), ¶ms, output.size()); + return NvErrCodes::InvalidInput; + } + if (const auto size{RemoveBufferMap(object->dma_map_addr)}; size) { + gpu.MemoryManager().Unmap(object->dma_map_addr, *size); + } else { + // This occurs quite frequently, however does not seem to impact functionality + LOG_DEBUG(Service_NVDRV, "invalid offset=0x{:X} dma=0x{:X}", object->addr, + object->dma_map_addr); + } + object->dma_map_addr = 0; + } + std::memset(output.data(), 0, output.size()); + return NvErrCodes::Success; +} + +u32 nvhost_nvdec_common::SetSubmitTimeout(const std::vector& input, std::vector& output) { + std::memcpy(&submit_timeout, input.data(), input.size()); + LOG_WARNING(Service_NVDRV, "(STUBBED) called"); + return NvErrCodes::Success; +} + +std::optional nvhost_nvdec_common::FindBufferMap( + GPUVAddr gpu_addr) const { + const auto it = std::find_if( + buffer_mappings.begin(), buffer_mappings.upper_bound(gpu_addr), [&](const auto& entry) { + return (gpu_addr >= entry.second.StartAddr() && gpu_addr < entry.second.EndAddr()); + }); + + ASSERT(it != buffer_mappings.end()); + return it->second; +} + +void nvhost_nvdec_common::AddBufferMap(GPUVAddr gpu_addr, std::size_t size, VAddr cpu_addr, + bool is_allocated) { + buffer_mappings.insert_or_assign(gpu_addr, BufferMap{gpu_addr, size, cpu_addr, is_allocated}); +} + +std::optional nvhost_nvdec_common::RemoveBufferMap(GPUVAddr gpu_addr) { + const auto iter{buffer_mappings.find(gpu_addr)}; + if (iter == buffer_mappings.end()) { + return std::nullopt; + } + std::size_t size = 0; + if (iter->second.IsAllocated()) { + size = iter->second.Size(); + } + buffer_mappings.erase(iter); + return size; +} + +} // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.h b/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.h new file mode 100644 index 000000000..c249c5349 --- /dev/null +++ b/src/core/hle/service/nvdrv/devices/nvhost_nvdec_common.h @@ -0,0 +1,168 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/common_types.h" +#include "common/swap.h" +#include "core/hle/service/nvdrv/devices/nvdevice.h" + +namespace Service::Nvidia::Devices { +class nvmap; + +class nvhost_nvdec_common : public nvdevice { +public: + explicit nvhost_nvdec_common(Core::System& system, std::shared_ptr nvmap_dev); + ~nvhost_nvdec_common() override; + + virtual u32 ioctl(Ioctl command, const std::vector& input, const std::vector& input2, + std::vector& output, std::vector& output2, IoctlCtrl& ctrl, + IoctlVersion version) = 0; + +protected: + class BufferMap final { + public: + constexpr BufferMap() = default; + + constexpr BufferMap(GPUVAddr start_addr, std::size_t size) + : start_addr{start_addr}, end_addr{start_addr + size} {} + + constexpr BufferMap(GPUVAddr start_addr, std::size_t size, VAddr cpu_addr, + bool is_allocated) + : start_addr{start_addr}, end_addr{start_addr + size}, cpu_addr{cpu_addr}, + is_allocated{is_allocated} {} + + constexpr VAddr StartAddr() const { + return start_addr; + } + + constexpr VAddr EndAddr() const { + return end_addr; + } + + constexpr std::size_t Size() const { + return end_addr - start_addr; + } + + constexpr VAddr CpuAddr() const { + return cpu_addr; + } + + constexpr bool IsAllocated() const { + return is_allocated; + } + + private: + GPUVAddr start_addr{}; + GPUVAddr end_addr{}; + VAddr cpu_addr{}; + bool is_allocated{}; + }; + + struct IoctlSetNvmapFD { + u32_le nvmap_fd; + }; + static_assert(sizeof(IoctlSetNvmapFD) == 4, "IoctlSetNvmapFD is incorrect size"); + + struct IoctlSubmitCommandBuffer { + u32_le id; + u32_le offset; + u32_le count; + }; + static_assert(sizeof(IoctlSubmitCommandBuffer) == 0xC, + "IoctlSubmitCommandBuffer is incorrect size"); + struct IoctlSubmit { + u32_le cmd_buffer_count; + u32_le relocation_count; + u32_le syncpoint_count; + u32_le fence_count; + }; + static_assert(sizeof(IoctlSubmit) == 0x10, "IoctlSubmit has incorrect size"); + + struct CommandBuffer { + s32 memory_id; + u32 offset; + s32 word_count; + }; + static_assert(sizeof(CommandBuffer) == 0xC, "CommandBuffer has incorrect size"); + + struct Reloc { + s32 cmdbuffer_memory; + s32 cmdbuffer_offset; + s32 target; + s32 target_offset; + }; + static_assert(sizeof(Reloc) == 0x10, "CommandBuffer has incorrect size"); + + struct SyncptIncr { + u32 id; + u32 increments; + }; + static_assert(sizeof(SyncptIncr) == 0x8, "CommandBuffer has incorrect size"); + + struct Fence { + u32 id; + u32 value; + }; + static_assert(sizeof(Fence) == 0x8, "CommandBuffer has incorrect size"); + + struct IoctlGetSyncpoint { + // Input + u32_le param; + // Output + u32_le value; + }; + static_assert(sizeof(IoctlGetSyncpoint) == 8, "IocGetIdParams has wrong size"); + + struct IoctlGetWaitbase { + u32_le unknown; // seems to be ignored? Nintendo added this + u32_le value; + }; + static_assert(sizeof(IoctlGetWaitbase) == 0x8, "IoctlGetWaitbase is incorrect size"); + + struct IoctlMapBuffer { + u32_le num_entries; + u32_le data_address; // Ignored by the driver. + u32_le attach_host_ch_das; + }; + static_assert(sizeof(IoctlMapBuffer) == 0x0C, "IoctlMapBuffer is incorrect size"); + + struct IocGetIdParams { + // Input + u32_le param; + // Output + u32_le value; + }; + static_assert(sizeof(IocGetIdParams) == 8, "IocGetIdParams has wrong size"); + + // Used for mapping and unmapping command buffers + struct MapBufferEntry { + u32_le map_handle; + u32_le map_address; + }; + static_assert(sizeof(IoctlMapBuffer) == 0x0C, "IoctlMapBuffer is incorrect size"); + + /// Ioctl command implementations + u32 SetNVMAPfd(const std::vector& input); + u32 Submit(const std::vector& input, std::vector& output); + u32 GetSyncpoint(const std::vector& input, std::vector& output); + u32 GetWaitbase(const std::vector& input, std::vector& output); + u32 MapBuffer(const std::vector& input, std::vector& output); + u32 UnmapBuffer(const std::vector& input, std::vector& output); + u32 SetSubmitTimeout(const std::vector& input, std::vector& output); + + std::optional FindBufferMap(GPUVAddr gpu_addr) const; + void AddBufferMap(GPUVAddr gpu_addr, std::size_t size, VAddr cpu_addr, bool is_allocated); + std::optional RemoveBufferMap(GPUVAddr gpu_addr); + + u32_le nvmap_fd{}; + u32_le submit_timeout{}; + std::shared_ptr nvmap_dev; + + // This is expected to be ordered, therefore we must use a map, not unordered_map + std::map buffer_mappings; +}; +}; // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/devices/nvhost_vic.cpp b/src/core/hle/service/nvdrv/devices/nvhost_vic.cpp index 9da19ad56..60db54d00 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_vic.cpp +++ b/src/core/hle/service/nvdrv/devices/nvhost_vic.cpp @@ -2,15 +2,17 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include - #include "common/assert.h" #include "common/logging/log.h" +#include "core/core.h" #include "core/hle/service/nvdrv/devices/nvhost_vic.h" +#include "video_core/memory_manager.h" +#include "video_core/renderer_base.h" namespace Service::Nvidia::Devices { +nvhost_vic::nvhost_vic(Core::System& system, std::shared_ptr nvmap_dev) + : nvhost_nvdec_common(system, std::move(nvmap_dev)) {} -nvhost_vic::nvhost_vic(Core::System& system) : nvdevice(system) {} nvhost_vic::~nvhost_vic() = default; u32 nvhost_vic::ioctl(Ioctl command, const std::vector& input, const std::vector& input2, @@ -21,7 +23,7 @@ u32 nvhost_vic::ioctl(Ioctl command, const std::vector& input, const std::ve switch (static_cast(command.raw)) { case IoctlCommand::IocSetNVMAPfdCommand: - return SetNVMAPfd(input, output); + return SetNVMAPfd(input); case IoctlCommand::IocSubmit: return Submit(input, output); case IoctlCommand::IocGetSyncpoint: @@ -29,83 +31,19 @@ u32 nvhost_vic::ioctl(Ioctl command, const std::vector& input, const std::ve case IoctlCommand::IocGetWaitbase: return GetWaitbase(input, output); case IoctlCommand::IocMapBuffer: - return MapBuffer(input, output); + case IoctlCommand::IocMapBuffer2: + case IoctlCommand::IocMapBuffer3: + case IoctlCommand::IocMapBuffer4: case IoctlCommand::IocMapBufferEx: return MapBuffer(input, output); + case IoctlCommand::IocUnmapBuffer: + case IoctlCommand::IocUnmapBuffer2: + case IoctlCommand::IocUnmapBuffer3: case IoctlCommand::IocUnmapBufferEx: - return UnmapBufferEx(input, output); + return UnmapBuffer(input, output); } - UNIMPLEMENTED_MSG("Unimplemented ioctl"); - return 0; -} - -u32 nvhost_vic::SetNVMAPfd(const std::vector& input, std::vector& output) { - IoctlSetNvmapFD params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlSetNvmapFD)); - LOG_DEBUG(Service_NVDRV, "called, fd={}", params.nvmap_fd); - - nvmap_fd = params.nvmap_fd; - return 0; -} - -u32 nvhost_vic::Submit(const std::vector& input, std::vector& output) { - IoctlSubmit params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlSubmit)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called"); - - // Workaround for Luigi's Mansion 3, as nvhost_vic is not implemented for asynch GPU - params.command_buffer = {}; - - std::memcpy(output.data(), ¶ms, sizeof(IoctlSubmit)); - return 0; -} - -u32 nvhost_vic::GetSyncpoint(const std::vector& input, std::vector& output) { - IoctlGetSyncpoint params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlGetSyncpoint)); - LOG_INFO(Service_NVDRV, "called, unknown=0x{:X}", params.unknown); - params.value = 0; // Seems to be hard coded at 0 - std::memcpy(output.data(), ¶ms, sizeof(IoctlGetSyncpoint)); - return 0; -} - -u32 nvhost_vic::GetWaitbase(const std::vector& input, std::vector& output) { - IoctlGetWaitbase params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlGetWaitbase)); - LOG_INFO(Service_NVDRV, "called, unknown=0x{:X}", params.unknown); - params.value = 0; // Seems to be hard coded at 0 - std::memcpy(output.data(), ¶ms, sizeof(IoctlGetWaitbase)); - return 0; -} - -u32 nvhost_vic::MapBuffer(const std::vector& input, std::vector& output) { - IoctlMapBuffer params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlMapBuffer)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called with address={:08X}{:08X}", params.address_2, - params.address_1); - params.address_1 = 0; - params.address_2 = 0; - std::memcpy(output.data(), ¶ms, sizeof(IoctlMapBuffer)); - return 0; -} - -u32 nvhost_vic::MapBufferEx(const std::vector& input, std::vector& output) { - IoctlMapBufferEx params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlMapBufferEx)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called with address={:08X}{:08X}", params.address_2, - params.address_1); - params.address_1 = 0; - params.address_2 = 0; - std::memcpy(output.data(), ¶ms, sizeof(IoctlMapBufferEx)); - return 0; -} - -u32 nvhost_vic::UnmapBufferEx(const std::vector& input, std::vector& output) { - IoctlUnmapBufferEx params{}; - std::memcpy(¶ms, input.data(), sizeof(IoctlUnmapBufferEx)); - LOG_WARNING(Service_NVDRV, "(STUBBED) called"); - std::memcpy(output.data(), ¶ms, sizeof(IoctlUnmapBufferEx)); + UNIMPLEMENTED_MSG("Unimplemented ioctl 0x{:X}", command.raw); return 0; } diff --git a/src/core/hle/service/nvdrv/devices/nvhost_vic.h b/src/core/hle/service/nvdrv/devices/nvhost_vic.h index a7bb7bbd5..f975b190c 100644 --- a/src/core/hle/service/nvdrv/devices/nvhost_vic.h +++ b/src/core/hle/service/nvdrv/devices/nvhost_vic.h @@ -4,19 +4,15 @@ #pragma once -#include -#include -#include "common/common_types.h" -#include "common/swap.h" -#include "core/hle/service/nvdrv/devices/nvdevice.h" +#include "core/hle/service/nvdrv/devices/nvhost_nvdec_common.h" namespace Service::Nvidia::Devices { +class nvmap; -class nvhost_vic final : public nvdevice { +class nvhost_vic final : public nvhost_nvdec_common { public: - explicit nvhost_vic(Core::System& system); - ~nvhost_vic() override; - + explicit nvhost_vic(Core::System& system, std::shared_ptr nvmap_dev); + ~nvhost_vic(); u32 ioctl(Ioctl command, const std::vector& input, const std::vector& input2, std::vector& output, std::vector& output2, IoctlCtrl& ctrl, IoctlVersion version) override; @@ -28,74 +24,14 @@ private: IocGetSyncpoint = 0xC0080002, IocGetWaitbase = 0xC0080003, IocMapBuffer = 0xC01C0009, + IocMapBuffer2 = 0xC0340009, + IocMapBuffer3 = 0xC0140009, + IocMapBuffer4 = 0xC00C0009, IocMapBufferEx = 0xC03C0009, - IocUnmapBufferEx = 0xC03C000A, - }; - - struct IoctlSetNvmapFD { - u32_le nvmap_fd; - }; - static_assert(sizeof(IoctlSetNvmapFD) == 4, "IoctlSetNvmapFD is incorrect size"); - - struct IoctlSubmitCommandBuffer { - u32 id; - u32 offset; - u32 count; - }; - static_assert(sizeof(IoctlSubmitCommandBuffer) == 0xC, - "IoctlSubmitCommandBuffer is incorrect size"); - - struct IoctlSubmit { - u32 command_buffer_count; - u32 relocations_count; - u32 syncpt_count; - u32 wait_count; - std::array command_buffer; - }; - static_assert(sizeof(IoctlSubmit) == 0x40, "IoctlSubmit is incorrect size"); - - struct IoctlGetSyncpoint { - u32 unknown; // seems to be ignored? Nintendo added this - u32 value; - }; - static_assert(sizeof(IoctlGetSyncpoint) == 0x8, "IoctlGetSyncpoint is incorrect size"); - - struct IoctlGetWaitbase { - u32 unknown; // seems to be ignored? Nintendo added this - u32 value; - }; - static_assert(sizeof(IoctlGetWaitbase) == 0x8, "IoctlGetWaitbase is incorrect size"); - - struct IoctlMapBuffer { - u32 unknown; - u32 address_1; - u32 address_2; - INSERT_PADDING_BYTES(0x10); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlMapBuffer) == 0x1C, "IoctlMapBuffer is incorrect size"); - - struct IoctlMapBufferEx { - u32 unknown; - u32 address_1; - u32 address_2; - INSERT_PADDING_BYTES(0x30); // TODO(DarkLordZach): RE this structure + IocUnmapBuffer = 0xC03C000A, + IocUnmapBuffer2 = 0xC034000A, + IocUnmapBuffer3 = 0xC00C000A, + IocUnmapBufferEx = 0xC01C000A, }; - static_assert(sizeof(IoctlMapBufferEx) == 0x3C, "IoctlMapBufferEx is incorrect size"); - - struct IoctlUnmapBufferEx { - INSERT_PADDING_BYTES(0x3C); // TODO(DarkLordZach): RE this structure - }; - static_assert(sizeof(IoctlUnmapBufferEx) == 0x3C, "IoctlUnmapBufferEx is incorrect size"); - - u32_le nvmap_fd{}; - - u32 SetNVMAPfd(const std::vector& input, std::vector& output); - u32 Submit(const std::vector& input, std::vector& output); - u32 GetSyncpoint(const std::vector& input, std::vector& output); - u32 GetWaitbase(const std::vector& input, std::vector& output); - u32 MapBuffer(const std::vector& input, std::vector& output); - u32 MapBufferEx(const std::vector& input, std::vector& output); - u32 UnmapBufferEx(const std::vector& input, std::vector& output); }; - } // namespace Service::Nvidia::Devices diff --git a/src/core/hle/service/nvdrv/devices/nvmap.h b/src/core/hle/service/nvdrv/devices/nvmap.h index 84624be00..04b9ef540 100644 --- a/src/core/hle/service/nvdrv/devices/nvmap.h +++ b/src/core/hle/service/nvdrv/devices/nvmap.h @@ -37,6 +37,7 @@ public: VAddr addr; Status status; u32 refcount; + u32 dma_map_addr; }; std::shared_ptr GetObject(u32 handle) const { diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp index 197c77db0..803c1a984 100644 --- a/src/core/hle/service/nvdrv/nvdrv.cpp +++ b/src/core/hle/service/nvdrv/nvdrv.cpp @@ -51,9 +51,9 @@ Module::Module(Core::System& system) { devices["/dev/nvmap"] = nvmap_dev; devices["/dev/nvdisp_disp0"] = std::make_shared(system, nvmap_dev); devices["/dev/nvhost-ctrl"] = std::make_shared(system, events_interface); - devices["/dev/nvhost-nvdec"] = std::make_shared(system); + devices["/dev/nvhost-nvdec"] = std::make_shared(system, nvmap_dev); devices["/dev/nvhost-nvjpg"] = std::make_shared(system); - devices["/dev/nvhost-vic"] = std::make_shared(system); + devices["/dev/nvhost-vic"] = std::make_shared(system, nvmap_dev); } Module::~Module() = default; diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 28d3f9099..e14c02045 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -63,6 +63,7 @@ void LogSettings() { log_setting("Renderer_GPUAccuracyLevel", values.gpu_accuracy.GetValue()); log_setting("Renderer_UseAsynchronousGpuEmulation", values.use_asynchronous_gpu_emulation.GetValue()); + log_setting("Renderer_UseNvdecEmulation", values.use_nvdec_emulation.GetValue()); log_setting("Renderer_UseVsync", values.use_vsync.GetValue()); log_setting("Renderer_UseAssemblyShaders", values.use_assembly_shaders.GetValue()); log_setting("Renderer_UseAsynchronousShaders", values.use_asynchronous_shaders.GetValue()); @@ -119,6 +120,7 @@ void RestoreGlobalState() { values.use_disk_shader_cache.SetGlobal(true); values.gpu_accuracy.SetGlobal(true); values.use_asynchronous_gpu_emulation.SetGlobal(true); + values.use_nvdec_emulation.SetGlobal(true); values.use_vsync.SetGlobal(true); values.use_assembly_shaders.SetGlobal(true); values.use_asynchronous_shaders.SetGlobal(true); diff --git a/src/core/settings.h b/src/core/settings.h index 9834f44bb..604805615 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -111,6 +111,7 @@ struct Values { Setting use_disk_shader_cache; Setting gpu_accuracy; Setting use_asynchronous_gpu_emulation; + Setting use_nvdec_emulation; Setting use_vsync; Setting use_assembly_shaders; Setting use_asynchronous_shaders; diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index da09c0dbc..ebc19e18a 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -206,6 +206,8 @@ void TelemetrySession::AddInitialInfo(Loader::AppLoader& app_loader) { TranslateGPUAccuracyLevel(Settings::values.gpu_accuracy.GetValue())); AddField(field_type, "Renderer_UseAsynchronousGpuEmulation", Settings::values.use_asynchronous_gpu_emulation.GetValue()); + AddField(field_type, "Renderer_UseNvdecEmulation", + Settings::values.use_nvdec_emulation.GetValue()); AddField(field_type, "Renderer_UseVsync", Settings::values.use_vsync.GetValue()); AddField(field_type, "Renderer_UseAssemblyShaders", Settings::values.use_assembly_shaders.GetValue()); diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 77ebac19f..fdfc885fc 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -5,6 +5,24 @@ add_library(video_core STATIC buffer_cache/buffer_cache.h buffer_cache/map_interval.cpp buffer_cache/map_interval.h + cdma_pusher.cpp + cdma_pusher.h + command_classes/codecs/codec.cpp + command_classes/codecs/codec.h + command_classes/codecs/h264.cpp + command_classes/codecs/h264.h + command_classes/codecs/vp9.cpp + command_classes/codecs/vp9.h + command_classes/codecs/vp9_types.h + command_classes/host1x.cpp + command_classes/host1x.h + command_classes/nvdec.cpp + command_classes/nvdec.h + command_classes/nvdec_common.h + command_classes/sync_manager.cpp + command_classes/sync_manager.h + command_classes/vic.cpp + command_classes/vic.h compatible_formats.cpp compatible_formats.h dirty_flags.cpp @@ -250,6 +268,14 @@ create_target_directory_groups(video_core) target_link_libraries(video_core PUBLIC common core) target_link_libraries(video_core PRIVATE glad xbyak) +if (MSVC) + target_include_directories(video_core PRIVATE ${FFMPEG_INCLUDE_DIR}) + target_link_libraries(video_core PUBLIC ${FFMPEG_LIBRARY_DIR}/swscale.lib ${FFMPEG_LIBRARY_DIR}/avcodec.lib ${FFMPEG_LIBRARY_DIR}/avutil.lib) +else() + target_include_directories(video_core PRIVATE ${FFMPEG_INCLUDE_DIR}) + target_link_libraries(video_core PRIVATE ${FFMPEG_LIBRARIES}) +endif() + add_dependencies(video_core host_shaders) target_include_directories(video_core PRIVATE ${HOST_SHADERS_INCLUDE}) diff --git a/src/video_core/cdma_pusher.cpp b/src/video_core/cdma_pusher.cpp new file mode 100644 index 000000000..d774db107 --- /dev/null +++ b/src/video_core/cdma_pusher.cpp @@ -0,0 +1,171 @@ +// MIT License +// +// Copyright (c) Ryujinx Team and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +// associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +#include "command_classes/host1x.h" +#include "command_classes/nvdec.h" +#include "command_classes/vic.h" +#include "common/bit_util.h" +#include "video_core/cdma_pusher.h" +#include "video_core/command_classes/nvdec_common.h" +#include "video_core/engines/maxwell_3d.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" + +namespace Tegra { +CDmaPusher::CDmaPusher(GPU& gpu) + : gpu(gpu), nvdec_processor(std::make_shared(gpu)), + vic_processor(std::make_unique(gpu, nvdec_processor)), + host1x_processor(std::make_unique(gpu)), + nvdec_sync(std::make_unique(gpu)), + vic_sync(std::make_unique(gpu)) {} + +CDmaPusher::~CDmaPusher() = default; + +void CDmaPusher::Push(ChCommandHeaderList&& entries) { + cdma_queue.push(std::move(entries)); +} + +void CDmaPusher::DispatchCalls() { + while (!cdma_queue.empty()) { + Step(); + } +} + +void CDmaPusher::Step() { + const auto entries{cdma_queue.front()}; + cdma_queue.pop(); + + std::vector values(entries.size()); + std::memcpy(values.data(), entries.data(), entries.size() * sizeof(u32)); + + for (const u32 value : values) { + if (mask != 0) { + const u32 lbs = Common::CountTrailingZeroes32(mask); + mask &= ~(1U << lbs); + ExecuteCommand(static_cast(offset + lbs), value); + continue; + } else if (count != 0) { + --count; + ExecuteCommand(static_cast(offset), value); + if (incrementing) { + ++offset; + } + continue; + } + const auto mode = static_cast((value >> 28) & 0xf); + switch (mode) { + case ChSubmissionMode::SetClass: { + mask = value & 0x3f; + offset = (value >> 16) & 0xfff; + current_class = static_cast((value >> 6) & 0x3ff); + break; + } + case ChSubmissionMode::Incrementing: + case ChSubmissionMode::NonIncrementing: + count = value & 0xffff; + offset = (value >> 16) & 0xfff; + incrementing = mode == ChSubmissionMode::Incrementing; + break; + case ChSubmissionMode::Mask: + mask = value & 0xffff; + offset = (value >> 16) & 0xfff; + break; + case ChSubmissionMode::Immediate: { + const u32 data = value & 0xfff; + offset = (value >> 16) & 0xfff; + ExecuteCommand(static_cast(offset), data); + break; + } + default: + UNIMPLEMENTED_MSG("ChSubmission mode {} is not implemented!", static_cast(mode)); + break; + } + } +} + +void CDmaPusher::ExecuteCommand(u32 offset, u32 data) { + switch (current_class) { + case ChClassId::NvDec: + ThiStateWrite(nvdec_thi_state, offset, {data}); + switch (static_cast(offset)) { + case ThiMethod::IncSyncpt: { + LOG_DEBUG(Service_NVDRV, "NVDEC Class IncSyncpt Method"); + const auto syncpoint_id = static_cast(data & 0xFF); + const auto cond = static_cast((data >> 8) & 0xFF); + if (cond == 0) { + nvdec_sync->Increment(syncpoint_id); + } else { + nvdec_sync->IncrementWhenDone(static_cast(current_class), syncpoint_id); + nvdec_sync->SignalDone(syncpoint_id); + } + break; + } + case ThiMethod::SetMethod1: + LOG_DEBUG(Service_NVDRV, "NVDEC method 0x{:X}", + static_cast(nvdec_thi_state.method_0)); + nvdec_processor->ProcessMethod( + static_cast(nvdec_thi_state.method_0), {data}); + break; + default: + break; + } + break; + case ChClassId::GraphicsVic: + ThiStateWrite(vic_thi_state, static_cast(offset), {data}); + switch (static_cast(offset)) { + case ThiMethod::IncSyncpt: { + LOG_DEBUG(Service_NVDRV, "VIC Class IncSyncpt Method"); + const auto syncpoint_id = static_cast(data & 0xFF); + const auto cond = static_cast((data >> 8) & 0xFF); + if (cond == 0) { + vic_sync->Increment(syncpoint_id); + } else { + vic_sync->IncrementWhenDone(static_cast(current_class), syncpoint_id); + vic_sync->SignalDone(syncpoint_id); + } + break; + } + case ThiMethod::SetMethod1: + LOG_DEBUG(Service_NVDRV, "VIC method 0x{:X}, Args=({})", + static_cast(vic_thi_state.method_0)); + vic_processor->ProcessMethod(static_cast(vic_thi_state.method_0), + {data}); + break; + default: + break; + } + break; + case ChClassId::Host1x: + // This device is mainly for syncpoint synchronization + LOG_DEBUG(Service_NVDRV, "Host1X Class Method"); + host1x_processor->ProcessMethod(static_cast(offset), {data}); + break; + default: + UNIMPLEMENTED_MSG("Current class not implemented {:X}", static_cast(current_class)); + break; + } +} + +void CDmaPusher::ThiStateWrite(ThiRegisters& state, u32 offset, const std::vector& arguments) { + u8* const state_offset = reinterpret_cast(&state) + sizeof(u32) * offset; + std::memcpy(state_offset, arguments.data(), sizeof(u32) * arguments.size()); +} + +} // namespace Tegra diff --git a/src/video_core/cdma_pusher.h b/src/video_core/cdma_pusher.h new file mode 100644 index 000000000..982f309c5 --- /dev/null +++ b/src/video_core/cdma_pusher.h @@ -0,0 +1,138 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include + +#include "common/bit_field.h" +#include "common/common_types.h" +#include "video_core/command_classes/sync_manager.h" + +namespace Tegra { + +class GPU; +class Nvdec; +class Vic; +class Host1x; + +enum class ChSubmissionMode : u32 { + SetClass = 0, + Incrementing = 1, + NonIncrementing = 2, + Mask = 3, + Immediate = 4, + Restart = 5, + Gather = 6, +}; + +enum class ChClassId : u32 { + NoClass = 0x0, + Host1x = 0x1, + VideoEncodeMpeg = 0x20, + VideoEncodeNvEnc = 0x21, + VideoStreamingVi = 0x30, + VideoStreamingIsp = 0x32, + VideoStreamingIspB = 0x34, + VideoStreamingViI2c = 0x36, + GraphicsVic = 0x5d, + Graphics3D = 0x60, + GraphicsGpu = 0x61, + Tsec = 0xe0, + TsecB = 0xe1, + NvJpg = 0xc0, + NvDec = 0xf0 +}; + +enum class ChMethod : u32 { + Empty = 0, + SetMethod = 0x10, + SetData = 0x11, +}; + +union ChCommandHeader { + u32 raw; + BitField<0, 16, u32> value; + BitField<16, 12, ChMethod> method_offset; + BitField<28, 4, ChSubmissionMode> submission_mode; +}; +static_assert(sizeof(ChCommandHeader) == sizeof(u32), "ChCommand header is an invalid size"); + +struct ChCommand { + ChClassId class_id{}; + int method_offset{}; + std::vector arguments; +}; + +using ChCommandHeaderList = std::vector; +using ChCommandList = std::vector; + +struct ThiRegisters { + u32_le increment_syncpt{}; + INSERT_PADDING_WORDS(1); + u32_le increment_syncpt_error{}; + u32_le ctx_switch_incremement_syncpt{}; + INSERT_PADDING_WORDS(4); + u32_le ctx_switch{}; + INSERT_PADDING_WORDS(1); + u32_le ctx_syncpt_eof{}; + INSERT_PADDING_WORDS(5); + u32_le method_0{}; + u32_le method_1{}; + INSERT_PADDING_WORDS(12); + u32_le int_status{}; + u32_le int_mask{}; +}; + +enum class ThiMethod : u32 { + IncSyncpt = offsetof(ThiRegisters, increment_syncpt) / sizeof(u32), + SetMethod0 = offsetof(ThiRegisters, method_0) / sizeof(u32), + SetMethod1 = offsetof(ThiRegisters, method_1) / sizeof(u32), +}; + +class CDmaPusher { +public: + explicit CDmaPusher(GPU& gpu); + ~CDmaPusher(); + + /// Push NVDEC command buffer entries into queue + void Push(ChCommandHeaderList&& entries); + + /// Process queued command buffer entries + void DispatchCalls(); + + /// Process one queue element + void Step(); + + /// Invoke command class devices to execute the command based on the current state + void ExecuteCommand(u32 offset, u32 data); + +private: + /// Write arguments value to the ThiRegisters member at the specified offset + void ThiStateWrite(ThiRegisters& state, u32 offset, const std::vector& arguments); + + GPU& gpu; + + std::shared_ptr nvdec_processor; + std::unique_ptr vic_processor; + std::unique_ptr host1x_processor; + std::unique_ptr nvdec_sync; + std::unique_ptr vic_sync; + ChClassId current_class{}; + ThiRegisters vic_thi_state{}; + ThiRegisters nvdec_thi_state{}; + + s32 count{}; + s32 offset{}; + s32 mask{}; + bool incrementing{}; + + // Queue of command lists to be processed + std::queue cdma_queue; +}; + +} // namespace Tegra diff --git a/src/video_core/command_classes/codecs/codec.cpp b/src/video_core/command_classes/codecs/codec.cpp new file mode 100644 index 000000000..2df410be8 --- /dev/null +++ b/src/video_core/command_classes/codecs/codec.cpp @@ -0,0 +1,114 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include +#include "common/assert.h" +#include "video_core/command_classes/codecs/codec.h" +#include "video_core/command_classes/codecs/h264.h" +#include "video_core/command_classes/codecs/vp9.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" + +extern "C" { +#include +} + +namespace Tegra { + +Codec::Codec(GPU& gpu_) + : gpu(gpu_), h264_decoder(std::make_unique(gpu)), + vp9_decoder(std::make_unique(gpu)) {} + +Codec::~Codec() { + if (!initialized) { + return; + } + // Free libav memory + avcodec_send_packet(av_codec_ctx, nullptr); + avcodec_receive_frame(av_codec_ctx, av_frame); + avcodec_flush_buffers(av_codec_ctx); + + av_frame_unref(av_frame); + av_free(av_frame); + avcodec_close(av_codec_ctx); +} + +void Codec::SetTargetCodec(NvdecCommon::VideoCodec codec) { + LOG_INFO(Service_NVDRV, "NVDEC video codec initialized to {}", static_cast(codec)); + current_codec = codec; +} + +void Codec::StateWrite(u32 offset, u64 arguments) { + u8* const state_offset = reinterpret_cast(&state) + offset * sizeof(u64); + std::memcpy(state_offset, &arguments, sizeof(u64)); +} + +void Codec::Decode() { + bool is_first_frame = false; + + if (!initialized) { + if (current_codec == NvdecCommon::VideoCodec::H264) { + av_codec = avcodec_find_decoder(AV_CODEC_ID_H264); + } else if (current_codec == NvdecCommon::VideoCodec::Vp9) { + av_codec = avcodec_find_decoder(AV_CODEC_ID_VP9); + } else { + LOG_ERROR(Service_NVDRV, "Unknown video codec {}", static_cast(current_codec)); + return; + } + + av_codec_ctx = avcodec_alloc_context3(av_codec); + av_frame = av_frame_alloc(); + av_opt_set(av_codec_ctx->priv_data, "tune", "zerolatency", 0); + + // TODO(ameerj): libavcodec gpu hw acceleration + + const auto av_error = avcodec_open2(av_codec_ctx, av_codec, nullptr); + if (av_error < 0) { + LOG_ERROR(Service_NVDRV, "avcodec_open2() Failed."); + av_frame_unref(av_frame); + av_free(av_frame); + avcodec_close(av_codec_ctx); + return; + } + initialized = true; + is_first_frame = true; + } + bool vp9_hidden_frame = false; + + AVPacket packet{}; + av_init_packet(&packet); + std::vector frame_data; + + if (current_codec == NvdecCommon::VideoCodec::H264) { + frame_data = h264_decoder->ComposeFrameHeader(state, is_first_frame); + } else if (current_codec == NvdecCommon::VideoCodec::Vp9) { + frame_data = vp9_decoder->ComposeFrameHeader(state); + vp9_hidden_frame = vp9_decoder->WasFrameHidden(); + } + + packet.data = frame_data.data(); + packet.size = static_cast(frame_data.size()); + + avcodec_send_packet(av_codec_ctx, &packet); + + if (!vp9_hidden_frame) { + // Only receive/store visible frames + avcodec_receive_frame(av_codec_ctx, av_frame); + } +} + +AVFrame* Codec::GetCurrentFrame() { + return av_frame; +} + +const AVFrame* Codec::GetCurrentFrame() const { + return av_frame; +} + +NvdecCommon::VideoCodec Codec::GetCurrentCodec() const { + return current_codec; +} + +} // namespace Tegra diff --git a/src/video_core/command_classes/codecs/codec.h b/src/video_core/command_classes/codecs/codec.h new file mode 100644 index 000000000..2e56daf29 --- /dev/null +++ b/src/video_core/command_classes/codecs/codec.h @@ -0,0 +1,68 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "video_core/command_classes/nvdec_common.h" + +extern "C" { +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic ignored "-Wconversion" +#endif +#include +#if defined(__GNUC__) || defined(__clang__) +#pragma GCC diagnostic pop +#endif +} + +namespace Tegra { +class GPU; +struct VicRegisters; + +namespace Decoder { +class H264; +class VP9; +} // namespace Decoder + +class Codec { +public: + explicit Codec(GPU& gpu); + ~Codec(); + + /// Sets NVDEC video stream codec + void SetTargetCodec(NvdecCommon::VideoCodec codec); + + /// Populate NvdecRegisters state with argument value at the provided offset + void StateWrite(u32 offset, u64 arguments); + + /// Call decoders to construct headers, decode AVFrame with ffmpeg + void Decode(); + + /// Returns most recently decoded frame + AVFrame* GetCurrentFrame(); + const AVFrame* GetCurrentFrame() const; + + /// Returns the value of current_codec + NvdecCommon::VideoCodec GetCurrentCodec() const; + +private: + bool initialized{}; + NvdecCommon::VideoCodec current_codec{NvdecCommon::VideoCodec::None}; + + AVCodec* av_codec{nullptr}; + AVCodecContext* av_codec_ctx{nullptr}; + AVFrame* av_frame{nullptr}; + + GPU& gpu; + std::unique_ptr h264_decoder; + std::unique_ptr vp9_decoder; + + NvdecCommon::NvdecRegisters state{}; +}; + +} // namespace Tegra diff --git a/src/video_core/command_classes/codecs/h264.cpp b/src/video_core/command_classes/codecs/h264.cpp new file mode 100644 index 000000000..1a39f7b23 --- /dev/null +++ b/src/video_core/command_classes/codecs/h264.cpp @@ -0,0 +1,276 @@ +// MIT License +// +// Copyright (c) Ryujinx Team and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +// associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +#include "common/bit_util.h" +#include "video_core/command_classes/codecs/h264.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" + +namespace Tegra::Decoder { +H264::H264(GPU& gpu_) : gpu(gpu_) {} + +H264::~H264() = default; + +std::vector& H264::ComposeFrameHeader(NvdecCommon::NvdecRegisters& state, bool is_first_frame) { + H264DecoderContext context{}; + gpu.MemoryManager().ReadBlock(state.picture_info_offset, &context, sizeof(H264DecoderContext)); + + const s32 frame_number = static_cast((context.h264_parameter_set.flags >> 46) & 0x1ffff); + if (!is_first_frame && frame_number != 0) { + frame.resize(context.frame_data_size); + + gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.data(), frame.size()); + } else { + /// Encode header + H264BitWriter writer{}; + writer.WriteU(1, 24); + writer.WriteU(0, 1); + writer.WriteU(3, 2); + writer.WriteU(7, 5); + writer.WriteU(100, 8); + writer.WriteU(0, 8); + writer.WriteU(31, 8); + writer.WriteUe(0); + const s32 chroma_format_idc = (context.h264_parameter_set.flags >> 12) & 0x3; + writer.WriteUe(chroma_format_idc); + if (chroma_format_idc == 3) { + writer.WriteBit(false); + } + + writer.WriteUe(0); + writer.WriteUe(0); + writer.WriteBit(false); // QpprimeYZeroTransformBypassFlag + writer.WriteBit(false); // Scaling matrix present flag + + const s32 order_cnt_type = static_cast((context.h264_parameter_set.flags >> 14) & 3); + writer.WriteUe(static_cast((context.h264_parameter_set.flags >> 8) & 0xf)); + writer.WriteUe(order_cnt_type); + if (order_cnt_type == 0) { + writer.WriteUe(context.h264_parameter_set.log2_max_pic_order_cnt); + } else if (order_cnt_type == 1) { + writer.WriteBit(context.h264_parameter_set.delta_pic_order_always_zero_flag != 0); + + writer.WriteSe(0); + writer.WriteSe(0); + writer.WriteUe(0); + } + + const s32 pic_height = context.h264_parameter_set.pic_height_in_map_units / + (context.h264_parameter_set.frame_mbs_only_flag ? 1 : 2); + + writer.WriteUe(16); + writer.WriteBit(false); + writer.WriteUe(context.h264_parameter_set.pic_width_in_mbs - 1); + writer.WriteUe(pic_height - 1); + writer.WriteBit(context.h264_parameter_set.frame_mbs_only_flag != 0); + + if (!context.h264_parameter_set.frame_mbs_only_flag) { + writer.WriteBit(((context.h264_parameter_set.flags >> 0) & 1) != 0); + } + + writer.WriteBit(((context.h264_parameter_set.flags >> 1) & 1) != 0); + writer.WriteBit(false); // Frame cropping flag + writer.WriteBit(false); // VUI parameter present flag + + writer.End(); + + // H264 PPS + writer.WriteU(1, 24); + writer.WriteU(0, 1); + writer.WriteU(3, 2); + writer.WriteU(8, 5); + + writer.WriteUe(0); + writer.WriteUe(0); + + writer.WriteBit(context.h264_parameter_set.entropy_coding_mode_flag); + writer.WriteBit(false); + writer.WriteUe(0); + writer.WriteUe(context.h264_parameter_set.num_refidx_l0_default_active); + writer.WriteUe(context.h264_parameter_set.num_refidx_l1_default_active); + writer.WriteBit(((context.h264_parameter_set.flags >> 2) & 1) != 0); + writer.WriteU(static_cast((context.h264_parameter_set.flags >> 32) & 0x3), 2); + s32 pic_init_qp = static_cast((context.h264_parameter_set.flags >> 16) & 0x3f); + pic_init_qp = (pic_init_qp << 26) >> 26; + writer.WriteSe(pic_init_qp); + writer.WriteSe(0); + s32 chroma_qp_index_offset = + static_cast((context.h264_parameter_set.flags >> 22) & 0x1f); + chroma_qp_index_offset = (chroma_qp_index_offset << 27) >> 27; + + writer.WriteSe(chroma_qp_index_offset); + writer.WriteBit(context.h264_parameter_set.deblocking_filter_control_flag != 0); + writer.WriteBit(((context.h264_parameter_set.flags >> 3) & 1) != 0); + writer.WriteBit(context.h264_parameter_set.redundant_pic_count_flag != 0); + writer.WriteBit(context.h264_parameter_set.transform_8x8_mode_flag != 0); + + writer.WriteBit(true); + + for (s32 index = 0; index < 6; index++) { + writer.WriteBit(true); + const auto matrix_x4 = + std::vector(context.scaling_matrix_4.begin(), context.scaling_matrix_4.end()); + writer.WriteScalingList(matrix_x4, index * 16, 16); + } + + if (context.h264_parameter_set.transform_8x8_mode_flag) { + for (s32 index = 0; index < 2; index++) { + writer.WriteBit(true); + const auto matrix_x8 = std::vector(context.scaling_matrix_8.begin(), + context.scaling_matrix_8.end()); + + writer.WriteScalingList(matrix_x8, index * 64, 64); + } + } + + s32 chroma_qp_index_offset2 = + static_cast((context.h264_parameter_set.flags >> 27) & 0x1f); + chroma_qp_index_offset2 = (chroma_qp_index_offset2 << 27) >> 27; + + writer.WriteSe(chroma_qp_index_offset2); + + writer.End(); + + const auto& encoded_header = writer.GetByteArray(); + frame.resize(encoded_header.size() + context.frame_data_size); + std::memcpy(frame.data(), encoded_header.data(), encoded_header.size()); + + gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, + frame.data() + encoded_header.size(), + context.frame_data_size); + } + + return frame; +} + +H264BitWriter::H264BitWriter() = default; + +H264BitWriter::~H264BitWriter() = default; + +void H264BitWriter::WriteU(s32 value, s32 value_sz) { + WriteBits(value, value_sz); +} + +void H264BitWriter::WriteSe(s32 value) { + WriteExpGolombCodedInt(value); +} + +void H264BitWriter::WriteUe(s32 value) { + WriteExpGolombCodedUInt((u32)value); +} + +void H264BitWriter::End() { + WriteBit(true); + Flush(); +} + +void H264BitWriter::WriteBit(bool state) { + WriteBits(state ? 1 : 0, 1); +} + +void H264BitWriter::WriteScalingList(const std::vector& list, s32 start, s32 count) { + std::vector scan(count); + if (count == 16) { + std::memcpy(scan.data(), zig_zag_scan.data(), scan.size()); + } else { + std::memcpy(scan.data(), zig_zag_direct.data(), scan.size()); + } + u8 last_scale = 8; + + for (s32 index = 0; index < count; index++) { + const u8 value = list[start + scan[index]]; + const s32 delta_scale = static_cast(value - last_scale); + + WriteSe(delta_scale); + + last_scale = value; + } +} + +std::vector& H264BitWriter::GetByteArray() { + return byte_array; +} + +const std::vector& H264BitWriter::GetByteArray() const { + return byte_array; +} + +void H264BitWriter::WriteBits(s32 value, s32 bit_count) { + s32 value_pos = 0; + + s32 remaining = bit_count; + + while (remaining > 0) { + s32 copy_size = remaining; + + const s32 free_bits = GetFreeBufferBits(); + + if (copy_size > free_bits) { + copy_size = free_bits; + } + + const s32 mask = (1 << copy_size) - 1; + + const s32 src_shift = (bit_count - value_pos) - copy_size; + const s32 dst_shift = (buffer_size - buffer_pos) - copy_size; + + buffer |= ((value >> src_shift) & mask) << dst_shift; + + value_pos += copy_size; + buffer_pos += copy_size; + remaining -= copy_size; + } +} + +void H264BitWriter::WriteExpGolombCodedInt(s32 value) { + const s32 sign = value <= 0 ? 0 : 1; + if (value < 0) { + value = -value; + } + value = (value << 1) - sign; + WriteExpGolombCodedUInt(value); +} + +void H264BitWriter::WriteExpGolombCodedUInt(u32 value) { + const s32 size = 32 - Common::CountLeadingZeroes32(static_cast(value + 1)); + WriteBits(1, size); + + value -= (1U << (size - 1)) - 1; + WriteBits(static_cast(value), size - 1); +} + +s32 H264BitWriter::GetFreeBufferBits() { + if (buffer_pos == buffer_size) { + Flush(); + } + + return buffer_size - buffer_pos; +} + +void H264BitWriter::Flush() { + if (buffer_pos == 0) { + return; + } + byte_array.push_back(static_cast(buffer)); + + buffer = 0; + buffer_pos = 0; +} +} // namespace Tegra::Decoder diff --git a/src/video_core/command_classes/codecs/h264.h b/src/video_core/command_classes/codecs/h264.h new file mode 100644 index 000000000..21752dd90 --- /dev/null +++ b/src/video_core/command_classes/codecs/h264.h @@ -0,0 +1,130 @@ +// MIT License +// +// Copyright (c) Ryujinx Team and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +// associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +#pragma once + +#include +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "video_core/command_classes/nvdec_common.h" + +namespace Tegra { +class GPU; +namespace Decoder { + +class H264BitWriter { +public: + H264BitWriter(); + ~H264BitWriter(); + + /// The following Write methods are based on clause 9.1 in the H.264 specification. + /// WriteSe and WriteUe write in the Exp-Golomb-coded syntax + void WriteU(s32 value, s32 value_sz); + void WriteSe(s32 value); + void WriteUe(s32 value); + + /// Finalize the bitstream + void End(); + + /// append a bit to the stream, equivalent value to the state parameter + void WriteBit(bool state); + + /// Based on section 7.3.2.1.1.1 and Table 7-4 in the H.264 specification + /// Writes the scaling matrices of the sream + void WriteScalingList(const std::vector& list, s32 start, s32 count); + + /// Return the bitstream as a vector. + std::vector& GetByteArray(); + const std::vector& GetByteArray() const; + +private: + // ZigZag LUTs from libavcodec. + static constexpr std::array zig_zag_direct{ + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, + 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, + 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, + }; + + static constexpr std::array zig_zag_scan{ + 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4, + 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4, 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4, + }; + + void WriteBits(s32 value, s32 bit_count); + void WriteExpGolombCodedInt(s32 value); + void WriteExpGolombCodedUInt(u32 value); + s32 GetFreeBufferBits(); + void Flush(); + + s32 buffer_size{8}; + + s32 buffer{}; + s32 buffer_pos{}; + std::vector byte_array; +}; + +class H264 { +public: + explicit H264(GPU& gpu); + ~H264(); + + /// Compose the H264 header of the frame for FFmpeg decoding + std::vector& ComposeFrameHeader(NvdecCommon::NvdecRegisters& state, + bool is_first_frame = false); + +private: + struct H264ParameterSet { + u32 log2_max_pic_order_cnt{}; + u32 delta_pic_order_always_zero_flag{}; + u32 frame_mbs_only_flag{}; + u32 pic_width_in_mbs{}; + u32 pic_height_in_map_units{}; + INSERT_PADDING_WORDS(1); + u32 entropy_coding_mode_flag{}; + u32 bottom_field_pic_order_flag{}; + u32 num_refidx_l0_default_active{}; + u32 num_refidx_l1_default_active{}; + u32 deblocking_filter_control_flag{}; + u32 redundant_pic_count_flag{}; + u32 transform_8x8_mode_flag{}; + INSERT_PADDING_WORDS(9); + u64 flags{}; + u32 frame_number{}; + u32 frame_number2{}; + }; + static_assert(sizeof(H264ParameterSet) == 0x68, "H264ParameterSet is an invalid size"); + + struct H264DecoderContext { + INSERT_PADDING_BYTES(0x48); + u32 frame_data_size{}; + INSERT_PADDING_BYTES(0xc); + H264ParameterSet h264_parameter_set{}; + INSERT_PADDING_BYTES(0x100); + std::array scaling_matrix_4; + std::array scaling_matrix_8; + }; + static_assert(sizeof(H264DecoderContext) == 0x2a0, "H264DecoderContext is an invalid size"); + + std::vector frame; + GPU& gpu; +}; + +} // namespace Decoder +} // namespace Tegra diff --git a/src/video_core/command_classes/codecs/vp9.cpp b/src/video_core/command_classes/codecs/vp9.cpp new file mode 100644 index 000000000..3bae0bb5d --- /dev/null +++ b/src/video_core/command_classes/codecs/vp9.cpp @@ -0,0 +1,1010 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include // for std::memcpy +#include +#include "video_core/command_classes/codecs/vp9.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" + +namespace Tegra::Decoder { + +// Default compressed header probabilities once frame context resets +constexpr Vp9EntropyProbs default_probs{ + .y_mode_prob{ + 65, 32, 18, 144, 162, 194, 41, 51, 98, 132, 68, 18, 165, 217, 196, 45, 40, 78, + 173, 80, 19, 176, 240, 193, 64, 35, 46, 221, 135, 38, 194, 248, 121, 96, 85, 29, + }, + .partition_prob{ + 199, 122, 141, 0, 147, 63, 159, 0, 148, 133, 118, 0, 121, 104, 114, 0, + 174, 73, 87, 0, 92, 41, 83, 0, 82, 99, 50, 0, 53, 39, 39, 0, + 177, 58, 59, 0, 68, 26, 63, 0, 52, 79, 25, 0, 17, 14, 12, 0, + 222, 34, 30, 0, 72, 16, 44, 0, 58, 32, 12, 0, 10, 7, 6, 0, + }, + .coef_probs{ + 195, 29, 183, 0, 84, 49, 136, 0, 8, 42, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 31, 107, 169, 0, 35, 99, 159, 0, 17, 82, 140, 0, 8, 66, 114, 0, + 2, 44, 76, 0, 1, 19, 32, 0, 40, 132, 201, 0, 29, 114, 187, 0, 13, 91, 157, 0, + 7, 75, 127, 0, 3, 58, 95, 0, 1, 28, 47, 0, 69, 142, 221, 0, 42, 122, 201, 0, + 15, 91, 159, 0, 6, 67, 121, 0, 1, 42, 77, 0, 1, 17, 31, 0, 102, 148, 228, 0, + 67, 117, 204, 0, 17, 82, 154, 0, 6, 59, 114, 0, 2, 39, 75, 0, 1, 15, 29, 0, + 156, 57, 233, 0, 119, 57, 212, 0, 58, 48, 163, 0, 29, 40, 124, 0, 12, 30, 81, 0, + 3, 12, 31, 0, 191, 107, 226, 0, 124, 117, 204, 0, 25, 99, 155, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 29, 148, 210, 0, 37, 126, 194, 0, 8, 93, 157, 0, + 2, 68, 118, 0, 1, 39, 69, 0, 1, 17, 33, 0, 41, 151, 213, 0, 27, 123, 193, 0, + 3, 82, 144, 0, 1, 58, 105, 0, 1, 32, 60, 0, 1, 13, 26, 0, 59, 159, 220, 0, + 23, 126, 198, 0, 4, 88, 151, 0, 1, 66, 114, 0, 1, 38, 71, 0, 1, 18, 34, 0, + 114, 136, 232, 0, 51, 114, 207, 0, 11, 83, 155, 0, 3, 56, 105, 0, 1, 33, 65, 0, + 1, 17, 34, 0, 149, 65, 234, 0, 121, 57, 215, 0, 61, 49, 166, 0, 28, 36, 114, 0, + 12, 25, 76, 0, 3, 16, 42, 0, 214, 49, 220, 0, 132, 63, 188, 0, 42, 65, 137, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 137, 221, 0, 104, 131, 216, 0, + 49, 111, 192, 0, 21, 87, 155, 0, 2, 49, 87, 0, 1, 16, 28, 0, 89, 163, 230, 0, + 90, 137, 220, 0, 29, 100, 183, 0, 10, 70, 135, 0, 2, 42, 81, 0, 1, 17, 33, 0, + 108, 167, 237, 0, 55, 133, 222, 0, 15, 97, 179, 0, 4, 72, 135, 0, 1, 45, 85, 0, + 1, 19, 38, 0, 124, 146, 240, 0, 66, 124, 224, 0, 17, 88, 175, 0, 4, 58, 122, 0, + 1, 36, 75, 0, 1, 18, 37, 0, 141, 79, 241, 0, 126, 70, 227, 0, 66, 58, 182, 0, + 30, 44, 136, 0, 12, 34, 96, 0, 2, 20, 47, 0, 229, 99, 249, 0, 143, 111, 235, 0, + 46, 109, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 82, 158, 236, 0, + 94, 146, 224, 0, 25, 117, 191, 0, 9, 87, 149, 0, 3, 56, 99, 0, 1, 33, 57, 0, + 83, 167, 237, 0, 68, 145, 222, 0, 10, 103, 177, 0, 2, 72, 131, 0, 1, 41, 79, 0, + 1, 20, 39, 0, 99, 167, 239, 0, 47, 141, 224, 0, 10, 104, 178, 0, 2, 73, 133, 0, + 1, 44, 85, 0, 1, 22, 47, 0, 127, 145, 243, 0, 71, 129, 228, 0, 17, 93, 177, 0, + 3, 61, 124, 0, 1, 41, 84, 0, 1, 21, 52, 0, 157, 78, 244, 0, 140, 72, 231, 0, + 69, 58, 184, 0, 31, 44, 137, 0, 14, 38, 105, 0, 8, 23, 61, 0, 125, 34, 187, 0, + 52, 41, 133, 0, 6, 31, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 37, 109, 153, 0, 51, 102, 147, 0, 23, 87, 128, 0, 8, 67, 101, 0, 1, 41, 63, 0, + 1, 19, 29, 0, 31, 154, 185, 0, 17, 127, 175, 0, 6, 96, 145, 0, 2, 73, 114, 0, + 1, 51, 82, 0, 1, 28, 45, 0, 23, 163, 200, 0, 10, 131, 185, 0, 2, 93, 148, 0, + 1, 67, 111, 0, 1, 41, 69, 0, 1, 14, 24, 0, 29, 176, 217, 0, 12, 145, 201, 0, + 3, 101, 156, 0, 1, 69, 111, 0, 1, 39, 63, 0, 1, 14, 23, 0, 57, 192, 233, 0, + 25, 154, 215, 0, 6, 109, 167, 0, 3, 78, 118, 0, 1, 48, 69, 0, 1, 21, 29, 0, + 202, 105, 245, 0, 108, 106, 216, 0, 18, 90, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 33, 172, 219, 0, 64, 149, 206, 0, 14, 117, 177, 0, 5, 90, 141, 0, + 2, 61, 95, 0, 1, 37, 57, 0, 33, 179, 220, 0, 11, 140, 198, 0, 1, 89, 148, 0, + 1, 60, 104, 0, 1, 33, 57, 0, 1, 12, 21, 0, 30, 181, 221, 0, 8, 141, 198, 0, + 1, 87, 145, 0, 1, 58, 100, 0, 1, 31, 55, 0, 1, 12, 20, 0, 32, 186, 224, 0, + 7, 142, 198, 0, 1, 86, 143, 0, 1, 58, 100, 0, 1, 31, 55, 0, 1, 12, 22, 0, + 57, 192, 227, 0, 20, 143, 204, 0, 3, 96, 154, 0, 1, 68, 112, 0, 1, 42, 69, 0, + 1, 19, 32, 0, 212, 35, 215, 0, 113, 47, 169, 0, 29, 48, 105, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 74, 129, 203, 0, 106, 120, 203, 0, 49, 107, 178, 0, + 19, 84, 144, 0, 4, 50, 84, 0, 1, 15, 25, 0, 71, 172, 217, 0, 44, 141, 209, 0, + 15, 102, 173, 0, 6, 76, 133, 0, 2, 51, 89, 0, 1, 24, 42, 0, 64, 185, 231, 0, + 31, 148, 216, 0, 8, 103, 175, 0, 3, 74, 131, 0, 1, 46, 81, 0, 1, 18, 30, 0, + 65, 196, 235, 0, 25, 157, 221, 0, 5, 105, 174, 0, 1, 67, 120, 0, 1, 38, 69, 0, + 1, 15, 30, 0, 65, 204, 238, 0, 30, 156, 224, 0, 7, 107, 177, 0, 2, 70, 124, 0, + 1, 42, 73, 0, 1, 18, 34, 0, 225, 86, 251, 0, 144, 104, 235, 0, 42, 99, 181, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 175, 239, 0, 112, 165, 229, 0, + 29, 136, 200, 0, 12, 103, 162, 0, 6, 77, 123, 0, 2, 53, 84, 0, 75, 183, 239, 0, + 30, 155, 221, 0, 3, 106, 171, 0, 1, 74, 128, 0, 1, 44, 76, 0, 1, 17, 28, 0, + 73, 185, 240, 0, 27, 159, 222, 0, 2, 107, 172, 0, 1, 75, 127, 0, 1, 42, 73, 0, + 1, 17, 29, 0, 62, 190, 238, 0, 21, 159, 222, 0, 2, 107, 172, 0, 1, 72, 122, 0, + 1, 40, 71, 0, 1, 18, 32, 0, 61, 199, 240, 0, 27, 161, 226, 0, 4, 113, 180, 0, + 1, 76, 129, 0, 1, 46, 80, 0, 1, 23, 41, 0, 7, 27, 153, 0, 5, 30, 95, 0, + 1, 16, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 75, 127, 0, + 57, 75, 124, 0, 27, 67, 108, 0, 10, 54, 86, 0, 1, 33, 52, 0, 1, 12, 18, 0, + 43, 125, 151, 0, 26, 108, 148, 0, 7, 83, 122, 0, 2, 59, 89, 0, 1, 38, 60, 0, + 1, 17, 27, 0, 23, 144, 163, 0, 13, 112, 154, 0, 2, 75, 117, 0, 1, 50, 81, 0, + 1, 31, 51, 0, 1, 14, 23, 0, 18, 162, 185, 0, 6, 123, 171, 0, 1, 78, 125, 0, + 1, 51, 86, 0, 1, 31, 54, 0, 1, 14, 23, 0, 15, 199, 227, 0, 3, 150, 204, 0, + 1, 91, 146, 0, 1, 55, 95, 0, 1, 30, 53, 0, 1, 11, 20, 0, 19, 55, 240, 0, + 19, 59, 196, 0, 3, 52, 105, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 41, 166, 207, 0, 104, 153, 199, 0, 31, 123, 181, 0, 14, 101, 152, 0, 5, 72, 106, 0, + 1, 36, 52, 0, 35, 176, 211, 0, 12, 131, 190, 0, 2, 88, 144, 0, 1, 60, 101, 0, + 1, 36, 60, 0, 1, 16, 28, 0, 28, 183, 213, 0, 8, 134, 191, 0, 1, 86, 142, 0, + 1, 56, 96, 0, 1, 30, 53, 0, 1, 12, 20, 0, 20, 190, 215, 0, 4, 135, 192, 0, + 1, 84, 139, 0, 1, 53, 91, 0, 1, 28, 49, 0, 1, 11, 20, 0, 13, 196, 216, 0, + 2, 137, 192, 0, 1, 86, 143, 0, 1, 57, 99, 0, 1, 32, 56, 0, 1, 13, 24, 0, + 211, 29, 217, 0, 96, 47, 156, 0, 22, 43, 87, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 78, 120, 193, 0, 111, 116, 186, 0, 46, 102, 164, 0, 15, 80, 128, 0, + 2, 49, 76, 0, 1, 18, 28, 0, 71, 161, 203, 0, 42, 132, 192, 0, 10, 98, 150, 0, + 3, 69, 109, 0, 1, 44, 70, 0, 1, 18, 29, 0, 57, 186, 211, 0, 30, 140, 196, 0, + 4, 93, 146, 0, 1, 62, 102, 0, 1, 38, 65, 0, 1, 16, 27, 0, 47, 199, 217, 0, + 14, 145, 196, 0, 1, 88, 142, 0, 1, 57, 98, 0, 1, 36, 62, 0, 1, 15, 26, 0, + 26, 219, 229, 0, 5, 155, 207, 0, 1, 94, 151, 0, 1, 60, 104, 0, 1, 36, 62, 0, + 1, 16, 28, 0, 233, 29, 248, 0, 146, 47, 220, 0, 43, 52, 140, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 100, 163, 232, 0, 179, 161, 222, 0, 63, 142, 204, 0, + 37, 113, 174, 0, 26, 89, 137, 0, 18, 68, 97, 0, 85, 181, 230, 0, 32, 146, 209, 0, + 7, 100, 164, 0, 3, 71, 121, 0, 1, 45, 77, 0, 1, 18, 30, 0, 65, 187, 230, 0, + 20, 148, 207, 0, 2, 97, 159, 0, 1, 68, 116, 0, 1, 40, 70, 0, 1, 14, 29, 0, + 40, 194, 227, 0, 8, 147, 204, 0, 1, 94, 155, 0, 1, 65, 112, 0, 1, 39, 66, 0, + 1, 14, 26, 0, 16, 208, 228, 0, 3, 151, 207, 0, 1, 98, 160, 0, 1, 67, 117, 0, + 1, 41, 74, 0, 1, 17, 31, 0, 17, 38, 140, 0, 7, 34, 80, 0, 1, 17, 29, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 75, 128, 0, 41, 76, 128, 0, + 26, 66, 116, 0, 12, 52, 94, 0, 2, 32, 55, 0, 1, 10, 16, 0, 50, 127, 154, 0, + 37, 109, 152, 0, 16, 82, 121, 0, 5, 59, 85, 0, 1, 35, 54, 0, 1, 13, 20, 0, + 40, 142, 167, 0, 17, 110, 157, 0, 2, 71, 112, 0, 1, 44, 72, 0, 1, 27, 45, 0, + 1, 11, 17, 0, 30, 175, 188, 0, 9, 124, 169, 0, 1, 74, 116, 0, 1, 48, 78, 0, + 1, 30, 49, 0, 1, 11, 18, 0, 10, 222, 223, 0, 2, 150, 194, 0, 1, 83, 128, 0, + 1, 48, 79, 0, 1, 27, 45, 0, 1, 11, 17, 0, 36, 41, 235, 0, 29, 36, 193, 0, + 10, 27, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 165, 222, 0, + 177, 162, 215, 0, 110, 135, 195, 0, 57, 113, 168, 0, 23, 83, 120, 0, 10, 49, 61, 0, + 85, 190, 223, 0, 36, 139, 200, 0, 5, 90, 146, 0, 1, 60, 103, 0, 1, 38, 65, 0, + 1, 18, 30, 0, 72, 202, 223, 0, 23, 141, 199, 0, 2, 86, 140, 0, 1, 56, 97, 0, + 1, 36, 61, 0, 1, 16, 27, 0, 55, 218, 225, 0, 13, 145, 200, 0, 1, 86, 141, 0, + 1, 57, 99, 0, 1, 35, 61, 0, 1, 13, 22, 0, 15, 235, 212, 0, 1, 132, 184, 0, + 1, 84, 139, 0, 1, 57, 97, 0, 1, 34, 56, 0, 1, 14, 23, 0, 181, 21, 201, 0, + 61, 37, 123, 0, 10, 38, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 47, 106, 172, 0, 95, 104, 173, 0, 42, 93, 159, 0, 18, 77, 131, 0, 4, 50, 81, 0, + 1, 17, 23, 0, 62, 147, 199, 0, 44, 130, 189, 0, 28, 102, 154, 0, 18, 75, 115, 0, + 2, 44, 65, 0, 1, 12, 19, 0, 55, 153, 210, 0, 24, 130, 194, 0, 3, 93, 146, 0, + 1, 61, 97, 0, 1, 31, 50, 0, 1, 10, 16, 0, 49, 186, 223, 0, 17, 148, 204, 0, + 1, 96, 142, 0, 1, 53, 83, 0, 1, 26, 44, 0, 1, 11, 17, 0, 13, 217, 212, 0, + 2, 136, 180, 0, 1, 78, 124, 0, 1, 50, 83, 0, 1, 29, 49, 0, 1, 14, 23, 0, + 197, 13, 247, 0, 82, 17, 222, 0, 25, 17, 162, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 126, 186, 247, 0, 234, 191, 243, 0, 176, 177, 234, 0, 104, 158, 220, 0, + 66, 128, 186, 0, 55, 90, 137, 0, 111, 197, 242, 0, 46, 158, 219, 0, 9, 104, 171, 0, + 2, 65, 125, 0, 1, 44, 80, 0, 1, 17, 91, 0, 104, 208, 245, 0, 39, 168, 224, 0, + 3, 109, 162, 0, 1, 79, 124, 0, 1, 50, 102, 0, 1, 43, 102, 0, 84, 220, 246, 0, + 31, 177, 231, 0, 2, 115, 180, 0, 1, 79, 134, 0, 1, 55, 77, 0, 1, 60, 79, 0, + 43, 243, 240, 0, 8, 180, 217, 0, 1, 115, 166, 0, 1, 84, 121, 0, 1, 51, 67, 0, + 1, 16, 6, 0, + }, + .switchable_interp_prob{235, 162, 36, 255, 34, 3, 149, 144}, + .inter_mode_prob{ + 2, 173, 34, 0, 7, 145, 85, 0, 7, 166, 63, 0, 7, 94, + 66, 0, 8, 64, 46, 0, 17, 81, 31, 0, 25, 29, 30, 0, + }, + .intra_inter_prob{9, 102, 187, 225}, + .comp_inter_prob{9, 102, 187, 225, 0}, + .single_ref_prob{33, 16, 77, 74, 142, 142, 172, 170, 238, 247}, + .comp_ref_prob{50, 126, 123, 221, 226}, + .tx_32x32_prob{3, 136, 37, 5, 52, 13}, + .tx_16x16_prob{20, 152, 15, 101}, + .tx_8x8_prob{100, 66}, + .skip_probs{192, 128, 64}, + .joints{32, 64, 96}, + .sign{128, 128}, + .classes{ + 224, 144, 192, 168, 192, 176, 192, 198, 198, 245, + 216, 128, 176, 160, 176, 176, 192, 198, 198, 208, + }, + .class_0{216, 208}, + .prob_bits{ + 136, 140, 148, 160, 176, 192, 224, 234, 234, 240, + 136, 140, 148, 160, 176, 192, 224, 234, 234, 240, + }, + .class_0_fr{128, 128, 64, 96, 112, 64, 128, 128, 64, 96, 112, 64}, + .fr{64, 96, 64, 64, 96, 64}, + .class_0_hp{160, 160}, + .high_precision{128, 128}, +}; + +VP9::VP9(GPU& gpu) : gpu(gpu) {} + +VP9::~VP9() = default; + +void VP9::WriteProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) { + const bool update = new_prob != old_prob; + + writer.Write(update, diff_update_probability); + + if (update) { + WriteProbabilityDelta(writer, new_prob, old_prob); + } +} +template +void VP9::WriteProbabilityUpdate(VpxRangeEncoder& writer, const std::array& new_prob, + const std::array& old_prob) { + for (std::size_t offset = 0; offset < new_prob.size(); ++offset) { + WriteProbabilityUpdate(writer, new_prob[offset], old_prob[offset]); + } +} + +template +void VP9::WriteProbabilityUpdateAligned4(VpxRangeEncoder& writer, const std::array& new_prob, + const std::array& old_prob) { + for (std::size_t offset = 0; offset < new_prob.size(); offset += 4) { + WriteProbabilityUpdate(writer, new_prob[offset + 0], old_prob[offset + 0]); + WriteProbabilityUpdate(writer, new_prob[offset + 1], old_prob[offset + 1]); + WriteProbabilityUpdate(writer, new_prob[offset + 2], old_prob[offset + 2]); + } +} + +void VP9::WriteProbabilityDelta(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) { + const int delta = RemapProbability(new_prob, old_prob); + + EncodeTermSubExp(writer, delta); +} + +s32 VP9::RemapProbability(s32 new_prob, s32 old_prob) { + new_prob--; + old_prob--; + + std::size_t index{}; + + if (old_prob * 2 <= 0xff) { + index = static_cast(std::max(0, RecenterNonNeg(new_prob, old_prob) - 1)); + } else { + index = static_cast( + std::max(0, RecenterNonNeg(0xff - 1 - new_prob, 0xff - 1 - old_prob) - 1)); + } + + return map_lut[index]; +} + +s32 VP9::RecenterNonNeg(s32 new_prob, s32 old_prob) { + if (new_prob > old_prob * 2) { + return new_prob; + } else if (new_prob >= old_prob) { + return (new_prob - old_prob) * 2; + } else { + return (old_prob - new_prob) * 2 - 1; + } +} + +void VP9::EncodeTermSubExp(VpxRangeEncoder& writer, s32 value) { + if (WriteLessThan(writer, value, 16)) { + writer.Write(value, 4); + } else if (WriteLessThan(writer, value, 32)) { + writer.Write(value - 16, 4); + } else if (WriteLessThan(writer, value, 64)) { + writer.Write(value - 32, 5); + } else { + value -= 64; + + constexpr s32 size = 8; + + const s32 mask = (1 << size) - 191; + + const s32 delta = value - mask; + + if (delta < 0) { + writer.Write(value, size - 1); + } else { + writer.Write(delta / 2 + mask, size - 1); + writer.Write(delta & 1, 1); + } + } +} + +bool VP9::WriteLessThan(VpxRangeEncoder& writer, s32 value, s32 test) { + const bool is_lt = value < test; + writer.Write(!is_lt); + return is_lt; +} + +void VP9::WriteCoefProbabilityUpdate(VpxRangeEncoder& writer, s32 tx_mode, + const std::array& new_prob, + const std::array& old_prob) { + // Note: There's 1 byte added on each packet for alignment, + // this byte is ignored when doing updates. + constexpr s32 block_bytes = 2 * 2 * 6 * 6 * 4; + + const auto needs_update = [&](s32 base_index) -> bool { + s32 index = base_index; + for (s32 i = 0; i < 2; i++) { + for (s32 j = 0; j < 2; j++) { + for (s32 k = 0; k < 6; k++) { + for (s32 l = 0; l < 6; l++) { + if (new_prob[index + 0] != old_prob[index + 0] || + new_prob[index + 1] != old_prob[index + 1] || + new_prob[index + 2] != old_prob[index + 2]) { + return true; + } + + index += 4; + } + } + } + } + return false; + }; + + for (s32 block_index = 0; block_index < 4; block_index++) { + const s32 base_index = block_index * block_bytes; + const bool update = needs_update(base_index); + writer.Write(update); + + if (update) { + s32 index = base_index; + for (s32 i = 0; i < 2; i++) { + for (s32 j = 0; j < 2; j++) { + for (s32 k = 0; k < 6; k++) { + for (s32 l = 0; l < 6; l++) { + if (k != 0 || l < 3) { + WriteProbabilityUpdate(writer, new_prob[index + 0], + old_prob[index + 0]); + WriteProbabilityUpdate(writer, new_prob[index + 1], + old_prob[index + 1]); + WriteProbabilityUpdate(writer, new_prob[index + 2], + old_prob[index + 2]); + } + index += 4; + } + } + } + } + } + + if (block_index == tx_mode) { + break; + } + } +} + +void VP9::WriteMvProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob) { + const bool update = new_prob != old_prob; + writer.Write(update, diff_update_probability); + + if (update) { + writer.Write(new_prob >> 1, 7); + } +} + +s32 VP9::CalcMinLog2TileCols(s32 frame_width) { + const s32 sb64_cols = (frame_width + 63) / 64; + s32 min_log2 = 0; + + while ((64 << min_log2) < sb64_cols) { + min_log2++; + } + + return min_log2; +} + +s32 VP9::CalcMaxLog2TileCols(s32 frameWidth) { + const s32 sb64_cols = (frameWidth + 63) / 64; + s32 max_log2 = 1; + + while ((sb64_cols >> max_log2) >= 4) { + max_log2++; + } + + return max_log2 - 1; +} + +Vp9PictureInfo VP9::GetVp9PictureInfo(const NvdecCommon::NvdecRegisters& state) { + PictureInfo picture_info{}; + gpu.MemoryManager().ReadBlock(state.picture_info_offset, &picture_info, sizeof(PictureInfo)); + Vp9PictureInfo vp9_info = picture_info.Convert(); + + InsertEntropy(state.vp9_entropy_probs_offset, vp9_info.entropy); + + // surface_luma_offset[0:3] contains the address of the reference frame offsets in the following + // order: last, golden, altref, current. It may be worthwhile to track the updates done here + // to avoid buffering frame data needed for reference frame updating in the header composition. + std::memcpy(vp9_info.frame_offsets.data(), state.surface_luma_offset.data(), 4 * sizeof(u64)); + + return std::move(vp9_info); +} + +void VP9::InsertEntropy(u64 offset, Vp9EntropyProbs& dst) { + EntropyProbs entropy{}; + gpu.MemoryManager().ReadBlock(offset, &entropy, sizeof(EntropyProbs)); + entropy.Convert(dst); +} + +Vp9FrameContainer VP9::GetCurrentFrame(const NvdecCommon::NvdecRegisters& state) { + Vp9FrameContainer frame{}; + { + gpu.SyncGuestHost(); + frame.info = std::move(GetVp9PictureInfo(state)); + + frame.bit_stream.resize(frame.info.bitstream_size); + gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.bit_stream.data(), + frame.info.bitstream_size); + } + // Buffer two frames, saving the last show frame info + if (next_next_frame.bit_stream.size() != 0) { + Vp9FrameContainer temp{ + .info = frame.info, + .bit_stream = frame.bit_stream, + }; + next_next_frame.info.show_frame = frame.info.last_frame_shown; + frame.info = next_next_frame.info; + frame.bit_stream = next_next_frame.bit_stream; + next_next_frame = std::move(temp); + + if (next_frame.bit_stream.size() != 0) { + Vp9FrameContainer temp{ + .info = frame.info, + .bit_stream = frame.bit_stream, + }; + next_frame.info.show_frame = frame.info.last_frame_shown; + frame.info = next_frame.info; + frame.bit_stream = next_frame.bit_stream; + next_frame = std::move(temp); + } else { + next_frame.info = frame.info; + next_frame.bit_stream = frame.bit_stream; + } + } else { + next_next_frame.info = frame.info; + next_next_frame.bit_stream = frame.bit_stream; + } + return frame; +} + +std::vector VP9::ComposeCompressedHeader() { + VpxRangeEncoder writer{}; + + if (!current_frame_info.lossless) { + if (static_cast(current_frame_info.transform_mode) >= 3) { + writer.Write(3, 2); + writer.Write(current_frame_info.transform_mode == 4); + } else { + writer.Write(current_frame_info.transform_mode, 2); + } + } + + if (current_frame_info.transform_mode == 4) { + // tx_mode_probs() in the spec + WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_8x8_prob, + prev_frame_probs.tx_8x8_prob); + WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_16x16_prob, + prev_frame_probs.tx_16x16_prob); + WriteProbabilityUpdate(writer, current_frame_info.entropy.tx_32x32_prob, + prev_frame_probs.tx_32x32_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.tx_8x8_prob = current_frame_info.entropy.tx_8x8_prob; + prev_frame_probs.tx_16x16_prob = current_frame_info.entropy.tx_16x16_prob; + prev_frame_probs.tx_32x32_prob = current_frame_info.entropy.tx_32x32_prob; + } + } + // read_coef_probs() in the spec + WriteCoefProbabilityUpdate(writer, current_frame_info.transform_mode, + current_frame_info.entropy.coef_probs, prev_frame_probs.coef_probs); + // read_skip_probs() in the spec + WriteProbabilityUpdate(writer, current_frame_info.entropy.skip_probs, + prev_frame_probs.skip_probs); + + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.coef_probs = current_frame_info.entropy.coef_probs; + prev_frame_probs.skip_probs = current_frame_info.entropy.skip_probs; + } + + if (!current_frame_info.intra_only) { + // read_inter_probs() in the spec + WriteProbabilityUpdateAligned4(writer, current_frame_info.entropy.inter_mode_prob, + prev_frame_probs.inter_mode_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.inter_mode_prob = current_frame_info.entropy.inter_mode_prob; + } + + if (current_frame_info.interp_filter == 4) { + // read_interp_filter_probs() in the spec + WriteProbabilityUpdate(writer, current_frame_info.entropy.switchable_interp_prob, + prev_frame_probs.switchable_interp_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.switchable_interp_prob = + current_frame_info.entropy.switchable_interp_prob; + } + } + + // read_is_inter_probs() in the spec + WriteProbabilityUpdate(writer, current_frame_info.entropy.intra_inter_prob, + prev_frame_probs.intra_inter_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.intra_inter_prob = current_frame_info.entropy.intra_inter_prob; + } + // frame_reference_mode() in the spec + if ((current_frame_info.ref_frame_sign_bias[1] & 1) != + (current_frame_info.ref_frame_sign_bias[2] & 1) || + (current_frame_info.ref_frame_sign_bias[1] & 1) != + (current_frame_info.ref_frame_sign_bias[3] & 1)) { + if (current_frame_info.reference_mode >= 1) { + writer.Write(1, 1); + writer.Write(current_frame_info.reference_mode == 2); + } else { + writer.Write(0, 1); + } + } + + // frame_reference_mode_probs() in the spec + if (current_frame_info.reference_mode == 2) { + WriteProbabilityUpdate(writer, current_frame_info.entropy.comp_inter_prob, + prev_frame_probs.comp_inter_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.comp_inter_prob = current_frame_info.entropy.comp_inter_prob; + } + } + + if (current_frame_info.reference_mode != 1) { + WriteProbabilityUpdate(writer, current_frame_info.entropy.single_ref_prob, + prev_frame_probs.single_ref_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.single_ref_prob = current_frame_info.entropy.single_ref_prob; + } + } + + if (current_frame_info.reference_mode != 0) { + WriteProbabilityUpdate(writer, current_frame_info.entropy.comp_ref_prob, + prev_frame_probs.comp_ref_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.comp_ref_prob = current_frame_info.entropy.comp_ref_prob; + } + } + + // read_y_mode_probs + for (std::size_t index = 0; index < current_frame_info.entropy.y_mode_prob.size(); + ++index) { + WriteProbabilityUpdate(writer, current_frame_info.entropy.y_mode_prob[index], + prev_frame_probs.y_mode_prob[index]); + } + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.y_mode_prob = current_frame_info.entropy.y_mode_prob; + } + // read_partition_probs + WriteProbabilityUpdateAligned4(writer, current_frame_info.entropy.partition_prob, + prev_frame_probs.partition_prob); + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.partition_prob = current_frame_info.entropy.partition_prob; + } + + // mv_probs + for (s32 i = 0; i < 3; i++) { + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.joints[i], + prev_frame_probs.joints[i]); + } + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.joints = current_frame_info.entropy.joints; + } + + for (s32 i = 0; i < 2; i++) { + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.sign[i], + prev_frame_probs.sign[i]); + + for (s32 j = 0; j < 10; j++) { + const int index = i * 10 + j; + + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.classes[index], + prev_frame_probs.classes[index]); + } + + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0[i], + prev_frame_probs.class_0[i]); + + for (s32 j = 0; j < 10; j++) { + const int index = i * 10 + j; + + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.prob_bits[index], + prev_frame_probs.prob_bits[index]); + } + } + + for (s32 i = 0; i < 2; i++) { + for (s32 j = 0; j < 2; j++) { + for (s32 k = 0; k < 3; k++) { + const int index = i * 2 * 3 + j * 3 + k; + + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0_fr[index], + prev_frame_probs.class_0_fr[index]); + } + } + + for (s32 j = 0; j < 3; j++) { + const int index = i * 3 + j; + + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.fr[index], + prev_frame_probs.fr[index]); + } + } + + if (current_frame_info.allow_high_precision_mv) { + for (s32 index = 0; index < 2; index++) { + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.class_0_hp[index], + prev_frame_probs.class_0_hp[index]); + WriteMvProbabilityUpdate(writer, current_frame_info.entropy.high_precision[index], + prev_frame_probs.high_precision[index]); + } + } + + // save previous probs + if (current_frame_info.show_frame && !current_frame_info.is_key_frame) { + prev_frame_probs.sign = current_frame_info.entropy.sign; + prev_frame_probs.classes = current_frame_info.entropy.classes; + prev_frame_probs.class_0 = current_frame_info.entropy.class_0; + prev_frame_probs.prob_bits = current_frame_info.entropy.prob_bits; + prev_frame_probs.class_0_fr = current_frame_info.entropy.class_0_fr; + prev_frame_probs.fr = current_frame_info.entropy.fr; + prev_frame_probs.class_0_hp = current_frame_info.entropy.class_0_hp; + prev_frame_probs.high_precision = current_frame_info.entropy.high_precision; + } + } + + writer.End(); + return writer.GetBuffer(); + + const auto writer_bytearray = writer.GetBuffer(); + + std::vector compressed_header(writer_bytearray.size()); + std::memcpy(compressed_header.data(), writer_bytearray.data(), writer_bytearray.size()); + return compressed_header; +} + +VpxBitStreamWriter VP9::ComposeUncompressedHeader() { + VpxBitStreamWriter uncomp_writer{}; + + uncomp_writer.WriteU(2, 2); // Frame marker. + uncomp_writer.WriteU(0, 2); // Profile. + uncomp_writer.WriteBit(false); // Show existing frame. + uncomp_writer.WriteBit(!current_frame_info.is_key_frame); // is key frame? + uncomp_writer.WriteBit(current_frame_info.show_frame); // show frame? + uncomp_writer.WriteBit(current_frame_info.error_resilient_mode); // error reslience + + if (current_frame_info.is_key_frame) { + uncomp_writer.WriteU(frame_sync_code, 24); + uncomp_writer.WriteU(0, 3); // Color space. + uncomp_writer.WriteU(0, 1); // Color range. + uncomp_writer.WriteU(current_frame_info.frame_size.width - 1, 16); + uncomp_writer.WriteU(current_frame_info.frame_size.height - 1, 16); + uncomp_writer.WriteBit(false); // Render and frame size different. + + // Reset context + prev_frame_probs = default_probs; + swap_next_golden = false; + loop_filter_ref_deltas.fill(0); + loop_filter_mode_deltas.fill(0); + + // allow frames offsets to stabilize before checking for golden frames + grace_period = 4; + + // On key frames, all frame slots are set to the current frame, + // so the value of the selected slot doesn't really matter. + frame_ctxs.fill({current_frame_number, false, default_probs}); + + // intra only, meaning the frame can be recreated with no other references + current_frame_info.intra_only = true; + + } else { + std::array ref_frame_index; + + if (!current_frame_info.show_frame) { + uncomp_writer.WriteBit(current_frame_info.intra_only); + if (!current_frame_info.last_frame_was_key) { + swap_next_golden = !swap_next_golden; + } + } else { + current_frame_info.intra_only = false; + } + if (!current_frame_info.error_resilient_mode) { + uncomp_writer.WriteU(0, 2); // Reset frame context. + } + + // Last, Golden, Altref frames + ref_frame_index = std::array{0, 1, 2}; + + // set when next frame is hidden + // altref and golden references are swapped + if (swap_next_golden) { + ref_frame_index = std::array{0, 2, 1}; + } + + // update Last Frame + u64 refresh_frame_flags = 1; + + // golden frame may refresh, determined if the next golden frame offset is changed + bool golden_refresh = false; + if (grace_period <= 0) { + for (s32 index = 1; index < 3; ++index) { + if (current_frame_info.frame_offsets[index] != + next_frame.info.frame_offsets[index]) { + current_frame_info.refresh_frame[index] = true; + golden_refresh = true; + grace_period = 3; + } + } + } + + if (current_frame_info.show_frame && + (!next_frame.info.show_frame || next_frame.info.is_key_frame)) { + // Update golden frame + refresh_frame_flags = swap_next_golden ? 2 : 4; + } + + if (!current_frame_info.show_frame) { + // Update altref + refresh_frame_flags = swap_next_golden ? 2 : 4; + } else if (golden_refresh) { + refresh_frame_flags = 3; + } + + if (current_frame_info.intra_only) { + uncomp_writer.WriteU(frame_sync_code, 24); + uncomp_writer.WriteU(static_cast(refresh_frame_flags), 8); + uncomp_writer.WriteU(current_frame_info.frame_size.width - 1, 16); + uncomp_writer.WriteU(current_frame_info.frame_size.height - 1, 16); + uncomp_writer.WriteBit(false); // Render and frame size different. + } else { + uncomp_writer.WriteU(static_cast(refresh_frame_flags), 8); + + for (s32 index = 1; index < 4; index++) { + uncomp_writer.WriteU(ref_frame_index[index - 1], 3); + uncomp_writer.WriteU(current_frame_info.ref_frame_sign_bias[index], 1); + } + + uncomp_writer.WriteBit(true); // Frame size with refs. + uncomp_writer.WriteBit(false); // Render and frame size different. + uncomp_writer.WriteBit(current_frame_info.allow_high_precision_mv); + uncomp_writer.WriteBit(current_frame_info.interp_filter == 4); + + if (current_frame_info.interp_filter != 4) { + uncomp_writer.WriteU(current_frame_info.interp_filter, 2); + } + } + } + + if (!current_frame_info.error_resilient_mode) { + uncomp_writer.WriteBit(true); // Refresh frame context. where do i get this info from? + uncomp_writer.WriteBit(true); // Frame parallel decoding mode. + } + + int frame_ctx_idx = 0; + if (!current_frame_info.show_frame) { + frame_ctx_idx = 1; + } + + uncomp_writer.WriteU(frame_ctx_idx, 2); // Frame context index. + prev_frame_probs = + frame_ctxs[frame_ctx_idx].probs; // reference probabilities for compressed header + frame_ctxs[frame_ctx_idx] = {current_frame_number, false, current_frame_info.entropy}; + + uncomp_writer.WriteU(current_frame_info.first_level, 6); + uncomp_writer.WriteU(current_frame_info.sharpness_level, 3); + uncomp_writer.WriteBit(current_frame_info.mode_ref_delta_enabled); + + if (current_frame_info.mode_ref_delta_enabled) { + // check if ref deltas are different, update accordingly + std::array update_loop_filter_ref_deltas; + std::array update_loop_filter_mode_deltas; + + bool loop_filter_delta_update = false; + + for (std::size_t index = 0; index < current_frame_info.ref_deltas.size(); index++) { + const s8 old_deltas = loop_filter_ref_deltas[index]; + const s8 new_deltas = current_frame_info.ref_deltas[index]; + + loop_filter_delta_update |= + (update_loop_filter_ref_deltas[index] = old_deltas != new_deltas); + } + + for (std::size_t index = 0; index < current_frame_info.mode_deltas.size(); index++) { + const s8 old_deltas = loop_filter_mode_deltas[index]; + const s8 new_deltas = current_frame_info.mode_deltas[index]; + + loop_filter_delta_update |= + (update_loop_filter_mode_deltas[index] = old_deltas != new_deltas); + } + + uncomp_writer.WriteBit(loop_filter_delta_update); + + if (loop_filter_delta_update) { + for (std::size_t index = 0; index < current_frame_info.ref_deltas.size(); index++) { + uncomp_writer.WriteBit(update_loop_filter_ref_deltas[index]); + + if (update_loop_filter_ref_deltas[index]) { + uncomp_writer.WriteS(current_frame_info.ref_deltas[index], 6); + } + } + + for (std::size_t index = 0; index < current_frame_info.mode_deltas.size(); index++) { + uncomp_writer.WriteBit(update_loop_filter_mode_deltas[index]); + + if (update_loop_filter_mode_deltas[index]) { + uncomp_writer.WriteS(current_frame_info.mode_deltas[index], 6); + } + } + // save new deltas + loop_filter_ref_deltas = current_frame_info.ref_deltas; + loop_filter_mode_deltas = current_frame_info.mode_deltas; + } + } + + uncomp_writer.WriteU(current_frame_info.base_q_index, 8); + + uncomp_writer.WriteDeltaQ(current_frame_info.y_dc_delta_q); + uncomp_writer.WriteDeltaQ(current_frame_info.uv_dc_delta_q); + uncomp_writer.WriteDeltaQ(current_frame_info.uv_ac_delta_q); + + uncomp_writer.WriteBit(false); // Segmentation enabled (TODO). + + const s32 min_tile_cols_log2 = CalcMinLog2TileCols(current_frame_info.frame_size.width); + const s32 max_tile_cols_log2 = CalcMaxLog2TileCols(current_frame_info.frame_size.width); + + const s32 tile_cols_log2_diff = current_frame_info.log2_tile_cols - min_tile_cols_log2; + const s32 tile_cols_log2_inc_mask = (1 << tile_cols_log2_diff) - 1; + + // If it's less than the maximum, we need to add an extra 0 on the bitstream + // to indicate that it should stop reading. + if (current_frame_info.log2_tile_cols < max_tile_cols_log2) { + uncomp_writer.WriteU(tile_cols_log2_inc_mask << 1, tile_cols_log2_diff + 1); + } else { + uncomp_writer.WriteU(tile_cols_log2_inc_mask, tile_cols_log2_diff); + } + + const bool tile_rows_log2_is_nonzero = current_frame_info.log2_tile_rows != 0; + + uncomp_writer.WriteBit(tile_rows_log2_is_nonzero); + + if (tile_rows_log2_is_nonzero) { + uncomp_writer.WriteBit(current_frame_info.log2_tile_rows > 1); + } + + return uncomp_writer; +} + +std::vector& VP9::ComposeFrameHeader(NvdecCommon::NvdecRegisters& state) { + std::vector bitstream; + { + Vp9FrameContainer curr_frame = GetCurrentFrame(state); + current_frame_info = curr_frame.info; + bitstream = curr_frame.bit_stream; + } + + // The uncompressed header routine sets PrevProb parameters needed for the compressed header + auto uncomp_writer = ComposeUncompressedHeader(); + std::vector compressed_header = ComposeCompressedHeader(); + + uncomp_writer.WriteU(static_cast(compressed_header.size()), 16); + uncomp_writer.Flush(); + std::vector uncompressed_header = uncomp_writer.GetByteArray(); + + // Write headers and frame to buffer + frame.resize(uncompressed_header.size() + compressed_header.size() + bitstream.size()); + std::memcpy(frame.data(), uncompressed_header.data(), uncompressed_header.size()); + std::memcpy(frame.data() + uncompressed_header.size(), compressed_header.data(), + compressed_header.size()); + std::memcpy(frame.data() + uncompressed_header.size() + compressed_header.size(), + bitstream.data(), bitstream.size()); + + // keep track of frame number + current_frame_number++; + grace_period--; + + // don't display hidden frames + hidden = !current_frame_info.show_frame; + return frame; +} + +VpxRangeEncoder::VpxRangeEncoder() { + Write(false); +} + +VpxRangeEncoder::~VpxRangeEncoder() = default; + +void VpxRangeEncoder::Write(s32 value, s32 value_size) { + for (s32 bit = value_size - 1; bit >= 0; bit--) { + Write(((value >> bit) & 1) != 0); + } +} + +void VpxRangeEncoder::Write(bool bit) { + Write(bit, half_probability); +} + +void VpxRangeEncoder::Write(bool bit, s32 probability) { + u32 local_range = range; + const u32 split = 1 + (((local_range - 1) * static_cast(probability)) >> 8); + local_range = split; + + if (bit) { + low_value += split; + local_range = range - split; + } + + s32 shift = norm_lut[local_range]; + local_range <<= shift; + count += shift; + + if (count >= 0) { + const s32 offset = shift - count; + + if (((low_value << (offset - 1)) >> 31) != 0) { + const s32 current_pos = static_cast(base_stream.GetPosition()); + base_stream.Seek(-1, Common::SeekOrigin::FromCurrentPos); + while (base_stream.GetPosition() >= 0 && PeekByte() == 0xff) { + base_stream.WriteByte(0); + + base_stream.Seek(-2, Common::SeekOrigin::FromCurrentPos); + } + base_stream.WriteByte(static_cast((PeekByte() + 1))); + base_stream.Seek(current_pos, Common::SeekOrigin::SetOrigin); + } + base_stream.WriteByte(static_cast((low_value >> (24 - offset)))); + + low_value <<= offset; + shift = count; + low_value &= 0xffffff; + count -= 8; + } + + low_value <<= shift; + range = local_range; +} + +void VpxRangeEncoder::End() { + for (std::size_t index = 0; index < 32; ++index) { + Write(false); + } +} + +u8 VpxRangeEncoder::PeekByte() { + const u8 value = base_stream.ReadByte(); + base_stream.Seek(-1, Common::SeekOrigin::FromCurrentPos); + + return value; +} + +VpxBitStreamWriter::VpxBitStreamWriter() = default; + +VpxBitStreamWriter::~VpxBitStreamWriter() = default; + +void VpxBitStreamWriter::WriteU(u32 value, u32 value_size) { + WriteBits(value, value_size); +} + +void VpxBitStreamWriter::WriteS(s32 value, u32 value_size) { + const bool sign = value < 0; + if (sign) { + value = -value; + } + + WriteBits(static_cast(value << 1) | (sign ? 1 : 0), value_size + 1); +} + +void VpxBitStreamWriter::WriteDeltaQ(u32 value) { + const bool delta_coded = value != 0; + WriteBit(delta_coded); + + if (delta_coded) { + WriteBits(value, 4); + } +} + +void VpxBitStreamWriter::WriteBits(u32 value, u32 bit_count) { + s32 value_pos = 0; + s32 remaining = bit_count; + + while (remaining > 0) { + s32 copy_size = remaining; + + const s32 free = GetFreeBufferBits(); + + if (copy_size > free) { + copy_size = free; + } + + const s32 mask = (1 << copy_size) - 1; + + const s32 src_shift = (bit_count - value_pos) - copy_size; + const s32 dst_shift = (buffer_size - buffer_pos) - copy_size; + + buffer |= ((value >> src_shift) & mask) << dst_shift; + + value_pos += copy_size; + buffer_pos += copy_size; + remaining -= copy_size; + } +} + +void VpxBitStreamWriter::WriteBit(bool state) { + WriteBits(state ? 1 : 0, 1); +} + +s32 VpxBitStreamWriter::GetFreeBufferBits() { + if (buffer_pos == buffer_size) { + Flush(); + } + + return buffer_size - buffer_pos; +} + +void VpxBitStreamWriter::Flush() { + if (buffer_pos == 0) { + return; + } + byte_array.push_back(static_cast(buffer)); + buffer = 0; + buffer_pos = 0; +} + +std::vector& VpxBitStreamWriter::GetByteArray() { + return byte_array; +} + +const std::vector& VpxBitStreamWriter::GetByteArray() const { + return byte_array; +} + +} // namespace Tegra::Decoder diff --git a/src/video_core/command_classes/codecs/vp9.h b/src/video_core/command_classes/codecs/vp9.h new file mode 100644 index 000000000..748e11bae --- /dev/null +++ b/src/video_core/command_classes/codecs/vp9.h @@ -0,0 +1,216 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/stream.h" +#include "video_core/command_classes/codecs/vp9_types.h" +#include "video_core/command_classes/nvdec_common.h" + +namespace Tegra { +class GPU; +enum class FrameType { KeyFrame = 0, InterFrame = 1 }; +namespace Decoder { + +/// The VpxRangeEncoder, and VpxBitStreamWriter classes are used to compose the +/// VP9 header bitstreams. + +class VpxRangeEncoder { +public: + VpxRangeEncoder(); + ~VpxRangeEncoder(); + + /// Writes the rightmost value_size bits from value into the stream + void Write(s32 value, s32 value_size); + + /// Writes a single bit with half probability + void Write(bool bit); + + /// Writes a bit to the base_stream encoded with probability + void Write(bool bit, s32 probability); + + /// Signal the end of the bitstream + void End(); + + std::vector& GetBuffer() { + return base_stream.GetBuffer(); + } + + const std::vector& GetBuffer() const { + return base_stream.GetBuffer(); + } + +private: + u8 PeekByte(); + Common::Stream base_stream{}; + u32 low_value{}; + u32 range{0xff}; + s32 count{-24}; + s32 half_probability{128}; + static constexpr std::array norm_lut{ + 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + }; +}; + +class VpxBitStreamWriter { +public: + VpxBitStreamWriter(); + ~VpxBitStreamWriter(); + + /// Write an unsigned integer value + void WriteU(u32 value, u32 value_size); + + /// Write a signed integer value + void WriteS(s32 value, u32 value_size); + + /// Based on 6.2.10 of VP9 Spec, writes a delta coded value + void WriteDeltaQ(u32 value); + + /// Write a single bit. + void WriteBit(bool state); + + /// Pushes current buffer into buffer_array, resets buffer + void Flush(); + + /// Returns byte_array + std::vector& GetByteArray(); + + /// Returns const byte_array + const std::vector& GetByteArray() const; + +private: + /// Write bit_count bits from value into buffer + void WriteBits(u32 value, u32 bit_count); + + /// Gets next available position in buffer, invokes Flush() if buffer is full + s32 GetFreeBufferBits(); + + s32 buffer_size{8}; + + s32 buffer{}; + s32 buffer_pos{}; + std::vector byte_array; +}; + +class VP9 { +public: + explicit VP9(GPU& gpu); + ~VP9(); + + /// Composes the VP9 frame from the GPU state information. Based on the official VP9 spec + /// documentation + std::vector& ComposeFrameHeader(NvdecCommon::NvdecRegisters& state); + + /// Returns true if the most recent frame was a hidden frame. + bool WasFrameHidden() const { + return hidden; + } + +private: + /// Generates compressed header probability updates in the bitstream writer + template + void WriteProbabilityUpdate(VpxRangeEncoder& writer, const std::array& new_prob, + const std::array& old_prob); + + /// Generates compressed header probability updates in the bitstream writer + /// If probs are not equal, WriteProbabilityDelta is invoked + void WriteProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob); + + /// Generates compressed header probability deltas in the bitstream writer + void WriteProbabilityDelta(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob); + + /// Adjusts old_prob depending on new_prob. Based on section 6.3.5 of VP9 Specification + s32 RemapProbability(s32 new_prob, s32 old_prob); + + /// Recenters probability. Based on section 6.3.6 of VP9 Specification + s32 RecenterNonNeg(s32 new_prob, s32 old_prob); + + /// Inverse of 6.3.4 Decode term subexp + void EncodeTermSubExp(VpxRangeEncoder& writer, s32 value); + + /// Writes if the value is less than the test value + bool WriteLessThan(VpxRangeEncoder& writer, s32 value, s32 test); + + /// Writes probability updates for the Coef probabilities + void WriteCoefProbabilityUpdate(VpxRangeEncoder& writer, s32 tx_mode, + const std::array& new_prob, + const std::array& old_prob); + + /// Write probabilities for 4-byte aligned structures + template + void WriteProbabilityUpdateAligned4(VpxRangeEncoder& writer, const std::array& new_prob, + const std::array& old_prob); + + /// Write motion vector probability updates. 6.3.17 in the spec + void WriteMvProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob); + + /// 6.2.14 Tile size calculation + s32 CalcMinLog2TileCols(s32 frame_width); + s32 CalcMaxLog2TileCols(s32 frame_width); + + /// Returns VP9 information from NVDEC provided offset and size + Vp9PictureInfo GetVp9PictureInfo(const NvdecCommon::NvdecRegisters& state); + + /// Read and convert NVDEC provided entropy probs to Vp9EntropyProbs struct + void InsertEntropy(u64 offset, Vp9EntropyProbs& dst); + + /// Returns frame to be decoded after buffering + Vp9FrameContainer GetCurrentFrame(const NvdecCommon::NvdecRegisters& state); + + /// Use NVDEC providied information to compose the headers for the current frame + std::vector ComposeCompressedHeader(); + VpxBitStreamWriter ComposeUncompressedHeader(); + + GPU& gpu; + std::vector frame; + + std::array loop_filter_ref_deltas{}; + std::array loop_filter_mode_deltas{}; + + bool hidden; + s64 current_frame_number = -2; // since we buffer 2 frames + s32 grace_period = 6; // frame offsets need to stabilize + std::array frame_ctxs{}; + Vp9FrameContainer next_frame{}; + Vp9FrameContainer next_next_frame{}; + bool swap_next_golden{}; + + Vp9PictureInfo current_frame_info{}; + Vp9EntropyProbs prev_frame_probs{}; + + s32 diff_update_probability = 252; + s32 frame_sync_code = 0x498342; + static constexpr std::array map_lut = { + 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, + 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 2, 50, + 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 3, 62, 63, 64, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 4, 74, 75, 76, 77, 78, 79, 80, 81, 82, + 83, 84, 85, 5, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 6, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113, + 114, 115, 116, 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129, + 130, 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, + 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11, 158, 159, 160, + 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171, 172, 173, 174, 175, 176, + 177, 178, 179, 180, 181, 13, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, + 193, 14, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 15, 206, 207, + 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223, + 224, 225, 226, 227, 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, + 240, 241, 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19, + }; +}; + +} // namespace Decoder +} // namespace Tegra diff --git a/src/video_core/command_classes/codecs/vp9_types.h b/src/video_core/command_classes/codecs/vp9_types.h new file mode 100644 index 000000000..8688fdac0 --- /dev/null +++ b/src/video_core/command_classes/codecs/vp9_types.h @@ -0,0 +1,369 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include "common/cityhash.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "video_core/command_classes/nvdec_common.h" + +namespace Tegra { +class GPU; + +namespace Decoder { +struct Vp9FrameDimensions { + s16 width{}; + s16 height{}; + s16 luma_pitch{}; + s16 chroma_pitch{}; +}; +static_assert(sizeof(Vp9FrameDimensions) == 0x8, "Vp9 Vp9FrameDimensions is an invalid size"); + +enum FrameFlags : u32 { + IsKeyFrame = 1 << 0, + LastFrameIsKeyFrame = 1 << 1, + FrameSizeChanged = 1 << 2, + ErrorResilientMode = 1 << 3, + LastShowFrame = 1 << 4, + IntraOnly = 1 << 5, +}; + +enum class MvJointType { + MvJointZero = 0, /* Zero vector */ + MvJointHnzvz = 1, /* Vert zero, hor nonzero */ + MvJointHzvnz = 2, /* Hor zero, vert nonzero */ + MvJointHnzvnz = 3, /* Both components nonzero */ +}; +enum class MvClassType { + MvClass0 = 0, /* (0, 2] integer pel */ + MvClass1 = 1, /* (2, 4] integer pel */ + MvClass2 = 2, /* (4, 8] integer pel */ + MvClass3 = 3, /* (8, 16] integer pel */ + MvClass4 = 4, /* (16, 32] integer pel */ + MvClass5 = 5, /* (32, 64] integer pel */ + MvClass6 = 6, /* (64, 128] integer pel */ + MvClass7 = 7, /* (128, 256] integer pel */ + MvClass8 = 8, /* (256, 512] integer pel */ + MvClass9 = 9, /* (512, 1024] integer pel */ + MvClass10 = 10, /* (1024,2048] integer pel */ +}; + +enum class BlockSize { + Block4x4 = 0, + Block4x8 = 1, + Block8x4 = 2, + Block8x8 = 3, + Block8x16 = 4, + Block16x8 = 5, + Block16x16 = 6, + Block16x32 = 7, + Block32x16 = 8, + Block32x32 = 9, + Block32x64 = 10, + Block64x32 = 11, + Block64x64 = 12, + BlockSizes = 13, + BlockInvalid = BlockSizes +}; + +enum class PredictionMode { + DcPred = 0, // Average of above and left pixels + VPred = 1, // Vertical + HPred = 2, // Horizontal + D45Pred = 3, // Directional 45 deg = round(arctan(1 / 1) * 180 / pi) + D135Pred = 4, // Directional 135 deg = 180 - 45 + D117Pred = 5, // Directional 117 deg = 180 - 63 + D153Pred = 6, // Directional 153 deg = 180 - 27 + D207Pred = 7, // Directional 207 deg = 180 + 27 + D63Pred = 8, // Directional 63 deg = round(arctan(2 / 1) * 180 / pi) + TmPred = 9, // True-motion + NearestMv = 10, + NearMv = 11, + ZeroMv = 12, + NewMv = 13, + MbModeCount = 14 +}; + +enum class TxSize { + Tx4x4 = 0, // 4x4 transform + Tx8x8 = 1, // 8x8 transform + Tx16x16 = 2, // 16x16 transform + Tx32x32 = 3, // 32x32 transform + TxSizes = 4 +}; + +enum class TxMode { + Only4X4 = 0, // Only 4x4 transform used + Allow8X8 = 1, // Allow block transform size up to 8x8 + Allow16X16 = 2, // Allow block transform size up to 16x16 + Allow32X32 = 3, // Allow block transform size up to 32x32 + TxModeSelect = 4, // Transform specified for each block + TxModes = 5 +}; + +enum class reference_mode { + SingleReference = 0, + CompoundReference = 1, + ReferenceModeSelect = 2, + ReferenceModes = 3 +}; + +struct Segmentation { + u8 enabled{}; + u8 update_map{}; + u8 temporal_update{}; + u8 abs_delta{}; + std::array feature_mask{}; + std::array, 8> feature_data{}; +}; +static_assert(sizeof(Segmentation) == 0x64, "Segmentation is an invalid size"); + +struct LoopFilter { + u8 mode_ref_delta_enabled{}; + std::array ref_deltas{}; + std::array mode_deltas{}; +}; +static_assert(sizeof(LoopFilter) == 0x7, "LoopFilter is an invalid size"); + +struct Vp9EntropyProbs { + std::array y_mode_prob{}; + std::array partition_prob{}; + std::array coef_probs{}; + std::array switchable_interp_prob{}; + std::array inter_mode_prob{}; + std::array intra_inter_prob{}; + std::array comp_inter_prob{}; + std::array single_ref_prob{}; + std::array comp_ref_prob{}; + std::array tx_32x32_prob{}; + std::array tx_16x16_prob{}; + std::array tx_8x8_prob{}; + std::array skip_probs{}; + std::array joints{}; + std::array sign{}; + std::array classes{}; + std::array class_0{}; + std::array prob_bits{}; + std::array class_0_fr{}; + std::array fr{}; + std::array class_0_hp{}; + std::array high_precision{}; +}; +static_assert(sizeof(Vp9EntropyProbs) == 0x9F4, "Vp9EntropyProbs is an invalid size"); + +struct Vp9PictureInfo { + bool is_key_frame{}; + bool intra_only{}; + bool last_frame_was_key{}; + bool frame_size_changed{}; + bool error_resilient_mode{}; + bool last_frame_shown{}; + bool show_frame{}; + std::array ref_frame_sign_bias{}; + s32 base_q_index{}; + s32 y_dc_delta_q{}; + s32 uv_dc_delta_q{}; + s32 uv_ac_delta_q{}; + bool lossless{}; + s32 transform_mode{}; + bool allow_high_precision_mv{}; + s32 interp_filter{}; + s32 reference_mode{}; + s8 comp_fixed_ref{}; + std::array comp_var_ref{}; + s32 log2_tile_cols{}; + s32 log2_tile_rows{}; + bool segment_enabled{}; + bool segment_map_update{}; + bool segment_map_temporal_update{}; + s32 segment_abs_delta{}; + std::array segment_feature_enable{}; + std::array, 8> segment_feature_data{}; + bool mode_ref_delta_enabled{}; + bool use_prev_in_find_mv_refs{}; + std::array ref_deltas{}; + std::array mode_deltas{}; + Vp9EntropyProbs entropy{}; + Vp9FrameDimensions frame_size{}; + u8 first_level{}; + u8 sharpness_level{}; + u32 bitstream_size{}; + std::array frame_offsets{}; + std::array refresh_frame{}; +}; + +struct Vp9FrameContainer { + Vp9PictureInfo info{}; + std::vector bit_stream; +}; + +struct PictureInfo { + INSERT_PADDING_WORDS(12); + u32 bitstream_size{}; + INSERT_PADDING_WORDS(5); + Vp9FrameDimensions last_frame_size{}; + Vp9FrameDimensions golden_frame_size{}; + Vp9FrameDimensions alt_frame_size{}; + Vp9FrameDimensions current_frame_size{}; + u32 vp9_flags{}; + std::array ref_frame_sign_bias{}; + u8 first_level{}; + u8 sharpness_level{}; + u8 base_q_index{}; + u8 y_dc_delta_q{}; + u8 uv_ac_delta_q{}; + u8 uv_dc_delta_q{}; + u8 lossless{}; + u8 tx_mode{}; + u8 allow_high_precision_mv{}; + u8 interp_filter{}; + u8 reference_mode{}; + s8 comp_fixed_ref{}; + std::array comp_var_ref{}; + u8 log2_tile_cols{}; + u8 log2_tile_rows{}; + Segmentation segmentation{}; + LoopFilter loop_filter{}; + INSERT_PADDING_BYTES(5); + u32 surface_params{}; + INSERT_PADDING_WORDS(3); + + Vp9PictureInfo Convert() const { + + return Vp9PictureInfo{ + .is_key_frame = (vp9_flags & FrameFlags::IsKeyFrame) != 0, + .intra_only = (vp9_flags & FrameFlags::IntraOnly) != 0, + .last_frame_was_key = (vp9_flags & FrameFlags::LastFrameIsKeyFrame) != 0, + .frame_size_changed = (vp9_flags & FrameFlags::FrameSizeChanged) != 0, + .error_resilient_mode = (vp9_flags & FrameFlags::ErrorResilientMode) != 0, + .last_frame_shown = (vp9_flags & FrameFlags::LastShowFrame) != 0, + .ref_frame_sign_bias = ref_frame_sign_bias, + .base_q_index = base_q_index, + .y_dc_delta_q = y_dc_delta_q, + .uv_dc_delta_q = uv_dc_delta_q, + .uv_ac_delta_q = uv_ac_delta_q, + .lossless = lossless != 0, + .transform_mode = tx_mode, + .allow_high_precision_mv = allow_high_precision_mv != 0, + .interp_filter = interp_filter, + .reference_mode = reference_mode, + .comp_fixed_ref = comp_fixed_ref, + .comp_var_ref = comp_var_ref, + .log2_tile_cols = log2_tile_cols, + .log2_tile_rows = log2_tile_rows, + .segment_enabled = segmentation.enabled != 0, + .segment_map_update = segmentation.update_map != 0, + .segment_map_temporal_update = segmentation.temporal_update != 0, + .segment_abs_delta = segmentation.abs_delta, + .segment_feature_enable = segmentation.feature_mask, + .segment_feature_data = segmentation.feature_data, + .mode_ref_delta_enabled = loop_filter.mode_ref_delta_enabled != 0, + .use_prev_in_find_mv_refs = !(vp9_flags == (FrameFlags::ErrorResilientMode)) && + !(vp9_flags == (FrameFlags::FrameSizeChanged)) && + !(vp9_flags == (FrameFlags::IntraOnly)) && + (vp9_flags == (FrameFlags::LastShowFrame)) && + !(vp9_flags == (FrameFlags::LastFrameIsKeyFrame)), + .ref_deltas = loop_filter.ref_deltas, + .mode_deltas = loop_filter.mode_deltas, + .frame_size = current_frame_size, + .first_level = first_level, + .sharpness_level = sharpness_level, + .bitstream_size = bitstream_size, + }; + } +}; +static_assert(sizeof(PictureInfo) == 0x100, "PictureInfo is an invalid size"); + +struct EntropyProbs { + INSERT_PADDING_BYTES(1024); + std::array, 7> inter_mode_prob{}; + std::array intra_inter_prob{}; + INSERT_PADDING_BYTES(80); + std::array, 2> tx_8x8_prob{}; + std::array, 2> tx_16x16_prob{}; + std::array, 2> tx_32x32_prob{}; + std::array y_mode_prob_e8{}; + std::array, 4> y_mode_prob_e0e7{}; + INSERT_PADDING_BYTES(64); + std::array, 16> partition_prob{}; + INSERT_PADDING_BYTES(10); + std::array, 4> switchable_interp_prob{}; + std::array comp_inter_prob{}; + std::array skip_probs{}; + std::array joints{}; + std::array sign{}; + std::array, 2> class_0{}; + std::array, 2> fr{}; + std::array class_0_hp{}; + std::array high_precision{}; + std::array, 2> classes{}; + std::array, 2>, 2> class_0_fr{}; + std::array, 2> pred_bits{}; + std::array, 5> single_ref_prob{}; + std::array comp_ref_prob{}; + INSERT_PADDING_BYTES(17); + std::array, 6>, 6>, 2>, 2>, 4> + coef_probs{}; + + void Convert(Vp9EntropyProbs& fc) { + std::memcpy(fc.inter_mode_prob.data(), inter_mode_prob.data(), fc.inter_mode_prob.size()); + + std::memcpy(fc.intra_inter_prob.data(), intra_inter_prob.data(), + fc.intra_inter_prob.size()); + + std::memcpy(fc.tx_8x8_prob.data(), tx_8x8_prob.data(), fc.tx_8x8_prob.size()); + std::memcpy(fc.tx_16x16_prob.data(), tx_16x16_prob.data(), fc.tx_16x16_prob.size()); + std::memcpy(fc.tx_32x32_prob.data(), tx_32x32_prob.data(), fc.tx_32x32_prob.size()); + + for (s32 i = 0; i < 4; i++) { + for (s32 j = 0; j < 9; j++) { + fc.y_mode_prob[j + 9 * i] = j < 8 ? y_mode_prob_e0e7[i][j] : y_mode_prob_e8[i]; + } + } + + std::memcpy(fc.partition_prob.data(), partition_prob.data(), fc.partition_prob.size()); + + std::memcpy(fc.switchable_interp_prob.data(), switchable_interp_prob.data(), + fc.switchable_interp_prob.size()); + std::memcpy(fc.comp_inter_prob.data(), comp_inter_prob.data(), fc.comp_inter_prob.size()); + std::memcpy(fc.skip_probs.data(), skip_probs.data(), fc.skip_probs.size()); + + std::memcpy(fc.joints.data(), joints.data(), fc.joints.size()); + + std::memcpy(fc.sign.data(), sign.data(), fc.sign.size()); + std::memcpy(fc.class_0.data(), class_0.data(), fc.class_0.size()); + std::memcpy(fc.fr.data(), fr.data(), fc.fr.size()); + std::memcpy(fc.class_0_hp.data(), class_0_hp.data(), fc.class_0_hp.size()); + std::memcpy(fc.high_precision.data(), high_precision.data(), fc.high_precision.size()); + std::memcpy(fc.classes.data(), classes.data(), fc.classes.size()); + std::memcpy(fc.class_0_fr.data(), class_0_fr.data(), fc.class_0_fr.size()); + std::memcpy(fc.prob_bits.data(), pred_bits.data(), fc.prob_bits.size()); + std::memcpy(fc.single_ref_prob.data(), single_ref_prob.data(), fc.single_ref_prob.size()); + std::memcpy(fc.comp_ref_prob.data(), comp_ref_prob.data(), fc.comp_ref_prob.size()); + + std::memcpy(fc.coef_probs.data(), coef_probs.data(), fc.coef_probs.size()); + } +}; +static_assert(sizeof(EntropyProbs) == 0xEA0, "EntropyProbs is an invalid size"); + +enum class Ref { Last, Golden, AltRef }; + +struct RefPoolElement { + s64 frame{}; + Ref ref{}; + bool refresh{}; +}; + +struct FrameContexts { + s64 from{}; + bool adapted{}; + Vp9EntropyProbs probs{}; +}; + +}; // namespace Decoder +}; // namespace Tegra diff --git a/src/video_core/command_classes/host1x.cpp b/src/video_core/command_classes/host1x.cpp new file mode 100644 index 000000000..a5234ee47 --- /dev/null +++ b/src/video_core/command_classes/host1x.cpp @@ -0,0 +1,39 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "video_core/command_classes/host1x.h" +#include "video_core/gpu.h" + +Tegra::Host1x::Host1x(GPU& gpu_) : gpu(gpu_) {} + +Tegra::Host1x::~Host1x() = default; + +void Tegra::Host1x::StateWrite(u32 offset, u32 arguments) { + u8* const state_offset = reinterpret_cast(&state) + offset * sizeof(u32); + std::memcpy(state_offset, &arguments, sizeof(u32)); +} + +void Tegra::Host1x::ProcessMethod(Host1x::Method method, const std::vector& arguments) { + StateWrite(static_cast(method), arguments[0]); + switch (method) { + case Method::WaitSyncpt: + Execute(arguments[0]); + break; + case Method::LoadSyncptPayload32: + syncpoint_value = arguments[0]; + break; + case Method::WaitSyncpt32: + Execute(arguments[0]); + break; + default: + UNIMPLEMENTED_MSG("Host1x method 0x{:X}", static_cast(method)); + break; + } +} + +void Tegra::Host1x::Execute(u32 data) { + // This method waits on a valid syncpoint. + // TODO: Implement when proper Async is in place +} diff --git a/src/video_core/command_classes/host1x.h b/src/video_core/command_classes/host1x.h new file mode 100644 index 000000000..501a5ed2e --- /dev/null +++ b/src/video_core/command_classes/host1x.h @@ -0,0 +1,78 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace Tegra { +class GPU; +class Nvdec; + +class Host1x { +public: + struct Host1xClassRegisters { + u32 incr_syncpt{}; + u32 incr_syncpt_ctrl{}; + u32 incr_syncpt_error{}; + INSERT_PADDING_WORDS(5); + u32 wait_syncpt{}; + u32 wait_syncpt_base{}; + u32 wait_syncpt_incr{}; + u32 load_syncpt_base{}; + u32 incr_syncpt_base{}; + u32 clear{}; + u32 wait{}; + u32 wait_with_interrupt{}; + u32 delay_use{}; + u32 tick_count_high{}; + u32 tick_count_low{}; + u32 tick_ctrl{}; + INSERT_PADDING_WORDS(23); + u32 ind_ctrl{}; + u32 ind_off2{}; + u32 ind_off{}; + std::array ind_data{}; + INSERT_PADDING_WORDS(1); + u32 load_syncpoint_payload32{}; + u32 stall_ctrl{}; + u32 wait_syncpt32{}; + u32 wait_syncpt_base32{}; + u32 load_syncpt_base32{}; + u32 incr_syncpt_base32{}; + u32 stall_count_high{}; + u32 stall_count_low{}; + u32 xref_ctrl{}; + u32 channel_xref_high{}; + u32 channel_xref_low{}; + }; + static_assert(sizeof(Host1xClassRegisters) == 0x164, "Host1xClassRegisters is an invalid size"); + + enum class Method : u32 { + WaitSyncpt = offsetof(Host1xClassRegisters, wait_syncpt) / 4, + LoadSyncptPayload32 = offsetof(Host1xClassRegisters, load_syncpoint_payload32) / 4, + WaitSyncpt32 = offsetof(Host1xClassRegisters, wait_syncpt32) / 4, + }; + + explicit Host1x(GPU& gpu); + ~Host1x(); + + /// Writes the method into the state, Invoke Execute() if encountered + void ProcessMethod(Host1x::Method method, const std::vector& arguments); + +private: + /// For Host1x, execute is waiting on a syncpoint previously written into the state + void Execute(u32 data); + + /// Write argument into the provided offset + void StateWrite(u32 offset, u32 arguments); + + u32 syncpoint_value{}; + Host1xClassRegisters state{}; + GPU& gpu; +}; + +} // namespace Tegra diff --git a/src/video_core/command_classes/nvdec.cpp b/src/video_core/command_classes/nvdec.cpp new file mode 100644 index 000000000..ede9466eb --- /dev/null +++ b/src/video_core/command_classes/nvdec.cpp @@ -0,0 +1,56 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "common/assert.h" +#include "common/bit_util.h" +#include "core/memory.h" +#include "video_core/command_classes/nvdec.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" + +namespace Tegra { + +Nvdec::Nvdec(GPU& gpu_) : gpu(gpu_), codec(std::make_unique(gpu)) {} + +Nvdec::~Nvdec() = default; + +void Nvdec::ProcessMethod(Nvdec::Method method, const std::vector& arguments) { + if (method == Method::SetVideoCodec) { + codec->StateWrite(static_cast(method), arguments[0]); + } else { + codec->StateWrite(static_cast(method), static_cast(arguments[0]) << 8); + } + + switch (method) { + case Method::SetVideoCodec: + codec->SetTargetCodec(static_cast(arguments[0])); + break; + case Method::Execute: + Execute(); + break; + } +} + +AVFrame* Nvdec::GetFrame() { + return codec->GetCurrentFrame(); +} + +const AVFrame* Nvdec::GetFrame() const { + return codec->GetCurrentFrame(); +} + +void Nvdec::Execute() { + switch (codec->GetCurrentCodec()) { + case NvdecCommon::VideoCodec::H264: + case NvdecCommon::VideoCodec::Vp9: + codec->Decode(); + break; + default: + UNIMPLEMENTED_MSG("Unknown codec {}", static_cast(codec->GetCurrentCodec())); + break; + } +} + +} // namespace Tegra diff --git a/src/video_core/command_classes/nvdec.h b/src/video_core/command_classes/nvdec.h new file mode 100644 index 000000000..c1a9d843e --- /dev/null +++ b/src/video_core/command_classes/nvdec.h @@ -0,0 +1,39 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "video_core/command_classes/codecs/codec.h" + +namespace Tegra { +class GPU; + +class Nvdec { +public: + enum class Method : u32 { + SetVideoCodec = 0x80, + Execute = 0xc0, + }; + + explicit Nvdec(GPU& gpu); + ~Nvdec(); + + /// Writes the method into the state, Invoke Execute() if encountered + void ProcessMethod(Nvdec::Method method, const std::vector& arguments); + + /// Return most recently decoded frame + AVFrame* GetFrame(); + const AVFrame* GetFrame() const; + +private: + /// Invoke codec to decode a frame + void Execute(); + + GPU& gpu; + std::unique_ptr codec; +}; +} // namespace Tegra diff --git a/src/video_core/command_classes/nvdec_common.h b/src/video_core/command_classes/nvdec_common.h new file mode 100644 index 000000000..01b5e086d --- /dev/null +++ b/src/video_core/command_classes/nvdec_common.h @@ -0,0 +1,48 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_funcs.h" +#include "common/common_types.h" + +namespace Tegra::NvdecCommon { + +struct NvdecRegisters { + INSERT_PADDING_WORDS(256); + u64 set_codec_id{}; + INSERT_PADDING_WORDS(254); + u64 set_platform_id{}; + u64 picture_info_offset{}; + u64 frame_bitstream_offset{}; + u64 frame_number{}; + u64 h264_slice_data_offsets{}; + u64 h264_mv_dump_offset{}; + INSERT_PADDING_WORDS(6); + u64 frame_stats_offset{}; + u64 h264_last_surface_luma_offset{}; + u64 h264_last_surface_chroma_offset{}; + std::array surface_luma_offset{}; + std::array surface_chroma_offset{}; + INSERT_PADDING_WORDS(132); + u64 vp9_entropy_probs_offset{}; + u64 vp9_backward_updates_offset{}; + u64 vp9_last_frame_segmap_offset{}; + u64 vp9_curr_frame_segmap_offset{}; + INSERT_PADDING_WORDS(2); + u64 vp9_last_frame_mvs_offset{}; + u64 vp9_curr_frame_mvs_offset{}; + INSERT_PADDING_WORDS(2); +}; +static_assert(sizeof(NvdecRegisters) == (0xBC0), "NvdecRegisters is incorrect size"); + +enum class VideoCodec : u32 { + None = 0x0, + H264 = 0x3, + Vp8 = 0x5, + H265 = 0x7, + Vp9 = 0x9, +}; + +} // namespace Tegra::NvdecCommon diff --git a/src/video_core/command_classes/sync_manager.cpp b/src/video_core/command_classes/sync_manager.cpp new file mode 100644 index 000000000..a0ab44855 --- /dev/null +++ b/src/video_core/command_classes/sync_manager.cpp @@ -0,0 +1,60 @@ +// MIT License +// +// Copyright (c) Ryujinx Team and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +// associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +#include +#include "sync_manager.h" +#include "video_core/gpu.h" + +namespace Tegra { +SyncptIncrManager::SyncptIncrManager(GPU& gpu_) : gpu(gpu_) {} +SyncptIncrManager::~SyncptIncrManager() = default; + +void SyncptIncrManager::Increment(u32 id) { + increments.push_back(SyncptIncr{0, id, true}); + IncrementAllDone(); +} + +u32 SyncptIncrManager::IncrementWhenDone(u32 class_id, u32 id) { + const u32 handle = current_id++; + increments.push_back(SyncptIncr{handle, class_id, id}); + return handle; +} + +void SyncptIncrManager::SignalDone(u32 handle) { + auto done_incr = std::find_if(increments.begin(), increments.end(), + [handle](SyncptIncr incr) { return incr.id == handle; }); + if (done_incr != increments.end()) { + const SyncptIncr incr = *done_incr; + *done_incr = SyncptIncr{incr.id, incr.class_id, incr.syncpt_id, true}; + } + IncrementAllDone(); +} + +void SyncptIncrManager::IncrementAllDone() { + std::size_t done_count = 0; + for (; done_count < increments.size(); ++done_count) { + if (!increments[done_count].complete) { + break; + } + gpu.IncrementSyncPoint(increments[done_count].syncpt_id); + } + increments.erase(increments.begin(), increments.begin() + done_count); +} +} // namespace Tegra diff --git a/src/video_core/command_classes/sync_manager.h b/src/video_core/command_classes/sync_manager.h new file mode 100644 index 000000000..353b67573 --- /dev/null +++ b/src/video_core/command_classes/sync_manager.h @@ -0,0 +1,64 @@ +// MIT License +// +// Copyright (c) Ryujinx Team and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +// associated documentation files (the "Software"), to deal in the Software without restriction, +// including without limitation the rights to use, copy, modify, merge, publish, distribute, +// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +#pragma once + +#include +#include +#include "common/common_types.h" + +namespace Tegra { +class GPU; +struct SyncptIncr { + u32 id; + u32 class_id; + u32 syncpt_id; + bool complete; + + SyncptIncr(u32 id, u32 syncpt_id_, u32 class_id_, bool done = false) + : id(id), class_id(class_id_), syncpt_id(syncpt_id_), complete(done) {} +}; + +class SyncptIncrManager { +public: + explicit SyncptIncrManager(GPU& gpu); + ~SyncptIncrManager(); + + /// Add syncpoint id and increment all + void Increment(u32 id); + + /// Returns a handle to increment later + u32 IncrementWhenDone(u32 class_id, u32 id); + + /// IncrememntAllDone, including handle + void SignalDone(u32 handle); + + /// Increment all sequential pending increments that are already done. + void IncrementAllDone(); + +private: + std::vector increments; + std::mutex increment_lock; + u32 current_id{}; + + GPU& gpu; +}; + +} // namespace Tegra diff --git a/src/video_core/command_classes/vic.cpp b/src/video_core/command_classes/vic.cpp new file mode 100644 index 000000000..66e15a1a8 --- /dev/null +++ b/src/video_core/command_classes/vic.cpp @@ -0,0 +1,180 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "common/assert.h" +#include "video_core/command_classes/nvdec.h" +#include "video_core/command_classes/vic.h" +#include "video_core/engines/maxwell_3d.h" +#include "video_core/gpu.h" +#include "video_core/memory_manager.h" +#include "video_core/texture_cache/surface_params.h" + +extern "C" { +#include +} + +namespace Tegra { + +Vic::Vic(GPU& gpu_, std::shared_ptr nvdec_processor_) + : gpu(gpu_), nvdec_processor(std::move(nvdec_processor_)) {} +Vic::~Vic() = default; + +void Vic::VicStateWrite(u32 offset, u32 arguments) { + u8* const state_offset = reinterpret_cast(&vic_state) + offset * sizeof(u32); + std::memcpy(state_offset, &arguments, sizeof(u32)); +} + +void Vic::ProcessMethod(Vic::Method method, const std::vector& arguments) { + LOG_DEBUG(HW_GPU, "Vic method 0x{:X}", static_cast(method)); + VicStateWrite(static_cast(method), arguments[0]); + const u64 arg = static_cast(arguments[0]) << 8; + switch (method) { + case Method::Execute: + Execute(); + break; + case Method::SetConfigStructOffset: + config_struct_address = arg; + break; + case Method::SetOutputSurfaceLumaOffset: + output_surface_luma_address = arg; + break; + case Method::SetOutputSurfaceChromaUOffset: + output_surface_chroma_u_address = arg; + break; + case Method::SetOutputSurfaceChromaVOffset: + output_surface_chroma_v_address = arg; + break; + default: + break; + } +} + +void Vic::Execute() { + if (output_surface_luma_address == 0) { + LOG_ERROR(Service_NVDRV, "VIC Luma address not set. Recieved 0x{:X}", + vic_state.output_surface.luma_offset); + return; + } + const VicConfig config{gpu.MemoryManager().Read(config_struct_address + 0x20)}; + const VideoPixelFormat pixel_format = + static_cast(config.pixel_format.Value()); + switch (pixel_format) { + case VideoPixelFormat::BGRA8: + case VideoPixelFormat::RGBA8: { + LOG_TRACE(Service_NVDRV, "Writing RGB Frame"); + const auto* frame = nvdec_processor->GetFrame(); + + if (!frame || frame->width == 0 || frame->height == 0) { + return; + } + if (scaler_ctx == nullptr || frame->width != scaler_width || + frame->height != scaler_height) { + const AVPixelFormat target_format = + (pixel_format == VideoPixelFormat::RGBA8) ? AV_PIX_FMT_RGBA : AV_PIX_FMT_BGRA; + + sws_freeContext(scaler_ctx); + scaler_ctx = nullptr; + + // FFmpeg returns all frames in YUV420, convert it into expected format + scaler_ctx = + sws_getContext(frame->width, frame->height, AV_PIX_FMT_YUV420P, frame->width, + frame->height, target_format, 0, nullptr, nullptr, nullptr); + + scaler_width = frame->width; + scaler_height = frame->height; + } + // Get Converted frame + const std::size_t linear_size = frame->width * frame->height * 4; + + using AVMallocPtr = std::unique_ptr; + AVMallocPtr converted_frame_buffer{static_cast(av_malloc(linear_size)), av_free}; + + const int converted_stride{frame->width * 4}; + u8* const converted_frame_buf_addr{converted_frame_buffer.get()}; + + sws_scale(scaler_ctx, frame->data, frame->linesize, 0, frame->height, + &converted_frame_buf_addr, &converted_stride); + + const u32 blk_kind = static_cast(config.block_linear_kind); + if (blk_kind != 0) { + // swizzle pitch linear to block linear + const u32 block_height = static_cast(config.block_linear_height_log2); + const auto size = Tegra::Texture::CalculateSize(true, 4, frame->width, frame->height, 1, + block_height, 0); + std::vector swizzled_data(size); + Tegra::Texture::CopySwizzledData(frame->width, frame->height, 1, 4, 4, + swizzled_data.data(), converted_frame_buffer.get(), + false, block_height, 0, 1); + + gpu.MemoryManager().WriteBlock(output_surface_luma_address, swizzled_data.data(), size); + gpu.Maxwell3D().OnMemoryWrite(); + } else { + // send pitch linear frame + gpu.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr, + linear_size); + gpu.Maxwell3D().OnMemoryWrite(); + } + break; + } + case VideoPixelFormat::Yuv420: { + LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame"); + + const auto* frame = nvdec_processor->GetFrame(); + + if (!frame || frame->width == 0 || frame->height == 0) { + return; + } + + const std::size_t surface_width = config.surface_width_minus1 + 1; + const std::size_t surface_height = config.surface_height_minus1 + 1; + const std::size_t half_width = surface_width / 2; + const std::size_t half_height = config.surface_height_minus1 / 2; + const std::size_t aligned_width = (surface_width + 0xff) & ~0xff; + + const auto* luma_ptr = frame->data[0]; + const auto* chroma_b_ptr = frame->data[1]; + const auto* chroma_r_ptr = frame->data[2]; + const auto stride = frame->linesize[0]; + const auto half_stride = frame->linesize[1]; + + std::vector luma_buffer(aligned_width * surface_height); + std::vector chroma_buffer(aligned_width * half_height); + + // Populate luma buffer + for (std::size_t y = 0; y < surface_height - 1; ++y) { + std::size_t src = y * stride; + std::size_t dst = y * aligned_width; + + std::size_t size = surface_width; + + for (std::size_t offset = 0; offset < size; ++offset) { + luma_buffer[dst + offset] = luma_ptr[src + offset]; + } + } + gpu.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(), + luma_buffer.size()); + + // Populate chroma buffer from both channels with interleaving. + for (std::size_t y = 0; y < half_height; ++y) { + std::size_t src = y * half_stride; + std::size_t dst = y * aligned_width; + + for (std::size_t x = 0; x < half_width; ++x) { + chroma_buffer[dst + x * 2] = chroma_b_ptr[src + x]; + chroma_buffer[dst + x * 2 + 1] = chroma_r_ptr[src + x]; + } + } + gpu.MemoryManager().WriteBlock(output_surface_chroma_u_address, chroma_buffer.data(), + chroma_buffer.size()); + gpu.Maxwell3D().OnMemoryWrite(); + break; + } + default: + UNIMPLEMENTED_MSG("Unknown video pixel format {}", config.pixel_format.Value()); + break; + } +} + +} // namespace Tegra diff --git a/src/video_core/command_classes/vic.h b/src/video_core/command_classes/vic.h new file mode 100644 index 000000000..dd0a2aed8 --- /dev/null +++ b/src/video_core/command_classes/vic.h @@ -0,0 +1,110 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include "common/bit_field.h" +#include "common/common_types.h" + +struct SwsContext; + +namespace Tegra { +class GPU; +class Nvdec; + +struct PlaneOffsets { + u32 luma_offset{}; + u32 chroma_u_offset{}; + u32 chroma_v_offset{}; +}; + +struct VicRegisters { + INSERT_PADDING_WORDS(64); + u32 nop{}; + INSERT_PADDING_WORDS(15); + u32 pm_trigger{}; + INSERT_PADDING_WORDS(47); + u32 set_application_id{}; + u32 set_watchdog_timer{}; + INSERT_PADDING_WORDS(17); + u32 context_save_area{}; + u32 context_switch{}; + INSERT_PADDING_WORDS(43); + u32 execute{}; + INSERT_PADDING_WORDS(63); + std::array, 8> surfacex_slots{}; + u32 picture_index{}; + u32 control_params{}; + u32 config_struct_offset{}; + u32 filter_struct_offset{}; + u32 palette_offset{}; + u32 hist_offset{}; + u32 context_id{}; + u32 fce_ucode_size{}; + PlaneOffsets output_surface{}; + u32 fce_ucode_offset{}; + INSERT_PADDING_WORDS(4); + std::array slot_context_id{}; + INSERT_PADDING_WORDS(16); +}; +static_assert(sizeof(VicRegisters) == 0x7A0, "VicRegisters is an invalid size"); + +class Vic { +public: + enum class Method : u32 { + Execute = 0xc0, + SetControlParams = 0x1c1, + SetConfigStructOffset = 0x1c2, + SetOutputSurfaceLumaOffset = 0x1c8, + SetOutputSurfaceChromaUOffset = 0x1c9, + SetOutputSurfaceChromaVOffset = 0x1ca + }; + + explicit Vic(GPU& gpu, std::shared_ptr nvdec_processor); + ~Vic(); + + /// Write to the device state. + void ProcessMethod(Vic::Method method, const std::vector& arguments); + +private: + void Execute(); + + void VicStateWrite(u32 offset, u32 arguments); + VicRegisters vic_state{}; + + enum class VideoPixelFormat : u64_le { + RGBA8 = 0x1f, + BGRA8 = 0x20, + Yuv420 = 0x44, + }; + + union VicConfig { + u64_le raw{}; + BitField<0, 7, u64_le> pixel_format; + BitField<7, 2, u64_le> chroma_loc_horiz; + BitField<9, 2, u64_le> chroma_loc_vert; + BitField<11, 4, u64_le> block_linear_kind; + BitField<15, 4, u64_le> block_linear_height_log2; + BitField<19, 3, u64_le> reserved0; + BitField<22, 10, u64_le> reserved1; + BitField<32, 14, u64_le> surface_width_minus1; + BitField<46, 14, u64_le> surface_height_minus1; + }; + + GPU& gpu; + std::shared_ptr nvdec_processor; + + GPUVAddr config_struct_address{}; + GPUVAddr output_surface_luma_address{}; + GPUVAddr output_surface_chroma_u_address{}; + GPUVAddr output_surface_chroma_v_address{}; + + SwsContext* scaler_ctx{}; + s32 scaler_width{}; + s32 scaler_height{}; +}; + +} // namespace Tegra diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp index 4bb9256e9..171f78183 100644 --- a/src/video_core/gpu.cpp +++ b/src/video_core/gpu.cpp @@ -27,9 +27,10 @@ namespace Tegra { MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192)); -GPU::GPU(Core::System& system_, bool is_async_) +GPU::GPU(Core::System& system_, bool is_async_, bool use_nvdec_) : system{system_}, memory_manager{std::make_unique(system)}, dma_pusher{std::make_unique(system, *this)}, + cdma_pusher{std::make_unique(*this)}, use_nvdec{use_nvdec_}, maxwell_3d{std::make_unique(system, *memory_manager)}, fermi_2d{std::make_unique()}, kepler_compute{std::make_unique(system, *memory_manager)}, @@ -77,10 +78,18 @@ DmaPusher& GPU::DmaPusher() { return *dma_pusher; } +Tegra::CDmaPusher& GPU::CDmaPusher() { + return *cdma_pusher; +} + const DmaPusher& GPU::DmaPusher() const { return *dma_pusher; } +const Tegra::CDmaPusher& GPU::CDmaPusher() const { + return *cdma_pusher; +} + void GPU::WaitFence(u32 syncpoint_id, u32 value) { // Synced GPU, is always in sync if (!is_async) { diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h index 2d15d1c6f..b8c613b11 100644 --- a/src/video_core/gpu.h +++ b/src/video_core/gpu.h @@ -13,6 +13,7 @@ #include "common/common_types.h" #include "core/hle/service/nvdrv/nvdata.h" #include "core/hle/service/nvflinger/buffer_queue.h" +#include "video_core/cdma_pusher.h" #include "video_core/dma_pusher.h" using CacheAddr = std::uintptr_t; @@ -157,7 +158,7 @@ public: method_count(method_count) {} }; - explicit GPU(Core::System& system, bool is_async); + explicit GPU(Core::System& system, bool is_async, bool use_nvdec); virtual ~GPU(); /// Binds a renderer to the GPU. @@ -209,6 +210,15 @@ public: /// Returns a reference to the GPU DMA pusher. Tegra::DmaPusher& DmaPusher(); + /// Returns a const reference to the GPU DMA pusher. + const Tegra::DmaPusher& DmaPusher() const; + + /// Returns a reference to the GPU CDMA pusher. + Tegra::CDmaPusher& CDmaPusher(); + + /// Returns a const reference to the GPU CDMA pusher. + const Tegra::CDmaPusher& CDmaPusher() const; + VideoCore::RendererBase& Renderer() { return *renderer; } @@ -249,8 +259,9 @@ public: return is_async; } - /// Returns a const reference to the GPU DMA pusher. - const Tegra::DmaPusher& DmaPusher() const; + bool UseNvdec() const { + return use_nvdec; + } struct Regs { static constexpr size_t NUM_REGS = 0x40; @@ -311,6 +322,9 @@ public: /// Push GPU command entries to be processed virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0; + /// Push GPU command buffer entries to be processed + virtual void PushCommandBuffer(Tegra::ChCommandHeaderList& entries) = 0; + /// Swap buffers (render frame) virtual void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) = 0; @@ -349,7 +363,9 @@ protected: Core::System& system; std::unique_ptr memory_manager; std::unique_ptr dma_pusher; + std::unique_ptr cdma_pusher; std::unique_ptr renderer; + const bool use_nvdec; private: /// Mapping of command subchannels to their bound engine ids @@ -372,6 +388,7 @@ private: std::array, Service::Nvidia::MaxSyncPoints> syncpt_interrupts; std::mutex sync_mutex; + std::mutex device_mutex; std::condition_variable sync_cv; diff --git a/src/video_core/gpu_asynch.cpp b/src/video_core/gpu_asynch.cpp index 70a3d5738..a9baaf7ef 100644 --- a/src/video_core/gpu_asynch.cpp +++ b/src/video_core/gpu_asynch.cpp @@ -10,12 +10,13 @@ namespace VideoCommon { -GPUAsynch::GPUAsynch(Core::System& system) : GPU{system, true}, gpu_thread{system} {} +GPUAsynch::GPUAsynch(Core::System& system, bool use_nvdec) + : GPU{system, true, use_nvdec}, gpu_thread{system} {} GPUAsynch::~GPUAsynch() = default; void GPUAsynch::Start() { - gpu_thread.StartThread(*renderer, renderer->Context(), *dma_pusher); + gpu_thread.StartThread(*renderer, renderer->Context(), *dma_pusher, *cdma_pusher); cpu_context = renderer->GetRenderWindow().CreateSharedContext(); cpu_context->MakeCurrent(); } @@ -32,6 +33,27 @@ void GPUAsynch::PushGPUEntries(Tegra::CommandList&& entries) { gpu_thread.SubmitList(std::move(entries)); } +void GPUAsynch::PushCommandBuffer(Tegra::ChCommandHeaderList& entries) { + if (!use_nvdec) { + return; + } + // This condition fires when a video stream ends, clear all intermediary data + if (entries[0].raw == 0xDEADB33F) { + cdma_pusher.reset(); + return; + } + if (!cdma_pusher) { + cdma_pusher = std::make_unique(*this); + } + + // SubmitCommandBuffer would make the nvdec operations async, this is not currently working + // TODO(ameerj): RE proper async nvdec operation + // gpu_thread.SubmitCommandBuffer(std::move(entries)); + + cdma_pusher->Push(std::move(entries)); + cdma_pusher->DispatchCalls(); +} + void GPUAsynch::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { gpu_thread.SwapBuffers(framebuffer); } diff --git a/src/video_core/gpu_asynch.h b/src/video_core/gpu_asynch.h index f89c855a5..0c0872e73 100644 --- a/src/video_core/gpu_asynch.h +++ b/src/video_core/gpu_asynch.h @@ -20,13 +20,14 @@ namespace VideoCommon { /// Implementation of GPU interface that runs the GPU asynchronously class GPUAsynch final : public Tegra::GPU { public: - explicit GPUAsynch(Core::System& system); + explicit GPUAsynch(Core::System& system, bool use_nvdec); ~GPUAsynch() override; void Start() override; void ObtainContext() override; void ReleaseContext() override; void PushGPUEntries(Tegra::CommandList&& entries) override; + void PushCommandBuffer(Tegra::ChCommandHeaderList& entries) override; void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override; void FlushRegion(VAddr addr, u64 size) override; void InvalidateRegion(VAddr addr, u64 size) override; diff --git a/src/video_core/gpu_synch.cpp b/src/video_core/gpu_synch.cpp index 1ca47ddef..ecf7bbdf3 100644 --- a/src/video_core/gpu_synch.cpp +++ b/src/video_core/gpu_synch.cpp @@ -7,7 +7,7 @@ namespace VideoCommon { -GPUSynch::GPUSynch(Core::System& system) : GPU{system, false} {} +GPUSynch::GPUSynch(Core::System& system, bool use_nvdec) : GPU{system, false, use_nvdec} {} GPUSynch::~GPUSynch() = default; @@ -26,6 +26,22 @@ void GPUSynch::PushGPUEntries(Tegra::CommandList&& entries) { dma_pusher->DispatchCalls(); } +void GPUSynch::PushCommandBuffer(Tegra::ChCommandHeaderList& entries) { + if (!use_nvdec) { + return; + } + // This condition fires when a video stream ends, clears all intermediary data + if (entries[0].raw == 0xDEADB33F) { + cdma_pusher.reset(); + return; + } + if (!cdma_pusher) { + cdma_pusher = std::make_unique(*this); + } + cdma_pusher->Push(std::move(entries)); + cdma_pusher->DispatchCalls(); +} + void GPUSynch::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { renderer->SwapBuffers(framebuffer); } diff --git a/src/video_core/gpu_synch.h b/src/video_core/gpu_synch.h index 297258cb1..9d778c71a 100644 --- a/src/video_core/gpu_synch.h +++ b/src/video_core/gpu_synch.h @@ -19,13 +19,14 @@ namespace VideoCommon { /// Implementation of GPU interface that runs the GPU synchronously class GPUSynch final : public Tegra::GPU { public: - explicit GPUSynch(Core::System& system); + explicit GPUSynch(Core::System& system, bool use_nvdec); ~GPUSynch() override; void Start() override; void ObtainContext() override; void ReleaseContext() override; void PushGPUEntries(Tegra::CommandList&& entries) override; + void PushCommandBuffer(Tegra::ChCommandHeaderList& entries) override; void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) override; void FlushRegion(VAddr addr, u64 size) override; void InvalidateRegion(VAddr addr, u64 size) override; diff --git a/src/video_core/gpu_thread.cpp b/src/video_core/gpu_thread.cpp index bf761abf2..4b8f58283 100644 --- a/src/video_core/gpu_thread.cpp +++ b/src/video_core/gpu_thread.cpp @@ -18,7 +18,7 @@ namespace VideoCommon::GPUThread { /// Runs the GPU thread static void RunThread(Core::System& system, VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, Tegra::DmaPusher& dma_pusher, - SynchState& state) { + SynchState& state, Tegra::CDmaPusher& cdma_pusher) { std::string name = "yuzu:GPU"; MicroProfileOnThreadCreate(name.c_str()); Common::SetCurrentThreadName(name.c_str()); @@ -42,6 +42,10 @@ static void RunThread(Core::System& system, VideoCore::RendererBase& renderer, if (const auto submit_list = std::get_if(&next.data)) { dma_pusher.Push(std::move(submit_list->entries)); dma_pusher.DispatchCalls(); + } else if (const auto command_list = std::get_if(&next.data)) { + // NVDEC + cdma_pusher.Push(std::move(command_list->entries)); + cdma_pusher.DispatchCalls(); } else if (const auto data = std::get_if(&next.data)) { renderer.SwapBuffers(data->framebuffer ? &*data->framebuffer : nullptr); } else if (std::holds_alternative(next.data)) { @@ -75,15 +79,19 @@ ThreadManager::~ThreadManager() { void ThreadManager::StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, - Tegra::DmaPusher& dma_pusher) { - thread = std::thread{RunThread, std::ref(system), std::ref(renderer), - std::ref(context), std::ref(dma_pusher), std::ref(state)}; + Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher) { + thread = std::thread(RunThread, std::ref(system), std::ref(renderer), std::ref(context), + std::ref(dma_pusher), std::ref(state), std::ref(cdma_pusher)); } void ThreadManager::SubmitList(Tegra::CommandList&& entries) { PushCommand(SubmitListCommand(std::move(entries))); } +void ThreadManager::SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries) { + PushCommand(SubmitChCommandEntries(std::move(entries))); +} + void ThreadManager::SwapBuffers(const Tegra::FramebufferConfig* framebuffer) { PushCommand(SwapBuffersCommand(framebuffer ? std::make_optional(*framebuffer) : std::nullopt)); } diff --git a/src/video_core/gpu_thread.h b/src/video_core/gpu_thread.h index 5a28335d6..32a34e3a7 100644 --- a/src/video_core/gpu_thread.h +++ b/src/video_core/gpu_thread.h @@ -37,6 +37,14 @@ struct SubmitListCommand final { Tegra::CommandList entries; }; +/// Command to signal to the GPU thread that a cdma command list is ready for processing +struct SubmitChCommandEntries final { + explicit SubmitChCommandEntries(Tegra::ChCommandHeaderList&& entries) + : entries{std::move(entries)} {} + + Tegra::ChCommandHeaderList entries; +}; + /// Command to signal to the GPU thread that a swap buffers is pending struct SwapBuffersCommand final { explicit SwapBuffersCommand(std::optional framebuffer) @@ -77,9 +85,9 @@ struct OnCommandListEndCommand final {}; struct GPUTickCommand final {}; using CommandData = - std::variant; + std::variant; struct CommandDataContainer { CommandDataContainer() = default; @@ -109,11 +117,14 @@ public: /// Creates and starts the GPU thread. void StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context, - Tegra::DmaPusher& dma_pusher); + Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher); /// Push GPU command entries to be processed void SubmitList(Tegra::CommandList&& entries); + /// Push GPU CDMA command buffer entries to be processed + void SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries); + /// Swap buffers (render frame) void SwapBuffers(const Tegra::FramebufferConfig* framebuffer); diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp index 02cf53d15..6e70bd362 100644 --- a/src/video_core/memory_manager.cpp +++ b/src/video_core/memory_manager.cpp @@ -11,6 +11,7 @@ #include "video_core/gpu.h" #include "video_core/memory_manager.h" #include "video_core/rasterizer_interface.h" +#include "video_core/renderer_base.h" namespace Tegra { @@ -44,6 +45,12 @@ GPUVAddr MemoryManager::MapAllocate(VAddr cpu_addr, std::size_t size, std::size_ return Map(cpu_addr, *FindFreeRange(size, align), size); } +GPUVAddr MemoryManager::MapAllocate32(VAddr cpu_addr, std::size_t size) { + const std::optional gpu_addr = FindFreeRange(size, 1, true); + ASSERT(gpu_addr); + return Map(cpu_addr, *gpu_addr, size); +} + void MemoryManager::Unmap(GPUVAddr gpu_addr, std::size_t size) { if (!size) { return; @@ -108,7 +115,8 @@ void MemoryManager::SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::s page_table[PageEntryIndex(gpu_addr)] = page_entry; } -std::optional MemoryManager::FindFreeRange(std::size_t size, std::size_t align) const { +std::optional MemoryManager::FindFreeRange(std::size_t size, std::size_t align, + bool start_32bit_address) const { if (!align) { align = page_size; } else { @@ -116,7 +124,7 @@ std::optional MemoryManager::FindFreeRange(std::size_t size, std::size } u64 available_size{}; - GPUVAddr gpu_addr{address_space_start}; + GPUVAddr gpu_addr{start_32bit_address ? address_space_start_low : address_space_start}; while (gpu_addr + available_size < address_space_size) { if (GetPageEntry(gpu_addr + available_size).IsUnmapped()) { available_size += page_size; diff --git a/src/video_core/memory_manager.h b/src/video_core/memory_manager.h index 53c8d122a..c078193d9 100644 --- a/src/video_core/memory_manager.h +++ b/src/video_core/memory_manager.h @@ -116,6 +116,7 @@ public: [[nodiscard]] GPUVAddr Map(VAddr cpu_addr, GPUVAddr gpu_addr, std::size_t size); [[nodiscard]] GPUVAddr MapAllocate(VAddr cpu_addr, std::size_t size, std::size_t align); + [[nodiscard]] GPUVAddr MapAllocate32(VAddr cpu_addr, std::size_t size); [[nodiscard]] std::optional AllocateFixed(GPUVAddr gpu_addr, std::size_t size); [[nodiscard]] GPUVAddr Allocate(std::size_t size, std::size_t align); void Unmap(GPUVAddr gpu_addr, std::size_t size); @@ -124,7 +125,8 @@ private: [[nodiscard]] PageEntry GetPageEntry(GPUVAddr gpu_addr) const; void SetPageEntry(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size = page_size); GPUVAddr UpdateRange(GPUVAddr gpu_addr, PageEntry page_entry, std::size_t size); - [[nodiscard]] std::optional FindFreeRange(std::size_t size, std::size_t align) const; + [[nodiscard]] std::optional FindFreeRange(std::size_t size, std::size_t align, + bool start_32bit_address = false) const; void TryLockPage(PageEntry page_entry, std::size_t size); void TryUnlockPage(PageEntry page_entry, std::size_t size); @@ -135,6 +137,7 @@ private: static constexpr u64 address_space_size = 1ULL << 40; static constexpr u64 address_space_start = 1ULL << 32; + static constexpr u64 address_space_start_low = 1ULL << 16; static constexpr u64 page_bits{16}; static constexpr u64 page_size{1 << page_bits}; static constexpr u64 page_mask{page_size - 1}; diff --git a/src/video_core/video_core.cpp b/src/video_core/video_core.cpp index a14df06a3..dd5cee4a1 100644 --- a/src/video_core/video_core.cpp +++ b/src/video_core/video_core.cpp @@ -44,10 +44,11 @@ namespace VideoCore { std::unique_ptr CreateGPU(Core::Frontend::EmuWindow& emu_window, Core::System& system) { std::unique_ptr gpu; + const bool use_nvdec = Settings::values.use_nvdec_emulation.GetValue(); if (Settings::values.use_asynchronous_gpu_emulation.GetValue()) { - gpu = std::make_unique(system); + gpu = std::make_unique(system, use_nvdec); } else { - gpu = std::make_unique(system); + gpu = std::make_unique(system, use_nvdec); } auto context = emu_window.CreateSharedContext(); diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt index cc0291b15..4659e1f89 100644 --- a/src/yuzu/CMakeLists.txt +++ b/src/yuzu/CMakeLists.txt @@ -265,9 +265,11 @@ if (MSVC) include(CopyYuzuQt5Deps) include(CopyYuzuSDLDeps) include(CopyYuzuUnicornDeps) + include(CopyYuzuFFmpegDeps) copy_yuzu_Qt5_deps(yuzu) copy_yuzu_SDL_deps(yuzu) copy_yuzu_unicorn_deps(yuzu) + copy_yuzu_FFmpeg_deps(yuzu) endif() if (NOT APPLE) diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index d2913d613..abbc83929 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -717,6 +717,8 @@ void Config::ReadRendererValues() { ReadSettingGlobal(Settings::values.gpu_accuracy, QStringLiteral("gpu_accuracy"), 0); ReadSettingGlobal(Settings::values.use_asynchronous_gpu_emulation, QStringLiteral("use_asynchronous_gpu_emulation"), false); + ReadSettingGlobal(Settings::values.use_nvdec_emulation, QStringLiteral("use_nvdec_emulation"), + true); ReadSettingGlobal(Settings::values.use_vsync, QStringLiteral("use_vsync"), true); ReadSettingGlobal(Settings::values.use_assembly_shaders, QStringLiteral("use_assembly_shaders"), false); @@ -1265,6 +1267,8 @@ void Config::SaveRendererValues() { Settings::values.gpu_accuracy.UsingGlobal(), 0); WriteSettingGlobal(QStringLiteral("use_asynchronous_gpu_emulation"), Settings::values.use_asynchronous_gpu_emulation, false); + WriteSettingGlobal(QStringLiteral("use_nvdec_emulation"), Settings::values.use_nvdec_emulation, + true); WriteSettingGlobal(QStringLiteral("use_vsync"), Settings::values.use_vsync, true); WriteSettingGlobal(QStringLiteral("use_assembly_shaders"), Settings::values.use_assembly_shaders, false); diff --git a/src/yuzu/configuration/configure_graphics.cpp b/src/yuzu/configuration/configure_graphics.cpp index 07d818548..4f083ecda 100644 --- a/src/yuzu/configuration/configure_graphics.cpp +++ b/src/yuzu/configuration/configure_graphics.cpp @@ -70,9 +70,11 @@ void ConfigureGraphics::SetConfiguration() { ui->api->setEnabled(runtime_lock); ui->use_asynchronous_gpu_emulation->setEnabled(runtime_lock); ui->use_disk_shader_cache->setEnabled(runtime_lock); + ui->use_nvdec_emulation->setEnabled(runtime_lock); ui->use_disk_shader_cache->setChecked(Settings::values.use_disk_shader_cache.GetValue()); ui->use_asynchronous_gpu_emulation->setChecked( Settings::values.use_asynchronous_gpu_emulation.GetValue()); + ui->use_nvdec_emulation->setChecked(Settings::values.use_nvdec_emulation.GetValue()); if (Settings::configuring_global) { ui->api->setCurrentIndex(static_cast(Settings::values.renderer_backend.GetValue())); @@ -116,6 +118,9 @@ void ConfigureGraphics::ApplyConfiguration() { Settings::values.use_asynchronous_gpu_emulation.SetValue( ui->use_asynchronous_gpu_emulation->isChecked()); } + if (Settings::values.use_nvdec_emulation.UsingGlobal()) { + Settings::values.use_nvdec_emulation.SetValue(ui->use_nvdec_emulation->isChecked()); + } if (Settings::values.bg_red.UsingGlobal()) { Settings::values.bg_red.SetValue(static_cast(bg_color.redF())); Settings::values.bg_green.SetValue(static_cast(bg_color.greenF())); @@ -144,6 +149,8 @@ void ConfigureGraphics::ApplyConfiguration() { ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_asynchronous_gpu_emulation, ui->use_asynchronous_gpu_emulation, use_asynchronous_gpu_emulation); + ConfigurationShared::ApplyPerGameSetting(&Settings::values.use_nvdec_emulation, + ui->use_nvdec_emulation, use_nvdec_emulation); if (ui->bg_combobox->currentIndex() == ConfigurationShared::USE_GLOBAL_INDEX) { Settings::values.bg_red.SetGlobal(true); @@ -240,6 +247,7 @@ void ConfigureGraphics::SetupPerGameUI() { ui->aspect_ratio_combobox->setEnabled(Settings::values.aspect_ratio.UsingGlobal()); ui->use_asynchronous_gpu_emulation->setEnabled( Settings::values.use_asynchronous_gpu_emulation.UsingGlobal()); + ui->use_nvdec_emulation->setEnabled(Settings::values.use_nvdec_emulation.UsingGlobal()); ui->use_disk_shader_cache->setEnabled(Settings::values.use_disk_shader_cache.UsingGlobal()); ui->bg_button->setEnabled(Settings::values.bg_red.UsingGlobal()); @@ -253,6 +261,8 @@ void ConfigureGraphics::SetupPerGameUI() { ConfigurationShared::SetColoredTristate( ui->use_disk_shader_cache, Settings::values.use_disk_shader_cache, use_disk_shader_cache); + ConfigurationShared::SetColoredTristate( + ui->use_nvdec_emulation, Settings::values.use_nvdec_emulation, use_nvdec_emulation); ConfigurationShared::SetColoredTristate(ui->use_asynchronous_gpu_emulation, Settings::values.use_asynchronous_gpu_emulation, use_asynchronous_gpu_emulation); diff --git a/src/yuzu/configuration/configure_graphics.h b/src/yuzu/configuration/configure_graphics.h index b4961f719..1fefc88eb 100644 --- a/src/yuzu/configuration/configure_graphics.h +++ b/src/yuzu/configuration/configure_graphics.h @@ -46,6 +46,7 @@ private: std::unique_ptr ui; QColor bg_color; + ConfigurationShared::CheckState use_nvdec_emulation; ConfigurationShared::CheckState use_disk_shader_cache; ConfigurationShared::CheckState use_asynchronous_gpu_emulation; diff --git a/src/yuzu/configuration/configure_graphics.ui b/src/yuzu/configuration/configure_graphics.ui index 62aa337e7..58486eb1e 100644 --- a/src/yuzu/configuration/configure_graphics.ui +++ b/src/yuzu/configuration/configure_graphics.ui @@ -97,6 +97,13 @@ + + + + Use NVDEC emulation + + + -- cgit v1.2.3 From cdb2480d391b3c57fa014c2ab65824f7c9e378fc Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 28 Oct 2020 01:42:41 -0300 Subject: common/fiber: Take shared_ptr by copy in YieldTo YieldTo does not intend to modify the passed shared_ptrs. Pass it by copy to keep a reference count while this function executes. --- src/common/fiber.cpp | 4 ++-- src/common/fiber.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 1c1d09ccb..e186ed880 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -91,7 +91,7 @@ void Fiber::Rewind() { SwitchToFiber(impl->rewind_handle); } -void Fiber::YieldTo(std::shared_ptr& from, std::shared_ptr& to) { +void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); to->guard.lock(); @@ -199,7 +199,7 @@ void Fiber::Rewind() { boost::context::detail::jump_fcontext(impl->rewind_context, this); } -void Fiber::YieldTo(std::shared_ptr& from, std::shared_ptr& to) { +void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); to->guard.lock(); diff --git a/src/common/fiber.h b/src/common/fiber.h index 89dde5e36..cefd61df9 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -46,7 +46,7 @@ public: /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. - static void YieldTo(std::shared_ptr& from, std::shared_ptr& to); + static void YieldTo(std::shared_ptr from, std::shared_ptr to); [[nodiscard]] static std::shared_ptr ThreadToFiber(); void SetRewindPoint(std::function&& rewind_func, void* start_parameter); -- cgit v1.2.3 From 8049b8beb625686a4edd9fd1bdf133496e6f462c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 29 Oct 2020 22:55:56 -0400 Subject: common/stream: Be explicit with copy and move operators --- src/common/stream.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/stream.h b/src/common/stream.h index 2585c16af..0e40692de 100644 --- a/src/common/stream.h +++ b/src/common/stream.h @@ -21,6 +21,12 @@ public: explicit Stream(); ~Stream(); + Stream(const Stream&) = delete; + Stream& operator=(const Stream&) = delete; + + Stream(Stream&&) = default; + Stream& operator=(Stream&&) = default; + /// Reposition bitstream "cursor" to the specified offset from origin void Seek(s32 offset, SeekOrigin origin); @@ -30,15 +36,15 @@ public: /// Writes byte at current position void WriteByte(u8 byte); - std::size_t GetPosition() const { + [[nodiscard]] std::size_t GetPosition() const { return position; } - std::vector& GetBuffer() { + [[nodiscard]] std::vector& GetBuffer() { return buffer; } - const std::vector& GetBuffer() const { + [[nodiscard]] const std::vector& GetBuffer() const { return buffer; } -- cgit v1.2.3 From 26547d3e3be403047445fd39e671d7ef3b88dabb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 29 Oct 2020 23:30:42 -0400 Subject: General: Make ignoring a discarded return value an error Allows our CI to catch more potential bugs. This also removes the [[nodiscard]] attribute of IOFile's Open member function. There are cases where a file may want to be opened, but have the status of it checked at a later time. --- src/CMakeLists.txt | 6 +++++- src/common/file_util.h | 2 +- src/common/misc.cpp | 15 +++++++++++---- 3 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src/common') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71efbb40d..29c6ffc67 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -32,7 +32,6 @@ if (MSVC) # /Zc:inline - Let codegen omit inline functions in object files # /Zc:throwingNew - Let codegen assume `operator new` (without std::nothrow) will never return null add_compile_options( - /W3 /MP /Zi /Zo @@ -43,6 +42,10 @@ if (MSVC) /Zc:externConstexpr /Zc:inline /Zc:throwingNew + + # Warnings + /W3 + /we4834 # Discarding return value of function with 'nodiscard' attribute ) # /GS- - No stack buffer overflow checks @@ -56,6 +59,7 @@ else() -Werror=implicit-fallthrough -Werror=missing-declarations -Werror=reorder + -Werror=unused-result -Wextra -Wmissing-declarations -Wno-attributes diff --git a/src/common/file_util.h b/src/common/file_util.h index 8b587320f..840cde2a6 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -232,7 +232,7 @@ public: void Swap(IOFile& other) noexcept; - [[nodiscard]] bool Open(const std::string& filename, const char openmode[], int flags = 0); + bool Open(const std::string& filename, const char openmode[], int flags = 0); bool Close(); template diff --git a/src/common/misc.cpp b/src/common/misc.cpp index 68cb86cd1..1d5393597 100644 --- a/src/common/misc.cpp +++ b/src/common/misc.cpp @@ -16,16 +16,23 @@ // Call directly after the command or use the error num. // This function might change the error code. std::string GetLastErrorMsg() { - static const std::size_t buff_size = 255; + static constexpr std::size_t buff_size = 255; char err_str[buff_size]; #ifdef _WIN32 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), err_str, buff_size, nullptr); + return std::string(err_str, buff_size); +#elif defined(__GLIBC__) && (_GNU_SOURCE || (_POSIX_C_SOURCE < 200112L && _XOPEN_SOURCE < 600)) + // Thread safe (GNU-specific) + const char* str = strerror_r(errno, err_str, buff_size); + return std::string(str); #else // Thread safe (XSI-compliant) - strerror_r(errno, err_str, buff_size); + const int success = strerror_r(errno, err_str, buff_size); + if (success != 0) { + return {}; + } + return std::string(err_str); #endif - - return std::string(err_str, buff_size); } -- cgit v1.2.3 From 4a4b685a0420b0ac7c026cd2370c23d54f469976 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 30 Oct 2020 15:02:02 -0400 Subject: common: Enable warnings as errors Cleans up common so that we can enable warnings as errors. --- externals/microprofile/microprofile.h | 14 ++++++++------ src/common/CMakeLists.txt | 16 ++++++++++++++++ src/common/fiber.cpp | 8 ++++---- src/common/fiber.h | 2 +- src/common/file_util.cpp | 31 ++++++++++++++++++------------- src/common/logging/backend.cpp | 2 -- src/common/string_util.cpp | 5 +++-- src/common/timer.cpp | 12 +++++------- src/common/wall_clock.cpp | 2 +- src/common/x64/native_clock.h | 2 +- 10 files changed, 57 insertions(+), 37 deletions(-) (limited to 'src/common') diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h index 85d5bd5de..5c381f002 100644 --- a/externals/microprofile/microprofile.h +++ b/externals/microprofile/microprofile.h @@ -902,8 +902,10 @@ inline uint16_t MicroProfileGetGroupIndex(MicroProfileToken t) #include #define snprintf _snprintf +#ifdef _MSC_VER #pragma warning(push) #pragma warning(disable: 4244) +#endif int64_t MicroProfileTicksPerSecondCpu() { static int64_t nTicksPerSecond = 0; @@ -946,7 +948,7 @@ typedef HANDLE MicroProfileThread; DWORD _stdcall ThreadTrampoline(void* pFunc) { MicroProfileThreadFunc F = (MicroProfileThreadFunc)pFunc; - return (uint32_t)F(0); + return (DWORD)F(0); } inline void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) @@ -1742,10 +1744,10 @@ void MicroProfileFlip() } } } - for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) + for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j) { - pLog->nGroupTicks[i] += nGroupTicks[i]; - pFrameGroup[i] += nGroupTicks[i]; + pLog->nGroupTicks[j] += nGroupTicks[j]; + pFrameGroup[j] += nGroupTicks[j]; } pLog->nStackPos = nStackPos; } @@ -3328,7 +3330,7 @@ bool MicroProfileIsLocalThread(uint32_t nThreadId) #endif #else -bool MicroProfileIsLocalThread(uint32_t nThreadId){return false;} +bool MicroProfileIsLocalThread([[maybe_unused]] uint32_t nThreadId) { return false; } void MicroProfileStopContextSwitchTrace(){} void MicroProfileStartContextSwitchTrace(){} @@ -3576,7 +3578,7 @@ int MicroProfileGetGpuTickReference(int64_t* pOutCpu, int64_t* pOutGpu) #undef S -#ifdef _WIN32 +#ifdef _MSC_VER #pragma warning(pop) #endif diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index e50ab2922..207c7a0a6 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -190,6 +190,22 @@ if(ARCHITECTURE_x86_64) ) endif() +if (MSVC) + target_compile_definitions(common PRIVATE + # The standard library doesn't provide any replacement for codecvt yet + # so we can disable this deprecation warning for the time being. + _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING + ) + target_compile_options(common PRIVATE + /W4 + /WX + ) +else() + target_compile_options(common PRIVATE + -Werror + ) +endif() + create_target_directory_groups(common) find_package(Boost 1.71 COMPONENTS context headers REQUIRED) diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index e186ed880..b209f52fc 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -79,9 +79,9 @@ void Fiber::Exit() { released = true; } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* start_parameter) { +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { rewind_point = std::move(rewind_func); - rewind_parameter = start_parameter; + rewind_parameter = rewind_param; } void Fiber::Rewind() { @@ -161,9 +161,9 @@ Fiber::Fiber(std::function&& entry_point_func, void* start_paramete boost::context::detail::make_fcontext(stack_base, impl->stack.size(), FiberStartFunc); } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* start_parameter) { +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { rewind_point = std::move(rewind_func); - rewind_parameter = start_parameter; + rewind_parameter = rewind_param; } Fiber::Fiber() : impl{std::make_unique()} {} diff --git a/src/common/fiber.h b/src/common/fiber.h index cefd61df9..699286ee2 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -49,7 +49,7 @@ public: static void YieldTo(std::shared_ptr from, std::shared_ptr to); [[nodiscard]] static std::shared_ptr ThreadToFiber(); - void SetRewindPoint(std::function&& rewind_func, void* start_parameter); + void SetRewindPoint(std::function&& rewind_func, void* rewind_param); void Rewind(); diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 16c3713e0..18fbfa25b 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -472,13 +472,14 @@ u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, } bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { - const auto callback = [recursion](u64* num_entries_out, const std::string& directory, - const std::string& virtual_name) -> bool { - std::string new_path = directory + DIR_SEP_CHR + virtual_name; + const auto callback = [recursion](u64*, const std::string& directory, + const std::string& virtual_name) { + const std::string new_path = directory + DIR_SEP_CHR + virtual_name; if (IsDirectory(new_path)) { - if (recursion == 0) + if (recursion == 0) { return false; + } return DeleteDirRecursively(new_path, recursion - 1); } return Delete(new_path); @@ -492,7 +493,8 @@ bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) return true; } -void CopyDir(const std::string& source_path, const std::string& dest_path) { +void CopyDir([[maybe_unused]] const std::string& source_path, + [[maybe_unused]] const std::string& dest_path) { #ifndef _WIN32 if (source_path == dest_path) { return; @@ -553,7 +555,7 @@ std::optional GetCurrentDir() { std::string strDir = dir; #endif free(dir); - return std::move(strDir); + return strDir; } bool SetCurrentDir(const std::string& directory) { @@ -772,21 +774,23 @@ std::size_t ReadFileToString(bool text_file, const std::string& filename, std::s void SplitFilename83(const std::string& filename, std::array& short_name, std::array& extension) { - const std::string forbidden_characters = ".\"/\\[]:;=, "; + static constexpr std::string_view forbidden_characters = ".\"/\\[]:;=, "; // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces. short_name = {{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'}}; extension = {{' ', ' ', ' ', '\0'}}; - std::string::size_type point = filename.rfind('.'); - if (point == filename.size() - 1) + auto point = filename.rfind('.'); + if (point == filename.size() - 1) { point = filename.rfind('.', point); + } // Get short name. int j = 0; for (char letter : filename.substr(0, point)) { - if (forbidden_characters.find(letter, 0) != std::string::npos) + if (forbidden_characters.find(letter, 0) != std::string::npos) { continue; + } if (j == 8) { // TODO(Link Mauve): also do that for filenames containing a space. // TODO(Link Mauve): handle multiple files having the same short name. @@ -794,14 +798,15 @@ void SplitFilename83(const std::string& filename, std::array& short_nam short_name[7] = '1'; break; } - short_name[j++] = toupper(letter); + short_name[j++] = static_cast(std::toupper(letter)); } // Get extension. if (point != std::string::npos) { j = 0; - for (char letter : filename.substr(point + 1, 3)) - extension[j++] = toupper(letter); + for (char letter : filename.substr(point + 1, 3)) { + extension[j++] = static_cast(std::toupper(letter)); + } } } diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 62cfde397..90dfa22ca 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -274,7 +274,6 @@ const char* GetLogClassName(Class log_class) { case Class::Count: break; } - UNREACHABLE(); return "Invalid"; } @@ -293,7 +292,6 @@ const char* GetLevelName(Level log_level) { break; } #undef LVL - UNREACHABLE(); return "Invalid"; } diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp index 84883a1d3..4cba2aaa4 100644 --- a/src/common/string_util.cpp +++ b/src/common/string_util.cpp @@ -8,6 +8,7 @@ #include #include #include + #include "common/common_paths.h" #include "common/logging/log.h" #include "common/string_util.h" @@ -21,14 +22,14 @@ namespace Common { /// Make a string lowercase std::string ToLower(std::string str) { std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return std::tolower(c); }); + [](unsigned char c) { return static_cast(std::tolower(c)); }); return str; } /// Make a string uppercase std::string ToUpper(std::string str) { std::transform(str.begin(), str.end(), str.begin(), - [](unsigned char c) { return std::toupper(c); }); + [](unsigned char c) { return static_cast(std::toupper(c)); }); return str; } diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 2dc15e434..d17dc2a50 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -142,20 +142,18 @@ std::string Timer::GetTimeFormatted() { // ---------------- double Timer::GetDoubleTime() { // Get continuous timestamp - u64 TmpSeconds = static_cast(Common::Timer::GetTimeSinceJan1970().count()); - double ms = static_cast(GetTimeMs().count()) % 1000; + auto tmp_seconds = static_cast(GetTimeSinceJan1970().count()); + const auto ms = static_cast(static_cast(GetTimeMs().count()) % 1000); // Remove a few years. We only really want enough seconds to make // sure that we are detecting actual actions, perhaps 60 seconds is // enough really, but I leave a year of seconds anyway, in case the // user's clock is incorrect or something like that. - TmpSeconds = TmpSeconds - (38 * 365 * 24 * 60 * 60); + tmp_seconds = tmp_seconds - (38 * 365 * 24 * 60 * 60); // Make a smaller integer that fits in the double - u32 Seconds = static_cast(TmpSeconds); - double TmpTime = Seconds + ms; - - return TmpTime; + const auto seconds = static_cast(tmp_seconds); + return seconds + ms; } } // Namespace Common diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 7a20e95b7..452a2837e 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -53,7 +53,7 @@ public: return Common::Divide128On32(temporary, 1000000000).first; } - void Pause(bool is_paused) override { + void Pause([[maybe_unused]] bool is_paused) override { // Do nothing in this clock type. } diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 7c503df26..97aab6ac9 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -34,7 +34,7 @@ private: /// value used to reduce the native clocks accuracy as some apss rely on /// undefined behavior where the level of accuracy in the clock shouldn't /// be higher. - static constexpr u64 inaccuracy_mask = ~(0x400 - 1); + static constexpr u64 inaccuracy_mask = ~(UINT64_C(0x400) - 1); SpinLock rtsc_serialize{}; u64 last_measure{}; -- cgit v1.2.3 From 6f006d051e1fad075048ea5664e1ef0605e48a46 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 4 Nov 2020 20:41:16 -0500 Subject: General: Fix clang build Allows building on clang to work again --- src/common/fiber.h | 4 ++-- src/common/spin_lock.h | 8 ++++++++ src/video_core/dma_pusher.h | 7 +++---- src/video_core/gpu.h | 2 +- 4 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.h b/src/common/fiber.h index 699286ee2..1a027be96 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -41,8 +41,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = default; - Fiber& operator=(Fiber&&) = default; + Fiber(Fiber&&) = delete; + Fiber& operator=(Fiber&&) = delete; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. diff --git a/src/common/spin_lock.h b/src/common/spin_lock.h index 4f946a258..06ac2f5bb 100644 --- a/src/common/spin_lock.h +++ b/src/common/spin_lock.h @@ -15,6 +15,14 @@ namespace Common { */ class SpinLock { public: + SpinLock() = default; + + SpinLock(const SpinLock&) = delete; + SpinLock& operator=(const SpinLock&) = delete; + + SpinLock(SpinLock&&) = delete; + SpinLock& operator=(SpinLock&&) = delete; + void lock(); void unlock(); [[nodiscard]] bool try_lock(); diff --git a/src/video_core/dma_pusher.h b/src/video_core/dma_pusher.h index 8496ba2da..9d9a750d9 100644 --- a/src/video_core/dma_pusher.h +++ b/src/video_core/dma_pusher.h @@ -18,6 +18,8 @@ class System; namespace Tegra { +class GPU; + enum class SubmissionMode : u32 { IncreasingOld = 0, Increasing = 1, @@ -74,8 +76,7 @@ union CommandHeader { static_assert(std::is_standard_layout_v, "CommandHeader is not standard layout"); static_assert(sizeof(CommandHeader) == sizeof(u32), "CommandHeader has incorrect size!"); -static constexpr CommandHeader BuildCommandHeader(BufferMethods method, u32 arg_count, - SubmissionMode mode) { +inline CommandHeader BuildCommandHeader(BufferMethods method, u32 arg_count, SubmissionMode mode) { CommandHeader result{}; result.method.Assign(static_cast(method)); result.arg_count.Assign(arg_count); @@ -83,8 +84,6 @@ static constexpr CommandHeader BuildCommandHeader(BufferMethods method, u32 arg_ return result; } -class GPU; - struct CommandList final { CommandList() = default; explicit CommandList(std::size_t size) : command_lists(size) {} diff --git a/src/video_core/gpu.h b/src/video_core/gpu.h index 5444b49f3..cf5235a79 100644 --- a/src/video_core/gpu.h +++ b/src/video_core/gpu.h @@ -273,7 +273,7 @@ public: BitField<0, 1, FenceOperation> op; BitField<8, 24, u32> syncpoint_id; - static constexpr CommandHeader Build(FenceOperation op, u32 syncpoint_id) { + static CommandHeader Build(FenceOperation op, u32 syncpoint_id) { FenceAction result{}; result.op.Assign(op); result.syncpoint_id.Assign(syncpoint_id); -- cgit v1.2.3 From 00fb79b2f3f49335543c57f1d27b057320f3ff05 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Nov 2020 20:29:54 -0500 Subject: common/fiber: Move all member variables into impl class Hides all of the implementation details for users of the class. This has the benefit of reducing includes and also making the fiber classes movable again. --- src/common/fiber.cpp | 155 +++++++++++++++++++++++++++------------------------ src/common/fiber.h | 20 +------ 2 files changed, 86 insertions(+), 89 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index b209f52fc..3e3029cd1 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -4,6 +4,8 @@ #include "common/assert.h" #include "common/fiber.h" +#include "common/spin_lock.h" + #if defined(_WIN32) || defined(WIN32) #include #else @@ -14,18 +16,45 @@ namespace Common { constexpr std::size_t default_stack_size = 256 * 1024; // 256kb -#if defined(_WIN32) || defined(WIN32) - struct Fiber::FiberImpl { + SpinLock guard{}; + std::function entry_point; + std::function rewind_point; + void* rewind_parameter{}; + void* start_parameter{}; + std::shared_ptr previous_fiber; + bool is_thread_fiber{}; + bool released{}; + +#if defined(_WIN32) || defined(WIN32) LPVOID handle = nullptr; LPVOID rewind_handle = nullptr; +#else + alignas(64) std::array stack; + alignas(64) std::array rewind_stack; + u8* stack_limit; + u8* rewind_stack_limit; + boost::context::detail::fcontext_t context; + boost::context::detail::fcontext_t rewind_context; +#endif }; +void Fiber::SetStartParameter(void* new_parameter) { + impl->start_parameter = new_parameter; +} + +void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { + impl->rewind_point = std::move(rewind_func); + impl->rewind_parameter = rewind_param; +} + +#if defined(_WIN32) || defined(WIN32) + void Fiber::Start() { - ASSERT(previous_fiber != nullptr); - previous_fiber->guard.unlock(); - previous_fiber.reset(); - entry_point(start_parameter); + ASSERT(impl->previous_fiber != nullptr); + impl->previous_fiber->impl->guard.unlock(); + impl->previous_fiber.reset(); + impl->entry_point(impl->start_parameter); UNREACHABLE(); } @@ -34,58 +63,54 @@ void Fiber::OnRewind() { DeleteFiber(impl->handle); impl->handle = impl->rewind_handle; impl->rewind_handle = nullptr; - rewind_point(rewind_parameter); + impl->rewind_point(impl->rewind_parameter); UNREACHABLE(); } void Fiber::FiberStartFunc(void* fiber_parameter) { - auto fiber = static_cast(fiber_parameter); + auto* fiber = static_cast(fiber_parameter); fiber->Start(); } void Fiber::RewindStartFunc(void* fiber_parameter) { - auto fiber = static_cast(fiber_parameter); + auto* fiber = static_cast(fiber_parameter); fiber->OnRewind(); } Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : entry_point{std::move(entry_point_func)}, start_parameter{start_parameter} { - impl = std::make_unique(); + : impl{std::make_unique()} { + impl->entry_point = std::move(entry_point_func); + impl->start_parameter = start_parameter; impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this); } Fiber::Fiber() : impl{std::make_unique()} {} Fiber::~Fiber() { - if (released) { + if (impl->released) { return; } // Make sure the Fiber is not being used - const bool locked = guard.try_lock(); + const bool locked = impl->guard.try_lock(); ASSERT_MSG(locked, "Destroying a fiber that's still running"); if (locked) { - guard.unlock(); + impl->guard.unlock(); } DeleteFiber(impl->handle); } void Fiber::Exit() { - ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber"); - if (!is_thread_fiber) { + ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); + if (!impl->is_thread_fiber) { return; } ConvertFiberToThread(); - guard.unlock(); - released = true; -} - -void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { - rewind_point = std::move(rewind_func); - rewind_parameter = rewind_param; + impl->guard.unlock(); + impl->released = true; } void Fiber::Rewind() { - ASSERT(rewind_point); + ASSERT(impl->rewind_point); ASSERT(impl->rewind_handle == nullptr); impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this); SwitchToFiber(impl->rewind_handle); @@ -94,39 +119,30 @@ void Fiber::Rewind() { void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->guard.lock(); - to->previous_fiber = from; + to->impl->guard.lock(); + to->impl->previous_fiber = from; SwitchToFiber(to->impl->handle); - ASSERT(from->previous_fiber != nullptr); - from->previous_fiber->guard.unlock(); - from->previous_fiber.reset(); + ASSERT(from->impl->previous_fiber != nullptr); + from->impl->previous_fiber->impl->guard.unlock(); + from->impl->previous_fiber.reset(); } std::shared_ptr Fiber::ThreadToFiber() { std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->guard.lock(); + fiber->impl->guard.lock(); fiber->impl->handle = ConvertThreadToFiber(nullptr); - fiber->is_thread_fiber = true; + fiber->impl->is_thread_fiber = true; return fiber; } #else -struct Fiber::FiberImpl { - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; - u8* stack_limit; - u8* rewind_stack_limit; - boost::context::detail::fcontext_t context; - boost::context::detail::fcontext_t rewind_context; -}; - void Fiber::Start(boost::context::detail::transfer_t& transfer) { - ASSERT(previous_fiber != nullptr); - previous_fiber->impl->context = transfer.fctx; - previous_fiber->guard.unlock(); - previous_fiber.reset(); - entry_point(start_parameter); + ASSERT(impl->previous_fiber != nullptr); + impl->previous_fiber->impl->context = transfer.fctx; + impl->previous_fiber->impl->guard.unlock(); + impl->previous_fiber.reset(); + impl->entry_point(impl->start_parameter); UNREACHABLE(); } @@ -137,23 +153,24 @@ void Fiber::OnRewind([[maybe_unused]] boost::context::detail::transfer_t& transf u8* tmp = impl->stack_limit; impl->stack_limit = impl->rewind_stack_limit; impl->rewind_stack_limit = tmp; - rewind_point(rewind_parameter); + impl->rewind_point(impl->rewind_parameter); UNREACHABLE(); } void Fiber::FiberStartFunc(boost::context::detail::transfer_t transfer) { - auto fiber = static_cast(transfer.data); + auto* fiber = static_cast(transfer.data); fiber->Start(transfer); } void Fiber::RewindStartFunc(boost::context::detail::transfer_t transfer) { - auto fiber = static_cast(transfer.data); + auto* fiber = static_cast(transfer.data); fiber->OnRewind(transfer); } Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : entry_point{std::move(entry_point_func)}, start_parameter{start_parameter} { - impl = std::make_unique(); + : impl{std::make_unique()} { + impl->entry_point = std::move(entry_point_func); + impl->start_parameter = start_parameter; impl->stack_limit = impl->stack.data(); impl->rewind_stack_limit = impl->rewind_stack.data(); u8* stack_base = impl->stack_limit + default_stack_size; @@ -161,37 +178,31 @@ Fiber::Fiber(std::function&& entry_point_func, void* start_paramete boost::context::detail::make_fcontext(stack_base, impl->stack.size(), FiberStartFunc); } -void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewind_param) { - rewind_point = std::move(rewind_func); - rewind_parameter = rewind_param; -} - Fiber::Fiber() : impl{std::make_unique()} {} Fiber::~Fiber() { - if (released) { + if (impl->released) { return; } // Make sure the Fiber is not being used - const bool locked = guard.try_lock(); + const bool locked = impl->guard.try_lock(); ASSERT_MSG(locked, "Destroying a fiber that's still running"); if (locked) { - guard.unlock(); + impl->guard.unlock(); } } void Fiber::Exit() { - - ASSERT_MSG(is_thread_fiber, "Exitting non main thread fiber"); - if (!is_thread_fiber) { + ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); + if (!impl->is_thread_fiber) { return; } - guard.unlock(); - released = true; + impl->guard.unlock(); + impl->released = true; } void Fiber::Rewind() { - ASSERT(rewind_point); + ASSERT(impl->rewind_point); ASSERT(impl->rewind_context == nullptr); u8* stack_base = impl->rewind_stack_limit + default_stack_size; impl->rewind_context = @@ -202,19 +213,19 @@ void Fiber::Rewind() { void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->guard.lock(); - to->previous_fiber = from; + to->impl->guard.lock(); + to->impl->previous_fiber = from; auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); - ASSERT(from->previous_fiber != nullptr); - from->previous_fiber->impl->context = transfer.fctx; - from->previous_fiber->guard.unlock(); - from->previous_fiber.reset(); + ASSERT(from->impl->previous_fiber != nullptr); + from->impl->previous_fiber->impl->context = transfer.fctx; + from->impl->previous_fiber->impl->guard.unlock(); + from->impl->previous_fiber.reset(); } std::shared_ptr Fiber::ThreadToFiber() { std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->guard.lock(); - fiber->is_thread_fiber = true; + fiber->impl->guard.lock(); + fiber->impl->is_thread_fiber = true; return fiber; } diff --git a/src/common/fiber.h b/src/common/fiber.h index 1a027be96..5323e8579 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -7,9 +7,6 @@ #include #include -#include "common/common_types.h" -#include "common/spin_lock.h" - #if !defined(_WIN32) && !defined(WIN32) namespace boost::context::detail { struct transfer_t; @@ -41,8 +38,8 @@ public: Fiber(const Fiber&) = delete; Fiber& operator=(const Fiber&) = delete; - Fiber(Fiber&&) = delete; - Fiber& operator=(Fiber&&) = delete; + Fiber(Fiber&&) = default; + Fiber& operator=(Fiber&&) = default; /// Yields control from Fiber 'from' to Fiber 'to' /// Fiber 'from' must be the currently running fiber. @@ -57,9 +54,7 @@ public: void Exit(); /// Changes the start parameter of the fiber. Has no effect if the fiber already started - void SetStartParameter(void* new_parameter) { - start_parameter = new_parameter; - } + void SetStartParameter(void* new_parameter); private: Fiber(); @@ -77,16 +72,7 @@ private: #endif struct FiberImpl; - - SpinLock guard{}; - std::function entry_point; - std::function rewind_point; - void* rewind_parameter{}; - void* start_parameter{}; - std::shared_ptr previous_fiber; std::unique_ptr impl; - bool is_thread_fiber{}; - bool released{}; }; } // namespace Common -- cgit v1.2.3 From 0890451c554973b907dc8febe6e65b499f21ab09 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:43:24 -0500 Subject: page_table: Remove unnecessary header inclusions Prevents indirect inclusions for these headers. --- src/common/page_table.h | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index cf5eed780..9908947ac 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,10 +4,6 @@ #pragma once -#include - -#include - #include "common/common_types.h" #include "common/memory_hook.h" #include "common/virtual_buffer.h" -- cgit v1.2.3 From 3cfd962ef471fa064f0f72eceff2a592e37b6642 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:45:17 -0500 Subject: page_table: Add missing doxygen parameters to Resize() Resolves two -Wdocumentation warnings. --- src/common/page_table.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index 9908947ac..a5be7a668 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -54,6 +54,8 @@ struct PageTable { * a given address space. * * @param address_space_width_in_bits The address size width in bits. + * @param page_size_in_bits The page size in bits. + * @param has_attribute Whether or not this page has any backing attributes. */ void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, bool has_attribute); -- cgit v1.2.3 From b3c8997829ed986c948d195bc1e7c8f66c42755e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 19:58:41 -0500 Subject: page_table: Allow page tables to be moved Makes page tables and virtual buffers able to be moved, but not copied, making the interface more flexible. Previously, with the destructor specified, but no move assignment or constructor specified, they wouldn't be implicitly generated. --- src/common/page_table.cpp | 2 +- src/common/page_table.h | 10 +++++++++- src/common/virtual_buffer.cpp | 4 ++-- src/common/virtual_buffer.h | 23 ++++++++++++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp index e5d3090d5..bccea0894 100644 --- a/src/common/page_table.cpp +++ b/src/common/page_table.cpp @@ -8,7 +8,7 @@ namespace Common { PageTable::PageTable() = default; -PageTable::~PageTable() = default; +PageTable::~PageTable() noexcept = default; void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, bool has_attribute) { diff --git a/src/common/page_table.h b/src/common/page_table.h index a5be7a668..9754fabf9 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,6 +4,8 @@ #pragma once +#include + #include "common/common_types.h" #include "common/memory_hook.h" #include "common/virtual_buffer.h" @@ -47,7 +49,13 @@ struct SpecialRegion { */ struct PageTable { PageTable(); - ~PageTable(); + ~PageTable() noexcept; + + PageTable(const PageTable&) = delete; + PageTable& operator=(const PageTable&) = delete; + + PageTable(PageTable&&) noexcept = default; + PageTable& operator=(PageTable&&) noexcept = default; /** * Resizes the page table to be able to accomodate enough pages within diff --git a/src/common/virtual_buffer.cpp b/src/common/virtual_buffer.cpp index b009cb500..e3ca29258 100644 --- a/src/common/virtual_buffer.cpp +++ b/src/common/virtual_buffer.cpp @@ -13,7 +13,7 @@ namespace Common { -void* AllocateMemoryPages(std::size_t size) { +void* AllocateMemoryPages(std::size_t size) noexcept { #ifdef _WIN32 void* base{VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE)}; #else @@ -29,7 +29,7 @@ void* AllocateMemoryPages(std::size_t size) { return base; } -void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) { +void FreeMemoryPages(void* base, [[maybe_unused]] std::size_t size) noexcept { if (!base) { return; } diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 125cb42f0..363913d45 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -4,25 +4,38 @@ #pragma once -#include "common/common_funcs.h" +#include namespace Common { -void* AllocateMemoryPages(std::size_t size); -void FreeMemoryPages(void* base, std::size_t size); +void* AllocateMemoryPages(std::size_t size) noexcept; +void FreeMemoryPages(void* base, std::size_t size) noexcept; template -class VirtualBuffer final : NonCopyable { +class VirtualBuffer final { public: constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); } - ~VirtualBuffer() { + ~VirtualBuffer() noexcept { FreeMemoryPages(base_ptr, alloc_size); } + VirtualBuffer(const VirtualBuffer&) = delete; + VirtualBuffer& operator=(const VirtualBuffer&) = delete; + + VirtualBuffer(VirtualBuffer&& other) noexcept + : alloc_size{std::exchange(other.alloc_size, 0)}, base_ptr{std::exchange(other.base_ptr), + nullptr} {} + + VirtualBuffer& operator=(VirtualBuffer&& other) noexcept { + alloc_size = std::exchange(other.alloc_size, 0); + base_ptr = std::exchange(other.base_ptr, nullptr); + return *this; + } + void resize(std::size_t count) { FreeMemoryPages(base_ptr, alloc_size); -- cgit v1.2.3 From 0ca91ced2dab3654e78e4c465506d7baad3cbeeb Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 17 Nov 2020 20:09:55 -0500 Subject: virtual_buffer: Add compile-time type-safety guarantees with VirtualBuffer VirtualBuffer makes use of VirtualAlloc (on Windows) and mmap() (on other platforms). Neither of these ensure that non-trivial objects are properly constructed in the allocated memory. To prevent potential undefined behavior occurring due to that, we can add a static assert to loudly complain about cases where that is done. --- src/common/virtual_buffer.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/common') diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 363913d45..078e61c77 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -4,6 +4,7 @@ #pragma once +#include #include namespace Common { @@ -14,6 +15,11 @@ void FreeMemoryPages(void* base, std::size_t size) noexcept; template class VirtualBuffer final { public: + static_assert( + std::is_trivially_constructible_v, + "T must be trivially constructible, as non-trivial constructors will not be executed " + "with the current allocator"); + constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); -- cgit v1.2.3 From 412044960a39180bc9990f6b35e1872c6a69591b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 19 Nov 2020 07:54:00 -0500 Subject: virtual_buffer: Do nothing on resize() calls with same sizes Prevents us from churning memory by freeing and reallocating a memory block that would have already been adequate as is. --- src/common/virtual_buffer.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 078e61c77..91d430036 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -43,9 +43,14 @@ public: } void resize(std::size_t count) { + const auto new_size = count * sizeof(T); + if (new_size == alloc_size) { + return; + } + FreeMemoryPages(base_ptr, alloc_size); - alloc_size = count * sizeof(T); + alloc_size = new_size; base_ptr = reinterpret_cast(AllocateMemoryPages(alloc_size)); } -- cgit v1.2.3 From 6e3767648253b2162b86ecdca6fcbc31e9471595 Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 19 Nov 2020 12:35:07 -0800 Subject: hle: service: Stub OLSC Initialize and SetSaveDataBackupSettingEnabled functions. - Used by Animal Cross: New Horizons v1.6.0 update, minimal stub gets this update working. --- src/common/logging/backend.cpp | 1 + src/common/logging/log.h | 1 + src/core/CMakeLists.txt | 2 ++ src/core/hle/service/olsc/olsc.cpp | 69 ++++++++++++++++++++++++++++++++++++++ src/core/hle/service/olsc/olsc.h | 16 +++++++++ src/core/hle/service/service.cpp | 2 ++ 6 files changed, 91 insertions(+) create mode 100644 src/core/hle/service/olsc/olsc.cpp create mode 100644 src/core/hle/service/olsc/olsc.h (limited to 'src/common') diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 90dfa22ca..7859344b9 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -222,6 +222,7 @@ void DebuggerBackend::Write(const Entry& entry) { SUB(Service, NPNS) \ SUB(Service, NS) \ SUB(Service, NVDRV) \ + SUB(Service, OLSC) \ SUB(Service, PCIE) \ SUB(Service, PCTL) \ SUB(Service, PCV) \ diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 13a4f1e30..835894918 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -95,6 +95,7 @@ enum class Class : ClassType { Service_NPNS, ///< The NPNS service Service_NS, ///< The NS services Service_NVDRV, ///< The NVDRV (Nvidia driver) service + Service_OLSC, ///< The OLSC service Service_PCIE, ///< The PCIe service Service_PCTL, ///< The PCTL (Parental control) service Service_PCV, ///< The PCV service diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index a1d8dcfa5..e370fd225 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -458,6 +458,8 @@ add_library(core STATIC hle/service/nvflinger/buffer_queue.h hle/service/nvflinger/nvflinger.cpp hle/service/nvflinger/nvflinger.h + hle/service/olsc/olsc.cpp + hle/service/olsc/olsc.h hle/service/pcie/pcie.cpp hle/service/pcie/pcie.h hle/service/pctl/module.cpp diff --git a/src/core/hle/service/olsc/olsc.cpp b/src/core/hle/service/olsc/olsc.cpp new file mode 100644 index 000000000..deb23dbde --- /dev/null +++ b/src/core/hle/service/olsc/olsc.cpp @@ -0,0 +1,69 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/ipc_helpers.h" +#include "core/hle/kernel/hle_ipc.h" +#include "core/hle/service/olsc/olsc.h" +#include "core/hle/service/service.h" +#include "core/hle/service/sm/sm.h" + +namespace Service::OLSC { + +class OLSC final : public ServiceFramework { +public: + explicit OLSC() : ServiceFramework{"olsc:u"} { + // clang-format off + static const FunctionInfo functions[] = { + {0, &OLSC::Initialize, "Initialize"}, + {10, nullptr, "VerifySaveDataBackupLicenseAsync"}, + {13, nullptr, "GetSaveDataBackupSetting"}, + {14, &OLSC::SetSaveDataBackupSettingEnabled, "SetSaveDataBackupSettingEnabled"}, + {15, nullptr, "SetCustomData"}, + {16, nullptr, "DeleteSaveDataBackupSetting"}, + {18, nullptr, "GetSaveDataBackupInfoCache"}, + {19, nullptr, "UpdateSaveDataBackupInfoCacheAsync"}, + {22, nullptr, "DeleteSaveDataBackupAsync"}, + {25, nullptr, "ListDownloadableSaveDataBackupInfoAsync"}, + {26, nullptr, "DownloadSaveDataBackupAsync"}, + {9010, nullptr, "VerifySaveDataBackupLicenseAsyncForDebug"}, + {9013, nullptr, "GetSaveDataBackupSettingForDebug"}, + {9014, nullptr, "SetSaveDataBackupSettingEnabledForDebug"}, + {9015, nullptr, "SetCustomDataForDebug"}, + {9016, nullptr, "DeleteSaveDataBackupSettingForDebug"}, + {9018, nullptr, "GetSaveDataBackupInfoCacheForDebug"}, + {9019, nullptr, "UpdateSaveDataBackupInfoCacheAsyncForDebug"}, + {9022, nullptr, "DeleteSaveDataBackupAsyncForDebug"}, + {9025, nullptr, "ListDownloadableSaveDataBackupInfoAsyncForDebug"}, + {9026, nullptr, "DownloadSaveDataBackupAsyncForDebug"}, + }; + // clang-format on + + RegisterHandlers(functions); + } + +private: + bool initialized{}; + + void Initialize(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_OLSC, "(STUBBED) called"); + + initialized = true; + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); + } + + void SetSaveDataBackupSettingEnabled(Kernel::HLERequestContext& ctx) { + LOG_WARNING(Service_OLSC, "(STUBBED) called"); + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(RESULT_SUCCESS); + } +}; + +void InstallInterfaces(SM::ServiceManager& service_manager) { + std::make_shared()->InstallAsService(service_manager); +} + +} // namespace Service::OLSC diff --git a/src/core/hle/service/olsc/olsc.h b/src/core/hle/service/olsc/olsc.h new file mode 100644 index 000000000..edee4376b --- /dev/null +++ b/src/core/hle/service/olsc/olsc.h @@ -0,0 +1,16 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +namespace Service::SM { +class ServiceManager; +} + +namespace Service::OLSC { + +/// Registers all SSL services with the specified service manager. +void InstallInterfaces(SM::ServiceManager& service_manager); + +} // namespace Service::OLSC diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp index ba9159ee0..fbfda2d5b 100644 --- a/src/core/hle/service/service.cpp +++ b/src/core/hle/service/service.cpp @@ -51,6 +51,7 @@ #include "core/hle/service/ns/ns.h" #include "core/hle/service/nvdrv/nvdrv.h" #include "core/hle/service/nvflinger/nvflinger.h" +#include "core/hle/service/olsc/olsc.h" #include "core/hle/service/pcie/pcie.h" #include "core/hle/service/pctl/module.h" #include "core/hle/service/pcv/pcv.h" @@ -231,6 +232,7 @@ void Init(std::shared_ptr& sm, Core::System& system) { NPNS::InstallInterfaces(*sm); NS::InstallInterfaces(*sm, system); Nvidia::InstallInterfaces(*sm, *nv_flinger, system); + OLSC::InstallInterfaces(*sm); PCIe::InstallInterfaces(*sm); PCTL::InstallInterfaces(*sm); PCV::InstallInterfaces(*sm); -- cgit v1.2.3 From 3f2e605dd12e91f81dd8debf46e69accab3aa1d0 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Fri, 20 Nov 2020 01:52:37 -0300 Subject: common/bit_cast: Add function matching std::bit_cast without constexpr Add a std::bit_cast-like function archiving the same runtime results as the standard function, without compile time support. This allows us to use bit_cast while we wait for compiler support, it can be trivially replaced in the future. --- src/common/CMakeLists.txt | 1 + src/common/bit_cast.h | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/common/bit_cast.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 207c7a0a6..d20e6c3b5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -102,6 +102,7 @@ add_library(common STATIC atomic_ops.h detached_tasks.cpp detached_tasks.h + bit_cast.h bit_field.h bit_util.h cityhash.cpp diff --git a/src/common/bit_cast.h b/src/common/bit_cast.h new file mode 100644 index 000000000..a32a063d1 --- /dev/null +++ b/src/common/bit_cast.h @@ -0,0 +1,22 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +namespace Common { + +template +[[nodiscard]] std::enable_if_t && + std::is_trivially_copyable_v, + To> +BitCast(const From& src) noexcept { + To dst; + std::memcpy(&dst, &src, sizeof(To)); + return dst; +} + +} // namespace Common -- cgit v1.2.3 From 630823e3630f2bb77a3a5e4aa0f8f87859d05ee1 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 25 Nov 2020 23:03:50 -0300 Subject: common: Add Common::DivCeil and Common::DivCeilLog2 Add an equivalent to 'Common::AlignUp(n, d) / d' and a log2 alternative. --- src/common/CMakeLists.txt | 1 + src/common/div_ceil.h | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/common/div_ceil.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d20e6c3b5..56c7e21f5 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -112,6 +112,7 @@ add_library(common STATIC common_paths.h common_types.h concepts.h + div_ceil.h dynamic_library.cpp dynamic_library.h fiber.cpp diff --git a/src/common/div_ceil.h b/src/common/div_ceil.h new file mode 100644 index 000000000..6b2c48f91 --- /dev/null +++ b/src/common/div_ceil.h @@ -0,0 +1,26 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +namespace Common { + +/// Ceiled integer division. +template +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeil( + N number, D divisor) { + return (static_cast(number) + divisor - 1) / divisor; +} + +/// Ceiled integer division with logarithmic divisor in base 2 +template +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeilLog2( + N value, D alignment_log2) { + return (static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2; +} + +} // namespace Common -- cgit v1.2.3 From c042a89113617f75e81163f103ef82d6d714cd87 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 13 Nov 2020 15:17:47 -0800 Subject: common: fiber: Use boost::context instead of native fibers on Windows. --- src/common/fiber.cpp | 114 +++-------------------------------------- src/common/fiber.h | 9 ---- src/core/hle/kernel/kernel.cpp | 2 +- 3 files changed, 9 insertions(+), 116 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3e3029cd1..3978c8624 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -6,17 +6,16 @@ #include "common/fiber.h" #include "common/spin_lock.h" -#if defined(_WIN32) || defined(WIN32) -#include -#else #include -#endif namespace Common { -constexpr std::size_t default_stack_size = 256 * 1024; // 256kb +constexpr std::size_t default_stack_size = 256 * 1024; struct Fiber::FiberImpl { + alignas(64) std::array stack; + alignas(64) std::array rewind_stack; + SpinLock guard{}; std::function entry_point; std::function rewind_point; @@ -26,17 +25,10 @@ struct Fiber::FiberImpl { bool is_thread_fiber{}; bool released{}; -#if defined(_WIN32) || defined(WIN32) - LPVOID handle = nullptr; - LPVOID rewind_handle = nullptr; -#else - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; - u8* stack_limit; - u8* rewind_stack_limit; - boost::context::detail::fcontext_t context; - boost::context::detail::fcontext_t rewind_context; -#endif + u8* stack_limit{}; + u8* rewind_stack_limit{}; + boost::context::detail::fcontext_t context{}; + boost::context::detail::fcontext_t rewind_context{}; }; void Fiber::SetStartParameter(void* new_parameter) { @@ -48,95 +40,6 @@ void Fiber::SetRewindPoint(std::function&& rewind_func, void* rewin impl->rewind_parameter = rewind_param; } -#if defined(_WIN32) || defined(WIN32) - -void Fiber::Start() { - ASSERT(impl->previous_fiber != nullptr); - impl->previous_fiber->impl->guard.unlock(); - impl->previous_fiber.reset(); - impl->entry_point(impl->start_parameter); - UNREACHABLE(); -} - -void Fiber::OnRewind() { - ASSERT(impl->handle != nullptr); - DeleteFiber(impl->handle); - impl->handle = impl->rewind_handle; - impl->rewind_handle = nullptr; - impl->rewind_point(impl->rewind_parameter); - UNREACHABLE(); -} - -void Fiber::FiberStartFunc(void* fiber_parameter) { - auto* fiber = static_cast(fiber_parameter); - fiber->Start(); -} - -void Fiber::RewindStartFunc(void* fiber_parameter) { - auto* fiber = static_cast(fiber_parameter); - fiber->OnRewind(); -} - -Fiber::Fiber(std::function&& entry_point_func, void* start_parameter) - : impl{std::make_unique()} { - impl->entry_point = std::move(entry_point_func); - impl->start_parameter = start_parameter; - impl->handle = CreateFiber(default_stack_size, &FiberStartFunc, this); -} - -Fiber::Fiber() : impl{std::make_unique()} {} - -Fiber::~Fiber() { - if (impl->released) { - return; - } - // Make sure the Fiber is not being used - const bool locked = impl->guard.try_lock(); - ASSERT_MSG(locked, "Destroying a fiber that's still running"); - if (locked) { - impl->guard.unlock(); - } - DeleteFiber(impl->handle); -} - -void Fiber::Exit() { - ASSERT_MSG(impl->is_thread_fiber, "Exitting non main thread fiber"); - if (!impl->is_thread_fiber) { - return; - } - ConvertFiberToThread(); - impl->guard.unlock(); - impl->released = true; -} - -void Fiber::Rewind() { - ASSERT(impl->rewind_point); - ASSERT(impl->rewind_handle == nullptr); - impl->rewind_handle = CreateFiber(default_stack_size, &RewindStartFunc, this); - SwitchToFiber(impl->rewind_handle); -} - -void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { - ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); - ASSERT_MSG(to != nullptr, "Next fiber is null!"); - to->impl->guard.lock(); - to->impl->previous_fiber = from; - SwitchToFiber(to->impl->handle); - ASSERT(from->impl->previous_fiber != nullptr); - from->impl->previous_fiber->impl->guard.unlock(); - from->impl->previous_fiber.reset(); -} - -std::shared_ptr Fiber::ThreadToFiber() { - std::shared_ptr fiber = std::shared_ptr{new Fiber()}; - fiber->impl->guard.lock(); - fiber->impl->handle = ConvertThreadToFiber(nullptr); - fiber->impl->is_thread_fiber = true; - return fiber; -} - -#else - void Fiber::Start(boost::context::detail::transfer_t& transfer) { ASSERT(impl->previous_fiber != nullptr); impl->previous_fiber->impl->context = transfer.fctx; @@ -229,5 +132,4 @@ std::shared_ptr Fiber::ThreadToFiber() { return fiber; } -#endif } // namespace Common diff --git a/src/common/fiber.h b/src/common/fiber.h index 5323e8579..f7f587f8c 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h @@ -7,11 +7,9 @@ #include #include -#if !defined(_WIN32) && !defined(WIN32) namespace boost::context::detail { struct transfer_t; } -#endif namespace Common { @@ -59,17 +57,10 @@ public: private: Fiber(); -#if defined(_WIN32) || defined(WIN32) - void OnRewind(); - void Start(); - static void FiberStartFunc(void* fiber_parameter); - static void RewindStartFunc(void* fiber_parameter); -#else void OnRewind(boost::context::detail::transfer_t& transfer); void Start(boost::context::detail::transfer_t& transfer); static void FiberStartFunc(boost::context::detail::transfer_t transfer); static void RewindStartFunc(boost::context::detail::transfer_t transfer); -#endif struct FiberImpl; std::unique_ptr impl; diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp index 4cf9cee42..c426b6378 100644 --- a/src/core/hle/kernel/kernel.cpp +++ b/src/core/hle/kernel/kernel.cpp @@ -502,7 +502,7 @@ void KernelCore::InvalidateAllInstructionCaches() { physical_core.ArmInterface().ClearInstructionCache(); } } else { - ASSERT_MSG(false, "UNIMPLEMENTED!!!!!!!!!!!"); + UNIMPLEMENTED(); } } -- cgit v1.2.3 From 24cae76d16d7344154c1a507889e33793b369be7 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 15 Nov 2020 00:36:26 -0800 Subject: common: fiber: Use VirtualBuffer for stack memory. - This will be aligned by default, and helps memory usage. --- src/common/fiber.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3978c8624..3c1eefcb7 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -5,6 +5,7 @@ #include "common/assert.h" #include "common/fiber.h" #include "common/spin_lock.h" +#include "common/virtual_buffer.h" #include @@ -13,8 +14,10 @@ namespace Common { constexpr std::size_t default_stack_size = 256 * 1024; struct Fiber::FiberImpl { - alignas(64) std::array stack; - alignas(64) std::array rewind_stack; + FiberImpl() : stack{default_stack_size}, rewind_stack{default_stack_size} {} + + VirtualBuffer stack; + VirtualBuffer rewind_stack; SpinLock guard{}; std::function entry_point; -- cgit v1.2.3 From 1ea6bdef058a789e2771511f741bffcca73c3525 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 25 Nov 2020 15:21:03 -0500 Subject: audio_core: Make shadowing and unused parameters errors Moves the audio code closer to enabling warnings as errors in general. --- src/audio_core/CMakeLists.txt | 2 ++ src/audio_core/algorithm/filter.cpp | 6 ++-- src/audio_core/algorithm/filter.h | 4 +-- src/audio_core/audio_renderer.cpp | 6 ++-- src/audio_core/audio_renderer.h | 3 +- src/audio_core/buffer.h | 2 +- src/audio_core/command_generator.cpp | 27 +++++++++------- src/audio_core/command_generator.h | 8 ++--- src/audio_core/cubeb_sink.cpp | 15 +++++---- src/audio_core/effect_context.cpp | 60 ++++++++++++++++++------------------ src/audio_core/effect_context.h | 6 ++-- src/audio_core/info_updater.cpp | 32 +++++++++---------- src/audio_core/info_updater.h | 4 +-- src/audio_core/memory_pool.cpp | 13 ++++---- src/audio_core/memory_pool.h | 11 +++---- src/audio_core/sink_context.cpp | 2 +- src/audio_core/sink_context.h | 12 ++++---- src/audio_core/splitter_context.cpp | 22 ++++++------- src/audio_core/splitter_context.h | 20 ++++++------ src/audio_core/stream.cpp | 10 +++--- src/audio_core/stream.h | 4 +-- src/audio_core/voice_context.cpp | 7 +++-- src/audio_core/voice_context.h | 10 +++--- src/common/wall_clock.cpp | 4 +-- src/common/wall_clock.h | 6 ++-- src/common/x64/native_clock.cpp | 8 ++--- src/common/x64/native_clock.h | 3 +- src/core/core_timing.h | 8 ++--- src/core/hle/result.h | 2 +- 29 files changed, 163 insertions(+), 154 deletions(-) (limited to 'src/common') diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index 68c67507b..d1d177b51 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt @@ -51,6 +51,8 @@ if (NOT MSVC) -Werror=implicit-fallthrough -Werror=reorder -Werror=sign-compare + -Werror=shadow + -Werror=unused-parameter -Werror=unused-variable $<$:-Werror=unused-but-set-parameter> diff --git a/src/audio_core/algorithm/filter.cpp b/src/audio_core/algorithm/filter.cpp index f34a5b9f3..01b8dff6b 100644 --- a/src/audio_core/algorithm/filter.cpp +++ b/src/audio_core/algorithm/filter.cpp @@ -31,8 +31,8 @@ Filter Filter::LowPass(double cutoff, double Q) { Filter::Filter() : Filter(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) {} -Filter::Filter(double a0, double a1, double a2, double b0, double b1, double b2) - : a1(a1 / a0), a2(a2 / a0), b0(b0 / a0), b1(b1 / a0), b2(b2 / a0) {} +Filter::Filter(double a0_, double a1_, double a2_, double b0_, double b1_, double b2_) + : a1(a1_ / a0_), a2(a2_ / a0_), b0(b0_ / a0_), b1(b1_ / a0_), b2(b2_ / a0_) {} void Filter::Process(std::vector& signal) { const std::size_t num_frames = signal.size() / 2; @@ -69,7 +69,7 @@ CascadingFilter CascadingFilter::LowPass(double cutoff, std::size_t cascade_size } CascadingFilter::CascadingFilter() = default; -CascadingFilter::CascadingFilter(std::vector filters) : filters(std::move(filters)) {} +CascadingFilter::CascadingFilter(std::vector filters_) : filters(std::move(filters_)) {} void CascadingFilter::Process(std::vector& signal) { for (auto& filter : filters) { diff --git a/src/audio_core/algorithm/filter.h b/src/audio_core/algorithm/filter.h index 3546d149b..a291fe79b 100644 --- a/src/audio_core/algorithm/filter.h +++ b/src/audio_core/algorithm/filter.h @@ -25,7 +25,7 @@ public: /// Passthrough filter. Filter(); - Filter(double a0, double a1, double a2, double b0, double b1, double b2); + Filter(double a0_, double a1_, double a2_, double b0_, double b1_, double b2_); void Process(std::vector& signal); @@ -51,7 +51,7 @@ public: /// Passthrough. CascadingFilter(); - explicit CascadingFilter(std::vector filters); + explicit CascadingFilter(std::vector filters_); void Process(std::vector& signal); diff --git a/src/audio_core/audio_renderer.cpp b/src/audio_core/audio_renderer.cpp index e1ded84e0..75ab9ea0b 100644 --- a/src/audio_core/audio_renderer.cpp +++ b/src/audio_core/audio_renderer.cpp @@ -71,9 +71,9 @@ namespace { namespace AudioCore { AudioRenderer::AudioRenderer(Core::Timing::CoreTiming& core_timing, Core::Memory::Memory& memory_, AudioCommon::AudioRendererParameter params, - std::shared_ptr buffer_event, + std::shared_ptr buffer_event_, std::size_t instance_number) - : worker_params{params}, buffer_event{buffer_event}, + : worker_params{params}, buffer_event{buffer_event_}, memory_pool_info(params.effect_count + params.voice_count * 4), voice_context(params.voice_count), effect_context(params.effect_count), mix_context(), sink_context(params.sink_count), splitter_context(), @@ -89,7 +89,7 @@ AudioRenderer::AudioRenderer(Core::Timing::CoreTiming& core_timing, Core::Memory stream = audio_out->OpenStream(core_timing, params.sample_rate, AudioCommon::STREAM_NUM_CHANNELS, fmt::format("AudioRenderer-Instance{}", instance_number), - [=]() { buffer_event->Signal(); }); + [=]() { buffer_event_->Signal(); }); audio_out->StartStream(stream); QueueMixedBuffer(0); diff --git a/src/audio_core/audio_renderer.h b/src/audio_core/audio_renderer.h index a85219045..1f96013f7 100644 --- a/src/audio_core/audio_renderer.h +++ b/src/audio_core/audio_renderer.h @@ -44,7 +44,8 @@ class AudioRenderer { public: AudioRenderer(Core::Timing::CoreTiming& core_timing, Core::Memory::Memory& memory_, AudioCommon::AudioRendererParameter params, - std::shared_ptr buffer_event, std::size_t instance_number); + std::shared_ptr buffer_event_, + std::size_t instance_number); ~AudioRenderer(); [[nodiscard]] ResultCode UpdateAudioRenderer(const std::vector& input_params, diff --git a/src/audio_core/buffer.h b/src/audio_core/buffer.h index 5ee09e9aa..ccc46ef82 100644 --- a/src/audio_core/buffer.h +++ b/src/audio_core/buffer.h @@ -18,7 +18,7 @@ class Buffer { public: using Tag = u64; - Buffer(Tag tag, std::vector&& samples) : tag{tag}, samples{std::move(samples)} {} + Buffer(Tag tag_, std::vector&& samples_) : tag{tag_}, samples{std::move(samples_)} {} /// Returns the raw audio data for the buffer std::vector& GetSamples() { diff --git a/src/audio_core/command_generator.cpp b/src/audio_core/command_generator.cpp index fb8700ccf..a4a9a757d 100644 --- a/src/audio_core/command_generator.cpp +++ b/src/audio_core/command_generator.cpp @@ -67,12 +67,12 @@ s32 ApplyMixDepop(s32* output, s32 first_sample, s32 delta, s32 sample_count) { } // namespace -CommandGenerator::CommandGenerator(AudioCommon::AudioRendererParameter& worker_params, - VoiceContext& voice_context, MixContext& mix_context, - SplitterContext& splitter_context, EffectContext& effect_context, - Core::Memory::Memory& memory) - : worker_params(worker_params), voice_context(voice_context), mix_context(mix_context), - splitter_context(splitter_context), effect_context(effect_context), memory(memory), +CommandGenerator::CommandGenerator(AudioCommon::AudioRendererParameter& worker_params_, + VoiceContext& voice_context_, MixContext& mix_context_, + SplitterContext& splitter_context_, + EffectContext& effect_context_, Core::Memory::Memory& memory_) + : worker_params(worker_params_), voice_context(voice_context_), mix_context(mix_context_), + splitter_context(splitter_context_), effect_context(effect_context_), memory(memory_), mix_buffer((worker_params.mix_buffer_count + AudioCommon::MAX_CHANNEL_COUNT) * worker_params.sample_count), sample_buffer(MIX_BUFFER_SIZE), @@ -255,7 +255,8 @@ void CommandGenerator::GenerateDataSourceCommand(ServerVoiceInfo& voice_info, Vo void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - s32 mix_buffer_count, s32 channel) { + [[maybe_unused]] s32 mix_buffer_count, + [[maybe_unused]] s32 channel) { for (std::size_t i = 0; i < AudioCommon::MAX_BIQUAD_FILTERS; i++) { const auto& in_params = voice_info.GetInParams(); auto& biquad_filter = in_params.biquad_filter[i]; @@ -278,9 +279,12 @@ void CommandGenerator::GenerateBiquadFilterCommandForVoice(ServerVoiceInfo& voic } } -void AudioCore::CommandGenerator::GenerateBiquadFilterCommand( - s32 mix_buffer, const BiquadFilterParameter& params, std::array& state, - std::size_t input_offset, std::size_t output_offset, s32 sample_count, s32 node_id) { +void CommandGenerator::GenerateBiquadFilterCommand([[maybe_unused]] s32 mix_buffer_id, + const BiquadFilterParameter& params, + std::array& state, + std::size_t input_offset, + std::size_t output_offset, s32 sample_count, + s32 node_id) { if (dumping_frame) { LOG_DEBUG(Audio, "(DSP_TRACE) GenerateBiquadFilterCommand node_id={}, " @@ -714,7 +718,8 @@ s32 CommandGenerator::DecodePcm16(ServerVoiceInfo& voice_info, VoiceState& dsp_s } s32 CommandGenerator::DecodeAdpcm(ServerVoiceInfo& voice_info, VoiceState& dsp_state, - s32 sample_count, s32 channel, std::size_t mix_offset) { + s32 sample_count, [[maybe_unused]] s32 channel, + std::size_t mix_offset) { const auto& in_params = voice_info.GetInParams(); const auto& wave_buffer = in_params.wave_buffer[dsp_state.wave_buffer_index]; if (wave_buffer.buffer_address == 0) { diff --git a/src/audio_core/command_generator.h b/src/audio_core/command_generator.h index 87ece00c4..b937350b1 100644 --- a/src/audio_core/command_generator.h +++ b/src/audio_core/command_generator.h @@ -25,10 +25,10 @@ using MixVolumeBuffer = std::array; class CommandGenerator { public: - explicit CommandGenerator(AudioCommon::AudioRendererParameter& worker_params, - VoiceContext& voice_context, MixContext& mix_context, - SplitterContext& splitter_context, EffectContext& effect_context, - Core::Memory::Memory& memory); + explicit CommandGenerator(AudioCommon::AudioRendererParameter& worker_params_, + VoiceContext& voice_context_, MixContext& mix_context_, + SplitterContext& splitter_context_, EffectContext& effect_context_, + Core::Memory::Memory& memory_); ~CommandGenerator(); void ClearMixBuffers(); diff --git a/src/audio_core/cubeb_sink.cpp b/src/audio_core/cubeb_sink.cpp index 6eaa60815..cf7b186e4 100644 --- a/src/audio_core/cubeb_sink.cpp +++ b/src/audio_core/cubeb_sink.cpp @@ -21,10 +21,10 @@ namespace AudioCore { class CubebSinkStream final : public SinkStream { public: - CubebSinkStream(cubeb* ctx, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, + CubebSinkStream(cubeb* ctx_, u32 sample_rate, u32 num_channels_, cubeb_devid output_device, const std::string& name) - : ctx{ctx}, num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate, - num_channels} { + : ctx{ctx_}, num_channels{std::min(num_channels_, 6u)}, time_stretch{sample_rate, + num_channels} { cubeb_stream_params params{}; params.rate = sample_rate; @@ -192,8 +192,9 @@ SinkStream& CubebSink::AcquireSinkStream(u32 sample_rate, u32 num_channels, return *sink_streams.back(); } -long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const void* input_buffer, - void* output_buffer, long num_frames) { +long CubebSinkStream::DataCallback([[maybe_unused]] cubeb_stream* stream, void* user_data, + [[maybe_unused]] const void* input_buffer, void* output_buffer, + long num_frames) { auto* impl = static_cast(user_data); auto* buffer = static_cast(output_buffer); @@ -236,7 +237,9 @@ long CubebSinkStream::DataCallback(cubeb_stream* stream, void* user_data, const return num_frames; } -void CubebSinkStream::StateCallback(cubeb_stream* stream, void* user_data, cubeb_state state) {} +void CubebSinkStream::StateCallback([[maybe_unused]] cubeb_stream* stream, + [[maybe_unused]] void* user_data, + [[maybe_unused]] cubeb_state state) {} std::vector ListCubebSinkDevices() { std::vector device_list; diff --git a/src/audio_core/effect_context.cpp b/src/audio_core/effect_context.cpp index 4d9cdf524..f770b9608 100644 --- a/src/audio_core/effect_context.cpp +++ b/src/audio_core/effect_context.cpp @@ -12,7 +12,7 @@ bool ValidChannelCountForEffect(s32 channel_count) { } } // namespace -EffectContext::EffectContext(std::size_t effect_count) : effect_count(effect_count) { +EffectContext::EffectContext(std::size_t effect_count_) : effect_count(effect_count_) { effects.reserve(effect_count); std::generate_n(std::back_inserter(effects), effect_count, [] { return std::make_unique(); }); @@ -61,13 +61,13 @@ const EffectBase* EffectContext::GetInfo(std::size_t i) const { return effects.at(i).get(); } -EffectStubbed::EffectStubbed() : EffectBase::EffectBase(EffectType::Invalid) {} +EffectStubbed::EffectStubbed() : EffectBase(EffectType::Invalid) {} EffectStubbed::~EffectStubbed() = default; -void EffectStubbed::Update(EffectInfo::InParams& in_params) {} +void EffectStubbed::Update([[maybe_unused]] EffectInfo::InParams& in_params) {} void EffectStubbed::UpdateForCommandGeneration() {} -EffectBase::EffectBase(EffectType effect_type) : effect_type(effect_type) {} +EffectBase::EffectBase(EffectType effect_type_) : effect_type(effect_type_) {} EffectBase::~EffectBase() = default; UsageState EffectBase::GetUsage() const { @@ -90,32 +90,32 @@ s32 EffectBase::GetProcessingOrder() const { return processing_order; } -EffectI3dl2Reverb::EffectI3dl2Reverb() : EffectGeneric::EffectGeneric(EffectType::I3dl2Reverb) {} +EffectI3dl2Reverb::EffectI3dl2Reverb() : EffectGeneric(EffectType::I3dl2Reverb) {} EffectI3dl2Reverb::~EffectI3dl2Reverb() = default; void EffectI3dl2Reverb::Update(EffectInfo::InParams& in_params) { - auto& internal_params = GetParams(); + auto& params = GetParams(); const auto* reverb_params = reinterpret_cast(in_params.raw.data()); if (!ValidChannelCountForEffect(reverb_params->max_channels)) { UNREACHABLE_MSG("Invalid reverb max channel count {}", reverb_params->max_channels); return; } - const auto last_status = internal_params.status; + const auto last_status = params.status; mix_id = in_params.mix_id; processing_order = in_params.processing_order; - internal_params = *reverb_params; + params = *reverb_params; if (!ValidChannelCountForEffect(reverb_params->channel_count)) { - internal_params.channel_count = internal_params.max_channels; + params.channel_count = params.max_channels; } enabled = in_params.is_enabled; if (last_status != ParameterStatus::Updated) { - internal_params.status = last_status; + params.status = last_status; } if (in_params.is_new || skipped) { usage = UsageState::Initialized; - internal_params.status = ParameterStatus::Initialized; + params.status = ParameterStatus::Initialized; skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; } } @@ -129,15 +129,15 @@ void EffectI3dl2Reverb::UpdateForCommandGeneration() { GetParams().status = ParameterStatus::Updated; } -EffectBiquadFilter::EffectBiquadFilter() : EffectGeneric::EffectGeneric(EffectType::BiquadFilter) {} +EffectBiquadFilter::EffectBiquadFilter() : EffectGeneric(EffectType::BiquadFilter) {} EffectBiquadFilter::~EffectBiquadFilter() = default; void EffectBiquadFilter::Update(EffectInfo::InParams& in_params) { - auto& internal_params = GetParams(); + auto& params = GetParams(); const auto* biquad_params = reinterpret_cast(in_params.raw.data()); mix_id = in_params.mix_id; processing_order = in_params.processing_order; - internal_params = *biquad_params; + params = *biquad_params; enabled = in_params.is_enabled; } @@ -150,7 +150,7 @@ void EffectBiquadFilter::UpdateForCommandGeneration() { GetParams().status = ParameterStatus::Updated; } -EffectAuxInfo::EffectAuxInfo() : EffectGeneric::EffectGeneric(EffectType::Aux) {} +EffectAuxInfo::EffectAuxInfo() : EffectGeneric(EffectType::Aux) {} EffectAuxInfo::~EffectAuxInfo() = default; void EffectAuxInfo::Update(EffectInfo::InParams& in_params) { @@ -200,32 +200,32 @@ VAddr EffectAuxInfo::GetRecvBuffer() const { return recv_buffer; } -EffectDelay::EffectDelay() : EffectGeneric::EffectGeneric(EffectType::Delay) {} +EffectDelay::EffectDelay() : EffectGeneric(EffectType::Delay) {} EffectDelay::~EffectDelay() = default; void EffectDelay::Update(EffectInfo::InParams& in_params) { const auto* delay_params = reinterpret_cast(in_params.raw.data()); - auto& internal_params = GetParams(); + auto& params = GetParams(); if (!ValidChannelCountForEffect(delay_params->max_channels)) { return; } - const auto last_status = internal_params.status; + const auto last_status = params.status; mix_id = in_params.mix_id; processing_order = in_params.processing_order; - internal_params = *delay_params; + params = *delay_params; if (!ValidChannelCountForEffect(delay_params->channels)) { - internal_params.channels = internal_params.max_channels; + params.channels = params.max_channels; } enabled = in_params.is_enabled; if (last_status != ParameterStatus::Updated) { - internal_params.status = last_status; + params.status = last_status; } if (in_params.is_new || skipped) { usage = UsageState::Initialized; - internal_params.status = ParameterStatus::Initialized; + params.status = ParameterStatus::Initialized; skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; } } @@ -239,7 +239,7 @@ void EffectDelay::UpdateForCommandGeneration() { GetParams().status = ParameterStatus::Updated; } -EffectBufferMixer::EffectBufferMixer() : EffectGeneric::EffectGeneric(EffectType::BufferMixer) {} +EffectBufferMixer::EffectBufferMixer() : EffectGeneric(EffectType::BufferMixer) {} EffectBufferMixer::~EffectBufferMixer() = default; void EffectBufferMixer::Update(EffectInfo::InParams& in_params) { @@ -257,32 +257,32 @@ void EffectBufferMixer::UpdateForCommandGeneration() { } } -EffectReverb::EffectReverb() : EffectGeneric::EffectGeneric(EffectType::Reverb) {} +EffectReverb::EffectReverb() : EffectGeneric(EffectType::Reverb) {} EffectReverb::~EffectReverb() = default; void EffectReverb::Update(EffectInfo::InParams& in_params) { const auto* reverb_params = reinterpret_cast(in_params.raw.data()); - auto& internal_params = GetParams(); + auto& params = GetParams(); if (!ValidChannelCountForEffect(reverb_params->max_channels)) { return; } - const auto last_status = internal_params.status; + const auto last_status = params.status; mix_id = in_params.mix_id; processing_order = in_params.processing_order; - internal_params = *reverb_params; + params = *reverb_params; if (!ValidChannelCountForEffect(reverb_params->channels)) { - internal_params.channels = internal_params.max_channels; + params.channels = params.max_channels; } enabled = in_params.is_enabled; if (last_status != ParameterStatus::Updated) { - internal_params.status = last_status; + params.status = last_status; } if (in_params.is_new || skipped) { usage = UsageState::Initialized; - internal_params.status = ParameterStatus::Initialized; + params.status = ParameterStatus::Initialized; skipped = in_params.buffer_address == 0 || in_params.buffer_size == 0; } } diff --git a/src/audio_core/effect_context.h b/src/audio_core/effect_context.h index 03c5a0f04..c5e0b398c 100644 --- a/src/audio_core/effect_context.h +++ b/src/audio_core/effect_context.h @@ -184,7 +184,7 @@ struct AuxAddress { class EffectBase { public: - explicit EffectBase(EffectType effect_type); + explicit EffectBase(EffectType effect_type_); virtual ~EffectBase(); virtual void Update(EffectInfo::InParams& in_params) = 0; @@ -206,7 +206,7 @@ protected: template class EffectGeneric : public EffectBase { public: - explicit EffectGeneric(EffectType effect_type) : EffectBase(effect_type) {} + explicit EffectGeneric(EffectType effect_type_) : EffectBase(effect_type_) {} T& GetParams() { return internal_params; @@ -306,7 +306,7 @@ private: class EffectContext { public: - explicit EffectContext(std::size_t effect_count); + explicit EffectContext(std::size_t effect_count_); ~EffectContext(); [[nodiscard]] std::size_t GetCount() const; diff --git a/src/audio_core/info_updater.cpp b/src/audio_core/info_updater.cpp index 2940e53a9..d3ac90827 100644 --- a/src/audio_core/info_updater.cpp +++ b/src/audio_core/info_updater.cpp @@ -14,9 +14,9 @@ namespace AudioCore { -InfoUpdater::InfoUpdater(const std::vector& in_params, std::vector& out_params, - BehaviorInfo& behavior_info) - : in_params(in_params), out_params(out_params), behavior_info(behavior_info) { +InfoUpdater::InfoUpdater(const std::vector& in_params_, std::vector& out_params_, + BehaviorInfo& behavior_info_) + : in_params(in_params_), out_params(out_params_), behavior_info(behavior_info_) { ASSERT( AudioCommon::CanConsumeBuffer(in_params.size(), 0, sizeof(AudioCommon::UpdateDataHeader))); std::memcpy(&input_header, in_params.data(), sizeof(AudioCommon::UpdateDataHeader)); @@ -135,8 +135,8 @@ bool InfoUpdater::UpdateVoiceChannelResources(VoiceContext& voice_context) { } bool InfoUpdater::UpdateVoices(VoiceContext& voice_context, - std::vector& memory_pool_info, - VAddr audio_codec_dsp_addr) { + [[maybe_unused]] std::vector& memory_pool_info, + [[maybe_unused]] VAddr audio_codec_dsp_addr) { const auto voice_count = voice_context.GetVoiceCount(); std::vector voice_in(voice_count); std::vector voice_out(voice_count); @@ -165,28 +165,28 @@ bool InfoUpdater::UpdateVoices(VoiceContext& voice_context, // Update our voices for (std::size_t i = 0; i < voice_count; i++) { - auto& in_params = voice_in[i]; - const auto channel_count = static_cast(in_params.channel_count); + auto& voice_in_params = voice_in[i]; + const auto channel_count = static_cast(voice_in_params.channel_count); // Skip if it's not currently in use - if (!in_params.is_in_use) { + if (!voice_in_params.is_in_use) { continue; } // Voice states for each channel std::array voice_states{}; - ASSERT(static_cast(in_params.id) < voice_count); + ASSERT(static_cast(voice_in_params.id) < voice_count); // Grab our current voice info - auto& voice_info = voice_context.GetInfo(static_cast(in_params.id)); + auto& voice_info = voice_context.GetInfo(static_cast(voice_in_params.id)); ASSERT(channel_count <= AudioCommon::MAX_CHANNEL_COUNT); // Get all our channel voice states for (std::size_t channel = 0; channel < channel_count; channel++) { voice_states[channel] = - &voice_context.GetState(in_params.voice_channel_resource_ids[channel]); + &voice_context.GetState(voice_in_params.voice_channel_resource_ids[channel]); } - if (in_params.is_new) { + if (voice_in_params.is_new) { // Default our values for our voice voice_info.Initialize(); if (channel_count == 0 || channel_count > AudioCommon::MAX_CHANNEL_COUNT) { @@ -200,12 +200,12 @@ bool InfoUpdater::UpdateVoices(VoiceContext& voice_context, } // Update our voice - voice_info.UpdateParameters(in_params, behavior_info); + voice_info.UpdateParameters(voice_in_params, behavior_info); // TODO(ogniK): Handle mapping errors with behavior info based on in params response // Update our wave buffers - voice_info.UpdateWaveBuffers(in_params, voice_states, behavior_info); - voice_info.WriteOutStatus(voice_out[i], in_params, voice_states); + voice_info.UpdateWaveBuffers(voice_in_params, voice_states, behavior_info); + voice_info.WriteOutStatus(voice_out[i], voice_in_params, voice_states); } if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, voice_out_size)) { @@ -445,7 +445,7 @@ bool InfoUpdater::UpdatePerformanceBuffer() { return true; } -bool InfoUpdater::UpdateErrorInfo(BehaviorInfo& in_behavior_info) { +bool InfoUpdater::UpdateErrorInfo([[maybe_unused]] BehaviorInfo& in_behavior_info) { const auto total_beahvior_info_out = sizeof(BehaviorInfo::OutParams); if (!AudioCommon::CanConsumeBuffer(out_params.size(), output_offset, total_beahvior_info_out)) { diff --git a/src/audio_core/info_updater.h b/src/audio_core/info_updater.h index 06f9d770f..d315c91ed 100644 --- a/src/audio_core/info_updater.h +++ b/src/audio_core/info_updater.h @@ -21,8 +21,8 @@ class SplitterContext; class InfoUpdater { public: // TODO(ogniK): Pass process handle when we support it - InfoUpdater(const std::vector& in_params, std::vector& out_params, - BehaviorInfo& behavior_info); + InfoUpdater(const std::vector& in_params_, std::vector& out_params_, + BehaviorInfo& behavior_info_); ~InfoUpdater(); bool UpdateBehaviorInfo(BehaviorInfo& in_behavior_info); diff --git a/src/audio_core/memory_pool.cpp b/src/audio_core/memory_pool.cpp index 5a3453063..6b6908d26 100644 --- a/src/audio_core/memory_pool.cpp +++ b/src/audio_core/memory_pool.cpp @@ -10,11 +10,10 @@ namespace AudioCore { ServerMemoryPoolInfo::ServerMemoryPoolInfo() = default; ServerMemoryPoolInfo::~ServerMemoryPoolInfo() = default; -bool ServerMemoryPoolInfo::Update(const ServerMemoryPoolInfo::InParams& in_params, - ServerMemoryPoolInfo::OutParams& out_params) { + +bool ServerMemoryPoolInfo::Update(const InParams& in_params, OutParams& out_params) { // Our state does not need to be changed - if (in_params.state != ServerMemoryPoolInfo::State::RequestAttach && - in_params.state != ServerMemoryPoolInfo::State::RequestDetach) { + if (in_params.state != State::RequestAttach && in_params.state != State::RequestDetach) { return true; } @@ -32,11 +31,11 @@ bool ServerMemoryPoolInfo::Update(const ServerMemoryPoolInfo::InParams& in_param return false; } - if (in_params.state == ServerMemoryPoolInfo::State::RequestAttach) { + if (in_params.state == State::RequestAttach) { cpu_address = in_params.address; size = in_params.size; used = true; - out_params.state = ServerMemoryPoolInfo::State::Attached; + out_params.state = State::Attached; } else { // Unexpected address if (cpu_address != in_params.address) { @@ -54,7 +53,7 @@ bool ServerMemoryPoolInfo::Update(const ServerMemoryPoolInfo::InParams& in_param cpu_address = 0; size = 0; used = false; - out_params.state = ServerMemoryPoolInfo::State::Detached; + out_params.state = State::Detached; } return true; } diff --git a/src/audio_core/memory_pool.h b/src/audio_core/memory_pool.h index 8ac503f1c..3e9e777ae 100644 --- a/src/audio_core/memory_pool.h +++ b/src/audio_core/memory_pool.h @@ -28,19 +28,18 @@ public: struct InParams { u64_le address{}; u64_le size{}; - ServerMemoryPoolInfo::State state{}; + State state{}; INSERT_PADDING_WORDS(3); }; - static_assert(sizeof(ServerMemoryPoolInfo::InParams) == 0x20, "InParams are an invalid size"); + static_assert(sizeof(InParams) == 0x20, "InParams are an invalid size"); struct OutParams { - ServerMemoryPoolInfo::State state{}; + State state{}; INSERT_PADDING_WORDS(3); }; - static_assert(sizeof(ServerMemoryPoolInfo::OutParams) == 0x10, "OutParams are an invalid size"); + static_assert(sizeof(OutParams) == 0x10, "OutParams are an invalid size"); - bool Update(const ServerMemoryPoolInfo::InParams& in_params, - ServerMemoryPoolInfo::OutParams& out_params); + bool Update(const InParams& in_params, OutParams& out_params); private: // There's another entry here which is the DSP address, however since we're not talking to the diff --git a/src/audio_core/sink_context.cpp b/src/audio_core/sink_context.cpp index b29b47890..a69543696 100644 --- a/src/audio_core/sink_context.cpp +++ b/src/audio_core/sink_context.cpp @@ -5,7 +5,7 @@ #include "audio_core/sink_context.h" namespace AudioCore { -SinkContext::SinkContext(std::size_t sink_count) : sink_count(sink_count) {} +SinkContext::SinkContext(std::size_t sink_count_) : sink_count{sink_count_} {} SinkContext::~SinkContext() = default; std::size_t SinkContext::GetCount() const { diff --git a/src/audio_core/sink_context.h b/src/audio_core/sink_context.h index e2e7880b7..05541becb 100644 --- a/src/audio_core/sink_context.h +++ b/src/audio_core/sink_context.h @@ -42,7 +42,7 @@ public: bool in_use; INSERT_UNION_PADDING_BYTES(5); }; - static_assert(sizeof(SinkInfo::CircularBufferIn) == 0x28, + static_assert(sizeof(CircularBufferIn) == 0x28, "SinkInfo::CircularBufferIn is in invalid size"); struct DeviceIn { @@ -54,7 +54,7 @@ public: bool down_matrix_enabled; DownmixCoefficients down_matrix_coef; }; - static_assert(sizeof(SinkInfo::DeviceIn) == 0x11c, "SinkInfo::DeviceIn is an invalid size"); + static_assert(sizeof(DeviceIn) == 0x11c, "SinkInfo::DeviceIn is an invalid size"); struct InParams { SinkTypes type{}; @@ -64,16 +64,16 @@ public: INSERT_PADDING_WORDS(6); union { // std::array raw{}; - SinkInfo::DeviceIn device; - SinkInfo::CircularBufferIn circular_buffer; + DeviceIn device; + CircularBufferIn circular_buffer; }; }; - static_assert(sizeof(SinkInfo::InParams) == 0x140, "SinkInfo::InParams are an invalid size!"); + static_assert(sizeof(InParams) == 0x140, "SinkInfo::InParams are an invalid size!"); }; class SinkContext { public: - explicit SinkContext(std::size_t sink_count); + explicit SinkContext(std::size_t sink_count_); ~SinkContext(); [[nodiscard]] std::size_t GetCount() const; diff --git a/src/audio_core/splitter_context.cpp b/src/audio_core/splitter_context.cpp index f21b53147..f4bcd0391 100644 --- a/src/audio_core/splitter_context.cpp +++ b/src/audio_core/splitter_context.cpp @@ -10,7 +10,7 @@ namespace AudioCore { -ServerSplitterDestinationData::ServerSplitterDestinationData(s32 id) : id(id) {} +ServerSplitterDestinationData::ServerSplitterDestinationData(s32 id_) : id{id_} {} ServerSplitterDestinationData::~ServerSplitterDestinationData() = default; void ServerSplitterDestinationData::Update(SplitterInfo::InDestinationParams& header) { @@ -87,7 +87,7 @@ void ServerSplitterDestinationData::UpdateInternalState() { needs_update = false; } -ServerSplitterInfo::ServerSplitterInfo(s32 id) : id(id) {} +ServerSplitterInfo::ServerSplitterInfo(s32 id_) : id(id_) {} ServerSplitterInfo::~ServerSplitterInfo() = default; void ServerSplitterInfo::InitializeInfos() { @@ -121,7 +121,7 @@ const ServerSplitterDestinationData* ServerSplitterInfo::GetHead() const { } ServerSplitterDestinationData* ServerSplitterInfo::GetData(std::size_t depth) { - auto current_head = head; + auto* current_head = head; for (std::size_t i = 0; i < depth; i++) { if (current_head == nullptr) { return nullptr; @@ -132,7 +132,7 @@ ServerSplitterDestinationData* ServerSplitterInfo::GetData(std::size_t depth) { } const ServerSplitterDestinationData* ServerSplitterInfo::GetData(std::size_t depth) const { - auto current_head = head; + auto* current_head = head; for (std::size_t i = 0; i < depth; i++) { if (current_head == nullptr) { return nullptr; @@ -245,7 +245,7 @@ ServerSplitterDestinationData* SplitterContext::GetDestinationData(std::size_t i const ServerSplitterDestinationData* SplitterContext::GetDestinationData(std::size_t info, std::size_t data) const { ASSERT(info < info_count); - auto& cur_info = GetInfo(info); + const auto& cur_info = GetInfo(info); return cur_info.GetData(data); } @@ -267,11 +267,11 @@ std::size_t SplitterContext::GetDataCount() const { return data_count; } -void SplitterContext::Setup(std::size_t _info_count, std::size_t _data_count, +void SplitterContext::Setup(std::size_t info_count_, std::size_t data_count_, bool is_splitter_bug_fixed) { - info_count = _info_count; - data_count = _data_count; + info_count = info_count_; + data_count = data_count_; for (std::size_t i = 0; i < info_count; i++) { auto& splitter = infos.emplace_back(static_cast(i)); @@ -364,7 +364,7 @@ bool SplitterContext::RecomposeDestination(ServerSplitterInfo& info, // Clear our current destinations auto* current_head = info.GetHead(); while (current_head != nullptr) { - auto next_head = current_head->GetNextDestination(); + auto* next_head = current_head->GetNextDestination(); current_head->SetNextDestination(nullptr); current_head = next_head; } @@ -471,8 +471,8 @@ bool NodeStates::DepthFirstSearch(EdgeMatrix& edge_matrix) { continue; } - const auto node_count = edge_matrix.GetNodeCount(); - for (s32 j = 0; j < static_cast(node_count); j++) { + const auto edge_node_count = edge_matrix.GetNodeCount(); + for (s32 j = 0; j < static_cast(edge_node_count); j++) { // Check if our node is connected to our edge matrix if (!edge_matrix.Connected(current_stack_index, j)) { continue; diff --git a/src/audio_core/splitter_context.h b/src/audio_core/splitter_context.h index ea6239fdb..b490627f5 100644 --- a/src/audio_core/splitter_context.h +++ b/src/audio_core/splitter_context.h @@ -63,7 +63,7 @@ public: NodeStates(); ~NodeStates(); - void Initialize(std::size_t _node_count); + void Initialize(std::size_t node_count_); bool Tsort(EdgeMatrix& edge_matrix); std::size_t GetIndexPos() const; const std::vector& GetIndexList() const; @@ -72,15 +72,15 @@ private: void PushTsortResult(s32 index); bool DepthFirstSearch(EdgeMatrix& edge_matrix); void ResetState(); - void UpdateState(NodeStates::State state, std::size_t i); - NodeStates::State GetState(std::size_t i); + void UpdateState(State state, std::size_t i); + State GetState(std::size_t i); std::size_t node_count{}; std::vector was_node_found{}; std::vector was_node_completed{}; std::size_t index_pos{}; std::vector index_list{}; - NodeStates::Stack index_stack{}; + Stack index_stack{}; }; enum class SplitterMagic : u32_le { @@ -97,8 +97,7 @@ public: s32_le data_count{}; INSERT_PADDING_WORDS(5); }; - static_assert(sizeof(SplitterInfo::InHeader) == 0x20, - "SplitterInfo::InHeader is an invalid size"); + static_assert(sizeof(InHeader) == 0x20, "SplitterInfo::InHeader is an invalid size"); struct InInfoPrams { SplitterMagic magic{}; @@ -107,8 +106,7 @@ public: s32_le length{}; s32_le resource_id_base{}; }; - static_assert(sizeof(SplitterInfo::InInfoPrams) == 0x14, - "SplitterInfo::InInfoPrams is an invalid size"); + static_assert(sizeof(InInfoPrams) == 0x14, "SplitterInfo::InInfoPrams is an invalid size"); struct InDestinationParams { SplitterMagic magic{}; @@ -118,13 +116,13 @@ public: bool in_use{}; INSERT_PADDING_BYTES(3); }; - static_assert(sizeof(SplitterInfo::InDestinationParams) == 0x70, + static_assert(sizeof(InDestinationParams) == 0x70, "SplitterInfo::InDestinationParams is an invalid size"); }; class ServerSplitterDestinationData { public: - explicit ServerSplitterDestinationData(s32 id); + explicit ServerSplitterDestinationData(s32 id_); ~ServerSplitterDestinationData(); void Update(SplitterInfo::InDestinationParams& header); @@ -153,7 +151,7 @@ private: class ServerSplitterInfo { public: - explicit ServerSplitterInfo(s32 id); + explicit ServerSplitterInfo(s32 id_); ~ServerSplitterInfo(); void InitializeInfos(); diff --git a/src/audio_core/stream.cpp b/src/audio_core/stream.cpp index 41bc2f4d6..eca296589 100644 --- a/src/audio_core/stream.cpp +++ b/src/audio_core/stream.cpp @@ -31,10 +31,10 @@ u32 Stream::GetNumChannels() const { return {}; } -Stream::Stream(Core::Timing::CoreTiming& core_timing, u32 sample_rate, Format format, - ReleaseCallback&& release_callback, SinkStream& sink_stream, std::string&& name_) - : sample_rate{sample_rate}, format{format}, release_callback{std::move(release_callback)}, - sink_stream{sink_stream}, core_timing{core_timing}, name{std::move(name_)} { +Stream::Stream(Core::Timing::CoreTiming& core_timing_, u32 sample_rate_, Format format_, + ReleaseCallback&& release_callback_, SinkStream& sink_stream_, std::string&& name_) + : sample_rate{sample_rate_}, format{format_}, release_callback{std::move(release_callback_)}, + sink_stream{sink_stream_}, core_timing{core_timing_}, name{std::move(name_)} { release_event = Core::Timing::CreateEvent(name, [this](std::uintptr_t, std::chrono::nanoseconds ns_late) { ReleaseActiveBuffer(ns_late); @@ -122,7 +122,7 @@ bool Stream::QueueBuffer(BufferPtr&& buffer) { return false; } -bool Stream::ContainsBuffer(Buffer::Tag tag) const { +bool Stream::ContainsBuffer([[maybe_unused]] Buffer::Tag tag) const { UNIMPLEMENTED(); return {}; } diff --git a/src/audio_core/stream.h b/src/audio_core/stream.h index 71c2d0b4f..506ac536b 100644 --- a/src/audio_core/stream.h +++ b/src/audio_core/stream.h @@ -44,8 +44,8 @@ public: /// Callback function type, used to change guest state on a buffer being released using ReleaseCallback = std::function; - Stream(Core::Timing::CoreTiming& core_timing, u32 sample_rate, Format format, - ReleaseCallback&& release_callback, SinkStream& sink_stream, std::string&& name_); + Stream(Core::Timing::CoreTiming& core_timing_, u32 sample_rate_, Format format_, + ReleaseCallback&& release_callback_, SinkStream& sink_stream_, std::string&& name_); /// Plays the audio stream void Play(); diff --git a/src/audio_core/voice_context.cpp b/src/audio_core/voice_context.cpp index c46ee55f1..867b8fc6b 100644 --- a/src/audio_core/voice_context.cpp +++ b/src/audio_core/voice_context.cpp @@ -8,7 +8,7 @@ namespace AudioCore { -ServerVoiceChannelResource::ServerVoiceChannelResource(s32 id) : id(id) {} +ServerVoiceChannelResource::ServerVoiceChannelResource(s32 id_) : id(id_) {} ServerVoiceChannelResource::~ServerVoiceChannelResource() = default; bool ServerVoiceChannelResource::InUse() const { @@ -209,7 +209,8 @@ void ServerVoiceInfo::UpdateWaveBuffers( void ServerVoiceInfo::UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, const WaveBuffer& in_wave_buffer, SampleFormat sample_format, - bool is_buffer_valid, BehaviorInfo& behavior_info) { + bool is_buffer_valid, + [[maybe_unused]] BehaviorInfo& behavior_info) { if (!is_buffer_valid && out_wavebuffer.sent_to_dsp) { out_wavebuffer.buffer_address = 0; out_wavebuffer.buffer_size = 0; @@ -400,7 +401,7 @@ bool ServerVoiceInfo::HasValidWaveBuffer(const VoiceState* state) const { return std::find(valid_wb.begin(), valid_wb.end(), true) != valid_wb.end(); } -VoiceContext::VoiceContext(std::size_t voice_count) : voice_count(voice_count) { +VoiceContext::VoiceContext(std::size_t voice_count_) : voice_count{voice_count_} { for (std::size_t i = 0; i < voice_count; i++) { voice_channel_resources.emplace_back(static_cast(i)); sorted_voice_info.push_back(&voice_info.emplace_back()); diff --git a/src/audio_core/voice_context.h b/src/audio_core/voice_context.h index 59d3d7dfb..863248761 100644 --- a/src/audio_core/voice_context.h +++ b/src/audio_core/voice_context.h @@ -118,12 +118,12 @@ public: bool in_use{}; INSERT_PADDING_BYTES(11); }; - static_assert(sizeof(VoiceChannelResource::InParams) == 0x70, "InParams is an invalid size"); + static_assert(sizeof(InParams) == 0x70, "InParams is an invalid size"); }; class ServerVoiceChannelResource { public: - explicit ServerVoiceChannelResource(s32 id); + explicit ServerVoiceChannelResource(s32 id_); ~ServerVoiceChannelResource(); bool InUse() const; @@ -174,7 +174,7 @@ public: BehaviorFlags behavior_flags{}; INSERT_PADDING_BYTES(16); }; - static_assert(sizeof(VoiceInfo::InParams) == 0x170, "InParams is an invalid size"); + static_assert(sizeof(InParams) == 0x170, "InParams is an invalid size"); struct OutParams { u64_le played_sample_count{}; @@ -182,7 +182,7 @@ public: u8 voice_dropped{}; INSERT_PADDING_BYTES(3); }; - static_assert(sizeof(VoiceInfo::OutParams) == 0x10, "OutParams is an invalid size"); + static_assert(sizeof(OutParams) == 0x10, "OutParams is an invalid size"); }; class ServerVoiceInfo { @@ -263,7 +263,7 @@ private: class VoiceContext { public: - VoiceContext(std::size_t voice_count); + explicit VoiceContext(std::size_t voice_count_); ~VoiceContext(); std::size_t GetVoiceCount() const; diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 452a2837e..a8c143f85 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -17,8 +17,8 @@ using base_time_point = std::chrono::time_point; class StandardWallClock final : public WallClock { public: - StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) - : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { + explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) { start_time = base_timer::now(); } diff --git a/src/common/wall_clock.h b/src/common/wall_clock.h index bc7adfbf8..cef3e9499 100644 --- a/src/common/wall_clock.h +++ b/src/common/wall_clock.h @@ -38,9 +38,9 @@ public: } protected: - WallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, bool is_native) - : emulated_cpu_frequency{emulated_cpu_frequency}, - emulated_clock_frequency{emulated_clock_frequency}, is_native{is_native} {} + explicit WallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, bool is_native_) + : emulated_cpu_frequency{emulated_cpu_frequency_}, + emulated_clock_frequency{emulated_clock_frequency_}, is_native{is_native_} {} u64 emulated_cpu_frequency; u64 emulated_clock_frequency; diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 424b39b1f..eb8a7782f 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp @@ -43,10 +43,10 @@ u64 EstimateRDTSCFrequency() { } namespace X64 { -NativeClock::NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, - u64 rtsc_frequency) - : WallClock(emulated_cpu_frequency, emulated_clock_frequency, true), rtsc_frequency{ - rtsc_frequency} { +NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, + u64 rtsc_frequency_) + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, true), rtsc_frequency{ + rtsc_frequency_} { _mm_mfence(); last_measure = __rdtsc(); accumulated_ticks = 0U; diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 97aab6ac9..6d1e32ac8 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h @@ -14,7 +14,8 @@ namespace Common { namespace X64 { class NativeClock final : public WallClock { public: - NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency); + explicit NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_, + u64 rtsc_frequency_); std::chrono::nanoseconds GetTimeNS() override; diff --git a/src/core/core_timing.h b/src/core/core_timing.h index b0b6036e4..77ff4c6fe 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h @@ -27,8 +27,8 @@ using TimedCallback = /// Contains the characteristics of a particular event. struct EventType { - EventType(TimedCallback&& callback, std::string&& name) - : callback{std::move(callback)}, name{std::move(name)} {} + explicit EventType(TimedCallback&& callback_, std::string&& name_) + : callback{std::move(callback_)}, name{std::move(name_)} {} /// The event's callback function. TimedCallback callback; @@ -67,8 +67,8 @@ public: void Shutdown(); /// Sets if emulation is multicore or single core, must be set before Initialize - void SetMulticore(bool is_multicore) { - this->is_multicore = is_multicore; + void SetMulticore(bool is_multicore_) { + is_multicore = is_multicore_; } /// Check if it's using host timing. diff --git a/src/core/hle/result.h b/src/core/hle/result.h index b6bdbd988..8feda7ad7 100644 --- a/src/core/hle/result.h +++ b/src/core/hle/result.h @@ -119,7 +119,7 @@ union ResultCode { BitField<0, 9, ErrorModule> module; BitField<9, 13, u32> description; - constexpr explicit ResultCode(u32 raw) : raw(raw) {} + constexpr explicit ResultCode(u32 raw_) : raw(raw_) {} constexpr ResultCode(ErrorModule module_, u32 description_) : raw(module.FormatValue(module_) | description.FormatValue(description_)) {} -- cgit v1.2.3 From b1262674425318eaafc4f4d159b04a0e0e88a4b5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 00:40:42 -0500 Subject: xbyak_abi: Avoid implicit sign conversions --- src/common/x64/xbyak_abi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h index 26e4bfda5..f25e968aa 100644 --- a/src/common/x64/xbyak_abi.h +++ b/src/common/x64/xbyak_abi.h @@ -11,12 +11,12 @@ namespace Common::X64 { -constexpr std::size_t RegToIndex(const Xbyak::Reg& reg) { +constexpr size_t RegToIndex(const Xbyak::Reg& reg) { using Kind = Xbyak::Reg::Kind; ASSERT_MSG((reg.getKind() & (Kind::REG | Kind::XMM)) != 0, "RegSet only support GPRs and XMM registers."); ASSERT_MSG(reg.getIdx() < 16, "RegSet only supports XXM0-15."); - return reg.getIdx() + (reg.getKind() == Kind::REG ? 0 : 16); + return static_cast(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16); } constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { -- cgit v1.2.3 From 2c375013dd96e2b4e57bf50f3c7a62c08ccf5816 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 00:42:49 -0500 Subject: xbyak_abi: Shorten std::size_t to size_t Makes for less reading. --- src/common/x64/xbyak_abi.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/common') diff --git a/src/common/x64/xbyak_abi.h b/src/common/x64/xbyak_abi.h index f25e968aa..c2c9b6134 100644 --- a/src/common/x64/xbyak_abi.h +++ b/src/common/x64/xbyak_abi.h @@ -19,17 +19,17 @@ constexpr size_t RegToIndex(const Xbyak::Reg& reg) { return static_cast(reg.getIdx()) + (reg.getKind() == Kind::REG ? 0 : 16); } -constexpr Xbyak::Reg64 IndexToReg64(std::size_t reg_index) { +constexpr Xbyak::Reg64 IndexToReg64(size_t reg_index) { ASSERT(reg_index < 16); return Xbyak::Reg64(static_cast(reg_index)); } -constexpr Xbyak::Xmm IndexToXmm(std::size_t reg_index) { +constexpr Xbyak::Xmm IndexToXmm(size_t reg_index) { ASSERT(reg_index >= 16 && reg_index < 32); return Xbyak::Xmm(static_cast(reg_index - 16)); } -constexpr Xbyak::Reg IndexToReg(std::size_t reg_index) { +constexpr Xbyak::Reg IndexToReg(size_t reg_index) { if (reg_index < 16) { return IndexToReg64(reg_index); } else { @@ -182,7 +182,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b size_t rsp_alignment, size_t needed_frame_size = 0) { auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size); - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_GPRS[i]) { code.push(IndexToReg64(i)); } @@ -192,7 +192,7 @@ inline size_t ABI_PushRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::b code.sub(code.rsp, frame_info.subtraction); } - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_XMMS[i]) { code.movaps(code.xword[code.rsp + frame_info.xmm_offset], IndexToXmm(i)); frame_info.xmm_offset += 0x10; @@ -206,7 +206,7 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits size_t rsp_alignment, size_t needed_frame_size = 0) { auto frame_info = ABI_CalculateFrameSize(regs, rsp_alignment, needed_frame_size); - for (std::size_t i = 0; i < regs.size(); ++i) { + for (size_t i = 0; i < regs.size(); ++i) { if (regs[i] && ABI_ALL_XMMS[i]) { code.movaps(IndexToXmm(i), code.xword[code.rsp + frame_info.xmm_offset]); frame_info.xmm_offset += 0x10; @@ -218,8 +218,8 @@ inline void ABI_PopRegistersAndAdjustStack(Xbyak::CodeGenerator& code, std::bits } // GPRs need to be popped in reverse order - for (std::size_t j = 0; j < regs.size(); ++j) { - const std::size_t i = regs.size() - j - 1; + for (size_t j = 0; j < regs.size(); ++j) { + const size_t i = regs.size() - j - 1; if (regs[i] && ABI_ALL_GPRS[i]) { code.pop(IndexToReg64(i)); } -- cgit v1.2.3 From f95602f15207851b849c57e2a2dd313a087b2493 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 5 Dec 2020 11:40:14 -0500 Subject: video_core: Resolve more variable shadowing scenarios pt.3 Cleans out the rest of the occurrences of variable shadowing and makes any further occurrences of shadowing compiler errors. --- src/common/scope_exit.h | 2 +- src/common/telemetry.h | 4 +- src/core/file_sys/vfs_vector.h | 12 +-- src/core/hle/kernel/memory/memory_block.h | 6 +- src/video_core/CMakeLists.txt | 9 +- src/video_core/query_cache.h | 8 +- src/video_core/renderer_opengl/gl_buffer_cache.cpp | 32 +++--- src/video_core/renderer_opengl/gl_buffer_cache.h | 14 +-- src/video_core/renderer_opengl/gl_query_cache.cpp | 23 +++-- src/video_core/renderer_opengl/gl_query_cache.h | 10 +- src/video_core/renderer_opengl/gl_rasterizer.cpp | 12 +-- src/video_core/renderer_opengl/gl_rasterizer.h | 8 +- src/video_core/renderer_opengl/gl_shader_cache.cpp | 7 +- src/video_core/renderer_opengl/gl_shader_cache.h | 9 +- .../renderer_opengl/gl_shader_decompiler.h | 4 +- .../renderer_opengl/gl_texture_cache.cpp | 18 ++-- src/video_core/renderer_opengl/gl_texture_cache.h | 6 +- src/video_core/renderer_opengl/renderer_opengl.cpp | 4 +- src/video_core/renderer_opengl/renderer_opengl.h | 8 +- src/video_core/renderer_vulkan/vk_buffer_cache.cpp | 110 ++++++++++----------- src/video_core/renderer_vulkan/vk_buffer_cache.h | 16 +-- src/video_core/renderer_vulkan/vk_command_pool.cpp | 4 +- src/video_core/renderer_vulkan/vk_command_pool.h | 2 +- .../renderer_vulkan/vk_pipeline_cache.cpp | 19 ++-- src/video_core/renderer_vulkan/vk_pipeline_cache.h | 20 ++-- src/video_core/renderer_vulkan/vk_query_cache.cpp | 22 ++--- src/video_core/renderer_vulkan/vk_query_cache.h | 4 +- src/video_core/renderer_vulkan/vk_rasterizer.cpp | 14 +-- src/video_core/renderer_vulkan/vk_rasterizer.h | 10 +- .../renderer_vulkan/vk_texture_cache.cpp | 8 +- src/video_core/renderer_vulkan/vk_texture_cache.h | 9 +- src/video_core/shader/ast.cpp | 4 +- src/video_core/shader/ast.h | 31 +++--- src/video_core/shader/async_shaders.cpp | 2 +- src/video_core/shader/async_shaders.h | 2 +- src/video_core/shader/control_flow.cpp | 4 +- src/video_core/shader/control_flow.h | 14 +-- src/video_core/shader/decode.cpp | 2 +- .../shader/decode/arithmetic_integer.cpp | 7 +- src/video_core/shader/expr.h | 6 +- src/video_core/shader/node.h | 16 +-- src/video_core/shader/shader_ir.cpp | 7 +- src/video_core/shader/shader_ir.h | 8 +- src/video_core/texture_cache/copy_params.h | 18 ++-- .../texture_cache/format_lookup_table.cpp | 12 +-- src/video_core/texture_cache/surface_base.cpp | 8 +- src/video_core/texture_cache/surface_base.h | 10 +- src/video_core/texture_cache/surface_view.h | 10 +- src/video_core/textures/texture.h | 2 +- 49 files changed, 305 insertions(+), 292 deletions(-) (limited to 'src/common') diff --git a/src/common/scope_exit.h b/src/common/scope_exit.h index 68ef5f197..fa46cb394 100644 --- a/src/common/scope_exit.h +++ b/src/common/scope_exit.h @@ -10,7 +10,7 @@ namespace detail { template struct ScopeExitHelper { - explicit ScopeExitHelper(Func&& func) : func(std::move(func)) {} + explicit ScopeExitHelper(Func&& func_) : func(std::move(func_)) {} ~ScopeExitHelper() { if (active) { func(); diff --git a/src/common/telemetry.h b/src/common/telemetry.h index a50c5d1de..49186e848 100644 --- a/src/common/telemetry.h +++ b/src/common/telemetry.h @@ -52,8 +52,8 @@ public: template class Field : public FieldInterface { public: - Field(FieldType type, std::string name, T value) - : name(std::move(name)), type(type), value(std::move(value)) {} + Field(FieldType type_, std::string name_, T value_) + : name(std::move(name_)), type(type_), value(std::move(value_)) {} Field(const Field&) = default; Field& operator=(const Field&) = default; diff --git a/src/core/file_sys/vfs_vector.h b/src/core/file_sys/vfs_vector.h index 95d3da2f2..c214db422 100644 --- a/src/core/file_sys/vfs_vector.h +++ b/src/core/file_sys/vfs_vector.h @@ -17,9 +17,9 @@ namespace FileSys { template class ArrayVfsFile : public VfsFile { public: - explicit ArrayVfsFile(const std::array& data, std::string name = "", - VirtualDir parent = nullptr) - : data(data), name(std::move(name)), parent(std::move(parent)) {} + explicit ArrayVfsFile(const std::array& data_, std::string name_ = "", + VirtualDir parent_ = nullptr) + : data(data_), name(std::move(name_)), parent(std::move(parent_)) {} std::string GetName() const override { return name; @@ -51,12 +51,12 @@ public: return read; } - std::size_t Write(const u8* data, std::size_t length, std::size_t offset) override { + std::size_t Write(const u8* data_, std::size_t length, std::size_t offset) override { return 0; } - bool Rename(std::string_view name) override { - this->name = name; + bool Rename(std::string_view new_name) override { + name = new_name; return true; } diff --git a/src/core/hle/kernel/memory/memory_block.h b/src/core/hle/kernel/memory/memory_block.h index 9d7839d08..37fe19916 100644 --- a/src/core/hle/kernel/memory/memory_block.h +++ b/src/core/hle/kernel/memory/memory_block.h @@ -222,9 +222,9 @@ public: public: constexpr MemoryBlock() = default; - constexpr MemoryBlock(VAddr addr, std::size_t num_pages, MemoryState state, - MemoryPermission perm, MemoryAttribute attribute) - : addr{addr}, num_pages(num_pages), state{state}, perm{perm}, attribute{attribute} {} + constexpr MemoryBlock(VAddr addr_, std::size_t num_pages_, MemoryState state_, + MemoryPermission perm_, MemoryAttribute attribute_) + : addr{addr_}, num_pages(num_pages_), state{state_}, perm{perm_}, attribute{attribute_} {} constexpr VAddr GetAddress() const { return addr; diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index abcee2a1c..a021d61f5 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -297,13 +297,20 @@ if (ENABLE_NSIGHT_AFTERMATH) endif() if (MSVC) - target_compile_options(video_core PRIVATE /we4267) + target_compile_options(video_core PRIVATE + /we4267 # 'var' : conversion from 'size_t' to 'type', possible loss of data + /we4456 # Declaration of 'identifier' hides previous local declaration + /we4457 # Declaration of 'identifier' hides function parameter + /we4458 # Declaration of 'identifier' hides class member + /we4459 # Declaration of 'identifier' hides global declaration + ) else() target_compile_options(video_core PRIVATE -Werror=conversion -Wno-error=sign-conversion -Werror=pessimizing-move -Werror=redundant-move + -Werror=shadow -Werror=switch -Werror=type-limits -Werror=unused-variable diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h index fc54ca0ef..203f2af05 100644 --- a/src/video_core/query_cache.h +++ b/src/video_core/query_cache.h @@ -28,8 +28,8 @@ namespace VideoCommon { template class CounterStreamBase { public: - explicit CounterStreamBase(QueryCache& cache, VideoCore::QueryType type) - : cache{cache}, type{type} {} + explicit CounterStreamBase(QueryCache& cache_, VideoCore::QueryType type_) + : cache{cache_}, type{type_} {} /// Updates the state of the stream, enabling or disabling as needed. void Update(bool enabled) { @@ -334,8 +334,8 @@ private: template class CachedQueryBase { public: - explicit CachedQueryBase(VAddr cpu_addr, u8* host_ptr) - : cpu_addr{cpu_addr}, host_ptr{host_ptr} {} + explicit CachedQueryBase(VAddr cpu_addr_, u8* host_ptr_) + : cpu_addr{cpu_addr_}, host_ptr{host_ptr_} {} virtual ~CachedQueryBase() = default; CachedQueryBase(CachedQueryBase&&) noexcept = default; diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.cpp b/src/video_core/renderer_opengl/gl_buffer_cache.cpp index b1c4cd62f..60735d502 100644 --- a/src/video_core/renderer_opengl/gl_buffer_cache.cpp +++ b/src/video_core/renderer_opengl/gl_buffer_cache.cpp @@ -22,11 +22,11 @@ using Maxwell = Tegra::Engines::Maxwell3D::Regs; MICROPROFILE_DEFINE(OpenGL_Buffer_Download, "OpenGL", "Buffer Download", MP_RGB(192, 192, 128)); -Buffer::Buffer(const Device& device, VAddr cpu_addr, std::size_t size) - : VideoCommon::BufferBlock{cpu_addr, size} { +Buffer::Buffer(const Device& device_, VAddr cpu_addr_, std::size_t size_) + : BufferBlock{cpu_addr_, size_} { gl_buffer.Create(); - glNamedBufferData(gl_buffer.handle, static_cast(size), nullptr, GL_DYNAMIC_DRAW); - if (device.UseAssemblyShaders() || device.HasVertexBufferUnifiedMemory()) { + glNamedBufferData(gl_buffer.handle, static_cast(size_), nullptr, GL_DYNAMIC_DRAW); + if (device_.UseAssemblyShaders() || device_.HasVertexBufferUnifiedMemory()) { glMakeNamedBufferResidentNV(gl_buffer.handle, GL_READ_WRITE); glGetNamedBufferParameterui64vNV(gl_buffer.handle, GL_BUFFER_GPU_ADDRESS_NV, &gpu_address); } @@ -34,14 +34,14 @@ Buffer::Buffer(const Device& device, VAddr cpu_addr, std::size_t size) Buffer::~Buffer() = default; -void Buffer::Upload(std::size_t offset, std::size_t size, const u8* data) { - glNamedBufferSubData(Handle(), static_cast(offset), static_cast(size), - data); +void Buffer::Upload(std::size_t offset, std::size_t data_size, const u8* data) { + glNamedBufferSubData(Handle(), static_cast(offset), + static_cast(data_size), data); } -void Buffer::Download(std::size_t offset, std::size_t size, u8* data) { +void Buffer::Download(std::size_t offset, std::size_t data_size, u8* data) { MICROPROFILE_SCOPE(OpenGL_Buffer_Download); - const GLsizeiptr gl_size = static_cast(size); + const GLsizeiptr gl_size = static_cast(data_size); const GLintptr gl_offset = static_cast(offset); if (read_buffer.handle == 0) { read_buffer.Create(); @@ -54,16 +54,16 @@ void Buffer::Download(std::size_t offset, std::size_t size, u8* data) { } void Buffer::CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst_offset, - std::size_t size) { + std::size_t copy_size) { glCopyNamedBufferSubData(src.Handle(), Handle(), static_cast(src_offset), - static_cast(dst_offset), static_cast(size)); + static_cast(dst_offset), static_cast(copy_size)); } -OGLBufferCache::OGLBufferCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::MemoryManager& gpu_memory, Core::Memory::Memory& cpu_memory, - const Device& device_, std::size_t stream_size) - : GenericBufferCache{rasterizer, gpu_memory, cpu_memory, - std::make_unique(device_, stream_size, true)}, +OGLBufferCache::OGLBufferCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_, + const Device& device_, std::size_t stream_size_) + : GenericBufferCache{rasterizer_, gpu_memory_, cpu_memory_, + std::make_unique(device_, stream_size_, true)}, device{device_} { if (!device.HasFastBufferSubData()) { return; diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.h b/src/video_core/renderer_opengl/gl_buffer_cache.h index f75b32e31..95251e26b 100644 --- a/src/video_core/renderer_opengl/gl_buffer_cache.h +++ b/src/video_core/renderer_opengl/gl_buffer_cache.h @@ -25,15 +25,15 @@ class RasterizerOpenGL; class Buffer : public VideoCommon::BufferBlock { public: - explicit Buffer(const Device& device, VAddr cpu_addr, std::size_t size); + explicit Buffer(const Device& device_, VAddr cpu_addr_, std::size_t size_); ~Buffer(); - void Upload(std::size_t offset, std::size_t size, const u8* data); + void Upload(std::size_t offset, std::size_t data_size, const u8* data); - void Download(std::size_t offset, std::size_t size, u8* data); + void Download(std::size_t offset, std::size_t data_size, u8* data); void CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst_offset, - std::size_t size); + std::size_t copy_size); GLuint Handle() const noexcept { return gl_buffer.handle; @@ -52,9 +52,9 @@ private: using GenericBufferCache = VideoCommon::BufferCache; class OGLBufferCache final : public GenericBufferCache { public: - explicit OGLBufferCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::MemoryManager& gpu_memory, Core::Memory::Memory& cpu_memory, - const Device& device, std::size_t stream_size); + explicit OGLBufferCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_, + const Device& device_, std::size_t stream_size_); ~OGLBufferCache(); BufferInfo GetEmptyBuffer(std::size_t) override; diff --git a/src/video_core/renderer_opengl/gl_query_cache.cpp b/src/video_core/renderer_opengl/gl_query_cache.cpp index bcc37471f..acebbf5f4 100644 --- a/src/video_core/renderer_opengl/gl_query_cache.cpp +++ b/src/video_core/renderer_opengl/gl_query_cache.cpp @@ -30,11 +30,9 @@ constexpr GLenum GetTarget(VideoCore::QueryType type) { } // Anonymous namespace -QueryCache::QueryCache(RasterizerOpenGL& rasterizer, Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::MemoryManager& gpu_memory) - : VideoCommon::QueryCacheBase( - rasterizer, maxwell3d, gpu_memory), - gl_rasterizer{rasterizer} {} +QueryCache::QueryCache(RasterizerOpenGL& rasterizer_, Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_) + : QueryCacheBase(rasterizer_, maxwell3d_, gpu_memory_), gl_rasterizer{rasterizer_} {} QueryCache::~QueryCache() = default; @@ -59,10 +57,11 @@ bool QueryCache::AnyCommandQueued() const noexcept { return gl_rasterizer.AnyCommandQueued(); } -HostCounter::HostCounter(QueryCache& cache_, std::shared_ptr dependency, +HostCounter::HostCounter(QueryCache& cache_, std::shared_ptr dependency_, VideoCore::QueryType type_) - : HostCounterBase{std::move(dependency)}, cache{cache_}, type{type_}, - query{cache.AllocateQuery(type)} { + : HostCounterBase{std::move(dependency_)}, cache{cache_}, type{type_}, query{ + cache.AllocateQuery( + type)} { glBeginQuery(GetTarget(type), query.handle); } @@ -86,14 +85,14 @@ u64 HostCounter::BlockingQuery() const { return static_cast(value); } -CachedQuery::CachedQuery(QueryCache& cache_, VideoCore::QueryType type_, VAddr cpu_addr, - u8* host_ptr) - : CachedQueryBase{cpu_addr, host_ptr}, cache{&cache_}, type{type_} {} +CachedQuery::CachedQuery(QueryCache& cache_, VideoCore::QueryType type_, VAddr cpu_addr_, + u8* host_ptr_) + : CachedQueryBase{cpu_addr_, host_ptr_}, cache{&cache_}, type{type_} {} CachedQuery::~CachedQuery() = default; CachedQuery::CachedQuery(CachedQuery&& rhs) noexcept - : CachedQueryBase(std::move(rhs)), cache{rhs.cache}, type{rhs.type} {} + : CachedQueryBase(std::move(rhs)), cache{rhs.cache}, type{rhs.type} {} CachedQuery& CachedQuery::operator=(CachedQuery&& rhs) noexcept { cache = rhs.cache; diff --git a/src/video_core/renderer_opengl/gl_query_cache.h b/src/video_core/renderer_opengl/gl_query_cache.h index d9851e880..7bbe5cfe9 100644 --- a/src/video_core/renderer_opengl/gl_query_cache.h +++ b/src/video_core/renderer_opengl/gl_query_cache.h @@ -29,8 +29,8 @@ using CounterStream = VideoCommon::CounterStreamBase; class QueryCache final : public VideoCommon::QueryCacheBase { public: - explicit QueryCache(RasterizerOpenGL& rasterizer, Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::MemoryManager& gpu_memory); + explicit QueryCache(RasterizerOpenGL& rasterizer_, Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_); ~QueryCache(); OGLQuery AllocateQuery(VideoCore::QueryType type); @@ -46,7 +46,7 @@ private: class HostCounter final : public VideoCommon::HostCounterBase { public: - explicit HostCounter(QueryCache& cache_, std::shared_ptr dependency, + explicit HostCounter(QueryCache& cache_, std::shared_ptr dependency_, VideoCore::QueryType type_); ~HostCounter(); @@ -62,8 +62,8 @@ private: class CachedQuery final : public VideoCommon::CachedQueryBase { public: - explicit CachedQuery(QueryCache& cache_, VideoCore::QueryType type_, VAddr cpu_addr, - u8* host_ptr); + explicit CachedQuery(QueryCache& cache_, VideoCore::QueryType type_, VAddr cpu_addr_, + u8* host_ptr_); ~CachedQuery() override; CachedQuery(CachedQuery&& rhs) noexcept; diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index cfddbde5d..8572af5a5 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -149,19 +149,19 @@ void UpdateBindlessSSBOs(GLenum target, const BindlessSSBO* ssbos, size_t num_ss } // Anonymous namespace -RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window, Tegra::GPU& gpu_, - Core::Memory::Memory& cpu_memory, const Device& device_, +RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_, + Core::Memory::Memory& cpu_memory_, const Device& device_, ScreenInfo& screen_info_, ProgramManager& program_manager_, StateTracker& state_tracker_) - : RasterizerAccelerated{cpu_memory}, gpu(gpu_), maxwell3d(gpu.Maxwell3D()), + : RasterizerAccelerated{cpu_memory_}, gpu(gpu_), maxwell3d(gpu.Maxwell3D()), kepler_compute(gpu.KeplerCompute()), gpu_memory(gpu.MemoryManager()), device(device_), screen_info(screen_info_), program_manager(program_manager_), state_tracker(state_tracker_), texture_cache(*this, maxwell3d, gpu_memory, device, state_tracker), - shader_cache(*this, emu_window, gpu, maxwell3d, kepler_compute, gpu_memory, device), + shader_cache(*this, emu_window_, gpu, maxwell3d, kepler_compute, gpu_memory, device), query_cache(*this, maxwell3d, gpu_memory), - buffer_cache(*this, gpu_memory, cpu_memory, device, STREAM_BUFFER_SIZE), + buffer_cache(*this, gpu_memory, cpu_memory_, device, STREAM_BUFFER_SIZE), fence_manager(*this, gpu, texture_cache, buffer_cache, query_cache), - async_shaders(emu_window) { + async_shaders(emu_window_) { CheckExtensions(); unified_uniform_buffer.Create(); diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 1d0f585fa..de28cff15 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -62,10 +62,10 @@ static_assert(sizeof(BindlessSSBO) * CHAR_BIT == 128); class RasterizerOpenGL : public VideoCore::RasterizerAccelerated { public: - explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window, Tegra::GPU& gpu, - Core::Memory::Memory& cpu_memory, const Device& device, - ScreenInfo& screen_info, ProgramManager& program_manager, - StateTracker& state_tracker); + explicit RasterizerOpenGL(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_, + Core::Memory::Memory& cpu_memory_, const Device& device_, + ScreenInfo& screen_info_, ProgramManager& program_manager_, + StateTracker& state_tracker_); ~RasterizerOpenGL() override; void Draw(bool is_indexed, bool is_instanced) override; diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp index 9f2c0a222..0b96481f5 100644 --- a/src/video_core/renderer_opengl/gl_shader_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp @@ -318,14 +318,13 @@ std::unique_ptr Shader::CreateFromCache(const ShaderParameters& params, precompiled_shader.registry, precompiled_shader.entries, precompiled_shader.program)); } -ShaderCacheOpenGL::ShaderCacheOpenGL(RasterizerOpenGL& rasterizer, +ShaderCacheOpenGL::ShaderCacheOpenGL(RasterizerOpenGL& rasterizer_, Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_, Tegra::Engines::Maxwell3D& maxwell3d_, Tegra::Engines::KeplerCompute& kepler_compute_, Tegra::MemoryManager& gpu_memory_, const Device& device_) - : VideoCommon::ShaderCache{rasterizer}, emu_window{emu_window_}, gpu{gpu_}, - gpu_memory{gpu_memory_}, maxwell3d{maxwell3d_}, - kepler_compute{kepler_compute_}, device{device_} {} + : ShaderCache{rasterizer_}, emu_window{emu_window_}, gpu{gpu_}, gpu_memory{gpu_memory_}, + maxwell3d{maxwell3d_}, kepler_compute{kepler_compute_}, device{device_} {} ShaderCacheOpenGL::~ShaderCacheOpenGL() = default; diff --git a/src/video_core/renderer_opengl/gl_shader_cache.h b/src/video_core/renderer_opengl/gl_shader_cache.h index ab5374fac..2aed0697e 100644 --- a/src/video_core/renderer_opengl/gl_shader_cache.h +++ b/src/video_core/renderer_opengl/gl_shader_cache.h @@ -119,10 +119,11 @@ private: class ShaderCacheOpenGL final : public VideoCommon::ShaderCache { public: - explicit ShaderCacheOpenGL(RasterizerOpenGL& rasterizer, Core::Frontend::EmuWindow& emu_window, - Tegra::GPU& gpu, Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::Engines::KeplerCompute& kepler_compute, - Tegra::MemoryManager& gpu_memory, const Device& device); + explicit ShaderCacheOpenGL(RasterizerOpenGL& rasterizer_, + Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::Engines::KeplerCompute& kepler_compute_, + Tegra::MemoryManager& gpu_memory_, const Device& device_); ~ShaderCacheOpenGL() override; /// Loads disk cache for the current game diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.h b/src/video_core/renderer_opengl/gl_shader_decompiler.h index f5a5249f2..c4ff47875 100644 --- a/src/video_core/renderer_opengl/gl_shader_decompiler.h +++ b/src/video_core/renderer_opengl/gl_shader_decompiler.h @@ -25,8 +25,8 @@ using ImageEntry = VideoCommon::Shader::Image; class ConstBufferEntry : public VideoCommon::Shader::ConstBuffer { public: - explicit ConstBufferEntry(u32 max_offset, bool is_indirect, u32 index_) - : ConstBuffer{max_offset, is_indirect}, index{index_} {} + explicit ConstBufferEntry(u32 max_offset_, bool is_indirect_, u32 index_) + : ConstBuffer{max_offset_, is_indirect_}, index{index_} {} u32 GetIndex() const { return index; diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp index a59fe853e..f19ef2173 100644 --- a/src/video_core/renderer_opengl/gl_texture_cache.cpp +++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp @@ -347,14 +347,14 @@ void CachedSurface::UploadTextureMipmap(u32 level, const std::vector& stagin internal_format, image_size, buffer); break; case SurfaceTarget::TextureCubemap: { - const std::size_t layer_size{params.GetHostLayerSize(level)}; + const std::size_t host_layer_size{params.GetHostLayerSize(level)}; for (std::size_t face = 0; face < params.depth; ++face) { glCompressedTextureSubImage3D(texture.handle, level, 0, 0, static_cast(face), static_cast(params.GetMipWidth(level)), static_cast(params.GetMipHeight(level)), 1, - internal_format, static_cast(layer_size), - buffer); - buffer += layer_size; + internal_format, + static_cast(host_layer_size), buffer); + buffer += host_layer_size; } break; } @@ -532,12 +532,12 @@ OGLTextureView CachedSurfaceView::CreateTextureView() const { return texture_view; } -TextureCacheOpenGL::TextureCacheOpenGL(VideoCore::RasterizerInterface& rasterizer, - Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::MemoryManager& gpu_memory, const Device& device, +TextureCacheOpenGL::TextureCacheOpenGL(VideoCore::RasterizerInterface& rasterizer_, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_, const Device& device_, StateTracker& state_tracker_) - : TextureCacheBase{rasterizer, maxwell3d, gpu_memory, device.HasASTC()}, state_tracker{ - state_tracker_} { + : TextureCacheBase{rasterizer_, maxwell3d_, gpu_memory_, device_.HasASTC()}, + state_tracker{state_tracker_} { src_framebuffer.Create(); dst_framebuffer.Create(); } diff --git a/src/video_core/renderer_opengl/gl_texture_cache.h b/src/video_core/renderer_opengl/gl_texture_cache.h index 76a7b2316..72b284fab 100644 --- a/src/video_core/renderer_opengl/gl_texture_cache.h +++ b/src/video_core/renderer_opengl/gl_texture_cache.h @@ -130,9 +130,9 @@ private: class TextureCacheOpenGL final : public TextureCacheBase { public: - explicit TextureCacheOpenGL(VideoCore::RasterizerInterface& rasterizer, - Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::MemoryManager& gpu_memory, const Device& device, + explicit TextureCacheOpenGL(VideoCore::RasterizerInterface& rasterizer_, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_, const Device& device_, StateTracker& state_tracker); ~TextureCacheOpenGL(); diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp index 1523cd6fa..521b03ba2 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.cpp +++ b/src/video_core/renderer_opengl/renderer_opengl.cpp @@ -130,8 +130,8 @@ void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum severit RendererOpenGL::RendererOpenGL(Core::TelemetrySession& telemetry_session_, Core::Frontend::EmuWindow& emu_window_, Core::Memory::Memory& cpu_memory_, Tegra::GPU& gpu_, - std::unique_ptr context) - : RendererBase{emu_window_, std::move(context)}, telemetry_session{telemetry_session_}, + std::unique_ptr context_) + : RendererBase{emu_window_, std::move(context_)}, telemetry_session{telemetry_session_}, emu_window{emu_window_}, cpu_memory{cpu_memory_}, gpu{gpu_}, program_manager{device} {} RendererOpenGL::~RendererOpenGL() = default; diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h index 9ef181f95..376f88766 100644 --- a/src/video_core/renderer_opengl/renderer_opengl.h +++ b/src/video_core/renderer_opengl/renderer_opengl.h @@ -57,10 +57,10 @@ struct ScreenInfo { class RendererOpenGL final : public VideoCore::RendererBase { public: - explicit RendererOpenGL(Core::TelemetrySession& telemetry_session, - Core::Frontend::EmuWindow& emu_window, Core::Memory::Memory& cpu_memory, - Tegra::GPU& gpu, - std::unique_ptr context); + explicit RendererOpenGL(Core::TelemetrySession& telemetry_session_, + Core::Frontend::EmuWindow& emu_window_, + Core::Memory::Memory& cpu_memory_, Tegra::GPU& gpu_, + std::unique_ptr context_); ~RendererOpenGL() override; bool Init() override; diff --git a/src/video_core/renderer_vulkan/vk_buffer_cache.cpp b/src/video_core/renderer_vulkan/vk_buffer_cache.cpp index d9d3da9ea..444d3fb93 100644 --- a/src/video_core/renderer_vulkan/vk_buffer_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_buffer_cache.cpp @@ -38,13 +38,13 @@ std::unique_ptr CreateStreamBuffer(const VKDevice& device, VKSch } // Anonymous namespace Buffer::Buffer(const VKDevice& device, VKMemoryManager& memory_manager, VKScheduler& scheduler_, - VKStagingBufferPool& staging_pool_, VAddr cpu_addr, std::size_t size) - : BufferBlock{cpu_addr, size}, scheduler{scheduler_}, staging_pool{staging_pool_} { + VKStagingBufferPool& staging_pool_, VAddr cpu_addr_, std::size_t size_) + : BufferBlock{cpu_addr_, size_}, scheduler{scheduler_}, staging_pool{staging_pool_} { const VkBufferCreateInfo ci{ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = nullptr, .flags = 0, - .size = static_cast(size), + .size = static_cast(size_), .usage = BUFFER_USAGE | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE, .queueFamilyIndexCount = 0, @@ -57,69 +57,71 @@ Buffer::Buffer(const VKDevice& device, VKMemoryManager& memory_manager, VKSchedu Buffer::~Buffer() = default; -void Buffer::Upload(std::size_t offset, std::size_t size, const u8* data) { - const auto& staging = staging_pool.GetUnusedBuffer(size, true); - std::memcpy(staging.commit->Map(size), data, size); +void Buffer::Upload(std::size_t offset, std::size_t data_size, const u8* data) { + const auto& staging = staging_pool.GetUnusedBuffer(data_size, true); + std::memcpy(staging.commit->Map(data_size), data, data_size); scheduler.RequestOutsideRenderPassOperationContext(); const VkBuffer handle = Handle(); - scheduler.Record([staging = *staging.handle, handle, offset, size](vk::CommandBuffer cmdbuf) { - cmdbuf.CopyBuffer(staging, handle, VkBufferCopy{0, offset, size}); - - const VkBufferMemoryBarrier barrier{ - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, - .dstAccessMask = UPLOAD_ACCESS_BARRIERS, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .buffer = handle, - .offset = offset, - .size = size, - }; - cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, UPLOAD_PIPELINE_STAGE, 0, {}, - barrier, {}); - }); + scheduler.Record( + [staging = *staging.handle, handle, offset, data_size](vk::CommandBuffer cmdbuf) { + cmdbuf.CopyBuffer(staging, handle, VkBufferCopy{0, offset, data_size}); + + const VkBufferMemoryBarrier barrier{ + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, + .dstAccessMask = UPLOAD_ACCESS_BARRIERS, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .buffer = handle, + .offset = offset, + .size = data_size, + }; + cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, UPLOAD_PIPELINE_STAGE, 0, {}, + barrier, {}); + }); } -void Buffer::Download(std::size_t offset, std::size_t size, u8* data) { - const auto& staging = staging_pool.GetUnusedBuffer(size, true); +void Buffer::Download(std::size_t offset, std::size_t data_size, u8* data) { + const auto& staging = staging_pool.GetUnusedBuffer(data_size, true); scheduler.RequestOutsideRenderPassOperationContext(); const VkBuffer handle = Handle(); - scheduler.Record([staging = *staging.handle, handle, offset, size](vk::CommandBuffer cmdbuf) { - const VkBufferMemoryBarrier barrier{ - .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, - .pNext = nullptr, - .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, - .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT, - .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, - .buffer = handle, - .offset = offset, - .size = size, - }; - - cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | - VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | - VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, - VK_PIPELINE_STAGE_TRANSFER_BIT, 0, {}, barrier, {}); - cmdbuf.CopyBuffer(handle, staging, VkBufferCopy{offset, 0, size}); - }); + scheduler.Record( + [staging = *staging.handle, handle, offset, data_size](vk::CommandBuffer cmdbuf) { + const VkBufferMemoryBarrier barrier{ + .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER, + .pNext = nullptr, + .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, + .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .buffer = handle, + .offset = offset, + .size = data_size, + }; + + cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | + VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | + VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, 0, {}, barrier, {}); + cmdbuf.CopyBuffer(handle, staging, VkBufferCopy{offset, 0, data_size}); + }); scheduler.Finish(); - std::memcpy(data, staging.commit->Map(size), size); + std::memcpy(data, staging.commit->Map(data_size), data_size); } void Buffer::CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst_offset, - std::size_t size) { + std::size_t copy_size) { scheduler.RequestOutsideRenderPassOperationContext(); const VkBuffer dst_buffer = Handle(); scheduler.Record([src_buffer = src.Handle(), dst_buffer, src_offset, dst_offset, - size](vk::CommandBuffer cmdbuf) { - cmdbuf.CopyBuffer(src_buffer, dst_buffer, VkBufferCopy{src_offset, dst_offset, size}); + copy_size](vk::CommandBuffer cmdbuf) { + cmdbuf.CopyBuffer(src_buffer, dst_buffer, VkBufferCopy{src_offset, dst_offset, copy_size}); std::array barriers; barriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; @@ -130,7 +132,7 @@ void Buffer::CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst barriers[0].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barriers[0].buffer = src_buffer; barriers[0].offset = src_offset; - barriers[0].size = size; + barriers[0].size = copy_size; barriers[1].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; barriers[1].pNext = nullptr; barriers[1].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; @@ -139,19 +141,17 @@ void Buffer::CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst barriers[1].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barriers[1].buffer = dst_buffer; barriers[1].offset = dst_offset; - barriers[1].size = size; + barriers[1].size = copy_size; cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, UPLOAD_PIPELINE_STAGE, 0, {}, barriers, {}); }); } -VKBufferCache::VKBufferCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::MemoryManager& gpu_memory, Core::Memory::Memory& cpu_memory, +VKBufferCache::VKBufferCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_, const VKDevice& device_, VKMemoryManager& memory_manager_, VKScheduler& scheduler_, VKStagingBufferPool& staging_pool_) - : VideoCommon::BufferCache{rasterizer, gpu_memory, cpu_memory, - CreateStreamBuffer(device_, - scheduler_)}, + : BufferCache{rasterizer_, gpu_memory_, cpu_memory_, CreateStreamBuffer(device_, scheduler_)}, device{device_}, memory_manager{memory_manager_}, scheduler{scheduler_}, staging_pool{ staging_pool_} {} diff --git a/src/video_core/renderer_vulkan/vk_buffer_cache.h b/src/video_core/renderer_vulkan/vk_buffer_cache.h index 7fb5ceedf..6008b8373 100644 --- a/src/video_core/renderer_vulkan/vk_buffer_cache.h +++ b/src/video_core/renderer_vulkan/vk_buffer_cache.h @@ -22,15 +22,15 @@ class VKScheduler; class Buffer final : public VideoCommon::BufferBlock { public: explicit Buffer(const VKDevice& device, VKMemoryManager& memory_manager, VKScheduler& scheduler, - VKStagingBufferPool& staging_pool, VAddr cpu_addr, std::size_t size); + VKStagingBufferPool& staging_pool, VAddr cpu_addr_, std::size_t size_); ~Buffer(); - void Upload(std::size_t offset, std::size_t size, const u8* data); + void Upload(std::size_t offset, std::size_t data_size, const u8* data); - void Download(std::size_t offset, std::size_t size, u8* data); + void Download(std::size_t offset, std::size_t data_size, u8* data); void CopyFrom(const Buffer& src, std::size_t src_offset, std::size_t dst_offset, - std::size_t size); + std::size_t copy_size); VkBuffer Handle() const { return *buffer.handle; @@ -49,10 +49,10 @@ private: class VKBufferCache final : public VideoCommon::BufferCache { public: - explicit VKBufferCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::MemoryManager& gpu_memory, Core::Memory::Memory& cpu_memory, - const VKDevice& device, VKMemoryManager& memory_manager, - VKScheduler& scheduler, VKStagingBufferPool& staging_pool); + explicit VKBufferCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_, + const VKDevice& device_, VKMemoryManager& memory_manager_, + VKScheduler& scheduler_, VKStagingBufferPool& staging_pool_); ~VKBufferCache(); BufferInfo GetEmptyBuffer(std::size_t size) override; diff --git a/src/video_core/renderer_vulkan/vk_command_pool.cpp b/src/video_core/renderer_vulkan/vk_command_pool.cpp index 256a39148..8f7d6410e 100644 --- a/src/video_core/renderer_vulkan/vk_command_pool.cpp +++ b/src/video_core/renderer_vulkan/vk_command_pool.cpp @@ -17,8 +17,8 @@ struct CommandPool::Pool { vk::CommandBuffers cmdbufs; }; -CommandPool::CommandPool(MasterSemaphore& master_semaphore, const VKDevice& device_) - : ResourcePool(master_semaphore, COMMAND_BUFFER_POOL_SIZE), device{device_} {} +CommandPool::CommandPool(MasterSemaphore& master_semaphore_, const VKDevice& device_) + : ResourcePool(master_semaphore_, COMMAND_BUFFER_POOL_SIZE), device{device_} {} CommandPool::~CommandPool() = default; diff --git a/src/video_core/renderer_vulkan/vk_command_pool.h b/src/video_core/renderer_vulkan/vk_command_pool.h index 33655eca4..62a7ce3f1 100644 --- a/src/video_core/renderer_vulkan/vk_command_pool.h +++ b/src/video_core/renderer_vulkan/vk_command_pool.h @@ -17,7 +17,7 @@ class VKDevice; class CommandPool final : public ResourcePool { public: - explicit CommandPool(MasterSemaphore& master_semaphore, const VKDevice& device_); + explicit CommandPool(MasterSemaphore& master_semaphore_, const VKDevice& device_); ~CommandPool() override; void Allocate(size_t begin, size_t end) override; diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp index df7e8c864..39e58a56f 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp @@ -136,26 +136,25 @@ bool ComputePipelineCacheKey::operator==(const ComputePipelineCacheKey& rhs) con return std::memcmp(&rhs, this, sizeof *this) == 0; } -Shader::Shader(Tegra::Engines::ConstBufferEngineInterface& engine, Tegra::Engines::ShaderType stage, - GPUVAddr gpu_addr_, VAddr cpu_addr, VideoCommon::Shader::ProgramCode program_code_, - u32 main_offset) - : gpu_addr(gpu_addr_), program_code(std::move(program_code_)), registry(stage, engine), - shader_ir(program_code, main_offset, compiler_settings, registry), +Shader::Shader(Tegra::Engines::ConstBufferEngineInterface& engine_, ShaderType stage_, + GPUVAddr gpu_addr_, VAddr cpu_addr_, ProgramCode program_code_, u32 main_offset_) + : gpu_addr(gpu_addr_), program_code(std::move(program_code_)), registry(stage_, engine_), + shader_ir(program_code, main_offset_, compiler_settings, registry), entries(GenerateShaderEntries(shader_ir)) {} Shader::~Shader() = default; -VKPipelineCache::VKPipelineCache(RasterizerVulkan& rasterizer, Tegra::GPU& gpu_, +VKPipelineCache::VKPipelineCache(RasterizerVulkan& rasterizer_, Tegra::GPU& gpu_, Tegra::Engines::Maxwell3D& maxwell3d_, Tegra::Engines::KeplerCompute& kepler_compute_, Tegra::MemoryManager& gpu_memory_, const VKDevice& device_, VKScheduler& scheduler_, VKDescriptorPool& descriptor_pool_, VKUpdateDescriptorQueue& update_descriptor_queue_, VKRenderPassCache& renderpass_cache_) - : VideoCommon::ShaderCache{rasterizer}, gpu{gpu_}, maxwell3d{maxwell3d_}, - kepler_compute{kepler_compute_}, gpu_memory{gpu_memory_}, device{device_}, - scheduler{scheduler_}, descriptor_pool{descriptor_pool_}, - update_descriptor_queue{update_descriptor_queue_}, renderpass_cache{renderpass_cache_} {} + : ShaderCache{rasterizer_}, gpu{gpu_}, maxwell3d{maxwell3d_}, kepler_compute{kepler_compute_}, + gpu_memory{gpu_memory_}, device{device_}, scheduler{scheduler_}, + descriptor_pool{descriptor_pool_}, update_descriptor_queue{update_descriptor_queue_}, + renderpass_cache{renderpass_cache_} {} VKPipelineCache::~VKPipelineCache() = default; diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.h b/src/video_core/renderer_vulkan/vk_pipeline_cache.h index e558e6658..9e1f8fcbb 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.h +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.h @@ -84,9 +84,9 @@ namespace Vulkan { class Shader { public: - explicit Shader(Tegra::Engines::ConstBufferEngineInterface& engine, - Tegra::Engines::ShaderType stage, GPUVAddr gpu_addr, VAddr cpu_addr, - VideoCommon::Shader::ProgramCode program_code, u32 main_offset); + explicit Shader(Tegra::Engines::ConstBufferEngineInterface& engine_, + Tegra::Engines::ShaderType stage_, GPUVAddr gpu_addr, VAddr cpu_addr_, + VideoCommon::Shader::ProgramCode program_code, u32 main_offset_); ~Shader(); GPUVAddr GetGpuAddr() const { @@ -119,13 +119,13 @@ private: class VKPipelineCache final : public VideoCommon::ShaderCache { public: - explicit VKPipelineCache(RasterizerVulkan& rasterizer, Tegra::GPU& gpu, - Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::Engines::KeplerCompute& kepler_compute, - Tegra::MemoryManager& gpu_memory, const VKDevice& device, - VKScheduler& scheduler, VKDescriptorPool& descriptor_pool, - VKUpdateDescriptorQueue& update_descriptor_queue, - VKRenderPassCache& renderpass_cache); + explicit VKPipelineCache(RasterizerVulkan& rasterizer_, Tegra::GPU& gpu_, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::Engines::KeplerCompute& kepler_compute_, + Tegra::MemoryManager& gpu_memory_, const VKDevice& device_, + VKScheduler& scheduler_, VKDescriptorPool& descriptor_pool_, + VKUpdateDescriptorQueue& update_descriptor_queue_, + VKRenderPassCache& renderpass_cache_); ~VKPipelineCache() override; std::array GetShaders(); diff --git a/src/video_core/renderer_vulkan/vk_query_cache.cpp b/src/video_core/renderer_vulkan/vk_query_cache.cpp index 6fa071737..038760de3 100644 --- a/src/video_core/renderer_vulkan/vk_query_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_query_cache.cpp @@ -69,12 +69,10 @@ void QueryPool::Reserve(std::pair query) { VKQueryCache::VKQueryCache(VideoCore::RasterizerInterface& rasterizer_, Tegra::Engines::Maxwell3D& maxwell3d_, Tegra::MemoryManager& gpu_memory_, const VKDevice& device_, VKScheduler& scheduler_) - : QueryCacheBase{rasterizer_, maxwell3d_, - gpu_memory_}, - device{device_}, scheduler{scheduler_}, query_pools{ - QueryPool{device_, scheduler_, - QueryType::SamplesPassed}, - } {} + : QueryCacheBase{rasterizer_, maxwell3d_, gpu_memory_}, device{device_}, scheduler{scheduler_}, + query_pools{ + QueryPool{device_, scheduler_, QueryType::SamplesPassed}, + } {} VKQueryCache::~VKQueryCache() { // TODO(Rodrigo): This is a hack to destroy all HostCounter instances before the base class @@ -97,8 +95,8 @@ void VKQueryCache::Reserve(QueryType type, std::pair query) { HostCounter::HostCounter(VKQueryCache& cache_, std::shared_ptr dependency_, QueryType type_) - : HostCounterBase{std::move(dependency_)}, cache{cache_}, - type{type_}, query{cache_.AllocateQuery(type_)}, tick{cache_.Scheduler().CurrentTick()} { + : HostCounterBase{std::move(dependency_)}, cache{cache_}, type{type_}, + query{cache_.AllocateQuery(type_)}, tick{cache_.Scheduler().CurrentTick()} { const vk::Device* logical = &cache_.Device().GetLogical(); cache_.Scheduler().Record([logical, query = query](vk::CommandBuffer cmdbuf) { logical->ResetQueryPoolEXT(query.first, query.second, 1); @@ -119,18 +117,20 @@ u64 HostCounter::BlockingQuery() const { if (tick >= cache.Scheduler().CurrentTick()) { cache.Scheduler().Flush(); } + u64 data; - const VkResult result = cache.Device().GetLogical().GetQueryResults( + const VkResult query_result = cache.Device().GetLogical().GetQueryResults( query.first, query.second, 1, sizeof(data), &data, sizeof(data), VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WAIT_BIT); - switch (result) { + + switch (query_result) { case VK_SUCCESS: return data; case VK_ERROR_DEVICE_LOST: cache.Device().ReportLoss(); [[fallthrough]]; default: - throw vk::Exception(result); + throw vk::Exception(query_result); } } diff --git a/src/video_core/renderer_vulkan/vk_query_cache.h b/src/video_core/renderer_vulkan/vk_query_cache.h index 201fca888..837fe9ebf 100644 --- a/src/video_core/renderer_vulkan/vk_query_cache.h +++ b/src/video_core/renderer_vulkan/vk_query_cache.h @@ -95,8 +95,8 @@ private: class CachedQuery : public VideoCommon::CachedQueryBase { public: - explicit CachedQuery(VKQueryCache&, VideoCore::QueryType, VAddr cpu_addr, u8* host_ptr) - : VideoCommon::CachedQueryBase{cpu_addr, host_ptr} {} + explicit CachedQuery(VKQueryCache&, VideoCore::QueryType, VAddr cpu_addr_, u8* host_ptr_) + : CachedQueryBase{cpu_addr_, host_ptr_} {} }; } // namespace Vulkan diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp index 560386081..f93986aab 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp +++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp @@ -128,12 +128,12 @@ Tegra::Texture::FullTextureInfo GetTextureInfo(const Engine& engine, const Entry const u32 offset_2 = entry.secondary_offset; const u32 handle_1 = engine.AccessConstBuffer32(stage_type, buffer_1, offset_1); const u32 handle_2 = engine.AccessConstBuffer32(stage_type, buffer_2, offset_2); - return engine.GetTextureInfo(handle_1 | handle_2); + return engine.GetTextureInfo(Tegra::Texture::TextureHandle{handle_1 | handle_2}); } } if (entry.is_bindless) { const auto tex_handle = engine.AccessConstBuffer32(stage_type, entry.buffer, entry.offset); - return engine.GetTextureInfo(tex_handle); + return engine.GetTextureInfo(Tegra::Texture::TextureHandle{tex_handle}); } const auto& gpu_profile = engine.AccessGuestDriverProfile(); const u32 entry_offset = static_cast(index * gpu_profile.GetTextureHandlerSize()); @@ -380,12 +380,12 @@ void RasterizerVulkan::DrawParameters::Draw(vk::CommandBuffer cmdbuf) const { } } -RasterizerVulkan::RasterizerVulkan(Core::Frontend::EmuWindow& emu_window, Tegra::GPU& gpu_, +RasterizerVulkan::RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_, Tegra::MemoryManager& gpu_memory_, - Core::Memory::Memory& cpu_memory, VKScreenInfo& screen_info_, + Core::Memory::Memory& cpu_memory_, VKScreenInfo& screen_info_, const VKDevice& device_, VKMemoryManager& memory_manager_, StateTracker& state_tracker_, VKScheduler& scheduler_) - : RasterizerAccelerated(cpu_memory), gpu(gpu_), gpu_memory(gpu_memory_), + : RasterizerAccelerated(cpu_memory_), gpu(gpu_), gpu_memory(gpu_memory_), maxwell3d(gpu.Maxwell3D()), kepler_compute(gpu.KeplerCompute()), screen_info(screen_info_), device(device_), memory_manager(memory_manager_), state_tracker(state_tracker_), scheduler(scheduler_), staging_pool(device, memory_manager, scheduler), @@ -397,11 +397,11 @@ RasterizerVulkan::RasterizerVulkan(Core::Frontend::EmuWindow& emu_window, Tegra: texture_cache(*this, maxwell3d, gpu_memory, device, memory_manager, scheduler, staging_pool), pipeline_cache(*this, gpu, maxwell3d, kepler_compute, gpu_memory, device, scheduler, descriptor_pool, update_descriptor_queue, renderpass_cache), - buffer_cache(*this, gpu_memory, cpu_memory, device, memory_manager, scheduler, staging_pool), + buffer_cache(*this, gpu_memory, cpu_memory_, device, memory_manager, scheduler, staging_pool), sampler_cache(device), query_cache(*this, maxwell3d, gpu_memory, device, scheduler), fence_manager(*this, gpu, gpu_memory, texture_cache, buffer_cache, query_cache, device, scheduler), - wfi_event(device.GetLogical().CreateEvent()), async_shaders(emu_window) { + wfi_event(device.GetLogical().CreateEvent()), async_shaders(emu_window_) { scheduler.SetQueryCache(query_cache); if (device.UseAsynchronousShaders()) { async_shaders.AllocateWorkers(); diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h index 1789fb285..30ec58eb4 100644 --- a/src/video_core/renderer_vulkan/vk_rasterizer.h +++ b/src/video_core/renderer_vulkan/vk_rasterizer.h @@ -105,11 +105,11 @@ struct ImageView { class RasterizerVulkan final : public VideoCore::RasterizerAccelerated { public: - explicit RasterizerVulkan(Core::Frontend::EmuWindow& emu_window, Tegra::GPU& gpu, - Tegra::MemoryManager& gpu_memory, Core::Memory::Memory& cpu_memory, - VKScreenInfo& screen_info, const VKDevice& device, - VKMemoryManager& memory_manager, StateTracker& state_tracker, - VKScheduler& scheduler); + explicit RasterizerVulkan(Core::Frontend::EmuWindow& emu_window_, Tegra::GPU& gpu_, + Tegra::MemoryManager& gpu_memory_, Core::Memory::Memory& cpu_memory_, + VKScreenInfo& screen_info_, const VKDevice& device_, + VKMemoryManager& memory_manager_, StateTracker& state_tracker_, + VKScheduler& scheduler_); ~RasterizerVulkan() override; void Draw(bool is_indexed, bool is_instanced) override; diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp index 64649699f..1ff109880 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp @@ -489,12 +489,12 @@ VkImageView CachedSurfaceView::GetAttachment() { return *render_target; } -VKTextureCache::VKTextureCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::Engines::Maxwell3D& maxwell3d, - Tegra::MemoryManager& gpu_memory, const VKDevice& device_, +VKTextureCache::VKTextureCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_, const VKDevice& device_, VKMemoryManager& memory_manager_, VKScheduler& scheduler_, VKStagingBufferPool& staging_pool_) - : TextureCache(rasterizer, maxwell3d, gpu_memory, device_.IsOptimalAstcSupported()), + : TextureCache(rasterizer_, maxwell3d_, gpu_memory_, device_.IsOptimalAstcSupported()), device{device_}, memory_manager{memory_manager_}, scheduler{scheduler_}, staging_pool{ staging_pool_} {} diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.h b/src/video_core/renderer_vulkan/vk_texture_cache.h index 06880f228..1c632bd2c 100644 --- a/src/video_core/renderer_vulkan/vk_texture_cache.h +++ b/src/video_core/renderer_vulkan/vk_texture_cache.h @@ -193,10 +193,11 @@ private: class VKTextureCache final : public TextureCacheBase { public: - explicit VKTextureCache(VideoCore::RasterizerInterface& rasterizer, - Tegra::Engines::Maxwell3D& maxwell3d, Tegra::MemoryManager& gpu_memory, - const VKDevice& device, VKMemoryManager& memory_manager, - VKScheduler& scheduler, VKStagingBufferPool& staging_pool); + explicit VKTextureCache(VideoCore::RasterizerInterface& rasterizer_, + Tegra::Engines::Maxwell3D& maxwell3d_, + Tegra::MemoryManager& gpu_memory_, const VKDevice& device_, + VKMemoryManager& memory_manager_, VKScheduler& scheduler_, + VKStagingBufferPool& staging_pool_); ~VKTextureCache(); private: diff --git a/src/video_core/shader/ast.cpp b/src/video_core/shader/ast.cpp index 3f96d9076..cc2dbe36c 100644 --- a/src/video_core/shader/ast.cpp +++ b/src/video_core/shader/ast.cpp @@ -374,8 +374,8 @@ std::string ASTManager::Print() const { return printer.GetResult(); } -ASTManager::ASTManager(bool full_decompile, bool disable_else_derivation) - : full_decompile{full_decompile}, disable_else_derivation{disable_else_derivation} {}; +ASTManager::ASTManager(bool do_full_decompile, bool disable_else_derivation_) + : full_decompile{do_full_decompile}, disable_else_derivation{disable_else_derivation_} {} ASTManager::~ASTManager() { Clear(); diff --git a/src/video_core/shader/ast.h b/src/video_core/shader/ast.h index 8e5a22ab3..dc49b369e 100644 --- a/src/video_core/shader/ast.h +++ b/src/video_core/shader/ast.h @@ -76,7 +76,7 @@ public: class ASTIfThen { public: - explicit ASTIfThen(Expr condition) : condition{std::move(condition)} {} + explicit ASTIfThen(Expr condition_) : condition{std::move(condition_)} {} Expr condition; ASTZipper nodes{}; }; @@ -88,63 +88,68 @@ public: class ASTBlockEncoded { public: - explicit ASTBlockEncoded(u32 start, u32 end) : start{start}, end{end} {} + explicit ASTBlockEncoded(u32 start_, u32 _) : start{start_}, end{_} {} u32 start; u32 end; }; class ASTBlockDecoded { public: - explicit ASTBlockDecoded(NodeBlock&& new_nodes) : nodes(std::move(new_nodes)) {} + explicit ASTBlockDecoded(NodeBlock&& new_nodes_) : nodes(std::move(new_nodes_)) {} NodeBlock nodes; }; class ASTVarSet { public: - explicit ASTVarSet(u32 index, Expr condition) : index{index}, condition{std::move(condition)} {} + explicit ASTVarSet(u32 index_, Expr condition_) + : index{index_}, condition{std::move(condition_)} {} + u32 index; Expr condition; }; class ASTLabel { public: - explicit ASTLabel(u32 index) : index{index} {} + explicit ASTLabel(u32 index_) : index{index_} {} u32 index; bool unused{}; }; class ASTGoto { public: - explicit ASTGoto(Expr condition, u32 label) : condition{std::move(condition)}, label{label} {} + explicit ASTGoto(Expr condition_, u32 label_) + : condition{std::move(condition_)}, label{label_} {} + Expr condition; u32 label; }; class ASTDoWhile { public: - explicit ASTDoWhile(Expr condition) : condition{std::move(condition)} {} + explicit ASTDoWhile(Expr condition_) : condition{std::move(condition_)} {} Expr condition; ASTZipper nodes{}; }; class ASTReturn { public: - explicit ASTReturn(Expr condition, bool kills) - : condition{std::move(condition)}, kills{kills} {} + explicit ASTReturn(Expr condition_, bool kills_) + : condition{std::move(condition_)}, kills{kills_} {} + Expr condition; bool kills; }; class ASTBreak { public: - explicit ASTBreak(Expr condition) : condition{std::move(condition)} {} + explicit ASTBreak(Expr condition_) : condition{std::move(condition_)} {} Expr condition; }; class ASTBase { public: - explicit ASTBase(ASTNode parent, ASTData data) - : data{std::move(data)}, parent{std::move(parent)} {} + explicit ASTBase(ASTNode parent_, ASTData data_) + : data{std::move(data_)}, parent{std::move(parent_)} {} template static ASTNode Make(ASTNode parent, Args&&... args) { @@ -300,7 +305,7 @@ private: class ASTManager final { public: - ASTManager(bool full_decompile, bool disable_else_derivation); + explicit ASTManager(bool do_full_decompile, bool disable_else_derivation_); ~ASTManager(); ASTManager(const ASTManager& o) = delete; diff --git a/src/video_core/shader/async_shaders.cpp b/src/video_core/shader/async_shaders.cpp index 6920afdf2..78245473c 100644 --- a/src/video_core/shader/async_shaders.cpp +++ b/src/video_core/shader/async_shaders.cpp @@ -13,7 +13,7 @@ namespace VideoCommon::Shader { -AsyncShaders::AsyncShaders(Core::Frontend::EmuWindow& emu_window) : emu_window(emu_window) {} +AsyncShaders::AsyncShaders(Core::Frontend::EmuWindow& emu_window_) : emu_window(emu_window_) {} AsyncShaders::~AsyncShaders() { KillWorkers(); diff --git a/src/video_core/shader/async_shaders.h b/src/video_core/shader/async_shaders.h index 7a99e1dc5..5a7216019 100644 --- a/src/video_core/shader/async_shaders.h +++ b/src/video_core/shader/async_shaders.h @@ -66,7 +66,7 @@ public: Tegra::Engines::ShaderType shader_type; }; - explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window); + explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window_); ~AsyncShaders(); /// Start up shader worker threads diff --git a/src/video_core/shader/control_flow.cpp b/src/video_core/shader/control_flow.cpp index d656e0668..9120bf705 100644 --- a/src/video_core/shader/control_flow.cpp +++ b/src/video_core/shader/control_flow.cpp @@ -66,8 +66,8 @@ struct BlockInfo { }; struct CFGRebuildState { - explicit CFGRebuildState(const ProgramCode& program_code, u32 start, Registry& registry) - : program_code{program_code}, registry{registry}, start{start} {} + explicit CFGRebuildState(const ProgramCode& program_code_, u32 start_, Registry& registry_) + : program_code{program_code_}, registry{registry_}, start{start_} {} const ProgramCode& program_code; Registry& registry; diff --git a/src/video_core/shader/control_flow.h b/src/video_core/shader/control_flow.h index 62a3510d8..37bf96492 100644 --- a/src/video_core/shader/control_flow.h +++ b/src/video_core/shader/control_flow.h @@ -42,10 +42,10 @@ struct Condition { class SingleBranch { public: SingleBranch() = default; - SingleBranch(Condition condition, s32 address, bool kill, bool is_sync, bool is_brk, - bool ignore) - : condition{condition}, address{address}, kill{kill}, is_sync{is_sync}, is_brk{is_brk}, - ignore{ignore} {} + explicit SingleBranch(Condition condition_, s32 address_, bool kill_, bool is_sync_, + bool is_brk_, bool ignore_) + : condition{condition_}, address{address_}, kill{kill_}, is_sync{is_sync_}, is_brk{is_brk_}, + ignore{ignore_} {} bool operator==(const SingleBranch& b) const { return std::tie(condition, address, kill, is_sync, is_brk, ignore) == @@ -65,15 +65,15 @@ public: }; struct CaseBranch { - CaseBranch(u32 cmp_value, u32 address) : cmp_value{cmp_value}, address{address} {} + explicit CaseBranch(u32 cmp_value_, u32 address_) : cmp_value{cmp_value_}, address{address_} {} u32 cmp_value; u32 address; }; class MultiBranch { public: - MultiBranch(u32 gpr, std::vector&& branches) - : gpr{gpr}, branches{std::move(branches)} {} + explicit MultiBranch(u32 gpr_, std::vector&& branches_) + : gpr{gpr_}, branches{std::move(branches_)} {} u32 gpr{}; std::vector branches{}; diff --git a/src/video_core/shader/decode.cpp b/src/video_core/shader/decode.cpp index eeac328a6..c8f4da6df 100644 --- a/src/video_core/shader/decode.cpp +++ b/src/video_core/shader/decode.cpp @@ -66,7 +66,7 @@ std::optional TryDeduceSamplerSize(const Sampler& sampler_to_deduce, class ASTDecoder { public: - ASTDecoder(ShaderIR& ir) : ir(ir) {} + explicit ASTDecoder(ShaderIR& ir_) : ir(ir_) {} void operator()(ASTProgram& ast) { ASTNode current = ast.nodes.GetFirst(); diff --git a/src/video_core/shader/decode/arithmetic_integer.cpp b/src/video_core/shader/decode/arithmetic_integer.cpp index 73155966f..f32c3134b 100644 --- a/src/video_core/shader/decode/arithmetic_integer.cpp +++ b/src/video_core/shader/decode/arithmetic_integer.cpp @@ -258,7 +258,7 @@ u32 ShaderIR::DecodeArithmeticInteger(NodeBlock& bb, u32 pc) { case OpCode::Id::LEA_IMM: case OpCode::Id::LEA_RZ: case OpCode::Id::LEA_HI: { - auto [op_a, op_b, op_c] = [&]() -> std::tuple { + auto [op_a_, op_b_, op_c_] = [&]() -> std::tuple { switch (opcode->get().GetId()) { case OpCode::Id::LEA_R2: { return {GetRegister(instr.gpr20), GetRegister(instr.gpr39), @@ -294,8 +294,9 @@ u32 ShaderIR::DecodeArithmeticInteger(NodeBlock& bb, u32 pc) { UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast(Pred::UnusedIndex), "Unhandled LEA Predicate"); - Node value = Operation(OperationCode::ILogicalShiftLeft, std::move(op_a), std::move(op_c)); - value = Operation(OperationCode::IAdd, std::move(op_b), std::move(value)); + Node value = + Operation(OperationCode::ILogicalShiftLeft, std::move(op_a_), std::move(op_c_)); + value = Operation(OperationCode::IAdd, std::move(op_b_), std::move(value)); SetRegister(bb, instr.gpr0, std::move(value)); break; diff --git a/src/video_core/shader/expr.h b/src/video_core/shader/expr.h index 4e8264367..cda284c72 100644 --- a/src/video_core/shader/expr.h +++ b/src/video_core/shader/expr.h @@ -76,7 +76,7 @@ public: class ExprPredicate final { public: - explicit ExprPredicate(u32 predicate) : predicate{predicate} {} + explicit ExprPredicate(u32 predicate_) : predicate{predicate_} {} bool operator==(const ExprPredicate& b) const { return predicate == b.predicate; @@ -91,7 +91,7 @@ public: class ExprCondCode final { public: - explicit ExprCondCode(ConditionCode cc) : cc{cc} {} + explicit ExprCondCode(ConditionCode condition_code) : cc{condition_code} {} bool operator==(const ExprCondCode& b) const { return cc == b.cc; @@ -121,7 +121,7 @@ public: class ExprGprEqual final { public: - ExprGprEqual(u32 gpr, u32 value) : gpr{gpr}, value{value} {} + explicit ExprGprEqual(u32 gpr_, u32 value_) : gpr{gpr_}, value{value_} {} bool operator==(const ExprGprEqual& b) const { return gpr == b.gpr && value == b.value; diff --git a/src/video_core/shader/node.h b/src/video_core/shader/node.h index a1e2c4d8e..8db9e1de7 100644 --- a/src/video_core/shader/node.h +++ b/src/video_core/shader/node.h @@ -290,18 +290,18 @@ struct Sampler { is_buffer{is_buffer_}, is_indexed{is_indexed_} {} /// Separate sampler constructor - constexpr explicit Sampler(u32 index_, std::pair offsets, std::pair buffers, - Tegra::Shader::TextureType type, bool is_array_, bool is_shadow_, - bool is_buffer_) - : index{index_}, offset{offsets.first}, secondary_offset{offsets.second}, - buffer{buffers.first}, secondary_buffer{buffers.second}, type{type}, is_array{is_array_}, - is_shadow{is_shadow_}, is_buffer{is_buffer_}, is_separated{true} {} + constexpr explicit Sampler(u32 index_, std::pair offsets_, + std::pair buffers_, Tegra::Shader::TextureType type_, + bool is_array_, bool is_shadow_, bool is_buffer_) + : index{index_}, offset{offsets_.first}, secondary_offset{offsets_.second}, + buffer{buffers_.first}, secondary_buffer{buffers_.second}, type{type_}, + is_array{is_array_}, is_shadow{is_shadow_}, is_buffer{is_buffer_}, is_separated{true} {} /// Bindless samplers constructor constexpr explicit Sampler(u32 index_, u32 offset_, u32 buffer_, - Tegra::Shader::TextureType type, bool is_array_, bool is_shadow_, + Tegra::Shader::TextureType type_, bool is_array_, bool is_shadow_, bool is_buffer_, bool is_indexed_) - : index{index_}, offset{offset_}, buffer{buffer_}, type{type}, is_array{is_array_}, + : index{index_}, offset{offset_}, buffer{buffer_}, type{type_}, is_array{is_array_}, is_shadow{is_shadow_}, is_buffer{is_buffer_}, is_bindless{true}, is_indexed{is_indexed_} { } diff --git a/src/video_core/shader/shader_ir.cpp b/src/video_core/shader/shader_ir.cpp index 29d794b34..879088a27 100644 --- a/src/video_core/shader/shader_ir.cpp +++ b/src/video_core/shader/shader_ir.cpp @@ -25,9 +25,10 @@ using Tegra::Shader::PredCondition; using Tegra::Shader::PredOperation; using Tegra::Shader::Register; -ShaderIR::ShaderIR(const ProgramCode& program_code, u32 main_offset, CompilerSettings settings, - Registry& registry) - : program_code{program_code}, main_offset{main_offset}, settings{settings}, registry{registry} { +ShaderIR::ShaderIR(const ProgramCode& program_code_, u32 main_offset_, CompilerSettings settings_, + Registry& registry_) + : program_code{program_code_}, main_offset{main_offset_}, settings{settings_}, registry{ + registry_} { Decode(); PostDecode(); } diff --git a/src/video_core/shader/shader_ir.h b/src/video_core/shader/shader_ir.h index 3a98b2104..6aae14e34 100644 --- a/src/video_core/shader/shader_ir.h +++ b/src/video_core/shader/shader_ir.h @@ -29,8 +29,8 @@ struct ShaderBlock; constexpr u32 MAX_PROGRAM_LENGTH = 0x1000; struct ConstBuffer { - constexpr explicit ConstBuffer(u32 max_offset, bool is_indirect) - : max_offset{max_offset}, is_indirect{is_indirect} {} + constexpr explicit ConstBuffer(u32 max_offset_, bool is_indirect_) + : max_offset{max_offset_}, is_indirect{is_indirect_} {} constexpr ConstBuffer() = default; @@ -66,8 +66,8 @@ struct GlobalMemoryUsage { class ShaderIR final { public: - explicit ShaderIR(const ProgramCode& program_code, u32 main_offset, CompilerSettings settings, - Registry& registry); + explicit ShaderIR(const ProgramCode& program_code_, u32 main_offset_, + CompilerSettings settings_, Registry& registry_); ~ShaderIR(); const std::map& GetBasicBlocks() const { diff --git a/src/video_core/texture_cache/copy_params.h b/src/video_core/texture_cache/copy_params.h index 9c21a0649..5b475fe06 100644 --- a/src/video_core/texture_cache/copy_params.h +++ b/src/video_core/texture_cache/copy_params.h @@ -9,16 +9,16 @@ namespace VideoCommon { struct CopyParams { - constexpr CopyParams(u32 source_x, u32 source_y, u32 source_z, u32 dest_x, u32 dest_y, - u32 dest_z, u32 source_level, u32 dest_level, u32 width, u32 height, - u32 depth) - : source_x{source_x}, source_y{source_y}, source_z{source_z}, dest_x{dest_x}, - dest_y{dest_y}, dest_z{dest_z}, source_level{source_level}, - dest_level{dest_level}, width{width}, height{height}, depth{depth} {} + constexpr CopyParams(u32 source_x_, u32 source_y_, u32 source_z_, u32 dest_x_, u32 dest_y_, + u32 dest_z_, u32 source_level_, u32 dest_level_, u32 width_, u32 height_, + u32 depth_) + : source_x{source_x_}, source_y{source_y_}, source_z{source_z_}, dest_x{dest_x_}, + dest_y{dest_y_}, dest_z{dest_z_}, source_level{source_level_}, + dest_level{dest_level_}, width{width_}, height{height_}, depth{depth_} {} - constexpr CopyParams(u32 width, u32 height, u32 depth, u32 level) - : source_x{}, source_y{}, source_z{}, dest_x{}, dest_y{}, dest_z{}, source_level{level}, - dest_level{level}, width{width}, height{height}, depth{depth} {} + constexpr CopyParams(u32 width_, u32 height_, u32 depth_, u32 level_) + : source_x{}, source_y{}, source_z{}, dest_x{}, dest_y{}, dest_z{}, source_level{level_}, + dest_level{level_}, width{width_}, height{height_}, depth{depth_} {} u32 source_x; u32 source_y; diff --git a/src/video_core/texture_cache/format_lookup_table.cpp b/src/video_core/texture_cache/format_lookup_table.cpp index 7d5a75648..7938d71eb 100644 --- a/src/video_core/texture_cache/format_lookup_table.cpp +++ b/src/video_core/texture_cache/format_lookup_table.cpp @@ -24,12 +24,12 @@ constexpr bool C = false; // Normal color constexpr bool S = true; // Srgb struct Table { - constexpr Table(TextureFormat texture_format, bool is_srgb, ComponentType red_component, - ComponentType green_component, ComponentType blue_component, - ComponentType alpha_component, PixelFormat pixel_format) - : texture_format{texture_format}, pixel_format{pixel_format}, red_component{red_component}, - green_component{green_component}, blue_component{blue_component}, - alpha_component{alpha_component}, is_srgb{is_srgb} {} + constexpr Table(TextureFormat texture_format_, bool is_srgb_, ComponentType red_component_, + ComponentType green_component_, ComponentType blue_component_, + ComponentType alpha_component_, PixelFormat pixel_format_) + : texture_format{texture_format_}, pixel_format{pixel_format_}, + red_component{red_component_}, green_component{green_component_}, + blue_component{blue_component_}, alpha_component{alpha_component_}, is_srgb{is_srgb_} {} TextureFormat texture_format; PixelFormat pixel_format; diff --git a/src/video_core/texture_cache/surface_base.cpp b/src/video_core/texture_cache/surface_base.cpp index 42a1c0c6f..efbcf6723 100644 --- a/src/video_core/texture_cache/surface_base.cpp +++ b/src/video_core/texture_cache/surface_base.cpp @@ -25,11 +25,11 @@ StagingCache::StagingCache() = default; StagingCache::~StagingCache() = default; -SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params, - bool is_astc_supported) - : params{params}, gpu_addr{gpu_addr}, mipmap_sizes(params.num_levels), +SurfaceBaseImpl::SurfaceBaseImpl(GPUVAddr gpu_addr_, const SurfaceParams& params_, + bool is_astc_supported_) + : params{params_}, gpu_addr{gpu_addr_}, mipmap_sizes(params_.num_levels), mipmap_offsets(params.num_levels) { - is_converted = IsPixelFormatASTC(params.pixel_format) && !is_astc_supported; + is_converted = IsPixelFormatASTC(params.pixel_format) && !is_astc_supported_; host_memory_size = params.GetHostSizeInBytes(is_converted); std::size_t offset = 0; diff --git a/src/video_core/texture_cache/surface_base.h b/src/video_core/texture_cache/surface_base.h index cfcfa5b3a..b57135fe4 100644 --- a/src/video_core/texture_cache/surface_base.h +++ b/src/video_core/texture_cache/surface_base.h @@ -148,8 +148,8 @@ public: } protected: - explicit SurfaceBaseImpl(GPUVAddr gpu_addr, const SurfaceParams& params, - bool is_astc_supported); + explicit SurfaceBaseImpl(GPUVAddr gpu_addr_, const SurfaceParams& params_, + bool is_astc_supported_); ~SurfaceBaseImpl() = default; virtual void DecorateSurfaceName() = 0; @@ -297,9 +297,9 @@ public: } protected: - explicit SurfaceBase(const GPUVAddr gpu_addr, const SurfaceParams& params, - bool is_astc_supported) - : SurfaceBaseImpl(gpu_addr, params, is_astc_supported) {} + explicit SurfaceBase(const GPUVAddr gpu_addr_, const SurfaceParams& params_, + bool is_astc_supported_) + : SurfaceBaseImpl{gpu_addr_, params_, is_astc_supported_} {} ~SurfaceBase() = default; diff --git a/src/video_core/texture_cache/surface_view.h b/src/video_core/texture_cache/surface_view.h index 90a8bb0ae..199f72732 100644 --- a/src/video_core/texture_cache/surface_view.h +++ b/src/video_core/texture_cache/surface_view.h @@ -13,10 +13,10 @@ namespace VideoCommon { struct ViewParams { - constexpr explicit ViewParams(VideoCore::Surface::SurfaceTarget target, u32 base_layer, - u32 num_layers, u32 base_level, u32 num_levels) - : target{target}, base_layer{base_layer}, num_layers{num_layers}, base_level{base_level}, - num_levels{num_levels} {} + constexpr explicit ViewParams(VideoCore::Surface::SurfaceTarget target_, u32 base_layer_, + u32 num_layers_, u32 base_level_, u32 num_levels_) + : target{target_}, base_layer{base_layer_}, num_layers{num_layers_}, + base_level{base_level_}, num_levels{num_levels_} {} std::size_t Hash() const; @@ -44,7 +44,7 @@ struct ViewParams { class ViewBase { public: - constexpr explicit ViewBase(const ViewParams& params) : params{params} {} + constexpr explicit ViewBase(const ViewParams& view_params) : params{view_params} {} constexpr const ViewParams& GetViewParams() const { return params; diff --git a/src/video_core/textures/texture.h b/src/video_core/textures/texture.h index 0574fef12..bbc7e3eaf 100644 --- a/src/video_core/textures/texture.h +++ b/src/video_core/textures/texture.h @@ -146,7 +146,7 @@ enum class MsaaMode : u32 { }; union TextureHandle { - TextureHandle(u32 raw) : raw{raw} {} + /* implicit */ TextureHandle(u32 raw_) : raw{raw_} {} u32 raw; BitField<0, 20, u32> tic_id; -- cgit v1.2.3 From 8dbfa4e1a4af2cda4500304ba8fe1a1b09bbb814 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 27 Nov 2020 14:13:18 -0800 Subject: common: Port BitSet from Mesosphere. --- src/common/CMakeLists.txt | 1 + src/common/bit_set.h | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/common/bit_set.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 56c7e21f5..fc2ed9999 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -104,6 +104,7 @@ add_library(common STATIC detached_tasks.h bit_cast.h bit_field.h + bit_set.h bit_util.h cityhash.cpp cityhash.h diff --git a/src/common/bit_set.h b/src/common/bit_set.h new file mode 100644 index 000000000..4f966de40 --- /dev/null +++ b/src/common/bit_set.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include "common/alignment.h" +#include "common/bit_util.h" +#include "common/common_types.h" + +namespace Common { + +namespace impl { + +#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT) + +template +class BitSet { +private: + static_assert(std::is_integral::value); + static_assert(std::is_unsigned::value); + static_assert(sizeof(Storage) <= sizeof(u64)); + + static constexpr size_t FlagsPerWord = BITSIZEOF(Storage); + static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; + + static constexpr auto CountLeadingZeroImpl(Storage word) { + return CountLeadingZeroes64(static_cast(word)) - + (BITSIZEOF(unsigned long long) - FlagsPerWord); + } + + static constexpr Storage GetBitMask(size_t bit) { + return Storage(1) << (FlagsPerWord - 1 - bit); + } + +private: + Storage words[NumWords]; + +public: + constexpr BitSet() : words() { /* ... */ + } + + constexpr void SetBit(size_t i) { + this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord); + } + + constexpr void ClearBit(size_t i) { + this->words[i / FlagsPerWord] &= ~GetBitMask(i % FlagsPerWord); + } + + constexpr size_t CountLeadingZero() const { + for (size_t i = 0; i < NumWords; i++) { + if (this->words[i]) { + return FlagsPerWord * i + CountLeadingZeroImpl(this->words[i]); + } + } + return FlagsPerWord * NumWords; + } + + constexpr size_t GetNextSet(size_t n) const { + for (size_t i = (n + 1) / FlagsPerWord; i < NumWords; i++) { + Storage word = this->words[i]; + if (!IsAligned(n + 1, FlagsPerWord)) { + word &= GetBitMask(n % FlagsPerWord) - 1; + } + if (word) { + return FlagsPerWord * i + CountLeadingZeroImpl(word); + } + } + return FlagsPerWord * NumWords; + } +}; + +} // namespace impl + +template +using BitSet8 = impl::BitSet; + +template +using BitSet16 = impl::BitSet; + +template +using BitSet32 = impl::BitSet; + +template +using BitSet64 = impl::BitSet; + +} // namespace Common -- cgit v1.2.3 From 8d3e06349e12e7de17c334619f1f986792d1de4b Mon Sep 17 00:00:00 2001 From: bunnei Date: Thu, 3 Dec 2020 16:43:18 -0800 Subject: hle: kernel: Separate KScheduler from GlobalSchedulerContext class. --- src/common/CMakeLists.txt | 1 - src/common/multi_level_queue.h | 345 ----------------------- src/core/CMakeLists.txt | 2 + src/core/hle/kernel/global_scheduler_context.cpp | 55 ++++ src/core/hle/kernel/global_scheduler_context.h | 79 ++++++ src/core/hle/kernel/k_scheduler.cpp | 48 +--- src/core/hle/kernel/k_scheduler.h | 74 +---- src/tests/CMakeLists.txt | 1 - src/tests/common/multi_level_queue.cpp | 55 ---- 9 files changed, 140 insertions(+), 520 deletions(-) delete mode 100644 src/common/multi_level_queue.h create mode 100644 src/core/hle/kernel/global_scheduler_context.cpp create mode 100644 src/core/hle/kernel/global_scheduler_context.h delete mode 100644 src/tests/common/multi_level_queue.cpp (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index fc2ed9999..8e51104a1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -141,7 +141,6 @@ add_library(common STATIC microprofile.h microprofileui.h misc.cpp - multi_level_queue.h page_table.cpp page_table.h param_package.cpp diff --git a/src/common/multi_level_queue.h b/src/common/multi_level_queue.h deleted file mode 100644 index 4b305bf40..000000000 --- a/src/common/multi_level_queue.h +++ /dev/null @@ -1,345 +0,0 @@ -// Copyright 2019 TuxSH -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include -#include - -#include "common/bit_util.h" -#include "common/common_types.h" - -namespace Common { - -/** - * A MultiLevelQueue is a type of priority queue which has the following characteristics: - * - iteratable through each of its elements. - * - back can be obtained. - * - O(1) add, lookup (both front and back) - * - discrete priorities and a max of 64 priorities (limited domain) - * This type of priority queue is normaly used for managing threads within an scheduler - */ -template -class MultiLevelQueue { -public: - using value_type = T; - using reference = value_type&; - using const_reference = const value_type&; - using pointer = value_type*; - using const_pointer = const value_type*; - - using difference_type = typename std::pointer_traits::difference_type; - using size_type = std::size_t; - - template - class iterator_impl { - public: - using iterator_category = std::bidirectional_iterator_tag; - using value_type = T; - using pointer = std::conditional_t; - using reference = std::conditional_t; - using difference_type = typename std::pointer_traits::difference_type; - - friend bool operator==(const iterator_impl& lhs, const iterator_impl& rhs) { - if (lhs.IsEnd() && rhs.IsEnd()) - return true; - return std::tie(lhs.current_priority, lhs.it) == std::tie(rhs.current_priority, rhs.it); - } - - friend bool operator!=(const iterator_impl& lhs, const iterator_impl& rhs) { - return !operator==(lhs, rhs); - } - - reference operator*() const { - return *it; - } - - pointer operator->() const { - return it.operator->(); - } - - iterator_impl& operator++() { - if (IsEnd()) { - return *this; - } - - ++it; - - if (it == GetEndItForPrio()) { - u64 prios = mlq.used_priorities; - prios &= ~((1ULL << (current_priority + 1)) - 1); - if (prios == 0) { - current_priority = static_cast(mlq.depth()); - } else { - current_priority = CountTrailingZeroes64(prios); - it = GetBeginItForPrio(); - } - } - return *this; - } - - iterator_impl& operator--() { - if (IsEnd()) { - if (mlq.used_priorities != 0) { - current_priority = 63 - CountLeadingZeroes64(mlq.used_priorities); - it = GetEndItForPrio(); - --it; - } - } else if (it == GetBeginItForPrio()) { - u64 prios = mlq.used_priorities; - prios &= (1ULL << current_priority) - 1; - if (prios != 0) { - current_priority = CountTrailingZeroes64(prios); - it = GetEndItForPrio(); - --it; - } - } else { - --it; - } - return *this; - } - - iterator_impl operator++(int) { - const iterator_impl v{*this}; - ++(*this); - return v; - } - - iterator_impl operator--(int) { - const iterator_impl v{*this}; - --(*this); - return v; - } - - // allow implicit const->non-const - iterator_impl(const iterator_impl& other) - : mlq(other.mlq), it(other.it), current_priority(other.current_priority) {} - - iterator_impl(const iterator_impl& other) - : mlq(other.mlq), it(other.it), current_priority(other.current_priority) {} - - iterator_impl& operator=(const iterator_impl& other) { - mlq = other.mlq; - it = other.it; - current_priority = other.current_priority; - return *this; - } - - friend class iterator_impl; - iterator_impl() = default; - - private: - friend class MultiLevelQueue; - using container_ref = - std::conditional_t; - using list_iterator = std::conditional_t::const_iterator, - typename std::list::iterator>; - - explicit iterator_impl(container_ref mlq, list_iterator it, u32 current_priority) - : mlq(mlq), it(it), current_priority(current_priority) {} - explicit iterator_impl(container_ref mlq, u32 current_priority) - : mlq(mlq), it(), current_priority(current_priority) {} - - bool IsEnd() const { - return current_priority == mlq.depth(); - } - - list_iterator GetBeginItForPrio() const { - return mlq.levels[current_priority].begin(); - } - - list_iterator GetEndItForPrio() const { - return mlq.levels[current_priority].end(); - } - - container_ref mlq; - list_iterator it; - u32 current_priority; - }; - - using iterator = iterator_impl; - using const_iterator = iterator_impl; - - void add(const T& element, u32 priority, bool send_back = true) { - if (send_back) - levels[priority].push_back(element); - else - levels[priority].push_front(element); - used_priorities |= 1ULL << priority; - } - - void remove(const T& element, u32 priority) { - auto it = ListIterateTo(levels[priority], element); - if (it == levels[priority].end()) - return; - levels[priority].erase(it); - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void adjust(const T& element, u32 old_priority, u32 new_priority, bool adjust_front = false) { - remove(element, old_priority); - add(element, new_priority, !adjust_front); - } - void adjust(const_iterator it, u32 old_priority, u32 new_priority, bool adjust_front = false) { - adjust(*it, old_priority, new_priority, adjust_front); - } - - void transfer_to_front(const T& element, u32 priority, MultiLevelQueue& other) { - ListSplice(other.levels[priority], other.levels[priority].begin(), levels[priority], - ListIterateTo(levels[priority], element)); - - other.used_priorities |= 1ULL << priority; - - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void transfer_to_front(const_iterator it, u32 priority, MultiLevelQueue& other) { - transfer_to_front(*it, priority, other); - } - - void transfer_to_back(const T& element, u32 priority, MultiLevelQueue& other) { - ListSplice(other.levels[priority], other.levels[priority].end(), levels[priority], - ListIterateTo(levels[priority], element)); - - other.used_priorities |= 1ULL << priority; - - if (levels[priority].empty()) { - used_priorities &= ~(1ULL << priority); - } - } - - void transfer_to_back(const_iterator it, u32 priority, MultiLevelQueue& other) { - transfer_to_back(*it, priority, other); - } - - void yield(u32 priority, std::size_t n = 1) { - ListShiftForward(levels[priority], n); - } - - [[nodiscard]] std::size_t depth() const { - return Depth; - } - - [[nodiscard]] std::size_t size(u32 priority) const { - return levels[priority].size(); - } - - [[nodiscard]] std::size_t size() const { - u64 priorities = used_priorities; - std::size_t size = 0; - while (priorities != 0) { - const u64 current_priority = CountTrailingZeroes64(priorities); - size += levels[current_priority].size(); - priorities &= ~(1ULL << current_priority); - } - return size; - } - - [[nodiscard]] bool empty() const { - return used_priorities == 0; - } - - [[nodiscard]] bool empty(u32 priority) const { - return (used_priorities & (1ULL << priority)) == 0; - } - - [[nodiscard]] u32 highest_priority_set(u32 max_priority = 0) const { - const u64 priorities = - max_priority == 0 ? used_priorities : (used_priorities & ~((1ULL << max_priority) - 1)); - return priorities == 0 ? Depth : static_cast(CountTrailingZeroes64(priorities)); - } - - [[nodiscard]] u32 lowest_priority_set(u32 min_priority = Depth - 1) const { - const u64 priorities = min_priority >= Depth - 1 - ? used_priorities - : (used_priorities & ((1ULL << (min_priority + 1)) - 1)); - return priorities == 0 ? Depth : 63 - CountLeadingZeroes64(priorities); - } - - [[nodiscard]] const_iterator cbegin(u32 max_prio = 0) const { - const u32 priority = highest_priority_set(max_prio); - return priority == Depth ? cend() - : const_iterator{*this, levels[priority].cbegin(), priority}; - } - [[nodiscard]] const_iterator begin(u32 max_prio = 0) const { - return cbegin(max_prio); - } - [[nodiscard]] iterator begin(u32 max_prio = 0) { - const u32 priority = highest_priority_set(max_prio); - return priority == Depth ? end() : iterator{*this, levels[priority].begin(), priority}; - } - - [[nodiscard]] const_iterator cend(u32 min_prio = Depth - 1) const { - return min_prio == Depth - 1 ? const_iterator{*this, Depth} : cbegin(min_prio + 1); - } - [[nodiscard]] const_iterator end(u32 min_prio = Depth - 1) const { - return cend(min_prio); - } - [[nodiscard]] iterator end(u32 min_prio = Depth - 1) { - return min_prio == Depth - 1 ? iterator{*this, Depth} : begin(min_prio + 1); - } - - [[nodiscard]] T& front(u32 max_priority = 0) { - const u32 priority = highest_priority_set(max_priority); - return levels[priority == Depth ? 0 : priority].front(); - } - [[nodiscard]] const T& front(u32 max_priority = 0) const { - const u32 priority = highest_priority_set(max_priority); - return levels[priority == Depth ? 0 : priority].front(); - } - - [[nodiscard]] T& back(u32 min_priority = Depth - 1) { - const u32 priority = lowest_priority_set(min_priority); // intended - return levels[priority == Depth ? 63 : priority].back(); - } - [[nodiscard]] const T& back(u32 min_priority = Depth - 1) const { - const u32 priority = lowest_priority_set(min_priority); // intended - return levels[priority == Depth ? 63 : priority].back(); - } - - void clear() { - used_priorities = 0; - for (std::size_t i = 0; i < Depth; i++) { - levels[i].clear(); - } - } - -private: - using const_list_iterator = typename std::list::const_iterator; - - static void ListShiftForward(std::list& list, const std::size_t shift = 1) { - if (shift >= list.size()) { - return; - } - - const auto begin_range = list.begin(); - const auto end_range = std::next(begin_range, shift); - list.splice(list.end(), list, begin_range, end_range); - } - - static void ListSplice(std::list& in_list, const_list_iterator position, - std::list& out_list, const_list_iterator element) { - in_list.splice(position, out_list, element); - } - - [[nodiscard]] static const_list_iterator ListIterateTo(const std::list& list, - const T& element) { - auto it = list.cbegin(); - while (it != list.cend() && *it != element) { - ++it; - } - return it; - } - - std::array, Depth> levels; - u64 used_priorities = 0; -}; - -} // namespace Common diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 662839ff8..ee61f22c0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -148,6 +148,8 @@ add_library(core STATIC hle/kernel/code_set.cpp hle/kernel/code_set.h hle/kernel/errors.h + hle/kernel/global_scheduler_context.cpp + hle/kernel/global_scheduler_context.h hle/kernel/handle_table.cpp hle/kernel/handle_table.h hle/kernel/hle_ipc.cpp diff --git a/src/core/hle/kernel/global_scheduler_context.cpp b/src/core/hle/kernel/global_scheduler_context.cpp new file mode 100644 index 000000000..40e9adf47 --- /dev/null +++ b/src/core/hle/kernel/global_scheduler_context.cpp @@ -0,0 +1,55 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include + +#include "common/assert.h" +#include "core/core.h" +#include "core/hle/kernel/global_scheduler_context.h" +#include "core/hle/kernel/k_scheduler.h" +#include "core/hle/kernel/kernel.h" + +namespace Kernel { + +GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel) + : kernel{kernel}, scheduler_lock{kernel} {} + +GlobalSchedulerContext::~GlobalSchedulerContext() = default; + +void GlobalSchedulerContext::AddThread(std::shared_ptr thread) { + std::scoped_lock lock{global_list_guard}; + thread_list.push_back(std::move(thread)); +} + +void GlobalSchedulerContext::RemoveThread(std::shared_ptr thread) { + std::scoped_lock lock{global_list_guard}; + thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread), + thread_list.end()); +} + +void GlobalSchedulerContext::PreemptThreads() { + // The priority levels at which the global scheduler preempts threads every 10 ms. They are + // ordered from Core 0 to Core 3. + std::array preemption_priorities = {59, 59, 59, 63}; + + ASSERT(IsLocked()); + for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { + const u32 priority = preemption_priorities[core_id]; + kernel.Scheduler(core_id).RotateScheduledQueue(core_id, priority); + } +} + +bool GlobalSchedulerContext::IsLocked() const { + return scheduler_lock.IsLockedByCurrentThread(); +} + +void GlobalSchedulerContext::Lock() { + scheduler_lock.Lock(); +} + +void GlobalSchedulerContext::Unlock() { + scheduler_lock.Unlock(); +} + +} // namespace Kernel diff --git a/src/core/hle/kernel/global_scheduler_context.h b/src/core/hle/kernel/global_scheduler_context.h new file mode 100644 index 000000000..40fe44cc0 --- /dev/null +++ b/src/core/hle/kernel/global_scheduler_context.h @@ -0,0 +1,79 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include + +#include "common/common_types.h" +#include "common/spin_lock.h" +#include "core/hardware_properties.h" +#include "core/hle/kernel/k_priority_queue.h" +#include "core/hle/kernel/k_scheduler_lock.h" +#include "core/hle/kernel/thread.h" + +namespace Kernel { + +class KernelCore; +class SchedulerLock; + +using KSchedulerPriorityQueue = + KPriorityQueue; +static constexpr s32 HighestCoreMigrationAllowedPriority = 2; + +class GlobalSchedulerContext final { + friend class KScheduler; + +public: + explicit GlobalSchedulerContext(KernelCore& kernel); + ~GlobalSchedulerContext(); + + /// Adds a new thread to the scheduler + void AddThread(std::shared_ptr thread); + + /// Removes a thread from the scheduler + void RemoveThread(std::shared_ptr thread); + + /// Returns a list of all threads managed by the scheduler + const std::vector>& GetThreadList() const { + return thread_list; + } + + /** + * Rotates the scheduling queues of threads at a preemption priority and then does + * some core rebalancing. Preemption priorities can be found in the array + * 'preemption_priorities'. + * + * @note This operation happens every 10ms. + */ + void PreemptThreads(); + + /// Returns true if the global scheduler lock is acquired + bool IsLocked() const; + +private: + friend class SchedulerLock; + + /// Lock the scheduler to the current thread. + void Lock(); + + /// Unlocks the scheduler, reselects threads, interrupts cores for rescheduling + /// and reschedules current core if needed. + void Unlock(); + + using LockType = KAbstractSchedulerLock; + + KernelCore& kernel; + + std::atomic_bool scheduler_update_needed{}; + KSchedulerPriorityQueue priority_queue; + LockType scheduler_lock; + + /// Lists all thread ids that aren't deleted/etc. + std::vector> thread_list; + Common::SpinLock global_list_guard{}; +}; + +} // namespace Kernel diff --git a/src/core/hle/kernel/k_scheduler.cpp b/src/core/hle/kernel/k_scheduler.cpp index 7f7da610d..c7e2eabd4 100644 --- a/src/core/hle/kernel/k_scheduler.cpp +++ b/src/core/hle/kernel/k_scheduler.cpp @@ -5,12 +5,6 @@ // This file references various implementation details from Atmosphere, an open-source firmware for // the Nintendo Switch. Copyright 2018-2020 Atmosphere-NX. -#include -#include -#include -#include -#include - #include "common/assert.h" #include "common/bit_util.h" #include "common/fiber.h" @@ -19,10 +13,10 @@ #include "core/core.h" #include "core/core_timing.h" #include "core/cpu_manager.h" +#include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/physical_core.h" #include "core/hle/kernel/process.h" -#include "core/hle/kernel/k_scheduler.h" #include "core/hle/kernel/thread.h" #include "core/hle/kernel/time_manager.h" @@ -34,11 +28,6 @@ static void IncrementScheduledCount(Kernel::Thread* thread) { } } -GlobalSchedulerContext::GlobalSchedulerContext(KernelCore& kernel) - : kernel{kernel}, scheduler_lock{kernel} {} - -GlobalSchedulerContext::~GlobalSchedulerContext() = default; - /*static*/ void KScheduler::RescheduleCores(KernelCore& kernel, u64 cores_pending_reschedule, Core::EmuThreadHandle global_thread) { u32 current_core = global_thread.host_handle; @@ -205,33 +194,6 @@ u64 KScheduler::UpdateHighestPriorityThread(Thread* highest_thread) { return cores_needing_scheduling; } -void GlobalSchedulerContext::AddThread(std::shared_ptr thread) { - std::scoped_lock lock{global_list_guard}; - thread_list.push_back(std::move(thread)); -} - -void GlobalSchedulerContext::RemoveThread(std::shared_ptr thread) { - std::scoped_lock lock{global_list_guard}; - thread_list.erase(std::remove(thread_list.begin(), thread_list.end(), thread), - thread_list.end()); -} - -void GlobalSchedulerContext::PreemptThreads() { - // The priority levels at which the global scheduler preempts threads every 10 ms. They are - // ordered from Core 0 to Core 3. - std::array preemption_priorities = {59, 59, 59, 63}; - - ASSERT(IsLocked()); - for (u32 core_id = 0; core_id < Core::Hardware::NUM_CPU_CORES; core_id++) { - const u32 priority = preemption_priorities[core_id]; - kernel.Scheduler(core_id).RotateScheduledQueue(core_id, priority); - } -} - -bool GlobalSchedulerContext::IsLocked() const { - return scheduler_lock.IsLockedByCurrentThread(); -} - /*static*/ void KScheduler::OnThreadStateChanged(KernelCore& kernel, Thread* thread, u32 old_state) { ASSERT(kernel.GlobalSchedulerContext().IsLocked()); @@ -635,14 +597,6 @@ void KScheduler::YieldToAnyThread() { } } -void GlobalSchedulerContext::Lock() { - scheduler_lock.Lock(); -} - -void GlobalSchedulerContext::Unlock() { - scheduler_lock.Unlock(); -} - KScheduler::KScheduler(Core::System& system, std::size_t core_id) : system(system), core_id(core_id) { switch_fiber = std::make_shared(std::function(OnSwitch), this); diff --git a/src/core/hle/kernel/k_scheduler.h b/src/core/hle/kernel/k_scheduler.h index 535ee34b9..7f020d96e 100644 --- a/src/core/hle/kernel/k_scheduler.h +++ b/src/core/hle/kernel/k_scheduler.h @@ -8,94 +8,27 @@ #pragma once #include -#include -#include -#include #include "common/common_types.h" -#include "common/multi_level_queue.h" -#include "common/scope_exit.h" #include "common/spin_lock.h" -#include "core/core_timing.h" -#include "core/hardware_properties.h" +#include "core/hle/kernel/global_scheduler_context.h" #include "core/hle/kernel/k_priority_queue.h" #include "core/hle/kernel/k_scheduler_lock.h" -#include "core/hle/kernel/thread.h" namespace Common { class Fiber; } namespace Core { -class ARM_Interface; class System; -} // namespace Core +} namespace Kernel { class KernelCore; class Process; class SchedulerLock; - -using KSchedulerPriorityQueue = - KPriorityQueue; -static constexpr s32 HighestCoreMigrationAllowedPriority = 2; - -class GlobalSchedulerContext final { - friend class KScheduler; - -public: - explicit GlobalSchedulerContext(KernelCore& kernel); - ~GlobalSchedulerContext(); - - /// Adds a new thread to the scheduler - void AddThread(std::shared_ptr thread); - - /// Removes a thread from the scheduler - void RemoveThread(std::shared_ptr thread); - - /// Returns a list of all threads managed by the scheduler - const std::vector>& GetThreadList() const { - return thread_list; - } - - /** - * Rotates the scheduling queues of threads at a preemption priority and then does - * some core rebalancing. Preemption priorities can be found in the array - * 'preemption_priorities'. - * - * @note This operation happens every 10ms. - */ - void PreemptThreads(); - - u32 CpuCoresCount() const { - return Core::Hardware::NUM_CPU_CORES; - } - - bool IsLocked() const; - -private: - friend class SchedulerLock; - - /// Lock the scheduler to the current thread. - void Lock(); - - /// Unlocks the scheduler, reselects threads, interrupts cores for rescheduling - /// and reschedules current core if needed. - void Unlock(); - - using LockType = KAbstractSchedulerLock; - - KernelCore& kernel; - - std::atomic_bool scheduler_update_needed{}; - KSchedulerPriorityQueue priority_queue; - LockType scheduler_lock; - - /// Lists all thread ids that aren't deleted/etc. - std::vector> thread_list; - Common::SpinLock global_list_guard{}; -}; +class Thread; class KScheduler final { public: @@ -221,7 +154,6 @@ private: /// Switches the CPU's active thread context to that of the specified thread void ScheduleImpl(); - void SwitchThread(Thread* next_thread); /// When a thread wakes up, it must run this through it's new scheduler void SwitchContextStep2(); diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index 47ef30aa9..d80b0b688 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -2,7 +2,6 @@ add_executable(tests common/bit_field.cpp common/bit_utils.cpp common/fibers.cpp - common/multi_level_queue.cpp common/param_package.cpp common/ring_buffer.cpp core/arm/arm_test_common.cpp diff --git a/src/tests/common/multi_level_queue.cpp b/src/tests/common/multi_level_queue.cpp deleted file mode 100644 index cca7ec7da..000000000 --- a/src/tests/common/multi_level_queue.cpp +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2019 Yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include -#include "common/common_types.h" -#include "common/multi_level_queue.h" - -namespace Common { - -TEST_CASE("MultiLevelQueue", "[common]") { - std::array values = {0.0, 5.0, 1.0, 9.0, 8.0, 2.0, 6.0, 7.0}; - Common::MultiLevelQueue mlq; - REQUIRE(mlq.empty()); - mlq.add(values[2], 2); - mlq.add(values[7], 7); - mlq.add(values[3], 3); - mlq.add(values[4], 4); - mlq.add(values[0], 0); - mlq.add(values[5], 5); - mlq.add(values[6], 6); - mlq.add(values[1], 1); - u32 index = 0; - bool all_set = true; - for (auto& f : mlq) { - all_set &= (f == values[index]); - index++; - } - REQUIRE(all_set); - REQUIRE(!mlq.empty()); - f32 v = 8.0; - mlq.add(v, 2); - v = -7.0; - mlq.add(v, 2, false); - REQUIRE(mlq.front(2) == -7.0); - mlq.yield(2); - REQUIRE(mlq.front(2) == values[2]); - REQUIRE(mlq.back(2) == -7.0); - REQUIRE(mlq.empty(8)); - v = 10.0; - mlq.add(v, 8); - mlq.adjust(v, 8, 9); - REQUIRE(mlq.front(9) == v); - REQUIRE(mlq.empty(8)); - REQUIRE(!mlq.empty(9)); - mlq.adjust(values[0], 0, 9); - REQUIRE(mlq.highest_priority_set() == 1); - REQUIRE(mlq.lowest_priority_set() == 9); - mlq.remove(values[1], 1); - REQUIRE(mlq.highest_priority_set() == 2); - REQUIRE(mlq.empty(1)); -} - -} // namespace Common -- cgit v1.2.3 From d2c0c94f0b76fbae9dd5a7b7cb81b9b53db8be0e Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 4 Dec 2020 23:46:37 -0800 Subject: common: BitSet: Various style fixes based on code review feedback. --- src/common/bit_set.h | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'src/common') diff --git a/src/common/bit_set.h b/src/common/bit_set.h index 4f966de40..9235ad412 100644 --- a/src/common/bit_set.h +++ b/src/common/bit_set.h @@ -16,6 +16,9 @@ #pragma once +#include +#include + #include "common/alignment.h" #include "common/bit_util.h" #include "common/common_types.h" @@ -24,33 +27,11 @@ namespace Common { namespace impl { -#define BITSIZEOF(x) (sizeof(x) * CHAR_BIT) - template class BitSet { -private: - static_assert(std::is_integral::value); - static_assert(std::is_unsigned::value); - static_assert(sizeof(Storage) <= sizeof(u64)); - - static constexpr size_t FlagsPerWord = BITSIZEOF(Storage); - static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; - - static constexpr auto CountLeadingZeroImpl(Storage word) { - return CountLeadingZeroes64(static_cast(word)) - - (BITSIZEOF(unsigned long long) - FlagsPerWord); - } - - static constexpr Storage GetBitMask(size_t bit) { - return Storage(1) << (FlagsPerWord - 1 - bit); - } - -private: - Storage words[NumWords]; public: - constexpr BitSet() : words() { /* ... */ - } + constexpr BitSet() = default; constexpr void SetBit(size_t i) { this->words[i / FlagsPerWord] |= GetBitMask(i % FlagsPerWord); @@ -81,6 +62,24 @@ public: } return FlagsPerWord * NumWords; } + +private: + static_assert(std::is_unsigned_v); + static_assert(sizeof(Storage) <= sizeof(u64)); + + static constexpr size_t FlagsPerWord = BitSize(); + static constexpr size_t NumWords = AlignUp(N, FlagsPerWord) / FlagsPerWord; + + static constexpr auto CountLeadingZeroImpl(Storage word) { + return std::countl_zero(static_cast(word)) - + (BitSize() - FlagsPerWord); + } + + static constexpr Storage GetBitMask(size_t bit) { + return Storage(1) << (FlagsPerWord - 1 - bit); + } + + std::array words{}; }; } // namespace impl -- cgit v1.2.3 From 0e54aa17e64a5c5b62d60a70414742be29eccff9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 8 Dec 2020 18:36:49 -0500 Subject: file_util: Migrate Exists() and IsDirectory() over to std::filesystem Greatly simplifies our file-handling code for these functions. --- src/common/file_util.cpp | 61 +++++++----------------------------------------- src/common/file_util.h | 9 +++---- 2 files changed, 13 insertions(+), 57 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 18fbfa25b..67fcef3e4 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include +#include #include #include #include @@ -75,62 +76,16 @@ // The code still needs a ton of cleanup. // REMEMBER: strdup considered harmful! namespace Common::FS { +namespace fs = std::filesystem; -// Remove any ending forward slashes from directory paths -// Modifies argument. -static void StripTailDirSlashes(std::string& fname) { - if (fname.length() <= 1) { - return; - } - - std::size_t i = fname.length(); - while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { - --i; - } - fname.resize(i); +bool Exists(const fs::path& path) { + std::error_code ec; + return fs::exists(path, ec); } -bool Exists(const std::string& filename) { - struct stat file_info; - - std::string copy(filename); - StripTailDirSlashes(copy); - -#ifdef _WIN32 - // Windows needs a slash to identify a driver root - if (copy.size() != 0 && copy.back() == ':') - copy += DIR_SEP_CHR; - - int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); -#else - int result = stat(copy.c_str(), &file_info); -#endif - - return (result == 0); -} - -bool IsDirectory(const std::string& filename) { - struct stat file_info; - - std::string copy(filename); - StripTailDirSlashes(copy); - -#ifdef _WIN32 - // Windows needs a slash to identify a driver root - if (copy.size() != 0 && copy.back() == ':') - copy += DIR_SEP_CHR; - - int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); -#else - int result = stat(copy.c_str(), &file_info); -#endif - - if (result < 0) { - LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); - return false; - } - - return S_ISDIR(file_info.st_mode); +bool IsDirectory(const fs::path& path) { + std::error_code ec; + return fs::is_directory(path, ec); } bool Delete(const std::string& filename) { diff --git a/src/common/file_util.h b/src/common/file_util.h index 840cde2a6..be0906434 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -47,11 +48,11 @@ struct FSTEntry { std::vector children; }; -// Returns true if file filename exists -[[nodiscard]] bool Exists(const std::string& filename); +// Returns true if the exists +[[nodiscard]] bool Exists(const std::filesystem::path& path); -// Returns true if filename is a directory -[[nodiscard]] bool IsDirectory(const std::string& filename); +// Returns true if path is a directory +[[nodiscard]] bool IsDirectory(const std::filesystem::path& path); // Returns the size of filename (64bit) [[nodiscard]] u64 GetSize(const std::string& filename); -- cgit v1.2.3 From 20aad9e01af958b6c8bf7d18faebd498541651f5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 8 Dec 2020 18:51:04 -0500 Subject: file_util: Migrate remaining file handling functions over to std::filesystem Converts creation and deletion functions over to std::filesystem, simplifying our file-handling code. Notably with this, CopyDir will now function on Windows. --- src/common/file_util.cpp | 378 ++++++++++------------------------------------- src/common/file_util.h | 62 +++----- 2 files changed, 100 insertions(+), 340 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 67fcef3e4..d5f6ea2ee 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -68,13 +68,6 @@ #include #include -#ifndef S_ISDIR -#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) -#endif - -// This namespace has various generic functions related to files and paths. -// The code still needs a ton of cleanup. -// REMEMBER: strdup considered harmful! namespace Common::FS { namespace fs = std::filesystem; @@ -88,224 +81,89 @@ bool IsDirectory(const fs::path& path) { return fs::is_directory(path, ec); } -bool Delete(const std::string& filename) { - LOG_TRACE(Common_Filesystem, "file {}", filename); +bool Delete(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "file {}", path.string()); // Return true because we care about the file no // being there, not the actual delete. - if (!Exists(filename)) { - LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); + if (!Exists(path)) { + LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string()); return true; } - // We can't delete a directory - if (IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); - return false; - } + std::error_code ec; + return fs::remove(path, ec); +} -#ifdef _WIN32 - if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) { - LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg()); - return false; - } -#else - if (unlink(filename.c_str()) == -1) { - LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); +bool CreateDir(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "directory {}", path.string()); + + std::error_code ec; + const bool success = fs::create_directory(path, ec); + + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message()); return false; } -#endif return true; } -bool CreateDir(const std::string& path) { - LOG_TRACE(Common_Filesystem, "directory {}", path); -#ifdef _WIN32 - if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr)) - return true; - DWORD error = GetLastError(); - if (error == ERROR_ALREADY_EXISTS) { - LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path); - return true; - } - LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error); - return false; -#else - if (mkdir(path.c_str(), 0755) == 0) - return true; +bool CreateFullPath(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "path {}", path.string()); - int err = errno; + std::error_code ec; + const bool success = fs::create_directories(path, ec); - if (err == EEXIST) { - LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path); - return true; + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); + return false; } - LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err)); - return false; -#endif + return true; } -bool CreateFullPath(const std::string& fullPath) { - int panicCounter = 100; - LOG_TRACE(Common_Filesystem, "path {}", fullPath); - - if (Exists(fullPath)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); - return true; - } +bool Rename(const fs::path& src, const fs::path& dst) { + LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); - std::size_t position = 0; - while (true) { - // Find next sub path - position = fullPath.find(DIR_SEP_CHR, position); - - // we're done, yay! - if (position == fullPath.npos) - return true; - - // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") - std::string const subPath(fullPath.substr(0, position + 1)); - if (!IsDirectory(subPath) && !CreateDir(subPath)) { - LOG_ERROR(Common, "CreateFullPath: directory creation failed"); - return false; - } - - // A safety check - panicCounter--; - if (panicCounter <= 0) { - LOG_ERROR(Common, "CreateFullPath: directory structure is too deep"); - return false; - } - position++; - } -} - -bool DeleteDir(const std::string& filename) { - LOG_TRACE(Common_Filesystem, "directory {}", filename); + std::error_code ec; + fs::rename(src, dst, ec); - // check if a directory - if (!IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(), + dst.string(), ec.message()); return false; } -#ifdef _WIN32 - if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) - return true; -#else - if (rmdir(filename.c_str()) == 0) - return true; -#endif - LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); - - return false; -} - -bool Rename(const std::string& srcFilename, const std::string& destFilename) { - LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); -#ifdef _WIN32 - if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), - Common::UTF8ToUTF16W(destFilename).c_str()) == 0) - return true; -#else - if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) - return true; -#endif - LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, - GetLastErrorMsg()); - return false; + return true; } -bool Copy(const std::string& srcFilename, const std::string& destFilename) { - LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); -#ifdef _WIN32 - if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(), - Common::UTF8ToUTF16W(destFilename).c_str(), FALSE)) - return true; +bool Copy(const fs::path& src, const fs::path& dst) { + LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); - LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, - GetLastErrorMsg()); - return false; -#else - using CFilePointer = std::unique_ptr; - - // Open input file - CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose}; - if (!input) { - LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); - return false; - } + std::error_code ec; + const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec); - // open output file - CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose}; - if (!output) { - LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); + if (!success) { + LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(), + ec.message()); return false; } - // copy loop - std::array buffer; - while (!feof(input.get())) { - // read input - std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get()); - if (rnum != buffer.size()) { - if (ferror(input.get()) != 0) { - LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", - srcFilename, destFilename, GetLastErrorMsg()); - return false; - } - } - - // write output - std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get()); - if (wnum != rnum) { - LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, - destFilename, GetLastErrorMsg()); - return false; - } - } - return true; -#endif } -u64 GetSize(const std::string& filename) { - if (!Exists(filename)) { - LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); - return 0; - } +u64 GetSize(const fs::path& path) { + std::error_code ec; + const auto size = fs::file_size(path, ec); - if (IsDirectory(filename)) { - LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename); + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(), + ec.message()); return 0; } - struct stat buf; -#ifdef _WIN32 - if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) -#else - if (stat(filename.c_str(), &buf) == 0) -#endif - { - LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size); - return buf.st_size; - } - - LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg()); - return 0; -} - -u64 GetSize(const int fd) { - struct stat buf; - if (fstat(fd, &buf) != 0) { - LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg()); - return 0; - } - return buf.st_size; + return size; } u64 GetSize(FILE* f) { @@ -393,132 +251,58 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, return true; } -u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, - unsigned int recursion) { - const auto callback = [recursion, &parent_entry](u64* num_entries_out, - const std::string& directory, - const std::string& virtual_name) -> bool { - FSTEntry entry; - entry.virtualName = virtual_name; - entry.physicalName = directory + DIR_SEP + virtual_name; - - if (IsDirectory(entry.physicalName)) { - entry.isDirectory = true; - // is a directory, lets go inside if we didn't recurse to often - if (recursion > 0) { - entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1); - *num_entries_out += entry.size; - } else { - entry.size = 0; - } - } else { // is a file - entry.isDirectory = false; - entry.size = GetSize(entry.physicalName); - } - (*num_entries_out)++; - - // Push into the tree - parent_entry.children.push_back(std::move(entry)); - return true; - }; - - u64 num_entries; - return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; -} - -bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { - const auto callback = [recursion](u64*, const std::string& directory, - const std::string& virtual_name) { - const std::string new_path = directory + DIR_SEP_CHR + virtual_name; - - if (IsDirectory(new_path)) { - if (recursion == 0) { - return false; - } - return DeleteDirRecursively(new_path, recursion - 1); - } - return Delete(new_path); - }; +bool DeleteDirRecursively(const fs::path& path) { + std::error_code ec; + fs::remove_all(path, ec); - if (!ForeachDirectoryEntry(nullptr, directory, callback)) + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(), + ec.message()); return false; + } - // Delete the outermost directory - DeleteDir(directory); return true; } -void CopyDir([[maybe_unused]] const std::string& source_path, - [[maybe_unused]] const std::string& dest_path) { -#ifndef _WIN32 - if (source_path == dest_path) { - return; - } - if (!Exists(source_path)) { - return; - } - if (!Exists(dest_path)) { - CreateFullPath(dest_path); - } +void CopyDir(const fs::path& src, const fs::path& dst) { + constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive; + + std::error_code ec; + fs::copy(src, dst, copy_flags, ec); - DIR* dirp = opendir(source_path.c_str()); - if (!dirp) { + if (ec) { + LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(), + dst.string(), ec.message()); return; } - while (struct dirent* result = readdir(dirp)) { - const std::string virtualName(result->d_name); - // check for "." and ".." - if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || - ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { - continue; - } - - std::string source, dest; - source = source_path + virtualName; - dest = dest_path + virtualName; - if (IsDirectory(source)) { - source += '/'; - dest += '/'; - if (!Exists(dest)) { - CreateFullPath(dest); - } - CopyDir(source, dest); - } else if (!Exists(dest)) { - Copy(source, dest); - } - } - closedir(dirp); -#endif + LOG_TRACE(Common_Filesystem, "Successfully copied directory."); } -std::optional GetCurrentDir() { -// Get the current working directory (getcwd uses malloc) -#ifdef _WIN32 - wchar_t* dir = _wgetcwd(nullptr, 0); - if (!dir) { -#else - char* dir = getcwd(nullptr, 0); - if (!dir) { -#endif - LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); +std::optional GetCurrentDir() { + std::error_code ec; + auto path = fs::current_path(ec); + + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}", + ec.message()); return std::nullopt; } -#ifdef _WIN32 - std::string strDir = Common::UTF16ToUTF8(dir); -#else - std::string strDir = dir; -#endif - free(dir); - return strDir; + + return {std::move(path)}; } -bool SetCurrentDir(const std::string& directory) { -#ifdef _WIN32 - return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; -#else - return chdir(directory.c_str()) == 0; -#endif +bool SetCurrentDir(const fs::path& path) { + std::error_code ec; + fs::current_path(path, ec); + + if (ec) { + LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(), + ec.message()); + return false; + } + + return true; } #if defined(__APPLE__) diff --git a/src/common/file_util.h b/src/common/file_util.h index be0906434..c2ee7ca27 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -39,48 +39,34 @@ enum class UserPath { UserDir, }; -// FileSystem tree node/ -struct FSTEntry { - bool isDirectory; - u64 size; // file length or number of entries from children - std::string physicalName; // name on disk - std::string virtualName; // name in FST names table - std::vector children; -}; - -// Returns true if the exists +// Returns true if the path exists [[nodiscard]] bool Exists(const std::filesystem::path& path); // Returns true if path is a directory [[nodiscard]] bool IsDirectory(const std::filesystem::path& path); // Returns the size of filename (64bit) -[[nodiscard]] u64 GetSize(const std::string& filename); - -// Overloaded GetSize, accepts file descriptor -[[nodiscard]] u64 GetSize(int fd); +[[nodiscard]] u64 GetSize(const std::filesystem::path& path); // Overloaded GetSize, accepts FILE* [[nodiscard]] u64 GetSize(FILE* f); // Returns true if successful, or path already exists. -bool CreateDir(const std::string& filename); - -// Creates the full path of fullPath returns true on success -bool CreateFullPath(const std::string& fullPath); +bool CreateDir(const std::filesystem::path& path); -// Deletes a given filename, return true on success -// Doesn't supports deleting a directory -bool Delete(const std::string& filename); +// Creates the full path of path. Returns true on success +bool CreateFullPath(const std::filesystem::path& path); -// Deletes a directory filename, returns true on success -bool DeleteDir(const std::string& filename); +// Deletes a given file at the path. +// This will also delete empty directories. +// Return true on success +bool Delete(const std::filesystem::path& path); -// renames file srcFilename to destFilename, returns true on success -bool Rename(const std::string& srcFilename, const std::string& destFilename); +// Renames file src to dst, returns true on success +bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); -// copies file srcFilename to destFilename, returns true on success -bool Copy(const std::string& srcFilename, const std::string& destFilename); +// copies file src to dst, returns true on success +bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); // creates an empty file filename, returns true on success bool CreateEmptyFile(const std::string& filename); @@ -107,27 +93,17 @@ using DirectoryEntryCallable = std::function GetCurrentDir(); +[[nodiscard]] std::optional GetCurrentDir(); // Create directory and copy contents (does not overwrite existing files) -void CopyDir(const std::string& source_path, const std::string& dest_path); +void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); -// Set the current directory to given directory -bool SetCurrentDir(const std::string& directory); +// Set the current directory to given path +bool SetCurrentDir(const std::filesystem::path& path); // Returns a pointer to a string with a yuzu data dir in the user's home // directory. To be used in "multi-user" mode (that is, installed). -- cgit v1.2.3 From 52f13f2339f8c7259d2aa646346b29997b85c0ec Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 05:21:08 -0300 Subject: common/file_util: Succeed on CreateDir when the directory exists --- src/common/file_util.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d5f6ea2ee..c4d738bb6 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -98,6 +98,11 @@ bool Delete(const fs::path& path) { bool CreateDir(const fs::path& path) { LOG_TRACE(Common_Filesystem, "directory {}", path.string()); + if (Exists(path)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); + return true; + } + std::error_code ec; const bool success = fs::create_directory(path, ec); -- cgit v1.2.3 From 532983437616e15539b448594a360c138995e282 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 05:22:16 -0300 Subject: common/file_util: Fix and deprecate CreateFullPath, add CreateDirs Fix CreateFullPath to have its intended previous behavior (whatever that was), and deprecate it in favor of the new CreateDirs function. Unlike CreateDir, CreateDirs is marked as [[nodiscard]] to avoid new code ignoring its result value. --- src/common/file_util.cpp | 25 +++++++++++++++++++++++-- src/common/file_util.h | 10 ++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index c4d738bb6..7752c0421 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -114,20 +114,41 @@ bool CreateDir(const fs::path& path) { return true; } -bool CreateFullPath(const fs::path& path) { +bool CreateDirs(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path.string()); + if (Exists(path)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); + return true; + } + std::error_code ec; const bool success = fs::create_directories(path, ec); if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); + LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message()); return false; } return true; } +bool CreateFullPath(const fs::path& path) { + LOG_TRACE(Common_Filesystem, "path {}", path); + + // Removes trailing slashes and turns any '\' into '/' + const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); + + if (new_path.rfind('.') == std::string::npos) { + // The path is a directory + return CreateDirs(new_path); + } else { + // The path is a file + // Creates directory preceding the last '/' + return CreateDirs(new_path.substr(0, new_path.rfind('/'))); + } +} + bool Rename(const fs::path& src, const fs::path& dst) { LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..cf006cc9d 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -54,8 +54,14 @@ enum class UserPath { // Returns true if successful, or path already exists. bool CreateDir(const std::filesystem::path& path); -// Creates the full path of path. Returns true on success -bool CreateFullPath(const std::filesystem::path& path); +// Create all directories in path +// Returns true if successful, or path already exists. +[[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs( + const std::filesystem::path& path); + +// Creates directories in path. Returns true on success. +[[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath( + const std::filesystem::path& path); // Deletes a given file at the path. // This will also delete empty directories. -- cgit v1.2.3 From bab9cae71f5cd5a0078853e64bdf1d1f8af1fc30 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 9 Dec 2020 18:52:36 -0300 Subject: common/file_util: Let std::filesystem cast from UTF16 to std::string Fix invalid encoding paths when iterating over a directory on Windows. --- src/common/file_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7752c0421..a286b9341 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -239,7 +239,7 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, } // windows loop do { - const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName)); + const std::string virtual_name = std::filesystem::path(ffd.cFileName).string(); #else DIR* dirp = opendir(directory.c_str()); if (!dirp) -- cgit v1.2.3 From ec8548b414d39bf389ca55154e383ca5574344e7 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Wed, 9 Dec 2020 19:25:00 -0500 Subject: common/file_util: Simplify the behavior of CreateFullPath --- src/common/file_util.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a286b9341..8e061ff6c 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -136,16 +136,10 @@ bool CreateDirs(const fs::path& path) { bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path); - // Removes trailing slashes and turns any '\' into '/' - const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); - - if (new_path.rfind('.') == std::string::npos) { - // The path is a directory - return CreateDirs(new_path); + if (path.has_extension()) { + return CreateDirs(path.parent_path()); } else { - // The path is a file - // Creates directory preceding the last '/' - return CreateDirs(new_path.substr(0, new_path.rfind('/'))); + return CreateDirs(path); } } -- cgit v1.2.3 From ac3ec5ed132a8d8b63c4e95205521addb90fdd0f Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:21:21 -0500 Subject: Revert "Merge pull request #5181 from Morph1984/5174-review" This reverts commit cdb36aef9ec9d30bdef1953f9ed46776ae2f12af, reversing changes made to 5e9b77129f2cf8c039a8d98033cae4ac0f93f515. --- src/common/file_util.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 8e061ff6c..a286b9341 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -136,10 +136,16 @@ bool CreateDirs(const fs::path& path) { bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path); - if (path.has_extension()) { - return CreateDirs(path.parent_path()); + // Removes trailing slashes and turns any '\' into '/' + const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); + + if (new_path.rfind('.') == std::string::npos) { + // The path is a directory + return CreateDirs(new_path); } else { - return CreateDirs(path); + // The path is a file + // Creates directory preceding the last '/' + return CreateDirs(new_path.substr(0, new_path.rfind('/'))); } } -- cgit v1.2.3 From 0195038c07f88bb62fd492559bbc264a71c3ac7a Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:21:46 -0500 Subject: Revert "Merge pull request #5179 from ReinUsesLisp/fs-path" This reverts commit 4e94d0d53af2cdb7b03ef9de23cc29f3565df97a, reversing changes made to 6d6115475b4edccdf1bb4e96ecc3d3b1be319e76. --- src/common/file_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a286b9341..7752c0421 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -239,7 +239,7 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, } // windows loop do { - const std::string virtual_name = std::filesystem::path(ffd.cFileName).string(); + const std::string virtual_name(Common::UTF16ToUTF8(ffd.cFileName)); #else DIR* dirp = opendir(directory.c_str()); if (!dirp) -- cgit v1.2.3 From 8941cdb7d2355ba0f3084b306165923520f9db65 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:22:01 -0500 Subject: Revert "Merge pull request #5174 from ReinUsesLisp/fs-fix" This reverts commit 5fe55b16a11d9ec607fb8a3fdddc77a4393cd96a, reversing changes made to e94dd7e2c4fc3f7ca2c15c01bdc301be2b8a4c1b. --- src/common/file_util.cpp | 30 ++---------------------------- src/common/file_util.h | 10 ++-------- 2 files changed, 4 insertions(+), 36 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 7752c0421..d5f6ea2ee 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -98,11 +98,6 @@ bool Delete(const fs::path& path) { bool CreateDir(const fs::path& path) { LOG_TRACE(Common_Filesystem, "directory {}", path.string()); - if (Exists(path)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); - return true; - } - std::error_code ec; const bool success = fs::create_directory(path, ec); @@ -114,41 +109,20 @@ bool CreateDir(const fs::path& path) { return true; } -bool CreateDirs(const fs::path& path) { +bool CreateFullPath(const fs::path& path) { LOG_TRACE(Common_Filesystem, "path {}", path.string()); - if (Exists(path)) { - LOG_DEBUG(Common_Filesystem, "path exists {}", path.string()); - return true; - } - std::error_code ec; const bool success = fs::create_directories(path, ec); if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create directories: {}", ec.message()); + LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); return false; } return true; } -bool CreateFullPath(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "path {}", path); - - // Removes trailing slashes and turns any '\' into '/' - const auto new_path = SanitizePath(path.string(), DirectorySeparator::ForwardSlash); - - if (new_path.rfind('.') == std::string::npos) { - // The path is a directory - return CreateDirs(new_path); - } else { - // The path is a file - // Creates directory preceding the last '/' - return CreateDirs(new_path.substr(0, new_path.rfind('/'))); - } -} - bool Rename(const fs::path& src, const fs::path& dst) { LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); diff --git a/src/common/file_util.h b/src/common/file_util.h index cf006cc9d..c2ee7ca27 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -54,14 +54,8 @@ enum class UserPath { // Returns true if successful, or path already exists. bool CreateDir(const std::filesystem::path& path); -// Create all directories in path -// Returns true if successful, or path already exists. -[[nodiscard("Directory creation can fail and must be tested")]] bool CreateDirs( - const std::filesystem::path& path); - -// Creates directories in path. Returns true on success. -[[deprecated("This function is deprecated, use CreateDirs")]] bool CreateFullPath( - const std::filesystem::path& path); +// Creates the full path of path. Returns true on success +bool CreateFullPath(const std::filesystem::path& path); // Deletes a given file at the path. // This will also delete empty directories. -- cgit v1.2.3 From 4de079b256ce320c7e261a2c35b703e477e46fa5 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:22:18 -0500 Subject: Revert "Merge pull request #5173 from lioncash/common-fs" This reverts commit ce5fcb6bb2c358b0251a2ce87945bda52789a76d, reversing changes made to 6f41763061082d5fa2ab039c554427152243cb46. --- src/common/file_util.cpp | 439 +++++++++++++++++++++++++++++++++++++---------- src/common/file_util.h | 69 +++++--- 2 files changed, 396 insertions(+), 112 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d5f6ea2ee..18fbfa25b 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,7 +3,6 @@ // Refer to the license.txt file included. #include -#include #include #include #include @@ -68,102 +67,290 @@ #include #include +#ifndef S_ISDIR +#define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR) +#endif + +// This namespace has various generic functions related to files and paths. +// The code still needs a ton of cleanup. +// REMEMBER: strdup considered harmful! namespace Common::FS { -namespace fs = std::filesystem; -bool Exists(const fs::path& path) { - std::error_code ec; - return fs::exists(path, ec); +// Remove any ending forward slashes from directory paths +// Modifies argument. +static void StripTailDirSlashes(std::string& fname) { + if (fname.length() <= 1) { + return; + } + + std::size_t i = fname.length(); + while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { + --i; + } + fname.resize(i); } -bool IsDirectory(const fs::path& path) { - std::error_code ec; - return fs::is_directory(path, ec); +bool Exists(const std::string& filename) { + struct stat file_info; + + std::string copy(filename); + StripTailDirSlashes(copy); + +#ifdef _WIN32 + // Windows needs a slash to identify a driver root + if (copy.size() != 0 && copy.back() == ':') + copy += DIR_SEP_CHR; + + int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); +#else + int result = stat(copy.c_str(), &file_info); +#endif + + return (result == 0); } -bool Delete(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "file {}", path.string()); +bool IsDirectory(const std::string& filename) { + struct stat file_info; - // Return true because we care about the file no - // being there, not the actual delete. - if (!Exists(path)) { - LOG_DEBUG(Common_Filesystem, "{} does not exist", path.string()); - return true; + std::string copy(filename); + StripTailDirSlashes(copy); + +#ifdef _WIN32 + // Windows needs a slash to identify a driver root + if (copy.size() != 0 && copy.back() == ':') + copy += DIR_SEP_CHR; + + int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); +#else + int result = stat(copy.c_str(), &file_info); +#endif + + if (result < 0) { + LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); + return false; } - std::error_code ec; - return fs::remove(path, ec); + return S_ISDIR(file_info.st_mode); } -bool CreateDir(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "directory {}", path.string()); +bool Delete(const std::string& filename) { + LOG_TRACE(Common_Filesystem, "file {}", filename); - std::error_code ec; - const bool success = fs::create_directory(path, ec); + // Return true because we care about the file no + // being there, not the actual delete. + if (!Exists(filename)) { + LOG_DEBUG(Common_Filesystem, "{} does not exist", filename); + return true; + } + + // We can't delete a directory + if (IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "Failed: {} is a directory", filename); + return false; + } - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create directory: {}", ec.message()); +#ifdef _WIN32 + if (!DeleteFileW(Common::UTF8ToUTF16W(filename).c_str())) { + LOG_ERROR(Common_Filesystem, "DeleteFile failed on {}: {}", filename, GetLastErrorMsg()); + return false; + } +#else + if (unlink(filename.c_str()) == -1) { + LOG_ERROR(Common_Filesystem, "unlink failed on {}: {}", filename, GetLastErrorMsg()); return false; } +#endif return true; } -bool CreateFullPath(const fs::path& path) { - LOG_TRACE(Common_Filesystem, "path {}", path.string()); +bool CreateDir(const std::string& path) { + LOG_TRACE(Common_Filesystem, "directory {}", path); +#ifdef _WIN32 + if (::CreateDirectoryW(Common::UTF8ToUTF16W(path).c_str(), nullptr)) + return true; + DWORD error = GetLastError(); + if (error == ERROR_ALREADY_EXISTS) { + LOG_DEBUG(Common_Filesystem, "CreateDirectory failed on {}: already exists", path); + return true; + } + LOG_ERROR(Common_Filesystem, "CreateDirectory failed on {}: {}", path, error); + return false; +#else + if (mkdir(path.c_str(), 0755) == 0) + return true; - std::error_code ec; - const bool success = fs::create_directories(path, ec); + int err = errno; - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to create full path: {}", ec.message()); - return false; + if (err == EEXIST) { + LOG_DEBUG(Common_Filesystem, "mkdir failed on {}: already exists", path); + return true; } - return true; + LOG_ERROR(Common_Filesystem, "mkdir failed on {}: {}", path, strerror(err)); + return false; +#endif } -bool Rename(const fs::path& src, const fs::path& dst) { - LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); +bool CreateFullPath(const std::string& fullPath) { + int panicCounter = 100; + LOG_TRACE(Common_Filesystem, "path {}", fullPath); + + if (Exists(fullPath)) { + LOG_DEBUG(Common_Filesystem, "path exists {}", fullPath); + return true; + } + + std::size_t position = 0; + while (true) { + // Find next sub path + position = fullPath.find(DIR_SEP_CHR, position); - std::error_code ec; - fs::rename(src, dst, ec); + // we're done, yay! + if (position == fullPath.npos) + return true; - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to rename file from {} to {}: {}", src.string(), - dst.string(), ec.message()); + // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") + std::string const subPath(fullPath.substr(0, position + 1)); + if (!IsDirectory(subPath) && !CreateDir(subPath)) { + LOG_ERROR(Common, "CreateFullPath: directory creation failed"); + return false; + } + + // A safety check + panicCounter--; + if (panicCounter <= 0) { + LOG_ERROR(Common, "CreateFullPath: directory structure is too deep"); + return false; + } + position++; + } +} + +bool DeleteDir(const std::string& filename) { + LOG_TRACE(Common_Filesystem, "directory {}", filename); + + // check if a directory + if (!IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "Not a directory {}", filename); return false; } - return true; +#ifdef _WIN32 + if (::RemoveDirectoryW(Common::UTF8ToUTF16W(filename).c_str())) + return true; +#else + if (rmdir(filename.c_str()) == 0) + return true; +#endif + LOG_ERROR(Common_Filesystem, "failed {}: {}", filename, GetLastErrorMsg()); + + return false; +} + +bool Rename(const std::string& srcFilename, const std::string& destFilename) { + LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); +#ifdef _WIN32 + if (_wrename(Common::UTF8ToUTF16W(srcFilename).c_str(), + Common::UTF8ToUTF16W(destFilename).c_str()) == 0) + return true; +#else + if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) + return true; +#endif + LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, + GetLastErrorMsg()); + return false; } -bool Copy(const fs::path& src, const fs::path& dst) { - LOG_TRACE(Common_Filesystem, "{} --> {}", src.string(), dst.string()); +bool Copy(const std::string& srcFilename, const std::string& destFilename) { + LOG_TRACE(Common_Filesystem, "{} --> {}", srcFilename, destFilename); +#ifdef _WIN32 + if (CopyFileW(Common::UTF8ToUTF16W(srcFilename).c_str(), + Common::UTF8ToUTF16W(destFilename).c_str(), FALSE)) + return true; - std::error_code ec; - const bool success = fs::copy_file(src, dst, fs::copy_options::overwrite_existing, ec); + LOG_ERROR(Common_Filesystem, "failed {} --> {}: {}", srcFilename, destFilename, + GetLastErrorMsg()); + return false; +#else + using CFilePointer = std::unique_ptr; - if (!success) { - LOG_ERROR(Common_Filesystem, "Unable to copy file {} to {}: {}", src.string(), dst.string(), - ec.message()); + // Open input file + CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose}; + if (!input) { + LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); return false; } + // open output file + CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose}; + if (!output) { + LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); + return false; + } + + // copy loop + std::array buffer; + while (!feof(input.get())) { + // read input + std::size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get()); + if (rnum != buffer.size()) { + if (ferror(input.get()) != 0) { + LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", + srcFilename, destFilename, GetLastErrorMsg()); + return false; + } + } + + // write output + std::size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get()); + if (wnum != rnum) { + LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, + destFilename, GetLastErrorMsg()); + return false; + } + } + return true; +#endif } -u64 GetSize(const fs::path& path) { - std::error_code ec; - const auto size = fs::file_size(path, ec); +u64 GetSize(const std::string& filename) { + if (!Exists(filename)) { + LOG_ERROR(Common_Filesystem, "failed {}: No such file", filename); + return 0; + } - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to retrieve file size ({}): {}", path.string(), - ec.message()); + if (IsDirectory(filename)) { + LOG_ERROR(Common_Filesystem, "failed {}: is a directory", filename); return 0; } - return size; + struct stat buf; +#ifdef _WIN32 + if (_wstat64(Common::UTF8ToUTF16W(filename).c_str(), &buf) == 0) +#else + if (stat(filename.c_str(), &buf) == 0) +#endif + { + LOG_TRACE(Common_Filesystem, "{}: {}", filename, buf.st_size); + return buf.st_size; + } + + LOG_ERROR(Common_Filesystem, "Stat failed {}: {}", filename, GetLastErrorMsg()); + return 0; +} + +u64 GetSize(const int fd) { + struct stat buf; + if (fstat(fd, &buf) != 0) { + LOG_ERROR(Common_Filesystem, "GetSize: stat failed {}: {}", fd, GetLastErrorMsg()); + return 0; + } + return buf.st_size; } u64 GetSize(FILE* f) { @@ -251,58 +438,132 @@ bool ForeachDirectoryEntry(u64* num_entries_out, const std::string& directory, return true; } -bool DeleteDirRecursively(const fs::path& path) { - std::error_code ec; - fs::remove_all(path, ec); +u64 ScanDirectoryTree(const std::string& directory, FSTEntry& parent_entry, + unsigned int recursion) { + const auto callback = [recursion, &parent_entry](u64* num_entries_out, + const std::string& directory, + const std::string& virtual_name) -> bool { + FSTEntry entry; + entry.virtualName = virtual_name; + entry.physicalName = directory + DIR_SEP + virtual_name; + + if (IsDirectory(entry.physicalName)) { + entry.isDirectory = true; + // is a directory, lets go inside if we didn't recurse to often + if (recursion > 0) { + entry.size = ScanDirectoryTree(entry.physicalName, entry, recursion - 1); + *num_entries_out += entry.size; + } else { + entry.size = 0; + } + } else { // is a file + entry.isDirectory = false; + entry.size = GetSize(entry.physicalName); + } + (*num_entries_out)++; - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to completely delete directory {}: {}", path.string(), - ec.message()); - return false; - } + // Push into the tree + parent_entry.children.push_back(std::move(entry)); + return true; + }; - return true; + u64 num_entries; + return ForeachDirectoryEntry(&num_entries, directory, callback) ? num_entries : 0; } -void CopyDir(const fs::path& src, const fs::path& dst) { - constexpr auto copy_flags = fs::copy_options::skip_existing | fs::copy_options::recursive; +bool DeleteDirRecursively(const std::string& directory, unsigned int recursion) { + const auto callback = [recursion](u64*, const std::string& directory, + const std::string& virtual_name) { + const std::string new_path = directory + DIR_SEP_CHR + virtual_name; - std::error_code ec; - fs::copy(src, dst, copy_flags, ec); + if (IsDirectory(new_path)) { + if (recursion == 0) { + return false; + } + return DeleteDirRecursively(new_path, recursion - 1); + } + return Delete(new_path); + }; - if (ec) { - LOG_ERROR(Common_Filesystem, "Error copying directory {} to {}: {}", src.string(), - dst.string(), ec.message()); - return; - } + if (!ForeachDirectoryEntry(nullptr, directory, callback)) + return false; - LOG_TRACE(Common_Filesystem, "Successfully copied directory."); + // Delete the outermost directory + DeleteDir(directory); + return true; } -std::optional GetCurrentDir() { - std::error_code ec; - auto path = fs::current_path(ec); +void CopyDir([[maybe_unused]] const std::string& source_path, + [[maybe_unused]] const std::string& dest_path) { +#ifndef _WIN32 + if (source_path == dest_path) { + return; + } + if (!Exists(source_path)) { + return; + } + if (!Exists(dest_path)) { + CreateFullPath(dest_path); + } - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to retrieve current working directory: {}", - ec.message()); - return std::nullopt; + DIR* dirp = opendir(source_path.c_str()); + if (!dirp) { + return; } - return {std::move(path)}; -} + while (struct dirent* result = readdir(dirp)) { + const std::string virtualName(result->d_name); + // check for "." and ".." + if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || + ((virtualName[0] == '.') && (virtualName[1] == '.') && (virtualName[2] == '\0'))) { + continue; + } -bool SetCurrentDir(const fs::path& path) { - std::error_code ec; - fs::current_path(path, ec); + std::string source, dest; + source = source_path + virtualName; + dest = dest_path + virtualName; + if (IsDirectory(source)) { + source += '/'; + dest += '/'; + if (!Exists(dest)) { + CreateFullPath(dest); + } + CopyDir(source, dest); + } else if (!Exists(dest)) { + Copy(source, dest); + } + } + closedir(dirp); +#endif +} - if (ec) { - LOG_ERROR(Common_Filesystem, "Unable to set {} as working directory: {}", path.string(), - ec.message()); - return false; +std::optional GetCurrentDir() { +// Get the current working directory (getcwd uses malloc) +#ifdef _WIN32 + wchar_t* dir = _wgetcwd(nullptr, 0); + if (!dir) { +#else + char* dir = getcwd(nullptr, 0); + if (!dir) { +#endif + LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); + return std::nullopt; } +#ifdef _WIN32 + std::string strDir = Common::UTF16ToUTF8(dir); +#else + std::string strDir = dir; +#endif + free(dir); + return strDir; +} - return true; +bool SetCurrentDir(const std::string& directory) { +#ifdef _WIN32 + return _wchdir(Common::UTF8ToUTF16W(directory).c_str()) == 0; +#else + return chdir(directory.c_str()) == 0; +#endif } #if defined(__APPLE__) diff --git a/src/common/file_util.h b/src/common/file_util.h index c2ee7ca27..840cde2a6 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -39,34 +38,48 @@ enum class UserPath { UserDir, }; -// Returns true if the path exists -[[nodiscard]] bool Exists(const std::filesystem::path& path); +// FileSystem tree node/ +struct FSTEntry { + bool isDirectory; + u64 size; // file length or number of entries from children + std::string physicalName; // name on disk + std::string virtualName; // name in FST names table + std::vector children; +}; + +// Returns true if file filename exists +[[nodiscard]] bool Exists(const std::string& filename); -// Returns true if path is a directory -[[nodiscard]] bool IsDirectory(const std::filesystem::path& path); +// Returns true if filename is a directory +[[nodiscard]] bool IsDirectory(const std::string& filename); // Returns the size of filename (64bit) -[[nodiscard]] u64 GetSize(const std::filesystem::path& path); +[[nodiscard]] u64 GetSize(const std::string& filename); + +// Overloaded GetSize, accepts file descriptor +[[nodiscard]] u64 GetSize(int fd); // Overloaded GetSize, accepts FILE* [[nodiscard]] u64 GetSize(FILE* f); // Returns true if successful, or path already exists. -bool CreateDir(const std::filesystem::path& path); +bool CreateDir(const std::string& filename); + +// Creates the full path of fullPath returns true on success +bool CreateFullPath(const std::string& fullPath); -// Creates the full path of path. Returns true on success -bool CreateFullPath(const std::filesystem::path& path); +// Deletes a given filename, return true on success +// Doesn't supports deleting a directory +bool Delete(const std::string& filename); -// Deletes a given file at the path. -// This will also delete empty directories. -// Return true on success -bool Delete(const std::filesystem::path& path); +// Deletes a directory filename, returns true on success +bool DeleteDir(const std::string& filename); -// Renames file src to dst, returns true on success -bool Rename(const std::filesystem::path& src, const std::filesystem::path& dst); +// renames file srcFilename to destFilename, returns true on success +bool Rename(const std::string& srcFilename, const std::string& destFilename); -// copies file src to dst, returns true on success -bool Copy(const std::filesystem::path& src, const std::filesystem::path& dst); +// copies file srcFilename to destFilename, returns true on success +bool Copy(const std::string& srcFilename, const std::string& destFilename); // creates an empty file filename, returns true on success bool CreateEmptyFile(const std::string& filename); @@ -93,17 +106,27 @@ using DirectoryEntryCallable = std::function GetCurrentDir(); +[[nodiscard]] std::optional GetCurrentDir(); // Create directory and copy contents (does not overwrite existing files) -void CopyDir(const std::filesystem::path& src, const std::filesystem::path& dst); +void CopyDir(const std::string& source_path, const std::string& dest_path); -// Set the current directory to given path -bool SetCurrentDir(const std::filesystem::path& path); +// Set the current directory to given directory +bool SetCurrentDir(const std::string& directory); // Returns a pointer to a string with a yuzu data dir in the user's home // directory. To be used in "multi-user" mode (that is, installed). -- cgit v1.2.3 From 761206cf81b271f7f4dd6a167a120325b760dbf3 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 12 Dec 2020 11:50:07 -0800 Subject: common: Update CMakeList to fix build issue with Boost. --- src/common/CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 56c7e21f5..0981fe90b 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -209,9 +209,8 @@ else() endif() create_target_directory_groups(common) -find_package(Boost 1.71 COMPONENTS context headers REQUIRED) -target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) +target_link_libraries(common PUBLIC Boost::boost fmt::fmt microprofile) target_link_libraries(common PRIVATE lz4::lz4 xbyak) if (MSVC) target_link_libraries(common PRIVATE zstd::zstd) -- cgit v1.2.3 From 292dd642cefb3717363e96974f055da9dc1cbf23 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sat, 12 Dec 2020 22:20:24 -0500 Subject: cmake: Fix generating CMake configs and linking with Boost Fixes regression by 761206cf81b271f7f4dd6a167a120325b760dbf3, causing yuzu to not build on Linux with any version of Boost except a cached 1.73 Conan version from before about a day ago. Moves the Boost requirement out of the `REQUIRED_LIBS` psuedo-2D-array for Conan to instead be manually configured, using Conan as a fallback solution if the system does not meet our requirements. Requires any update from the linux-fresh container in order to build. **DO NOT MERGE** until someone with the MSVC toolchain can verify this works there, too. --- CMakeLists.txt | 17 ++++++++++++++++- src/common/CMakeLists.txt | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'src/common') diff --git a/CMakeLists.txt b/CMakeLists.txt index 2316f7684..2366d372c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -160,7 +160,6 @@ macro(yuzu_find_packages) # Capitalization matters here. We need the naming to match the generated paths from Conan set(REQUIRED_LIBS # Cmake Pkg Prefix Version Conan Pkg - "Boost 1.73 boost/1.73.0" "Catch2 2.13 catch2/2.13.0" "fmt 7.1 fmt/7.1.2" # can't use until https://github.com/bincrafters/community/issues/1173 @@ -195,6 +194,17 @@ macro(yuzu_find_packages) unset(FN_FORCE_REQUIRED) endmacro() +if (NOT Boost_FOUND) + find_package(Boost 1.73.0 COMPONENTS context headers QUIET) + if (Boost_FOUND) + set(Boost_LIBRARIES Boost::boost Boost::context) + endif() +endif() +if (NOT Boost_FOUND) + message(STATUS "Boost 1.73.0 or newer not found, falling back to Conan") + list(APPEND CONAN_REQUIRED_LIBS "boost/1.73.0") +endif() + # Attempt to locate any packages that are required and report the missing ones in CONAN_REQUIRED_LIBS yuzu_find_packages() @@ -299,6 +309,11 @@ if (CONAN_REQUIRED_LIBS) # this time with required, so we bail if its not found. yuzu_find_packages(FORCE_REQUIRED) + if (NOT Boost_FOUND) + find_package(Boost 1.73.0 REQUIRED COMPONENTS context headers) + set(Boost_LIBRARIES Boost::boost) + endif() + # Due to issues with variable scopes in functions, we need to also find_package(qt5) outside of the function if(ENABLE_QT) list(APPEND CMAKE_MODULE_PATH "${CONAN_QT_ROOT_RELEASE}") diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 0981fe90b..0acf70a0a 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -210,7 +210,7 @@ endif() create_target_directory_groups(common) -target_link_libraries(common PUBLIC Boost::boost fmt::fmt microprofile) +target_link_libraries(common PUBLIC ${Boost_LIBRARIES} fmt::fmt microprofile) target_link_libraries(common PRIVATE lz4::lz4 xbyak) if (MSVC) target_link_libraries(common PRIVATE zstd::zstd) -- cgit v1.2.3 From 388cf58b31d6480007901785851e6369b0efe64b Mon Sep 17 00:00:00 2001 From: comex Date: Tue, 29 Dec 2020 14:26:16 -0500 Subject: k_priority_queue: Fix concepts use - For `std::same_as`, add missing include of ``. - For `std::convertible_to`, create a replacement in `common/concepts.h` and use that instead. This would also be found in ``, but unlike `std::same_as`, `std::convertible_to` is not yet implemented in libc++, LLVM's STL implementation - not even in master. (In fact, `std::same_as` is the *only* concept currently implemented. For some reason.) --- src/common/concepts.h | 4 ++++ src/core/hle/kernel/k_priority_queue.h | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src/common') diff --git a/src/common/concepts.h b/src/common/concepts.h index 5bef3ad67..aa08065a7 100644 --- a/src/common/concepts.h +++ b/src/common/concepts.h @@ -31,4 +31,8 @@ concept DerivedFrom = requires { std::is_convertible_v; }; +// TODO: Replace with std::convertible_to when libc++ implements it. +template +concept ConvertibleTo = std::is_convertible_v; + } // namespace Common diff --git a/src/core/hle/kernel/k_priority_queue.h b/src/core/hle/kernel/k_priority_queue.h index 01a577d0c..99fb8fe93 100644 --- a/src/core/hle/kernel/k_priority_queue.h +++ b/src/core/hle/kernel/k_priority_queue.h @@ -8,11 +8,13 @@ #pragma once #include +#include #include "common/assert.h" #include "common/bit_set.h" #include "common/bit_util.h" #include "common/common_types.h" +#include "common/concepts.h" namespace Kernel { @@ -21,7 +23,7 @@ class Thread; template concept KPriorityQueueAffinityMask = !std::is_reference_v && requires(T & t) { { t.GetAffinityMask() } - ->std::convertible_to; + ->Common::ConvertibleTo; {t.SetAffinityMask(std::declval())}; { t.GetAffinity(std::declval()) } @@ -48,9 +50,9 @@ concept KPriorityQueueMember = !std::is_reference_v && requires(T & t) { ->KPriorityQueueAffinityMask; { t.GetActiveCore() } - ->std::convertible_to; + ->Common::ConvertibleTo; { t.GetPriority() } - ->std::convertible_to; + ->Common::ConvertibleTo; }; template -- cgit v1.2.3 From 69e82d01d5012c0078f65a294ecbb7118707f73c Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 29 Dec 2020 16:36:35 -0800 Subject: common: ThreadWorker: Add class to help do asynchronous work. --- src/common/CMakeLists.txt | 2 ++ src/common/thread_worker.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++ src/common/thread_worker.h | 30 +++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 src/common/thread_worker.cpp create mode 100644 src/common/thread_worker.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 943ff996e..5c8003eb1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -162,6 +162,8 @@ add_library(common STATIC thread.cpp thread.h thread_queue_list.h + thread_worker.cpp + thread_worker.h threadsafe_queue.h time_zone.cpp time_zone.h diff --git a/src/common/thread_worker.cpp b/src/common/thread_worker.cpp new file mode 100644 index 000000000..8f9bf447a --- /dev/null +++ b/src/common/thread_worker.cpp @@ -0,0 +1,58 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/thread.h" +#include "common/thread_worker.h" + +namespace Common { + +ThreadWorker::ThreadWorker(std::size_t num_workers, const std::string& name) { + for (std::size_t i = 0; i < num_workers; ++i) + threads.emplace_back([this, thread_name{std::string{name}}] { + Common::SetCurrentThreadName(thread_name.c_str()); + + // Wait for first request + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + } + + while (true) { + std::function task; + + { + std::unique_lock lock{queue_mutex}; + condition.wait(lock, [this] { return stop || !requests.empty(); }); + if (stop || requests.empty()) { + return; + } + task = std::move(requests.front()); + requests.pop(); + } + + task(); + } + }); +} + +ThreadWorker::~ThreadWorker() { + { + std::unique_lock lock{queue_mutex}; + stop = true; + } + condition.notify_all(); + for (std::thread& thread : threads) { + thread.join(); + } +} + +void ThreadWorker::QueueWork(std::function&& work) { + { + std::unique_lock lock{queue_mutex}; + requests.emplace(work); + } + condition.notify_one(); +} + +} // namespace Common diff --git a/src/common/thread_worker.h b/src/common/thread_worker.h new file mode 100644 index 000000000..f1859971f --- /dev/null +++ b/src/common/thread_worker.h @@ -0,0 +1,30 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace Common { + +class ThreadWorker final { +public: + explicit ThreadWorker(std::size_t num_workers, const std::string& name); + ~ThreadWorker(); + void QueueWork(std::function&& work); + +private: + std::vector threads; + std::queue> requests; + std::mutex queue_mutex; + std::condition_variable condition; + std::atomic_bool stop{}; +}; + +} // namespace Common -- cgit v1.2.3 From b3587102d160fb74a12935a79f06ee8a12712f12 Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Dec 2020 21:16:57 -0300 Subject: core/memory: Read and write page table atomically Squash attributes into the pointer's integer, making them an uintptr_t pair containing 2 bits at the bottom and then the pointer. These bits are currently unused thanks to alignment requirements. Configure Dynarmic to mask out these bits on pointer reads. While we are at it, remove some unused attributes carried over from Citra. Read/Write and other hot functions use a two step unpacking process that is less readable to stop MSVC from emitting an extra AND instruction in the hot path: mov rdi,rcx shr rdx,0Ch mov r8,qword ptr [rax+8] mov rax,qword ptr [r8+rdx*8] mov rdx,rax -and al,3 and rdx,0FFFFFFFFFFFFFFFCh je Core::Memory::Memory::Impl::Read mov rax,qword ptr [vaddr] movzx eax,byte ptr [rdx+rax] --- src/common/page_table.cpp | 10 +- src/common/page_table.h | 68 +++++++++-- src/common/virtual_buffer.h | 10 +- src/core/arm/dynarmic/arm_dynarmic_32.cpp | 1 + src/core/arm/dynarmic/arm_dynarmic_64.cpp | 1 + src/core/hle/kernel/memory/page_table.cpp | 2 +- src/core/memory.cpp | 187 ++++++++++-------------------- 7 files changed, 132 insertions(+), 147 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.cpp b/src/common/page_table.cpp index bccea0894..8fd8620fd 100644 --- a/src/common/page_table.cpp +++ b/src/common/page_table.cpp @@ -10,16 +10,10 @@ PageTable::PageTable() = default; PageTable::~PageTable() noexcept = default; -void PageTable::Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, - bool has_attribute) { - const std::size_t num_page_table_entries{1ULL - << (address_space_width_in_bits - page_size_in_bits)}; +void PageTable::Resize(size_t address_space_width_in_bits, size_t page_size_in_bits) { + const size_t num_page_table_entries{1ULL << (address_space_width_in_bits - page_size_in_bits)}; pointers.resize(num_page_table_entries); backing_addr.resize(num_page_table_entries); - - if (has_attribute) { - attributes.resize(num_page_table_entries); - } } } // namespace Common diff --git a/src/common/page_table.h b/src/common/page_table.h index 9754fabf9..8d4ee9249 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include "common/common_types.h" @@ -20,10 +21,6 @@ enum class PageType : u8 { /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and /// invalidation RasterizerCachedMemory, - /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions. - Special, - /// Page is allocated for use. - Allocated, }; struct SpecialRegion { @@ -48,6 +45,59 @@ struct SpecialRegion { * mimics the way a real CPU page table works. */ struct PageTable { + /// Number of bits reserved for attribute tagging. + /// This can be at most the guaranteed alignment of the pointers in the page table. + static constexpr int ATTRIBUTE_BITS = 2; + + /** + * Pair of host pointer and page type attribute. + * This uses the lower bits of a given pointer to store the attribute tag. + * Writing and reading the pointer attribute pair is guaranteed to be atomic for the same method + * call. In other words, they are guaranteed to be synchronized at all times. + */ + class PageInfo { + public: + /// Returns the page pointer + [[nodiscard]] u8* Pointer() const noexcept { + return ExtractPointer(raw.load(std::memory_order_relaxed)); + } + + /// Returns the page type attribute + [[nodiscard]] PageType Type() const noexcept { + return ExtractType(raw.load(std::memory_order_relaxed)); + } + + /// Returns the page pointer and attribute pair, extracted from the same atomic read + [[nodiscard]] std::pair PointerType() const noexcept { + const uintptr_t non_atomic_raw = raw.load(std::memory_order_relaxed); + return {ExtractPointer(non_atomic_raw), ExtractType(non_atomic_raw)}; + } + + /// Returns the raw representation of the page information. + /// Use ExtractPointer and ExtractType to unpack the value. + [[nodiscard]] uintptr_t Raw() const noexcept { + return raw.load(std::memory_order_relaxed); + } + + /// Write a page pointer and type pair atomically + void Store(u8* pointer, PageType type) noexcept { + raw.store(reinterpret_cast(pointer) | static_cast(type)); + } + + /// Unpack a pointer from a page info raw representation + [[nodiscard]] static u8* ExtractPointer(uintptr_t raw) noexcept { + return reinterpret_cast(raw & (~uintptr_t{0} << ATTRIBUTE_BITS)); + } + + /// Unpack a page type from a page info raw representation + [[nodiscard]] static PageType ExtractType(uintptr_t raw) noexcept { + return static_cast(raw & ((uintptr_t{1} << ATTRIBUTE_BITS) - 1)); + } + + private: + std::atomic raw; + }; + PageTable(); ~PageTable() noexcept; @@ -63,20 +113,16 @@ struct PageTable { * * @param address_space_width_in_bits The address size width in bits. * @param page_size_in_bits The page size in bits. - * @param has_attribute Whether or not this page has any backing attributes. */ - void Resize(std::size_t address_space_width_in_bits, std::size_t page_size_in_bits, - bool has_attribute); + void Resize(size_t address_space_width_in_bits, size_t page_size_in_bits); /** * Vector of memory pointers backing each page. An entry can only be non-null if the - * corresponding entry in the `attributes` vector is of type `Memory`. + * corresponding attribute element is of type `Memory`. */ - VirtualBuffer pointers; + VirtualBuffer pointers; VirtualBuffer backing_addr; - - VirtualBuffer attributes; }; } // namespace Common diff --git a/src/common/virtual_buffer.h b/src/common/virtual_buffer.h index 91d430036..fb1a6f81f 100644 --- a/src/common/virtual_buffer.h +++ b/src/common/virtual_buffer.h @@ -15,10 +15,12 @@ void FreeMemoryPages(void* base, std::size_t size) noexcept; template class VirtualBuffer final { public: - static_assert( - std::is_trivially_constructible_v, - "T must be trivially constructible, as non-trivial constructors will not be executed " - "with the current allocator"); + // TODO: Uncomment this and change Common::PageTable::PageInfo to be trivially constructible + // using std::atomic_ref once libc++ has support for it + // static_assert( + // std::is_trivially_constructible_v, + // "T must be trivially constructible, as non-trivial constructors will not be executed " + // "with the current allocator"); constexpr VirtualBuffer() = default; explicit VirtualBuffer(std::size_t count) : alloc_size{count * sizeof(T)} { diff --git a/src/core/arm/dynarmic/arm_dynarmic_32.cpp b/src/core/arm/dynarmic/arm_dynarmic_32.cpp index e9c74b1a6..8aaf11eee 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_32.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_32.cpp @@ -133,6 +133,7 @@ std::shared_ptr ARM_Dynarmic_32::MakeJit(Common::PageTable& config.page_table = reinterpret_cast*>( page_table.pointers.data()); config.absolute_offset_page_table = true; + config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS; config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128; config.only_detect_misalignment_via_page_table_on_page_boundary = true; diff --git a/src/core/arm/dynarmic/arm_dynarmic_64.cpp b/src/core/arm/dynarmic/arm_dynarmic_64.cpp index 7a4eb88a2..d2e1dc724 100644 --- a/src/core/arm/dynarmic/arm_dynarmic_64.cpp +++ b/src/core/arm/dynarmic/arm_dynarmic_64.cpp @@ -152,6 +152,7 @@ std::shared_ptr ARM_Dynarmic_64::MakeJit(Common::PageTable& // Memory config.page_table = reinterpret_cast(page_table.pointers.data()); config.page_table_address_space_bits = address_space_bits; + config.page_table_pointer_mask_bits = Common::PageTable::ATTRIBUTE_BITS; config.silently_mirror_page_table = false; config.absolute_offset_page_table = true; config.detect_misaligned_access_via_page_table = 16 | 32 | 64 | 128; diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index f53a7be82..f3e8bc333 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -265,7 +265,7 @@ ResultCode PageTable::InitializeForProcess(FileSys::ProgramAddressSpaceType as_t physical_memory_usage = 0; memory_pool = pool; - page_table_impl.Resize(address_space_width, PageBits, true); + page_table_impl.Resize(address_space_width, PageBits); return InitializeMemoryLayout(start, end); } diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 54a848936..f209c4949 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -68,21 +67,8 @@ struct Memory::Impl { bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const { const auto& page_table = process.PageTable().PageTableImpl(); - - const u8* const page_pointer = page_table.pointers[vaddr >> PAGE_BITS]; - if (page_pointer != nullptr) { - return true; - } - - if (page_table.attributes[vaddr >> PAGE_BITS] == Common::PageType::RasterizerCachedMemory) { - return true; - } - - if (page_table.attributes[vaddr >> PAGE_BITS] != Common::PageType::Special) { - return false; - } - - return false; + const auto [pointer, type] = page_table.pointers[vaddr >> PAGE_BITS].PointerType(); + return pointer != nullptr || type == Common::PageType::RasterizerCachedMemory; } bool IsValidVirtualAddress(VAddr vaddr) const { @@ -100,17 +86,15 @@ struct Memory::Impl { } u8* GetPointer(const VAddr vaddr) const { - u8* const page_pointer{current_page_table->pointers[vaddr >> PAGE_BITS]}; - if (page_pointer) { - return page_pointer + vaddr; + const uintptr_t raw_pointer = current_page_table->pointers[vaddr >> PAGE_BITS].Raw(); + if (u8* const pointer = Common::PageTable::PageInfo::ExtractPointer(raw_pointer)) { + return pointer + vaddr; } - - if (current_page_table->attributes[vaddr >> PAGE_BITS] == - Common::PageType::RasterizerCachedMemory) { + const auto type = Common::PageTable::PageInfo::ExtractType(raw_pointer); + if (type == Common::PageType::RasterizerCachedMemory) { return GetPointerFromRasterizerCachedMemory(vaddr); } - - return {}; + return nullptr; } u8 Read8(const VAddr addr) { @@ -222,7 +206,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -231,10 +216,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - - const u8* const src_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + const u8* const src_ptr = pointer + page_offset + (page_index << PAGE_BITS); std::memcpy(dest_buffer, src_ptr, copy_amount); break; } @@ -268,7 +251,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -277,10 +261,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - - const u8* const src_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + const u8* const src_ptr = pointer + page_offset + (page_index << PAGE_BITS); std::memcpy(dest_buffer, src_ptr, copy_amount); break; } @@ -320,7 +302,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -328,10 +311,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - - u8* const dest_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + u8* const dest_ptr = pointer + page_offset + (page_index << PAGE_BITS); std::memcpy(dest_ptr, src_buffer, copy_amount); break; } @@ -364,7 +345,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -372,10 +354,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - - u8* const dest_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + u8* const dest_ptr = pointer + page_offset + (page_index << PAGE_BITS); std::memcpy(dest_ptr, src_buffer, copy_amount); break; } @@ -414,7 +394,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped ZeroBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -422,10 +403,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - - u8* dest_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + u8* const dest_ptr = pointer + page_offset + (page_index << PAGE_BITS); std::memset(dest_ptr, 0, copy_amount); break; } @@ -461,7 +440,8 @@ struct Memory::Impl { std::min(static_cast(PAGE_SIZE) - page_offset, remaining_size); const auto current_vaddr = static_cast((page_index << PAGE_BITS) + page_offset); - switch (page_table.attributes[page_index]) { + const auto [pointer, type] = page_table.pointers[page_index].PointerType(); + switch (type) { case Common::PageType::Unmapped: { LOG_ERROR(HW_Memory, "Unmapped CopyBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})", @@ -470,9 +450,8 @@ struct Memory::Impl { break; } case Common::PageType::Memory: { - DEBUG_ASSERT(page_table.pointers[page_index]); - const u8* src_ptr = - page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS); + DEBUG_ASSERT(pointer); + const u8* src_ptr = pointer + page_offset + (page_index << PAGE_BITS); WriteBlock(process, dest_addr, src_ptr, copy_amount); break; } @@ -498,34 +477,19 @@ struct Memory::Impl { return CopyBlock(*system.CurrentProcess(), dest_addr, src_addr, size); } - struct PageEntry { - u8* const pointer; - const Common::PageType attribute; - }; - - PageEntry SafePageEntry(std::size_t base) const { - std::lock_guard lock{rasterizer_cache_guard}; - return { - .pointer = current_page_table->pointers[base], - .attribute = current_page_table->attributes[base], - }; - } - void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) { - std::lock_guard lock{rasterizer_cache_guard}; if (vaddr == 0) { return; } - // Iterate over a contiguous CPU address space, which corresponds to the specified GPU // address space, marking the region as un/cached. The region is marked un/cached at a // granularity of CPU pages, hence why we iterate on a CPU page basis (note: GPU page size // is different). This assumes the specified GPU address region is contiguous as well. - u64 num_pages = ((vaddr + size - 1) >> PAGE_BITS) - (vaddr >> PAGE_BITS) + 1; - for (unsigned i = 0; i < num_pages; ++i, vaddr += PAGE_SIZE) { - Common::PageType& page_type{current_page_table->attributes[vaddr >> PAGE_BITS]}; - + const u64 num_pages = ((vaddr + size - 1) >> PAGE_BITS) - (vaddr >> PAGE_BITS) + 1; + for (u64 i = 0; i < num_pages; ++i, vaddr += PAGE_SIZE) { + const Common::PageType page_type{ + current_page_table->pointers[vaddr >> PAGE_BITS].Type()}; if (cached) { // Switch page type to cached if now cached switch (page_type) { @@ -534,8 +498,8 @@ struct Memory::Impl { // space, for example, a system module need not have a VRAM mapping. break; case Common::PageType::Memory: - page_type = Common::PageType::RasterizerCachedMemory; - current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr; + current_page_table->pointers[vaddr >> PAGE_BITS].Store( + nullptr, Common::PageType::RasterizerCachedMemory); break; case Common::PageType::RasterizerCachedMemory: // There can be more than one GPU region mapped per CPU region, so it's common @@ -556,16 +520,16 @@ struct Memory::Impl { // that this area is already unmarked as cached. break; case Common::PageType::RasterizerCachedMemory: { - u8* pointer{GetPointerFromRasterizerCachedMemory(vaddr & ~PAGE_MASK)}; + u8* const pointer{GetPointerFromRasterizerCachedMemory(vaddr & ~PAGE_MASK)}; if (pointer == nullptr) { // It's possible that this function has been called while updating the // pagetable after unmapping a VMA. In that case the underlying VMA will no // longer exist, and we should just leave the pagetable entry blank. - page_type = Common::PageType::Unmapped; + current_page_table->pointers[vaddr >> PAGE_BITS].Store( + nullptr, Common::PageType::Unmapped); } else { - current_page_table->pointers[vaddr >> PAGE_BITS] = - pointer - (vaddr & ~PAGE_MASK); - page_type = Common::PageType::Memory; + current_page_table->pointers[vaddr >> PAGE_BITS].Store( + pointer - (vaddr & ~PAGE_MASK), Common::PageType::Memory); } break; } @@ -595,7 +559,7 @@ struct Memory::Impl { auto& gpu = system.GPU(); for (u64 i = 0; i < size; i++) { const auto page = base + i; - if (page_table.attributes[page] == Common::PageType::RasterizerCachedMemory) { + if (page_table.pointers[page].Type() == Common::PageType::RasterizerCachedMemory) { gpu.FlushAndInvalidateRegion(page << PAGE_BITS, PAGE_SIZE); } } @@ -610,20 +574,18 @@ struct Memory::Impl { "Mapping memory page without a pointer @ {:016x}", base * PAGE_SIZE); while (base != end) { - page_table.attributes[base] = type; - page_table.pointers[base] = nullptr; + page_table.pointers[base].Store(nullptr, type); page_table.backing_addr[base] = 0; base += 1; } } else { while (base != end) { - page_table.pointers[base] = - system.DeviceMemory().GetPointer(target) - (base << PAGE_BITS); - page_table.attributes[base] = type; + page_table.pointers[base].Store( + system.DeviceMemory().GetPointer(target) - (base << PAGE_BITS), type); page_table.backing_addr[base] = target - (base << PAGE_BITS); - ASSERT_MSG(page_table.pointers[base], + ASSERT_MSG(page_table.pointers[base].Pointer(), "memory mapping base yield a nullptr within the table"); base += 1; @@ -646,21 +608,13 @@ struct Memory::Impl { template T Read(const VAddr vaddr) { // Avoid adding any extra logic to this fast-path block - if (const u8* const pointer = current_page_table->pointers[vaddr >> PAGE_BITS]) { + const uintptr_t raw_pointer = current_page_table->pointers[vaddr >> PAGE_BITS].Raw(); + if (const u8* const pointer = Common::PageTable::PageInfo::ExtractPointer(raw_pointer)) { T value; std::memcpy(&value, &pointer[vaddr], sizeof(T)); return value; } - - // Otherwise, we need to grab the page with a lock, in case it is currently being modified - const auto entry = SafePageEntry(vaddr >> PAGE_BITS); - if (entry.pointer) { - T value; - std::memcpy(&value, &entry.pointer[vaddr], sizeof(T)); - return value; - } - - switch (entry.attribute) { + switch (Common::PageTable::PageInfo::ExtractType(raw_pointer)) { case Common::PageType::Unmapped: LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:08X}", sizeof(T) * 8, vaddr); return 0; @@ -692,20 +646,12 @@ struct Memory::Impl { template void Write(const VAddr vaddr, const T data) { // Avoid adding any extra logic to this fast-path block - if (u8* const pointer = current_page_table->pointers[vaddr >> PAGE_BITS]) { + const uintptr_t raw_pointer = current_page_table->pointers[vaddr >> PAGE_BITS].Raw(); + if (u8* const pointer = Common::PageTable::PageInfo::ExtractPointer(raw_pointer)) { std::memcpy(&pointer[vaddr], &data, sizeof(T)); return; } - - // Otherwise, we need to grab the page with a lock, in case it is currently being modified - const auto entry = SafePageEntry(vaddr >> PAGE_BITS); - if (entry.pointer) { - // Memory was mapped, we are done - std::memcpy(&entry.pointer[vaddr], &data, sizeof(T)); - return; - } - - switch (entry.attribute) { + switch (Common::PageTable::PageInfo::ExtractType(raw_pointer)) { case Common::PageType::Unmapped: LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8, static_cast(data), vaddr); @@ -726,15 +672,13 @@ struct Memory::Impl { template bool WriteExclusive(const VAddr vaddr, const T data, const T expected) { - u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS]; - if (page_pointer != nullptr) { + const uintptr_t raw_pointer = current_page_table->pointers[vaddr >> PAGE_BITS].Raw(); + if (u8* const pointer = Common::PageTable::PageInfo::ExtractPointer(raw_pointer)) { // NOTE: Avoid adding any extra logic to this fast-path block - auto* pointer = reinterpret_cast(&page_pointer[vaddr]); - return Common::AtomicCompareAndSwap(pointer, data, expected); + const auto volatile_pointer = reinterpret_cast(&pointer[vaddr]); + return Common::AtomicCompareAndSwap(volatile_pointer, data, expected); } - - const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS]; - switch (type) { + switch (Common::PageTable::PageInfo::ExtractType(raw_pointer)) { case Common::PageType::Unmapped: LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8, static_cast(data), vaddr); @@ -755,15 +699,13 @@ struct Memory::Impl { } bool WriteExclusive128(const VAddr vaddr, const u128 data, const u128 expected) { - u8* const page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS]; - if (page_pointer != nullptr) { + const uintptr_t raw_pointer = current_page_table->pointers[vaddr >> PAGE_BITS].Raw(); + if (u8* const pointer = Common::PageTable::PageInfo::ExtractPointer(raw_pointer)) { // NOTE: Avoid adding any extra logic to this fast-path block - auto* pointer = reinterpret_cast(&page_pointer[vaddr]); - return Common::AtomicCompareAndSwap(pointer, data, expected); + const auto volatile_pointer = reinterpret_cast(&pointer[vaddr]); + return Common::AtomicCompareAndSwap(volatile_pointer, data, expected); } - - const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS]; - switch (type) { + switch (Common::PageTable::PageInfo::ExtractType(raw_pointer)) { case Common::PageType::Unmapped: LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}{:016X}", sizeof(data) * 8, static_cast(data[1]), static_cast(data[0]), vaddr); @@ -783,7 +725,6 @@ struct Memory::Impl { return true; } - mutable std::mutex rasterizer_cache_guard; Common::PageTable* current_page_table = nullptr; Core::System& system; }; -- cgit v1.2.3 From 6d30745d772c7e332bbea1462a92033386b85b08 Mon Sep 17 00:00:00 2001 From: MerryMage Date: Fri, 1 Jan 2021 11:30:30 +0000 Subject: memory: Remove MemoryHook --- src/common/CMakeLists.txt | 2 - src/common/memory_hook.cpp | 11 --- src/common/memory_hook.h | 47 ----------- src/common/page_table.h | 18 ---- src/core/memory.cpp | 30 ------- src/core/memory.h | 34 -------- src/tests/CMakeLists.txt | 2 - src/tests/core/arm/arm_test_common.cpp | 145 --------------------------------- src/tests/core/arm/arm_test_common.h | 93 --------------------- 9 files changed, 382 deletions(-) delete mode 100644 src/common/memory_hook.cpp delete mode 100644 src/common/memory_hook.h delete mode 100644 src/tests/core/arm/arm_test_common.cpp delete mode 100644 src/tests/core/arm/arm_test_common.h (limited to 'src/common') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5c8003eb1..2c2bd2ee8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -135,8 +135,6 @@ add_library(common STATIC math_util.h memory_detect.cpp memory_detect.h - memory_hook.cpp - memory_hook.h microprofile.cpp microprofile.h microprofileui.h diff --git a/src/common/memory_hook.cpp b/src/common/memory_hook.cpp deleted file mode 100644 index 3986986d6..000000000 --- a/src/common/memory_hook.cpp +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include "common/memory_hook.h" - -namespace Common { - -MemoryHook::~MemoryHook() = default; - -} // namespace Common diff --git a/src/common/memory_hook.h b/src/common/memory_hook.h deleted file mode 100644 index adaa4c2c5..000000000 --- a/src/common/memory_hook.h +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include - -#include "common/common_types.h" - -namespace Common { - -/** - * Memory hooks have two purposes: - * 1. To allow reads and writes to a region of memory to be intercepted. This is used to implement - * texture forwarding and memory breakpoints for debugging. - * 2. To allow for the implementation of MMIO devices. - * - * A hook may be mapped to multiple regions of memory. - * - * If a std::nullopt or false is returned from a function, the read/write request is passed through - * to the underlying memory region. - */ -class MemoryHook { -public: - virtual ~MemoryHook(); - - virtual std::optional IsValidAddress(VAddr addr) = 0; - - virtual std::optional Read8(VAddr addr) = 0; - virtual std::optional Read16(VAddr addr) = 0; - virtual std::optional Read32(VAddr addr) = 0; - virtual std::optional Read64(VAddr addr) = 0; - - virtual bool ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) = 0; - - virtual bool Write8(VAddr addr, u8 data) = 0; - virtual bool Write16(VAddr addr, u16 data) = 0; - virtual bool Write32(VAddr addr, u32 data) = 0; - virtual bool Write64(VAddr addr, u64 data) = 0; - - virtual bool WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size) = 0; -}; - -using MemoryHookPointer = std::shared_ptr; -} // namespace Common diff --git a/src/common/page_table.h b/src/common/page_table.h index 8d4ee9249..0c14e6433 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -8,7 +8,6 @@ #include #include "common/common_types.h" -#include "common/memory_hook.h" #include "common/virtual_buffer.h" namespace Common { @@ -23,23 +22,6 @@ enum class PageType : u8 { RasterizerCachedMemory, }; -struct SpecialRegion { - enum class Type { - DebugHook, - IODevice, - } type; - - MemoryHookPointer handler; - - [[nodiscard]] bool operator<(const SpecialRegion& other) const { - return std::tie(type, handler) < std::tie(other.type, other.handler); - } - - [[nodiscard]] bool operator==(const SpecialRegion& other) const { - return std::tie(type, handler) == std::tie(other.type, other.handler); - } -}; - /** * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely * mimics the way a real CPU page table works. diff --git a/src/core/memory.cpp b/src/core/memory.cpp index f209c4949..11609682a 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -44,27 +44,12 @@ struct Memory::Impl { MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, target, Common::PageType::Memory); } - void MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer mmio_handler) { - UNIMPLEMENTED(); - } - void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) { ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size); ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base); MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, 0, Common::PageType::Unmapped); } - void AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook) { - UNIMPLEMENTED(); - } - - void RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook) { - UNIMPLEMENTED(); - } - bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const { const auto& page_table = process.PageTable().PageTableImpl(); const auto [pointer, type] = page_table.pointers[vaddr >> PAGE_BITS].PointerType(); @@ -740,25 +725,10 @@ void Memory::MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size impl->MapMemoryRegion(page_table, base, size, target); } -void Memory::MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer mmio_handler) { - impl->MapIoRegion(page_table, base, size, std::move(mmio_handler)); -} - void Memory::UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) { impl->UnmapRegion(page_table, base, size); } -void Memory::AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook) { - impl->AddDebugHook(page_table, base, size, std::move(hook)); -} - -void Memory::RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook) { - impl->RemoveDebugHook(page_table, base, size, std::move(hook)); -} - bool Memory::IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const { return impl->IsValidVirtualAddress(process, vaddr); } diff --git a/src/core/memory.h b/src/core/memory.h index 4a1cc63f4..705ebb23d 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -8,7 +8,6 @@ #include #include #include "common/common_types.h" -#include "common/memory_hook.h" namespace Common { struct PageTable; @@ -77,17 +76,6 @@ public: */ void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, PAddr target); - /** - * Maps a region of the emulated process address space as a IO region. - * - * @param page_table The page table of the emulated process. - * @param base The address to start mapping at. Must be page-aligned. - * @param size The amount of bytes to map. Must be page-aligned. - * @param mmio_handler The handler that backs the mapping. - */ - void MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer mmio_handler); - /** * Unmaps a region of the emulated process address space. * @@ -97,28 +85,6 @@ public: */ void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size); - /** - * Adds a memory hook to intercept reads and writes to given region of memory. - * - * @param page_table The page table of the emulated process - * @param base The starting address to apply the hook to. - * @param size The size of the memory region to apply the hook to, in bytes. - * @param hook The hook to apply to the region of memory. - */ - void AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook); - - /** - * Removes a memory hook from a given range of memory. - * - * @param page_table The page table of the emulated process. - * @param base The starting address to remove the hook from. - * @param size The size of the memory region to remove the hook from, in bytes. - * @param hook The hook to remove from the specified region of memory. - */ - void RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size, - Common::MemoryHookPointer hook); - /** * Checks whether or not the supplied address is a valid virtual * address for the given process. diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt index d80b0b688..8a606b448 100644 --- a/src/tests/CMakeLists.txt +++ b/src/tests/CMakeLists.txt @@ -4,8 +4,6 @@ add_executable(tests common/fibers.cpp common/param_package.cpp common/ring_buffer.cpp - core/arm/arm_test_common.cpp - core/arm/arm_test_common.h core/core_timing.cpp tests.cpp ) diff --git a/src/tests/core/arm/arm_test_common.cpp b/src/tests/core/arm/arm_test_common.cpp deleted file mode 100644 index e54674d11..000000000 --- a/src/tests/core/arm/arm_test_common.cpp +++ /dev/null @@ -1,145 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include - -#include "common/page_table.h" -#include "core/core.h" -#include "core/hle/kernel/memory/page_table.h" -#include "core/hle/kernel/process.h" -#include "core/memory.h" -#include "tests/core/arm/arm_test_common.h" - -namespace ArmTests { - -TestEnvironment::TestEnvironment(bool mutable_memory_) - : mutable_memory(mutable_memory_), - test_memory(std::make_shared(this)), kernel{Core::System::GetInstance()} { - auto& system = Core::System::GetInstance(); - - auto process = Kernel::Process::Create(system, "", Kernel::Process::ProcessType::Userland); - page_table = &process->PageTable().PageTableImpl(); - - system.Memory().MapIoRegion(*page_table, 0x00000000, 0x80000000, test_memory); - system.Memory().MapIoRegion(*page_table, 0x80000000, 0x80000000, test_memory); - - kernel.MakeCurrentProcess(process.get()); -} - -TestEnvironment::~TestEnvironment() { - auto& system = Core::System::GetInstance(); - system.Memory().UnmapRegion(*page_table, 0x80000000, 0x80000000); - system.Memory().UnmapRegion(*page_table, 0x00000000, 0x80000000); -} - -void TestEnvironment::SetMemory64(VAddr vaddr, u64 value) { - SetMemory32(vaddr + 0, static_cast(value)); - SetMemory32(vaddr + 4, static_cast(value >> 32)); -} - -void TestEnvironment::SetMemory32(VAddr vaddr, u32 value) { - SetMemory16(vaddr + 0, static_cast(value)); - SetMemory16(vaddr + 2, static_cast(value >> 16)); -} - -void TestEnvironment::SetMemory16(VAddr vaddr, u16 value) { - SetMemory8(vaddr + 0, static_cast(value)); - SetMemory8(vaddr + 1, static_cast(value >> 8)); -} - -void TestEnvironment::SetMemory8(VAddr vaddr, u8 value) { - test_memory->data[vaddr] = value; -} - -std::vector TestEnvironment::GetWriteRecords() const { - return write_records; -} - -void TestEnvironment::ClearWriteRecords() { - write_records.clear(); -} - -TestEnvironment::TestMemory::~TestMemory() {} - -std::optional TestEnvironment::TestMemory::IsValidAddress(VAddr addr) { - return true; -} - -std::optional TestEnvironment::TestMemory::Read8(VAddr addr) { - const auto iter = data.find(addr); - - if (iter == data.end()) { - // Some arbitrary data - return static_cast(addr); - } - - return iter->second; -} - -std::optional TestEnvironment::TestMemory::Read16(VAddr addr) { - return *Read8(addr) | static_cast(*Read8(addr + 1)) << 8; -} - -std::optional TestEnvironment::TestMemory::Read32(VAddr addr) { - return *Read16(addr) | static_cast(*Read16(addr + 2)) << 16; -} - -std::optional TestEnvironment::TestMemory::Read64(VAddr addr) { - return *Read32(addr) | static_cast(*Read32(addr + 4)) << 32; -} - -bool TestEnvironment::TestMemory::ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) { - VAddr addr = src_addr; - u8* data = static_cast(dest_buffer); - - for (std::size_t i = 0; i < size; i++, addr++, data++) { - *data = *Read8(addr); - } - - return true; -} - -bool TestEnvironment::TestMemory::Write8(VAddr addr, u8 data) { - env->write_records.emplace_back(8, addr, data); - if (env->mutable_memory) - env->SetMemory8(addr, data); - return true; -} - -bool TestEnvironment::TestMemory::Write16(VAddr addr, u16 data) { - env->write_records.emplace_back(16, addr, data); - if (env->mutable_memory) - env->SetMemory16(addr, data); - return true; -} - -bool TestEnvironment::TestMemory::Write32(VAddr addr, u32 data) { - env->write_records.emplace_back(32, addr, data); - if (env->mutable_memory) - env->SetMemory32(addr, data); - return true; -} - -bool TestEnvironment::TestMemory::Write64(VAddr addr, u64 data) { - env->write_records.emplace_back(64, addr, data); - if (env->mutable_memory) - env->SetMemory64(addr, data); - return true; -} - -bool TestEnvironment::TestMemory::WriteBlock(VAddr dest_addr, const void* src_buffer, - std::size_t size) { - VAddr addr = dest_addr; - const u8* data = static_cast(src_buffer); - - for (std::size_t i = 0; i < size; i++, addr++, data++) { - env->write_records.emplace_back(8, addr, *data); - if (env->mutable_memory) - env->SetMemory8(addr, *data); - } - - return true; -} - -} // namespace ArmTests diff --git a/src/tests/core/arm/arm_test_common.h b/src/tests/core/arm/arm_test_common.h deleted file mode 100644 index d145dbfcc..000000000 --- a/src/tests/core/arm/arm_test_common.h +++ /dev/null @@ -1,93 +0,0 @@ -// Copyright 2016 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include -#include -#include - -#include "common/common_types.h" -#include "common/memory_hook.h" -#include "core/hle/kernel/kernel.h" - -namespace Common { -struct PageTable; -} - -namespace ArmTests { - -struct WriteRecord { - WriteRecord(std::size_t size, VAddr addr, u64 data) : size(size), addr(addr), data(data) {} - std::size_t size; - VAddr addr; - u64 data; - bool operator==(const WriteRecord& o) const { - return std::tie(size, addr, data) == std::tie(o.size, o.addr, o.data); - } -}; - -class TestEnvironment final { -public: - /* - * Inititalise test environment - * @param mutable_memory If false, writes to memory can never be read back. - * (Memory is immutable.) - */ - explicit TestEnvironment(bool mutable_memory = false); - - /// Shutdown test environment - ~TestEnvironment(); - - /// Sets value at memory location vaddr. - void SetMemory8(VAddr vaddr, u8 value); - void SetMemory16(VAddr vaddr, u16 value); - void SetMemory32(VAddr vaddr, u32 value); - void SetMemory64(VAddr vaddr, u64 value); - - /** - * Whenever Memory::Write{8,16,32,64} is called within the test environment, - * a new write-record is made. - * @returns A vector of write records made since they were last cleared. - */ - std::vector GetWriteRecords() const; - - /// Empties the internal write-record store. - void ClearWriteRecords(); - -private: - friend struct TestMemory; - struct TestMemory final : Common::MemoryHook { - explicit TestMemory(TestEnvironment* env_) : env(env_) {} - TestEnvironment* env; - - ~TestMemory() override; - - std::optional IsValidAddress(VAddr addr) override; - - std::optional Read8(VAddr addr) override; - std::optional Read16(VAddr addr) override; - std::optional Read32(VAddr addr) override; - std::optional Read64(VAddr addr) override; - - bool ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) override; - - bool Write8(VAddr addr, u8 data) override; - bool Write16(VAddr addr, u16 data) override; - bool Write32(VAddr addr, u32 data) override; - bool Write64(VAddr addr, u64 data) override; - - bool WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size) override; - - std::unordered_map data; - }; - - bool mutable_memory; - std::shared_ptr test_memory; - std::vector write_records; - Common::PageTable* page_table = nullptr; - Kernel::KernelCore kernel; -}; - -} // namespace ArmTests -- cgit v1.2.3 From a745d87971b2c9795e1b2c587bfe30b849b522fa Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sat, 2 Jan 2021 09:00:05 -0500 Subject: general: Fix various spelling errors --- src/common/page_table.h | 2 +- src/common/swap.h | 4 ++-- src/core/hle/kernel/memory/memory_block.h | 14 +++++++------- src/core/hle/kernel/memory/page_table.cpp | 12 ++++++------ src/core/hle/kernel/svc_types.h | 4 ++-- src/core/hle/service/am/am.cpp | 6 +++--- src/core/hle/service/am/am.h | 2 +- src/core/settings.h | 2 +- src/input_common/gcadapter/gc_adapter.h | 6 +++--- src/input_common/motion_input.cpp | 2 +- src/input_common/mouse/mouse_input.h | 2 +- src/input_common/udp/udp.cpp | 8 ++++---- src/tests/common/fibers.cpp | 4 ++-- src/video_core/command_classes/vic.cpp | 2 +- src/video_core/renderer_vulkan/vk_device.cpp | 2 +- src/yuzu/applets/error.cpp | 6 +++--- src/yuzu/compatdb.cpp | 2 +- src/yuzu/main.cpp | 2 +- src/yuzu_cmd/yuzu.cpp | 2 +- src/yuzu_tester/yuzu.cpp | 2 +- 20 files changed, 43 insertions(+), 43 deletions(-) (limited to 'src/common') diff --git a/src/common/page_table.h b/src/common/page_table.h index 0c14e6433..61c5552e0 100644 --- a/src/common/page_table.h +++ b/src/common/page_table.h @@ -90,7 +90,7 @@ struct PageTable { PageTable& operator=(PageTable&&) noexcept = default; /** - * Resizes the page table to be able to accomodate enough pages within + * Resizes the page table to be able to accommodate enough pages within * a given address space. * * @param address_space_width_in_bits The address size width in bits. diff --git a/src/common/swap.h b/src/common/swap.h index 7665942a2..a80e191dc 100644 --- a/src/common/swap.h +++ b/src/common/swap.h @@ -394,7 +394,7 @@ public: template friend S operator%(const S& p, const swapped_t v); - // Arithmetics + assignements + // Arithmetics + assignments template friend S operator+=(const S& p, const swapped_t v); @@ -451,7 +451,7 @@ S operator%(const S& i, const swap_struct_t v) { return i % v.swap(); } -// Arithmetics + assignements +// Arithmetics + assignments template S& operator+=(S& i, const swap_struct_t v) { i += v.swap(); diff --git a/src/core/hle/kernel/memory/memory_block.h b/src/core/hle/kernel/memory/memory_block.h index 37fe19916..83acece1e 100644 --- a/src/core/hle/kernel/memory/memory_block.h +++ b/src/core/hle/kernel/memory/memory_block.h @@ -73,12 +73,12 @@ enum class MemoryState : u32 { ThreadLocal = static_cast(Svc::MemoryState::ThreadLocal) | FlagMapped | FlagReferenceCounted, - Transfered = static_cast(Svc::MemoryState::Transfered) | FlagsMisc | - FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc | - FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, + Transferred = static_cast(Svc::MemoryState::Transferred) | FlagsMisc | + FlagCanAlignedDeviceMap | FlagCanChangeAttribute | FlagCanUseIpc | + FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, - SharedTransfered = static_cast(Svc::MemoryState::SharedTransfered) | FlagsMisc | - FlagCanAlignedDeviceMap | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, + SharedTransferred = static_cast(Svc::MemoryState::SharedTransferred) | FlagsMisc | + FlagCanAlignedDeviceMap | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, SharedCode = static_cast(Svc::MemoryState::SharedCode) | FlagMapped | FlagReferenceCounted | FlagCanUseNonSecureIpc | FlagCanUseNonDeviceIpc, @@ -111,8 +111,8 @@ static_assert(static_cast(MemoryState::AliasCodeData) == 0x03FFBD09); static_assert(static_cast(MemoryState::Ipc) == 0x005C3C0A); static_assert(static_cast(MemoryState::Stack) == 0x005C3C0B); static_assert(static_cast(MemoryState::ThreadLocal) == 0x0040200C); -static_assert(static_cast(MemoryState::Transfered) == 0x015C3C0D); -static_assert(static_cast(MemoryState::SharedTransfered) == 0x005C380E); +static_assert(static_cast(MemoryState::Transferred) == 0x015C3C0D); +static_assert(static_cast(MemoryState::SharedTransferred) == 0x005C380E); static_assert(static_cast(MemoryState::SharedCode) == 0x0040380F); static_assert(static_cast(MemoryState::Inaccessible) == 0x00000010); static_assert(static_cast(MemoryState::NonSecureIpc) == 0x005C3811); diff --git a/src/core/hle/kernel/memory/page_table.cpp b/src/core/hle/kernel/memory/page_table.cpp index f3e8bc333..080886554 100644 --- a/src/core/hle/kernel/memory/page_table.cpp +++ b/src/core/hle/kernel/memory/page_table.cpp @@ -1007,8 +1007,8 @@ constexpr VAddr PageTable::GetRegionAddress(MemoryState state) const { case MemoryState::Shared: case MemoryState::AliasCode: case MemoryState::AliasCodeData: - case MemoryState::Transfered: - case MemoryState::SharedTransfered: + case MemoryState::Transferred: + case MemoryState::SharedTransferred: case MemoryState::SharedCode: case MemoryState::GeneratedCode: case MemoryState::CodeOut: @@ -1042,8 +1042,8 @@ constexpr std::size_t PageTable::GetRegionSize(MemoryState state) const { case MemoryState::Shared: case MemoryState::AliasCode: case MemoryState::AliasCodeData: - case MemoryState::Transfered: - case MemoryState::SharedTransfered: + case MemoryState::Transferred: + case MemoryState::SharedTransferred: case MemoryState::SharedCode: case MemoryState::GeneratedCode: case MemoryState::CodeOut: @@ -1080,8 +1080,8 @@ constexpr bool PageTable::CanContain(VAddr addr, std::size_t size, MemoryState s case MemoryState::AliasCodeData: case MemoryState::Stack: case MemoryState::ThreadLocal: - case MemoryState::Transfered: - case MemoryState::SharedTransfered: + case MemoryState::Transferred: + case MemoryState::SharedTransferred: case MemoryState::SharedCode: case MemoryState::GeneratedCode: case MemoryState::CodeOut: diff --git a/src/core/hle/kernel/svc_types.h b/src/core/hle/kernel/svc_types.h index 986724beb..11e1d8e2d 100644 --- a/src/core/hle/kernel/svc_types.h +++ b/src/core/hle/kernel/svc_types.h @@ -23,8 +23,8 @@ enum class MemoryState : u32 { Ipc = 0x0A, Stack = 0x0B, ThreadLocal = 0x0C, - Transfered = 0x0D, - SharedTransfered = 0x0E, + Transferred = 0x0D, + SharedTransferred = 0x0E, SharedCode = 0x0F, Inaccessible = 0x10, NonSecureIpc = 0x11, diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index cb13210e5..c9808060a 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -560,14 +560,14 @@ void ISelfController::GetAccumulatedSuspendedTickChangedEvent(Kernel::HLERequest AppletMessageQueue::AppletMessageQueue(Kernel::KernelCore& kernel) { on_new_message = - Kernel::WritableEvent::CreateEventPair(kernel, "AMMessageQueue:OnMessageRecieved"); + Kernel::WritableEvent::CreateEventPair(kernel, "AMMessageQueue:OnMessageReceived"); on_operation_mode_changed = Kernel::WritableEvent::CreateEventPair(kernel, "AMMessageQueue:OperationModeChanged"); } AppletMessageQueue::~AppletMessageQueue() = default; -const std::shared_ptr& AppletMessageQueue::GetMesssageRecieveEvent() const { +const std::shared_ptr& AppletMessageQueue::GetMessageReceiveEvent() const { return on_new_message.readable; } @@ -675,7 +675,7 @@ void ICommonStateGetter::GetEventHandle(Kernel::HLERequestContext& ctx) { IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(msg_queue->GetMesssageRecieveEvent()); + rb.PushCopyObjects(msg_queue->GetMessageReceiveEvent()); } void ICommonStateGetter::ReceiveMessage(Kernel::HLERequestContext& ctx) { diff --git a/src/core/hle/service/am/am.h b/src/core/hle/service/am/am.h index b1da0d081..f51aca1af 100644 --- a/src/core/hle/service/am/am.h +++ b/src/core/hle/service/am/am.h @@ -55,7 +55,7 @@ public: explicit AppletMessageQueue(Kernel::KernelCore& kernel); ~AppletMessageQueue(); - const std::shared_ptr& GetMesssageRecieveEvent() const; + const std::shared_ptr& GetMessageReceiveEvent() const; const std::shared_ptr& GetOperationModeChangedEvent() const; void PushMessage(AppletMessage msg); AppletMessage PopMessage(); diff --git a/src/core/settings.h b/src/core/settings.h index 0cd3c0c84..1cb7ff7f5 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -221,7 +221,7 @@ struct Values { bool disable_macro_jit; bool extended_logging; - // Misceallaneous + // Miscellaneous std::string log_filter; bool use_dev_keys; diff --git a/src/input_common/gcadapter/gc_adapter.h b/src/input_common/gcadapter/gc_adapter.h index f1256c9da..7a6c545bd 100644 --- a/src/input_common/gcadapter/gc_adapter.h +++ b/src/input_common/gcadapter/gc_adapter.h @@ -120,17 +120,17 @@ private: /// For use in initialization, querying devices to find the adapter void Setup(); - /// Resets status of all GC controller devices to a disconected state + /// Resets status of all GC controller devices to a disconnected state void ResetDevices(); - /// Resets status of device connected to a disconected state + /// Resets status of device connected to a disconnected state void ResetDevice(std::size_t port); /// Returns true if we successfully gain access to GC Adapter bool CheckDeviceAccess(); /// Captures GC Adapter endpoint address - /// Returns true if the endpoind was set correctly + /// Returns true if the endpoint was set correctly bool GetGCEndpoint(libusb_device* device); /// For shutting down, clear all data, join all threads, release usb diff --git a/src/input_common/motion_input.cpp b/src/input_common/motion_input.cpp index f77ba535d..6a65f175e 100644 --- a/src/input_common/motion_input.cpp +++ b/src/input_common/motion_input.cpp @@ -129,7 +129,7 @@ void MotionInput::UpdateOrientation(u64 elapsed_time) { rad_gyro += ki * integral_error; rad_gyro += kd * derivative_error; } else { - // Give more weight to acelerometer values to compensate for the lack of gyro + // Give more weight to accelerometer values to compensate for the lack of gyro rad_gyro += 35.0f * kp * real_error; rad_gyro += 10.0f * ki * integral_error; rad_gyro += 10.0f * kd * derivative_error; diff --git a/src/input_common/mouse/mouse_input.h b/src/input_common/mouse/mouse_input.h index 65e64bee7..58803c1bf 100644 --- a/src/input_common/mouse/mouse_input.h +++ b/src/input_common/mouse/mouse_input.h @@ -20,7 +20,7 @@ enum class MouseButton { Left, Wheel, Right, - Foward, + Forward, Backward, Undefined, }; diff --git a/src/input_common/udp/udp.cpp b/src/input_common/udp/udp.cpp index 8686a059c..c5da27a38 100644 --- a/src/input_common/udp/udp.cpp +++ b/src/input_common/udp/udp.cpp @@ -28,14 +28,14 @@ private: mutable std::mutex mutex; }; -/// A motion device factory that creates motion devices from JC Adapter +/// A motion device factory that creates motion devices from a UDP client UDPMotionFactory::UDPMotionFactory(std::shared_ptr client_) : client(std::move(client_)) {} /** * Creates motion device * @param params contains parameters for creating the device: - * - "port": the nth jcpad on the adapter + * - "port": the UDP port number */ std::unique_ptr UDPMotionFactory::Create(const Common::ParamPackage& params) { auto ip = params.Get("ip", "127.0.0.1"); @@ -90,14 +90,14 @@ private: mutable std::mutex mutex; }; -/// A motion device factory that creates motion devices from JC Adapter +/// A motion device factory that creates motion devices from a UDP client UDPTouchFactory::UDPTouchFactory(std::shared_ptr client_) : client(std::move(client_)) {} /** * Creates motion device * @param params contains parameters for creating the device: - * - "port": the nth jcpad on the adapter + * - "port": the UDP port number */ std::unique_ptr UDPTouchFactory::Create(const Common::ParamPackage& params) { auto ip = params.Get("ip", "127.0.0.1"); diff --git a/src/tests/common/fibers.cpp b/src/tests/common/fibers.cpp index 4757dd2b4..d94492fc6 100644 --- a/src/tests/common/fibers.cpp +++ b/src/tests/common/fibers.cpp @@ -207,7 +207,7 @@ static void ThreadStart2_2(u32 id, TestControl2& test_control) { } /** This test checks for fiber thread exchange configuration and validates that fibers are - * that a fiber has been succesfully transfered from one thread to another and that the TLS + * that a fiber has been successfully transferred from one thread to another and that the TLS * region of the thread is kept while changing fibers. */ TEST_CASE("Fibers::InterExchange", "[common]") { @@ -299,7 +299,7 @@ static void ThreadStart3(u32 id, TestControl3& test_control) { } /** This test checks for one two threads racing for starting the same fiber. - * It checks execution occured in an ordered manner and by no time there were + * It checks execution occurred in an ordered manner and by no time there were * two contexts at the same time. */ TEST_CASE("Fibers::StartRace", "[common]") { diff --git a/src/video_core/command_classes/vic.cpp b/src/video_core/command_classes/vic.cpp index aa8c9f9de..55e632346 100644 --- a/src/video_core/command_classes/vic.cpp +++ b/src/video_core/command_classes/vic.cpp @@ -53,7 +53,7 @@ void Vic::ProcessMethod(Method method, const std::vector& arguments) { void Vic::Execute() { if (output_surface_luma_address == 0) { - LOG_ERROR(Service_NVDRV, "VIC Luma address not set. Recieved 0x{:X}", + LOG_ERROR(Service_NVDRV, "VIC Luma address not set. Received 0x{:X}", vic_state.output_surface.luma_offset); return; } diff --git a/src/video_core/renderer_vulkan/vk_device.cpp b/src/video_core/renderer_vulkan/vk_device.cpp index 370a63f74..85b4f0dff 100644 --- a/src/video_core/renderer_vulkan/vk_device.cpp +++ b/src/video_core/renderer_vulkan/vk_device.cpp @@ -491,7 +491,7 @@ VkFormat VKDevice::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFla } void VKDevice::ReportLoss() const { - LOG_CRITICAL(Render_Vulkan, "Device loss occured!"); + LOG_CRITICAL(Render_Vulkan, "Device loss occurred!"); // Wait for the log to flush and for Nsight Aftermath to dump the results std::this_thread::sleep_for(std::chrono::seconds{15}); diff --git a/src/yuzu/applets/error.cpp b/src/yuzu/applets/error.cpp index 53a993cf6..8ee03ddb3 100644 --- a/src/yuzu/applets/error.cpp +++ b/src/yuzu/applets/error.cpp @@ -19,7 +19,7 @@ QtErrorDisplay::~QtErrorDisplay() = default; void QtErrorDisplay::ShowError(ResultCode error, std::function finished) const { callback = std::move(finished); emit MainWindowDisplayError( - tr("An error has occured.\nPlease try again or contact the developer of the " + tr("An error has occurred.\nPlease try again or contact the developer of the " "software.\n\nError Code: %1-%2 (0x%3)") .arg(static_cast(error.module.Value()) + 2000, 4, 10, QChar::fromLatin1('0')) .arg(error.description, 4, 10, QChar::fromLatin1('0')) @@ -32,7 +32,7 @@ void QtErrorDisplay::ShowErrorWithTimestamp(ResultCode error, std::chrono::secon const QDateTime date_time = QDateTime::fromSecsSinceEpoch(time.count()); emit MainWindowDisplayError( - tr("An error occured on %1 at %2.\nPlease try again or contact the " + tr("An error occurred on %1 at %2.\nPlease try again or contact the " "developer of the software.\n\nError Code: %3-%4 (0x%5)") .arg(date_time.toString(QStringLiteral("dddd, MMMM d, yyyy"))) .arg(date_time.toString(QStringLiteral("h:mm:ss A"))) @@ -46,7 +46,7 @@ void QtErrorDisplay::ShowCustomErrorText(ResultCode error, std::string dialog_te std::function finished) const { callback = std::move(finished); emit MainWindowDisplayError( - tr("An error has occured.\nError Code: %1-%2 (0x%3)\n\n%4\n\n%5") + tr("An error has occurred.\nError Code: %1-%2 (0x%3)\n\n%4\n\n%5") .arg(static_cast(error.module.Value()) + 2000, 4, 10, QChar::fromLatin1('0')) .arg(error.description, 4, 10, QChar::fromLatin1('0')) .arg(error.raw, 8, 16, QChar::fromLatin1('0')) diff --git a/src/yuzu/compatdb.cpp b/src/yuzu/compatdb.cpp index 649912557..a470056ef 100644 --- a/src/yuzu/compatdb.cpp +++ b/src/yuzu/compatdb.cpp @@ -72,7 +72,7 @@ void CompatDB::Submit() { void CompatDB::OnTestcaseSubmitted() { if (!testcase_watcher.result()) { QMessageBox::critical(this, tr("Communication error"), - tr("An error occured while sending the Testcase")); + tr("An error occurred while sending the Testcase")); button(NextButton)->setEnabled(true); button(NextButton)->setText(tr("Next")); button(CancelButton)->setVisible(true); diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index ab66d7f93..f39da90ba 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -142,7 +142,7 @@ constexpr int default_mouse_timeout = 2500; /** * "Callouts" are one-time instructional messages shown to the user. In the config settings, there * is a bitfield "callout_flags" options, used to track if a message has already been shown to the - * user. This is 32-bits - if we have more than 32 callouts, we should retire and recyle old ones. + * user. This is 32-bits - if we have more than 32 callouts, we should retire and recycle old ones. */ enum class CalloutFlag : uint32_t { Telemetry = 0x1, diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp index 2497c71ae..39e0d35aa 100644 --- a/src/yuzu_cmd/yuzu.cpp +++ b/src/yuzu_cmd/yuzu.cpp @@ -202,7 +202,7 @@ int main(int argc, char** argv) { const u16 loader_id = static_cast(Core::System::ResultStatus::ErrorLoader); const u16 error_id = static_cast(load_result) - loader_id; LOG_CRITICAL(Frontend, - "While attempting to load the ROM requested, an error occured. Please " + "While attempting to load the ROM requested, an error occurred. Please " "refer to the yuzu wiki for more information or the yuzu discord for " "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}", loader_id, error_id, static_cast(error_id)); diff --git a/src/yuzu_tester/yuzu.cpp b/src/yuzu_tester/yuzu.cpp index 6435ffabb..09cf2ad77 100644 --- a/src/yuzu_tester/yuzu.cpp +++ b/src/yuzu_tester/yuzu.cpp @@ -242,7 +242,7 @@ int main(int argc, char** argv) { const u16 loader_id = static_cast(Core::System::ResultStatus::ErrorLoader); const u16 error_id = static_cast(load_result) - loader_id; LOG_CRITICAL(Frontend, - "While attempting to load the ROM requested, an error occured. Please " + "While attempting to load the ROM requested, an error occurred. Please " "refer to the yuzu wiki for more information or the yuzu discord for " "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}", loader_id, error_id, static_cast(error_id)); -- cgit v1.2.3 From c19058659770fe4b235859aa4c617ae56947051d Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Sat, 9 Jan 2021 03:14:18 -0300 Subject: common/div_ceil: Return numerator type Fixes instances where DivCeil(u32, u64) would surprisingly return u64, instead of the more natural u32. --- src/common/div_ceil.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/common') diff --git a/src/common/div_ceil.h b/src/common/div_ceil.h index 6b2c48f91..95e1489a9 100644 --- a/src/common/div_ceil.h +++ b/src/common/div_ceil.h @@ -11,16 +11,16 @@ namespace Common { /// Ceiled integer division. template -requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeil( - N number, D divisor) { - return (static_cast(number) + divisor - 1) / divisor; +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr N DivCeil(N number, + D divisor) { + return static_cast((static_cast(number) + divisor - 1) / divisor); } /// Ceiled integer division with logarithmic divisor in base 2 template -requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr auto DivCeilLog2( +requires std::is_integral_v&& std::is_unsigned_v[[nodiscard]] constexpr N DivCeilLog2( N value, D alignment_log2) { - return (static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2; + return static_cast((static_cast(value) + (D(1) << alignment_log2) - 1) >> alignment_log2); } } // namespace Common -- cgit v1.2.3