| 1 |
#include "GsDevice.h" |
| 2 |
#include "GsShaderProgram.h" |
| 3 |
#include "GsRenderTarget.h" |
| 4 |
#include "GsStagingBuffer.h" |
| 5 |
#include "GsTexture.h" |
| 6 |
#include "GsBuffer.h" |
| 7 |
#include "../FixedFunctionPass.h" |
| 8 |
|
| 9 |
#include "../../../../Main.h" |
| 10 |
|
| 11 |
#include <cstring> |
| 12 |
|
| 13 |
extern "C" { |
| 14 |
#include <graph.h> |
| 15 |
#include <draw.h> |
| 16 |
#include <dma.h> |
| 17 |
#include <gif_tags.h> |
| 18 |
#include <gs_gp.h> |
| 19 |
#include <gs_psm.h> |
| 20 |
#include <kernel.h> |
| 21 |
} |
| 22 |
|
| 23 |
namespace nCine::RHI::GS |
| 24 |
{ |
| 25 |
namespace |
| 26 |
{ |
| 27 |
/** @brief Words per page and per block - `libgraph`/`libdraw` address local memory in 32-bit words */ |
| 28 |
constexpr std::uint32_t WordsPerPage = GsVram::PageBytes / 4; |
| 29 |
constexpr std::uint32_t WordsPerBlock = GsVram::BlockBytes / 4; |
| 30 |
|
| 31 |
/** @brief Display geometry. NTSC 640x448 interlaced, the mode the probe confirmed the GS accepts */ |
| 32 |
constexpr std::int32_t DisplayWidth = 640; |
| 33 |
constexpr std::int32_t DisplayHeight = 448; |
| 34 |
|
| 35 |
/** |
| 36 |
@brief GIF packet scratch for state and draws |
| 37 |
|
| 38 |
Sized for a whole frame's state changes plus a generous draw margin; @ref FlushPackets() empties |
| 39 |
it whenever it approaches full, so overflowing only costs an extra DMA rather than dropping work. |
| 40 |
*/ |
| 41 |
alignas(64) qword_t _packet[16384]; |
| 42 |
qword_t* _packetCursor = _packet; |
| 43 |
|
| 44 |
/** @brief Staging buffer for CLUT uploads (256 entries of 32 bits, qword-aligned for the transfer) */ |
| 45 |
alignas(64) std::uint32_t _clutStaging[256]; |
| 46 |
|
| 47 |
/** |
| 48 |
@brief The CPU lightmap's factor surface in local memory, and the main-memory image it comes from |
| 49 |
|
| 50 |
Allocated once and rewritten every frame (see @ref GsDevice::ApplyPendingSoftwareLighting()) - a |
| 51 |
per-frame allocation would compete with the texture cache for pages exactly when a level is already |
| 52 |
short of them. A GS transfer reads main memory directly, so the staging image has to be contiguous, |
| 53 |
qword-aligned and in the destination's pitch (see @ref GsStagingBuffer). |
| 54 |
*/ |
| 55 |
std::uint32_t _lightmapPage = GsVram::InvalidPage; |
| 56 |
std::uint32_t _lightmapPageCount = 0; |
| 57 |
GsStagingBuffer _lightmapStaging; |
| 58 |
|
| 59 |
/** @brief Opaque white as a pass colour, for the passes whose colour carries no information */ |
| 60 |
constexpr float OpaqueWhite[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; |
| 61 |
|
| 62 |
framebuffer_t _frame; |
| 63 |
zbuffer_t _zbuffer; |
| 64 |
|
| 65 |
inline std::uint8_t QuantizeChannel(float v) |
| 66 |
{ |
| 67 |
v = (v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v)); |
| 68 |
return std::uint8_t(v * 255.0f + 0.5f); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
@brief A 0..1 float on the GS's FRAGMENT-colour scale, where 0x80 - not 0xFF - is 1.0 |
| 73 |
|
| 74 |
Both the texture function and the blender shift their products right by 7, so a vertex colour is |
| 75 |
a 0..2 multiplier with 128 at unity: `(0xFF texel * 0x80) >> 7` is 0xFF, and a blend factor of |
| 76 |
0x80 passes the source through unchanged. Values above 1.0 are clamped rather than allowed to run |
| 77 |
into the 0x81..0xFF overbright range, because that is the range the PVR's `argb` has too - the |
| 78 |
effects that want a multiplier above one (Colorized) split it into additive passes instead, and |
| 79 |
letting one pass carry 2x here would double-count what the next pass adds. |
| 80 |
*/ |
| 81 |
inline std::uint8_t QuantizePassChannel(float v) |
| 82 |
{ |
| 83 |
v = (v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v)); |
| 84 |
return std::uint8_t(v * 128.0f + 0.5f); |
| 85 |
} |
| 86 |
|
| 87 |
/** @brief A `FixedFunctionPass` colour packed into `RGBAQ` for a TEXTURED primitive */ |
| 88 |
color_t PackPassColor(const float* rgba) |
| 89 |
{ |
| 90 |
color_t out; |
| 91 |
out.r = QuantizePassChannel(rgba[0]); |
| 92 |
out.g = QuantizePassChannel(rgba[1]); |
| 93 |
out.b = QuantizePassChannel(rgba[2]); |
| 94 |
out.a = QuantizePassChannel(rgba[3]); |
| 95 |
// Unused under PRIM_MAP_UV, but it shares the register, so it is given the 1.0 every helper uses |
| 96 |
out.q = 1.0f; |
| 97 |
return out; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
@brief A `FixedFunctionPass` colour packed into `RGBAQ` for an UNTEXTURED primitive |
| 102 |
|
| 103 |
The `>> 7` that makes 0x80 mean 1.0 belongs to the TEXTURE FUNCTION, and a primitive submitted with |
| 104 |
`TME = 0` never reaches it: its interpolated RGB *is* the fragment colour, on the full 0..255 scale |
| 105 |
@ref QuantizeChannel() packs - which is why @ref GsDevice::Clear(), the one untextured draw that was |
| 106 |
always right, quantizes its rectangle that way. Packing an untextured colour like a modulating pass |
| 107 |
instead halves it, and that is what left the horizon tint of the textured background and the fur |
| 108 |
gradient swatches of the user-profile section visibly dark against every other backend. |
| 109 |
|
| 110 |
ALPHA is the exception and stays on the 0..0x80 scale, because nothing about it is a colour: it is |
| 111 |
the blender's `C` in `((A - B) * C >> 7) + D` and the alpha test's operand, and 0x80 is 1.0 to both |
| 112 |
(it is also what every textured draw leaves in the frame buffer, so the two agree there too). |
| 113 |
*/ |
| 114 |
color_t PackVertexColor(const float* rgba) |
| 115 |
{ |
| 116 |
color_t out; |
| 117 |
out.r = QuantizeChannel(rgba[0]); |
| 118 |
out.g = QuantizeChannel(rgba[1]); |
| 119 |
out.b = QuantizeChannel(rgba[2]); |
| 120 |
out.a = QuantizePassChannel(rgba[3]); |
| 121 |
out.q = 1.0f; |
| 122 |
return out; |
| 123 |
} |
| 124 |
|
| 125 |
/** @brief Sends whatever is in the scratch packet and resets the cursor */ |
| 126 |
void FlushPackets() |
| 127 |
{ |
| 128 |
if (_packetCursor == _packet) { |
| 129 |
return; |
| 130 |
} |
| 131 |
dma_channel_send_normal(DMA_CHANNEL_GIF, _packet, std::int32_t(_packetCursor - _packet), 0, 0); |
| 132 |
dma_wait_fast(); |
| 133 |
_packetCursor = _packet; |
| 134 |
} |
| 135 |
|
| 136 |
/** @brief Reserves room for @p qwords, flushing first if the scratch cannot hold them */ |
| 137 |
qword_t* Reserve(std::size_t qwords) |
| 138 |
{ |
| 139 |
const std::size_t capacity = sizeof(_packet) / sizeof(_packet[0]); |
| 140 |
if (std::size_t(_packetCursor - _packet) + qwords > capacity) { |
| 141 |
FlushPackets(); |
| 142 |
} |
| 143 |
return _packetCursor; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
@brief Queues a `TEXFLUSH` into the ordinary register packet |
| 148 |
|
| 149 |
Deliberately hand-assembled instead of calling `draw_texture_flush()`, which looks like the |
| 150 |
obvious way to do this and is not: that helper emits a DMA **END tag** ahead of its GIF tag, |
| 151 |
because every one of its callers in `libdraw` is building a transfer CHAIN. Appended to a packet |
| 152 |
that goes out with `dma_channel_send_normal()`, the tag reaches the GIF where a GIF tag is |
| 153 |
expected, and the channel wedges with no error of any kind - the same trap |
| 154 |
`draw_texture_transfer()` sets, documented at the top of GsDevice.h. What is actually wanted here |
| 155 |
is two qwords: a packed A+D tag and the register write, exactly as the primitive path emits PRIM. |
| 156 |
*/ |
| 157 |
void QueueTextureFlush() |
| 158 |
{ |
| 159 |
qword_t* q = Reserve(4); |
| 160 |
q->dw[0] = GIF_SET_TAG(1, 0, 0, 0, GIF_FLG_PACKED, 1); |
| 161 |
q->dw[1] = GIF_REG_AD; |
| 162 |
q++; |
| 163 |
q->dw[0] = GS_SET_TEXFLUSH(0); |
| 164 |
q->dw[1] = GS_REG_TEXFLUSH; |
| 165 |
q++; |
| 166 |
_packetCursor = q; |
| 167 |
} |
| 168 |
|
| 169 |
// The DefaultSprite / DefaultBatchedSprites instance layout is a hard contract of the shader family |
| 170 |
// (std140 offsets within the InstanceBlock / each batched Instance) - identical to the software and |
| 171 |
// PVR backends' decode |
| 172 |
constexpr std::uint32_t kModelMatrixOffset = 0; |
| 173 |
constexpr std::uint32_t kColorOffset = 64; |
| 174 |
constexpr std::uint32_t kTexRectOffset = 80; |
| 175 |
constexpr std::uint32_t kSpriteSizeOffset = 96; |
| 176 |
constexpr std::uint32_t kPaletteOffsetOffset = 104; |
| 177 |
constexpr std::uint32_t kSpriteSizeNoTexOffset = 80; |
| 178 |
|
| 179 |
const float IdentityMatrix[16] = { |
| 180 |
1, 0, 0, 0, |
| 181 |
0, 1, 0, 0, |
| 182 |
0, 0, 1, 0, |
| 183 |
0, 0, 0, 1 |
| 184 |
}; |
| 185 |
|
| 186 |
/** @brief Column-major 4x4 multiply, out = a * b */ |
| 187 |
void Mat4Mul(const float* a, const float* b, float* out) |
| 188 |
{ |
| 189 |
for (std::int32_t col = 0; col < 4; col++) { |
| 190 |
for (std::int32_t row = 0; row < 4; row++) { |
| 191 |
out[col * 4 + row] = |
| 192 |
a[0 * 4 + row] * b[col * 4 + 0] + |
| 193 |
a[1 * 4 + row] * b[col * 4 + 1] + |
| 194 |
a[2 * 4 + row] * b[col * 4 + 2] + |
| 195 |
a[3 * 4 + row] * b[col * 4 + 3]; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
@brief The six products of projection*view*model a 2D point of the form (x, y, 0, 1) ever reads |
| 202 |
|
| 203 |
The same reduction the PVR backend makes: a sprite pays this per instance, so the other ten |
| 204 |
products of a full 4x4 multiply are never computed. |
| 205 |
*/ |
| 206 |
struct Transform2D |
| 207 |
{ |
| 208 |
float Xx, Xy; // Column 0, rows 0 and 1 |
| 209 |
float Yx, Yy; // Column 1, rows 0 and 1 |
| 210 |
float Tx, Ty; // Column 3, rows 0 and 1 |
| 211 |
}; |
| 212 |
|
| 213 |
void Mat4MulTransform2D(const float* pv, const float* model, Transform2D& out) |
| 214 |
{ |
| 215 |
out.Xx = pv[0] * model[0] + pv[4] * model[1] + pv[8] * model[2] + pv[12] * model[3]; |
| 216 |
out.Xy = pv[1] * model[0] + pv[5] * model[1] + pv[9] * model[2] + pv[13] * model[3]; |
| 217 |
out.Yx = pv[0] * model[4] + pv[4] * model[5] + pv[8] * model[6] + pv[12] * model[7]; |
| 218 |
out.Yy = pv[1] * model[4] + pv[5] * model[5] + pv[9] * model[6] + pv[13] * model[7]; |
| 219 |
out.Tx = pv[0] * model[12] + pv[4] * model[13] + pv[8] * model[14] + pv[12] * model[15]; |
| 220 |
out.Ty = pv[1] * model[12] + pv[5] * model[13] + pv[9] * model[14] + pv[13] * model[15]; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
@brief One blend mode expressed in the terms the GS actually has |
| 225 |
|
| 226 |
The GS computes exactly ONE equation, `Cv = ((A - B) * C) >> 7 + D`, in which A, B and D each |
| 227 |
select the source colour, the destination colour or zero, and C selects the source alpha, the |
| 228 |
destination alpha or the fixed value carried in the same register. There are no independent |
| 229 |
source/destination factors and no blend equation to choose, so a GL-style factor pair either |
| 230 |
collapses onto this form exactly or it does not exist on this hardware at all. |
| 231 |
|
| 232 |
Note `>> 7`: **0x80 is 1.0** to that shifter, not 0xFF, which is why every alpha reaching the |
| 233 |
rasterizer is halved on the way in (see @ref GsTexture::RefreshStore and @ref AcquireClut). |
| 234 |
*/ |
| 235 |
struct BlendEquation |
| 236 |
{ |
| 237 |
std::uint8_t A = BLEND_COLOR_SOURCE; |
| 238 |
std::uint8_t B = BLEND_COLOR_DEST; |
| 239 |
std::uint8_t C = BLEND_ALPHA_SOURCE; |
| 240 |
std::uint8_t D = BLEND_COLOR_DEST; |
| 241 |
std::uint8_t Fixed = 0x80; |
| 242 |
/** |
| 243 |
@brief Whether a source texel of zero alpha leaves the destination exactly as it was |
| 244 |
|
| 245 |
True for every alpha-weighted mode, and it is what makes the alpha test a free optimisation |
| 246 |
rather than a change of behaviour: a fragment the test throws away would have blended to the |
| 247 |
destination value anyway. False for the modes that write the source unconditionally, where |
| 248 |
discarding those fragments would drop pixels that belong on screen. |
| 249 |
*/ |
| 250 |
bool AlphaWeighted = true; |
| 251 |
|
| 252 |
bool operator!=(const BlendEquation& other) const { |
| 253 |
return (A != other.A || B != other.B || C != other.C || D != other.D || |
| 254 |
Fixed != other.Fixed || AlphaWeighted != other.AlphaWeighted); |
| 255 |
} |
| 256 |
}; |
| 257 |
|
| 258 |
/** @brief `Cv = Cs` - what "blending disabled" means, expressed as a GS equation */ |
| 259 |
constexpr BlendEquation OpaqueEquation() |
| 260 |
{ |
| 261 |
// (Cs - 0) * 0x80 >> 7 + 0, which is Cs exactly |
| 262 |
return { BLEND_COLOR_SOURCE, BLEND_COLOR_ZERO, BLEND_ALPHA_FIXED, BLEND_COLOR_ZERO, 0x80, false }; |
| 263 |
} |
| 264 |
|
| 265 |
/** @brief `Cv = Cs*As + Cd*(1-As)` - ordinary source-over, and the fallback for anything unmappable */ |
| 266 |
constexpr BlendEquation SourceOverEquation() |
| 267 |
{ |
| 268 |
return { BLEND_COLOR_SOURCE, BLEND_COLOR_DEST, BLEND_ALPHA_SOURCE, BLEND_COLOR_DEST, 0x80, true }; |
| 269 |
} |
| 270 |
|
| 271 |
constexpr std::uint32_t BlendKey(nCine::BlendingFactor src, nCine::BlendingFactor dst) |
| 272 |
{ |
| 273 |
return (std::uint32_t(src) << 16) | std::uint32_t(dst); |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
@brief Maps a GL-style (source, destination) factor pair onto @ref BlendEquation |
| 278 |
|
| 279 |
Returns `false` when the pair has no GS form, in which case @p out is left holding source-over - |
| 280 |
the same "fall back to blend_mix" answer `Material::SetShader()` gives for the blend equations |
| 281 |
materials cannot express either. |
| 282 |
*/ |
| 283 |
bool ResolveBlendEquation(nCine::BlendingFactor src, nCine::BlendingFactor dst, BlendEquation& out) |
| 284 |
{ |
| 285 |
out = SourceOverEquation(); |
| 286 |
switch (BlendKey(src, dst)) { |
| 287 |
case BlendKey(nCine::BlendingFactor::One, nCine::BlendingFactor::Zero): |
| 288 |
out = OpaqueEquation(); |
| 289 |
return true; |
| 290 |
case BlendKey(nCine::BlendingFactor::SrcAlpha, nCine::BlendingFactor::OneMinusSrcAlpha): |
| 291 |
// Cs*As + Cd*(1-As) |
| 292 |
return true; |
| 293 |
case BlendKey(nCine::BlendingFactor::SrcAlpha, nCine::BlendingFactor::One): |
| 294 |
// Cs*As + Cd - additive, which the weapons, the shields and the lighting pass all ask for |
| 295 |
out = { BLEND_COLOR_SOURCE, BLEND_COLOR_ZERO, BLEND_ALPHA_SOURCE, BLEND_COLOR_DEST, 0x80, true }; |
| 296 |
return true; |
| 297 |
case BlendKey(nCine::BlendingFactor::One, nCine::BlendingFactor::One): |
| 298 |
// Cs + Cd |
| 299 |
out = { BLEND_COLOR_SOURCE, BLEND_COLOR_ZERO, BLEND_ALPHA_FIXED, BLEND_COLOR_DEST, 0x80, false }; |
| 300 |
return true; |
| 301 |
case BlendKey(nCine::BlendingFactor::Zero, nCine::BlendingFactor::One): |
| 302 |
// Cd - the destination is not touched at all |
| 303 |
out = { BLEND_COLOR_ZERO, BLEND_COLOR_ZERO, BLEND_ALPHA_FIXED, BLEND_COLOR_DEST, 0x00, true }; |
| 304 |
return true; |
| 305 |
case BlendKey(nCine::BlendingFactor::Zero, nCine::BlendingFactor::OneMinusSrcAlpha): |
| 306 |
// Cd*(1-As), as (0 - Cd)*As + Cd |
| 307 |
out = { BLEND_COLOR_ZERO, BLEND_COLOR_DEST, BLEND_ALPHA_SOURCE, BLEND_COLOR_DEST, 0x80, true }; |
| 308 |
return true; |
| 309 |
case BlendKey(nCine::BlendingFactor::OneMinusSrcAlpha, nCine::BlendingFactor::SrcAlpha): |
| 310 |
// Cs*(1-As) + Cd*As, as (Cd - Cs)*As + Cs |
| 311 |
out = { BLEND_COLOR_DEST, BLEND_COLOR_SOURCE, BLEND_ALPHA_SOURCE, BLEND_COLOR_SOURCE, 0x80, true }; |
| 312 |
return true; |
| 313 |
case BlendKey(nCine::BlendingFactor::One, nCine::BlendingFactor::OneMinusSrcAlpha): |
| 314 |
// Premultiplied alpha wants Cs + Cd*(1-As), and C multiplies (A - B) as one term, so the |
| 315 |
// GS cannot separate the two factors. Source-over is the closest it has: it agrees exactly |
| 316 |
// at both ends of the range (an opaque texel gives Cs, a clear one gives Cd) and only |
| 317 |
// under-weights the source in between, where additive - the other candidate - is wrong at |
| 318 |
// the opaque end, which is the end that shows. |
| 319 |
return true; |
| 320 |
default: |
| 321 |
// Everything left needs a factor the GS has no slot for: the colour-product modes |
| 322 |
// (Multiply's DstColor, SrcColor and their complements) and the constant-colour modes |
| 323 |
return false; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** @brief The equation the material's own blending state resolves to */ |
| 328 |
BlendEquation MaterialEquation(const GsDevice::BlendingState& blending) |
| 329 |
{ |
| 330 |
BlendEquation equation; |
| 331 |
if (!blending.Enabled) { |
| 332 |
// Blending disabled means `Cv = Cs` whatever factors were last recorded, exactly as it does |
| 333 |
// in GL - and as the PowerVR backend spells out by mapping to ONE/ZERO |
| 334 |
return OpaqueEquation(); |
| 335 |
} |
| 336 |
if (!ResolveBlendEquation(blending.SrcRgb, blending.DstRgb, equation)) { |
| 337 |
static bool warnedUnsupported = false; |
| 338 |
if (!warnedUnsupported) { |
| 339 |
warnedUnsupported = true; |
| 340 |
LOGW("Blending factors 0x{:x}/0x{:x} have no Graphics Synthesizer equation, falling back to source-over", |
| 341 |
std::uint32_t(blending.SrcRgb), std::uint32_t(blending.DstRgb)); |
| 342 |
} |
| 343 |
} |
| 344 |
// The alpha blending factors are per-RGB only: the GS writes the fragment's own alpha to the |
| 345 |
// buffer, so `SrcAlpha`/`DstAlpha` of a four-factor call have no register to go to (and the |
| 346 |
// display buffer is PSMCT16, whose single alpha bit FBA pins to 1 regardless) |
| 347 |
return equation; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
@brief The equation one pass draws under, given the equation its material resolved to |
| 352 |
|
| 353 |
A `FixedFunctionPass::BlendMode` is an override of the material's blending, so `Material` is the |
| 354 |
identity here and the other three name equations of their own. |
| 355 |
*/ |
| 356 |
BlendEquation PassEquation(const BlendEquation& material, FixedFunctionPass::BlendMode mode) |
| 357 |
{ |
| 358 |
switch (mode) { |
| 359 |
case FixedFunctionPass::BlendMode::Additive: |
| 360 |
// `Cs*As + Cd`. Deliberately SRCALPHA + ONE rather than ONE + ONE, which is the mapping |
| 361 |
// FixedFunctionPass documents for the split-multiplier passes: their contributions are |
| 362 |
// scaled by the pass alpha, so this stays bit-identical with the PVR's. |
| 363 |
return { BLEND_COLOR_SOURCE, BLEND_COLOR_ZERO, BLEND_ALPHA_SOURCE, BLEND_COLOR_DEST, 0x80, true }; |
| 364 |
case FixedFunctionPass::BlendMode::Opaque: |
| 365 |
return OpaqueEquation(); |
| 366 |
case FixedFunctionPass::BlendMode::Alpha: |
| 367 |
return SourceOverEquation(); |
| 368 |
default: |
| 369 |
return material; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
/** @brief The equation currently programmed into `ALPHA`/`TEST`, so unchanged state costs nothing */ |
| 374 |
BlendEquation _appliedBlend; |
| 375 |
bool _appliedBlendValid = false; |
| 376 |
|
| 377 |
/** |
| 378 |
@brief Programs `ALPHA` and `TEST` for @p equation if they are not already programmed for it |
| 379 |
|
| 380 |
Both registers are part of the GS's context state and survive across packets, so this only ever |
| 381 |
writes when the mode actually changes - which for a frame of this game is a handful of times. |
| 382 |
*/ |
| 383 |
void ApplyBlendEquation(const BlendEquation& equation) |
| 384 |
{ |
| 385 |
if (_appliedBlendValid && !(_appliedBlend != equation)) { |
| 386 |
return; |
| 387 |
} |
| 388 |
_appliedBlend = equation; |
| 389 |
_appliedBlendValid = true; |
| 390 |
|
| 391 |
qword_t* q = Reserve(16); |
| 392 |
|
| 393 |
blend_t blend; |
| 394 |
blend.color1 = char(equation.A); |
| 395 |
blend.color2 = char(equation.B); |
| 396 |
blend.alpha = char(equation.C); |
| 397 |
blend.color3 = char(equation.D); |
| 398 |
blend.fixed_alpha = equation.Fixed; |
| 399 |
q = draw_alpha_blending(q, 0, &blend); |
| 400 |
|
| 401 |
// Alpha test as a cutout. It is worth having because the GS is fill-rate bound long before it is |
| 402 |
// anything else and this game's sheets are mostly empty space, but it is only equivalent while an |
| 403 |
// alpha-zero source blends to the destination - so the modes that write the source unconditionally |
| 404 |
// turn it off instead of quietly losing their transparent texels. |
| 405 |
atest_t atest; |
| 406 |
atest.enable = (equation.AlphaWeighted ? DRAW_ENABLE : DRAW_DISABLE); |
| 407 |
atest.method = ATEST_METHOD_NOTEQUAL; |
| 408 |
atest.compval = 0x00; |
| 409 |
// Reads backwards: PS2SDK names the `AFAIL` values after what a FAILING fragment leaves alone, so |
| 410 |
// "keep the framebuffer" is the one that discards the fragment |
| 411 |
atest.keep = ATEST_KEEP_FRAMEBUFFER; |
| 412 |
dtest_t dtest; |
| 413 |
dtest.enable = DRAW_DISABLE; |
| 414 |
dtest.pass = DRAW_DISABLE; |
| 415 |
// There is no Z buffer here (see InitializeGs), but ZTE = 0 is prohibited by the hardware, so the |
| 416 |
// test stays enabled and passes everything - which is also what draw_setup_environment() programs |
| 417 |
ztest_t ztest; |
| 418 |
ztest.enable = DRAW_ENABLE; |
| 419 |
ztest.method = ZTEST_METHOD_ALLPASS; |
| 420 |
q = draw_pixel_test(q, 0, &atest, &dtest, &ztest); |
| 421 |
|
| 422 |
_packetCursor = q; |
| 423 |
} |
| 424 |
|
| 425 |
// ------------------------------------------------------------ texture state and primitives |
| 426 |
|
| 427 |
/** |
| 428 |
@brief Everything one primitive needs from `TEX0`/`TEX1`/`CLAMP`, resolved per instance |
| 429 |
|
| 430 |
The fixed-function effects submit passes over a quad whose *texture* state is settled before the |
| 431 |
effect runs (page residency, the palette row's CLUT, the sampled extent), and a pass may swap |
| 432 |
part of it - a silhouette pass samples the same store's coverage instead of its colour. Bundling |
| 433 |
it makes that swap a copy-and-edit of a value rather than a second parameter list. |
| 434 |
*/ |
| 435 |
struct DrawState |
| 436 |
{ |
| 437 |
/** @brief Store page, or @ref GsVram::InvalidPage for an untextured primitive */ |
| 438 |
std::uint32_t Page = GsVram::InvalidPage; |
| 439 |
/** @brief `TEX0.CBP`, or @ref GsVram::InvalidBlock when the mode needs no CLUT */ |
| 440 |
std::uint32_t ClutBlock = GsVram::InvalidBlock; |
| 441 |
/** @brief Store pitch in TEXELS (libdraw divides by 64 itself) */ |
| 442 |
std::int32_t BufferPitch = 0; |
| 443 |
/** @brief Hardware `PSM` the store is sampled with */ |
| 444 |
std::int32_t Psm = GS_PSM_32; |
| 445 |
/** @brief `TEX0.TW`/`TH` extents (the padded power of two) */ |
| 446 |
std::int32_t SampledWidth = 8, SampledHeight = 8; |
| 447 |
/** @brief `CLAMP.WMS`/`WMT` */ |
| 448 |
std::int32_t WrapU = WRAP_CLAMP, WrapV = WRAP_CLAMP; |
| 449 |
/** @brief `TEX1.MMAG` (1 bit) and `TEX1.MMIN` (3 bits - different encodings, hence two mappers) */ |
| 450 |
std::int32_t MagFilter = LOD_MAG_NEAREST, MinFilter = LOD_MIN_NEAREST; |
| 451 |
|
| 452 |
bool operator!=(const DrawState& other) const { |
| 453 |
return (Page != other.Page || ClutBlock != other.ClutBlock || BufferPitch != other.BufferPitch || |
| 454 |
Psm != other.Psm || SampledWidth != other.SampledWidth || SampledHeight != other.SampledHeight || |
| 455 |
WrapU != other.WrapU || WrapV != other.WrapV || |
| 456 |
MagFilter != other.MagFilter || MinFilter != other.MinFilter); |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
/** |
| 461 |
@brief A pass colour packed on whichever of the two scales the primitives of @p state read |
| 462 |
|
| 463 |
The choice is exactly the one @ref SubmitVertexPrimitive() makes for `PRIM.TME`, and it is made |
| 464 |
from the same field, so a pass cannot be packed for a texture function the primitive does not run |
| 465 |
(see @ref PackVertexColor()). |
| 466 |
*/ |
| 467 |
color_t PackStateColor(const DrawState& state, const float* rgba) |
| 468 |
{ |
| 469 |
return (state.Page == GsVram::InvalidPage ? PackVertexColor(rgba) : PackPassColor(rgba)); |
| 470 |
} |
| 471 |
|
| 472 |
DrawState _appliedTexture; |
| 473 |
bool _appliedTextureValid = false; |
| 474 |
|
| 475 |
/** @brief `SamplerWrapping` as the `CLAMP` register spells it */ |
| 476 |
std::int32_t MapWrapGs(SamplerWrapping wrap) |
| 477 |
{ |
| 478 |
return (wrap == SamplerWrapping::Repeat || wrap == SamplerWrapping::MirroredRepeat |
| 479 |
? WRAP_REPEAT : WRAP_CLAMP); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
@brief Whether @p filter asks for interpolation rather than point sampling |
| 484 |
|
| 485 |
The GS does have bilinear sampling, and it interpolates the CLUT'ed colours rather than the |
| 486 |
indices, so it is usable on the indexed stores too. The mipmapped variants degrade to their |
| 487 |
non-mipmapped base: they need a `MIPTBP1`/`MIPTBP2` level chain this backend never builds, and |
| 488 |
nothing in a 2D game asks for one. |
| 489 |
*/ |
| 490 |
bool IsLinearFilter(nCine::SamplerFilter filter) |
| 491 |
{ |
| 492 |
return (filter == nCine::SamplerFilter::Linear || |
| 493 |
filter == nCine::SamplerFilter::LinearMipmapNearest || |
| 494 |
filter == nCine::SamplerFilter::LinearMipmapLinear); |
| 495 |
} |
| 496 |
// MMAG and MMIN are separate fields with separate encodings, so they get separate mappers rather than |
| 497 |
// one shared "filter" value that happens to agree for 0 and 1 |
| 498 |
std::int32_t MapMagFilterGs(nCine::SamplerFilter filter) |
| 499 |
{ |
| 500 |
return (IsLinearFilter(filter) ? LOD_MAG_LINEAR : LOD_MAG_NEAREST); |
| 501 |
} |
| 502 |
std::int32_t MapMinFilterGs(nCine::SamplerFilter filter) |
| 503 |
{ |
| 504 |
return (IsLinearFilter(filter) ? LOD_MIN_LINEAR : LOD_MIN_NEAREST); |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
@brief Programs `TEX0` and `CLAMP` for @p state |
| 509 |
|
| 510 |
Unchanged state is skipped, CLUT or not. Getting this wrong is expensive in both directions and it |
| 511 |
has been wrong both ways: |
| 512 |
|
| 513 |
- Skipping on the block address ALONE is not safe. The slab reuses slots, so the same `CBP` can |
| 514 |
hold different palette contents from one draw to the next, and `TEX0` with `CLD = 1` is the only |
| 515 |
thing that reloads the GS's internal CLUT buffer - so a skipped write leaves it sampling the |
| 516 |
previous palette, which is what turned the intro and the glow flat. |
| 517 |
- Restating unconditionally, which is what that was fixed with, costs far more than it looks. The |
| 518 |
write is two qwords, but `CLD = 1` makes the GS re-read a whole kilobyte of local memory into |
| 519 |
its CLUT buffer, and writing `TEX0` at all invalidates its texture cache. Doing that once per |
| 520 |
SPRITE - and on this content nearly every sprite is indexed - is a thousand CLUT reloads and a |
| 521 |
thousand texture-cache refills a frame, which is fill rate this hardware cannot spare. |
| 522 |
|
| 523 |
The answer is to keep the state cache and invalidate it where the hazard actually is: |
| 524 |
@ref GsDevice::AcquireClut() clears it whenever it overwrites a slot, which is the only way the |
| 525 |
contents behind an unchanged `CBP` can change. Everything else - a different row, a different |
| 526 |
store, an untextured draw in between - already differs in @ref DrawState and restates on its own. |
| 527 |
*/ |
| 528 |
void ApplyTexture(const DrawState& state) |
| 529 |
{ |
| 530 |
if (state.Page == GsVram::InvalidPage) { |
| 531 |
return; // Untextured primitive - TEX0 is not read at all |
| 532 |
} |
| 533 |
if (_appliedTextureValid && !(_appliedTexture != state)) { |
| 534 |
return; |
| 535 |
} |
| 536 |
const bool hasClut = (state.ClutBlock != GsVram::InvalidBlock); |
| 537 |
_appliedTexture = state; |
| 538 |
_appliedTextureValid = true; |
| 539 |
|
| 540 |
texbuffer_t texbuf; |
| 541 |
texbuf.address = state.Page * WordsPerPage; |
| 542 |
texbuf.width = state.BufferPitch; |
| 543 |
texbuf.psm = state.Psm; |
| 544 |
texbuf.info.width = draw_log2(state.SampledWidth); |
| 545 |
texbuf.info.height = draw_log2(state.SampledHeight); |
| 546 |
texbuf.info.components = TEXTURE_COMPONENTS_RGBA; |
| 547 |
texbuf.info.function = TEXTURE_FUNCTION_MODULATE; |
| 548 |
|
| 549 |
clutbuffer_t clutbuf; |
| 550 |
clutbuf.address = (hasClut ? state.ClutBlock * WordsPerBlock : 0); |
| 551 |
clutbuf.psm = GS_PSM_32; |
| 552 |
clutbuf.storage_mode = CLUT_STORAGE_MODE1; |
| 553 |
clutbuf.start = 0; |
| 554 |
// CLUT_LOAD forces the palette to be re-read rather than trusting the GS's CLUT cache |
| 555 |
clutbuf.load_method = (hasClut ? CLUT_LOAD : CLUT_NO_LOAD); |
| 556 |
|
| 557 |
texwrap_t wrap; |
| 558 |
wrap.horizontal = state.WrapU; |
| 559 |
wrap.vertical = state.WrapV; |
| 560 |
wrap.minu = wrap.maxu = 0; |
| 561 |
wrap.minv = wrap.maxv = 0; |
| 562 |
|
| 563 |
// TEX1 selects the sampling filter. `LOD_USE_K` with K = 0 and L = 0 keeps the level fixed at the |
| 564 |
// base texture - there is no mip chain here - so MMIN and MMAG both act as the plain filter choice. |
| 565 |
lod_t lod; |
| 566 |
lod.calculation = LOD_USE_K; |
| 567 |
lod.max_level = 0; |
| 568 |
lod.mag_filter = state.MagFilter; |
| 569 |
lod.min_filter = state.MinFilter; |
| 570 |
lod.mipmap_select = LOD_MIPMAP_REGISTER; |
| 571 |
lod.l = 0; |
| 572 |
lod.k = 0.0f; |
| 573 |
|
| 574 |
qword_t* q = Reserve(24); |
| 575 |
q = draw_texturebuffer(q, 0, &texbuf, &clutbuf); |
| 576 |
q = draw_texture_wrapping(q, 0, &wrap); |
| 577 |
q = draw_texture_sampling(q, 0, &lod); |
| 578 |
_packetCursor = q; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
@brief Largest texel coordinate the `UV` register can carry |
| 583 |
|
| 584 |
`UV` holds U and V as 14-bit 12.4 fixed point, so a texel coordinate above this wraps around |
| 585 |
silently. A repeating texture reaches it easily: the legacy menu background asks for 96 repeats of |
| 586 |
its sheet, which is 1536 texels across - and a wrapped coordinate there reads as a zoomed |
| 587 |
background rather than as a glitch. Those primitives switch to `ST` instead (see |
| 588 |
@ref SubmitVertexPrimitive()), whose coordinates are IEEE floats normalized to the sampled extent |
| 589 |
and therefore effectively unbounded. |
| 590 |
*/ |
| 591 |
constexpr float MaxUvTexel = 1023.0f; |
| 592 |
|
| 593 |
/** @brief Whether every texel coordinate a primitive needs fits the `UV` register */ |
| 594 |
bool FitsUvRegister(const float* tu, const float* tv, std::int32_t count, |
| 595 |
float sampledWidth, float sampledHeight) |
| 596 |
{ |
| 597 |
for (std::int32_t i = 0; i < count; i++) { |
| 598 |
if (tu[i] < 0.0f || tv[i] < 0.0f || |
| 599 |
tu[i] * sampledWidth > MaxUvTexel || tv[i] * sampledHeight > MaxUvTexel) { |
| 600 |
return false; |
| 601 |
} |
| 602 |
} |
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
// Forward-declared because a rotated quad falls through to it (see below), and it is the longer of the |
| 607 |
// two so it reads better after the common case |
| 608 |
void SubmitVertexPrimitive(std::int32_t primType, const DrawState& state, const float* sx, const float* sy, |
| 609 |
const float* tu, const float* tv, const std::uint64_t* colors, const color_t& flat, |
| 610 |
std::int32_t count, float offsetX, float offsetY); |
| 611 |
|
| 612 |
/** @brief A triangle strip out of a vertex list (see @ref SubmitVertexPrimitive()) */ |
| 613 |
inline void SubmitStripPrimitive(const DrawState& state, const float* sx, const float* sy, |
| 614 |
const float* tu, const float* tv, const std::uint64_t* colors, const color_t& flat, |
| 615 |
std::int32_t count, float offsetX, float offsetY) |
| 616 |
{ |
| 617 |
SubmitVertexPrimitive(PRIM_TRIANGLE_STRIP, state, sx, sy, tu, tv, colors, flat, count, offsetX, offsetY); |
| 618 |
} |
| 619 |
|
| 620 |
/** @brief A connected line strip out of a vertex list (the weapon wheel) */ |
| 621 |
inline void SubmitLineStripPrimitive(const DrawState& state, const float* sx, const float* sy, |
| 622 |
const float* tu, const float* tv, const color_t& flat, std::int32_t count) |
| 623 |
{ |
| 624 |
SubmitVertexPrimitive(PRIM_LINE_STRIP, state, sx, sy, tu, tv, nullptr, flat, count, 0.0f, 0.0f); |
| 625 |
} |
| 626 |
|
| 627 |
/** @brief An independent triangle list out of a vertex list (the tile mesh's odd primitives) */ |
| 628 |
inline void SubmitTrianglePrimitive(const DrawState& state, const float* sx, const float* sy, |
| 629 |
const float* tu, const float* tv, const color_t& flat, float offsetX, float offsetY) |
| 630 |
{ |
| 631 |
SubmitVertexPrimitive(PRIM_TRIANGLE, state, sx, sy, tu, tv, nullptr, flat, 3, offsetX, offsetY); |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
@brief One quad, textured or not, displaced by a pass's screen offset |
| 636 |
|
| 637 |
@p px / @p py are the four synthesized corners and @p pu / @p pv their NORMALIZED texture |
| 638 |
coordinates - the sampled extent is folded in here, so a caller never has to know it. |
| 639 |
*/ |
| 640 |
void SubmitQuadPrimitive(const DrawState& state, const float* px, const float* py, |
| 641 |
const float* pu, const float* pv, const color_t& color, float offsetX, float offsetY, |
| 642 |
bool axisAligned) |
| 643 |
{ |
| 644 |
if (!axisAligned) { |
| 645 |
// A rotated quad is not a GS SPRITE - that primitive takes two opposite corners and is |
| 646 |
// axis-aligned by construction - so it goes out as the two triangles of a strip instead. The |
| 647 |
// corner array is indexed by (ax, ay) weights: 0 = (1,0), 1 = (1,1), 2 = (0,0), 3 = (0,1), and |
| 648 |
// a strip wants them in scan order (0,0) (1,0) (0,1) (1,1) = 2, 0, 3, 1. |
| 649 |
static const std::int32_t StripOrder[4] = { 2, 0, 3, 1 }; |
| 650 |
float sx[4], sy[4], tu[4], tv[4]; |
| 651 |
for (std::int32_t i = 0; i < 4; i++) { |
| 652 |
const std::int32_t c = StripOrder[i]; |
| 653 |
sx[i] = px[c]; |
| 654 |
sy[i] = py[c]; |
| 655 |
tu[i] = pu[c]; |
| 656 |
tv[i] = pv[c]; |
| 657 |
} |
| 658 |
SubmitStripPrimitive(state, sx, sy, tu, tv, nullptr, color, 4, offsetX, offsetY); |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
if (state.Page == GsVram::InvalidPage) { |
| 663 |
qword_t* q = Reserve(8); |
| 664 |
rect_t rect; |
| 665 |
rect.v0.x = px[2] + offsetX; |
| 666 |
rect.v0.y = py[0] + offsetY; |
| 667 |
rect.v0.z = 0; |
| 668 |
rect.v1.x = px[0] + offsetX; |
| 669 |
rect.v1.y = py[1] + offsetY; |
| 670 |
rect.v1.z = 0; |
| 671 |
rect.color = color; |
| 672 |
q = draw_rect_filled(q, 0, &rect); |
| 673 |
_packetCursor = q; |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
// draw_rect_textured() writes the UV register, so a coordinate that does not fit it has to take the |
| 678 |
// strip path instead - which can fall back to ST. Recursing with axisAligned = false is the whole |
| 679 |
// change of plan: the geometry is identical, only the primitive and the coordinate register differ. |
| 680 |
if (!FitsUvRegister(pu, pv, 4, float(state.SampledWidth), float(state.SampledHeight))) { |
| 681 |
SubmitQuadPrimitive(state, px, py, pu, pv, color, offsetX, offsetY, false); |
| 682 |
return; |
| 683 |
} |
| 684 |
|
| 685 |
ApplyTexture(state); |
| 686 |
qword_t* q = Reserve(8); |
| 687 |
texrect_t rect; |
| 688 |
rect.v0.x = px[2] + offsetX; |
| 689 |
rect.v0.y = py[0] + offsetY; |
| 690 |
rect.v0.z = 0; |
| 691 |
rect.v1.x = px[0] + offsetX; |
| 692 |
rect.v1.y = py[1] + offsetY; |
| 693 |
rect.v1.z = 0; |
| 694 |
// UVs arrive normalized; the GS addresses texels, so they scale by the sampled extent |
| 695 |
rect.t0.u = pu[2] * float(state.SampledWidth); |
| 696 |
rect.t0.v = pv[0] * float(state.SampledHeight); |
| 697 |
rect.t1.u = pu[0] * float(state.SampledWidth); |
| 698 |
rect.t1.v = pv[1] * float(state.SampledHeight); |
| 699 |
rect.color = color; |
| 700 |
q = draw_rect_textured(q, 0, &rect); |
| 701 |
_packetCursor = q; |
| 702 |
} |
| 703 |
|
| 704 |
/** |
| 705 |
@brief A vertex list of @p count vertices as one GS primitive of @p primType |
| 706 |
|
| 707 |
@p colors is per-vertex when non-null (a shaded strip - the only way to draw a gradient without a |
| 708 |
fragment shader) and @p flat is the uniform colour otherwise. Positions are in raster space and |
| 709 |
@p tu / @p tv NORMALIZED to the sampled extent; both are null for an untextured primitive. |
| 710 |
|
| 711 |
Texture coordinates go out through `UV` when they fit its 14-bit fixed-point field and through |
| 712 |
`ST` - IEEE floats, divided by the `Q` of `RGBAQ`, which every colour this backend packs sets to |
| 713 |
1.0 - when they do not. `UV` is the cheaper of the two to interpolate and is what the validated |
| 714 |
quad path uses, so the choice is made per primitive rather than once. |
| 715 |
|
| 716 |
Emitted as one packed GIF list rather than through a libdraw helper, because `libdraw` has no |
| 717 |
primitive that takes UVs and per-vertex colours together - `draw_prim_start`/`_end` build the whole |
| 718 |
vertex format from a `prim_t`, which is the same PRMODE juggling the quad paths avoid. |
| 719 |
*/ |
| 720 |
void SubmitVertexPrimitive(std::int32_t primType, const DrawState& state, const float* sx, const float* sy, |
| 721 |
const float* tu, const float* tv, const std::uint64_t* colors, const color_t& flat, |
| 722 |
std::int32_t count, float offsetX, float offsetY) |
| 723 |
{ |
| 724 |
// A line strip is the one primitive here that means something with two vertices |
| 725 |
const std::int32_t minVertices = (primType == PRIM_LINE_STRIP || primType == PRIM_LINE ? 2 : 3); |
| 726 |
if (count < minVertices) { |
| 727 |
return; |
| 728 |
} |
| 729 |
const bool textured = (state.Page != GsVram::InvalidPage && tu != nullptr && tv != nullptr); |
| 730 |
if (textured) { |
| 731 |
ApplyTexture(state); |
| 732 |
} |
| 733 |
|
| 734 |
// Three qwords of header - the A+D GIF tag, the PRIM it carries, and the REGLIST tag - and then a |
| 735 |
// strip vertex is RGBAQ + (UV) + XYZ2, one register-list qword per PAIR of registers. Reserving |
| 736 |
// two for the header was one short, so a primitive submitted with the scratch nearly full wrote |
| 737 |
// its last qword past the end of it |
| 738 |
const std::int32_t regsPerVertex = (textured ? 3 : 2); |
| 739 |
qword_t* q = Reserve(std::size_t(3 + (count * regsPerVertex + 1) / 2)); |
| 740 |
|
| 741 |
// PRIM, then a REGLIST of `regsPerVertex` registers repeated once per vertex. ABE comes from the |
| 742 |
// same library-wide blending flag draw_enable_blending() set, so a strip blends like a quad. |
| 743 |
const std::int32_t tme = (textured ? 1 : 0); |
| 744 |
const bool useUv = (!textured || |
| 745 |
FitsUvRegister(tu, tv, count, float(state.SampledWidth), float(state.SampledHeight))); |
| 746 |
q->dw[0] = GIF_SET_TAG(1, 0, 0, 0, GIF_FLG_PACKED, 1); |
| 747 |
q->dw[1] = GIF_REG_AD; |
| 748 |
q++; |
| 749 |
q->dw[0] = GS_SET_PRIM(primType, 1 /*IIP: interpolate the vertex colours*/, tme, 0, |
| 750 |
1 /*ABE*/, 0, (useUv ? PRIM_MAP_UV : PRIM_MAP_ST), 0, 0); |
| 751 |
q->dw[1] = GS_REG_PRIM; |
| 752 |
q++; |
| 753 |
|
| 754 |
std::uint64_t regList = (std::uint64_t(GIF_REG_RGBAQ) << 0); |
| 755 |
if (textured) { |
| 756 |
regList |= (std::uint64_t(useUv ? GIF_REG_UV : GIF_REG_ST) << 4) | (std::uint64_t(GIF_REG_XYZ2) << 8); |
| 757 |
} else { |
| 758 |
regList |= (std::uint64_t(GIF_REG_XYZ2) << 4); |
| 759 |
} |
| 760 |
q->dw[0] = GIF_SET_TAG(count, 0, 0, 0, GIF_FLG_REGLIST, regsPerVertex); |
| 761 |
q->dw[1] = regList; |
| 762 |
q++; |
| 763 |
|
| 764 |
std::uint64_t* dw = reinterpret_cast<std::uint64_t*>(q); |
| 765 |
for (std::int32_t i = 0; i < count; i++) { |
| 766 |
*dw++ = (colors != nullptr ? colors[i] : flat.rgbaq); |
| 767 |
if (textured) { |
| 768 |
if (useUv) { |
| 769 |
*dw++ = GIF_SET_UV(ftoi4(tu[i] * float(state.SampledWidth)), |
| 770 |
ftoi4(tv[i] * float(state.SampledHeight))); |
| 771 |
} else { |
| 772 |
// ST takes the float BIT PATTERNS, and it is already normalized to the sampled extent |
| 773 |
std::uint32_t s, tt; |
| 774 |
std::memcpy(&s, &tu[i], sizeof(s)); |
| 775 |
std::memcpy(&tt, &tv[i], sizeof(tt)); |
| 776 |
*dw++ = GIF_SET_ST(s, tt); |
| 777 |
} |
| 778 |
} |
| 779 |
// The 2048 bias is the primitive-space origin draw_setup_environment() put XYOFFSET at |
| 780 |
*dw++ = GIF_SET_XYZ(ftoi4(sx[i] + offsetX + 2048.0f), ftoi4(sy[i] + offsetY + 2048.0f), 0); |
| 781 |
} |
| 782 |
// A REGLIST payload is qword-granular, so an odd register count needs the last half filled |
| 783 |
if ((count * regsPerVertex) & 1) { |
| 784 |
*dw++ = 0; |
| 785 |
} |
| 786 |
_packetCursor = reinterpret_cast<qword_t*>(dw); |
| 787 |
} |
| 788 |
|
| 789 |
/** @brief The scissor window currently programmed, in the inclusive form the register takes */ |
| 790 |
std::int32_t _appliedScissor[4] = {}; |
| 791 |
bool _appliedScissorValid = false; |
| 792 |
|
| 793 |
/** |
| 794 |
@brief Programs `SCISSOR` for the inclusive window [@p x0, @p x1] x [@p y0, @p y1] |
| 795 |
|
| 796 |
Every draw and every clear goes through this, including the ones that do not scissor at all - |
| 797 |
those pass their whole target. Leaving the register alone when the test is off was wrong in both |
| 798 |
directions: the rectangle of the last scissored pass stayed active for everything after it, |
| 799 |
the next frame's clear included, and a render-target pass inherited the display's rectangle. |
| 800 |
*/ |
| 801 |
void ApplyScissorArea(std::int32_t x0, std::int32_t x1, std::int32_t y0, std::int32_t y1) |
| 802 |
{ |
| 803 |
// The register fields are 11-bit unsigned, so an out-of-range edge would wrap rather than clip. |
| 804 |
// A degenerate rectangle collapses to a single row/column rather than to nothing, which no caller |
| 805 |
// in the engine produces. |
| 806 |
x0 = (x0 < 0 ? 0 : (x0 > 2047 ? 2047 : x0)); |
| 807 |
y0 = (y0 < 0 ? 0 : (y0 > 2047 ? 2047 : y0)); |
| 808 |
x1 = (x1 < x0 ? x0 : (x1 > 2047 ? 2047 : x1)); |
| 809 |
y1 = (y1 < y0 ? y0 : (y1 > 2047 ? 2047 : y1)); |
| 810 |
|
| 811 |
if (_appliedScissorValid && _appliedScissor[0] == x0 && _appliedScissor[1] == x1 && |
| 812 |
_appliedScissor[2] == y0 && _appliedScissor[3] == y1) { |
| 813 |
return; |
| 814 |
} |
| 815 |
_appliedScissor[0] = x0; _appliedScissor[1] = x1; |
| 816 |
_appliedScissor[2] = y0; _appliedScissor[3] = y1; |
| 817 |
_appliedScissorValid = true; |
| 818 |
|
| 819 |
qword_t* q = Reserve(8); |
| 820 |
q = draw_scissor_area(q, 0, x0, x1, y0, y1); |
| 821 |
_packetCursor = q; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
GsDevice::BlendingState GsDevice::_blending; |
| 826 |
GsDevice::DepthTestState GsDevice::_depthTest; |
| 827 |
GsDevice::CullFaceState GsDevice::_cullFace; |
| 828 |
GsDevice::ScissorState GsDevice::_scissor; |
| 829 |
Recti GsDevice::_viewport(0, 0, 0, 0); |
| 830 |
Colorf GsDevice::_clearColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 831 |
|
| 832 |
GsShaderProgram* GsDevice::_currentProgram = nullptr; |
| 833 |
const GsTexture* GsDevice::_boundTextures[GsDevice::MaxTextureUnits] = {}; |
| 834 |
GsDevice::UniformRange GsDevice::_boundUniformRanges[GsDevice::MaxUniformBindings] = {}; |
| 835 |
GsRenderTarget* GsDevice::_currentRenderTarget = nullptr; |
| 836 |
bool GsDevice::_renderTargetSurfaceMissing = false; |
| 837 |
|
| 838 |
bool GsDevice::_gsInitialized = false; |
| 839 |
std::int32_t GsDevice::_logicalWidth = DisplayWidth; |
| 840 |
std::int32_t GsDevice::_logicalHeight = DisplayHeight; |
| 841 |
std::int32_t GsDevice::_displayBufferIndex = 0; |
| 842 |
|
| 843 |
GsTexture* GsDevice::_paletteTexture = nullptr; |
| 844 |
std::uint32_t GsDevice::_paletteGeneration = 1; |
| 845 |
std::uint32_t GsDevice::_paletteRowStamp[GsDevice::MaxPaletteRows] = {}; |
| 846 |
GsDevice::ClutSlot GsDevice::_clutSlots[GsDevice::MaxClutSlots] = {}; |
| 847 |
std::uint32_t GsDevice::_clutUseCounter = 0; |
| 848 |
std::uint32_t GsDevice::_frameCounter = 0; |
| 849 |
|
| 850 |
std::vector<GsDevice::PendingSoftwareLight> GsDevice::_pendingSoftwareLights; |
| 851 |
|
| 852 |
// ------------------------------------------------------------------ session |
| 853 |
|
| 854 |
void GsDevice::InitializeGs() |
| 855 |
{ |
| 856 |
if (_gsInitialized) { |
| 857 |
return; |
| 858 |
} |
| 859 |
|
| 860 |
GsVramLayout layout; |
| 861 |
layout.DisplayWidth = DisplayWidth; |
| 862 |
layout.DisplayHeight = DisplayHeight; |
| 863 |
// Two 16-bit colour surfaces. Single-buffering was tried, to hand the cache the 70 pages (560 KB) a |
| 864 |
// second one costs - but the rasterizer and the CRT then share a buffer, and the CRT does not wait. |
| 865 |
// A frame is cleared and redrawn starting at the vertical blank while the beam is already scanning |
| 866 |
// the same memory, so whatever is drawn LAST only appears in the frames where the rasterizer beat the |
| 867 |
// beam to those rows. That is the HUD, every time, because it is drawn last - and it flashed on and |
| 868 |
// off frame to frame. (Not visible under an emulator, which presents a finished buffer at the vsync |
| 869 |
// and has no beam, which is why it survived.) The pages come back out of the CPU lightmap instead: |
| 870 |
// see CombineRenderer, where the PS2 now builds a quarter-resolution map like the other consoles. |
| 871 |
// |
| 872 |
// PSMCT32 would cost 1120 KB per buffer and leave the cache barely larger than a level's biggest |
| 873 |
// atlas, so 16-bit it stays; the game's art is indexed and dithers into it without banding. |
| 874 |
layout.DisplayPsm = GsPsm::Ct16; |
| 875 |
layout.DisplayBufferCount = 2; |
| 876 |
layout.ReservedRttPages = 16; |
| 877 |
layout.ClutSlotCount = MaxClutSlots; |
| 878 |
if (!GsVram::Initialize(layout)) { |
| 879 |
LOGE("The GS video-memory layout does not fit in local memory"); |
| 880 |
return; |
| 881 |
} |
| 882 |
|
| 883 |
dma_channel_initialize(DMA_CHANNEL_GIF, nullptr, 0); |
| 884 |
dma_channel_fast_waits(DMA_CHANNEL_GIF); |
| 885 |
|
| 886 |
// `_displayBufferIndex` is the buffer being RENDERED into, so the one handed to the read circuits |
| 887 |
// below is the other one - blank for the first frame, and swapped for good by PresentFrame() |
| 888 |
_displayBufferIndex = 0; |
| 889 |
_frame.address = GsVram::GetDisplayBufferPage(_displayBufferIndex) * WordsPerPage; |
| 890 |
_frame.width = DisplayWidth; |
| 891 |
_frame.height = DisplayHeight; |
| 892 |
_frame.psm = GS_PSM_16; |
| 893 |
_frame.mask = 0; |
| 894 |
|
| 895 |
// No Z buffer at all: the engine's queue is already in painter's order, so the test is ALWAYS with |
| 896 |
// writes masked and the pointer is never read or written. It can therefore alias the frame, which is |
| 897 |
// worth 560 KB of local memory - the single largest saving available in this layout. |
| 898 |
_zbuffer.enable = DRAW_DISABLE; |
| 899 |
_zbuffer.method = ZTEST_METHOD_ALLPASS; |
| 900 |
_zbuffer.address = _frame.address; |
| 901 |
_zbuffer.zsm = GS_PSMZ_16; |
| 902 |
_zbuffer.mask = 1; |
| 903 |
|
| 904 |
// The read circuits get the buffer NOT being rendered into. `graph_initialize` and |
| 905 |
// `graph_set_framebuffer_filtered` both take the address in the same 32-bit words `framebuffer_t` |
| 906 |
// uses (they shift it down by 11 for `DISPFB.FBP`), so the two agree with `draw_framebuffer` and no |
| 907 |
// unit conversion belongs between them. |
| 908 |
graph_initialize(std::int32_t(GsVram::GetDisplayBufferPage(_displayBufferIndex ^ 1) * WordsPerPage), |
| 909 |
DisplayWidth, DisplayHeight, _frame.psm, 0, 0); |
| 910 |
|
| 911 |
// PS2SDK's primitive helpers bake the ABE bit of the PRIM register they emit from a LIBRARY-WIDE |
| 912 |
// flag that starts out clear, so every `draw_rect_textured()` and `draw_rect_filled()` asks the |
| 913 |
// rasterizer for an unblended primitive until this is called. That is what made the picture read as |
| 914 |
// pure cutout - the ALPHA register below was programmed correctly all along and simply never |
| 915 |
// consulted, while the alpha test draw_setup_environment() leaves enabled discarded the fully |
| 916 |
// transparent texels. The two together are exactly "opaque or invisible, nothing in between". |
| 917 |
// |
| 918 |
// Enabling it here rather than overriding PRMODE per draw is deliberate: PRMODE applies to EVERY |
| 919 |
// primitive including the untextured strips draw_clear() emits, and forcing a textured/blended |
| 920 |
// PRMODE onto those made the clear sample whatever texture happened to be bound (a flat grey |
| 921 |
// screen). With the override left off, each helper's own PRIM governs and this flag is the one |
| 922 |
// knob that reaches it. |
| 923 |
draw_enable_blending(); |
| 924 |
|
| 925 |
qword_t* q = Reserve(96); |
| 926 |
// draw_setup_environment() already programs XYOFFSET for a screen-space origin at the top left. |
| 927 |
// Setting it again here was wrong: the extra call left the origin at the centre of the 4096x4096 |
| 928 |
// primitive space instead of the top-left of the display, which drew every sprite exactly half a |
| 929 |
// screen down and to the right while draw_clear() - which does not go through the same path - kept |
| 930 |
// covering the whole frame and hid the cause. |
| 931 |
// |
| 932 |
// It also leaves PRMODECONT selecting the PRIM register (`PRIM_OVERRIDE_DISABLE`), which is the |
| 933 |
// steady state the draw paths rely on. Nothing flips it afterwards: draw_clear() switches to PRMODE |
| 934 |
// for its own strips and switches back before returning. |
| 935 |
q = draw_setup_environment(q, 0, &_frame, &_zbuffer); |
| 936 |
|
| 937 |
// The ALPHA/TEST and SCISSOR registers are programmed from the requested state instead, per draw, by |
| 938 |
// ApplyBlending() and ApplyScissor() - what the environment left in them is not assumed to still hold |
| 939 |
_appliedBlendValid = false; |
| 940 |
_appliedScissorValid = false; |
| 941 |
_appliedTextureValid = false; |
| 942 |
|
| 943 |
q = draw_finish(q); |
| 944 |
_packetCursor = q; |
| 945 |
FlushPackets(); |
| 946 |
draw_wait_finish(); |
| 947 |
|
| 948 |
_gsInitialized = true; |
| 949 |
} |
| 950 |
|
| 951 |
void GsDevice::PresentFrame() |
| 952 |
{ |
| 953 |
if (!_gsInitialized) { |
| 954 |
return; |
| 955 |
} |
| 956 |
|
| 957 |
qword_t* q = Reserve(8); |
| 958 |
q = draw_finish(q); |
| 959 |
_packetCursor = q; |
| 960 |
FlushPackets(); |
| 961 |
draw_wait_finish(); |
| 962 |
|
| 963 |
graph_wait_vsync(); |
| 964 |
|
| 965 |
// The flip. The buffer just finished goes to the read circuits and the rasterizer moves to the other |
| 966 |
// one, so nothing is ever drawn into memory the CRT is scanning (see InitializeGs for what that cost |
| 967 |
// when there was only one). DISPFB is an EE register write rather than a GIF register, so it takes |
| 968 |
// effect immediately - which is why it is done here, after the GS is known to be finished and right |
| 969 |
// at the vertical blank, rather than queued in a packet. |
| 970 |
graph_set_framebuffer_filtered(std::int32_t(GsVram::GetDisplayBufferPage(_displayBufferIndex) * WordsPerPage), |
| 971 |
DisplayWidth, GS_PSM_16, 0, 0); |
| 972 |
|
| 973 |
_displayBufferIndex ^= 1; |
| 974 |
_frame.address = GsVram::GetDisplayBufferPage(_displayBufferIndex) * WordsPerPage; |
| 975 |
// FRAME has to follow the flip. Skipped while a render target owns the colour buffer - it restores |
| 976 |
// from `_frame` when it unbinds, which is now the new back buffer - though no frame ends that way. |
| 977 |
if (_currentRenderTarget == nullptr) { |
| 978 |
qword_t* qf = Reserve(32); |
| 979 |
qf = draw_framebuffer(qf, 0, &_frame); |
| 980 |
_packetCursor = qf; |
| 981 |
} |
| 982 |
|
| 983 |
_frameCounter++; |
| 984 |
} |
| 985 |
|
| 986 |
void GsDevice::ResizeScreenFramebuffer(std::int32_t width, std::int32_t height) |
| 987 |
{ |
| 988 |
if (width > 0 && height > 0) { |
| 989 |
_logicalWidth = width; |
| 990 |
_logicalHeight = height; |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
void GsDevice::GetTargetScale(float& scaleX, float& scaleY, float& offsetX, float& offsetY) |
| 995 |
{ |
| 996 |
offsetX = 0.0f; |
| 997 |
offsetY = 0.0f; |
| 998 |
if (_currentRenderTarget != nullptr) { |
| 999 |
// Render-to-texture passes render 1:1 into the target surface |
| 1000 |
scaleX = 1.0f; |
| 1001 |
scaleY = 1.0f; |
| 1002 |
} else { |
| 1003 |
scaleX = (_logicalWidth > 0 ? float(DisplayWidth) / float(_logicalWidth) : 1.0f); |
| 1004 |
scaleY = (_logicalHeight > 0 ? float(DisplayHeight) / float(_logicalHeight) : 1.0f); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
// ------------------------------------------------------------------ state |
| 1009 |
|
| 1010 |
void GsDevice::SetBlendingEnabled(bool enabled) { _blending.Enabled = enabled; } |
| 1011 |
void GsDevice::SetBlendingFactors(nCine::BlendingFactor srcRgb, nCine::BlendingFactor dstRgb, nCine::BlendingFactor srcAlpha, nCine::BlendingFactor dstAlpha) |
| 1012 |
{ |
| 1013 |
_blending.SrcRgb = srcRgb; |
| 1014 |
_blending.DstRgb = dstRgb; |
| 1015 |
_blending.SrcAlpha = srcAlpha; |
| 1016 |
_blending.DstAlpha = dstAlpha; |
| 1017 |
} |
| 1018 |
GsDevice::BlendingState GsDevice::GetBlendingState() { return _blending; } |
| 1019 |
void GsDevice::SetBlendingState(const BlendingState& state) { _blending = state; } |
| 1020 |
|
| 1021 |
void GsDevice::SetDepthTestEnabled(bool enabled) { _depthTest.TestEnabled = enabled; } |
| 1022 |
void GsDevice::SetDepthMaskEnabled(bool enabled) { _depthTest.MaskEnabled = enabled; } |
| 1023 |
GsDevice::DepthTestState GsDevice::GetDepthTestState() { return _depthTest; } |
| 1024 |
void GsDevice::SetDepthTestState(const DepthTestState& state) { _depthTest = state; } |
| 1025 |
|
| 1026 |
void GsDevice::SetCullFaceEnabled(bool enabled) { _cullFace.Enabled = enabled; } |
| 1027 |
GsDevice::CullFaceState GsDevice::GetCullFaceState() { return _cullFace; } |
| 1028 |
void GsDevice::SetCullFaceState(const CullFaceState& state) { _cullFace = state; } |
| 1029 |
|
| 1030 |
GsDevice::ScissorState GsDevice::GetScissorState() { return _scissor; } |
| 1031 |
void GsDevice::SetScissorState(const ScissorState& state) { _scissor = state; } |
| 1032 |
void GsDevice::SetScissor(const Recti& rect) |
| 1033 |
{ |
| 1034 |
// Same contract as the GL device: setting a rect also enables the test (RenderCommand and Viewport |
| 1035 |
// rely on it and restore via SetScissorState afterwards) |
| 1036 |
_scissor.Enabled = true; |
| 1037 |
_scissor.Rect = rect; |
| 1038 |
} |
| 1039 |
void GsDevice::SetScissorTestEnabled(bool enabled) { _scissor.Enabled = enabled; } |
| 1040 |
|
| 1041 |
Recti GsDevice::GetViewport() { return _viewport; } |
| 1042 |
void GsDevice::SetViewport(const Recti& rect) { _viewport = rect; } |
| 1043 |
void GsDevice::InitViewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height) |
| 1044 |
{ |
| 1045 |
_viewport = Recti(x, y, width, height); |
| 1046 |
} |
| 1047 |
|
| 1048 |
Colorf GsDevice::GetClearColor() { return _clearColor; } |
| 1049 |
void GsDevice::SetClearColor(const Colorf& color) { _clearColor = color; } |
| 1050 |
|
| 1051 |
void GsDevice::ApplyScissor() |
| 1052 |
{ |
| 1053 |
// Extent of the colour surface being rendered into, which is what "no scissor" has to pass |
| 1054 |
std::int32_t extentW = DisplayWidth, extentH = DisplayHeight; |
| 1055 |
if (_currentRenderTarget != nullptr) { |
| 1056 |
if (const GsTexture* target = _currentRenderTarget->GetColorTexture(0); target != nullptr) { |
| 1057 |
extentW = target->GetBufferPitch(); |
| 1058 |
extentH = target->GetPaddedHeight(); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
// A render-target pass renders bottom-up (see the Y mirror in Dispatch), so a scissor rectangle |
| 1063 |
// given in top-down logical coordinates does not describe the rows it would clip. Those passes |
| 1064 |
// therefore pass the whole target - which is what they effectively did before this existed, minus |
| 1065 |
// the stale rectangle left behind by the previous screen pass. |
| 1066 |
if (!_scissor.Enabled || _currentRenderTarget != nullptr) { |
| 1067 |
ApplyScissorArea(0, extentW - 1, 0, extentH - 1); |
| 1068 |
return; |
| 1069 |
} |
| 1070 |
|
| 1071 |
float scaleX, scaleY, offsetX, offsetY; |
| 1072 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 1073 |
const std::int32_t x0 = std::int32_t(float(_scissor.Rect.X) * scaleX + offsetX); |
| 1074 |
const std::int32_t y0 = std::int32_t(float(_scissor.Rect.Y) * scaleY + offsetY); |
| 1075 |
const std::int32_t x1 = std::int32_t(float(_scissor.Rect.X + _scissor.Rect.W) * scaleX + offsetX) - 1; |
| 1076 |
const std::int32_t y1 = std::int32_t(float(_scissor.Rect.Y + _scissor.Rect.H) * scaleY + offsetY) - 1; |
| 1077 |
ApplyScissorArea(x0, (x1 < extentW ? x1 : extentW - 1), y0, (y1 < extentH ? y1 : extentH - 1)); |
| 1078 |
} |
| 1079 |
|
| 1080 |
void GsDevice::Clear(ClearFlags flags) |
| 1081 |
{ |
| 1082 |
static_cast<void>(flags); |
| 1083 |
if (!_gsInitialized || _renderTargetSurfaceMissing) { |
| 1084 |
return; |
| 1085 |
} |
| 1086 |
|
| 1087 |
// A clear is bounded by the scissor test, exactly as glClear is - and this is also what stops it |
| 1088 |
// inheriting the rectangle of whatever drew last |
| 1089 |
ApplyScissor(); |
| 1090 |
|
| 1091 |
float scaleX, scaleY, offsetX, offsetY; |
| 1092 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 1093 |
const float w = float(_currentRenderTarget != nullptr ? _viewport.W : _logicalWidth) * scaleX; |
| 1094 |
const float h = float(_currentRenderTarget != nullptr ? _viewport.H : _logicalHeight) * scaleY; |
| 1095 |
|
| 1096 |
// Unlike the PowerVR - where the frame clear comes free with the scene background plane and an |
| 1097 |
// explicit clear would push ~300k blended pixels through the translucent pipe - the GS has no |
| 1098 |
// background plane, so a clear really is a filled rectangle. Its fill rate is enormous, which is why |
| 1099 |
// that trade goes the other way here. |
| 1100 |
// draw_clear() overrides PRMODE itself - untextured, unblended, fixed colour - for the duration of |
| 1101 |
// its own strips and puts PRMODECONT back to the PRIM register before it returns, so the clear |
| 1102 |
// cannot inherit the sprite path's primitive state and needs no override programmed around it. |
| 1103 |
// It writes its rectangle with alpha 0x80, which passes the alpha test the blended modes leave on. |
| 1104 |
qword_t* q = Reserve(48); |
| 1105 |
q = draw_clear(q, 0, 0.0f, 0.0f, w, h, |
| 1106 |
QuantizeChannel(_clearColor.R), QuantizeChannel(_clearColor.G), QuantizeChannel(_clearColor.B)); |
| 1107 |
_packetCursor = q; |
| 1108 |
} |
| 1109 |
|
| 1110 |
// ------------------------------------------------------------------ draw entry points |
| 1111 |
|
| 1112 |
void GsDevice::DrawArrays(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices) |
| 1113 |
{ |
| 1114 |
Dispatch(primitive, firstVertex, numVertices); |
| 1115 |
} |
| 1116 |
|
| 1117 |
void GsDevice::DrawArraysInstanced(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices, std::int32_t numInstances) |
| 1118 |
{ |
| 1119 |
static_cast<void>(numInstances); |
| 1120 |
Dispatch(primitive, firstVertex, numVertices); |
| 1121 |
} |
| 1122 |
|
| 1123 |
void GsDevice::DrawElements(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t baseVertex) |
| 1124 |
{ |
| 1125 |
static_cast<void>(indexFormat); |
| 1126 |
static_cast<void>(indexOffset); |
| 1127 |
Dispatch(primitive, baseVertex, std::int32_t(numIndices)); |
| 1128 |
} |
| 1129 |
|
| 1130 |
void GsDevice::DrawElementsInstanced(PrimitiveType primitive, std::uint32_t numIndices, IndexFormat indexFormat, std::uintptr_t indexOffset, std::int32_t numInstances, std::int32_t baseVertex) |
| 1131 |
{ |
| 1132 |
static_cast<void>(indexFormat); |
| 1133 |
static_cast<void>(indexOffset); |
| 1134 |
static_cast<void>(numInstances); |
| 1135 |
Dispatch(primitive, baseVertex, std::int32_t(numIndices)); |
| 1136 |
} |
| 1137 |
|
| 1138 |
FenceHandle GsDevice::InsertFence() |
| 1139 |
{ |
| 1140 |
// The device synchronises with the GS through FINISH inside FlushPackets(), so a fence is already |
| 1141 |
// satisfied by the time it could be waited on |
| 1142 |
return FenceHandle(); |
| 1143 |
} |
| 1144 |
|
| 1145 |
void GsDevice::DeleteFence(FenceHandle& fence) |
| 1146 |
{ |
| 1147 |
fence = FenceHandle(); |
| 1148 |
} |
| 1149 |
|
| 1150 |
bool GsDevice::ClientWaitFence(FenceHandle fence, std::uint64_t timeoutNs) |
| 1151 |
{ |
| 1152 |
static_cast<void>(fence); |
| 1153 |
static_cast<void>(timeoutNs); |
| 1154 |
return true; |
| 1155 |
} |
| 1156 |
|
| 1157 |
void GsDevice::SetupInitialState() |
| 1158 |
{ |
| 1159 |
_blending = BlendingState(); |
| 1160 |
_depthTest = DepthTestState(); |
| 1161 |
_cullFace = CullFaceState(); |
| 1162 |
_scissor = ScissorState(); |
| 1163 |
} |
| 1164 |
|
| 1165 |
// ------------------------------------------------------------------ bindings |
| 1166 |
|
| 1167 |
void GsDevice::BindProgram(GsShaderProgram* program) |
| 1168 |
{ |
| 1169 |
_currentProgram = program; |
| 1170 |
} |
| 1171 |
GsShaderProgram* GsDevice::CurrentProgram() { return _currentProgram; } |
| 1172 |
|
| 1173 |
void GsDevice::BindTexture(std::uint32_t unit, const GsTexture* texture) |
| 1174 |
{ |
| 1175 |
if (unit < MaxTextureUnits) { |
| 1176 |
_boundTextures[unit] = texture; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
void GsDevice::UnbindTexture(const GsTexture* texture) |
| 1181 |
{ |
| 1182 |
for (std::uint32_t i = 0; i < MaxTextureUnits; i++) { |
| 1183 |
if (_boundTextures[i] == texture) { |
| 1184 |
_boundTextures[i] = nullptr; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
// A destroyed texture must not stay referenced by a CLUT slot either, or a later slot-cache hit |
| 1188 |
// would compare against freed memory |
| 1189 |
for (std::uint32_t i = 0; i < MaxClutSlots; i++) { |
| 1190 |
if (_clutSlots[i].Palette == texture) { |
| 1191 |
_clutSlots[i].Palette = nullptr; |
| 1192 |
_clutSlots[i].PaletteOffset = -1; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
if (_paletteTexture == texture) { |
| 1196 |
_paletteTexture = nullptr; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
const GsTexture* GsDevice::GetBoundTexture(std::uint32_t unit) |
| 1201 |
{ |
| 1202 |
return (unit < MaxTextureUnits ? _boundTextures[unit] : nullptr); |
| 1203 |
} |
| 1204 |
|
| 1205 |
void GsDevice::BindUniformRange(std::uint32_t index, const std::uint8_t* data, std::uint32_t size) |
| 1206 |
{ |
| 1207 |
if (index < MaxUniformBindings) { |
| 1208 |
_boundUniformRanges[index].Data = data; |
| 1209 |
_boundUniformRanges[index].Size = size; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
void GsDevice::SetRenderTarget(GsRenderTarget* renderTarget) |
| 1214 |
{ |
| 1215 |
if (_currentRenderTarget == renderTarget) { |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
|
| 1219 |
// Everything already submitted must reach the GS before the colour buffer moves under it. |
| 1220 |
// draw_wait_finish() waits on the GS FINISH event, which is only ever raised by a draw_finish() |
| 1221 |
// in the packet - waiting without having asked for one blocks forever, which is exactly what hung |
| 1222 |
// the first time the menu (whose textured-background pass owns a render target) switched targets. |
| 1223 |
// TEXFLUSH first: a target that has just been rendered into is sampled as a texture by the pass that |
| 1224 |
// follows, and the GS's texture cache may still hold what was at those addresses before. The upload |
| 1225 |
// path flushes for the same reason, but a render target never goes through it - nothing transfers |
| 1226 |
// into it, the rasterizer writes it directly. |
| 1227 |
QueueTextureFlush(); |
| 1228 |
qword_t* qf = Reserve(8); |
| 1229 |
qf = draw_finish(qf); |
| 1230 |
_packetCursor = qf; |
| 1231 |
FlushPackets(); |
| 1232 |
draw_wait_finish(); |
| 1233 |
_currentRenderTarget = renderTarget; |
| 1234 |
_renderTargetSurfaceMissing = false; |
| 1235 |
|
| 1236 |
framebuffer_t target = _frame; |
| 1237 |
if (renderTarget != nullptr) { |
| 1238 |
GsTexture* texture = renderTarget->GetColorTexture(0); |
| 1239 |
if (texture == nullptr || texture->GetTexturePage() == GsVram::InvalidPage) { |
| 1240 |
// Nothing to render into. FRAME is deliberately left where it was and the pass is dropped |
| 1241 |
// instead: returning while _currentRenderTarget is set used to leave the colour buffer |
| 1242 |
// pointing at the DISPLAY, so a target that failed to allocate did not lose its pass - it |
| 1243 |
// drew the pass over the screen, bottom-up and unscissored, because everything downstream |
| 1244 |
// reads _currentRenderTarget to decide the Y direction and the scissor extent |
| 1245 |
_renderTargetSurfaceMissing = true; |
| 1246 |
return; |
| 1247 |
} |
| 1248 |
target.address = texture->GetTexturePage() * WordsPerPage; |
| 1249 |
target.width = texture->GetBufferPitch(); |
| 1250 |
target.height = texture->GetPaddedHeight(); |
| 1251 |
target.psm = GS_PSM_16; |
| 1252 |
target.mask = 0; |
| 1253 |
} |
| 1254 |
|
| 1255 |
qword_t* q = Reserve(32); |
| 1256 |
q = draw_framebuffer(q, 0, &target); |
| 1257 |
_packetCursor = q; |
| 1258 |
} |
| 1259 |
|
| 1260 |
void GsDevice::UnbindRenderTarget(const GsRenderTarget* renderTarget) |
| 1261 |
{ |
| 1262 |
if (_currentRenderTarget == renderTarget) { |
| 1263 |
SetRenderTarget(nullptr); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
// ------------------------------------------------------------------ palettes |
| 1268 |
|
| 1269 |
void GsDevice::FlushPendingPackets() |
| 1270 |
{ |
| 1271 |
FlushPackets(); |
| 1272 |
} |
| 1273 |
|
| 1274 |
void GsDevice::WritebackForDma(const void* start, std::size_t bytes) |
| 1275 |
{ |
| 1276 |
if (start == nullptr || bytes == 0) { |
| 1277 |
return; |
| 1278 |
} |
| 1279 |
|
| 1280 |
// At most the whole data cache can be dirty, so a range larger than it is cheaper to clear by |
| 1281 |
// walking the cache (128 lines) than by walking the range - which for a full-screen PSMCT32 upload |
| 1282 |
// would be sixteen thousand line operations to write back at most eight kilobytes |
| 1283 |
constexpr std::size_t DataCacheBytes = 8 * 1024; |
| 1284 |
if (bytes <= DataCacheBytes) { |
| 1285 |
void* first = const_cast<void*>(start); |
| 1286 |
SyncDCache(first, static_cast<std::uint8_t*>(first) + bytes); |
| 1287 |
} else { |
| 1288 |
FlushCache(WRITEBACK_DCACHE); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
void GsDevice::RegisterPaletteTexture(GsTexture* texture) |
| 1293 |
{ |
| 1294 |
_paletteTexture = texture; |
| 1295 |
// A new palette texture invalidates everything keyed on the old one, so every row is restamped |
| 1296 |
_paletteGeneration++; |
| 1297 |
for (std::int32_t row = 0; row < MaxPaletteRows; row++) { |
| 1298 |
_paletteRowStamp[row] = _paletteGeneration; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
|
| 1302 |
void GsDevice::NotifyPaletteTextureChanged(GsTexture* texture, std::int32_t firstRow, std::int32_t rowCount) |
| 1303 |
{ |
| 1304 |
if (texture != _paletteTexture) { |
| 1305 |
return; |
| 1306 |
} |
| 1307 |
// Stamp only the rows that were uploaded. Everything keyed on a palette - the resident CLUTs, their |
| 1308 |
// coverage forms and the RG8 bakes - carries the stamp of the rows it read (see |
| 1309 |
// PaletteVersionForOffset), so a cache entry survives an upload that did not touch its entries. A |
| 1310 |
// single global counter here would invalidate all of them on every animated-palette frame. |
| 1311 |
_paletteGeneration++; |
| 1312 |
const std::int32_t lastRow = (rowCount > 0 ? firstRow + rowCount : firstRow + 1); |
| 1313 |
for (std::int32_t row = (firstRow > 0 ? firstRow : 0); row < lastRow && row < MaxPaletteRows; row++) { |
| 1314 |
_paletteRowStamp[row] = _paletteGeneration; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
std::uint32_t GsDevice::PaletteVersionForOffset(const GsTexture* palette, std::int32_t paletteOffset) |
| 1319 |
{ |
| 1320 |
if (palette != _paletteTexture) { |
| 1321 |
return (palette != nullptr ? palette->GetContentVersion() : 0); |
| 1322 |
} |
| 1323 |
// The 256 entries starting at a flat offset span one row when it is row-aligned and two otherwise |
| 1324 |
const std::int32_t firstRow = paletteOffset / 256; |
| 1325 |
const std::int32_t lastRow = (paletteOffset + 255) / 256; |
| 1326 |
std::uint32_t version = 0; |
| 1327 |
for (std::int32_t row = firstRow; row <= lastRow; row++) { |
| 1328 |
if (row >= 0 && row < MaxPaletteRows && _paletteRowStamp[row] > version) { |
| 1329 |
version = _paletteRowStamp[row]; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
return version; |
| 1333 |
} |
| 1334 |
|
| 1335 |
const std::uint32_t* GsDevice::ResolvePaletteEntries(const GsTexture* palette, std::int32_t paletteOffset) |
| 1336 |
{ |
| 1337 |
// The offset is a FLAT index into the palette texture's entries and does NOT have to be row-aligned: |
| 1338 |
// the gem gradients pack two palettes into a single 256-entry row, so their offsets are 128, 256, 384 |
| 1339 |
// and 512. Reading it as a row index (and multiplying by 256 again) walks off the end of the palette |
| 1340 |
// and is what made every gem invisible while row-0 sprites carried on working. |
| 1341 |
const std::int32_t maxOffset = (palette != nullptr |
| 1342 |
? palette->GetWidth() * palette->GetHeight() - 256 : 0); |
| 1343 |
if (palette == nullptr || palette->GetPixels() == nullptr || paletteOffset < 0 || paletteOffset > maxOffset) { |
| 1344 |
return nullptr; |
| 1345 |
} |
| 1346 |
return reinterpret_cast<const std::uint32_t*>(palette->GetPixels()) + paletteOffset; |
| 1347 |
} |
| 1348 |
|
| 1349 |
std::uint32_t GsDevice::AcquireClutForOffset(const GsTexture* palette, std::int32_t paletteOffset) |
| 1350 |
{ |
| 1351 |
const std::uint32_t* entries = ResolvePaletteEntries(palette, paletteOffset); |
| 1352 |
if (entries == nullptr) { |
| 1353 |
return GsVram::InvalidBlock; |
| 1354 |
} |
| 1355 |
return AcquireClut(palette, paletteOffset, PaletteVersionForOffset(palette, paletteOffset), entries); |
| 1356 |
} |
| 1357 |
|
| 1358 |
std::uint32_t GsDevice::AcquireCoverageClutForOffset(const GsTexture* palette, std::int32_t paletteOffset) |
| 1359 |
{ |
| 1360 |
const std::uint32_t* entries = ResolvePaletteEntries(palette, paletteOffset); |
| 1361 |
if (entries == nullptr) { |
| 1362 |
return GsVram::InvalidBlock; |
| 1363 |
} |
| 1364 |
return AcquireClut(palette, paletteOffset, PaletteVersionForOffset(palette, paletteOffset), entries, true); |
| 1365 |
} |
| 1366 |
|
| 1367 |
std::uint32_t GsDevice::AcquireAlphaCoverageClut() |
| 1368 |
{ |
| 1369 |
// Index i is a texel's own alpha byte (see the declaration: the store is sampled as PSMT8H), so |
| 1370 |
// entry i is white at alpha i. The alphas the stores carry are ALREADY in the GS's 0..0x80 |
| 1371 |
// convention, so they pass straight through - which is why this goes in as a coverage payload |
| 1372 |
// rather than through the usual 0xFF-to-0x80 rescale. |
| 1373 |
static std::uint32_t entries[256]; |
| 1374 |
static bool built = false; |
| 1375 |
if (!built) { |
| 1376 |
built = true; |
| 1377 |
for (std::int32_t i = 0; i < 256; i++) { |
| 1378 |
entries[i] = 0x00FFFFFFu | (std::uint32_t(i) << 24); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
// Keyed on the palette-texture pointer being null, which no real palette ever is, and on a row |
| 1382 |
// index no palette has - so it can never collide with a row's own coverage slot. Its version never |
| 1383 |
// changes, so once it is in a slot it stays a cache hit for the rest of the run. |
| 1384 |
return AcquireClut(nullptr, -2, 1, entries, true); |
| 1385 |
} |
| 1386 |
|
| 1387 |
std::uint32_t GsDevice::AcquireClut(const GsTexture* palette, std::int32_t paletteOffset, |
| 1388 |
std::uint32_t version, const std::uint32_t* entries, bool coverage) |
| 1389 |
{ |
| 1390 |
_clutUseCounter++; |
| 1391 |
|
| 1392 |
// A slot already holding this row of this palette at this version is a hit; no transfer at all |
| 1393 |
for (std::uint32_t i = 0; i < MaxClutSlots; i++) { |
| 1394 |
ClutSlot& slot = _clutSlots[i]; |
| 1395 |
if (slot.Block != GsVram::InvalidBlock && slot.Palette == palette && slot.Coverage == coverage && |
| 1396 |
slot.PaletteOffset == paletteOffset && slot.PaletteVersion == version) { |
| 1397 |
slot.LastUse = _clutUseCounter; |
| 1398 |
return slot.Block; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
// Otherwise take a never-used slot, or the least recently used one. Unlike the PowerVR - which has |
| 1403 |
// only four hardware banks and had to keep an LRU over them - the GS holds a CLUT per 1 KB of local |
| 1404 |
// memory, so the slab is large enough that eviction is rare rather than every-draw. |
| 1405 |
ClutSlot* target = nullptr; |
| 1406 |
for (std::uint32_t i = 0; i < MaxClutSlots; i++) { |
| 1407 |
ClutSlot& slot = _clutSlots[i]; |
| 1408 |
if (slot.Block == GsVram::InvalidBlock) { |
| 1409 |
slot.Block = GsVram::AllocateClut(); |
| 1410 |
if (slot.Block == GsVram::InvalidBlock) { |
| 1411 |
continue; |
| 1412 |
} |
| 1413 |
} |
| 1414 |
if (slot.PaletteOffset == -1) { |
| 1415 |
target = &slot; // Never used, or invalidated by a palette upload |
| 1416 |
break; |
| 1417 |
} |
| 1418 |
if (target == nullptr || slot.LastUse < target->LastUse) { |
| 1419 |
target = &slot; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
if (target == nullptr || target->Block == GsVram::InvalidBlock) { |
| 1423 |
return GsVram::InvalidBlock; |
| 1424 |
} |
| 1425 |
|
| 1426 |
// The engine's palette entries are RGBA8 with 0xFF meaning opaque; the GS treats **0x80** as fully |
| 1427 |
// opaque, so a straight copy makes every entry read as twice opaque and blend wrong. The colour |
| 1428 |
// bytes are already in the GS's order (R in the low byte), so only alpha is rescaled. |
| 1429 |
// |
| 1430 |
// A COVERAGE payload keeps the alphas verbatim and forces every colour to white instead: it is what |
| 1431 |
// turns MODULATE into a silhouette pass (see AcquireCoverageClutForRow), and its alphas either come |
| 1432 |
// from a row that was already rescaled on its way in or ARE the indices themselves. |
| 1433 |
for (std::int32_t i = 0; i < 256; i++) { |
| 1434 |
const std::uint32_t entry = entries[i]; |
| 1435 |
const std::uint32_t alpha = (coverage ? (entry >> 24) : (((entry >> 24) + 1) >> 1)); |
| 1436 |
// A 256-entry CLUT in CSM1 is NOT stored linearly: the hardware reads it back through the same |
| 1437 |
// 16x16 PSMCT32 block addressing a texture would use, which interleaves the middle two groups of |
| 1438 |
// every 32 entries. Uploading in index order therefore gives every texel the wrong colour - the |
| 1439 |
// symptom being a flat wash rather than anything recognisable. Entries whose bits 3-4 are 01 swap |
| 1440 |
// with those 8 higher, and vice versa; the other two quarters are already in place. |
| 1441 |
std::int32_t target = i; |
| 1442 |
if ((i & 0x18) == 0x08) { |
| 1443 |
target = i + 8; |
| 1444 |
} else if ((i & 0x18) == 0x10) { |
| 1445 |
target = i - 8; |
| 1446 |
} |
| 1447 |
// White here is 0xFF, not 0x80: a CLUT entry's colour is a full-range 0..255 texel, and it is |
| 1448 |
// the FRAGMENT colour that runs on the GS's 0..0x80 scale. MODULATE then computes |
| 1449 |
// (0xFF * Cf) >> 7 = 2*Cf, which maps the pass colour's 0x80 back onto a full-scale 0xFF - so a |
| 1450 |
// white silhouette comes out white rather than half grey. |
| 1451 |
_clutStaging[target] = (coverage ? 0x00FFFFFFu : (entry & 0x00FFFFFFu)) | (alpha << 24); |
| 1452 |
} |
| 1453 |
|
| 1454 |
// 256 entries of 32 bits transferred as a 16x16 PSMCT32 image - the form the GS's CSM1 layout |
| 1455 |
// expects, and the one the probe verified end to end. `draw_texture_transfer` builds a DMA CHAIN, |
| 1456 |
// so this cannot be appended to the ordinary register packet - and the draws already queued there |
| 1457 |
// have to reach the GS first, or they would sample the palette this is about to overwrite. |
| 1458 |
FlushPackets(); |
| 1459 |
// ...and the 1 KB just written has to leave the data cache, or the DMA reads the previous palette |
| 1460 |
WritebackForDma(_clutStaging, sizeof(_clutStaging)); |
| 1461 |
qword_t* q = _packet; |
| 1462 |
q = draw_texture_transfer(q, _clutStaging, 16, 16, GS_PSM_32, |
| 1463 |
std::int32_t(target->Block * WordsPerBlock), 16); |
| 1464 |
q = draw_texture_flush(q); |
| 1465 |
dma_channel_send_chain(DMA_CHANNEL_GIF, _packet, std::int32_t(q - _packet), 0, 0); |
| 1466 |
dma_wait_fast(); |
| 1467 |
_packetCursor = _packet; |
| 1468 |
// The contents behind this slot's CBP have just changed, and TEX0 is what reloads the GS's internal |
| 1469 |
// CLUT buffer. A draw that is otherwise identical to the last one - same store, same block - would |
| 1470 |
// skip the write and keep sampling the palette that was there, so the state cache is dropped here. |
| 1471 |
// This is the ONLY place local memory behind a live CBP is rewritten, which is what lets ApplyTexture |
| 1472 |
// treat an unchanged DrawState as safe everywhere else. |
| 1473 |
_appliedTextureValid = false; |
| 1474 |
|
| 1475 |
target->Palette = palette; |
| 1476 |
target->PaletteOffset = paletteOffset; |
| 1477 |
target->PaletteVersion = version; |
| 1478 |
// The payload KIND is part of the slot's identity, not an argument that only picks what to upload. |
| 1479 |
// Leaving it out meant the two forms of a row shared one cache key: a coverage payload - white in |
| 1480 |
// every entry - was stamped as that row's colours, so the next material draw of the row scored a hit |
| 1481 |
// on it and came out white, and every silhouette draw (the hit flash, the shield, the outline) |
| 1482 |
// poisoned one more slot. That is the "everything turns white after a while in a level" failure, and |
| 1483 |
// the reason it took a while is that it needed a silhouette pass per palette row to get there. |
| 1484 |
target->Coverage = coverage; |
| 1485 |
target->LastUse = _clutUseCounter; |
| 1486 |
return target->Block; |
| 1487 |
} |
| 1488 |
|
| 1489 |
// ------------------------------------------------------------------ direct-tier lighting |
| 1490 |
|
| 1491 |
void GsDevice::SetPendingSoftwareLighting(const float* lightmap, std::int32_t lmW, std::int32_t lmH, std::int32_t scale, |
| 1492 |
std::int32_t vpX, std::int32_t vpY, std::int32_t vpW, std::int32_t vpH, float ambR, float ambG, float ambB, |
| 1493 |
bool waterActive, float waterLevelPx, float waterTime, float waterCamY) |
| 1494 |
{ |
| 1495 |
PendingSoftwareLight entry; |
| 1496 |
entry.Lightmap = lightmap; |
| 1497 |
entry.LmW = lmW; |
| 1498 |
entry.LmH = lmH; |
| 1499 |
entry.Scale = scale; |
| 1500 |
entry.VpX = vpX; |
| 1501 |
entry.VpY = vpY; |
| 1502 |
entry.VpW = vpW; |
| 1503 |
entry.VpH = vpH; |
| 1504 |
entry.AmbR = ambR; |
| 1505 |
entry.AmbG = ambG; |
| 1506 |
entry.AmbB = ambB; |
| 1507 |
entry.WaterActive = waterActive; |
| 1508 |
entry.WaterLevelPx = waterLevelPx; |
| 1509 |
entry.WaterTime = waterTime; |
| 1510 |
entry.WaterCamY = waterCamY; |
| 1511 |
_pendingSoftwareLights.push_back(entry); |
| 1512 |
} |
| 1513 |
|
| 1514 |
void GsDevice::EndFrame() |
| 1515 |
{ |
| 1516 |
_pendingSoftwareLights.clear(); |
| 1517 |
} |
| 1518 |
|
| 1519 |
void GsDevice::ApplyPendingSoftwareLighting() |
| 1520 |
{ |
| 1521 |
if (_pendingSoftwareLights.empty()) { |
| 1522 |
return; |
| 1523 |
} |
| 1524 |
const PendingSoftwareLight light = _pendingSoftwareLights.front(); |
| 1525 |
_pendingSoftwareLights.erase(_pendingSoftwareLights.begin()); |
| 1526 |
|
| 1527 |
const bool hasLighting = (light.Lightmap != nullptr && light.LmW > 0 && light.LmH > 0); |
| 1528 |
const bool hasWater = light.WaterActive; |
| 1529 |
if ((!hasLighting && !hasWater) || !_gsInitialized) { |
| 1530 |
return; |
| 1531 |
} |
| 1532 |
|
| 1533 |
ApplyScissor(); |
| 1534 |
|
| 1535 |
float scaleX, scaleY, offsetX, offsetY; |
| 1536 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 1537 |
const float vpX = float(light.VpX) * scaleX + offsetX, vpY = float(light.VpY) * scaleY + offsetY; |
| 1538 |
const float vpW = float(light.VpW) * scaleX, vpH = float(light.VpH) * scaleY; |
| 1539 |
|
| 1540 |
if (hasLighting) { |
| 1541 |
/* |
| 1542 |
The CPU lightmap composites as `scene * (r*(1 + g) + amb*(1 - r))` per channel - a per-pixel |
| 1543 |
MULTIPLY of the destination, which the PVR and the GE both draw as a `dst * src` blend of a |
| 1544 |
factor texture. The GS cannot: its one blend equation is `((A - B) * C) >> 7 + D` where C is an |
| 1545 |
ALPHA (or a constant), never a colour, so no configuration of it multiplies the destination by |
| 1546 |
a per-pixel colour. |
| 1547 |
|
| 1548 |
What it CAN do per pixel is `A = 0, B = Cd, C = As, D = Cd`, i.e. `Cd * (1 - As)` - an |
| 1549 |
achromatic multiply driven by the source alpha. So the factor is reduced to one channel here |
| 1550 |
and uploaded as an 8-bit surface whose texels ARE the alpha, read through the same |
| 1551 |
alpha-to-coverage CLUT the silhouette passes use. That keeps the light SHAPE and the darkness, |
| 1552 |
which is what the effect mostly is, and loses only the ambient HUE in unlit areas - the three |
| 1553 |
channels of the real factor differ solely through the frame-constant ambient colour. |
| 1554 |
|
| 1555 |
Exact per-channel output is reachable at three times the cost: three of these passes, each |
| 1556 |
carrying one channel's factor, with `FRAME.FBMSK` masking the other two. That is a fill-rate |
| 1557 |
trade the GS could afford, but it needs three surfaces and three uploads a frame, and local |
| 1558 |
memory is already the tight resource here - so it is the documented upgrade, not the default. |
| 1559 |
|
| 1560 |
One benefit falls out of the alpha form: a fully-lit texel has alpha 0, which the alpha test |
| 1561 |
discards, so bright areas cost no blending at all. |
| 1562 |
*/ |
| 1563 |
std::int32_t texW = 8, texH = 8; |
| 1564 |
while (texW < light.LmW && texW < 1024) texW <<= 1; |
| 1565 |
while (texH < light.LmH && texH < 1024) texH <<= 1; |
| 1566 |
|
| 1567 |
// PSMT8's page is 128x64 texels, and the pitch is padded to it exactly as a texture store is |
| 1568 |
const std::int32_t pitch = GsVram::GetPaddedWidth(GsPsm::T8, texW); |
| 1569 |
std::int32_t pageWidth, pageHeight; |
| 1570 |
GsVram::GetPageGeometry(GsPsm::T8, pageWidth, pageHeight); |
| 1571 |
const std::int32_t storeHeight = ((texH + pageHeight - 1) / pageHeight) * pageHeight; |
| 1572 |
const std::uint32_t pageCount = GsVram::GetPageCount(GsPsm::T8, pitch, storeHeight); |
| 1573 |
|
| 1574 |
// The surface is allocated once and rewritten every frame, so it is held for the lifetime of the |
| 1575 |
// session rather than churned - a per-frame allocation would compete with the texture cache for |
| 1576 |
// pages at exactly the moment a level is already short of them |
| 1577 |
if (_lightmapPage != GsVram::InvalidPage && _lightmapPageCount != pageCount) { |
| 1578 |
GsVram::FreePages(_lightmapPage, _lightmapPageCount); |
| 1579 |
_lightmapPage = GsVram::InvalidPage; |
| 1580 |
_lightmapPageCount = 0; |
| 1581 |
} |
| 1582 |
if (_lightmapPage == GsVram::InvalidPage) { |
| 1583 |
// Through GsTexture's allocator rather than GsVram's, so a cache that is momentarily full |
| 1584 |
// evicts a sheet for it instead of refusing. The surface is one or two pages and the whole |
| 1585 |
// scene is unlit without it, which is a far worse trade than one sheet being re-uploaded - |
| 1586 |
// and because it is allocated once and held, the eviction is paid once too |
| 1587 |
_lightmapPage = GsTexture::AllocatePages(pageCount, nullptr); |
| 1588 |
if (_lightmapPage == GsVram::InvalidPage) { |
| 1589 |
return; // No room for the compositor surface; the scene stays unlit this frame |
| 1590 |
} |
| 1591 |
_lightmapPageCount = pageCount; |
| 1592 |
} |
| 1593 |
|
| 1594 |
const std::uint32_t clutBlock = AcquireAlphaCoverageClut(); |
| 1595 |
if (clutBlock == GsVram::InvalidBlock) { |
| 1596 |
return; |
| 1597 |
} |
| 1598 |
|
| 1599 |
// Luminance weights of the ambient colour, so the one channel carried is the perceptual factor |
| 1600 |
const float ambGray = 0.299f * light.AmbR + 0.587f * light.AmbG + 0.114f * light.AmbB; |
| 1601 |
|
| 1602 |
std::uint8_t* const surface = _lightmapStaging.Reserve(std::size_t(pitch) * std::size_t(storeHeight)); |
| 1603 |
if (surface == nullptr) { |
| 1604 |
return; // No main memory for the compositor surface; the scene stays unlit this frame |
| 1605 |
} |
| 1606 |
for (std::int32_t y = 0; y < light.LmH; y++) { |
| 1607 |
const float* DEATH_RESTRICT src = light.Lightmap + std::size_t(y) * light.LmW * 2; |
| 1608 |
std::uint8_t* DEATH_RESTRICT dst = surface + std::size_t(y) * pitch; |
| 1609 |
// Unlit runs repeat the same pair of factors across long spans, so remembering the last |
| 1610 |
// converted texel turns most of the surface into a compare and a store |
| 1611 |
float prevR = -1.0f, prevG = -1.0f; |
| 1612 |
std::uint8_t prevTexel = 0; |
| 1613 |
for (std::int32_t x = 0; x < light.LmW; x++) { |
| 1614 |
const float rawR = src[x * 2]; |
| 1615 |
const float rawG = src[x * 2 + 1]; |
| 1616 |
if (rawR == prevR && rawG == prevG) { |
| 1617 |
dst[x] = prevTexel; |
| 1618 |
continue; |
| 1619 |
} |
| 1620 |
prevR = rawR; |
| 1621 |
prevG = rawG; |
| 1622 |
const float r = (rawR < 0.0f ? 0.0f : (rawR > 1.0f ? 1.0f : rawR)); |
| 1623 |
const float g = (rawG < 0.0f ? 0.0f : (rawG > 1.0f ? 1.0f : rawG)); |
| 1624 |
// The stored value is the ATTENUATION (1 - factor), because the equation subtracts it |
| 1625 |
prevTexel = QuantizePassChannel(1.0f - (r * (1.0f + g) + ambGray * (1.0f - r))); |
| 1626 |
dst[x] = prevTexel; |
| 1627 |
} |
| 1628 |
// The padding columns are reached by the bilinear tap at the last texel |
| 1629 |
for (std::int32_t x = light.LmW; x < pitch; x++) { |
| 1630 |
dst[x] = prevTexel; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
for (std::int32_t y = light.LmH; y < storeHeight; y++) { |
| 1634 |
std::memcpy(surface + std::size_t(y) * pitch, surface + std::size_t(light.LmH - 1) * pitch, |
| 1635 |
std::size_t(pitch)); |
| 1636 |
} |
| 1637 |
|
| 1638 |
// The transfer builds a DMA CHAIN, so it goes out on its own after the pending register packet - |
| 1639 |
// and the surface the CPU has just filled has to be written back for the DMA to see it |
| 1640 |
FlushPackets(); |
| 1641 |
WritebackForDma(surface, std::size_t(pitch) * std::size_t(storeHeight)); |
| 1642 |
qword_t* qu = _packet; |
| 1643 |
qu = draw_texture_transfer(qu, surface, pitch, storeHeight, GS_PSM_8, |
| 1644 |
std::int32_t(_lightmapPage * WordsPerPage), pitch); |
| 1645 |
qu = draw_texture_flush(qu); |
| 1646 |
dma_channel_send_chain(DMA_CHANNEL_GIF, _packet, std::int32_t(qu - _packet), 0, 0); |
| 1647 |
dma_wait_fast(); |
| 1648 |
_packetCursor = _packet; |
| 1649 |
|
| 1650 |
DrawState state; |
| 1651 |
state.Page = _lightmapPage; |
| 1652 |
state.ClutBlock = clutBlock; |
| 1653 |
state.BufferPitch = pitch; |
| 1654 |
state.Psm = GS_PSM_8; |
| 1655 |
state.SampledWidth = texW; |
| 1656 |
state.SampledHeight = texH; |
| 1657 |
state.WrapU = WRAP_CLAMP; |
| 1658 |
state.WrapV = WRAP_CLAMP; |
| 1659 |
// The lightmap is one texel per `light.Scale` pixels, so it is interpolated rather than blocky |
| 1660 |
state.MagFilter = LOD_MAG_LINEAR; |
| 1661 |
state.MinFilter = LOD_MIN_LINEAR; |
| 1662 |
|
| 1663 |
BlendEquation equation; |
| 1664 |
ResolveBlendEquation(nCine::BlendingFactor::Zero, nCine::BlendingFactor::OneMinusSrcAlpha, equation); |
| 1665 |
ApplyBlendEquation(equation); |
| 1666 |
|
| 1667 |
// The lightmap's row 0 is the BOTTOM of the displayed viewport (the software buffer convention), |
| 1668 |
// so V runs used -> 0 from top to bottom. Corner indexing is the sprite path's (ax, ay) weights. |
| 1669 |
const float uMax = float(light.LmW) / float(texW); |
| 1670 |
const float vMax = float(light.LmH) / float(texH); |
| 1671 |
const float px[4] = { vpX + vpW, vpX + vpW, vpX, vpX }; |
| 1672 |
const float py[4] = { vpY, vpY + vpH, vpY, vpY + vpH }; |
| 1673 |
const float pu[4] = { uMax, uMax, 0.0f, 0.0f }; |
| 1674 |
const float pv[4] = { vMax, 0.0f, vMax, 0.0f }; |
| 1675 |
SubmitQuadPrimitive(state, px, py, pu, pv, PackPassColor(OpaqueWhite), 0.0f, 0.0f, true); |
| 1676 |
} |
| 1677 |
|
| 1678 |
if (hasWater) { |
| 1679 |
// Water v1: constant underwater tint band + above-deep-water darkening (shared with the PVR and |
| 1680 |
// the GE), both flat source-over quads |
| 1681 |
BlendEquation equation = SourceOverEquation(); |
| 1682 |
ApplyBlendEquation(equation); |
| 1683 |
|
| 1684 |
DrawState untextured; |
| 1685 |
const float waterTop = vpY + light.WaterLevelPx * scaleY; |
| 1686 |
const float uv[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1687 |
if (waterTop < vpY + vpH) { |
| 1688 |
const float px[4] = { vpX + vpW, vpX + vpW, vpX, vpX }; |
| 1689 |
const float py[4] = { waterTop, vpY + vpH, waterTop, vpY + vpH }; |
| 1690 |
const float tint[4] = { 0.4f, 0.6f, 0.8f, 0.4f }; |
| 1691 |
SubmitQuadPrimitive(untextured, px, py, uv, uv, PackVertexColor(tint), 0.0f, 0.0f, true); |
| 1692 |
} |
| 1693 |
const float waterLevelNorm = (light.VpH > 0 ? light.WaterLevelPx / float(light.VpH) : 1.0f); |
| 1694 |
if (waterLevelNorm < 0.4f && waterTop > vpY) { |
| 1695 |
const float px[4] = { vpX + vpW, vpX + vpW, vpX, vpX }; |
| 1696 |
const float py[4] = { vpY, waterTop, vpY, waterTop }; |
| 1697 |
const float above[4] = { light.AmbR, light.AmbG, light.AmbB, 0.4f - waterLevelNorm }; |
| 1698 |
SubmitQuadPrimitive(untextured, px, py, uv, uv, PackVertexColor(above), 0.0f, 0.0f, true); |
| 1699 |
} |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
// ------------------------------------------------------------------ fixed-function quad effects |
| 1704 |
// |
| 1705 |
// The quad-family effects are expressed as FixedFunctionPass descriptors handed to this EffectContext - |
| 1706 |
// the structural contract documented in FixedFunctionPass.h, implemented here against the GIF |
| 1707 |
// submission helpers above. The per-effect functions themselves are GENERATED from the shaders' |
| 1708 |
// void fixed_function([gs]) blocks by the ShaderCompiler, exactly as on the PVR, the GX and the GU |
| 1709 |
// (Shaders/Generated/GsGeneratedEffects.h, included below), so this file contains no effect-specific |
| 1710 |
// code at all. |
| 1711 |
// |
| 1712 |
// EffectContext deliberately has EXTERNAL linkage (namespace scope, though it is still defined only in |
| 1713 |
// this translation unit), for the same reason as on the other consoles: the effect-table struct is at |
| 1714 |
// namespace scope - so GsShaderProgram can forward-declare it and hold a typed entry pointer - and |
| 1715 |
// names EffectContext in a member type. |
| 1716 |
|
| 1717 |
struct EffectContext |
| 1718 |
{ |
| 1719 |
// Matches the GX's and the GU's capacity rather than the PVR's: one GIF packet carries a triangle |
| 1720 |
// strip of any length, so there is nothing to gain from splitting the geometry into small pieces |
| 1721 |
static constexpr std::int32_t MaxStripVertices = 16; |
| 1722 |
|
| 1723 |
// Decoded instance data of the draw being dispatched (the documented contract) |
| 1724 |
const float* InstanceColor; |
| 1725 |
float TexelW; |
| 1726 |
float TexelH; |
| 1727 |
bool Batched; |
| 1728 |
|
| 1729 |
// The texture state this instance resolved to, its COVERAGE form for silhouette passes (null when |
| 1730 |
// the store has none - see ResolveCoverageState) and the equation the material's blending resolved |
| 1731 |
// to, which a pass's BlendMode overrides |
| 1732 |
const DrawState* Material; |
| 1733 |
const DrawState* Coverage; |
| 1734 |
BlendEquation MaterialBlend; |
| 1735 |
|
| 1736 |
// The instance's corner arrays: raster positions, and NORMALIZED texture coordinates over the |
| 1737 |
// sampled (padded) extent |
| 1738 |
const float* Px; |
| 1739 |
const float* Py; |
| 1740 |
const float* Pu; |
| 1741 |
const float* Pv; |
| 1742 |
const float* TexRect; |
| 1743 |
// Whether the four corners form an axis-aligned rectangle. They usually do under an orthographic |
| 1744 |
// projection, which is what lets a pass go out as one GS SPRITE; a rotated one becomes two triangles |
| 1745 |
// instead (see SubmitQuadPrimitive). |
| 1746 |
bool AxisAligned; |
| 1747 |
|
| 1748 |
// Pre-clip quad geometry (the quad_origin/quad_axis_* built-ins), the program (for resolved |
| 1749 |
// uniforms) and the conversion from the shader's normalized texture space into texels |
| 1750 |
float OriginX, OriginY; |
| 1751 |
float AxisXx, AxisXy; |
| 1752 |
float AxisYx, AxisYy; |
| 1753 |
const GsShaderProgram* Program; |
| 1754 |
// The shader's texture space is normalized over the REAL extent; the primitives want it normalized |
| 1755 |
// over the SAMPLED (padded power-of-two) one, which is what this scale folds in |
| 1756 |
float UvScaleU, UvScaleV; |
| 1757 |
|
| 1758 |
// The strip builder scratch. Colours are packed at set time (same quantization as the quad path, so |
| 1759 |
// identical float inputs produce identical vertex words) and UVs converted to texels there too. |
| 1760 |
float StripX[MaxStripVertices]; |
| 1761 |
float StripY[MaxStripVertices]; |
| 1762 |
float StripU[MaxStripVertices]; |
| 1763 |
float StripV[MaxStripVertices]; |
| 1764 |
std::uint64_t StripRgbaq[MaxStripVertices]; |
| 1765 |
|
| 1766 |
const float* Color() const { return InstanceColor; } |
| 1767 |
float TexelWidth() const { return TexelW; } |
| 1768 |
float TexelHeight() const { return TexelH; } |
| 1769 |
bool IsBatched() const { return Batched; } |
| 1770 |
|
| 1771 |
float QuadOriginX() const { return OriginX; } |
| 1772 |
float QuadOriginY() const { return OriginY; } |
| 1773 |
float QuadAxisXx() const { return AxisXx; } |
| 1774 |
float QuadAxisXy() const { return AxisXy; } |
| 1775 |
float QuadAxisYx() const { return AxisYx; } |
| 1776 |
float QuadAxisYy() const { return AxisYy; } |
| 1777 |
|
| 1778 |
bool HasUniform(const char* name) const |
| 1779 |
{ |
| 1780 |
return (Program->ResolveUniform(name) != nullptr); |
| 1781 |
} |
| 1782 |
void LoadUniform(const char* name, float* out, std::int32_t floatCount) const |
| 1783 |
{ |
| 1784 |
// An unresolved name leaves the caller's zeros in place - blocks guard with has_uniform() |
| 1785 |
const std::uint8_t* bytes = Program->ResolveUniform(name); |
| 1786 |
if (bytes != nullptr) { |
| 1787 |
std::memcpy(out, bytes, std::size_t(floatCount) * sizeof(float)); |
| 1788 |
} |
| 1789 |
} |
| 1790 |
|
| 1791 |
void SetStripVertexPosition(std::int32_t i, float x, float y) |
| 1792 |
{ |
| 1793 |
if (std::uint32_t(i) < std::uint32_t(MaxStripVertices)) { |
| 1794 |
StripX[i] = x; |
| 1795 |
StripY[i] = y; |
| 1796 |
} |
| 1797 |
} |
| 1798 |
void SetStripVertexUv(std::int32_t i, float u, float v) |
| 1799 |
{ |
| 1800 |
if (std::uint32_t(i) < std::uint32_t(MaxStripVertices)) { |
| 1801 |
StripU[i] = u * UvScaleU; |
| 1802 |
StripV[i] = v * UvScaleV; |
| 1803 |
} |
| 1804 |
} |
| 1805 |
// Per-vertex colours are only ever read by SubmitStripShaded(), whose strip is untextured by |
| 1806 |
// construction, so they are packed on the full-range scale that primitive's RGB reaches the blender on |
| 1807 |
void SetStripVertexColor(std::int32_t i, float r, float g, float b, float a) |
| 1808 |
{ |
| 1809 |
if (std::uint32_t(i) < std::uint32_t(MaxStripVertices)) { |
| 1810 |
const float rgba[4] = { r, g, b, a }; |
| 1811 |
StripRgbaq[i] = PackVertexColor(rgba).rgbaq; |
| 1812 |
} |
| 1813 |
} |
| 1814 |
|
| 1815 |
// Whether a UV span can be mapped onto the screen at all (a zero texRect has no scale) |
| 1816 |
bool HasTexelStep() const { return TexRect[0] != 0.0f && TexRect[2] != 0.0f; } |
| 1817 |
// Maps a span in the sprite's UV space onto the quad's on-screen extent - the texel step the |
| 1818 |
// Outline ring taps use. The corners are already in raster space, so the result is a raster |
| 1819 |
// displacement. |
| 1820 |
float TexelToRasterX(float uvSpan) const { return (Px[0] - Px[2]) * (uvSpan / TexRect[0]); } |
| 1821 |
float TexelToRasterY(float uvSpan) const { return (Py[1] - Py[0]) * (uvSpan / TexRect[2]); } |
| 1822 |
// The documented texel_size() built-in of the fixed_function contract: the Outline shader family |
| 1823 |
// carries the sprite's UV-space texel size in its instance color.xy (exactly like the GLSL derives |
| 1824 |
// its tap offsets), folded through the raster conversion above |
| 1825 |
float TexelStepX() const { return TexelToRasterX(InstanceColor[0]); } |
| 1826 |
float TexelStepY() const { return TexelToRasterY(InstanceColor[1]); } |
| 1827 |
|
| 1828 |
/* |
| 1829 |
One quad draw. @p silhouette selects the store's COVERAGE form - the pass colour wherever the |
| 1830 |
texture has alpha - which the GS reaches by swapping the CLUT rather than the texture function |
| 1831 |
(see GsDevice::AcquireCoverageClutForRow). A store with no coverage form falls back to the |
| 1832 |
modulated one, which shows the sprite's own colours where a flat tone belongs; that is only |
| 1833 |
reachable for a PSMCT16 render target, whose 1-bit alpha carries no coverage to sample, and no |
| 1834 |
effect that offsets a colour draws over one. |
| 1835 |
*/ |
| 1836 |
void SubmitQuadPass(const FixedFunctionPass& pass, bool silhouette) |
| 1837 |
{ |
| 1838 |
const DrawState* state = Material; |
| 1839 |
if (silhouette) { |
| 1840 |
if (Coverage != nullptr) { |
| 1841 |
state = Coverage; |
| 1842 |
} else { |
| 1843 |
static bool warnedNoCoverage = false; |
| 1844 |
if (!warnedNoCoverage) { |
| 1845 |
warnedNoCoverage = true; |
| 1846 |
LOGW("A silhouette pass has no coverage form of its store, drawing it modulated"); |
| 1847 |
} |
| 1848 |
} |
| 1849 |
} |
| 1850 |
ApplyBlendEquation(PassEquation(MaterialBlend, pass.Blend)); |
| 1851 |
SubmitQuadPrimitive(*state, Px, Py, Pu, Pv, PackStateColor(*state, pass.Color), |
| 1852 |
pass.ScreenOffset[0], pass.ScreenOffset[1], AxisAligned); |
| 1853 |
} |
| 1854 |
|
| 1855 |
/* |
| 1856 |
Submits one pass over the current instance's quad. A pass carrying an offset colour is EXPANDED |
| 1857 |
here, because the GS has no post-texture additive term for a COLOUR: of its four texture |
| 1858 |
functions, MODULATE has no additive stage at all and both HIGHLIGHT forms can only add the |
| 1859 |
fragment ALPHA broadcast to all three channels, so `texel*colour + offset` is not any single GS |
| 1860 |
draw. Doing the expansion in the mechanism (rather than spelling both passes in every shader's |
| 1861 |
gs block) is what keeps the portable core portable - the same generic block still describes the |
| 1862 |
effect on all four consoles, exactly as the GX reinterprets an offset colour as its silhouette |
| 1863 |
form and the GU splits it the same way this does. |
| 1864 |
|
| 1865 |
The expansion is EXACT, not an approximation. With a = texel.a * colour.a, the PVR's single draw |
| 1866 |
over the destination dst is (its blend being SRCALPHA + INVSRCALPHA): |
| 1867 |
|
| 1868 |
dst*(1 - a) + a*(texel*colour.rgb + offset) |
| 1869 |
|
| 1870 |
and the two draws below produce, in order, |
| 1871 |
|
| 1872 |
pass 1 (modulate, the pass's own blend): dst1 = dst*(1 - a) + a*texel*colour.rgb |
| 1873 |
pass 2 (silhouette, additive): dst2 = dst1 + a*offset |
| 1874 |
|
| 1875 |
whose sum is the same expression term for term. Pass 2 samples the coverage form, so its colour |
| 1876 |
is the offset colour with no texel dependence and its coverage is still the texel's own alpha; |
| 1877 |
its ADDITIVE blend is what keeps it from attenuating the destination a second time. |
| 1878 |
|
| 1879 |
When colour.rgb is zero - the "pure offset colour" idiom of the mask/outline/shield family, |
| 1880 |
where the offset colour IS the effect - the modulate term vanishes and the pair collapses to ONE |
| 1881 |
draw: the silhouette under the pass's own (alpha-over) blend already computes |
| 1882 |
dst*(1 - a) + a*offset. That is the common case, so those effects cost the PVR's draw count. |
| 1883 |
*/ |
| 1884 |
void SubmitQuad(const FixedFunctionPass& pass) |
| 1885 |
{ |
| 1886 |
if (pass.HasOffsetColor) { |
| 1887 |
const bool modulateVisible = (pass.Color[0] != 0.0f || pass.Color[1] != 0.0f || pass.Color[2] != 0.0f); |
| 1888 |
if (modulateVisible) { |
| 1889 |
FixedFunctionPass modulate = pass; |
| 1890 |
modulate.HasOffsetColor = false; |
| 1891 |
SubmitQuadPass(modulate, false); |
| 1892 |
} |
| 1893 |
FixedFunctionPass silhouette = pass; |
| 1894 |
silhouette.HasOffsetColor = false; |
| 1895 |
silhouette.Color[0] = pass.OffsetColor[0]; |
| 1896 |
silhouette.Color[1] = pass.OffsetColor[1]; |
| 1897 |
silhouette.Color[2] = pass.OffsetColor[2]; |
| 1898 |
// Only the second half of a split pair is additive; a collapsed one keeps the pass's blend |
| 1899 |
// (the alpha-over the effects rely on) so it still attenuates the destination itself |
| 1900 |
if (modulateVisible) { |
| 1901 |
silhouette.Blend = FixedFunctionPass::BlendMode::Additive; |
| 1902 |
} |
| 1903 |
SubmitQuadPass(silhouette, true); |
| 1904 |
return; |
| 1905 |
} |
| 1906 |
// Modulate and Silhouette are the two presets this hardware realises; the output scales are |
| 1907 |
// rejected for the gs target at generation time and the other two are GX-only |
| 1908 |
SubmitQuadPass(pass, pass.Tev == FixedFunctionPass::TevPreset::Silhouette); |
| 1909 |
} |
| 1910 |
|
| 1911 |
// Textured strip out of the builder scratch: the pass's flat colour over the material state |
| 1912 |
void SubmitStrip(const FixedFunctionPass& pass, std::int32_t count) |
| 1913 |
{ |
| 1914 |
if (count > MaxStripVertices) { |
| 1915 |
count = MaxStripVertices; |
| 1916 |
} |
| 1917 |
// Same intent mapping as the GX's and the GU's SubmitStrip: with no post-texture colour add on |
| 1918 |
// this hardware, an offset colour becomes the silhouette form filled flat with it at the pass |
| 1919 |
// alpha. Deliberately NOT split into the exact two draws SubmitQuad builds - a strip's geometry |
| 1920 |
// would have to be resubmitted, and no effect asks for it (both strip users, the iris and the |
| 1921 |
// warp bands, carry no offset colour at all). |
| 1922 |
FixedFunctionPass effective = pass; |
| 1923 |
const bool silhouette = (pass.HasOffsetColor || pass.Tev == FixedFunctionPass::TevPreset::Silhouette); |
| 1924 |
if (pass.HasOffsetColor) { |
| 1925 |
effective.HasOffsetColor = false; |
| 1926 |
effective.Color[0] = pass.OffsetColor[0]; |
| 1927 |
effective.Color[1] = pass.OffsetColor[1]; |
| 1928 |
effective.Color[2] = pass.OffsetColor[2]; |
| 1929 |
} |
| 1930 |
const DrawState* state = (silhouette && Coverage != nullptr ? Coverage : Material); |
| 1931 |
ApplyBlendEquation(PassEquation(MaterialBlend, effective.Blend)); |
| 1932 |
SubmitStripPrimitive(*state, StripX, StripY, StripU, StripV, nullptr, |
| 1933 |
PackStateColor(*state, effective.Color), count, pass.ScreenOffset[0], pass.ScreenOffset[1]); |
| 1934 |
} |
| 1935 |
|
| 1936 |
// Shaded (per-vertex-colour) strip out of the builder scratch: always UNTEXTURED - a gradient has no |
| 1937 |
// texture to modulate - whose blend comes from the pass |
| 1938 |
void SubmitStripShaded(const FixedFunctionPass& pass, std::int32_t count) |
| 1939 |
{ |
| 1940 |
if (count > MaxStripVertices) { |
| 1941 |
count = MaxStripVertices; |
| 1942 |
} |
| 1943 |
DrawState untextured = *Material; |
| 1944 |
untextured.Page = GsVram::InvalidPage; |
| 1945 |
ApplyBlendEquation(PassEquation(MaterialBlend, pass.Blend)); |
| 1946 |
SubmitStripPrimitive(untextured, StripX, StripY, nullptr, nullptr, StripRgbaq, |
| 1947 |
color_t{}, count, pass.ScreenOffset[0], pass.ScreenOffset[1]); |
| 1948 |
} |
| 1949 |
}; |
| 1950 |
} |
| 1951 |
|
| 1952 |
// The per-effect functions themselves are GENERATED: the ShaderCompiler transpiles each shader's |
| 1953 |
// fixed_function block into C++ over the EffectContext defined above (the type name itself is the |
| 1954 |
// "using EffectContext = ...;" alias the header expects). Included at global scope because the header |
| 1955 |
// opens nCine::RHI::GS itself. Programs with no fixed_function block are absent from its table and |
| 1956 |
// their draws are skipped with a one-time warning, exactly as on the other consoles - which is also what |
| 1957 |
// keeps the CPU-lightmap tier (Lighting, whose Combine hook IS in the table) from painting its light |
| 1958 |
// quads over the scene. |
| 1959 |
#include "../../../../Shaders/Generated/GsGeneratedEffects.h" |
| 1960 |
|
| 1961 |
namespace nCine::RHI::GS |
| 1962 |
{ |
| 1963 |
const FixedFunctionGeneratedEffect* GsDevice::FindGeneratedEffect(const char* program, const char* variant) |
| 1964 |
{ |
| 1965 |
// A linear scan is fine - the lookup runs once per program load, not per draw |
| 1966 |
for (std::size_t i = 0; i < FixedFunctionGeneratedEffectCount; i++) { |
| 1967 |
const FixedFunctionGeneratedEffect& e = FixedFunctionGeneratedEffects[i]; |
| 1968 |
if (std::strcmp(e.Program, program) == 0 && std::strcmp(e.Variant, variant) == 0) { |
| 1969 |
return &e; |
| 1970 |
} |
| 1971 |
} |
| 1972 |
return nullptr; |
| 1973 |
} |
| 1974 |
|
| 1975 |
// ------------------------------------------------------------------ dispatch |
| 1976 |
|
| 1977 |
void GsDevice::Dispatch(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices) |
| 1978 |
{ |
| 1979 |
static_cast<void>(firstVertex); |
| 1980 |
if (_currentProgram == nullptr || numVertices <= 0 || !_gsInitialized) { |
| 1981 |
return; |
| 1982 |
} |
| 1983 |
if (_renderTargetSurfaceMissing) { |
| 1984 |
// The bound target has no colour surface, so FRAME is still on the previous one (see |
| 1985 |
// SetRenderTarget). Every entry point below funnels through here, so this is the one guard needed |
| 1986 |
return; |
| 1987 |
} |
| 1988 |
|
| 1989 |
// The program's own shader file says what it draws: either a transpiled effect function or the |
| 1990 |
// backend pipeline stage a `pipeline <name>;` block bound it to. A program with neither has no |
| 1991 |
// fixed_function block at all, which is how the desktop-only post-process programs and the |
| 1992 |
// CPU-lightmap tier's light quads stay off the screen rather than being matched by name here. |
| 1993 |
const FixedFunctionGeneratedEffect* generated = _currentProgram->GetGeneratedEffect(); |
| 1994 |
if (generated == nullptr) { |
| 1995 |
if (!_currentProgram->FetchUnsupportedWarned()) { |
| 1996 |
LOGW("Skipping draws of program \"{}\": it has no fixed_function block for the gs target", |
| 1997 |
_currentProgram->GetObjectLabel()); |
| 1998 |
} |
| 1999 |
return; |
| 2000 |
} |
| 2001 |
|
| 2002 |
const FixedFunctionIntrinsic intrinsic = generated->Intrinsic; |
| 2003 |
if (intrinsic != FixedFunctionIntrinsic::None) { |
| 2004 |
if (intrinsic == FixedFunctionIntrinsic::LightingCombine) { |
| 2005 |
ApplyPendingSoftwareLighting(); |
| 2006 |
} else if (intrinsic == FixedFunctionIntrinsic::TileMapMesh) { |
| 2007 |
DispatchTileMesh(primitive, firstVertex, numVertices); |
| 2008 |
} else if (intrinsic == FixedFunctionIntrinsic::LineStripMesh) { |
| 2009 |
DispatchLineStrip(firstVertex, numVertices); |
| 2010 |
} |
| 2011 |
return; |
| 2012 |
} |
| 2013 |
|
| 2014 |
if (primitive != PrimitiveType::TriangleStrip && primitive != PrimitiveType::Triangles) { |
| 2015 |
if (!_currentProgram->FetchUnsupportedWarned()) { |
| 2016 |
LOGW("Skipping draws of program \"{}\": the quad effect path takes only triangle primitives", |
| 2017 |
_currentProgram->GetObjectLabel()); |
| 2018 |
} |
| 2019 |
return; |
| 2020 |
} |
| 2021 |
|
| 2022 |
const std::uint8_t* projBytes = _currentProgram->GetResolvedProjection(); |
| 2023 |
const std::uint8_t* viewBytes = _currentProgram->GetResolvedView(); |
| 2024 |
const float* projMat = (projBytes != nullptr ? reinterpret_cast<const float*>(projBytes) : IdentityMatrix); |
| 2025 |
const float* viewMat = (viewBytes != nullptr ? reinterpret_cast<const float*>(viewBytes) : IdentityMatrix); |
| 2026 |
|
| 2027 |
const GsUniformBlock* block = _currentProgram->FindBlock("InstanceBlock"); |
| 2028 |
if (block == nullptr) { |
| 2029 |
block = _currentProgram->FindBlock("InstancesBlock"); |
| 2030 |
} |
| 2031 |
if (block == nullptr) { |
| 2032 |
return; |
| 2033 |
} |
| 2034 |
std::int32_t binding = block->GetBindingIndex(); |
| 2035 |
if (binding < 0 || std::uint32_t(binding) >= MaxUniformBindings) { |
| 2036 |
binding = 0; |
| 2037 |
} |
| 2038 |
const std::uint8_t* blockData = _boundUniformRanges[binding].Data; |
| 2039 |
if (blockData == nullptr) { |
| 2040 |
return; |
| 2041 |
} |
| 2042 |
|
| 2043 |
// The reflection is the only source of truth for the instance layout: a strided block means a batched |
| 2044 |
// program, a declared `uTexture` means the draw samples one, and a declared `texRect` means the |
| 2045 |
// textured member offsets apply even when nothing is sampled (the Transition carries one) |
| 2046 |
const ShaderCompiler::ProgramVariant* reflection = _currentProgram->GetReflection(); |
| 2047 |
std::uint32_t instanceStride = 0; |
| 2048 |
bool hasTexture = false; |
| 2049 |
bool texturedLayout = false; |
| 2050 |
if (reflection != nullptr) { |
| 2051 |
for (std::size_t i = 0; i < reflection->BlockCount; i++) { |
| 2052 |
if (reflection->Blocks[i].InstanceStride > 0) { |
| 2053 |
instanceStride = reflection->Blocks[i].InstanceStride; |
| 2054 |
break; |
| 2055 |
} |
| 2056 |
} |
| 2057 |
for (std::size_t i = 0; i < reflection->TextureCount; i++) { |
| 2058 |
if (std::strcmp(reflection->Textures[i].Name, "uTexture") == 0) { |
| 2059 |
hasTexture = true; |
| 2060 |
break; |
| 2061 |
} |
| 2062 |
} |
| 2063 |
texturedLayout = hasTexture; |
| 2064 |
for (std::size_t i = 0; i < reflection->BlockCount && !texturedLayout; i++) { |
| 2065 |
const ShaderCompiler::UniformBlock& b = reflection->Blocks[i]; |
| 2066 |
for (std::size_t j = 0; j < b.MemberCount; j++) { |
| 2067 |
if (std::strcmp(b.Members[j].Name, "texRect") == 0) { |
| 2068 |
texturedLayout = true; |
| 2069 |
break; |
| 2070 |
} |
| 2071 |
} |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
GsTexture* texture = (hasTexture ? const_cast<GsTexture*>(_boundTextures[0]) : nullptr); |
| 2076 |
if (hasTexture && texture == nullptr) { |
| 2077 |
return; |
| 2078 |
} |
| 2079 |
// A program with no `uTexture` draws flat-colour geometry (the menu dimmer, panels, separators). It |
| 2080 |
// takes the same instance decode and corner synthesis as a textured draw, with an untextured |
| 2081 |
// primitive at the end of it, so there is nothing to branch on here - only the DrawState differs. |
| 2082 |
|
| 2083 |
const bool batched = (instanceStride > 0); |
| 2084 |
std::int32_t numInstances = 1; |
| 2085 |
if (batched) { |
| 2086 |
numInstances = numVertices / 6; |
| 2087 |
if (numInstances < 1) { |
| 2088 |
numInstances = 1; |
| 2089 |
} |
| 2090 |
const std::uint32_t rangeSize = _boundUniformRanges[binding].Size; |
| 2091 |
if (rangeSize > 0 && std::uint32_t(numInstances) * instanceStride > rangeSize) { |
| 2092 |
numInstances = std::int32_t(rangeSize / instanceStride); |
| 2093 |
} |
| 2094 |
} |
| 2095 |
|
| 2096 |
float pv[16]; |
| 2097 |
Mat4Mul(projMat, viewMat, pv); |
| 2098 |
|
| 2099 |
const Recti viewport = (_viewport.W > 0 && _viewport.H > 0) |
| 2100 |
? _viewport : Recti(0, 0, _logicalWidth, _logicalHeight); |
| 2101 |
float scaleX, scaleY, offsetX, offsetY; |
| 2102 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 2103 |
|
| 2104 |
// Constant NDC-to-raster mapping, folded in once rather than reapplied per corner. The GS scans out |
| 2105 |
// its buffer top-down, so screen passes mirror NDC exactly as the PVR backend does. |
| 2106 |
const bool screenPass = (_currentRenderTarget == nullptr); |
| 2107 |
const float rasterScaleX = 0.5f * float(viewport.W) * scaleX; |
| 2108 |
const float rasterBiasX = rasterScaleX + float(viewport.X) * scaleX + offsetX; |
| 2109 |
const float rasterScaleY = 0.5f * float(viewport.H) * scaleY * (screenPass ? 1.0f : -1.0f); |
| 2110 |
const float rasterBiasY = 0.5f * float(viewport.H) * scaleY + float(viewport.Y) * scaleY + offsetY; |
| 2111 |
|
| 2112 |
// The GS has a HARDWARE scissor, so unlike the PowerVR there is no geometric quad clipping here. |
| 2113 |
// Both of these are register state shared by every pass of every instance in the draw. |
| 2114 |
ApplyScissor(); |
| 2115 |
const BlendEquation materialBlend = MaterialEquation(_blending); |
| 2116 |
|
| 2117 |
const std::int32_t sampledWidth = (texture != nullptr ? texture->GetPaddedWidth() : 8); |
| 2118 |
const std::int32_t sampledHeight = (texture != nullptr ? texture->GetPaddedHeight() : 8); |
| 2119 |
const float uvScaleU = (texture != nullptr ? texture->GetUScale() : 1.0f); |
| 2120 |
const float uvScaleV = (texture != nullptr ? texture->GetVScale() : 1.0f); |
| 2121 |
|
| 2122 |
for (std::int32_t k = 0; k < numInstances; k++) { |
| 2123 |
const std::uint8_t* inst = blockData + std::size_t(k) * (batched ? instanceStride : 0); |
| 2124 |
|
| 2125 |
Transform2D mvp; |
| 2126 |
Mat4MulTransform2D(pv, reinterpret_cast<const float*>(inst + kModelMatrixOffset), mvp); |
| 2127 |
float color[4]; |
| 2128 |
std::memcpy(color, inst + kColorOffset, sizeof(color)); |
| 2129 |
float texRect[4] = { 0.0f, 0.0f, 1.0f, 1.0f }; |
| 2130 |
float spriteSize[2]; |
| 2131 |
if (texturedLayout) { |
| 2132 |
std::memcpy(texRect, inst + kTexRectOffset, sizeof(texRect)); |
| 2133 |
std::memcpy(spriteSize, inst + kSpriteSizeOffset, sizeof(spriteSize)); |
| 2134 |
} else { |
| 2135 |
std::memcpy(spriteSize, inst + kSpriteSizeNoTexOffset, sizeof(spriteSize)); |
| 2136 |
} |
| 2137 |
|
| 2138 |
// The corner weights are 0 or 1, so the sprite's raster extent is its transformed axes scaled by |
| 2139 |
// its size and the corners are sums of those (identical to the PVR synthesis) |
| 2140 |
const float originX = mvp.Tx * rasterScaleX + rasterBiasX; |
| 2141 |
const float originY = mvp.Ty * rasterScaleY + rasterBiasY; |
| 2142 |
const float spanXx = mvp.Xx * rasterScaleX * spriteSize[0]; |
| 2143 |
const float spanXy = mvp.Xy * rasterScaleY * spriteSize[0]; |
| 2144 |
const float spanYx = mvp.Yx * rasterScaleX * spriteSize[1]; |
| 2145 |
const float spanYy = mvp.Yy * rasterScaleY * spriteSize[1]; |
| 2146 |
float px[4], py[4], pu[4], pvv[4]; |
| 2147 |
for (std::int32_t i = 0; i < 4; i++) { |
| 2148 |
const float ax = ((i & ~1) == 0) ? 1.0f : 0.0f; |
| 2149 |
const float ay = (i & 1) ? 1.0f : 0.0f; |
| 2150 |
px[i] = originX + ax * spanXx + ay * spanYx; |
| 2151 |
py[i] = originY + ax * spanXy + ay * spanYy; |
| 2152 |
pu[i] = (ax * texRect[0] + texRect[1]) * uvScaleU; |
| 2153 |
pvv[i] = (ay * texRect[2] + texRect[3]) * uvScaleV; |
| 2154 |
} |
| 2155 |
|
| 2156 |
// An axis-aligned quad - the overwhelming majority under an orthographic projection - goes out as |
| 2157 |
// one GS SPRITE through draw_rect_textured(), the encoding tests/GsProbe.cpp validated on |
| 2158 |
// hardware. A rotated one cannot: SPRITE takes two opposite corners and is axis-aligned by |
| 2159 |
// construction, so SubmitQuadPrimitive() draws those as two triangles instead. They used to be |
| 2160 |
// skipped, which is what left the legacy menu background (three counter-rotating tiled layers) |
| 2161 |
// completely black. |
| 2162 |
const bool axisAligned = (px[0] == px[1] && px[2] == px[3] && py[0] == py[2] && py[1] == py[3]); |
| 2163 |
|
| 2164 |
DrawState material; |
| 2165 |
DrawState coverage; |
| 2166 |
bool hasCoverage = false; |
| 2167 |
const GsTexture* paletteTex = nullptr; |
| 2168 |
std::int32_t paletteOffset = 0; |
| 2169 |
if (texture != nullptr) { |
| 2170 |
// RG8 is an index plus a PER-PIXEL alpha, which a PSMT8 texel cannot express - its alpha comes |
| 2171 |
// from the CLUT entry, so every texel sharing an index would share an alpha. Those textures are |
| 2172 |
// baked to PSMCT32 through one palette row instead, exactly as the PowerVR backend does. |
| 2173 |
const bool needsBake = texture->NeedsPaletteBake(); |
| 2174 |
if (needsBake || texture->IsIndexed()) { |
| 2175 |
// Unit 1 is only the palette when the PROGRAM says it samples one. `Material::Bind()` |
| 2176 |
// leaves units above the ones a material uses exactly as the previous material left |
| 2177 |
// them - which is correct for a shader that never reads them, and would have this |
| 2178 |
// backend take a leftover sheet for a palette on every indexed draw whose program has |
| 2179 |
// no palette sampler at all (the colorized HUD text is the common one). |
| 2180 |
paletteTex = _paletteTexture; |
| 2181 |
if (_currentProgram->UsesPalette()) { |
| 2182 |
if (_boundTextures[1] != nullptr && _boundTextures[1] != texture) { |
| 2183 |
paletteTex = _boundTextures[1]; |
| 2184 |
} |
| 2185 |
float palOffset = 0.0f; |
| 2186 |
std::memcpy(&palOffset, inst + kPaletteOffsetOffset, sizeof(palOffset)); |
| 2187 |
paletteOffset = std::int32_t(palOffset + 0.5f); |
| 2188 |
} |
| 2189 |
} |
| 2190 |
|
| 2191 |
std::uint32_t page; |
| 2192 |
if (needsBake) { |
| 2193 |
const std::uint32_t* entries = ResolvePaletteEntries(paletteTex, paletteOffset); |
| 2194 |
if (entries == nullptr) { |
| 2195 |
continue; |
| 2196 |
} |
| 2197 |
page = texture->EnsureBakedColor(entries, std::uint32_t(paletteOffset), |
| 2198 |
PaletteVersionForOffset(paletteTex, paletteOffset), paletteTex); |
| 2199 |
} else { |
| 2200 |
page = texture->AcquireTexturePage(); |
| 2201 |
} |
| 2202 |
if (page == GsVram::InvalidPage) { |
| 2203 |
continue; |
| 2204 |
} |
| 2205 |
|
| 2206 |
material.Page = page; |
| 2207 |
// In TEXELS - libdraw divides by 64 itself; a TBW value here would sample a buffer 64 times |
| 2208 |
// too narrow (see GsVram::GetPaddedWidth) |
| 2209 |
material.BufferPitch = (needsBake ? texture->GetBakedBufferPitch() : texture->GetBufferPitch()); |
| 2210 |
// GsPsm's enumerators ARE the hardware PSM values, so the store's own mode goes straight into |
| 2211 |
// the field. It has to: a render target is a PSMCT16 colour surface, and a "PSMT8 or else |
| 2212 |
// PSMCT32" test would sample one as PSMCT32 - two texels read as one. |
| 2213 |
material.Psm = (needsBake ? GS_PSM_32 : std::int32_t(texture->GetPsm())); |
| 2214 |
material.SampledWidth = sampledWidth; |
| 2215 |
material.SampledHeight = sampledHeight; |
| 2216 |
material.WrapU = MapWrapGs(texture->GetWrapS()); |
| 2217 |
material.WrapV = MapWrapGs(texture->GetWrapT()); |
| 2218 |
material.MagFilter = MapMagFilterGs(texture->GetMagFiltering()); |
| 2219 |
material.MinFilter = MapMinFilterGs(texture->GetMinFiltering()); |
| 2220 |
if (!needsBake && texture->IsIndexed()) { |
| 2221 |
material.ClutBlock = AcquireClutForOffset(paletteTex, paletteOffset); |
| 2222 |
if (material.ClutBlock == GsVram::InvalidBlock) { |
| 2223 |
continue; |
| 2224 |
} |
| 2225 |
} |
| 2226 |
|
| 2227 |
// The coverage form a silhouette pass samples, resolved only for the effects that can ask for |
| 2228 |
// one. UsesOffsetColor is exactly "some reachable pass writes an offset colour", which is what |
| 2229 |
// SubmitQuad expands into a silhouette - so it is also the answer to "can this effect need |
| 2230 |
// coverage", and the masks and outlines are the only draws that pay for the extra CLUT. |
| 2231 |
if (generated->UsesOffsetColor) { |
| 2232 |
coverage = material; |
| 2233 |
if (material.Psm == GS_PSM_8 && paletteTex != nullptr) { |
| 2234 |
const std::uint32_t clut = AcquireCoverageClutForOffset(paletteTex, paletteOffset); |
| 2235 |
if (clut != GsVram::InvalidBlock) { |
| 2236 |
coverage.ClutBlock = clut; |
| 2237 |
hasCoverage = true; |
| 2238 |
} |
| 2239 |
} else if (material.Psm == GS_PSM_32) { |
| 2240 |
const std::uint32_t clut = AcquireAlphaCoverageClut(); |
| 2241 |
if (clut != GsVram::InvalidBlock) { |
| 2242 |
coverage.Psm = GS_PSM_8H; |
| 2243 |
coverage.ClutBlock = clut; |
| 2244 |
hasCoverage = true; |
| 2245 |
} |
| 2246 |
} |
| 2247 |
} |
| 2248 |
} |
| 2249 |
|
| 2250 |
EffectContext ctx; |
| 2251 |
ctx.InstanceColor = color; |
| 2252 |
ctx.TexelW = (texture != nullptr && texture->GetWidth() > 0 ? 1.0f / float(texture->GetWidth()) : 0.0f); |
| 2253 |
ctx.TexelH = (texture != nullptr && texture->GetHeight() > 0 ? 1.0f / float(texture->GetHeight()) : 0.0f); |
| 2254 |
ctx.Batched = batched; |
| 2255 |
ctx.Material = &material; |
| 2256 |
ctx.Coverage = (hasCoverage ? &coverage : nullptr); |
| 2257 |
ctx.MaterialBlend = materialBlend; |
| 2258 |
ctx.Px = px; |
| 2259 |
ctx.Py = py; |
| 2260 |
ctx.Pu = pu; |
| 2261 |
ctx.Pv = pvv; |
| 2262 |
ctx.TexRect = texRect; |
| 2263 |
ctx.AxisAligned = axisAligned; |
| 2264 |
// PRE-CLIP quad geometry: the raster origin of the sprite's (0,0) corner and the raster |
| 2265 |
// displacements of its local axes. Deliberately the synthesis inputs rather than the corner |
| 2266 |
// array, so geometry built from them cannot be distorted by a clipped quad. |
| 2267 |
ctx.OriginX = originX; |
| 2268 |
ctx.OriginY = originY; |
| 2269 |
ctx.AxisXx = spanXx; |
| 2270 |
ctx.AxisXy = spanXy; |
| 2271 |
ctx.AxisYx = spanYx; |
| 2272 |
ctx.AxisYy = spanYy; |
| 2273 |
ctx.Program = _currentProgram; |
| 2274 |
// Strip UVs arrive in the shader's texture space, so only the padded-store scale is folded in |
| 2275 |
ctx.UvScaleU = uvScaleU; |
| 2276 |
ctx.UvScaleV = uvScaleV; |
| 2277 |
|
| 2278 |
generated->Fn(ctx); |
| 2279 |
|
| 2280 |
} |
| 2281 |
} |
| 2282 |
|
| 2283 |
void GsDevice::DispatchTileMesh(PrimitiveType primitive, std::int32_t firstVertex, std::int32_t numVertices) |
| 2284 |
{ |
| 2285 |
// A tile-layer mesh is a plain triangle list of 8-float vertices (position.xy, texcoords.uv, |
| 2286 |
// color.rgba) - the layout TileMap::AppendTileQuad() writes and TileMapVs.inc declares. It is a hard |
| 2287 |
// contract of this shader family exactly like the std140 instance block is of the sprite one. |
| 2288 |
constexpr std::int32_t FloatsPerVertex = 8; |
| 2289 |
if (primitive != PrimitiveType::Triangles || numVertices < 3 || !_gsInitialized) { |
| 2290 |
return; |
| 2291 |
} |
| 2292 |
|
| 2293 |
const GsBuffer* vbo = _currentProgram->GetBoundVbo(); |
| 2294 |
if (vbo == nullptr) { |
| 2295 |
return; |
| 2296 |
} |
| 2297 |
const std::size_t firstFloat = (std::size_t(_currentProgram->GetBoundVboOffset()) / sizeof(float)) + |
| 2298 |
std::size_t(firstVertex) * FloatsPerVertex; |
| 2299 |
const std::size_t floatCount = std::size_t(numVertices) * FloatsPerVertex; |
| 2300 |
if ((firstFloat + floatCount) * sizeof(float) > vbo->GetSize()) { |
| 2301 |
return; |
| 2302 |
} |
| 2303 |
const float* DEATH_RESTRICT vertices = reinterpret_cast<const float*>(vbo->HostData()) + firstFloat; |
| 2304 |
|
| 2305 |
const GsUniformBlock* block = _currentProgram->FindBlock("InstanceBlock"); |
| 2306 |
if (block == nullptr) { |
| 2307 |
return; |
| 2308 |
} |
| 2309 |
std::int32_t binding = block->GetBindingIndex(); |
| 2310 |
if (binding < 0 || std::uint32_t(binding) >= MaxUniformBindings) { |
| 2311 |
binding = 0; |
| 2312 |
} |
| 2313 |
const std::uint8_t* blockData = _boundUniformRanges[binding].Data; |
| 2314 |
if (blockData == nullptr) { |
| 2315 |
return; |
| 2316 |
} |
| 2317 |
|
| 2318 |
GsTexture* texture = const_cast<GsTexture*>(_boundTextures[0]); |
| 2319 |
if (texture == nullptr) { |
| 2320 |
return; |
| 2321 |
} |
| 2322 |
|
| 2323 |
const std::uint8_t* projBytes = _currentProgram->GetResolvedProjection(); |
| 2324 |
const std::uint8_t* viewBytes = _currentProgram->GetResolvedView(); |
| 2325 |
const float* projMat = (projBytes != nullptr ? reinterpret_cast<const float*>(projBytes) : IdentityMatrix); |
| 2326 |
const float* viewMat = (viewBytes != nullptr ? reinterpret_cast<const float*>(viewBytes) : IdentityMatrix); |
| 2327 |
float pvm[16]; |
| 2328 |
Mat4Mul(projMat, viewMat, pvm); |
| 2329 |
Transform2D mvp; |
| 2330 |
Mat4MulTransform2D(pvm, reinterpret_cast<const float*>(blockData + kModelMatrixOffset), mvp); |
| 2331 |
|
| 2332 |
// The layer tint modulates every vertex colour, which already carries the per-tile alpha |
| 2333 |
float layerColor[4]; |
| 2334 |
std::memcpy(layerColor, blockData + kColorOffset, sizeof(layerColor)); |
| 2335 |
|
| 2336 |
// The palette to remap with is whatever the material bound to the palette sampler; the registered |
| 2337 |
// global palette is the fallback (mirrors the sprite path). TileMapMeshPalette binds uTexturePalette |
| 2338 |
// in its reflection, which is exactly what UsesPalette() reports. |
| 2339 |
const bool isPaletteRemap = _currentProgram->UsesPalette(); |
| 2340 |
const GsTexture* paletteTex = nullptr; |
| 2341 |
if (isPaletteRemap || texture->IsIndexed() || texture->NeedsPaletteBake()) { |
| 2342 |
paletteTex = _paletteTexture; |
| 2343 |
if (isPaletteRemap && _boundTextures[1] != nullptr && _boundTextures[1] != texture) { |
| 2344 |
paletteTex = _boundTextures[1]; |
| 2345 |
} |
| 2346 |
} |
| 2347 |
|
| 2348 |
// Unlike a sprite batch, the whole mesh shares one texture and one palette offset, so residency, the |
| 2349 |
// CLUT and the GS state are resolved once for the entire layer. The offset is a flat entry index, not |
| 2350 |
// a row (see ResolvePaletteEntries). |
| 2351 |
std::int32_t paletteOffset = 0; |
| 2352 |
if (isPaletteRemap) { |
| 2353 |
float palOffset = 0.0f; |
| 2354 |
std::memcpy(&palOffset, blockData + kPaletteOffsetOffset, sizeof(palOffset)); |
| 2355 |
paletteOffset = std::int32_t(palOffset + 0.5f); |
| 2356 |
} |
| 2357 |
|
| 2358 |
DrawState state; |
| 2359 |
const bool needsBake = texture->NeedsPaletteBake(); |
| 2360 |
std::uint32_t page; |
| 2361 |
if (needsBake) { |
| 2362 |
const std::uint32_t* entries = ResolvePaletteEntries(paletteTex, paletteOffset); |
| 2363 |
if (entries == nullptr) { |
| 2364 |
return; |
| 2365 |
} |
| 2366 |
page = texture->EnsureBakedColor(entries, std::uint32_t(paletteOffset), |
| 2367 |
PaletteVersionForOffset(paletteTex, paletteOffset), paletteTex); |
| 2368 |
} else { |
| 2369 |
page = texture->AcquireTexturePage(); |
| 2370 |
} |
| 2371 |
if (page == GsVram::InvalidPage) { |
| 2372 |
return; |
| 2373 |
} |
| 2374 |
state.Page = page; |
| 2375 |
state.BufferPitch = (needsBake ? texture->GetBakedBufferPitch() : texture->GetBufferPitch()); |
| 2376 |
state.Psm = (needsBake ? GS_PSM_32 : std::int32_t(texture->GetPsm())); |
| 2377 |
state.SampledWidth = texture->GetPaddedWidth(); |
| 2378 |
state.SampledHeight = texture->GetPaddedHeight(); |
| 2379 |
state.WrapU = MapWrapGs(texture->GetWrapS()); |
| 2380 |
state.WrapV = MapWrapGs(texture->GetWrapT()); |
| 2381 |
state.MagFilter = MapMagFilterGs(texture->GetMagFiltering()); |
| 2382 |
state.MinFilter = MapMinFilterGs(texture->GetMinFiltering()); |
| 2383 |
if (!needsBake && texture->IsIndexed()) { |
| 2384 |
state.ClutBlock = AcquireClutForOffset(paletteTex, paletteOffset); |
| 2385 |
if (state.ClutBlock == GsVram::InvalidBlock) { |
| 2386 |
return; |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
ApplyScissor(); |
| 2391 |
ApplyBlendEquation(MaterialEquation(_blending)); |
| 2392 |
|
| 2393 |
const Recti viewport = (_viewport.W > 0 && _viewport.H > 0) |
| 2394 |
? _viewport : Recti(0, 0, _logicalWidth, _logicalHeight); |
| 2395 |
float scaleX, scaleY, offsetX, offsetY; |
| 2396 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 2397 |
const bool screenPass = (_currentRenderTarget == nullptr); |
| 2398 |
|
| 2399 |
// The NDC-to-raster mapping is affine and constant for the whole mesh, so it is folded into the |
| 2400 |
// transform once instead of being reapplied per vertex. A screen pass mirrors NDC, which is just the |
| 2401 |
// sign of the Y scale. |
| 2402 |
const float rasterScaleX = 0.5f * float(viewport.W) * scaleX; |
| 2403 |
const float rasterBiasX = rasterScaleX + float(viewport.X) * scaleX + offsetX; |
| 2404 |
const float rasterScaleY = 0.5f * float(viewport.H) * scaleY * (screenPass ? 1.0f : -1.0f); |
| 2405 |
const float rasterBiasY = 0.5f * float(viewport.H) * scaleY + float(viewport.Y) * scaleY + offsetY; |
| 2406 |
const Transform2D raster = { |
| 2407 |
mvp.Xx * rasterScaleX, mvp.Xy * rasterScaleY, |
| 2408 |
mvp.Yx * rasterScaleX, mvp.Yy * rasterScaleY, |
| 2409 |
mvp.Tx * rasterScaleX + rasterBiasX, mvp.Ty * rasterScaleY + rasterBiasY |
| 2410 |
}; |
| 2411 |
// Axis-alignment is a property of the layer transform, not of an individual tile, so it is decided |
| 2412 |
// once: with no rotation or shear every tile quad is a GS SPRITE, which costs one primitive instead |
| 2413 |
// of the two triangles it would otherwise take. |
| 2414 |
const bool axisAligned = (raster.Xy == 0.0f && raster.Yx == 0.0f); |
| 2415 |
|
| 2416 |
const float uvScaleU = texture->GetUScale(); |
| 2417 |
const float uvScaleV = texture->GetVScale(); |
| 2418 |
auto project = [&](const float* v, float& outX, float& outY, float& outU, float& outV) { |
| 2419 |
outX = raster.Xx * v[0] + raster.Yx * v[1] + raster.Tx; |
| 2420 |
outY = raster.Xy * v[0] + raster.Yy * v[1] + raster.Ty; |
| 2421 |
outU = v[2] * uvScaleU; |
| 2422 |
outV = v[3] * uvScaleV; |
| 2423 |
}; |
| 2424 |
|
| 2425 |
const std::int32_t triangleCount = numVertices / 3; |
| 2426 |
std::int32_t triangle = 0; |
| 2427 |
// Virtually every tile of a layer carries the same colour (white at the layer's alpha), so the four |
| 2428 |
// clamp-and-quantize steps run once per change instead of once per tile |
| 2429 |
float lastColor[4] = { -1.0f, -1.0f, -1.0f, -1.0f }; |
| 2430 |
color_t packed{}; |
| 2431 |
while (triangle < triangleCount) { |
| 2432 |
// Tiles reach here as the six vertices of two triangles, of which the fourth and fifth repeat the |
| 2433 |
// first and third. Recognizing that pattern lets a tile go out as one quad. |
| 2434 |
const float* group = vertices + std::size_t(triangle) * 3 * FloatsPerVertex; |
| 2435 |
const float* v0 = group; |
| 2436 |
const float* v1 = group + FloatsPerVertex; |
| 2437 |
const float* v2 = group + 2 * FloatsPerVertex; |
| 2438 |
const float* v3 = group + 5 * FloatsPerVertex; |
| 2439 |
const bool isQuad = (triangle + 2 <= triangleCount && |
| 2440 |
group[3 * FloatsPerVertex + 0] == v0[0] && group[3 * FloatsPerVertex + 1] == v0[1] && |
| 2441 |
group[4 * FloatsPerVertex + 0] == v2[0] && group[4 * FloatsPerVertex + 1] == v2[1]); |
| 2442 |
// Whether that quad is a rectangle is a separate question, and the answer is not always yes: |
| 2443 |
// the destructible debris is appended to this same stream with its spin folded into its four |
| 2444 |
// corners (see TileMap::AppendDebrisQuad). Rebuilding one of those from two OPPOSITE corners |
| 2445 |
// collapses it onto its bounding box, which does not turn - it stays upright and its width |
| 2446 |
// runs to zero and back as the diagonal sweeps through vertical, so a spinning particle reads |
| 2447 |
// as one being squashed flat. |
| 2448 |
// |
| 2449 |
// Tested on the source vertices, so a tile pays eight compares and no extra transforms. That |
| 2450 |
// is equivalent to testing the projected ones: the layer transform is affine, so a source |
| 2451 |
// rectangle stays a rectangle exactly when the layer carries no rotation or shear, which is |
| 2452 |
// what axisAligned already says. |
| 2453 |
const bool isRect = (isQuad && axisAligned && |
| 2454 |
v1[1] == v0[1] && v2[0] == v1[0] && v3[0] == v0[0] && v3[1] == v2[1] && |
| 2455 |
v1[3] == v0[3] && v2[2] == v1[2] && v3[2] == v0[2] && v3[3] == v2[3]); |
| 2456 |
|
| 2457 |
if (group[4] != lastColor[0] || group[5] != lastColor[1] || group[6] != lastColor[2] || group[7] != lastColor[3]) { |
| 2458 |
lastColor[0] = group[4]; lastColor[1] = group[5]; lastColor[2] = group[6]; lastColor[3] = group[7]; |
| 2459 |
const float modulated[4] = { group[4] * layerColor[0], group[5] * layerColor[1], |
| 2460 |
group[6] * layerColor[2], group[7] * layerColor[3] }; |
| 2461 |
packed = PackPassColor(modulated); |
| 2462 |
} |
| 2463 |
|
| 2464 |
// SubmitQuadPrimitive indexes its corners by the sprite path's (ax, ay) weights - 0 = (1,0), |
| 2465 |
// 1 = (1,1), 2 = (0,0), 3 = (0,1) - which the mesh's vertex order maps onto as v1, v2, v0, v3. |
| 2466 |
if (isRect) { |
| 2467 |
// Two opposite corners describe the whole of it, so only those are transformed. The |
| 2468 |
// duplicated ones keep the axis-aligned test inside SubmitQuadPrimitive agreeing with the |
| 2469 |
// decision made here, and it goes out as a single GS SPRITE. |
| 2470 |
float tlX, tlY, tlU, tlV, brX, brY, brU, brV; |
| 2471 |
project(v0, tlX, tlY, tlU, tlV); |
| 2472 |
project(v2, brX, brY, brU, brV); |
| 2473 |
const float px[4] = { brX, brX, tlX, tlX }; |
| 2474 |
const float py[4] = { tlY, brY, tlY, brY }; |
| 2475 |
const float pu[4] = { brU, brU, tlU, tlU }; |
| 2476 |
const float pvv[4] = { tlV, brV, tlV, brV }; |
| 2477 |
SubmitQuadPrimitive(state, px, py, pu, pvv, packed, 0.0f, 0.0f, true); |
| 2478 |
triangle += 2; |
| 2479 |
} else if (isQuad) { |
| 2480 |
// Turned, or sheared by the layer: every corner has to be carried through, and the two |
| 2481 |
// triangles of a strip are what can draw it |
| 2482 |
float px[4], py[4], pu[4], pvv[4]; |
| 2483 |
project(v1, px[0], py[0], pu[0], pvv[0]); |
| 2484 |
project(v2, px[1], py[1], pu[1], pvv[1]); |
| 2485 |
project(v0, px[2], py[2], pu[2], pvv[2]); |
| 2486 |
project(v3, px[3], py[3], pu[3], pvv[3]); |
| 2487 |
SubmitQuadPrimitive(state, px, py, pu, pvv, packed, 0.0f, 0.0f, false); |
| 2488 |
triangle += 2; |
| 2489 |
} else { |
| 2490 |
// A lone triangle, from a mesh whose vertices do not pair up into quads at all |
| 2491 |
float sx[3], sy[3], tu[3], tv[3]; |
| 2492 |
for (std::int32_t i = 0; i < 3; i++) { |
| 2493 |
project(group + std::size_t(i) * FloatsPerVertex, sx[i], sy[i], tu[i], tv[i]); |
| 2494 |
} |
| 2495 |
SubmitTrianglePrimitive(state, sx, sy, tu, tv, packed, 0.0f, 0.0f); |
| 2496 |
triangle++; |
| 2497 |
} |
| 2498 |
} |
| 2499 |
} |
| 2500 |
|
| 2501 |
void GsDevice::DispatchLineStrip(std::int32_t firstVertex, std::int32_t numVertices) |
| 2502 |
{ |
| 2503 |
// The weapon wheel arrives as a textured line strip of 4-float vertices (position.xy, texcoords.uv) - |
| 2504 |
// the layout the MeshSprite shader's attributes declare. The GS has a native LINE_STRIP primitive, so |
| 2505 |
// unlike the PowerVR (which expands every segment into a thin quad) the strip goes out as it is. |
| 2506 |
constexpr std::int32_t FloatsPerVertex = 4; |
| 2507 |
if (numVertices < 2 || !_gsInitialized) { |
| 2508 |
return; |
| 2509 |
} |
| 2510 |
|
| 2511 |
const GsBuffer* vbo = _currentProgram->GetBoundVbo(); |
| 2512 |
if (vbo == nullptr) { |
| 2513 |
return; |
| 2514 |
} |
| 2515 |
const std::size_t firstFloat = (std::size_t(_currentProgram->GetBoundVboOffset()) / sizeof(float)) + |
| 2516 |
std::size_t(firstVertex) * FloatsPerVertex; |
| 2517 |
const std::size_t floatCount = std::size_t(numVertices) * FloatsPerVertex; |
| 2518 |
if ((firstFloat + floatCount) * sizeof(float) > vbo->GetSize()) { |
| 2519 |
return; |
| 2520 |
} |
| 2521 |
const float* DEATH_RESTRICT vertices = reinterpret_cast<const float*>(vbo->HostData()) + firstFloat; |
| 2522 |
|
| 2523 |
const GsUniformBlock* block = _currentProgram->FindBlock("InstanceBlock"); |
| 2524 |
if (block == nullptr) { |
| 2525 |
return; |
| 2526 |
} |
| 2527 |
std::int32_t binding = block->GetBindingIndex(); |
| 2528 |
if (binding < 0 || std::uint32_t(binding) >= MaxUniformBindings) { |
| 2529 |
binding = 0; |
| 2530 |
} |
| 2531 |
const std::uint8_t* blockData = _boundUniformRanges[binding].Data; |
| 2532 |
if (blockData == nullptr) { |
| 2533 |
return; |
| 2534 |
} |
| 2535 |
|
| 2536 |
GsTexture* texture = const_cast<GsTexture*>(_boundTextures[0]); |
| 2537 |
if (texture == nullptr) { |
| 2538 |
return; |
| 2539 |
} |
| 2540 |
|
| 2541 |
const std::uint8_t* projBytes = _currentProgram->GetResolvedProjection(); |
| 2542 |
const std::uint8_t* viewBytes = _currentProgram->GetResolvedView(); |
| 2543 |
const float* projMat = (projBytes != nullptr ? reinterpret_cast<const float*>(projBytes) : IdentityMatrix); |
| 2544 |
const float* viewMat = (viewBytes != nullptr ? reinterpret_cast<const float*>(viewBytes) : IdentityMatrix); |
| 2545 |
float pvMat[16]; |
| 2546 |
Mat4Mul(projMat, viewMat, pvMat); |
| 2547 |
Transform2D mvp; |
| 2548 |
Mat4MulTransform2D(pvMat, reinterpret_cast<const float*>(blockData + kModelMatrixOffset), mvp); |
| 2549 |
|
| 2550 |
// Every vertex of the strip carries the instance colour, so it is packed once |
| 2551 |
float color[4]; |
| 2552 |
std::memcpy(color, blockData + kColorOffset, sizeof(color)); |
| 2553 |
const color_t packed = PackPassColor(color); |
| 2554 |
|
| 2555 |
// The wheel's sheet is drawn at palette row 0 - it carries no per-instance palette offset. Unit 1 is |
| 2556 |
// only consulted when the program declares a palette sampler, for the reason spelled out in Dispatch |
| 2557 |
const GsTexture* paletteTex = nullptr; |
| 2558 |
if (texture->NeedsPaletteBake() || texture->IsIndexed()) { |
| 2559 |
paletteTex = _paletteTexture; |
| 2560 |
if (_currentProgram->UsesPalette() && _boundTextures[1] != nullptr && _boundTextures[1] != texture) { |
| 2561 |
paletteTex = _boundTextures[1]; |
| 2562 |
} |
| 2563 |
} |
| 2564 |
|
| 2565 |
DrawState state; |
| 2566 |
const bool needsBake = texture->NeedsPaletteBake(); |
| 2567 |
std::uint32_t page; |
| 2568 |
if (needsBake) { |
| 2569 |
const std::uint32_t* entries = ResolvePaletteEntries(paletteTex, 0); |
| 2570 |
if (entries == nullptr) { |
| 2571 |
return; |
| 2572 |
} |
| 2573 |
page = texture->EnsureBakedColor(entries, 0, PaletteVersionForOffset(paletteTex, 0), paletteTex); |
| 2574 |
} else { |
| 2575 |
page = texture->AcquireTexturePage(); |
| 2576 |
} |
| 2577 |
if (page == GsVram::InvalidPage) { |
| 2578 |
return; |
| 2579 |
} |
| 2580 |
state.Page = page; |
| 2581 |
state.BufferPitch = (needsBake ? texture->GetBakedBufferPitch() : texture->GetBufferPitch()); |
| 2582 |
state.Psm = (needsBake ? GS_PSM_32 : std::int32_t(texture->GetPsm())); |
| 2583 |
state.SampledWidth = texture->GetPaddedWidth(); |
| 2584 |
state.SampledHeight = texture->GetPaddedHeight(); |
| 2585 |
state.WrapU = MapWrapGs(texture->GetWrapS()); |
| 2586 |
state.WrapV = MapWrapGs(texture->GetWrapT()); |
| 2587 |
state.MagFilter = MapMagFilterGs(texture->GetMagFiltering()); |
| 2588 |
state.MinFilter = MapMinFilterGs(texture->GetMinFiltering()); |
| 2589 |
if (!needsBake && texture->IsIndexed()) { |
| 2590 |
state.ClutBlock = AcquireClutForOffset(paletteTex, 0); |
| 2591 |
if (state.ClutBlock == GsVram::InvalidBlock) { |
| 2592 |
return; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
|
| 2596 |
ApplyScissor(); |
| 2597 |
ApplyBlendEquation(MaterialEquation(_blending)); |
| 2598 |
|
| 2599 |
const Recti viewport = (_viewport.W > 0 && _viewport.H > 0) |
| 2600 |
? _viewport : Recti(0, 0, _logicalWidth, _logicalHeight); |
| 2601 |
float scaleX, scaleY, offsetX, offsetY; |
| 2602 |
GetTargetScale(scaleX, scaleY, offsetX, offsetY); |
| 2603 |
const bool screenPass = (_currentRenderTarget == nullptr); |
| 2604 |
const float rasterScaleX = 0.5f * float(viewport.W) * scaleX; |
| 2605 |
const float rasterBiasX = rasterScaleX + float(viewport.X) * scaleX + offsetX; |
| 2606 |
const float rasterScaleY = 0.5f * float(viewport.H) * scaleY * (screenPass ? 1.0f : -1.0f); |
| 2607 |
const float rasterBiasY = 0.5f * float(viewport.H) * scaleY + float(viewport.Y) * scaleY + offsetY; |
| 2608 |
const Transform2D raster = { |
| 2609 |
mvp.Xx * rasterScaleX, mvp.Xy * rasterScaleY, |
| 2610 |
mvp.Yx * rasterScaleX, mvp.Yy * rasterScaleY, |
| 2611 |
mvp.Tx * rasterScaleX + rasterBiasX, mvp.Ty * rasterScaleY + rasterBiasY |
| 2612 |
}; |
| 2613 |
const float uvScaleU = texture->GetUScale(); |
| 2614 |
const float uvScaleV = texture->GetVScale(); |
| 2615 |
|
| 2616 |
// Emitted in chunks so the scratch stays a fixed size; consecutive chunks repeat the joining vertex, |
| 2617 |
// which is what keeps a split strip continuous |
| 2618 |
constexpr std::int32_t MaxChunk = 96; |
| 2619 |
float sx[MaxChunk], sy[MaxChunk], tu[MaxChunk], tv[MaxChunk]; |
| 2620 |
std::int32_t first = 0; |
| 2621 |
while (first + 1 < numVertices) { |
| 2622 |
const std::int32_t count = (numVertices - first < MaxChunk ? numVertices - first : MaxChunk); |
| 2623 |
for (std::int32_t i = 0; i < count; i++) { |
| 2624 |
const float* src = vertices + std::size_t(first + i) * FloatsPerVertex; |
| 2625 |
sx[i] = raster.Xx * src[0] + raster.Yx * src[1] + raster.Tx; |
| 2626 |
sy[i] = raster.Xy * src[0] + raster.Yy * src[1] + raster.Ty; |
| 2627 |
tu[i] = src[2] * uvScaleU; |
| 2628 |
tv[i] = src[3] * uvScaleV; |
| 2629 |
} |
| 2630 |
SubmitLineStripPrimitive(state, sx, sy, tu, tv, packed, count); |
| 2631 |
first += count - 1; |
| 2632 |
} |
| 2633 |
} |
| 2634 |
} |