- [GPU] Rename TextureMemoryUsage to TextureExtent (and relevant functions/etc).

- [GPU] Precalculate memory byte usage for base mip (and any additional mip levels).
- [GPU] Change TextureCache::WatchCallback so that if it's fired multiple times for the same texture it does not assert.
- [GPU] Add write watches for texture memory associated with additional mipmap levels.
This commit is contained in:
gibbed 2018-05-31 19:36:25 -05:00
parent cd39bbed5c
commit e89a31006f
9 changed files with 213 additions and 152 deletions

View File

@ -67,8 +67,7 @@ bool TextureInfo::Prepare(const xe_gpu_texture_fetch_t& fetch,
info.is_tiled = fetch.tiled; info.is_tiled = fetch.tiled;
info.has_packed_mips = fetch.packed_mips; info.has_packed_mips = fetch.packed_mips;
info.guest_address = fetch.address << 12; info.SetupMemoryInfo(fetch.base_address << 12, fetch.mip_address << 12);
info.mip_address = fetch.mip_address << 12;
if (info.format_info()->format == TextureFormat::kUnknown) { if (info.format_info()->format == TextureFormat::kUnknown) {
XELOGE("Attempting to fetch from unsupported texture format %d", XELOGE("Attempting to fetch from unsupported texture format %d",
@ -76,7 +75,7 @@ bool TextureInfo::Prepare(const xe_gpu_texture_fetch_t& fetch,
return false; return false;
} }
info.memory_usage = TextureMemoryUsage::Calculate(out_info, true); info.extent = TextureExtent::Calculate(out_info, true);
return true; return true;
} }
@ -92,25 +91,27 @@ bool TextureInfo::PrepareResolve(uint32_t physical_address,
auto& info = *out_info; auto& info = *out_info;
info.format = format; info.format = format;
info.endianness = endian;
info.dimension = Dimension::k2D; info.dimension = Dimension::k2D;
info.width = width - 1; info.width = width - 1;
info.height = height - 1; info.height = height - 1;
info.mip_levels = 1;
info.depth = depth - 1; info.depth = depth - 1;
info.pitch = pitch; info.pitch = pitch;
info.mip_levels = 1;
info.endianness = endian;
info.is_tiled = true; info.is_tiled = true;
info.has_packed_mips = false;
info.guest_address = physical_address; info.SetupMemoryInfo(physical_address, 0);
info.mip_address = 0;
if (info.format_info()->format == TextureFormat::kUnknown) { if (info.format_info()->format == TextureFormat::kUnknown) {
assert_true("Unsupported texture format"); assert_true("Unsupported texture format");
return false; return false;
} }
info.memory_usage = TextureMemoryUsage::Calculate(out_info, true); info.extent = TextureExtent::Calculate(out_info, true);
return true; return true;
} }
@ -118,15 +119,15 @@ uint32_t TextureInfo::GetMaxMipLevels() const {
return 1 + xe::log2_floor(std::max({width + 1, height + 1, depth + 1})); return 1 + xe::log2_floor(std::max({width + 1, height + 1, depth + 1}));
} }
const TextureMemoryUsage TextureInfo::GetMipMemoryUsage(uint32_t mip, const TextureExtent TextureInfo::GetMipExtent(uint32_t mip,
bool is_guest) const { bool is_guest) const {
if (mip == 0) { if (mip == 0) {
return memory_usage; return extent;
} }
uint32_t mip_width = xe::next_pow2(width + 1) >> mip; uint32_t mip_width = xe::next_pow2(width + 1) >> mip;
uint32_t mip_height = xe::next_pow2(height + 1) >> mip; uint32_t mip_height = xe::next_pow2(height + 1) >> mip;
return TextureMemoryUsage::Calculate(format_info(), mip_width, mip_height, return TextureExtent::Calculate(format_info(), mip_width, mip_height,
depth + 1, is_tiled, is_guest); depth + 1, is_tiled, is_guest);
} }
void TextureInfo::GetMipSize(uint32_t mip, uint32_t* out_width, void TextureInfo::GetMipSize(uint32_t mip, uint32_t* out_width,
@ -154,7 +155,7 @@ uint32_t TextureInfo::GetMipLocation(uint32_t mip, uint32_t* offset_x,
} else { } else {
GetPackedTileOffset(0, offset_x, offset_y); GetPackedTileOffset(0, offset_x, offset_y);
} }
return guest_address; return memory.base_address;
} }
uint32_t address_base, address_offset; uint32_t address_base, address_offset;
@ -162,19 +163,22 @@ uint32_t TextureInfo::GetMipLocation(uint32_t mip, uint32_t* offset_x,
// If the texture is <= 16 pixels w/h, the mips are packed with the base // If the texture is <= 16 pixels w/h, the mips are packed with the base
// texture. Otherwise, they're stored beginning from mip_address. // texture. Otherwise, they're stored beginning from mip_address.
if (std::min(width, height) < 16) { if (std::min(width, height) < 16) {
address_base = guest_address; address_base = memory.base_address;
address_offset = 0; address_offset = 0;
} else if (guest_address == mip_address) { } else if (memory.base_address == memory.mip_address) {
address_base = guest_address; address_base = memory.base_address;
address_offset = GetMipByteSize(0, is_guest); address_offset = memory.base_size;
} else { } else {
address_base = mip_address; address_base = memory.mip_address;
address_offset = 0; address_offset = 0;
} }
auto bytes_per_block = format_info()->bytes_per_block();
if (!has_packed_mips) { if (!has_packed_mips) {
for (uint32_t i = 1; i < mip; i++) { for (uint32_t i = 1; i < mip; i++) {
address_offset += GetMipByteSize(i, is_guest); address_offset +=
GetMipExtent(i, is_guest).all_blocks() * bytes_per_block;
} }
*offset_x = 0; *offset_x = 0;
*offset_y = 0; *offset_y = 0;
@ -193,7 +197,7 @@ uint32_t TextureInfo::GetMipLocation(uint32_t mip, uint32_t* offset_x,
// We've reached the point where the mips are packed into a single tile. // We've reached the point where the mips are packed into a single tile.
break; break;
} }
address_offset += GetMipByteSize(i, is_guest); address_offset += GetMipExtent(i, is_guest).all_blocks() * bytes_per_block;
} }
// Now, check if the mip is packed at an offset. // Now, check if the mip is packed at an offset.
@ -202,26 +206,6 @@ uint32_t TextureInfo::GetMipLocation(uint32_t mip, uint32_t* offset_x,
return address_base + address_offset; return address_base + address_offset;
} }
uint32_t TextureInfo::GetMipByteSize(uint32_t mip, bool is_guest) const {
uint32_t bytes_per_block = format_info()->bytes_per_block();
auto mip_usage = GetMipMemoryUsage(mip, is_guest);
return mip_usage.all_blocks() * bytes_per_block;
}
uint32_t TextureInfo::GetMipVisibleByteSize(uint32_t mip, bool is_guest) const {
uint32_t bytes_per_block = format_info()->bytes_per_block();
auto mip_usage = GetMipMemoryUsage(mip, is_guest);
return mip_usage.visible_blocks() * bytes_per_block;
}
uint32_t TextureInfo::GetByteSize(bool is_guest) const {
uint32_t length = 0;
for (uint32_t mip = 0; mip < mip_levels; ++mip) {
length += GetMipByteSize(mip, is_guest);
}
return length;
}
bool TextureInfo::GetPackedTileOffset(uint32_t width, uint32_t height, bool TextureInfo::GetPackedTileOffset(uint32_t width, uint32_t height,
const FormatInfo* format_info, const FormatInfo* format_info,
int packed_tile, uint32_t* offset_x, int packed_tile, uint32_t* offset_x,
@ -314,5 +298,57 @@ uint64_t TextureInfo::hash() const {
return XXH64(this, sizeof(TextureInfo), 0); return XXH64(this, sizeof(TextureInfo), 0);
} }
void TextureInfo::SetupMemoryInfo(uint32_t base_address, uint32_t mip_address) {
memory.base_address = base_address;
memory.base_size = 0;
memory.mip_address = 0;
memory.mip_size = 0;
uint32_t bytes_per_block = format_info()->bytes_per_block();
if (mip_levels <= 1) {
// Sort circuit. Only one mip.
memory.base_size = GetMipExtent(0, true).all_blocks() * bytes_per_block;
return;
}
if (!has_packed_mips) {
assert_true(mip_address == 0 || mip_address == base_address);
for (uint32_t mip = 0; mip < mip_levels - 1; mip++) {
memory.base_size +=
GetMipExtent(mip, true).all_blocks() * bytes_per_block;
}
memory.base_size +=
GetMipExtent(mip_levels - 1, true).visible_blocks() * bytes_per_block;
return;
}
uint32_t total_size = 0;
uint32_t width_pow2 = xe::next_pow2(width + 1);
uint32_t height_pow2 = xe::next_pow2(height + 1);
// Walk forward to find the address of the mip.
uint32_t packed_mip_base = 1;
for (uint32_t mip = packed_mip_base; mip < mip_levels - 1;
mip++, packed_mip_base++) {
uint32_t mip_width = std::max(width_pow2 >> mip, 1u);
uint32_t mip_height = std::max(height_pow2 >> mip, 1u);
if (std::min(mip_width, mip_height) <= 16) {
// We've reached the point where the mips are packed into a single tile.
break;
}
total_size += GetMipExtent(mip, true).all_blocks() * bytes_per_block;
}
if (mip_address != base_address) {
memory.mip_address = mip_address;
memory.mip_size = total_size;
} else {
memory.base_size = GetMipExtent(0, true).all_blocks() * bytes_per_block;
memory.base_size += total_size;
}
}
} // namespace gpu } // namespace gpu
} // namespace xe } // namespace xe

View File

@ -290,7 +290,7 @@ struct FormatInfo {
struct TextureInfo; struct TextureInfo;
struct TextureMemoryUsage { struct TextureExtent {
uint32_t pitch; // texel pitch uint32_t pitch; // texel pitch
uint32_t height; // texel height uint32_t height; // texel height
uint32_t block_height; // # of vertical blocks uint32_t block_height; // # of vertical blocks
@ -303,12 +303,18 @@ struct TextureMemoryUsage {
return block_pitch_h * block_height * depth; return block_pitch_h * block_height * depth;
} }
static TextureMemoryUsage Calculate(const FormatInfo* format_info, static TextureExtent Calculate(const FormatInfo* format_info, uint32_t pitch,
uint32_t pitch, uint32_t height, uint32_t height, uint32_t depth, bool is_tiled,
uint32_t depth, bool is_tiled, bool is_guest);
bool is_guest); static TextureExtent Calculate(const TextureInfo* texture_info,
static TextureMemoryUsage Calculate(const TextureInfo* texture_info, bool is_guest);
bool is_guest); };
struct TextureMemoryInfo {
uint32_t base_address;
uint32_t base_size;
uint32_t mip_address;
uint32_t mip_size;
}; };
struct TextureInfo { struct TextureInfo {
@ -324,10 +330,8 @@ struct TextureInfo {
bool is_tiled; bool is_tiled;
bool has_packed_mips; bool has_packed_mips;
TextureMemoryUsage memory_usage; TextureMemoryInfo memory;
TextureExtent extent;
uint32_t guest_address;
uint32_t mip_address;
const FormatInfo* format_info() const { const FormatInfo* format_info() const {
return FormatInfo::Get(static_cast<uint32_t>(format)); return FormatInfo::Get(static_cast<uint32_t>(format));
@ -347,7 +351,7 @@ struct TextureInfo {
uint32_t GetMaxMipLevels() const; uint32_t GetMaxMipLevels() const;
const TextureMemoryUsage GetMipMemoryUsage(uint32_t mip, bool is_guest) const; const TextureExtent GetMipExtent(uint32_t mip, bool is_guest) const;
void GetMipSize(uint32_t mip, uint32_t* width, uint32_t* height) const; void GetMipSize(uint32_t mip, uint32_t* width, uint32_t* height) const;
@ -355,11 +359,6 @@ struct TextureInfo {
uint32_t GetMipLocation(uint32_t mip, uint32_t* offset_x, uint32_t* offset_y, uint32_t GetMipLocation(uint32_t mip, uint32_t* offset_x, uint32_t* offset_y,
bool is_guest) const; bool is_guest) const;
uint32_t GetMipByteSize(uint32_t mip, bool is_guest) const;
uint32_t GetMipVisibleByteSize(uint32_t mip, bool is_guest) const;
uint32_t GetByteSize(bool is_guest) const;
static bool GetPackedTileOffset(uint32_t width, uint32_t height, static bool GetPackedTileOffset(uint32_t width, uint32_t height,
const FormatInfo* format_info, const FormatInfo* format_info,
int packed_tile, uint32_t* offset_x, int packed_tile, uint32_t* offset_x,
@ -372,6 +371,9 @@ struct TextureInfo {
bool operator==(const TextureInfo& other) const { bool operator==(const TextureInfo& other) const {
return std::memcmp(this, &other, sizeof(TextureInfo)) == 0; return std::memcmp(this, &other, sizeof(TextureInfo)) == 0;
} }
private:
void SetupMemoryInfo(uint32_t base_address, uint32_t mip_address);
}; };
} // namespace gpu } // namespace gpu

View File

@ -19,11 +19,11 @@ namespace gpu {
using namespace xe::gpu::xenos; using namespace xe::gpu::xenos;
static TextureMemoryUsage CalculateMemoryUsage(const FormatInfo* format_info, static TextureExtent CalculateExtent(const FormatInfo* format_info,
uint32_t pitch, uint32_t height, uint32_t pitch, uint32_t height,
uint32_t depth, bool is_tiled, uint32_t depth, bool is_tiled,
bool is_guest) { bool is_guest) {
TextureMemoryUsage usage; TextureExtent usage;
usage.pitch = pitch; usage.pitch = pitch;
usage.height = height; usage.height = height;
@ -60,21 +60,17 @@ static TextureMemoryUsage CalculateMemoryUsage(const FormatInfo* format_info,
return usage; return usage;
} }
TextureMemoryUsage TextureMemoryUsage::Calculate(const FormatInfo* format_info, TextureExtent TextureExtent::Calculate(const FormatInfo* format_info,
uint32_t pitch, uint32_t pitch, uint32_t height,
uint32_t height, uint32_t depth, bool is_tiled,
uint32_t depth, bool is_tiled, bool is_guest) {
bool is_guest) { return CalculateExtent(format_info, pitch, height, depth, is_tiled, is_guest);
return CalculateMemoryUsage(format_info, pitch, height, depth, is_tiled,
is_guest);
} }
TextureMemoryUsage TextureMemoryUsage::Calculate(const TextureInfo* info, TextureExtent TextureExtent::Calculate(const TextureInfo* info, bool is_guest) {
bool is_guest) {
assert_not_null(info); assert_not_null(info);
return CalculateMemoryUsage(info->format_info(), info->pitch, return CalculateExtent(info->format_info(), info->pitch, info->height + 1,
info->height + 1, info->depth + 1, info->is_tiled, info->depth + 1, info->is_tiled, is_guest);
is_guest);
} }
} // namespace gpu } // namespace gpu

View File

@ -686,7 +686,7 @@ void TraceViewer::DrawTextureInfo(
} }
ImGui::NextColumn(); ImGui::NextColumn();
ImGui::Text("Fetch Slot: %u", texture_binding.fetch_constant); ImGui::Text("Fetch Slot: %u", texture_binding.fetch_constant);
ImGui::Text("Guest Address: %.8X", texture_info.guest_address); ImGui::Text("Guest Address: %.8X", texture_info.memory.base_address);
ImGui::Text("Format: %s", texture_info.format_info()->name); ImGui::Text("Format: %s", texture_info.format_info()->name);
switch (texture_info.dimension) { switch (texture_info.dimension) {
case Dimension::k1D: case Dimension::k1D:

View File

@ -316,6 +316,10 @@ void TextureCache::WatchCallback(void* context_ptr, void* data_ptr,
uint32_t address) { uint32_t address) {
auto self = reinterpret_cast<TextureCache*>(context_ptr); auto self = reinterpret_cast<TextureCache*>(context_ptr);
auto touched_texture = reinterpret_cast<Texture*>(data_ptr); auto touched_texture = reinterpret_cast<Texture*>(data_ptr);
if (!touched_texture || !touched_texture->access_watch_handle) {
return;
}
// Clear watch handle first so we don't redundantly // Clear watch handle first so we don't redundantly
// remove. // remove.
assert_not_zero(touched_texture->access_watch_handle); assert_not_zero(touched_texture->access_watch_handle);
@ -343,8 +347,12 @@ TextureCache::Texture* TextureCache::DemandResolveTexture(
} }
// Tell the trace writer to "cache" this memory (but not read it) // Tell the trace writer to "cache" this memory (but not read it)
trace_writer_->WriteMemoryReadCachedNop(texture_info.guest_address, trace_writer_->WriteMemoryReadCached(texture_info.memory.base_address,
texture_info.GetByteSize(true)); texture_info.memory.base_size);
if (texture_info.memory.mip_address) {
trace_writer_->WriteMemoryReadCached(texture_info.memory.mip_address,
texture_info.memory.mip_size);
}
return it->second; return it->second;
} }
@ -370,18 +378,21 @@ TextureCache::Texture* TextureCache::DemandResolveTexture(
device_->DbgSetObjectName( device_->DbgSetObjectName(
reinterpret_cast<uint64_t>(texture->image), reinterpret_cast<uint64_t>(texture->image),
VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
xe::format_string("RT: 0x%.8X - 0x%.8X (%s, %s)", xe::format_string(
texture_info.guest_address, "RT: 0x%.8X - 0x%.8X (%s, %s)", texture_info.memory.base_address,
texture_info.guest_address + texture_info.memory.base_address + texture_info.memory.base_size,
texture_info.GetMipVisibleByteSize(0, true), texture_info.format_info()->name,
texture_info.format_info()->name, get_dimension_name(texture_info.dimension)));
get_dimension_name(texture_info.dimension)));
// Setup an access watch. If this texture is touched, it is destroyed. // Setup an access watch. If this texture is touched, it is destroyed.
// TODO(gibbed): Setup access watch for mipmap data.
texture->access_watch_handle = memory_->AddPhysicalAccessWatch( texture->access_watch_handle = memory_->AddPhysicalAccessWatch(
texture_info.guest_address, texture_info.GetMipVisibleByteSize(0, true), texture_info.memory.base_address, texture_info.memory.base_size,
cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture); cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture);
if (texture_info.memory.mip_address) {
texture->access_watch_handle = memory_->AddPhysicalAccessWatch(
texture_info.memory.mip_address, texture_info.memory.mip_size,
cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture);
}
textures_[texture_hash] = texture; textures_[texture_hash] = texture;
COUNT_profile_set("gpu/texture_cache/textures", textures_.size()); COUNT_profile_set("gpu/texture_cache/textures", textures_.size());
@ -401,8 +412,12 @@ TextureCache::Texture* TextureCache::Demand(const TextureInfo& texture_info,
break; break;
} }
trace_writer_->WriteMemoryReadCached(texture_info.guest_address, trace_writer_->WriteMemoryReadCached(texture_info.memory.base_address,
texture_info.GetByteSize(true)); texture_info.memory.base_size);
if (texture_info.memory.mip_address) {
trace_writer_->WriteMemoryReadCached(texture_info.memory.mip_address,
texture_info.memory.mip_size);
}
return it->second; return it->second;
} }
} }
@ -427,21 +442,30 @@ TextureCache::Texture* TextureCache::Demand(const TextureInfo& texture_info,
// TODO: Byte count -> pixel count (on x and y axes) // TODO: Byte count -> pixel count (on x and y axes)
VkOffset2D offset; VkOffset2D offset;
auto collide_tex = LookupAddress( auto collide_tex = LookupAddress(
texture_info.guest_address, texture_info.width + 1, texture_info.memory.base_address, texture_info.width + 1,
texture_info.height + 1, texture_info.format_info()->format, &offset); texture_info.height + 1, texture_info.format_info()->format, &offset);
if (collide_tex != nullptr) { if (collide_tex != nullptr) {
// assert_always(); // assert_always();
} }
trace_writer_->WriteMemoryRead(texture_info.guest_address, trace_writer_->WriteMemoryReadCached(texture_info.memory.base_address,
texture_info.GetByteSize(true)); texture_info.memory.base_size);
if (texture_info.memory.mip_address) {
trace_writer_->WriteMemoryReadCached(texture_info.memory.mip_address,
texture_info.memory.mip_size);
}
// Okay. Put a writewatch on it to tell us if it's been modified from the // Okay. Put a writewatch on it to tell us if it's been modified from the
// guest. // guest.
// TODO(gibbed): Setup access watch for mipmap data. // TODO(gibbed): Setup access watch for mipmap data.
texture->access_watch_handle = memory_->AddPhysicalAccessWatch( texture->access_watch_handle = memory_->AddPhysicalAccessWatch(
texture_info.guest_address, texture_info.GetMipVisibleByteSize(0, true), texture_info.memory.base_address, texture_info.memory.base_size,
cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture); cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture);
if (texture_info.memory.mip_address) {
texture->access_watch_handle = memory_->AddPhysicalAccessWatch(
texture_info.memory.mip_address, texture_info.memory.mip_size,
cpu::MMIOHandler::kWatchWrite, &WatchCallback, this, texture);
}
if (!UploadTexture(command_buffer, completion_fence, texture, texture_info)) { if (!UploadTexture(command_buffer, completion_fence, texture, texture_info)) {
FreeTexture(texture); FreeTexture(texture);
@ -453,8 +477,8 @@ TextureCache::Texture* TextureCache::Demand(const TextureInfo& texture_info,
reinterpret_cast<uint64_t>(texture->image), reinterpret_cast<uint64_t>(texture->image),
VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
xe::format_string( xe::format_string(
"T: 0x%.8X - 0x%.8X (%s, %s)", texture_info.guest_address, "T: 0x%.8X - 0x%.8X (%s, %s)", texture_info.memory.base_address,
texture_info.guest_address + texture_info.GetByteSize(true), texture_info.memory.base_address + texture_info.memory.base_size,
texture_info.format_info()->name, texture_info.format_info()->name,
get_dimension_name(texture_info.dimension))); get_dimension_name(texture_info.dimension)));
@ -712,24 +736,28 @@ TextureCache::Texture* TextureCache::Lookup(const TextureInfo& texture_info) {
return it->second; return it->second;
} }
} }
// slow path // slow path
for (auto it = textures_.begin(); it != textures_.end(); ++it) { for (auto it = textures_.begin(); it != textures_.end(); ++it) {
const auto& other_texture_info = it->second->texture_info; const auto& other_texture_info = it->second->texture_info;
#define COMPARE_FIELD(x) \ #define COMPARE_FIELD(x) \
if (texture_info.x != other_texture_info.x) continue if (texture_info.x != other_texture_info.x) continue
COMPARE_FIELD(guest_address); COMPARE_FIELD(memory.base_address);
COMPARE_FIELD(memory.base_size);
COMPARE_FIELD(dimension); COMPARE_FIELD(dimension);
COMPARE_FIELD(width); COMPARE_FIELD(width);
COMPARE_FIELD(height); COMPARE_FIELD(height);
COMPARE_FIELD(depth); COMPARE_FIELD(depth);
COMPARE_FIELD(endianness); COMPARE_FIELD(endianness);
COMPARE_FIELD(is_tiled); COMPARE_FIELD(is_tiled);
COMPARE_FIELD(GetByteSize(true));
#undef COMPARE_FIELD #undef COMPARE_FIELD
if (!TextureFormatIsSimilar(texture_info.format, if (!TextureFormatIsSimilar(texture_info.format,
other_texture_info.format)) { other_texture_info.format)) {
continue; continue;
} }
/*const auto format_info = texture_info.format_info(); /*const auto format_info = texture_info.format_info();
const auto other_format_info = other_texture_info.format_info(); const auto other_format_info = other_texture_info.format_info();
#define COMPARE_FIELD(x) if (format_info->x != other_format_info->x) continue #define COMPARE_FIELD(x) if (format_info->x != other_format_info->x) continue
@ -751,12 +779,12 @@ TextureCache::Texture* TextureCache::LookupAddress(uint32_t guest_address,
VkOffset2D* out_offset) { VkOffset2D* out_offset) {
for (auto it = textures_.begin(); it != textures_.end(); ++it) { for (auto it = textures_.begin(); it != textures_.end(); ++it) {
const auto& texture_info = it->second->texture_info; const auto& texture_info = it->second->texture_info;
if (guest_address >= texture_info.guest_address && if (guest_address >= texture_info.memory.base_address &&
guest_address < texture_info.guest_address + guest_address <
texture_info.GetMipVisibleByteSize(0, true) && texture_info.memory.base_address + texture_info.memory.base_size &&
texture_info.pitch >= width && texture_info.height >= height && texture_info.pitch >= width && texture_info.height >= height &&
out_offset) { out_offset) {
auto offset_bytes = guest_address - texture_info.guest_address; auto offset_bytes = guest_address - texture_info.memory.base_address;
if (texture_info.dimension == Dimension::k2D) { if (texture_info.dimension == Dimension::k2D) {
out_offset->x = 0; out_offset->x = 0;
@ -769,7 +797,7 @@ TextureCache::Texture* TextureCache::LookupAddress(uint32_t guest_address,
return it->second; return it->second;
} }
if (texture_info.guest_address == guest_address && if (texture_info.memory.base_address == guest_address &&
texture_info.dimension == Dimension::k2D && texture_info.dimension == Dimension::k2D &&
texture_info.pitch == width && texture_info.height == height) { texture_info.pitch == width && texture_info.height == height) {
if (out_offset) { if (out_offset) {
@ -831,60 +859,60 @@ bool TextureCache::ConvertTexture(uint8_t* dest, VkBufferImageCopy* copy_region,
void* host_address = memory_->TranslatePhysical(address); void* host_address = memory_->TranslatePhysical(address);
auto is_cube = src.dimension == Dimension::kCube; auto is_cube = src.dimension == Dimension::kCube;
auto src_usage = src.GetMipMemoryUsage(mip, true); auto src_extent = src.GetMipExtent(mip, true);
auto dst_usage = GetMipMemoryUsage(src, mip); auto dst_extent = GetMipExtent(src, mip);
uint32_t src_pitch = uint32_t src_pitch =
src_usage.block_pitch_h * src.format_info()->bytes_per_block(); src_extent.block_pitch_h * src.format_info()->bytes_per_block();
uint32_t dst_pitch = uint32_t dst_pitch =
dst_usage.block_pitch_h * GetFormatInfo(src.format)->bytes_per_block(); dst_extent.block_pitch_h * GetFormatInfo(src.format)->bytes_per_block();
auto copy_block = GetFormatCopyBlock(src.format); auto copy_block = GetFormatCopyBlock(src.format);
const uint8_t* src_mem = reinterpret_cast<const uint8_t*>(host_address); const uint8_t* src_mem = reinterpret_cast<const uint8_t*>(host_address);
if (!src.is_tiled) { if (!src.is_tiled) {
for (uint32_t face = 0; face < dst_usage.depth; face++) { for (uint32_t face = 0; face < dst_extent.depth; face++) {
src_mem += offset_y * src_pitch; src_mem += offset_y * src_pitch;
src_mem += offset_x * src.format_info()->bytes_per_block(); src_mem += offset_x * src.format_info()->bytes_per_block();
for (uint32_t y = 0; y < dst_usage.block_height; y++) { for (uint32_t y = 0; y < dst_extent.block_height; y++) {
copy_block(src.endianness, dest + y * dst_pitch, copy_block(src.endianness, dest + y * dst_pitch,
src_mem + y * src_pitch, dst_pitch); src_mem + y * src_pitch, dst_pitch);
} }
src_mem += src_pitch * src_usage.block_pitch_v; src_mem += src_pitch * src_extent.block_pitch_v;
dest += dst_pitch * dst_usage.block_pitch_v; dest += dst_pitch * dst_extent.block_pitch_v;
} }
} else { } else {
// Untile image. // Untile image.
// We could do this in a shader to speed things up, as this is pretty slow. // We could do this in a shader to speed things up, as this is pretty slow.
for (uint32_t face = 0; face < dst_usage.depth; face++) { for (uint32_t face = 0; face < dst_extent.depth; face++) {
texture_conversion::UntileInfo untile_info; texture_conversion::UntileInfo untile_info;
std::memset(&untile_info, 0, sizeof(untile_info)); std::memset(&untile_info, 0, sizeof(untile_info));
untile_info.offset_x = offset_x; untile_info.offset_x = offset_x;
untile_info.offset_y = offset_y; untile_info.offset_y = offset_y;
untile_info.width = dst_usage.block_pitch_h; untile_info.width = dst_extent.block_pitch_h;
untile_info.height = dst_usage.block_height; untile_info.height = dst_extent.block_height;
untile_info.input_pitch = src_usage.block_pitch_h; untile_info.input_pitch = src_extent.block_pitch_h;
untile_info.output_pitch = dst_usage.block_pitch_h; untile_info.output_pitch = dst_extent.block_pitch_h;
untile_info.input_format_info = src.format_info(); untile_info.input_format_info = src.format_info();
untile_info.output_format_info = GetFormatInfo(src.format); untile_info.output_format_info = GetFormatInfo(src.format);
untile_info.copy_callback = [=](auto o, auto i, auto l) { untile_info.copy_callback = [=](auto o, auto i, auto l) {
copy_block(src.endianness, o, i, l); copy_block(src.endianness, o, i, l);
}; };
texture_conversion::Untile(dest, src_mem, &untile_info); texture_conversion::Untile(dest, src_mem, &untile_info);
src_mem += src_pitch * src_usage.block_pitch_v; src_mem += src_pitch * src_extent.block_pitch_v;
dest += dst_pitch * dst_usage.block_pitch_v; dest += dst_pitch * dst_extent.block_pitch_v;
} }
} }
copy_region->bufferRowLength = dst_usage.pitch; copy_region->bufferRowLength = dst_extent.pitch;
copy_region->bufferImageHeight = dst_usage.height; copy_region->bufferImageHeight = dst_extent.height;
copy_region->imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copy_region->imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_region->imageSubresource.mipLevel = mip; copy_region->imageSubresource.mipLevel = mip;
copy_region->imageSubresource.baseArrayLayer = 0; copy_region->imageSubresource.baseArrayLayer = 0;
copy_region->imageSubresource.layerCount = !is_cube ? 1 : 6; copy_region->imageSubresource.layerCount = !is_cube ? 1 : 6;
copy_region->imageExtent.width = std::max(1u, (src.width + 1) >> mip); copy_region->imageExtent.width = std::max(1u, (src.width + 1) >> mip);
copy_region->imageExtent.height = std::max(1u, (src.height + 1) >> mip); copy_region->imageExtent.height = std::max(1u, (src.height + 1) >> mip);
copy_region->imageExtent.depth = !is_cube ? dst_usage.depth : 1; copy_region->imageExtent.depth = !is_cube ? dst_extent.depth : 1;
return true; return true;
} }
@ -898,11 +926,12 @@ bool TextureCache::UploadTexture(VkCommandBuffer command_buffer,
size_t unpack_length = ComputeTextureStorage(src); size_t unpack_length = ComputeTextureStorage(src);
XELOGGPU( XELOGGPU(
"Uploading texture @ 0x%.8X (%dx%dx%d, length: 0x%.8X, format: %s, dim: " "Uploading texture @ 0x%.8X/0x%.8X (%dx%dx%d, length: 0x%.8X, format: "
"%s, levels: %d, tiled: %s)", "%s, dim: %s, levels: %d, tiled: %s)",
src.guest_address, src.width + 1, src.height + 1, src.depth + 1, src.memory.base_address, src.memory.mip_address, src.width + 1,
unpack_length, src.format_info()->name, get_dimension_name(src.dimension), src.height + 1, src.depth + 1, unpack_length, src.format_info()->name,
src.mip_levels, src.is_tiled ? "yes" : "no"); get_dimension_name(src.dimension), src.mip_levels,
src.is_tiled ? "yes" : "no");
if (!unpack_length) { if (!unpack_length) {
XELOGW("Failed to compute texture storage!"); XELOGW("Failed to compute texture storage!");
@ -936,8 +965,8 @@ bool TextureCache::UploadTexture(VkCommandBuffer command_buffer,
// DEBUG: Check the source address. If it's completely zero'd out, print it. // DEBUG: Check the source address. If it's completely zero'd out, print it.
bool valid = false; bool valid = false;
auto src_data = memory_->TranslatePhysical(src.guest_address); auto src_data = memory_->TranslatePhysical(src.memory.base_address);
for (uint32_t i = 0; i < unpack_length; i++) { for (uint32_t i = 0; i < src.memory.base_size; i++) {
if (src_data[i] != 0) { if (src_data[i] != 0) {
valid = true; valid = true;
break; break;
@ -945,7 +974,7 @@ bool TextureCache::UploadTexture(VkCommandBuffer command_buffer,
} }
if (!valid) { if (!valid) {
XELOGW("Warning: Texture @ 0x%.8X is blank!", src.guest_address); XELOGW("Warning: Texture @ 0x%.8X is blank!", src.memory.base_address);
} }
// Upload texture into GPU memory. // Upload texture into GPU memory.
@ -1057,21 +1086,20 @@ texture_conversion::CopyBlockCallback TextureCache::GetFormatCopyBlock(
} }
} }
TextureMemoryUsage TextureCache::GetMipMemoryUsage(const TextureInfo& src, TextureExtent TextureCache::GetMipExtent(const TextureInfo& src, uint32_t mip) {
uint32_t mip) {
auto format_info = GetFormatInfo(src.format); auto format_info = GetFormatInfo(src.format);
uint32_t width = src.width + 1; uint32_t width = src.width + 1;
uint32_t height = src.height + 1; uint32_t height = src.height + 1;
uint32_t depth = src.depth + 1; uint32_t depth = src.depth + 1;
TextureMemoryUsage usage; TextureExtent usage;
if (mip == 0) { if (mip == 0) {
usage = TextureMemoryUsage::Calculate(format_info, width, height, depth, usage = TextureExtent::Calculate(format_info, width, height, depth, width,
width, false); false);
} else { } else {
uint32_t mip_width = xe::next_pow2(width) >> mip; uint32_t mip_width = xe::next_pow2(width) >> mip;
uint32_t mip_height = xe::next_pow2(height) >> mip; uint32_t mip_height = xe::next_pow2(height) >> mip;
usage = TextureMemoryUsage::Calculate(format_info, mip_width, mip_height, usage = TextureExtent::Calculate(format_info, mip_width, mip_height, depth,
depth, mip_width, false); mip_width, false);
} }
return usage; return usage;
} }
@ -1080,15 +1108,15 @@ uint32_t TextureCache::ComputeMipStorage(const FormatInfo* format_info,
uint32_t width, uint32_t height, uint32_t width, uint32_t height,
uint32_t depth, uint32_t mip) { uint32_t depth, uint32_t mip) {
assert_not_null(format_info); assert_not_null(format_info);
TextureMemoryUsage usage; TextureExtent usage;
if (mip == 0) { if (mip == 0) {
usage = TextureMemoryUsage::Calculate(format_info, width, height, depth, usage = TextureExtent::Calculate(format_info, width, height, depth, false,
false, false); false);
} else { } else {
uint32_t mip_width = xe::next_pow2(width) >> mip; uint32_t mip_width = xe::next_pow2(width) >> mip;
uint32_t mip_height = xe::next_pow2(height) >> mip; uint32_t mip_height = xe::next_pow2(height) >> mip;
usage = TextureMemoryUsage::Calculate(format_info, mip_width, mip_height, usage = TextureExtent::Calculate(format_info, mip_width, mip_height, depth,
depth, false, false); false, false);
} }
uint32_t bytes_per_block = format_info->bytes_per_block(); uint32_t bytes_per_block = format_info->bytes_per_block();
return usage.all_blocks() * bytes_per_block; return usage.all_blocks() * bytes_per_block;
@ -1184,10 +1212,10 @@ void TextureCache::WritebackTexture(Texture* texture) {
wb_command_pool_->EndBatch(); wb_command_pool_->EndBatch();
auto dest = memory_->TranslatePhysical(texture->texture_info.guest_address);
if (status == VK_SUCCESS) { if (status == VK_SUCCESS) {
std::memcpy(dest, alloc->host_ptr, auto dest =
texture->texture_info.GetByteSize(false)); memory_->TranslatePhysical(texture->texture_info.memory.base_address);
std::memcpy(dest, alloc->host_ptr, texture->texture_info.memory.base_size);
} }
wb_staging_buffer_.Scavenge(); wb_staging_buffer_.Scavenge();

View File

@ -156,8 +156,7 @@ class TextureCache {
static const FormatInfo* GetFormatInfo(TextureFormat format); static const FormatInfo* GetFormatInfo(TextureFormat format);
static texture_conversion::CopyBlockCallback GetFormatCopyBlock( static texture_conversion::CopyBlockCallback GetFormatCopyBlock(
TextureFormat format); TextureFormat format);
static TextureMemoryUsage GetMipMemoryUsage(const TextureInfo& src, static TextureExtent GetMipExtent(const TextureInfo& src, uint32_t mip);
uint32_t mip);
static uint32_t ComputeMipStorage(const FormatInfo* format_info, static uint32_t ComputeMipStorage(const FormatInfo* format_info,
uint32_t width, uint32_t height, uint32_t width, uint32_t height,
uint32_t depth, uint32_t mip); uint32_t depth, uint32_t mip);

View File

@ -1048,7 +1048,7 @@ bool VulkanCommandProcessor::IssueCopy() {
texture->in_flight_fence = current_batch_fence_; texture->in_flight_fence = current_batch_fence_;
// For debugging purposes only (trace viewer) // For debugging purposes only (trace viewer)
last_copy_base_ = texture->texture_info.guest_address; last_copy_base_ = texture->texture_info.memory.base_address;
if (!frame_open_) { if (!frame_open_) {
BeginFrame(); BeginFrame();

View File

@ -408,12 +408,12 @@ XEPACKEDUNION(xe_gpu_texture_fetch_t, {
uint32_t pitch : 9; // +22 byte_pitch >> 5 uint32_t pitch : 9; // +22 byte_pitch >> 5
uint32_t tiled : 1; // +31 uint32_t tiled : 1; // +31
uint32_t format : 6; // +0 dword_1 uint32_t format : 6; // +0 dword_1
uint32_t endianness : 2; // +6 uint32_t endianness : 2; // +6
uint32_t request_size : 2; // +8 uint32_t request_size : 2; // +8
uint32_t stacked : 1; // +10 uint32_t stacked : 1; // +10
uint32_t clamp_policy : 1; // +11 d3d/opengl uint32_t clamp_policy : 1; // +11 d3d/opengl
uint32_t address : 20; // +12 uint32_t base_address : 20; // +12
union { // dword_2 union { // dword_2
struct { struct {

View File

@ -381,7 +381,7 @@ void VdSwap(lpvoid_t buffer_ptr, // ptr into primary ringbuffer
buffer_ptr.Zero(64 * 4); buffer_ptr.Zero(64 * 4);
// virtual -> physical // virtual -> physical
fetch.address &= 0x1FFFF; fetch.base_address &= 0x1FFFF;
uint32_t offset = 0; uint32_t offset = 0;
auto dwords = buffer_ptr.as_array<uint32_t>(); auto dwords = buffer_ptr.as_array<uint32_t>();