Advertisement
xerpi

GSH_GXM PS2 PSVita

Mar 30th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.51 KB | None | 0 0
  1. #include <cstring>
  2. #include "GSH_GXM.h"
  3. #include "gxm.h"
  4. #include "../../Log.h"
  5.  
  6. #include "../../ui_vita/netlog.h"
  7. #include <psp2/kernel/threadmgr.h>
  8.  
  9. #define LOG(...) netlogf(__VA_ARGS__)
  10.  
  11. static inline uint32_t rgba_u32_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
  12. {
  13.     return (a << 24) | (b << 16) | (g << 8) | (r);
  14. }
  15.  
  16. CGSH_GXM::CGSH_GXM()
  17. {
  18.     LOG("CGSH_GXM start\n");
  19. }
  20.  
  21. CGSH_GXM::~CGSH_GXM()
  22. {
  23. }
  24.  
  25. void CGSH_GXM::InitializeImpl()
  26. {
  27.     gxm_init();
  28.  
  29.     PRESENTATION_PARAMS presentationParams;
  30.     presentationParams.mode = CGSHandler::PRESENTATION_MODE::PRESENTATION_MODE_ORIGINAL;
  31.     presentationParams.windowWidth = 960;
  32.     presentationParams.windowHeight = 544;
  33.  
  34.     SetPresentationParams(presentationParams);
  35. }
  36.  
  37. void CGSH_GXM::ReleaseImpl()
  38. {
  39.     gxm_finish();
  40. }
  41.  
  42. void CGSH_GXM::ProcessHostToLocalTransfer()
  43. {
  44. }
  45.  
  46. void CGSH_GXM::ProcessLocalToHostTransfer()
  47. {
  48. }
  49.  
  50. void CGSH_GXM::ProcessLocalToLocalTransfer()
  51. {
  52. }
  53.  
  54. void CGSH_GXM::ProcessClutTransfer(uint32, uint32)
  55. {
  56. }
  57.  
  58. void CGSH_GXM::ReadFramebuffer(uint32, uint32, void*)
  59. {
  60. }
  61.  
  62. static float CalcZ(float nZ)
  63. {
  64.     // 32768.0f
  65.     // 8388608.0f
  66.     // 2147483647.0f
  67.     constexpr float m_nMaxZ = 8388608.0f;
  68.     if (nZ < 256){
  69.         //The number is small, so scale to a smaller ratio (65536)
  70.         return nZ / 32768.0f;
  71.     } else {
  72.         //      nZ -= m_nMaxZ;
  73.         if(nZ > m_nMaxZ) return 1.0;
  74.         //      if(nZ < -m_nMaxZ) return -1.0;
  75.         return nZ / m_nMaxZ;
  76.     }
  77. }
  78.  
  79.  
  80. void CGSH_GXM::Prim_Triangle()
  81. {
  82.     XYZ pos[3];
  83.     pos[0] <<= m_vtxBuffer[2].position;
  84.     pos[1] <<= m_vtxBuffer[1].position;
  85.     pos[2] <<= m_vtxBuffer[0].position;
  86.  
  87.     float x1 = pos[0].GetX(), x2 = pos[1].GetX(), x3 = pos[2].GetX();
  88.     float y1 = pos[0].GetY(), y2 = pos[1].GetY(), y3 = pos[2].GetY();
  89.     uint32 z1 = pos[0].nZ, z2 = pos[1].nZ, z3 = pos[2].nZ;
  90.  
  91.     RGBAQ rgbaq[3];
  92.     rgbaq[0] <<= m_vtxBuffer[2].rgbaq;
  93.     rgbaq[1] <<= m_vtxBuffer[1].rgbaq;
  94.     rgbaq[2] <<= m_vtxBuffer[0].rgbaq;
  95.  
  96.     x1 -= m_primOfsX;
  97.     x2 -= m_primOfsX;
  98.     x3 -= m_primOfsX;
  99.  
  100.     y1 -= m_primOfsY;
  101.     y2 -= m_primOfsY;
  102.     y3 -= m_primOfsY;
  103.  
  104.     float s[3] = {0, 0, 0};
  105.     float t[3] = {0, 0, 0};
  106.     float q[3] = {1, 1, 1};
  107.  
  108.     float f[3] = {0, 0, 0};
  109.  
  110.     if (m_primitiveMode.nFog) {
  111.         f[0] = static_cast<float>(0xFF - m_vtxBuffer[2].fog) / 255.0f;
  112.         f[1] = static_cast<float>(0xFF - m_vtxBuffer[1].fog) / 255.0f;
  113.         f[2] = static_cast<float>(0xFF - m_vtxBuffer[0].fog) / 255.0f;
  114.     }
  115.  
  116.     if (m_primitiveMode.nTexture) {
  117.         if(m_primitiveMode.nUseUV) {
  118.             UV uv[3];
  119.             uv[0] <<= m_vtxBuffer[2].uv;
  120.             uv[1] <<= m_vtxBuffer[1].uv;
  121.             uv[2] <<= m_vtxBuffer[0].uv;
  122.  
  123.             s[0] = uv[0].GetU() / static_cast<float>(m_texWidth);
  124.             s[1] = uv[1].GetU() / static_cast<float>(m_texWidth);
  125.             s[2] = uv[2].GetU() / static_cast<float>(m_texWidth);
  126.  
  127.             t[0] = uv[0].GetV() / static_cast<float>(m_texHeight);
  128.             t[1] = uv[1].GetV() / static_cast<float>(m_texHeight);
  129.             t[2] = uv[2].GetV() / static_cast<float>(m_texHeight);
  130.         } else {
  131.             ST st[3];
  132.             st[0] <<= m_vtxBuffer[2].st;
  133.             st[1] <<= m_vtxBuffer[1].st;
  134.             st[2] <<= m_vtxBuffer[0].st;
  135.  
  136.             s[0] = st[0].nS;
  137.             s[1] = st[1].nS;
  138.             s[2] = st[2].nS;
  139.             t[0] = st[0].nT;
  140.             t[1] = st[1].nT;
  141.             t[2] = st[2].nT;
  142.  
  143.             q[0] = rgbaq[0].nQ;
  144.             q[1] = rgbaq[1].nQ;
  145.             q[2] = rgbaq[2].nQ;
  146.         }
  147.     }
  148.  
  149.     auto color1 = rgba_u32_color(rgbaq[0].nR, rgbaq[0].nG, rgbaq[0].nB, rgbaq[0].nA);
  150.     auto color2 = rgba_u32_color(rgbaq[1].nR, rgbaq[1].nG, rgbaq[1].nB, rgbaq[1].nA);
  151.     auto color3 = rgba_u32_color(rgbaq[2].nR, rgbaq[2].nG, rgbaq[2].nB, rgbaq[2].nA);
  152.  
  153.     // Flat shading
  154.     if (m_primitiveMode.nShading == 0)
  155.         color1 = color2 = color3;
  156.  
  157.     struct color_vertex vertices[3] = {
  158.         { .position = {x1, y1, (float)z1}, .color = color1 },
  159.         { .position = {x2, y2, (float)z2}, .color = color2 },
  160.         { .position = {x3, y3, (float)z3}, .color = color3 }
  161.     };
  162.  
  163.     gxm_vertex_buffer_push(vertices, 3);
  164.  
  165.     /*vertex.position.x = x;
  166.     vertex.position.y = y;
  167.     vertex.position.z = 1.0f - z;
  168.     vertex.color.r = rgbaq.nR;
  169.     vertex.color.g = rgbaq.nG;
  170.     vertex.color.b = rgbaq.nB;
  171.     vertex.color.a = rgbaq.nA;*/
  172.  
  173.     /*LOG("TFM {%f, %f, %f}, {%f, %f, %f}\n",
  174.         vertex.position.x, vertex.position.y, vertex.position.z,
  175.         vertex.color.r, vertex.color.g, vertex.color.b);*/
  176.  
  177.     //gxm_vertex_buffer_push(&vertex);
  178.  
  179.     /*CDraw::PRIM_VERTEX vertices[] =
  180.     {
  181.         {   x1, y1, z1, color1, s[0], t[0], q[0], f[0]},
  182.         {   x2, y2, z2, color2, s[1], t[1], q[1], f[1]},
  183.         {   x3, y3, z3, color3, s[2], t[2], q[2], f[2]},
  184.     };*/
  185.  
  186.     //m_draw->AddVertices(std::begin(vertices), std::end(vertices));
  187. }
  188.  
  189. void CGSH_GXM::VertexKick(uint8 registerId, uint64 data)
  190. {
  191.     if (m_vtxCount == 0)
  192.         return;
  193.  
  194.     bool drawingKick = (registerId == GS_REG_XYZ2) || (registerId == GS_REG_XYZF2);
  195.     bool fog = (registerId == GS_REG_XYZF2) || (registerId == GS_REG_XYZF3);
  196.  
  197.     if (fog) {
  198.         m_vtxBuffer[m_vtxCount - 1].position = data & 0x00FFFFFFFFFFFFFFULL;
  199.         m_vtxBuffer[m_vtxCount - 1].rgbaq = m_nReg[GS_REG_RGBAQ];
  200.         m_vtxBuffer[m_vtxCount - 1].uv = m_nReg[GS_REG_UV];
  201.         m_vtxBuffer[m_vtxCount - 1].st = m_nReg[GS_REG_ST];
  202.         m_vtxBuffer[m_vtxCount - 1].fog = static_cast<uint8>(data >> 56);
  203.     } else {
  204.         m_vtxBuffer[m_vtxCount - 1].position = data;
  205.         m_vtxBuffer[m_vtxCount - 1].rgbaq = m_nReg[GS_REG_RGBAQ];
  206.         m_vtxBuffer[m_vtxCount - 1].uv = m_nReg[GS_REG_UV];
  207.         m_vtxBuffer[m_vtxCount - 1].st = m_nReg[GS_REG_ST];
  208.         m_vtxBuffer[m_vtxCount - 1].fog = static_cast<uint8>(m_nReg[GS_REG_FOG] >> 56);
  209.     }
  210.  
  211.     m_vtxCount--;
  212.  
  213.     if(m_vtxCount == 0) {
  214.         if ((m_nReg[GS_REG_PRMODECONT] & 1) != 0)
  215.             m_primitiveMode <<= m_nReg[GS_REG_PRIM];
  216.         else
  217.             m_primitiveMode <<= m_nReg[GS_REG_PRMODE];
  218.  
  219.         if (drawingKick)
  220.             SetRenderingContext(m_primitiveMode);
  221.  
  222.         switch (m_primitiveType) {
  223.         case PRIM_POINT:
  224.             //if (drawingKick) Prim_Point();
  225.             m_vtxCount = 1;
  226.             break;
  227.         case PRIM_LINE:
  228.             //if (drawingKick) Prim_Line();
  229.             m_vtxCount = 2;
  230.             break;
  231.         case PRIM_LINESTRIP:
  232.             //if (drawingKick) Prim_Line();
  233.             memcpy(&m_vtxBuffer[1], &m_vtxBuffer[0], sizeof(VERTEX));
  234.             m_vtxCount = 1;
  235.             break;
  236.         case PRIM_TRIANGLE:
  237.             if (drawingKick) Prim_Triangle();
  238.             m_vtxCount = 3;
  239.             break;
  240.         case PRIM_TRIANGLESTRIP:
  241.             if (drawingKick) Prim_Triangle();
  242.             memcpy(&m_vtxBuffer[2], &m_vtxBuffer[1], sizeof(VERTEX));
  243.             memcpy(&m_vtxBuffer[1], &m_vtxBuffer[0], sizeof(VERTEX));
  244.             m_vtxCount = 1;
  245.             break;
  246.         case PRIM_TRIANGLEFAN:
  247.             if (drawingKick) Prim_Triangle();
  248.             memcpy(&m_vtxBuffer[1], &m_vtxBuffer[0], sizeof(VERTEX));
  249.             m_vtxCount = 1;
  250.             break;
  251.         case PRIM_SPRITE:
  252.             //if (drawingKick) Prim_Sprite();
  253.             m_vtxCount = 2;
  254.             break;
  255.         }
  256.     }
  257. #if 0
  258.     //LOG("Vertex kick\n");
  259.  
  260.     bool draw = (registerId == GS_REG_XYZ2) || (registerId == GS_REG_XYZF2);
  261.     if (!draw)
  262.         return;
  263.  
  264.     if (m_primitiveType != PRIM_TRIANGLE)
  265.         return;
  266.  
  267.     if ((m_nReg[GS_REG_PRMODECONT] & 1) != 0)
  268.         m_primitiveMode <<= m_nReg[GS_REG_PRIM];
  269.     else
  270.         m_primitiveMode <<= m_nReg[GS_REG_PRMODE];
  271.  
  272.     auto prim = make_convertible<PRMODE>(m_primitiveMode);
  273.     unsigned int context = m_primitiveMode.nContext;
  274.     auto offset = make_convertible<XYOFFSET>(m_nReg[GS_REG_XYOFFSET_1 + context]);
  275.     auto scissor = make_convertible<SCISSOR>(m_nReg[GS_REG_SCISSOR_1 + context]);
  276.     uint64 pos = data & 0x00FFFFFFFFFFFFFFULL;
  277.     uint64 nRGBAQ = m_nReg[GS_REG_RGBAQ];
  278.     auto xyz = make_convertible<XYZ>(pos);
  279.     auto rgbaq = make_convertible<RGBAQ>(nRGBAQ);
  280.  
  281.     /*if (GetCrtIsInterlaced() && GetCrtIsFrameMode()) {
  282.         if (m_nCSR & CSR_FIELD)
  283.             m_nPrimOfsY += 0.5;
  284.     }*/
  285.  
  286.     // V         {1728.000000, 1792.000000, 0.000000}
  287.     // XYOFFSET: {1728.000000, 1792.000000}
  288.     // SCISSOR   {0, 639, 0, 511}
  289.     // PRESVP:   { x: 160, y: 48, w: 640, h: 448}
  290.     // CRT:      {640,         448}
  291.     // TFM       { 160.000000,   48.000000, 1.000000}, {0.501961, 0.000000, 0.000000}
  292.  
  293.     /*LOG("V {%f, %f, %f}\n", xyz.GetX(), xyz.GetY(), xyz.GetZ());
  294.     LOG("XYOFFSET: {%f, %f}\n", offset.GetX(), offset.GetY());
  295.     LOG("SCISSOR: {%d, %d, %d, %d}\n", scissor.scax0, scissor.scax1, scissor.scay0, scissor.scay1);
  296.     LOG("CRT:    {%d, %d}\n", GetCrtWidth(), GetCrtHeight());
  297.     LOG("PRESVP: {x: %d, y: %d, w: %d, h: %d}\n", GetPresentationViewport().offsetX,
  298.         GetPresentationViewport().offsetY, GetPresentationViewport().width,
  299.         GetPresentationViewport().height);*/
  300.  
  301.     float x = xyz.GetX() - offset.GetX();
  302.     float y = xyz.GetY() - offset.GetY();
  303.     float z = CalcZ(xyz.GetZ());
  304.  
  305.     /* Hacky presentation viewport */
  306.     CGSHandler::PRESENTATION_VIEWPORT presentation_vp = GetPresentationViewport();
  307.     x += presentation_vp.offsetX;
  308.     y += presentation_vp.offsetY;
  309.  
  310.     struct color_vertex vertex;
  311.     vertex.position.x = x;
  312.     vertex.position.y = y;
  313.     vertex.position.z = 1.0f - z;
  314.     vertex.color.r = rgbaq.nR;
  315.     vertex.color.g = rgbaq.nG;
  316.     vertex.color.b = rgbaq.nB;
  317.     vertex.color.a = rgbaq.nA;
  318.  
  319.     /*LOG("TFM {%f, %f, %f}, {%f, %f, %f}\n",
  320.         vertex.position.x, vertex.position.y, vertex.position.z,
  321.         vertex.color.r, vertex.color.g, vertex.color.b);*/
  322.  
  323.     gxm_vertex_buffer_push(&vertex);
  324. #endif
  325. }
  326.  
  327. void CGSH_GXM::WriteRegisterImpl(uint8 registerId, uint64 data)
  328. {
  329.     CGSHandler::WriteRegisterImpl(registerId, data);
  330.  
  331.     //LOG("WriteRegisterImpl\n");
  332.  
  333.     switch (registerId) {
  334.     case GS_REG_PRIM:
  335.         m_primitiveType = static_cast<unsigned int>(data & 0x07);
  336.         switch (m_primitiveType) {
  337.         case PRIM_POINT:
  338.             m_vtxCount = 1;
  339.             break;
  340.         case PRIM_LINE:
  341.         case PRIM_LINESTRIP:
  342.             m_vtxCount = 2;
  343.             break;
  344.         case PRIM_TRIANGLE:
  345.         case PRIM_TRIANGLESTRIP:
  346.         case PRIM_TRIANGLEFAN:
  347.             m_vtxCount = 3;
  348.             break;
  349.         case PRIM_SPRITE:
  350.             m_vtxCount = 2;
  351.             break;
  352.         }
  353.         break;
  354.     case GS_REG_XYZ2:
  355.     case GS_REG_XYZ3:
  356.     case GS_REG_XYZF2:
  357.     case GS_REG_XYZF3:
  358.         VertexKick(registerId, data);
  359.         break;
  360.     }
  361. }
  362.  
  363.  
  364. void CGSH_GXM::FlipImpl()
  365. {
  366.     LOG("CGSH_GXM::FlipImpl\n");
  367.  
  368.     auto dispInfo = GetCurrentDisplayInfo();
  369.     auto fb = make_convertible<DISPFB>(dispInfo.first);
  370.     auto display = make_convertible<DISPLAY>(dispInfo.second);
  371.  
  372.     unsigned int dispWidth = (display.nW + 1) / (display.nMagX + 1);
  373.     unsigned int dispHeight = (display.nH + 1);
  374.     if (GetCrtIsInterlaced() && GetCrtIsFrameMode())
  375.         dispHeight /= 2;
  376.  
  377.     //CGSHandler::PRESENTATION_VIEWPORT vp = GetPresentationViewport();
  378.     //gxm_set_viewport(vp.offsetX, vp.offsetY, vp.width, vp.height, 0.5f, 0.5f);
  379.     //LOG("VP: {%d, %d, %d, %d}\n", vp.offsetX, vp.width, vp.offsetY, vp.height);
  380.  
  381.     //m_present->SetPresentationViewport(GetPresentationViewport());
  382.     //m_present->DoPresent(fb.nPSM, fb.GetBufPtr(), fb.GetBufWidth(), dispWidth, dispHeight);
  383.  
  384.     gxm_flip();
  385.  
  386.     CGSHandler::FlipImpl();
  387.  
  388.     LOG("Flip done\n");
  389.  
  390.     gxm_vertex_buffer_reset();
  391. }
  392.  
  393. CGSHandler::FactoryFunction CGSH_GXM::GetFactoryFunction()
  394. {
  395.     return [=]() { return new CGSH_GXM(); };
  396. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement