Advertisement
Guest User

Wii U swizzling code

a guest
Oct 5th, 2016
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.90 KB | None | 0 0
  1. swizzle=False
  2.  
  3. m_banks = 4
  4. m_banksBitcount = 2
  5. m_pipes = 2
  6. m_pipesBitcount = 1
  7. m_pipeInterleaveBytes = 256
  8. m_pipeInterleaveBytesBitcount = 8
  9. m_rowSize = 2048
  10. m_swapSize = 256
  11. m_splitSize = 2048
  12.  
  13. m_chipFamily = 2
  14.  
  15. def swizzle(width, height, depth, format_, tileMode, swizzle, pitch, data):
  16.     result = bytearray(data)
  17.  
  18.     for y in range(height):
  19.         for x in range(width):
  20.             bitPos = 0
  21.             bpp = surfaceGetBitsPerPixel(format_)
  22.             pipeSwizzle = (swizzle >> 8) & 1
  23.             bankSwizzle = (swizzle >> 9) & 3
  24.  
  25.             if (tileMode == 0 or tileMode == 1):
  26.                 pos = AddrLib_computeSurfaceAddrFromCoordLinear(x, y, 0, 0, bpp, pitch, height, depth, bitPos)
  27.             elif (tileMode == 2 or tileMode == 3):
  28.                 pos = AddrLib_computeSurfaceAddrFromCoordMicroTiled(x, y, 0, bpp, pitch, height, tileMode, 0, 0, 0, bitPos)
  29.             else:
  30.                 pos = AddrLib_computeSurfaceAddrFromCoordMacroTiled(x, y, 0, 0, bpp, pitch, height, 1*1, tileMode, 0, 0, 0, pipeSwizzle, bankSwizzle, bitPos)
  31.  
  32.             bpp //= 8
  33.  
  34.             pos_ = (y * width + x) * bpp
  35.  
  36.             if swizzle:
  37.                 result[pos:pos + bpp] = data[pos_:pos_ + bpp]
  38.             else:
  39.                 result[pos_:pos_ + bpp] = data[pos:pos + bpp]
  40.  
  41.     return result
  42.  
  43. def swizzle_BC(width, height, depth, format_, tileMode, swizzle, pitch, data):
  44.     result = bytearray(data)
  45.  
  46.     width = width // 4
  47.     height = height // 4
  48.  
  49.     for y in range(height):
  50.         for x in range(width):
  51.             bitPos = 0
  52.             bpp = surfaceGetBitsPerPixel(format_)
  53.             pipeSwizzle = (swizzle >> 8) & 1
  54.             bankSwizzle = (swizzle >> 9) & 3
  55.  
  56.             if (tileMode == 0 or tileMode == 1):
  57.                 pos = AddrLib_computeSurfaceAddrFromCoordLinear(x, y, 0, 0, bpp, pitch, height, depth, bitPos)
  58.             elif (tileMode == 2 or tileMode == 3):
  59.                 pos = AddrLib_computeSurfaceAddrFromCoordMicroTiled(x, y, 0, bpp, pitch, height, tileMode, 0, 0, 0, bitPos)
  60.             else:
  61.                 pos = AddrLib_computeSurfaceAddrFromCoordMacroTiled(x, y, 0, 0, bpp, pitch, height, 1, tileMode, 0, 0, 0, pipeSwizzle, bankSwizzle, bitPos)
  62.  
  63.             if (format_ == 0x31 or format_ == 0x431 or format_ == 0x34 or format_ == 0x234):
  64.                 pos_ = (y * width + x) * 8
  65.  
  66.                 if swizzle:
  67.                     result[pos:pos + 8] = data[pos_:pos_ + 8]
  68.                 else:
  69.                     result[pos_:pos_ + 8] = data[pos:pos + 8]
  70.             else:
  71.                 pos_ = (y * width + x) * 16
  72.  
  73.                 if swizzle:
  74.                     result[pos:pos + 16] = data[pos_:pos_ + 16]
  75.                 else:
  76.                     result[pos_:pos_ + 16] = data[pos:pos + 16]
  77.  
  78.     return result
  79.  
  80. formatHwInfo = b"\x00\x00\x00\x01\x08\x03\x00\x01\x08\x01\x00\x01\x00\x00\x00\x01" \
  81.     b"\x00\x00\x00\x01\x10\x07\x00\x00\x10\x03\x00\x01\x10\x03\x00\x01" \
  82.     b"\x10\x0B\x00\x01\x10\x01\x00\x01\x10\x03\x00\x01\x10\x03\x00\x01" \
  83.     b"\x10\x03\x00\x01\x20\x03\x00\x00\x20\x07\x00\x00\x20\x03\x00\x00" \
  84.     b"\x20\x03\x00\x01\x20\x05\x00\x00\x00\x00\x00\x00\x20\x03\x00\x00" \
  85.     b"\x00\x00\x00\x00\x00\x00\x00\x01\x20\x03\x00\x01\x00\x00\x00\x01" \
  86.     b"\x00\x00\x00\x01\x20\x0B\x00\x01\x20\x0B\x00\x01\x20\x0B\x00\x01" \
  87.     b"\x40\x05\x00\x00\x40\x03\x00\x00\x40\x03\x00\x00\x40\x03\x00\x00" \
  88.     b"\x40\x03\x00\x01\x00\x00\x00\x00\x80\x03\x00\x00\x80\x03\x00\x00" \
  89.     b"\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x10\x01\x00\x00" \
  90.     b"\x10\x01\x00\x00\x20\x01\x00\x00\x20\x01\x00\x00\x20\x01\x00\x00" \
  91.     b"\x00\x01\x00\x01\x00\x01\x00\x00\x00\x01\x00\x00\x60\x01\x00\x00" \
  92.     b"\x60\x01\x00\x00\x40\x01\x00\x01\x80\x01\x00\x01\x80\x01\x00\x01" \
  93.     b"\x40\x01\x00\x01\x80\x01\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" \
  94.     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
  95.     b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  96.  
  97. def surfaceGetBitsPerPixel(surfaceFormat):
  98.     hwFormat = surfaceFormat & 0x3F
  99.     bpp = formatHwInfo[hwFormat * 4 + 0]
  100.     return bpp
  101.  
  102. def computeSurfaceThickness(tileMode):
  103.     if (tileMode == 3 or tileMode == 7 or tileMode == 11 or tileMode == 13 or tileMode == 15):
  104.         thickness = 4
  105.     elif (tileMode == 16 or tileMode == 17):
  106.         thickness = 8
  107.     else:
  108.         thickness = 1
  109.     return thickness
  110.  
  111. def computePixelIndexWithinMicroTile(x, y, z, bpp, tileMode, microTileType):
  112.     pixelBit6 = 0
  113.     pixelBit7 = 0
  114.     pixelBit8 = 0
  115.     thickness = computeSurfaceThickness(tileMode)
  116.  
  117.     if microTileType == 3:
  118.         pixelBit0 = x & 1
  119.         pixelBit1 = y & 1
  120.         pixelBit2 = z & 1
  121.         pixelBit3 = (x & 2) >> 1
  122.         pixelBit4 = (y & 2) >> 1
  123.         pixelBit5 = (z & 2) >> 1
  124.         pixelBit6 = (x & 4) >> 2
  125.         pixelBit7 = (y & 4) >> 2
  126.     else:
  127.         if microTileType != 0:
  128.             pixelBit0 = x & 1
  129.             pixelBit1 = y & 1
  130.             pixelBit2 = (x & 2) >> 1
  131.             pixelBit3 = (y & 2) >> 1
  132.             pixelBit4 = (x & 4) >> 2
  133.             pixelBit5 = (y & 4) >> 2
  134.         else:
  135.             if bpp == 0x08:
  136.                 pixelBit0 = x & 1
  137.                 pixelBit1 = (x & 2) >> 1
  138.                 pixelBit2 = (x & 4) >> 2
  139.                 pixelBit3 = (y & 2) >> 1
  140.                 pixelBit4 = y & 1
  141.                 pixelBit5 = (y & 4) >> 2
  142.             elif bpp == 0x10:
  143.                 pixelBit0 = x & 1
  144.                 pixelBit1 = (x & 2) >> 1
  145.                 pixelBit2 = (x & 4) >> 2
  146.                 pixelBit3 = y & 1
  147.                 pixelBit4 = (y & 2) >> 1
  148.                 pixelBit5 = (y & 4) >> 2
  149.             elif (bpp == 0x20 or bpp == 0x60):
  150.                 pixelBit0 = x & 1
  151.                 pixelBit1 = (x & 2) >> 1
  152.                 pixelBit2 = y & 1
  153.                 pixelBit3 = (x & 4) >> 2
  154.                 pixelBit4 = (y & 2) >> 1
  155.                 pixelBit5 = (y & 4) >> 2
  156.             elif bpp == 0x40:
  157.                 pixelBit0 = x & 1
  158.                 pixelBit1 = y & 1
  159.                 pixelBit2 = (x & 2) >> 1
  160.                 pixelBit3 = (x & 4) >> 2
  161.                 pixelBit4 = (y & 2) >> 1
  162.                 pixelBit5 = (y & 4) >> 2
  163.             elif bpp == 0x80:
  164.                 pixelBit0 = y & 1
  165.                 pixelBit1 = x & 1
  166.                 pixelBit2 = (x & 2) >> 1
  167.                 pixelBit3 = (x & 4) >> 2
  168.                 pixelBit4 = (y & 2) >> 1
  169.                 pixelBit5 = (y & 4) >> 2
  170.             else:
  171.                 pixelBit0 = x & 1
  172.                 pixelBit1 = (x & 2) >> 1
  173.                 pixelBit2 = y & 1
  174.                 pixelBit3 = (x & 4) >> 2
  175.                 pixelBit4 = (y & 2) >> 1
  176.                 pixelBit5 = (y & 4) >> 2
  177.         if thickness > 1:
  178.             pixelBit6 = z & 1
  179.             pixelBit7 = (z & 2) >> 1
  180.     if thickness == 8:
  181.         pixelBit8 = (z & 4) >> 2
  182.     return (pixelBit8 << 8) | (pixelBit7 << 7) | (pixelBit6 << 6) | 32 * pixelBit5 | 16 * pixelBit4 | 8 * pixelBit3 | 4 * pixelBit2 | pixelBit0 | 2 * pixelBit1
  183.  
  184. def computePipeFromCoordWoRotation(x, y):
  185.     # hardcoded to assume 2 pipes
  186.     pipe = ((y >> 3) ^ (x >> 3)) & 1
  187.     return pipe
  188.  
  189. def computeBankFromCoordWoRotation(x, y):
  190.     numPipes = m_pipes
  191.     numBanks = m_banks
  192.     bankOpt = 0
  193.     if numBanks == 4:
  194.         bankBit0 = ((y // (16 * numPipes)) ^ (x >> 3)) & 1
  195.         if (bankOpt == 1 and numPipes == 8):
  196.             bankBit0 ^= x // 0x20 & 1
  197.         bank = bankBit0 | 2 * (((y // (8 * numPipes)) ^ (x >> 4)) & 1)
  198.     elif numBanks == 8:
  199.         bankBit0a = ((y // (32 * numPipes)) ^ (x >> 3)) & 1
  200.         if (bankOpt == 1 and numPipes == 8):
  201.             bankBit0a ^= x // (8 * numBanks) & 1
  202.         bank = bankBit0a | 2 * (((y // (32 * numPipes)) ^ (y // (16 * numPipes) ^ (x >> 4))) & 1) | 4 * (((y // (8 * numPipes)) ^ (x >> 5)) & 1)
  203.     else:
  204.         bank = 0
  205.  
  206.     return bank
  207.  
  208. def computeSurfaceRotationFromTileMode(tileMode):
  209.     pipes = m_pipes
  210.     if (tileMode == 4 or tileMode == 5 or tileMode == 6 or tileMode == 7 or tileMode == 8 or tileMode == 9 or tileMode == 10 or tileMode == 11):
  211.         result = pipes * ((m_banks >> 1) - 1)
  212.     elif (tileMode == 12 or tileMode == 13 or tileMode == 14 or tileMode == 15):
  213.         if (pipes > 4 or pipes == 4):
  214.             result = (pipes >> 1) - 1
  215.         else:
  216.             result = 1
  217.     else:
  218.         result = 0
  219.     return result
  220.  
  221. def isThickMacroTiled(tileMode):
  222.     thickMacroTiled = 0
  223.     if (tileMode == 7 or tileMode == 11 or tileMode == 13 or tileMode == 15):
  224.         thickMacroTiled = 1
  225.     else:
  226.         thickMacroTiled = thickMacroTiled
  227.     return thickMacroTiled
  228.  
  229. def isBankSwappedTileMode(tileMode):
  230.     bankSwapped = 0
  231.     if (tileMode == 8 or tileMode == 9 or tileMode == 10 or tileMode == 11 or tileMode == 14 or tileMode == 15):
  232.         bankSwapped = 1
  233.     else:
  234.         bankSwapped = bankSwapped
  235.     return bankSwapped
  236.  
  237. def computeMacroTileAspectRatio(tileMode):
  238.     ratio = 1
  239.     if (tileMode == 8 or tileMode == 12 or tileMode == 14):
  240.         ratio = 1
  241.     elif (tileMode == 5 or tileMode == 9):
  242.         ratio = 2
  243.     elif (tileMode == 6 or tileMode == 10):
  244.         ratio = 4
  245.     else:
  246.         ratio = ratio
  247.     return ratio
  248.  
  249. def computeSurfaceBankSwappedWidth(tileMode, bpp, numSamples, pitch, pSlicesPerTile):
  250.     bankSwapWidth = 0
  251.     numBanks = m_banks
  252.     numPipes = m_pipes
  253.     swapSize = m_swapSize
  254.     rowSize = m_rowSize
  255.     splitSize = m_splitSize
  256.     groupSize = m_pipeInterleaveBytes
  257.     slicesPerTile = 1
  258.     bytesPerSample = 8 * bpp & 0x1FFFFFFF
  259.     samplesPerTile = splitSize // bytesPerSample
  260.     if (splitSize // bytesPerSample) != 0:
  261.         slicesPerTile = numSamples // samplesPerTile
  262.         if not ((numSamples // samplesPerTile) != 0):
  263.             slicesPerTile = 1
  264.     if pSlicesPerTile != 0:
  265.         pSlicesPerTile = slicesPerTile
  266.     if isThickMacroTiled(tileMode) == 1:
  267.         numSamples = 4
  268.     bytesPerTileSlice = numSamples * bytesPerSample // slicesPerTile
  269.     if isBankSwappedTileMode(tileMode) != 0:
  270.         factor = computeMacroTileAspectRatio(tileMode)
  271.         swapTiles = (swapSize >> 1) // bpp
  272.         if swapTiles != 0:
  273.             v9 = swapTiles
  274.         else:
  275.             v9 = 1
  276.         swapWidth = v9 * 8 * numBanks
  277.         heightBytes = numSamples * factor * numPipes * bpp // slicesPerTile
  278.         swapMax = numPipes * numBanks * rowSize // heightBytes
  279.         swapMin = groupSize * 8 * numBanks // bytesPerTileSlice
  280.         if (swapMax > swapWidth or swapMax == swapWidth):
  281.             if (swapMin < swapWidth or swapMin == swapWidth):
  282.                 v7 = swapWidth
  283.             else:
  284.                 v7 = swapMin
  285.             v8 = v7
  286.         else:
  287.             v8 = swapMax
  288.         bankSwapWidth = v8
  289.         while (bankSwapWidth > (2 * pitch) or bankSwapWidth == (2 * pitch)): # Let's wish this works :P
  290.             bankSwapWidth >>= 1
  291.     return bankSwapWidth
  292.  
  293. bankSwapOrder = bytes([0, 1, 3, 2])
  294.  
  295. def AddrLib_getTileType(isDepth):
  296.     return (1 if isDepth != 0 else 0)
  297.  
  298. def AddrLib_computePixelIndexWithinMicroTile(x, y, z, bpp, tileMode, microTileType):
  299.     pixelBit6 = 0
  300.     pixelBit7 = 0
  301.     pixelBit8 = 0
  302.     thickness = computeSurfaceThickness(tileMode)
  303.     if microTileType == 3:
  304.         pixelBit0 = x & 1
  305.         pixelBit1 = y & 1
  306.         pixelBit2 = z & 1
  307.         pixelBit3 = (x & 2) >> 1
  308.         pixelBit4 = (y & 2) >> 1
  309.         pixelBit5 = (z & 2) >> 1
  310.         pixelBit6 = (x & 4) >> 2
  311.         pixelBit7 = (y & 4) >> 2
  312.     else:
  313.         if microTileType != 0:
  314.             pixelBit0 = x & 1
  315.             pixelBit1 = y & 1
  316.             pixelBit2 = (x & 2) >> 1
  317.             pixelBit3 = (y & 2) >> 1
  318.             pixelBit4 = (x & 4) >> 2
  319.             pixelBit5 = (y & 4) >> 2
  320.         else:
  321.             v8 = bpp - 8
  322.             if bpp == 0x08:
  323.                 pixelBit0 = x & 1
  324.                 pixelBit1 = (x & 2) >> 1
  325.                 pixelBit2 = (x & 4) >> 2
  326.                 pixelBit3 = (y & 2) >> 1
  327.                 pixelBit4 = y & 1
  328.                 pixelBit5 = (y & 4) >> 2
  329.             elif bpp == 0x10:
  330.                 pixelBit0 = x & 1
  331.                 pixelBit1 = (x & 2) >> 1
  332.                 pixelBit2 = (x & 4) >> 2
  333.                 pixelBit3 = y & 1
  334.                 pixelBit4 = (y & 2) >> 1
  335.                 pixelBit5 = (y & 4) >> 2
  336.             elif (bpp == 0x20 or bpp == 0x60):
  337.                 pixelBit0 = x & 1
  338.                 pixelBit1 = (x & 2) >> 1
  339.                 pixelBit2 = y & 1
  340.                 pixelBit3 = (x & 4) >> 2
  341.                 pixelBit4 = (y & 2) >> 1
  342.                 pixelBit5 = (y & 4) >> 2
  343.             elif bpp == 0x40:
  344.                 pixelBit0 = x & 1
  345.                 pixelBit1 = y & 1
  346.                 pixelBit2 = (x & 2) >> 1
  347.                 pixelBit3 = (x & 4) >> 2
  348.                 pixelBit4 = (y & 2) >> 1
  349.                 pixelBit5 = (y & 4) >> 2
  350.             elif bpp == 0x80:
  351.                 pixelBit0 = y & 1
  352.                 pixelBit1 = x & 1
  353.                 pixelBit2 = (x & 2) >> 1
  354.                 pixelBit3 = (x & 4) >> 2
  355.                 pixelBit4 = (y & 2) >> 1
  356.                 pixelBit5 = (y & 4) >> 2
  357.             else:
  358.                 pixelBit0 = x & 1
  359.                 pixelBit1 = (x & 2) >> 1
  360.                 pixelBit2 = y & 1
  361.                 pixelBit3 = (x & 4) >> 2
  362.                 pixelBit4 = (y & 2) >> 1
  363.                 pixelBit5 = (y & 4) >> 2
  364.         if thickness > 1:
  365.             pixelBit6 = z & 1
  366.             pixelBit7 = (z & 2) >> 1
  367.     if thickness == 8:
  368.         pixelBit8 = (z & 4) >> 2
  369.     return (pixelBit8 << 8) | (pixelBit7 << 7) | (pixelBit6 << 6) | 32 * pixelBit5 | 16 * pixelBit4 | 8 * pixelBit3 | 4 * pixelBit2 | pixelBit0 | 2 * pixelBit1
  370.  
  371. def AddrLib_computeSurfaceAddrFromCoordLinear(x, y, slice, sample, bpp, pitch, height, numSlices, pBitPosition):
  372.     v9 = x + pitch * y + (slice + numSlices * sample) * height * pitch
  373.  
  374.     addr = v9 * bpp
  375.  
  376.     pBitPosition = v9 * bpp % 8
  377.     return addr // 8
  378.  
  379. def AddrLib_computeSurfaceAddrFromCoordMicroTiled(x, y, slice, bpp, pitch, height, tileMode, isDepth, tileBase, compBits, pBitPosition):
  380.     v14 = tileMode
  381.     if tileMode == 3:
  382.         microTileThickness = 4
  383.     else:
  384.         microTileThickness = 1
  385.     microTileBytes = microTileThickness * (((bpp << 6) + 7) >> 3)
  386.     microTilesPerRow = pitch >> 3
  387.     microTileIndexX = x >> 3
  388.     microTileIndexY = y >> 3
  389.     microTileOffset = microTileThickness * (((bpp << 6) + 7) >> 3) * (x >> 3 + (pitch >> 3) * (y >> 3))
  390.     sliceBytes = (height * pitch * microTileThickness * bpp + 7) // 8
  391.     sliceOffset = sliceBytes * (slice // microTileThickness)
  392.     v12 = AddrLib_getTileType(isDepth)
  393.     pixelIndex = AddrLib_computePixelIndexWithinMicroTile(x, y, slice, bpp, tileMode, v12)
  394.     if (compBits != 0 and compBits != bpp and isDepth!= 0):
  395.         pixelOffset = tileBase + compBits * pixelIndex
  396.     else:
  397.         pixelOffset = bpp * pixelIndex
  398.     pBitPosition = pixelOffset % 8
  399.     pixelOffset >>= 3
  400.     return pixelOffset + microTileOffset + sliceOffset
  401.  
  402. def AddrLib_computeSurfaceAddrFromCoordMacroTiled(x, y, slice, sample, bpp, pitch, height, numSamples, tileMode, isDepth, tileBase, compBits, pipeSwizzle, bankSwizzle, pBitPosition):
  403.     # numSamples is used for AA surfaces and can be set to 1 for all others
  404.     numPipes = m_pipes
  405.     numBanks = m_banks
  406.     numGroupBits = m_pipeInterleaveBytesBitcount
  407.     numPipeBits = m_pipesBitcount
  408.     numBankBits = m_banksBitcount
  409.     microTileThickness = computeSurfaceThickness(tileMode)
  410.     microTileBits = numSamples * bpp * (microTileThickness * (8*8))
  411.     microTileBytes = microTileBits >> 3
  412.     microTileType = (1 if isDepth != 0 else 0)
  413.     pixelIndex = computePixelIndexWithinMicroTile(x, y, slice, bpp, tileMode, microTileType)
  414.     if isDepth != 0:
  415.         if (compBits != 0 and compBits != bpp):
  416.             sampleOffset = tileBase + compBits * sample
  417.             pixelOffset = numSamples * compBits * pixelIndex
  418.         else:
  419.             sampleOffset = bpp * sample
  420.             pixelOffset = numSamples * bpp * pixelIndex
  421.     else:
  422.         sampleOffset = sample * (microTileBits // numSamples)
  423.         pixelOffset = bpp * pixelIndex
  424.     elemOffset = pixelOffset + sampleOffset
  425.     pBitPosition = (pixelOffset + sampleOffset) % 8
  426.     bytesPerSample = microTileBytes // numSamples
  427.     if (numSamples <= 1 or microTileBytes <= m_splitSize):
  428.         samplesPerSlice = numSamples
  429.         numSampleSplits = 1
  430.         sampleSlice = 0
  431.     else:
  432.         samplesPerSlice = m_splitSize // bytesPerSample
  433.         numSampleSplits = numSamples // samplesPerSlice
  434.         numSamples = samplesPerSlice
  435.         tileSliceBits = microTileBits // numSampleSplits
  436.         sampleSlice = elemOffset // (microTileBits // numSampleSplits)
  437.         elemOffset %= microTileBits // numSampleSplits
  438.     elemOffset >>= 3
  439.     pipe = computePipeFromCoordWoRotation(x, y)
  440.     bank = computeBankFromCoordWoRotation(x, y)
  441.     bankPipe = pipe + numPipes * bank
  442.     rotation = computeSurfaceRotationFromTileMode(tileMode)
  443.     swizzle = pipeSwizzle + numPipes * bankSwizzle
  444.     sliceIn = slice
  445.     if isThickMacroTiled(tileMode) != 0:
  446.         sliceIn >>= 2
  447.     bankPipe ^= numPipes * sampleSlice * ((numBanks >> 1) + 1) ^ (swizzle + sliceIn * rotation)
  448.     bankPipe %= numPipes * numBanks
  449.     pipe = bankPipe % numPipes
  450.     bank = bankPipe // numPipes
  451.     sliceBytes = (height * pitch * microTileThickness * bpp * numSamples + 7) // 8
  452.     sliceOffset = sliceBytes * ((sampleSlice + numSampleSplits * slice) // microTileThickness)
  453.     macroTilePitch = 8 * m_banks
  454.     macroTileHeight = 8 * m_pipes
  455.     v18 = tileMode - 5
  456.     if (tileMode == 5 or tileMode == 9): # GX2_TILE_MODE_2D_TILED_THIN4 and GX2_TILE_MODE_2B_TILED_THIN2
  457.         macroTilePitch >>= 1
  458.         macroTileHeight *= 2
  459.     elif (tileMode == 6 or tileMode == 10): # GX2_TILE_MODE_2D_TILED_THIN4 and GX2_TILE_MODE_2B_TILED_THIN4
  460.         macroTilePitch >>= 2
  461.         macroTileHeight *= 4
  462.     macroTilesPerRow = pitch // macroTilePitch
  463.     macroTileBytes = (numSamples * microTileThickness * bpp * macroTileHeight * macroTilePitch + 7) >> 3
  464.     macroTileIndexX = x // macroTilePitch
  465.     macroTileIndexY = y // macroTileHeight
  466.     macroTileOffset = (x // macroTilePitch + pitch // macroTilePitch * (y // macroTileHeight)) * macroTileBytes
  467.     if (tileMode == 8 or tileMode == 9 or tileMode == 10 or tileMode == 11 or tileMode == 14 or tileMode == 15):
  468.         bankSwapWidth = computeSurfaceBankSwappedWidth(tileMode, bpp, numSamples, pitch, 0)
  469.         swapIndex = macroTilePitch * macroTileIndexX // bankSwapWidth
  470.         if m_banks > 4:
  471.             raise ValueError("TODO.")
  472.         bankMask = m_banks-1
  473.         bank ^= bankSwapOrder[swapIndex & bankMask]
  474.     p4 = (pipe << numGroupBits)
  475.     p5 = (bank << (numPipeBits + numGroupBits))
  476.     numSwizzleBits = (numBankBits + numPipeBits)
  477.     ukn1 = ((macroTileOffset + sliceOffset) >> numSwizzleBits)
  478.     ukn2 = ~((1 << numGroupBits) - 1)
  479.     ukn3 = ((elemOffset + ukn1) & ukn2)
  480.     groupMask = ((1 << numGroupBits) - 1)
  481.     offset1 = (macroTileOffset + sliceOffset)
  482.     ukn4 = (elemOffset + (offset1 >> numSwizzleBits))
  483.  
  484.     subOffset1 = (ukn3 << numSwizzleBits)
  485.     subOffset2 = groupMask & ukn4
  486.  
  487.     return subOffset1 | subOffset2 | p4 | p5
  488.  
  489. def addrLib_computeTileDataWidthAndHeight(bpp, cacheBits, pTileInfo, pMacroWidth, pMacroHeight):
  490.     height = 1
  491.     width = cacheBits // bpp
  492.     pipes = m_pipes
  493.     while (width > (pipes * 2 * height) and (width & 1) == 0):
  494.         width >>= 1
  495.         height *= 2
  496.     pMacroWidth = 8 * width
  497.     pMacroHeight = pipes * 8 * height
  498.  
  499. def AddrLib_computeCmaskBytes(pitch, height, numSlices):
  500.     return (4 * height * pitch * numSlices + 7) // 8 // 64
  501.  
  502. def AddrLib__ComputeCmaskBaseAlign(pTileInfo):
  503.     print("AddrLib__ComputeCmaskBaseAlign(): Uknown")
  504.     v2 = 1 # uknown
  505.     return m_pipeInterleaveBytes * v2
  506.  
  507. def AddrLib_computeCmaskInfo(pitchIn, heightIn, numSlices, isLinear, pTileInfo, pPitchOut, pHeightOut, pCmaskBytes, pMacroWidth, pMacroHeight, pBaseAlign, pBlockMax):
  508.     bpp = 4
  509.     cacheBits = 1024
  510.     returnCode = 0
  511.     if isLinear != 0:
  512.         raise ValueError("Invalid isLinear value!")
  513.     else:
  514.         addrLib_computeTileDataWidthAndHeight(bpp, cacheBits, pTileInfo, macroWidth, macroHeight)
  515.     pPitchOut = ~(macroWidth - 1) & (pitchIn + macroWidth - 1)
  516.     pHeightOut = ~(macroHeight - 1) & (heightIn + macroHeight - 1)
  517.     sliceBytes = AddrLib_computeCmaskBytes(pPitchOut, pHeightOut, 1)
  518.     baseAlign = AddrLib__ComputeCmaskBaseAlign(pTileInfo)
  519.     while 1:
  520.         v14 = sliceBytes % baseAlign
  521.         if not (sliceBytes % baseAlign != 0):
  522.             break
  523.         pHeightOut += macroHeight
  524.         sliceBytes = AddrLib_computeCmaskBytes(pPitchOut, pHeightOut, 1)
  525.     surfBytes = sliceBytes * numSlices
  526.     pCmaskBytes = surfBytes
  527.     pMacroWidth = macroWidth
  528.     pMacroHeight = macroHeight
  529.     pBaseAlign = baseAlign
  530.     slice = pHeightOut * pPitchOut
  531.     blockMax = (slice >> 14) - 1
  532.     # uknown part possibly missing here
  533.     pBlockMax = blockMax
  534.     return returnCode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement