summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt8
-rw-r--r--externals/httplib/README.md2
-rw-r--r--externals/httplib/httplib.h2419
-rw-r--r--src/audio_core/algorithm/interpolate.cpp198
-rw-r--r--src/audio_core/algorithm/interpolate.h9
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/core.cpp8
-rw-r--r--src/core/core.h6
-rw-r--r--src/core/frontend/framebuffer_layout.cpp21
-rw-r--r--src/core/frontend/framebuffer_layout.h15
-rw-r--r--src/core/hardware_properties.h2
-rw-r--r--src/core/hle/kernel/kernel.cpp121
-rw-r--r--src/core/hle/kernel/kernel.h37
-rw-r--r--src/core/hle/kernel/scheduler.cpp56
-rw-r--r--src/core/hle/kernel/scheduler.h46
-rw-r--r--src/core/hle/kernel/thread.cpp12
-rw-r--r--src/core/hle/kernel/thread.h6
-rw-r--r--src/core/hle/kernel/time_manager.cpp44
-rw-r--r--src/core/hle/kernel/time_manager.h43
-rw-r--r--src/core/hle/service/bcat/backend/boxcat.cpp7
-rw-r--r--src/core/hle/service/ldn/ldn.cpp10
-rw-r--r--src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp12
-rw-r--r--src/core/hle/service/nvdrv/devices/nvhost_gpu.h8
-rw-r--r--src/core/settings.h1
-rw-r--r--src/video_core/CMakeLists.txt5
-rw-r--r--src/video_core/engines/maxwell_3d.cpp81
-rw-r--r--src/video_core/engines/maxwell_3d.h51
-rw-r--r--src/video_core/gpu.cpp2
-rw-r--r--src/video_core/memory_manager.cpp17
-rw-r--r--src/video_core/memory_manager.h7
-rw-r--r--src/video_core/query_cache.h359
-rw-r--r--src/video_core/rasterizer_interface.h19
-rw-r--r--src/video_core/renderer_opengl/gl_query_cache.cpp120
-rw-r--r--src/video_core/renderer_opengl/gl_query_cache.h78
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp78
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h47
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.cpp17
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.h25
-rw-r--r--src/video_core/renderer_opengl/gl_texture_cache.cpp35
-rw-r--r--src/video_core/renderer_vulkan/maxwell_to_vk.cpp4
-rw-r--r--src/video_core/renderer_vulkan/vk_device.cpp15
-rw-r--r--src/video_core/renderer_vulkan/vk_device.h7
-rw-r--r--src/video_core/renderer_vulkan/vk_query_cache.cpp122
-rw-r--r--src/video_core/renderer_vulkan/vk_query_cache.h104
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.cpp48
-rw-r--r--src/video_core/renderer_vulkan/vk_rasterizer.h21
-rw-r--r--src/video_core/renderer_vulkan/vk_sampler_cache.cpp11
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.cpp8
-rw-r--r--src/video_core/renderer_vulkan/vk_scheduler.h15
-rw-r--r--src/video_core/renderer_vulkan/vk_shader_decompiler.cpp69
-rw-r--r--src/video_core/renderer_vulkan/vk_texture_cache.h4
-rw-r--r--src/video_core/shader/decode/conversion.cpp14
-rw-r--r--src/video_core/shader/decode/texture.cpp71
-rw-r--r--src/video_core/texture_cache/surface_base.cpp4
-rw-r--r--src/video_core/texture_cache/surface_params.cpp47
-rw-r--r--src/video_core/texture_cache/surface_params.h5
-rw-r--r--src/video_core/texture_cache/texture_cache.h24
-rw-r--r--src/web_service/web_backend.cpp7
-rw-r--r--src/yuzu/configuration/config.cpp2
-rw-r--r--src/yuzu/configuration/configure_graphics.cpp2
-rw-r--r--src/yuzu/configuration/configure_graphics.ui35
-rw-r--r--src/yuzu_cmd/config.cpp2
-rw-r--r--src/yuzu_cmd/default_ini.h4
-rw-r--r--src/yuzu_tester/config.cpp2
-rw-r--r--src/yuzu_tester/default_ini.h4
65 files changed, 3687 insertions, 998 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 44ed4196d..467d769a2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -157,8 +157,14 @@ if (ENABLE_SDL2)
157 target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}") 157 target_include_directories(SDL2 INTERFACE "${SDL2_INCLUDE_DIR}")
158 else() 158 else()
159 find_package(SDL2 REQUIRED) 159 find_package(SDL2 REQUIRED)
160 include_directories(${SDL2_INCLUDE_DIRS})
161 160
161 # Some installations don't set SDL2_LIBRARIES
162 if("${SDL2_LIBRARIES}" STREQUAL "")
163 message(WARNING "SDL2_LIBRARIES wasn't set, manually setting to SDL2::SDL2")
164 set(SDL2_LIBRARIES "SDL2::SDL2")
165 endif()
166
167 include_directories(${SDL2_INCLUDE_DIRS})
162 add_library(SDL2 INTERFACE) 168 add_library(SDL2 INTERFACE)
163 target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}") 169 target_link_libraries(SDL2 INTERFACE "${SDL2_LIBRARIES}")
164 endif() 170 endif()
diff --git a/externals/httplib/README.md b/externals/httplib/README.md
index 0e26522b5..73037d297 100644
--- a/externals/httplib/README.md
+++ b/externals/httplib/README.md
@@ -1,4 +1,4 @@
1From https://github.com/yhirose/cpp-httplib/commit/d9479bc0b12e8a1e8bce2d34da4feeef488581f3 1From https://github.com/yhirose/cpp-httplib/tree/fce8e6fefdab4ad48bc5b25c98e5ebfda4f3cf53
2 2
3MIT License 3MIT License
4 4
diff --git a/externals/httplib/httplib.h b/externals/httplib/httplib.h
index fa2edcc94..e03842e6d 100644
--- a/externals/httplib/httplib.h
+++ b/externals/httplib/httplib.h
@@ -1,7 +1,7 @@
1// 1//
2// httplib.h 2// httplib.h
3// 3//
4// Copyright (c) 2019 Yuji Hirose. All rights reserved. 4// Copyright (c) 2020 Yuji Hirose. All rights reserved.
5// MIT License 5// MIT License
6// 6//
7 7
@@ -11,6 +11,7 @@
11/* 11/*
12 * Configuration 12 * Configuration
13 */ 13 */
14
14#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 15#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND
15#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 16#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
16#endif 17#endif
@@ -40,7 +41,7 @@
40#endif 41#endif
41 42
42#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH 43#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH
43#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max)() 44#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max())
44#endif 45#endif
45 46
46#ifndef CPPHTTPLIB_RECV_BUFSIZ 47#ifndef CPPHTTPLIB_RECV_BUFSIZ
@@ -48,9 +49,14 @@
48#endif 49#endif
49 50
50#ifndef CPPHTTPLIB_THREAD_POOL_COUNT 51#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
51#define CPPHTTPLIB_THREAD_POOL_COUNT 8 52#define CPPHTTPLIB_THREAD_POOL_COUNT \
53 (std::max(1u, std::thread::hardware_concurrency() - 1))
52#endif 54#endif
53 55
56/*
57 * Headers
58 */
59
54#ifdef _WIN32 60#ifdef _WIN32
55#ifndef _CRT_SECURE_NO_WARNINGS 61#ifndef _CRT_SECURE_NO_WARNINGS
56#define _CRT_SECURE_NO_WARNINGS 62#define _CRT_SECURE_NO_WARNINGS
@@ -62,9 +68,9 @@
62 68
63#if defined(_MSC_VER) 69#if defined(_MSC_VER)
64#ifdef _WIN64 70#ifdef _WIN64
65typedef __int64 ssize_t; 71using ssize_t = __int64;
66#else 72#else
67typedef int ssize_t; 73using ssize_t = int;
68#endif 74#endif
69 75
70#if _MSC_VER < 1900 76#if _MSC_VER < 1900
@@ -100,7 +106,7 @@ typedef int ssize_t;
100#define strcasecmp _stricmp 106#define strcasecmp _stricmp
101#endif // strcasecmp 107#endif // strcasecmp
102 108
103typedef SOCKET socket_t; 109using socket_t = SOCKET;
104#ifdef CPPHTTPLIB_USE_POLL 110#ifdef CPPHTTPLIB_USE_POLL
105#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout) 111#define poll(fds, nfds, timeout) WSAPoll(fds, nfds, timeout)
106#endif 112#endif
@@ -109,23 +115,25 @@ typedef SOCKET socket_t;
109 115
110#include <arpa/inet.h> 116#include <arpa/inet.h>
111#include <cstring> 117#include <cstring>
118#include <ifaddrs.h>
112#include <netdb.h> 119#include <netdb.h>
113#include <netinet/in.h> 120#include <netinet/in.h>
114#ifdef CPPHTTPLIB_USE_POLL 121#ifdef CPPHTTPLIB_USE_POLL
115#include <poll.h> 122#include <poll.h>
116#endif 123#endif
124#include <csignal>
117#include <pthread.h> 125#include <pthread.h>
118#include <signal.h>
119#include <sys/select.h> 126#include <sys/select.h>
120#include <sys/socket.h> 127#include <sys/socket.h>
121#include <unistd.h> 128#include <unistd.h>
122 129
123typedef int socket_t; 130using socket_t = int;
124#define INVALID_SOCKET (-1) 131#define INVALID_SOCKET (-1)
125#endif //_WIN32 132#endif //_WIN32
126 133
127#include <assert.h> 134#include <array>
128#include <atomic> 135#include <atomic>
136#include <cassert>
129#include <condition_variable> 137#include <condition_variable>
130#include <errno.h> 138#include <errno.h>
131#include <fcntl.h> 139#include <fcntl.h>
@@ -143,9 +151,13 @@ typedef int socket_t;
143 151
144#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 152#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
145#include <openssl/err.h> 153#include <openssl/err.h>
154#include <openssl/md5.h>
146#include <openssl/ssl.h> 155#include <openssl/ssl.h>
147#include <openssl/x509v3.h> 156#include <openssl/x509v3.h>
148 157
158#include <iomanip>
159#include <sstream>
160
149// #if OPENSSL_VERSION_NUMBER < 0x1010100fL 161// #if OPENSSL_VERSION_NUMBER < 0x1010100fL
150// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported 162// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported
151// #endif 163// #endif
@@ -162,6 +174,9 @@ inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) {
162#include <zlib.h> 174#include <zlib.h>
163#endif 175#endif
164 176
177/*
178 * Declaration
179 */
165namespace httplib { 180namespace httplib {
166 181
167namespace detail { 182namespace detail {
@@ -176,37 +191,15 @@ struct ci {
176 191
177} // namespace detail 192} // namespace detail
178 193
179enum class HttpVersion { v1_0 = 0, v1_1 }; 194using Headers = std::multimap<std::string, std::string, detail::ci>;
180
181typedef std::multimap<std::string, std::string, detail::ci> Headers;
182
183typedef std::multimap<std::string, std::string> Params;
184typedef std::smatch Match;
185
186typedef std::function<void(const char *data, size_t data_len)> DataSink;
187
188typedef std::function<void()> Done;
189
190typedef std::function<void(size_t offset, size_t length, DataSink sink,
191 Done done)>
192 ContentProvider;
193 195
194typedef std::function<bool(const char *data, size_t data_length, size_t offset, 196using Params = std::multimap<std::string, std::string>;
195 uint64_t content_length)> 197using Match = std::smatch;
196 ContentReceiver;
197 198
198typedef std::function<bool(uint64_t current, uint64_t total)> Progress; 199using Progress = std::function<bool(uint64_t current, uint64_t total)>;
199 200
200struct Response; 201struct Response;
201typedef std::function<bool(const Response &response)> ResponseHandler; 202using ResponseHandler = std::function<bool(const Response &response)>;
202
203struct MultipartFile {
204 std::string filename;
205 std::string content_type;
206 size_t offset = 0;
207 size_t length = 0;
208};
209typedef std::multimap<std::string, MultipartFile> MultipartFiles;
210 203
211struct MultipartFormData { 204struct MultipartFormData {
212 std::string name; 205 std::string name;
@@ -214,10 +207,53 @@ struct MultipartFormData {
214 std::string filename; 207 std::string filename;
215 std::string content_type; 208 std::string content_type;
216}; 209};
217typedef std::vector<MultipartFormData> MultipartFormDataItems; 210using MultipartFormDataItems = std::vector<MultipartFormData>;
211using MultipartFormDataMap = std::multimap<std::string, MultipartFormData>;
212
213class DataSink {
214public:
215 DataSink() = default;
216 DataSink(const DataSink &) = delete;
217 DataSink &operator=(const DataSink &) = delete;
218 DataSink(DataSink &&) = delete;
219 DataSink &operator=(DataSink &&) = delete;
220
221 std::function<void(const char *data, size_t data_len)> write;
222 std::function<void()> done;
223 std::function<bool()> is_writable;
224};
225
226using ContentProvider =
227 std::function<void(size_t offset, size_t length, DataSink &sink)>;
228
229using ContentReceiver =
230 std::function<bool(const char *data, size_t data_length)>;
231
232using MultipartContentHeader =
233 std::function<bool(const MultipartFormData &file)>;
234
235class ContentReader {
236public:
237 using Reader = std::function<bool(ContentReceiver receiver)>;
238 using MultipartReader = std::function<bool(MultipartContentHeader header,
239 ContentReceiver receiver)>;
240
241 ContentReader(Reader reader, MultipartReader muitlpart_reader)
242 : reader_(reader), muitlpart_reader_(muitlpart_reader) {}
243
244 bool operator()(MultipartContentHeader header,
245 ContentReceiver receiver) const {
246 return muitlpart_reader_(header, receiver);
247 }
248
249 bool operator()(ContentReceiver receiver) const { return reader_(receiver); }
250
251 Reader reader_;
252 MultipartReader muitlpart_reader_;
253};
218 254
219typedef std::pair<ssize_t, ssize_t> Range; 255using Range = std::pair<ssize_t, ssize_t>;
220typedef std::vector<Range> Ranges; 256using Ranges = std::vector<Range>;
221 257
222struct Request { 258struct Request {
223 std::string method; 259 std::string method;
@@ -229,7 +265,7 @@ struct Request {
229 std::string version; 265 std::string version;
230 std::string target; 266 std::string target;
231 Params params; 267 Params params;
232 MultipartFiles files; 268 MultipartFormDataMap files;
233 Ranges ranges; 269 Ranges ranges;
234 Match matches; 270 Match matches;
235 271
@@ -253,13 +289,19 @@ struct Request {
253 std::string get_param_value(const char *key, size_t id = 0) const; 289 std::string get_param_value(const char *key, size_t id = 0) const;
254 size_t get_param_value_count(const char *key) const; 290 size_t get_param_value_count(const char *key) const;
255 291
292 bool is_multipart_form_data() const;
293
256 bool has_file(const char *key) const; 294 bool has_file(const char *key) const;
257 MultipartFile get_file_value(const char *key) const; 295 MultipartFormData get_file_value(const char *key) const;
296
297 // private members...
298 size_t content_length;
299 ContentProvider content_provider;
258}; 300};
259 301
260struct Response { 302struct Response {
261 std::string version; 303 std::string version;
262 int status; 304 int status = -1;
263 Headers headers; 305 Headers headers;
264 std::string body; 306 std::string body;
265 307
@@ -269,106 +311,81 @@ struct Response {
269 void set_header(const char *key, const char *val); 311 void set_header(const char *key, const char *val);
270 void set_header(const char *key, const std::string &val); 312 void set_header(const char *key, const std::string &val);
271 313
272 void set_redirect(const char *uri); 314 void set_redirect(const char *url);
273 void set_content(const char *s, size_t n, const char *content_type); 315 void set_content(const char *s, size_t n, const char *content_type);
274 void set_content(const std::string &s, const char *content_type); 316 void set_content(const std::string &s, const char *content_type);
275 317
276 void set_content_provider( 318 void set_content_provider(
277 size_t length, 319 size_t length,
278 std::function<void(size_t offset, size_t length, DataSink sink)> provider, 320 std::function<void(size_t offset, size_t length, DataSink &sink)>
321 provider,
279 std::function<void()> resource_releaser = [] {}); 322 std::function<void()> resource_releaser = [] {});
280 323
281 void set_chunked_content_provider( 324 void set_chunked_content_provider(
282 std::function<void(size_t offset, DataSink sink, Done done)> provider, 325 std::function<void(size_t offset, DataSink &sink)> provider,
283 std::function<void()> resource_releaser = [] {}); 326 std::function<void()> resource_releaser = [] {});
284 327
285 Response() : status(-1), content_provider_resource_length(0) {} 328 Response() = default;
286 329 Response(const Response &) = default;
330 Response &operator=(const Response &) = default;
331 Response(Response &&) = default;
332 Response &operator=(Response &&) = default;
287 ~Response() { 333 ~Response() {
288 if (content_provider_resource_releaser) { 334 if (content_provider_resource_releaser) {
289 content_provider_resource_releaser(); 335 content_provider_resource_releaser();
290 } 336 }
291 } 337 }
292 338
293 size_t content_provider_resource_length; 339 // private members...
340 size_t content_length = 0;
294 ContentProvider content_provider; 341 ContentProvider content_provider;
295 std::function<void()> content_provider_resource_releaser; 342 std::function<void()> content_provider_resource_releaser;
296}; 343};
297 344
298class Stream { 345class Stream {
299public: 346public:
300 virtual ~Stream() {} 347 virtual ~Stream() = default;
348
349 virtual bool is_readable() const = 0;
350 virtual bool is_writable() const = 0;
351
301 virtual int read(char *ptr, size_t size) = 0; 352 virtual int read(char *ptr, size_t size) = 0;
302 virtual int write(const char *ptr, size_t size1) = 0; 353 virtual int write(const char *ptr, size_t size) = 0;
303 virtual int write(const char *ptr) = 0;
304 virtual int write(const std::string &s) = 0;
305 virtual std::string get_remote_addr() const = 0; 354 virtual std::string get_remote_addr() const = 0;
306 355
307 template <typename... Args> 356 template <typename... Args>
308 int write_format(const char *fmt, const Args &... args); 357 int write_format(const char *fmt, const Args &... args);
309}; 358 int write(const char *ptr);
310 359 int write(const std::string &s);
311class SocketStream : public Stream {
312public:
313 SocketStream(socket_t sock);
314 virtual ~SocketStream();
315
316 virtual int read(char *ptr, size_t size);
317 virtual int write(const char *ptr, size_t size);
318 virtual int write(const char *ptr);
319 virtual int write(const std::string &s);
320 virtual std::string get_remote_addr() const;
321
322private:
323 socket_t sock_;
324};
325
326class BufferStream : public Stream {
327public:
328 BufferStream() {}
329 virtual ~BufferStream() {}
330
331 virtual int read(char *ptr, size_t size);
332 virtual int write(const char *ptr, size_t size);
333 virtual int write(const char *ptr);
334 virtual int write(const std::string &s);
335 virtual std::string get_remote_addr() const;
336
337 const std::string &get_buffer() const;
338
339private:
340 std::string buffer;
341}; 360};
342 361
343class TaskQueue { 362class TaskQueue {
344public: 363public:
345 TaskQueue() {} 364 TaskQueue() = default;
346 virtual ~TaskQueue() {} 365 virtual ~TaskQueue() = default;
347 virtual void enqueue(std::function<void()> fn) = 0; 366 virtual void enqueue(std::function<void()> fn) = 0;
348 virtual void shutdown() = 0; 367 virtual void shutdown() = 0;
349}; 368};
350 369
351#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
352class ThreadPool : public TaskQueue { 370class ThreadPool : public TaskQueue {
353public: 371public:
354 ThreadPool(size_t n) : shutdown_(false) { 372 explicit ThreadPool(size_t n) : shutdown_(false) {
355 while (n) { 373 while (n) {
356 auto t = std::make_shared<std::thread>(worker(*this)); 374 threads_.emplace_back(worker(*this));
357 threads_.push_back(t);
358 n--; 375 n--;
359 } 376 }
360 } 377 }
361 378
362 ThreadPool(const ThreadPool &) = delete; 379 ThreadPool(const ThreadPool &) = delete;
363 virtual ~ThreadPool() {} 380 ~ThreadPool() override = default;
364 381
365 virtual void enqueue(std::function<void()> fn) override { 382 void enqueue(std::function<void()> fn) override {
366 std::unique_lock<std::mutex> lock(mutex_); 383 std::unique_lock<std::mutex> lock(mutex_);
367 jobs_.push_back(fn); 384 jobs_.push_back(fn);
368 cond_.notify_one(); 385 cond_.notify_one();
369 } 386 }
370 387
371 virtual void shutdown() override { 388 void shutdown() override {
372 // Stop all worker threads... 389 // Stop all worker threads...
373 { 390 {
374 std::unique_lock<std::mutex> lock(mutex_); 391 std::unique_lock<std::mutex> lock(mutex_);
@@ -378,14 +395,14 @@ public:
378 cond_.notify_all(); 395 cond_.notify_all();
379 396
380 // Join... 397 // Join...
381 for (auto t : threads_) { 398 for (auto &t : threads_) {
382 t->join(); 399 t.join();
383 } 400 }
384 } 401 }
385 402
386private: 403private:
387 struct worker { 404 struct worker {
388 worker(ThreadPool &pool) : pool_(pool) {} 405 explicit worker(ThreadPool &pool) : pool_(pool) {}
389 406
390 void operator()() { 407 void operator()() {
391 for (;;) { 408 for (;;) {
@@ -411,7 +428,7 @@ private:
411 }; 428 };
412 friend struct worker; 429 friend struct worker;
413 430
414 std::vector<std::shared_ptr<std::thread>> threads_; 431 std::vector<std::thread> threads_;
415 std::list<std::function<void()>> jobs_; 432 std::list<std::function<void()>> jobs_;
416 433
417 bool shutdown_; 434 bool shutdown_;
@@ -419,46 +436,16 @@ private:
419 std::condition_variable cond_; 436 std::condition_variable cond_;
420 std::mutex mutex_; 437 std::mutex mutex_;
421}; 438};
422#else
423class Threads : public TaskQueue {
424public:
425 Threads() : running_threads_(0) {}
426 virtual ~Threads() {}
427
428 virtual void enqueue(std::function<void()> fn) override {
429 std::thread([=]() {
430 {
431 std::lock_guard<std::mutex> guard(running_threads_mutex_);
432 running_threads_++;
433 }
434
435 fn();
436
437 {
438 std::lock_guard<std::mutex> guard(running_threads_mutex_);
439 running_threads_--;
440 }
441 }).detach();
442 }
443
444 virtual void shutdown() override {
445 for (;;) {
446 std::this_thread::sleep_for(std::chrono::milliseconds(10));
447 std::lock_guard<std::mutex> guard(running_threads_mutex_);
448 if (!running_threads_) { break; }
449 }
450 }
451 439
452private: 440using Logger = std::function<void(const Request &, const Response &)>;
453 std::mutex running_threads_mutex_;
454 int running_threads_;
455};
456#endif
457 441
458class Server { 442class Server {
459public: 443public:
460 typedef std::function<void(const Request &, Response &)> Handler; 444 using Handler = std::function<void(const Request &, Response &)>;
461 typedef std::function<void(const Request &, const Response &)> Logger; 445 using HandlerWithContentReader = std::function<void(
446 const Request &, Response &, const ContentReader &content_reader)>;
447 using Expect100ContinueHandler =
448 std::function<int(const Request &, Response &)>;
462 449
463 Server(); 450 Server();
464 451
@@ -468,21 +455,32 @@ public:
468 455
469 Server &Get(const char *pattern, Handler handler); 456 Server &Get(const char *pattern, Handler handler);
470 Server &Post(const char *pattern, Handler handler); 457 Server &Post(const char *pattern, Handler handler);
471 458 Server &Post(const char *pattern, HandlerWithContentReader handler);
472 Server &Put(const char *pattern, Handler handler); 459 Server &Put(const char *pattern, Handler handler);
460 Server &Put(const char *pattern, HandlerWithContentReader handler);
473 Server &Patch(const char *pattern, Handler handler); 461 Server &Patch(const char *pattern, Handler handler);
462 Server &Patch(const char *pattern, HandlerWithContentReader handler);
474 Server &Delete(const char *pattern, Handler handler); 463 Server &Delete(const char *pattern, Handler handler);
475 Server &Options(const char *pattern, Handler handler); 464 Server &Options(const char *pattern, Handler handler);
476 465
477 bool set_base_dir(const char *path); 466 [[deprecated]] bool set_base_dir(const char *dir,
467 const char *mount_point = nullptr);
468 bool set_mount_point(const char *mount_point, const char *dir);
469 bool remove_mount_point(const char *mount_point);
470 void set_file_extension_and_mimetype_mapping(const char *ext,
471 const char *mime);
478 void set_file_request_handler(Handler handler); 472 void set_file_request_handler(Handler handler);
479 473
480 void set_error_handler(Handler handler); 474 void set_error_handler(Handler handler);
481 void set_logger(Logger logger); 475 void set_logger(Logger logger);
482 476
477 void set_expect_100_continue_handler(Expect100ContinueHandler handler);
478
483 void set_keep_alive_max_count(size_t count); 479 void set_keep_alive_max_count(size_t count);
480 void set_read_timeout(time_t sec, time_t usec);
484 void set_payload_max_length(size_t length); 481 void set_payload_max_length(size_t length);
485 482
483 bool bind_to_port(const char *host, int port, int socket_flags = 0);
486 int bind_to_any_port(const char *host, int socket_flags = 0); 484 int bind_to_any_port(const char *host, int socket_flags = 0);
487 bool listen_after_bind(); 485 bool listen_after_bind();
488 486
@@ -496,22 +494,29 @@ public:
496protected: 494protected:
497 bool process_request(Stream &strm, bool last_connection, 495 bool process_request(Stream &strm, bool last_connection,
498 bool &connection_close, 496 bool &connection_close,
499 std::function<void(Request &)> setup_request); 497 const std::function<void(Request &)> &setup_request);
500 498
501 size_t keep_alive_max_count_; 499 size_t keep_alive_max_count_;
500 time_t read_timeout_sec_;
501 time_t read_timeout_usec_;
502 size_t payload_max_length_; 502 size_t payload_max_length_;
503 503
504private: 504private:
505 typedef std::vector<std::pair<std::regex, Handler>> Handlers; 505 using Handlers = std::vector<std::pair<std::regex, Handler>>;
506 using HandlersForContentReader =
507 std::vector<std::pair<std::regex, HandlerWithContentReader>>;
506 508
507 socket_t create_server_socket(const char *host, int port, 509 socket_t create_server_socket(const char *host, int port,
508 int socket_flags) const; 510 int socket_flags) const;
509 int bind_internal(const char *host, int port, int socket_flags); 511 int bind_internal(const char *host, int port, int socket_flags);
510 bool listen_internal(); 512 bool listen_internal();
511 513
512 bool routing(Request &req, Response &res); 514 bool routing(Request &req, Response &res, Stream &strm, bool last_connection);
513 bool handle_file_request(Request &req, Response &res); 515 bool handle_file_request(Request &req, Response &res, bool head = false);
514 bool dispatch_request(Request &req, Response &res, Handlers &handlers); 516 bool dispatch_request(Request &req, Response &res, Handlers &handlers);
517 bool dispatch_request_for_content_reader(Request &req, Response &res,
518 ContentReader content_reader,
519 HandlersForContentReader &handlers);
515 520
516 bool parse_request_line(const char *s, Request &req); 521 bool parse_request_line(const char *s, Request &req);
517 bool write_response(Stream &strm, bool last_connection, const Request &req, 522 bool write_response(Stream &strm, bool last_connection, const Request &req,
@@ -519,26 +524,43 @@ private:
519 bool write_content_with_provider(Stream &strm, const Request &req, 524 bool write_content_with_provider(Stream &strm, const Request &req,
520 Response &res, const std::string &boundary, 525 Response &res, const std::string &boundary,
521 const std::string &content_type); 526 const std::string &content_type);
527 bool read_content(Stream &strm, bool last_connection, Request &req,
528 Response &res);
529 bool read_content_with_content_receiver(
530 Stream &strm, bool last_connection, Request &req, Response &res,
531 ContentReceiver receiver, MultipartContentHeader multipart_header,
532 ContentReceiver multipart_receiver);
533 bool read_content_core(Stream &strm, bool last_connection, Request &req,
534 Response &res, ContentReceiver receiver,
535 MultipartContentHeader mulitpart_header,
536 ContentReceiver multipart_receiver);
522 537
523 virtual bool process_and_close_socket(socket_t sock); 538 virtual bool process_and_close_socket(socket_t sock);
524 539
525 std::atomic<bool> is_running_; 540 std::atomic<bool> is_running_;
526 std::atomic<socket_t> svr_sock_; 541 std::atomic<socket_t> svr_sock_;
527 std::string base_dir_; 542 std::vector<std::pair<std::string, std::string>> base_dirs_;
543 std::map<std::string, std::string> file_extension_and_mimetype_map_;
528 Handler file_request_handler_; 544 Handler file_request_handler_;
529 Handlers get_handlers_; 545 Handlers get_handlers_;
530 Handlers post_handlers_; 546 Handlers post_handlers_;
547 HandlersForContentReader post_handlers_for_content_reader_;
531 Handlers put_handlers_; 548 Handlers put_handlers_;
549 HandlersForContentReader put_handlers_for_content_reader_;
532 Handlers patch_handlers_; 550 Handlers patch_handlers_;
551 HandlersForContentReader patch_handlers_for_content_reader_;
533 Handlers delete_handlers_; 552 Handlers delete_handlers_;
534 Handlers options_handlers_; 553 Handlers options_handlers_;
535 Handler error_handler_; 554 Handler error_handler_;
536 Logger logger_; 555 Logger logger_;
556 Expect100ContinueHandler expect_100_continue_handler_;
537}; 557};
538 558
539class Client { 559class Client {
540public: 560public:
541 Client(const char *host, int port = 80, time_t timeout_sec = 300); 561 explicit Client(const std::string &host, int port = 80,
562 const std::string &client_cert_path = std::string(),
563 const std::string &client_key_path = std::string());
542 564
543 virtual ~Client(); 565 virtual ~Client();
544 566
@@ -586,6 +608,15 @@ public:
586 const std::string &body, 608 const std::string &body,
587 const char *content_type); 609 const char *content_type);
588 610
611 std::shared_ptr<Response> Post(const char *path, size_t content_length,
612 ContentProvider content_provider,
613 const char *content_type);
614
615 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
616 size_t content_length,
617 ContentProvider content_provider,
618 const char *content_type);
619
589 std::shared_ptr<Response> Post(const char *path, const Params &params); 620 std::shared_ptr<Response> Post(const char *path, const Params &params);
590 621
591 std::shared_ptr<Response> Post(const char *path, const Headers &headers, 622 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
@@ -604,6 +635,20 @@ public:
604 const std::string &body, 635 const std::string &body,
605 const char *content_type); 636 const char *content_type);
606 637
638 std::shared_ptr<Response> Put(const char *path, size_t content_length,
639 ContentProvider content_provider,
640 const char *content_type);
641
642 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
643 size_t content_length,
644 ContentProvider content_provider,
645 const char *content_type);
646
647 std::shared_ptr<Response> Put(const char *path, const Params &params);
648
649 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
650 const Params &params);
651
607 std::shared_ptr<Response> Patch(const char *path, const std::string &body, 652 std::shared_ptr<Response> Patch(const char *path, const std::string &body,
608 const char *content_type); 653 const char *content_type);
609 654
@@ -611,6 +656,15 @@ public:
611 const std::string &body, 656 const std::string &body,
612 const char *content_type); 657 const char *content_type);
613 658
659 std::shared_ptr<Response> Patch(const char *path, size_t content_length,
660 ContentProvider content_provider,
661 const char *content_type);
662
663 std::shared_ptr<Response> Patch(const char *path, const Headers &headers,
664 size_t content_length,
665 ContentProvider content_provider,
666 const char *content_type);
667
614 std::shared_ptr<Response> Delete(const char *path); 668 std::shared_ptr<Response> Delete(const char *path);
615 669
616 std::shared_ptr<Response> Delete(const char *path, const std::string &body, 670 std::shared_ptr<Response> Delete(const char *path, const std::string &body,
@@ -631,9 +685,33 @@ public:
631 bool send(const std::vector<Request> &requests, 685 bool send(const std::vector<Request> &requests,
632 std::vector<Response> &responses); 686 std::vector<Response> &responses);
633 687
688 void set_timeout_sec(time_t timeout_sec);
689
690 void set_read_timeout(time_t sec, time_t usec);
691
634 void set_keep_alive_max_count(size_t count); 692 void set_keep_alive_max_count(size_t count);
635 693
636 void follow_location(bool on); 694 void set_basic_auth(const char *username, const char *password);
695
696#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
697 void set_digest_auth(const char *username, const char *password);
698#endif
699
700 void set_follow_location(bool on);
701
702 void set_compress(bool on);
703
704 void set_interface(const char *intf);
705
706 void set_proxy(const char *host, int port);
707
708 void set_proxy_basic_auth(const char *username, const char *password);
709
710#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
711 void set_proxy_digest_auth(const char *username, const char *password);
712#endif
713
714 void set_logger(Logger logger);
637 715
638protected: 716protected:
639 bool process_request(Stream &strm, const Request &req, Response &res, 717 bool process_request(Stream &strm, const Request &req, Response &res,
@@ -641,16 +719,85 @@ protected:
641 719
642 const std::string host_; 720 const std::string host_;
643 const int port_; 721 const int port_;
644 time_t timeout_sec_;
645 const std::string host_and_port_; 722 const std::string host_and_port_;
646 size_t keep_alive_max_count_; 723
647 size_t follow_location_; 724 // Settings
725 std::string client_cert_path_;
726 std::string client_key_path_;
727
728 time_t timeout_sec_ = 300;
729 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
730 time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
731
732 size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
733
734 std::string basic_auth_username_;
735 std::string basic_auth_password_;
736#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
737 std::string digest_auth_username_;
738 std::string digest_auth_password_;
739#endif
740
741 bool follow_location_ = false;
742
743 bool compress_ = false;
744
745 std::string interface_;
746
747 std::string proxy_host_;
748 int proxy_port_;
749
750 std::string proxy_basic_auth_username_;
751 std::string proxy_basic_auth_password_;
752#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
753 std::string proxy_digest_auth_username_;
754 std::string proxy_digest_auth_password_;
755#endif
756
757 Logger logger_;
758
759 void copy_settings(const Client &rhs) {
760 client_cert_path_ = rhs.client_cert_path_;
761 client_key_path_ = rhs.client_key_path_;
762 timeout_sec_ = rhs.timeout_sec_;
763 read_timeout_sec_ = rhs.read_timeout_sec_;
764 read_timeout_usec_ = rhs.read_timeout_usec_;
765 keep_alive_max_count_ = rhs.keep_alive_max_count_;
766 basic_auth_username_ = rhs.basic_auth_username_;
767 basic_auth_password_ = rhs.basic_auth_password_;
768#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
769 digest_auth_username_ = rhs.digest_auth_username_;
770 digest_auth_password_ = rhs.digest_auth_password_;
771#endif
772 follow_location_ = rhs.follow_location_;
773 compress_ = rhs.compress_;
774 interface_ = rhs.interface_;
775 proxy_host_ = rhs.proxy_host_;
776 proxy_port_ = rhs.proxy_port_;
777 proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
778 proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
779#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
780 proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
781 proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
782#endif
783 logger_ = rhs.logger_;
784 }
648 785
649private: 786private:
650 socket_t create_client_socket() const; 787 socket_t create_client_socket() const;
651 bool read_response_line(Stream &strm, Response &res); 788 bool read_response_line(Stream &strm, Response &res);
652 void write_request(Stream &strm, const Request &req, bool last_connection); 789 bool write_request(Stream &strm, const Request &req, bool last_connection);
653 bool redirect(const Request &req, Response &res); 790 bool redirect(const Request &req, Response &res);
791 bool handle_request(Stream &strm, const Request &req, Response &res,
792 bool last_connection, bool &connection_close);
793#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
794 bool connect(socket_t sock, Response &res, bool &error);
795#endif
796
797 std::shared_ptr<Response> send_with_content_provider(
798 const char *method, const char *path, const Headers &headers,
799 const std::string &body, size_t content_length,
800 ContentProvider content_provider, const char *content_type);
654 801
655 virtual bool process_and_close_socket( 802 virtual bool process_and_close_socket(
656 socket_t sock, size_t request_count, 803 socket_t sock, size_t request_count,
@@ -692,22 +839,6 @@ inline void Post(std::vector<Request> &requests, const char *path,
692} 839}
693 840
694#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 841#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
695class SSLSocketStream : public Stream {
696public:
697 SSLSocketStream(socket_t sock, SSL *ssl);
698 virtual ~SSLSocketStream();
699
700 virtual int read(char *ptr, size_t size);
701 virtual int write(const char *ptr, size_t size);
702 virtual int write(const char *ptr);
703 virtual int write(const std::string &s);
704 virtual std::string get_remote_addr() const;
705
706private:
707 socket_t sock_;
708 SSL *ssl_;
709};
710
711class SSLServer : public Server { 842class SSLServer : public Server {
712public: 843public:
713 SSLServer(const char *cert_path, const char *private_key_path, 844 SSLServer(const char *cert_path, const char *private_key_path,
@@ -727,9 +858,9 @@ private:
727 858
728class SSLClient : public Client { 859class SSLClient : public Client {
729public: 860public:
730 SSLClient(const char *host, int port = 443, time_t timeout_sec = 300, 861 SSLClient(const std::string &host, int port = 443,
731 const char *client_cert_path = nullptr, 862 const std::string &client_cert_path = std::string(),
732 const char *client_key_path = nullptr); 863 const std::string &client_key_path = std::string());
733 864
734 virtual ~SSLClient(); 865 virtual ~SSLClient();
735 866
@@ -737,11 +868,12 @@ public:
737 868
738 void set_ca_cert_path(const char *ca_ceert_file_path, 869 void set_ca_cert_path(const char *ca_ceert_file_path,
739 const char *ca_cert_dir_path = nullptr); 870 const char *ca_cert_dir_path = nullptr);
871
740 void enable_server_certificate_verification(bool enabled); 872 void enable_server_certificate_verification(bool enabled);
741 873
742 long get_openssl_verify_result() const; 874 long get_openssl_verify_result() const;
743 875
744 SSL_CTX* ssl_context() const noexcept; 876 SSL_CTX *ssl_context() const noexcept;
745 877
746private: 878private:
747 virtual bool process_and_close_socket( 879 virtual bool process_and_close_socket(
@@ -759,6 +891,7 @@ private:
759 SSL_CTX *ctx_; 891 SSL_CTX *ctx_;
760 std::mutex ctx_mutex_; 892 std::mutex ctx_mutex_;
761 std::vector<std::string> host_components_; 893 std::vector<std::string> host_components_;
894
762 std::string ca_cert_file_path_; 895 std::string ca_cert_file_path_;
763 std::string ca_cert_dir_path_; 896 std::string ca_cert_dir_path_;
764 bool server_certificate_verification_ = false; 897 bool server_certificate_verification_ = false;
@@ -766,9 +899,12 @@ private:
766}; 899};
767#endif 900#endif
768 901
902// ----------------------------------------------------------------------------
903
769/* 904/*
770 * Implementation 905 * Implementation
771 */ 906 */
907
772namespace detail { 908namespace detail {
773 909
774inline bool is_hex(char c, int &v) { 910inline bool is_hex(char c, int &v) {
@@ -932,8 +1068,8 @@ inline void read_file(const std::string &path, std::string &out) {
932 1068
933inline std::string file_extension(const std::string &path) { 1069inline std::string file_extension(const std::string &path) {
934 std::smatch m; 1070 std::smatch m;
935 auto pat = std::regex("\\.([a-zA-Z0-9]+)$"); 1071 static auto re = std::regex("\\.([a-zA-Z0-9]+)$");
936 if (std::regex_search(path, m, pat)) { return m[1].str(); } 1072 if (std::regex_search(path, m, re)) { return m[1].str(); }
937 return std::string(); 1073 return std::string();
938} 1074}
939 1075
@@ -976,6 +1112,11 @@ public:
976 } 1112 }
977 } 1113 }
978 1114
1115 bool end_with_crlf() const {
1116 auto end = ptr() + size();
1117 return size() >= 2 && end[-2] == '\r' && end[-1] == '\n';
1118 }
1119
979 bool getline() { 1120 bool getline() {
980 fixed_buffer_used_size_ = 0; 1121 fixed_buffer_used_size_ = 0;
981 glowable_buffer_.clear(); 1122 glowable_buffer_.clear();
@@ -1019,7 +1160,7 @@ private:
1019 Stream &strm_; 1160 Stream &strm_;
1020 char *fixed_buffer_; 1161 char *fixed_buffer_;
1021 const size_t fixed_buffer_size_; 1162 const size_t fixed_buffer_size_;
1022 size_t fixed_buffer_used_size_; 1163 size_t fixed_buffer_used_size_ = 0;
1023 std::string glowable_buffer_; 1164 std::string glowable_buffer_;
1024}; 1165};
1025 1166
@@ -1053,6 +1194,28 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
1053#endif 1194#endif
1054} 1195}
1055 1196
1197inline int select_write(socket_t sock, time_t sec, time_t usec) {
1198#ifdef CPPHTTPLIB_USE_POLL
1199 struct pollfd pfd_read;
1200 pfd_read.fd = sock;
1201 pfd_read.events = POLLOUT;
1202
1203 auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
1204
1205 return poll(&pfd_read, 1, timeout);
1206#else
1207 fd_set fds;
1208 FD_ZERO(&fds);
1209 FD_SET(sock, &fds);
1210
1211 timeval tv;
1212 tv.tv_sec = static_cast<long>(sec);
1213 tv.tv_usec = static_cast<long>(usec);
1214
1215 return select(static_cast<int>(sock + 1), nullptr, &fds, nullptr, &tv);
1216#endif
1217}
1218
1056inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) { 1219inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1057#ifdef CPPHTTPLIB_USE_POLL 1220#ifdef CPPHTTPLIB_USE_POLL
1058 struct pollfd pfd_read; 1221 struct pollfd pfd_read;
@@ -1065,7 +1228,8 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1065 pfd_read.revents & (POLLIN | POLLOUT)) { 1228 pfd_read.revents & (POLLIN | POLLOUT)) {
1066 int error = 0; 1229 int error = 0;
1067 socklen_t len = sizeof(error); 1230 socklen_t len = sizeof(error);
1068 return getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &len) >= 0 && 1231 return getsockopt(sock, SOL_SOCKET, SO_ERROR,
1232 reinterpret_cast<char *>(&error), &len) >= 0 &&
1069 !error; 1233 !error;
1070 } 1234 }
1071 return false; 1235 return false;
@@ -1085,27 +1249,86 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1085 (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) { 1249 (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
1086 int error = 0; 1250 int error = 0;
1087 socklen_t len = sizeof(error); 1251 socklen_t len = sizeof(error);
1088 return getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&error, &len) >= 0 && 1252 return getsockopt(sock, SOL_SOCKET, SO_ERROR,
1253 reinterpret_cast<char *>(&error), &len) >= 0 &&
1089 !error; 1254 !error;
1090 } 1255 }
1091 return false; 1256 return false;
1092#endif 1257#endif
1093} 1258}
1094 1259
1260class SocketStream : public Stream {
1261public:
1262 SocketStream(socket_t sock, time_t read_timeout_sec,
1263 time_t read_timeout_usec);
1264 ~SocketStream() override;
1265
1266 bool is_readable() const override;
1267 bool is_writable() const override;
1268 int read(char *ptr, size_t size) override;
1269 int write(const char *ptr, size_t size) override;
1270 std::string get_remote_addr() const override;
1271
1272private:
1273 socket_t sock_;
1274 time_t read_timeout_sec_;
1275 time_t read_timeout_usec_;
1276};
1277
1278#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1279class SSLSocketStream : public Stream {
1280public:
1281 SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec,
1282 time_t read_timeout_usec);
1283 virtual ~SSLSocketStream();
1284
1285 bool is_readable() const override;
1286 bool is_writable() const override;
1287 int read(char *ptr, size_t size) override;
1288 int write(const char *ptr, size_t size) override;
1289 std::string get_remote_addr() const override;
1290
1291private:
1292 socket_t sock_;
1293 SSL *ssl_;
1294 time_t read_timeout_sec_;
1295 time_t read_timeout_usec_;
1296};
1297#endif
1298
1299class BufferStream : public Stream {
1300public:
1301 BufferStream() = default;
1302 ~BufferStream() override = default;
1303
1304 bool is_readable() const override;
1305 bool is_writable() const override;
1306 int read(char *ptr, size_t size) override;
1307 int write(const char *ptr, size_t size) override;
1308 std::string get_remote_addr() const override;
1309
1310 const std::string &get_buffer() const;
1311
1312private:
1313 std::string buffer;
1314 int position = 0;
1315};
1316
1095template <typename T> 1317template <typename T>
1096inline bool process_and_close_socket(bool is_client_request, socket_t sock, 1318inline bool process_socket(bool is_client_request, socket_t sock,
1097 size_t keep_alive_max_count, T callback) { 1319 size_t keep_alive_max_count, time_t read_timeout_sec,
1320 time_t read_timeout_usec, T callback) {
1098 assert(keep_alive_max_count > 0); 1321 assert(keep_alive_max_count > 0);
1099 1322
1100 bool ret = false; 1323 auto ret = false;
1101 1324
1102 if (keep_alive_max_count > 1) { 1325 if (keep_alive_max_count > 1) {
1103 auto count = keep_alive_max_count; 1326 auto count = keep_alive_max_count;
1104 while (count > 0 && 1327 while (count > 0 &&
1105 (is_client_request || 1328 (is_client_request ||
1106 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 1329 select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND,
1107 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 1330 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) {
1108 SocketStream strm(sock); 1331 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1109 auto last_connection = count == 1; 1332 auto last_connection = count == 1;
1110 auto connection_close = false; 1333 auto connection_close = false;
1111 1334
@@ -1114,12 +1337,22 @@ inline bool process_and_close_socket(bool is_client_request, socket_t sock,
1114 1337
1115 count--; 1338 count--;
1116 } 1339 }
1117 } else { 1340 } else { // keep_alive_max_count is 0 or 1
1118 SocketStream strm(sock); 1341 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1119 auto dummy_connection_close = false; 1342 auto dummy_connection_close = false;
1120 ret = callback(strm, true, dummy_connection_close); 1343 ret = callback(strm, true, dummy_connection_close);
1121 } 1344 }
1122 1345
1346 return ret;
1347}
1348
1349template <typename T>
1350inline bool process_and_close_socket(bool is_client_request, socket_t sock,
1351 size_t keep_alive_max_count,
1352 time_t read_timeout_sec,
1353 time_t read_timeout_usec, T callback) {
1354 auto ret = process_socket(is_client_request, sock, keep_alive_max_count,
1355 read_timeout_sec, read_timeout_usec, callback);
1123 close_socket(sock); 1356 close_socket(sock);
1124 return ret; 1357 return ret;
1125} 1358}
@@ -1165,6 +1398,23 @@ socket_t create_socket(const char *host, int port, Fn fn,
1165#ifdef _WIN32 1398#ifdef _WIN32
1166 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol, 1399 auto sock = WSASocketW(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
1167 nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT); 1400 nullptr, 0, WSA_FLAG_NO_HANDLE_INHERIT);
1401 /**
1402 * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1
1403 * and above the socket creation fails on older Windows Systems.
1404 *
1405 * Let's try to create a socket the old way in this case.
1406 *
1407 * Reference:
1408 * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
1409 *
1410 * WSA_FLAG_NO_HANDLE_INHERIT:
1411 * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with
1412 * SP1, and later
1413 *
1414 */
1415 if (sock == INVALID_SOCKET) {
1416 sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1417 }
1168#else 1418#else
1169 auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); 1419 auto sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
1170#endif 1420#endif
@@ -1176,9 +1426,11 @@ socket_t create_socket(const char *host, int port, Fn fn,
1176 1426
1177 // Make 'reuse address' option available 1427 // Make 'reuse address' option available
1178 int yes = 1; 1428 int yes = 1;
1179 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&yes), sizeof(yes)); 1429 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&yes),
1430 sizeof(yes));
1180#ifdef SO_REUSEPORT 1431#ifdef SO_REUSEPORT
1181 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&yes), sizeof(yes)); 1432 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char *>(&yes),
1433 sizeof(yes));
1182#endif 1434#endif
1183 1435
1184 // bind or connect 1436 // bind or connect
@@ -1213,27 +1465,105 @@ inline bool is_connection_error() {
1213#endif 1465#endif
1214} 1466}
1215 1467
1468inline bool bind_ip_address(socket_t sock, const char *host) {
1469 struct addrinfo hints;
1470 struct addrinfo *result;
1471
1472 memset(&hints, 0, sizeof(struct addrinfo));
1473 hints.ai_family = AF_UNSPEC;
1474 hints.ai_socktype = SOCK_STREAM;
1475 hints.ai_protocol = 0;
1476
1477 if (getaddrinfo(host, "0", &hints, &result)) { return false; }
1478
1479 auto ret = false;
1480 for (auto rp = result; rp; rp = rp->ai_next) {
1481 const auto &ai = *rp;
1482 if (!::bind(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen))) {
1483 ret = true;
1484 break;
1485 }
1486 }
1487
1488 freeaddrinfo(result);
1489 return ret;
1490}
1491
1492inline std::string if2ip(const std::string &ifn) {
1493#ifndef _WIN32
1494 struct ifaddrs *ifap;
1495 getifaddrs(&ifap);
1496 for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
1497 if (ifa->ifa_addr && ifn == ifa->ifa_name) {
1498 if (ifa->ifa_addr->sa_family == AF_INET) {
1499 auto sa = reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr);
1500 char buf[INET_ADDRSTRLEN];
1501 if (inet_ntop(AF_INET, &sa->sin_addr, buf, INET_ADDRSTRLEN)) {
1502 freeifaddrs(ifap);
1503 return std::string(buf, INET_ADDRSTRLEN);
1504 }
1505 }
1506 }
1507 }
1508 freeifaddrs(ifap);
1509#endif
1510 return std::string();
1511}
1512
1513inline socket_t create_client_socket(const char *host, int port,
1514 time_t timeout_sec,
1515 const std::string &intf) {
1516 return create_socket(
1517 host, port, [&](socket_t sock, struct addrinfo &ai) -> bool {
1518 if (!intf.empty()) {
1519 auto ip = if2ip(intf);
1520 if (ip.empty()) { ip = intf; }
1521 if (!bind_ip_address(sock, ip.c_str())) { return false; }
1522 }
1523
1524 set_nonblocking(sock, true);
1525
1526 auto ret = ::connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen));
1527 if (ret < 0) {
1528 if (is_connection_error() ||
1529 !wait_until_socket_is_ready(sock, timeout_sec, 0)) {
1530 close_socket(sock);
1531 return false;
1532 }
1533 }
1534
1535 set_nonblocking(sock, false);
1536 return true;
1537 });
1538}
1539
1216inline std::string get_remote_addr(socket_t sock) { 1540inline std::string get_remote_addr(socket_t sock) {
1217 struct sockaddr_storage addr; 1541 struct sockaddr_storage addr;
1218 socklen_t len = sizeof(addr); 1542 socklen_t len = sizeof(addr);
1219 1543
1220 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), &len)) { 1544 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), &len)) {
1221 char ipstr[NI_MAXHOST]; 1545 std::array<char, NI_MAXHOST> ipstr{};
1222 1546
1223 if (!getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), len, ipstr, sizeof(ipstr), 1547 if (!getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), len,
1224 nullptr, 0, NI_NUMERICHOST)) { 1548 ipstr.data(), static_cast<unsigned int>(ipstr.size()), nullptr, 0, NI_NUMERICHOST)) {
1225 return ipstr; 1549 return ipstr.data();
1226 } 1550 }
1227 } 1551 }
1228 1552
1229 return std::string(); 1553 return std::string();
1230} 1554}
1231 1555
1232inline const char *find_content_type(const std::string &path) { 1556inline const char *
1557find_content_type(const std::string &path,
1558 const std::map<std::string, std::string> &user_data) {
1233 auto ext = file_extension(path); 1559 auto ext = file_extension(path);
1560
1561 auto it = user_data.find(ext);
1562 if (it != user_data.end()) { return it->second.c_str(); }
1563
1234 if (ext == "txt") { 1564 if (ext == "txt") {
1235 return "text/plain"; 1565 return "text/plain";
1236 } else if (ext == "html") { 1566 } else if (ext == "html" || ext == "htm") {
1237 return "text/html"; 1567 return "text/html";
1238 } else if (ext == "css") { 1568 } else if (ext == "css") {
1239 return "text/css"; 1569 return "text/css";
@@ -1253,6 +1583,8 @@ inline const char *find_content_type(const std::string &path) {
1253 return "application/pdf"; 1583 return "application/pdf";
1254 } else if (ext == "js") { 1584 } else if (ext == "js") {
1255 return "application/javascript"; 1585 return "application/javascript";
1586 } else if (ext == "wasm") {
1587 return "application/wasm";
1256 } else if (ext == "xml") { 1588 } else if (ext == "xml") {
1257 return "application/xml"; 1589 return "application/xml";
1258 } else if (ext == "xhtml") { 1590 } else if (ext == "xhtml") {
@@ -1263,19 +1595,25 @@ inline const char *find_content_type(const std::string &path) {
1263 1595
1264inline const char *status_message(int status) { 1596inline const char *status_message(int status) {
1265 switch (status) { 1597 switch (status) {
1598 case 100: return "Continue";
1266 case 200: return "OK"; 1599 case 200: return "OK";
1600 case 202: return "Accepted";
1601 case 204: return "No Content";
1267 case 206: return "Partial Content"; 1602 case 206: return "Partial Content";
1268 case 301: return "Moved Permanently"; 1603 case 301: return "Moved Permanently";
1269 case 302: return "Found"; 1604 case 302: return "Found";
1270 case 303: return "See Other"; 1605 case 303: return "See Other";
1271 case 304: return "Not Modified"; 1606 case 304: return "Not Modified";
1272 case 400: return "Bad Request"; 1607 case 400: return "Bad Request";
1608 case 401: return "Unauthorized";
1273 case 403: return "Forbidden"; 1609 case 403: return "Forbidden";
1274 case 404: return "Not Found"; 1610 case 404: return "Not Found";
1275 case 413: return "Payload Too Large"; 1611 case 413: return "Payload Too Large";
1276 case 414: return "Request-URI Too Long"; 1612 case 414: return "Request-URI Too Long";
1277 case 415: return "Unsupported Media Type"; 1613 case 415: return "Unsupported Media Type";
1278 case 416: return "Range Not Satisfiable"; 1614 case 416: return "Range Not Satisfiable";
1615 case 417: return "Expectation Failed";
1616 case 503: return "Service Unavailable";
1279 1617
1280 default: 1618 default:
1281 case 500: return "Internal Server Error"; 1619 case 500: return "Internal Server Error";
@@ -1302,18 +1640,18 @@ inline bool compress(std::string &content) {
1302 if (ret != Z_OK) { return false; } 1640 if (ret != Z_OK) { return false; }
1303 1641
1304 strm.avail_in = content.size(); 1642 strm.avail_in = content.size();
1305 strm.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(content.data())); 1643 strm.next_in =
1644 const_cast<Bytef *>(reinterpret_cast<const Bytef *>(content.data()));
1306 1645
1307 std::string compressed; 1646 std::string compressed;
1308 1647
1309 const auto bufsiz = 16384; 1648 std::array<char, 16384> buff{};
1310 char buff[bufsiz];
1311 do { 1649 do {
1312 strm.avail_out = bufsiz; 1650 strm.avail_out = buff.size();
1313 strm.next_out = reinterpret_cast<Bytef*>(buff); 1651 strm.next_out = reinterpret_cast<Bytef *>(buff.data());
1314 ret = deflate(&strm, Z_FINISH); 1652 ret = deflate(&strm, Z_FINISH);
1315 assert(ret != Z_STREAM_ERROR); 1653 assert(ret != Z_STREAM_ERROR);
1316 compressed.append(buff, bufsiz - strm.avail_out); 1654 compressed.append(buff.data(), buff.size() - strm.avail_out);
1317 } while (strm.avail_out == 0); 1655 } while (strm.avail_out == 0);
1318 1656
1319 assert(ret == Z_STREAM_END); 1657 assert(ret == Z_STREAM_END);
@@ -1347,13 +1685,12 @@ public:
1347 int ret = Z_OK; 1685 int ret = Z_OK;
1348 1686
1349 strm.avail_in = data_length; 1687 strm.avail_in = data_length;
1350 strm.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef *>(data)); 1688 strm.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
1351 1689
1352 const auto bufsiz = 16384; 1690 std::array<char, 16384> buff{};
1353 char buff[bufsiz];
1354 do { 1691 do {
1355 strm.avail_out = bufsiz; 1692 strm.avail_out = buff.size();
1356 strm.next_out = reinterpret_cast<Bytef*>(buff); 1693 strm.next_out = reinterpret_cast<Bytef *>(buff.data());
1357 1694
1358 ret = inflate(&strm, Z_NO_FLUSH); 1695 ret = inflate(&strm, Z_NO_FLUSH);
1359 assert(ret != Z_STREAM_ERROR); 1696 assert(ret != Z_STREAM_ERROR);
@@ -1363,10 +1700,12 @@ public:
1363 case Z_MEM_ERROR: inflateEnd(&strm); return false; 1700 case Z_MEM_ERROR: inflateEnd(&strm); return false;
1364 } 1701 }
1365 1702
1366 if (!callback(buff, bufsiz - strm.avail_out)) { return false; } 1703 if (!callback(buff.data(), buff.size() - strm.avail_out)) {
1704 return false;
1705 }
1367 } while (strm.avail_out == 0); 1706 } while (strm.avail_out == 0);
1368 1707
1369 return ret == Z_STREAM_END; 1708 return ret == Z_OK || ret == Z_STREAM_END;
1370 } 1709 }
1371 1710
1372private: 1711private:
@@ -1397,18 +1736,35 @@ inline uint64_t get_header_value_uint64(const Headers &headers, const char *key,
1397} 1736}
1398 1737
1399inline bool read_headers(Stream &strm, Headers &headers) { 1738inline bool read_headers(Stream &strm, Headers &headers) {
1400 static std::regex re(R"((.+?):\s*(.+?)\s*\r\n)");
1401
1402 const auto bufsiz = 2048; 1739 const auto bufsiz = 2048;
1403 char buf[bufsiz]; 1740 char buf[bufsiz];
1404 1741 stream_line_reader line_reader(strm, buf, bufsiz);
1405 stream_line_reader reader(strm, buf, bufsiz);
1406 1742
1407 for (;;) { 1743 for (;;) {
1408 if (!reader.getline()) { return false; } 1744 if (!line_reader.getline()) { return false; }
1409 if (!strcmp(reader.ptr(), "\r\n")) { break; } 1745
1746 // Check if the line ends with CRLF.
1747 if (line_reader.end_with_crlf()) {
1748 // Blank line indicates end of headers.
1749 if (line_reader.size() == 2) { break; }
1750 } else {
1751 continue; // Skip invalid line.
1752 }
1753
1754 // Skip trailing spaces and tabs.
1755 auto end = line_reader.ptr() + line_reader.size() - 2;
1756 while (line_reader.ptr() < end && (end[-1] == ' ' || end[-1] == '\t')) {
1757 end--;
1758 }
1759
1760 // Horizontal tab and ' ' are considered whitespace and are ignored when on
1761 // the left or right side of the header value:
1762 // - https://stackoverflow.com/questions/50179659/
1763 // - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
1764 static const std::regex re(R"((.+?):[\t ]*(.+))");
1765
1410 std::cmatch m; 1766 std::cmatch m;
1411 if (std::regex_match(reader.ptr(), m, re)) { 1767 if (std::regex_match(line_reader.ptr(), end, m, re)) {
1412 auto key = std::string(m[1]); 1768 auto key = std::string(m[1]);
1413 auto val = std::string(m[2]); 1769 auto val = std::string(m[2]);
1414 headers.emplace(key, val); 1770 headers.emplace(key, val);
@@ -1418,12 +1774,8 @@ inline bool read_headers(Stream &strm, Headers &headers) {
1418 return true; 1774 return true;
1419} 1775}
1420 1776
1421typedef std::function<bool(const char *data, size_t data_length)>
1422 ContentReceiverCore;
1423
1424inline bool read_content_with_length(Stream &strm, uint64_t len, 1777inline bool read_content_with_length(Stream &strm, uint64_t len,
1425 Progress progress, 1778 Progress progress, ContentReceiver out) {
1426 ContentReceiverCore out) {
1427 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 1779 char buf[CPPHTTPLIB_RECV_BUFSIZ];
1428 1780
1429 uint64_t r = 0; 1781 uint64_t r = 0;
@@ -1455,7 +1807,7 @@ inline void skip_content_with_length(Stream &strm, uint64_t len) {
1455 } 1807 }
1456} 1808}
1457 1809
1458inline bool read_content_without_length(Stream &strm, ContentReceiverCore out) { 1810inline bool read_content_without_length(Stream &strm, ContentReceiver out) {
1459 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 1811 char buf[CPPHTTPLIB_RECV_BUFSIZ];
1460 for (;;) { 1812 for (;;) {
1461 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); 1813 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ);
@@ -1470,33 +1822,34 @@ inline bool read_content_without_length(Stream &strm, ContentReceiverCore out) {
1470 return true; 1822 return true;
1471} 1823}
1472 1824
1473inline bool read_content_chunked(Stream &strm, ContentReceiverCore out) { 1825inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
1474 const auto bufsiz = 16; 1826 const auto bufsiz = 16;
1475 char buf[bufsiz]; 1827 char buf[bufsiz];
1476 1828
1477 stream_line_reader reader(strm, buf, bufsiz); 1829 stream_line_reader line_reader(strm, buf, bufsiz);
1478 1830
1479 if (!reader.getline()) { return false; } 1831 if (!line_reader.getline()) { return false; }
1480 1832
1481 auto chunk_len = std::stoi(reader.ptr(), 0, 16); 1833 auto chunk_len = std::stoi(line_reader.ptr(), 0, 16);
1482 1834
1483 while (chunk_len > 0) { 1835 while (chunk_len > 0) {
1484 if (!read_content_with_length(strm, chunk_len, nullptr, out)) { 1836 if (!read_content_with_length(strm, chunk_len, nullptr, out)) {
1485 return false; 1837 return false;
1486 } 1838 }
1487 1839
1488 if (!reader.getline()) { return false; } 1840 if (!line_reader.getline()) { return false; }
1489 1841
1490 if (strcmp(reader.ptr(), "\r\n")) { break; } 1842 if (strcmp(line_reader.ptr(), "\r\n")) { break; }
1491 1843
1492 if (!reader.getline()) { return false; } 1844 if (!line_reader.getline()) { return false; }
1493 1845
1494 chunk_len = std::stoi(reader.ptr(), 0, 16); 1846 chunk_len = std::stoi(line_reader.ptr(), 0, 16);
1495 } 1847 }
1496 1848
1497 if (chunk_len == 0) { 1849 if (chunk_len == 0) {
1498 // Reader terminator after chunks 1850 // Reader terminator after chunks
1499 if (!reader.getline() || strcmp(reader.ptr(), "\r\n")) return false; 1851 if (!line_reader.getline() || strcmp(line_reader.ptr(), "\r\n"))
1852 return false;
1500 } 1853 }
1501 1854
1502 return true; 1855 return true;
@@ -1509,14 +1862,14 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) {
1509 1862
1510template <typename T> 1863template <typename T>
1511bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, 1864bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
1512 Progress progress, ContentReceiverCore receiver) { 1865 Progress progress, ContentReceiver receiver) {
1513 1866
1514 ContentReceiverCore out = [&](const char *buf, size_t n) { 1867 ContentReceiver out = [&](const char *buf, size_t n) {
1515 return receiver(buf, n); 1868 return receiver(buf, n);
1516 }; 1869 };
1517 1870
1518#ifdef CPPHTTPLIB_ZLIB_SUPPORT 1871#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1519 detail::decompressor decompressor; 1872 decompressor decompressor;
1520 1873
1521 if (!decompressor.is_valid()) { 1874 if (!decompressor.is_valid()) {
1522 status = 500; 1875 status = 500;
@@ -1586,39 +1939,47 @@ inline ssize_t write_content(Stream &strm, ContentProvider content_provider,
1586 size_t end_offset = offset + length; 1939 size_t end_offset = offset + length;
1587 while (offset < end_offset) { 1940 while (offset < end_offset) {
1588 ssize_t written_length = 0; 1941 ssize_t written_length = 0;
1589 content_provider( 1942
1590 offset, end_offset - offset, 1943 DataSink data_sink;
1591 [&](const char *d, size_t l) { 1944 data_sink.write = [&](const char *d, size_t l) {
1592 offset += l; 1945 offset += l;
1593 written_length = strm.write(d, l); 1946 written_length = strm.write(d, l);
1594 }, 1947 };
1595 [&](void) { written_length = -1; }); 1948 data_sink.done = [&](void) { written_length = -1; };
1949 data_sink.is_writable = [&](void) { return strm.is_writable(); };
1950
1951 content_provider(offset, end_offset - offset, data_sink);
1596 if (written_length < 0) { return written_length; } 1952 if (written_length < 0) { return written_length; }
1597 } 1953 }
1598 return static_cast<ssize_t>(offset - begin_offset); 1954 return static_cast<ssize_t>(offset - begin_offset);
1599} 1955}
1600 1956
1957template <typename T>
1601inline ssize_t write_content_chunked(Stream &strm, 1958inline ssize_t write_content_chunked(Stream &strm,
1602 ContentProvider content_provider) { 1959 ContentProvider content_provider,
1960 T is_shutting_down) {
1603 size_t offset = 0; 1961 size_t offset = 0;
1604 auto data_available = true; 1962 auto data_available = true;
1605 ssize_t total_written_length = 0; 1963 ssize_t total_written_length = 0;
1606 while (data_available) { 1964 while (data_available && !is_shutting_down()) {
1607 ssize_t written_length = 0; 1965 ssize_t written_length = 0;
1608 content_provider( 1966
1609 offset, 0, 1967 DataSink data_sink;
1610 [&](const char *d, size_t l) { 1968 data_sink.write = [&](const char *d, size_t l) {
1611 data_available = l > 0; 1969 data_available = l > 0;
1612 offset += l; 1970 offset += l;
1613 1971
1614 // Emit chunked response header and footer for each chunk 1972 // Emit chunked response header and footer for each chunk
1615 auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n"; 1973 auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n";
1616 written_length = strm.write(chunk); 1974 written_length = strm.write(chunk);
1617 }, 1975 };
1618 [&](void) { 1976 data_sink.done = [&](void) {
1619 data_available = false; 1977 data_available = false;
1620 written_length = strm.write("0\r\n\r\n"); 1978 written_length = strm.write("0\r\n\r\n");
1621 }); 1979 };
1980 data_sink.is_writable = [&](void) { return strm.is_writable(); };
1981
1982 content_provider(offset, 0, data_sink);
1622 1983
1623 if (written_length < 0) { return written_length; } 1984 if (written_length < 0) { return written_length; }
1624 total_written_length += written_length; 1985 total_written_length += written_length;
@@ -1629,17 +1990,12 @@ inline ssize_t write_content_chunked(Stream &strm,
1629template <typename T> 1990template <typename T>
1630inline bool redirect(T &cli, const Request &req, Response &res, 1991inline bool redirect(T &cli, const Request &req, Response &res,
1631 const std::string &path) { 1992 const std::string &path) {
1632 Request new_req; 1993 Request new_req = req;
1633 new_req.method = req.method;
1634 new_req.path = path; 1994 new_req.path = path;
1635 new_req.headers = req.headers; 1995 new_req.redirect_count -= 1;
1636 new_req.body = req.body;
1637 new_req.redirect_count = req.redirect_count - 1;
1638 new_req.response_handler = req.response_handler;
1639 new_req.content_receiver = req.content_receiver;
1640 new_req.progress = req.progress;
1641 1996
1642 Response new_res; 1997 Response new_res;
1998
1643 auto ret = cli.send(new_req, new_res); 1999 auto ret = cli.send(new_req, new_res);
1644 if (ret) { res = new_res; } 2000 if (ret) { res = new_res; }
1645 return ret; 2001 return ret;
@@ -1656,7 +2012,7 @@ inline std::string encode_url(const std::string &s) {
1656 case '\n': result += "%0A"; break; 2012 case '\n': result += "%0A"; break;
1657 case '\'': result += "%27"; break; 2013 case '\'': result += "%27"; break;
1658 case ',': result += "%2C"; break; 2014 case ',': result += "%2C"; break;
1659 case ':': result += "%3A"; break; 2015 // case ':': result += "%3A"; break; // ok? probably...
1660 case ';': result += "%3B"; break; 2016 case ';': result += "%3B"; break;
1661 default: 2017 default:
1662 auto c = static_cast<uint8_t>(s[i]); 2018 auto c = static_cast<uint8_t>(s[i]);
@@ -1716,11 +2072,11 @@ inline void parse_query_text(const std::string &s, Params &params) {
1716 split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) { 2072 split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) {
1717 std::string key; 2073 std::string key;
1718 std::string val; 2074 std::string val;
1719 split(b, e, '=', [&](const char *b, const char *e) { 2075 split(b, e, '=', [&](const char *b2, const char *e2) {
1720 if (key.empty()) { 2076 if (key.empty()) {
1721 key.assign(b, e); 2077 key.assign(b2, e2);
1722 } else { 2078 } else {
1723 val.assign(b, e); 2079 val.assign(b2, e2);
1724 } 2080 }
1725 }); 2081 });
1726 params.emplace(key, decode_url(val)); 2082 params.emplace(key, decode_url(val));
@@ -1736,112 +2092,207 @@ inline bool parse_multipart_boundary(const std::string &content_type,
1736 return true; 2092 return true;
1737} 2093}
1738 2094
1739inline bool parse_multipart_formdata(const std::string &boundary, 2095inline bool parse_range_header(const std::string &s, Ranges &ranges) {
1740 const std::string &body, 2096 static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
1741 MultipartFiles &files) { 2097 std::smatch m;
1742 static std::string dash = "--"; 2098 if (std::regex_match(s, m, re_first_range)) {
1743 static std::string crlf = "\r\n"; 2099 auto pos = m.position(1);
1744 2100 auto len = m.length(1);
1745 static std::regex re_content_type("Content-Type: (.*?)", 2101 bool all_valid_ranges = true;
1746 std::regex_constants::icase); 2102 split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
1747 2103 if (!all_valid_ranges) return;
1748 static std::regex re_content_disposition( 2104 static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
1749 "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?", 2105 std::cmatch cm;
1750 std::regex_constants::icase); 2106 if (std::regex_match(b, e, cm, re_another_range)) {
1751 2107 ssize_t first = -1;
1752 auto dash_boundary = dash + boundary; 2108 if (!cm.str(1).empty()) {
1753 2109 first = static_cast<ssize_t>(std::stoll(cm.str(1)));
1754 auto pos = body.find(dash_boundary); 2110 }
1755 if (pos != 0) { return false; }
1756
1757 pos += dash_boundary.size();
1758 2111
1759 auto next_pos = body.find(crlf, pos); 2112 ssize_t last = -1;
1760 if (next_pos == std::string::npos) { return false; } 2113 if (!cm.str(2).empty()) {
2114 last = static_cast<ssize_t>(std::stoll(cm.str(2)));
2115 }
1761 2116
1762 pos = next_pos + crlf.size(); 2117 if (first != -1 && last != -1 && first > last) {
2118 all_valid_ranges = false;
2119 return;
2120 }
2121 ranges.emplace_back(std::make_pair(first, last));
2122 }
2123 });
2124 return all_valid_ranges;
2125 }
2126 return false;
2127}
1763 2128
1764 while (pos < body.size()) { 2129class MultipartFormDataParser {
1765 next_pos = body.find(crlf, pos); 2130public:
1766 if (next_pos == std::string::npos) { return false; } 2131 MultipartFormDataParser() {}
1767 2132
1768 std::string name; 2133 void set_boundary(const std::string &boundary) { boundary_ = boundary; }
1769 MultipartFile file;
1770 2134
1771 auto header = body.substr(pos, (next_pos - pos)); 2135 bool is_valid() const { return is_valid_; }
1772 2136
1773 while (pos != next_pos) { 2137 template <typename T, typename U>
1774 std::smatch m; 2138 bool parse(const char *buf, size_t n, T content_callback, U header_callback) {
1775 if (std::regex_match(header, m, re_content_type)) { 2139 static const std::regex re_content_type(R"(^Content-Type:\s*(.*?)\s*$)",
1776 file.content_type = m[1]; 2140 std::regex_constants::icase);
1777 } else if (std::regex_match(header, m, re_content_disposition)) { 2141
1778 name = m[1]; 2142 static const std::regex re_content_disposition(
1779 file.filename = m[2]; 2143 "^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename="
2144 "\"(.*?)\")?\\s*$",
2145 std::regex_constants::icase);
2146
2147 buf_.append(buf, n); // TODO: performance improvement
2148
2149 while (!buf_.empty()) {
2150 switch (state_) {
2151 case 0: { // Initial boundary
2152 auto pattern = dash_ + boundary_ + crlf_;
2153 if (pattern.size() > buf_.size()) { return true; }
2154 auto pos = buf_.find(pattern);
2155 if (pos != 0) {
2156 is_done_ = true;
2157 return false;
2158 }
2159 buf_.erase(0, pattern.size());
2160 off_ += pattern.size();
2161 state_ = 1;
2162 break;
1780 } 2163 }
2164 case 1: { // New entry
2165 clear_file_info();
2166 state_ = 2;
2167 break;
2168 }
2169 case 2: { // Headers
2170 auto pos = buf_.find(crlf_);
2171 while (pos != std::string::npos) {
2172 // Empty line
2173 if (pos == 0) {
2174 if (!header_callback(file_)) {
2175 is_valid_ = false;
2176 is_done_ = false;
2177 return false;
2178 }
2179 buf_.erase(0, crlf_.size());
2180 off_ += crlf_.size();
2181 state_ = 3;
2182 break;
2183 }
1781 2184
1782 pos = next_pos + crlf.size(); 2185 auto header = buf_.substr(0, pos);
1783 2186 {
1784 next_pos = body.find(crlf, pos); 2187 std::smatch m;
1785 if (next_pos == std::string::npos) { return false; } 2188 if (std::regex_match(header, m, re_content_type)) {
1786 2189 file_.content_type = m[1];
1787 header = body.substr(pos, (next_pos - pos)); 2190 } else if (std::regex_match(header, m, re_content_disposition)) {
1788 } 2191 file_.name = m[1];
1789 2192 file_.filename = m[2];
1790 pos = next_pos + crlf.size(); 2193 }
2194 }
1791 2195
1792 next_pos = body.find(crlf + dash_boundary, pos); 2196 buf_.erase(0, pos + crlf_.size());
2197 off_ += pos + crlf_.size();
2198 pos = buf_.find(crlf_);
2199 }
2200 break;
2201 }
2202 case 3: { // Body
2203 {
2204 auto pattern = crlf_ + dash_;
2205 if (pattern.size() > buf_.size()) { return true; }
2206
2207 auto pos = buf_.find(pattern);
2208 if (pos == std::string::npos) { pos = buf_.size(); }
2209 if (!content_callback(buf_.data(), pos)) {
2210 is_valid_ = false;
2211 is_done_ = false;
2212 return false;
2213 }
1793 2214
1794 if (next_pos == std::string::npos) { return false; } 2215 off_ += pos;
2216 buf_.erase(0, pos);
2217 }
1795 2218
1796 file.offset = pos; 2219 {
1797 file.length = next_pos - pos; 2220 auto pattern = crlf_ + dash_ + boundary_;
2221 if (pattern.size() > buf_.size()) { return true; }
2222
2223 auto pos = buf_.find(pattern);
2224 if (pos != std::string::npos) {
2225 if (!content_callback(buf_.data(), pos)) {
2226 is_valid_ = false;
2227 is_done_ = false;
2228 return false;
2229 }
1798 2230
1799 pos = next_pos + crlf.size() + dash_boundary.size(); 2231 off_ += pos + pattern.size();
2232 buf_.erase(0, pos + pattern.size());
2233 state_ = 4;
2234 } else {
2235 if (!content_callback(buf_.data(), pattern.size())) {
2236 is_valid_ = false;
2237 is_done_ = false;
2238 return false;
2239 }
1800 2240
1801 next_pos = body.find(crlf, pos); 2241 off_ += pattern.size();
1802 if (next_pos == std::string::npos) { return false; } 2242 buf_.erase(0, pattern.size());
2243 }
2244 }
2245 break;
2246 }
2247 case 4: { // Boundary
2248 if (crlf_.size() > buf_.size()) { return true; }
2249 if (buf_.find(crlf_) == 0) {
2250 buf_.erase(0, crlf_.size());
2251 off_ += crlf_.size();
2252 state_ = 1;
2253 } else {
2254 auto pattern = dash_ + crlf_;
2255 if (pattern.size() > buf_.size()) { return true; }
2256 if (buf_.find(pattern) == 0) {
2257 buf_.erase(0, pattern.size());
2258 off_ += pattern.size();
2259 is_valid_ = true;
2260 state_ = 5;
2261 } else {
2262 is_done_ = true;
2263 return true;
2264 }
2265 }
2266 break;
2267 }
2268 case 5: { // Done
2269 is_valid_ = false;
2270 return false;
2271 }
2272 }
2273 }
1803 2274
1804 files.emplace(name, file); 2275 return true;
2276 }
1805 2277
1806 pos = next_pos + crlf.size(); 2278private:
2279 void clear_file_info() {
2280 file_.name.clear();
2281 file_.filename.clear();
2282 file_.content_type.clear();
1807 } 2283 }
1808 2284
1809 return true; 2285 const std::string dash_ = "--";
1810} 2286 const std::string crlf_ = "\r\n";
2287 std::string boundary_;
1811 2288
1812inline bool parse_range_header(const std::string &s, Ranges &ranges) { 2289 std::string buf_;
1813 try { 2290 size_t state_ = 0;
1814 static auto re = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); 2291 size_t is_valid_ = false;
1815 std::smatch m; 2292 size_t is_done_ = false;
1816 if (std::regex_match(s, m, re)) { 2293 size_t off_ = 0;
1817 auto pos = m.position(1); 2294 MultipartFormData file_;
1818 auto len = m.length(1); 2295};
1819 detail::split(&s[pos], &s[pos + len], ',',
1820 [&](const char *b, const char *e) {
1821 static auto re = std::regex(R"(\s*(\d*)-(\d*))");
1822 std::cmatch m;
1823 if (std::regex_match(b, e, m, re)) {
1824 ssize_t first = -1;
1825 if (!m.str(1).empty()) {
1826 first = static_cast<ssize_t>(std::stoll(m.str(1)));
1827 }
1828
1829 ssize_t last = -1;
1830 if (!m.str(2).empty()) {
1831 last = static_cast<ssize_t>(std::stoll(m.str(2)));
1832 }
1833
1834 if (first != -1 && last != -1 && first > last) {
1835 throw std::runtime_error("invalid range error");
1836 }
1837 ranges.emplace_back(std::make_pair(first, last));
1838 }
1839 });
1840 return true;
1841 }
1842 return false;
1843 } catch (...) { return false; }
1844}
1845 2296
1846inline std::string to_lower(const char *beg, const char *end) { 2297inline std::string to_lower(const char *beg, const char *end) {
1847 std::string out; 2298 std::string out;
@@ -1915,7 +2366,7 @@ bool process_multipart_ranges_data(const Request &req, Response &res,
1915 ctoken("\r\n"); 2366 ctoken("\r\n");
1916 } 2367 }
1917 2368
1918 auto offsets = detail::get_range_offset_and_length(req, res.body.size(), i); 2369 auto offsets = get_range_offset_and_length(req, res.body.size(), i);
1919 auto offset = offsets.first; 2370 auto offset = offsets.first;
1920 auto length = offsets.second; 2371 auto length = offsets.second;
1921 2372
@@ -1978,8 +2429,7 @@ inline bool write_multipart_ranges_data(Stream &strm, const Request &req,
1978 [&](const std::string &token) { strm.write(token); }, 2429 [&](const std::string &token) { strm.write(token); },
1979 [&](const char *token) { strm.write(token); }, 2430 [&](const char *token) { strm.write(token); },
1980 [&](size_t offset, size_t length) { 2431 [&](size_t offset, size_t length) {
1981 return detail::write_content(strm, res.content_provider, offset, 2432 return write_content(strm, res.content_provider, offset, length) >= 0;
1982 length) >= 0;
1983 }); 2433 });
1984} 2434}
1985 2435
@@ -1988,11 +2438,56 @@ get_range_offset_and_length(const Request &req, const Response &res,
1988 size_t index) { 2438 size_t index) {
1989 auto r = req.ranges[index]; 2439 auto r = req.ranges[index];
1990 2440
1991 if (r.second == -1) { r.second = res.content_provider_resource_length - 1; } 2441 if (r.second == -1) { r.second = res.content_length - 1; }
1992 2442
1993 return std::make_pair(r.first, r.second - r.first + 1); 2443 return std::make_pair(r.first, r.second - r.first + 1);
1994} 2444}
1995 2445
2446inline bool expect_content(const Request &req) {
2447 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
2448 req.method == "PRI") {
2449 return true;
2450 }
2451 // TODO: check if Content-Length is set
2452 return false;
2453}
2454
2455#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2456template <typename CTX, typename Init, typename Update, typename Final>
2457inline std::string message_digest(const std::string &s, Init init,
2458 Update update, Final final,
2459 size_t digest_length) {
2460 using namespace std;
2461
2462 std::vector<unsigned char> md(digest_length, 0);
2463 CTX ctx;
2464 init(&ctx);
2465 update(&ctx, s.data(), s.size());
2466 final(md.data(), &ctx);
2467
2468 stringstream ss;
2469 for (auto c : md) {
2470 ss << setfill('0') << setw(2) << hex << (unsigned int)c;
2471 }
2472 return ss.str();
2473}
2474
2475inline std::string MD5(const std::string &s) {
2476 return message_digest<MD5_CTX>(s, MD5_Init, MD5_Update, MD5_Final,
2477 MD5_DIGEST_LENGTH);
2478}
2479
2480inline std::string SHA_256(const std::string &s) {
2481 return message_digest<SHA256_CTX>(s, SHA256_Init, SHA256_Update, SHA256_Final,
2482 SHA256_DIGEST_LENGTH);
2483}
2484
2485inline std::string SHA_512(const std::string &s) {
2486 return message_digest<SHA512_CTX>(s, SHA512_Init, SHA512_Update, SHA512_Final,
2487 SHA512_DIGEST_LENGTH);
2488}
2489#endif
2490
1996#ifdef _WIN32 2491#ifdef _WIN32
1997class WSInit { 2492class WSInit {
1998public: 2493public:
@@ -2025,9 +2520,103 @@ inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
2025 2520
2026inline std::pair<std::string, std::string> 2521inline std::pair<std::string, std::string>
2027make_basic_authentication_header(const std::string &username, 2522make_basic_authentication_header(const std::string &username,
2028 const std::string &password) { 2523 const std::string &password,
2524 bool is_proxy = false) {
2029 auto field = "Basic " + detail::base64_encode(username + ":" + password); 2525 auto field = "Basic " + detail::base64_encode(username + ":" + password);
2030 return std::make_pair("Authorization", field); 2526 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2527 return std::make_pair(key, field);
2528}
2529
2530#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2531inline std::pair<std::string, std::string> make_digest_authentication_header(
2532 const Request &req, const std::map<std::string, std::string> &auth,
2533 size_t cnonce_count, const std::string &cnonce, const std::string &username,
2534 const std::string &password, bool is_proxy = false) {
2535 using namespace std;
2536
2537 string nc;
2538 {
2539 stringstream ss;
2540 ss << setfill('0') << setw(8) << hex << cnonce_count;
2541 nc = ss.str();
2542 }
2543
2544 auto qop = auth.at("qop");
2545 if (qop.find("auth-int") != std::string::npos) {
2546 qop = "auth-int";
2547 } else {
2548 qop = "auth";
2549 }
2550
2551 std::string algo = "MD5";
2552 if (auth.find("algorithm") != auth.end()) { algo = auth.at("algorithm"); }
2553
2554 string response;
2555 {
2556 auto H = algo == "SHA-256"
2557 ? detail::SHA_256
2558 : algo == "SHA-512" ? detail::SHA_512 : detail::MD5;
2559
2560 auto A1 = username + ":" + auth.at("realm") + ":" + password;
2561
2562 auto A2 = req.method + ":" + req.path;
2563 if (qop == "auth-int") { A2 += ":" + H(req.body); }
2564
2565 response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce +
2566 ":" + qop + ":" + H(A2));
2567 }
2568
2569 auto field = "Digest username=\"hello\", realm=\"" + auth.at("realm") +
2570 "\", nonce=\"" + auth.at("nonce") + "\", uri=\"" + req.path +
2571 "\", algorithm=" + algo + ", qop=" + qop + ", nc=\"" + nc +
2572 "\", cnonce=\"" + cnonce + "\", response=\"" + response + "\"";
2573
2574 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2575 return std::make_pair(key, field);
2576}
2577#endif
2578
2579inline bool parse_www_authenticate(const httplib::Response &res,
2580 std::map<std::string, std::string> &auth,
2581 bool is_proxy) {
2582 auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
2583 if (res.has_header(auth_key)) {
2584 static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~");
2585 auto s = res.get_header_value(auth_key);
2586 auto pos = s.find(' ');
2587 if (pos != std::string::npos) {
2588 auto type = s.substr(0, pos);
2589 if (type == "Basic") {
2590 return false;
2591 } else if (type == "Digest") {
2592 s = s.substr(pos + 1);
2593 auto beg = std::sregex_iterator(s.begin(), s.end(), re);
2594 for (auto i = beg; i != std::sregex_iterator(); ++i) {
2595 auto m = *i;
2596 auto key = s.substr(m.position(1), m.length(1));
2597 auto val = m.length(2) > 0 ? s.substr(m.position(2), m.length(2))
2598 : s.substr(m.position(3), m.length(3));
2599 auth[key] = val;
2600 }
2601 return true;
2602 }
2603 }
2604 }
2605 return false;
2606}
2607
2608// https://stackoverflow.com/questions/440133/how-do-i-create-a-random-alpha-numeric-string-in-c/440240#answer-440240
2609inline std::string random_string(size_t length) {
2610 auto randchar = []() -> char {
2611 const char charset[] = "0123456789"
2612 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2613 "abcdefghijklmnopqrstuvwxyz";
2614 const size_t max_index = (sizeof(charset) - 1);
2615 return charset[rand() % max_index];
2616 };
2617 std::string str(length, 0);
2618 std::generate_n(str.begin(), length, randchar);
2619 return str;
2031} 2620}
2032 2621
2033// Request implementation 2622// Request implementation
@@ -2068,14 +2657,19 @@ inline size_t Request::get_param_value_count(const char *key) const {
2068 return std::distance(r.first, r.second); 2657 return std::distance(r.first, r.second);
2069} 2658}
2070 2659
2660inline bool Request::is_multipart_form_data() const {
2661 const auto &content_type = get_header_value("Content-Type");
2662 return !content_type.find("multipart/form-data");
2663}
2664
2071inline bool Request::has_file(const char *key) const { 2665inline bool Request::has_file(const char *key) const {
2072 return files.find(key) != files.end(); 2666 return files.find(key) != files.end();
2073} 2667}
2074 2668
2075inline MultipartFile Request::get_file_value(const char *key) const { 2669inline MultipartFormData Request::get_file_value(const char *key) const {
2076 auto it = files.find(key); 2670 auto it = files.find(key);
2077 if (it != files.end()) { return it->second; } 2671 if (it != files.end()) { return it->second; }
2078 return MultipartFile(); 2672 return MultipartFormData();
2079} 2673}
2080 2674
2081// Response implementation 2675// Response implementation
@@ -2119,40 +2713,47 @@ inline void Response::set_content(const std::string &s,
2119} 2713}
2120 2714
2121inline void Response::set_content_provider( 2715inline void Response::set_content_provider(
2122 size_t length, 2716 size_t in_length,
2123 std::function<void(size_t offset, size_t length, DataSink sink)> provider, 2717 std::function<void(size_t offset, size_t length, DataSink &sink)> provider,
2124 std::function<void()> resource_releaser) { 2718 std::function<void()> resource_releaser) {
2125 assert(length > 0); 2719 assert(in_length > 0);
2126 content_provider_resource_length = length; 2720 content_length = in_length;
2127 content_provider = [provider](size_t offset, size_t length, DataSink sink, 2721 content_provider = [provider](size_t offset, size_t length, DataSink &sink) {
2128 Done) { provider(offset, length, sink); }; 2722 provider(offset, length, sink);
2723 };
2129 content_provider_resource_releaser = resource_releaser; 2724 content_provider_resource_releaser = resource_releaser;
2130} 2725}
2131 2726
2132inline void Response::set_chunked_content_provider( 2727inline void Response::set_chunked_content_provider(
2133 std::function<void(size_t offset, DataSink sink, Done done)> provider, 2728 std::function<void(size_t offset, DataSink &sink)> provider,
2134 std::function<void()> resource_releaser) { 2729 std::function<void()> resource_releaser) {
2135 content_provider_resource_length = 0; 2730 content_length = 0;
2136 content_provider = [provider](size_t offset, size_t, DataSink sink, 2731 content_provider = [provider](size_t offset, size_t, DataSink &sink) {
2137 Done done) { provider(offset, sink, done); }; 2732 provider(offset, sink);
2733 };
2138 content_provider_resource_releaser = resource_releaser; 2734 content_provider_resource_releaser = resource_releaser;
2139} 2735}
2140 2736
2141// Rstream implementation 2737// Rstream implementation
2738inline int Stream::write(const char *ptr) { return write(ptr, strlen(ptr)); }
2739
2740inline int Stream::write(const std::string &s) {
2741 return write(s.data(), s.size());
2742}
2743
2142template <typename... Args> 2744template <typename... Args>
2143inline int Stream::write_format(const char *fmt, const Args &... args) { 2745inline int Stream::write_format(const char *fmt, const Args &... args) {
2144 const auto bufsiz = 2048; 2746 std::array<char, 2048> buf;
2145 char buf[bufsiz];
2146 2747
2147#if defined(_MSC_VER) && _MSC_VER < 1900 2748#if defined(_MSC_VER) && _MSC_VER < 1900
2148 auto n = _snprintf_s(buf, bufsiz, bufsiz - 1, fmt, args...); 2749 auto n = _snprintf_s(buf, bufsiz, buf.size() - 1, fmt, args...);
2149#else 2750#else
2150 auto n = snprintf(buf, bufsiz - 1, fmt, args...); 2751 auto n = snprintf(buf.data(), buf.size() - 1, fmt, args...);
2151#endif 2752#endif
2152 if (n <= 0) { return n; } 2753 if (n <= 0) { return n; }
2153 2754
2154 if (n >= bufsiz - 1) { 2755 if (n >= static_cast<int>(buf.size()) - 1) {
2155 std::vector<char> glowable_buf(bufsiz); 2756 std::vector<char> glowable_buf(buf.size());
2156 2757
2157 while (n >= static_cast<int>(glowable_buf.size() - 1)) { 2758 while (n >= static_cast<int>(glowable_buf.size() - 1)) {
2158 glowable_buf.resize(glowable_buf.size() * 2); 2759 glowable_buf.resize(glowable_buf.size() * 2);
@@ -2165,33 +2766,36 @@ inline int Stream::write_format(const char *fmt, const Args &... args) {
2165 } 2766 }
2166 return write(&glowable_buf[0], n); 2767 return write(&glowable_buf[0], n);
2167 } else { 2768 } else {
2168 return write(buf, n); 2769 return write(buf.data(), n);
2169 } 2770 }
2170} 2771}
2171 2772
2773namespace detail {
2774
2172// Socket stream implementation 2775// Socket stream implementation
2173inline SocketStream::SocketStream(socket_t sock) : sock_(sock) {} 2776inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec,
2777 time_t read_timeout_usec)
2778 : sock_(sock), read_timeout_sec_(read_timeout_sec),
2779 read_timeout_usec_(read_timeout_usec) {}
2174 2780
2175inline SocketStream::~SocketStream() {} 2781inline SocketStream::~SocketStream() {}
2176 2782
2177inline int SocketStream::read(char *ptr, size_t size) { 2783inline bool SocketStream::is_readable() const {
2178 if (detail::select_read(sock_, CPPHTTPLIB_READ_TIMEOUT_SECOND, 2784 return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
2179 CPPHTTPLIB_READ_TIMEOUT_USECOND) > 0) {
2180 return recv(sock_, ptr, static_cast<int>(size), 0);
2181 }
2182 return -1;
2183} 2785}
2184 2786
2185inline int SocketStream::write(const char *ptr, size_t size) { 2787inline bool SocketStream::is_writable() const {
2186 return send(sock_, ptr, static_cast<int>(size), 0); 2788 return detail::select_write(sock_, 0, 0) > 0;
2187} 2789}
2188 2790
2189inline int SocketStream::write(const char *ptr) { 2791inline int SocketStream::read(char *ptr, size_t size) {
2190 return write(ptr, strlen(ptr)); 2792 if (is_readable()) { return recv(sock_, ptr, static_cast<int>(size), 0); }
2793 return -1;
2191} 2794}
2192 2795
2193inline int SocketStream::write(const std::string &s) { 2796inline int SocketStream::write(const char *ptr, size_t size) {
2194 return write(s.data(), s.size()); 2797 if (is_writable()) { return send(sock_, ptr, static_cast<int>(size), 0); }
2798 return -1;
2195} 2799}
2196 2800
2197inline std::string SocketStream::get_remote_addr() const { 2801inline std::string SocketStream::get_remote_addr() const {
@@ -2199,12 +2803,18 @@ inline std::string SocketStream::get_remote_addr() const {
2199} 2803}
2200 2804
2201// Buffer stream implementation 2805// Buffer stream implementation
2806inline bool BufferStream::is_readable() const { return true; }
2807
2808inline bool BufferStream::is_writable() const { return true; }
2809
2202inline int BufferStream::read(char *ptr, size_t size) { 2810inline int BufferStream::read(char *ptr, size_t size) {
2203#if defined(_MSC_VER) && _MSC_VER < 1900 2811#if defined(_MSC_VER) && _MSC_VER < 1900
2204 return static_cast<int>(buffer._Copy_s(ptr, size, size)); 2812 int len_read = static_cast<int>(buffer._Copy_s(ptr, size, size, position));
2205#else 2813#else
2206 return static_cast<int>(buffer.copy(ptr, size)); 2814 int len_read = static_cast<int>(buffer.copy(ptr, size, position));
2207#endif 2815#endif
2816 position += len_read;
2817 return len_read;
2208} 2818}
2209 2819
2210inline int BufferStream::write(const char *ptr, size_t size) { 2820inline int BufferStream::write(const char *ptr, size_t size) {
@@ -2212,33 +2822,23 @@ inline int BufferStream::write(const char *ptr, size_t size) {
2212 return static_cast<int>(size); 2822 return static_cast<int>(size);
2213} 2823}
2214 2824
2215inline int BufferStream::write(const char *ptr) {
2216 return write(ptr, strlen(ptr));
2217}
2218
2219inline int BufferStream::write(const std::string &s) {
2220 return write(s.data(), s.size());
2221}
2222
2223inline std::string BufferStream::get_remote_addr() const { return ""; } 2825inline std::string BufferStream::get_remote_addr() const { return ""; }
2224 2826
2225inline const std::string &BufferStream::get_buffer() const { return buffer; } 2827inline const std::string &BufferStream::get_buffer() const { return buffer; }
2226 2828
2829} // namespace detail
2830
2227// HTTP server implementation 2831// HTTP server implementation
2228inline Server::Server() 2832inline Server::Server()
2229 : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), 2833 : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
2834 read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
2835 read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
2230 payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false), 2836 payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false),
2231 svr_sock_(INVALID_SOCKET) { 2837 svr_sock_(INVALID_SOCKET) {
2232#ifndef _WIN32 2838#ifndef _WIN32
2233 signal(SIGPIPE, SIG_IGN); 2839 signal(SIGPIPE, SIG_IGN);
2234#endif 2840#endif
2235 new_task_queue = [] { 2841 new_task_queue = [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); };
2236#if CPPHTTPLIB_THREAD_POOL_COUNT > 0
2237 return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT);
2238#else
2239 return new Threads();
2240#endif
2241 };
2242} 2842}
2243 2843
2244inline Server::~Server() {} 2844inline Server::~Server() {}
@@ -2253,16 +2853,37 @@ inline Server &Server::Post(const char *pattern, Handler handler) {
2253 return *this; 2853 return *this;
2254} 2854}
2255 2855
2856inline Server &Server::Post(const char *pattern,
2857 HandlerWithContentReader handler) {
2858 post_handlers_for_content_reader_.push_back(
2859 std::make_pair(std::regex(pattern), handler));
2860 return *this;
2861}
2862
2256inline Server &Server::Put(const char *pattern, Handler handler) { 2863inline Server &Server::Put(const char *pattern, Handler handler) {
2257 put_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2864 put_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2258 return *this; 2865 return *this;
2259} 2866}
2260 2867
2868inline Server &Server::Put(const char *pattern,
2869 HandlerWithContentReader handler) {
2870 put_handlers_for_content_reader_.push_back(
2871 std::make_pair(std::regex(pattern), handler));
2872 return *this;
2873}
2874
2261inline Server &Server::Patch(const char *pattern, Handler handler) { 2875inline Server &Server::Patch(const char *pattern, Handler handler) {
2262 patch_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2876 patch_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2263 return *this; 2877 return *this;
2264} 2878}
2265 2879
2880inline Server &Server::Patch(const char *pattern,
2881 HandlerWithContentReader handler) {
2882 patch_handlers_for_content_reader_.push_back(
2883 std::make_pair(std::regex(pattern), handler));
2884 return *this;
2885}
2886
2266inline Server &Server::Delete(const char *pattern, Handler handler) { 2887inline Server &Server::Delete(const char *pattern, Handler handler) {
2267 delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 2888 delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler));
2268 return *this; 2889 return *this;
@@ -2273,32 +2894,68 @@ inline Server &Server::Options(const char *pattern, Handler handler) {
2273 return *this; 2894 return *this;
2274} 2895}
2275 2896
2276inline bool Server::set_base_dir(const char *path) { 2897inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
2277 if (detail::is_dir(path)) { 2898 return set_mount_point(mount_point, dir);
2278 base_dir_ = path; 2899}
2279 return true; 2900
2901inline bool Server::set_mount_point(const char *mount_point, const char *dir) {
2902 if (detail::is_dir(dir)) {
2903 std::string mnt = mount_point ? mount_point : "/";
2904 if (!mnt.empty() && mnt[0] == '/') {
2905 base_dirs_.emplace_back(mnt, dir);
2906 return true;
2907 }
2908 }
2909 return false;
2910}
2911
2912inline bool Server::remove_mount_point(const char *mount_point) {
2913 for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
2914 if (it->first == mount_point) {
2915 base_dirs_.erase(it);
2916 return true;
2917 }
2280 } 2918 }
2281 return false; 2919 return false;
2282} 2920}
2283 2921
2922inline void Server::set_file_extension_and_mimetype_mapping(const char *ext,
2923 const char *mime) {
2924 file_extension_and_mimetype_map_[ext] = mime;
2925}
2926
2284inline void Server::set_file_request_handler(Handler handler) { 2927inline void Server::set_file_request_handler(Handler handler) {
2285 file_request_handler_ = handler; 2928 file_request_handler_ = std::move(handler);
2286} 2929}
2287 2930
2288inline void Server::set_error_handler(Handler handler) { 2931inline void Server::set_error_handler(Handler handler) {
2289 error_handler_ = handler; 2932 error_handler_ = std::move(handler);
2290} 2933}
2291 2934
2292inline void Server::set_logger(Logger logger) { logger_ = logger; } 2935inline void Server::set_logger(Logger logger) { logger_ = std::move(logger); }
2936
2937inline void
2938Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
2939 expect_100_continue_handler_ = std::move(handler);
2940}
2293 2941
2294inline void Server::set_keep_alive_max_count(size_t count) { 2942inline void Server::set_keep_alive_max_count(size_t count) {
2295 keep_alive_max_count_ = count; 2943 keep_alive_max_count_ = count;
2296} 2944}
2297 2945
2946inline void Server::set_read_timeout(time_t sec, time_t usec) {
2947 read_timeout_sec_ = sec;
2948 read_timeout_usec_ = usec;
2949}
2950
2298inline void Server::set_payload_max_length(size_t length) { 2951inline void Server::set_payload_max_length(size_t length) {
2299 payload_max_length_ = length; 2952 payload_max_length_ = length;
2300} 2953}
2301 2954
2955inline bool Server::bind_to_port(const char *host, int port, int socket_flags) {
2956 if (bind_internal(host, port, socket_flags) < 0) return false;
2957 return true;
2958}
2302inline int Server::bind_to_any_port(const char *host, int socket_flags) { 2959inline int Server::bind_to_any_port(const char *host, int socket_flags) {
2303 return bind_internal(host, 0, socket_flags); 2960 return bind_internal(host, 0, socket_flags);
2304} 2961}
@@ -2306,8 +2963,7 @@ inline int Server::bind_to_any_port(const char *host, int socket_flags) {
2306inline bool Server::listen_after_bind() { return listen_internal(); } 2963inline bool Server::listen_after_bind() { return listen_internal(); }
2307 2964
2308inline bool Server::listen(const char *host, int port, int socket_flags) { 2965inline bool Server::listen(const char *host, int port, int socket_flags) {
2309 if (bind_internal(host, port, socket_flags) < 0) return false; 2966 return bind_to_port(host, port, socket_flags) && listen_internal();
2310 return listen_internal();
2311} 2967}
2312 2968
2313inline bool Server::is_running() const { return is_running_; } 2969inline bool Server::is_running() const { return is_running_; }
@@ -2322,8 +2978,9 @@ inline void Server::stop() {
2322} 2978}
2323 2979
2324inline bool Server::parse_request_line(const char *s, Request &req) { 2980inline bool Server::parse_request_line(const char *s, Request &req) {
2325 static std::regex re("(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI) " 2981 const static std::regex re(
2326 "(([^?]+)(?:\\?(.+?))?) (HTTP/1\\.[01])\r\n"); 2982 "(GET|HEAD|POST|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH|PRI) "
2983 "(([^?]+)(?:\\?(.*?))?) (HTTP/1\\.[01])\r\n");
2327 2984
2328 std::cmatch m; 2985 std::cmatch m;
2329 if (std::regex_match(s, m, re)) { 2986 if (std::regex_match(s, m, re)) {
@@ -2348,9 +3005,11 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2348 3005
2349 if (400 <= res.status && error_handler_) { error_handler_(req, res); } 3006 if (400 <= res.status && error_handler_) { error_handler_(req, res); }
2350 3007
3008 detail::BufferStream bstrm;
3009
2351 // Response line 3010 // Response line
2352 if (!strm.write_format("HTTP/1.1 %d %s\r\n", res.status, 3011 if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status,
2353 detail::status_message(res.status))) { 3012 detail::status_message(res.status))) {
2354 return false; 3013 return false;
2355 } 3014 }
2356 3015
@@ -2363,11 +3022,12 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2363 res.set_header("Connection", "Keep-Alive"); 3022 res.set_header("Connection", "Keep-Alive");
2364 } 3023 }
2365 3024
2366 if (!res.has_header("Content-Type")) { 3025 if (!res.has_header("Content-Type") &&
3026 (!res.body.empty() || res.content_length > 0)) {
2367 res.set_header("Content-Type", "text/plain"); 3027 res.set_header("Content-Type", "text/plain");
2368 } 3028 }
2369 3029
2370 if (!res.has_header("Accept-Ranges")) { 3030 if (!res.has_header("Accept-Ranges") && req.method == "HEAD") {
2371 res.set_header("Accept-Ranges", "bytes"); 3031 res.set_header("Accept-Ranges", "bytes");
2372 } 3032 }
2373 3033
@@ -2388,17 +3048,17 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2388 } 3048 }
2389 3049
2390 if (res.body.empty()) { 3050 if (res.body.empty()) {
2391 if (res.content_provider_resource_length > 0) { 3051 if (res.content_length > 0) {
2392 size_t length = 0; 3052 size_t length = 0;
2393 if (req.ranges.empty()) { 3053 if (req.ranges.empty()) {
2394 length = res.content_provider_resource_length; 3054 length = res.content_length;
2395 } else if (req.ranges.size() == 1) { 3055 } else if (req.ranges.size() == 1) {
2396 auto offsets = detail::get_range_offset_and_length( 3056 auto offsets =
2397 req, res.content_provider_resource_length, 0); 3057 detail::get_range_offset_and_length(req, res.content_length, 0);
2398 auto offset = offsets.first; 3058 auto offset = offsets.first;
2399 length = offsets.second; 3059 length = offsets.second;
2400 auto content_range = detail::make_content_range_header_field( 3060 auto content_range = detail::make_content_range_header_field(
2401 offset, length, res.content_provider_resource_length); 3061 offset, length, res.content_length);
2402 res.set_header("Content-Range", content_range); 3062 res.set_header("Content-Range", content_range);
2403 } else { 3063 } else {
2404 length = detail::get_multipart_ranges_data_length(req, res, boundary, 3064 length = detail::get_multipart_ranges_data_length(req, res, boundary,
@@ -2430,7 +3090,7 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2430 } 3090 }
2431 3091
2432#ifdef CPPHTTPLIB_ZLIB_SUPPORT 3092#ifdef CPPHTTPLIB_ZLIB_SUPPORT
2433 // TODO: 'Accpet-Encoding' has gzip, not gzip;q=0 3093 // TODO: 'Accept-Encoding' has gzip, not gzip;q=0
2434 const auto &encodings = req.get_header_value("Accept-Encoding"); 3094 const auto &encodings = req.get_header_value("Accept-Encoding");
2435 if (encodings.find("gzip") != std::string::npos && 3095 if (encodings.find("gzip") != std::string::npos &&
2436 detail::can_compress(res.get_header_value("Content-Type"))) { 3096 detail::can_compress(res.get_header_value("Content-Type"))) {
@@ -2444,7 +3104,11 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
2444 res.set_header("Content-Length", length); 3104 res.set_header("Content-Length", length);
2445 } 3105 }
2446 3106
2447 if (!detail::write_headers(strm, res, Headers())) { return false; } 3107 if (!detail::write_headers(bstrm, res, Headers())) { return false; }
3108
3109 // Flush buffer
3110 auto &data = bstrm.get_buffer();
3111 strm.write(data.data(), data.size());
2448 3112
2449 // Body 3113 // Body
2450 if (req.method != "HEAD") { 3114 if (req.method != "HEAD") {
@@ -2468,15 +3132,15 @@ inline bool
2468Server::write_content_with_provider(Stream &strm, const Request &req, 3132Server::write_content_with_provider(Stream &strm, const Request &req,
2469 Response &res, const std::string &boundary, 3133 Response &res, const std::string &boundary,
2470 const std::string &content_type) { 3134 const std::string &content_type) {
2471 if (res.content_provider_resource_length) { 3135 if (res.content_length) {
2472 if (req.ranges.empty()) { 3136 if (req.ranges.empty()) {
2473 if (detail::write_content(strm, res.content_provider, 0, 3137 if (detail::write_content(strm, res.content_provider, 0,
2474 res.content_provider_resource_length) < 0) { 3138 res.content_length) < 0) {
2475 return false; 3139 return false;
2476 } 3140 }
2477 } else if (req.ranges.size() == 1) { 3141 } else if (req.ranges.size() == 1) {
2478 auto offsets = detail::get_range_offset_and_length( 3142 auto offsets =
2479 req, res.content_provider_resource_length, 0); 3143 detail::get_range_offset_and_length(req, res.content_length, 0);
2480 auto offset = offsets.first; 3144 auto offset = offsets.first;
2481 auto length = offsets.second; 3145 auto length = offsets.second;
2482 if (detail::write_content(strm, res.content_provider, offset, length) < 3146 if (detail::write_content(strm, res.content_provider, offset, length) <
@@ -2490,29 +3154,123 @@ Server::write_content_with_provider(Stream &strm, const Request &req,
2490 } 3154 }
2491 } 3155 }
2492 } else { 3156 } else {
2493 if (detail::write_content_chunked(strm, res.content_provider) < 0) { 3157 auto is_shutting_down = [this]() {
3158 return this->svr_sock_ == INVALID_SOCKET;
3159 };
3160 if (detail::write_content_chunked(strm, res.content_provider,
3161 is_shutting_down) < 0) {
2494 return false; 3162 return false;
2495 } 3163 }
2496 } 3164 }
2497 return true; 3165 return true;
2498} 3166}
2499 3167
2500inline bool Server::handle_file_request(Request &req, Response &res) { 3168inline bool Server::read_content(Stream &strm, bool last_connection,
2501 if (!base_dir_.empty() && detail::is_valid_path(req.path)) { 3169 Request &req, Response &res) {
2502 std::string path = base_dir_ + req.path; 3170 MultipartFormDataMap::iterator cur;
3171 auto ret = read_content_core(
3172 strm, last_connection, req, res,
3173 // Regular
3174 [&](const char *buf, size_t n) {
3175 if (req.body.size() + n > req.body.max_size()) { return false; }
3176 req.body.append(buf, n);
3177 return true;
3178 },
3179 // Multipart
3180 [&](const MultipartFormData &file) {
3181 cur = req.files.emplace(file.name, file);
3182 return true;
3183 },
3184 [&](const char *buf, size_t n) {
3185 auto &content = cur->second.content;
3186 if (content.size() + n > content.max_size()) { return false; }
3187 content.append(buf, n);
3188 return true;
3189 });
2503 3190
2504 if (!path.empty() && path.back() == '/') { path += "index.html"; } 3191 const auto &content_type = req.get_header_value("Content-Type");
3192 if (!content_type.find("application/x-www-form-urlencoded")) {
3193 detail::parse_query_text(req.body, req.params);
3194 }
2505 3195
2506 if (detail::is_file(path)) { 3196 return ret;
2507 detail::read_file(path, res.body); 3197}
2508 auto type = detail::find_content_type(path); 3198
2509 if (type) { res.set_header("Content-Type", type); } 3199inline bool Server::read_content_with_content_receiver(
2510 res.status = 200; 3200 Stream &strm, bool last_connection, Request &req, Response &res,
2511 if (file_request_handler_) { file_request_handler_(req, res); } 3201 ContentReceiver receiver, MultipartContentHeader multipart_header,
2512 return true; 3202 ContentReceiver multipart_receiver) {
3203 return read_content_core(strm, last_connection, req, res, receiver,
3204 multipart_header, multipart_receiver);
3205}
3206
3207inline bool Server::read_content_core(Stream &strm, bool last_connection,
3208 Request &req, Response &res,
3209 ContentReceiver receiver,
3210 MultipartContentHeader mulitpart_header,
3211 ContentReceiver multipart_receiver) {
3212 detail::MultipartFormDataParser multipart_form_data_parser;
3213 ContentReceiver out;
3214
3215 if (req.is_multipart_form_data()) {
3216 const auto &content_type = req.get_header_value("Content-Type");
3217 std::string boundary;
3218 if (!detail::parse_multipart_boundary(content_type, boundary)) {
3219 res.status = 400;
3220 return write_response(strm, last_connection, req, res);
2513 } 3221 }
3222
3223 multipart_form_data_parser.set_boundary(boundary);
3224 out = [&](const char *buf, size_t n) {
3225 return multipart_form_data_parser.parse(buf, n, multipart_receiver,
3226 mulitpart_header);
3227 };
3228 } else {
3229 out = receiver;
3230 }
3231
3232 if (!detail::read_content(strm, req, payload_max_length_, res.status,
3233 Progress(), out)) {
3234 return write_response(strm, last_connection, req, res);
2514 } 3235 }
2515 3236
3237 if (req.is_multipart_form_data()) {
3238 if (!multipart_form_data_parser.is_valid()) {
3239 res.status = 400;
3240 return write_response(strm, last_connection, req, res);
3241 }
3242 }
3243
3244 return true;
3245}
3246
3247inline bool Server::handle_file_request(Request &req, Response &res,
3248 bool head) {
3249 for (const auto &kv : base_dirs_) {
3250 const auto &mount_point = kv.first;
3251 const auto &base_dir = kv.second;
3252
3253 // Prefix match
3254 if (!req.path.find(mount_point)) {
3255 std::string sub_path = "/" + req.path.substr(mount_point.size());
3256 if (detail::is_valid_path(sub_path)) {
3257 auto path = base_dir + sub_path;
3258 if (path.back() == '/') { path += "index.html"; }
3259
3260 if (detail::is_file(path)) {
3261 detail::read_file(path, res.body);
3262 auto type =
3263 detail::find_content_type(path, file_extension_and_mimetype_map_);
3264 if (type) { res.set_header("Content-Type", type); }
3265 res.status = 200;
3266 if (!head && file_request_handler_) {
3267 file_request_handler_(req, res);
3268 }
3269 return true;
3270 }
3271 }
3272 }
3273 }
2516 return false; 3274 return false;
2517} 3275}
2518 3276
@@ -2605,9 +3363,51 @@ inline bool Server::listen_internal() {
2605 return ret; 3363 return ret;
2606} 3364}
2607 3365
2608inline bool Server::routing(Request &req, Response &res) { 3366inline bool Server::routing(Request &req, Response &res, Stream &strm,
2609 if (req.method == "GET" && handle_file_request(req, res)) { return true; } 3367 bool last_connection) {
3368 // File handler
3369 bool is_head_request = req.method == "HEAD";
3370 if ((req.method == "GET" || is_head_request) &&
3371 handle_file_request(req, res, is_head_request)) {
3372 return true;
3373 }
3374
3375 if (detail::expect_content(req)) {
3376 // Content reader handler
3377 {
3378 ContentReader reader(
3379 [&](ContentReceiver receiver) {
3380 return read_content_with_content_receiver(
3381 strm, last_connection, req, res, receiver, nullptr, nullptr);
3382 },
3383 [&](MultipartContentHeader header, ContentReceiver receiver) {
3384 return read_content_with_content_receiver(
3385 strm, last_connection, req, res, nullptr, header, receiver);
3386 });
3387
3388 if (req.method == "POST") {
3389 if (dispatch_request_for_content_reader(
3390 req, res, reader, post_handlers_for_content_reader_)) {
3391 return true;
3392 }
3393 } else if (req.method == "PUT") {
3394 if (dispatch_request_for_content_reader(
3395 req, res, reader, put_handlers_for_content_reader_)) {
3396 return true;
3397 }
3398 } else if (req.method == "PATCH") {
3399 if (dispatch_request_for_content_reader(
3400 req, res, reader, patch_handlers_for_content_reader_)) {
3401 return true;
3402 }
3403 }
3404 }
2610 3405
3406 // Read content into `req.body`
3407 if (!read_content(strm, last_connection, req, res)) { return false; }
3408 }
3409
3410 // Regular handler
2611 if (req.method == "GET" || req.method == "HEAD") { 3411 if (req.method == "GET" || req.method == "HEAD") {
2612 return dispatch_request(req, res, get_handlers_); 3412 return dispatch_request(req, res, get_handlers_);
2613 } else if (req.method == "POST") { 3413 } else if (req.method == "POST") {
@@ -2640,17 +3440,31 @@ inline bool Server::dispatch_request(Request &req, Response &res,
2640 return false; 3440 return false;
2641} 3441}
2642 3442
3443inline bool Server::dispatch_request_for_content_reader(
3444 Request &req, Response &res, ContentReader content_reader,
3445 HandlersForContentReader &handlers) {
3446 for (const auto &x : handlers) {
3447 const auto &pattern = x.first;
3448 const auto &handler = x.second;
3449
3450 if (std::regex_match(req.path, req.matches, pattern)) {
3451 handler(req, res, content_reader);
3452 return true;
3453 }
3454 }
3455 return false;
3456}
3457
2643inline bool 3458inline bool
2644Server::process_request(Stream &strm, bool last_connection, 3459Server::process_request(Stream &strm, bool last_connection,
2645 bool &connection_close, 3460 bool &connection_close,
2646 std::function<void(Request &)> setup_request) { 3461 const std::function<void(Request &)> &setup_request) {
2647 const auto bufsiz = 2048; 3462 std::array<char, 2048> buf{};
2648 char buf[bufsiz];
2649 3463
2650 detail::stream_line_reader reader(strm, buf, bufsiz); 3464 detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
2651 3465
2652 // Connection has been closed on client 3466 // Connection has been closed on client
2653 if (!reader.getline()) { return false; } 3467 if (!line_reader.getline()) { return false; }
2654 3468
2655 Request req; 3469 Request req;
2656 Response res; 3470 Response res;
@@ -2658,7 +3472,7 @@ Server::process_request(Stream &strm, bool last_connection,
2658 res.version = "HTTP/1.1"; 3472 res.version = "HTTP/1.1";
2659 3473
2660 // Check if the request URI doesn't exceed the limit 3474 // Check if the request URI doesn't exceed the limit
2661 if (reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) { 3475 if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
2662 Headers dummy; 3476 Headers dummy;
2663 detail::read_headers(strm, dummy); 3477 detail::read_headers(strm, dummy);
2664 res.status = 414; 3478 res.status = 414;
@@ -2666,7 +3480,7 @@ Server::process_request(Stream &strm, bool last_connection,
2666 } 3480 }
2667 3481
2668 // Request line and headers 3482 // Request line and headers
2669 if (!parse_request_line(reader.ptr(), req) || 3483 if (!parse_request_line(line_reader.ptr(), req) ||
2670 !detail::read_headers(strm, req.headers)) { 3484 !detail::read_headers(strm, req.headers)) {
2671 res.status = 400; 3485 res.status = 400;
2672 return write_response(strm, last_connection, req, res); 3486 return write_response(strm, last_connection, req, res);
@@ -2683,33 +3497,6 @@ Server::process_request(Stream &strm, bool last_connection,
2683 3497
2684 req.set_header("REMOTE_ADDR", strm.get_remote_addr()); 3498 req.set_header("REMOTE_ADDR", strm.get_remote_addr());
2685 3499
2686 // Body
2687 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || req.method == "PRI") {
2688 if (!detail::read_content(strm, req, payload_max_length_, res.status,
2689 Progress(), [&](const char *buf, size_t n) {
2690 if (req.body.size() + n > req.body.max_size()) {
2691 return false;
2692 }
2693 req.body.append(buf, n);
2694 return true;
2695 })) {
2696 return write_response(strm, last_connection, req, res);
2697 }
2698
2699 const auto &content_type = req.get_header_value("Content-Type");
2700
2701 if (!content_type.find("application/x-www-form-urlencoded")) {
2702 detail::parse_query_text(req.body, req.params);
2703 } else if (!content_type.find("multipart/form-data")) {
2704 std::string boundary;
2705 if (!detail::parse_multipart_boundary(content_type, boundary) ||
2706 !detail::parse_multipart_formdata(boundary, req.body, req.files)) {
2707 res.status = 400;
2708 return write_response(strm, last_connection, req, res);
2709 }
2710 }
2711 }
2712
2713 if (req.has_header("Range")) { 3500 if (req.has_header("Range")) {
2714 const auto &range_header_value = req.get_header_value("Range"); 3501 const auto &range_header_value = req.get_header_value("Range");
2715 if (!detail::parse_range_header(range_header_value, req.ranges)) { 3502 if (!detail::parse_range_header(range_header_value, req.ranges)) {
@@ -2719,7 +3506,23 @@ Server::process_request(Stream &strm, bool last_connection,
2719 3506
2720 if (setup_request) { setup_request(req); } 3507 if (setup_request) { setup_request(req); }
2721 3508
2722 if (routing(req, res)) { 3509 if (req.get_header_value("Expect") == "100-continue") {
3510 auto status = 100;
3511 if (expect_100_continue_handler_) {
3512 status = expect_100_continue_handler_(req, res);
3513 }
3514 switch (status) {
3515 case 100:
3516 case 417:
3517 strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status,
3518 detail::status_message(status));
3519 break;
3520 default: return write_response(strm, last_connection, req, res);
3521 }
3522 }
3523
3524 // Rounting
3525 if (routing(req, res, strm, last_connection)) {
2723 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } 3526 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
2724 } else { 3527 } else {
2725 if (res.status == -1) { res.status = 404; } 3528 if (res.status == -1) { res.status = 404; }
@@ -2732,7 +3535,7 @@ inline bool Server::is_valid() const { return true; }
2732 3535
2733inline bool Server::process_and_close_socket(socket_t sock) { 3536inline bool Server::process_and_close_socket(socket_t sock) {
2734 return detail::process_and_close_socket( 3537 return detail::process_and_close_socket(
2735 false, sock, keep_alive_max_count_, 3538 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
2736 [this](Stream &strm, bool last_connection, bool &connection_close) { 3539 [this](Stream &strm, bool last_connection, bool &connection_close) {
2737 return process_request(strm, last_connection, connection_close, 3540 return process_request(strm, last_connection, connection_close,
2738 nullptr); 3541 nullptr);
@@ -2740,47 +3543,37 @@ inline bool Server::process_and_close_socket(socket_t sock) {
2740} 3543}
2741 3544
2742// HTTP client implementation 3545// HTTP client implementation
2743inline Client::Client(const char *host, int port, time_t timeout_sec) 3546inline Client::Client(const std::string &host, int port,
2744 : host_(host), port_(port), timeout_sec_(timeout_sec), 3547 const std::string &client_cert_path,
3548 const std::string &client_key_path)
3549 : host_(host), port_(port),
2745 host_and_port_(host_ + ":" + std::to_string(port_)), 3550 host_and_port_(host_ + ":" + std::to_string(port_)),
2746 keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), 3551 client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
2747 follow_location_(false) {}
2748 3552
2749inline Client::~Client() {} 3553inline Client::~Client() {}
2750 3554
2751inline bool Client::is_valid() const { return true; } 3555inline bool Client::is_valid() const { return true; }
2752 3556
2753inline socket_t Client::create_client_socket() const { 3557inline socket_t Client::create_client_socket() const {
2754 return detail::create_socket( 3558 if (!proxy_host_.empty()) {
2755 host_.c_str(), port_, [=](socket_t sock, struct addrinfo &ai) -> bool { 3559 return detail::create_client_socket(proxy_host_.c_str(), proxy_port_,
2756 detail::set_nonblocking(sock, true); 3560 timeout_sec_, interface_);
2757 3561 }
2758 auto ret = connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen)); 3562 return detail::create_client_socket(host_.c_str(), port_, timeout_sec_,
2759 if (ret < 0) { 3563 interface_);
2760 if (detail::is_connection_error() ||
2761 !detail::wait_until_socket_is_ready(sock, timeout_sec_, 0)) {
2762 detail::close_socket(sock);
2763 return false;
2764 }
2765 }
2766
2767 detail::set_nonblocking(sock, false);
2768 return true;
2769 });
2770} 3564}
2771 3565
2772inline bool Client::read_response_line(Stream &strm, Response &res) { 3566inline bool Client::read_response_line(Stream &strm, Response &res) {
2773 const auto bufsiz = 2048; 3567 std::array<char, 2048> buf;
2774 char buf[bufsiz];
2775 3568
2776 detail::stream_line_reader reader(strm, buf, bufsiz); 3569 detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
2777 3570
2778 if (!reader.getline()) { return false; } 3571 if (!line_reader.getline()) { return false; }
2779 3572
2780 const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n"); 3573 const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n");
2781 3574
2782 std::cmatch m; 3575 std::cmatch m;
2783 if (std::regex_match(reader.ptr(), m, re)) { 3576 if (std::regex_match(line_reader.ptr(), m, re)) {
2784 res.version = std::string(m[1]); 3577 res.version = std::string(m[1]);
2785 res.status = std::stoi(std::string(m[2])); 3578 res.status = std::stoi(std::string(m[2]));
2786 } 3579 }
@@ -2789,22 +3582,21 @@ inline bool Client::read_response_line(Stream &strm, Response &res) {
2789} 3582}
2790 3583
2791inline bool Client::send(const Request &req, Response &res) { 3584inline bool Client::send(const Request &req, Response &res) {
2792 if (req.path.empty()) { return false; }
2793
2794 auto sock = create_client_socket(); 3585 auto sock = create_client_socket();
2795 if (sock == INVALID_SOCKET) { return false; } 3586 if (sock == INVALID_SOCKET) { return false; }
2796 3587
2797 auto ret = process_and_close_socket( 3588#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2798 sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) { 3589 if (is_ssl() && !proxy_host_.empty()) {
2799 return process_request(strm, req, res, last_connection, 3590 bool error;
2800 connection_close); 3591 if (!connect(sock, res, error)) { return error; }
2801 });
2802
2803 if (ret && follow_location_ && (300 < res.status && res.status < 400)) {
2804 ret = redirect(req, res);
2805 } 3592 }
3593#endif
2806 3594
2807 return ret; 3595 return process_and_close_socket(
3596 sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) {
3597 return handle_request(strm, req, res, last_connection,
3598 connection_close);
3599 });
2808} 3600}
2809 3601
2810inline bool Client::send(const std::vector<Request> &requests, 3602inline bool Client::send(const std::vector<Request> &requests,
@@ -2814,32 +3606,136 @@ inline bool Client::send(const std::vector<Request> &requests,
2814 auto sock = create_client_socket(); 3606 auto sock = create_client_socket();
2815 if (sock == INVALID_SOCKET) { return false; } 3607 if (sock == INVALID_SOCKET) { return false; }
2816 3608
2817 if (!process_and_close_socket( 3609#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2818 sock, requests.size() - i, 3610 if (is_ssl() && !proxy_host_.empty()) {
2819 [&](Stream &strm, bool last_connection, bool &connection_close) -> bool { 3611 Response res;
2820 auto &req = requests[i]; 3612 bool error;
2821 auto res = Response(); 3613 if (!connect(sock, res, error)) { return false; }
2822 i++; 3614 }
3615#endif
3616
3617 if (!process_and_close_socket(sock, requests.size() - i,
3618 [&](Stream &strm, bool last_connection,
3619 bool &connection_close) -> bool {
3620 auto &req = requests[i++];
3621 auto res = Response();
3622 auto ret = handle_request(strm, req, res,
3623 last_connection,
3624 connection_close);
3625 if (ret) {
3626 responses.emplace_back(std::move(res));
3627 }
3628 return ret;
3629 })) {
3630 return false;
3631 }
3632 }
2823 3633
2824 if (req.path.empty()) { return false; } 3634 return true;
2825 auto ret = process_request(strm, req, res, last_connection, 3635}
2826 connection_close); 3636
3637inline bool Client::handle_request(Stream &strm, const Request &req,
3638 Response &res, bool last_connection,
3639 bool &connection_close) {
3640 if (req.path.empty()) { return false; }
3641
3642 bool ret;
3643
3644 if (!is_ssl() && !proxy_host_.empty()) {
3645 auto req2 = req;
3646 req2.path = "http://" + host_and_port_ + req.path;
3647 ret = process_request(strm, req2, res, last_connection, connection_close);
3648 } else {
3649 ret = process_request(strm, req, res, last_connection, connection_close);
3650 }
3651
3652 if (!ret) { return false; }
3653
3654 if (300 < res.status && res.status < 400 && follow_location_) {
3655 ret = redirect(req, res);
3656 }
2827 3657
2828 if (ret && follow_location_ && 3658#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2829 (300 < res.status && res.status < 400)) { 3659 if (res.status == 401 || res.status == 407) {
2830 ret = redirect(req, res); 3660 auto is_proxy = res.status == 407;
2831 } 3661 const auto &username =
3662 is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
3663 const auto &password =
3664 is_proxy ? proxy_digest_auth_password_ : digest_auth_password_;
3665
3666 if (!username.empty() && !password.empty()) {
3667 std::map<std::string, std::string> auth;
3668 if (parse_www_authenticate(res, auth, is_proxy)) {
3669 Request new_req = req;
3670 auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization";
3671 new_req.headers.erase(key);
3672 new_req.headers.insert(make_digest_authentication_header(
3673 req, auth, 1, random_string(10), username, password, is_proxy));
3674
3675 Response new_res;
3676
3677 ret = send(new_req, new_res);
3678 if (ret) { res = new_res; }
3679 }
3680 }
3681 }
3682#endif
3683
3684 return ret;
3685}
2832 3686
2833 if (ret) { responses.emplace_back(std::move(res)); } 3687#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3688inline bool Client::connect(socket_t sock, Response &res, bool &error) {
3689 error = true;
3690 Response res2;
3691
3692 if (!detail::process_socket(
3693 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3694 [&](Stream &strm, bool /*last_connection*/, bool &connection_close) {
3695 Request req2;
3696 req2.method = "CONNECT";
3697 req2.path = host_and_port_;
3698 return process_request(strm, req2, res2, false, connection_close);
3699 })) {
3700 detail::close_socket(sock);
3701 error = false;
3702 return false;
3703 }
2834 3704
2835 return ret; 3705 if (res2.status == 407) {
2836 })) { 3706 if (!proxy_digest_auth_username_.empty() &&
3707 !proxy_digest_auth_password_.empty()) {
3708 std::map<std::string, std::string> auth;
3709 if (parse_www_authenticate(res2, auth, true)) {
3710 Response res3;
3711 if (!detail::process_socket(
3712 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3713 [&](Stream &strm, bool /*last_connection*/,
3714 bool &connection_close) {
3715 Request req3;
3716 req3.method = "CONNECT";
3717 req3.path = host_and_port_;
3718 req3.headers.insert(make_digest_authentication_header(
3719 req3, auth, 1, random_string(10),
3720 proxy_digest_auth_username_, proxy_digest_auth_password_,
3721 true));
3722 return process_request(strm, req3, res3, false,
3723 connection_close);
3724 })) {
3725 detail::close_socket(sock);
3726 error = false;
3727 return false;
3728 }
3729 }
3730 } else {
3731 res = res2;
2837 return false; 3732 return false;
2838 } 3733 }
2839 } 3734 }
2840 3735
2841 return true; 3736 return true;
2842} 3737}
3738#endif
2843 3739
2844inline bool Client::redirect(const Request &req, Response &res) { 3740inline bool Client::redirect(const Request &req, Response &res) {
2845 if (req.redirect_count == 0) { return false; } 3741 if (req.redirect_count == 0) { return false; }
@@ -2847,46 +3743,47 @@ inline bool Client::redirect(const Request &req, Response &res) {
2847 auto location = res.get_header_value("location"); 3743 auto location = res.get_header_value("location");
2848 if (location.empty()) { return false; } 3744 if (location.empty()) { return false; }
2849 3745
2850 std::regex re( 3746 const static std::regex re(
2851 R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); 3747 R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)");
2852 3748
3749 std::smatch m;
3750 if (!regex_match(location, m, re)) { return false; }
3751
2853 auto scheme = is_ssl() ? "https" : "http"; 3752 auto scheme = is_ssl() ? "https" : "http";
2854 3753
2855 std::smatch m; 3754 auto next_scheme = m[1].str();
2856 if (regex_match(location, m, re)) { 3755 auto next_host = m[2].str();
2857 auto next_scheme = m[1].str(); 3756 auto next_path = m[3].str();
2858 auto next_host = m[2].str(); 3757 if (next_scheme.empty()) { next_scheme = scheme; }
2859 auto next_path = m[3].str(); 3758 if (next_scheme.empty()) { next_scheme = scheme; }
2860 if (next_host.empty()) { next_host = host_; } 3759 if (next_host.empty()) { next_host = host_; }
2861 if (next_path.empty()) { next_path = "/"; } 3760 if (next_path.empty()) { next_path = "/"; }
2862 3761
2863 if (next_scheme == scheme && next_host == host_) { 3762 if (next_scheme == scheme && next_host == host_) {
2864 return detail::redirect(*this, req, res, next_path); 3763 return detail::redirect(*this, req, res, next_path);
2865 } else { 3764 } else {
2866 if (next_scheme == "https") { 3765 if (next_scheme == "https") {
2867#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 3766#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2868 SSLClient cli(next_host.c_str()); 3767 SSLClient cli(next_host.c_str());
2869 cli.follow_location(true); 3768 cli.copy_settings(*this);
2870 return detail::redirect(cli, req, res, next_path); 3769 return detail::redirect(cli, req, res, next_path);
2871#else 3770#else
2872 return false; 3771 return false;
2873#endif 3772#endif
2874 } else { 3773 } else {
2875 Client cli(next_host.c_str()); 3774 Client cli(next_host.c_str());
2876 cli.follow_location(true); 3775 cli.copy_settings(*this);
2877 return detail::redirect(cli, req, res, next_path); 3776 return detail::redirect(cli, req, res, next_path);
2878 }
2879 } 3777 }
2880 } 3778 }
2881 return false;
2882} 3779}
2883 3780
2884inline void Client::write_request(Stream &strm, const Request &req, 3781inline bool Client::write_request(Stream &strm, const Request &req,
2885 bool last_connection) { 3782 bool last_connection) {
2886 BufferStream bstrm; 3783 detail::BufferStream bstrm;
2887 3784
2888 // Request line 3785 // Request line
2889 auto path = detail::encode_url(req.path); 3786 const auto &path = detail::encode_url(req.path);
2890 3787
2891 bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str()); 3788 bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());
2892 3789
@@ -2913,11 +3810,14 @@ inline void Client::write_request(Stream &strm, const Request &req,
2913 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); } 3810 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); }
2914 3811
2915 if (!req.has_header("User-Agent")) { 3812 if (!req.has_header("User-Agent")) {
2916 headers.emplace("User-Agent", "cpp-httplib/0.2"); 3813 headers.emplace("User-Agent", "cpp-httplib/0.5");
2917 } 3814 }
2918 3815
2919 if (req.body.empty()) { 3816 if (req.body.empty()) {
2920 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH") { 3817 if (req.content_provider) {
3818 auto length = std::to_string(req.content_length);
3819 headers.emplace("Content-Length", length);
3820 } else {
2921 headers.emplace("Content-Length", "0"); 3821 headers.emplace("Content-Length", "0");
2922 } 3822 }
2923 } else { 3823 } else {
@@ -2931,21 +3831,100 @@ inline void Client::write_request(Stream &strm, const Request &req,
2931 } 3831 }
2932 } 3832 }
2933 3833
2934 detail::write_headers(bstrm, req, headers); 3834 if (!basic_auth_username_.empty() && !basic_auth_password_.empty()) {
3835 headers.insert(make_basic_authentication_header(
3836 basic_auth_username_, basic_auth_password_, false));
3837 }
2935 3838
2936 // Body 3839 if (!proxy_basic_auth_username_.empty() &&
2937 if (!req.body.empty()) { bstrm.write(req.body); } 3840 !proxy_basic_auth_password_.empty()) {
3841 headers.insert(make_basic_authentication_header(
3842 proxy_basic_auth_username_, proxy_basic_auth_password_, true));
3843 }
3844
3845 detail::write_headers(bstrm, req, headers);
2938 3846
2939 // Flush buffer 3847 // Flush buffer
2940 auto &data = bstrm.get_buffer(); 3848 auto &data = bstrm.get_buffer();
2941 strm.write(data.data(), data.size()); 3849 strm.write(data.data(), data.size());
3850
3851 // Body
3852 if (req.body.empty()) {
3853 if (req.content_provider) {
3854 size_t offset = 0;
3855 size_t end_offset = req.content_length;
3856
3857 DataSink data_sink;
3858 data_sink.write = [&](const char *d, size_t l) {
3859 auto written_length = strm.write(d, l);
3860 offset += written_length;
3861 };
3862 data_sink.is_writable = [&](void) { return strm.is_writable(); };
3863
3864 while (offset < end_offset) {
3865 req.content_provider(offset, end_offset - offset, data_sink);
3866 }
3867 }
3868 } else {
3869 strm.write(req.body);
3870 }
3871
3872 return true;
3873}
3874
3875inline std::shared_ptr<Response> Client::send_with_content_provider(
3876 const char *method, const char *path, const Headers &headers,
3877 const std::string &body, size_t content_length,
3878 ContentProvider content_provider, const char *content_type) {
3879 Request req;
3880 req.method = method;
3881 req.headers = headers;
3882 req.path = path;
3883
3884 req.headers.emplace("Content-Type", content_type);
3885
3886#ifdef CPPHTTPLIB_ZLIB_SUPPORT
3887 if (compress_) {
3888 if (content_provider) {
3889 size_t offset = 0;
3890
3891 DataSink data_sink;
3892 data_sink.write = [&](const char *data, size_t data_len) {
3893 req.body.append(data, data_len);
3894 offset += data_len;
3895 };
3896 data_sink.is_writable = [&](void) { return true; };
3897
3898 while (offset < content_length) {
3899 content_provider(offset, content_length - offset, data_sink);
3900 }
3901 } else {
3902 req.body = body;
3903 }
3904
3905 if (!detail::compress(req.body)) { return nullptr; }
3906 req.headers.emplace("Content-Encoding", "gzip");
3907 } else
3908#endif
3909 {
3910 if (content_provider) {
3911 req.content_length = content_length;
3912 req.content_provider = content_provider;
3913 } else {
3914 req.body = body;
3915 }
3916 }
3917
3918 auto res = std::make_shared<Response>();
3919
3920 return send(req, *res) ? res : nullptr;
2942} 3921}
2943 3922
2944inline bool Client::process_request(Stream &strm, const Request &req, 3923inline bool Client::process_request(Stream &strm, const Request &req,
2945 Response &res, bool last_connection, 3924 Response &res, bool last_connection,
2946 bool &connection_close) { 3925 bool &connection_close) {
2947 // Send request 3926 // Send request
2948 write_request(strm, req, last_connection); 3927 if (!write_request(strm, req, last_connection)) { return false; }
2949 3928
2950 // Receive response and headers 3929 // Receive response and headers
2951 if (!read_response_line(strm, res) || 3930 if (!read_response_line(strm, res) ||
@@ -2963,21 +3942,16 @@ inline bool Client::process_request(Stream &strm, const Request &req,
2963 } 3942 }
2964 3943
2965 // Body 3944 // Body
2966 if (req.method != "HEAD") { 3945 if (req.method != "HEAD" && req.method != "CONNECT") {
2967 detail::ContentReceiverCore out = [&](const char *buf, size_t n) { 3946 ContentReceiver out = [&](const char *buf, size_t n) {
2968 if (res.body.size() + n > res.body.max_size()) { return false; } 3947 if (res.body.size() + n > res.body.max_size()) { return false; }
2969 res.body.append(buf, n); 3948 res.body.append(buf, n);
2970 return true; 3949 return true;
2971 }; 3950 };
2972 3951
2973 if (req.content_receiver) { 3952 if (req.content_receiver) {
2974 auto offset = std::make_shared<size_t>(); 3953 out = [&](const char *buf, size_t n) {
2975 auto length = get_header_value_uint64(res.headers, "Content-Length", 0); 3954 return req.content_receiver(buf, n);
2976 auto receiver = req.content_receiver;
2977 out = [offset, length, receiver](const char *buf, size_t n) {
2978 auto ret = receiver(buf, n, *offset, length);
2979 (*offset) += n;
2980 return ret;
2981 }; 3955 };
2982 } 3956 }
2983 3957
@@ -2988,6 +3962,9 @@ inline bool Client::process_request(Stream &strm, const Request &req,
2988 } 3962 }
2989 } 3963 }
2990 3964
3965 // Log
3966 if (logger_) { logger_(req, res); }
3967
2991 return true; 3968 return true;
2992} 3969}
2993 3970
@@ -2997,25 +3974,25 @@ inline bool Client::process_and_close_socket(
2997 bool &connection_close)> 3974 bool &connection_close)>
2998 callback) { 3975 callback) {
2999 request_count = std::min(request_count, keep_alive_max_count_); 3976 request_count = std::min(request_count, keep_alive_max_count_);
3000 return detail::process_and_close_socket(true, sock, request_count, callback); 3977 return detail::process_and_close_socket(true, sock, request_count,
3978 read_timeout_sec_, read_timeout_usec_,
3979 callback);
3001} 3980}
3002 3981
3003inline bool Client::is_ssl() const { return false; } 3982inline bool Client::is_ssl() const { return false; }
3004 3983
3005inline std::shared_ptr<Response> Client::Get(const char *path) { 3984inline std::shared_ptr<Response> Client::Get(const char *path) {
3006 Progress dummy; 3985 return Get(path, Headers(), Progress());
3007 return Get(path, Headers(), dummy);
3008} 3986}
3009 3987
3010inline std::shared_ptr<Response> Client::Get(const char *path, 3988inline std::shared_ptr<Response> Client::Get(const char *path,
3011 Progress progress) { 3989 Progress progress) {
3012 return Get(path, Headers(), progress); 3990 return Get(path, Headers(), std::move(progress));
3013} 3991}
3014 3992
3015inline std::shared_ptr<Response> Client::Get(const char *path, 3993inline std::shared_ptr<Response> Client::Get(const char *path,
3016 const Headers &headers) { 3994 const Headers &headers) {
3017 Progress dummy; 3995 return Get(path, headers, Progress());
3018 return Get(path, headers, dummy);
3019} 3996}
3020 3997
3021inline std::shared_ptr<Response> 3998inline std::shared_ptr<Response>
@@ -3024,7 +4001,7 @@ Client::Get(const char *path, const Headers &headers, Progress progress) {
3024 req.method = "GET"; 4001 req.method = "GET";
3025 req.path = path; 4002 req.path = path;
3026 req.headers = headers; 4003 req.headers = headers;
3027 req.progress = progress; 4004 req.progress = std::move(progress);
3028 4005
3029 auto res = std::make_shared<Response>(); 4006 auto res = std::make_shared<Response>();
3030 return send(req, *res) ? res : nullptr; 4007 return send(req, *res) ? res : nullptr;
@@ -3032,36 +4009,36 @@ Client::Get(const char *path, const Headers &headers, Progress progress) {
3032 4009
3033inline std::shared_ptr<Response> Client::Get(const char *path, 4010inline std::shared_ptr<Response> Client::Get(const char *path,
3034 ContentReceiver content_receiver) { 4011 ContentReceiver content_receiver) {
3035 Progress dummy; 4012 return Get(path, Headers(), nullptr, std::move(content_receiver), Progress());
3036 return Get(path, Headers(), nullptr, content_receiver, dummy);
3037} 4013}
3038 4014
3039inline std::shared_ptr<Response> Client::Get(const char *path, 4015inline std::shared_ptr<Response> Client::Get(const char *path,
3040 ContentReceiver content_receiver, 4016 ContentReceiver content_receiver,
3041 Progress progress) { 4017 Progress progress) {
3042 return Get(path, Headers(), nullptr, content_receiver, progress); 4018 return Get(path, Headers(), nullptr, std::move(content_receiver),
4019 std::move(progress));
3043} 4020}
3044 4021
3045inline std::shared_ptr<Response> Client::Get(const char *path, 4022inline std::shared_ptr<Response> Client::Get(const char *path,
3046 const Headers &headers, 4023 const Headers &headers,
3047 ContentReceiver content_receiver) { 4024 ContentReceiver content_receiver) {
3048 Progress dummy; 4025 return Get(path, headers, nullptr, std::move(content_receiver), Progress());
3049 return Get(path, headers, nullptr, content_receiver, dummy);
3050} 4026}
3051 4027
3052inline std::shared_ptr<Response> Client::Get(const char *path, 4028inline std::shared_ptr<Response> Client::Get(const char *path,
3053 const Headers &headers, 4029 const Headers &headers,
3054 ContentReceiver content_receiver, 4030 ContentReceiver content_receiver,
3055 Progress progress) { 4031 Progress progress) {
3056 return Get(path, headers, nullptr, content_receiver, progress); 4032 return Get(path, headers, nullptr, std::move(content_receiver),
4033 std::move(progress));
3057} 4034}
3058 4035
3059inline std::shared_ptr<Response> Client::Get(const char *path, 4036inline std::shared_ptr<Response> Client::Get(const char *path,
3060 const Headers &headers, 4037 const Headers &headers,
3061 ResponseHandler response_handler, 4038 ResponseHandler response_handler,
3062 ContentReceiver content_receiver) { 4039 ContentReceiver content_receiver) {
3063 Progress dummy; 4040 return Get(path, headers, std::move(response_handler), content_receiver,
3064 return Get(path, headers, response_handler, content_receiver, dummy); 4041 Progress());
3065} 4042}
3066 4043
3067inline std::shared_ptr<Response> Client::Get(const char *path, 4044inline std::shared_ptr<Response> Client::Get(const char *path,
@@ -3073,9 +4050,9 @@ inline std::shared_ptr<Response> Client::Get(const char *path,
3073 req.method = "GET"; 4050 req.method = "GET";
3074 req.path = path; 4051 req.path = path;
3075 req.headers = headers; 4052 req.headers = headers;
3076 req.response_handler = response_handler; 4053 req.response_handler = std::move(response_handler);
3077 req.content_receiver = content_receiver; 4054 req.content_receiver = std::move(content_receiver);
3078 req.progress = progress; 4055 req.progress = std::move(progress);
3079 4056
3080 auto res = std::make_shared<Response>(); 4057 auto res = std::make_shared<Response>();
3081 return send(req, *res) ? res : nullptr; 4058 return send(req, *res) ? res : nullptr;
@@ -3107,17 +4084,8 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3107 const Headers &headers, 4084 const Headers &headers,
3108 const std::string &body, 4085 const std::string &body,
3109 const char *content_type) { 4086 const char *content_type) {
3110 Request req; 4087 return send_with_content_provider("POST", path, headers, body, 0, nullptr,
3111 req.method = "POST"; 4088 content_type);
3112 req.headers = headers;
3113 req.path = path;
3114
3115 req.headers.emplace("Content-Type", content_type);
3116 req.body = body;
3117
3118 auto res = std::make_shared<Response>();
3119
3120 return send(req, *res) ? res : nullptr;
3121} 4089}
3122 4090
3123inline std::shared_ptr<Response> Client::Post(const char *path, 4091inline std::shared_ptr<Response> Client::Post(const char *path,
@@ -3125,6 +4093,21 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3125 return Post(path, Headers(), params); 4093 return Post(path, Headers(), params);
3126} 4094}
3127 4095
4096inline std::shared_ptr<Response> Client::Post(const char *path,
4097 size_t content_length,
4098 ContentProvider content_provider,
4099 const char *content_type) {
4100 return Post(path, Headers(), content_length, content_provider, content_type);
4101}
4102
4103inline std::shared_ptr<Response>
4104Client::Post(const char *path, const Headers &headers, size_t content_length,
4105 ContentProvider content_provider, const char *content_type) {
4106 return send_with_content_provider("POST", path, headers, std::string(),
4107 content_length, content_provider,
4108 content_type);
4109}
4110
3128inline std::shared_ptr<Response> 4111inline std::shared_ptr<Response>
3129Client::Post(const char *path, const Headers &headers, const Params &params) { 4112Client::Post(const char *path, const Headers &headers, const Params &params) {
3130 std::string query; 4113 std::string query;
@@ -3146,35 +4129,28 @@ Client::Post(const char *path, const MultipartFormDataItems &items) {
3146inline std::shared_ptr<Response> 4129inline std::shared_ptr<Response>
3147Client::Post(const char *path, const Headers &headers, 4130Client::Post(const char *path, const Headers &headers,
3148 const MultipartFormDataItems &items) { 4131 const MultipartFormDataItems &items) {
3149 Request req;
3150 req.method = "POST";
3151 req.headers = headers;
3152 req.path = path;
3153
3154 auto boundary = detail::make_multipart_data_boundary(); 4132 auto boundary = detail::make_multipart_data_boundary();
3155 4133
3156 req.headers.emplace("Content-Type", 4134 std::string body;
3157 "multipart/form-data; boundary=" + boundary);
3158 4135
3159 for (const auto &item : items) { 4136 for (const auto &item : items) {
3160 req.body += "--" + boundary + "\r\n"; 4137 body += "--" + boundary + "\r\n";
3161 req.body += "Content-Disposition: form-data; name=\"" + item.name + "\""; 4138 body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
3162 if (!item.filename.empty()) { 4139 if (!item.filename.empty()) {
3163 req.body += "; filename=\"" + item.filename + "\""; 4140 body += "; filename=\"" + item.filename + "\"";
3164 } 4141 }
3165 req.body += "\r\n"; 4142 body += "\r\n";
3166 if (!item.content_type.empty()) { 4143 if (!item.content_type.empty()) {
3167 req.body += "Content-Type: " + item.content_type + "\r\n"; 4144 body += "Content-Type: " + item.content_type + "\r\n";
3168 } 4145 }
3169 req.body += "\r\n"; 4146 body += "\r\n";
3170 req.body += item.content + "\r\n"; 4147 body += item.content + "\r\n";
3171 } 4148 }
3172 4149
3173 req.body += "--" + boundary + "--\r\n"; 4150 body += "--" + boundary + "--\r\n";
3174 4151
3175 auto res = std::make_shared<Response>(); 4152 std::string content_type = "multipart/form-data; boundary=" + boundary;
3176 4153 return Post(path, headers, body, content_type.c_str());
3177 return send(req, *res) ? res : nullptr;
3178} 4154}
3179 4155
3180inline std::shared_ptr<Response> Client::Put(const char *path, 4156inline std::shared_ptr<Response> Client::Put(const char *path,
@@ -3187,17 +4163,41 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
3187 const Headers &headers, 4163 const Headers &headers,
3188 const std::string &body, 4164 const std::string &body,
3189 const char *content_type) { 4165 const char *content_type) {
3190 Request req; 4166 return send_with_content_provider("PUT", path, headers, body, 0, nullptr,
3191 req.method = "PUT"; 4167 content_type);
3192 req.headers = headers; 4168}
3193 req.path = path;
3194 4169
3195 req.headers.emplace("Content-Type", content_type); 4170inline std::shared_ptr<Response> Client::Put(const char *path,
3196 req.body = body; 4171 size_t content_length,
4172 ContentProvider content_provider,
4173 const char *content_type) {
4174 return Put(path, Headers(), content_length, content_provider, content_type);
4175}
3197 4176
3198 auto res = std::make_shared<Response>(); 4177inline std::shared_ptr<Response>
4178Client::Put(const char *path, const Headers &headers, size_t content_length,
4179 ContentProvider content_provider, const char *content_type) {
4180 return send_with_content_provider("PUT", path, headers, std::string(),
4181 content_length, content_provider,
4182 content_type);
4183}
3199 4184
3200 return send(req, *res) ? res : nullptr; 4185inline std::shared_ptr<Response> Client::Put(const char *path,
4186 const Params &params) {
4187 return Put(path, Headers(), params);
4188}
4189
4190inline std::shared_ptr<Response>
4191Client::Put(const char *path, const Headers &headers, const Params &params) {
4192 std::string query;
4193 for (auto it = params.begin(); it != params.end(); ++it) {
4194 if (it != params.begin()) { query += "&"; }
4195 query += it->first;
4196 query += "=";
4197 query += detail::encode_url(it->second);
4198 }
4199
4200 return Put(path, headers, query, "application/x-www-form-urlencoded");
3201} 4201}
3202 4202
3203inline std::shared_ptr<Response> Client::Patch(const char *path, 4203inline std::shared_ptr<Response> Client::Patch(const char *path,
@@ -3210,17 +4210,23 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
3210 const Headers &headers, 4210 const Headers &headers,
3211 const std::string &body, 4211 const std::string &body,
3212 const char *content_type) { 4212 const char *content_type) {
3213 Request req; 4213 return send_with_content_provider("PATCH", path, headers, body, 0, nullptr,
3214 req.method = "PATCH"; 4214 content_type);
3215 req.headers = headers; 4215}
3216 req.path = path;
3217
3218 req.headers.emplace("Content-Type", content_type);
3219 req.body = body;
3220 4216
3221 auto res = std::make_shared<Response>(); 4217inline std::shared_ptr<Response> Client::Patch(const char *path,
4218 size_t content_length,
4219 ContentProvider content_provider,
4220 const char *content_type) {
4221 return Patch(path, Headers(), content_length, content_provider, content_type);
4222}
3222 4223
3223 return send(req, *res) ? res : nullptr; 4224inline std::shared_ptr<Response>
4225Client::Patch(const char *path, const Headers &headers, size_t content_length,
4226 ContentProvider content_provider, const char *content_type) {
4227 return send_with_content_provider("PATCH", path, headers, std::string(),
4228 content_length, content_provider,
4229 content_type);
3224} 4230}
3225 4231
3226inline std::shared_ptr<Response> Client::Delete(const char *path) { 4232inline std::shared_ptr<Response> Client::Delete(const char *path) {
@@ -3271,11 +4277,58 @@ inline std::shared_ptr<Response> Client::Options(const char *path,
3271 return send(req, *res) ? res : nullptr; 4277 return send(req, *res) ? res : nullptr;
3272} 4278}
3273 4279
4280inline void Client::set_timeout_sec(time_t timeout_sec) {
4281 timeout_sec_ = timeout_sec;
4282}
4283
4284inline void Client::set_read_timeout(time_t sec, time_t usec) {
4285 read_timeout_sec_ = sec;
4286 read_timeout_usec_ = usec;
4287}
4288
3274inline void Client::set_keep_alive_max_count(size_t count) { 4289inline void Client::set_keep_alive_max_count(size_t count) {
3275 keep_alive_max_count_ = count; 4290 keep_alive_max_count_ = count;
3276} 4291}
3277 4292
3278inline void Client::follow_location(bool on) { follow_location_ = on; } 4293inline void Client::set_basic_auth(const char *username, const char *password) {
4294 basic_auth_username_ = username;
4295 basic_auth_password_ = password;
4296}
4297
4298#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4299inline void Client::set_digest_auth(const char *username,
4300 const char *password) {
4301 digest_auth_username_ = username;
4302 digest_auth_password_ = password;
4303}
4304#endif
4305
4306inline void Client::set_follow_location(bool on) { follow_location_ = on; }
4307
4308inline void Client::set_compress(bool on) { compress_ = on; }
4309
4310inline void Client::set_interface(const char *intf) { interface_ = intf; }
4311
4312inline void Client::set_proxy(const char *host, int port) {
4313 proxy_host_ = host;
4314 proxy_port_ = port;
4315}
4316
4317inline void Client::set_proxy_basic_auth(const char *username,
4318 const char *password) {
4319 proxy_basic_auth_username_ = username;
4320 proxy_basic_auth_password_ = password;
4321}
4322
4323#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4324inline void Client::set_proxy_digest_auth(const char *username,
4325 const char *password) {
4326 proxy_digest_auth_username_ = username;
4327 proxy_digest_auth_password_ = password;
4328}
4329#endif
4330
4331inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); }
3279 4332
3280/* 4333/*
3281 * SSL Implementation 4334 * SSL Implementation
@@ -3284,11 +4337,10 @@ inline void Client::follow_location(bool on) { follow_location_ = on; }
3284namespace detail { 4337namespace detail {
3285 4338
3286template <typename U, typename V, typename T> 4339template <typename U, typename V, typename T>
3287inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock, 4340inline bool process_and_close_socket_ssl(
3288 size_t keep_alive_max_count, 4341 bool is_client_request, socket_t sock, size_t keep_alive_max_count,
3289 SSL_CTX *ctx, std::mutex &ctx_mutex, 4342 time_t read_timeout_sec, time_t read_timeout_usec, SSL_CTX *ctx,
3290 U SSL_connect_or_accept, V setup, 4343 std::mutex &ctx_mutex, U SSL_connect_or_accept, V setup, T callback) {
3291 T callback) {
3292 assert(keep_alive_max_count > 0); 4344 assert(keep_alive_max_count > 0);
3293 4345
3294 SSL *ssl = nullptr; 4346 SSL *ssl = nullptr;
@@ -3316,7 +4368,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3316 return false; 4368 return false;
3317 } 4369 }
3318 4370
3319 bool ret = false; 4371 auto ret = false;
3320 4372
3321 if (SSL_connect_or_accept(ssl) == 1) { 4373 if (SSL_connect_or_accept(ssl) == 1) {
3322 if (keep_alive_max_count > 1) { 4374 if (keep_alive_max_count > 1) {
@@ -3325,7 +4377,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3325 (is_client_request || 4377 (is_client_request ||
3326 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 4378 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND,
3327 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 4379 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) {
3328 SSLSocketStream strm(sock, ssl); 4380 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
3329 auto last_connection = count == 1; 4381 auto last_connection = count == 1;
3330 auto connection_close = false; 4382 auto connection_close = false;
3331 4383
@@ -3335,7 +4387,7 @@ inline bool process_and_close_socket_ssl(bool is_client_request, socket_t sock,
3335 count--; 4387 count--;
3336 } 4388 }
3337 } else { 4389 } else {
3338 SSLSocketStream strm(sock, ssl); 4390 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
3339 auto dummy_connection_close = false; 4391 auto dummy_connection_close = false;
3340 ret = callback(ssl, strm, true, dummy_connection_close); 4392 ret = callback(ssl, strm, true, dummy_connection_close);
3341 } 4393 }
@@ -3382,11 +4434,20 @@ private:
3382class SSLInit { 4434class SSLInit {
3383public: 4435public:
3384 SSLInit() { 4436 SSLInit() {
4437#if OPENSSL_VERSION_NUMBER < 0x1010001fL
3385 SSL_load_error_strings(); 4438 SSL_load_error_strings();
3386 SSL_library_init(); 4439 SSL_library_init();
4440#else
4441 OPENSSL_init_ssl(
4442 OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
4443#endif
3387 } 4444 }
3388 4445
3389 ~SSLInit() { ERR_free_strings(); } 4446 ~SSLInit() {
4447#if OPENSSL_VERSION_NUMBER < 0x1010001fL
4448 ERR_free_strings();
4449#endif
4450 }
3390 4451
3391private: 4452private:
3392#if OPENSSL_VERSION_NUMBER < 0x10100000L 4453#if OPENSSL_VERSION_NUMBER < 0x10100000L
@@ -3394,41 +4455,44 @@ private:
3394#endif 4455#endif
3395}; 4456};
3396 4457
3397static SSLInit sslinit_;
3398
3399} // namespace detail
3400
3401// SSL socket stream implementation 4458// SSL socket stream implementation
3402inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl) 4459inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl,
3403 : sock_(sock), ssl_(ssl) {} 4460 time_t read_timeout_sec,
4461 time_t read_timeout_usec)
4462 : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec),
4463 read_timeout_usec_(read_timeout_usec) {}
3404 4464
3405inline SSLSocketStream::~SSLSocketStream() {} 4465inline SSLSocketStream::~SSLSocketStream() {}
3406 4466
4467inline bool SSLSocketStream::is_readable() const {
4468 return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
4469}
4470
4471inline bool SSLSocketStream::is_writable() const {
4472 return detail::select_write(sock_, 0, 0) > 0;
4473}
4474
3407inline int SSLSocketStream::read(char *ptr, size_t size) { 4475inline int SSLSocketStream::read(char *ptr, size_t size) {
3408 if (SSL_pending(ssl_) > 0 || 4476 if (SSL_pending(ssl_) > 0 ||
3409 detail::select_read(sock_, CPPHTTPLIB_READ_TIMEOUT_SECOND, 4477 select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0) {
3410 CPPHTTPLIB_READ_TIMEOUT_USECOND) > 0) {
3411 return SSL_read(ssl_, ptr, static_cast<int>(size)); 4478 return SSL_read(ssl_, ptr, static_cast<int>(size));
3412 } 4479 }
3413 return -1; 4480 return -1;
3414} 4481}
3415 4482
3416inline int SSLSocketStream::write(const char *ptr, size_t size) { 4483inline int SSLSocketStream::write(const char *ptr, size_t size) {
3417 return SSL_write(ssl_, ptr, static_cast<int>(size)); 4484 if (is_writable()) { return SSL_write(ssl_, ptr, static_cast<int>(size)); }
3418} 4485 return -1;
3419
3420inline int SSLSocketStream::write(const char *ptr) {
3421 return write(ptr, strlen(ptr));
3422}
3423
3424inline int SSLSocketStream::write(const std::string &s) {
3425 return write(s.data(), s.size());
3426} 4486}
3427 4487
3428inline std::string SSLSocketStream::get_remote_addr() const { 4488inline std::string SSLSocketStream::get_remote_addr() const {
3429 return detail::get_remote_addr(sock_); 4489 return detail::get_remote_addr(sock_);
3430} 4490}
3431 4491
4492static SSLInit sslinit_;
4493
4494} // namespace detail
4495
3432// SSL HTTP server implementation 4496// SSL HTTP server implementation
3433inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path, 4497inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path,
3434 const char *client_ca_cert_file_path, 4498 const char *client_ca_cert_file_path,
@@ -3476,8 +4540,8 @@ inline bool SSLServer::is_valid() const { return ctx_; }
3476 4540
3477inline bool SSLServer::process_and_close_socket(socket_t sock) { 4541inline bool SSLServer::process_and_close_socket(socket_t sock) {
3478 return detail::process_and_close_socket_ssl( 4542 return detail::process_and_close_socket_ssl(
3479 false, sock, keep_alive_max_count_, ctx_, ctx_mutex_, SSL_accept, 4543 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
3480 [](SSL * /*ssl*/) { return true; }, 4544 ctx_, ctx_mutex_, SSL_accept, [](SSL * /*ssl*/) { return true; },
3481 [this](SSL *ssl, Stream &strm, bool last_connection, 4545 [this](SSL *ssl, Stream &strm, bool last_connection,
3482 bool &connection_close) { 4546 bool &connection_close) {
3483 return process_request(strm, last_connection, connection_close, 4547 return process_request(strm, last_connection, connection_close,
@@ -3486,21 +4550,21 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
3486} 4550}
3487 4551
3488// SSL HTTP client implementation 4552// SSL HTTP client implementation
3489inline SSLClient::SSLClient(const char *host, int port, time_t timeout_sec, 4553inline SSLClient::SSLClient(const std::string &host, int port,
3490 const char *client_cert_path, 4554 const std::string &client_cert_path,
3491 const char *client_key_path) 4555 const std::string &client_key_path)
3492 : Client(host, port, timeout_sec) { 4556 : Client(host, port, client_cert_path, client_key_path) {
3493 ctx_ = SSL_CTX_new(SSLv23_client_method()); 4557 ctx_ = SSL_CTX_new(SSLv23_client_method());
3494 4558
3495 detail::split(&host_[0], &host_[host_.size()], '.', 4559 detail::split(&host_[0], &host_[host_.size()], '.',
3496 [&](const char *b, const char *e) { 4560 [&](const char *b, const char *e) {
3497 host_components_.emplace_back(std::string(b, e)); 4561 host_components_.emplace_back(std::string(b, e));
3498 }); 4562 });
3499 if (client_cert_path && client_key_path) { 4563 if (!client_cert_path.empty() && !client_key_path.empty()) {
3500 if (SSL_CTX_use_certificate_file(ctx_, client_cert_path, 4564 if (SSL_CTX_use_certificate_file(ctx_, client_cert_path.c_str(),
3501 SSL_FILETYPE_PEM) != 1 || 4565 SSL_FILETYPE_PEM) != 1 ||
3502 SSL_CTX_use_PrivateKey_file(ctx_, client_key_path, SSL_FILETYPE_PEM) != 4566 SSL_CTX_use_PrivateKey_file(ctx_, client_key_path.c_str(),
3503 1) { 4567 SSL_FILETYPE_PEM) != 1) {
3504 SSL_CTX_free(ctx_); 4568 SSL_CTX_free(ctx_);
3505 ctx_ = nullptr; 4569 ctx_ = nullptr;
3506 } 4570 }
@@ -3527,9 +4591,7 @@ inline long SSLClient::get_openssl_verify_result() const {
3527 return verify_result_; 4591 return verify_result_;
3528} 4592}
3529 4593
3530inline SSL_CTX* SSLClient::ssl_context() const noexcept { 4594inline SSL_CTX *SSLClient::ssl_context() const noexcept { return ctx_; }
3531 return ctx_;
3532}
3533 4595
3534inline bool SSLClient::process_and_close_socket( 4596inline bool SSLClient::process_and_close_socket(
3535 socket_t sock, size_t request_count, 4597 socket_t sock, size_t request_count,
@@ -3541,7 +4603,8 @@ inline bool SSLClient::process_and_close_socket(
3541 4603
3542 return is_valid() && 4604 return is_valid() &&
3543 detail::process_and_close_socket_ssl( 4605 detail::process_and_close_socket_ssl(
3544 true, sock, request_count, ctx_, ctx_mutex_, 4606 true, sock, request_count, read_timeout_sec_, read_timeout_usec_,
4607 ctx_, ctx_mutex_,
3545 [&](SSL *ssl) { 4608 [&](SSL *ssl) {
3546 if (ca_cert_file_path_.empty()) { 4609 if (ca_cert_file_path_.empty()) {
3547 SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr); 4610 SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr);
@@ -3712,6 +4775,8 @@ inline bool SSLClient::check_host_name(const char *pattern,
3712} 4775}
3713#endif 4776#endif
3714 4777
4778// ----------------------------------------------------------------------------
4779
3715} // namespace httplib 4780} // namespace httplib
3716 4781
3717#endif // CPPHTTPLIB_HTTPLIB_H 4782#endif // CPPHTTPLIB_HTTPLIB_H
diff --git a/src/audio_core/algorithm/interpolate.cpp b/src/audio_core/algorithm/interpolate.cpp
index 5005ba519..a58f24169 100644
--- a/src/audio_core/algorithm/interpolate.cpp
+++ b/src/audio_core/algorithm/interpolate.cpp
@@ -5,6 +5,7 @@
5#define _USE_MATH_DEFINES 5#define _USE_MATH_DEFINES
6 6
7#include <algorithm> 7#include <algorithm>
8#include <climits>
8#include <cmath> 9#include <cmath>
9#include <vector> 10#include <vector>
10#include "audio_core/algorithm/interpolate.h" 11#include "audio_core/algorithm/interpolate.h"
@@ -13,13 +14,131 @@
13 14
14namespace AudioCore { 15namespace AudioCore {
15 16
16/// The Lanczos kernel 17constexpr std::array<s16, 512> curve_lut0 = {
17static double Lanczos(std::size_t a, double x) { 18 6600, 19426, 6722, 3, 6479, 19424, 6845, 9, 6359, 19419, 6968, 15, 6239,
18 if (x == 0.0) 19 19412, 7093, 22, 6121, 19403, 7219, 28, 6004, 19391, 7345, 34, 5888, 19377,
19 return 1.0; 20 7472, 41, 5773, 19361, 7600, 48, 5659, 19342, 7728, 55, 5546, 19321, 7857,
20 const double px = M_PI * x; 21 62, 5434, 19298, 7987, 69, 5323, 19273, 8118, 77, 5213, 19245, 8249, 84,
21 return a * std::sin(px) * std::sin(px / a) / (px * px); 22 5104, 19215, 8381, 92, 4997, 19183, 8513, 101, 4890, 19148, 8646, 109, 4785,
22} 23 19112, 8780, 118, 4681, 19073, 8914, 127, 4579, 19031, 9048, 137, 4477, 18988,
24 9183, 147, 4377, 18942, 9318, 157, 4277, 18895, 9454, 168, 4179, 18845, 9590,
25 179, 4083, 18793, 9726, 190, 3987, 18738, 9863, 202, 3893, 18682, 10000, 215,
26 3800, 18624, 10137, 228, 3709, 18563, 10274, 241, 3618, 18500, 10411, 255, 3529,
27 18436, 10549, 270, 3441, 18369, 10687, 285, 3355, 18300, 10824, 300, 3269, 18230,
28 10962, 317, 3186, 18157, 11100, 334, 3103, 18082, 11238, 351, 3022, 18006, 11375,
29 369, 2942, 17927, 11513, 388, 2863, 17847, 11650, 408, 2785, 17765, 11788, 428,
30 2709, 17681, 11925, 449, 2635, 17595, 12062, 471, 2561, 17507, 12198, 494, 2489,
31 17418, 12334, 517, 2418, 17327, 12470, 541, 2348, 17234, 12606, 566, 2280, 17140,
32 12741, 592, 2213, 17044, 12876, 619, 2147, 16946, 13010, 647, 2083, 16846, 13144,
33 675, 2020, 16745, 13277, 704, 1958, 16643, 13409, 735, 1897, 16539, 13541, 766,
34 1838, 16434, 13673, 798, 1780, 16327, 13803, 832, 1723, 16218, 13933, 866, 1667,
35 16109, 14062, 901, 1613, 15998, 14191, 937, 1560, 15885, 14318, 975, 1508, 15772,
36 14445, 1013, 1457, 15657, 14571, 1052, 1407, 15540, 14695, 1093, 1359, 15423, 14819,
37 1134, 1312, 15304, 14942, 1177, 1266, 15185, 15064, 1221, 1221, 15064, 15185, 1266,
38 1177, 14942, 15304, 1312, 1134, 14819, 15423, 1359, 1093, 14695, 15540, 1407, 1052,
39 14571, 15657, 1457, 1013, 14445, 15772, 1508, 975, 14318, 15885, 1560, 937, 14191,
40 15998, 1613, 901, 14062, 16109, 1667, 866, 13933, 16218, 1723, 832, 13803, 16327,
41 1780, 798, 13673, 16434, 1838, 766, 13541, 16539, 1897, 735, 13409, 16643, 1958,
42 704, 13277, 16745, 2020, 675, 13144, 16846, 2083, 647, 13010, 16946, 2147, 619,
43 12876, 17044, 2213, 592, 12741, 17140, 2280, 566, 12606, 17234, 2348, 541, 12470,
44 17327, 2418, 517, 12334, 17418, 2489, 494, 12198, 17507, 2561, 471, 12062, 17595,
45 2635, 449, 11925, 17681, 2709, 428, 11788, 17765, 2785, 408, 11650, 17847, 2863,
46 388, 11513, 17927, 2942, 369, 11375, 18006, 3022, 351, 11238, 18082, 3103, 334,
47 11100, 18157, 3186, 317, 10962, 18230, 3269, 300, 10824, 18300, 3355, 285, 10687,
48 18369, 3441, 270, 10549, 18436, 3529, 255, 10411, 18500, 3618, 241, 10274, 18563,
49 3709, 228, 10137, 18624, 3800, 215, 10000, 18682, 3893, 202, 9863, 18738, 3987,
50 190, 9726, 18793, 4083, 179, 9590, 18845, 4179, 168, 9454, 18895, 4277, 157,
51 9318, 18942, 4377, 147, 9183, 18988, 4477, 137, 9048, 19031, 4579, 127, 8914,
52 19073, 4681, 118, 8780, 19112, 4785, 109, 8646, 19148, 4890, 101, 8513, 19183,
53 4997, 92, 8381, 19215, 5104, 84, 8249, 19245, 5213, 77, 8118, 19273, 5323,
54 69, 7987, 19298, 5434, 62, 7857, 19321, 5546, 55, 7728, 19342, 5659, 48,
55 7600, 19361, 5773, 41, 7472, 19377, 5888, 34, 7345, 19391, 6004, 28, 7219,
56 19403, 6121, 22, 7093, 19412, 6239, 15, 6968, 19419, 6359, 9, 6845, 19424,
57 6479, 3, 6722, 19426, 6600};
58
59constexpr std::array<s16, 512> curve_lut1 = {
60 -68, 32639, 69, -5, -200, 32630, 212, -15, -328, 32613, 359, -26, -450,
61 32586, 512, -36, -568, 32551, 669, -47, -680, 32507, 832, -58, -788, 32454,
62 1000, -69, -891, 32393, 1174, -80, -990, 32323, 1352, -92, -1084, 32244, 1536,
63 -103, -1173, 32157, 1724, -115, -1258, 32061, 1919, -128, -1338, 31956, 2118, -140,
64 -1414, 31844, 2322, -153, -1486, 31723, 2532, -167, -1554, 31593, 2747, -180, -1617,
65 31456, 2967, -194, -1676, 31310, 3192, -209, -1732, 31157, 3422, -224, -1783, 30995,
66 3657, -240, -1830, 30826, 3897, -256, -1874, 30649, 4143, -272, -1914, 30464, 4393,
67 -289, -1951, 30272, 4648, -307, -1984, 30072, 4908, -325, -2014, 29866, 5172, -343,
68 -2040, 29652, 5442, -362, -2063, 29431, 5716, -382, -2083, 29203, 5994, -403, -2100,
69 28968, 6277, -424, -2114, 28727, 6565, -445, -2125, 28480, 6857, -468, -2133, 28226,
70 7153, -490, -2139, 27966, 7453, -514, -2142, 27700, 7758, -538, -2142, 27428, 8066,
71 -563, -2141, 27151, 8378, -588, -2136, 26867, 8694, -614, -2130, 26579, 9013, -641,
72 -2121, 26285, 9336, -668, -2111, 25987, 9663, -696, -2098, 25683, 9993, -724, -2084,
73 25375, 10326, -753, -2067, 25063, 10662, -783, -2049, 24746, 11000, -813, -2030, 24425,
74 11342, -844, -2009, 24100, 11686, -875, -1986, 23771, 12033, -907, -1962, 23438, 12382,
75 -939, -1937, 23103, 12733, -972, -1911, 22764, 13086, -1005, -1883, 22422, 13441, -1039,
76 -1855, 22077, 13798, -1072, -1825, 21729, 14156, -1107, -1795, 21380, 14516, -1141, -1764,
77 21027, 14877, -1176, -1732, 20673, 15239, -1211, -1700, 20317, 15602, -1246, -1667, 19959,
78 15965, -1282, -1633, 19600, 16329, -1317, -1599, 19239, 16694, -1353, -1564, 18878, 17058,
79 -1388, -1530, 18515, 17423, -1424, -1495, 18151, 17787, -1459, -1459, 17787, 18151, -1495,
80 -1424, 17423, 18515, -1530, -1388, 17058, 18878, -1564, -1353, 16694, 19239, -1599, -1317,
81 16329, 19600, -1633, -1282, 15965, 19959, -1667, -1246, 15602, 20317, -1700, -1211, 15239,
82 20673, -1732, -1176, 14877, 21027, -1764, -1141, 14516, 21380, -1795, -1107, 14156, 21729,
83 -1825, -1072, 13798, 22077, -1855, -1039, 13441, 22422, -1883, -1005, 13086, 22764, -1911,
84 -972, 12733, 23103, -1937, -939, 12382, 23438, -1962, -907, 12033, 23771, -1986, -875,
85 11686, 24100, -2009, -844, 11342, 24425, -2030, -813, 11000, 24746, -2049, -783, 10662,
86 25063, -2067, -753, 10326, 25375, -2084, -724, 9993, 25683, -2098, -696, 9663, 25987,
87 -2111, -668, 9336, 26285, -2121, -641, 9013, 26579, -2130, -614, 8694, 26867, -2136,
88 -588, 8378, 27151, -2141, -563, 8066, 27428, -2142, -538, 7758, 27700, -2142, -514,
89 7453, 27966, -2139, -490, 7153, 28226, -2133, -468, 6857, 28480, -2125, -445, 6565,
90 28727, -2114, -424, 6277, 28968, -2100, -403, 5994, 29203, -2083, -382, 5716, 29431,
91 -2063, -362, 5442, 29652, -2040, -343, 5172, 29866, -2014, -325, 4908, 30072, -1984,
92 -307, 4648, 30272, -1951, -289, 4393, 30464, -1914, -272, 4143, 30649, -1874, -256,
93 3897, 30826, -1830, -240, 3657, 30995, -1783, -224, 3422, 31157, -1732, -209, 3192,
94 31310, -1676, -194, 2967, 31456, -1617, -180, 2747, 31593, -1554, -167, 2532, 31723,
95 -1486, -153, 2322, 31844, -1414, -140, 2118, 31956, -1338, -128, 1919, 32061, -1258,
96 -115, 1724, 32157, -1173, -103, 1536, 32244, -1084, -92, 1352, 32323, -990, -80,
97 1174, 32393, -891, -69, 1000, 32454, -788, -58, 832, 32507, -680, -47, 669,
98 32551, -568, -36, 512, 32586, -450, -26, 359, 32613, -328, -15, 212, 32630,
99 -200, -5, 69, 32639, -68};
100
101constexpr std::array<s16, 512> curve_lut2 = {
102 3195, 26287, 3329, -32, 3064, 26281, 3467, -34, 2936, 26270, 3608, -38, 2811,
103 26253, 3751, -42, 2688, 26230, 3897, -46, 2568, 26202, 4046, -50, 2451, 26169,
104 4199, -54, 2338, 26130, 4354, -58, 2227, 26085, 4512, -63, 2120, 26035, 4673,
105 -67, 2015, 25980, 4837, -72, 1912, 25919, 5004, -76, 1813, 25852, 5174, -81,
106 1716, 25780, 5347, -87, 1622, 25704, 5522, -92, 1531, 25621, 5701, -98, 1442,
107 25533, 5882, -103, 1357, 25440, 6066, -109, 1274, 25342, 6253, -115, 1193, 25239,
108 6442, -121, 1115, 25131, 6635, -127, 1040, 25018, 6830, -133, 967, 24899, 7027,
109 -140, 897, 24776, 7227, -146, 829, 24648, 7430, -153, 764, 24516, 7635, -159,
110 701, 24379, 7842, -166, 641, 24237, 8052, -174, 583, 24091, 8264, -181, 526,
111 23940, 8478, -187, 472, 23785, 8695, -194, 420, 23626, 8914, -202, 371, 23462,
112 9135, -209, 324, 23295, 9358, -215, 279, 23123, 9583, -222, 236, 22948, 9809,
113 -230, 194, 22769, 10038, -237, 154, 22586, 10269, -243, 117, 22399, 10501, -250,
114 81, 22208, 10735, -258, 47, 22015, 10970, -265, 15, 21818, 11206, -271, -16,
115 21618, 11444, -277, -44, 21415, 11684, -283, -71, 21208, 11924, -290, -97, 20999,
116 12166, -296, -121, 20786, 12409, -302, -143, 20571, 12653, -306, -163, 20354, 12898,
117 -311, -183, 20134, 13143, -316, -201, 19911, 13389, -321, -218, 19686, 13635, -325,
118 -234, 19459, 13882, -328, -248, 19230, 14130, -332, -261, 18998, 14377, -335, -273,
119 18765, 14625, -337, -284, 18531, 14873, -339, -294, 18295, 15121, -341, -302, 18057,
120 15369, -341, -310, 17817, 15617, -341, -317, 17577, 15864, -340, -323, 17335, 16111,
121 -340, -328, 17092, 16357, -338, -332, 16848, 16603, -336, -336, 16603, 16848, -332,
122 -338, 16357, 17092, -328, -340, 16111, 17335, -323, -340, 15864, 17577, -317, -341,
123 15617, 17817, -310, -341, 15369, 18057, -302, -341, 15121, 18295, -294, -339, 14873,
124 18531, -284, -337, 14625, 18765, -273, -335, 14377, 18998, -261, -332, 14130, 19230,
125 -248, -328, 13882, 19459, -234, -325, 13635, 19686, -218, -321, 13389, 19911, -201,
126 -316, 13143, 20134, -183, -311, 12898, 20354, -163, -306, 12653, 20571, -143, -302,
127 12409, 20786, -121, -296, 12166, 20999, -97, -290, 11924, 21208, -71, -283, 11684,
128 21415, -44, -277, 11444, 21618, -16, -271, 11206, 21818, 15, -265, 10970, 22015,
129 47, -258, 10735, 22208, 81, -250, 10501, 22399, 117, -243, 10269, 22586, 154,
130 -237, 10038, 22769, 194, -230, 9809, 22948, 236, -222, 9583, 23123, 279, -215,
131 9358, 23295, 324, -209, 9135, 23462, 371, -202, 8914, 23626, 420, -194, 8695,
132 23785, 472, -187, 8478, 23940, 526, -181, 8264, 24091, 583, -174, 8052, 24237,
133 641, -166, 7842, 24379, 701, -159, 7635, 24516, 764, -153, 7430, 24648, 829,
134 -146, 7227, 24776, 897, -140, 7027, 24899, 967, -133, 6830, 25018, 1040, -127,
135 6635, 25131, 1115, -121, 6442, 25239, 1193, -115, 6253, 25342, 1274, -109, 6066,
136 25440, 1357, -103, 5882, 25533, 1442, -98, 5701, 25621, 1531, -92, 5522, 25704,
137 1622, -87, 5347, 25780, 1716, -81, 5174, 25852, 1813, -76, 5004, 25919, 1912,
138 -72, 4837, 25980, 2015, -67, 4673, 26035, 2120, -63, 4512, 26085, 2227, -58,
139 4354, 26130, 2338, -54, 4199, 26169, 2451, -50, 4046, 26202, 2568, -46, 3897,
140 26230, 2688, -42, 3751, 26253, 2811, -38, 3608, 26270, 2936, -34, 3467, 26281,
141 3064, -32, 3329, 26287, 3195};
23 142
24std::vector<s16> Interpolate(InterpolationState& state, std::vector<s16> input, double ratio) { 143std::vector<s16> Interpolate(InterpolationState& state, std::vector<s16> input, double ratio) {
25 if (input.size() < 2) 144 if (input.size() < 2)
@@ -30,40 +149,39 @@ std::vector<s16> Interpolate(InterpolationState& state, std::vector<s16> input,
30 ratio = 1.0; 149 ratio = 1.0;
31 } 150 }
32 151
33 if (ratio != state.current_ratio) { 152 const int step = static_cast<int>(ratio * 0x8000);
34 const double cutoff_frequency = std::min(0.5 / ratio, 0.5 * ratio); 153 const std::array<s16, 512>& lut = [step] {
35 state.nyquist = CascadingFilter::LowPass(std::clamp(cutoff_frequency, 0.0, 0.4), 3); 154 if (step > 0xaaaa) {
36 state.current_ratio = ratio; 155 return curve_lut0;
37 } 156 }
38 state.nyquist.Process(input); 157 if (step <= 0x8000) {
39 158 return curve_lut1;
40 constexpr std::size_t taps = InterpolationState::lanczos_taps;
41 const std::size_t num_frames = input.size() / 2;
42
43 std::vector<s16> output;
44 output.reserve(static_cast<std::size_t>(input.size() / ratio + 4));
45
46 double& pos = state.position;
47 auto& h = state.history;
48 for (std::size_t i = 0; i < num_frames; ++i) {
49 std::rotate(h.begin(), h.end() - 1, h.end());
50 h[0][0] = input[i * 2 + 0];
51 h[0][1] = input[i * 2 + 1];
52
53 while (pos <= 1.0) {
54 double l = 0.0;
55 double r = 0.0;
56 for (std::size_t j = 0; j < h.size(); j++) {
57 const double lanczos_calc = Lanczos(taps, pos + j - taps + 1);
58 l += lanczos_calc * h[j][0];
59 r += lanczos_calc * h[j][1];
60 }
61 output.emplace_back(static_cast<s16>(std::clamp(l, -32768.0, 32767.0)));
62 output.emplace_back(static_cast<s16>(std::clamp(r, -32768.0, 32767.0)));
63
64 pos += ratio;
65 } 159 }
66 pos -= 1.0; 160 return curve_lut2;
161 }();
162
163 std::vector<s16> output(static_cast<std::size_t>(input.size() / ratio));
164 int in_offset = 0;
165 for (std::size_t out_offset = 0; out_offset < output.size(); out_offset += 2) {
166 const int lut_index = (state.fraction >> 8) * 4;
167
168 const int l = input[(in_offset + 0) * 2 + 0] * lut[lut_index + 0] +
169 input[(in_offset + 1) * 2 + 0] * lut[lut_index + 1] +
170 input[(in_offset + 2) * 2 + 0] * lut[lut_index + 2] +
171 input[(in_offset + 3) * 2 + 0] * lut[lut_index + 3];
172
173 const int r = input[(in_offset + 0) * 2 + 1] * lut[lut_index + 0] +
174 input[(in_offset + 1) * 2 + 1] * lut[lut_index + 1] +
175 input[(in_offset + 2) * 2 + 1] * lut[lut_index + 2] +
176 input[(in_offset + 3) * 2 + 1] * lut[lut_index + 3];
177
178 const int new_offset = state.fraction + step;
179
180 in_offset += new_offset >> 15;
181 state.fraction = new_offset & 0x7fff;
182
183 output[out_offset + 0] = static_cast<s16>(std::clamp(l >> 15, SHRT_MIN, SHRT_MAX));
184 output[out_offset + 1] = static_cast<s16>(std::clamp(r >> 15, SHRT_MIN, SHRT_MAX));
67 } 185 }
68 186
69 return output; 187 return output;
diff --git a/src/audio_core/algorithm/interpolate.h b/src/audio_core/algorithm/interpolate.h
index edbd6460f..1b9831a75 100644
--- a/src/audio_core/algorithm/interpolate.h
+++ b/src/audio_core/algorithm/interpolate.h
@@ -6,19 +6,12 @@
6 6
7#include <array> 7#include <array>
8#include <vector> 8#include <vector>
9#include "audio_core/algorithm/filter.h"
10#include "common/common_types.h" 9#include "common/common_types.h"
11 10
12namespace AudioCore { 11namespace AudioCore {
13 12
14struct InterpolationState { 13struct InterpolationState {
15 static constexpr std::size_t lanczos_taps = 4; 14 int fraction = 0;
16 static constexpr std::size_t history_size = lanczos_taps * 2 - 1;
17
18 double current_ratio = 0.0;
19 CascadingFilter nyquist;
20 std::array<std::array<s16, 2>, history_size> history = {};
21 double position = 0;
22}; 15};
23 16
24/// Interpolates input signal to produce output signal. 17/// Interpolates input signal to produce output signal.
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 26612e692..88c06b2ce 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -187,6 +187,8 @@ add_library(core STATIC
187 hle/kernel/synchronization.h 187 hle/kernel/synchronization.h
188 hle/kernel/thread.cpp 188 hle/kernel/thread.cpp
189 hle/kernel/thread.h 189 hle/kernel/thread.h
190 hle/kernel/time_manager.cpp
191 hle/kernel/time_manager.h
190 hle/kernel/transfer_memory.cpp 192 hle/kernel/transfer_memory.cpp
191 hle/kernel/transfer_memory.h 193 hle/kernel/transfer_memory.h
192 hle/kernel/vm_manager.cpp 194 hle/kernel/vm_manager.cpp
diff --git a/src/core/core.cpp b/src/core/core.cpp
index 0eb0c0dca..86e314c94 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -707,4 +707,12 @@ const Service::SM::ServiceManager& System::ServiceManager() const {
707 return *impl->service_manager; 707 return *impl->service_manager;
708} 708}
709 709
710void System::RegisterCoreThread(std::size_t id) {
711 impl->kernel.RegisterCoreThread(id);
712}
713
714void System::RegisterHostThread() {
715 impl->kernel.RegisterHostThread();
716}
717
710} // namespace Core 718} // namespace Core
diff --git a/src/core/core.h b/src/core/core.h
index e69d68fcf..8d862a8e6 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -360,6 +360,12 @@ public:
360 360
361 const CurrentBuildProcessID& GetCurrentProcessBuildID() const; 361 const CurrentBuildProcessID& GetCurrentProcessBuildID() const;
362 362
363 /// Register a host thread as an emulated CPU Core.
364 void RegisterCoreThread(std::size_t id);
365
366 /// Register a host thread as an auxiliary thread.
367 void RegisterHostThread();
368
363private: 369private:
364 System(); 370 System();
365 371
diff --git a/src/core/frontend/framebuffer_layout.cpp b/src/core/frontend/framebuffer_layout.cpp
index d6d2cf3f0..2dc795d56 100644
--- a/src/core/frontend/framebuffer_layout.cpp
+++ b/src/core/frontend/framebuffer_layout.cpp
@@ -27,9 +27,9 @@ FramebufferLayout DefaultFrameLayout(u32 width, u32 height) {
27 // so just calculate them both even if the other isn't showing. 27 // so just calculate them both even if the other isn't showing.
28 FramebufferLayout res{width, height}; 28 FramebufferLayout res{width, height};
29 29
30 const float emulation_aspect_ratio{static_cast<float>(ScreenUndocked::Height) / 30 const float window_aspect_ratio = static_cast<float>(height) / width;
31 ScreenUndocked::Width}; 31 const float emulation_aspect_ratio = EmulationAspectRatio(
32 const auto window_aspect_ratio = static_cast<float>(height) / width; 32 static_cast<AspectRatio>(Settings::values.aspect_ratio), window_aspect_ratio);
33 33
34 const Common::Rectangle<u32> screen_window_area{0, 0, width, height}; 34 const Common::Rectangle<u32> screen_window_area{0, 0, width, height};
35 Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio); 35 Common::Rectangle<u32> screen = MaxRectangle(screen_window_area, emulation_aspect_ratio);
@@ -58,4 +58,19 @@ FramebufferLayout FrameLayoutFromResolutionScale(u32 res_scale) {
58 return DefaultFrameLayout(width, height); 58 return DefaultFrameLayout(width, height);
59} 59}
60 60
61float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio) {
62 switch (aspect) {
63 case AspectRatio::Default:
64 return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
65 case AspectRatio::R4_3:
66 return 3.0f / 4.0f;
67 case AspectRatio::R21_9:
68 return 9.0f / 21.0f;
69 case AspectRatio::StretchToWindow:
70 return window_aspect_ratio;
71 default:
72 return static_cast<float>(ScreenUndocked::Height) / ScreenUndocked::Width;
73 }
74}
75
61} // namespace Layout 76} // namespace Layout
diff --git a/src/core/frontend/framebuffer_layout.h b/src/core/frontend/framebuffer_layout.h
index d2370adde..1d39c1faf 100644
--- a/src/core/frontend/framebuffer_layout.h
+++ b/src/core/frontend/framebuffer_layout.h
@@ -18,6 +18,13 @@ enum ScreenDocked : u32 {
18 HeightDocked = 1080, 18 HeightDocked = 1080,
19}; 19};
20 20
21enum class AspectRatio {
22 Default,
23 R4_3,
24 R21_9,
25 StretchToWindow,
26};
27
21/// Describes the layout of the window framebuffer 28/// Describes the layout of the window framebuffer
22struct FramebufferLayout { 29struct FramebufferLayout {
23 u32 width{ScreenUndocked::Width}; 30 u32 width{ScreenUndocked::Width};
@@ -48,4 +55,12 @@ FramebufferLayout DefaultFrameLayout(u32 width, u32 height);
48 */ 55 */
49FramebufferLayout FrameLayoutFromResolutionScale(u32 res_scale); 56FramebufferLayout FrameLayoutFromResolutionScale(u32 res_scale);
50 57
58/**
59 * Convenience method to determine emulation aspect ratio
60 * @param aspect Represents the index of aspect ratio stored in Settings::values.aspect_ratio
61 * @param window_aspect_ratio Current window aspect ratio
62 * @return Emulation render window aspect ratio
63 */
64float EmulationAspectRatio(AspectRatio aspect, float window_aspect_ratio);
65
51} // namespace Layout 66} // namespace Layout
diff --git a/src/core/hardware_properties.h b/src/core/hardware_properties.h
index 213461b6a..b04e046ed 100644
--- a/src/core/hardware_properties.h
+++ b/src/core/hardware_properties.h
@@ -20,6 +20,8 @@ constexpr u32 NUM_CPU_CORES = 4; // Number of CPU Cores
20 20
21} // namespace Hardware 21} // namespace Hardware
22 22
23constexpr u32 INVALID_HOST_THREAD_ID = 0xFFFFFFFF;
24
23struct EmuThreadHandle { 25struct EmuThreadHandle {
24 u32 host_handle; 26 u32 host_handle;
25 u32 guest_handle; 27 u32 guest_handle;
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 4eb1d8703..9232f4d7e 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -3,9 +3,12 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <atomic> 5#include <atomic>
6#include <bitset>
6#include <functional> 7#include <functional>
7#include <memory> 8#include <memory>
8#include <mutex> 9#include <mutex>
10#include <thread>
11#include <unordered_map>
9#include <utility> 12#include <utility>
10 13
11#include "common/assert.h" 14#include "common/assert.h"
@@ -15,6 +18,7 @@
15#include "core/core.h" 18#include "core/core.h"
16#include "core/core_timing.h" 19#include "core/core_timing.h"
17#include "core/core_timing_util.h" 20#include "core/core_timing_util.h"
21#include "core/hardware_properties.h"
18#include "core/hle/kernel/client_port.h" 22#include "core/hle/kernel/client_port.h"
19#include "core/hle/kernel/errors.h" 23#include "core/hle/kernel/errors.h"
20#include "core/hle/kernel/handle_table.h" 24#include "core/hle/kernel/handle_table.h"
@@ -25,6 +29,7 @@
25#include "core/hle/kernel/scheduler.h" 29#include "core/hle/kernel/scheduler.h"
26#include "core/hle/kernel/synchronization.h" 30#include "core/hle/kernel/synchronization.h"
27#include "core/hle/kernel/thread.h" 31#include "core/hle/kernel/thread.h"
32#include "core/hle/kernel/time_manager.h"
28#include "core/hle/lock.h" 33#include "core/hle/lock.h"
29#include "core/hle/result.h" 34#include "core/hle/result.h"
30#include "core/memory.h" 35#include "core/memory.h"
@@ -44,7 +49,7 @@ static void ThreadWakeupCallback(u64 thread_handle, [[maybe_unused]] s64 cycles_
44 std::lock_guard lock{HLE::g_hle_lock}; 49 std::lock_guard lock{HLE::g_hle_lock};
45 50
46 std::shared_ptr<Thread> thread = 51 std::shared_ptr<Thread> thread =
47 system.Kernel().RetrieveThreadFromWakeupCallbackHandleTable(proper_handle); 52 system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
48 if (thread == nullptr) { 53 if (thread == nullptr) {
49 LOG_CRITICAL(Kernel, "Callback fired for invalid thread {:08X}", proper_handle); 54 LOG_CRITICAL(Kernel, "Callback fired for invalid thread {:08X}", proper_handle);
50 return; 55 return;
@@ -97,8 +102,8 @@ static void ThreadWakeupCallback(u64 thread_handle, [[maybe_unused]] s64 cycles_
97} 102}
98 103
99struct KernelCore::Impl { 104struct KernelCore::Impl {
100 explicit Impl(Core::System& system) 105 explicit Impl(Core::System& system, KernelCore& kernel)
101 : system{system}, global_scheduler{system}, synchronization{system} {} 106 : system{system}, global_scheduler{kernel}, synchronization{system}, time_manager{system} {}
102 107
103 void Initialize(KernelCore& kernel) { 108 void Initialize(KernelCore& kernel) {
104 Shutdown(); 109 Shutdown();
@@ -120,7 +125,7 @@ struct KernelCore::Impl {
120 125
121 system_resource_limit = nullptr; 126 system_resource_limit = nullptr;
122 127
123 thread_wakeup_callback_handle_table.Clear(); 128 global_handle_table.Clear();
124 thread_wakeup_event_type = nullptr; 129 thread_wakeup_event_type = nullptr;
125 preemption_event = nullptr; 130 preemption_event = nullptr;
126 131
@@ -138,8 +143,8 @@ struct KernelCore::Impl {
138 143
139 void InitializePhysicalCores() { 144 void InitializePhysicalCores() {
140 exclusive_monitor = 145 exclusive_monitor =
141 Core::MakeExclusiveMonitor(system.Memory(), global_scheduler.CpuCoresCount()); 146 Core::MakeExclusiveMonitor(system.Memory(), Core::Hardware::NUM_CPU_CORES);
142 for (std::size_t i = 0; i < global_scheduler.CpuCoresCount(); i++) { 147 for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
143 cores.emplace_back(system, i, *exclusive_monitor); 148 cores.emplace_back(system, i, *exclusive_monitor);
144 } 149 }
145 } 150 }
@@ -184,6 +189,50 @@ struct KernelCore::Impl {
184 system.Memory().SetCurrentPageTable(*process); 189 system.Memory().SetCurrentPageTable(*process);
185 } 190 }
186 191
192 void RegisterCoreThread(std::size_t core_id) {
193 std::unique_lock lock{register_thread_mutex};
194 const std::thread::id this_id = std::this_thread::get_id();
195 const auto it = host_thread_ids.find(this_id);
196 ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
197 ASSERT(it == host_thread_ids.end());
198 ASSERT(!registered_core_threads[core_id]);
199 host_thread_ids[this_id] = static_cast<u32>(core_id);
200 registered_core_threads.set(core_id);
201 }
202
203 void RegisterHostThread() {
204 std::unique_lock lock{register_thread_mutex};
205 const std::thread::id this_id = std::this_thread::get_id();
206 const auto it = host_thread_ids.find(this_id);
207 ASSERT(it == host_thread_ids.end());
208 host_thread_ids[this_id] = registered_thread_ids++;
209 }
210
211 u32 GetCurrentHostThreadID() const {
212 const std::thread::id this_id = std::this_thread::get_id();
213 const auto it = host_thread_ids.find(this_id);
214 if (it == host_thread_ids.end()) {
215 return Core::INVALID_HOST_THREAD_ID;
216 }
217 return it->second;
218 }
219
220 Core::EmuThreadHandle GetCurrentEmuThreadID() const {
221 Core::EmuThreadHandle result = Core::EmuThreadHandle::InvalidHandle();
222 result.host_handle = GetCurrentHostThreadID();
223 if (result.host_handle >= Core::Hardware::NUM_CPU_CORES) {
224 return result;
225 }
226 const Kernel::Scheduler& sched = cores[result.host_handle].Scheduler();
227 const Kernel::Thread* current = sched.GetCurrentThread();
228 if (current != nullptr) {
229 result.guest_handle = current->GetGlobalHandle();
230 } else {
231 result.guest_handle = InvalidHandle;
232 }
233 return result;
234 }
235
187 std::atomic<u32> next_object_id{0}; 236 std::atomic<u32> next_object_id{0};
188 std::atomic<u64> next_kernel_process_id{Process::InitialKIPIDMin}; 237 std::atomic<u64> next_kernel_process_id{Process::InitialKIPIDMin};
189 std::atomic<u64> next_user_process_id{Process::ProcessIDMin}; 238 std::atomic<u64> next_user_process_id{Process::ProcessIDMin};
@@ -194,15 +243,16 @@ struct KernelCore::Impl {
194 Process* current_process = nullptr; 243 Process* current_process = nullptr;
195 Kernel::GlobalScheduler global_scheduler; 244 Kernel::GlobalScheduler global_scheduler;
196 Kernel::Synchronization synchronization; 245 Kernel::Synchronization synchronization;
246 Kernel::TimeManager time_manager;
197 247
198 std::shared_ptr<ResourceLimit> system_resource_limit; 248 std::shared_ptr<ResourceLimit> system_resource_limit;
199 249
200 std::shared_ptr<Core::Timing::EventType> thread_wakeup_event_type; 250 std::shared_ptr<Core::Timing::EventType> thread_wakeup_event_type;
201 std::shared_ptr<Core::Timing::EventType> preemption_event; 251 std::shared_ptr<Core::Timing::EventType> preemption_event;
202 252
203 // TODO(yuriks): This can be removed if Thread objects are explicitly pooled in the future, 253 // This is the kernel's handle table or supervisor handle table which
204 // allowing us to simply use a pool index or similar. 254 // stores all the objects in place.
205 Kernel::HandleTable thread_wakeup_callback_handle_table; 255 Kernel::HandleTable global_handle_table;
206 256
207 /// Map of named ports managed by the kernel, which can be retrieved using 257 /// Map of named ports managed by the kernel, which can be retrieved using
208 /// the ConnectToPort SVC. 258 /// the ConnectToPort SVC.
@@ -211,11 +261,17 @@ struct KernelCore::Impl {
211 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor; 261 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
212 std::vector<Kernel::PhysicalCore> cores; 262 std::vector<Kernel::PhysicalCore> cores;
213 263
264 // 0-3 IDs represent core threads, >3 represent others
265 std::unordered_map<std::thread::id, u32> host_thread_ids;
266 u32 registered_thread_ids{Core::Hardware::NUM_CPU_CORES};
267 std::bitset<Core::Hardware::NUM_CPU_CORES> registered_core_threads;
268 std::mutex register_thread_mutex;
269
214 // System context 270 // System context
215 Core::System& system; 271 Core::System& system;
216}; 272};
217 273
218KernelCore::KernelCore(Core::System& system) : impl{std::make_unique<Impl>(system)} {} 274KernelCore::KernelCore(Core::System& system) : impl{std::make_unique<Impl>(system, *this)} {}
219KernelCore::~KernelCore() { 275KernelCore::~KernelCore() {
220 Shutdown(); 276 Shutdown();
221} 277}
@@ -232,9 +288,8 @@ std::shared_ptr<ResourceLimit> KernelCore::GetSystemResourceLimit() const {
232 return impl->system_resource_limit; 288 return impl->system_resource_limit;
233} 289}
234 290
235std::shared_ptr<Thread> KernelCore::RetrieveThreadFromWakeupCallbackHandleTable( 291std::shared_ptr<Thread> KernelCore::RetrieveThreadFromGlobalHandleTable(Handle handle) const {
236 Handle handle) const { 292 return impl->global_handle_table.Get<Thread>(handle);
237 return impl->thread_wakeup_callback_handle_table.Get<Thread>(handle);
238} 293}
239 294
240void KernelCore::AppendNewProcess(std::shared_ptr<Process> process) { 295void KernelCore::AppendNewProcess(std::shared_ptr<Process> process) {
@@ -265,6 +320,14 @@ const Kernel::GlobalScheduler& KernelCore::GlobalScheduler() const {
265 return impl->global_scheduler; 320 return impl->global_scheduler;
266} 321}
267 322
323Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) {
324 return impl->cores[id].Scheduler();
325}
326
327const Kernel::Scheduler& KernelCore::Scheduler(std::size_t id) const {
328 return impl->cores[id].Scheduler();
329}
330
268Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) { 331Kernel::PhysicalCore& KernelCore::PhysicalCore(std::size_t id) {
269 return impl->cores[id]; 332 return impl->cores[id];
270} 333}
@@ -281,6 +344,14 @@ const Kernel::Synchronization& KernelCore::Synchronization() const {
281 return impl->synchronization; 344 return impl->synchronization;
282} 345}
283 346
347Kernel::TimeManager& KernelCore::TimeManager() {
348 return impl->time_manager;
349}
350
351const Kernel::TimeManager& KernelCore::TimeManager() const {
352 return impl->time_manager;
353}
354
284Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() { 355Core::ExclusiveMonitor& KernelCore::GetExclusiveMonitor() {
285 return *impl->exclusive_monitor; 356 return *impl->exclusive_monitor;
286} 357}
@@ -338,12 +409,28 @@ const std::shared_ptr<Core::Timing::EventType>& KernelCore::ThreadWakeupCallback
338 return impl->thread_wakeup_event_type; 409 return impl->thread_wakeup_event_type;
339} 410}
340 411
341Kernel::HandleTable& KernelCore::ThreadWakeupCallbackHandleTable() { 412Kernel::HandleTable& KernelCore::GlobalHandleTable() {
342 return impl->thread_wakeup_callback_handle_table; 413 return impl->global_handle_table;
414}
415
416const Kernel::HandleTable& KernelCore::GlobalHandleTable() const {
417 return impl->global_handle_table;
418}
419
420void KernelCore::RegisterCoreThread(std::size_t core_id) {
421 impl->RegisterCoreThread(core_id);
422}
423
424void KernelCore::RegisterHostThread() {
425 impl->RegisterHostThread();
426}
427
428u32 KernelCore::GetCurrentHostThreadID() const {
429 return impl->GetCurrentHostThreadID();
343} 430}
344 431
345const Kernel::HandleTable& KernelCore::ThreadWakeupCallbackHandleTable() const { 432Core::EmuThreadHandle KernelCore::GetCurrentEmuThreadID() const {
346 return impl->thread_wakeup_callback_handle_table; 433 return impl->GetCurrentEmuThreadID();
347} 434}
348 435
349} // namespace Kernel 436} // namespace Kernel
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index 1eede3063..c4f78ab71 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -11,6 +11,7 @@
11#include "core/hle/kernel/object.h" 11#include "core/hle/kernel/object.h"
12 12
13namespace Core { 13namespace Core {
14struct EmuThreadHandle;
14class ExclusiveMonitor; 15class ExclusiveMonitor;
15class System; 16class System;
16} // namespace Core 17} // namespace Core
@@ -29,8 +30,10 @@ class HandleTable;
29class PhysicalCore; 30class PhysicalCore;
30class Process; 31class Process;
31class ResourceLimit; 32class ResourceLimit;
33class Scheduler;
32class Synchronization; 34class Synchronization;
33class Thread; 35class Thread;
36class TimeManager;
34 37
35/// Represents a single instance of the kernel. 38/// Represents a single instance of the kernel.
36class KernelCore { 39class KernelCore {
@@ -64,7 +67,7 @@ public:
64 std::shared_ptr<ResourceLimit> GetSystemResourceLimit() const; 67 std::shared_ptr<ResourceLimit> GetSystemResourceLimit() const;
65 68
66 /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table. 69 /// Retrieves a shared pointer to a Thread instance within the thread wakeup handle table.
67 std::shared_ptr<Thread> RetrieveThreadFromWakeupCallbackHandleTable(Handle handle) const; 70 std::shared_ptr<Thread> RetrieveThreadFromGlobalHandleTable(Handle handle) const;
68 71
69 /// Adds the given shared pointer to an internal list of active processes. 72 /// Adds the given shared pointer to an internal list of active processes.
70 void AppendNewProcess(std::shared_ptr<Process> process); 73 void AppendNewProcess(std::shared_ptr<Process> process);
@@ -87,6 +90,12 @@ public:
87 /// Gets the sole instance of the global scheduler 90 /// Gets the sole instance of the global scheduler
88 const Kernel::GlobalScheduler& GlobalScheduler() const; 91 const Kernel::GlobalScheduler& GlobalScheduler() const;
89 92
93 /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
94 Kernel::Scheduler& Scheduler(std::size_t id);
95
96 /// Gets the sole instance of the Scheduler assoviated with cpu core 'id'
97 const Kernel::Scheduler& Scheduler(std::size_t id) const;
98
90 /// Gets the an instance of the respective physical CPU core. 99 /// Gets the an instance of the respective physical CPU core.
91 Kernel::PhysicalCore& PhysicalCore(std::size_t id); 100 Kernel::PhysicalCore& PhysicalCore(std::size_t id);
92 101
@@ -99,6 +108,12 @@ public:
99 /// Gets the an instance of the Synchronization Interface. 108 /// Gets the an instance of the Synchronization Interface.
100 const Kernel::Synchronization& Synchronization() const; 109 const Kernel::Synchronization& Synchronization() const;
101 110
111 /// Gets the an instance of the TimeManager Interface.
112 Kernel::TimeManager& TimeManager();
113
114 /// Gets the an instance of the TimeManager Interface.
115 const Kernel::TimeManager& TimeManager() const;
116
102 /// Stops execution of 'id' core, in order to reschedule a new thread. 117 /// Stops execution of 'id' core, in order to reschedule a new thread.
103 void PrepareReschedule(std::size_t id); 118 void PrepareReschedule(std::size_t id);
104 119
@@ -120,6 +135,18 @@ public:
120 /// Determines whether or not the given port is a valid named port. 135 /// Determines whether or not the given port is a valid named port.
121 bool IsValidNamedPort(NamedPortTable::const_iterator port) const; 136 bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
122 137
138 /// Gets the current host_thread/guest_thread handle.
139 Core::EmuThreadHandle GetCurrentEmuThreadID() const;
140
141 /// Gets the current host_thread handle.
142 u32 GetCurrentHostThreadID() const;
143
144 /// Register the current thread as a CPU Core Thread.
145 void RegisterCoreThread(std::size_t core_id);
146
147 /// Register the current thread as a non CPU core thread.
148 void RegisterHostThread();
149
123private: 150private:
124 friend class Object; 151 friend class Object;
125 friend class Process; 152 friend class Process;
@@ -140,11 +167,11 @@ private:
140 /// Retrieves the event type used for thread wakeup callbacks. 167 /// Retrieves the event type used for thread wakeup callbacks.
141 const std::shared_ptr<Core::Timing::EventType>& ThreadWakeupCallbackEventType() const; 168 const std::shared_ptr<Core::Timing::EventType>& ThreadWakeupCallbackEventType() const;
142 169
143 /// Provides a reference to the thread wakeup callback handle table. 170 /// Provides a reference to the global handle table.
144 Kernel::HandleTable& ThreadWakeupCallbackHandleTable(); 171 Kernel::HandleTable& GlobalHandleTable();
145 172
146 /// Provides a const reference to the thread wakeup callback handle table. 173 /// Provides a const reference to the global handle table.
147 const Kernel::HandleTable& ThreadWakeupCallbackHandleTable() const; 174 const Kernel::HandleTable& GlobalHandleTable() const;
148 175
149 struct Impl; 176 struct Impl;
150 std::unique_ptr<Impl> impl; 177 std::unique_ptr<Impl> impl;
diff --git a/src/core/hle/kernel/scheduler.cpp b/src/core/hle/kernel/scheduler.cpp
index 86f1421bf..c65f82fb7 100644
--- a/src/core/hle/kernel/scheduler.cpp
+++ b/src/core/hle/kernel/scheduler.cpp
@@ -18,10 +18,11 @@
18#include "core/hle/kernel/kernel.h" 18#include "core/hle/kernel/kernel.h"
19#include "core/hle/kernel/process.h" 19#include "core/hle/kernel/process.h"
20#include "core/hle/kernel/scheduler.h" 20#include "core/hle/kernel/scheduler.h"
21#include "core/hle/kernel/time_manager.h"
21 22
22namespace Kernel { 23namespace Kernel {
23 24
24GlobalScheduler::GlobalScheduler(Core::System& system) : system{system} {} 25GlobalScheduler::GlobalScheduler(KernelCore& kernel) : kernel{kernel} {}
25 26
26GlobalScheduler::~GlobalScheduler() = default; 27GlobalScheduler::~GlobalScheduler() = default;
27 28
@@ -35,7 +36,7 @@ void GlobalScheduler::RemoveThread(std::shared_ptr<Thread> thread) {
35} 36}
36 37
37void GlobalScheduler::UnloadThread(std::size_t core) { 38void GlobalScheduler::UnloadThread(std::size_t core) {
38 Scheduler& sched = system.Scheduler(core); 39 Scheduler& sched = kernel.Scheduler(core);
39 sched.UnloadThread(); 40 sched.UnloadThread();
40} 41}
41 42
@@ -50,7 +51,7 @@ void GlobalScheduler::SelectThread(std::size_t core) {
50 sched.is_context_switch_pending = sched.selected_thread != sched.current_thread; 51 sched.is_context_switch_pending = sched.selected_thread != sched.current_thread;
51 std::atomic_thread_fence(std::memory_order_seq_cst); 52 std::atomic_thread_fence(std::memory_order_seq_cst);
52 }; 53 };
53 Scheduler& sched = system.Scheduler(core); 54 Scheduler& sched = kernel.Scheduler(core);
54 Thread* current_thread = nullptr; 55 Thread* current_thread = nullptr;
55 // Step 1: Get top thread in schedule queue. 56 // Step 1: Get top thread in schedule queue.
56 current_thread = scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front(); 57 current_thread = scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front();
@@ -356,6 +357,32 @@ void GlobalScheduler::Shutdown() {
356 thread_list.clear(); 357 thread_list.clear();
357} 358}
358 359
360void GlobalScheduler::Lock() {
361 Core::EmuThreadHandle current_thread = kernel.GetCurrentEmuThreadID();
362 if (current_thread == current_owner) {
363 ++scope_lock;
364 } else {
365 inner_lock.lock();
366 current_owner = current_thread;
367 ASSERT(current_owner != Core::EmuThreadHandle::InvalidHandle());
368 scope_lock = 1;
369 }
370}
371
372void GlobalScheduler::Unlock() {
373 if (--scope_lock != 0) {
374 ASSERT(scope_lock > 0);
375 return;
376 }
377 for (std::size_t i = 0; i < Core::Hardware::NUM_CPU_CORES; i++) {
378 SelectThread(i);
379 }
380 current_owner = Core::EmuThreadHandle::InvalidHandle();
381 scope_lock = 1;
382 inner_lock.unlock();
383 // TODO(Blinkhawk): Setup the interrupts and change context on current core.
384}
385
359Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, std::size_t core_id) 386Scheduler::Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, std::size_t core_id)
360 : system(system), cpu_core(cpu_core), core_id(core_id) {} 387 : system(system), cpu_core(cpu_core), core_id(core_id) {}
361 388
@@ -485,4 +512,27 @@ void Scheduler::Shutdown() {
485 selected_thread = nullptr; 512 selected_thread = nullptr;
486} 513}
487 514
515SchedulerLock::SchedulerLock(KernelCore& kernel) : kernel{kernel} {
516 kernel.GlobalScheduler().Lock();
517}
518
519SchedulerLock::~SchedulerLock() {
520 kernel.GlobalScheduler().Unlock();
521}
522
523SchedulerLockAndSleep::SchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle,
524 Thread* time_task, s64 nanoseconds)
525 : SchedulerLock{kernel}, event_handle{event_handle}, time_task{time_task}, nanoseconds{
526 nanoseconds} {
527 event_handle = InvalidHandle;
528}
529
530SchedulerLockAndSleep::~SchedulerLockAndSleep() {
531 if (sleep_cancelled) {
532 return;
533 }
534 auto& time_manager = kernel.TimeManager();
535 time_manager.ScheduleTimeEvent(event_handle, time_task, nanoseconds);
536}
537
488} // namespace Kernel 538} // namespace Kernel
diff --git a/src/core/hle/kernel/scheduler.h b/src/core/hle/kernel/scheduler.h
index 96db049cb..1c93a838c 100644
--- a/src/core/hle/kernel/scheduler.h
+++ b/src/core/hle/kernel/scheduler.h
@@ -6,6 +6,7 @@
6 6
7#include <atomic> 7#include <atomic>
8#include <memory> 8#include <memory>
9#include <mutex>
9#include <vector> 10#include <vector>
10 11
11#include "common/common_types.h" 12#include "common/common_types.h"
@@ -20,11 +21,13 @@ class System;
20 21
21namespace Kernel { 22namespace Kernel {
22 23
24class KernelCore;
23class Process; 25class Process;
26class SchedulerLock;
24 27
25class GlobalScheduler final { 28class GlobalScheduler final {
26public: 29public:
27 explicit GlobalScheduler(Core::System& system); 30 explicit GlobalScheduler(KernelCore& kernel);
28 ~GlobalScheduler(); 31 ~GlobalScheduler();
29 32
30 /// Adds a new thread to the scheduler 33 /// Adds a new thread to the scheduler
@@ -138,6 +141,14 @@ public:
138 void Shutdown(); 141 void Shutdown();
139 142
140private: 143private:
144 friend class SchedulerLock;
145
146 /// Lock the scheduler to the current thread.
147 void Lock();
148
149 /// Unlocks the scheduler, reselects threads, interrupts cores for rescheduling
150 /// and reschedules current core if needed.
151 void Unlock();
141 /** 152 /**
142 * Transfers a thread into an specific core. If the destination_core is -1 153 * Transfers a thread into an specific core. If the destination_core is -1
143 * it will be unscheduled from its source code and added into its suggested 154 * it will be unscheduled from its source code and added into its suggested
@@ -158,9 +169,14 @@ private:
158 // ordered from Core 0 to Core 3. 169 // ordered from Core 0 to Core 3.
159 std::array<u32, Core::Hardware::NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62}; 170 std::array<u32, Core::Hardware::NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62};
160 171
172 /// Scheduler lock mechanisms.
173 std::mutex inner_lock{}; // TODO(Blinkhawk): Replace for a SpinLock
174 std::atomic<s64> scope_lock{};
175 Core::EmuThreadHandle current_owner{Core::EmuThreadHandle::InvalidHandle()};
176
161 /// Lists all thread ids that aren't deleted/etc. 177 /// Lists all thread ids that aren't deleted/etc.
162 std::vector<std::shared_ptr<Thread>> thread_list; 178 std::vector<std::shared_ptr<Thread>> thread_list;
163 Core::System& system; 179 KernelCore& kernel;
164}; 180};
165 181
166class Scheduler final { 182class Scheduler final {
@@ -227,4 +243,30 @@ private:
227 bool is_context_switch_pending = false; 243 bool is_context_switch_pending = false;
228}; 244};
229 245
246class SchedulerLock {
247public:
248 explicit SchedulerLock(KernelCore& kernel);
249 ~SchedulerLock();
250
251protected:
252 KernelCore& kernel;
253};
254
255class SchedulerLockAndSleep : public SchedulerLock {
256public:
257 explicit SchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle, Thread* time_task,
258 s64 nanoseconds);
259 ~SchedulerLockAndSleep();
260
261 void CancelSleep() {
262 sleep_cancelled = true;
263 }
264
265private:
266 Handle& event_handle;
267 Thread* time_task;
268 s64 nanoseconds;
269 bool sleep_cancelled{};
270};
271
230} // namespace Kernel 272} // namespace Kernel
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index ae5f2c8bd..bf850e0b2 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -46,9 +46,9 @@ Thread::~Thread() = default;
46void Thread::Stop() { 46void Thread::Stop() {
47 // Cancel any outstanding wakeup events for this thread 47 // Cancel any outstanding wakeup events for this thread
48 Core::System::GetInstance().CoreTiming().UnscheduleEvent(kernel.ThreadWakeupCallbackEventType(), 48 Core::System::GetInstance().CoreTiming().UnscheduleEvent(kernel.ThreadWakeupCallbackEventType(),
49 callback_handle); 49 global_handle);
50 kernel.ThreadWakeupCallbackHandleTable().Close(callback_handle); 50 kernel.GlobalHandleTable().Close(global_handle);
51 callback_handle = 0; 51 global_handle = 0;
52 SetStatus(ThreadStatus::Dead); 52 SetStatus(ThreadStatus::Dead);
53 Signal(); 53 Signal();
54 54
@@ -73,12 +73,12 @@ void Thread::WakeAfterDelay(s64 nanoseconds) {
73 // thread-safe version of ScheduleEvent. 73 // thread-safe version of ScheduleEvent.
74 const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds}); 74 const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds});
75 Core::System::GetInstance().CoreTiming().ScheduleEvent( 75 Core::System::GetInstance().CoreTiming().ScheduleEvent(
76 cycles, kernel.ThreadWakeupCallbackEventType(), callback_handle); 76 cycles, kernel.ThreadWakeupCallbackEventType(), global_handle);
77} 77}
78 78
79void Thread::CancelWakeupTimer() { 79void Thread::CancelWakeupTimer() {
80 Core::System::GetInstance().CoreTiming().UnscheduleEvent(kernel.ThreadWakeupCallbackEventType(), 80 Core::System::GetInstance().CoreTiming().UnscheduleEvent(kernel.ThreadWakeupCallbackEventType(),
81 callback_handle); 81 global_handle);
82} 82}
83 83
84void Thread::ResumeFromWait() { 84void Thread::ResumeFromWait() {
@@ -190,7 +190,7 @@ ResultVal<std::shared_ptr<Thread>> Thread::Create(KernelCore& kernel, std::strin
190 thread->condvar_wait_address = 0; 190 thread->condvar_wait_address = 0;
191 thread->wait_handle = 0; 191 thread->wait_handle = 0;
192 thread->name = std::move(name); 192 thread->name = std::move(name);
193 thread->callback_handle = kernel.ThreadWakeupCallbackHandleTable().Create(thread).Unwrap(); 193 thread->global_handle = kernel.GlobalHandleTable().Create(thread).Unwrap();
194 thread->owner_process = &owner_process; 194 thread->owner_process = &owner_process;
195 auto& scheduler = kernel.GlobalScheduler(); 195 auto& scheduler = kernel.GlobalScheduler();
196 scheduler.AddThread(thread); 196 scheduler.AddThread(thread);
diff --git a/src/core/hle/kernel/thread.h b/src/core/hle/kernel/thread.h
index 7a4916318..129e7858a 100644
--- a/src/core/hle/kernel/thread.h
+++ b/src/core/hle/kernel/thread.h
@@ -453,6 +453,10 @@ public:
453 is_sync_cancelled = value; 453 is_sync_cancelled = value;
454 } 454 }
455 455
456 Handle GetGlobalHandle() const {
457 return global_handle;
458 }
459
456private: 460private:
457 void SetSchedulingStatus(ThreadSchedStatus new_status); 461 void SetSchedulingStatus(ThreadSchedStatus new_status);
458 void SetCurrentPriority(u32 new_priority); 462 void SetCurrentPriority(u32 new_priority);
@@ -514,7 +518,7 @@ private:
514 VAddr arb_wait_address{0}; 518 VAddr arb_wait_address{0};
515 519
516 /// Handle used as userdata to reference this object when inserting into the CoreTiming queue. 520 /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
517 Handle callback_handle = 0; 521 Handle global_handle = 0;
518 522
519 /// Callback that will be invoked when the thread is resumed from a waiting state. If the thread 523 /// Callback that will be invoked when the thread is resumed from a waiting state. If the thread
520 /// was waiting via WaitSynchronization then the object will be the last object that became 524 /// was waiting via WaitSynchronization then the object will be the last object that became
diff --git a/src/core/hle/kernel/time_manager.cpp b/src/core/hle/kernel/time_manager.cpp
new file mode 100644
index 000000000..21b290468
--- /dev/null
+++ b/src/core/hle/kernel/time_manager.cpp
@@ -0,0 +1,44 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "common/assert.h"
6#include "core/core.h"
7#include "core/core_timing.h"
8#include "core/core_timing_util.h"
9#include "core/hle/kernel/handle_table.h"
10#include "core/hle/kernel/kernel.h"
11#include "core/hle/kernel/thread.h"
12#include "core/hle/kernel/time_manager.h"
13
14namespace Kernel {
15
16TimeManager::TimeManager(Core::System& system) : system{system} {
17 time_manager_event_type = Core::Timing::CreateEvent(
18 "Kernel::TimeManagerCallback", [this](u64 thread_handle, [[maybe_unused]] s64 cycles_late) {
19 Handle proper_handle = static_cast<Handle>(thread_handle);
20 std::shared_ptr<Thread> thread =
21 this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
22 thread->ResumeFromWait();
23 });
24}
25
26void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
27 if (nanoseconds > 0) {
28 ASSERT(timetask);
29 event_handle = timetask->GetGlobalHandle();
30 const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds});
31 system.CoreTiming().ScheduleEvent(cycles, time_manager_event_type, event_handle);
32 } else {
33 event_handle = InvalidHandle;
34 }
35}
36
37void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
38 if (event_handle == InvalidHandle) {
39 return;
40 }
41 system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
42}
43
44} // namespace Kernel
diff --git a/src/core/hle/kernel/time_manager.h b/src/core/hle/kernel/time_manager.h
new file mode 100644
index 000000000..eaec486d1
--- /dev/null
+++ b/src/core/hle/kernel/time_manager.h
@@ -0,0 +1,43 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <memory>
8
9#include "core/hle/kernel/object.h"
10
11namespace Core {
12class System;
13} // namespace Core
14
15namespace Core::Timing {
16struct EventType;
17} // namespace Core::Timing
18
19namespace Kernel {
20
21class Thread;
22
23/**
24 * The `TimeManager` takes care of scheduling time events on threads and executes their TimeUp
25 * method when the event is triggered.
26 */
27class TimeManager {
28public:
29 explicit TimeManager(Core::System& system);
30
31 /// Schedule a time event on `timetask` thread that will expire in 'nanoseconds'
32 /// returns a non-invalid handle in `event_handle` if correctly scheduled
33 void ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds);
34
35 /// Unschedule an existing time event
36 void UnscheduleTimeEvent(Handle event_handle);
37
38private:
39 Core::System& system;
40 std::shared_ptr<Core::Timing::EventType> time_manager_event_type;
41};
42
43} // namespace Kernel
diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp
index 67e39a5c4..f589864ee 100644
--- a/src/core/hle/service/bcat/backend/boxcat.cpp
+++ b/src/core/hle/service/bcat/backend/boxcat.cpp
@@ -200,7 +200,8 @@ private:
200 DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds, 200 DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds,
201 const std::string& content_type_name) { 201 const std::string& content_type_name) {
202 if (client == nullptr) { 202 if (client == nullptr) {
203 client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT, timeout_seconds); 203 client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT);
204 client->set_timeout_sec(timeout_seconds);
204 } 205 }
205 206
206 httplib::Headers headers{ 207 httplib::Headers headers{
@@ -448,8 +449,8 @@ std::optional<std::vector<u8>> Boxcat::GetLaunchParameter(TitleIDVersion title)
448 449
449Boxcat::StatusResult Boxcat::GetStatus(std::optional<std::string>& global, 450Boxcat::StatusResult Boxcat::GetStatus(std::optional<std::string>& global,
450 std::map<std::string, EventStatus>& games) { 451 std::map<std::string, EventStatus>& games) {
451 httplib::SSLClient client{BOXCAT_HOSTNAME, static_cast<int>(PORT), 452 httplib::SSLClient client{BOXCAT_HOSTNAME, static_cast<int>(PORT)};
452 static_cast<int>(TIMEOUT_SECONDS)}; 453 client.set_timeout_sec(static_cast<int>(TIMEOUT_SECONDS));
453 454
454 httplib::Headers headers{ 455 httplib::Headers headers{
455 {std::string("Game-Assets-API-Version"), std::string(BOXCAT_API_VERSION)}, 456 {std::string("Game-Assets-API-Version"), std::string(BOXCAT_API_VERSION)},
diff --git a/src/core/hle/service/ldn/ldn.cpp b/src/core/hle/service/ldn/ldn.cpp
index ed5059047..92adde6d4 100644
--- a/src/core/hle/service/ldn/ldn.cpp
+++ b/src/core/hle/service/ldn/ldn.cpp
@@ -129,12 +129,20 @@ public:
129 {304, nullptr, "Disconnect"}, 129 {304, nullptr, "Disconnect"},
130 {400, nullptr, "Initialize"}, 130 {400, nullptr, "Initialize"},
131 {401, nullptr, "Finalize"}, 131 {401, nullptr, "Finalize"},
132 {402, nullptr, "SetOperationMode"}, 132 {402, &IUserLocalCommunicationService::Initialize2, "Initialize2"}, // 7.0.0+
133 }; 133 };
134 // clang-format on 134 // clang-format on
135 135
136 RegisterHandlers(functions); 136 RegisterHandlers(functions);
137 } 137 }
138
139 void Initialize2(Kernel::HLERequestContext& ctx) {
140 LOG_WARNING(Service_LDN, "(STUBBED) called");
141 // Result success seem make this services start network and continue.
142 // If we just pass result error then it will stop and maybe try again and again.
143 IPC::ResponseBuilder rb{ctx, 2};
144 rb.Push(RESULT_UNKNOWN);
145 }
138}; 146};
139 147
140class LDNS final : public ServiceFramework<LDNS> { 148class LDNS final : public ServiceFramework<LDNS> {
diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp
index 6d8bca8bb..f1966ac0e 100644
--- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp
+++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.cpp
@@ -44,6 +44,8 @@ u32 nvhost_gpu::ioctl(Ioctl command, const std::vector<u8>& input, const std::ve
44 return GetWaitbase(input, output); 44 return GetWaitbase(input, output);
45 case IoctlCommand::IocChannelSetTimeoutCommand: 45 case IoctlCommand::IocChannelSetTimeoutCommand:
46 return ChannelSetTimeout(input, output); 46 return ChannelSetTimeout(input, output);
47 case IoctlCommand::IocChannelSetTimeslice:
48 return ChannelSetTimeslice(input, output);
47 default: 49 default:
48 break; 50 break;
49 } 51 }
@@ -228,4 +230,14 @@ u32 nvhost_gpu::ChannelSetTimeout(const std::vector<u8>& input, std::vector<u8>&
228 return 0; 230 return 0;
229} 231}
230 232
233u32 nvhost_gpu::ChannelSetTimeslice(const std::vector<u8>& input, std::vector<u8>& output) {
234 IoctlSetTimeslice params{};
235 std::memcpy(&params, input.data(), sizeof(IoctlSetTimeslice));
236 LOG_INFO(Service_NVDRV, "called, timeslice=0x{:X}", params.timeslice);
237
238 channel_timeslice = params.timeslice;
239
240 return 0;
241}
242
231} // namespace Service::Nvidia::Devices 243} // namespace Service::Nvidia::Devices
diff --git a/src/core/hle/service/nvdrv/devices/nvhost_gpu.h b/src/core/hle/service/nvdrv/devices/nvhost_gpu.h
index d056dd046..2ac74743f 100644
--- a/src/core/hle/service/nvdrv/devices/nvhost_gpu.h
+++ b/src/core/hle/service/nvdrv/devices/nvhost_gpu.h
@@ -48,6 +48,7 @@ private:
48 IocAllocObjCtxCommand = 0xC0104809, 48 IocAllocObjCtxCommand = 0xC0104809,
49 IocChannelGetWaitbaseCommand = 0xC0080003, 49 IocChannelGetWaitbaseCommand = 0xC0080003,
50 IocChannelSetTimeoutCommand = 0x40044803, 50 IocChannelSetTimeoutCommand = 0x40044803,
51 IocChannelSetTimeslice = 0xC004481D,
51 }; 52 };
52 53
53 enum class CtxObjects : u32_le { 54 enum class CtxObjects : u32_le {
@@ -101,6 +102,11 @@ private:
101 static_assert(sizeof(IoctlChannelSetPriority) == 4, 102 static_assert(sizeof(IoctlChannelSetPriority) == 4,
102 "IoctlChannelSetPriority is incorrect size"); 103 "IoctlChannelSetPriority is incorrect size");
103 104
105 struct IoctlSetTimeslice {
106 u32_le timeslice;
107 };
108 static_assert(sizeof(IoctlSetTimeslice) == 4, "IoctlSetTimeslice is incorrect size");
109
104 struct IoctlEventIdControl { 110 struct IoctlEventIdControl {
105 u32_le cmd; // 0=disable, 1=enable, 2=clear 111 u32_le cmd; // 0=disable, 1=enable, 2=clear
106 u32_le id; 112 u32_le id;
@@ -174,6 +180,7 @@ private:
174 u64_le user_data{}; 180 u64_le user_data{};
175 IoctlZCullBind zcull_params{}; 181 IoctlZCullBind zcull_params{};
176 u32_le channel_priority{}; 182 u32_le channel_priority{};
183 u32_le channel_timeslice{};
177 184
178 u32 SetNVMAPfd(const std::vector<u8>& input, std::vector<u8>& output); 185 u32 SetNVMAPfd(const std::vector<u8>& input, std::vector<u8>& output);
179 u32 SetClientData(const std::vector<u8>& input, std::vector<u8>& output); 186 u32 SetClientData(const std::vector<u8>& input, std::vector<u8>& output);
@@ -188,6 +195,7 @@ private:
188 const std::vector<u8>& input2, IoctlVersion version); 195 const std::vector<u8>& input2, IoctlVersion version);
189 u32 GetWaitbase(const std::vector<u8>& input, std::vector<u8>& output); 196 u32 GetWaitbase(const std::vector<u8>& input, std::vector<u8>& output);
190 u32 ChannelSetTimeout(const std::vector<u8>& input, std::vector<u8>& output); 197 u32 ChannelSetTimeout(const std::vector<u8>& input, std::vector<u8>& output);
198 u32 ChannelSetTimeslice(const std::vector<u8>& input, std::vector<u8>& output);
191 199
192 std::shared_ptr<nvmap> nvmap_dev; 200 std::shared_ptr<nvmap> nvmap_dev;
193 u32 assigned_syncpoints{}; 201 u32 assigned_syncpoints{};
diff --git a/src/core/settings.h b/src/core/settings.h
index e1a9a0ffa..f837d3fbc 100644
--- a/src/core/settings.h
+++ b/src/core/settings.h
@@ -429,6 +429,7 @@ struct Values {
429 int vulkan_device; 429 int vulkan_device;
430 430
431 float resolution_factor; 431 float resolution_factor;
432 int aspect_ratio;
432 bool use_frame_limit; 433 bool use_frame_limit;
433 u16 frame_limit; 434 u16 frame_limit;
434 bool use_disk_shader_cache; 435 bool use_disk_shader_cache;
diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt
index db9332d00..4b0c6346f 100644
--- a/src/video_core/CMakeLists.txt
+++ b/src/video_core/CMakeLists.txt
@@ -37,6 +37,7 @@ add_library(video_core STATIC
37 memory_manager.h 37 memory_manager.h
38 morton.cpp 38 morton.cpp
39 morton.h 39 morton.h
40 query_cache.h
40 rasterizer_accelerated.cpp 41 rasterizer_accelerated.cpp
41 rasterizer_accelerated.h 42 rasterizer_accelerated.h
42 rasterizer_cache.cpp 43 rasterizer_cache.cpp
@@ -74,6 +75,8 @@ add_library(video_core STATIC
74 renderer_opengl/gl_stream_buffer.h 75 renderer_opengl/gl_stream_buffer.h
75 renderer_opengl/gl_texture_cache.cpp 76 renderer_opengl/gl_texture_cache.cpp
76 renderer_opengl/gl_texture_cache.h 77 renderer_opengl/gl_texture_cache.h
78 renderer_opengl/gl_query_cache.cpp
79 renderer_opengl/gl_query_cache.h
77 renderer_opengl/maxwell_to_gl.h 80 renderer_opengl/maxwell_to_gl.h
78 renderer_opengl/renderer_opengl.cpp 81 renderer_opengl/renderer_opengl.cpp
79 renderer_opengl/renderer_opengl.h 82 renderer_opengl/renderer_opengl.h
@@ -177,6 +180,8 @@ if (ENABLE_VULKAN)
177 renderer_vulkan/vk_memory_manager.h 180 renderer_vulkan/vk_memory_manager.h
178 renderer_vulkan/vk_pipeline_cache.cpp 181 renderer_vulkan/vk_pipeline_cache.cpp
179 renderer_vulkan/vk_pipeline_cache.h 182 renderer_vulkan/vk_pipeline_cache.h
183 renderer_vulkan/vk_query_cache.cpp
184 renderer_vulkan/vk_query_cache.h
180 renderer_vulkan/vk_rasterizer.cpp 185 renderer_vulkan/vk_rasterizer.cpp
181 renderer_vulkan/vk_rasterizer.h 186 renderer_vulkan/vk_rasterizer.h
182 renderer_vulkan/vk_renderpass_cache.cpp 187 renderer_vulkan/vk_renderpass_cache.cpp
diff --git a/src/video_core/engines/maxwell_3d.cpp b/src/video_core/engines/maxwell_3d.cpp
index 0b3e8749b..b28de1092 100644
--- a/src/video_core/engines/maxwell_3d.cpp
+++ b/src/video_core/engines/maxwell_3d.cpp
@@ -4,6 +4,7 @@
4 4
5#include <cinttypes> 5#include <cinttypes>
6#include <cstring> 6#include <cstring>
7#include <optional>
7#include "common/assert.h" 8#include "common/assert.h"
8#include "core/core.h" 9#include "core/core.h"
9#include "core/core_timing.h" 10#include "core/core_timing.h"
@@ -16,6 +17,8 @@
16 17
17namespace Tegra::Engines { 18namespace Tegra::Engines {
18 19
20using VideoCore::QueryType;
21
19/// First register id that is actually a Macro call. 22/// First register id that is actually a Macro call.
20constexpr u32 MacroRegistersStart = 0xE00; 23constexpr u32 MacroRegistersStart = 0xE00;
21 24
@@ -400,6 +403,10 @@ void Maxwell3D::CallMethod(const GPU::MethodCall& method_call) {
400 ProcessQueryCondition(); 403 ProcessQueryCondition();
401 break; 404 break;
402 } 405 }
406 case MAXWELL3D_REG_INDEX(counter_reset): {
407 ProcessCounterReset();
408 break;
409 }
403 case MAXWELL3D_REG_INDEX(sync_info): { 410 case MAXWELL3D_REG_INDEX(sync_info): {
404 ProcessSyncPoint(); 411 ProcessSyncPoint();
405 break; 412 break;
@@ -482,7 +489,7 @@ void Maxwell3D::FlushMMEInlineDraw() {
482 489
483 const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed; 490 const bool is_indexed = mme_draw.current_mode == MMEDrawMode::Indexed;
484 if (ShouldExecute()) { 491 if (ShouldExecute()) {
485 rasterizer.DrawMultiBatch(is_indexed); 492 rasterizer.Draw(is_indexed, true);
486 } 493 }
487 494
488 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if 495 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
@@ -544,40 +551,28 @@ void Maxwell3D::ProcessQueryGet() {
544 "Units other than CROP are unimplemented"); 551 "Units other than CROP are unimplemented");
545 552
546 switch (regs.query.query_get.operation) { 553 switch (regs.query.query_get.operation) {
547 case Regs::QueryOperation::Release: { 554 case Regs::QueryOperation::Release:
548 const u64 result = regs.query.query_sequence; 555 StampQueryResult(regs.query.query_sequence, regs.query.query_get.short_query == 0);
549 StampQueryResult(result, regs.query.query_get.short_query == 0);
550 break; 556 break;
551 } 557 case Regs::QueryOperation::Acquire:
552 case Regs::QueryOperation::Acquire: { 558 // TODO(Blinkhawk): Under this operation, the GPU waits for the CPU to write a value that
553 // Todo(Blinkhawk): Under this operation, the GPU waits for the CPU 559 // matches the current payload.
554 // to write a value that matches the current payload.
555 UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE"); 560 UNIMPLEMENTED_MSG("Unimplemented query operation ACQUIRE");
556 break; 561 break;
557 } 562 case Regs::QueryOperation::Counter:
558 case Regs::QueryOperation::Counter: { 563 if (const std::optional<u64> result = GetQueryResult()) {
559 u64 result{}; 564 // If the query returns an empty optional it means it's cached and deferred.
560 switch (regs.query.query_get.select) { 565 // In this case we have a non-empty result, so we stamp it immediately.
561 case Regs::QuerySelect::Zero: 566 StampQueryResult(*result, regs.query.query_get.short_query == 0);
562 result = 0;
563 break;
564 default:
565 result = 1;
566 UNIMPLEMENTED_MSG("Unimplemented query select type {}",
567 static_cast<u32>(regs.query.query_get.select.Value()));
568 } 567 }
569 StampQueryResult(result, regs.query.query_get.short_query == 0);
570 break; 568 break;
571 } 569 case Regs::QueryOperation::Trap:
572 case Regs::QueryOperation::Trap: {
573 UNIMPLEMENTED_MSG("Unimplemented query operation TRAP"); 570 UNIMPLEMENTED_MSG("Unimplemented query operation TRAP");
574 break; 571 break;
575 } 572 default:
576 default: {
577 UNIMPLEMENTED_MSG("Unknown query operation"); 573 UNIMPLEMENTED_MSG("Unknown query operation");
578 break; 574 break;
579 } 575 }
580 }
581} 576}
582 577
583void Maxwell3D::ProcessQueryCondition() { 578void Maxwell3D::ProcessQueryCondition() {
@@ -593,20 +588,20 @@ void Maxwell3D::ProcessQueryCondition() {
593 } 588 }
594 case Regs::ConditionMode::ResNonZero: { 589 case Regs::ConditionMode::ResNonZero: {
595 Regs::QueryCompare cmp; 590 Regs::QueryCompare cmp;
596 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 591 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
597 execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U; 592 execute_on = cmp.initial_sequence != 0U && cmp.initial_mode != 0U;
598 break; 593 break;
599 } 594 }
600 case Regs::ConditionMode::Equal: { 595 case Regs::ConditionMode::Equal: {
601 Regs::QueryCompare cmp; 596 Regs::QueryCompare cmp;
602 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 597 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
603 execute_on = 598 execute_on =
604 cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode; 599 cmp.initial_sequence == cmp.current_sequence && cmp.initial_mode == cmp.current_mode;
605 break; 600 break;
606 } 601 }
607 case Regs::ConditionMode::NotEqual: { 602 case Regs::ConditionMode::NotEqual: {
608 Regs::QueryCompare cmp; 603 Regs::QueryCompare cmp;
609 memory_manager.ReadBlockUnsafe(condition_address, &cmp, sizeof(cmp)); 604 memory_manager.ReadBlock(condition_address, &cmp, sizeof(cmp));
610 execute_on = 605 execute_on =
611 cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode; 606 cmp.initial_sequence != cmp.current_sequence || cmp.initial_mode != cmp.current_mode;
612 break; 607 break;
@@ -619,6 +614,18 @@ void Maxwell3D::ProcessQueryCondition() {
619 } 614 }
620} 615}
621 616
617void Maxwell3D::ProcessCounterReset() {
618 switch (regs.counter_reset) {
619 case Regs::CounterReset::SampleCnt:
620 rasterizer.ResetCounter(QueryType::SamplesPassed);
621 break;
622 default:
623 LOG_WARNING(Render_OpenGL, "Unimplemented counter reset={}",
624 static_cast<int>(regs.counter_reset));
625 break;
626 }
627}
628
622void Maxwell3D::ProcessSyncPoint() { 629void Maxwell3D::ProcessSyncPoint() {
623 const u32 sync_point = regs.sync_info.sync_point.Value(); 630 const u32 sync_point = regs.sync_info.sync_point.Value();
624 const u32 increment = regs.sync_info.increment.Value(); 631 const u32 increment = regs.sync_info.increment.Value();
@@ -647,7 +654,7 @@ void Maxwell3D::DrawArrays() {
647 654
648 const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count}; 655 const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
649 if (ShouldExecute()) { 656 if (ShouldExecute()) {
650 rasterizer.DrawBatch(is_indexed); 657 rasterizer.Draw(is_indexed, false);
651 } 658 }
652 659
653 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if 660 // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
@@ -661,6 +668,22 @@ void Maxwell3D::DrawArrays() {
661 } 668 }
662} 669}
663 670
671std::optional<u64> Maxwell3D::GetQueryResult() {
672 switch (regs.query.query_get.select) {
673 case Regs::QuerySelect::Zero:
674 return 0;
675 case Regs::QuerySelect::SamplesPassed:
676 // Deferred.
677 rasterizer.Query(regs.query.QueryAddress(), VideoCore::QueryType::SamplesPassed,
678 system.GPU().GetTicks());
679 return {};
680 default:
681 UNIMPLEMENTED_MSG("Unimplemented query select type {}",
682 static_cast<u32>(regs.query.query_get.select.Value()));
683 return 1;
684 }
685}
686
664void Maxwell3D::ProcessCBBind(std::size_t stage_index) { 687void Maxwell3D::ProcessCBBind(std::size_t stage_index) {
665 // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage. 688 // Bind the buffer currently in CB_ADDRESS to the specified index in the desired shader stage.
666 auto& shader = state.shader_stages[stage_index]; 689 auto& shader = state.shader_stages[stage_index];
diff --git a/src/video_core/engines/maxwell_3d.h b/src/video_core/engines/maxwell_3d.h
index 0a2af54e5..6ea7cc6a5 100644
--- a/src/video_core/engines/maxwell_3d.h
+++ b/src/video_core/engines/maxwell_3d.h
@@ -6,6 +6,7 @@
6 6
7#include <array> 7#include <array>
8#include <bitset> 8#include <bitset>
9#include <optional>
9#include <type_traits> 10#include <type_traits>
10#include <unordered_map> 11#include <unordered_map>
11#include <vector> 12#include <vector>
@@ -409,6 +410,27 @@ public:
409 Linear = 1, 410 Linear = 1,
410 }; 411 };
411 412
413 enum class CounterReset : u32 {
414 SampleCnt = 0x01,
415 Unk02 = 0x02,
416 Unk03 = 0x03,
417 Unk04 = 0x04,
418 EmittedPrimitives = 0x10, // Not tested
419 Unk11 = 0x11,
420 Unk12 = 0x12,
421 Unk13 = 0x13,
422 Unk15 = 0x15,
423 Unk16 = 0x16,
424 Unk17 = 0x17,
425 Unk18 = 0x18,
426 Unk1A = 0x1A,
427 Unk1B = 0x1B,
428 Unk1C = 0x1C,
429 Unk1D = 0x1D,
430 Unk1E = 0x1E,
431 GeneratedPrimitives = 0x1F,
432 };
433
412 struct Cull { 434 struct Cull {
413 enum class FrontFace : u32 { 435 enum class FrontFace : u32 {
414 ClockWise = 0x0900, 436 ClockWise = 0x0900,
@@ -520,7 +542,7 @@ public:
520 BitField<12, 1, InvMemoryLayout> type; 542 BitField<12, 1, InvMemoryLayout> type;
521 } memory_layout; 543 } memory_layout;
522 union { 544 union {
523 BitField<0, 16, u32> array_mode; 545 BitField<0, 16, u32> layers;
524 BitField<16, 1, u32> volume; 546 BitField<16, 1, u32> volume;
525 }; 547 };
526 u32 layer_stride; 548 u32 layer_stride;
@@ -778,8 +800,12 @@ public:
778 800
779 u32 zeta_width; 801 u32 zeta_width;
780 u32 zeta_height; 802 u32 zeta_height;
803 union {
804 BitField<0, 16, u32> zeta_layers;
805 BitField<16, 1, u32> zeta_volume;
806 };
781 807
782 INSERT_UNION_PADDING_WORDS(0x27); 808 INSERT_UNION_PADDING_WORDS(0x26);
783 809
784 u32 depth_test_enable; 810 u32 depth_test_enable;
785 811
@@ -857,7 +883,7 @@ public:
857 BitField<7, 1, u32> c7; 883 BitField<7, 1, u32> c7;
858 } clip_distance_enabled; 884 } clip_distance_enabled;
859 885
860 INSERT_UNION_PADDING_WORDS(0x1); 886 u32 samplecnt_enable;
861 887
862 float point_size; 888 float point_size;
863 889
@@ -865,7 +891,11 @@ public:
865 891
866 u32 point_sprite_enable; 892 u32 point_sprite_enable;
867 893
868 INSERT_UNION_PADDING_WORDS(0x5); 894 INSERT_UNION_PADDING_WORDS(0x3);
895
896 CounterReset counter_reset;
897
898 INSERT_UNION_PADDING_WORDS(0x1);
869 899
870 u32 zeta_enable; 900 u32 zeta_enable;
871 901
@@ -1412,12 +1442,15 @@ private:
1412 /// Handles a write to the QUERY_GET register. 1442 /// Handles a write to the QUERY_GET register.
1413 void ProcessQueryGet(); 1443 void ProcessQueryGet();
1414 1444
1415 // Writes the query result accordingly 1445 /// Writes the query result accordingly.
1416 void StampQueryResult(u64 payload, bool long_query); 1446 void StampQueryResult(u64 payload, bool long_query);
1417 1447
1418 // Handles Conditional Rendering 1448 /// Handles conditional rendering.
1419 void ProcessQueryCondition(); 1449 void ProcessQueryCondition();
1420 1450
1451 /// Handles counter resets.
1452 void ProcessCounterReset();
1453
1421 /// Handles writes to syncing register. 1454 /// Handles writes to syncing register.
1422 void ProcessSyncPoint(); 1455 void ProcessSyncPoint();
1423 1456
@@ -1434,6 +1467,9 @@ private:
1434 1467
1435 // Handles a instance drawcall from MME 1468 // Handles a instance drawcall from MME
1436 void StepInstance(MMEDrawMode expected_mode, u32 count); 1469 void StepInstance(MMEDrawMode expected_mode, u32 count);
1470
1471 /// Returns a query's value or an empty object if the value will be deferred through a cache.
1472 std::optional<u64> GetQueryResult();
1437}; 1473};
1438 1474
1439#define ASSERT_REG_POSITION(field_name, position) \ 1475#define ASSERT_REG_POSITION(field_name, position) \
@@ -1475,6 +1511,7 @@ ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
1475ASSERT_REG_POSITION(rt_control, 0x487); 1511ASSERT_REG_POSITION(rt_control, 0x487);
1476ASSERT_REG_POSITION(zeta_width, 0x48a); 1512ASSERT_REG_POSITION(zeta_width, 0x48a);
1477ASSERT_REG_POSITION(zeta_height, 0x48b); 1513ASSERT_REG_POSITION(zeta_height, 0x48b);
1514ASSERT_REG_POSITION(zeta_layers, 0x48c);
1478ASSERT_REG_POSITION(depth_test_enable, 0x4B3); 1515ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
1479ASSERT_REG_POSITION(independent_blend_enable, 0x4B9); 1516ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
1480ASSERT_REG_POSITION(depth_write_enabled, 0x4BA); 1517ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
@@ -1499,8 +1536,10 @@ ASSERT_REG_POSITION(screen_y_control, 0x4EB);
1499ASSERT_REG_POSITION(vb_element_base, 0x50D); 1536ASSERT_REG_POSITION(vb_element_base, 0x50D);
1500ASSERT_REG_POSITION(vb_base_instance, 0x50E); 1537ASSERT_REG_POSITION(vb_base_instance, 0x50E);
1501ASSERT_REG_POSITION(clip_distance_enabled, 0x544); 1538ASSERT_REG_POSITION(clip_distance_enabled, 0x544);
1539ASSERT_REG_POSITION(samplecnt_enable, 0x545);
1502ASSERT_REG_POSITION(point_size, 0x546); 1540ASSERT_REG_POSITION(point_size, 0x546);
1503ASSERT_REG_POSITION(point_sprite_enable, 0x548); 1541ASSERT_REG_POSITION(point_sprite_enable, 0x548);
1542ASSERT_REG_POSITION(counter_reset, 0x54C);
1504ASSERT_REG_POSITION(zeta_enable, 0x54E); 1543ASSERT_REG_POSITION(zeta_enable, 0x54E);
1505ASSERT_REG_POSITION(multisample_control, 0x54F); 1544ASSERT_REG_POSITION(multisample_control, 0x54F);
1506ASSERT_REG_POSITION(condition, 0x554); 1545ASSERT_REG_POSITION(condition, 0x554);
diff --git a/src/video_core/gpu.cpp b/src/video_core/gpu.cpp
index 4419ab735..7d7137109 100644
--- a/src/video_core/gpu.cpp
+++ b/src/video_core/gpu.cpp
@@ -24,7 +24,7 @@ MICROPROFILE_DEFINE(GPU_wait, "GPU", "Wait for the GPU", MP_RGB(128, 128, 192));
24GPU::GPU(Core::System& system, VideoCore::RendererBase& renderer, bool is_async) 24GPU::GPU(Core::System& system, VideoCore::RendererBase& renderer, bool is_async)
25 : system{system}, renderer{renderer}, is_async{is_async} { 25 : system{system}, renderer{renderer}, is_async{is_async} {
26 auto& rasterizer{renderer.Rasterizer()}; 26 auto& rasterizer{renderer.Rasterizer()};
27 memory_manager = std::make_unique<Tegra::MemoryManager>(system); 27 memory_manager = std::make_unique<Tegra::MemoryManager>(system, rasterizer);
28 dma_pusher = std::make_unique<Tegra::DmaPusher>(*this); 28 dma_pusher = std::make_unique<Tegra::DmaPusher>(*this);
29 maxwell_3d = std::make_unique<Engines::Maxwell3D>(system, rasterizer, *memory_manager); 29 maxwell_3d = std::make_unique<Engines::Maxwell3D>(system, rasterizer, *memory_manager);
30 fermi_2d = std::make_unique<Engines::Fermi2D>(rasterizer); 30 fermi_2d = std::make_unique<Engines::Fermi2D>(rasterizer);
diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp
index f1d50be3e..f5d33f27a 100644
--- a/src/video_core/memory_manager.cpp
+++ b/src/video_core/memory_manager.cpp
@@ -11,10 +11,12 @@
11#include "core/memory.h" 11#include "core/memory.h"
12#include "video_core/gpu.h" 12#include "video_core/gpu.h"
13#include "video_core/memory_manager.h" 13#include "video_core/memory_manager.h"
14#include "video_core/rasterizer_interface.h"
14 15
15namespace Tegra { 16namespace Tegra {
16 17
17MemoryManager::MemoryManager(Core::System& system) : system{system} { 18MemoryManager::MemoryManager(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
19 : rasterizer{rasterizer}, system{system} {
18 std::fill(page_table.pointers.begin(), page_table.pointers.end(), nullptr); 20 std::fill(page_table.pointers.begin(), page_table.pointers.end(), nullptr);
19 std::fill(page_table.attributes.begin(), page_table.attributes.end(), 21 std::fill(page_table.attributes.begin(), page_table.attributes.end(),
20 Common::PageType::Unmapped); 22 Common::PageType::Unmapped);
@@ -83,6 +85,7 @@ GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
83 const auto cpu_addr = GpuToCpuAddress(gpu_addr); 85 const auto cpu_addr = GpuToCpuAddress(gpu_addr);
84 ASSERT(cpu_addr); 86 ASSERT(cpu_addr);
85 87
88 // Flush and invalidate through the GPU interface, to be asynchronous if possible.
86 system.GPU().FlushAndInvalidateRegion(cache_addr, aligned_size); 89 system.GPU().FlushAndInvalidateRegion(cache_addr, aligned_size);
87 90
88 UnmapRange(gpu_addr, aligned_size); 91 UnmapRange(gpu_addr, aligned_size);
@@ -242,7 +245,9 @@ void MemoryManager::ReadBlock(GPUVAddr src_addr, void* dest_buffer, const std::s
242 switch (page_table.attributes[page_index]) { 245 switch (page_table.attributes[page_index]) {
243 case Common::PageType::Memory: { 246 case Common::PageType::Memory: {
244 const u8* src_ptr{page_table.pointers[page_index] + page_offset}; 247 const u8* src_ptr{page_table.pointers[page_index] + page_offset};
245 system.GPU().FlushRegion(ToCacheAddr(src_ptr), copy_amount); 248 // Flush must happen on the rasterizer interface, such that memory is always synchronous
249 // when it is read (even when in asynchronous GPU mode). Fixes Dead Cells title menu.
250 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount);
246 std::memcpy(dest_buffer, src_ptr, copy_amount); 251 std::memcpy(dest_buffer, src_ptr, copy_amount);
247 break; 252 break;
248 } 253 }
@@ -292,7 +297,9 @@ void MemoryManager::WriteBlock(GPUVAddr dest_addr, const void* src_buffer, const
292 switch (page_table.attributes[page_index]) { 297 switch (page_table.attributes[page_index]) {
293 case Common::PageType::Memory: { 298 case Common::PageType::Memory: {
294 u8* dest_ptr{page_table.pointers[page_index] + page_offset}; 299 u8* dest_ptr{page_table.pointers[page_index] + page_offset};
295 system.GPU().InvalidateRegion(ToCacheAddr(dest_ptr), copy_amount); 300 // Invalidate must happen on the rasterizer interface, such that memory is always
301 // synchronous when it is written (even when in asynchronous GPU mode).
302 rasterizer.InvalidateRegion(ToCacheAddr(dest_ptr), copy_amount);
296 std::memcpy(dest_ptr, src_buffer, copy_amount); 303 std::memcpy(dest_ptr, src_buffer, copy_amount);
297 break; 304 break;
298 } 305 }
@@ -339,8 +346,10 @@ void MemoryManager::CopyBlock(GPUVAddr dest_addr, GPUVAddr src_addr, const std::
339 346
340 switch (page_table.attributes[page_index]) { 347 switch (page_table.attributes[page_index]) {
341 case Common::PageType::Memory: { 348 case Common::PageType::Memory: {
349 // Flush must happen on the rasterizer interface, such that memory is always synchronous
350 // when it is copied (even when in asynchronous GPU mode).
342 const u8* src_ptr{page_table.pointers[page_index] + page_offset}; 351 const u8* src_ptr{page_table.pointers[page_index] + page_offset};
343 system.GPU().FlushRegion(ToCacheAddr(src_ptr), copy_amount); 352 rasterizer.FlushRegion(ToCacheAddr(src_ptr), copy_amount);
344 WriteBlock(dest_addr, src_ptr, copy_amount); 353 WriteBlock(dest_addr, src_ptr, copy_amount);
345 break; 354 break;
346 } 355 }
diff --git a/src/video_core/memory_manager.h b/src/video_core/memory_manager.h
index 393447eb4..aea010087 100644
--- a/src/video_core/memory_manager.h
+++ b/src/video_core/memory_manager.h
@@ -10,6 +10,10 @@
10#include "common/common_types.h" 10#include "common/common_types.h"
11#include "common/page_table.h" 11#include "common/page_table.h"
12 12
13namespace VideoCore {
14class RasterizerInterface;
15}
16
13namespace Core { 17namespace Core {
14class System; 18class System;
15} 19}
@@ -47,7 +51,7 @@ struct VirtualMemoryArea {
47 51
48class MemoryManager final { 52class MemoryManager final {
49public: 53public:
50 explicit MemoryManager(Core::System& system); 54 explicit MemoryManager(Core::System& system, VideoCore::RasterizerInterface& rasterizer);
51 ~MemoryManager(); 55 ~MemoryManager();
52 56
53 GPUVAddr AllocateSpace(u64 size, u64 align); 57 GPUVAddr AllocateSpace(u64 size, u64 align);
@@ -172,6 +176,7 @@ private:
172 176
173 Common::PageTable page_table{page_bits}; 177 Common::PageTable page_table{page_bits};
174 VMAMap vma_map; 178 VMAMap vma_map;
179 VideoCore::RasterizerInterface& rasterizer;
175 180
176 Core::System& system; 181 Core::System& system;
177}; 182};
diff --git a/src/video_core/query_cache.h b/src/video_core/query_cache.h
new file mode 100644
index 000000000..e66054ed0
--- /dev/null
+++ b/src/video_core/query_cache.h
@@ -0,0 +1,359 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include <cstring>
10#include <iterator>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <unordered_map>
15#include <vector>
16
17#include "common/assert.h"
18#include "core/core.h"
19#include "video_core/engines/maxwell_3d.h"
20#include "video_core/gpu.h"
21#include "video_core/memory_manager.h"
22#include "video_core/rasterizer_interface.h"
23
24namespace VideoCommon {
25
26template <class QueryCache, class HostCounter>
27class CounterStreamBase {
28public:
29 explicit CounterStreamBase(QueryCache& cache, VideoCore::QueryType type)
30 : cache{cache}, type{type} {}
31
32 /// Updates the state of the stream, enabling or disabling as needed.
33 void Update(bool enabled) {
34 if (enabled) {
35 Enable();
36 } else {
37 Disable();
38 }
39 }
40
41 /// Resets the stream to zero. It doesn't disable the query after resetting.
42 void Reset() {
43 if (current) {
44 current->EndQuery();
45
46 // Immediately start a new query to avoid disabling its state.
47 current = cache.Counter(nullptr, type);
48 }
49 last = nullptr;
50 }
51
52 /// Returns the current counter slicing as needed.
53 std::shared_ptr<HostCounter> Current() {
54 if (!current) {
55 return nullptr;
56 }
57 current->EndQuery();
58 last = std::move(current);
59 current = cache.Counter(last, type);
60 return last;
61 }
62
63 /// Returns true when the counter stream is enabled.
64 bool IsEnabled() const {
65 return current != nullptr;
66 }
67
68private:
69 /// Enables the stream.
70 void Enable() {
71 if (current) {
72 return;
73 }
74 current = cache.Counter(last, type);
75 }
76
77 // Disables the stream.
78 void Disable() {
79 if (current) {
80 current->EndQuery();
81 }
82 last = std::exchange(current, nullptr);
83 }
84
85 QueryCache& cache;
86 const VideoCore::QueryType type;
87
88 std::shared_ptr<HostCounter> current;
89 std::shared_ptr<HostCounter> last;
90};
91
92template <class QueryCache, class CachedQuery, class CounterStream, class HostCounter,
93 class QueryPool>
94class QueryCacheBase {
95public:
96 explicit QueryCacheBase(Core::System& system, VideoCore::RasterizerInterface& rasterizer)
97 : system{system}, rasterizer{rasterizer}, streams{{CounterStream{
98 static_cast<QueryCache&>(*this),
99 VideoCore::QueryType::SamplesPassed}}} {}
100
101 void InvalidateRegion(CacheAddr addr, std::size_t size) {
102 std::unique_lock lock{mutex};
103 FlushAndRemoveRegion(addr, size);
104 }
105
106 void FlushRegion(CacheAddr addr, std::size_t size) {
107 std::unique_lock lock{mutex};
108 FlushAndRemoveRegion(addr, size);
109 }
110
111 /**
112 * Records a query in GPU mapped memory, potentially marked with a timestamp.
113 * @param gpu_addr GPU address to flush to when the mapped memory is read.
114 * @param type Query type, e.g. SamplesPassed.
115 * @param timestamp Timestamp, when empty the flushed query is assumed to be short.
116 */
117 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) {
118 std::unique_lock lock{mutex};
119 auto& memory_manager = system.GPU().MemoryManager();
120 const auto host_ptr = memory_manager.GetPointer(gpu_addr);
121
122 CachedQuery* query = TryGet(ToCacheAddr(host_ptr));
123 if (!query) {
124 const auto cpu_addr = memory_manager.GpuToCpuAddress(gpu_addr);
125 ASSERT_OR_EXECUTE(cpu_addr, return;);
126
127 query = Register(type, *cpu_addr, host_ptr, timestamp.has_value());
128 }
129
130 query->BindCounter(Stream(type).Current(), timestamp);
131 }
132
133 /// Updates counters from GPU state. Expected to be called once per draw, clear or dispatch.
134 void UpdateCounters() {
135 std::unique_lock lock{mutex};
136 const auto& regs = system.GPU().Maxwell3D().regs;
137 Stream(VideoCore::QueryType::SamplesPassed).Update(regs.samplecnt_enable);
138 }
139
140 /// Resets a counter to zero. It doesn't disable the query after resetting.
141 void ResetCounter(VideoCore::QueryType type) {
142 std::unique_lock lock{mutex};
143 Stream(type).Reset();
144 }
145
146 /// Disable all active streams. Expected to be called at the end of a command buffer.
147 void DisableStreams() {
148 std::unique_lock lock{mutex};
149 for (auto& stream : streams) {
150 stream.Update(false);
151 }
152 }
153
154 /// Returns a new host counter.
155 std::shared_ptr<HostCounter> Counter(std::shared_ptr<HostCounter> dependency,
156 VideoCore::QueryType type) {
157 return std::make_shared<HostCounter>(static_cast<QueryCache&>(*this), std::move(dependency),
158 type);
159 }
160
161 /// Returns the counter stream of the specified type.
162 CounterStream& Stream(VideoCore::QueryType type) {
163 return streams[static_cast<std::size_t>(type)];
164 }
165
166 /// Returns the counter stream of the specified type.
167 const CounterStream& Stream(VideoCore::QueryType type) const {
168 return streams[static_cast<std::size_t>(type)];
169 }
170
171protected:
172 std::array<QueryPool, VideoCore::NumQueryTypes> query_pools;
173
174private:
175 /// Flushes a memory range to guest memory and removes it from the cache.
176 void FlushAndRemoveRegion(CacheAddr addr, std::size_t size) {
177 const u64 addr_begin = static_cast<u64>(addr);
178 const u64 addr_end = addr_begin + static_cast<u64>(size);
179 const auto in_range = [addr_begin, addr_end](CachedQuery& query) {
180 const u64 cache_begin = query.GetCacheAddr();
181 const u64 cache_end = cache_begin + query.SizeInBytes();
182 return cache_begin < addr_end && addr_begin < cache_end;
183 };
184
185 const u64 page_end = addr_end >> PAGE_SHIFT;
186 for (u64 page = addr_begin >> PAGE_SHIFT; page <= page_end; ++page) {
187 const auto& it = cached_queries.find(page);
188 if (it == std::end(cached_queries)) {
189 continue;
190 }
191 auto& contents = it->second;
192 for (auto& query : contents) {
193 if (!in_range(query)) {
194 continue;
195 }
196 rasterizer.UpdatePagesCachedCount(query.CpuAddr(), query.SizeInBytes(), -1);
197 query.Flush();
198 }
199 contents.erase(std::remove_if(std::begin(contents), std::end(contents), in_range),
200 std::end(contents));
201 }
202 }
203
204 /// Registers the passed parameters as cached and returns a pointer to the stored cached query.
205 CachedQuery* Register(VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr, bool timestamp) {
206 rasterizer.UpdatePagesCachedCount(cpu_addr, CachedQuery::SizeInBytes(timestamp), 1);
207 const u64 page = static_cast<u64>(ToCacheAddr(host_ptr)) >> PAGE_SHIFT;
208 return &cached_queries[page].emplace_back(static_cast<QueryCache&>(*this), type, cpu_addr,
209 host_ptr);
210 }
211
212 /// Tries to a get a cached query. Returns nullptr on failure.
213 CachedQuery* TryGet(CacheAddr addr) {
214 const u64 page = static_cast<u64>(addr) >> PAGE_SHIFT;
215 const auto it = cached_queries.find(page);
216 if (it == std::end(cached_queries)) {
217 return nullptr;
218 }
219 auto& contents = it->second;
220 const auto found =
221 std::find_if(std::begin(contents), std::end(contents),
222 [addr](auto& query) { return query.GetCacheAddr() == addr; });
223 return found != std::end(contents) ? &*found : nullptr;
224 }
225
226 static constexpr std::uintptr_t PAGE_SIZE = 4096;
227 static constexpr unsigned PAGE_SHIFT = 12;
228
229 Core::System& system;
230 VideoCore::RasterizerInterface& rasterizer;
231
232 std::recursive_mutex mutex;
233
234 std::unordered_map<u64, std::vector<CachedQuery>> cached_queries;
235
236 std::array<CounterStream, VideoCore::NumQueryTypes> streams;
237};
238
239template <class QueryCache, class HostCounter>
240class HostCounterBase {
241public:
242 explicit HostCounterBase(std::shared_ptr<HostCounter> dependency_)
243 : dependency{std::move(dependency_)}, depth{dependency ? (dependency->Depth() + 1) : 0} {
244 // Avoid nesting too many dependencies to avoid a stack overflow when these are deleted.
245 constexpr u64 depth_threshold = 96;
246 if (depth > depth_threshold) {
247 depth = 0;
248 base_result = dependency->Query();
249 dependency = nullptr;
250 }
251 }
252 virtual ~HostCounterBase() = default;
253
254 /// Returns the current value of the query.
255 u64 Query() {
256 if (result) {
257 return *result;
258 }
259
260 u64 value = BlockingQuery() + base_result;
261 if (dependency) {
262 value += dependency->Query();
263 dependency = nullptr;
264 }
265
266 result = value;
267 return *result;
268 }
269
270 /// Returns true when flushing this query will potentially wait.
271 bool WaitPending() const noexcept {
272 return result.has_value();
273 }
274
275 u64 Depth() const noexcept {
276 return depth;
277 }
278
279protected:
280 /// Returns the value of query from the backend API blocking as needed.
281 virtual u64 BlockingQuery() const = 0;
282
283private:
284 std::shared_ptr<HostCounter> dependency; ///< Counter to add to this value.
285 std::optional<u64> result; ///< Filled with the already returned value.
286 u64 depth; ///< Number of nested dependencies.
287 u64 base_result = 0; ///< Equivalent to nested dependencies value.
288};
289
290template <class HostCounter>
291class CachedQueryBase {
292public:
293 explicit CachedQueryBase(VAddr cpu_addr, u8* host_ptr)
294 : cpu_addr{cpu_addr}, host_ptr{host_ptr} {}
295 virtual ~CachedQueryBase() = default;
296
297 CachedQueryBase(CachedQueryBase&&) noexcept = default;
298 CachedQueryBase(const CachedQueryBase&) = delete;
299
300 CachedQueryBase& operator=(CachedQueryBase&&) noexcept = default;
301 CachedQueryBase& operator=(const CachedQueryBase&) = delete;
302
303 /// Flushes the query to guest memory.
304 virtual void Flush() {
305 // When counter is nullptr it means that it's just been reseted. We are supposed to write a
306 // zero in these cases.
307 const u64 value = counter ? counter->Query() : 0;
308 std::memcpy(host_ptr, &value, sizeof(u64));
309
310 if (timestamp) {
311 std::memcpy(host_ptr + TIMESTAMP_OFFSET, &*timestamp, sizeof(u64));
312 }
313 }
314
315 /// Binds a counter to this query.
316 void BindCounter(std::shared_ptr<HostCounter> counter_, std::optional<u64> timestamp_) {
317 if (counter) {
318 // If there's an old counter set it means the query is being rewritten by the game.
319 // To avoid losing the data forever, flush here.
320 Flush();
321 }
322 counter = std::move(counter_);
323 timestamp = timestamp_;
324 }
325
326 VAddr CpuAddr() const noexcept {
327 return cpu_addr;
328 }
329
330 CacheAddr GetCacheAddr() const noexcept {
331 return ToCacheAddr(host_ptr);
332 }
333
334 u64 SizeInBytes() const noexcept {
335 return SizeInBytes(timestamp.has_value());
336 }
337
338 static constexpr u64 SizeInBytes(bool with_timestamp) noexcept {
339 return with_timestamp ? LARGE_QUERY_SIZE : SMALL_QUERY_SIZE;
340 }
341
342protected:
343 /// Returns true when querying the counter may potentially block.
344 bool WaitPending() const noexcept {
345 return counter && counter->WaitPending();
346 }
347
348private:
349 static constexpr std::size_t SMALL_QUERY_SIZE = 8; // Query size without timestamp.
350 static constexpr std::size_t LARGE_QUERY_SIZE = 16; // Query size with timestamp.
351 static constexpr std::intptr_t TIMESTAMP_OFFSET = 8; // Timestamp offset in a large query.
352
353 VAddr cpu_addr; ///< Guest CPU address.
354 u8* host_ptr; ///< Writable host pointer.
355 std::shared_ptr<HostCounter> counter; ///< Host counter to query, owns the dependency tree.
356 std::optional<u64> timestamp; ///< Timestamp to flush to guest memory.
357};
358
359} // namespace VideoCommon
diff --git a/src/video_core/rasterizer_interface.h b/src/video_core/rasterizer_interface.h
index c586cd6fe..f18eaf4bc 100644
--- a/src/video_core/rasterizer_interface.h
+++ b/src/video_core/rasterizer_interface.h
@@ -6,6 +6,7 @@
6 6
7#include <atomic> 7#include <atomic>
8#include <functional> 8#include <functional>
9#include <optional>
9#include "common/common_types.h" 10#include "common/common_types.h"
10#include "video_core/engines/fermi_2d.h" 11#include "video_core/engines/fermi_2d.h"
11#include "video_core/gpu.h" 12#include "video_core/gpu.h"
@@ -17,6 +18,11 @@ class MemoryManager;
17 18
18namespace VideoCore { 19namespace VideoCore {
19 20
21enum class QueryType {
22 SamplesPassed,
23};
24constexpr std::size_t NumQueryTypes = 1;
25
20enum class LoadCallbackStage { 26enum class LoadCallbackStage {
21 Prepare, 27 Prepare,
22 Decompile, 28 Decompile,
@@ -29,11 +35,8 @@ class RasterizerInterface {
29public: 35public:
30 virtual ~RasterizerInterface() {} 36 virtual ~RasterizerInterface() {}
31 37
32 /// Draw the current batch of vertex arrays 38 /// Dispatches a draw invocation
33 virtual bool DrawBatch(bool is_indexed) = 0; 39 virtual void Draw(bool is_indexed, bool is_instanced) = 0;
34
35 /// Draw the current batch of multiple instances of vertex arrays
36 virtual bool DrawMultiBatch(bool is_indexed) = 0;
37 40
38 /// Clear the current framebuffer 41 /// Clear the current framebuffer
39 virtual void Clear() = 0; 42 virtual void Clear() = 0;
@@ -41,6 +44,12 @@ public:
41 /// Dispatches a compute shader invocation 44 /// Dispatches a compute shader invocation
42 virtual void DispatchCompute(GPUVAddr code_addr) = 0; 45 virtual void DispatchCompute(GPUVAddr code_addr) = 0;
43 46
47 /// Resets the counter of a query
48 virtual void ResetCounter(QueryType type) = 0;
49
50 /// Records a GPU query and caches it
51 virtual void Query(GPUVAddr gpu_addr, QueryType type, std::optional<u64> timestamp) = 0;
52
44 /// Notify rasterizer that all caches should be flushed to Switch memory 53 /// Notify rasterizer that all caches should be flushed to Switch memory
45 virtual void FlushAll() = 0; 54 virtual void FlushAll() = 0;
46 55
diff --git a/src/video_core/renderer_opengl/gl_query_cache.cpp b/src/video_core/renderer_opengl/gl_query_cache.cpp
new file mode 100644
index 000000000..f12e9f55f
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_query_cache.cpp
@@ -0,0 +1,120 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cstring>
7#include <memory>
8#include <unordered_map>
9#include <utility>
10#include <vector>
11
12#include <glad/glad.h>
13
14#include "common/assert.h"
15#include "core/core.h"
16#include "video_core/engines/maxwell_3d.h"
17#include "video_core/memory_manager.h"
18#include "video_core/renderer_opengl/gl_query_cache.h"
19#include "video_core/renderer_opengl/gl_rasterizer.h"
20
21namespace OpenGL {
22
23namespace {
24
25constexpr std::array<GLenum, VideoCore::NumQueryTypes> QueryTargets = {GL_SAMPLES_PASSED};
26
27constexpr GLenum GetTarget(VideoCore::QueryType type) {
28 return QueryTargets[static_cast<std::size_t>(type)];
29}
30
31} // Anonymous namespace
32
33QueryCache::QueryCache(Core::System& system, RasterizerOpenGL& gl_rasterizer)
34 : VideoCommon::QueryCacheBase<
35 QueryCache, CachedQuery, CounterStream, HostCounter,
36 std::vector<OGLQuery>>{system,
37 static_cast<VideoCore::RasterizerInterface&>(gl_rasterizer)},
38 gl_rasterizer{gl_rasterizer} {}
39
40QueryCache::~QueryCache() = default;
41
42OGLQuery QueryCache::AllocateQuery(VideoCore::QueryType type) {
43 auto& reserve = query_pools[static_cast<std::size_t>(type)];
44 OGLQuery query;
45 if (reserve.empty()) {
46 query.Create(GetTarget(type));
47 return query;
48 }
49
50 query = std::move(reserve.back());
51 reserve.pop_back();
52 return query;
53}
54
55void QueryCache::Reserve(VideoCore::QueryType type, OGLQuery&& query) {
56 query_pools[static_cast<std::size_t>(type)].push_back(std::move(query));
57}
58
59bool QueryCache::AnyCommandQueued() const noexcept {
60 return gl_rasterizer.AnyCommandQueued();
61}
62
63HostCounter::HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency,
64 VideoCore::QueryType type)
65 : VideoCommon::HostCounterBase<QueryCache, HostCounter>{std::move(dependency)}, cache{cache},
66 type{type}, query{cache.AllocateQuery(type)} {
67 glBeginQuery(GetTarget(type), query.handle);
68}
69
70HostCounter::~HostCounter() {
71 cache.Reserve(type, std::move(query));
72}
73
74void HostCounter::EndQuery() {
75 if (!cache.AnyCommandQueued()) {
76 // There are chances a query waited on without commands (glDraw, glClear, glDispatch). Not
77 // having any of these causes a lock. glFlush is considered a command, so we can safely wait
78 // for this. Insert to the OpenGL command stream a flush.
79 glFlush();
80 }
81 glEndQuery(GetTarget(type));
82}
83
84u64 HostCounter::BlockingQuery() const {
85 GLint64 value;
86 glGetQueryObjecti64v(query.handle, GL_QUERY_RESULT, &value);
87 return static_cast<u64>(value);
88}
89
90CachedQuery::CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr, u8* host_ptr)
91 : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr}, cache{&cache}, type{type} {}
92
93CachedQuery::CachedQuery(CachedQuery&& rhs) noexcept
94 : VideoCommon::CachedQueryBase<HostCounter>(std::move(rhs)), cache{rhs.cache}, type{rhs.type} {}
95
96CachedQuery& CachedQuery::operator=(CachedQuery&& rhs) noexcept {
97 VideoCommon::CachedQueryBase<HostCounter>::operator=(std::move(rhs));
98 cache = rhs.cache;
99 type = rhs.type;
100 return *this;
101}
102
103void CachedQuery::Flush() {
104 // Waiting for a query while another query of the same target is enabled locks Nvidia's driver.
105 // To avoid this disable and re-enable keeping the dependency stream.
106 // But we only have to do this if we have pending waits to be done.
107 auto& stream = cache->Stream(type);
108 const bool slice_counter = WaitPending() && stream.IsEnabled();
109 if (slice_counter) {
110 stream.Update(false);
111 }
112
113 VideoCommon::CachedQueryBase<HostCounter>::Flush();
114
115 if (slice_counter) {
116 stream.Update(true);
117 }
118}
119
120} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_query_cache.h b/src/video_core/renderer_opengl/gl_query_cache.h
new file mode 100644
index 000000000..d8e7052a1
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_query_cache.h
@@ -0,0 +1,78 @@
1// Copyright 2019 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <memory>
9#include <vector>
10
11#include "common/common_types.h"
12#include "video_core/query_cache.h"
13#include "video_core/rasterizer_interface.h"
14#include "video_core/renderer_opengl/gl_resource_manager.h"
15
16namespace Core {
17class System;
18}
19
20namespace OpenGL {
21
22class CachedQuery;
23class HostCounter;
24class QueryCache;
25class RasterizerOpenGL;
26
27using CounterStream = VideoCommon::CounterStreamBase<QueryCache, HostCounter>;
28
29class QueryCache final : public VideoCommon::QueryCacheBase<QueryCache, CachedQuery, CounterStream,
30 HostCounter, std::vector<OGLQuery>> {
31public:
32 explicit QueryCache(Core::System& system, RasterizerOpenGL& rasterizer);
33 ~QueryCache();
34
35 OGLQuery AllocateQuery(VideoCore::QueryType type);
36
37 void Reserve(VideoCore::QueryType type, OGLQuery&& query);
38
39 bool AnyCommandQueued() const noexcept;
40
41private:
42 RasterizerOpenGL& gl_rasterizer;
43};
44
45class HostCounter final : public VideoCommon::HostCounterBase<QueryCache, HostCounter> {
46public:
47 explicit HostCounter(QueryCache& cache, std::shared_ptr<HostCounter> dependency,
48 VideoCore::QueryType type);
49 ~HostCounter();
50
51 void EndQuery();
52
53private:
54 u64 BlockingQuery() const override;
55
56 QueryCache& cache;
57 const VideoCore::QueryType type;
58 OGLQuery query;
59};
60
61class CachedQuery final : public VideoCommon::CachedQueryBase<HostCounter> {
62public:
63 explicit CachedQuery(QueryCache& cache, VideoCore::QueryType type, VAddr cpu_addr,
64 u8* host_ptr);
65 CachedQuery(CachedQuery&& rhs) noexcept;
66 CachedQuery(const CachedQuery&) = delete;
67
68 CachedQuery& operator=(CachedQuery&& rhs) noexcept;
69 CachedQuery& operator=(const CachedQuery&) = delete;
70
71 void Flush() override;
72
73private:
74 QueryCache* cache;
75 VideoCore::QueryType type;
76};
77
78} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index b0eb14c8b..e1965fb21 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -25,6 +25,7 @@
25#include "video_core/engines/maxwell_3d.h" 25#include "video_core/engines/maxwell_3d.h"
26#include "video_core/engines/shader_type.h" 26#include "video_core/engines/shader_type.h"
27#include "video_core/memory_manager.h" 27#include "video_core/memory_manager.h"
28#include "video_core/renderer_opengl/gl_query_cache.h"
28#include "video_core/renderer_opengl/gl_rasterizer.h" 29#include "video_core/renderer_opengl/gl_rasterizer.h"
29#include "video_core/renderer_opengl/gl_shader_cache.h" 30#include "video_core/renderer_opengl/gl_shader_cache.h"
30#include "video_core/renderer_opengl/gl_shader_gen.h" 31#include "video_core/renderer_opengl/gl_shader_gen.h"
@@ -92,8 +93,8 @@ std::size_t GetConstBufferSize(const Tegra::Engines::ConstBufferInfo& buffer,
92RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window, 93RasterizerOpenGL::RasterizerOpenGL(Core::System& system, Core::Frontend::EmuWindow& emu_window,
93 ScreenInfo& info) 94 ScreenInfo& info)
94 : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device}, 95 : RasterizerAccelerated{system.Memory()}, texture_cache{system, *this, device},
95 shader_cache{*this, system, emu_window, device}, system{system}, screen_info{info}, 96 shader_cache{*this, system, emu_window, device}, query_cache{system, *this}, system{system},
96 buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} { 97 screen_info{info}, buffer_cache{*this, system, device, STREAM_BUFFER_SIZE} {
97 shader_program_manager = std::make_unique<GLShader::ProgramManager>(); 98 shader_program_manager = std::make_unique<GLShader::ProgramManager>();
98 state.draw.shader_program = 0; 99 state.draw.shader_program = 0;
99 state.Apply(); 100 state.Apply();
@@ -541,11 +542,16 @@ void RasterizerOpenGL::Clear() {
541 } else if (use_stencil) { 542 } else if (use_stencil) {
542 glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil); 543 glClearBufferiv(GL_STENCIL, 0, &regs.clear_stencil);
543 } 544 }
545
546 ++num_queued_commands;
544} 547}
545 548
546void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) { 549void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
547 MICROPROFILE_SCOPE(OpenGL_Drawing); 550 MICROPROFILE_SCOPE(OpenGL_Drawing);
548 auto& gpu = system.GPU().Maxwell3D(); 551 auto& gpu = system.GPU().Maxwell3D();
552 const auto& regs = gpu.regs;
553
554 query_cache.UpdateCounters();
549 555
550 SyncRasterizeEnable(state); 556 SyncRasterizeEnable(state);
551 SyncColorMask(); 557 SyncColorMask();
@@ -611,7 +617,7 @@ void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
611 617
612 // Setup shaders and their used resources. 618 // Setup shaders and their used resources.
613 texture_cache.GuardSamplers(true); 619 texture_cache.GuardSamplers(true);
614 const auto primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology); 620 const GLenum primitive_mode = MaxwellToGL::PrimitiveTopology(gpu.regs.draw.topology);
615 SetupShaders(primitive_mode); 621 SetupShaders(primitive_mode);
616 texture_cache.GuardSamplers(false); 622 texture_cache.GuardSamplers(false);
617 623
@@ -638,35 +644,47 @@ void RasterizerOpenGL::Draw(bool is_indexed, bool is_instanced) {
638 glTextureBarrier(); 644 glTextureBarrier();
639 } 645 }
640 646
647 ++num_queued_commands;
648
641 const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance); 649 const GLuint base_instance = static_cast<GLuint>(gpu.regs.vb_base_instance);
642 const GLsizei num_instances = 650 const GLsizei num_instances =
643 static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1); 651 static_cast<GLsizei>(is_instanced ? gpu.mme_draw.instance_count : 1);
644 if (is_indexed) { 652 if (is_indexed) {
645 const GLenum index_format = MaxwellToGL::IndexFormat(gpu.regs.index_array.format);
646 const GLint base_vertex = static_cast<GLint>(gpu.regs.vb_element_base); 653 const GLint base_vertex = static_cast<GLint>(gpu.regs.vb_element_base);
647 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.index_array.count); 654 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.index_array.count);
648 glDrawElementsInstancedBaseVertexBaseInstance( 655 const GLvoid* offset = reinterpret_cast<const GLvoid*>(index_buffer_offset);
649 primitive_mode, num_vertices, index_format, 656 const GLenum format = MaxwellToGL::IndexFormat(gpu.regs.index_array.format);
650 reinterpret_cast<const void*>(index_buffer_offset), num_instances, base_vertex, 657 if (num_instances == 1 && base_instance == 0 && base_vertex == 0) {
651 base_instance); 658 glDrawElements(primitive_mode, num_vertices, format, offset);
659 } else if (num_instances == 1 && base_instance == 0) {
660 glDrawElementsBaseVertex(primitive_mode, num_vertices, format, offset, base_vertex);
661 } else if (base_vertex == 0 && base_instance == 0) {
662 glDrawElementsInstanced(primitive_mode, num_vertices, format, offset, num_instances);
663 } else if (base_vertex == 0) {
664 glDrawElementsInstancedBaseInstance(primitive_mode, num_vertices, format, offset,
665 num_instances, base_instance);
666 } else if (base_instance == 0) {
667 glDrawElementsInstancedBaseVertex(primitive_mode, num_vertices, format, offset,
668 num_instances, base_vertex);
669 } else {
670 glDrawElementsInstancedBaseVertexBaseInstance(primitive_mode, num_vertices, format,
671 offset, num_instances, base_vertex,
672 base_instance);
673 }
652 } else { 674 } else {
653 const GLint base_vertex = static_cast<GLint>(gpu.regs.vertex_buffer.first); 675 const GLint base_vertex = static_cast<GLint>(gpu.regs.vertex_buffer.first);
654 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.vertex_buffer.count); 676 const GLsizei num_vertices = static_cast<GLsizei>(gpu.regs.vertex_buffer.count);
655 glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices, num_instances, 677 if (num_instances == 1 && base_instance == 0) {
656 base_instance); 678 glDrawArrays(primitive_mode, base_vertex, num_vertices);
679 } else if (base_instance == 0) {
680 glDrawArraysInstanced(primitive_mode, base_vertex, num_vertices, num_instances);
681 } else {
682 glDrawArraysInstancedBaseInstance(primitive_mode, base_vertex, num_vertices,
683 num_instances, base_instance);
684 }
657 } 685 }
658} 686}
659 687
660bool RasterizerOpenGL::DrawBatch(bool is_indexed) {
661 Draw(is_indexed, false);
662 return true;
663}
664
665bool RasterizerOpenGL::DrawMultiBatch(bool is_indexed) {
666 Draw(is_indexed, true);
667 return true;
668}
669
670void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) { 688void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {
671 if (device.HasBrokenCompute()) { 689 if (device.HasBrokenCompute()) {
672 return; 690 return;
@@ -707,6 +725,16 @@ void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {
707 state.ApplyProgramPipeline(); 725 state.ApplyProgramPipeline();
708 726
709 glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z); 727 glDispatchCompute(launch_desc.grid_dim_x, launch_desc.grid_dim_y, launch_desc.grid_dim_z);
728 ++num_queued_commands;
729}
730
731void RasterizerOpenGL::ResetCounter(VideoCore::QueryType type) {
732 query_cache.ResetCounter(type);
733}
734
735void RasterizerOpenGL::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
736 std::optional<u64> timestamp) {
737 query_cache.Query(gpu_addr, type, timestamp);
710} 738}
711 739
712void RasterizerOpenGL::FlushAll() {} 740void RasterizerOpenGL::FlushAll() {}
@@ -718,6 +746,7 @@ void RasterizerOpenGL::FlushRegion(CacheAddr addr, u64 size) {
718 } 746 }
719 texture_cache.FlushRegion(addr, size); 747 texture_cache.FlushRegion(addr, size);
720 buffer_cache.FlushRegion(addr, size); 748 buffer_cache.FlushRegion(addr, size);
749 query_cache.FlushRegion(addr, size);
721} 750}
722 751
723void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) { 752void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) {
@@ -728,6 +757,7 @@ void RasterizerOpenGL::InvalidateRegion(CacheAddr addr, u64 size) {
728 texture_cache.InvalidateRegion(addr, size); 757 texture_cache.InvalidateRegion(addr, size);
729 shader_cache.InvalidateRegion(addr, size); 758 shader_cache.InvalidateRegion(addr, size);
730 buffer_cache.InvalidateRegion(addr, size); 759 buffer_cache.InvalidateRegion(addr, size);
760 query_cache.InvalidateRegion(addr, size);
731} 761}
732 762
733void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { 763void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
@@ -738,10 +768,18 @@ void RasterizerOpenGL::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
738} 768}
739 769
740void RasterizerOpenGL::FlushCommands() { 770void RasterizerOpenGL::FlushCommands() {
771 // Only flush when we have commands queued to OpenGL.
772 if (num_queued_commands == 0) {
773 return;
774 }
775 num_queued_commands = 0;
741 glFlush(); 776 glFlush();
742} 777}
743 778
744void RasterizerOpenGL::TickFrame() { 779void RasterizerOpenGL::TickFrame() {
780 // Ticking a frame means that buffers will be swapped, calling glFlush implicitly.
781 num_queued_commands = 0;
782
745 buffer_cache.TickFrame(); 783 buffer_cache.TickFrame();
746} 784}
747 785
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 0501f3828..68abe9a21 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -24,6 +24,7 @@
24#include "video_core/renderer_opengl/gl_buffer_cache.h" 24#include "video_core/renderer_opengl/gl_buffer_cache.h"
25#include "video_core/renderer_opengl/gl_device.h" 25#include "video_core/renderer_opengl/gl_device.h"
26#include "video_core/renderer_opengl/gl_framebuffer_cache.h" 26#include "video_core/renderer_opengl/gl_framebuffer_cache.h"
27#include "video_core/renderer_opengl/gl_query_cache.h"
27#include "video_core/renderer_opengl/gl_resource_manager.h" 28#include "video_core/renderer_opengl/gl_resource_manager.h"
28#include "video_core/renderer_opengl/gl_sampler_cache.h" 29#include "video_core/renderer_opengl/gl_sampler_cache.h"
29#include "video_core/renderer_opengl/gl_shader_cache.h" 30#include "video_core/renderer_opengl/gl_shader_cache.h"
@@ -57,10 +58,11 @@ public:
57 ScreenInfo& info); 58 ScreenInfo& info);
58 ~RasterizerOpenGL() override; 59 ~RasterizerOpenGL() override;
59 60
60 bool DrawBatch(bool is_indexed) override; 61 void Draw(bool is_indexed, bool is_instanced) override;
61 bool DrawMultiBatch(bool is_indexed) override;
62 void Clear() override; 62 void Clear() override;
63 void DispatchCompute(GPUVAddr code_addr) override; 63 void DispatchCompute(GPUVAddr code_addr) override;
64 void ResetCounter(VideoCore::QueryType type) override;
65 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
64 void FlushAll() override; 66 void FlushAll() override;
65 void FlushRegion(CacheAddr addr, u64 size) override; 67 void FlushRegion(CacheAddr addr, u64 size) override;
66 void InvalidateRegion(CacheAddr addr, u64 size) override; 68 void InvalidateRegion(CacheAddr addr, u64 size) override;
@@ -75,6 +77,11 @@ public:
75 void LoadDiskResources(const std::atomic_bool& stop_loading, 77 void LoadDiskResources(const std::atomic_bool& stop_loading,
76 const VideoCore::DiskResourceLoadCallback& callback) override; 78 const VideoCore::DiskResourceLoadCallback& callback) override;
77 79
80 /// Returns true when there are commands queued to the OpenGL server.
81 bool AnyCommandQueued() const {
82 return num_queued_commands > 0;
83 }
84
78private: 85private:
79 /// Configures the color and depth framebuffer states. 86 /// Configures the color and depth framebuffer states.
80 void ConfigureFramebuffers(); 87 void ConfigureFramebuffers();
@@ -102,9 +109,6 @@ private:
102 void SetupGlobalMemory(u32 binding, const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr, 109 void SetupGlobalMemory(u32 binding, const GLShader::GlobalMemoryEntry& entry, GPUVAddr gpu_addr,
103 std::size_t size); 110 std::size_t size);
104 111
105 /// Syncs all the state, shaders, render targets and textures setting before a draw call.
106 void Draw(bool is_indexed, bool is_instanced);
107
108 /// Configures the current textures to use for the draw command. 112 /// Configures the current textures to use for the draw command.
109 void SetupDrawTextures(std::size_t stage_index, const Shader& shader); 113 void SetupDrawTextures(std::size_t stage_index, const Shader& shader);
110 114
@@ -180,10 +184,23 @@ private:
180 /// Syncs the alpha test state to match the guest state 184 /// Syncs the alpha test state to match the guest state
181 void SyncAlphaTest(); 185 void SyncAlphaTest();
182 186
183 /// Check for extension that are not strictly required 187 /// Check for extension that are not strictly required but are needed for correct emulation
184 /// but are needed for correct emulation
185 void CheckExtensions(); 188 void CheckExtensions();
186 189
190 std::size_t CalculateVertexArraysSize() const;
191
192 std::size_t CalculateIndexBufferSize() const;
193
194 /// Updates and returns a vertex array object representing current vertex format
195 GLuint SetupVertexFormat();
196
197 void SetupVertexBuffer(GLuint vao);
198 void SetupVertexInstances(GLuint vao);
199
200 GLintptr SetupIndexBuffer();
201
202 void SetupShaders(GLenum primitive_mode);
203
187 const Device device; 204 const Device device;
188 OpenGLState state; 205 OpenGLState state;
189 206
@@ -191,6 +208,7 @@ private:
191 ShaderCacheOpenGL shader_cache; 208 ShaderCacheOpenGL shader_cache;
192 SamplerCacheOpenGL sampler_cache; 209 SamplerCacheOpenGL sampler_cache;
193 FramebufferCacheOpenGL framebuffer_cache; 210 FramebufferCacheOpenGL framebuffer_cache;
211 QueryCache query_cache;
194 212
195 Core::System& system; 213 Core::System& system;
196 ScreenInfo& screen_info; 214 ScreenInfo& screen_info;
@@ -208,19 +226,8 @@ private:
208 BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER}; 226 BindBuffersRangePushBuffer bind_ubo_pushbuffer{GL_UNIFORM_BUFFER};
209 BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER}; 227 BindBuffersRangePushBuffer bind_ssbo_pushbuffer{GL_SHADER_STORAGE_BUFFER};
210 228
211 std::size_t CalculateVertexArraysSize() const; 229 /// Number of commands queued to the OpenGL driver. Reseted on flush.
212 230 std::size_t num_queued_commands = 0;
213 std::size_t CalculateIndexBufferSize() const;
214
215 /// Updates and returns a vertex array object representing current vertex format
216 GLuint SetupVertexFormat();
217
218 void SetupVertexBuffer(GLuint vao);
219 void SetupVertexInstances(GLuint vao);
220
221 GLintptr SetupIndexBuffer();
222
223 void SetupShaders(GLenum primitive_mode);
224}; 231};
225 232
226} // namespace OpenGL 233} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp
index 5c96c1d46..f0ddfb276 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.cpp
+++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp
@@ -207,4 +207,21 @@ void OGLFramebuffer::Release() {
207 handle = 0; 207 handle = 0;
208} 208}
209 209
210void OGLQuery::Create(GLenum target) {
211 if (handle != 0)
212 return;
213
214 MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
215 glCreateQueries(target, 1, &handle);
216}
217
218void OGLQuery::Release() {
219 if (handle == 0)
220 return;
221
222 MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
223 glDeleteQueries(1, &handle);
224 handle = 0;
225}
226
210} // namespace OpenGL 227} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.h b/src/video_core/renderer_opengl/gl_resource_manager.h
index 3a85a1d4c..514d1d165 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.h
+++ b/src/video_core/renderer_opengl/gl_resource_manager.h
@@ -266,4 +266,29 @@ public:
266 GLuint handle = 0; 266 GLuint handle = 0;
267}; 267};
268 268
269class OGLQuery : private NonCopyable {
270public:
271 OGLQuery() = default;
272
273 OGLQuery(OGLQuery&& o) noexcept : handle(std::exchange(o.handle, 0)) {}
274
275 ~OGLQuery() {
276 Release();
277 }
278
279 OGLQuery& operator=(OGLQuery&& o) noexcept {
280 Release();
281 handle = std::exchange(o.handle, 0);
282 return *this;
283 }
284
285 /// Creates a new internal OpenGL resource and stores the handle
286 void Create(GLenum target);
287
288 /// Deletes the internal OpenGL resource
289 void Release();
290
291 GLuint handle = 0;
292};
293
269} // namespace OpenGL 294} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_texture_cache.cpp b/src/video_core/renderer_opengl/gl_texture_cache.cpp
index c9d8aeca9..cf934b0d8 100644
--- a/src/video_core/renderer_opengl/gl_texture_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_texture_cache.cpp
@@ -261,6 +261,13 @@ CachedSurface::~CachedSurface() = default;
261void CachedSurface::DownloadTexture(std::vector<u8>& staging_buffer) { 261void CachedSurface::DownloadTexture(std::vector<u8>& staging_buffer) {
262 MICROPROFILE_SCOPE(OpenGL_Texture_Download); 262 MICROPROFILE_SCOPE(OpenGL_Texture_Download);
263 263
264 if (params.IsBuffer()) {
265 glGetNamedBufferSubData(texture_buffer.handle, 0,
266 static_cast<GLsizeiptr>(params.GetHostSizeInBytes()),
267 staging_buffer.data());
268 return;
269 }
270
264 SCOPE_EXIT({ glPixelStorei(GL_PACK_ROW_LENGTH, 0); }); 271 SCOPE_EXIT({ glPixelStorei(GL_PACK_ROW_LENGTH, 0); });
265 272
266 for (u32 level = 0; level < params.emulated_levels; ++level) { 273 for (u32 level = 0; level < params.emulated_levels; ++level) {
@@ -399,24 +406,36 @@ CachedSurfaceView::CachedSurfaceView(CachedSurface& surface, const ViewParams& p
399CachedSurfaceView::~CachedSurfaceView() = default; 406CachedSurfaceView::~CachedSurfaceView() = default;
400 407
401void CachedSurfaceView::Attach(GLenum attachment, GLenum target) const { 408void CachedSurfaceView::Attach(GLenum attachment, GLenum target) const {
402 ASSERT(params.num_layers == 1 && params.num_levels == 1); 409 ASSERT(params.num_levels == 1);
403 410
404 const auto& owner_params = surface.GetSurfaceParams(); 411 const GLuint texture = surface.GetTexture();
412 if (params.num_layers > 1) {
413 // Layered framebuffer attachments
414 UNIMPLEMENTED_IF(params.base_layer != 0);
415
416 switch (params.target) {
417 case SurfaceTarget::Texture2DArray:
418 glFramebufferTexture(target, attachment, texture, params.base_level);
419 break;
420 default:
421 UNIMPLEMENTED();
422 }
423 return;
424 }
405 425
406 switch (owner_params.target) { 426 const GLenum view_target = surface.GetTarget();
427 switch (surface.GetSurfaceParams().target) {
407 case SurfaceTarget::Texture1D: 428 case SurfaceTarget::Texture1D:
408 glFramebufferTexture1D(target, attachment, surface.GetTarget(), surface.GetTexture(), 429 glFramebufferTexture1D(target, attachment, view_target, texture, params.base_level);
409 params.base_level);
410 break; 430 break;
411 case SurfaceTarget::Texture2D: 431 case SurfaceTarget::Texture2D:
412 glFramebufferTexture2D(target, attachment, surface.GetTarget(), surface.GetTexture(), 432 glFramebufferTexture2D(target, attachment, view_target, texture, params.base_level);
413 params.base_level);
414 break; 433 break;
415 case SurfaceTarget::Texture1DArray: 434 case SurfaceTarget::Texture1DArray:
416 case SurfaceTarget::Texture2DArray: 435 case SurfaceTarget::Texture2DArray:
417 case SurfaceTarget::TextureCubemap: 436 case SurfaceTarget::TextureCubemap:
418 case SurfaceTarget::TextureCubeArray: 437 case SurfaceTarget::TextureCubeArray:
419 glFramebufferTextureLayer(target, attachment, surface.GetTexture(), params.base_level, 438 glFramebufferTextureLayer(target, attachment, texture, params.base_level,
420 params.base_layer); 439 params.base_layer);
421 break; 440 break;
422 default: 441 default:
diff --git a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp
index 8c49c66a7..ef66dd141 100644
--- a/src/video_core/renderer_vulkan/maxwell_to_vk.cpp
+++ b/src/video_core/renderer_vulkan/maxwell_to_vk.cpp
@@ -165,7 +165,7 @@ struct FormatTuple {
165 {vk::Format::eUndefined, {}}, // ASTC_2D_5X4 165 {vk::Format::eUndefined, {}}, // ASTC_2D_5X4
166 {vk::Format::eUndefined, {}}, // BGRA8_SRGB 166 {vk::Format::eUndefined, {}}, // BGRA8_SRGB
167 {vk::Format::eBc1RgbaSrgbBlock, {}}, // DXT1_SRGB 167 {vk::Format::eBc1RgbaSrgbBlock, {}}, // DXT1_SRGB
168 {vk::Format::eUndefined, {}}, // DXT23_SRGB 168 {vk::Format::eBc2SrgbBlock, {}}, // DXT23_SRGB
169 {vk::Format::eBc3SrgbBlock, {}}, // DXT45_SRGB 169 {vk::Format::eBc3SrgbBlock, {}}, // DXT45_SRGB
170 {vk::Format::eBc7SrgbBlock, {}}, // BC7U_SRGB 170 {vk::Format::eBc7SrgbBlock, {}}, // BC7U_SRGB
171 {vk::Format::eR4G4B4A4UnormPack16, Attachable}, // R4G4B4A4U 171 {vk::Format::eR4G4B4A4UnormPack16, Attachable}, // R4G4B4A4U
@@ -364,6 +364,8 @@ vk::Format VertexFormat(Maxwell::VertexAttribute::Type type, Maxwell::VertexAttr
364 return vk::Format::eR8G8B8A8Uint; 364 return vk::Format::eR8G8B8A8Uint;
365 case Maxwell::VertexAttribute::Size::Size_32: 365 case Maxwell::VertexAttribute::Size::Size_32:
366 return vk::Format::eR32Uint; 366 return vk::Format::eR32Uint;
367 case Maxwell::VertexAttribute::Size::Size_32_32_32_32:
368 return vk::Format::eR32G32B32A32Uint;
367 default: 369 default:
368 break; 370 break;
369 } 371 }
diff --git a/src/video_core/renderer_vulkan/vk_device.cpp b/src/video_core/renderer_vulkan/vk_device.cpp
index de712223e..886bde3b9 100644
--- a/src/video_core/renderer_vulkan/vk_device.cpp
+++ b/src/video_core/renderer_vulkan/vk_device.cpp
@@ -104,8 +104,11 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan
104 features.depthBiasClamp = true; 104 features.depthBiasClamp = true;
105 features.geometryShader = true; 105 features.geometryShader = true;
106 features.tessellationShader = true; 106 features.tessellationShader = true;
107 features.occlusionQueryPrecise = true;
107 features.fragmentStoresAndAtomics = true; 108 features.fragmentStoresAndAtomics = true;
108 features.shaderImageGatherExtended = true; 109 features.shaderImageGatherExtended = true;
110 features.shaderStorageImageReadWithoutFormat =
111 is_shader_storage_img_read_without_format_supported;
109 features.shaderStorageImageWriteWithoutFormat = true; 112 features.shaderStorageImageWriteWithoutFormat = true;
110 features.textureCompressionASTC_LDR = is_optimal_astc_supported; 113 features.textureCompressionASTC_LDR = is_optimal_astc_supported;
111 114
@@ -117,6 +120,10 @@ bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instan
117 bit8_storage.uniformAndStorageBuffer8BitAccess = true; 120 bit8_storage.uniformAndStorageBuffer8BitAccess = true;
118 SetNext(next, bit8_storage); 121 SetNext(next, bit8_storage);
119 122
123 vk::PhysicalDeviceHostQueryResetFeaturesEXT host_query_reset;
124 host_query_reset.hostQueryReset = true;
125 SetNext(next, host_query_reset);
126
120 vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8; 127 vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8;
121 if (is_float16_supported) { 128 if (is_float16_supported) {
122 float16_int8.shaderFloat16 = true; 129 float16_int8.shaderFloat16 = true;
@@ -273,6 +280,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev
273 VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME, 280 VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
274 VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME, 281 VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME,
275 VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME, 282 VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME,
283 VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
276 }; 284 };
277 std::bitset<required_extensions.size()> available_extensions{}; 285 std::bitset<required_extensions.size()> available_extensions{};
278 286
@@ -340,6 +348,7 @@ bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDev
340 std::make_pair(features.depthBiasClamp, "depthBiasClamp"), 348 std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
341 std::make_pair(features.geometryShader, "geometryShader"), 349 std::make_pair(features.geometryShader, "geometryShader"),
342 std::make_pair(features.tessellationShader, "tessellationShader"), 350 std::make_pair(features.tessellationShader, "tessellationShader"),
351 std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
343 std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"), 352 std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
344 std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"), 353 std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
345 std::make_pair(features.shaderStorageImageWriteWithoutFormat, 354 std::make_pair(features.shaderStorageImageWriteWithoutFormat,
@@ -376,7 +385,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami
376 } 385 }
377 }; 386 };
378 387
379 extensions.reserve(13); 388 extensions.reserve(14);
380 extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); 389 extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
381 extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME); 390 extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME);
382 extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); 391 extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME);
@@ -384,6 +393,7 @@ std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynami
384 extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME); 393 extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME);
385 extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME); 394 extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME);
386 extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME); 395 extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME);
396 extensions.push_back(VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME);
387 397
388 [[maybe_unused]] const bool nsight = 398 [[maybe_unused]] const bool nsight =
389 std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED"); 399 std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED");
@@ -457,6 +467,8 @@ void VKDevice::SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceK
457 467
458void VKDevice::SetupFeatures(const vk::DispatchLoaderDynamic& dldi) { 468void VKDevice::SetupFeatures(const vk::DispatchLoaderDynamic& dldi) {
459 const auto supported_features{physical.getFeatures(dldi)}; 469 const auto supported_features{physical.getFeatures(dldi)};
470 is_shader_storage_img_read_without_format_supported =
471 supported_features.shaderStorageImageReadWithoutFormat;
460 is_optimal_astc_supported = IsOptimalAstcSupported(supported_features, dldi); 472 is_optimal_astc_supported = IsOptimalAstcSupported(supported_features, dldi);
461} 473}
462 474
@@ -531,6 +543,7 @@ std::unordered_map<vk::Format, vk::FormatProperties> VKDevice::GetFormatProperti
531 vk::Format::eBc6HUfloatBlock, 543 vk::Format::eBc6HUfloatBlock,
532 vk::Format::eBc6HSfloatBlock, 544 vk::Format::eBc6HSfloatBlock,
533 vk::Format::eBc1RgbaSrgbBlock, 545 vk::Format::eBc1RgbaSrgbBlock,
546 vk::Format::eBc2SrgbBlock,
534 vk::Format::eBc3SrgbBlock, 547 vk::Format::eBc3SrgbBlock,
535 vk::Format::eBc7SrgbBlock, 548 vk::Format::eBc7SrgbBlock,
536 vk::Format::eAstc4x4SrgbBlock, 549 vk::Format::eAstc4x4SrgbBlock,
diff --git a/src/video_core/renderer_vulkan/vk_device.h b/src/video_core/renderer_vulkan/vk_device.h
index 72603f9f6..2c27ad730 100644
--- a/src/video_core/renderer_vulkan/vk_device.h
+++ b/src/video_core/renderer_vulkan/vk_device.h
@@ -122,6 +122,11 @@ public:
122 return properties.limits.maxPushConstantsSize; 122 return properties.limits.maxPushConstantsSize;
123 } 123 }
124 124
125 /// Returns true if Shader storage Image Read Without Format supported.
126 bool IsShaderStorageImageReadWithoutFormatSupported() const {
127 return is_shader_storage_img_read_without_format_supported;
128 }
129
125 /// Returns true if ASTC is natively supported. 130 /// Returns true if ASTC is natively supported.
126 bool IsOptimalAstcSupported() const { 131 bool IsOptimalAstcSupported() const {
127 return is_optimal_astc_supported; 132 return is_optimal_astc_supported;
@@ -227,6 +232,8 @@ private:
227 bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted. 232 bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
228 bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer. 233 bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
229 bool nv_device_diagnostic_checkpoints{}; ///< Support for VK_NV_device_diagnostic_checkpoints. 234 bool nv_device_diagnostic_checkpoints{}; ///< Support for VK_NV_device_diagnostic_checkpoints.
235 bool is_shader_storage_img_read_without_format_supported{}; ///< Support for shader storage
236 ///< image read without format
230 237
231 // Telemetry parameters 238 // Telemetry parameters
232 std::string vendor_name; ///< Device's driver name. 239 std::string vendor_name; ///< Device's driver name.
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.cpp b/src/video_core/renderer_vulkan/vk_query_cache.cpp
new file mode 100644
index 000000000..ffbf60dda
--- /dev/null
+++ b/src/video_core/renderer_vulkan/vk_query_cache.cpp
@@ -0,0 +1,122 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <cstddef>
7#include <cstdint>
8#include <utility>
9#include <vector>
10
11#include "video_core/renderer_vulkan/declarations.h"
12#include "video_core/renderer_vulkan/vk_device.h"
13#include "video_core/renderer_vulkan/vk_query_cache.h"
14#include "video_core/renderer_vulkan/vk_resource_manager.h"
15#include "video_core/renderer_vulkan/vk_scheduler.h"
16
17namespace Vulkan {
18
19namespace {
20
21constexpr std::array QUERY_TARGETS = {vk::QueryType::eOcclusion};
22
23constexpr vk::QueryType GetTarget(VideoCore::QueryType type) {
24 return QUERY_TARGETS[static_cast<std::size_t>(type)];
25}
26
27} // Anonymous namespace
28
29QueryPool::QueryPool() : VKFencedPool{GROW_STEP} {}
30
31QueryPool::~QueryPool() = default;
32
33void QueryPool::Initialize(const VKDevice& device_, VideoCore::QueryType type_) {
34 device = &device_;
35 type = type_;
36}
37
38std::pair<vk::QueryPool, std::uint32_t> QueryPool::Commit(VKFence& fence) {
39 std::size_t index;
40 do {
41 index = CommitResource(fence);
42 } while (usage[index]);
43 usage[index] = true;
44
45 return {*pools[index / GROW_STEP], static_cast<std::uint32_t>(index % GROW_STEP)};
46}
47
48void QueryPool::Allocate(std::size_t begin, std::size_t end) {
49 usage.resize(end);
50
51 const auto dev = device->GetLogical();
52 const u32 size = static_cast<u32>(end - begin);
53 const vk::QueryPoolCreateInfo query_pool_ci({}, GetTarget(type), size, {});
54 pools.push_back(dev.createQueryPoolUnique(query_pool_ci, nullptr, device->GetDispatchLoader()));
55}
56
57void QueryPool::Reserve(std::pair<vk::QueryPool, std::uint32_t> query) {
58 const auto it =
59 std::find_if(std::begin(pools), std::end(pools),
60 [query_pool = query.first](auto& pool) { return query_pool == *pool; });
61 ASSERT(it != std::end(pools));
62
63 const std::ptrdiff_t pool_index = std::distance(std::begin(pools), it);
64 usage[pool_index * GROW_STEP + static_cast<std::ptrdiff_t>(query.second)] = false;
65}
66
67VKQueryCache::VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
68 const VKDevice& device, VKScheduler& scheduler)
69 : VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter,
70 QueryPool>{system, rasterizer},
71 device{device}, scheduler{scheduler} {
72 for (std::size_t i = 0; i < static_cast<std::size_t>(VideoCore::NumQueryTypes); ++i) {
73 query_pools[i].Initialize(device, static_cast<VideoCore::QueryType>(i));
74 }
75}
76
77VKQueryCache::~VKQueryCache() = default;
78
79std::pair<vk::QueryPool, std::uint32_t> VKQueryCache::AllocateQuery(VideoCore::QueryType type) {
80 return query_pools[static_cast<std::size_t>(type)].Commit(scheduler.GetFence());
81}
82
83void VKQueryCache::Reserve(VideoCore::QueryType type,
84 std::pair<vk::QueryPool, std::uint32_t> query) {
85 query_pools[static_cast<std::size_t>(type)].Reserve(query);
86}
87
88HostCounter::HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency,
89 VideoCore::QueryType type)
90 : VideoCommon::HostCounterBase<VKQueryCache, HostCounter>{std::move(dependency)}, cache{cache},
91 type{type}, query{cache.AllocateQuery(type)}, ticks{cache.Scheduler().Ticks()} {
92 const auto dev = cache.Device().GetLogical();
93 cache.Scheduler().Record([dev, query = query](vk::CommandBuffer cmdbuf, auto& dld) {
94 dev.resetQueryPoolEXT(query.first, query.second, 1, dld);
95 cmdbuf.beginQuery(query.first, query.second, vk::QueryControlFlagBits::ePrecise, dld);
96 });
97}
98
99HostCounter::~HostCounter() {
100 cache.Reserve(type, query);
101}
102
103void HostCounter::EndQuery() {
104 cache.Scheduler().Record([query = query](auto cmdbuf, auto& dld) {
105 cmdbuf.endQuery(query.first, query.second, dld);
106 });
107}
108
109u64 HostCounter::BlockingQuery() const {
110 if (ticks >= cache.Scheduler().Ticks()) {
111 cache.Scheduler().Flush();
112 }
113
114 const auto dev = cache.Device().GetLogical();
115 const auto& dld = cache.Device().GetDispatchLoader();
116 u64 value;
117 dev.getQueryPoolResults(query.first, query.second, 1, sizeof(value), &value, sizeof(value),
118 vk::QueryResultFlagBits::e64 | vk::QueryResultFlagBits::eWait, dld);
119 return value;
120}
121
122} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_query_cache.h b/src/video_core/renderer_vulkan/vk_query_cache.h
new file mode 100644
index 000000000..c3092ee96
--- /dev/null
+++ b/src/video_core/renderer_vulkan/vk_query_cache.h
@@ -0,0 +1,104 @@
1// Copyright 2020 yuzu Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <memory>
10#include <utility>
11#include <vector>
12
13#include "common/common_types.h"
14#include "video_core/query_cache.h"
15#include "video_core/renderer_vulkan/declarations.h"
16#include "video_core/renderer_vulkan/vk_resource_manager.h"
17
18namespace VideoCore {
19class RasterizerInterface;
20}
21
22namespace Vulkan {
23
24class CachedQuery;
25class HostCounter;
26class VKDevice;
27class VKQueryCache;
28class VKScheduler;
29
30using CounterStream = VideoCommon::CounterStreamBase<VKQueryCache, HostCounter>;
31
32class QueryPool final : public VKFencedPool {
33public:
34 explicit QueryPool();
35 ~QueryPool() override;
36
37 void Initialize(const VKDevice& device, VideoCore::QueryType type);
38
39 std::pair<vk::QueryPool, std::uint32_t> Commit(VKFence& fence);
40
41 void Reserve(std::pair<vk::QueryPool, std::uint32_t> query);
42
43protected:
44 void Allocate(std::size_t begin, std::size_t end) override;
45
46private:
47 static constexpr std::size_t GROW_STEP = 512;
48
49 const VKDevice* device = nullptr;
50 VideoCore::QueryType type = {};
51
52 std::vector<UniqueQueryPool> pools;
53 std::vector<bool> usage;
54};
55
56class VKQueryCache final
57 : public VideoCommon::QueryCacheBase<VKQueryCache, CachedQuery, CounterStream, HostCounter,
58 QueryPool> {
59public:
60 explicit VKQueryCache(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
61 const VKDevice& device, VKScheduler& scheduler);
62 ~VKQueryCache();
63
64 std::pair<vk::QueryPool, std::uint32_t> AllocateQuery(VideoCore::QueryType type);
65
66 void Reserve(VideoCore::QueryType type, std::pair<vk::QueryPool, std::uint32_t> query);
67
68 const VKDevice& Device() const noexcept {
69 return device;
70 }
71
72 VKScheduler& Scheduler() const noexcept {
73 return scheduler;
74 }
75
76private:
77 const VKDevice& device;
78 VKScheduler& scheduler;
79};
80
81class HostCounter final : public VideoCommon::HostCounterBase<VKQueryCache, HostCounter> {
82public:
83 explicit HostCounter(VKQueryCache& cache, std::shared_ptr<HostCounter> dependency,
84 VideoCore::QueryType type);
85 ~HostCounter();
86
87 void EndQuery();
88
89private:
90 u64 BlockingQuery() const override;
91
92 VKQueryCache& cache;
93 const VideoCore::QueryType type;
94 const std::pair<vk::QueryPool, std::uint32_t> query;
95 const u64 ticks;
96};
97
98class CachedQuery : public VideoCommon::CachedQueryBase<HostCounter> {
99public:
100 explicit CachedQuery(VKQueryCache&, VideoCore::QueryType, VAddr cpu_addr, u8* host_ptr)
101 : VideoCommon::CachedQueryBase<HostCounter>{cpu_addr, host_ptr} {}
102};
103
104} // namespace Vulkan
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.cpp b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
index aada38702..3bf86da87 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.cpp
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.cpp
@@ -289,25 +289,19 @@ RasterizerVulkan::RasterizerVulkan(Core::System& system, Core::Frontend::EmuWind
289 staging_pool), 289 staging_pool),
290 pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue), 290 pipeline_cache(system, *this, device, scheduler, descriptor_pool, update_descriptor_queue),
291 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool), 291 buffer_cache(*this, system, device, memory_manager, scheduler, staging_pool),
292 sampler_cache(device) {} 292 sampler_cache(device), query_cache(system, *this, device, scheduler) {
293 293 scheduler.SetQueryCache(query_cache);
294RasterizerVulkan::~RasterizerVulkan() = default;
295
296bool RasterizerVulkan::DrawBatch(bool is_indexed) {
297 Draw(is_indexed, false);
298 return true;
299} 294}
300 295
301bool RasterizerVulkan::DrawMultiBatch(bool is_indexed) { 296RasterizerVulkan::~RasterizerVulkan() = default;
302 Draw(is_indexed, true);
303 return true;
304}
305 297
306void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) { 298void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
307 MICROPROFILE_SCOPE(Vulkan_Drawing); 299 MICROPROFILE_SCOPE(Vulkan_Drawing);
308 300
309 FlushWork(); 301 FlushWork();
310 302
303 query_cache.UpdateCounters();
304
311 const auto& gpu = system.GPU().Maxwell3D(); 305 const auto& gpu = system.GPU().Maxwell3D();
312 GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)}; 306 GraphicsPipelineCacheKey key{GetFixedPipelineState(gpu.regs)};
313 307
@@ -362,6 +356,8 @@ void RasterizerVulkan::Draw(bool is_indexed, bool is_instanced) {
362void RasterizerVulkan::Clear() { 356void RasterizerVulkan::Clear() {
363 MICROPROFILE_SCOPE(Vulkan_Clearing); 357 MICROPROFILE_SCOPE(Vulkan_Clearing);
364 358
359 query_cache.UpdateCounters();
360
365 const auto& gpu = system.GPU().Maxwell3D(); 361 const auto& gpu = system.GPU().Maxwell3D();
366 if (!system.GPU().Maxwell3D().ShouldExecute()) { 362 if (!system.GPU().Maxwell3D().ShouldExecute()) {
367 return; 363 return;
@@ -429,6 +425,8 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) {
429 sampled_views.clear(); 425 sampled_views.clear();
430 image_views.clear(); 426 image_views.clear();
431 427
428 query_cache.UpdateCounters();
429
432 const auto& launch_desc = system.GPU().KeplerCompute().launch_description; 430 const auto& launch_desc = system.GPU().KeplerCompute().launch_description;
433 const ComputePipelineCacheKey key{ 431 const ComputePipelineCacheKey key{
434 code_addr, 432 code_addr,
@@ -471,17 +469,28 @@ void RasterizerVulkan::DispatchCompute(GPUVAddr code_addr) {
471 }); 469 });
472} 470}
473 471
472void RasterizerVulkan::ResetCounter(VideoCore::QueryType type) {
473 query_cache.ResetCounter(type);
474}
475
476void RasterizerVulkan::Query(GPUVAddr gpu_addr, VideoCore::QueryType type,
477 std::optional<u64> timestamp) {
478 query_cache.Query(gpu_addr, type, timestamp);
479}
480
474void RasterizerVulkan::FlushAll() {} 481void RasterizerVulkan::FlushAll() {}
475 482
476void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) { 483void RasterizerVulkan::FlushRegion(CacheAddr addr, u64 size) {
477 texture_cache.FlushRegion(addr, size); 484 texture_cache.FlushRegion(addr, size);
478 buffer_cache.FlushRegion(addr, size); 485 buffer_cache.FlushRegion(addr, size);
486 query_cache.FlushRegion(addr, size);
479} 487}
480 488
481void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) { 489void RasterizerVulkan::InvalidateRegion(CacheAddr addr, u64 size) {
482 texture_cache.InvalidateRegion(addr, size); 490 texture_cache.InvalidateRegion(addr, size);
483 pipeline_cache.InvalidateRegion(addr, size); 491 pipeline_cache.InvalidateRegion(addr, size);
484 buffer_cache.InvalidateRegion(addr, size); 492 buffer_cache.InvalidateRegion(addr, size);
493 query_cache.InvalidateRegion(addr, size);
485} 494}
486 495
487void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) { 496void RasterizerVulkan::FlushAndInvalidateRegion(CacheAddr addr, u64 size) {
@@ -602,33 +611,34 @@ bool RasterizerVulkan::WalkAttachmentOverlaps(const CachedSurfaceView& attachmen
602std::tuple<vk::Framebuffer, vk::Extent2D> RasterizerVulkan::ConfigureFramebuffers( 611std::tuple<vk::Framebuffer, vk::Extent2D> RasterizerVulkan::ConfigureFramebuffers(
603 vk::RenderPass renderpass) { 612 vk::RenderPass renderpass) {
604 FramebufferCacheKey key{renderpass, std::numeric_limits<u32>::max(), 613 FramebufferCacheKey key{renderpass, std::numeric_limits<u32>::max(),
605 std::numeric_limits<u32>::max()}; 614 std::numeric_limits<u32>::max(), std::numeric_limits<u32>::max()};
606 615
607 const auto MarkAsModifiedAndPush = [&](const View& view) { 616 const auto try_push = [&](const View& view) {
608 if (view == nullptr) { 617 if (!view) {
609 return false; 618 return false;
610 } 619 }
611 key.views.push_back(view->GetHandle()); 620 key.views.push_back(view->GetHandle());
612 key.width = std::min(key.width, view->GetWidth()); 621 key.width = std::min(key.width, view->GetWidth());
613 key.height = std::min(key.height, view->GetHeight()); 622 key.height = std::min(key.height, view->GetHeight());
623 key.layers = std::min(key.layers, view->GetNumLayers());
614 return true; 624 return true;
615 }; 625 };
616 626
617 for (std::size_t index = 0; index < std::size(color_attachments); ++index) { 627 for (std::size_t index = 0; index < std::size(color_attachments); ++index) {
618 if (MarkAsModifiedAndPush(color_attachments[index])) { 628 if (try_push(color_attachments[index])) {
619 texture_cache.MarkColorBufferInUse(index); 629 texture_cache.MarkColorBufferInUse(index);
620 } 630 }
621 } 631 }
622 if (MarkAsModifiedAndPush(zeta_attachment)) { 632 if (try_push(zeta_attachment)) {
623 texture_cache.MarkDepthBufferInUse(); 633 texture_cache.MarkDepthBufferInUse();
624 } 634 }
625 635
626 const auto [fbentry, is_cache_miss] = framebuffer_cache.try_emplace(key); 636 const auto [fbentry, is_cache_miss] = framebuffer_cache.try_emplace(key);
627 auto& framebuffer = fbentry->second; 637 auto& framebuffer = fbentry->second;
628 if (is_cache_miss) { 638 if (is_cache_miss) {
629 const vk::FramebufferCreateInfo framebuffer_ci({}, key.renderpass, 639 const vk::FramebufferCreateInfo framebuffer_ci(
630 static_cast<u32>(key.views.size()), 640 {}, key.renderpass, static_cast<u32>(key.views.size()), key.views.data(), key.width,
631 key.views.data(), key.width, key.height, 1); 641 key.height, key.layers);
632 const auto dev = device.GetLogical(); 642 const auto dev = device.GetLogical();
633 const auto& dld = device.GetDispatchLoader(); 643 const auto& dld = device.GetDispatchLoader();
634 framebuffer = dev.createFramebufferUnique(framebuffer_ci, nullptr, dld); 644 framebuffer = dev.createFramebufferUnique(framebuffer_ci, nullptr, dld);
diff --git a/src/video_core/renderer_vulkan/vk_rasterizer.h b/src/video_core/renderer_vulkan/vk_rasterizer.h
index 7be71e734..4dc8af6e8 100644
--- a/src/video_core/renderer_vulkan/vk_rasterizer.h
+++ b/src/video_core/renderer_vulkan/vk_rasterizer.h
@@ -24,6 +24,7 @@
24#include "video_core/renderer_vulkan/vk_descriptor_pool.h" 24#include "video_core/renderer_vulkan/vk_descriptor_pool.h"
25#include "video_core/renderer_vulkan/vk_memory_manager.h" 25#include "video_core/renderer_vulkan/vk_memory_manager.h"
26#include "video_core/renderer_vulkan/vk_pipeline_cache.h" 26#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
27#include "video_core/renderer_vulkan/vk_query_cache.h"
27#include "video_core/renderer_vulkan/vk_renderpass_cache.h" 28#include "video_core/renderer_vulkan/vk_renderpass_cache.h"
28#include "video_core/renderer_vulkan/vk_resource_manager.h" 29#include "video_core/renderer_vulkan/vk_resource_manager.h"
29#include "video_core/renderer_vulkan/vk_sampler_cache.h" 30#include "video_core/renderer_vulkan/vk_sampler_cache.h"
@@ -55,6 +56,7 @@ struct FramebufferCacheKey {
55 vk::RenderPass renderpass{}; 56 vk::RenderPass renderpass{};
56 u32 width = 0; 57 u32 width = 0;
57 u32 height = 0; 58 u32 height = 0;
59 u32 layers = 0;
58 ImageViewsPack views; 60 ImageViewsPack views;
59 61
60 std::size_t Hash() const noexcept { 62 std::size_t Hash() const noexcept {
@@ -65,12 +67,17 @@ struct FramebufferCacheKey {
65 } 67 }
66 boost::hash_combine(hash, width); 68 boost::hash_combine(hash, width);
67 boost::hash_combine(hash, height); 69 boost::hash_combine(hash, height);
70 boost::hash_combine(hash, layers);
68 return hash; 71 return hash;
69 } 72 }
70 73
71 bool operator==(const FramebufferCacheKey& rhs) const noexcept { 74 bool operator==(const FramebufferCacheKey& rhs) const noexcept {
72 return std::tie(renderpass, views, width, height) == 75 return std::tie(renderpass, views, width, height, layers) ==
73 std::tie(rhs.renderpass, rhs.views, rhs.width, rhs.height); 76 std::tie(rhs.renderpass, rhs.views, rhs.width, rhs.height, rhs.layers);
77 }
78
79 bool operator!=(const FramebufferCacheKey& rhs) const noexcept {
80 return !operator==(rhs);
74 } 81 }
75}; 82};
76 83
@@ -96,7 +103,7 @@ struct ImageView {
96 vk::ImageLayout* layout = nullptr; 103 vk::ImageLayout* layout = nullptr;
97}; 104};
98 105
99class RasterizerVulkan : public VideoCore::RasterizerAccelerated { 106class RasterizerVulkan final : public VideoCore::RasterizerAccelerated {
100public: 107public:
101 explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window, 108 explicit RasterizerVulkan(Core::System& system, Core::Frontend::EmuWindow& render_window,
102 VKScreenInfo& screen_info, const VKDevice& device, 109 VKScreenInfo& screen_info, const VKDevice& device,
@@ -104,10 +111,11 @@ public:
104 VKScheduler& scheduler); 111 VKScheduler& scheduler);
105 ~RasterizerVulkan() override; 112 ~RasterizerVulkan() override;
106 113
107 bool DrawBatch(bool is_indexed) override; 114 void Draw(bool is_indexed, bool is_instanced) override;
108 bool DrawMultiBatch(bool is_indexed) override;
109 void Clear() override; 115 void Clear() override;
110 void DispatchCompute(GPUVAddr code_addr) override; 116 void DispatchCompute(GPUVAddr code_addr) override;
117 void ResetCounter(VideoCore::QueryType type) override;
118 void Query(GPUVAddr gpu_addr, VideoCore::QueryType type, std::optional<u64> timestamp) override;
111 void FlushAll() override; 119 void FlushAll() override;
112 void FlushRegion(CacheAddr addr, u64 size) override; 120 void FlushRegion(CacheAddr addr, u64 size) override;
113 void InvalidateRegion(CacheAddr addr, u64 size) override; 121 void InvalidateRegion(CacheAddr addr, u64 size) override;
@@ -140,8 +148,6 @@ private:
140 148
141 static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8; 149 static constexpr std::size_t ZETA_TEXCEPTION_INDEX = 8;
142 150
143 void Draw(bool is_indexed, bool is_instanced);
144
145 void FlushWork(); 151 void FlushWork();
146 152
147 Texceptions UpdateAttachments(); 153 Texceptions UpdateAttachments();
@@ -247,6 +253,7 @@ private:
247 VKPipelineCache pipeline_cache; 253 VKPipelineCache pipeline_cache;
248 VKBufferCache buffer_cache; 254 VKBufferCache buffer_cache;
249 VKSamplerCache sampler_cache; 255 VKSamplerCache sampler_cache;
256 VKQueryCache query_cache;
250 257
251 std::array<View, Maxwell::NumRenderTargets> color_attachments; 258 std::array<View, Maxwell::NumRenderTargets> color_attachments;
252 View zeta_attachment; 259 View zeta_attachment;
diff --git a/src/video_core/renderer_vulkan/vk_sampler_cache.cpp b/src/video_core/renderer_vulkan/vk_sampler_cache.cpp
index 0a8ec8398..204b7c39c 100644
--- a/src/video_core/renderer_vulkan/vk_sampler_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_sampler_cache.cpp
@@ -23,7 +23,14 @@ static std::optional<vk::BorderColor> TryConvertBorderColor(std::array<float, 4>
23 } else if (color == std::array<float, 4>{1, 1, 1, 1}) { 23 } else if (color == std::array<float, 4>{1, 1, 1, 1}) {
24 return vk::BorderColor::eFloatOpaqueWhite; 24 return vk::BorderColor::eFloatOpaqueWhite;
25 } else { 25 } else {
26 return {}; 26 if (color[0] + color[1] + color[2] > 1.35f) {
27 // If color elements are brighter than roughly 0.5 average, use white border
28 return vk::BorderColor::eFloatOpaqueWhite;
29 }
30 if (color[3] > 0.5f) {
31 return vk::BorderColor::eFloatOpaqueBlack;
32 }
33 return vk::BorderColor::eFloatTransparentBlack;
27 } 34 }
28} 35}
29 36
@@ -37,8 +44,6 @@ UniqueSampler VKSamplerCache::CreateSampler(const Tegra::Texture::TSCEntry& tsc)
37 44
38 const auto border_color{tsc.GetBorderColor()}; 45 const auto border_color{tsc.GetBorderColor()};
39 const auto vk_border_color{TryConvertBorderColor(border_color)}; 46 const auto vk_border_color{TryConvertBorderColor(border_color)};
40 UNIMPLEMENTED_IF_MSG(!vk_border_color, "Unimplemented border color {} {} {} {}",
41 border_color[0], border_color[1], border_color[2], border_color[3]);
42 47
43 constexpr bool unnormalized_coords{false}; 48 constexpr bool unnormalized_coords{false};
44 49
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.cpp b/src/video_core/renderer_vulkan/vk_scheduler.cpp
index d66133ad1..92bd6c344 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.cpp
+++ b/src/video_core/renderer_vulkan/vk_scheduler.cpp
@@ -6,6 +6,7 @@
6#include "common/microprofile.h" 6#include "common/microprofile.h"
7#include "video_core/renderer_vulkan/declarations.h" 7#include "video_core/renderer_vulkan/declarations.h"
8#include "video_core/renderer_vulkan/vk_device.h" 8#include "video_core/renderer_vulkan/vk_device.h"
9#include "video_core/renderer_vulkan/vk_query_cache.h"
9#include "video_core/renderer_vulkan/vk_resource_manager.h" 10#include "video_core/renderer_vulkan/vk_resource_manager.h"
10#include "video_core/renderer_vulkan/vk_scheduler.h" 11#include "video_core/renderer_vulkan/vk_scheduler.h"
11 12
@@ -139,6 +140,8 @@ void VKScheduler::SubmitExecution(vk::Semaphore semaphore) {
139} 140}
140 141
141void VKScheduler::AllocateNewContext() { 142void VKScheduler::AllocateNewContext() {
143 ++ticks;
144
142 std::unique_lock lock{mutex}; 145 std::unique_lock lock{mutex};
143 current_fence = next_fence; 146 current_fence = next_fence;
144 next_fence = &resource_manager.CommitFence(); 147 next_fence = &resource_manager.CommitFence();
@@ -146,6 +149,10 @@ void VKScheduler::AllocateNewContext() {
146 current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence); 149 current_cmdbuf = resource_manager.CommitCommandBuffer(*current_fence);
147 current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit}, 150 current_cmdbuf.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit},
148 device.GetDispatchLoader()); 151 device.GetDispatchLoader());
152 // Enable counters once again. These are disabled when a command buffer is finished.
153 if (query_cache) {
154 query_cache->UpdateCounters();
155 }
149} 156}
150 157
151void VKScheduler::InvalidateState() { 158void VKScheduler::InvalidateState() {
@@ -159,6 +166,7 @@ void VKScheduler::InvalidateState() {
159} 166}
160 167
161void VKScheduler::EndPendingOperations() { 168void VKScheduler::EndPendingOperations() {
169 query_cache->DisableStreams();
162 EndRenderPass(); 170 EndRenderPass();
163} 171}
164 172
diff --git a/src/video_core/renderer_vulkan/vk_scheduler.h b/src/video_core/renderer_vulkan/vk_scheduler.h
index bcdffbba0..62fd7858b 100644
--- a/src/video_core/renderer_vulkan/vk_scheduler.h
+++ b/src/video_core/renderer_vulkan/vk_scheduler.h
@@ -4,6 +4,7 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <atomic>
7#include <condition_variable> 8#include <condition_variable>
8#include <memory> 9#include <memory>
9#include <optional> 10#include <optional>
@@ -18,6 +19,7 @@ namespace Vulkan {
18 19
19class VKDevice; 20class VKDevice;
20class VKFence; 21class VKFence;
22class VKQueryCache;
21class VKResourceManager; 23class VKResourceManager;
22 24
23class VKFenceView { 25class VKFenceView {
@@ -67,6 +69,11 @@ public:
67 /// Binds a pipeline to the current execution context. 69 /// Binds a pipeline to the current execution context.
68 void BindGraphicsPipeline(vk::Pipeline pipeline); 70 void BindGraphicsPipeline(vk::Pipeline pipeline);
69 71
72 /// Assigns the query cache.
73 void SetQueryCache(VKQueryCache& query_cache_) {
74 query_cache = &query_cache_;
75 }
76
70 /// Returns true when viewports have been set in the current command buffer. 77 /// Returns true when viewports have been set in the current command buffer.
71 bool TouchViewports() { 78 bool TouchViewports() {
72 return std::exchange(state.viewports, true); 79 return std::exchange(state.viewports, true);
@@ -112,6 +119,11 @@ public:
112 return current_fence; 119 return current_fence;
113 } 120 }
114 121
122 /// Returns the current command buffer tick.
123 u64 Ticks() const {
124 return ticks;
125 }
126
115private: 127private:
116 class Command { 128 class Command {
117 public: 129 public:
@@ -205,6 +217,8 @@ private:
205 217
206 const VKDevice& device; 218 const VKDevice& device;
207 VKResourceManager& resource_manager; 219 VKResourceManager& resource_manager;
220 VKQueryCache* query_cache = nullptr;
221
208 vk::CommandBuffer current_cmdbuf; 222 vk::CommandBuffer current_cmdbuf;
209 VKFence* current_fence = nullptr; 223 VKFence* current_fence = nullptr;
210 VKFence* next_fence = nullptr; 224 VKFence* next_fence = nullptr;
@@ -227,6 +241,7 @@ private:
227 Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve; 241 Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve;
228 std::mutex mutex; 242 std::mutex mutex;
229 std::condition_variable cv; 243 std::condition_variable cv;
244 std::atomic<u64> ticks = 0;
230 bool quit = false; 245 bool quit = false;
231}; 246};
232 247
diff --git a/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp b/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
index 24a658dce..2da622d15 100644
--- a/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
+++ b/src/video_core/renderer_vulkan/vk_shader_decompiler.cpp
@@ -86,6 +86,7 @@ struct AttributeType {
86 86
87struct VertexIndices { 87struct VertexIndices {
88 std::optional<u32> position; 88 std::optional<u32> position;
89 std::optional<u32> layer;
89 std::optional<u32> viewport; 90 std::optional<u32> viewport;
90 std::optional<u32> point_size; 91 std::optional<u32> point_size;
91 std::optional<u32> clip_distances; 92 std::optional<u32> clip_distances;
@@ -275,21 +276,29 @@ public:
275 AddCapability(spv::Capability::ImageGatherExtended); 276 AddCapability(spv::Capability::ImageGatherExtended);
276 AddCapability(spv::Capability::SampledBuffer); 277 AddCapability(spv::Capability::SampledBuffer);
277 AddCapability(spv::Capability::StorageImageWriteWithoutFormat); 278 AddCapability(spv::Capability::StorageImageWriteWithoutFormat);
279 AddCapability(spv::Capability::DrawParameters);
278 AddCapability(spv::Capability::SubgroupBallotKHR); 280 AddCapability(spv::Capability::SubgroupBallotKHR);
279 AddCapability(spv::Capability::SubgroupVoteKHR); 281 AddCapability(spv::Capability::SubgroupVoteKHR);
280 AddExtension("SPV_KHR_shader_ballot"); 282 AddExtension("SPV_KHR_shader_ballot");
281 AddExtension("SPV_KHR_subgroup_vote"); 283 AddExtension("SPV_KHR_subgroup_vote");
282 AddExtension("SPV_KHR_storage_buffer_storage_class"); 284 AddExtension("SPV_KHR_storage_buffer_storage_class");
283 AddExtension("SPV_KHR_variable_pointers"); 285 AddExtension("SPV_KHR_variable_pointers");
286 AddExtension("SPV_KHR_shader_draw_parameters");
284 287
285 if (ir.UsesViewportIndex()) { 288 if (ir.UsesLayer() || ir.UsesViewportIndex()) {
286 AddCapability(spv::Capability::MultiViewport); 289 if (ir.UsesViewportIndex()) {
287 if (device.IsExtShaderViewportIndexLayerSupported()) { 290 AddCapability(spv::Capability::MultiViewport);
291 }
292 if (stage != ShaderType::Geometry && device.IsExtShaderViewportIndexLayerSupported()) {
288 AddExtension("SPV_EXT_shader_viewport_index_layer"); 293 AddExtension("SPV_EXT_shader_viewport_index_layer");
289 AddCapability(spv::Capability::ShaderViewportIndexLayerEXT); 294 AddCapability(spv::Capability::ShaderViewportIndexLayerEXT);
290 } 295 }
291 } 296 }
292 297
298 if (device.IsShaderStorageImageReadWithoutFormatSupported()) {
299 AddCapability(spv::Capability::StorageImageReadWithoutFormat);
300 }
301
293 if (device.IsFloat16Supported()) { 302 if (device.IsFloat16Supported()) {
294 AddCapability(spv::Capability::Float16); 303 AddCapability(spv::Capability::Float16);
295 } 304 }
@@ -492,9 +501,11 @@ private:
492 interfaces.push_back(AddGlobalVariable(Name(out_vertex, "out_vertex"))); 501 interfaces.push_back(AddGlobalVariable(Name(out_vertex, "out_vertex")));
493 502
494 // Declare input attributes 503 // Declare input attributes
495 vertex_index = DeclareInputBuiltIn(spv::BuiltIn::VertexIndex, t_in_uint, "vertex_index"); 504 vertex_index = DeclareInputBuiltIn(spv::BuiltIn::VertexIndex, t_in_int, "vertex_index");
496 instance_index = 505 instance_index =
497 DeclareInputBuiltIn(spv::BuiltIn::InstanceIndex, t_in_uint, "instance_index"); 506 DeclareInputBuiltIn(spv::BuiltIn::InstanceIndex, t_in_int, "instance_index");
507 base_vertex = DeclareInputBuiltIn(spv::BuiltIn::BaseVertex, t_in_int, "base_vertex");
508 base_instance = DeclareInputBuiltIn(spv::BuiltIn::BaseInstance, t_in_int, "base_instance");
498 } 509 }
499 510
500 void DeclareTessControl() { 511 void DeclareTessControl() {
@@ -920,13 +931,22 @@ private:
920 VertexIndices indices; 931 VertexIndices indices;
921 indices.position = AddBuiltIn(t_float4, spv::BuiltIn::Position, "position"); 932 indices.position = AddBuiltIn(t_float4, spv::BuiltIn::Position, "position");
922 933
934 if (ir.UsesLayer()) {
935 if (stage != ShaderType::Vertex || device.IsExtShaderViewportIndexLayerSupported()) {
936 indices.layer = AddBuiltIn(t_int, spv::BuiltIn::Layer, "layer");
937 } else {
938 LOG_ERROR(
939 Render_Vulkan,
940 "Shader requires Layer but it's not supported on this stage with this device.");
941 }
942 }
943
923 if (ir.UsesViewportIndex()) { 944 if (ir.UsesViewportIndex()) {
924 if (stage != ShaderType::Vertex || device.IsExtShaderViewportIndexLayerSupported()) { 945 if (stage != ShaderType::Vertex || device.IsExtShaderViewportIndexLayerSupported()) {
925 indices.viewport = AddBuiltIn(t_int, spv::BuiltIn::ViewportIndex, "viewport_index"); 946 indices.viewport = AddBuiltIn(t_int, spv::BuiltIn::ViewportIndex, "viewport_index");
926 } else { 947 } else {
927 LOG_ERROR(Render_Vulkan, 948 LOG_ERROR(Render_Vulkan, "Shader requires ViewportIndex but it's not supported on "
928 "Shader requires ViewportIndex but it's not supported on this " 949 "this stage with this device.");
929 "stage with this device.");
930 } 950 }
931 } 951 }
932 952
@@ -1068,9 +1088,12 @@ private:
1068 return {OpLoad(t_float, AccessElement(t_in_float, tess_coord, element)), 1088 return {OpLoad(t_float, AccessElement(t_in_float, tess_coord, element)),
1069 Type::Float}; 1089 Type::Float};
1070 case 2: 1090 case 2:
1071 return {OpLoad(t_uint, instance_index), Type::Uint}; 1091 return {
1092 OpISub(t_int, OpLoad(t_int, instance_index), OpLoad(t_int, base_instance)),
1093 Type::Int};
1072 case 3: 1094 case 3:
1073 return {OpLoad(t_uint, vertex_index), Type::Uint}; 1095 return {OpISub(t_int, OpLoad(t_int, vertex_index), OpLoad(t_int, base_vertex)),
1096 Type::Int};
1074 } 1097 }
1075 UNIMPLEMENTED_MSG("Unmanaged TessCoordInstanceIDVertexID element={}", element); 1098 UNIMPLEMENTED_MSG("Unmanaged TessCoordInstanceIDVertexID element={}", element);
1076 return {Constant(t_uint, 0U), Type::Uint}; 1099 return {Constant(t_uint, 0U), Type::Uint};
@@ -1285,6 +1308,13 @@ private:
1285 } 1308 }
1286 case Attribute::Index::LayerViewportPointSize: 1309 case Attribute::Index::LayerViewportPointSize:
1287 switch (element) { 1310 switch (element) {
1311 case 1: {
1312 if (!out_indices.layer) {
1313 return {};
1314 }
1315 const u32 index = out_indices.layer.value();
1316 return {AccessElement(t_out_int, out_vertex, index), Type::Int};
1317 }
1288 case 2: { 1318 case 2: {
1289 if (!out_indices.viewport) { 1319 if (!out_indices.viewport) {
1290 return {}; 1320 return {};
@@ -1355,6 +1385,11 @@ private:
1355 UNIMPLEMENTED(); 1385 UNIMPLEMENTED();
1356 } 1386 }
1357 1387
1388 if (!target.id) {
1389 // On failure we return a nullptr target.id, skip these stores.
1390 return {};
1391 }
1392
1358 OpStore(target.id, As(Visit(src), target.type)); 1393 OpStore(target.id, As(Visit(src), target.type));
1359 return {}; 1394 return {};
1360 } 1395 }
@@ -1748,8 +1783,16 @@ private:
1748 } 1783 }
1749 1784
1750 Expression ImageLoad(Operation operation) { 1785 Expression ImageLoad(Operation operation) {
1751 UNIMPLEMENTED(); 1786 if (!device.IsShaderStorageImageReadWithoutFormatSupported()) {
1752 return {}; 1787 return {v_float_zero, Type::Float};
1788 }
1789
1790 const auto& meta{std::get<MetaImage>(operation.GetMeta())};
1791
1792 const Id coords = GetCoordinates(operation, Type::Int);
1793 const Id texel = OpImageRead(t_uint4, GetImage(operation), coords);
1794
1795 return {OpCompositeExtract(t_uint, texel, meta.element), Type::Uint};
1753 } 1796 }
1754 1797
1755 Expression ImageStore(Operation operation) { 1798 Expression ImageStore(Operation operation) {
@@ -2542,6 +2585,8 @@ private:
2542 2585
2543 Id instance_index{}; 2586 Id instance_index{};
2544 Id vertex_index{}; 2587 Id vertex_index{};
2588 Id base_instance{};
2589 Id base_vertex{};
2545 std::array<Id, Maxwell::NumRenderTargets> frag_colors{}; 2590 std::array<Id, Maxwell::NumRenderTargets> frag_colors{};
2546 Id frag_depth{}; 2591 Id frag_depth{};
2547 Id frag_coord{}; 2592 Id frag_coord{};
diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.h b/src/video_core/renderer_vulkan/vk_texture_cache.h
index d3edbe80c..22e3d34de 100644
--- a/src/video_core/renderer_vulkan/vk_texture_cache.h
+++ b/src/video_core/renderer_vulkan/vk_texture_cache.h
@@ -151,6 +151,10 @@ public:
151 return params.GetMipHeight(base_level); 151 return params.GetMipHeight(base_level);
152 } 152 }
153 153
154 u32 GetNumLayers() const {
155 return num_layers;
156 }
157
154 bool IsBufferView() const { 158 bool IsBufferView() const {
155 return buffer_view; 159 return buffer_view;
156 } 160 }
diff --git a/src/video_core/shader/decode/conversion.cpp b/src/video_core/shader/decode/conversion.cpp
index 0eeb75559..6ead42070 100644
--- a/src/video_core/shader/decode/conversion.cpp
+++ b/src/video_core/shader/decode/conversion.cpp
@@ -83,14 +83,14 @@ u32 ShaderIR::DecodeConversion(NodeBlock& bb, u32 pc) {
83 83
84 const bool input_signed = instr.conversion.is_input_signed; 84 const bool input_signed = instr.conversion.is_input_signed;
85 85
86 if (instr.conversion.src_size == Register::Size::Byte) { 86 if (const u32 offset = static_cast<u32>(instr.conversion.int_src.selector); offset > 0) {
87 const u32 offset = static_cast<u32>(instr.conversion.int_src.selector) * 8; 87 ASSERT(instr.conversion.src_size == Register::Size::Byte ||
88 if (offset > 0) { 88 instr.conversion.src_size == Register::Size::Short);
89 value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed, 89 if (instr.conversion.src_size == Register::Size::Short) {
90 std::move(value), Immediate(offset)); 90 ASSERT(offset == 0 || offset == 2);
91 } 91 }
92 } else { 92 value = SignedOperation(OperationCode::ILogicalShiftRight, input_signed,
93 UNIMPLEMENTED_IF(instr.conversion.int_src.selector != 0); 93 std::move(value), Immediate(offset * 8));
94 } 94 }
95 95
96 value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed); 96 value = ConvertIntegerSize(value, instr.conversion.src_size, input_signed);
diff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cpp
index 351c8c2f1..bee7d8cad 100644
--- a/src/video_core/shader/decode/texture.cpp
+++ b/src/video_core/shader/decode/texture.cpp
@@ -522,68 +522,53 @@ Node4 ShaderIR::GetTextureCode(Instruction instr, TextureType texture_type,
522 Node array, Node depth_compare, u32 bias_offset, 522 Node array, Node depth_compare, u32 bias_offset,
523 std::vector<Node> aoffi, 523 std::vector<Node> aoffi,
524 std::optional<Tegra::Shader::Register> bindless_reg) { 524 std::optional<Tegra::Shader::Register> bindless_reg) {
525 const auto is_array = static_cast<bool>(array); 525 const bool is_array = array != nullptr;
526 const auto is_shadow = static_cast<bool>(depth_compare); 526 const bool is_shadow = depth_compare != nullptr;
527 const bool is_bindless = bindless_reg.has_value(); 527 const bool is_bindless = bindless_reg.has_value();
528 528
529 UNIMPLEMENTED_IF_MSG((texture_type == TextureType::Texture3D && (is_array || is_shadow)) || 529 UNIMPLEMENTED_IF(texture_type == TextureType::TextureCube && is_array && is_shadow);
530 (texture_type == TextureType::TextureCube && is_array && is_shadow), 530 ASSERT_MSG(texture_type != TextureType::Texture3D || !is_array || !is_shadow,
531 "This method is not supported."); 531 "Illegal texture type");
532 532
533 const SamplerInfo info{texture_type, is_array, is_shadow, false}; 533 const SamplerInfo info{texture_type, is_array, is_shadow, false};
534 Node index_var{}; 534 Node index_var;
535 const Sampler* sampler = is_bindless ? GetBindlessSampler(*bindless_reg, index_var, info) 535 const Sampler* sampler = is_bindless ? GetBindlessSampler(*bindless_reg, index_var, info)
536 : GetSampler(instr.sampler, info); 536 : GetSampler(instr.sampler, info);
537 Node4 values; 537 if (!sampler) {
538 if (sampler == nullptr) { 538 return {Immediate(0), Immediate(0), Immediate(0), Immediate(0)};
539 for (u32 element = 0; element < values.size(); ++element) {
540 values[element] = Immediate(0);
541 }
542 return values;
543 } 539 }
544 540
545 const bool lod_needed = process_mode == TextureProcessMode::LZ || 541 const bool lod_needed = process_mode == TextureProcessMode::LZ ||
546 process_mode == TextureProcessMode::LL || 542 process_mode == TextureProcessMode::LL ||
547 process_mode == TextureProcessMode::LLA; 543 process_mode == TextureProcessMode::LLA;
548 544 const OperationCode opcode = lod_needed ? OperationCode::TextureLod : OperationCode::Texture;
549 // LOD selection (either via bias or explicit textureLod) not supported in GL for
550 // sampler2DArrayShadow and samplerCubeArrayShadow.
551 const bool gl_lod_supported =
552 !((texture_type == Tegra::Shader::TextureType::Texture2D && is_array && is_shadow) ||
553 (texture_type == Tegra::Shader::TextureType::TextureCube && is_array && is_shadow));
554
555 const OperationCode read_method =
556 (lod_needed && gl_lod_supported) ? OperationCode::TextureLod : OperationCode::Texture;
557
558 UNIMPLEMENTED_IF(process_mode != TextureProcessMode::None && !gl_lod_supported);
559 545
560 Node bias; 546 Node bias;
561 Node lod; 547 Node lod;
562 if (process_mode != TextureProcessMode::None && gl_lod_supported) { 548 switch (process_mode) {
563 switch (process_mode) { 549 case TextureProcessMode::None:
564 case TextureProcessMode::LZ: 550 break;
565 lod = Immediate(0.0f); 551 case TextureProcessMode::LZ:
566 break; 552 lod = Immediate(0.0f);
567 case TextureProcessMode::LB: 553 break;
568 // If present, lod or bias are always stored in the register 554 case TextureProcessMode::LB:
569 // indexed by the gpr20 field with an offset depending on the 555 // If present, lod or bias are always stored in the register indexed by the gpr20 field with
570 // usage of the other registers 556 // an offset depending on the usage of the other registers.
571 bias = GetRegister(instr.gpr20.Value() + bias_offset); 557 bias = GetRegister(instr.gpr20.Value() + bias_offset);
572 break; 558 break;
573 case TextureProcessMode::LL: 559 case TextureProcessMode::LL:
574 lod = GetRegister(instr.gpr20.Value() + bias_offset); 560 lod = GetRegister(instr.gpr20.Value() + bias_offset);
575 break; 561 break;
576 default: 562 default:
577 UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode)); 563 UNIMPLEMENTED_MSG("Unimplemented process mode={}", static_cast<u32>(process_mode));
578 break; 564 break;
579 }
580 } 565 }
581 566
567 Node4 values;
582 for (u32 element = 0; element < values.size(); ++element) { 568 for (u32 element = 0; element < values.size(); ++element) {
583 auto copy_coords = coords;
584 MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias, 569 MetaTexture meta{*sampler, array, depth_compare, aoffi, {}, {}, bias,
585 lod, {}, element, index_var}; 570 lod, {}, element, index_var};
586 values[element] = Operation(read_method, meta, std::move(copy_coords)); 571 values[element] = Operation(opcode, meta, coords);
587 } 572 }
588 573
589 return values; 574 return values;
diff --git a/src/video_core/texture_cache/surface_base.cpp b/src/video_core/texture_cache/surface_base.cpp
index 84469b7ba..002df414f 100644
--- a/src/video_core/texture_cache/surface_base.cpp
+++ b/src/video_core/texture_cache/surface_base.cpp
@@ -277,6 +277,10 @@ void SurfaceBaseImpl::FlushBuffer(Tegra::MemoryManager& memory_manager,
277 SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params, 277 SwizzleFunc(MortonSwizzleMode::LinearToMorton, host_ptr, params,
278 staging_buffer.data() + host_offset, level); 278 staging_buffer.data() + host_offset, level);
279 } 279 }
280 } else if (params.IsBuffer()) {
281 // Buffers don't have pitch or any fancy layout property. We can just memcpy them to guest
282 // memory.
283 std::memcpy(host_ptr, staging_buffer.data(), guest_memory_size);
280 } else { 284 } else {
281 ASSERT(params.target == SurfaceTarget::Texture2D); 285 ASSERT(params.target == SurfaceTarget::Texture2D);
282 ASSERT(params.num_levels == 1); 286 ASSERT(params.num_levels == 1);
diff --git a/src/video_core/texture_cache/surface_params.cpp b/src/video_core/texture_cache/surface_params.cpp
index 38b3a4ba8..f00839313 100644
--- a/src/video_core/texture_cache/surface_params.cpp
+++ b/src/video_core/texture_cache/surface_params.cpp
@@ -84,19 +84,16 @@ SurfaceParams SurfaceParams::CreateForTexture(const FormatLookupTable& lookup_ta
84 if (entry.IsShadow() && params.type == SurfaceType::ColorTexture) { 84 if (entry.IsShadow() && params.type == SurfaceType::ColorTexture) {
85 switch (params.pixel_format) { 85 switch (params.pixel_format) {
86 case PixelFormat::R16U: 86 case PixelFormat::R16U:
87 case PixelFormat::R16F: { 87 case PixelFormat::R16F:
88 params.pixel_format = PixelFormat::Z16; 88 params.pixel_format = PixelFormat::Z16;
89 break; 89 break;
90 } 90 case PixelFormat::R32F:
91 case PixelFormat::R32F: {
92 params.pixel_format = PixelFormat::Z32F; 91 params.pixel_format = PixelFormat::Z32F;
93 break; 92 break;
94 } 93 default:
95 default: {
96 UNIMPLEMENTED_MSG("Unimplemented shadow convert format: {}", 94 UNIMPLEMENTED_MSG("Unimplemented shadow convert format: {}",
97 static_cast<u32>(params.pixel_format)); 95 static_cast<u32>(params.pixel_format));
98 } 96 }
99 }
100 params.type = GetFormatType(params.pixel_format); 97 params.type = GetFormatType(params.pixel_format);
101 } 98 }
102 params.type = GetFormatType(params.pixel_format); 99 params.type = GetFormatType(params.pixel_format);
@@ -168,27 +165,29 @@ SurfaceParams SurfaceParams::CreateForImage(const FormatLookupTable& lookup_tabl
168 return params; 165 return params;
169} 166}
170 167
171SurfaceParams SurfaceParams::CreateForDepthBuffer( 168SurfaceParams SurfaceParams::CreateForDepthBuffer(Core::System& system) {
172 Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format, 169 const auto& regs = system.GPU().Maxwell3D().regs;
173 u32 block_width, u32 block_height, u32 block_depth, 170 regs.zeta_width, regs.zeta_height, regs.zeta.format, regs.zeta.memory_layout.type;
174 Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type) {
175 SurfaceParams params; 171 SurfaceParams params;
176 params.is_tiled = type == Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear; 172 params.is_tiled = regs.zeta.memory_layout.type ==
173 Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout::BlockLinear;
177 params.srgb_conversion = false; 174 params.srgb_conversion = false;
178 params.block_width = std::min(block_width, 5U); 175 params.block_width = std::min(regs.zeta.memory_layout.block_width.Value(), 5U);
179 params.block_height = std::min(block_height, 5U); 176 params.block_height = std::min(regs.zeta.memory_layout.block_height.Value(), 5U);
180 params.block_depth = std::min(block_depth, 5U); 177 params.block_depth = std::min(regs.zeta.memory_layout.block_depth.Value(), 5U);
181 params.tile_width_spacing = 1; 178 params.tile_width_spacing = 1;
182 params.pixel_format = PixelFormatFromDepthFormat(format); 179 params.pixel_format = PixelFormatFromDepthFormat(regs.zeta.format);
183 params.type = GetFormatType(params.pixel_format); 180 params.type = GetFormatType(params.pixel_format);
184 params.width = zeta_width; 181 params.width = regs.zeta_width;
185 params.height = zeta_height; 182 params.height = regs.zeta_height;
186 params.target = SurfaceTarget::Texture2D;
187 params.depth = 1;
188 params.pitch = 0; 183 params.pitch = 0;
189 params.num_levels = 1; 184 params.num_levels = 1;
190 params.emulated_levels = 1; 185 params.emulated_levels = 1;
191 params.is_layered = false; 186
187 const bool is_layered = regs.zeta_layers > 1 && params.block_depth == 0;
188 params.is_layered = is_layered;
189 params.target = is_layered ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D;
190 params.depth = is_layered ? regs.zeta_layers.Value() : 1U;
192 return params; 191 return params;
193} 192}
194 193
@@ -214,11 +213,13 @@ SurfaceParams SurfaceParams::CreateForFramebuffer(Core::System& system, std::siz
214 params.width = params.pitch / bpp; 213 params.width = params.pitch / bpp;
215 } 214 }
216 params.height = config.height; 215 params.height = config.height;
217 params.depth = 1;
218 params.target = SurfaceTarget::Texture2D;
219 params.num_levels = 1; 216 params.num_levels = 1;
220 params.emulated_levels = 1; 217 params.emulated_levels = 1;
221 params.is_layered = false; 218
219 const bool is_layered = config.layers > 1 && params.block_depth == 0;
220 params.is_layered = is_layered;
221 params.depth = is_layered ? config.layers.Value() : 1;
222 params.target = is_layered ? SurfaceTarget::Texture2DArray : SurfaceTarget::Texture2D;
222 return params; 223 return params;
223} 224}
224 225
diff --git a/src/video_core/texture_cache/surface_params.h b/src/video_core/texture_cache/surface_params.h
index 9256fd6d9..995cc3818 100644
--- a/src/video_core/texture_cache/surface_params.h
+++ b/src/video_core/texture_cache/surface_params.h
@@ -35,10 +35,7 @@ public:
35 const VideoCommon::Shader::Image& entry); 35 const VideoCommon::Shader::Image& entry);
36 36
37 /// Creates SurfaceCachedParams for a depth buffer configuration. 37 /// Creates SurfaceCachedParams for a depth buffer configuration.
38 static SurfaceParams CreateForDepthBuffer( 38 static SurfaceParams CreateForDepthBuffer(Core::System& system);
39 Core::System& system, u32 zeta_width, u32 zeta_height, Tegra::DepthFormat format,
40 u32 block_width, u32 block_height, u32 block_depth,
41 Tegra::Engines::Maxwell3D::Regs::InvMemoryLayout type);
42 39
43 /// Creates SurfaceCachedParams from a framebuffer configuration. 40 /// Creates SurfaceCachedParams from a framebuffer configuration.
44 static SurfaceParams CreateForFramebuffer(Core::System& system, std::size_t index); 41 static SurfaceParams CreateForFramebuffer(Core::System& system, std::size_t index);
diff --git a/src/video_core/texture_cache/texture_cache.h b/src/video_core/texture_cache/texture_cache.h
index f4c015635..c70e4aec2 100644
--- a/src/video_core/texture_cache/texture_cache.h
+++ b/src/video_core/texture_cache/texture_cache.h
@@ -160,10 +160,7 @@ public:
160 SetEmptyDepthBuffer(); 160 SetEmptyDepthBuffer();
161 return {}; 161 return {};
162 } 162 }
163 const auto depth_params{SurfaceParams::CreateForDepthBuffer( 163 const auto depth_params{SurfaceParams::CreateForDepthBuffer(system)};
164 system, regs.zeta_width, regs.zeta_height, regs.zeta.format,
165 regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
166 regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
167 auto surface_view = GetSurface(gpu_addr, cache_addr, depth_params, preserve_contents, true); 164 auto surface_view = GetSurface(gpu_addr, cache_addr, depth_params, preserve_contents, true);
168 if (depth_buffer.target) 165 if (depth_buffer.target)
169 depth_buffer.target->MarkAsRenderTarget(false, NO_RT); 166 depth_buffer.target->MarkAsRenderTarget(false, NO_RT);
@@ -721,7 +718,6 @@ private:
721 std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const CacheAddr cache_addr, 718 std::pair<TSurface, TView> GetSurface(const GPUVAddr gpu_addr, const CacheAddr cache_addr,
722 const SurfaceParams& params, bool preserve_contents, 719 const SurfaceParams& params, bool preserve_contents,
723 bool is_render) { 720 bool is_render) {
724
725 // Step 1 721 // Step 1
726 // Check Level 1 Cache for a fast structural match. If candidate surface 722 // Check Level 1 Cache for a fast structural match. If candidate surface
727 // matches at certain level we are pretty much done. 723 // matches at certain level we are pretty much done.
@@ -733,14 +729,18 @@ private:
733 return RecycleSurface(overlaps, params, gpu_addr, preserve_contents, 729 return RecycleSurface(overlaps, params, gpu_addr, preserve_contents,
734 topological_result); 730 topological_result);
735 } 731 }
732
736 const auto struct_result = current_surface->MatchesStructure(params); 733 const auto struct_result = current_surface->MatchesStructure(params);
737 if (struct_result != MatchStructureResult::None && 734 if (struct_result != MatchStructureResult::None) {
738 (params.target != SurfaceTarget::Texture3D || 735 const auto& old_params = current_surface->GetSurfaceParams();
739 current_surface->MatchTarget(params.target))) { 736 const bool not_3d = params.target != SurfaceTarget::Texture3D &&
740 if (struct_result == MatchStructureResult::FullMatch) { 737 old_params.target != SurfaceTarget::Texture3D;
741 return ManageStructuralMatch(current_surface, params, is_render); 738 if (not_3d || current_surface->MatchTarget(params.target)) {
742 } else { 739 if (struct_result == MatchStructureResult::FullMatch) {
743 return RebuildSurface(current_surface, params, is_render); 740 return ManageStructuralMatch(current_surface, params, is_render);
741 } else {
742 return RebuildSurface(current_surface, params, is_render);
743 }
744 } 744 }
745 } 745 }
746 } 746 }
diff --git a/src/web_service/web_backend.cpp b/src/web_service/web_backend.cpp
index 6683f459f..737ffe409 100644
--- a/src/web_service/web_backend.cpp
+++ b/src/web_service/web_backend.cpp
@@ -73,14 +73,12 @@ struct Client::Impl {
73 if (!parsedUrl.GetPort(&port)) { 73 if (!parsedUrl.GetPort(&port)) {
74 port = HTTP_PORT; 74 port = HTTP_PORT;
75 } 75 }
76 cli = std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port, 76 cli = std::make_unique<httplib::Client>(parsedUrl.m_Host.c_str(), port);
77 TIMEOUT_SECONDS);
78 } else if (parsedUrl.m_Scheme == "https") { 77 } else if (parsedUrl.m_Scheme == "https") {
79 if (!parsedUrl.GetPort(&port)) { 78 if (!parsedUrl.GetPort(&port)) {
80 port = HTTPS_PORT; 79 port = HTTPS_PORT;
81 } 80 }
82 cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port, 81 cli = std::make_unique<httplib::SSLClient>(parsedUrl.m_Host.c_str(), port);
83 TIMEOUT_SECONDS);
84 } else { 82 } else {
85 LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme); 83 LOG_ERROR(WebService, "Bad URL scheme {}", parsedUrl.m_Scheme);
86 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"}; 84 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Bad URL scheme"};
@@ -90,6 +88,7 @@ struct Client::Impl {
90 LOG_ERROR(WebService, "Invalid URL {}", host + path); 88 LOG_ERROR(WebService, "Invalid URL {}", host + path);
91 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"}; 89 return Common::WebResult{Common::WebResult::Code::InvalidURL, "Invalid URL"};
92 } 90 }
91 cli->set_timeout_sec(TIMEOUT_SECONDS);
93 92
94 httplib::Headers params; 93 httplib::Headers params;
95 if (!jwt.empty()) { 94 if (!jwt.empty()) {
diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp
index cd94693c1..6209fff75 100644
--- a/src/yuzu/configuration/config.cpp
+++ b/src/yuzu/configuration/config.cpp
@@ -630,6 +630,7 @@ void Config::ReadRendererValues() {
630 Settings::values.vulkan_device = ReadSetting(QStringLiteral("vulkan_device"), 0).toInt(); 630 Settings::values.vulkan_device = ReadSetting(QStringLiteral("vulkan_device"), 0).toInt();
631 Settings::values.resolution_factor = 631 Settings::values.resolution_factor =
632 ReadSetting(QStringLiteral("resolution_factor"), 1.0).toFloat(); 632 ReadSetting(QStringLiteral("resolution_factor"), 1.0).toFloat();
633 Settings::values.aspect_ratio = ReadSetting(QStringLiteral("aspect_ratio"), 0).toInt();
633 Settings::values.use_frame_limit = 634 Settings::values.use_frame_limit =
634 ReadSetting(QStringLiteral("use_frame_limit"), true).toBool(); 635 ReadSetting(QStringLiteral("use_frame_limit"), true).toBool();
635 Settings::values.frame_limit = ReadSetting(QStringLiteral("frame_limit"), 100).toInt(); 636 Settings::values.frame_limit = ReadSetting(QStringLiteral("frame_limit"), 100).toInt();
@@ -1064,6 +1065,7 @@ void Config::SaveRendererValues() {
1064 WriteSetting(QStringLiteral("vulkan_device"), Settings::values.vulkan_device, 0); 1065 WriteSetting(QStringLiteral("vulkan_device"), Settings::values.vulkan_device, 0);
1065 WriteSetting(QStringLiteral("resolution_factor"), 1066 WriteSetting(QStringLiteral("resolution_factor"),
1066 static_cast<double>(Settings::values.resolution_factor), 1.0); 1067 static_cast<double>(Settings::values.resolution_factor), 1.0);
1068 WriteSetting(QStringLiteral("aspect_ratio"), Settings::values.aspect_ratio, 0);
1067 WriteSetting(QStringLiteral("use_frame_limit"), Settings::values.use_frame_limit, true); 1069 WriteSetting(QStringLiteral("use_frame_limit"), Settings::values.use_frame_limit, true);
1068 WriteSetting(QStringLiteral("frame_limit"), Settings::values.frame_limit, 100); 1070 WriteSetting(QStringLiteral("frame_limit"), Settings::values.frame_limit, 100);
1069 WriteSetting(QStringLiteral("use_disk_shader_cache"), Settings::values.use_disk_shader_cache, 1071 WriteSetting(QStringLiteral("use_disk_shader_cache"), Settings::values.use_disk_shader_cache,
diff --git a/src/yuzu/configuration/configure_graphics.cpp b/src/yuzu/configuration/configure_graphics.cpp
index f57a24e36..ea899c080 100644
--- a/src/yuzu/configuration/configure_graphics.cpp
+++ b/src/yuzu/configuration/configure_graphics.cpp
@@ -97,6 +97,7 @@ void ConfigureGraphics::SetConfiguration() {
97 ui->api->setCurrentIndex(static_cast<int>(Settings::values.renderer_backend)); 97 ui->api->setCurrentIndex(static_cast<int>(Settings::values.renderer_backend));
98 ui->resolution_factor_combobox->setCurrentIndex( 98 ui->resolution_factor_combobox->setCurrentIndex(
99 static_cast<int>(FromResolutionFactor(Settings::values.resolution_factor))); 99 static_cast<int>(FromResolutionFactor(Settings::values.resolution_factor)));
100 ui->aspect_ratio_combobox->setCurrentIndex(Settings::values.aspect_ratio);
100 ui->use_disk_shader_cache->setEnabled(runtime_lock); 101 ui->use_disk_shader_cache->setEnabled(runtime_lock);
101 ui->use_disk_shader_cache->setChecked(Settings::values.use_disk_shader_cache); 102 ui->use_disk_shader_cache->setChecked(Settings::values.use_disk_shader_cache);
102 ui->use_accurate_gpu_emulation->setChecked(Settings::values.use_accurate_gpu_emulation); 103 ui->use_accurate_gpu_emulation->setChecked(Settings::values.use_accurate_gpu_emulation);
@@ -114,6 +115,7 @@ void ConfigureGraphics::ApplyConfiguration() {
114 Settings::values.vulkan_device = vulkan_device; 115 Settings::values.vulkan_device = vulkan_device;
115 Settings::values.resolution_factor = 116 Settings::values.resolution_factor =
116 ToResolutionFactor(static_cast<Resolution>(ui->resolution_factor_combobox->currentIndex())); 117 ToResolutionFactor(static_cast<Resolution>(ui->resolution_factor_combobox->currentIndex()));
118 Settings::values.aspect_ratio = ui->aspect_ratio_combobox->currentIndex();
117 Settings::values.use_disk_shader_cache = ui->use_disk_shader_cache->isChecked(); 119 Settings::values.use_disk_shader_cache = ui->use_disk_shader_cache->isChecked();
118 Settings::values.use_accurate_gpu_emulation = ui->use_accurate_gpu_emulation->isChecked(); 120 Settings::values.use_accurate_gpu_emulation = ui->use_accurate_gpu_emulation->isChecked();
119 Settings::values.use_asynchronous_gpu_emulation = 121 Settings::values.use_asynchronous_gpu_emulation =
diff --git a/src/yuzu/configuration/configure_graphics.ui b/src/yuzu/configuration/configure_graphics.ui
index e24372204..db60426ab 100644
--- a/src/yuzu/configuration/configure_graphics.ui
+++ b/src/yuzu/configuration/configure_graphics.ui
@@ -139,6 +139,41 @@
139 </layout> 139 </layout>
140 </item> 140 </item>
141 <item> 141 <item>
142 <layout class="QHBoxLayout" name="horizontalLayout_6">
143 <item>
144 <widget class="QLabel" name="ar_label">
145 <property name="text">
146 <string>Aspect Ratio:</string>
147 </property>
148 </widget>
149 </item>
150 <item>
151 <widget class="QComboBox" name="aspect_ratio_combobox">
152 <item>
153 <property name="text">
154 <string>Default (16:9)</string>
155 </property>
156 </item>
157 <item>
158 <property name="text">
159 <string>Force 4:3</string>
160 </property>
161 </item>
162 <item>
163 <property name="text">
164 <string>Force 21:9</string>
165 </property>
166 </item>
167 <item>
168 <property name="text">
169 <string>Stretch to Window</string>
170 </property>
171 </item>
172 </widget>
173 </item>
174 </layout>
175 </item>
176 <item>
142 <layout class="QHBoxLayout" name="horizontalLayout_3"> 177 <layout class="QHBoxLayout" name="horizontalLayout_3">
143 <item> 178 <item>
144 <widget class="QLabel" name="bg_label"> 179 <widget class="QLabel" name="bg_label">
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index b01a36023..96f1ce3af 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -379,6 +379,8 @@ void Config::ReadValues() {
379 379
380 Settings::values.resolution_factor = 380 Settings::values.resolution_factor =
381 static_cast<float>(sdl2_config->GetReal("Renderer", "resolution_factor", 1.0)); 381 static_cast<float>(sdl2_config->GetReal("Renderer", "resolution_factor", 1.0));
382 Settings::values.aspect_ratio =
383 static_cast<int>(sdl2_config->GetInteger("Renderer", "aspect_ratio", 0));
382 Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true); 384 Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true);
383 Settings::values.frame_limit = 385 Settings::values.frame_limit =
384 static_cast<u16>(sdl2_config->GetInteger("Renderer", "frame_limit", 100)); 386 static_cast<u16>(sdl2_config->GetInteger("Renderer", "frame_limit", 100));
diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h
index 00fd88279..8a2b658cd 100644
--- a/src/yuzu_cmd/default_ini.h
+++ b/src/yuzu_cmd/default_ini.h
@@ -122,6 +122,10 @@ use_shader_jit =
122# factor for the Switch resolution 122# factor for the Switch resolution
123resolution_factor = 123resolution_factor =
124 124
125# Aspect ratio
126# 0: Default (16:9), 1: Force 4:3, 2: Force 21:9, 3: Stretch to Window
127aspect_ratio =
128
125# Whether to enable V-Sync (caps the framerate at 60FPS) or not. 129# Whether to enable V-Sync (caps the framerate at 60FPS) or not.
126# 0 (default): Off, 1: On 130# 0 (default): Off, 1: On
127use_vsync = 131use_vsync =
diff --git a/src/yuzu_tester/config.cpp b/src/yuzu_tester/config.cpp
index 84ab4d687..0ac93b62a 100644
--- a/src/yuzu_tester/config.cpp
+++ b/src/yuzu_tester/config.cpp
@@ -118,6 +118,8 @@ void Config::ReadValues() {
118 // Renderer 118 // Renderer
119 Settings::values.resolution_factor = 119 Settings::values.resolution_factor =
120 static_cast<float>(sdl2_config->GetReal("Renderer", "resolution_factor", 1.0)); 120 static_cast<float>(sdl2_config->GetReal("Renderer", "resolution_factor", 1.0));
121 Settings::values.aspect_ratio =
122 static_cast<int>(sdl2_config->GetInteger("Renderer", "aspect_ratio", 0));
121 Settings::values.use_frame_limit = false; 123 Settings::values.use_frame_limit = false;
122 Settings::values.frame_limit = 100; 124 Settings::values.frame_limit = 100;
123 Settings::values.use_disk_shader_cache = 125 Settings::values.use_disk_shader_cache =
diff --git a/src/yuzu_tester/default_ini.h b/src/yuzu_tester/default_ini.h
index 9a3e86d68..8d93f7b88 100644
--- a/src/yuzu_tester/default_ini.h
+++ b/src/yuzu_tester/default_ini.h
@@ -26,6 +26,10 @@ use_shader_jit =
26# factor for the Switch resolution 26# factor for the Switch resolution
27resolution_factor = 27resolution_factor =
28 28
29# Aspect ratio
30# 0: Default (16:9), 1: Force 4:3, 2: Force 21:9, 3: Stretch to Window
31aspect_ratio =
32
29# Whether to enable V-Sync (caps the framerate at 60FPS) or not. 33# Whether to enable V-Sync (caps the framerate at 60FPS) or not.
30# 0 (default): Off, 1: On 34# 0 (default): Off, 1: On
31use_vsync = 35use_vsync =