decoders.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "common/assert.h"
  6. #include "video_core/textures/decoders.h"
  7. #include "video_core/textures/texture.h"
  8. namespace Tegra {
  9. namespace Texture {
  10. /**
  11. * Calculates the offset of an (x, y) position within a swizzled texture.
  12. * Taken from the Tegra X1 TRM.
  13. */
  14. static u32 GetSwizzleOffset(u32 x, u32 y, u32 image_width, u32 bytes_per_pixel, u32 block_height) {
  15. u32 image_width_in_gobs = image_width * bytes_per_pixel / 64;
  16. u32 GOB_address = 0 + (y / (8 * block_height)) * 512 * block_height * image_width_in_gobs +
  17. (x * bytes_per_pixel / 64) * 512 * block_height +
  18. (y % (8 * block_height) / 8) * 512;
  19. x *= bytes_per_pixel;
  20. u32 address = GOB_address + ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 +
  21. (y % 2) * 16 + (x % 16);
  22. return address;
  23. }
  24. static void CopySwizzledData(u32 width, u32 height, u32 bytes_per_pixel, u32 out_bytes_per_pixel,
  25. u8* swizzled_data, u8* unswizzled_data, bool unswizzle,
  26. u32 block_height) {
  27. u8* data_ptrs[2];
  28. for (unsigned y = 0; y < height; ++y) {
  29. for (unsigned x = 0; x < width; ++x) {
  30. u32 swizzle_offset = GetSwizzleOffset(x, y, width, bytes_per_pixel, block_height);
  31. u32 pixel_index = (x + y * width) * out_bytes_per_pixel;
  32. data_ptrs[unswizzle] = swizzled_data + swizzle_offset;
  33. data_ptrs[!unswizzle] = &unswizzled_data[pixel_index];
  34. std::memcpy(data_ptrs[0], data_ptrs[1], bytes_per_pixel);
  35. }
  36. }
  37. }
  38. u32 BytesPerPixel(TextureFormat format) {
  39. switch (format) {
  40. case TextureFormat::DXT1:
  41. // In this case a 'pixel' actually refers to a 4x4 tile.
  42. return 8;
  43. default:
  44. UNIMPLEMENTED_MSG("Format not implemented");
  45. break;
  46. }
  47. }
  48. std::vector<u8> UnswizzleTexture(VAddr address, TextureFormat format, u32 width, u32 height) {
  49. u8* data = Memory::GetPointer(address);
  50. u32 bytes_per_pixel = BytesPerPixel(format);
  51. static constexpr u32 DefaultBlockHeight = 16;
  52. std::vector<u8> unswizzled_data(width * height * bytes_per_pixel);
  53. switch (format) {
  54. case TextureFormat::DXT1:
  55. // In the DXT1 format, each 4x4 tile is swizzled instead of just individual pixel values.
  56. CopySwizzledData(width / 4, height / 4, bytes_per_pixel, bytes_per_pixel, data,
  57. unswizzled_data.data(), true, DefaultBlockHeight);
  58. break;
  59. default:
  60. UNIMPLEMENTED_MSG("Format not implemented");
  61. break;
  62. }
  63. return unswizzled_data;
  64. }
  65. std::vector<u8> DecodeTexture(const std::vector<u8>& texture_data, TextureFormat format, u32 width,
  66. u32 height) {
  67. std::vector<u8> rgba_data;
  68. // TODO(Subv): Implement.
  69. switch (format) {
  70. default:
  71. UNIMPLEMENTED_MSG("Format not implemented");
  72. break;
  73. }
  74. return rgba_data;
  75. }
  76. } // namespace Texture
  77. } // namespace Tegra