| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Software/TransformUnit.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <cmath> |
| 9 | #include <cstring> |
| 10 | |
| 11 | #include "Common/Assert.h" |
| 12 | #include "Common/CommonTypes.h" |
| 13 | #include "Common/Logging/Log.h" |
| 14 | #include "Common/Matrix.h" |
| 15 | #include "Common/MsgHandler.h" |
| 16 | #include "Common/Swap.h" |
| 17 | |
| 18 | #include "VideoBackends/Software/NativeVertexFormat.h" |
| 19 | |
| 20 | #include "VideoCommon/BPMemory.h" |
| 21 | #include "VideoCommon/XFMemory.h" |
| 22 | |
| 23 | namespace TransformUnit |
| 24 | { |
| 25 | |
| 26 | using Vec3 = Common::Vec3; |
| 27 | using Vec4 = Common::Vec4; |
| 28 | |
| 29 | static void MultiplyVec2Mat24(const Vec3& vec, const float* mat, Vec3& result) |
| 30 | { |
| 31 | result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] + mat[3]; |
| 32 | result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] + mat[7]; |
| 33 | result.z = 1.0f; |
| 34 | } |
| 35 | |
| 36 | static void MultiplyVec2Mat34(const Vec3& vec, const float* mat, Vec3& result) |
| 37 | { |
| 38 | result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] + mat[3]; |
| 39 | result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] + mat[7]; |
| 40 | result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] + mat[11]; |
| 41 | } |
| 42 | |
| 43 | static void MultiplyVec3Mat33(const Vec3& vec, const float* mat, Vec3& result) |
| 44 | { |
| 45 | result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z; |
| 46 | result.y = mat[3] * vec.x + mat[4] * vec.y + mat[5] * vec.z; |
| 47 | result.z = mat[6] * vec.x + mat[7] * vec.y + mat[8] * vec.z; |
| 48 | } |
| 49 | |
| 50 | static void MultiplyVec3Mat24(const Vec3& vec, const float* mat, Vec3& result) |
| 51 | { |
| 52 | result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z + mat[3]; |
| 53 | result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z + mat[7]; |
| 54 | result.z = 1.0f; |
| 55 | } |
| 56 | |
| 57 | static void MultiplyVec3Mat34(const Vec3& vec, const float* mat, Vec3& result) |
| 58 | { |
| 59 | result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z + mat[3]; |
| 60 | result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z + mat[7]; |
| 61 | result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] * vec.z + mat[11]; |
| 62 | } |
| 63 | |
| 64 | static void MultipleVec3Perspective(const Vec3& vec, const Projection::Raw& proj, Vec4& result) |
| 65 | { |
| 66 | result.x = proj[0] * vec.x + proj[1] * vec.z; |
| 67 | result.y = proj[2] * vec.y + proj[3] * vec.z; |
| 68 | // result.z = (proj[4] * vec.z + proj[5]); |
| 69 | result.z = (proj[4] * vec.z + proj[5]) * (1.0f - (float)1e-7); |
| 70 | result.w = -vec.z; |
| 71 | } |
| 72 | |
| 73 | static void MultipleVec3Ortho(const Vec3& vec, const Projection::Raw& proj, Vec4& result) |
| 74 | { |
| 75 | result.x = proj[0] * vec.x + proj[1]; |
| 76 | result.y = proj[2] * vec.y + proj[3]; |
| 77 | result.z = proj[4] * vec.z + proj[5]; |
| 78 | result.w = 1; |
| 79 | } |
| 80 | |
| 81 | void TransformPosition(const InputVertexData* src, OutputVertexData* dst) |
| 82 | { |
| 83 | const float* mat = &xfmem.posMatrices[src->posMtx * 4]; |
| 84 | MultiplyVec3Mat34(src->position, mat, dst->mvPosition); |
| 85 | |
| 86 | if (xfmem.projection.type == ProjectionType::Perspective) |
| 87 | { |
| 88 | MultipleVec3Perspective(dst->mvPosition, xfmem.projection.rawProjection, |
| 89 | dst->projectedPosition); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | MultipleVec3Ortho(dst->mvPosition, xfmem.projection.rawProjection, dst->projectedPosition); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void TransformNormal(const InputVertexData* src, OutputVertexData* dst) |
| 98 | { |
| 99 | const float* mat = &xfmem.normalMatrices[(src->posMtx & 31) * 3]; |
| 100 | |
| 101 | MultiplyVec3Mat33(src->normal[0], mat, dst->normal[0]); |
| 102 | MultiplyVec3Mat33(src->normal[1], mat, dst->normal[1]); |
| 103 | MultiplyVec3Mat33(src->normal[2], mat, dst->normal[2]); |
| 104 | // The scale of the transform matrix is used to control the size of the emboss map effect, by |
| 105 | // changing the scale of the transformed binormals (which only get used by emboss map texgens). |
| 106 | // By normalising the first transformed normal (which is used by lighting calculations and needs |
| 107 | // to be unit length), the same transform matrix can do double duty, scaling for emboss mapping, |
| 108 | // and not scaling for lighting. |
| 109 | dst->normal[0] = dst->normal[0].Normalized(); |
| 110 | } |
| 111 | |
| 112 | static void TransformTexCoordRegular(const TexMtxInfo& texinfo, int coordNum, |
| 113 | const InputVertexData* srcVertex, OutputVertexData* dstVertex) |
| 114 | { |
| 115 | Vec3 src; |
| 116 | switch (texinfo.sourcerow) |
| 117 | { |
| 118 | case SourceRow::Geom: |
| 119 | src = srcVertex->position; |
| 120 | break; |
| 121 | case SourceRow::Normal: |
| 122 | src = srcVertex->normal[0]; |
| 123 | break; |
| 124 | case SourceRow::BinormalT: |
| 125 | src = srcVertex->normal[1]; |
| 126 | break; |
| 127 | case SourceRow::BinormalB: |
| 128 | src = srcVertex->normal[2]; |
| 129 | break; |
| 130 | default: |
| 131 | { |
| 132 | ASSERT(texinfo.sourcerow >= SourceRow::Tex0 && texinfo.sourcerow <= SourceRow::Tex7); |
| 133 | u32 texnum = static_cast<u32>(texinfo.sourcerow.Value()) - static_cast<u32>(SourceRow::Tex0); |
| 134 | src.x = srcVertex->texCoords[texnum].x; |
| 135 | src.y = srcVertex->texCoords[texnum].y; |
| 136 | src.z = 1.0f; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Convert NaNs to 1 - needed to fix eyelids in Shadow the Hedgehog during cutscenes |
| 142 | // See https://bugs.dolphin-emu.org/issues/11458 |
| 143 | if (std::isnan(src.x)) |
| 144 | src.x = 1; |
| 145 | if (std::isnan(src.y)) |
| 146 | src.y = 1; |
| 147 | if (std::isnan(src.z)) |
| 148 | src.z = 1; |
| 149 | |
| 150 | const float* mat = &xfmem.posMatrices[srcVertex->texMtx[coordNum] * 4]; |
| 151 | Vec3* dst = &dstVertex->texCoords[coordNum]; |
| 152 | |
| 153 | if (texinfo.projection == TexSize::ST) |
| 154 | { |
| 155 | if (texinfo.inputform == TexInputForm::AB11) |
| 156 | MultiplyVec2Mat24(src, mat, *dst); |
| 157 | else |
| 158 | MultiplyVec3Mat24(src, mat, *dst); |
| 159 | } |
| 160 | else // texinfo.projection == TexSize::STQ |
| 161 | { |
| 162 | if (texinfo.inputform == TexInputForm::AB11) |
| 163 | MultiplyVec2Mat34(src, mat, *dst); |
| 164 | else |
| 165 | MultiplyVec3Mat34(src, mat, *dst); |
| 166 | } |
| 167 | |
| 168 | if (xfmem.dualTexTrans.enabled) |
| 169 | { |
| 170 | Vec3 tempCoord; |
| 171 | |
| 172 | // normalize |
| 173 | const PostMtxInfo& postInfo = xfmem.postMtxInfo[coordNum]; |
| 174 | const float* postMat = &xfmem.postMatrices[postInfo.index * 4]; |
| 175 | |
| 176 | if (postInfo.normalize) |
| 177 | tempCoord = dst->Normalized(); |
| 178 | else |
| 179 | tempCoord = *dst; |
| 180 | |
| 181 | MultiplyVec3Mat34(tempCoord, postMat, *dst); |
| 182 | } |
| 183 | |
| 184 | // When q is 0, the GameCube appears to have a special case |
| 185 | // This can be seen in devkitPro's neheGX Lesson08 example for Wii |
| 186 | // Makes differences in Rogue Squadron 3 (Hoth sky) and The Last Story (shadow culling) |
| 187 | if (dst->z == 0.0f) |
| 188 | { |
| 189 | dst->x = std::clamp(dst->x / 2.0f, -1.0f, 1.0f); |
| 190 | dst->y = std::clamp(dst->y / 2.0f, -1.0f, 1.0f); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | struct LightPointer |
| 195 | { |
| 196 | u32 reserved[3]; |
| 197 | u8 color[4]; |
| 198 | Vec3 cosatt; |
| 199 | Vec3 distatt; |
| 200 | Vec3 pos; |
| 201 | Vec3 dir; |
| 202 | }; |
| 203 | |
| 204 | static inline void AddScaledIntegerColor(const u8* src, float scale, Vec3& dst) |
| 205 | { |
| 206 | dst.x += src[1] * scale; |
| 207 | dst.y += src[2] * scale; |
| 208 | dst.z += src[3] * scale; |
| 209 | } |
| 210 | |
| 211 | static inline float SafeDivide(float n, float d) |
| 212 | { |
| 213 | return (d == 0) ? (n > 0 ? 1 : 0) : n / d; |
| 214 | } |
| 215 | |
| 216 | static float CalculateLightAttn(const LightPointer* light, Vec3* _ldir, const Vec3& normal, |
| 217 | const LitChannel& chan) |
| 218 | { |
| 219 | float attn = 1.0f; |
| 220 | Vec3& ldir = *_ldir; |
| 221 | |
| 222 | switch (chan.attnfunc) |
| 223 | { |
| 224 | case AttenuationFunc::None: |
| 225 | case AttenuationFunc::Dir: |
| 226 | { |
| 227 | ldir = ldir.Normalized(); |
| 228 | if (ldir == Vec3(0.0f, 0.0f, 0.0f)) |
| 229 | ldir = normal; |
| 230 | break; |
| 231 | } |
| 232 | case AttenuationFunc::Spec: |
| 233 | { |
| 234 | ldir = ldir.Normalized(); |
| 235 | attn = ldir.Dot(normal) >= 0.0 ? std::max(0.0f, light->dir.Dot(normal)) : 0; |
| 236 | Vec3 attLen = Vec3(1.0, attn, attn * attn); |
| 237 | Vec3 cosAttn = light->cosatt; |
| 238 | Vec3 distAttn = light->distatt; |
| 239 | if (chan.diffusefunc != DiffuseFunc::None) |
| 240 | distAttn = distAttn.Normalized(); |
| 241 | |
| 242 | attn = SafeDivide(std::max(0.0f, attLen.Dot(cosAttn)), attLen.Dot(distAttn)); |
| 243 | break; |
| 244 | } |
| 245 | case AttenuationFunc::Spot: |
| 246 | { |
| 247 | float dist2 = ldir.LengthSquared(); |
| 248 | float dist = sqrtf(dist2); |
| 249 | ldir = ldir / dist; |
| 250 | attn = std::max(0.0f, ldir.Dot(light->dir)); |
| 251 | |
| 252 | float cosAtt = light->cosatt.x + (light->cosatt.y * attn) + (light->cosatt.z * attn * attn); |
| 253 | float distAtt = light->distatt.x + (light->distatt.y * dist) + (light->distatt.z * dist2); |
| 254 | attn = SafeDivide(std::max(0.0f, cosAtt), distAtt); |
| 255 | break; |
| 256 | } |
| 257 | default: |
| 258 | PanicAlertFmt("Invalid attnfunc: {}", chan.attnfunc); |
| 259 | } |
| 260 | |
| 261 | return attn; |
| 262 | } |
| 263 | |
| 264 | static void LightColor(const Vec3& pos, const Vec3& normal, u8 lightNum, const LitChannel& chan, |
| 265 | Vec3& lightCol) |
| 266 | { |
| 267 | const LightPointer* light = (const LightPointer*)&xfmem.lights[lightNum]; |
| 268 | |
| 269 | Vec3 ldir = light->pos - pos; |
| 270 | float attn = CalculateLightAttn(light, &ldir, normal, chan); |
| 271 | |
| 272 | float difAttn = ldir.Dot(normal); |
| 273 | switch (chan.diffusefunc) |
| 274 | { |
| 275 | case DiffuseFunc::None: |
| 276 | AddScaledIntegerColor(light->color, attn, lightCol); |
| 277 | break; |
| 278 | case DiffuseFunc::Sign: |
| 279 | AddScaledIntegerColor(light->color, attn * difAttn, lightCol); |
| 280 | break; |
| 281 | case DiffuseFunc::Clamp: |
| 282 | difAttn = std::max(0.0f, difAttn); |
| 283 | AddScaledIntegerColor(light->color, attn * difAttn, lightCol); |
| 284 | break; |
| 285 | default: |
| 286 | PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static void LightAlpha(const Vec3& pos, const Vec3& normal, u8 lightNum, const LitChannel& chan, |
| 291 | float& lightCol) |
| 292 | { |
| 293 | const LightPointer* light = (const LightPointer*)&xfmem.lights[lightNum]; |
| 294 | |
| 295 | Vec3 ldir = light->pos - pos; |
| 296 | float attn = CalculateLightAttn(light, &ldir, normal, chan); |
| 297 | |
| 298 | float difAttn = ldir.Dot(normal); |
| 299 | switch (chan.diffusefunc) |
| 300 | { |
| 301 | case DiffuseFunc::None: |
| 302 | lightCol += light->color[0] * attn; |
| 303 | break; |
| 304 | case DiffuseFunc::Sign: |
| 305 | lightCol += light->color[0] * attn * difAttn; |
| 306 | break; |
| 307 | case DiffuseFunc::Clamp: |
| 308 | difAttn = std::max(0.0f, difAttn); |
| 309 | lightCol += light->color[0] * attn * difAttn; |
| 310 | break; |
| 311 | default: |
| 312 | PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void TransformColor(const InputVertexData* src, OutputVertexData* dst) |
| 317 | { |
| 318 | for (u32 chan = 0; chan < NUM_XF_COLOR_CHANNELS; chan++) |
| 319 | { |
| 320 | // abgr |
| 321 | std::array<u8, 4> matcolor; |
| 322 | std::array<u8, 4> chancolor; |
| 323 | |
| 324 | // color |
| 325 | const LitChannel& colorchan = xfmem.color[chan]; |
| 326 | if (colorchan.matsource == MatSource::Vertex) |
| 327 | matcolor = src->color[chan]; |
| 328 | else |
| 329 | std::memcpy(matcolor.data(), &xfmem.matColor[chan], sizeof(u32)); |
| 330 | |
| 331 | if (colorchan.enablelighting) |
| 332 | { |
| 333 | Vec3 lightCol; |
| 334 | if (colorchan.ambsource == AmbSource::Vertex) |
| 335 | { |
| 336 | lightCol.x = src->color[chan][1]; |
| 337 | lightCol.y = src->color[chan][2]; |
| 338 | lightCol.z = src->color[chan][3]; |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | const u8* ambColor = reinterpret_cast<u8*>(&xfmem.ambColor[chan]); |
| 343 | lightCol.x = ambColor[1]; |
| 344 | lightCol.y = ambColor[2]; |
| 345 | lightCol.z = ambColor[3]; |
| 346 | } |
| 347 | |
| 348 | u8 mask = colorchan.GetFullLightMask(); |
| 349 | for (int i = 0; i < 8; ++i) |
| 350 | { |
| 351 | if (mask & (1 << i)) |
| 352 | LightColor(dst->mvPosition, dst->normal[0], i, colorchan, lightCol); |
| 353 | } |
| 354 | |
| 355 | int light_x = std::clamp(static_cast<int>(lightCol.x), 0, 255); |
| 356 | int light_y = std::clamp(static_cast<int>(lightCol.y), 0, 255); |
| 357 | int light_z = std::clamp(static_cast<int>(lightCol.z), 0, 255); |
| 358 | chancolor[1] = (matcolor[1] * (light_x + (light_x >> 7))) >> 8; |
| 359 | chancolor[2] = (matcolor[2] * (light_y + (light_y >> 7))) >> 8; |
| 360 | chancolor[3] = (matcolor[3] * (light_z + (light_z >> 7))) >> 8; |
| 361 | } |
| 362 | else |
| 363 | { |
| 364 | chancolor = matcolor; |
| 365 | } |
| 366 | |
| 367 | // alpha |
| 368 | const LitChannel& alphachan = xfmem.alpha[chan]; |
| 369 | if (alphachan.matsource == MatSource::Vertex) |
| 370 | matcolor[0] = src->color[chan][0]; |
| 371 | else |
| 372 | matcolor[0] = xfmem.matColor[chan] & 0xff; |
| 373 | |
| 374 | if (xfmem.alpha[chan].enablelighting) |
| 375 | { |
| 376 | float lightCol; |
| 377 | if (alphachan.ambsource == AmbSource::Vertex) |
| 378 | lightCol = src->color[chan][0]; |
| 379 | else |
| 380 | lightCol = static_cast<float>(xfmem.ambColor[chan] & 0xff); |
| 381 | |
| 382 | u8 mask = alphachan.GetFullLightMask(); |
| 383 | for (int i = 0; i < 8; ++i) |
| 384 | { |
| 385 | if (mask & (1 << i)) |
| 386 | LightAlpha(dst->mvPosition, dst->normal[0], i, alphachan, lightCol); |
| 387 | } |
| 388 | |
| 389 | int light_a = std::clamp(static_cast<int>(lightCol), 0, 255); |
| 390 | chancolor[0] = (matcolor[0] * (light_a + (light_a >> 7))) >> 8; |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | chancolor[0] = matcolor[0]; |
| 395 | } |
| 396 | |
| 397 | // abgr -> rgba |
| 398 | const u32 rgba_color = Common::swap32(chancolor.data()); |
| 399 | std::memcpy(dst->color[chan].data(), &rgba_color, sizeof(u32)); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void TransformTexCoord(const InputVertexData* src, OutputVertexData* dst) |
| 404 | { |
| 405 | for (u32 coordNum = 0; coordNum < xfmem.numTexGen.numTexGens; coordNum++) |
| 406 | { |
| 407 | const TexMtxInfo& texinfo = xfmem.texMtxInfo[coordNum]; |
| 408 | |
| 409 | switch (texinfo.texgentype) |
| 410 | { |
| 411 | case TexGenType::Regular: |
| 412 | TransformTexCoordRegular(texinfo, coordNum, src, dst); |
| 413 | break; |
| 414 | case TexGenType::EmbossMap: |
| 415 | { |
| 416 | const LightPointer* light = (const LightPointer*)&xfmem.lights[texinfo.embosslightshift]; |
| 417 | |
| 418 | Vec3 ldir = (light->pos - dst->mvPosition).Normalized(); |
| 419 | float d1 = ldir.Dot(dst->normal[1]); |
| 420 | float d2 = ldir.Dot(dst->normal[2]); |
| 421 | |
| 422 | dst->texCoords[coordNum].x = dst->texCoords[texinfo.embosssourceshift].x + d1; |
| 423 | dst->texCoords[coordNum].y = dst->texCoords[texinfo.embosssourceshift].y + d2; |
| 424 | dst->texCoords[coordNum].z = dst->texCoords[texinfo.embosssourceshift].z; |
| 425 | } |
| 426 | break; |
| 427 | case TexGenType::Color0: |
| 428 | ASSERT(texinfo.inputform == TexInputForm::AB11); |
| 429 | dst->texCoords[coordNum].x = (float)dst->color[0][0] / 255.0f; |
| 430 | dst->texCoords[coordNum].y = (float)dst->color[0][1] / 255.0f; |
| 431 | dst->texCoords[coordNum].z = 1.0f; |
| 432 | break; |
| 433 | case TexGenType::Color1: |
| 434 | ASSERT(texinfo.inputform == TexInputForm::AB11); |
| 435 | dst->texCoords[coordNum].x = (float)dst->color[1][0] / 255.0f; |
| 436 | dst->texCoords[coordNum].y = (float)dst->color[1][1] / 255.0f; |
| 437 | dst->texCoords[coordNum].z = 1.0f; |
| 438 | break; |
| 439 | default: |
| 440 | ERROR_LOG_FMT(VIDEO, "Bad tex gen type {}", texinfo.texgentype); |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | for (u32 coordNum = 0; coordNum < xfmem.numTexGen.numTexGens; coordNum++) |
| 446 | { |
| 447 | dst->texCoords[coordNum].x *= bpmem.texcoords[coordNum].s.scale_minus_1 + 1; |
| 448 | dst->texCoords[coordNum].y *= bpmem.texcoords[coordNum].t.scale_minus_1 + 1; |
| 449 | } |
| 450 | } |
| 451 | } // namespace TransformUnit |