taisei/src/util/miscmath.h

176 lines
6.3 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-07-25 02:31:02 +02:00
#define DEG2RAD (M_PI / 180.0)
#define RAD2DEG (180.0 / M_PI)
2019-03-11 00:21:43 +01:00
#define GOLDEN_RATIO 1.618033988749895
2019-07-25 02:31:02 +02:00
#define M_TAU (M_PI * 2)
#define re(z) (__real__ (z))
#define im(z) (__imag__ (z))
double (creal)(cmplx z) attr_deprecated("Use re() instead");
double (cimag)(cmplx z) attr_deprecated("Use im() instead");
float (crealf)(cmplxf z) attr_deprecated("Use re() instead");
float (cimagf)(cmplxf z) attr_deprecated("Use im() instead");
float fminf(float x, float y) attr_deprecated("Use min() instead");
double fmin(double x, double y) attr_deprecated("Use min() instead");
float fmaxf(float x, float y) attr_deprecated("Use max() instead");
double fmax(double x, double y) attr_deprecated("Use max() instead");
#define min(a, b) ({ \
typeof((a)+(b)) _temp_a = (a); \
typeof((a)+(b)) _temp_b = (b); \
_temp_a > _temp_b ? _temp_b : _temp_a; \
})
#define max(a, b) ({ \
typeof((a)+(b)) _temp_a = (a); \
typeof((a)+(b)) _temp_b = (b); \
_temp_a > _temp_b ? _temp_a : _temp_b; \
})
#define clamp(x, a, b) min(max(x, a), b)
double lerp(double v0, double v1, double f) attr_const;
float lerpf(float v0, float v1, float f) attr_const;
cmplx clerp(cmplx v0, cmplx v1, double f) attr_const;
cmplxf clerpf(cmplxf v0, cmplxf v1, float f) attr_const;
intmax_t imin(intmax_t, intmax_t) attr_const attr_deprecated("Use min() instead");
intmax_t imax(intmax_t, intmax_t) attr_const attr_deprecated("Use max() instead");
uintmax_t umin(uintmax_t, uintmax_t) attr_const attr_deprecated("Use min() instead");
uintmax_t umax(uintmax_t, uintmax_t) attr_const attr_deprecated("Use max() instead");
intmax_t iclamp(intmax_t, intmax_t, intmax_t) attr_const attr_deprecated("Use clamp() instead");
float clampf(float, float, float) attr_const attr_deprecated("Use clamp() instead");
2019-01-10 00:56:33 +01:00
double smoothstep(double edge0, double edge1, double x) attr_const;
double smoothmin(double a, double b, double k) attr_const;
double approach(double v, double t, double d) attr_const;
Lots of disorganized (mostly) visual overhaul (#156) * WIP some projectile effects * fix segfault * Laser smoothing and glow via post-processing blur magic TODO: make it optional * fix memory corruption * fix memory corruption for realsies now * fix color_get_hsl for out-of-range colors * some more bullet flare tweaks * some lame clear effect workarounds * spawn bullet flares after frame 0; looks better and fixes some problems * New baryon explosion; fix petal_explosion; leanify everything * Add missing bullet flare sprite, rebuild main atlas * improve batching efficiency with bullet spawn flares * forgot git add * Group projectiles/particles by shader where possible * Another take on baryon explosion; make fg framebuffers 16bit * WIP some settings for toasters * remove stupid debug log * microoptimization that probably does nothing anyway * somewhat more intuitive quality settings * Whitelist more particles (MarisaB is on hold) * Whitelist (and fix) some more stage6 particles (mostly ToE) * Add a spell name background * Experimental radial healthbar for bosses * healthbar tweaks * thiccer healthbars in response to feedback * remove healthbar survival timer; just fade out on survivals * Add linear healthbars option; WIP other boss HUD tweaks * Use the proper spell card name format * New font and some random garbage to go along with it * Generate static font outlines for use in text shaders * Use outlines in overlay text shader * Complete boss HUD/healthbar fading logic * fix boss timer limit * stage5 bombs explosion effect * split PFLAG_NOSPAWNZOOM into PFLAG_NOSPAWNFLARE and PFLAG_NOSPAWNFADE; introduce PFLAG_NOSPAWNEFFECTS which disables both (it's just the two values OR'd together) simplify vampiric vapor bullet spawning effect * Remove spawn fade-in from super-fast stage5 fairy projectiles (limiters) * lower particle density in v.vapor in minimal mode * graze effect tweaks * fix text shortening, tweak replay menu layout * stupid debug spam * revisit grazing effects again * dumb debug spam again * improve boss attack timer * overlay effect for boss deaths (similar to the player one) * spice up spellcard declaration (HUD) * don't spawn boss death overlay if fleed * modify Exo2 font to use tabular figures * adjust replay menu for the font change * draw timer & power with standard font (phasing out the numbers font) * WIP new HUD; random fixes/tweaks * hud: move difficulty indicator * hud: move debug stuff around * preloads, mostly * fix youmuA batching conflict * shitty workaround for the shitty screenshake shit * remove extraspell lag by stopping to draw stagebg sooner which is possible because extra spells have a different spellcard_intro timing. Fun fact of the day: the duration of spellcard_intro is always ATTACK_START_DELAY_EXTRA even for normal spells! * new stain particle * i disabled background rendering… * "batch" marisa_b masterspark draws * remove these once a new atlas is generated * make toe quick again * hopefully fix all occurences of changed stain and ScaleFade behavior * tweaking reimu_a and toe boson launch effects * make lhc fast again * softer involnerability effect * fix stage 1 snow on the water bug (and improve performance) translated the time to the future a bit because it only seemed to be an issue for small time values * remove unnecessary spawnflare from toe * tone down extra spell start effect * experimental ReimuB gap shader optimization * fix python3 shebangs * generate simple blur shaders w/ hardcoded kernels * New loading screen * lasers: fix incorrect draw hook registration * add webp support for atlas generator * Use ImageMagick for atlas composition (adds 16-bit support) * Atlas maintenance * make the vampiric vapor bullets less prone to invisibility * Revert a few particles to the quadratic fade curve * experimental baryon effect * improve baryon sprites * disable the baryon effect on minimal postprocessing setting
2019-01-04 23:59:39 +01:00
float fapproach(float v, float t, float d) attr_const;
double approach_p(double *v, double t, double d);
float fapproach_p(float *v, float t, float d);
Lots of disorganized (mostly) visual overhaul (#156) * WIP some projectile effects * fix segfault * Laser smoothing and glow via post-processing blur magic TODO: make it optional * fix memory corruption * fix memory corruption for realsies now * fix color_get_hsl for out-of-range colors * some more bullet flare tweaks * some lame clear effect workarounds * spawn bullet flares after frame 0; looks better and fixes some problems * New baryon explosion; fix petal_explosion; leanify everything * Add missing bullet flare sprite, rebuild main atlas * improve batching efficiency with bullet spawn flares * forgot git add * Group projectiles/particles by shader where possible * Another take on baryon explosion; make fg framebuffers 16bit * WIP some settings for toasters * remove stupid debug log * microoptimization that probably does nothing anyway * somewhat more intuitive quality settings * Whitelist more particles (MarisaB is on hold) * Whitelist (and fix) some more stage6 particles (mostly ToE) * Add a spell name background * Experimental radial healthbar for bosses * healthbar tweaks * thiccer healthbars in response to feedback * remove healthbar survival timer; just fade out on survivals * Add linear healthbars option; WIP other boss HUD tweaks * Use the proper spell card name format * New font and some random garbage to go along with it * Generate static font outlines for use in text shaders * Use outlines in overlay text shader * Complete boss HUD/healthbar fading logic * fix boss timer limit * stage5 bombs explosion effect * split PFLAG_NOSPAWNZOOM into PFLAG_NOSPAWNFLARE and PFLAG_NOSPAWNFADE; introduce PFLAG_NOSPAWNEFFECTS which disables both (it's just the two values OR'd together) simplify vampiric vapor bullet spawning effect * Remove spawn fade-in from super-fast stage5 fairy projectiles (limiters) * lower particle density in v.vapor in minimal mode * graze effect tweaks * fix text shortening, tweak replay menu layout * stupid debug spam * revisit grazing effects again * dumb debug spam again * improve boss attack timer * overlay effect for boss deaths (similar to the player one) * spice up spellcard declaration (HUD) * don't spawn boss death overlay if fleed * modify Exo2 font to use tabular figures * adjust replay menu for the font change * draw timer & power with standard font (phasing out the numbers font) * WIP new HUD; random fixes/tweaks * hud: move difficulty indicator * hud: move debug stuff around * preloads, mostly * fix youmuA batching conflict * shitty workaround for the shitty screenshake shit * remove extraspell lag by stopping to draw stagebg sooner which is possible because extra spells have a different spellcard_intro timing. Fun fact of the day: the duration of spellcard_intro is always ATTACK_START_DELAY_EXTRA even for normal spells! * new stain particle * i disabled background rendering… * "batch" marisa_b masterspark draws * remove these once a new atlas is generated * make toe quick again * hopefully fix all occurences of changed stain and ScaleFade behavior * tweaking reimu_a and toe boson launch effects * make lhc fast again * softer involnerability effect * fix stage 1 snow on the water bug (and improve performance) translated the time to the future a bit because it only seemed to be an issue for small time values * remove unnecessary spawnflare from toe * tone down extra spell start effect * experimental ReimuB gap shader optimization * fix python3 shebangs * generate simple blur shaders w/ hardcoded kernels * New loading screen * lasers: fix incorrect draw hook registration * add webp support for atlas generator * Use ImageMagick for atlas composition (adds 16-bit support) * Atlas maintenance * make the vampiric vapor bullets less prone to invisibility * Revert a few particles to the quadratic fade curve * experimental baryon effect * improve baryon sprites * disable the baryon effect on minimal postprocessing setting
2019-01-04 23:59:39 +01:00
double approach_asymptotic(double val, double target, double rate, double epsilon) attr_const;
float fapproach_asymptotic(float val, float target, float rate, float epsilon) attr_const;
cmplx capproach_asymptotic(cmplx val, cmplx target, double rate, double epsilon) attr_const;
double approach_asymptotic_p(double *val, double target, double rate, double epsilon);
float fapproach_asymptotic_p(float *val, float target, float rate, float epsilon);
cmplx capproach_asymptotic_p(cmplx *val, cmplx target, double rate, double epsilon);
cmplx cnormalize(cmplx c) attr_const;
cmplx cclampabs(cmplx c, double maxabs) attr_const;
2020-03-25 07:52:18 +01:00
cmplx cwclamp(cmplx c, cmplx cmin, cmplx cmax) attr_const;
cmplx cdir(double angle) attr_const;
cmplx cwmul(cmplx c0, cmplx c1) attr_const;
cmplxf cwmulf(cmplxf c0, cmplxf c1) attr_const;
cmplx cwdiv(cmplx c0, cmplx c1) attr_const;
cmplxf cwdivf(cmplxf c0, cmplxf c1) attr_const;
2020-03-25 07:52:18 +01:00
cmplx cswap(cmplx c) attr_const;
2024-03-21 23:20:47 +01:00
cmplxf cswapf(cmplxf c) attr_const;
double ccross(cmplx a, cmplx b) attr_const;
float ccrossf(cmplxf a, cmplxf b) attr_const;
cmplx csort(cmplx z) attr_const;
cmplxf csortf(cmplxf z) attr_const;
double psin(double) attr_const;
2020-02-01 00:37:13 +01:00
double pcos(double) attr_const;
float psinf(float) attr_const;
float pcosf(float) attr_const;
int sign(double) attr_const;
double swing(double x, double s) attr_const;
2019-12-04 19:06:42 +01:00
double sawtooth(double x) attr_const;
double triangle(double x) attr_const;
2019-04-12 10:36:40 +02:00
uint32_t topow2_u32(uint32_t x) attr_const;
uint64_t topow2_u64(uint64_t x) attr_const;
float smooth(float x) attr_const;
2019-07-25 02:31:02 +02:00
double circle_angle(double index, double max_elements) attr_const;
2019-12-18 15:19:13 +01:00
cmplx circle_dir(double index, double max_elements) attr_const;
cmplx circle_dir_ofs(double index, double max_elements, double ofs) attr_const;
uint64_t upow10(uint n) attr_const;
uint digitcnt(uint64_t x) attr_const;
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
uint64_t uceildiv64(uint64_t x, uint64_t y) attr_const;
int popcnt32(uint32_t) attr_const;
int popcnt64(uint64_t) attr_const;
// Compute (a*b)/c with 128-bit intermediate precision.
// If the final result would not fit into 64 bits, the return value is undefined.
uint64_t umuldiv64(uint64_t x, uint64_t multiplier, uint64_t divisor);
#define ASSUME_FINITE(x) ({ \
auto _temp = (x); \
assert(isfinite(_temp)) ; \
_temp; \
})
INLINE double cabs2(cmplx c) {
return re(c) * re(c) + im(c) * im(c);
}
INLINE float cabs2f(cmplxf c) {
return re(c) * re(c) + im(c) * im(c);
}
INLINE double cdot(cmplx c0, cmplx c1) {
return re(c0) * re(c1) + im(c0) * im(c1);
}
INLINE float cdotf(cmplxf c0, cmplxf c1) {
return re(c0) * re(c1) + im(c0) * im(c1);
}
INLINE cmplx cmul_finite(cmplx a, cmplx b) {
double ra = ASSUME_FINITE(re(a));
double ia = ASSUME_FINITE(im(a));
double rb = ASSUME_FINITE(re(b));
double ib = ASSUME_FINITE(im(b));
return CMPLX(ra * rb - ia * ib, ra * ib + ia * rb);
}
INLINE cmplxf cmulf_finite(cmplxf a, cmplxf b) {
float ra = ASSUME_FINITE(re(a));
float ia = ASSUME_FINITE(im(a));
float rb = ASSUME_FINITE(re(b));
float ib = ASSUME_FINITE(im(b));
return CMPLXF(ra * rb - ia * ib, ra * ib + ia * rb);
}
INLINE cmplx cdiv_finite(cmplx a, cmplx b) {
double ra = ASSUME_FINITE(re(a));
double ia = ASSUME_FINITE(im(a));
double rb = ASSUME_FINITE(re(b));
double ib = ASSUME_FINITE(im(b));
double denom = rb * rb + ib * ib;
return CMPLX((ra*rb + ia*ib) / denom, (ia*rb - ra*ib) / denom);
}
INLINE cmplxf cdivf_finite(cmplxf a, cmplxf b) {
float ra = ASSUME_FINITE(re(a));
float ia = ASSUME_FINITE(im(a));
float rb = ASSUME_FINITE(re(b));
float ib = ASSUME_FINITE(im(b));
float denom = rb * rb + ib * ib;
return CMPLXF((ra*rb + ia*ib) / denom, (ia*rb - ra*ib) / denom);
}
#define topow2(x) (_Generic((x), \
uint32_t: topow2_u32, \
uint64_t: topow2_u64, \
default: topow2_u64 \
)(x))
#include <cglm/types.h>
typedef float vec2_noalign[2];
typedef int ivec2_noalign[2];
typedef float vec3_noalign[3];
typedef int ivec3_noalign[3];
typedef float vec4_noalign[4];
typedef int ivec4_noalign[4];
typedef vec3_noalign mat3_noalign[3];
typedef vec4_noalign mat4_noalign[4];