taisei/src/util/compat.h

332 lines
9.5 KiB
C
Raw Permalink Normal View History

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
2024-05-16 23:30:41 +02:00
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
*/
#pragma once
#include "taisei.h"
2019-03-18 05:41:12 +01:00
// Common standard library headers
#include <complex.h> // IWYU pragma: export
#include <ctype.h> // IWYU pragma: export
#include <float.h> // IWYU pragma: export
#include <inttypes.h> // IWYU pragma: export
#include <limits.h> // IWYU pragma: export
#include <math.h> // IWYU pragma: export
#include <stdalign.h> // IWYU pragma: export
#include <stdbool.h> // IWYU pragma: export
#include <stddef.h> // IWYU pragma: export
#include <stdint.h> // IWYU pragma: export
#include <stdlib.h> // IWYU pragma: export
#include <stdnoreturn.h> // IWYU pragma: export
#include <string.h> // IWYU pragma: export
2023-03-26 03:08:33 +02:00
// clang defines this too
#ifndef __GNUC__
#warning Unsupported compiler. Only GCC and Clang are officially supported. Expect errors.
#endif
#ifdef TAISEI_BUILDCONF_REL_SRC_DIR
#define _TAISEI_SRC_FILE ((const char *)__FILE__ + sizeof(TAISEI_BUILDCONF_REL_SRC_DIR) - 1)
#else
#define _TAISEI_SRC_FILE __FILE__
#endif
#include "util/assert.h" // IWYU pragma: export
2018-07-04 10:55:33 +02:00
#ifdef __FAST_MATH__
#error -ffast-math is prohibited
#endif
#ifdef _WIN32
// Include the god-awful windows.h header here so that we can fight the obnoxious namespace pollution it introduces.
// We obviously don't need it in every source file, but it's easier to do it globally and early than to try to infer
// just where down the maze of includes some of our dependencies happens to smuggle it in.
//
// *sigh*
//
// Goddamn it.
// Make sure we get the "unicode" (actually UTF-16) versions of win32 APIs; it defaults to legacy crippled ones.
#ifndef UNICODE
#define UNICODE 1
#endif
#ifndef _UNICODE
#define _UNICODE 1
#endif
// Ask windows.h to include a little bit less of the stupid crap we'll never use.
// Some of it actually clashes with our names.
#define WIN32_LEAN_AND_MEAN
#define NOGDI
2018-07-28 12:53:12 +02:00
#define NOMINMAX
#include <windows.h>
// far/near pointers are obviously very relevant for modern CPUs and totally deserve their very own, unprefixed keywords!
#undef near
#undef far
// fixup other random name clashes
#define mouse_event _taisei_mouse_event
#endif
// This macro should be provided by stddef.h, but in practice it sometimes is not.
#ifndef offsetof
2023-03-26 03:08:33 +02:00
#define offsetof(type, field) __builtin_offsetof(type, field)
#endif
#define PRAGMA(p) _Pragma(#p)
#define UNREACHABLE ({ \
assert(0, "This code should never be reachable"); \
__builtin_unreachable(); \
})
2023-03-26 03:08:33 +02:00
#define DIAGNOSTIC(x) PRAGMA(GCC diagnostic x)
2023-03-26 03:08:33 +02:00
#if defined(__clang__)
#define DIAGNOSTIC_GCC(x)
2023-03-26 03:08:33 +02:00
#define DIAGNOSTIC_CLANG(x) PRAGMA(clang diagnostic x)
#else
2023-03-26 03:08:33 +02:00
#define DIAGNOSTIC_GCC(x) PRAGMA(GCC diagnostic x)
#define DIAGNOSTIC_CLANG(x)
#endif
2023-03-26 03:08:33 +02:00
#define LIKELY(x) __builtin_expect((bool)(x), 1)
#define UNLIKELY(x) __builtin_expect((bool)(x), 0)
#ifndef __has_attribute
2023-03-26 03:08:33 +02:00
// FIXME: maybe should be 0?
#define __has_attribute(attr) 1
#endif
#ifndef __has_feature
#define __has_feature(feature) 0
#endif
#undef ASSUME
#ifdef __has_builtin
#if __has_builtin(__builtin_assume)
#define ASSUME(x) __builtin_assume(x)
#endif
Texturing overhaul: GPU compression, sRGB sampling, swizzles, etc. (#240) * WIP compressed textures, swizzles, sRGB sampling, ... * refactor texture type info & fix random bugs * fix preprocessing of sRGB textures * handle y-flipped basis textures * glcommon: better WebGL compat for compressed format detection * missed WEBGL_compressed_texture_pvrtc * implement compressed texture xcoding and uploading * Add basis_universal submodule * Reorganize texture loader code Clean up some code Isolate Basis Universal loader into a separate module * Add wrapper script for encoding .basis textures * basisu: honor custom metadata written by the mkbasis.py script * mkbasis.py: add --incredibly-slow and --dry-run * Move pixmap code from util/ to pixmap/ * Add an on-disk transcode cache for basis textures to speed up loads * Compress texture cache with zlib * Use readable format names for basisu cache filenames * basisu: mip bias test code * basisu: small caching cleanup * add TAISEI_BASISU_MIP_BIAS env variable * Improve OpenGL format matching heuristics * Document considerations for compressed format priority * Remove dead code * Enable two forgotten formats, BC3_RGBA and ATC_RGBA Also prefer BC7 over BC1/BC3 * Recognize GL_ANGLE_compressed_texture_etc for ETC2 textures * Default depth buffers to 24-bit; remove ANGLE hack * Fix glcommon_check_extension for GLES2/legacy gl * Add renderer feature bit for texture swizzle masks * glcommon: Fixup internal formats for GLES2 Sized internal formats are not allowed in GLES2 * Fix emscripten compile errors * Update basis_universal * remove more dead code * revert irrelevant stage4 change * shut up UBSan * basisu: shut up some debug spam * Add normalmap sampling helper to util.glslh * basisu: add a gray-alpha mode * mkbasis.py: Abort if image dimansions aren't multiples of 4 * Add basic Basis Universal encoding documentation (WIP) * doc/basisu: Add paragraph about modes; minor tweaks * basisu: workarounds for GL texture size requirements * gles20: fix uncompressed sRGB formats * Partial workaround for missing swizzles in gles2 and webgl * remove invalid assertion * New renderer API to expose glDrawBuffers-like functionality * stagedraw: disable all color outputs for copy_depth pass required for WebGL compatibility * support GL_ANGLE_request_extension * emscripten: include *.basis in gfx package Also fix a potential problem when more than one .pkgdir is used to construct emscripten packages * Don't rely on emscripten runtime to enable webgl extensions
2020-08-15 13:51:12 +02:00
#if __has_builtin(__builtin_popcount)
#undef TAISEI_BUILDCONF_HAVE_BUILTIN_POPCOUNT
#define TAISEI_BUILDCONF_HAVE_BUILTIN_POPCOUNT 1
#endif
#if __has_builtin(__builtin_popcountll)
#undef TAISEI_BUILDCONF_HAVE_BUILTIN_POPCOUNTLL
#define TAISEI_BUILDCONF_HAVE_BUILTIN_POPCOUNTLL 1
#endif
#endif
2023-03-26 03:08:33 +02:00
#if !defined(ASSUME)
#define ASSUME(x) do { if(!(x)) { UNREACHABLE; } } while(0)
#endif
// On windows, use the MinGW implementations of printf and friends instead of the crippled mscrt ones.
#ifdef __USE_MINGW_ANSI_STDIO
#define FORMAT_ATTR __MINGW_PRINTF_FORMAT
#else
#define FORMAT_ATTR printf
#endif
#undef uint
typedef unsigned int uint;
#undef ushort
typedef unsigned short ushort;
#undef ulong
typedef unsigned long ulong;
#undef uchar
typedef unsigned char uchar;
#undef schar
typedef signed char schar;
#undef real
typedef double real;
#undef cmplxf
typedef _Complex float cmplxf;
#undef cmplx
typedef _Complex double cmplx;
// These definitions are common but non-standard, so we provide our own
#undef M_PI
#undef M_PI_2
#undef M_PI_4
#undef M_E
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
#define M_E 2.7182818284590452354
2019-12-18 14:33:36 +01:00
#ifndef TAISEI_BUILDCONF_HAVE_MAX_ALIGN_T
#if TAISEI_BUILDCONF_MALLOC_ALIGNMENT <= 0
#warning malloc alignment is unknown, assuming 8
#undef TAISEI_BUILDCONF_MALLOC_ALIGNMENT
#define TAISEI_BUILDCONF_MALLOC_ALIGNMENT 8
#endif
#undef max_align_t
#define max_align_t _fake_max_align_t
typedef struct { alignas(TAISEI_BUILDCONF_MALLOC_ALIGNMENT) char a; } max_align_t;
#endif
// polyfill CMPLX macros
#include "compat_cmplx.h" // IWYU pragma: export
/*
* Abstract away the nasty GNU attribute syntax.
*/
// Function is a hot spot.
#define attr_hot \
__attribute__ ((hot))
// Function has no side-effects.
#define attr_pure \
__attribute__ ((pure))
// Function has no side-effects, return value depends on arguments only.
// Must not take pointer parameters, must not return void.
#define attr_const \
__attribute__ ((const))
// Function never returns NULL.
#define attr_returns_nonnull \
__attribute__ ((returns_nonnull))
// Function must be called with NULL as the last argument (for varargs functions).
#define attr_sentinel \
__attribute__ ((sentinel))
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
// Symbol is meant to be possibly unused.
#define attr_unused \
__attribute__ ((unused))
Emscripten compatibility (#161) * Major refactoring of the main loop(s) and control flow (WIP) run_at_fps() is gone 🦀 Instead of nested blocking event loops, there is now an eventloop API that manages an explicit stack of scenes. This makes Taisei a lot more portable to async environments where spinning a loop forever without yielding control simply is not an option, and that is the entire point of this change. A prime example of such an environment is the Web (via emscripten). Taisei was able to run there through a terrible hack: inserting emscripten_sleep calls into the loop, which would yield to the browser. This has several major drawbacks: first of all, every function that could possibly call emscripten_sleep must be compiled into a special kind of bytecode, which then has to be interpreted at runtime, *much* slower than JITed WebAssembly. And that includes *everything* down the call stack, too! For more information, see https://emscripten.org/docs/porting/emterpreter.html Even though that method worked well enough for experimenting, despite suboptimal performance, there is another obvious drawback: emscripten_sleep is implemented via setTimeout(), which can be very imprecise and is generally not reliable for fluid animation. Browsers actually have an API specifically for that use case: window.requestAnimationFrame(), but Taisei's original blocking control flow style is simply not compatible with it. Emscripten exposes this API with its emscripten_set_main_loop(), which the eventloop backend now uses on that platform. Unfortunately, C is still C, with no fancy closures or coroutines. With blocking calls into menu/scene loops gone, the control flow is reimplemented via so-called (pun intended) "call chains". That is basically an euphemism for callback hell. With manual memory management and zero type-safety. Not that the menu system wasn't shitty enough already. I'll just keep telling myself that this is all temporary and will be replaced with scripts in v1.4. * improve build system for emscripten + various fixes * squish menu bugs * improve emscripten event loop; disable EMULATE_FUNCTION_POINTER_CASTS Note that stock freetype does not work without EMULATE_FUNCTION_POINTER_CASTS; use a patched version from the "emscripten" branch here: https://github.com/taisei-project/freetype2/tree/emscripten * Enable -Wcast-function-type Calling functions through incompatible pointers is nasal demons and doesn't work in WASM. * webgl: workaround a crash on some browsers * emscripten improvements: * Persist state (config, progress, replays, ...) in local IndexDB * Simpler HTML shell (temporary) * Enable more optimizations * fix build if validate_glsl=false * emscripten: improve asset packaging, with local cache Note that even though there are rules to build audio bundles, audio does *not* work yet. It looks like SDL2_mixer can not work without threads, which is a problem. Yet another reason to write an OpenAL backend - emscripten supports that natively. * emscripten: customize the html shell * emscripten: force "show log" checkbox unchecked initially * emscripten: remove quit shortcut from main menu (since there's no quit) * emscripten: log area fixes * emscripten/webgl: workaround for fullscreen viewport issue * emscripten: implement frameskip * emscripter: improve framerate limiter * align List to at least 8 bytes (shut up warnings) * fix non-emscripten builds * improve fullscreen handling, mainly for emscripten * Workaround to make audio work in chromium emscripten-core/emscripten#6511 * emscripten: better vsync handling; enable vsync & disable fxaa by default
2019-03-09 20:32:32 +01:00
// Symbol should be emitted even if it appears to be unused.
#define attr_used \
__attribute__ ((used))
// Function or type is deprecated and should not be used.
#define attr_deprecated(msg) \
__attribute__ ((deprecated(msg)))
// Function parameters at specified positions must not be NULL.
#define attr_nonnull(...) \
__attribute__ ((nonnull(__VA_ARGS__)))
// All pointer parameters must not be NULL.
#define attr_nonnull_all \
__attribute__ ((nonnull))
// The return value of this function must not be ignored.
#define attr_nodiscard \
__attribute__ ((warn_unused_result))
// Function takes a printf-style format string and variadic arguments.
#define attr_printf(fmt_index, firstarg_index) \
__attribute__ ((format(FORMAT_ATTR, fmt_index, firstarg_index)))
// Function must be inlined regardless of optimization settings.
#define attr_must_inline \
__attribute__ ((always_inline))
2019-04-12 10:36:40 +02:00
// Function returns a pointer aligned to x bytes
#define attr_returns_aligned(x) \
__attribute__ ((assume_aligned(x)))
// Function returns a pointer aligned the same as max_align_t
#define attr_returns_max_aligned \
attr_returns_aligned(alignof(max_align_t))
// Shorthand: always returns non-null pointer aligned to max_align_t; no discard.
#define attr_returns_allocated \
attr_returns_nonnull attr_returns_max_aligned attr_nodiscard
// Structure must not be initialized with an implicit (non-designated) initializer.
#if __has_attribute(designated_init) && defined(TAISEI_BUILDCONF_HAVE_ATTR_DESIGNATED_INIT)
#define attr_designated_init \
__attribute__ ((designated_init))
#else
#define attr_designated_init
#endif
// Function returns a pointer that can't alias any other pointer when the function returns.
// Storage pointed at doesn't contain pointers to any valid objects.
#define attr_malloc \
__attribute__ ((malloc))
#ifdef __clang__
#undef TAISEI_BUILDCONF_HAVE_ATTR_MALLOC_WITH_ARGS
#endif
// Function returns a pointer that must be 'freed' with the specified deallocator function
#ifdef TAISEI_BUILDCONF_HAVE_ATTR_MALLOC_WITH_ARGS
#define attr_dealloc(deallocator, arg_index) \
__attribute__ ((malloc(deallocator, arg_index)))
#else
#define attr_dealloc(deallocator, arg_index)
#endif
// With one argument n: function returns a pointer to object whose size is specified by the
// nth argument.
// With two arguments n, m: function returns a pointer to object whose size is specified by the
// product of nth and mth arguments.
#define attr_alloc_size(...) \
__attribute__ ((alloc_size(__VA_ARGS__)))
// Function returns a pointer aligned to a byte boundary specified by nth argument
#define attr_alloc_align(arg_index) \
__attribute__ ((alloc_align(arg_index)))
#define INLINE static inline attr_must_inline __attribute__((gnu_inline))
#define ASSUME_ALIGNED(expr, alignment) ({ \
static_assert(__builtin_constant_p(alignment), ""); \
auto _assume_aligned_ptr = (expr); \
assert(((uintptr_t)_assume_aligned_ptr & ((alignment) - 1)) == 0); \
__builtin_assume_aligned(_assume_aligned_ptr, (alignment)); \
})
#define UNION_CAST(_from_type, _to_type, _expr) \
((union { _from_type f; _to_type t; }) { .f = (_expr) }).t
#define CASTPTR_ASSUME_ALIGNED(expr, type) ((type*)ASSUME_ALIGNED((expr), alignof(type)))
#define NOT_NULL(expr) ({ \
auto _assume_not_null_ptr = (expr); \
assume(_assume_not_null_ptr != NULL); \
_assume_not_null_ptr; \
})
#ifdef __SWITCH__
#include "../arch_switch.h"
#define atexit nxAtExit
#define exit nxExit
#define abort nxAbort
#endif
#ifdef RNG_API_CHECK
#define _Generic(ignore, ...) _Generic(0, __VA_ARGS__)
#endif
#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
#define ADDRESS_SANITIZER
#endif
// `auto` for type inference is standardized in C23 based on GCC's __auto_type semantics.
// We want to have it now, and we don't care about the useless original purpose of C's `auto`.
// from __future__ import auto
#define auto __auto_type