| 1 |
#if defined(WITH_RHI_SOFTWARE) |
| 2 |
|
| 3 |
#include "SwRaster.h" |
| 4 |
#include "SwTileRenderer.h" |
| 5 |
#include "SwScanlineOps.h" |
| 6 |
|
| 7 |
#include <Cpu.h> |
| 8 |
#if defined(DEATH_TARGET_X86) |
| 9 |
# if defined(DEATH_ENABLE_AVX2) |
| 10 |
# include <IntrinsicsAvx.h> |
| 11 |
# elif defined(DEATH_ENABLE_SSE2) |
| 12 |
# include <IntrinsicsSse2.h> |
| 13 |
# endif |
| 14 |
#elif defined(DEATH_TARGET_ARM) |
| 15 |
# if defined(DEATH_ENABLE_NEON) |
| 16 |
# include <arm_neon.h> |
| 17 |
# endif |
| 18 |
#elif defined(DEATH_TARGET_WASM) |
| 19 |
# if defined(DEATH_ENABLE_SIMD128) |
| 20 |
# include <wasm_simd128.h> |
| 21 |
# endif |
| 22 |
#endif |
| 23 |
|
| 24 |
#include <Containers/SmallVector.h> |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <cmath> |
| 28 |
#include <cstddef> |
| 29 |
#include <cstdlib> |
| 30 |
#include <cstring> |
| 31 |
|
| 32 |
using namespace Death; |
| 33 |
using namespace Death::Containers; |
| 34 |
|
| 35 |
namespace nCine::RHI::Software |
| 36 |
{ |
| 37 |
// The sampler/filter enums live in the parent nCine namespace; pull them in so the ported rasterizer |
| 38 |
// bodies can name them unqualified (this TU never sees the SwRasterizer legacy shadow of the same names) |
| 39 |
using nCine::SamplerFilter; |
| 40 |
using nCine::SamplerWrapping; |
| 41 |
|
| 42 |
// ========================================================================= |
| 43 |
// Persistent SW render state |
| 44 |
// ========================================================================= |
| 45 |
namespace |
| 46 |
{ |
| 47 |
struct SWState |
| 48 |
{ |
| 49 |
// Active destination color buffer (owned by the device, RGBA8, width*height*4 bytes - unless |
| 50 |
// is16Bit, then native-endian RGB565 at width*height*2 bytes) |
| 51 |
std::uint8_t* colorBuffer = nullptr; |
| 52 |
std::int32_t bufferWidth = 0; |
| 53 |
std::int32_t bufferHeight = 0; |
| 54 |
// When true the buffer is a render-target texture and rows are written bottom-up (GL convention) |
| 55 |
bool isFboTarget = false; |
| 56 |
#if defined(RHI_USE_FB16) |
| 57 |
// When true the buffer is the RGB565 screen framebuffer (only the screen is ever 16-bit; render |
| 58 |
// targets stay RGBA8). Touched rows are staged through g_fbRowStage as RGBA8, see SwRaster.h. |
| 59 |
bool is16Bit = false; |
| 60 |
#endif |
| 61 |
|
| 62 |
bool blendingEnabled = false; |
| 63 |
SwBlendFactor blendSrc = SwBlendFactor::SrcAlpha; |
| 64 |
SwBlendFactor blendDst = SwBlendFactor::OneMinusSrcAlpha; |
| 65 |
|
| 66 |
bool scissorEnabled = false; |
| 67 |
std::int32_t scissorX = 0; |
| 68 |
std::int32_t scissorY = 0; |
| 69 |
std::int32_t scissorW = 0; |
| 70 |
std::int32_t scissorH = 0; |
| 71 |
|
| 72 |
// Viewport, used to map clip space to destination pixels in FetchVertex |
| 73 |
std::int32_t viewportX = 0; |
| 74 |
std::int32_t viewportY = 0; |
| 75 |
std::int32_t viewportW = 0; |
| 76 |
std::int32_t viewportH = 0; |
| 77 |
|
| 78 |
// Active draw context for the current draw call |
| 79 |
const DrawContext* drawCtx = nullptr; |
| 80 |
}; |
| 81 |
|
| 82 |
SWState g_state; |
| 83 |
} |
| 84 |
|
| 85 |
namespace |
| 86 |
{ |
| 87 |
// ===================================================================== |
| 88 |
// Shared scanline staging buffer of the immediate quad rasterizers (axis-aligned + affine). |
| 89 |
// The immediate path runs only on the main thread (the deferred tile workers rasterize through |
| 90 |
// SwTileRasterizer's per-worker tile scratch instead), so one static buffer safely replaces the |
| 91 |
// former two 16 KB stack arrays - stack a small-stack platform cannot afford. |
| 92 |
constexpr std::int32_t MaxScanBuf = 4096; |
| 93 |
alignas(32) std::uint8_t g_scanBuf[MaxScanBuf * 4]; |
| 94 |
|
| 95 |
#if defined(RHI_USE_FB16) |
| 96 |
// RGBA8 staging row for the 16-bit screen framebuffer: each rasterizer row-loop iteration loads the |
| 97 |
// touched span (565 -> RGBA8), runs its unchanged inner loop against it, and stores it back. Distinct |
| 98 |
// from g_scanBuf, which holds gathered SOURCE texels concurrently within the same row. Main-thread-only |
| 99 |
// like g_scanBuf (the deferred tile workers write RGBA8 tile scratch; only the tile copy converts). |
| 100 |
// Sized for the widest surface the tile renderer accepts, so any screen row fits. |
| 101 |
constexpr std::int32_t MaxFbRowStage = 8192; |
| 102 |
alignas(32) std::uint8_t g_fbRowStage[MaxFbRowStage * 4]; |
| 103 |
#endif |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
// ===================================================================== |
| 108 |
// SIMD-dispatched scanline blending (SrcAlpha / OneMinusSrcAlpha) |
| 109 |
// |
| 110 |
// The dispatched entry points, their DEATH_CPU_DISPATCHER_DECLARATION()s and the |
| 111 |
// DEATH_CPU_DISPATCHER_BASE()/DEATH_CPU_DISPATCHED() pairs below all have to sit in this named namespace, |
| 112 |
// with only the per-tag `xxxImplementation(Cpu::ScalarT/Sse2T/Avx2T/NeonT/Simd128T)` overloads inside the |
| 113 |
// unnamed namespaces - the same layout Shared/Containers/StringUtils.cpp uses. |
| 114 |
// |
| 115 |
// The reason is that in an IFUNC build (DEATH_CPU_USE_RUNTIME_DISPATCH + DEATH_CPU_USE_IFUNC, the desktop |
| 116 |
// default) DEATH_CPU_DISPATCHER_BASE() expands to `namespace { <dispatcher> }`, i.e. it wraps the generated |
| 117 |
// `xxxImplementation(Cpu::Features)` overload in an unnamed namespace itself. Invoked from the enclosing |
| 118 |
// named namespace, that generated `namespace {}` refers to the *same* unnamed namespace the tag overloads |
| 119 |
// live in, so all of them form one overload set and `return xxxImplementation(Cpu::Avx2)` picks the exact |
| 120 |
// Cpu::Avx2T match. |
| 121 |
// Invoked from *inside* the unnamed namespace it would nest a second, distinct one; unqualified lookup then |
| 122 |
// stops at the dispatcher's own declaration in that inner namespace and never reaches the tag overloads, so |
| 123 |
// every `return xxxImplementation(Cpu::Avx2)` binds back to the dispatcher through the implicit |
| 124 |
// Cpu::Avx2T -> Cpu::Features conversion. That still compiles (the return types match) but recurses |
| 125 |
// infinitely, overflowing the stack while the loader resolves the IFUNC relocations before main(). |
| 126 |
// |
| 127 |
// --------------------------------------------------------------------- |
| 128 |
// Quantization contract - every variant below MUST be bit-identical to its Cpu::ScalarT sibling |
| 129 |
// |
| 130 |
// The blend quantizes with `>> 8`, i.e. it divides by 256 rather than by 255. That is deliberate and NOT |
| 131 |
// something to "improve" here in isolation: the same expression is inlined per pixel in |
| 132 |
// SwTileRasterizer.cpp's fast-blend path, so the scanline ops and the per-pixel path have to round the |
| 133 |
// same way or a sprite would seam where one path takes over from the other. |
| 134 |
// |
| 135 |
// The exact division would be the well-known identity |
| 136 |
// round(n / 255) == (t + (t >> 8)) >> 8 with t = n + 0x80, valid for n in [0, 255*255] |
| 137 |
// (equivalently `_mm_mulhi_epu16(n + 0x80, 0x8081) >> 7`), and it happens to be exact at both endpoints: |
| 138 |
// a == 255 gives back src and a == 0 gives back dst. Switching to it would fix the endpoints for free but |
| 139 |
// would also shift *every* intermediate alpha by up to 1 LSB away from the scalar reference and away from |
| 140 |
// SwTileRasterizer.cpp - 16.6M of the 16.7M (srcAlpha, src, dst) byte triples change value. So `>> 8` |
| 141 |
// stays, and the two endpoints the truncation gets wrong are instead replayed the way the scalar variant |
| 142 |
// replays them - as the `sA == 0` / `sA >= 255` shortcuts - only branchlessly, as a per-pixel select. |
| 143 |
// Without that select the vector bodies computed `(src * 255 + dst * 0) >> 8 == src - 1` for an opaque |
| 144 |
// source (every opaque sprite pixel 1 LSB dark) and `(0 + dst * 255) >> 8 == dst - 1` for a fully |
| 145 |
// transparent one (a *transparent* pixel darkening the destination, cumulative across overlapping draws). |
| 146 |
// |
| 147 |
// Because that select is what a whole *block* of same-class pixels collapses to, the vector variants |
| 148 |
// additionally branch straight past the multiply chain when every pixel of a block is opaque (a plain |
| 149 |
// 16-byte copy) or every pixel is transparent (nothing to do at all). Those shortcuts are what stops the |
| 150 |
// exactness above from costing throughput - without them the SSE2 body was slower than the scalar one on |
| 151 |
// opaque sprite interiors, since the scalar variant gets the same two shortcuts for free from its |
| 152 |
// per-pixel branches. Being shortcuts of the *select* and not of the arithmetic they cannot perturb the |
| 153 |
// quantization, but they are the first thing to re-verify against the Cpu::ScalarT sibling if this |
| 154 |
// function is ever touched again, and the near-miss block (one lane out of four or eight in a different |
| 155 |
// alpha class) is the case that breaks a careless one. |
| 156 |
// ===================================================================== |
| 157 |
extern void DEATH_CPU_DISPATCHED_DECLARATION(blendScanlineSrcAlpha)(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count); |
| 158 |
DEATH_CPU_DISPATCHER_DECLARATION(blendScanlineSrcAlpha) |
| 159 |
|
| 160 |
namespace |
| 161 |
{ |
| 162 |
// Scalar fallback |
| 163 |
DEATH_CPU_MAYBE_UNUSED typename std::decay<decltype(blendScanlineSrcAlpha)>::type blendScanlineSrcAlphaImplementation(Cpu::ScalarT) { |
| 164 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count) { |
| 165 |
for (std::int32_t i = 0; i < count; ++i, dst += 4, src += 4) { |
| 166 |
const std::int32_t sA = src[3]; |
| 167 |
if (sA == 0) continue; |
| 168 |
if (sA >= 255) { |
| 169 |
std::memcpy(dst, src, 4); |
| 170 |
continue; |
| 171 |
} |
| 172 |
const std::int32_t inv = 255 - sA; |
| 173 |
dst[0] = static_cast<std::uint8_t>((src[0] * sA + dst[0] * inv) >> 8); |
| 174 |
dst[1] = static_cast<std::uint8_t>((src[1] * sA + dst[1] * inv) >> 8); |
| 175 |
dst[2] = static_cast<std::uint8_t>((src[2] * sA + dst[2] * inv) >> 8); |
| 176 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 177 |
} |
| 178 |
}; |
| 179 |
} |
| 180 |
|
| 181 |
#if defined(DEATH_ENABLE_SSE2) |
| 182 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SSE2 typename std::decay<decltype(blendScanlineSrcAlpha)>::type blendScanlineSrcAlphaImplementation(Cpu::Sse2T) { |
| 183 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count) DEATH_ENABLE_SSE2 { |
| 184 |
const __m128i zero = _mm_setzero_si128(); |
| 185 |
const __m128i c255 = _mm_set1_epi16(255); |
| 186 |
const __m128i c255d = _mm_set1_epi32(255); |
| 187 |
// Raises the source ALPHA word of each widened pixel to 255 so its product with the broadcast |
| 188 |
// alpha is the scalar's `sA * 255` numerator and not `sA * sA` (the broadcast is taken from the |
| 189 |
// untouched vector first). A bitwise OR suffices because any byte value OR 0xFF is 0xFF. |
| 190 |
const __m128i alphaOne = _mm_set_epi16(255, 0, 0, 0, 255, 0, 0, 0); |
| 191 |
|
| 192 |
std::int32_t i = 0; |
| 193 |
for (; i + 4 <= count; i += 4, dst += 16, src += 16) { |
| 194 |
__m128i srcPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src)); |
| 195 |
|
| 196 |
// Classify the four pixels up front as whole-lane masks. Shifting each 32-bit pixel down |
| 197 |
// by 24 leaves just its alpha, so a *dword* compare yields 0xFFFFFFFF for exactly those |
| 198 |
// pixels whose alpha is 255 (resp. 0) - which is precisely the per-pixel select the blend |
| 199 |
// has to apply at the end anyway. Doing it here instead of from the packed alpha bytes is |
| 200 |
// therefore free: these three ops replace the packus + two byte compares that used to sit |
| 201 |
// at the bottom of the loop. |
| 202 |
__m128i alpha32 = _mm_srli_epi32(srcPx, 24); |
| 203 |
__m128i opaque = _mm_cmpeq_epi32(alpha32, c255d); |
| 204 |
|
| 205 |
// Uniform-block shortcuts: when *all four* pixels land in one of those two classes the |
| 206 |
// ~40-op body below is dead code - the block is either a plain 16-byte copy or a no-op - |
| 207 |
// and all it costs to find out is a movemask plus a compare per class. Without them the |
| 208 |
// vector path was *slower than scalar* on the content that dominates sprite drawing, |
| 209 |
// because the scalar variant gets the same two shortcuts for free from its per-pixel |
| 210 |
// branches while the vector path paid the full multiply chain regardless. |
| 211 |
// |
| 212 |
// Sprite content is what makes this pay: interiors are long runs of alpha 255 and the |
| 213 |
// margins of a sprite's bounding box are long runs of alpha 0, so both branches predict |
| 214 |
// near-perfectly across a scanline. The blend therefore becomes data-dependent in timing. |
| 215 |
// That is a deliberate, acceptable tradeoff here - this is a rasterizer, and the "secret" |
| 216 |
// would be the picture the user is already looking at - but it is called out because the |
| 217 |
// identical construction in a crypto primitive would be a side-channel bug. |
| 218 |
if (_mm_movemask_epi8(opaque) == 0xFFFF) { |
| 219 |
// Every pixel opaque - the select would pick srcPx for all 16 bytes |
| 220 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), srcPx); |
| 221 |
continue; |
| 222 |
} |
| 223 |
// Deliberately computed after the test above so the all-opaque path, the most common one |
| 224 |
// by far, does not pay for a mask it cannot use |
| 225 |
__m128i clear = _mm_cmpeq_epi32(alpha32, zero); |
| 226 |
if (_mm_movemask_epi8(clear) == 0xFFFF) { |
| 227 |
// Every pixel fully transparent - the select would pick dstPx for all 16 bytes, so |
| 228 |
// dst is already the answer and does not even need to be loaded |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
__m128i dstPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst)); |
| 233 |
|
| 234 |
// Low 2 pixels: unpack bytes to 16-bit |
| 235 |
__m128i sLo = _mm_unpacklo_epi8(srcPx, zero); |
| 236 |
__m128i dLo = _mm_unpacklo_epi8(dstPx, zero); |
| 237 |
// Broadcast alpha of each pixel: word[3]→[0..3], word[7]→[4..7] |
| 238 |
__m128i aLo = _mm_shufflelo_epi16(sLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 239 |
aLo = _mm_shufflehi_epi16(aLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 240 |
sLo = _mm_or_si128(sLo, alphaOne); |
| 241 |
__m128i iaLo = _mm_sub_epi16(c255, aLo); |
| 242 |
__m128i rLo = _mm_add_epi16(_mm_mullo_epi16(sLo, aLo), _mm_mullo_epi16(dLo, iaLo)); |
| 243 |
rLo = _mm_srli_epi16(rLo, 8); |
| 244 |
|
| 245 |
// High 2 pixels |
| 246 |
__m128i sHi = _mm_unpackhi_epi8(srcPx, zero); |
| 247 |
__m128i dHi = _mm_unpackhi_epi8(dstPx, zero); |
| 248 |
__m128i aHi = _mm_shufflelo_epi16(sHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 249 |
aHi = _mm_shufflehi_epi16(aHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 250 |
sHi = _mm_or_si128(sHi, alphaOne); |
| 251 |
__m128i iaHi = _mm_sub_epi16(c255, aHi); |
| 252 |
__m128i rHi = _mm_add_epi16(_mm_mullo_epi16(sHi, aHi), _mm_mullo_epi16(dHi, iaHi)); |
| 253 |
rHi = _mm_srli_epi16(rHi, 8); |
| 254 |
|
| 255 |
// Mixed block, so the two shortcuts still have to be applied per pixel - branchlessly, |
| 256 |
// which is also what makes them safe to skip wholesale above (see the quantization |
| 257 |
// contract): take src where alpha is 255, dst where it is 0 and the `>> 8` blend |
| 258 |
// everywhere else. SSE2 has no _mm_blendv_epi8, hence the and/or form. |
| 259 |
__m128i blended = _mm_packus_epi16(rLo, rHi); |
| 260 |
__m128i picked = _mm_or_si128(_mm_and_si128(opaque, srcPx), _mm_and_si128(clear, dstPx)); |
| 261 |
__m128i keep = _mm_andnot_si128(_mm_or_si128(opaque, clear), blended); |
| 262 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), _mm_or_si128(picked, keep)); |
| 263 |
} |
| 264 |
// Scalar tail |
| 265 |
for (; i < count; ++i, dst += 4, src += 4) { |
| 266 |
const std::int32_t sA = src[3]; |
| 267 |
if (sA == 0) continue; |
| 268 |
if (sA >= 255) { std::memcpy(dst, src, 4); continue; } |
| 269 |
const std::int32_t inv = 255 - sA; |
| 270 |
dst[0] = static_cast<std::uint8_t>((src[0] * sA + dst[0] * inv) >> 8); |
| 271 |
dst[1] = static_cast<std::uint8_t>((src[1] * sA + dst[1] * inv) >> 8); |
| 272 |
dst[2] = static_cast<std::uint8_t>((src[2] * sA + dst[2] * inv) >> 8); |
| 273 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 274 |
} |
| 275 |
}; |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
#if defined(DEATH_ENABLE_AVX2) |
| 280 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_AVX2 typename std::decay<decltype(blendScanlineSrcAlpha)>::type blendScanlineSrcAlphaImplementation(Cpu::Avx2T) { |
| 281 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count) DEATH_ENABLE_AVX2 { |
| 282 |
const __m256i zero = _mm256_setzero_si256(); |
| 283 |
const __m256i c255 = _mm256_set1_epi16(255); |
| 284 |
const __m256i c255d = _mm256_set1_epi32(255); |
| 285 |
// See the SSE2 variant: forces the widened source alpha word to 255 so the alpha lane's |
| 286 |
// numerator is the scalar's `sA * 255` |
| 287 |
const __m256i alphaOne = _mm256_set1_epi64x(0x00FF000000000000LL); |
| 288 |
|
| 289 |
std::int32_t i = 0; |
| 290 |
for (; i + 8 <= count; i += 8, dst += 32, src += 32) { |
| 291 |
__m256i srcPx = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(src)); |
| 292 |
|
| 293 |
// Same whole-lane classify and uniform-block shortcuts as the SSE2 variant (see there). |
| 294 |
// The eight-pixel granularity makes a uniform block rarer than it is at four, so this |
| 295 |
// trades a few percent on alpha gradients for 2-2.7x on what a 2D sprite renderer |
| 296 |
// actually spends its time on: opaque tile and sprite interiors, and the transparent |
| 297 |
// margins of a sprite's bounding box (whose destination is not even loaded) |
| 298 |
__m256i alpha32 = _mm256_srli_epi32(srcPx, 24); |
| 299 |
__m256i opaque = _mm256_cmpeq_epi32(alpha32, c255d); |
| 300 |
if (_mm256_movemask_epi8(opaque) == -1) { |
| 301 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), srcPx); |
| 302 |
continue; |
| 303 |
} |
| 304 |
__m256i clear = _mm256_cmpeq_epi32(alpha32, zero); |
| 305 |
if (_mm256_movemask_epi8(clear) == -1) { |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
__m256i dstPx = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(dst)); |
| 310 |
|
| 311 |
// Within each 128-bit lane: unpacklo processes first 2 pixels, unpackhi the next 2 |
| 312 |
__m256i sLo = _mm256_unpacklo_epi8(srcPx, zero); |
| 313 |
__m256i dLo = _mm256_unpacklo_epi8(dstPx, zero); |
| 314 |
__m256i aLo = _mm256_shufflelo_epi16(sLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 315 |
aLo = _mm256_shufflehi_epi16(aLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 316 |
sLo = _mm256_or_si256(sLo, alphaOne); |
| 317 |
__m256i iaLo = _mm256_sub_epi16(c255, aLo); |
| 318 |
__m256i rLo = _mm256_add_epi16(_mm256_mullo_epi16(sLo, aLo), _mm256_mullo_epi16(dLo, iaLo)); |
| 319 |
rLo = _mm256_srli_epi16(rLo, 8); |
| 320 |
|
| 321 |
__m256i sHi = _mm256_unpackhi_epi8(srcPx, zero); |
| 322 |
__m256i dHi = _mm256_unpackhi_epi8(dstPx, zero); |
| 323 |
__m256i aHi = _mm256_shufflelo_epi16(sHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 324 |
aHi = _mm256_shufflehi_epi16(aHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 325 |
sHi = _mm256_or_si256(sHi, alphaOne); |
| 326 |
__m256i iaHi = _mm256_sub_epi16(c255, aHi); |
| 327 |
__m256i rHi = _mm256_add_epi16(_mm256_mullo_epi16(sHi, aHi), _mm256_mullo_epi16(dHi, iaHi)); |
| 328 |
rHi = _mm256_srli_epi16(rHi, 8); |
| 329 |
|
| 330 |
// Branchless replay of the scalar's `sA == 0` / `sA >= 255` shortcuts for the pixels a |
| 331 |
// mixed block holds. The classify above already produced these selects as whole-lane |
| 332 |
// masks, so reusing them replaces the packus + two byte compares this used to need |
| 333 |
__m256i blended = _mm256_packus_epi16(rLo, rHi); |
| 334 |
__m256i out = _mm256_blendv_epi8(blended, srcPx, opaque); |
| 335 |
out = _mm256_blendv_epi8(out, dstPx, clear); |
| 336 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), out); |
| 337 |
} |
| 338 |
// SSE2 tail (4 pixels at a time) |
| 339 |
const __m128i zero128 = _mm_setzero_si128(); |
| 340 |
const __m128i c255_128 = _mm_set1_epi16(255); |
| 341 |
const __m128i c255d128 = _mm_set1_epi32(255); |
| 342 |
const __m128i alphaOne128 = _mm_set_epi16(255, 0, 0, 0, 255, 0, 0, 0); |
| 343 |
for (; i + 4 <= count; i += 4, dst += 16, src += 16) { |
| 344 |
__m128i srcPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src)); |
| 345 |
// Same whole-lane classify and uniform-block shortcuts as the SSE2 variant (see there for |
| 346 |
// why they are free for the general path and why the data-dependent timing is fine). This |
| 347 |
// loop runs at most once per call, so it only shows up on scanlines shorter than 8 px - |
| 348 |
// narrow spans, clipped sprite edges, glyphs - where it is worth up to a third of the |
| 349 |
// call: 4 opaque pixels measured 1.06 -> 0.71 ns/px |
| 350 |
__m128i alpha32 = _mm_srli_epi32(srcPx, 24); |
| 351 |
__m128i opaque = _mm_cmpeq_epi32(alpha32, c255d128); |
| 352 |
if (_mm_movemask_epi8(opaque) == 0xFFFF) { |
| 353 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), srcPx); |
| 354 |
continue; |
| 355 |
} |
| 356 |
__m128i clear = _mm_cmpeq_epi32(alpha32, zero128); |
| 357 |
if (_mm_movemask_epi8(clear) == 0xFFFF) { |
| 358 |
continue; |
| 359 |
} |
| 360 |
__m128i dstPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst)); |
| 361 |
__m128i sLo = _mm_unpacklo_epi8(srcPx, zero128); |
| 362 |
__m128i dLo = _mm_unpacklo_epi8(dstPx, zero128); |
| 363 |
__m128i aLo = _mm_shufflelo_epi16(sLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 364 |
aLo = _mm_shufflehi_epi16(aLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 365 |
sLo = _mm_or_si128(sLo, alphaOne128); |
| 366 |
__m128i iaLo = _mm_sub_epi16(c255_128, aLo); |
| 367 |
__m128i rLo = _mm_add_epi16(_mm_mullo_epi16(sLo, aLo), _mm_mullo_epi16(dLo, iaLo)); |
| 368 |
rLo = _mm_srli_epi16(rLo, 8); |
| 369 |
__m128i sHi = _mm_unpackhi_epi8(srcPx, zero128); |
| 370 |
__m128i dHi = _mm_unpackhi_epi8(dstPx, zero128); |
| 371 |
__m128i aHi = _mm_shufflelo_epi16(sHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 372 |
aHi = _mm_shufflehi_epi16(aHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 373 |
sHi = _mm_or_si128(sHi, alphaOne128); |
| 374 |
__m128i iaHi = _mm_sub_epi16(c255_128, aHi); |
| 375 |
__m128i rHi = _mm_add_epi16(_mm_mullo_epi16(sHi, aHi), _mm_mullo_epi16(dHi, iaHi)); |
| 376 |
rHi = _mm_srli_epi16(rHi, 8); |
| 377 |
// AVX2 implies SSE4.1, so the 128-bit tail can use the two-blend form as well, driven by |
| 378 |
// the lane masks the classify above already produced |
| 379 |
__m128i out = _mm_blendv_epi8(_mm_packus_epi16(rLo, rHi), srcPx, opaque); |
| 380 |
out = _mm_blendv_epi8(out, dstPx, clear); |
| 381 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), out); |
| 382 |
} |
| 383 |
// Scalar tail |
| 384 |
for (; i < count; ++i, dst += 4, src += 4) { |
| 385 |
const std::int32_t sA = src[3]; |
| 386 |
if (sA == 0) continue; |
| 387 |
if (sA >= 255) { std::memcpy(dst, src, 4); continue; } |
| 388 |
const std::int32_t inv = 255 - sA; |
| 389 |
dst[0] = static_cast<std::uint8_t>((src[0] * sA + dst[0] * inv) >> 8); |
| 390 |
dst[1] = static_cast<std::uint8_t>((src[1] * sA + dst[1] * inv) >> 8); |
| 391 |
dst[2] = static_cast<std::uint8_t>((src[2] * sA + dst[2] * inv) >> 8); |
| 392 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 393 |
} |
| 394 |
}; |
| 395 |
} |
| 396 |
#endif |
| 397 |
|
| 398 |
#if defined(DEATH_ENABLE_NEON) |
| 399 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_NEON typename std::decay<decltype(blendScanlineSrcAlpha)>::type blendScanlineSrcAlphaImplementation(Cpu::NeonT) { |
| 400 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count) DEATH_ENABLE_NEON { |
| 401 |
static const std::uint8_t alphaIdxData[8] = { 3, 3, 3, 3, 7, 7, 7, 7 }; |
| 402 |
static const std::uint8_t alphaOneData[8] = { 0, 0, 0, 255, 0, 0, 0, 255 }; |
| 403 |
const uint8x8_t alphaIdx = vld1_u8(alphaIdxData); |
| 404 |
// See the SSE2 variant: raises the source alpha byte to 255 so the alpha lane's numerator is |
| 405 |
// the scalar's `sA * 255` rather than `sA * sA` |
| 406 |
const uint8x8_t alphaOne = vld1_u8(alphaOneData); |
| 407 |
const uint8x8_t c255x8 = vdup_n_u8(255); |
| 408 |
const uint8x16_t c255x16 = vdupq_n_u8(255); |
| 409 |
const uint8x16_t zerox16 = vdupq_n_u8(0); |
| 410 |
|
| 411 |
std::int32_t i = 0; |
| 412 |
for (; i + 4 <= count; i += 4, dst += 16, src += 16) { |
| 413 |
uint8x16_t srcPx = vld1q_u8(src); |
| 414 |
uint8x16_t dstPx = vld1q_u8(dst); |
| 415 |
|
| 416 |
// Low 2 pixels |
| 417 |
uint8x8_t srcLo = vget_low_u8(srcPx); |
| 418 |
uint8x8_t dstLo = vget_low_u8(dstPx); |
| 419 |
uint8x8_t aLo = vtbl1_u8(srcLo, alphaIdx); |
| 420 |
uint8x8_t iaLo = vsub_u8(c255x8, aLo); |
| 421 |
uint16x8_t rLo = vaddq_u16(vmull_u8(vorr_u8(srcLo, alphaOne), aLo), vmull_u8(dstLo, iaLo)); |
| 422 |
|
| 423 |
// High 2 pixels |
| 424 |
uint8x8_t srcHi = vget_high_u8(srcPx); |
| 425 |
uint8x8_t dstHi = vget_high_u8(dstPx); |
| 426 |
uint8x8_t aHi = vtbl1_u8(srcHi, alphaIdx); |
| 427 |
uint8x8_t iaHi = vsub_u8(c255x8, aHi); |
| 428 |
uint16x8_t rHi = vaddq_u16(vmull_u8(vorr_u8(srcHi, alphaOne), aHi), vmull_u8(dstHi, iaHi)); |
| 429 |
|
| 430 |
// Branchless replay of the scalar's `sA == 0` / `sA >= 255` shortcuts |
| 431 |
uint8x16_t blended = vcombine_u8(vshrn_n_u16(rLo, 8), vshrn_n_u16(rHi, 8)); |
| 432 |
uint8x16_t aBytes = vcombine_u8(aLo, aHi); |
| 433 |
uint8x16_t out = vbslq_u8(vceqq_u8(aBytes, c255x16), srcPx, blended); |
| 434 |
out = vbslq_u8(vceqq_u8(aBytes, zerox16), dstPx, out); |
| 435 |
vst1q_u8(dst, out); |
| 436 |
} |
| 437 |
// Scalar tail |
| 438 |
for (; i < count; ++i, dst += 4, src += 4) { |
| 439 |
const std::int32_t sA = src[3]; |
| 440 |
if (sA == 0) continue; |
| 441 |
if (sA >= 255) { std::memcpy(dst, src, 4); continue; } |
| 442 |
const std::int32_t inv = 255 - sA; |
| 443 |
dst[0] = static_cast<std::uint8_t>((src[0] * sA + dst[0] * inv) >> 8); |
| 444 |
dst[1] = static_cast<std::uint8_t>((src[1] * sA + dst[1] * inv) >> 8); |
| 445 |
dst[2] = static_cast<std::uint8_t>((src[2] * sA + dst[2] * inv) >> 8); |
| 446 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 447 |
} |
| 448 |
}; |
| 449 |
} |
| 450 |
#endif |
| 451 |
|
| 452 |
#if defined(DEATH_ENABLE_SIMD128) |
| 453 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SIMD128 typename std::decay<decltype(blendScanlineSrcAlpha)>::type blendScanlineSrcAlphaImplementation(Cpu::Simd128T) { |
| 454 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count) DEATH_ENABLE_SIMD128 { |
| 455 |
const v128_t c255 = wasm_i16x8_splat(255); |
| 456 |
const v128_t c255b = wasm_i8x16_splat((std::int8_t)255); |
| 457 |
const v128_t zerob = wasm_i8x16_splat(0); |
| 458 |
// See the SSE2 variant: raises the source alpha word to 255 so the alpha lane's numerator is |
| 459 |
// the scalar's `sA * 255` rather than `sA * sA` |
| 460 |
const v128_t alphaOne = wasm_i16x8_make(0, 0, 0, 255, 0, 0, 0, 255); |
| 461 |
|
| 462 |
std::int32_t i = 0; |
| 463 |
for (; i + 4 <= count; i += 4, dst += 16, src += 16) { |
| 464 |
v128_t srcPx = wasm_v128_load(src); |
| 465 |
v128_t dstPx = wasm_v128_load(dst); |
| 466 |
|
| 467 |
// Low 2 pixels: extend to 16-bit |
| 468 |
v128_t sLo = wasm_u16x8_extend_low_u8x16(srcPx); |
| 469 |
v128_t dLo = wasm_u16x8_extend_low_u8x16(dstPx); |
| 470 |
// Broadcast alpha: byte shuffle on 16-bit data |
| 471 |
v128_t aLo = wasm_i8x16_shuffle(sLo, sLo, 6, 7, 6, 7, 6, 7, 6, 7, 14, 15, 14, 15, 14, 15, 14, 15); |
| 472 |
v128_t iaLo = wasm_i16x8_sub(c255, aLo); |
| 473 |
v128_t rLo = wasm_u16x8_shr(wasm_i16x8_add(wasm_i16x8_mul(wasm_v128_or(sLo, alphaOne), aLo), wasm_i16x8_mul(dLo, iaLo)), 8); |
| 474 |
|
| 475 |
// High 2 pixels |
| 476 |
v128_t sHi = wasm_u16x8_extend_high_u8x16(srcPx); |
| 477 |
v128_t dHi = wasm_u16x8_extend_high_u8x16(dstPx); |
| 478 |
v128_t aHi = wasm_i8x16_shuffle(sHi, sHi, 6, 7, 6, 7, 6, 7, 6, 7, 14, 15, 14, 15, 14, 15, 14, 15); |
| 479 |
v128_t iaHi = wasm_i16x8_sub(c255, aHi); |
| 480 |
v128_t rHi = wasm_u16x8_shr(wasm_i16x8_add(wasm_i16x8_mul(wasm_v128_or(sHi, alphaOne), aHi), wasm_i16x8_mul(dHi, iaHi)), 8); |
| 481 |
|
| 482 |
// Branchless replay of the scalar's `sA == 0` / `sA >= 255` shortcuts (the alpha words are |
| 483 |
// all in [0, 255], so narrowing them back to bytes is lossless) |
| 484 |
v128_t blended = wasm_u8x16_narrow_i16x8(rLo, rHi); |
| 485 |
v128_t aBytes = wasm_u8x16_narrow_i16x8(aLo, aHi); |
| 486 |
v128_t out = wasm_v128_bitselect(srcPx, blended, wasm_i8x16_eq(aBytes, c255b)); |
| 487 |
out = wasm_v128_bitselect(dstPx, out, wasm_i8x16_eq(aBytes, zerob)); |
| 488 |
wasm_v128_store(dst, out); |
| 489 |
} |
| 490 |
// Scalar tail |
| 491 |
for (; i < count; ++i, dst += 4, src += 4) { |
| 492 |
const std::int32_t sA = src[3]; |
| 493 |
if (sA == 0) continue; |
| 494 |
if (sA >= 255) { std::memcpy(dst, src, 4); continue; } |
| 495 |
const std::int32_t inv = 255 - sA; |
| 496 |
dst[0] = static_cast<std::uint8_t>((src[0] * sA + dst[0] * inv) >> 8); |
| 497 |
dst[1] = static_cast<std::uint8_t>((src[1] * sA + dst[1] * inv) >> 8); |
| 498 |
dst[2] = static_cast<std::uint8_t>((src[2] * sA + dst[2] * inv) >> 8); |
| 499 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 500 |
} |
| 501 |
}; |
| 502 |
} |
| 503 |
#endif |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
DEATH_CPU_DISPATCHER_BASE(blendScanlineSrcAlphaImplementation) |
| 508 |
DEATH_CPU_DISPATCHED(blendScanlineSrcAlphaImplementation, void DEATH_CPU_DISPATCHED_DECLARATION(blendScanlineSrcAlpha)(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count))({ |
| 509 |
return blendScanlineSrcAlphaImplementation(Cpu::DefaultBase)(dst, src, count); |
| 510 |
}) |
| 511 |
|
| 512 |
// ===================================================================== |
| 513 |
// CPU-dispatched fused palette-LUT gather + blend (SrcAlpha / OneMinusSrcAlpha) |
| 514 |
// |
| 515 |
// One scanline of the dominant tile/sprite draw: consecutive R8 index bytes, a constant source alpha |
| 516 |
// (SwPaletteLut::alphaByteOffset < 0, so packed[idx] IS the final pixel) and the fast blend pair. Fusing |
| 517 |
// the LUT gather with the blend classification skips the 4-bytes-per-pixel staging round trip the |
| 518 |
// two-pass gather-then-BlendScanlineSrcAlpha form paid, and a transparent texel costs one byte read. |
| 519 |
// The blend math and its `sA == 0` / `sA >= 255` shortcuts are byte-for-byte the scalar reference of |
| 520 |
// blendScanlineSrcAlpha above (see the quantization contract there); the source pixel is packed[idx] |
| 521 |
// instead of a staged texel, which is exactly what the staging pass would have stored. |
| 522 |
// ===================================================================== |
| 523 |
extern void DEATH_CPU_DISPATCHED_DECLARATION(fusedLutBlendScanline)(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT srcIdx, std::int32_t count, const std::uint8_t (*DEATH_RESTRICT packed)[4]); |
| 524 |
DEATH_CPU_DISPATCHER_DECLARATION(fusedLutBlendScanline) |
| 525 |
|
| 526 |
namespace |
| 527 |
{ |
| 528 |
// Scalar fallback - also the only variant on CPUs without a vector gather (SSE2/NEON): the LUT |
| 529 |
// lookup is a data-dependent load either way, and the per-pixel branches already collapse the |
| 530 |
// opaque-interior / transparent-margin runs that dominate the content |
| 531 |
DEATH_CPU_MAYBE_UNUSED typename std::decay<decltype(fusedLutBlendScanline)>::type fusedLutBlendScanlineImplementation(Cpu::ScalarT) { |
| 532 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT srcIdx, std::int32_t count, const std::uint8_t (*DEATH_RESTRICT packed)[4]) { |
| 533 |
for (std::int32_t i = 0; i < count; ++i, dst += 4) { |
| 534 |
const std::uint8_t* entry = packed[srcIdx[i]]; |
| 535 |
const std::int32_t sA = entry[3]; |
| 536 |
if (sA == 0) continue; |
| 537 |
if (sA >= 255) { |
| 538 |
std::memcpy(dst, entry, 4); |
| 539 |
continue; |
| 540 |
} |
| 541 |
const std::int32_t inv = 255 - sA; |
| 542 |
dst[0] = static_cast<std::uint8_t>((entry[0] * sA + dst[0] * inv) >> 8); |
| 543 |
dst[1] = static_cast<std::uint8_t>((entry[1] * sA + dst[1] * inv) >> 8); |
| 544 |
dst[2] = static_cast<std::uint8_t>((entry[2] * sA + dst[2] * inv) >> 8); |
| 545 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 546 |
} |
| 547 |
}; |
| 548 |
} |
| 549 |
|
| 550 |
#if defined(DEATH_ENABLE_AVX2) |
| 551 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_AVX2 typename std::decay<decltype(fusedLutBlendScanline)>::type fusedLutBlendScanlineImplementation(Cpu::Avx2T) { |
| 552 |
return [](std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT srcIdx, std::int32_t count, const std::uint8_t (*DEATH_RESTRICT packed)[4]) DEATH_ENABLE_AVX2 { |
| 553 |
// The 256 LUT entries are 4 packed bytes each, so vpgatherdd fetches 8 final pixels per step |
| 554 |
const int* lut32 = reinterpret_cast<const int*>(packed); |
| 555 |
const __m256i zero = _mm256_setzero_si256(); |
| 556 |
const __m256i c255 = _mm256_set1_epi16(255); |
| 557 |
const __m256i c255d = _mm256_set1_epi32(255); |
| 558 |
// See blendScanlineSrcAlpha's SSE2 variant: forces the widened source alpha word to 255 so the |
| 559 |
// alpha lane's numerator is the scalar's `sA * 255` |
| 560 |
const __m256i alphaOne = _mm256_set1_epi64x(0x00FF000000000000LL); |
| 561 |
|
| 562 |
std::int32_t i = 0; |
| 563 |
for (; i + 8 <= count; i += 8, dst += 32, srcIdx += 8) { |
| 564 |
const __m128i idxBytes = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(srcIdx)); |
| 565 |
const __m256i idx32 = _mm256_cvtepu8_epi32(idxBytes); |
| 566 |
__m256i srcPx = _mm256_i32gather_epi32(lut32, idx32, 4); |
| 567 |
|
| 568 |
// Same whole-lane classify and uniform-block shortcuts as blendScanlineSrcAlpha's AVX2 |
| 569 |
// variant (see there): tile interiors are long all-opaque runs (a plain store, the |
| 570 |
// destination is never loaded) and sprite margins long all-transparent ones (a no-op) |
| 571 |
__m256i alpha32 = _mm256_srli_epi32(srcPx, 24); |
| 572 |
__m256i opaque = _mm256_cmpeq_epi32(alpha32, c255d); |
| 573 |
if (_mm256_movemask_epi8(opaque) == -1) { |
| 574 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), srcPx); |
| 575 |
continue; |
| 576 |
} |
| 577 |
__m256i clear = _mm256_cmpeq_epi32(alpha32, zero); |
| 578 |
if (_mm256_movemask_epi8(clear) == -1) { |
| 579 |
continue; |
| 580 |
} |
| 581 |
|
| 582 |
__m256i dstPx = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(dst)); |
| 583 |
|
| 584 |
__m256i sLo = _mm256_unpacklo_epi8(srcPx, zero); |
| 585 |
__m256i dLo = _mm256_unpacklo_epi8(dstPx, zero); |
| 586 |
__m256i aLo = _mm256_shufflelo_epi16(sLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 587 |
aLo = _mm256_shufflehi_epi16(aLo, _MM_SHUFFLE(3, 3, 3, 3)); |
| 588 |
sLo = _mm256_or_si256(sLo, alphaOne); |
| 589 |
__m256i iaLo = _mm256_sub_epi16(c255, aLo); |
| 590 |
__m256i rLo = _mm256_add_epi16(_mm256_mullo_epi16(sLo, aLo), _mm256_mullo_epi16(dLo, iaLo)); |
| 591 |
rLo = _mm256_srli_epi16(rLo, 8); |
| 592 |
|
| 593 |
__m256i sHi = _mm256_unpackhi_epi8(srcPx, zero); |
| 594 |
__m256i dHi = _mm256_unpackhi_epi8(dstPx, zero); |
| 595 |
__m256i aHi = _mm256_shufflelo_epi16(sHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 596 |
aHi = _mm256_shufflehi_epi16(aHi, _MM_SHUFFLE(3, 3, 3, 3)); |
| 597 |
sHi = _mm256_or_si256(sHi, alphaOne); |
| 598 |
__m256i iaHi = _mm256_sub_epi16(c255, aHi); |
| 599 |
__m256i rHi = _mm256_add_epi16(_mm256_mullo_epi16(sHi, aHi), _mm256_mullo_epi16(dHi, iaHi)); |
| 600 |
rHi = _mm256_srli_epi16(rHi, 8); |
| 601 |
|
| 602 |
// Branchless replay of the scalar's shortcuts for a mixed block (see the quantization |
| 603 |
// contract above blendScanlineSrcAlpha) |
| 604 |
__m256i blended = _mm256_packus_epi16(rLo, rHi); |
| 605 |
__m256i out = _mm256_blendv_epi8(blended, srcPx, opaque); |
| 606 |
out = _mm256_blendv_epi8(out, dstPx, clear); |
| 607 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), out); |
| 608 |
} |
| 609 |
// Scalar tail |
| 610 |
for (; i < count; ++i, dst += 4, srcIdx++) { |
| 611 |
const std::uint8_t* entry = packed[srcIdx[0]]; |
| 612 |
const std::int32_t sA = entry[3]; |
| 613 |
if (sA == 0) continue; |
| 614 |
if (sA >= 255) { |
| 615 |
std::memcpy(dst, entry, 4); |
| 616 |
continue; |
| 617 |
} |
| 618 |
const std::int32_t inv = 255 - sA; |
| 619 |
dst[0] = static_cast<std::uint8_t>((entry[0] * sA + dst[0] * inv) >> 8); |
| 620 |
dst[1] = static_cast<std::uint8_t>((entry[1] * sA + dst[1] * inv) >> 8); |
| 621 |
dst[2] = static_cast<std::uint8_t>((entry[2] * sA + dst[2] * inv) >> 8); |
| 622 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 623 |
} |
| 624 |
}; |
| 625 |
} |
| 626 |
#endif |
| 627 |
|
| 628 |
} |
| 629 |
|
| 630 |
DEATH_CPU_DISPATCHER_BASE(fusedLutBlendScanlineImplementation) |
| 631 |
DEATH_CPU_DISPATCHED(fusedLutBlendScanlineImplementation, void DEATH_CPU_DISPATCHED_DECLARATION(fusedLutBlendScanline)(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT srcIdx, std::int32_t count, const std::uint8_t (*DEATH_RESTRICT packed)[4]))({ |
| 632 |
return fusedLutBlendScanlineImplementation(Cpu::DefaultBase)(dst, srcIdx, count, packed); |
| 633 |
}) |
| 634 |
|
| 635 |
// ===================================================================== |
| 636 |
// CPU-dispatched scanline tint (multiply RGBA by constant color) |
| 637 |
// ===================================================================== |
| 638 |
extern void DEATH_CPU_DISPATCHED_DECLARATION(tintScanline)(std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA); |
| 639 |
DEATH_CPU_DISPATCHER_DECLARATION(tintScanline) |
| 640 |
|
| 641 |
namespace |
| 642 |
{ |
| 643 |
// Scalar fallback |
| 644 |
DEATH_CPU_MAYBE_UNUSED typename std::decay<decltype(tintScanline)>::type tintScanlineImplementation(Cpu::ScalarT) { |
| 645 |
return [](std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) { |
| 646 |
for (std::int32_t i = 0; i < count; ++i, buf += 4) { |
| 647 |
buf[0] = static_cast<std::uint8_t>((buf[0] * tR) >> 8); |
| 648 |
buf[1] = static_cast<std::uint8_t>((buf[1] * tG) >> 8); |
| 649 |
buf[2] = static_cast<std::uint8_t>((buf[2] * tB) >> 8); |
| 650 |
buf[3] = static_cast<std::uint8_t>((buf[3] * tA) >> 8); |
| 651 |
} |
| 652 |
}; |
| 653 |
} |
| 654 |
|
| 655 |
#if defined(DEATH_ENABLE_SSE2) |
| 656 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SSE2 typename std::decay<decltype(tintScanline)>::type tintScanlineImplementation(Cpu::Sse2T) { |
| 657 |
return [](std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) DEATH_ENABLE_SSE2 { |
| 658 |
const __m128i zero = _mm_setzero_si128(); |
| 659 |
// Pack tint as 16-bit multipliers: [tR, tG, tB, tA, tR, tG, tB, tA] |
| 660 |
const __m128i tint = _mm_set_epi16((std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR, |
| 661 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR); |
| 662 |
|
| 663 |
std::int32_t i = 0; |
| 664 |
for (; i + 4 <= count; i += 4, buf += 16) { |
| 665 |
__m128i px = _mm_loadu_si128(reinterpret_cast<const __m128i*>(buf)); |
| 666 |
|
| 667 |
// Process low 2 pixels |
| 668 |
__m128i lo = _mm_unpacklo_epi8(px, zero); |
| 669 |
lo = _mm_srli_epi16(_mm_mullo_epi16(lo, tint), 8); |
| 670 |
|
| 671 |
// Process high 2 pixels |
| 672 |
__m128i hi = _mm_unpackhi_epi8(px, zero); |
| 673 |
hi = _mm_srli_epi16(_mm_mullo_epi16(hi, tint), 8); |
| 674 |
|
| 675 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(buf), _mm_packus_epi16(lo, hi)); |
| 676 |
} |
| 677 |
// Scalar tail |
| 678 |
for (; i < count; i++, buf += 4) { |
| 679 |
buf[0] = static_cast<std::uint8_t>((buf[0] * tR) >> 8); |
| 680 |
buf[1] = static_cast<std::uint8_t>((buf[1] * tG) >> 8); |
| 681 |
buf[2] = static_cast<std::uint8_t>((buf[2] * tB) >> 8); |
| 682 |
buf[3] = static_cast<std::uint8_t>((buf[3] * tA) >> 8); |
| 683 |
} |
| 684 |
}; |
| 685 |
} |
| 686 |
#endif |
| 687 |
|
| 688 |
#if defined(DEATH_ENABLE_AVX2) |
| 689 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_AVX2 typename std::decay<decltype(tintScanline)>::type tintScanlineImplementation(Cpu::Avx2T) { |
| 690 |
return [](std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) DEATH_ENABLE_AVX2 { |
| 691 |
const __m256i zero = _mm256_setzero_si256(); |
| 692 |
const __m256i tint = _mm256_set_epi16( |
| 693 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR, |
| 694 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR, |
| 695 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR, |
| 696 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR); |
| 697 |
|
| 698 |
std::int32_t i = 0; |
| 699 |
for (; i + 8 <= count; i += 8, buf += 32) { |
| 700 |
__m256i px = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(buf)); |
| 701 |
|
| 702 |
__m256i lo = _mm256_unpacklo_epi8(px, zero); |
| 703 |
lo = _mm256_srli_epi16(_mm256_mullo_epi16(lo, tint), 8); |
| 704 |
|
| 705 |
__m256i hi = _mm256_unpackhi_epi8(px, zero); |
| 706 |
hi = _mm256_srli_epi16(_mm256_mullo_epi16(hi, tint), 8); |
| 707 |
|
| 708 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(buf), _mm256_packus_epi16(lo, hi)); |
| 709 |
} |
| 710 |
// SSE2 tail for remaining 4-pixel groups |
| 711 |
const __m128i tint128 = _mm_set_epi16((std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR, |
| 712 |
(std::int16_t)tA, (std::int16_t)tB, (std::int16_t)tG, (std::int16_t)tR); |
| 713 |
const __m128i zero128 = _mm_setzero_si128(); |
| 714 |
for (; i + 4 <= count; i += 4, buf += 16) { |
| 715 |
__m128i px = _mm_loadu_si128(reinterpret_cast<const __m128i*>(buf)); |
| 716 |
__m128i lo = _mm_srli_epi16(_mm_mullo_epi16(_mm_unpacklo_epi8(px, zero128), tint128), 8); |
| 717 |
__m128i hi = _mm_srli_epi16(_mm_mullo_epi16(_mm_unpackhi_epi8(px, zero128), tint128), 8); |
| 718 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(buf), _mm_packus_epi16(lo, hi)); |
| 719 |
} |
| 720 |
// Scalar tail |
| 721 |
for (; i < count; i++, buf += 4) { |
| 722 |
buf[0] = static_cast<std::uint8_t>((buf[0] * tR) >> 8); |
| 723 |
buf[1] = static_cast<std::uint8_t>((buf[1] * tG) >> 8); |
| 724 |
buf[2] = static_cast<std::uint8_t>((buf[2] * tB) >> 8); |
| 725 |
buf[3] = static_cast<std::uint8_t>((buf[3] * tA) >> 8); |
| 726 |
} |
| 727 |
}; |
| 728 |
} |
| 729 |
#endif |
| 730 |
|
| 731 |
#if defined(DEATH_ENABLE_NEON) |
| 732 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_NEON typename std::decay<decltype(tintScanline)>::type tintScanlineImplementation(Cpu::NeonT) { |
| 733 |
return [](std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) DEATH_ENABLE_NEON { |
| 734 |
// The multipliers span 0-256 (256 being the identity for the `>> 8` convention), so they have to |
| 735 |
// be held as 16-bit lanes - a vdup_n_u8() would truncate 256 to 0 and turn an unscaled channel |
| 736 |
// black. 255 * 256 still fits the 16-bit product, and for out-of-contract multipliers the |
| 737 |
// mod-2^16 wraparound before the shift equals the scalar's mod-2^8 wraparound after it. |
| 738 |
const uint16x8_t vtR = vdupq_n_u16((std::uint16_t)tR); |
| 739 |
const uint16x8_t vtG = vdupq_n_u16((std::uint16_t)tG); |
| 740 |
const uint16x8_t vtB = vdupq_n_u16((std::uint16_t)tB); |
| 741 |
const uint16x8_t vtA = vdupq_n_u16((std::uint16_t)tA); |
| 742 |
|
| 743 |
std::int32_t i = 0; |
| 744 |
for (; i + 8 <= count; i += 8, buf += 32) { |
| 745 |
uint8x8x4_t px = vld4_u8(buf); |
| 746 |
// Multiply and shift: (val * tint) >> 8 |
| 747 |
px.val[0] = vshrn_n_u16(vmulq_u16(vmovl_u8(px.val[0]), vtR), 8); |
| 748 |
px.val[1] = vshrn_n_u16(vmulq_u16(vmovl_u8(px.val[1]), vtG), 8); |
| 749 |
px.val[2] = vshrn_n_u16(vmulq_u16(vmovl_u8(px.val[2]), vtB), 8); |
| 750 |
px.val[3] = vshrn_n_u16(vmulq_u16(vmovl_u8(px.val[3]), vtA), 8); |
| 751 |
vst4_u8(buf, px); |
| 752 |
} |
| 753 |
// Scalar tail |
| 754 |
for (; i < count; ++i, buf += 4) { |
| 755 |
buf[0] = static_cast<std::uint8_t>((buf[0] * tR) >> 8); |
| 756 |
buf[1] = static_cast<std::uint8_t>((buf[1] * tG) >> 8); |
| 757 |
buf[2] = static_cast<std::uint8_t>((buf[2] * tB) >> 8); |
| 758 |
buf[3] = static_cast<std::uint8_t>((buf[3] * tA) >> 8); |
| 759 |
} |
| 760 |
}; |
| 761 |
} |
| 762 |
#endif |
| 763 |
|
| 764 |
#if defined(DEATH_ENABLE_SIMD128) |
| 765 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SIMD128 typename std::decay<decltype(tintScanline)>::type tintScanlineImplementation(Cpu::Simd128T) { |
| 766 |
return [](std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) DEATH_ENABLE_SIMD128 { |
| 767 |
const v128_t tint = wasm_i16x8_make(tR, tG, tB, tA, tR, tG, tB, tA); |
| 768 |
|
| 769 |
std::int32_t i = 0; |
| 770 |
for (; i + 4 <= count; i += 4, buf += 16) { |
| 771 |
v128_t px = wasm_v128_load(buf); |
| 772 |
|
| 773 |
// The shift has to be the UNSIGNED one: a product of 32768 or more (any channel above 128 |
| 774 |
// with a near-white tint) reads as a negative i16, so an arithmetic shift would hand |
| 775 |
// wasm_u8x16_narrow_i16x8() a negative value and it would saturate the channel to 0 |
| 776 |
v128_t lo = wasm_u16x8_shr(wasm_i16x8_mul(wasm_u16x8_extend_low_u8x16(px), tint), 8); |
| 777 |
v128_t hi = wasm_u16x8_shr(wasm_i16x8_mul(wasm_u16x8_extend_high_u8x16(px), tint), 8); |
| 778 |
|
| 779 |
wasm_v128_store(buf, wasm_u8x16_narrow_i16x8(lo, hi)); |
| 780 |
} |
| 781 |
// Scalar tail |
| 782 |
for (; i < count; ++i, buf += 4) { |
| 783 |
buf[0] = static_cast<std::uint8_t>((buf[0] * tR) >> 8); |
| 784 |
buf[1] = static_cast<std::uint8_t>((buf[1] * tG) >> 8); |
| 785 |
buf[2] = static_cast<std::uint8_t>((buf[2] * tB) >> 8); |
| 786 |
buf[3] = static_cast<std::uint8_t>((buf[3] * tA) >> 8); |
| 787 |
} |
| 788 |
}; |
| 789 |
} |
| 790 |
#endif |
| 791 |
|
| 792 |
} |
| 793 |
|
| 794 |
DEATH_CPU_DISPATCHER_BASE(tintScanlineImplementation) |
| 795 |
DEATH_CPU_DISPATCHED(tintScanlineImplementation, void DEATH_CPU_DISPATCHED_DECLARATION(tintScanline)(std::uint8_t* DEATH_RESTRICT buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA))({ |
| 796 |
return tintScanlineImplementation(Cpu::DefaultBase)(buf, count, tR, tG, tB, tA); |
| 797 |
}) |
| 798 |
|
| 799 |
// ===================================================================== |
| 800 |
// CPU-dispatched constant-source blend (solid no-texture fills) |
| 801 |
// dst = (src * a + dst * (255 - a)) >> 8 per channel, alpha = (a * 255 + dst.a * (255 - a)) >> 8, |
| 802 |
// i.e. exactly the per-pixel fast-blend including its destination-alpha convention. The numerators |
| 803 |
// are per-call constants, so the SIMD forms are one multiply-add per 16-bit lane; every sum is a |
| 804 |
// convex combination bounded by 255 * 255, so it never overflows the 16-bit intermediate. |
| 805 |
// ===================================================================== |
| 806 |
extern void DEATH_CPU_DISPATCHED_DECLARATION(blendScanlineConstSrcAlpha)(std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src); |
| 807 |
DEATH_CPU_DISPATCHER_DECLARATION(blendScanlineConstSrcAlpha) |
| 808 |
|
| 809 |
namespace |
| 810 |
{ |
| 811 |
// Scalar fallback |
| 812 |
DEATH_CPU_MAYBE_UNUSED typename std::decay<decltype(blendScanlineConstSrcAlpha)>::type blendScanlineConstSrcAlphaImplementation(Cpu::ScalarT) { |
| 813 |
return [](std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src) { |
| 814 |
const std::int32_t sA = src[3]; |
| 815 |
const std::int32_t inv = 255 - sA; |
| 816 |
const std::int32_t nR = src[0] * sA, nG = src[1] * sA, nB = src[2] * sA, nA = sA * 255; |
| 817 |
for (std::int32_t i = 0; i < count; ++i, dst += 4) { |
| 818 |
dst[0] = static_cast<std::uint8_t>((nR + dst[0] * inv) >> 8); |
| 819 |
dst[1] = static_cast<std::uint8_t>((nG + dst[1] * inv) >> 8); |
| 820 |
dst[2] = static_cast<std::uint8_t>((nB + dst[2] * inv) >> 8); |
| 821 |
dst[3] = static_cast<std::uint8_t>((nA + dst[3] * inv) >> 8); |
| 822 |
} |
| 823 |
}; |
| 824 |
} |
| 825 |
|
| 826 |
#if defined(DEATH_ENABLE_SSE2) |
| 827 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SSE2 typename std::decay<decltype(blendScanlineConstSrcAlpha)>::type blendScanlineConstSrcAlphaImplementation(Cpu::Sse2T) { |
| 828 |
return [](std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src) DEATH_ENABLE_SSE2 { |
| 829 |
const std::int32_t sA = src[3]; |
| 830 |
const std::int32_t inv = 255 - sA; |
| 831 |
const std::int32_t nR = src[0] * sA, nG = src[1] * sA, nB = src[2] * sA, nA = sA * 255; |
| 832 |
const __m128i zero = _mm_setzero_si128(); |
| 833 |
const __m128i num = _mm_set_epi16((std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR, |
| 834 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR); |
| 835 |
const __m128i vinv = _mm_set1_epi16((std::int16_t)inv); |
| 836 |
|
| 837 |
std::int32_t i = 0; |
| 838 |
for (; i + 4 <= count; i += 4, dst += 16) { |
| 839 |
__m128i dstPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst)); |
| 840 |
__m128i lo = _mm_srli_epi16(_mm_add_epi16(num, _mm_mullo_epi16(_mm_unpacklo_epi8(dstPx, zero), vinv)), 8); |
| 841 |
__m128i hi = _mm_srli_epi16(_mm_add_epi16(num, _mm_mullo_epi16(_mm_unpackhi_epi8(dstPx, zero), vinv)), 8); |
| 842 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), _mm_packus_epi16(lo, hi)); |
| 843 |
} |
| 844 |
// Scalar tail |
| 845 |
for (; i < count; ++i, dst += 4) { |
| 846 |
dst[0] = static_cast<std::uint8_t>((nR + dst[0] * inv) >> 8); |
| 847 |
dst[1] = static_cast<std::uint8_t>((nG + dst[1] * inv) >> 8); |
| 848 |
dst[2] = static_cast<std::uint8_t>((nB + dst[2] * inv) >> 8); |
| 849 |
dst[3] = static_cast<std::uint8_t>((nA + dst[3] * inv) >> 8); |
| 850 |
} |
| 851 |
}; |
| 852 |
} |
| 853 |
#endif |
| 854 |
|
| 855 |
#if defined(DEATH_ENABLE_AVX2) |
| 856 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_AVX2 typename std::decay<decltype(blendScanlineConstSrcAlpha)>::type blendScanlineConstSrcAlphaImplementation(Cpu::Avx2T) { |
| 857 |
return [](std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src) DEATH_ENABLE_AVX2 { |
| 858 |
const std::int32_t sA = src[3]; |
| 859 |
const std::int32_t inv = 255 - sA; |
| 860 |
const std::int32_t nR = src[0] * sA, nG = src[1] * sA, nB = src[2] * sA, nA = sA * 255; |
| 861 |
const __m256i zero = _mm256_setzero_si256(); |
| 862 |
const __m256i num = _mm256_set_epi16( |
| 863 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR, |
| 864 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR, |
| 865 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR, |
| 866 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR); |
| 867 |
const __m256i vinv = _mm256_set1_epi16((std::int16_t)inv); |
| 868 |
|
| 869 |
std::int32_t i = 0; |
| 870 |
for (; i + 8 <= count; i += 8, dst += 32) { |
| 871 |
__m256i dstPx = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(dst)); |
| 872 |
__m256i lo = _mm256_srli_epi16(_mm256_add_epi16(num, _mm256_mullo_epi16(_mm256_unpacklo_epi8(dstPx, zero), vinv)), 8); |
| 873 |
__m256i hi = _mm256_srli_epi16(_mm256_add_epi16(num, _mm256_mullo_epi16(_mm256_unpackhi_epi8(dstPx, zero), vinv)), 8); |
| 874 |
_mm256_storeu_si256(reinterpret_cast<__m256i*>(dst), _mm256_packus_epi16(lo, hi)); |
| 875 |
} |
| 876 |
// SSE2 tail (4 pixels at a time) |
| 877 |
const __m128i zero128 = _mm_setzero_si128(); |
| 878 |
const __m128i num128 = _mm_set_epi16((std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR, |
| 879 |
(std::int16_t)nA, (std::int16_t)nB, (std::int16_t)nG, (std::int16_t)nR); |
| 880 |
const __m128i vinv128 = _mm_set1_epi16((std::int16_t)inv); |
| 881 |
for (; i + 4 <= count; i += 4, dst += 16) { |
| 882 |
__m128i dstPx = _mm_loadu_si128(reinterpret_cast<const __m128i*>(dst)); |
| 883 |
__m128i lo = _mm_srli_epi16(_mm_add_epi16(num128, _mm_mullo_epi16(_mm_unpacklo_epi8(dstPx, zero128), vinv128)), 8); |
| 884 |
__m128i hi = _mm_srli_epi16(_mm_add_epi16(num128, _mm_mullo_epi16(_mm_unpackhi_epi8(dstPx, zero128), vinv128)), 8); |
| 885 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), _mm_packus_epi16(lo, hi)); |
| 886 |
} |
| 887 |
// Scalar tail |
| 888 |
for (; i < count; ++i, dst += 4) { |
| 889 |
dst[0] = static_cast<std::uint8_t>((nR + dst[0] * inv) >> 8); |
| 890 |
dst[1] = static_cast<std::uint8_t>((nG + dst[1] * inv) >> 8); |
| 891 |
dst[2] = static_cast<std::uint8_t>((nB + dst[2] * inv) >> 8); |
| 892 |
dst[3] = static_cast<std::uint8_t>((nA + dst[3] * inv) >> 8); |
| 893 |
} |
| 894 |
}; |
| 895 |
} |
| 896 |
#endif |
| 897 |
|
| 898 |
#if defined(DEATH_ENABLE_NEON) |
| 899 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_NEON typename std::decay<decltype(blendScanlineConstSrcAlpha)>::type blendScanlineConstSrcAlphaImplementation(Cpu::NeonT) { |
| 900 |
return [](std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src) DEATH_ENABLE_NEON { |
| 901 |
const std::int32_t sA = src[3]; |
| 902 |
const std::int32_t inv = 255 - sA; |
| 903 |
const std::int32_t nR = src[0] * sA, nG = src[1] * sA, nB = src[2] * sA, nA = sA * 255; |
| 904 |
const std::uint16_t numArr[8] = { |
| 905 |
(std::uint16_t)nR, (std::uint16_t)nG, (std::uint16_t)nB, (std::uint16_t)nA, |
| 906 |
(std::uint16_t)nR, (std::uint16_t)nG, (std::uint16_t)nB, (std::uint16_t)nA |
| 907 |
}; |
| 908 |
const uint16x8_t num = vld1q_u16(numArr); |
| 909 |
const uint8x8_t vinv = vdup_n_u8((std::uint8_t)inv); |
| 910 |
|
| 911 |
std::int32_t i = 0; |
| 912 |
for (; i + 4 <= count; i += 4, dst += 16) { |
| 913 |
uint8x16_t dstPx = vld1q_u8(dst); |
| 914 |
uint16x8_t lo = vaddq_u16(num, vmull_u8(vget_low_u8(dstPx), vinv)); |
| 915 |
uint16x8_t hi = vaddq_u16(num, vmull_u8(vget_high_u8(dstPx), vinv)); |
| 916 |
vst1q_u8(dst, vcombine_u8(vshrn_n_u16(lo, 8), vshrn_n_u16(hi, 8))); |
| 917 |
} |
| 918 |
// Scalar tail |
| 919 |
for (; i < count; ++i, dst += 4) { |
| 920 |
dst[0] = static_cast<std::uint8_t>((nR + dst[0] * inv) >> 8); |
| 921 |
dst[1] = static_cast<std::uint8_t>((nG + dst[1] * inv) >> 8); |
| 922 |
dst[2] = static_cast<std::uint8_t>((nB + dst[2] * inv) >> 8); |
| 923 |
dst[3] = static_cast<std::uint8_t>((nA + dst[3] * inv) >> 8); |
| 924 |
} |
| 925 |
}; |
| 926 |
} |
| 927 |
#endif |
| 928 |
|
| 929 |
#if defined(DEATH_ENABLE_SIMD128) |
| 930 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SIMD128 typename std::decay<decltype(blendScanlineConstSrcAlpha)>::type blendScanlineConstSrcAlphaImplementation(Cpu::Simd128T) { |
| 931 |
return [](std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src) DEATH_ENABLE_SIMD128 { |
| 932 |
const std::int32_t sA = src[3]; |
| 933 |
const std::int32_t inv = 255 - sA; |
| 934 |
const std::int32_t nR = src[0] * sA, nG = src[1] * sA, nB = src[2] * sA, nA = sA * 255; |
| 935 |
const v128_t num = wasm_i16x8_make((std::int16_t)nR, (std::int16_t)nG, (std::int16_t)nB, (std::int16_t)nA, |
| 936 |
(std::int16_t)nR, (std::int16_t)nG, (std::int16_t)nB, (std::int16_t)nA); |
| 937 |
const v128_t vinv = wasm_i16x8_splat((std::int16_t)inv); |
| 938 |
|
| 939 |
std::int32_t i = 0; |
| 940 |
for (; i + 4 <= count; i += 4, dst += 16) { |
| 941 |
v128_t dstPx = wasm_v128_load(dst); |
| 942 |
v128_t lo = wasm_u16x8_shr(wasm_i16x8_add(num, wasm_i16x8_mul(wasm_u16x8_extend_low_u8x16(dstPx), vinv)), 8); |
| 943 |
v128_t hi = wasm_u16x8_shr(wasm_i16x8_add(num, wasm_i16x8_mul(wasm_u16x8_extend_high_u8x16(dstPx), vinv)), 8); |
| 944 |
wasm_v128_store(dst, wasm_u8x16_narrow_i16x8(lo, hi)); |
| 945 |
} |
| 946 |
// Scalar tail |
| 947 |
for (; i < count; ++i, dst += 4) { |
| 948 |
dst[0] = static_cast<std::uint8_t>((nR + dst[0] * inv) >> 8); |
| 949 |
dst[1] = static_cast<std::uint8_t>((nG + dst[1] * inv) >> 8); |
| 950 |
dst[2] = static_cast<std::uint8_t>((nB + dst[2] * inv) >> 8); |
| 951 |
dst[3] = static_cast<std::uint8_t>((nA + dst[3] * inv) >> 8); |
| 952 |
} |
| 953 |
}; |
| 954 |
} |
| 955 |
#endif |
| 956 |
|
| 957 |
} |
| 958 |
|
| 959 |
DEATH_CPU_DISPATCHER_BASE(blendScanlineConstSrcAlphaImplementation) |
| 960 |
DEATH_CPU_DISPATCHED(blendScanlineConstSrcAlphaImplementation, void DEATH_CPU_DISPATCHED_DECLARATION(blendScanlineConstSrcAlpha)(std::uint8_t* DEATH_RESTRICT dst, std::int32_t count, const std::uint8_t* DEATH_RESTRICT src))({ |
| 961 |
return blendScanlineConstSrcAlphaImplementation(Cpu::DefaultBase)(dst, count, src); |
| 962 |
}) |
| 963 |
|
| 964 |
// ===================================================================== |
| 965 |
// CPU-dispatched dynamic-lighting combine row (see SwScanlineOps.h) |
| 966 |
// The SSE2 variant processes 4 pixels per step with the scalar float operations replayed in the |
| 967 |
// same order per 4-wide lane, so its output is bit-identical to the scalar loop: a fully lit lane |
| 968 |
// computes (px / 255) * 1 + 0 whose re-quantization is exact for every byte, and the alpha lane |
| 969 |
// is carried through the transpose untouched. Only a scalar fallback exists for NEON/WASM (the |
| 970 |
// combine runs on dark levels only; those targets keep the previous per-pixel cost). |
| 971 |
// ===================================================================== |
| 972 |
extern void DEATH_CPU_DISPATCHED_DECLARATION(combineLightingScanline)(std::uint8_t* DEATH_RESTRICT px, std::int32_t width, const float* DEATH_RESTRICT lmRow, std::int32_t lmW, std::int32_t scale, float ambR, float ambG, float ambB); |
| 973 |
DEATH_CPU_DISPATCHER_DECLARATION(combineLightingScanline) |
| 974 |
|
| 975 |
namespace |
| 976 |
{ |
| 977 |
// Scalar fallback: the exact per-pixel loop the device ran inline before |
| 978 |
DEATH_CPU_MAYBE_UNUSED typename std::decay<decltype(combineLightingScanline)>::type combineLightingScanlineImplementation(Cpu::ScalarT) { |
| 979 |
return [](std::uint8_t* DEATH_RESTRICT px, std::int32_t width, const float* DEATH_RESTRICT lmRow, std::int32_t lmW, std::int32_t scale, float ambR, float ambG, float ambB) { |
| 980 |
for (std::int32_t x = 0; x < width; x++, px += 4) { |
| 981 |
const std::int32_t lmX = std::min(x / scale, lmW - 1); |
| 982 |
const float r = std::clamp(lmRow[lmX * 2], 0.0f, 1.0f); |
| 983 |
const float g = std::clamp(lmRow[lmX * 2 + 1], 0.0f, 1.0f); |
| 984 |
const float darkT = std::clamp(1.0f - r, 0.0f, 1.0f); |
| 985 |
if (darkT <= 0.0f && g <= 0.0f) { |
| 986 |
continue; // Fully lit, no brightness core: leave the scene pixel as-is |
| 987 |
} |
| 988 |
const float lit = 1.0f + g; |
| 989 |
const float core = (g > 0.7f ? g - 0.7f : 0.0f); |
| 990 |
float cr = (px[0] / 255.0f) * lit + core; |
| 991 |
float cg = (px[1] / 255.0f) * lit + core; |
| 992 |
float cb = (px[2] / 255.0f) * lit + core; |
| 993 |
cr += (ambR - cr) * darkT; |
| 994 |
cg += (ambG - cg) * darkT; |
| 995 |
cb += (ambB - cb) * darkT; |
| 996 |
px[0] = (std::uint8_t)std::clamp(cr * 255.0f + 0.5f, 0.0f, 255.0f); |
| 997 |
px[1] = (std::uint8_t)std::clamp(cg * 255.0f + 0.5f, 0.0f, 255.0f); |
| 998 |
px[2] = (std::uint8_t)std::clamp(cb * 255.0f + 0.5f, 0.0f, 255.0f); |
| 999 |
} |
| 1000 |
}; |
| 1001 |
} |
| 1002 |
|
| 1003 |
#if defined(DEATH_ENABLE_SSE2) |
| 1004 |
DEATH_CPU_MAYBE_UNUSED DEATH_ENABLE_SSE2 typename std::decay<decltype(combineLightingScanline)>::type combineLightingScanlineImplementation(Cpu::Sse2T) { |
| 1005 |
return [](std::uint8_t* DEATH_RESTRICT px, std::int32_t width, const float* DEATH_RESTRICT lmRow, std::int32_t lmW, std::int32_t scale, float ambR, float ambG, float ambB) DEATH_ENABLE_SSE2 { |
| 1006 |
const __m128 zerof = _mm_setzero_ps(); |
| 1007 |
const __m128 onef = _mm_set1_ps(1.0f); |
| 1008 |
const __m128 halff = _mm_set1_ps(0.5f); |
| 1009 |
const __m128 c255f = _mm_set1_ps(255.0f); |
| 1010 |
const __m128 coreT = _mm_set1_ps(0.7f); |
| 1011 |
const __m128 vAmbR = _mm_set1_ps(ambR); |
| 1012 |
const __m128 vAmbG = _mm_set1_ps(ambG); |
| 1013 |
const __m128 vAmbB = _mm_set1_ps(ambB); |
| 1014 |
const __m128i zeroi = _mm_setzero_si128(); |
| 1015 |
|
| 1016 |
// x / scale collapses to a shift when the scale is a power of two (the compositor uses 2) |
| 1017 |
std::int32_t shift = -1; |
| 1018 |
if ((scale & (scale - 1)) == 0) { |
| 1019 |
shift = 0; |
| 1020 |
while ((1 << shift) < scale) shift++; |
| 1021 |
} |
| 1022 |
const std::int32_t lmLast = lmW - 1; |
| 1023 |
|
| 1024 |
std::int32_t x = 0; |
| 1025 |
for (; x + 4 <= width; x += 4, px += 16) { |
| 1026 |
std::int32_t i0, i1, i2, i3; |
| 1027 |
if (shift >= 0) { |
| 1028 |
i0 = (x) >> shift; i1 = (x + 1) >> shift; i2 = (x + 2) >> shift; i3 = (x + 3) >> shift; |
| 1029 |
} else { |
| 1030 |
i0 = (x) / scale; i1 = (x + 1) / scale; i2 = (x + 2) / scale; i3 = (x + 3) / scale; |
| 1031 |
} |
| 1032 |
if (i0 > lmLast) i0 = lmLast; |
| 1033 |
if (i1 > lmLast) i1 = lmLast; |
| 1034 |
if (i2 > lmLast) i2 = lmLast; |
| 1035 |
if (i3 > lmLast) i3 = lmLast; |
| 1036 |
|
| 1037 |
__m128 r = _mm_setr_ps(lmRow[i0 * 2], lmRow[i1 * 2], lmRow[i2 * 2], lmRow[i3 * 2]); |
| 1038 |
__m128 g = _mm_setr_ps(lmRow[i0 * 2 + 1], lmRow[i1 * 2 + 1], lmRow[i2 * 2 + 1], lmRow[i3 * 2 + 1]); |
| 1039 |
r = _mm_min_ps(_mm_max_ps(r, zerof), onef); |
| 1040 |
g = _mm_min_ps(_mm_max_ps(g, zerof), onef); |
| 1041 |
const __m128 darkT = _mm_min_ps(_mm_max_ps(_mm_sub_ps(onef, r), zerof), onef); |
| 1042 |
|
| 1043 |
// Per-4-pixel early out (the block form of the scalar per-pixel fully-lit skip) |
| 1044 |
const __m128 active = _mm_or_ps(_mm_cmpgt_ps(darkT, zerof), _mm_cmpgt_ps(g, zerof)); |
| 1045 |
if (_mm_movemask_ps(active) == 0) { |
| 1046 |
continue; |
| 1047 |
} |
| 1048 |
|
| 1049 |
const __m128 lit = _mm_add_ps(onef, g); |
| 1050 |
const __m128 core = _mm_and_ps(_mm_sub_ps(g, coreT), _mm_cmpgt_ps(g, coreT)); |
| 1051 |
|
| 1052 |
// Deinterleave the 4 RGBA8 pixels into per-channel float vectors |
| 1053 |
const __m128i pix = _mm_loadu_si128(reinterpret_cast<const __m128i*>(px)); |
| 1054 |
const __m128i lo16 = _mm_unpacklo_epi8(pix, zeroi); |
| 1055 |
const __m128i hi16 = _mm_unpackhi_epi8(pix, zeroi); |
| 1056 |
__m128 p0 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(lo16, zeroi)); |
| 1057 |
__m128 p1 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(lo16, zeroi)); |
| 1058 |
__m128 p2 = _mm_cvtepi32_ps(_mm_unpacklo_epi16(hi16, zeroi)); |
| 1059 |
__m128 p3 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(hi16, zeroi)); |
| 1060 |
_MM_TRANSPOSE4_PS(p0, p1, p2, p3); // p0 = R, p1 = G, p2 = B, p3 = A across the 4 pixels |
| 1061 |
|
| 1062 |
// (px / 255) * lit + core, then += (amb - c) * darkT, then quantize - same op order as scalar |
| 1063 |
p0 = _mm_add_ps(_mm_mul_ps(_mm_div_ps(p0, c255f), lit), core); |
| 1064 |
p0 = _mm_add_ps(p0, _mm_mul_ps(_mm_sub_ps(vAmbR, p0), darkT)); |
| 1065 |
p0 = _mm_min_ps(_mm_max_ps(_mm_add_ps(_mm_mul_ps(p0, c255f), halff), zerof), c255f); |
| 1066 |
p1 = _mm_add_ps(_mm_mul_ps(_mm_div_ps(p1, c255f), lit), core); |
| 1067 |
p1 = _mm_add_ps(p1, _mm_mul_ps(_mm_sub_ps(vAmbG, p1), darkT)); |
| 1068 |
p1 = _mm_min_ps(_mm_max_ps(_mm_add_ps(_mm_mul_ps(p1, c255f), halff), zerof), c255f); |
| 1069 |
p2 = _mm_add_ps(_mm_mul_ps(_mm_div_ps(p2, c255f), lit), core); |
| 1070 |
p2 = _mm_add_ps(p2, _mm_mul_ps(_mm_sub_ps(vAmbB, p2), darkT)); |
| 1071 |
p2 = _mm_min_ps(_mm_max_ps(_mm_add_ps(_mm_mul_ps(p2, c255f), halff), zerof), c255f); |
| 1072 |
// p3 (alpha) is left as loaded; the truncating convert below restores the exact byte |
| 1073 |
|
| 1074 |
// Reinterleave and store (values are in [0, 255], so the signed 16-bit pack is lossless) |
| 1075 |
_MM_TRANSPOSE4_PS(p0, p1, p2, p3); |
| 1076 |
const __m128i q01 = _mm_packs_epi32(_mm_cvttps_epi32(p0), _mm_cvttps_epi32(p1)); |
| 1077 |
const __m128i q23 = _mm_packs_epi32(_mm_cvttps_epi32(p2), _mm_cvttps_epi32(p3)); |
| 1078 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(px), _mm_packus_epi16(q01, q23)); |
| 1079 |
} |
| 1080 |
// Scalar tail |
| 1081 |
for (; x < width; x++, px += 4) { |
| 1082 |
const std::int32_t lmX = std::min(x / scale, lmW - 1); |
| 1083 |
const float r = std::clamp(lmRow[lmX * 2], 0.0f, 1.0f); |
| 1084 |
const float g = std::clamp(lmRow[lmX * 2 + 1], 0.0f, 1.0f); |
| 1085 |
const float darkT = std::clamp(1.0f - r, 0.0f, 1.0f); |
| 1086 |
if (darkT <= 0.0f && g <= 0.0f) { |
| 1087 |
continue; |
| 1088 |
} |
| 1089 |
const float lit = 1.0f + g; |
| 1090 |
const float core = (g > 0.7f ? g - 0.7f : 0.0f); |
| 1091 |
float cr = (px[0] / 255.0f) * lit + core; |
| 1092 |
float cg = (px[1] / 255.0f) * lit + core; |
| 1093 |
float cb = (px[2] / 255.0f) * lit + core; |
| 1094 |
cr += (ambR - cr) * darkT; |
| 1095 |
cg += (ambG - cg) * darkT; |
| 1096 |
cb += (ambB - cb) * darkT; |
| 1097 |
px[0] = (std::uint8_t)std::clamp(cr * 255.0f + 0.5f, 0.0f, 255.0f); |
| 1098 |
px[1] = (std::uint8_t)std::clamp(cg * 255.0f + 0.5f, 0.0f, 255.0f); |
| 1099 |
px[2] = (std::uint8_t)std::clamp(cb * 255.0f + 0.5f, 0.0f, 255.0f); |
| 1100 |
} |
| 1101 |
}; |
| 1102 |
} |
| 1103 |
#endif |
| 1104 |
|
| 1105 |
} |
| 1106 |
|
| 1107 |
DEATH_CPU_DISPATCHER_BASE(combineLightingScanlineImplementation) |
| 1108 |
DEATH_CPU_DISPATCHED(combineLightingScanlineImplementation, void DEATH_CPU_DISPATCHED_DECLARATION(combineLightingScanline)(std::uint8_t* DEATH_RESTRICT px, std::int32_t width, const float* DEATH_RESTRICT lmRow, std::int32_t lmW, std::int32_t scale, float ambR, float ambG, float ambB))({ |
| 1109 |
return combineLightingScanlineImplementation(Cpu::DefaultBase)(px, width, lmRow, lmW, scale, ambR, ambG, ambB); |
| 1110 |
}) |
| 1111 |
|
| 1112 |
// Externally-visible entry points (see SwScanlineOps.h) so the tile rasterizer TU runs the exact same |
| 1113 |
// CPU-dispatched implementations instead of keeping its own (previously scalar-on-x86) copies |
| 1114 |
void BlendScanlineSrcAlpha(std::uint8_t* dst, const std::uint8_t* src, std::int32_t count) |
| 1115 |
{ |
| 1116 |
blendScanlineSrcAlpha(dst, src, count); |
| 1117 |
} |
| 1118 |
|
| 1119 |
void FusedLutBlendScanline(std::uint8_t* dst, const std::uint8_t* srcIdx, std::int32_t count, const std::uint8_t (*packed)[4]) |
| 1120 |
{ |
| 1121 |
fusedLutBlendScanline(dst, srcIdx, count, packed); |
| 1122 |
} |
| 1123 |
|
| 1124 |
void TintScanline(std::uint8_t* buf, std::int32_t count, std::int32_t tR, std::int32_t tG, std::int32_t tB, std::int32_t tA) |
| 1125 |
{ |
| 1126 |
tintScanline(buf, count, tR, tG, tB, tA); |
| 1127 |
} |
| 1128 |
|
| 1129 |
void BlendScanlineConstSrcAlpha(std::uint8_t* dst, std::int32_t count, const std::uint8_t src[4]) |
| 1130 |
{ |
| 1131 |
blendScanlineConstSrcAlpha(dst, count, src); |
| 1132 |
} |
| 1133 |
|
| 1134 |
void CombineLightingScanline(std::uint8_t* px, std::int32_t width, const float* lmRow, std::int32_t lmW, |
| 1135 |
std::int32_t scale, float ambR, float ambG, float ambB) |
| 1136 |
{ |
| 1137 |
combineLightingScanline(px, width, lmRow, lmW, scale, ambR, ambG, ambB); |
| 1138 |
} |
| 1139 |
|
| 1140 |
// ========================================================================= |
| 1141 |
// Rasterization helpers |
| 1142 |
// ========================================================================= |
| 1143 |
namespace |
| 1144 |
{ |
| 1145 |
// UV wrapping for float coordinates [0..1] — shared by both rasterizers |
| 1146 |
inline float WrapUV(float t, SamplerWrapping mode) |
| 1147 |
{ |
| 1148 |
switch (mode) { |
| 1149 |
case SamplerWrapping::Repeat: |
| 1150 |
t -= std::floor(t); |
| 1151 |
return t; |
| 1152 |
case SamplerWrapping::MirroredRepeat: { |
| 1153 |
float f = std::floor(t); |
| 1154 |
t -= f; |
| 1155 |
if (static_cast<std::int32_t>(f) & 1) t = 1.0f - t; |
| 1156 |
return t; |
| 1157 |
} |
| 1158 |
default: // ClampToEdge |
| 1159 |
if (t < 0.0f) return 0.0f; |
| 1160 |
if (t > 1.0f) return 1.0f; |
| 1161 |
return t; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
// Fixed-point UV wrapping for the axis-aligned quad rasterizer. |
| 1166 |
// coordFix is in 16.16 format representing pixel index (u * texW * 65536). |
| 1167 |
// Returns pixel index in [0, texDim). |
| 1168 |
inline std::int32_t WrapTexelFix(std::int32_t coordFix, std::int32_t texDim, SamplerWrapping mode) |
| 1169 |
{ |
| 1170 |
std::int32_t idx = coordFix >> 16; |
| 1171 |
switch (mode) { |
| 1172 |
case SamplerWrapping::Repeat: |
| 1173 |
idx = idx % texDim; |
| 1174 |
if (idx < 0) idx += texDim; |
| 1175 |
return idx; |
| 1176 |
case SamplerWrapping::MirroredRepeat: { |
| 1177 |
std::int32_t period = texDim * 2; |
| 1178 |
idx = idx % period; |
| 1179 |
if (idx < 0) idx += period; |
| 1180 |
if (idx >= texDim) idx = period - 1 - idx; |
| 1181 |
return idx; |
| 1182 |
} |
| 1183 |
default: // ClampToEdge |
| 1184 |
if (idx < 0) return 0; |
| 1185 |
if (idx >= texDim) return texDim - 1; |
| 1186 |
return idx; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
|
| 1190 |
// Normalize a fixed-point UV to [0, texDimFix) for Repeat wrapping. |
| 1191 |
// This is called once per scanline to allow branch-based wrap per pixel. |
| 1192 |
inline std::int32_t NormalizeRepeatFix(std::int32_t coordFix, std::int32_t texDimFix) |
| 1193 |
{ |
| 1194 |
coordFix = coordFix % texDimFix; |
| 1195 |
if (coordFix < 0) coordFix += texDimFix; |
| 1196 |
return coordFix; |
| 1197 |
} |
| 1198 |
|
| 1199 |
// Advance a Repeat-normalized fixed-point UV by one step. |
| 1200 |
inline std::int32_t AdvanceRepeatFix(std::int32_t coordFix, std::int32_t step, std::int32_t texDimFix) |
| 1201 |
{ |
| 1202 |
coordFix += step; |
| 1203 |
if (coordFix >= texDimFix) coordFix -= texDimFix; |
| 1204 |
else if (coordFix < 0) coordFix += texDimFix; |
| 1205 |
return coordFix; |
| 1206 |
} |
| 1207 |
|
| 1208 |
// Generic integer blend for arbitrary SwBlendFactor (used by triangle rasterizer) |
| 1209 |
inline void BlendPixelGeneric(std::uint8_t* dst, std::int32_t sR, std::int32_t sG, |
| 1210 |
std::int32_t sB, std::int32_t sA, |
| 1211 |
SwBlendFactor sFactor, SwBlendFactor dFactor) |
| 1212 |
{ |
| 1213 |
if (sFactor == SwBlendFactor::SrcAlpha && dFactor == SwBlendFactor::OneMinusSrcAlpha) { |
| 1214 |
// Fast path: most common blend mode |
| 1215 |
const std::int32_t inv = 255 - sA; |
| 1216 |
dst[0] = static_cast<std::uint8_t>((sR * sA + dst[0] * inv) >> 8); |
| 1217 |
dst[1] = static_cast<std::uint8_t>((sG * sA + dst[1] * inv) >> 8); |
| 1218 |
dst[2] = static_cast<std::uint8_t>((sB * sA + dst[2] * inv) >> 8); |
| 1219 |
dst[3] = static_cast<std::uint8_t>((sA * 255 + dst[3] * inv) >> 8); |
| 1220 |
return; |
| 1221 |
} |
| 1222 |
// General fallback using float math |
| 1223 |
const float srcRf = sR / 255.0f, srcGf = sG / 255.0f, srcBf = sB / 255.0f, srcAf = sA / 255.0f; |
| 1224 |
const float dstRf = dst[0] / 255.0f, dstGf = dst[1] / 255.0f, dstBf = dst[2] / 255.0f, dstAf = dst[3] / 255.0f; |
| 1225 |
auto bf = [&](SwBlendFactor f, float c, bool isSrc) -> float { |
| 1226 |
switch (f) { |
| 1227 |
case SwBlendFactor::Zero: return 0.0f; |
| 1228 |
case SwBlendFactor::One: return c; |
| 1229 |
case SwBlendFactor::SrcAlpha: return c * srcAf; |
| 1230 |
case SwBlendFactor::OneMinusSrcAlpha: return c * (1.0f - srcAf); |
| 1231 |
case SwBlendFactor::DstAlpha: return c * dstAf; |
| 1232 |
case SwBlendFactor::OneMinusDstAlpha: return c * (1.0f - dstAf); |
| 1233 |
case SwBlendFactor::SrcColor: return c * (isSrc ? 1.0f : srcRf); |
| 1234 |
case SwBlendFactor::DstColor: return c * (isSrc ? dstRf : 1.0f); |
| 1235 |
default: return c; |
| 1236 |
} |
| 1237 |
}; |
| 1238 |
auto clamp01 = [](float v) { return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v); }; |
| 1239 |
dst[0] = static_cast<std::uint8_t>(clamp01(bf(sFactor, srcRf, true) + bf(dFactor, dstRf, false)) * 255.0f); |
| 1240 |
dst[1] = static_cast<std::uint8_t>(clamp01(bf(sFactor, srcGf, true) + bf(dFactor, dstGf, false)) * 255.0f); |
| 1241 |
dst[2] = static_cast<std::uint8_t>(clamp01(bf(sFactor, srcBf, true) + bf(dFactor, dstBf, false)) * 255.0f); |
| 1242 |
dst[3] = static_cast<std::uint8_t>(clamp01(bf(sFactor, srcAf, true) + bf(dFactor, dstAf, false)) * 255.0f); |
| 1243 |
} |
| 1244 |
|
| 1245 |
// Wrap a signed integer texel coordinate into [0, texDim) |
| 1246 |
inline std::int32_t WrapTexelCoord(std::int32_t idx, std::int32_t texDim, SamplerWrapping mode) |
| 1247 |
{ |
| 1248 |
switch (mode) { |
| 1249 |
case SamplerWrapping::Repeat: |
| 1250 |
idx = idx % texDim; |
| 1251 |
if (idx < 0) idx += texDim; |
| 1252 |
return idx; |
| 1253 |
case SamplerWrapping::MirroredRepeat: { |
| 1254 |
std::int32_t period = texDim * 2; |
| 1255 |
idx = idx % period; |
| 1256 |
if (idx < 0) idx += period; |
| 1257 |
if (idx >= texDim) idx = period - 1 - idx; |
| 1258 |
return idx; |
| 1259 |
} |
| 1260 |
default: // ClampToEdge |
| 1261 |
if (idx < 0) return 0; |
| 1262 |
if (idx >= texDim) return texDim - 1; |
| 1263 |
return idx; |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
// Bilinear texture sample from 16.16 fixed-point UV coordinates. |
| 1268 |
// uFix/vFix represent pixel coords (u * texW * 65536, v * texH * 65536). |
| 1269 |
// texBpp is the store's bytes per texel (SwTexture stores R8/RG8 natively); narrow texels are |
| 1270 |
// expanded to the 4-byte working form before weighting, identical to the promoted store's bytes. |
| 1271 |
// Result is written to out[0..3] as RGBA bytes. |
| 1272 |
inline void SampleBilinearFix(const std::uint8_t* texPixels, std::int32_t texW, std::int32_t texH, std::int32_t texBpp, |
| 1273 |
std::int32_t uFix, std::int32_t vFix, |
| 1274 |
SamplerWrapping wrapS, SamplerWrapping wrapT, |
| 1275 |
std::uint8_t* out) |
| 1276 |
{ |
| 1277 |
// Half-pixel offset for correct bilinear centering |
| 1278 |
const std::int32_t uf = uFix - (1 << 15); |
| 1279 |
const std::int32_t vf = vFix - (1 << 15); |
| 1280 |
|
| 1281 |
std::int32_t x0 = uf >> 16; |
| 1282 |
std::int32_t y0 = vf >> 16; |
| 1283 |
|
| 1284 |
// 8-bit fractional weights |
| 1285 |
const std::int32_t fx = (uf & 0xFFFF) >> 8; |
| 1286 |
const std::int32_t fy = (vf & 0xFFFF) >> 8; |
| 1287 |
|
| 1288 |
// Wrap all four sample coordinates |
| 1289 |
std::int32_t x1 = WrapTexelCoord(x0 + 1, texW, wrapS); |
| 1290 |
std::int32_t y1 = WrapTexelCoord(y0 + 1, texH, wrapT); |
| 1291 |
x0 = WrapTexelCoord(x0, texW, wrapS); |
| 1292 |
y0 = WrapTexelCoord(y0, texH, wrapT); |
| 1293 |
|
| 1294 |
const std::uint8_t* c00 = texPixels + (static_cast<std::size_t>(y0) * texW + x0) * texBpp; |
| 1295 |
const std::uint8_t* c10 = texPixels + (static_cast<std::size_t>(y0) * texW + x1) * texBpp; |
| 1296 |
const std::uint8_t* c01 = texPixels + (static_cast<std::size_t>(y1) * texW + x0) * texBpp; |
| 1297 |
const std::uint8_t* c11 = texPixels + (static_cast<std::size_t>(y1) * texW + x1) * texBpp; |
| 1298 |
std::uint8_t e00[4], e10[4], e01[4], e11[4]; |
| 1299 |
if DEATH_UNLIKELY(texBpp != 4) { |
| 1300 |
SwExpandTexel(e00, c00, texBpp); c00 = e00; |
| 1301 |
SwExpandTexel(e10, c10, texBpp); c10 = e10; |
| 1302 |
SwExpandTexel(e01, c01, texBpp); c01 = e01; |
| 1303 |
SwExpandTexel(e11, c11, texBpp); c11 = e11; |
| 1304 |
} |
| 1305 |
|
| 1306 |
const std::int32_t ifx = 255 - fx; |
| 1307 |
const std::int32_t ify = 255 - fy; |
| 1308 |
const std::int32_t w00 = ifx * ify; |
| 1309 |
const std::int32_t w10 = fx * ify; |
| 1310 |
const std::int32_t w01 = ifx * fy; |
| 1311 |
const std::int32_t w11 = fx * fy; |
| 1312 |
|
| 1313 |
out[0] = static_cast<std::uint8_t>((c00[0] * w00 + c10[0] * w10 + c01[0] * w01 + c11[0] * w11) >> 16); |
| 1314 |
out[1] = static_cast<std::uint8_t>((c00[1] * w00 + c10[1] * w10 + c01[1] * w01 + c11[1] * w11) >> 16); |
| 1315 |
out[2] = static_cast<std::uint8_t>((c00[2] * w00 + c10[2] * w10 + c01[2] * w01 + c11[2] * w11) >> 16); |
| 1316 |
out[3] = static_cast<std::uint8_t>((c00[3] * w00 + c10[3] * w10 + c01[3] * w01 + c11[3] * w11) >> 16); |
| 1317 |
} |
| 1318 |
|
| 1319 |
// Pre-computed bilinear Y context for scanline-optimized sampling. |
| 1320 |
// Y coordinates and weights are constant across a scanline, so precompute once. |
| 1321 |
struct BilinearRowCtx |
| 1322 |
{ |
| 1323 |
const std::uint8_t* row0; |
| 1324 |
const std::uint8_t* row1; |
| 1325 |
std::int32_t ify; // 255 - fy (top row weight) |
| 1326 |
std::int32_t fy; // fy (bottom row weight) |
| 1327 |
}; |
| 1328 |
|
| 1329 |
inline BilinearRowCtx PrepareBilinearRow(const std::uint8_t* texPixels, std::int32_t texW, std::int32_t texH, std::int32_t texBpp, |
| 1330 |
std::int32_t vFix, SamplerWrapping wrapT) |
| 1331 |
{ |
| 1332 |
const std::int32_t vf = vFix - (1 << 15); |
| 1333 |
std::int32_t y0 = vf >> 16; |
| 1334 |
const std::int32_t fy = (vf & 0xFFFF) >> 8; |
| 1335 |
std::int32_t y1 = WrapTexelCoord(y0 + 1, texH, wrapT); |
| 1336 |
y0 = WrapTexelCoord(y0, texH, wrapT); |
| 1337 |
return { |
| 1338 |
texPixels + static_cast<std::size_t>(y0) * texW * texBpp, |
| 1339 |
texPixels + static_cast<std::size_t>(y1) * texW * texBpp, |
| 1340 |
255 - fy, fy |
| 1341 |
}; |
| 1342 |
} |
| 1343 |
|
| 1344 |
inline void SampleBilinearFixRow(const BilinearRowCtx& row, std::int32_t texW, std::int32_t texBpp, |
| 1345 |
std::int32_t uFix, SamplerWrapping wrapS, |
| 1346 |
std::uint8_t* out) |
| 1347 |
{ |
| 1348 |
const std::int32_t uf = uFix - (1 << 15); |
| 1349 |
std::int32_t x0 = uf >> 16; |
| 1350 |
const std::int32_t fx = (uf & 0xFFFF) >> 8; |
| 1351 |
std::int32_t x1 = WrapTexelCoord(x0 + 1, texW, wrapS); |
| 1352 |
x0 = WrapTexelCoord(x0, texW, wrapS); |
| 1353 |
|
| 1354 |
const std::uint8_t* c00 = row.row0 + x0 * texBpp; |
| 1355 |
const std::uint8_t* c10 = row.row0 + x1 * texBpp; |
| 1356 |
const std::uint8_t* c01 = row.row1 + x0 * texBpp; |
| 1357 |
const std::uint8_t* c11 = row.row1 + x1 * texBpp; |
| 1358 |
std::uint8_t e00[4], e10[4], e01[4], e11[4]; |
| 1359 |
if DEATH_UNLIKELY(texBpp != 4) { |
| 1360 |
SwExpandTexel(e00, c00, texBpp); c00 = e00; |
| 1361 |
SwExpandTexel(e10, c10, texBpp); c10 = e10; |
| 1362 |
SwExpandTexel(e01, c01, texBpp); c01 = e01; |
| 1363 |
SwExpandTexel(e11, c11, texBpp); c11 = e11; |
| 1364 |
} |
| 1365 |
|
| 1366 |
const std::int32_t ifx = 255 - fx; |
| 1367 |
const std::int32_t w00 = ifx * row.ify; |
| 1368 |
const std::int32_t w10 = fx * row.ify; |
| 1369 |
const std::int32_t w01 = ifx * row.fy; |
| 1370 |
const std::int32_t w11 = fx * row.fy; |
| 1371 |
|
| 1372 |
out[0] = static_cast<std::uint8_t>((c00[0] * w00 + c10[0] * w10 + c01[0] * w01 + c11[0] * w11) >> 16); |
| 1373 |
out[1] = static_cast<std::uint8_t>((c00[1] * w00 + c10[1] * w10 + c01[1] * w01 + c11[1] * w11) >> 16); |
| 1374 |
out[2] = static_cast<std::uint8_t>((c00[2] * w00 + c10[2] * w10 + c01[2] * w01 + c11[2] * w11) >> 16); |
| 1375 |
out[3] = static_cast<std::uint8_t>((c00[3] * w00 + c10[3] * w10 + c01[3] * w01 + c11[3] * w11) >> 16); |
| 1376 |
} |
| 1377 |
|
| 1378 |
// Bilinear texture sample from float UV coordinates in [0..1] range. |
| 1379 |
// Used by the triangle rasterizer. |
| 1380 |
inline void SampleBilinearFloat(const std::uint8_t* texPixels, std::int32_t texW, std::int32_t texH, std::int32_t texBpp, |
| 1381 |
float u, float v, |
| 1382 |
SamplerWrapping wrapS, SamplerWrapping wrapT, |
| 1383 |
std::uint8_t* out) |
| 1384 |
{ |
| 1385 |
const float uf = u * texW - 0.5f; |
| 1386 |
const float vf = v * texH - 0.5f; |
| 1387 |
|
| 1388 |
std::int32_t x0 = static_cast<std::int32_t>(std::floor(uf)); |
| 1389 |
std::int32_t y0 = static_cast<std::int32_t>(std::floor(vf)); |
| 1390 |
|
| 1391 |
std::int32_t fx = static_cast<std::int32_t>((uf - x0) * 255.0f + 0.5f); |
| 1392 |
std::int32_t fy = static_cast<std::int32_t>((vf - y0) * 255.0f + 0.5f); |
| 1393 |
if (fx < 0) fx = 0; if (fx > 255) fx = 255; |
| 1394 |
if (fy < 0) fy = 0; if (fy > 255) fy = 255; |
| 1395 |
|
| 1396 |
std::int32_t x1 = WrapTexelCoord(x0 + 1, texW, wrapS); |
| 1397 |
std::int32_t y1 = WrapTexelCoord(y0 + 1, texH, wrapT); |
| 1398 |
x0 = WrapTexelCoord(x0, texW, wrapS); |
| 1399 |
y0 = WrapTexelCoord(y0, texH, wrapT); |
| 1400 |
|
| 1401 |
const std::uint8_t* c00 = texPixels + (static_cast<std::size_t>(y0) * texW + x0) * texBpp; |
| 1402 |
const std::uint8_t* c10 = texPixels + (static_cast<std::size_t>(y0) * texW + x1) * texBpp; |
| 1403 |
const std::uint8_t* c01 = texPixels + (static_cast<std::size_t>(y1) * texW + x0) * texBpp; |
| 1404 |
const std::uint8_t* c11 = texPixels + (static_cast<std::size_t>(y1) * texW + x1) * texBpp; |
| 1405 |
std::uint8_t e00[4], e10[4], e01[4], e11[4]; |
| 1406 |
if DEATH_UNLIKELY(texBpp != 4) { |
| 1407 |
SwExpandTexel(e00, c00, texBpp); c00 = e00; |
| 1408 |
SwExpandTexel(e10, c10, texBpp); c10 = e10; |
| 1409 |
SwExpandTexel(e01, c01, texBpp); c01 = e01; |
| 1410 |
SwExpandTexel(e11, c11, texBpp); c11 = e11; |
| 1411 |
} |
| 1412 |
|
| 1413 |
const std::int32_t ifx = 255 - fx; |
| 1414 |
const std::int32_t ify = 255 - fy; |
| 1415 |
const std::int32_t w00 = ifx * ify; |
| 1416 |
const std::int32_t w10 = fx * ify; |
| 1417 |
const std::int32_t w01 = ifx * fy; |
| 1418 |
const std::int32_t w11 = fx * fy; |
| 1419 |
|
| 1420 |
out[0] = static_cast<std::uint8_t>((c00[0] * w00 + c10[0] * w10 + c01[0] * w01 + c11[0] * w11) >> 16); |
| 1421 |
out[1] = static_cast<std::uint8_t>((c00[1] * w00 + c10[1] * w10 + c01[1] * w01 + c11[1] * w11) >> 16); |
| 1422 |
out[2] = static_cast<std::uint8_t>((c00[2] * w00 + c10[2] * w10 + c01[2] * w01 + c11[2] * w11) >> 16); |
| 1423 |
out[3] = static_cast<std::uint8_t>((c00[3] * w00 + c10[3] * w10 + c01[3] * w01 + c11[3] * w11) >> 16); |
| 1424 |
} |
| 1425 |
|
| 1426 |
// ===================================================================== |
| 1427 |
// Fast blit: direct memcpy / stretch for simple fullscreen texture copies. |
| 1428 |
// Returns true if the draw was handled as a fast blit. |
| 1429 |
// ===================================================================== |
| 1430 |
bool TryFastBlit(const DrawContext& ctx, PrimitiveType type, std::int32_t firstVertex, std::int32_t count) |
| 1431 |
{ |
| 1432 |
// Only handles procedural fullscreen quads |
| 1433 |
if (type != PrimitiveType::TriangleStrip || count != 4 || firstVertex != 0) return false; |
| 1434 |
if (ctx.vertexData != nullptr) return false; |
| 1435 |
if (ctx.fragmentShader != nullptr) return false; |
| 1436 |
if (ctx.scissorEnabled) return false; |
| 1437 |
|
| 1438 |
// Must be textured with white tint (pure blit) |
| 1439 |
if (!ctx.ff.hasTexture) return false; |
| 1440 |
if (ctx.ff.color[0] < 0.999f || ctx.ff.color[1] < 0.999f || |
| 1441 |
ctx.ff.color[2] < 0.999f || ctx.ff.color[3] < 0.999f) return false; |
| 1442 |
|
| 1443 |
// Must cover full texRect |
| 1444 |
if (ctx.ff.texRect[0] < 0.999f || ctx.ff.texRect[2] < 0.999f) return false; |
| 1445 |
|
| 1446 |
// No blending (opaque copy) |
| 1447 |
if (ctx.blendingEnabled) return false; |
| 1448 |
|
| 1449 |
const SwTexture* tex = ctx.textures[ctx.ff.textureUnit]; |
| 1450 |
if (tex == nullptr) return false; |
| 1451 |
// The blit copies 4-byte rows verbatim; a native R8/RG8 store would need per-texel expansion |
| 1452 |
// (only screen-sized RGBA8 sources ever hit this path, so nothing real is lost) |
| 1453 |
if (tex->GetBytesPerPixel() != 4) return false; |
| 1454 |
const std::uint8_t* srcPixels = tex->GetPixels(0); |
| 1455 |
if (srcPixels == nullptr) return false; |
| 1456 |
|
| 1457 |
const std::int32_t srcW = tex->GetWidth(); |
| 1458 |
const std::int32_t srcH = tex->GetHeight(); |
| 1459 |
if (srcW <= 0 || srcH <= 0) return false; |
| 1460 |
|
| 1461 |
// Check that spriteSize covers the full viewport (± 1 pixel tolerance) |
| 1462 |
const std::int32_t vpW = g_state.viewportW; |
| 1463 |
const std::int32_t vpH = g_state.viewportH; |
| 1464 |
if (std::abs(static_cast<std::int32_t>(ctx.ff.spriteSize[0]) - vpW) > 1) return false; |
| 1465 |
if (std::abs(static_cast<std::int32_t>(ctx.ff.spriteSize[1]) - vpH) > 1) return false; |
| 1466 |
|
| 1467 |
// Verify the MVP maps (0,0)-(spriteSize) to cover the viewport |
| 1468 |
// For a standard fullscreen sprite, m[0]*spriteSize[0] should map to +1 or -1 in NDC |
| 1469 |
const float* m = ctx.ff.mvpMatrix; |
| 1470 |
// After viewport transform: x = (ndcX + 1) * 0.5 * vpW + vpX |
| 1471 |
// For full coverage: NDC x ranges from -1 to +1 → screen 0 to vpW |
| 1472 |
// That means m[0]*spriteSize[0] + m[12] should be ~+1 (right edge) |
| 1473 |
// and m[12] should be ~-1 (left edge in NDC) |
| 1474 |
float ndcLeft = m[12]; |
| 1475 |
float ndcRight = m[0] * ctx.ff.spriteSize[0] + m[12]; |
| 1476 |
float ndcTop = m[5] * ctx.ff.spriteSize[1] + m[13]; |
| 1477 |
float ndcBottom = m[13]; |
| 1478 |
if (std::fabs(ndcLeft - (-1.0f)) > 0.01f || std::fabs(ndcRight - 1.0f) > 0.01f) return false; |
| 1479 |
// Allow both normal (bottom=-1, top=+1) and Y-flipped (bottom=+1, top=-1) quads |
| 1480 |
if (std::fabs(ndcBottom - (-1.0f)) < 0.01f && std::fabs(ndcTop - 1.0f) < 0.01f) { |
| 1481 |
// Normal orientation |
| 1482 |
} else if (std::fabs(ndcBottom - 1.0f) < 0.01f && std::fabs(ndcTop - (-1.0f)) < 0.01f) { |
| 1483 |
// Y-flipped quad (common for top-down game coordinate projections) |
| 1484 |
} else { |
| 1485 |
return false; |
| 1486 |
} |
| 1487 |
// No rotation |
| 1488 |
if (std::fabs(m[1]) > 0.001f || std::fabs(m[4]) > 0.001f) return false; |
| 1489 |
|
| 1490 |
std::uint8_t* dstBuffer = g_state.colorBuffer; |
| 1491 |
const std::int32_t dstW = g_state.bufferWidth; |
| 1492 |
const std::int32_t dstH = g_state.bufferHeight; |
| 1493 |
if (dstBuffer == nullptr) return false; |
| 1494 |
|
| 1495 |
// Flush any pending deferred draws first, so this opaque full-screen blit (which bypasses the |
| 1496 |
// tile queue and writes the buffer directly) lands after everything submitted before it. |
| 1497 |
SwTileRenderer::Flush(); |
| 1498 |
|
| 1499 |
// Y-flip needed when source and destination have different row orders: |
| 1500 |
// - FBO textures/buffers are stored bottom-up (row 0 = bottom) |
| 1501 |
// - Regular textures and screen buffer are stored top-down (row 0 = top) |
| 1502 |
// The quad's Y orientation in NDC is irrelevant — it's just the coordinate system, |
| 1503 |
// not an intentional "flip the image" instruction. |
| 1504 |
const bool sourceIsFbo = tex->IsRenderTarget(); |
| 1505 |
const bool flipY = (sourceIsFbo != g_state.isFboTarget); |
| 1506 |
|
| 1507 |
#if defined(RHI_USE_FB16) |
| 1508 |
if (g_state.is16Bit) { |
| 1509 |
// 16-bit screen destination: the same identity / nearest-stretch source walk, packing each |
| 1510 |
// row to 565 on the way out (SwStoreFbSpan565 - SIMD) instead of round-tripping the whole |
| 1511 |
// frame through the rasterizer's row staging (unpack, overwrite, repack). This is the |
| 1512 |
// present-family blit that runs every frame on the FB16 build, and the sampling positions |
| 1513 |
// now match the RGBA8 build's blit exactly, so both framebuffer modes present the same image. |
| 1514 |
if (srcW == dstW && srcH == dstH) { |
| 1515 |
for (std::int32_t y = 0; y < srcH; y++) { |
| 1516 |
const std::uint8_t* srcRow = srcPixels + std::size_t(y) * srcW * 4; |
| 1517 |
std::uint8_t* dstRow = dstBuffer + std::size_t(flipY ? (dstH - 1 - y) : y) * dstW * 2; |
| 1518 |
SwStoreFbSpan565(dstRow, srcRow, srcW); |
| 1519 |
} |
| 1520 |
} else { |
| 1521 |
const std::uint32_t scaleX_fp = (static_cast<std::uint32_t>(srcW) << 16) / static_cast<std::uint32_t>(dstW); |
| 1522 |
const std::uint32_t scaleY_fp = (static_cast<std::uint32_t>(srcH) << 16) / static_cast<std::uint32_t>(dstH); |
| 1523 |
for (std::int32_t dy = 0; dy < dstH; dy++) { |
| 1524 |
const std::int32_t sy = static_cast<std::int32_t>((static_cast<std::uint32_t>(dy) * scaleY_fp) >> 16); |
| 1525 |
const std::int32_t actualDstY = flipY ? (dstH - 1 - dy) : dy; |
| 1526 |
std::uint8_t* dstRow = dstBuffer + std::size_t(actualDstY) * dstW * 2; |
| 1527 |
const std::uint8_t* srcRow = srcPixels + std::size_t(sy) * srcW * 4; |
| 1528 |
if (srcW == dstW) { |
| 1529 |
SwStoreFbSpan565(dstRow, srcRow, dstW); |
| 1530 |
} else { |
| 1531 |
// Fused nearest gather + pack: one 565 store per destination pixel |
| 1532 |
std::uint16_t* dstRow16 = reinterpret_cast<std::uint16_t*>(dstRow); |
| 1533 |
std::uint32_t srcX_fp = 0; |
| 1534 |
for (std::int32_t dx = 0; dx < dstW; dx++) { |
| 1535 |
dstRow16[dx] = SwPack565(&srcRow[(srcX_fp >> 16) * 4]); |
| 1536 |
srcX_fp += scaleX_fp; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
} |
| 1540 |
} |
| 1541 |
return true; |
| 1542 |
} |
| 1543 |
#endif |
| 1544 |
|
| 1545 |
if (srcW == dstW && srcH == dstH) { |
| 1546 |
// Identity blit: direct memcpy (fastest path) |
| 1547 |
if (!flipY) { |
| 1548 |
std::memcpy(dstBuffer, srcPixels, static_cast<std::size_t>(srcW) * srcH * 4); |
| 1549 |
} else { |
| 1550 |
// FBO target: flip Y during copy |
| 1551 |
const std::int32_t rowBytes = srcW * 4; |
| 1552 |
for (std::int32_t y = 0; y < srcH; y++) { |
| 1553 |
const std::uint8_t* srcRow = srcPixels + y * rowBytes; |
| 1554 |
std::uint8_t* dstRow = dstBuffer + (dstH - 1 - y) * rowBytes; |
| 1555 |
std::memcpy(dstRow, srcRow, rowBytes); |
| 1556 |
} |
| 1557 |
} |
| 1558 |
} else { |
| 1559 |
// Nearest-neighbor stretch blit |
| 1560 |
const std::int32_t dstRowBytes = dstW * 4; |
| 1561 |
// Fixed-point scale factors (16.16) |
| 1562 |
const std::uint32_t scaleX_fp = (static_cast<std::uint32_t>(srcW) << 16) / static_cast<std::uint32_t>(dstW); |
| 1563 |
const std::uint32_t scaleY_fp = (static_cast<std::uint32_t>(srcH) << 16) / static_cast<std::uint32_t>(dstH); |
| 1564 |
|
| 1565 |
for (std::int32_t dy = 0; dy < dstH; dy++) { |
| 1566 |
const std::int32_t sy = static_cast<std::int32_t>((static_cast<std::uint32_t>(dy) * scaleY_fp) >> 16); |
| 1567 |
const std::int32_t actualDstY = flipY ? (dstH - 1 - dy) : dy; |
| 1568 |
std::uint8_t* dstRow = dstBuffer + actualDstY * dstRowBytes; |
| 1569 |
const std::uint8_t* srcRow = srcPixels + sy * srcW * 4; |
| 1570 |
|
| 1571 |
if (srcW == dstW) { |
| 1572 |
// Same width, different height: row copy |
| 1573 |
std::memcpy(dstRow, srcRow, dstRowBytes); |
| 1574 |
} else { |
| 1575 |
// Different width: nearest-neighbor horizontal stretch |
| 1576 |
std::uint32_t srcX_fp = 0; |
| 1577 |
const std::uint32_t* srcRow32 = reinterpret_cast<const std::uint32_t*>(srcRow); |
| 1578 |
std::uint32_t* dstRow32 = reinterpret_cast<std::uint32_t*>(dstRow); |
| 1579 |
for (std::int32_t dx = 0; dx < dstW; dx++) { |
| 1580 |
const std::int32_t sx = static_cast<std::int32_t>(srcX_fp >> 16); |
| 1581 |
dstRow32[dx] = srcRow32[sx]; |
| 1582 |
srcX_fp += scaleX_fp; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
return true; |
| 1589 |
} |
| 1590 |
|
| 1591 |
// ===================================================================== |
| 1592 |
// Vertex fetch (screen-space, after MVP + viewport transform) |
| 1593 |
// ===================================================================== |
| 1594 |
Vertex2D FetchVertex(const DrawContext& ctx, std::int32_t index) |
| 1595 |
{ |
| 1596 |
Vertex2D out = { 0, 0, 0, 0, 1, 1, 1, 1 }; |
| 1597 |
|
| 1598 |
if DEATH_LIKELY(ctx.vertexData == nullptr) { |
| 1599 |
// Procedural sprite quad — replicate the sprite_vs.glsl vertex synthesis |
| 1600 |
const float ax = ((index & ~1) == 0) ? 1.0f : 0.0f; |
| 1601 |
const float ay = (index & 1) ? 1.0f : 0.0f; |
| 1602 |
|
| 1603 |
const float wx = ax * ctx.ff.spriteSize[0]; |
| 1604 |
const float wy = ay * ctx.ff.spriteSize[1]; |
| 1605 |
|
| 1606 |
const float* m = ctx.ff.mvpMatrix; |
| 1607 |
out.x = m[0] * wx + m[4] * wy + m[12]; |
| 1608 |
out.y = m[1] * wx + m[5] * wy + m[13]; |
| 1609 |
|
| 1610 |
out.u = ax * ctx.ff.texRect[0] + ctx.ff.texRect[1]; |
| 1611 |
out.v = ay * ctx.ff.texRect[2] + ctx.ff.texRect[3]; |
| 1612 |
|
| 1613 |
out.r = ctx.ff.color[0]; |
| 1614 |
out.g = ctx.ff.color[1]; |
| 1615 |
out.b = ctx.ff.color[2]; |
| 1616 |
out.a = ctx.ff.color[3]; |
| 1617 |
} else { |
| 1618 |
// General path: interleaved clip-space vertices [x, y, u, v]. The device pre-transforms the |
| 1619 |
// positions (no MVP is applied here), matching the source's attribute-fed general path. |
| 1620 |
std::int32_t stride = ctx.vertexStride; |
| 1621 |
if DEATH_UNLIKELY(stride <= 0) stride = 4 * static_cast<std::int32_t>(sizeof(float)); |
| 1622 |
const std::uint8_t* base = static_cast<const std::uint8_t*>(ctx.vertexData); |
| 1623 |
const float* f = reinterpret_cast<const float*>(base + static_cast<std::size_t>(index) * stride); |
| 1624 |
out.x = f[0]; |
| 1625 |
out.y = f[1]; |
| 1626 |
out.u = f[2]; |
| 1627 |
out.v = f[3]; |
| 1628 |
|
| 1629 |
out.r = ctx.ff.color[0]; |
| 1630 |
out.g = ctx.ff.color[1]; |
| 1631 |
out.b = ctx.ff.color[2]; |
| 1632 |
out.a = ctx.ff.color[3]; |
| 1633 |
} |
| 1634 |
|
| 1635 |
// Viewport transform: NDC [-1,+1] → screen pixel coordinates |
| 1636 |
out.x = (out.x + 1.0f) * 0.5f * static_cast<float>(g_state.viewportW) + static_cast<float>(g_state.viewportX); |
| 1637 |
out.y = (1.0f - out.y) * 0.5f * static_cast<float>(g_state.viewportH) + static_cast<float>(g_state.viewportY); |
| 1638 |
|
| 1639 |
// Snap to pixel grid when within epsilon of an integer. |
| 1640 |
// Eliminates 1-scanline flicker caused by float precision drift in MVP calculation. |
| 1641 |
constexpr float SnapEps = 1.0f / 128.0f; |
| 1642 |
float rxSnap = std::round(out.x); |
| 1643 |
float rySnap = std::round(out.y); |
| 1644 |
if (std::fabs(out.x - rxSnap) < SnapEps) out.x = rxSnap; |
| 1645 |
if (std::fabs(out.y - rySnap) < SnapEps) out.y = rySnap; |
| 1646 |
|
| 1647 |
return out; |
| 1648 |
} |
| 1649 |
|
| 1650 |
// ===================================================================== |
| 1651 |
// Axis-aligned quad rasterizer (fast path for non-rotated sprites) |
| 1652 |
// Supports all SamplerWrapping modes and SIMD blending. |
| 1653 |
// ===================================================================== |
| 1654 |
void DrawAxisAlignedQuad(const DrawContext& ctx, Vertex2D v0, Vertex2D v1, Vertex2D v2, Vertex2D v3) |
| 1655 |
{ |
| 1656 |
if DEATH_UNLIKELY(g_state.colorBuffer == nullptr) return; |
| 1657 |
|
| 1658 |
// Screen-space bounding box from all four vertices |
| 1659 |
const float fxMin = std::min({v0.x, v1.x, v2.x, v3.x}); |
| 1660 |
const float fxMax = std::max({v0.x, v1.x, v2.x, v3.x}); |
| 1661 |
const float fyMin = std::min({v0.y, v1.y, v2.y, v3.y}); |
| 1662 |
const float fyMax = std::max({v0.y, v1.y, v2.y, v3.y}); |
| 1663 |
|
| 1664 |
const float fullW = fxMax - fxMin; |
| 1665 |
const float fullH = fyMax - fyMin; |
| 1666 |
if DEATH_UNLIKELY(fullW < 0.5f || fullH < 0.5f) { |
| 1667 |
return; |
| 1668 |
} |
| 1669 |
|
| 1670 |
// UV corners |
| 1671 |
const float uLeft = (v2.x <= v0.x) ? v2.u : v0.u; |
| 1672 |
const float uRight = (v2.x <= v0.x) ? v0.u : v2.u; |
| 1673 |
const float vTop = (v0.y <= v1.y) ? v0.v : v1.v; |
| 1674 |
const float vBot = (v0.y <= v1.y) ? v1.v : v0.v; |
| 1675 |
|
| 1676 |
// Pixel bbox clamped to buffer |
| 1677 |
std::int32_t xMin = std::max(0, static_cast<std::int32_t>(fxMin)); |
| 1678 |
std::int32_t xMax = std::min(g_state.bufferWidth - 1, static_cast<std::int32_t>(fxMax - 0.5f)); |
| 1679 |
std::int32_t yMin = std::max(0, static_cast<std::int32_t>(fyMin)); |
| 1680 |
std::int32_t yMax = std::min(g_state.bufferHeight - 1, static_cast<std::int32_t>(fyMax - 0.5f)); |
| 1681 |
|
| 1682 |
// Scissor pre-clip (Y always flipped — scissor is in bottom-up window coordinates) |
| 1683 |
if DEATH_UNLIKELY(g_state.scissorEnabled) { |
| 1684 |
xMin = std::max(xMin, g_state.scissorX); |
| 1685 |
xMax = std::min(xMax, g_state.scissorX + g_state.scissorW - 1); |
| 1686 |
const std::int32_t pyMin = g_state.bufferHeight - g_state.scissorY - g_state.scissorH; |
| 1687 |
const std::int32_t pyMax = g_state.bufferHeight - 1 - g_state.scissorY; |
| 1688 |
yMin = std::max(yMin, pyMin); |
| 1689 |
yMax = std::min(yMax, pyMax); |
| 1690 |
} |
| 1691 |
if DEATH_UNLIKELY(xMin > xMax || yMin > yMax) { |
| 1692 |
return; |
| 1693 |
} |
| 1694 |
|
| 1695 |
// Texture info |
| 1696 |
const SwTexture* tex = (ctx.ff.hasTexture && ctx.ff.textureUnit < static_cast<std::int32_t>(MaxTextureUnits) |
| 1697 |
? ctx.textures[ctx.ff.textureUnit] : nullptr); |
| 1698 |
const std::uint8_t* texPixels = (tex != nullptr ? tex->GetPixels(0) : nullptr); |
| 1699 |
const std::int32_t texW = (tex != nullptr ? tex->GetWidth() : 0); |
| 1700 |
const std::int32_t texH = (tex != nullptr ? tex->GetHeight() : 0); |
| 1701 |
const std::int32_t texBpp = (tex != nullptr ? tex->GetBytesPerPixel() : 4); |
| 1702 |
const SamplerWrapping wrapS = (tex != nullptr ? tex->GetWrapS() : SamplerWrapping::ClampToEdge); |
| 1703 |
const SamplerWrapping wrapT = (tex != nullptr ? tex->GetWrapT() : SamplerWrapping::ClampToEdge); |
| 1704 |
const bool useLinear = (tex != nullptr && tex->GetMagFilter() == SamplerFilter::Linear && texW > 1 && texH > 1); |
| 1705 |
|
| 1706 |
// Tint color as [0..255] |
| 1707 |
const std::int32_t tR = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[0]) * 255.0f + 0.5f); |
| 1708 |
const std::int32_t tG = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[1]) * 255.0f + 0.5f); |
| 1709 |
const std::int32_t tB = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[2]) * 255.0f + 0.5f); |
| 1710 |
const std::int32_t tA = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[3]) * 255.0f + 0.5f); |
| 1711 |
const bool whiteTint = (tR >= 255 && tG >= 255 && tB >= 255 && tA >= 255); |
| 1712 |
|
| 1713 |
const bool useBlend = g_state.blendingEnabled; |
| 1714 |
const bool useFastBlend = (useBlend && |
| 1715 |
g_state.blendSrc == SwBlendFactor::SrcAlpha && |
| 1716 |
g_state.blendDst == SwBlendFactor::OneMinusSrcAlpha); |
| 1717 |
|
| 1718 |
// 16.16 fixed-point UV steps |
| 1719 |
const std::int32_t texWFix = texW << 16; |
| 1720 |
const std::int32_t texHFix = texH << 16; |
| 1721 |
const std::int32_t dtxFix = (texPixels != nullptr ? static_cast<std::int32_t>((uRight - uLeft) * texW * 65536.0f / fullW) : 0); |
| 1722 |
const std::int32_t dtyFix = (texPixels != nullptr ? static_cast<std::int32_t>((vBot - vTop) * texH * 65536.0f / fullH) : 0); |
| 1723 |
|
| 1724 |
const std::int32_t txBase = (texPixels != nullptr ? static_cast<std::int32_t>((uLeft + (xMin + 0.5f - fxMin) * (uRight - uLeft) / fullW) * texW * 65536.0f) : 0); |
| 1725 |
std::int32_t tyFix = (texPixels != nullptr ? static_cast<std::int32_t>((vTop + (yMin + 0.5f - fyMin) * (vBot - vTop) / fullH) * texH * 65536.0f) : 0); |
| 1726 |
|
| 1727 |
const bool useRepeatS = (wrapS == SamplerWrapping::Repeat); |
| 1728 |
const bool useRepeatT = (wrapT == SamplerWrapping::Repeat); |
| 1729 |
const bool useClampS = (wrapS == SamplerWrapping::ClampToEdge); |
| 1730 |
// Check if all UVs are safely within texture bounds (skip per-pixel wrapping) |
| 1731 |
const bool uvSafeX = (useClampS && txBase >= 0 && (txBase + dtxFix * (xMax - xMin)) >= 0 && |
| 1732 |
(txBase >> 16) < texW && ((txBase + dtxFix * (xMax - xMin)) >> 16) < texW); |
| 1733 |
|
| 1734 |
const std::int32_t scanWidth = xMax - xMin + 1; |
| 1735 |
|
| 1736 |
// Scanline staging buffer for SIMD blending or direct copy (static, main-thread-only) |
| 1737 |
std::uint8_t* const scanBuf = g_scanBuf; |
| 1738 |
const bool useScanBuf = ((useFastBlend || !useBlend) && scanWidth <= MaxScanBuf && texPixels != nullptr); |
| 1739 |
|
| 1740 |
for (std::int32_t py = yMin; py <= yMax; py++, tyFix += dtyFix) { |
| 1741 |
// Y is flipped when the destination is a render-target texture (stored bottom-up) |
| 1742 |
const std::int32_t storeY = (g_state.isFboTarget ? (g_state.bufferHeight - 1 - py) : py); |
| 1743 |
std::uint8_t* dstRow; |
| 1744 |
#if defined(RHI_USE_FB16) |
| 1745 |
std::uint8_t* fbRow16 = nullptr; |
| 1746 |
if (g_state.is16Bit) { |
| 1747 |
// Stage the touched 565 span as RGBA8; stored back at the end of the row (see SwRaster.h) |
| 1748 |
fbRow16 = g_state.colorBuffer + (storeY * g_state.bufferWidth + xMin) * 2; |
| 1749 |
SwLoadFbSpan565(g_fbRowStage, fbRow16, scanWidth); |
| 1750 |
dstRow = g_fbRowStage; |
| 1751 |
} else |
| 1752 |
#endif |
| 1753 |
{ |
| 1754 |
dstRow = (g_state.colorBuffer + (storeY * g_state.bufferWidth + xMin) * 4); |
| 1755 |
} |
| 1756 |
|
| 1757 |
// Get source Y with wrapping |
| 1758 |
std::int32_t srcY; |
| 1759 |
if DEATH_LIKELY(texPixels != nullptr) { |
| 1760 |
if (useRepeatT) { |
| 1761 |
srcY = WrapTexelFix(tyFix, texH, SamplerWrapping::Repeat); |
| 1762 |
} else { |
| 1763 |
srcY = WrapTexelFix(tyFix, texH, wrapT); |
| 1764 |
} |
| 1765 |
} else { |
| 1766 |
srcY = 0; |
| 1767 |
} |
| 1768 |
const std::uint8_t* texRow = (texPixels != nullptr ? texPixels + static_cast<std::size_t>(srcY) * texW * texBpp : nullptr); |
| 1769 |
|
| 1770 |
if DEATH_LIKELY(useScanBuf) { |
| 1771 |
// === Scanline buffer path: gather raw texels → callback/tint → SIMD blend === |
| 1772 |
std::int32_t txFix = txBase; |
| 1773 |
|
| 1774 |
if (useRepeatS && texPixels != nullptr && !useLinear) { |
| 1775 |
txFix = NormalizeRepeatFix(txFix, texWFix); |
| 1776 |
} |
| 1777 |
|
| 1778 |
// Phase 1: gather RAW texels into scanBuf (no tint applied; narrow native stores are |
| 1779 |
// expanded to the 4-byte working form here, so phases 2/3 see the same bytes as ever) |
| 1780 |
if (useLinear && texPixels != nullptr) { |
| 1781 |
const BilinearRowCtx brow = PrepareBilinearRow(texPixels, texW, texH, texBpp, tyFix, wrapT); |
| 1782 |
for (std::int32_t i = 0; i < scanWidth; ++i) { |
| 1783 |
SampleBilinearFixRow(brow, texW, texBpp, txFix, wrapS, &scanBuf[i * 4]); |
| 1784 |
txFix += dtxFix; |
| 1785 |
} |
| 1786 |
} else if DEATH_LIKELY(texRow != nullptr) { |
| 1787 |
if (uvSafeX && dtxFix == 65536) { |
| 1788 |
// 1:1 texel mapping - direct memcpy (4-byte stores) or a SIMD expand run |
| 1789 |
const std::int32_t srcX = txFix >> 16; |
| 1790 |
SwExpandTexelRun(scanBuf, &texRow[srcX * texBpp], scanWidth, texBpp); |
| 1791 |
txFix += dtxFix * scanWidth; |
| 1792 |
} else if (uvSafeX) { |
| 1793 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 1794 |
const std::int32_t srcX = txFix >> 16; |
| 1795 |
SwExpandTexel(&scanBuf[i * 4], &texRow[srcX * texBpp], texBpp); |
| 1796 |
txFix += dtxFix; |
| 1797 |
} |
| 1798 |
} else if (useRepeatS) { |
| 1799 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 1800 |
const std::int32_t srcX = txFix >> 16; |
| 1801 |
SwExpandTexel(&scanBuf[i * 4], &texRow[srcX * texBpp], texBpp); |
| 1802 |
txFix = AdvanceRepeatFix(txFix, dtxFix, texWFix); |
| 1803 |
} |
| 1804 |
} else { |
| 1805 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 1806 |
const std::int32_t srcX = WrapTexelFix(txFix, texW, wrapS); |
| 1807 |
SwExpandTexel(&scanBuf[i * 4], &texRow[srcX * texBpp], texBpp); |
| 1808 |
txFix += dtxFix; |
| 1809 |
} |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
// Phase 2: apply fragment shader callback or vertex-color tint |
| 1814 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 1815 |
FragmentShaderInput fsInput; |
| 1816 |
fsInput.v = tyFix / 65536.0f / static_cast<float>(texH > 0 ? texH : 1); |
| 1817 |
fsInput.texWidth = texW; |
| 1818 |
fsInput.texHeight = texH; |
| 1819 |
fsInput.textures = ctx.textures; |
| 1820 |
fsInput.color = ctx.ff.color; |
| 1821 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 1822 |
const float invTexW = 1.0f / static_cast<float>(texW > 0 ? texW : 1); |
| 1823 |
std::int32_t txFixShader = txBase; |
| 1824 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 1825 |
fsInput.rgba = &scanBuf[i * 4]; |
| 1826 |
fsInput.u = txFixShader / 65536.0f * invTexW; |
| 1827 |
fsInput.x = xMin + i; |
| 1828 |
fsInput.y = py; |
| 1829 |
ctx.fragmentShader(fsInput); |
| 1830 |
txFixShader += dtxFix; |
| 1831 |
} |
| 1832 |
} else if DEATH_UNLIKELY(!whiteTint) { |
| 1833 |
tintScanline(scanBuf, scanWidth, tR, tG, tB, tA); |
| 1834 |
} |
| 1835 |
|
| 1836 |
// Phase 3: blend or direct copy to framebuffer |
| 1837 |
if (useFastBlend) { |
| 1838 |
blendScanlineSrcAlpha(dstRow, scanBuf, scanWidth); |
| 1839 |
} else { |
| 1840 |
// No blending - direct copy |
| 1841 |
std::memcpy(dstRow, scanBuf, static_cast<std::size_t>(scanWidth) * 4); |
| 1842 |
} |
| 1843 |
} else { |
| 1844 |
// === Direct per-pixel path (no SIMD, supports all blend modes) === |
| 1845 |
std::int32_t txFix = txBase; |
| 1846 |
if (useRepeatS && texPixels != nullptr && !useLinear) { |
| 1847 |
txFix = NormalizeRepeatFix(txFix, texWFix); |
| 1848 |
} |
| 1849 |
|
| 1850 |
for (std::int32_t px = xMin; px <= xMax; px++, dstRow += 4) { |
| 1851 |
std::int32_t sR, sG, sB, sA; |
| 1852 |
if (useLinear && texPixels != nullptr) { |
| 1853 |
std::uint8_t raw[4]; |
| 1854 |
SampleBilinearFix(texPixels, texW, texH, texBpp, txFix, tyFix, wrapS, wrapT, raw); |
| 1855 |
txFix += dtxFix; |
| 1856 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 1857 |
} else if (texRow != nullptr) { |
| 1858 |
std::int32_t srcX; |
| 1859 |
if (useRepeatS) { |
| 1860 |
srcX = txFix >> 16; |
| 1861 |
txFix = AdvanceRepeatFix(txFix, dtxFix, texWFix); |
| 1862 |
} else if (uvSafeX) { |
| 1863 |
srcX = txFix >> 16; |
| 1864 |
txFix += dtxFix; |
| 1865 |
} else { |
| 1866 |
srcX = WrapTexelFix(txFix, texW, wrapS); |
| 1867 |
txFix += dtxFix; |
| 1868 |
} |
| 1869 |
std::uint8_t raw[4]; |
| 1870 |
SwExpandTexel(raw, &texRow[srcX * texBpp], texBpp); |
| 1871 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 1872 |
} else { |
| 1873 |
sR = 255; sG = 255; sB = 255; sA = 255; |
| 1874 |
txFix += dtxFix; |
| 1875 |
} |
| 1876 |
|
| 1877 |
// Apply fragment shader callback or vertex-color tint |
| 1878 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 1879 |
std::uint8_t px4[4] = { |
| 1880 |
static_cast<std::uint8_t>(sR), static_cast<std::uint8_t>(sG), |
| 1881 |
static_cast<std::uint8_t>(sB), static_cast<std::uint8_t>(sA) |
| 1882 |
}; |
| 1883 |
FragmentShaderInput fsInput; |
| 1884 |
fsInput.rgba = px4; |
| 1885 |
fsInput.u = (txFix - dtxFix) / 65536.0f / static_cast<float>(texW > 0 ? texW : 1); |
| 1886 |
fsInput.v = tyFix / 65536.0f / static_cast<float>(texH > 0 ? texH : 1); |
| 1887 |
fsInput.x = px; |
| 1888 |
fsInput.y = py; |
| 1889 |
fsInput.texWidth = texW; |
| 1890 |
fsInput.texHeight = texH; |
| 1891 |
fsInput.textures = ctx.textures; |
| 1892 |
fsInput.color = ctx.ff.color; |
| 1893 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 1894 |
ctx.fragmentShader(fsInput); |
| 1895 |
sR = px4[0]; sG = px4[1]; sB = px4[2]; sA = px4[3]; |
| 1896 |
} else if DEATH_UNLIKELY(!whiteTint) { |
| 1897 |
sR = (sR * tR) >> 8; |
| 1898 |
sG = (sG * tG) >> 8; |
| 1899 |
sB = (sB * tB) >> 8; |
| 1900 |
sA = (sA * tA) >> 8; |
| 1901 |
} |
| 1902 |
|
| 1903 |
if (useBlend) { |
| 1904 |
if (sA == 0) { |
| 1905 |
continue; |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (useFastBlend) { |
| 1909 |
if (sA >= 255) { |
| 1910 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 1911 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 1912 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 1913 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 1914 |
} else { |
| 1915 |
const std::int32_t inv = 255 - sA; |
| 1916 |
dstRow[0] = static_cast<std::uint8_t>((sR * sA + dstRow[0] * inv) >> 8); |
| 1917 |
dstRow[1] = static_cast<std::uint8_t>((sG * sA + dstRow[1] * inv) >> 8); |
| 1918 |
dstRow[2] = static_cast<std::uint8_t>((sB * sA + dstRow[2] * inv) >> 8); |
| 1919 |
dstRow[3] = static_cast<std::uint8_t>((sA * 255 + dstRow[3] * inv) >> 8); |
| 1920 |
} |
| 1921 |
} else { |
| 1922 |
BlendPixelGeneric(dstRow, sR, sG, sB, sA, g_state.blendSrc, g_state.blendDst); |
| 1923 |
} |
| 1924 |
} else { |
| 1925 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 1926 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 1927 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 1928 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 1929 |
} |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
#if defined(RHI_USE_FB16) |
| 1934 |
if (fbRow16 != nullptr) { |
| 1935 |
SwStoreFbSpan565(fbRow16, g_fbRowStage, scanWidth); |
| 1936 |
} |
| 1937 |
#endif |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
// ===================================================================== |
| 1942 |
// Triangle rasterizer - incremental edge functions, pre-clipped |
| 1943 |
// Supports all SamplerWrapping modes and blend modes. |
| 1944 |
// ===================================================================== |
| 1945 |
void RasterizeTriangle(const DrawContext& ctx, Vertex2D v0, Vertex2D v1, Vertex2D v2) |
| 1946 |
{ |
| 1947 |
// Bounding box in screen space |
| 1948 |
std::int32_t minX = std::max(0, static_cast<std::int32_t>(std::min({v0.x, v1.x, v2.x}))); |
| 1949 |
std::int32_t maxX = std::min(g_state.bufferWidth - 1, static_cast<std::int32_t>(std::max({v0.x, v1.x, v2.x}))); |
| 1950 |
std::int32_t minY = std::max(0, static_cast<std::int32_t>(std::min({v0.y, v1.y, v2.y}))); |
| 1951 |
std::int32_t maxY = std::min(g_state.bufferHeight - 1, static_cast<std::int32_t>(std::max({v0.y, v1.y, v2.y}))); |
| 1952 |
|
| 1953 |
// Pre-clip to scissor (Y always flipped — scissor is in bottom-up window coordinates) |
| 1954 |
if DEATH_UNLIKELY(g_state.scissorEnabled) { |
| 1955 |
const std::int32_t pyMin = g_state.bufferHeight - g_state.scissorY - g_state.scissorH; |
| 1956 |
const std::int32_t pyMax = g_state.bufferHeight - 1 - g_state.scissorY; |
| 1957 |
minX = std::max(minX, g_state.scissorX); |
| 1958 |
maxX = std::min(maxX, g_state.scissorX + g_state.scissorW - 1); |
| 1959 |
minY = std::max(minY, pyMin); |
| 1960 |
maxY = std::min(maxY, pyMax); |
| 1961 |
} |
| 1962 |
if DEATH_UNLIKELY(minX > maxX || minY > maxY) { |
| 1963 |
return; |
| 1964 |
} |
| 1965 |
|
| 1966 |
// Edge function: E(A→B, P) = (B.x-A.x)*(P.y-A.y) - (B.y-A.y)*(P.x-A.x) |
| 1967 |
// Incremental: dE/dx = -(B.y-A.y), dE/dy = (B.x-A.x) |
| 1968 |
float w0_dx = v1.y - v2.y, w0_dy = v2.x - v1.x; |
| 1969 |
float w1_dx = v2.y - v0.y, w1_dy = v0.x - v2.x; |
| 1970 |
float w2_dx = v0.y - v1.y, w2_dy = v1.x - v0.x; |
| 1971 |
|
| 1972 |
float area = (v2.x - v1.x) * (v0.y - v1.y) - (v2.y - v1.y) * (v0.x - v1.x); |
| 1973 |
if (std::fabs(area) < 1e-6f) return; // degenerate |
| 1974 |
// Orientation-normalize so the interior is POSITIVE for every winding, then decide the exact |
| 1975 |
// w == 0 edge pixels with a top-left fill rule (include when the normalized edge gradient |
| 1976 |
// points right, or straight down for horizontal edges). Two strip triangles sharing a |
| 1977 |
// diagonal then cover each of its pixels EXACTLY once (kept identical in the tile |
| 1978 |
// rasterizer's rasterTri, see SwTileRasterizer.cpp). |
| 1979 |
const float orient = (area > 0.0f ? 1.0f : -1.0f); |
| 1980 |
area *= orient; |
| 1981 |
w0_dx *= orient; w0_dy *= orient; |
| 1982 |
w1_dx *= orient; w1_dy *= orient; |
| 1983 |
w2_dx *= orient; w2_dy *= orient; |
| 1984 |
const float invArea = 1.0f / area; |
| 1985 |
const bool incl0 = (w0_dx > 0.0f || (w0_dx == 0.0f && w0_dy > 0.0f)); |
| 1986 |
const bool incl1 = (w1_dx > 0.0f || (w1_dx == 0.0f && w1_dy > 0.0f)); |
| 1987 |
const bool incl2 = (w2_dx > 0.0f || (w2_dx == 0.0f && w2_dy > 0.0f)); |
| 1988 |
|
| 1989 |
// Edge functions at pixel center (minX+0.5, minY+0.5) |
| 1990 |
const float px0 = minX + 0.5f, py0 = minY + 0.5f; |
| 1991 |
float w0_row = orient * ((v2.x - v1.x) * (py0 - v1.y) - (v2.y - v1.y) * (px0 - v1.x)); |
| 1992 |
float w1_row = orient * ((v0.x - v2.x) * (py0 - v2.y) - (v0.y - v2.y) * (px0 - v2.x)); |
| 1993 |
float w2_row = orient * ((v1.x - v0.x) * (py0 - v0.y) - (v1.y - v0.y) * (px0 - v0.x)); |
| 1994 |
|
| 1995 |
const SwTexture* tex = (ctx.ff.hasTexture && ctx.ff.textureUnit < static_cast<std::int32_t>(MaxTextureUnits) |
| 1996 |
? ctx.textures[ctx.ff.textureUnit] : nullptr); |
| 1997 |
const std::uint8_t* texPixels = (tex != nullptr ? tex->GetPixels(0) : nullptr); |
| 1998 |
const std::int32_t texW = (tex != nullptr ? tex->GetWidth() : 0); |
| 1999 |
const std::int32_t texH = (tex != nullptr ? tex->GetHeight() : 0); |
| 2000 |
const std::int32_t texBpp = (tex != nullptr ? tex->GetBytesPerPixel() : 4); |
| 2001 |
const SamplerWrapping wrapS = (tex != nullptr ? tex->GetWrapS() : SamplerWrapping::ClampToEdge); |
| 2002 |
const SamplerWrapping wrapT = (tex != nullptr ? tex->GetWrapT() : SamplerWrapping::ClampToEdge); |
| 2003 |
const bool useLinear = (tex != nullptr && tex->GetMagFilter() == SamplerFilter::Linear && texW > 1 && texH > 1); |
| 2004 |
|
| 2005 |
const std::int32_t tR = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[0]) * 255.0f + 0.5f); |
| 2006 |
const std::int32_t tG = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[1]) * 255.0f + 0.5f); |
| 2007 |
const std::int32_t tB = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[2]) * 255.0f + 0.5f); |
| 2008 |
const std::int32_t tA = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[3]) * 255.0f + 0.5f); |
| 2009 |
|
| 2010 |
const bool useBlend = g_state.blendingEnabled; |
| 2011 |
const bool useFastBlend = (useBlend && |
| 2012 |
g_state.blendSrc == SwBlendFactor::SrcAlpha && |
| 2013 |
g_state.blendDst == SwBlendFactor::OneMinusSrcAlpha); |
| 2014 |
|
| 2015 |
for (std::int32_t py = minY; py <= maxY; py++) { |
| 2016 |
float w0 = w0_row, w1 = w1_row, w2 = w2_row; |
| 2017 |
// Y is flipped when the destination is a render-target texture (stored bottom-up) |
| 2018 |
const std::int32_t storeY = (g_state.isFboTarget ? (g_state.bufferHeight - 1 - py) : py); |
| 2019 |
std::uint8_t* dstRow; |
| 2020 |
#if defined(RHI_USE_FB16) |
| 2021 |
std::uint8_t* fbRow16 = nullptr; |
| 2022 |
if (g_state.is16Bit) { |
| 2023 |
// Stage the bounding-box 565 span as RGBA8; the lossless round trip leaves untouched |
| 2024 |
// (coverage-rejected) pixels unchanged when stored back at the end of the row |
| 2025 |
fbRow16 = g_state.colorBuffer + (storeY * g_state.bufferWidth + minX) * 2; |
| 2026 |
SwLoadFbSpan565(g_fbRowStage, fbRow16, maxX - minX + 1); |
| 2027 |
dstRow = g_fbRowStage; |
| 2028 |
} else |
| 2029 |
#endif |
| 2030 |
{ |
| 2031 |
dstRow = (g_state.colorBuffer + (storeY * g_state.bufferWidth + minX) * 4); |
| 2032 |
} |
| 2033 |
|
| 2034 |
for (std::int32_t px = minX; px <= maxX; ++px, dstRow += 4, w0 += w0_dx, w1 += w1_dx, w2 += w2_dx) { |
| 2035 |
if ((w0 < 0.0f || (w0 == 0.0f && !incl0)) || |
| 2036 |
(w1 < 0.0f || (w1 == 0.0f && !incl1)) || |
| 2037 |
(w2 < 0.0f || (w2 == 0.0f && !incl2))) |
| 2038 |
continue; |
| 2039 |
|
| 2040 |
const float b0 = w0 * invArea; |
| 2041 |
const float b1 = w1 * invArea; |
| 2042 |
const float b2 = w2 * invArea; |
| 2043 |
|
| 2044 |
float u = b0 * v0.u + b1 * v1.u + b2 * v2.u; |
| 2045 |
float vv = b0 * v0.v + b1 * v1.v + b2 * v2.v; |
| 2046 |
|
| 2047 |
std::int32_t sR, sG, sB, sA; |
| 2048 |
if (texPixels != nullptr && useLinear) { |
| 2049 |
u = WrapUV(u, wrapS); |
| 2050 |
vv = WrapUV(vv, wrapT); |
| 2051 |
std::uint8_t raw[4]; |
| 2052 |
SampleBilinearFloat(texPixels, texW, texH, texBpp, u, vv, wrapS, wrapT, raw); |
| 2053 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2054 |
} else if (texPixels != nullptr) { |
| 2055 |
u = WrapUV(u, wrapS); |
| 2056 |
vv = WrapUV(vv, wrapT); |
| 2057 |
const std::int32_t srcX = std::max(0, std::min(texW - 1, static_cast<std::int32_t>(u * (texW - 1) + 0.5f))); |
| 2058 |
const std::int32_t srcY = std::max(0, std::min(texH - 1, static_cast<std::int32_t>(vv * (texH - 1) + 0.5f))); |
| 2059 |
std::uint8_t raw[4]; |
| 2060 |
SwExpandTexel(raw, texPixels + (srcY * texW + srcX) * texBpp, texBpp); |
| 2061 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2062 |
} else { |
| 2063 |
sR = 255; sG = 255; sB = 255; sA = 255; |
| 2064 |
} |
| 2065 |
|
| 2066 |
// Apply fragment shader callback or vertex-color tint |
| 2067 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 2068 |
std::uint8_t px4[4] = { |
| 2069 |
static_cast<std::uint8_t>(sR), static_cast<std::uint8_t>(sG), |
| 2070 |
static_cast<std::uint8_t>(sB), static_cast<std::uint8_t>(sA) |
| 2071 |
}; |
| 2072 |
FragmentShaderInput fsInput; |
| 2073 |
fsInput.rgba = px4; |
| 2074 |
fsInput.u = u; |
| 2075 |
fsInput.v = vv; |
| 2076 |
fsInput.x = px; |
| 2077 |
fsInput.y = py; |
| 2078 |
fsInput.texWidth = texW; |
| 2079 |
fsInput.texHeight = texH; |
| 2080 |
fsInput.textures = ctx.textures; |
| 2081 |
fsInput.color = ctx.ff.color; |
| 2082 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 2083 |
ctx.fragmentShader(fsInput); |
| 2084 |
sR = px4[0]; sG = px4[1]; sB = px4[2]; sA = px4[3]; |
| 2085 |
} else { |
| 2086 |
sR = (sR * tR) >> 8; |
| 2087 |
sG = (sG * tG) >> 8; |
| 2088 |
sB = (sB * tB) >> 8; |
| 2089 |
sA = (sA * tA) >> 8; |
| 2090 |
} |
| 2091 |
|
| 2092 |
if (useBlend) { |
| 2093 |
if (sA == 0) { |
| 2094 |
continue; |
| 2095 |
} |
| 2096 |
|
| 2097 |
if (useFastBlend) { |
| 2098 |
if (sA >= 255) { |
| 2099 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 2100 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 2101 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 2102 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 2103 |
} else { |
| 2104 |
const std::int32_t inv = 255 - sA; |
| 2105 |
dstRow[0] = static_cast<std::uint8_t>((sR * sA + dstRow[0] * inv) >> 8); |
| 2106 |
dstRow[1] = static_cast<std::uint8_t>((sG * sA + dstRow[1] * inv) >> 8); |
| 2107 |
dstRow[2] = static_cast<std::uint8_t>((sB * sA + dstRow[2] * inv) >> 8); |
| 2108 |
dstRow[3] = static_cast<std::uint8_t>((sA * 255 + dstRow[3] * inv) >> 8); |
| 2109 |
} |
| 2110 |
} else { |
| 2111 |
BlendPixelGeneric(dstRow, sR, sG, sB, sA, g_state.blendSrc, g_state.blendDst); |
| 2112 |
} |
| 2113 |
} else { |
| 2114 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 2115 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 2116 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 2117 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 2118 |
} |
| 2119 |
} |
| 2120 |
|
| 2121 |
#if defined(RHI_USE_FB16) |
| 2122 |
if (fbRow16 != nullptr) { |
| 2123 |
SwStoreFbSpan565(fbRow16, g_fbRowStage, maxX - minX + 1); |
| 2124 |
} |
| 2125 |
#endif |
| 2126 |
w0_row += w0_dy; |
| 2127 |
w1_row += w1_dy; |
| 2128 |
w2_row += w2_dy; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
|
| 2132 |
// ===================================================================== |
| 2133 |
// Affine quad rasterizer - scanline-based for rotated/scaled quads. |
| 2134 |
// Rasterizes a convex quad defined by 4 vertices with UV interpolation |
| 2135 |
// per-scanline, enabling SIMD blending. Much faster than splitting into |
| 2136 |
// two triangles with per-pixel barycentric tests. |
| 2137 |
// ===================================================================== |
| 2138 |
void DrawAffineQuad(const DrawContext& ctx, Vertex2D v0, Vertex2D v1, Vertex2D v2, Vertex2D v3) |
| 2139 |
{ |
| 2140 |
if DEATH_UNLIKELY(g_state.colorBuffer == nullptr) return; |
| 2141 |
|
| 2142 |
// Reorder vertices as a quad: triangle strip (v0,v1,v2,v3) forms quad |
| 2143 |
// with edges v0-v1, v1-v3, v3-v2, v2-v0 (or equivalently, the two triangles |
| 2144 |
// share edge v1-v2, forming quad v0,v1,v3,v2 in CCW/CW order). |
| 2145 |
// Reorder to sorted array by Y for scanline processing. |
| 2146 |
struct QVert { float x, y, u, v; }; |
| 2147 |
QVert quad[4] = { |
| 2148 |
{ v0.x, v0.y, v0.u, v0.v }, |
| 2149 |
{ v1.x, v1.y, v1.u, v1.v }, |
| 2150 |
{ v2.x, v2.y, v2.u, v2.v }, |
| 2151 |
{ v3.x, v3.y, v3.u, v3.v } |
| 2152 |
}; |
| 2153 |
|
| 2154 |
// Sort by Y (insertion sort on 4 elements) |
| 2155 |
for (std::int32_t i = 1; i < 4; i++) { |
| 2156 |
QVert tmp = quad[i]; |
| 2157 |
std::int32_t j = i - 1; |
| 2158 |
while (j >= 0 && quad[j].y > tmp.y) { |
| 2159 |
quad[j + 1] = quad[j]; |
| 2160 |
j--; |
| 2161 |
} |
| 2162 |
quad[j + 1] = tmp; |
| 2163 |
} |
| 2164 |
|
| 2165 |
// Get bounding box |
| 2166 |
float fxMin = std::min({quad[0].x, quad[1].x, quad[2].x, quad[3].x}); |
| 2167 |
float fxMax = std::max({quad[0].x, quad[1].x, quad[2].x, quad[3].x}); |
| 2168 |
float fyMin = quad[0].y; |
| 2169 |
float fyMax = quad[3].y; |
| 2170 |
|
| 2171 |
std::int32_t yMin = std::max(0, static_cast<std::int32_t>(fyMin)); |
| 2172 |
std::int32_t yMax = std::min(g_state.bufferHeight - 1, static_cast<std::int32_t>(fyMax)); |
| 2173 |
std::int32_t xMinClamp = std::max(0, static_cast<std::int32_t>(fxMin)); |
| 2174 |
std::int32_t xMaxClamp = std::min(g_state.bufferWidth - 1, static_cast<std::int32_t>(fxMax)); |
| 2175 |
|
| 2176 |
// Scissor clamp (Y always flipped — scissor is in bottom-up window coordinates) |
| 2177 |
if DEATH_UNLIKELY(g_state.scissorEnabled) { |
| 2178 |
const std::int32_t pyMin = g_state.bufferHeight - g_state.scissorY - g_state.scissorH; |
| 2179 |
const std::int32_t pyMax = g_state.bufferHeight - 1 - g_state.scissorY; |
| 2180 |
xMinClamp = std::max(xMinClamp, g_state.scissorX); |
| 2181 |
xMaxClamp = std::min(xMaxClamp, g_state.scissorX + g_state.scissorW - 1); |
| 2182 |
yMin = std::max(yMin, pyMin); |
| 2183 |
yMax = std::min(yMax, pyMax); |
| 2184 |
} |
| 2185 |
if DEATH_UNLIKELY(yMin > yMax || xMinClamp > xMaxClamp) { |
| 2186 |
return; |
| 2187 |
} |
| 2188 |
|
| 2189 |
// Texture info |
| 2190 |
const SwTexture* tex = (ctx.ff.hasTexture && ctx.ff.textureUnit < static_cast<std::int32_t>(MaxTextureUnits) |
| 2191 |
? ctx.textures[ctx.ff.textureUnit] : nullptr); |
| 2192 |
const std::uint8_t* texPixels = (tex != nullptr ? tex->GetPixels(0) : nullptr); |
| 2193 |
const std::int32_t texW = (tex != nullptr ? tex->GetWidth() : 0); |
| 2194 |
const std::int32_t texH = (tex != nullptr ? tex->GetHeight() : 0); |
| 2195 |
const std::int32_t texBpp = (tex != nullptr ? tex->GetBytesPerPixel() : 4); |
| 2196 |
const SamplerWrapping wrapS = (tex != nullptr ? tex->GetWrapS() : SamplerWrapping::ClampToEdge); |
| 2197 |
const SamplerWrapping wrapT = (tex != nullptr ? tex->GetWrapT() : SamplerWrapping::ClampToEdge); |
| 2198 |
const bool useLinear = (tex != nullptr && tex->GetMagFilter() == SamplerFilter::Linear && texW > 1 && texH > 1); |
| 2199 |
|
| 2200 |
const std::int32_t tR = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[0]) * 255.0f + 0.5f); |
| 2201 |
const std::int32_t tG = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[1]) * 255.0f + 0.5f); |
| 2202 |
const std::int32_t tB = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[2]) * 255.0f + 0.5f); |
| 2203 |
const std::int32_t tA = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[3]) * 255.0f + 0.5f); |
| 2204 |
const bool whiteTint = (tR >= 255 && tG >= 255 && tB >= 255 && tA >= 255); |
| 2205 |
|
| 2206 |
const bool useBlend = g_state.blendingEnabled; |
| 2207 |
const bool useFastBlend = (useBlend && |
| 2208 |
g_state.blendSrc == SwBlendFactor::SrcAlpha && |
| 2209 |
g_state.blendDst == SwBlendFactor::OneMinusSrcAlpha); |
| 2210 |
|
| 2211 |
// Compute affine UV mapping from the first triangle (v0, v1, v2) in the original strip order |
| 2212 |
float e1x = v2.x - v0.x, e1y = v2.y - v0.y; |
| 2213 |
float e2x = v1.x - v0.x, e2y = v1.y - v0.y; |
| 2214 |
float det = e1x * e2y - e1y * e2x; |
| 2215 |
if DEATH_UNLIKELY(std::fabs(det) < 1e-6f) return; // Degenerate quad |
| 2216 |
|
| 2217 |
float invDet = 1.0f / det; |
| 2218 |
float du_s = v2.u - v0.u, du_t = v1.u - v0.u; |
| 2219 |
float dv_s = v2.v - v0.v, dv_t = v1.v - v0.v; |
| 2220 |
|
| 2221 |
// ds/dx = e2y * invDet, ds/dy = -e2x * invDet |
| 2222 |
// dt/dx = -e1y * invDet, dt/dy = e1x * invDet |
| 2223 |
float ds_dx = e2y * invDet, ds_dy = -e2x * invDet; |
| 2224 |
float dt_dx = -e1y * invDet, dt_dy = e1x * invDet; |
| 2225 |
|
| 2226 |
float dudx = du_s * ds_dx + du_t * dt_dx; |
| 2227 |
float dudy = du_s * ds_dy + du_t * dt_dy; |
| 2228 |
float dvdx = dv_s * ds_dx + dv_t * dt_dx; |
| 2229 |
float dvdy = dv_s * ds_dy + dv_t * dt_dy; |
| 2230 |
|
| 2231 |
// Scanline staging buffer for SIMD blending (static, main-thread-only) |
| 2232 |
std::uint8_t* const scanBuf = g_scanBuf; |
| 2233 |
|
| 2234 |
// Edge functions for triangle 1 (v0, v1, v2) |
| 2235 |
const float t1_w0_dx = v1.y - v2.y, t1_w0_dy = v2.x - v1.x; |
| 2236 |
const float t1_w1_dx = v2.y - v0.y, t1_w1_dy = v0.x - v2.x; |
| 2237 |
const float t1_w2_dx = v0.y - v1.y, t1_w2_dy = v1.x - v0.x; |
| 2238 |
const float area1 = (v2.x - v1.x) * (v0.y - v1.y) - (v2.y - v1.y) * (v0.x - v1.x); |
| 2239 |
|
| 2240 |
// Edge functions for triangle 2 (v2, v1, v3) |
| 2241 |
const float t2_w0_dx = v1.y - v3.y, t2_w0_dy = v3.x - v1.x; |
| 2242 |
const float t2_w1_dx = v3.y - v2.y, t2_w1_dy = v2.x - v3.x; |
| 2243 |
const float t2_w2_dx = v2.y - v1.y, t2_w2_dy = v1.x - v2.x; |
| 2244 |
const float area2 = (v3.x - v1.x) * (v2.y - v1.y) - (v3.y - v1.y) * (v2.x - v1.x); |
| 2245 |
|
| 2246 |
if DEATH_UNLIKELY(std::fabs(area1) < 1e-6f && std::fabs(area2) < 1e-6f) return; |
| 2247 |
const bool sign1 = (area1 > 0.0f); |
| 2248 |
const bool sign2 = (area2 > 0.0f); |
| 2249 |
|
| 2250 |
// 16.16 fixed-point UV steps for scanline |
| 2251 |
const std::int32_t dudxFix = (texPixels != nullptr ? static_cast<std::int32_t>(dudx * texW * 65536.0f) : 0); |
| 2252 |
const std::int32_t dvdxFix = (texPixels != nullptr ? static_cast<std::int32_t>(dvdx * texH * 65536.0f) : 0); |
| 2253 |
|
| 2254 |
for (std::int32_t py = yMin; py <= yMax; py++) { |
| 2255 |
const float pyCtr = py + 0.5f; |
| 2256 |
const std::int32_t storeY = (g_state.isFboTarget ? (g_state.bufferHeight - 1 - py) : py); |
| 2257 |
|
| 2258 |
// Initial edge function values at (xMinClamp+0.5, pyCtr) |
| 2259 |
float px0 = xMinClamp + 0.5f; |
| 2260 |
|
| 2261 |
float t1_w0 = (v2.x - v1.x) * (pyCtr - v1.y) - (v2.y - v1.y) * (px0 - v1.x); |
| 2262 |
float t1_w1 = (v0.x - v2.x) * (pyCtr - v2.y) - (v0.y - v2.y) * (px0 - v2.x); |
| 2263 |
float t1_w2 = (v1.x - v0.x) * (pyCtr - v0.y) - (v1.y - v0.y) * (px0 - v0.x); |
| 2264 |
|
| 2265 |
float t2_w0 = (v3.x - v1.x) * (pyCtr - v1.y) - (v3.y - v1.y) * (px0 - v1.x); |
| 2266 |
float t2_w1 = (v2.x - v3.x) * (pyCtr - v3.y) - (v2.y - v3.y) * (px0 - v3.x); |
| 2267 |
float t2_w2 = (v1.x - v2.x) * (pyCtr - v2.y) - (v1.y - v2.y) * (px0 - v2.x); |
| 2268 |
|
| 2269 |
// Find left edge: first pixel inside either triangle |
| 2270 |
std::int32_t xLeft = xMaxClamp + 1; |
| 2271 |
std::int32_t xRight = xMinClamp - 1; |
| 2272 |
|
| 2273 |
float w1_0 = t1_w0, w1_1 = t1_w1, w1_2 = t1_w2; |
| 2274 |
float w2_0 = t2_w0, w2_1 = t2_w1, w2_2 = t2_w2; |
| 2275 |
|
| 2276 |
for (std::int32_t px = xMinClamp; px <= xMaxClamp; px++) { |
| 2277 |
bool in1 = (std::fabs(area1) >= 1e-6f) && |
| 2278 |
((w1_0 >= 0.0f) == sign1) && ((w1_1 >= 0.0f) == sign1) && ((w1_2 >= 0.0f) == sign1); |
| 2279 |
bool in2 = (std::fabs(area2) >= 1e-6f) && |
| 2280 |
((w2_0 >= 0.0f) == sign2) && ((w2_1 >= 0.0f) == sign2) && ((w2_2 >= 0.0f) == sign2); |
| 2281 |
if (in1 || in2) { |
| 2282 |
xLeft = px; |
| 2283 |
break; |
| 2284 |
} |
| 2285 |
w1_0 += t1_w0_dx; w1_1 += t1_w1_dx; w1_2 += t1_w2_dx; |
| 2286 |
w2_0 += t2_w0_dx; w2_1 += t2_w1_dx; w2_2 += t2_w2_dx; |
| 2287 |
} |
| 2288 |
if (xLeft > xMaxClamp) continue; |
| 2289 |
|
| 2290 |
// Find right edge from the right |
| 2291 |
float rpx0 = xMaxClamp + 0.5f; |
| 2292 |
float rt1_w0 = (v2.x - v1.x) * (pyCtr - v1.y) - (v2.y - v1.y) * (rpx0 - v1.x); |
| 2293 |
float rt1_w1 = (v0.x - v2.x) * (pyCtr - v2.y) - (v0.y - v2.y) * (rpx0 - v2.x); |
| 2294 |
float rt1_w2 = (v1.x - v0.x) * (pyCtr - v0.y) - (v1.y - v0.y) * (rpx0 - v0.x); |
| 2295 |
float rt2_w0 = (v3.x - v1.x) * (pyCtr - v1.y) - (v3.y - v1.y) * (rpx0 - v1.x); |
| 2296 |
float rt2_w1 = (v2.x - v3.x) * (pyCtr - v3.y) - (v2.y - v3.y) * (rpx0 - v3.x); |
| 2297 |
float rt2_w2 = (v1.x - v2.x) * (pyCtr - v2.y) - (v1.y - v2.y) * (rpx0 - v2.x); |
| 2298 |
|
| 2299 |
for (std::int32_t px = xMaxClamp; px >= xLeft; px--) { |
| 2300 |
bool in1 = (std::fabs(area1) >= 1e-6f) && |
| 2301 |
((rt1_w0 >= 0.0f) == sign1) && ((rt1_w1 >= 0.0f) == sign1) && ((rt1_w2 >= 0.0f) == sign1); |
| 2302 |
bool in2 = (std::fabs(area2) >= 1e-6f) && |
| 2303 |
((rt2_w0 >= 0.0f) == sign2) && ((rt2_w1 >= 0.0f) == sign2) && ((rt2_w2 >= 0.0f) == sign2); |
| 2304 |
if (in1 || in2) { |
| 2305 |
xRight = px; |
| 2306 |
break; |
| 2307 |
} |
| 2308 |
rt1_w0 -= t1_w0_dx; rt1_w1 -= t1_w1_dx; rt1_w2 -= t1_w2_dx; |
| 2309 |
rt2_w0 -= t2_w0_dx; rt2_w1 -= t2_w1_dx; rt2_w2 -= t2_w2_dx; |
| 2310 |
} |
| 2311 |
if (xRight < xLeft) continue; |
| 2312 |
|
| 2313 |
std::int32_t scanWidth = xRight - xLeft + 1; |
| 2314 |
|
| 2315 |
// Compute UV at the left edge using the affine mapping |
| 2316 |
float leftCtrX = xLeft + 0.5f; |
| 2317 |
float relX = leftCtrX - v0.x; |
| 2318 |
float relY = pyCtr - v0.y; |
| 2319 |
float uStart = v0.u + dudx * relX + dudy * relY; |
| 2320 |
float vStart = v0.v + dvdx * relX + dvdy * relY; |
| 2321 |
|
| 2322 |
// Use scanline buffer when possible (fits + common blend mode) |
| 2323 |
bool useScanBuf = ((useFastBlend || !useBlend) && scanWidth <= MaxScanBuf && texPixels != nullptr); |
| 2324 |
|
| 2325 |
if (useScanBuf) { |
| 2326 |
// Fixed-point UV interpolation along scanline |
| 2327 |
std::int32_t uFix = static_cast<std::int32_t>(uStart * texW * 65536.0f); |
| 2328 |
std::int32_t vFix = static_cast<std::int32_t>(vStart * texH * 65536.0f); |
| 2329 |
|
| 2330 |
// Gather texels into scanline buffer |
| 2331 |
if (useLinear) { |
| 2332 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 2333 |
SampleBilinearFix(texPixels, texW, texH, texBpp, uFix, vFix, wrapS, wrapT, &scanBuf[i * 4]); |
| 2334 |
uFix += dudxFix; |
| 2335 |
vFix += dvdxFix; |
| 2336 |
} |
| 2337 |
} else { |
| 2338 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 2339 |
std::int32_t srcX = WrapTexelFix(uFix, texW, wrapS); |
| 2340 |
std::int32_t srcY = WrapTexelFix(vFix, texH, wrapT); |
| 2341 |
SwExpandTexel(&scanBuf[i * 4], texPixels + (static_cast<std::size_t>(srcY) * texW + srcX) * texBpp, texBpp); |
| 2342 |
uFix += dudxFix; |
| 2343 |
vFix += dvdxFix; |
| 2344 |
} |
| 2345 |
} |
| 2346 |
|
| 2347 |
// Apply fragment shader or tint |
| 2348 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 2349 |
FragmentShaderInput fsInput; |
| 2350 |
fsInput.texWidth = texW; |
| 2351 |
fsInput.texHeight = texH; |
| 2352 |
fsInput.textures = ctx.textures; |
| 2353 |
fsInput.color = ctx.ff.color; |
| 2354 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 2355 |
std::int32_t uFixShader = static_cast<std::int32_t>(uStart * texW * 65536.0f); |
| 2356 |
std::int32_t vFixShader = static_cast<std::int32_t>(vStart * texH * 65536.0f); |
| 2357 |
const float invTexW = 1.0f / static_cast<float>(texW > 0 ? texW : 1); |
| 2358 |
const float invTexH = 1.0f / static_cast<float>(texH > 0 ? texH : 1); |
| 2359 |
for (std::int32_t i = 0; i < scanWidth; i++) { |
| 2360 |
fsInput.rgba = &scanBuf[i * 4]; |
| 2361 |
fsInput.u = uFixShader / 65536.0f * invTexW; |
| 2362 |
fsInput.v = vFixShader / 65536.0f * invTexH; |
| 2363 |
fsInput.x = xLeft + i; |
| 2364 |
fsInput.y = py; |
| 2365 |
ctx.fragmentShader(fsInput); |
| 2366 |
uFixShader += dudxFix; |
| 2367 |
vFixShader += dvdxFix; |
| 2368 |
} |
| 2369 |
} else if DEATH_UNLIKELY(!whiteTint) { |
| 2370 |
tintScanline(scanBuf, scanWidth, tR, tG, tB, tA); |
| 2371 |
} |
| 2372 |
|
| 2373 |
// Blend or copy to framebuffer |
| 2374 |
std::uint8_t* dstRow; |
| 2375 |
#if defined(RHI_USE_FB16) |
| 2376 |
std::uint8_t* fbRow16 = nullptr; |
| 2377 |
if (g_state.is16Bit) { |
| 2378 |
fbRow16 = g_state.colorBuffer + (storeY * g_state.bufferWidth + xLeft) * 2; |
| 2379 |
SwLoadFbSpan565(g_fbRowStage, fbRow16, scanWidth); |
| 2380 |
dstRow = g_fbRowStage; |
| 2381 |
} else |
| 2382 |
#endif |
| 2383 |
{ |
| 2384 |
dstRow = g_state.colorBuffer + (storeY * g_state.bufferWidth + xLeft) * 4; |
| 2385 |
} |
| 2386 |
if (useFastBlend) { |
| 2387 |
blendScanlineSrcAlpha(dstRow, scanBuf, scanWidth); |
| 2388 |
} else { |
| 2389 |
std::memcpy(dstRow, scanBuf, static_cast<std::size_t>(scanWidth) * 4); |
| 2390 |
} |
| 2391 |
#if defined(RHI_USE_FB16) |
| 2392 |
if (fbRow16 != nullptr) { |
| 2393 |
SwStoreFbSpan565(fbRow16, g_fbRowStage, scanWidth); |
| 2394 |
} |
| 2395 |
#endif |
| 2396 |
} else { |
| 2397 |
// Per-pixel path (generic blend modes or no texture) |
| 2398 |
std::uint8_t* dstRow; |
| 2399 |
#if defined(RHI_USE_FB16) |
| 2400 |
std::uint8_t* fbRow16 = nullptr; |
| 2401 |
if (g_state.is16Bit) { |
| 2402 |
fbRow16 = g_state.colorBuffer + (storeY * g_state.bufferWidth + xLeft) * 2; |
| 2403 |
SwLoadFbSpan565(g_fbRowStage, fbRow16, xRight - xLeft + 1); |
| 2404 |
dstRow = g_fbRowStage; |
| 2405 |
} else |
| 2406 |
#endif |
| 2407 |
{ |
| 2408 |
dstRow = g_state.colorBuffer + (storeY * g_state.bufferWidth + xLeft) * 4; |
| 2409 |
} |
| 2410 |
float u = uStart, vv = vStart; |
| 2411 |
|
| 2412 |
for (std::int32_t px = xLeft; px <= xRight; px++, dstRow += 4) { |
| 2413 |
std::int32_t sR, sG, sB, sA; |
| 2414 |
if (texPixels != nullptr && useLinear) { |
| 2415 |
float wu = WrapUV(u, wrapS); |
| 2416 |
float wv = WrapUV(vv, wrapT); |
| 2417 |
std::uint8_t raw[4]; |
| 2418 |
SampleBilinearFloat(texPixels, texW, texH, texBpp, wu, wv, wrapS, wrapT, raw); |
| 2419 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2420 |
} else if (texPixels != nullptr) { |
| 2421 |
float wu = WrapUV(u, wrapS); |
| 2422 |
float wv = WrapUV(vv, wrapT); |
| 2423 |
std::int32_t srcX = std::max(0, std::min(texW - 1, static_cast<std::int32_t>(wu * (texW - 1) + 0.5f))); |
| 2424 |
std::int32_t srcY = std::max(0, std::min(texH - 1, static_cast<std::int32_t>(wv * (texH - 1) + 0.5f))); |
| 2425 |
std::uint8_t raw[4]; |
| 2426 |
SwExpandTexel(raw, texPixels + (srcY * texW + srcX) * texBpp, texBpp); |
| 2427 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2428 |
} else { |
| 2429 |
sR = 255; sG = 255; sB = 255; sA = 255; |
| 2430 |
} |
| 2431 |
u += dudx; |
| 2432 |
vv += dvdx; |
| 2433 |
|
| 2434 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 2435 |
std::uint8_t px4[4] = { |
| 2436 |
static_cast<std::uint8_t>(sR), static_cast<std::uint8_t>(sG), |
| 2437 |
static_cast<std::uint8_t>(sB), static_cast<std::uint8_t>(sA) |
| 2438 |
}; |
| 2439 |
FragmentShaderInput fsInput; |
| 2440 |
fsInput.rgba = px4; |
| 2441 |
fsInput.u = u - dudx; |
| 2442 |
fsInput.v = vv - dvdx; |
| 2443 |
fsInput.x = px; |
| 2444 |
fsInput.y = py; |
| 2445 |
fsInput.texWidth = texW; |
| 2446 |
fsInput.texHeight = texH; |
| 2447 |
fsInput.textures = ctx.textures; |
| 2448 |
fsInput.color = ctx.ff.color; |
| 2449 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 2450 |
ctx.fragmentShader(fsInput); |
| 2451 |
sR = px4[0]; sG = px4[1]; sB = px4[2]; sA = px4[3]; |
| 2452 |
} else if DEATH_UNLIKELY(!whiteTint) { |
| 2453 |
sR = (sR * tR) >> 8; |
| 2454 |
sG = (sG * tG) >> 8; |
| 2455 |
sB = (sB * tB) >> 8; |
| 2456 |
sA = (sA * tA) >> 8; |
| 2457 |
} |
| 2458 |
|
| 2459 |
if (useBlend) { |
| 2460 |
if (sA == 0) continue; |
| 2461 |
if (useFastBlend) { |
| 2462 |
if (sA >= 255) { |
| 2463 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 2464 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 2465 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 2466 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 2467 |
} else { |
| 2468 |
const std::int32_t inv = 255 - sA; |
| 2469 |
dstRow[0] = static_cast<std::uint8_t>((sR * sA + dstRow[0] * inv) >> 8); |
| 2470 |
dstRow[1] = static_cast<std::uint8_t>((sG * sA + dstRow[1] * inv) >> 8); |
| 2471 |
dstRow[2] = static_cast<std::uint8_t>((sB * sA + dstRow[2] * inv) >> 8); |
| 2472 |
dstRow[3] = static_cast<std::uint8_t>((sA * 255 + dstRow[3] * inv) >> 8); |
| 2473 |
} |
| 2474 |
} else { |
| 2475 |
BlendPixelGeneric(dstRow, sR, sG, sB, sA, g_state.blendSrc, g_state.blendDst); |
| 2476 |
} |
| 2477 |
} else { |
| 2478 |
dstRow[0] = static_cast<std::uint8_t>(sR); |
| 2479 |
dstRow[1] = static_cast<std::uint8_t>(sG); |
| 2480 |
dstRow[2] = static_cast<std::uint8_t>(sB); |
| 2481 |
dstRow[3] = static_cast<std::uint8_t>(sA); |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
#if defined(RHI_USE_FB16) |
| 2486 |
if (fbRow16 != nullptr) { |
| 2487 |
SwStoreFbSpan565(fbRow16, g_fbRowStage, xRight - xLeft + 1); |
| 2488 |
} |
| 2489 |
#endif |
| 2490 |
} |
| 2491 |
} |
| 2492 |
} |
| 2493 |
|
| 2494 |
// ===================================================================== |
| 2495 |
// Line rasterizer - a clipped DDA with interpolated texture coordinates. |
| 2496 |
// Walks one pixel per major-axis step from the start toward the end, |
| 2497 |
// excluding the final endpoint (the GL diamond-exit convention), so the |
| 2498 |
// shared joint of two strip segments is never blended twice. Each pixel |
| 2499 |
// runs the same sample -> fragment/tint -> blend pipeline as |
| 2500 |
// RasterizeTriangle. Consumers: the HUD weapon wheel draws its arcs as |
| 2501 |
// textured LineStrip mesh commands. |
| 2502 |
// ===================================================================== |
| 2503 |
void RasterizeLine(const DrawContext& ctx, const Vertex2D& v0, const Vertex2D& v1) |
| 2504 |
{ |
| 2505 |
if DEATH_UNLIKELY(g_state.colorBuffer == nullptr) { |
| 2506 |
return; |
| 2507 |
} |
| 2508 |
|
| 2509 |
// Inclusive pixel clip rectangle (scissor Y flipped - it is in bottom-up window coordinates) |
| 2510 |
std::int32_t clipMinX = 0, clipMaxX = g_state.bufferWidth - 1; |
| 2511 |
std::int32_t clipMinY = 0, clipMaxY = g_state.bufferHeight - 1; |
| 2512 |
if DEATH_UNLIKELY(g_state.scissorEnabled) { |
| 2513 |
clipMinX = std::max(clipMinX, g_state.scissorX); |
| 2514 |
clipMaxX = std::min(clipMaxX, g_state.scissorX + g_state.scissorW - 1); |
| 2515 |
clipMinY = std::max(clipMinY, g_state.bufferHeight - g_state.scissorY - g_state.scissorH); |
| 2516 |
clipMaxY = std::min(clipMaxY, g_state.bufferHeight - 1 - g_state.scissorY); |
| 2517 |
} |
| 2518 |
if DEATH_UNLIKELY(clipMinX > clipMaxX || clipMinY > clipMaxY) { |
| 2519 |
return; |
| 2520 |
} |
| 2521 |
|
| 2522 |
const SwTexture* tex = (ctx.ff.hasTexture && ctx.ff.textureUnit < static_cast<std::int32_t>(MaxTextureUnits) |
| 2523 |
? ctx.textures[ctx.ff.textureUnit] : nullptr); |
| 2524 |
const std::uint8_t* texPixels = (tex != nullptr ? tex->GetPixels(0) : nullptr); |
| 2525 |
const std::int32_t texW = (tex != nullptr ? tex->GetWidth() : 0); |
| 2526 |
const std::int32_t texH = (tex != nullptr ? tex->GetHeight() : 0); |
| 2527 |
const std::int32_t texBpp = (tex != nullptr ? tex->GetBytesPerPixel() : 4); |
| 2528 |
if DEATH_UNLIKELY(texPixels != nullptr && (texW <= 0 || texH <= 0)) { |
| 2529 |
texPixels = nullptr; // Malformed texture - treat as untextured |
| 2530 |
} |
| 2531 |
const SamplerWrapping wrapS = (tex != nullptr ? tex->GetWrapS() : SamplerWrapping::ClampToEdge); |
| 2532 |
const SamplerWrapping wrapT = (tex != nullptr ? tex->GetWrapT() : SamplerWrapping::ClampToEdge); |
| 2533 |
const bool useLinear = (tex != nullptr && tex->GetMagFilter() == SamplerFilter::Linear && texW > 1 && texH > 1); |
| 2534 |
|
| 2535 |
const std::int32_t tR = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[0]) * 255.0f + 0.5f); |
| 2536 |
const std::int32_t tG = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[1]) * 255.0f + 0.5f); |
| 2537 |
const std::int32_t tB = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[2]) * 255.0f + 0.5f); |
| 2538 |
const std::int32_t tA = static_cast<std::int32_t>(std::min(1.0f, ctx.ff.color[3]) * 255.0f + 0.5f); |
| 2539 |
|
| 2540 |
const bool useBlend = g_state.blendingEnabled; |
| 2541 |
const bool useFastBlend = (useBlend && |
| 2542 |
g_state.blendSrc == SwBlendFactor::SrcAlpha && |
| 2543 |
g_state.blendDst == SwBlendFactor::OneMinusSrcAlpha); |
| 2544 |
|
| 2545 |
const float dx = v1.x - v0.x; |
| 2546 |
const float dy = v1.y - v0.y; |
| 2547 |
const std::int32_t steps = std::max(1, static_cast<std::int32_t>(std::ceil(std::max(std::fabs(dx), std::fabs(dy))))); |
| 2548 |
const float invSteps = 1.0f / static_cast<float>(steps); |
| 2549 |
|
| 2550 |
for (std::int32_t i = 0; i < steps; i++) { |
| 2551 |
const float t = static_cast<float>(i) * invSteps; |
| 2552 |
const std::int32_t px = static_cast<std::int32_t>(v0.x + dx * t); |
| 2553 |
const std::int32_t py = static_cast<std::int32_t>(v0.y + dy * t); |
| 2554 |
if (px < clipMinX || px > clipMaxX || py < clipMinY || py > clipMaxY) { |
| 2555 |
continue; |
| 2556 |
} |
| 2557 |
|
| 2558 |
float u = v0.u + (v1.u - v0.u) * t; |
| 2559 |
float vv = v0.v + (v1.v - v0.v) * t; |
| 2560 |
|
| 2561 |
std::int32_t sR, sG, sB, sA; |
| 2562 |
if (texPixels != nullptr && useLinear) { |
| 2563 |
u = WrapUV(u, wrapS); |
| 2564 |
vv = WrapUV(vv, wrapT); |
| 2565 |
std::uint8_t raw[4]; |
| 2566 |
SampleBilinearFloat(texPixels, texW, texH, texBpp, u, vv, wrapS, wrapT, raw); |
| 2567 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2568 |
} else if (texPixels != nullptr) { |
| 2569 |
u = WrapUV(u, wrapS); |
| 2570 |
vv = WrapUV(vv, wrapT); |
| 2571 |
const std::int32_t srcX = std::max(0, std::min(texW - 1, static_cast<std::int32_t>(u * (texW - 1) + 0.5f))); |
| 2572 |
const std::int32_t srcY = std::max(0, std::min(texH - 1, static_cast<std::int32_t>(vv * (texH - 1) + 0.5f))); |
| 2573 |
std::uint8_t raw[4]; |
| 2574 |
SwExpandTexel(raw, texPixels + (srcY * texW + srcX) * texBpp, texBpp); |
| 2575 |
sR = raw[0]; sG = raw[1]; sB = raw[2]; sA = raw[3]; |
| 2576 |
} else { |
| 2577 |
sR = 255; sG = 255; sB = 255; sA = 255; |
| 2578 |
} |
| 2579 |
|
| 2580 |
if DEATH_UNLIKELY(ctx.fragmentShader != nullptr) { |
| 2581 |
std::uint8_t px4[4] = { |
| 2582 |
static_cast<std::uint8_t>(sR), static_cast<std::uint8_t>(sG), |
| 2583 |
static_cast<std::uint8_t>(sB), static_cast<std::uint8_t>(sA) |
| 2584 |
}; |
| 2585 |
FragmentShaderInput fsInput; |
| 2586 |
fsInput.rgba = px4; |
| 2587 |
fsInput.u = u; |
| 2588 |
fsInput.v = vv; |
| 2589 |
fsInput.x = px; |
| 2590 |
fsInput.y = py; |
| 2591 |
fsInput.texWidth = texW; |
| 2592 |
fsInput.texHeight = texH; |
| 2593 |
fsInput.textures = ctx.textures; |
| 2594 |
fsInput.color = ctx.ff.color; |
| 2595 |
fsInput.userData = ctx.fragmentShaderUserData; |
| 2596 |
ctx.fragmentShader(fsInput); |
| 2597 |
sR = px4[0]; sG = px4[1]; sB = px4[2]; sA = px4[3]; |
| 2598 |
} else { |
| 2599 |
sR = (sR * tR) >> 8; |
| 2600 |
sG = (sG * tG) >> 8; |
| 2601 |
sB = (sB * tB) >> 8; |
| 2602 |
sA = (sA * tA) >> 8; |
| 2603 |
} |
| 2604 |
|
| 2605 |
// Y is flipped when the destination is a render-target texture (stored bottom-up) |
| 2606 |
const std::int32_t storeY = (g_state.isFboTarget ? (g_state.bufferHeight - 1 - py) : py); |
| 2607 |
std::uint8_t* dstPx; |
| 2608 |
#if defined(RHI_USE_FB16) |
| 2609 |
std::uint8_t fbPxStage[4]; |
| 2610 |
std::uint8_t* fbPx16 = nullptr; |
| 2611 |
if (g_state.is16Bit) { |
| 2612 |
// Stage the single 565 pixel as RGBA8; packed back after the write below |
| 2613 |
fbPx16 = g_state.colorBuffer + (static_cast<std::size_t>(storeY) * g_state.bufferWidth + px) * 2; |
| 2614 |
std::uint16_t fbPx; |
| 2615 |
std::memcpy(&fbPx, fbPx16, 2); |
| 2616 |
SwUnpack565(fbPx, fbPxStage); |
| 2617 |
dstPx = fbPxStage; |
| 2618 |
} else |
| 2619 |
#endif |
| 2620 |
{ |
| 2621 |
dstPx = g_state.colorBuffer + (static_cast<std::size_t>(storeY) * g_state.bufferWidth + px) * 4; |
| 2622 |
} |
| 2623 |
|
| 2624 |
if (useBlend) { |
| 2625 |
if (sA == 0) { |
| 2626 |
continue; |
| 2627 |
} |
| 2628 |
if (useFastBlend) { |
| 2629 |
if (sA >= 255) { |
| 2630 |
dstPx[0] = static_cast<std::uint8_t>(sR); |
| 2631 |
dstPx[1] = static_cast<std::uint8_t>(sG); |
| 2632 |
dstPx[2] = static_cast<std::uint8_t>(sB); |
| 2633 |
dstPx[3] = static_cast<std::uint8_t>(sA); |
| 2634 |
} else { |
| 2635 |
const std::int32_t inv = 255 - sA; |
| 2636 |
dstPx[0] = static_cast<std::uint8_t>((sR * sA + dstPx[0] * inv) >> 8); |
| 2637 |
dstPx[1] = static_cast<std::uint8_t>((sG * sA + dstPx[1] * inv) >> 8); |
| 2638 |
dstPx[2] = static_cast<std::uint8_t>((sB * sA + dstPx[2] * inv) >> 8); |
| 2639 |
dstPx[3] = static_cast<std::uint8_t>((sA * 255 + dstPx[3] * inv) >> 8); |
| 2640 |
} |
| 2641 |
} else { |
| 2642 |
BlendPixelGeneric(dstPx, sR, sG, sB, sA, g_state.blendSrc, g_state.blendDst); |
| 2643 |
} |
| 2644 |
} else { |
| 2645 |
dstPx[0] = static_cast<std::uint8_t>(sR); |
| 2646 |
dstPx[1] = static_cast<std::uint8_t>(sG); |
| 2647 |
dstPx[2] = static_cast<std::uint8_t>(sB); |
| 2648 |
dstPx[3] = static_cast<std::uint8_t>(sA); |
| 2649 |
} |
| 2650 |
|
| 2651 |
#if defined(RHI_USE_FB16) |
| 2652 |
if (fbPx16 != nullptr) { |
| 2653 |
const std::uint16_t fbPx = SwPack565(fbPxStage); |
| 2654 |
std::memcpy(fbPx16, &fbPx, 2); |
| 2655 |
} |
| 2656 |
#endif |
| 2657 |
} |
| 2658 |
} |
| 2659 |
|
| 2660 |
void DrawPrimitive(const DrawContext& ctx, PrimitiveType type, const SmallVectorImpl<std::int32_t>& indices) |
| 2661 |
{ |
| 2662 |
switch (type) { |
| 2663 |
case PrimitiveType::Triangles: { |
| 2664 |
for (std::size_t i = 0; i + 2 < indices.size(); i += 3) { |
| 2665 |
RasterizeTriangle(ctx, |
| 2666 |
FetchVertex(ctx, indices[i]), |
| 2667 |
FetchVertex(ctx, indices[i + 1]), |
| 2668 |
FetchVertex(ctx, indices[i + 2])); |
| 2669 |
} |
| 2670 |
break; |
| 2671 |
} |
| 2672 |
case PrimitiveType::TriangleStrip: { |
| 2673 |
for (std::size_t i = 0; i + 2 < indices.size(); i++) { |
| 2674 |
if (i & 1) { |
| 2675 |
RasterizeTriangle(ctx, |
| 2676 |
FetchVertex(ctx, indices[i]), |
| 2677 |
FetchVertex(ctx, indices[i + 2]), |
| 2678 |
FetchVertex(ctx, indices[i + 1])); |
| 2679 |
} else { |
| 2680 |
RasterizeTriangle(ctx, |
| 2681 |
FetchVertex(ctx, indices[i]), |
| 2682 |
FetchVertex(ctx, indices[i + 1]), |
| 2683 |
FetchVertex(ctx, indices[i + 2])); |
| 2684 |
} |
| 2685 |
} |
| 2686 |
break; |
| 2687 |
} |
| 2688 |
case PrimitiveType::TriangleFan: { |
| 2689 |
for (std::size_t i = 1; i + 1 < indices.size(); i++) { |
| 2690 |
RasterizeTriangle(ctx, |
| 2691 |
FetchVertex(ctx, indices[0]), |
| 2692 |
FetchVertex(ctx, indices[i]), |
| 2693 |
FetchVertex(ctx, indices[i + 1])); |
| 2694 |
} |
| 2695 |
break; |
| 2696 |
} |
| 2697 |
case PrimitiveType::Lines: { |
| 2698 |
for (std::size_t i = 0; i + 1 < indices.size(); i += 2) { |
| 2699 |
RasterizeLine(ctx, FetchVertex(ctx, indices[i]), FetchVertex(ctx, indices[i + 1])); |
| 2700 |
} |
| 2701 |
break; |
| 2702 |
} |
| 2703 |
case PrimitiveType::LineStrip: { |
| 2704 |
for (std::size_t i = 0; i + 1 < indices.size(); i++) { |
| 2705 |
RasterizeLine(ctx, FetchVertex(ctx, indices[i]), FetchVertex(ctx, indices[i + 1])); |
| 2706 |
} |
| 2707 |
break; |
| 2708 |
} |
| 2709 |
case PrimitiveType::LineLoop: { |
| 2710 |
for (std::size_t i = 0; i + 1 < indices.size(); i++) { |
| 2711 |
RasterizeLine(ctx, FetchVertex(ctx, indices[i]), FetchVertex(ctx, indices[i + 1])); |
| 2712 |
} |
| 2713 |
if (indices.size() > 2) { |
| 2714 |
RasterizeLine(ctx, FetchVertex(ctx, indices[indices.size() - 1]), FetchVertex(ctx, indices[0])); |
| 2715 |
} |
| 2716 |
break; |
| 2717 |
} |
| 2718 |
default: |
| 2719 |
// PrimitiveType::Points stays unimplemented: nothing in the engine submits point |
| 2720 |
// primitives (repo-wide, the only PrimitiveType::Points mentions are the backend |
| 2721 |
// topology-mapping tables), so a point rasterizer would be dead code. |
| 2722 |
break; |
| 2723 |
} |
| 2724 |
} |
| 2725 |
} |
| 2726 |
|
| 2727 |
// ========================================================================= |
| 2728 |
// Public API |
| 2729 |
// ========================================================================= |
| 2730 |
void SwRaster::SetColorBuffer(std::uint8_t* pixels, std::int32_t width, std::int32_t height, bool isFboTarget) |
| 2731 |
{ |
| 2732 |
g_state.colorBuffer = pixels; |
| 2733 |
g_state.bufferWidth = width; |
| 2734 |
g_state.bufferHeight = height; |
| 2735 |
g_state.isFboTarget = isFboTarget; |
| 2736 |
#if defined(RHI_USE_FB16) |
| 2737 |
// On the software backend a non-FBO target is the screen framebuffer, which is the only 16-bit |
| 2738 |
// surface in this mode (render-target textures stay RGBA8) |
| 2739 |
g_state.is16Bit = !isFboTarget; |
| 2740 |
#endif |
| 2741 |
|
| 2742 |
// Keep the tile renderer pointed at the same surface. Initialize() is idempotent (spins up the worker |
| 2743 |
// pool once); SetTargetBuffer() early-returns when the target is unchanged (the device calls this |
| 2744 |
// before every draw), and flushes the previous target's queue when it actually changes. |
| 2745 |
SwTileRenderer::Initialize(); |
| 2746 |
SwTileRenderer::SetTargetBuffer(pixels, width, height, isFboTarget); |
| 2747 |
} |
| 2748 |
|
| 2749 |
void SwRaster::SetViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height) |
| 2750 |
{ |
| 2751 |
g_state.viewportX = x; |
| 2752 |
g_state.viewportY = y; |
| 2753 |
g_state.viewportW = width; |
| 2754 |
g_state.viewportH = height; |
| 2755 |
|
| 2756 |
// Mirror the viewport so the tile renderer snapshots it into each deferred command and reproduces the |
| 2757 |
// exact NDC→screen transform the immediate path uses. |
| 2758 |
SwTileRenderer::SetViewport(x, y, width, height); |
| 2759 |
} |
| 2760 |
|
| 2761 |
void SwRaster::SetScissor(bool enabled, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height) |
| 2762 |
{ |
| 2763 |
g_state.scissorEnabled = enabled; |
| 2764 |
g_state.scissorX = x; |
| 2765 |
g_state.scissorY = y; |
| 2766 |
g_state.scissorW = width; |
| 2767 |
g_state.scissorH = height; |
| 2768 |
} |
| 2769 |
|
| 2770 |
void SwRaster::SetBlending(bool enabled, SwBlendFactor src, SwBlendFactor dst) |
| 2771 |
{ |
| 2772 |
g_state.blendingEnabled = enabled; |
| 2773 |
g_state.blendSrc = src; |
| 2774 |
g_state.blendDst = dst; |
| 2775 |
} |
| 2776 |
|
| 2777 |
void SwRaster::Clear(float r, float g, float b, float a) |
| 2778 |
{ |
| 2779 |
if (g_state.colorBuffer == nullptr) { |
| 2780 |
return; |
| 2781 |
} |
| 2782 |
|
| 2783 |
// A full-buffer clear wipes everything drawn before it, so drop any deferred draws still queued for |
| 2784 |
// this surface rather than letting them re-render on top of the cleared buffer at the next flush. |
| 2785 |
if (SwTileRenderer::GetPendingCommandCount() > 0) { |
| 2786 |
SwTileRenderer::DiscardPending(); |
| 2787 |
} |
| 2788 |
|
| 2789 |
const std::uint8_t rb = static_cast<std::uint8_t>(r * 255.0f); |
| 2790 |
const std::uint8_t gb = static_cast<std::uint8_t>(g * 255.0f); |
| 2791 |
const std::uint8_t bb = static_cast<std::uint8_t>(b * 255.0f); |
| 2792 |
const std::uint8_t ab = static_cast<std::uint8_t>(a * 255.0f); |
| 2793 |
const std::int32_t totalPixels = g_state.bufferWidth * g_state.bufferHeight; |
| 2794 |
#if defined(RHI_USE_FB16) |
| 2795 |
if (g_state.is16Bit) { |
| 2796 |
const std::uint8_t rgba[4] = { rb, gb, bb, ab }; |
| 2797 |
const std::uint16_t pattern16 = SwPack565(rgba); |
| 2798 |
if ((pattern16 >> 8) == (pattern16 & 0xFF)) { |
| 2799 |
// Both pattern bytes identical (black, white, greys): a plain memset |
| 2800 |
std::memset(g_state.colorBuffer, pattern16 & 0xFF, static_cast<std::size_t>(totalPixels) * 2); |
| 2801 |
} else { |
| 2802 |
// Two pixels per 32-bit store (the buffer allocation is at least 4-byte aligned) |
| 2803 |
const std::uint32_t pattern32 = (static_cast<std::uint32_t>(pattern16) << 16) | pattern16; |
| 2804 |
std::uint32_t* dst32 = reinterpret_cast<std::uint32_t*>(g_state.colorBuffer); |
| 2805 |
const std::int32_t pairs = totalPixels >> 1; |
| 2806 |
for (std::int32_t i = 0; i < pairs; ++i) { |
| 2807 |
dst32[i] = pattern32; |
| 2808 |
} |
| 2809 |
if (totalPixels & 1) { |
| 2810 |
std::memcpy(g_state.colorBuffer + static_cast<std::size_t>(totalPixels - 1) * 2, &pattern16, 2); |
| 2811 |
} |
| 2812 |
} |
| 2813 |
return; |
| 2814 |
} |
| 2815 |
#endif |
| 2816 |
if (rb == gb && gb == bb && bb == ab) { |
| 2817 |
// All channels identical: single memset |
| 2818 |
std::memset(g_state.colorBuffer, rb, static_cast<std::size_t>(totalPixels) * 4); |
| 2819 |
} else { |
| 2820 |
// Build 32-bit RGBA pattern and fill using 32-bit writes |
| 2821 |
const std::uint32_t pattern = static_cast<std::uint32_t>(rb) |
| 2822 |
| (static_cast<std::uint32_t>(gb) << 8) |
| 2823 |
| (static_cast<std::uint32_t>(bb) << 16) |
| 2824 |
| (static_cast<std::uint32_t>(ab) << 24); |
| 2825 |
std::uint32_t* dst32 = reinterpret_cast<std::uint32_t*>(g_state.colorBuffer); |
| 2826 |
for (std::int32_t i = 0; i < totalPixels; ++i) { |
| 2827 |
dst32[i] = pattern; |
| 2828 |
} |
| 2829 |
} |
| 2830 |
} |
| 2831 |
|
| 2832 |
void SwRaster::SetDrawContext(const DrawContext& ctx) |
| 2833 |
{ |
| 2834 |
static DrawContext s_ctx; |
| 2835 |
s_ctx = ctx; |
| 2836 |
g_state.drawCtx = &s_ctx; |
| 2837 |
// Mirror blend / scissor state into global state so the rasterizer picks them up |
| 2838 |
g_state.blendingEnabled = ctx.blendingEnabled; |
| 2839 |
g_state.blendSrc = ctx.blendSrc; |
| 2840 |
g_state.blendDst = ctx.blendDst; |
| 2841 |
g_state.scissorEnabled = ctx.scissorEnabled; |
| 2842 |
g_state.scissorX = ctx.scissorRect.X; |
| 2843 |
g_state.scissorY = ctx.scissorRect.Y; |
| 2844 |
g_state.scissorW = ctx.scissorRect.W; |
| 2845 |
g_state.scissorH = ctx.scissorRect.H; |
| 2846 |
} |
| 2847 |
|
| 2848 |
void SwRaster::ClearDrawContext() |
| 2849 |
{ |
| 2850 |
g_state.drawCtx = nullptr; |
| 2851 |
} |
| 2852 |
|
| 2853 |
void SwRaster::Draw(PrimitiveType type, std::int32_t firstVertex, std::int32_t count) |
| 2854 |
{ |
| 2855 |
if DEATH_UNLIKELY(g_state.drawCtx == nullptr) return; |
| 2856 |
|
| 2857 |
// Fast path: detect fullscreen texture blits and handle with direct memcpy/stretch |
| 2858 |
if (TryFastBlit(*g_state.drawCtx, type, firstVertex, count)) { |
| 2859 |
return; |
| 2860 |
} |
| 2861 |
|
| 2862 |
// Defer to the tile-based renderer. An accepted command is rasterized later (tiled, multi-threaded); |
| 2863 |
// a declined one (an effect parameter block that can't be snapshotted, or the buffer being full) |
| 2864 |
// falls through to immediate rendering below — but flush the queue first so it lands after everything |
| 2865 |
// submitted before it, preserving draw order. |
| 2866 |
if (SwTileRenderer::IsActive()) { |
| 2867 |
if (SwTileRenderer::SubmitCommand(*g_state.drawCtx, type, firstVertex, count)) { |
| 2868 |
return; |
| 2869 |
} |
| 2870 |
SwTileRenderer::Flush(); |
| 2871 |
} |
| 2872 |
|
| 2873 |
// Fast path: procedural 4-vertex quad (TriangleStrip, no vertex buffer) |
| 2874 |
if DEATH_LIKELY(type == PrimitiveType::TriangleStrip && count == 4 && firstVertex == 0 && |
| 2875 |
g_state.drawCtx->vertexData == nullptr) { |
| 2876 |
const DrawContext& ctx = *g_state.drawCtx; |
| 2877 |
Vertex2D v0 = FetchVertex(ctx, 0), v1 = FetchVertex(ctx, 1); |
| 2878 |
Vertex2D v2 = FetchVertex(ctx, 2), v3 = FetchVertex(ctx, 3); |
| 2879 |
// Verify axis-aligned (no rotation: same x for vertical pairs, same y for horizontal pairs) |
| 2880 |
if DEATH_LIKELY(std::fabs(v0.x - v1.x) < 0.5f && std::fabs(v2.x - v3.x) < 0.5f && |
| 2881 |
std::fabs(v0.y - v2.y) < 0.5f && std::fabs(v1.y - v3.y) < 0.5f) { |
| 2882 |
DrawAxisAlignedQuad(ctx, v0, v1, v2, v3); |
| 2883 |
return; |
| 2884 |
} |
| 2885 |
// Rotated/scaled quad: use affine scanline rasterizer |
| 2886 |
DrawAffineQuad(ctx, v0, v1, v2, v3); |
| 2887 |
return; |
| 2888 |
} |
| 2889 |
|
| 2890 |
SmallVector<std::int32_t> indices(static_cast<std::size_t>(count)); |
| 2891 |
for (std::int32_t i = 0; i < count; ++i) { |
| 2892 |
indices[i] = firstVertex + i; |
| 2893 |
} |
| 2894 |
DrawPrimitive(*g_state.drawCtx, type, indices); |
| 2895 |
} |
| 2896 |
|
| 2897 |
void SwRaster::Flush() |
| 2898 |
{ |
| 2899 |
SwTileRenderer::Flush(); |
| 2900 |
} |
| 2901 |
} |
| 2902 |
|
| 2903 |
#endif |