image_base.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <optional>
  6. #include <vector>
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "video_core/texture_cache/image_info.h"
  10. #include "video_core/texture_cache/image_view_info.h"
  11. #include "video_core/texture_cache/types.h"
  12. namespace VideoCommon {
  13. enum class ImageFlagBits : u32 {
  14. AcceleratedUpload = 1 << 0, ///< Upload can be accelerated in the GPU
  15. Converted = 1 << 1, ///< Guest format is not supported natively and it has to be converted
  16. CpuModified = 1 << 2, ///< Contents have been modified from the CPU
  17. GpuModified = 1 << 3, ///< Contents have been modified from the GPU
  18. Tracked = 1 << 4, ///< Writes and reads are being hooked from the CPU JIT
  19. Strong = 1 << 5, ///< Exists in the image table, the dimensions are can be trusted
  20. Registered = 1 << 6, ///< True when the image is registered
  21. Picked = 1 << 7, ///< Temporary flag to mark the image as picked
  22. Remapped = 1 << 8, ///< Image has been remapped.
  23. Sparse = 1 << 9, ///< Image has non continous submemory.
  24. // Garbage Collection Flags
  25. BadOverlap = 1 << 10, ///< This image overlaps other but doesn't fit, has higher
  26. ///< garbage collection priority
  27. Alias = 1 << 11, ///< This image has aliases and has priority on garbage
  28. ///< collection
  29. CostlyLoad = 1 << 12, ///< Protected from low-tier GC as it is costly to load back.
  30. // Rescaler
  31. Rescaled = 1 << 13,
  32. CheckingRescalable = 1 << 14,
  33. IsRescalable = 1 << 15,
  34. };
  35. DECLARE_ENUM_FLAG_OPERATORS(ImageFlagBits)
  36. struct ImageViewInfo;
  37. struct AliasedImage {
  38. std::vector<ImageCopy> copies;
  39. ImageId id;
  40. };
  41. struct NullImageParams {};
  42. struct ImageBase {
  43. explicit ImageBase(const ImageInfo& info, GPUVAddr gpu_addr, VAddr cpu_addr);
  44. explicit ImageBase(const NullImageParams&);
  45. [[nodiscard]] std::optional<SubresourceBase> TryFindBase(GPUVAddr other_addr) const noexcept;
  46. [[nodiscard]] ImageViewId FindView(const ImageViewInfo& view_info) const noexcept;
  47. void InsertView(const ImageViewInfo& view_info, ImageViewId image_view_id);
  48. [[nodiscard]] bool IsSafeDownload() const noexcept;
  49. [[nodiscard]] bool Overlaps(VAddr overlap_cpu_addr, size_t overlap_size) const noexcept {
  50. const VAddr overlap_end = overlap_cpu_addr + overlap_size;
  51. return cpu_addr < overlap_end && overlap_cpu_addr < cpu_addr_end;
  52. }
  53. [[nodiscard]] bool OverlapsGPU(GPUVAddr overlap_gpu_addr, size_t overlap_size) const noexcept {
  54. const VAddr overlap_end = overlap_gpu_addr + overlap_size;
  55. const GPUVAddr gpu_addr_end = gpu_addr + guest_size_bytes;
  56. return gpu_addr < overlap_end && overlap_gpu_addr < gpu_addr_end;
  57. }
  58. void CheckBadOverlapState();
  59. void CheckAliasState();
  60. bool HasScaled() const {
  61. return has_scaled;
  62. }
  63. ImageInfo info;
  64. u32 guest_size_bytes = 0;
  65. u32 unswizzled_size_bytes = 0;
  66. u32 converted_size_bytes = 0;
  67. u32 scale_rating = 0;
  68. u64 scale_tick = 0;
  69. bool has_scaled = false;
  70. ImageFlagBits flags = ImageFlagBits::CpuModified;
  71. GPUVAddr gpu_addr = 0;
  72. VAddr cpu_addr = 0;
  73. VAddr cpu_addr_end = 0;
  74. u64 modification_tick = 0;
  75. size_t lru_index = SIZE_MAX;
  76. std::array<u32, MAX_MIP_LEVELS> mip_level_offsets{};
  77. std::vector<ImageViewInfo> image_view_infos;
  78. std::vector<ImageViewId> image_view_ids;
  79. std::vector<u32> slice_offsets;
  80. std::vector<SubresourceBase> slice_subresources;
  81. std::vector<AliasedImage> aliased_images;
  82. std::vector<ImageId> overlapping_images;
  83. ImageMapId map_view_id{};
  84. };
  85. struct ImageMapView {
  86. explicit ImageMapView(GPUVAddr gpu_addr, VAddr cpu_addr, size_t size, ImageId image_id);
  87. [[nodiscard]] bool Overlaps(VAddr overlap_cpu_addr, size_t overlap_size) const noexcept {
  88. const VAddr overlap_end = overlap_cpu_addr + overlap_size;
  89. const VAddr cpu_addr_end = cpu_addr + size;
  90. return cpu_addr < overlap_end && overlap_cpu_addr < cpu_addr_end;
  91. }
  92. [[nodiscard]] bool OverlapsGPU(GPUVAddr overlap_gpu_addr, size_t overlap_size) const noexcept {
  93. const GPUVAddr overlap_end = overlap_gpu_addr + overlap_size;
  94. const GPUVAddr gpu_addr_end = gpu_addr + size;
  95. return gpu_addr < overlap_end && overlap_gpu_addr < gpu_addr_end;
  96. }
  97. GPUVAddr gpu_addr;
  98. VAddr cpu_addr;
  99. size_t size;
  100. ImageId image_id;
  101. bool picked{};
  102. };
  103. struct ImageAllocBase {
  104. std::vector<ImageId> images;
  105. };
  106. void AddImageAlias(ImageBase& lhs, ImageBase& rhs, ImageId lhs_id, ImageId rhs_id);
  107. } // namespace VideoCommon