TransformUnit.cpp 13 KB

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