| 1 |
#pragma once |
| 2 |
|
| 3 |
#if defined(WITH_RHI_SOFTWARE) |
| 4 |
|
| 5 |
#include "SwTexture.h" |
| 6 |
#include "../RhiTypes.h" |
| 7 |
#include "../../../Primitives/Rect.h" |
| 8 |
|
| 9 |
#include <cstdint> |
| 10 |
#include <cstring> |
| 11 |
|
| 12 |
#if defined(DEATH_TARGET_SSE2) |
| 13 |
# include <emmintrin.h> |
| 14 |
#elif defined(DEATH_TARGET_NEON) |
| 15 |
# include <arm_neon.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
namespace nCine::RHI::Software |
| 19 |
{ |
| 20 |
/** @brief Number of texture units the rasterizer can sample from in a single draw */ |
| 21 |
static constexpr std::uint32_t MaxTextureUnits = SwTexture::MaxTextureUnits; |
| 22 |
|
| 23 |
/** |
| 24 |
@brief Upper bound on a fragment callback's parameter block |
| 25 |
|
| 26 |
The tile renderer defers draws and rasterizes them later (possibly on a worker thread), so it copies |
| 27 |
each draw's @ref DrawContext::fragmentShaderUserData block into per-command storage to make the |
| 28 |
deferred draw self-contained. This is the size of that inline storage; an effect whose parameter |
| 29 |
block is larger is declined for deferral and rasterized immediately instead. It must be at least as |
| 30 |
large as the biggest effect parameter struct the device fills. |
| 31 |
*/ |
| 32 |
static constexpr std::uint32_t MaxFragmentShaderUserDataSize = 256; |
| 33 |
|
| 34 |
/** |
| 35 |
@brief Source or destination factor of the blending equation (rasterizer-local mirror) |
| 36 |
|
| 37 |
A compact mirror of the pipeline-neutral @ref nCine::BlendingFactor holding just the ten factors the |
| 38 |
CPU blenders specialize. The device layer maps a `nCine::BlendingFactor` pair onto this enum when it |
| 39 |
fills a @ref DrawContext, and having a dedicated enum lets the ported blend code stay verbatim without |
| 40 |
being shadowed by the pipeline-neutral one. |
| 41 |
*/ |
| 42 |
enum class SwBlendFactor |
| 43 |
{ |
| 44 |
Zero, /**< Multiplies by zero */ |
| 45 |
One, /**< Multiplies by one */ |
| 46 |
SrcColor, /**< Multiplies by the source color */ |
| 47 |
DstColor, /**< Multiplies by the destination color */ |
| 48 |
OneMinusSrcColor, /**< Multiplies by one minus the source color */ |
| 49 |
OneMinusDstColor, /**< Multiplies by one minus the destination color */ |
| 50 |
SrcAlpha, /**< Multiplies by the source alpha */ |
| 51 |
OneMinusSrcAlpha, /**< Multiplies by one minus the source alpha */ |
| 52 |
DstAlpha, /**< Multiplies by the destination alpha */ |
| 53 |
OneMinusDstAlpha /**< Multiplies by one minus the destination alpha */ |
| 54 |
}; |
| 55 |
|
| 56 |
/** |
| 57 |
@brief Fixed-function per-draw parameters read by the rasterizer |
| 58 |
|
| 59 |
Without programmable shaders the sprite pipeline is described by this plain struct - the software |
| 60 |
counterpart of one instance's uniform block. @ref mvpMatrix transforms the procedural quad corners to |
| 61 |
clip space, @ref color modulates every sampled texel, @ref texRect selects the sampled sub-region and |
| 62 |
@ref spriteSize scales the synthesized corners. The rasterizer reads it directly. |
| 63 |
*/ |
| 64 |
struct FFState |
| 65 |
{ |
| 66 |
/** @brief Model-view-projection transform, 4x4 column-major */ |
| 67 |
float mvpMatrix[16] = { |
| 68 |
1, 0, 0, 0, |
| 69 |
0, 1, 0, 0, |
| 70 |
0, 0, 1, 0, |
| 71 |
0, 0, 0, 1 |
| 72 |
}; |
| 73 |
/** @brief Constant color modulation (tint), normalized RGBA */ |
| 74 |
float color[4] = { 1, 1, 1, 1 }; |
| 75 |
/** @brief Sampled sub-rectangle of the texture as `(uScale, uOffset, vScale, vOffset)` */ |
| 76 |
float texRect[4] = { 0, 0, 1, 1 }; |
| 77 |
/** @brief Sprite size in pixels the procedural quad corners are scaled by */ |
| 78 |
float spriteSize[2] = { 1, 1 }; |
| 79 |
/** @brief Depth value (unused by the 2D rasterizer, carried for parity with the shader uniforms) */ |
| 80 |
float depth = 0.0f; |
| 81 |
/** @brief Whether a texture is bound and should be sampled */ |
| 82 |
bool hasTexture = false; |
| 83 |
/** @brief Texture unit the sampled texture is bound to */ |
| 84 |
std::int32_t textureUnit = 0; |
| 85 |
}; |
| 86 |
|
| 87 |
/** |
| 88 |
@brief Per-pixel inputs handed to an optional C++ fragment callback |
| 89 |
|
| 90 |
Filled once per destination pixel after texture sampling and before blending, so a callback can |
| 91 |
reproduce a shader's `fragment()` in C++. @ref rgba is the sampled straight-alpha color (4 bytes, |
| 92 |
`0..255`) the callback rewrites in place; @ref u / @ref v are the interpolated texture coordinates and |
| 93 |
@ref x / @ref y the destination pixel. The remaining fields expose the primary texture size, every |
| 94 |
bound texture, the instance color and the effect's own opaque parameter block. |
| 95 |
*/ |
| 96 |
struct FragmentShaderInput |
| 97 |
{ |
| 98 |
std::uint8_t* rgba; /**< In/out pixel color (4 bytes, RGBA order), rewritten in place */ |
| 99 |
float u, v; /**< Interpolated texture coordinates */ |
| 100 |
std::int32_t x, y; /**< Destination pixel coordinates */ |
| 101 |
std::int32_t texWidth, texHeight; /**< Dimensions of the primary (unit `ff.textureUnit`) texture */ |
| 102 |
const SwTexture* const* textures; /**< The bound textures (@ref MaxTextureUnits entries) */ |
| 103 |
const float* color; /**< Instance color (4 floats, RGBA) */ |
| 104 |
void* userData; /**< Effect-owned parameter block, opaque to the rasterizer */ |
| 105 |
}; |
| 106 |
|
| 107 |
/** @brief Optional per-pixel fragment callback; runs after sampling, before blending */ |
| 108 |
using FragmentShaderFn = void (*)(const FragmentShaderInput& input); |
| 109 |
|
| 110 |
#if defined(RHI_USE_FB16) |
| 111 |
/** |
| 112 |
@brief Packs one 4-byte RGBA working pixel into an RGB565 framebuffer texel (alpha is dropped) |
| 113 |
|
| 114 |
Part of the optional 16-bit screen-framebuffer mode (`NCINE_RHI_USE_FB16`): the screen buffer |
| 115 |
stores native-endian RGB565 (2 bytes per pixel - half the memory and present bandwidth of RGBA8), |
| 116 |
while render-target textures and all intermediate rasterization stay 4-byte RGBA. The rasterizers |
| 117 |
stage each touched framebuffer row through @ref SwLoadFbSpan565 / @ref SwStoreFbSpan565, so their |
| 118 |
inner loops are unchanged. The mode has no destination alpha - blend factors reading it see 255. |
| 119 |
*/ |
| 120 |
inline std::uint16_t SwPack565(const std::uint8_t* rgba) |
| 121 |
{ |
| 122 |
return std::uint16_t(((rgba[0] & 0xF8) << 8) | ((rgba[1] & 0xFC) << 3) | (rgba[2] >> 3)); |
| 123 |
} |
| 124 |
|
| 125 |
/** @brief Unpacks one RGB565 framebuffer texel to 4-byte RGBA (bit-replicated, so a load-store round trip is lossless; alpha reads 255) */ |
| 126 |
inline void SwUnpack565(std::uint16_t px, std::uint8_t* rgba) |
| 127 |
{ |
| 128 |
const std::uint32_t r5 = (px >> 11) & 0x1F; |
| 129 |
const std::uint32_t g6 = (px >> 5) & 0x3F; |
| 130 |
const std::uint32_t b5 = px & 0x1F; |
| 131 |
rgba[0] = std::uint8_t((r5 << 3) | (r5 >> 2)); |
| 132 |
rgba[1] = std::uint8_t((g6 << 2) | (g6 >> 4)); |
| 133 |
rgba[2] = std::uint8_t((b5 << 3) | (b5 >> 2)); |
| 134 |
rgba[3] = 255; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
@brief Expands a run of RGB565 framebuffer texels into a 4-byte RGBA staging row |
| 139 |
|
| 140 |
The SSE2 / NEON main loops widen 8 texels per iteration with exactly the scalar |
| 141 |
@ref SwUnpack565 bit-replication (`(c << n) | (c >> m)` per channel, alpha 255), so every |
| 142 |
variant produces identical bytes. These spans run on both sides of every 16-bit staging |
| 143 |
round trip (tile copies, rasterizer rows, the lighting combine), which made the former |
| 144 |
per-texel scalar loops the largest FB16-only cost. |
| 145 |
*/ |
| 146 |
inline void SwLoadFbSpan565(std::uint8_t* DEATH_RESTRICT rgba, const std::uint8_t* DEATH_RESTRICT fb16, std::int32_t count) |
| 147 |
{ |
| 148 |
std::int32_t i = 0; |
| 149 |
#if defined(DEATH_TARGET_SSE2) |
| 150 |
const __m128i zero = _mm_setzero_si128(); |
| 151 |
const __m128i mask1F = _mm_set1_epi32(0x1F); |
| 152 |
const __m128i mask3F = _mm_set1_epi32(0x3F); |
| 153 |
const __m128i alphaFF = _mm_set1_epi32(std::int32_t(0xFF000000)); |
| 154 |
for (; i + 8 <= count; i += 8, rgba += 32, fb16 += 16) { |
| 155 |
const __m128i v16 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(fb16)); |
| 156 |
const __m128i lo = _mm_unpacklo_epi16(v16, zero); |
| 157 |
const __m128i hi = _mm_unpackhi_epi16(v16, zero); |
| 158 |
// Per 32-bit lane: extract the three fields and bit-replicate them to 8 bits |
| 159 |
const __m128i r5lo = _mm_and_si128(_mm_srli_epi32(lo, 11), mask1F); |
| 160 |
const __m128i g6lo = _mm_and_si128(_mm_srli_epi32(lo, 5), mask3F); |
| 161 |
const __m128i b5lo = _mm_and_si128(lo, mask1F); |
| 162 |
const __m128i r8lo = _mm_or_si128(_mm_slli_epi32(r5lo, 3), _mm_srli_epi32(r5lo, 2)); |
| 163 |
const __m128i g8lo = _mm_or_si128(_mm_slli_epi32(g6lo, 2), _mm_srli_epi32(g6lo, 4)); |
| 164 |
const __m128i b8lo = _mm_or_si128(_mm_slli_epi32(b5lo, 3), _mm_srli_epi32(b5lo, 2)); |
| 165 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(rgba), _mm_or_si128( |
| 166 |
_mm_or_si128(r8lo, _mm_slli_epi32(g8lo, 8)), _mm_or_si128(_mm_slli_epi32(b8lo, 16), alphaFF))); |
| 167 |
const __m128i r5hi = _mm_and_si128(_mm_srli_epi32(hi, 11), mask1F); |
| 168 |
const __m128i g6hi = _mm_and_si128(_mm_srli_epi32(hi, 5), mask3F); |
| 169 |
const __m128i b5hi = _mm_and_si128(hi, mask1F); |
| 170 |
const __m128i r8hi = _mm_or_si128(_mm_slli_epi32(r5hi, 3), _mm_srli_epi32(r5hi, 2)); |
| 171 |
const __m128i g8hi = _mm_or_si128(_mm_slli_epi32(g6hi, 2), _mm_srli_epi32(g6hi, 4)); |
| 172 |
const __m128i b8hi = _mm_or_si128(_mm_slli_epi32(b5hi, 3), _mm_srli_epi32(b5hi, 2)); |
| 173 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(rgba + 16), _mm_or_si128( |
| 174 |
_mm_or_si128(r8hi, _mm_slli_epi32(g8hi, 8)), _mm_or_si128(_mm_slli_epi32(b8hi, 16), alphaFF))); |
| 175 |
} |
| 176 |
#elif defined(DEATH_TARGET_NEON) |
| 177 |
for (; i + 8 <= count; i += 8, rgba += 32, fb16 += 16) { |
| 178 |
// vld1/vst1 only require element (2-byte) alignment, which a u16 framebuffer always has |
| 179 |
const uint16x8_t v = vld1q_u16(reinterpret_cast<const std::uint16_t*>(fb16)); |
| 180 |
const uint16x8_t r5 = vshrq_n_u16(v, 11); |
| 181 |
const uint16x8_t g6 = vandq_u16(vshrq_n_u16(v, 5), vdupq_n_u16(0x3F)); |
| 182 |
const uint16x8_t b5 = vandq_u16(v, vdupq_n_u16(0x1F)); |
| 183 |
uint8x8x4_t out; |
| 184 |
out.val[0] = vmovn_u16(vorrq_u16(vshlq_n_u16(r5, 3), vshrq_n_u16(r5, 2))); |
| 185 |
out.val[1] = vmovn_u16(vorrq_u16(vshlq_n_u16(g6, 2), vshrq_n_u16(g6, 4))); |
| 186 |
out.val[2] = vmovn_u16(vorrq_u16(vshlq_n_u16(b5, 3), vshrq_n_u16(b5, 2))); |
| 187 |
out.val[3] = vdup_n_u8(255); |
| 188 |
vst4_u8(rgba, out); |
| 189 |
} |
| 190 |
#endif |
| 191 |
for (; i < count; i++, rgba += 4, fb16 += 2) { |
| 192 |
std::uint16_t px; |
| 193 |
std::memcpy(&px, fb16, 2); |
| 194 |
SwUnpack565(px, rgba); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
@brief Packs a 4-byte RGBA staging row back into a run of RGB565 framebuffer texels |
| 200 |
|
| 201 |
The vector counterpart of the scalar @ref SwPack565 loop (see @ref SwLoadFbSpan565 for why); |
| 202 |
SSE2 / NEON pack 8 texels per iteration with the identical truncation, so the output is |
| 203 |
bit-identical to the scalar tail. |
| 204 |
*/ |
| 205 |
inline void SwStoreFbSpan565(std::uint8_t* DEATH_RESTRICT fb16, const std::uint8_t* DEATH_RESTRICT rgba, std::int32_t count) |
| 206 |
{ |
| 207 |
std::int32_t i = 0; |
| 208 |
#if defined(DEATH_TARGET_SSE2) |
| 209 |
const __m128i maskR = _mm_set1_epi32(0x000000F8); |
| 210 |
const __m128i maskG = _mm_set1_epi32(0x0000FC00); |
| 211 |
const __m128i maskB = _mm_set1_epi32(0x00F80000); |
| 212 |
for (; i + 8 <= count; i += 8, rgba += 32, fb16 += 16) { |
| 213 |
const __m128i px0 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(rgba)); |
| 214 |
const __m128i px1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(rgba + 16)); |
| 215 |
__m128i v0 = _mm_or_si128(_mm_slli_epi32(_mm_and_si128(px0, maskR), 8), |
| 216 |
_mm_or_si128(_mm_srli_epi32(_mm_and_si128(px0, maskG), 5), _mm_srli_epi32(_mm_and_si128(px0, maskB), 19))); |
| 217 |
__m128i v1 = _mm_or_si128(_mm_slli_epi32(_mm_and_si128(px1, maskR), 8), |
| 218 |
_mm_or_si128(_mm_srli_epi32(_mm_and_si128(px1, maskG), 5), _mm_srli_epi32(_mm_and_si128(px1, maskB), 19))); |
| 219 |
// The 16-bit results ride in 32-bit lanes; sign-extend the low word so the SIGNED |
| 220 |
// dword-to-word pack (all SSE2 has) passes every value through losslessly |
| 221 |
v0 = _mm_srai_epi32(_mm_slli_epi32(v0, 16), 16); |
| 222 |
v1 = _mm_srai_epi32(_mm_slli_epi32(v1, 16), 16); |
| 223 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(fb16), _mm_packs_epi32(v0, v1)); |
| 224 |
} |
| 225 |
#elif defined(DEATH_TARGET_NEON) |
| 226 |
for (; i + 8 <= count; i += 8, rgba += 32, fb16 += 16) { |
| 227 |
const uint8x8x4_t px = vld4_u8(rgba); |
| 228 |
// Classic shift-right-insert chain: R lands in bits 11-15, G's top 6 in 5-10, B's top 5 in 0-4 |
| 229 |
const uint16x8_t r = vshll_n_u8(px.val[0], 8); |
| 230 |
const uint16x8_t rg = vsriq_n_u16(r, vshll_n_u8(px.val[1], 8), 5); |
| 231 |
const uint16x8_t rgb = vsriq_n_u16(rg, vshll_n_u8(px.val[2], 8), 11); |
| 232 |
vst1q_u16(reinterpret_cast<std::uint16_t*>(fb16), rgb); |
| 233 |
} |
| 234 |
#endif |
| 235 |
for (; i < count; i++, rgba += 4, fb16 += 2) { |
| 236 |
const std::uint16_t px = SwPack565(rgba); |
| 237 |
std::memcpy(fb16, &px, 2); |
| 238 |
} |
| 239 |
} |
| 240 |
#endif |
| 241 |
|
| 242 |
/** |
| 243 |
@brief Quantizes a normalized channel to a byte exactly like the shader runtime's `packColor()` |
| 244 |
|
| 245 |
Kept bit-identical to `sw::packColor` (clamp to `[0, 1]`, scale, round) so the palette-LUT fast |
| 246 |
path below reproduces the transpiled fragment's output byte for byte. |
| 247 |
*/ |
| 248 |
inline std::uint8_t SwQuantizeColor(float v) |
| 249 |
{ |
| 250 |
v = (v < 0.0f) ? 0.0f : (v > 1.0f ? 1.0f : v); |
| 251 |
const std::int32_t i = static_cast<std::int32_t>(v * 255.0f + 0.5f); |
| 252 |
return static_cast<std::uint8_t>((i < 0) ? 0 : (i > 255 ? 255 : i)); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
@brief Expands one stored texel to the rasterizer's 4-byte RGBA working form |
| 257 |
|
| 258 |
@ref SwTexture stores R8 / RG8 index textures natively (1 / 2 bytes per texel); every gather |
| 259 |
expands the fetched texel through this helper, filling the missing channels with 255 - the exact |
| 260 |
bytes the former promoted-to-RGBA8 store held, so everything downstream of a gather (tint, palette |
| 261 |
LUT, fragment callbacks, blending) is bit-identical whatever the store width. A 4-byte store is a |
| 262 |
single 32-bit copy, kept first as the branch the RGBA8 paths take every time. |
| 263 |
*/ |
| 264 |
DEATH_ALWAYS_INLINE void SwExpandTexel(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t bpp) |
| 265 |
{ |
| 266 |
if DEATH_LIKELY(bpp == 4) { |
| 267 |
std::memcpy(dst, src, 4); |
| 268 |
} else { |
| 269 |
dst[0] = src[0]; |
| 270 |
dst[1] = (bpp >= 2 ? src[1] : std::uint8_t(255)); |
| 271 |
dst[2] = 255; |
| 272 |
dst[3] = 255; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
@brief Expands a contiguous run of stored texels to the 4-byte RGBA working form |
| 278 |
|
| 279 |
The vector form of @ref SwExpandTexel for the 1:1-mapped scanline gather (source texels are |
| 280 |
consecutive in the store). R8 expands to `[v,255,255,255]` and RG8 to `[v,a,255,255]` - SSE2 / |
| 281 |
NEON widen 16 resp. 8 texels per iteration by interleaving with all-ones lanes; the scalar tail |
| 282 |
(and the scalar-only build) emits one 32-bit store per texel. A 4-byte store is a plain copy. |
| 283 |
*/ |
| 284 |
inline void SwExpandTexelRun(std::uint8_t* DEATH_RESTRICT dst, const std::uint8_t* DEATH_RESTRICT src, std::int32_t count, std::int32_t bpp) |
| 285 |
{ |
| 286 |
if (bpp == 4) { |
| 287 |
std::memcpy(dst, src, std::size_t(count) * 4); |
| 288 |
return; |
| 289 |
} |
| 290 |
std::int32_t i = 0; |
| 291 |
if (bpp == 1) { |
| 292 |
#if defined(DEATH_TARGET_SSE2) |
| 293 |
const __m128i ones = _mm_set1_epi8(char(0xFF)); |
| 294 |
for (; i + 16 <= count; i += 16, src += 16, dst += 64) { |
| 295 |
const __m128i v = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src)); |
| 296 |
// [v,FF] byte pairs, then [v,FF,FF,FF] dwords |
| 297 |
const __m128i loVF = _mm_unpacklo_epi8(v, ones); |
| 298 |
const __m128i hiVF = _mm_unpackhi_epi8(v, ones); |
| 299 |
const __m128i ffff = ones; |
| 300 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), _mm_unpacklo_epi16(loVF, ffff)); |
| 301 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst + 16), _mm_unpackhi_epi16(loVF, ffff)); |
| 302 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst + 32), _mm_unpacklo_epi16(hiVF, ffff)); |
| 303 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst + 48), _mm_unpackhi_epi16(hiVF, ffff)); |
| 304 |
} |
| 305 |
#elif defined(DEATH_TARGET_NEON) |
| 306 |
const uint8x8_t ones8 = vdup_n_u8(0xFF); |
| 307 |
for (; i + 8 <= count; i += 8, src += 8, dst += 32) { |
| 308 |
const uint8x8_t v = vld1_u8(src); |
| 309 |
const uint8x8x2_t vf = vzip_u8(v, ones8); // [v,FF] byte pairs |
| 310 |
const uint16x4_t ffff = vdup_n_u16(0xFFFF); |
| 311 |
const uint16x4x2_t lo = vzip_u16(vreinterpret_u16_u8(vf.val[0]), ffff); |
| 312 |
const uint16x4x2_t hi = vzip_u16(vreinterpret_u16_u8(vf.val[1]), ffff); |
| 313 |
vst1_u8(dst, vreinterpret_u8_u16(lo.val[0])); |
| 314 |
vst1_u8(dst + 8, vreinterpret_u8_u16(lo.val[1])); |
| 315 |
vst1_u8(dst + 16, vreinterpret_u8_u16(hi.val[0])); |
| 316 |
vst1_u8(dst + 24, vreinterpret_u8_u16(hi.val[1])); |
| 317 |
} |
| 318 |
#endif |
| 319 |
for (; i < count; i++, src++, dst += 4) { |
| 320 |
dst[0] = src[0]; |
| 321 |
dst[1] = 255; |
| 322 |
dst[2] = 255; |
| 323 |
dst[3] = 255; |
| 324 |
} |
| 325 |
} else { |
| 326 |
// RG8: [index, alpha] pairs widen to [index, alpha, 255, 255] |
| 327 |
#if defined(DEATH_TARGET_SSE2) |
| 328 |
const __m128i ones = _mm_set1_epi8(char(0xFF)); |
| 329 |
for (; i + 8 <= count; i += 8, src += 16, dst += 32) { |
| 330 |
const __m128i v = _mm_loadu_si128(reinterpret_cast<const __m128i*>(src)); // 8 x [v,a] |
| 331 |
const __m128i vaFF = ones; |
| 332 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst), |
| 333 |
_mm_unpacklo_epi16(v, vaFF)); |
| 334 |
_mm_storeu_si128(reinterpret_cast<__m128i*>(dst + 16), |
| 335 |
_mm_unpackhi_epi16(v, vaFF)); |
| 336 |
} |
| 337 |
#elif defined(DEATH_TARGET_NEON) |
| 338 |
const uint16x4_t ffff = vdup_n_u16(0xFFFF); |
| 339 |
for (; i + 4 <= count; i += 4, src += 8, dst += 16) { |
| 340 |
const uint16x4_t v = vreinterpret_u16_u8(vld1_u8(src)); // 4 x [v,a] |
| 341 |
const uint16x4x2_t widened = vzip_u16(v, ffff); |
| 342 |
vst1_u8(dst, vreinterpret_u8_u16(widened.val[0])); |
| 343 |
vst1_u8(dst + 8, vreinterpret_u8_u16(widened.val[1])); |
| 344 |
} |
| 345 |
#endif |
| 346 |
for (; i < count; i++, src += 2, dst += 4) { |
| 347 |
dst[0] = src[0]; |
| 348 |
dst[1] = src[1]; |
| 349 |
dst[2] = 255; |
| 350 |
dst[3] = 255; |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
@brief Per-command lookup table replacing the PaletteRemap fragment on the deferred tile path |
| 357 |
|
| 358 |
The PaletteRemap / BatchedPaletteRemap fragment is fundamentally a 256-entry table lookup: the |
| 359 |
palette row (`vPaletteOffset`) and the instance tint are constant for a whole draw command, so the |
| 360 |
fully tinted, quantized output of every possible index byte can be precomputed once per command |
| 361 |
instead of running the generic transpiled fragment (function-pointer call, redundant re-sample, |
| 362 |
float palette math) per pixel. The alpha channel additionally depends on the texel's own source |
| 363 |
alpha when the index texture is RG8 (its sampling swizzle maps `.a` to a texel byte); that case |
| 364 |
keeps the RGB lookup and folds the per-pixel factor in with the same float math the fragment uses, |
| 365 |
so the fast path stays bit-identical in every case. |
| 366 |
|
| 367 |
Built by @ref SwTileRenderer::SubmitCommand() when @ref DrawContext::paletteRemapHint is set and |
| 368 |
every constraint holds (nearest-sampled index texture, byte-addressable swizzle); consumed by the |
| 369 |
tile rasterizer in place of the fragment callback. |
| 370 |
*/ |
| 371 |
struct SwPaletteLut |
| 372 |
{ |
| 373 |
/** @brief Final packed RGBA per index (palette texel x tint); alpha assumes a `1.0` (or constant) source alpha */ |
| 374 |
std::uint8_t packed[256][4]; |
| 375 |
/** @brief Palette texel alpha byte per index, for the per-pixel source-alpha case */ |
| 376 |
std::uint8_t palAlphaByte[256]; |
| 377 |
/** @brief Tint (instance color) alpha the fragment multiplies last */ |
| 378 |
float tintAlpha; |
| 379 |
/** @brief Byte of the gathered (4-byte expanded) texel carrying the palette index (the unit-0 sampling swizzle's `.r` source) */ |
| 380 |
std::int32_t indexByteOffset; |
| 381 |
/** @brief Byte of the gathered texel carrying source alpha (swizzle `.a` source), or `-1` (constant 1) / `-2` (constant 0) */ |
| 382 |
std::int32_t alphaByteOffset; |
| 383 |
/** |
| 384 |
* @brief Whether every one of the 256 @ref packed entries is fully opaque (alpha `255`) |
| 385 |
* |
| 386 |
* With a constant source alpha (@ref alphaByteOffset `== -1`) this proves the draw writes an opaque |
| 387 |
* pixel wherever it samples, whatever the texture holds - the submit-time opaque-overwrite |
| 388 |
* classification (reverse-painter cull) reads it. |
| 389 |
*/ |
| 390 |
bool allOpaque; |
| 391 |
}; |
| 392 |
|
| 393 |
/** |
| 394 |
@brief One transformed screen-space vertex |
| 395 |
|
| 396 |
The output of the vertex stage (@ref FFState transform + viewport map): pixel-space position, texture |
| 397 |
coordinates and an RGBA color forwarded to the fragment stage. |
| 398 |
*/ |
| 399 |
struct Vertex2D |
| 400 |
{ |
| 401 |
float x, y; /**< Screen-space pixel position */ |
| 402 |
float u, v; /**< Texture coordinates */ |
| 403 |
float r, g, b, a; /**< Vertex color */ |
| 404 |
}; |
| 405 |
|
| 406 |
/** |
| 407 |
@brief Everything one draw call needs beyond the persistent render state |
| 408 |
|
| 409 |
Gathers the bound textures, the fixed-function parameters (@ref ff), an optional fragment callback and |
| 410 |
a snapshot of the blend / scissor state. The device fills one of these per draw and hands it to |
| 411 |
@ref SwRaster::SetDrawContext(). When @ref vertexData is null the four sprite-quad corners are |
| 412 |
synthesized procedurally from @ref ff (the path the sprite pipeline uses); otherwise @ref vertexData |
| 413 |
points at interleaved, already clip-space vertices for the general primitive path. |
| 414 |
*/ |
| 415 |
struct DrawContext |
| 416 |
{ |
| 417 |
/** @brief Textures bound to each unit (indexed by `ff.textureUnit` and by a fragment callback) */ |
| 418 |
const SwTexture* textures[MaxTextureUnits] = {}; |
| 419 |
/** @brief Fixed-function per-draw parameters */ |
| 420 |
FFState ff; |
| 421 |
/** @brief Optional per-pixel fragment callback (null for the plain textured / tinted path) */ |
| 422 |
FragmentShaderFn fragmentShader = nullptr; |
| 423 |
/** @brief Opaque parameter block passed to @ref fragmentShader */ |
| 424 |
void* fragmentShaderUserData = nullptr; |
| 425 |
/** |
| 426 |
* @brief Size in bytes of the @ref fragmentShaderUserData block (0 when there is none) |
| 427 |
* |
| 428 |
* Set by the device so the tile renderer can snapshot the block into per-command storage when a draw |
| 429 |
* is deferred. The block must be trivially copyable. Leave 0 for the immediate-only path. |
| 430 |
*/ |
| 431 |
std::uint32_t fragmentShaderUserDataSize = 0; |
| 432 |
|
| 433 |
/** |
| 434 |
* @brief Marks a PaletteRemap / BatchedPaletteRemap draw (set by the device) |
| 435 |
* |
| 436 |
* A hint only: it promises @ref fragmentShader is the transpiled PaletteRemap fragment (whose |
| 437 |
* parameter block is a single `vPaletteOffset` float), so the tile renderer may build a |
| 438 |
* @ref SwPaletteLut for the deferred command. Every other constraint is validated at build time; |
| 439 |
* when any fails, the draw simply keeps the generic fragment callback. |
| 440 |
*/ |
| 441 |
bool paletteRemapHint = false; |
| 442 |
/** |
| 443 |
* @brief Palette LUT consumed in place of @ref fragmentShader (deferred tile path only) |
| 444 |
* |
| 445 |
* Never set by the device: the tile renderer builds the table per deferred command and points its |
| 446 |
* command snapshot here before the flush. Stays null on the immediate path. |
| 447 |
*/ |
| 448 |
const SwPaletteLut* paletteLut = nullptr; |
| 449 |
|
| 450 |
/** |
| 451 |
* @brief Marks a solid no-texture sprite draw (DefaultSpriteNoTexture family, set by the device) |
| 452 |
* |
| 453 |
* A hint only: it promises @ref fragmentShader writes a per-draw constant (`packColor(vColor)`, |
| 454 |
* independent of position and texel), so the tile renderer may evaluate the fragment once per |
| 455 |
* command and fill/blend the constant color instead of calling it per pixel. |
| 456 |
*/ |
| 457 |
bool constantColorHint = false; |
| 458 |
|
| 459 |
/** @brief Whether blending is enabled for this draw */ |
| 460 |
bool blendingEnabled = false; |
| 461 |
/** @brief Source blend factor */ |
| 462 |
SwBlendFactor blendSrc = SwBlendFactor::SrcAlpha; |
| 463 |
/** @brief Destination blend factor */ |
| 464 |
SwBlendFactor blendDst = SwBlendFactor::OneMinusSrcAlpha; |
| 465 |
|
| 466 |
/** @brief Whether the scissor test is enabled for this draw */ |
| 467 |
bool scissorEnabled = false; |
| 468 |
/** @brief Scissor rectangle in bottom-up (OpenGL) window coordinates */ |
| 469 |
Recti scissorRect = Recti(0, 0, 0, 0); |
| 470 |
|
| 471 |
/** @brief Interleaved general vertices `[x, y, u, v]` in clip space, or null to synthesize the sprite quad from @ref ff */ |
| 472 |
const void* vertexData = nullptr; |
| 473 |
/** @brief Byte stride between general vertices (0 == four tightly packed floats) */ |
| 474 |
std::int32_t vertexStride = 0; |
| 475 |
}; |
| 476 |
|
| 477 |
/** |
| 478 |
@brief Fixed-function CPU rasterizer of the software backend |
| 479 |
|
| 480 |
Rasterizes the sprite pipeline into a caller-supplied RGBA8 surface without any GPU. It keeps a small |
| 481 |
amount of persistent state (the destination color buffer, the viewport, and the blend / scissor state) |
| 482 |
set through the `Set*` calls, then rasterizes each draw against the @ref DrawContext installed with |
| 483 |
@ref SetDrawContext(). Three inner paths cover the pipeline: a direct memcpy / stretch blit for |
| 484 |
full-screen copies, a SIMD axis-aligned quad fast path for upright sprites, and affine-quad / |
| 485 |
general-triangle paths for rotated or sheared geometry. Every path supports the same set of blend |
| 486 |
modes, all @ref nCine::SamplerWrapping wrap modes, nearest / bilinear sampling and an optional per-pixel |
| 487 |
fragment callback. |
| 488 |
|
| 489 |
The rasterizer never owns the destination memory or the textures; the device layer resolves both and |
| 490 |
hands them in. Depth is a no-op (the renderer is 2D). |
| 491 |
*/ |
| 492 |
class SwRaster |
| 493 |
{ |
| 494 |
public: |
| 495 |
SwRaster() = delete; |
| 496 |
~SwRaster() = delete; |
| 497 |
|
| 498 |
/** |
| 499 |
@brief Points the rasterizer at the destination RGBA8 surface |
| 500 |
|
| 501 |
@param pixels Base of a tightly packed `width * height * 4` RGBA8 buffer (owned by the caller) |
| 502 |
@param width Surface width in pixels |
| 503 |
@param height Surface height in pixels |
| 504 |
@param isFboTarget `true` when the surface is a render-target texture (rows are written bottom-up |
| 505 |
to match the OpenGL framebuffer convention); `false` for the top-down screen buffer |
| 506 |
*/ |
| 507 |
static void SetColorBuffer(std::uint8_t* pixels, std::int32_t width, std::int32_t height, bool isFboTarget); |
| 508 |
|
| 509 |
/** @brief Sets the viewport used to map clip space to destination pixels */ |
| 510 |
static void SetViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height); |
| 511 |
/** @brief Sets the scissor test in bottom-up (OpenGL) window coordinates */ |
| 512 |
static void SetScissor(bool enabled, std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height); |
| 513 |
/** @brief Sets the blend state applied by every draw path */ |
| 514 |
static void SetBlending(bool enabled, SwBlendFactor src, SwBlendFactor dst); |
| 515 |
/** @brief Fills the whole color buffer with the given normalized RGBA (ignores the scissor) */ |
| 516 |
static void Clear(float r, float g, float b, float a); |
| 517 |
|
| 518 |
/** @brief Installs the per-draw context read by the next @ref Draw() call */ |
| 519 |
static void SetDrawContext(const DrawContext& ctx); |
| 520 |
/** @brief Detaches the per-draw context */ |
| 521 |
static void ClearDrawContext(); |
| 522 |
|
| 523 |
/** @brief Rasterizes @p count vertices from @p firstVertex as @p primitive into the current color buffer */ |
| 524 |
static void Draw(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t count); |
| 525 |
|
| 526 |
/** |
| 527 |
@brief Renders every draw the tile renderer has deferred for the current color buffer |
| 528 |
|
| 529 |
Forwards to the tile-based deferred layer's flush. The device calls this before the color buffer |
| 530 |
is read for presentation, so all queued draws have landed in it. A no-op when nothing is queued; |
| 531 |
it does not return until every worker thread has finished writing. |
| 532 |
*/ |
| 533 |
static void Flush(); |
| 534 |
}; |
| 535 |
} |
| 536 |
|
| 537 |
#endif |