| 1 |
#pragma once |
| 2 |
|
| 3 |
/** |
| 4 |
@file SwShaderRuntime.h |
| 5 |
|
| 6 |
C++ runtime library the GLSL-to-C++ transpiler (Utilities/ShaderCompiler/GlslToCpp) targets. |
| 7 |
|
| 8 |
The offline transpiler lowers a shader's fragment GLSL into a plain C++ function that the CPU |
| 9 |
software renderer calls once per pixel, so an effect runs in software exactly as its GLSL |
| 10 |
specifies instead of being hand-ported. That generated code is written against the small value |
| 11 |
types and free functions defined here: GLSL-shaped `vec2`/`vec3`/`vec4` (plus minimal integer and |
| 12 |
boolean vectors), the arithmetic operators and the built-in functions the shader corpus uses, a |
| 13 |
`swTexture()` sampler shim over @ref SwTexture and a `packColor()` writer to the rasterizer's RGBA8 |
| 14 |
output pixel. |
| 15 |
|
| 16 |
Swizzle-access convention (chosen so the emitter never needs C++ lvalue swizzles): |
| 17 |
- A single component is a plain field: `.x`/`.y`/`.z`/`.w`, aliased through a union to `.r`/`.g`/`.b`/`.a` |
| 18 |
and `.s`/`.t`/`.p`/`.q`. Because it is a real member it works as both a read and a write target, so |
| 19 |
the emitter maps `v.x` and an assignment `v.x = ...` straight through. |
| 20 |
- A multi-component read swizzle (`v.rgb`, `v.xy`) is a const method the emitter calls: `v.rgb()`, |
| 21 |
`v.xy()`. The base is evaluated once and a fresh vector is returned. Only the common/contiguous |
| 22 |
swizzles are provided here (see the file tail) — the full permutation set is a |
| 23 |
mechanical later completion. |
| 24 |
- A multi-component write swizzle (`v.xy = ...`) is NOT supported; the emitter would have to |
| 25 |
decompose it into per-component field writes. No corpus shader needs it yet. |
| 26 |
*/ |
| 27 |
|
| 28 |
#if defined(WITH_RHI_SOFTWARE) |
| 29 |
|
| 30 |
#include "SwRaster.h" |
| 31 |
#include "SwTexture.h" |
| 32 |
#include "../RhiTypes.h" |
| 33 |
|
| 34 |
#include <cmath> |
| 35 |
#include <cstdint> |
| 36 |
|
| 37 |
// Anonymous structs inside a union give the .x/.r/.s aliasing; they are a well-supported extension |
| 38 |
// that MSVC flags only at /W4. Silence it just around the value types. |
| 39 |
#if defined(_MSC_VER) |
| 40 |
# pragma warning(push) |
| 41 |
# pragma warning(disable: 4201) |
| 42 |
#endif |
| 43 |
|
| 44 |
namespace nCine::RHI::Software::sw |
| 45 |
{ |
| 46 |
struct vec2; |
| 47 |
struct vec3; |
| 48 |
struct vec4; |
| 49 |
|
| 50 |
/** |
| 51 |
@brief Two-component float vector mirroring GLSL `vec2` |
| 52 |
|
| 53 |
Components are reachable as `.x`/`.y`, `.r`/`.g` or `.s`/`.t` (union-aliased); multi-component |
| 54 |
read swizzles are const methods (@ref xy() etc.). |
| 55 |
*/ |
| 56 |
struct vec2 |
| 57 |
{ |
| 58 |
union { |
| 59 |
struct { float x, y; }; |
| 60 |
struct { float r, g; }; |
| 61 |
struct { float s, t; }; |
| 62 |
}; |
| 63 |
|
| 64 |
vec2() : x(0.0f), y(0.0f) {} |
| 65 |
explicit vec2(float v) : x(v), y(v) {} |
| 66 |
vec2(float x_, float y_) : x(x_), y(y_) {} |
| 67 |
// Truncating constructors from wider vectors (defined out of line once vec3/vec4 are complete) |
| 68 |
explicit vec2(const vec3& v); |
| 69 |
explicit vec2(const vec4& v); |
| 70 |
|
| 71 |
vec2& operator+=(const vec2& o) { x += o.x; y += o.y; return *this; } |
| 72 |
vec2& operator-=(const vec2& o) { x -= o.x; y -= o.y; return *this; } |
| 73 |
vec2& operator*=(const vec2& o) { x *= o.x; y *= o.y; return *this; } |
| 74 |
vec2& operator/=(const vec2& o) { x /= o.x; y /= o.y; return *this; } |
| 75 |
vec2& operator*=(float o) { x *= o; y *= o; return *this; } |
| 76 |
vec2& operator/=(float o) { x /= o; y /= o; return *this; } |
| 77 |
|
| 78 |
vec2 xy() const { return vec2(x, y); } |
| 79 |
vec2 rg() const { return vec2(x, y); } |
| 80 |
vec2 xx() const { return vec2(x, x); } |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
@brief Three-component float vector mirroring GLSL `vec3` |
| 85 |
|
| 86 |
Components are reachable as `.x`/`.y`/`.z`, `.r`/`.g`/`.b` or `.s`/`.t`/`.p` (union-aliased). |
| 87 |
*/ |
| 88 |
struct vec3 |
| 89 |
{ |
| 90 |
union { |
| 91 |
struct { float x, y, z; }; |
| 92 |
struct { float r, g, b; }; |
| 93 |
struct { float s, t, p; }; |
| 94 |
}; |
| 95 |
|
| 96 |
vec3() : x(0.0f), y(0.0f), z(0.0f) {} |
| 97 |
explicit vec3(float v) : x(v), y(v), z(v) {} |
| 98 |
vec3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} |
| 99 |
vec3(const vec2& xy_, float z_) : x(xy_.x), y(xy_.y), z(z_) {} |
| 100 |
vec3(float x_, const vec2& yz_) : x(x_), y(yz_.x), z(yz_.y) {} |
| 101 |
explicit vec3(const vec4& v); // Truncation (defined out of line) |
| 102 |
|
| 103 |
vec3& operator+=(const vec3& o) { x += o.x; y += o.y; z += o.z; return *this; } |
| 104 |
vec3& operator-=(const vec3& o) { x -= o.x; y -= o.y; z -= o.z; return *this; } |
| 105 |
vec3& operator*=(const vec3& o) { x *= o.x; y *= o.y; z *= o.z; return *this; } |
| 106 |
vec3& operator/=(const vec3& o) { x /= o.x; y /= o.y; z /= o.z; return *this; } |
| 107 |
vec3& operator*=(float o) { x *= o; y *= o; z *= o; return *this; } |
| 108 |
vec3& operator/=(float o) { x /= o; y /= o; z /= o; return *this; } |
| 109 |
|
| 110 |
vec2 xy() const { return vec2(x, y); } |
| 111 |
vec2 rg() const { return vec2(x, y); } |
| 112 |
vec2 xx() const { return vec2(x, x); } |
| 113 |
vec3 xyz() const { return vec3(x, y, z); } |
| 114 |
vec3 rgb() const { return vec3(x, y, z); } |
| 115 |
}; |
| 116 |
|
| 117 |
/** |
| 118 |
@brief Four-component float vector mirroring GLSL `vec4` |
| 119 |
|
| 120 |
Components are reachable as `.x`/`.y`/`.z`/`.w`, `.r`/`.g`/`.b`/`.a` or `.s`/`.t`/`.p`/`.q` |
| 121 |
(union-aliased). |
| 122 |
*/ |
| 123 |
struct vec4 |
| 124 |
{ |
| 125 |
union { |
| 126 |
struct { float x, y, z, w; }; |
| 127 |
struct { float r, g, b, a; }; |
| 128 |
struct { float s, t, p, q; }; |
| 129 |
}; |
| 130 |
|
| 131 |
vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) {} |
| 132 |
explicit vec4(float v) : x(v), y(v), z(v), w(v) {} |
| 133 |
vec4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {} |
| 134 |
vec4(const vec3& xyz_, float w_) : x(xyz_.x), y(xyz_.y), z(xyz_.z), w(w_) {} |
| 135 |
vec4(float x_, const vec3& yzw_) : x(x_), y(yzw_.x), z(yzw_.y), w(yzw_.z) {} |
| 136 |
vec4(const vec2& xy_, const vec2& zw_) : x(xy_.x), y(xy_.y), z(zw_.x), w(zw_.y) {} |
| 137 |
vec4(const vec2& xy_, float z_, float w_) : x(xy_.x), y(xy_.y), z(z_), w(w_) {} |
| 138 |
vec4(float x_, float y_, const vec2& zw_) : x(x_), y(y_), z(zw_.x), w(zw_.y) {} |
| 139 |
|
| 140 |
vec4& operator+=(const vec4& o) { x += o.x; y += o.y; z += o.z; w += o.w; return *this; } |
| 141 |
vec4& operator-=(const vec4& o) { x -= o.x; y -= o.y; z -= o.z; w -= o.w; return *this; } |
| 142 |
vec4& operator*=(const vec4& o) { x *= o.x; y *= o.y; z *= o.z; w *= o.w; return *this; } |
| 143 |
vec4& operator/=(const vec4& o) { x /= o.x; y /= o.y; z /= o.z; w /= o.w; return *this; } |
| 144 |
vec4& operator*=(float o) { x *= o; y *= o; z *= o; w *= o; return *this; } |
| 145 |
vec4& operator/=(float o) { x /= o; y /= o; z /= o; w /= o; return *this; } |
| 146 |
|
| 147 |
vec2 xy() const { return vec2(x, y); } |
| 148 |
vec2 rg() const { return vec2(x, y); } |
| 149 |
vec2 xx() const { return vec2(x, x); } |
| 150 |
vec2 zw() const { return vec2(z, w); } |
| 151 |
vec2 ba() const { return vec2(z, w); } |
| 152 |
vec3 xyz() const { return vec3(x, y, z); } |
| 153 |
vec3 rgb() const { return vec3(x, y, z); } |
| 154 |
vec3 yzw() const { return vec3(y, z, w); } |
| 155 |
vec3 gba() const { return vec3(y, z, w); } |
| 156 |
vec4 xyzw() const { return vec4(x, y, z, w); } |
| 157 |
vec4 rgba() const { return vec4(x, y, z, w); } |
| 158 |
}; |
| 159 |
|
| 160 |
inline vec2::vec2(const vec3& v) : x(v.x), y(v.y) {} |
| 161 |
inline vec2::vec2(const vec4& v) : x(v.x), y(v.y) {} |
| 162 |
inline vec3::vec3(const vec4& v) : x(v.x), y(v.y), z(v.z) {} |
| 163 |
|
| 164 |
// --- Arithmetic operators (component-wise, with scalar broadcast on either side) --------------- |
| 165 |
|
| 166 |
#define SW_DEFINE_VEC2_OP(OP) \ |
| 167 |
inline vec2 operator OP(const vec2& a, const vec2& b) { return vec2(a.x OP b.x, a.y OP b.y); } \ |
| 168 |
inline vec2 operator OP(const vec2& a, float b) { return vec2(a.x OP b, a.y OP b); } \ |
| 169 |
inline vec2 operator OP(float a, const vec2& b) { return vec2(a OP b.x, a OP b.y); } |
| 170 |
#define SW_DEFINE_VEC3_OP(OP) \ |
| 171 |
inline vec3 operator OP(const vec3& a, const vec3& b) { return vec3(a.x OP b.x, a.y OP b.y, a.z OP b.z); } \ |
| 172 |
inline vec3 operator OP(const vec3& a, float b) { return vec3(a.x OP b, a.y OP b, a.z OP b); } \ |
| 173 |
inline vec3 operator OP(float a, const vec3& b) { return vec3(a OP b.x, a OP b.y, a OP b.z); } |
| 174 |
#define SW_DEFINE_VEC4_OP(OP) \ |
| 175 |
inline vec4 operator OP(const vec4& a, const vec4& b) { return vec4(a.x OP b.x, a.y OP b.y, a.z OP b.z, a.w OP b.w); } \ |
| 176 |
inline vec4 operator OP(const vec4& a, float b) { return vec4(a.x OP b, a.y OP b, a.z OP b, a.w OP b); } \ |
| 177 |
inline vec4 operator OP(float a, const vec4& b) { return vec4(a OP b.x, a OP b.y, a OP b.z, a OP b.w); } |
| 178 |
|
| 179 |
SW_DEFINE_VEC2_OP(+) SW_DEFINE_VEC2_OP(-) SW_DEFINE_VEC2_OP(*) SW_DEFINE_VEC2_OP(/) |
| 180 |
SW_DEFINE_VEC3_OP(+) SW_DEFINE_VEC3_OP(-) SW_DEFINE_VEC3_OP(*) SW_DEFINE_VEC3_OP(/) |
| 181 |
SW_DEFINE_VEC4_OP(+) SW_DEFINE_VEC4_OP(-) SW_DEFINE_VEC4_OP(*) SW_DEFINE_VEC4_OP(/) |
| 182 |
|
| 183 |
#undef SW_DEFINE_VEC2_OP |
| 184 |
#undef SW_DEFINE_VEC3_OP |
| 185 |
#undef SW_DEFINE_VEC4_OP |
| 186 |
|
| 187 |
inline vec2 operator-(const vec2& v) { return vec2(-v.x, -v.y); } |
| 188 |
inline vec3 operator-(const vec3& v) { return vec3(-v.x, -v.y, -v.z); } |
| 189 |
inline vec4 operator-(const vec4& v) { return vec4(-v.x, -v.y, -v.z, -v.w); } |
| 190 |
|
| 191 |
// --- Minimal integer / boolean vectors (types only; the corpus does not compute with them) ----- |
| 192 |
|
| 193 |
struct ivec2 { union { struct { std::int32_t x, y; }; struct { std::int32_t r, g; }; }; |
| 194 |
ivec2() : x(0), y(0) {} explicit ivec2(std::int32_t v) : x(v), y(v) {} ivec2(std::int32_t x_, std::int32_t y_) : x(x_), y(y_) {} }; |
| 195 |
struct ivec3 { union { struct { std::int32_t x, y, z; }; struct { std::int32_t r, g, b; }; }; |
| 196 |
ivec3() : x(0), y(0), z(0) {} explicit ivec3(std::int32_t v) : x(v), y(v), z(v) {} ivec3(std::int32_t x_, std::int32_t y_, std::int32_t z_) : x(x_), y(y_), z(z_) {} }; |
| 197 |
struct ivec4 { union { struct { std::int32_t x, y, z, w; }; struct { std::int32_t r, g, b, a; }; }; |
| 198 |
ivec4() : x(0), y(0), z(0), w(0) {} explicit ivec4(std::int32_t v) : x(v), y(v), z(v), w(v) {} ivec4(std::int32_t x_, std::int32_t y_, std::int32_t z_, std::int32_t w_) : x(x_), y(y_), z(z_), w(w_) {} }; |
| 199 |
|
| 200 |
struct bvec2 { union { struct { bool x, y; }; struct { bool r, g; }; }; |
| 201 |
bvec2() : x(false), y(false) {} explicit bvec2(bool v) : x(v), y(v) {} bvec2(bool x_, bool y_) : x(x_), y(y_) {} }; |
| 202 |
struct bvec3 { union { struct { bool x, y, z; }; struct { bool r, g, b; }; }; |
| 203 |
bvec3() : x(false), y(false), z(false) {} explicit bvec3(bool v) : x(v), y(v), z(v) {} bvec3(bool x_, bool y_, bool z_) : x(x_), y(y_), z(z_) {} }; |
| 204 |
struct bvec4 { union { struct { bool x, y, z, w; }; struct { bool r, g, b, a; }; }; |
| 205 |
bvec4() : x(false), y(false), z(false), w(false) {} explicit bvec4(bool v) : x(v), y(v), z(v), w(v) {} bvec4(bool x_, bool y_, bool z_, bool w_) : x(x_), y(y_), z(z_), w(w_) {} }; |
| 206 |
|
| 207 |
// --- Built-in functions ------------------------------------------------------------------------- |
| 208 |
// |
| 209 |
// Scalar overloads first, then component-wise vector overloads. GLSL semantics are matched exactly |
| 210 |
// (e.g. mod(x, y) = x - y * floor(x / y), and the geometric functions operate on the whole vector). |
| 211 |
|
| 212 |
inline float radians(float d) { return d * 0.01745329251994329577f; } |
| 213 |
inline float degrees(float r) { return r * 57.2957795130823208768f; } |
| 214 |
inline float sin(float x) { return std::sin(x); } |
| 215 |
inline float cos(float x) { return std::cos(x); } |
| 216 |
inline float tan(float x) { return std::tan(x); } |
| 217 |
inline float asin(float x) { return std::asin(x); } |
| 218 |
inline float acos(float x) { return std::acos(x); } |
| 219 |
inline float atan(float x) { return std::atan(x); } |
| 220 |
inline float atan(float y, float x) { return std::atan2(y, x); } |
| 221 |
inline float pow(float x, float y) { return std::pow(x, y); } |
| 222 |
inline float exp2(float x) { return std::exp2(x); } |
| 223 |
inline float log2(float x) { return std::log2(x); } |
| 224 |
inline float sqrt(float x) { return std::sqrt(x); } |
| 225 |
inline float inversesqrt(float x) { return 1.0f / std::sqrt(x); } |
| 226 |
inline float abs(float x) { return std::fabs(x); } |
| 227 |
inline float sign(float x) { return (x > 0.0f) ? 1.0f : (x < 0.0f ? -1.0f : 0.0f); } |
| 228 |
inline float floor(float x) { return std::floor(x); } |
| 229 |
inline float ceil(float x) { return std::ceil(x); } |
| 230 |
inline float round(float x) { return std::floor(x + 0.5f); } // GLSL round is unspecified at .5; match the shader idiom floor(x + 0.5) |
| 231 |
inline float fract(float x) { return x - std::floor(x); } |
| 232 |
inline float mod(float x, float y) { return x - y * std::floor(x / y); } |
| 233 |
inline float min(float x, float y) { return (x < y) ? x : y; } |
| 234 |
inline float max(float x, float y) { return (x > y) ? x : y; } |
| 235 |
inline float clamp(float x, float lo, float hi) { return (x < lo) ? lo : (x > hi ? hi : x); } |
| 236 |
inline float mix(float a, float b, float t) { return a * (1.0f - t) + b * t; } |
| 237 |
inline float step(float edge, float x) { return (x < edge) ? 0.0f : 1.0f; } |
| 238 |
inline float smoothstep(float e0, float e1, float x) { |
| 239 |
float t = clamp((x - e0) / (e1 - e0), 0.0f, 1.0f); |
| 240 |
return t * t * (3.0f - 2.0f * t); |
| 241 |
} |
| 242 |
|
| 243 |
// Component-wise unary vector wrappers over the scalar built-ins |
| 244 |
#define SW_UNARY(func) \ |
| 245 |
inline vec2 func(const vec2& v) { return vec2(func(v.x), func(v.y)); } \ |
| 246 |
inline vec3 func(const vec3& v) { return vec3(func(v.x), func(v.y), func(v.z)); } \ |
| 247 |
inline vec4 func(const vec4& v) { return vec4(func(v.x), func(v.y), func(v.z), func(v.w)); } |
| 248 |
SW_UNARY(sin) SW_UNARY(cos) SW_UNARY(tan) SW_UNARY(asin) SW_UNARY(acos) SW_UNARY(atan) |
| 249 |
SW_UNARY(exp2) SW_UNARY(log2) SW_UNARY(sqrt) SW_UNARY(inversesqrt) SW_UNARY(abs) SW_UNARY(sign) |
| 250 |
SW_UNARY(floor) SW_UNARY(ceil) SW_UNARY(round) SW_UNARY(fract) SW_UNARY(radians) SW_UNARY(degrees) |
| 251 |
#undef SW_UNARY |
| 252 |
|
| 253 |
// Component-wise binary vector wrappers (vec/vec and vec/scalar), matching GLSL's genType rules |
| 254 |
#define SW_BINARY(func) \ |
| 255 |
inline vec2 func(const vec2& a, const vec2& b) { return vec2(func(a.x, b.x), func(a.y, b.y)); } \ |
| 256 |
inline vec3 func(const vec3& a, const vec3& b) { return vec3(func(a.x, b.x), func(a.y, b.y), func(a.z, b.z)); } \ |
| 257 |
inline vec4 func(const vec4& a, const vec4& b) { return vec4(func(a.x, b.x), func(a.y, b.y), func(a.z, b.z), func(a.w, b.w)); } \ |
| 258 |
inline vec2 func(const vec2& a, float b) { return vec2(func(a.x, b), func(a.y, b)); } \ |
| 259 |
inline vec3 func(const vec3& a, float b) { return vec3(func(a.x, b), func(a.y, b), func(a.z, b)); } \ |
| 260 |
inline vec4 func(const vec4& a, float b) { return vec4(func(a.x, b), func(a.y, b), func(a.z, b), func(a.w, b)); } |
| 261 |
SW_BINARY(pow) SW_BINARY(mod) SW_BINARY(min) SW_BINARY(max) |
| 262 |
#undef SW_BINARY |
| 263 |
|
| 264 |
// step(edge, x): GLSL also broadcasts a scalar edge over a vector x |
| 265 |
inline vec2 step(const vec2& e, const vec2& x) { return vec2(step(e.x, x.x), step(e.y, x.y)); } |
| 266 |
inline vec3 step(const vec3& e, const vec3& x) { return vec3(step(e.x, x.x), step(e.y, x.y), step(e.z, x.z)); } |
| 267 |
inline vec4 step(const vec4& e, const vec4& x) { return vec4(step(e.x, x.x), step(e.y, x.y), step(e.z, x.z), step(e.w, x.w)); } |
| 268 |
inline vec2 step(float e, const vec2& x) { return vec2(step(e, x.x), step(e, x.y)); } |
| 269 |
inline vec3 step(float e, const vec3& x) { return vec3(step(e, x.x), step(e, x.y), step(e, x.z)); } |
| 270 |
inline vec4 step(float e, const vec4& x) { return vec4(step(e, x.x), step(e, x.y), step(e, x.z), step(e, x.w)); } |
| 271 |
|
| 272 |
// clamp(v, float, float) and clamp(v, v, v) |
| 273 |
inline vec2 clamp(const vec2& v, float lo, float hi) { return vec2(clamp(v.x, lo, hi), clamp(v.y, lo, hi)); } |
| 274 |
inline vec3 clamp(const vec3& v, float lo, float hi) { return vec3(clamp(v.x, lo, hi), clamp(v.y, lo, hi), clamp(v.z, lo, hi)); } |
| 275 |
inline vec4 clamp(const vec4& v, float lo, float hi) { return vec4(clamp(v.x, lo, hi), clamp(v.y, lo, hi), clamp(v.z, lo, hi), clamp(v.w, lo, hi)); } |
| 276 |
inline vec2 clamp(const vec2& v, const vec2& lo, const vec2& hi) { return vec2(clamp(v.x, lo.x, hi.x), clamp(v.y, lo.y, hi.y)); } |
| 277 |
inline vec3 clamp(const vec3& v, const vec3& lo, const vec3& hi) { return vec3(clamp(v.x, lo.x, hi.x), clamp(v.y, lo.y, hi.y), clamp(v.z, lo.z, hi.z)); } |
| 278 |
inline vec4 clamp(const vec4& v, const vec4& lo, const vec4& hi) { return vec4(clamp(v.x, lo.x, hi.x), clamp(v.y, lo.y, hi.y), clamp(v.z, lo.z, hi.z), clamp(v.w, lo.w, hi.w)); } |
| 279 |
|
| 280 |
// mix(v, v, float) and mix(v, v, v) |
| 281 |
inline vec2 mix(const vec2& a, const vec2& b, float t) { return vec2(mix(a.x, b.x, t), mix(a.y, b.y, t)); } |
| 282 |
inline vec3 mix(const vec3& a, const vec3& b, float t) { return vec3(mix(a.x, b.x, t), mix(a.y, b.y, t), mix(a.z, b.z, t)); } |
| 283 |
inline vec4 mix(const vec4& a, const vec4& b, float t) { return vec4(mix(a.x, b.x, t), mix(a.y, b.y, t), mix(a.z, b.z, t), mix(a.w, b.w, t)); } |
| 284 |
inline vec2 mix(const vec2& a, const vec2& b, const vec2& t) { return vec2(mix(a.x, b.x, t.x), mix(a.y, b.y, t.y)); } |
| 285 |
inline vec3 mix(const vec3& a, const vec3& b, const vec3& t) { return vec3(mix(a.x, b.x, t.x), mix(a.y, b.y, t.y), mix(a.z, b.z, t.z)); } |
| 286 |
inline vec4 mix(const vec4& a, const vec4& b, const vec4& t) { return vec4(mix(a.x, b.x, t.x), mix(a.y, b.y, t.y), mix(a.z, b.z, t.z), mix(a.w, b.w, t.w)); } |
| 287 |
|
| 288 |
// smoothstep(float, float, v) and smoothstep(v, v, v) |
| 289 |
inline vec2 smoothstep(float e0, float e1, const vec2& x) { return vec2(smoothstep(e0, e1, x.x), smoothstep(e0, e1, x.y)); } |
| 290 |
inline vec3 smoothstep(float e0, float e1, const vec3& x) { return vec3(smoothstep(e0, e1, x.x), smoothstep(e0, e1, x.y), smoothstep(e0, e1, x.z)); } |
| 291 |
inline vec4 smoothstep(float e0, float e1, const vec4& x) { return vec4(smoothstep(e0, e1, x.x), smoothstep(e0, e1, x.y), smoothstep(e0, e1, x.z), smoothstep(e0, e1, x.w)); } |
| 292 |
|
| 293 |
// Geometric functions |
| 294 |
inline float dot(const vec2& a, const vec2& b) { return a.x * b.x + a.y * b.y; } |
| 295 |
inline float dot(const vec3& a, const vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } |
| 296 |
inline float dot(const vec4& a, const vec4& b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; } |
| 297 |
inline vec3 cross(const vec3& a, const vec3& b) { return vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } |
| 298 |
inline float length(const vec2& v) { return std::sqrt(dot(v, v)); } |
| 299 |
inline float length(const vec3& v) { return std::sqrt(dot(v, v)); } |
| 300 |
inline float length(const vec4& v) { return std::sqrt(dot(v, v)); } |
| 301 |
inline float distance(const vec2& a, const vec2& b) { return length(a - b); } |
| 302 |
inline float distance(const vec3& a, const vec3& b) { return length(a - b); } |
| 303 |
inline float distance(const vec4& a, const vec4& b) { return length(a - b); } |
| 304 |
inline vec2 normalize(const vec2& v) { return v * (1.0f / length(v)); } |
| 305 |
inline vec3 normalize(const vec3& v) { return v * (1.0f / length(v)); } |
| 306 |
inline vec4 normalize(const vec4& v) { return v * (1.0f / length(v)); } |
| 307 |
inline vec2 reflect(const vec2& i, const vec2& n) { return i - n * (2.0f * dot(n, i)); } |
| 308 |
inline vec3 reflect(const vec3& i, const vec3& n) { return i - n * (2.0f * dot(n, i)); } |
| 309 |
inline vec4 reflect(const vec4& i, const vec4& n) { return i - n * (2.0f * dot(n, i)); } |
| 310 |
|
| 311 |
// --- Screen-space derivatives (approximated) ---------------------------------------------------- |
| 312 |
// |
| 313 |
// The per-pixel CPU path shades one pixel at a time with no 2x2 quad, so true screen-space |
| 314 |
// derivatives are unavailable. Shaders use them only to widen an anti-aliasing edge (the aastep/fwidth |
| 315 |
// idiom: smoothstep(t - fw, t + fw, v)), so a small constant is returned instead of 0 - that keeps the |
| 316 |
// edge soft but finite (avoiding a divide-by-zero in smoothstep) while letting derivative-based effects |
| 317 |
// (e.g. the frozen-enemy mask outline) run at approximate fidelity. The magnitude (~1/50 in value space) |
| 318 |
// is a rough stand-in for the per-pixel rate of change of a quantity that spans a typical sprite; the |
| 319 |
// exact value only affects edge softness. |
| 320 |
inline float dFdx(float) { return 0.02f; } |
| 321 |
inline float dFdy(float) { return 0.02f; } |
| 322 |
inline vec2 dFdx(const vec2&) { return vec2(0.02f); } |
| 323 |
inline vec2 dFdy(const vec2&) { return vec2(0.02f); } |
| 324 |
inline vec3 dFdx(const vec3&) { return vec3(0.02f); } |
| 325 |
inline vec3 dFdy(const vec3&) { return vec3(0.02f); } |
| 326 |
inline vec4 dFdx(const vec4&) { return vec4(0.02f); } |
| 327 |
inline vec4 dFdy(const vec4&) { return vec4(0.02f); } |
| 328 |
inline float fwidth(float) { return 0.04f; } // abs(dFdx) + abs(dFdy) |
| 329 |
inline vec2 fwidth(const vec2&) { return vec2(0.04f); } |
| 330 |
inline vec3 fwidth(const vec3&) { return vec3(0.04f); } |
| 331 |
inline vec4 fwidth(const vec4&) { return vec4(0.04f); } |
| 332 |
|
| 333 |
// --- Sampler shim ------------------------------------------------------------------------------- |
| 334 |
|
| 335 |
namespace detail |
| 336 |
{ |
| 337 |
/** Wraps a signed integer texel coordinate into `[0, dim)`, mirroring SwRaster's WrapTexelCoord */ |
| 338 |
inline std::int32_t wrapTexel(std::int32_t idx, std::int32_t dim, nCine::SamplerWrapping mode) |
| 339 |
{ |
| 340 |
// Fast path: an in-range coordinate maps to itself under every wrap mode, so the vast |
| 341 |
// majority of samples skip the mode switch (and Repeat's integer division) entirely |
| 342 |
if (static_cast<std::uint32_t>(idx) < static_cast<std::uint32_t>(dim)) { |
| 343 |
return idx; |
| 344 |
} |
| 345 |
switch (mode) { |
| 346 |
case nCine::SamplerWrapping::Repeat: |
| 347 |
idx %= dim; |
| 348 |
if (idx < 0) idx += dim; |
| 349 |
return idx; |
| 350 |
case nCine::SamplerWrapping::MirroredRepeat: { |
| 351 |
std::int32_t period = dim * 2; |
| 352 |
idx %= period; |
| 353 |
if (idx < 0) idx += period; |
| 354 |
if (idx >= dim) idx = period - 1 - idx; |
| 355 |
return idx; |
| 356 |
} |
| 357 |
default: // ClampToEdge (and Unknown) |
| 358 |
return (idx < 0) ? 0 : (idx >= dim ? dim - 1 : idx); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
@brief Floor-to-integer without the `std::floor` library call |
| 364 |
|
| 365 |
Bit-identical to `static_cast<std::int32_t>(std::floor(f))` for every value on which that |
| 366 |
conversion is defined (any `f` whose floor fits in `int32`): truncate toward zero, then step |
| 367 |
down one when truncation rounded up (negative non-integers). The `INT32_MIN` guard keeps the |
| 368 |
already-out-of-range garbage inputs (a domain the old expression handled as UB) from |
| 369 |
overflowing the adjustment. |
| 370 |
*/ |
| 371 |
inline std::int32_t floorToInt(float f) |
| 372 |
{ |
| 373 |
const std::int32_t i = static_cast<std::int32_t>(f); |
| 374 |
return (static_cast<float>(i) > f && i != INT32_MIN) ? i - 1 : i; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* @brief Samples a bound texture, the software counterpart of GLSL `texture(sampler, uv)` |
| 380 |
* |
| 381 |
* Reads the texture bound to @p unit of @p in with nearest filtering, honoring the texture's wrap |
| 382 |
* mode, and returns its texel as normalized `0..1` RGBA. @ref SwTexture stores R8/RG8 natively |
| 383 |
* (1 / 2 bytes per texel), so a fetched texel is first expanded to the 4-byte working form (missing |
| 384 |
* channels fill with 255, the exact bytes the former RGBA8-promoted store held). An unbound unit or |
| 385 |
* empty store yields transparent black. |
| 386 |
*/ |
| 387 |
inline vec4 swTexture(const FragmentShaderInput& in, int unit, const vec2& uv) |
| 388 |
{ |
| 389 |
if (unit < 0 || unit >= static_cast<int>(MaxTextureUnits)) { |
| 390 |
return vec4(0.0f); |
| 391 |
} |
| 392 |
const SwTexture* tex = in.textures[unit]; |
| 393 |
if (tex == nullptr) { |
| 394 |
return vec4(0.0f); |
| 395 |
} |
| 396 |
const std::uint8_t* pixels = tex->GetPixels(0); |
| 397 |
std::int32_t width = tex->GetWidth(); |
| 398 |
std::int32_t height = tex->GetHeight(); |
| 399 |
if (pixels == nullptr || width <= 0 || height <= 0) { |
| 400 |
return vec4(0.0f); |
| 401 |
} |
| 402 |
std::int32_t stride = tex->GetStrideBytes(); |
| 403 |
const std::int32_t bpp = tex->GetBytesPerPixel(); |
| 404 |
std::int32_t tx = detail::wrapTexel(detail::floorToInt(uv.x * static_cast<float>(width)), width, tex->GetWrapS()); |
| 405 |
std::int32_t ty = detail::wrapTexel(detail::floorToInt(uv.y * static_cast<float>(height)), height, tex->GetWrapT()); |
| 406 |
const std::uint8_t* texel = pixels + static_cast<std::size_t>(ty) * static_cast<std::size_t>(stride) + static_cast<std::size_t>(tx) * bpp; |
| 407 |
std::uint8_t expanded[4]; |
| 408 |
if (bpp != 4) { |
| 409 |
expanded[0] = texel[0]; |
| 410 |
expanded[1] = (bpp >= 2 ? texel[1] : std::uint8_t(255)); |
| 411 |
expanded[2] = 255; |
| 412 |
expanded[3] = 255; |
| 413 |
texel = expanded; |
| 414 |
} |
| 415 |
|
| 416 |
// Honor the texture's sampling swizzle, exactly as GLSL texture() does. It is identity for normal |
| 417 |
// textures, so a four-way compare short-circuits the per-channel switch on the hot path; only the |
| 418 |
// palette-index RG8 textures take the generic remap below (their swizzle maps the sampled `.a` to |
| 419 |
// the green channel - the packed per-pixel alpha - the same source the hand-ported palette effect |
| 420 |
// reads. Without this the expanded texel's opaque byte-3 would drop that alpha). |
| 421 |
// |
| 422 |
// NOTE: the `/ 255.0f` byte normalization is kept verbatim (no reciprocal multiply, no lookup |
| 423 |
// table): the Debug (/fp:precise) build compiles it as a true division while the Release |
| 424 |
// (/fp:fast) build already rewrites it into `* (1/255)` on its own, and the two differ in the |
| 425 |
// last bit for 126 of the 256 byte values - so any manual substitution would break bit-parity |
| 426 |
// with the current output in one of the two float models. |
| 427 |
const nCine::SwizzleChannel* swizzle = tex->GetSwizzle(); |
| 428 |
if (swizzle[0] == nCine::SwizzleChannel::Red && swizzle[1] == nCine::SwizzleChannel::Green && |
| 429 |
swizzle[2] == nCine::SwizzleChannel::Blue && swizzle[3] == nCine::SwizzleChannel::Alpha) { |
| 430 |
return vec4(texel[0] / 255.0f, texel[1] / 255.0f, texel[2] / 255.0f, texel[3] / 255.0f); |
| 431 |
} |
| 432 |
auto pickChannel = [texel](nCine::SwizzleChannel channel) -> float { |
| 433 |
switch (channel) { |
| 434 |
case nCine::SwizzleChannel::Red: return texel[0] / 255.0f; |
| 435 |
case nCine::SwizzleChannel::Green: return texel[1] / 255.0f; |
| 436 |
case nCine::SwizzleChannel::Blue: return texel[2] / 255.0f; |
| 437 |
case nCine::SwizzleChannel::Alpha: return texel[3] / 255.0f; |
| 438 |
case nCine::SwizzleChannel::Zero: return 0.0f; |
| 439 |
case nCine::SwizzleChannel::One: return 1.0f; |
| 440 |
} |
| 441 |
return 0.0f; |
| 442 |
}; |
| 443 |
return vec4(pickChannel(swizzle[0]), pickChannel(swizzle[1]), pickChannel(swizzle[2]), pickChannel(swizzle[3])); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* @brief Reads the primary texel the rasterizer already sampled, the fast equivalent of |
| 448 |
* `swTexture(in, unit, vec2(in.u, in.v))` |
| 449 |
* |
| 450 |
* The transpiler emits this for the exact `texture(uTexture, vTexCoords)` pattern - the sampler the |
| 451 |
* device keys the gather on (`FFState::textureUnit` is set from the same reflected `uTexture` unit |
| 452 |
* baked into @p unit here), sampled at the unmodified interpolated texcoord. For that pattern the |
| 453 |
* rasterizer's gather phase has already fetched the texel of this pixel into `in.rgba` (the 4-byte |
| 454 |
* expanded texel, before the sampling swizzle), so the fragment reads it back - applying the unit's swizzle - |
| 455 |
* instead of re-deriving the texel address and re-fetching through the full sampler. For a |
| 456 |
* bilinear-filtered texture the gather holds the FILTERED sample - exactly what GL's `texture()` |
| 457 |
* returns there - so reading it back is what keeps `texture(uTexture, vTexCoords)` bilinear on the |
| 458 |
* software backend (the generic @ref swTexture samples nearest). |
| 459 |
* |
| 460 |
* Falls back to the generic @ref swTexture only when `in.rgba` is not guaranteed to hold that |
| 461 |
* sample: an unbound unit or empty store (the gather substitutes opaque white where `swTexture` |
| 462 |
* yields transparent black). |
| 463 |
*/ |
| 464 |
inline vec4 swTexturePrimary(const FragmentShaderInput& in, int unit) |
| 465 |
{ |
| 466 |
const SwTexture* tex = (unit >= 0 && unit < static_cast<int>(MaxTextureUnits) ? in.textures[unit] : nullptr); |
| 467 |
if (tex != nullptr && tex->GetPixels(0) != nullptr) { |
| 468 |
const std::int32_t width = tex->GetWidth(); |
| 469 |
const std::int32_t height = tex->GetHeight(); |
| 470 |
if (width > 0 && height > 0) { |
| 471 |
const std::uint8_t* texel = in.rgba; |
| 472 |
// Same swizzle handling (and the same verbatim `/ 255.0f`) as swTexture above |
| 473 |
const nCine::SwizzleChannel* swizzle = tex->GetSwizzle(); |
| 474 |
if (swizzle[0] == nCine::SwizzleChannel::Red && swizzle[1] == nCine::SwizzleChannel::Green && |
| 475 |
swizzle[2] == nCine::SwizzleChannel::Blue && swizzle[3] == nCine::SwizzleChannel::Alpha) { |
| 476 |
return vec4(texel[0] / 255.0f, texel[1] / 255.0f, texel[2] / 255.0f, texel[3] / 255.0f); |
| 477 |
} |
| 478 |
auto pickChannel = [texel](nCine::SwizzleChannel channel) -> float { |
| 479 |
switch (channel) { |
| 480 |
case nCine::SwizzleChannel::Red: return texel[0] / 255.0f; |
| 481 |
case nCine::SwizzleChannel::Green: return texel[1] / 255.0f; |
| 482 |
case nCine::SwizzleChannel::Blue: return texel[2] / 255.0f; |
| 483 |
case nCine::SwizzleChannel::Alpha: return texel[3] / 255.0f; |
| 484 |
case nCine::SwizzleChannel::Zero: return 0.0f; |
| 485 |
case nCine::SwizzleChannel::One: return 1.0f; |
| 486 |
} |
| 487 |
return 0.0f; |
| 488 |
}; |
| 489 |
return vec4(pickChannel(swizzle[0]), pickChannel(swizzle[1]), pickChannel(swizzle[2]), pickChannel(swizzle[3])); |
| 490 |
} |
| 491 |
} |
| 492 |
return swTexture(in, unit, vec2(in.u, in.v)); |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* @brief Writes a straight-alpha `0..1` color to the rasterizer's 4-byte RGBA output pixel |
| 497 |
* |
| 498 |
* Each channel is clamped to `[0, 1]` and rounded to the nearest of 256 levels, matching the byte |
| 499 |
* quantization the fixed-function paths perform. |
| 500 |
*/ |
| 501 |
inline void packColor(const vec4& c, std::uint8_t* rgba) |
| 502 |
{ |
| 503 |
auto quantize = [](float v) -> std::uint8_t { |
| 504 |
v = (v < 0.0f) ? 0.0f : (v > 1.0f ? 1.0f : v); |
| 505 |
std::int32_t i = static_cast<std::int32_t>(v * 255.0f + 0.5f); |
| 506 |
return static_cast<std::uint8_t>((i < 0) ? 0 : (i > 255 ? 255 : i)); |
| 507 |
}; |
| 508 |
rgba[0] = quantize(c.r); |
| 509 |
rgba[1] = quantize(c.g); |
| 510 |
rgba[2] = quantize(c.b); |
| 511 |
rgba[3] = quantize(c.a); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
#if defined(_MSC_VER) |
| 516 |
# pragma warning(pop) |
| 517 |
#endif |
| 518 |
|
| 519 |
#endif |