mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-20 19:07:01 +02:00
Optimize texture cache tracking (#123)
Optimize texture cache page tracking
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
#include "common/assert.h"
|
||||
#include "common/profiler.h"
|
||||
#include "graphics/host_gpu/renderer/cache/streamBuffer.h"
|
||||
#include "graphics/host_gpu/renderer/commandScheduler.h"
|
||||
#include "graphics/host_gpu/renderer/image/imageView.h"
|
||||
#include "graphics/host_gpu/renderer/renderTarget.h"
|
||||
#include "graphics/host_gpu/renderer/cache/streamBuffer.h"
|
||||
#include "kernel/memory.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -99,9 +99,9 @@ vk::ImageAspectFlags Image::FullAspectMask(vk::Format format) noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
Image::Barriers Image::GetBarriers(vk::ImageLayout destination_layout,
|
||||
vk::AccessFlags2 destination_access,
|
||||
vk::PipelineStageFlags2 destination_stage,
|
||||
Image::Barriers Image::GetBarriers(vk::ImageLayout destination_layout,
|
||||
vk::AccessFlags2 destination_access,
|
||||
vk::PipelineStageFlags2 destination_stage,
|
||||
std::optional<ImageSubresourceRange> range) {
|
||||
auto& state = backing.state;
|
||||
auto& subresource_states = backing.subresource_states;
|
||||
@@ -130,25 +130,25 @@ Image::Barriers Image::GetBarriers(vk::ImageLayout destination_layout,
|
||||
constexpr auto write_access = vk::AccessFlagBits2::eTransferWrite |
|
||||
vk::AccessFlagBits2::eShaderWrite |
|
||||
vk::AccessFlagBits2::eMemoryWrite;
|
||||
const bool repeated_write =
|
||||
const bool repeated_write =
|
||||
static_cast<bool>(subresource_state.access_mask & write_access);
|
||||
if (subresource_state.layout != destination_layout ||
|
||||
subresource_state.access_mask != destination_access || repeated_write) {
|
||||
vk::ImageMemoryBarrier2 barrier {};
|
||||
barrier.srcStageMask = subresource_state.pl_stage;
|
||||
barrier.srcAccessMask = subresource_state.access_mask;
|
||||
barrier.dstStageMask = destination_stage;
|
||||
barrier.dstAccessMask = destination_access;
|
||||
barrier.oldLayout = subresource_state.layout;
|
||||
barrier.newLayout = destination_layout;
|
||||
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.image = backing.image;
|
||||
barrier.subresourceRange.aspectMask = FullAspectMask(backing.format);
|
||||
barrier.subresourceRange.baseMipLevel = level;
|
||||
barrier.subresourceRange.levelCount = 1;
|
||||
barrier.subresourceRange.baseArrayLayer = layer;
|
||||
barrier.subresourceRange.layerCount = 1;
|
||||
barrier.srcStageMask = subresource_state.pl_stage;
|
||||
barrier.srcAccessMask = subresource_state.access_mask;
|
||||
barrier.dstStageMask = destination_stage;
|
||||
barrier.dstAccessMask = destination_access;
|
||||
barrier.oldLayout = subresource_state.layout;
|
||||
barrier.newLayout = destination_layout;
|
||||
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.image = backing.image;
|
||||
barrier.subresourceRange.aspectMask = FullAspectMask(backing.format);
|
||||
barrier.subresourceRange.baseMipLevel = level;
|
||||
barrier.subresourceRange.levelCount = 1;
|
||||
barrier.subresourceRange.baseArrayLayer = layer;
|
||||
barrier.subresourceRange.layerCount = 1;
|
||||
barriers.push_back(barrier);
|
||||
subresource_state = {destination_stage, destination_access, destination_layout};
|
||||
}
|
||||
@@ -159,10 +159,10 @@ Image::Barriers Image::GetBarriers(vk::ImageLayout destination_layout,
|
||||
subresource_states.clear();
|
||||
}
|
||||
} else {
|
||||
constexpr auto write_access = vk::AccessFlagBits2::eTransferWrite |
|
||||
vk::AccessFlagBits2::eShaderWrite |
|
||||
vk::AccessFlagBits2::eMemoryWrite;
|
||||
const bool repeated_write = static_cast<bool>(state.access_mask & write_access);
|
||||
constexpr auto write_access = vk::AccessFlagBits2::eTransferWrite |
|
||||
vk::AccessFlagBits2::eShaderWrite |
|
||||
vk::AccessFlagBits2::eMemoryWrite;
|
||||
const bool repeated_write = static_cast<bool>(state.access_mask & write_access);
|
||||
if (state.layout == destination_layout && state.access_mask == destination_access &&
|
||||
!repeated_write) {
|
||||
return {};
|
||||
@@ -191,8 +191,7 @@ Image::Barriers Image::GetBarriers(vk::ImageLayout destination_layout,
|
||||
}
|
||||
|
||||
void Image::Transit(vk::ImageLayout destination_layout, vk::AccessFlags2 destination_access,
|
||||
std::optional<ImageSubresourceRange> range,
|
||||
vk::CommandBuffer command_buffer) {
|
||||
std::optional<ImageSubresourceRange> range, vk::CommandBuffer command_buffer) {
|
||||
const auto transfer_access =
|
||||
vk::AccessFlagBits2::eTransferRead | vk::AccessFlagBits2::eTransferWrite;
|
||||
vk::PipelineStageFlags2 destination_stage {};
|
||||
@@ -201,8 +200,8 @@ void Image::Transit(vk::ImageLayout destination_layout, vk::AccessFlags2 destina
|
||||
}
|
||||
if (!destination_access ||
|
||||
static_cast<bool>(destination_access & ~vk::AccessFlags2 {transfer_access})) {
|
||||
destination_stage |= vk::PipelineStageFlagBits2::eAllGraphics |
|
||||
vk::PipelineStageFlagBits2::eComputeShader;
|
||||
destination_stage |=
|
||||
vk::PipelineStageFlagBits2::eAllGraphics | vk::PipelineStageFlagBits2::eComputeShader;
|
||||
}
|
||||
const auto barriers =
|
||||
GetBarriers(destination_layout, destination_access, destination_stage, range);
|
||||
@@ -218,10 +217,9 @@ void Image::Transit(vk::ImageLayout destination_layout, vk::AccessFlags2 destina
|
||||
command_buffer.pipelineBarrier2(dependency);
|
||||
}
|
||||
|
||||
void Image::Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer,
|
||||
uint64_t offset, uint64_t size) {
|
||||
EXIT_IF(m_scheduler == nullptr || copies.empty() || buffer == nullptr ||
|
||||
size == 0);
|
||||
void Image::Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer, uint64_t offset,
|
||||
uint64_t size) {
|
||||
EXIT_IF(m_scheduler == nullptr || copies.empty() || buffer == nullptr || size == 0);
|
||||
m_scheduler->EndRendering();
|
||||
vk::BufferMemoryBarrier2 buffer_barrier {};
|
||||
buffer_barrier.srcStageMask = vk::PipelineStageFlagBits2::eAllCommands;
|
||||
@@ -234,16 +232,15 @@ void Image::Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffe
|
||||
buffer_barrier.offset = offset;
|
||||
buffer_barrier.size = size;
|
||||
const auto image_barriers =
|
||||
GetBarriers(vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::AccessFlagBits2::eTransferWrite,
|
||||
GetBarriers(vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits2::eTransferWrite,
|
||||
vk::PipelineStageFlagBits2::eCopy, {});
|
||||
vk::DependencyInfo dependency {};
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.bufferMemoryBarrierCount = 1;
|
||||
dependency.pBufferMemoryBarriers = &buffer_barrier;
|
||||
dependency.imageMemoryBarrierCount = static_cast<uint32_t>(image_barriers.size());
|
||||
dependency.pImageMemoryBarriers = image_barriers.data();
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
dependency.imageMemoryBarrierCount = static_cast<uint32_t>(image_barriers.size());
|
||||
dependency.pImageMemoryBarriers = image_barriers.data();
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
command.pipelineBarrier2(dependency);
|
||||
command.copyBufferToImage(buffer, backing.image, vk::ImageLayout::eTransferDstOptimal,
|
||||
static_cast<uint32_t>(copies.size()), copies.data());
|
||||
@@ -256,8 +253,7 @@ void Image::Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffe
|
||||
dependency.pImageMemoryBarriers = nullptr;
|
||||
command.pipelineBarrier2(dependency);
|
||||
Transit(vk::ImageLayout::eGeneral,
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
}
|
||||
|
||||
void Image::Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer,
|
||||
@@ -265,7 +261,7 @@ void Image::Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buf
|
||||
EXIT_IF(m_scheduler == nullptr || copies.empty() || buffer == nullptr || size == 0);
|
||||
m_scheduler->EndRendering();
|
||||
vk::BufferMemoryBarrier2 buffer_barrier {};
|
||||
buffer_barrier.srcStageMask = vk::PipelineStageFlagBits2::eAllCommands;
|
||||
buffer_barrier.srcStageMask = vk::PipelineStageFlagBits2::eAllCommands;
|
||||
buffer_barrier.srcAccessMask =
|
||||
vk::AccessFlagBits2::eMemoryRead | vk::AccessFlagBits2::eMemoryWrite;
|
||||
buffer_barrier.dstStageMask = vk::PipelineStageFlagBits2::eCopy;
|
||||
@@ -276,16 +272,15 @@ void Image::Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buf
|
||||
buffer_barrier.offset = offset;
|
||||
buffer_barrier.size = size;
|
||||
const auto image_barriers =
|
||||
GetBarriers(vk::ImageLayout::eTransferSrcOptimal,
|
||||
vk::AccessFlagBits2::eTransferRead,
|
||||
GetBarriers(vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlagBits2::eTransferRead,
|
||||
vk::PipelineStageFlagBits2::eCopy, {});
|
||||
vk::DependencyInfo dependency {};
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.bufferMemoryBarrierCount = 1;
|
||||
dependency.pBufferMemoryBarriers = &buffer_barrier;
|
||||
dependency.imageMemoryBarrierCount = static_cast<uint32_t>(image_barriers.size());
|
||||
dependency.pImageMemoryBarriers = image_barriers.data();
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
dependency.imageMemoryBarrierCount = static_cast<uint32_t>(image_barriers.size());
|
||||
dependency.pImageMemoryBarriers = image_barriers.data();
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
command.pipelineBarrier2(dependency);
|
||||
command.copyImageToBuffer(backing.image, vk::ImageLayout::eTransferSrcOptimal, buffer,
|
||||
static_cast<uint32_t>(copies.size()), copies.data());
|
||||
@@ -299,11 +294,11 @@ void Image::Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buf
|
||||
command.pipelineBarrier2(dependency);
|
||||
}
|
||||
|
||||
std::pair<uint32_t, uint32_t>
|
||||
Image::SanitizeCopyLayers(const Image& source, const Image& destination, uint32_t depth) {
|
||||
const auto source_type = source.backing.image_type;
|
||||
const auto destination_type = destination.backing.image_type;
|
||||
uint32_t source_layers = source.backing.layers;
|
||||
std::pair<uint32_t, uint32_t> Image::SanitizeCopyLayers(const Image& source,
|
||||
const Image& destination, uint32_t depth) {
|
||||
const auto source_type = source.backing.image_type;
|
||||
const auto destination_type = destination.backing.image_type;
|
||||
uint32_t source_layers = source.backing.layers;
|
||||
uint32_t destination_layers = destination.backing.layers;
|
||||
if (source_type == vk::ImageType::e3D) {
|
||||
source_layers = 1;
|
||||
@@ -312,13 +307,10 @@ Image::SanitizeCopyLayers(const Image& source, const Image& destination, uint32_
|
||||
destination_layers = 1;
|
||||
}
|
||||
if (source_type == destination_type) {
|
||||
source_layers = destination_layers =
|
||||
std::min(source_layers, destination_layers);
|
||||
} else if (source_type == vk::ImageType::e2D &&
|
||||
destination_type == vk::ImageType::e3D) {
|
||||
source_layers = destination_layers = std::min(source_layers, destination_layers);
|
||||
} else if (source_type == vk::ImageType::e2D && destination_type == vk::ImageType::e3D) {
|
||||
source_layers = depth;
|
||||
} else if (source_type == vk::ImageType::e3D &&
|
||||
destination_type == vk::ImageType::e2D) {
|
||||
} else if (source_type == vk::ImageType::e3D && destination_type == vk::ImageType::e2D) {
|
||||
destination_layers = depth;
|
||||
}
|
||||
return {source_layers, destination_layers};
|
||||
@@ -327,12 +319,11 @@ Image::SanitizeCopyLayers(const Image& source, const Image& destination, uint32_
|
||||
void Image::CopyImage(Image& source) {
|
||||
EXIT_IF(m_scheduler == nullptr || source.backing.samples != backing.samples);
|
||||
m_scheduler->EndRendering();
|
||||
const uint32_t levels =
|
||||
std::min(source.backing.mip_levels, backing.mip_levels);
|
||||
const uint32_t levels = std::min(source.backing.mip_levels, backing.mip_levels);
|
||||
const uint32_t base_depth = backing.image_type == vk::ImageType::e3D
|
||||
? backing.extent.depth
|
||||
: source.backing.extent.depth;
|
||||
const auto source_aspect =
|
||||
const auto source_aspect =
|
||||
FullAspectMask(source.backing.format) & ~vk::ImageAspectFlagBits::eStencil;
|
||||
const auto destination_aspect =
|
||||
FullAspectMask(backing.format) & ~vk::ImageAspectFlagBits::eStencil;
|
||||
@@ -342,8 +333,7 @@ void Image::CopyImage(Image& source) {
|
||||
const auto width = std::max(source.backing.extent.width >> level, 1u);
|
||||
const auto height = std::max(source.backing.extent.height >> level, 1u);
|
||||
const auto depth = std::max(base_depth >> level, 1u);
|
||||
const auto [source_layers, destination_layers] =
|
||||
SanitizeCopyLayers(source, *this, depth);
|
||||
const auto [source_layers, destination_layers] = SanitizeCopyLayers(source, *this, depth);
|
||||
vk::ImageCopy copy {};
|
||||
copy.srcSubresource = {source_aspect, level, 0, 1};
|
||||
copy.dstSubresource = {destination_aspect, level, 0, 1};
|
||||
@@ -351,8 +341,7 @@ void Image::CopyImage(Image& source) {
|
||||
if (source.backing.image_type == vk::ImageType::e3D) {
|
||||
copy.extent = {width, height, depth};
|
||||
} else {
|
||||
copy.srcSubresource.layerCount =
|
||||
std::min(source_layers, destination_layers);
|
||||
copy.srcSubresource.layerCount = std::min(source_layers, destination_layers);
|
||||
copy.dstSubresource.layerCount = copy.srcSubresource.layerCount;
|
||||
copy.extent = {width, height, 1};
|
||||
}
|
||||
@@ -369,34 +358,30 @@ void Image::CopyImage(Image& source) {
|
||||
return;
|
||||
}
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal,
|
||||
vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal,
|
||||
backing.image, vk::ImageLayout::eTransferDstOptimal,
|
||||
static_cast<uint32_t>(copies.size()), copies.data());
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal, backing.image,
|
||||
vk::ImageLayout::eTransferDstOptimal, static_cast<uint32_t>(copies.size()),
|
||||
copies.data());
|
||||
Transit(vk::ImageLayout::eGeneral,
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
}
|
||||
|
||||
void Image::Resolve(Image& source, const ImageSubresourceRange& source_range,
|
||||
const ImageSubresourceRange& destination_range) {
|
||||
EXIT_IF(m_scheduler == nullptr || backing.samples != 1 ||
|
||||
source.backing.image_type != vk::ImageType::e2D ||
|
||||
backing.image_type != vk::ImageType::e2D ||
|
||||
source_range.level_count != 1 || destination_range.level_count != 1 ||
|
||||
backing.image_type != vk::ImageType::e2D || source_range.level_count != 1 ||
|
||||
destination_range.level_count != 1 ||
|
||||
source_range.base_level >= source.backing.mip_levels ||
|
||||
destination_range.base_level >= backing.mip_levels ||
|
||||
source_range.base_layer >= source.backing.layers ||
|
||||
destination_range.base_layer >= backing.layers);
|
||||
const auto layers = std::min(
|
||||
{source_range.layer_count, destination_range.layer_count,
|
||||
source.backing.layers - source_range.base_layer,
|
||||
backing.layers - destination_range.base_layer});
|
||||
const auto source_width =
|
||||
std::max(source.backing.extent.width >> source_range.base_level, 1u);
|
||||
const auto layers = std::min({source_range.layer_count, destination_range.layer_count,
|
||||
source.backing.layers - source_range.base_layer,
|
||||
backing.layers - destination_range.base_layer});
|
||||
const auto source_width = std::max(source.backing.extent.width >> source_range.base_level, 1u);
|
||||
const auto source_height =
|
||||
std::max(source.backing.extent.height >> source_range.base_level, 1u);
|
||||
const auto destination_width =
|
||||
@@ -404,43 +389,40 @@ void Image::Resolve(Image& source, const ImageSubresourceRange& source_range,
|
||||
const auto destination_height =
|
||||
std::max(backing.extent.height >> destination_range.base_level, 1u);
|
||||
const bool copy = source.backing.samples == 1;
|
||||
EXIT_IF(layers == 0 || info.extent.width > source_width ||
|
||||
info.extent.height > source_height || info.extent.width > destination_width ||
|
||||
info.extent.height > destination_height ||
|
||||
EXIT_IF(layers == 0 || info.extent.width > source_width || info.extent.height > source_height ||
|
||||
info.extent.width > destination_width || info.extent.height > destination_height ||
|
||||
(copy ? !ImageViewOps::FormatsCompatible(source.backing.format, backing.format)
|
||||
: source.backing.format != backing.format));
|
||||
auto resolved_source_range = source_range;
|
||||
auto resolved_destination_range = destination_range;
|
||||
auto resolved_source_range = source_range;
|
||||
auto resolved_destination_range = destination_range;
|
||||
resolved_source_range.layer_count = layers;
|
||||
resolved_destination_range.layer_count = layers;
|
||||
const vk::Extent3D resolve_extent {info.extent.width, info.extent.height, 1};
|
||||
|
||||
m_scheduler->EndRendering();
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal,
|
||||
vk::AccessFlagBits2::eTransferRead, resolved_source_range, command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::AccessFlagBits2::eTransferWrite, resolved_destination_range, command);
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlagBits2::eTransferRead,
|
||||
resolved_source_range, command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits2::eTransferWrite,
|
||||
resolved_destination_range, command);
|
||||
if (copy) {
|
||||
vk::ImageCopy region {};
|
||||
region.srcSubresource = {vk::ImageAspectFlagBits::eColor,
|
||||
resolved_source_range.base_level,
|
||||
region.srcSubresource = {vk::ImageAspectFlagBits::eColor, resolved_source_range.base_level,
|
||||
resolved_source_range.base_layer, layers};
|
||||
region.dstSubresource = {vk::ImageAspectFlagBits::eColor,
|
||||
resolved_destination_range.base_level,
|
||||
resolved_destination_range.base_layer, layers};
|
||||
region.extent = resolve_extent;
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal,
|
||||
backing.image, vk::ImageLayout::eTransferDstOptimal, region);
|
||||
region.extent = resolve_extent;
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal, backing.image,
|
||||
vk::ImageLayout::eTransferDstOptimal, region);
|
||||
} else {
|
||||
vk::ImageResolve region {};
|
||||
region.srcSubresource = {vk::ImageAspectFlagBits::eColor,
|
||||
resolved_source_range.base_level,
|
||||
region.srcSubresource = {vk::ImageAspectFlagBits::eColor, resolved_source_range.base_level,
|
||||
resolved_source_range.base_layer, layers};
|
||||
region.dstSubresource = {vk::ImageAspectFlagBits::eColor,
|
||||
resolved_destination_range.base_level,
|
||||
resolved_destination_range.base_layer, layers};
|
||||
region.extent = resolve_extent;
|
||||
region.extent = resolve_extent;
|
||||
command.resolveImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal,
|
||||
backing.image, vk::ImageLayout::eTransferDstOptimal, region);
|
||||
}
|
||||
@@ -454,22 +436,21 @@ uint32_t Image::CopyRows(uint64_t row_size, uint32_t rows, uint64_t capacity) no
|
||||
}
|
||||
|
||||
void Image::CopyImageWithBuffer(Image& source, Buffer& buffer) {
|
||||
EXIT_IF(m_scheduler == nullptr || buffer.Handle() == nullptr ||
|
||||
source.backing.samples != 1 || backing.samples != 1);
|
||||
EXIT_IF(m_scheduler == nullptr || buffer.Handle() == nullptr || source.backing.samples != 1 ||
|
||||
backing.samples != 1);
|
||||
m_scheduler->EndRendering();
|
||||
const uint32_t levels =
|
||||
std::min(source.backing.mip_levels, backing.mip_levels);
|
||||
const auto source_aspect =
|
||||
const uint32_t levels = std::min(source.backing.mip_levels, backing.mip_levels);
|
||||
const auto source_aspect =
|
||||
FullAspectMask(source.backing.format) & ~vk::ImageAspectFlagBits::eStencil;
|
||||
const auto destination_aspect =
|
||||
FullAspectMask(backing.format) & ~vk::ImageAspectFlagBits::eStencil;
|
||||
const auto source_bytes = DepthAspectTransferBytes(source.backing.format) != 0
|
||||
? DepthAspectTransferBytes(source.backing.format)
|
||||
: source.info.bytes_per_block;
|
||||
const auto destination_bytes = DepthAspectTransferBytes(backing.format) != 0
|
||||
? DepthAspectTransferBytes(backing.format)
|
||||
: info.bytes_per_block;
|
||||
const uint32_t source_block = source.info.IsBlock() ? 4u : 1u;
|
||||
const auto source_bytes = DepthAspectTransferBytes(source.backing.format) != 0
|
||||
? DepthAspectTransferBytes(source.backing.format)
|
||||
: source.info.bytes_per_block;
|
||||
const auto destination_bytes = DepthAspectTransferBytes(backing.format) != 0
|
||||
? DepthAspectTransferBytes(backing.format)
|
||||
: info.bytes_per_block;
|
||||
const uint32_t source_block = source.info.IsBlock() ? 4u : 1u;
|
||||
const uint32_t destination_block = info.IsBlock() ? 4u : 1u;
|
||||
EXIT_IF(levels == 0 || source_bytes == 0 || source_bytes != destination_bytes ||
|
||||
source_block != destination_block);
|
||||
@@ -484,74 +465,66 @@ void Image::CopyImageWithBuffer(Image& source, Buffer& buffer) {
|
||||
barrier.buffer = buffer.Handle();
|
||||
barrier.offset = 0;
|
||||
vk::DependencyInfo dependency {};
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.dependencyFlags = vk::DependencyFlagBits::eByRegion;
|
||||
dependency.bufferMemoryBarrierCount = 1;
|
||||
dependency.pBufferMemoryBarriers = &barrier;
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal,
|
||||
vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
for (uint32_t level = 0; level < levels; level++) {
|
||||
const auto width = std::max(source.backing.extent.width >> level, 1u);
|
||||
const auto height = std::max(source.backing.extent.height >> level, 1u);
|
||||
const auto source_depth = source.backing.image_type == vk::ImageType::e3D
|
||||
? std::max(source.backing.extent.depth >> level, 1u)
|
||||
: source.backing.layers;
|
||||
const auto width = std::max(source.backing.extent.width >> level, 1u);
|
||||
const auto height = std::max(source.backing.extent.height >> level, 1u);
|
||||
const auto source_depth = source.backing.image_type == vk::ImageType::e3D
|
||||
? std::max(source.backing.extent.depth >> level, 1u)
|
||||
: source.backing.layers;
|
||||
const auto destination_depth = backing.image_type == vk::ImageType::e3D
|
||||
? std::max(backing.extent.depth >> level, 1u)
|
||||
: backing.layers;
|
||||
const auto slices = std::min(source_depth, destination_depth);
|
||||
const auto block_rows = (height + source_block - 1) / source_block;
|
||||
const auto slices = std::min(source_depth, destination_depth);
|
||||
const auto block_rows = (height + source_block - 1) / source_block;
|
||||
const auto row_size =
|
||||
static_cast<uint64_t>((width + source_block - 1) / source_block) * source_bytes;
|
||||
const auto rows_per_copy = CopyRows(row_size, block_rows, buffer.Size());
|
||||
EXIT_IF(slices == 0 || rows_per_copy == 0);
|
||||
for (uint32_t slice = 0; slice < slices; slice++) {
|
||||
for (uint32_t block_row = 0; block_row < block_rows;
|
||||
block_row += rows_per_copy) {
|
||||
const auto copy_rows = std::min(rows_per_copy, block_rows - block_row);
|
||||
const auto y = block_row * source_block;
|
||||
const auto copy_height =
|
||||
std::min(copy_rows * source_block, height - y);
|
||||
const auto copy_size = row_size * copy_rows;
|
||||
for (uint32_t block_row = 0; block_row < block_rows; block_row += rows_per_copy) {
|
||||
const auto copy_rows = std::min(rows_per_copy, block_rows - block_row);
|
||||
const auto y = block_row * source_block;
|
||||
const auto copy_height = std::min(copy_rows * source_block, height - y);
|
||||
const auto copy_size = row_size * copy_rows;
|
||||
vk::BufferImageCopy source_copy {};
|
||||
source_copy.imageSubresource = {
|
||||
source_aspect, level,
|
||||
source.backing.image_type == vk::ImageType::e3D ? 0u : slice, 1};
|
||||
source_copy.imageOffset = {
|
||||
0, static_cast<int32_t>(y),
|
||||
source.backing.image_type == vk::ImageType::e3D
|
||||
? static_cast<int32_t>(slice)
|
||||
: 0};
|
||||
source_copy.imageExtent = {width, copy_height, 1};
|
||||
auto destination_copy = source_copy;
|
||||
source_copy.imageOffset = {0, static_cast<int32_t>(y),
|
||||
source.backing.image_type == vk::ImageType::e3D
|
||||
? static_cast<int32_t>(slice)
|
||||
: 0};
|
||||
source_copy.imageExtent = {width, copy_height, 1};
|
||||
auto destination_copy = source_copy;
|
||||
destination_copy.imageSubresource = {
|
||||
destination_aspect, level,
|
||||
backing.image_type == vk::ImageType::e3D ? 0u : slice, 1};
|
||||
destination_copy.imageOffset.z =
|
||||
backing.image_type == vk::ImageType::e3D
|
||||
? static_cast<int32_t>(slice)
|
||||
: 0;
|
||||
backing.image_type == vk::ImageType::e3D ? static_cast<int32_t>(slice) : 0;
|
||||
barrier.size = copy_size;
|
||||
barrier.srcAccessMask = vk::AccessFlagBits2::eTransferRead;
|
||||
barrier.dstAccessMask = vk::AccessFlagBits2::eTransferWrite;
|
||||
command.pipelineBarrier2(dependency);
|
||||
command.copyImageToBuffer(source.backing.image,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
buffer.Handle(), source_copy);
|
||||
vk::ImageLayout::eTransferSrcOptimal, buffer.Handle(),
|
||||
source_copy);
|
||||
barrier.srcAccessMask = vk::AccessFlagBits2::eTransferWrite;
|
||||
barrier.dstAccessMask = vk::AccessFlagBits2::eTransferRead;
|
||||
command.pipelineBarrier2(dependency);
|
||||
command.copyBufferToImage(buffer.Handle(), backing.image,
|
||||
vk::ImageLayout::eTransferDstOptimal,
|
||||
destination_copy);
|
||||
vk::ImageLayout::eTransferDstOptimal, destination_copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
Transit(vk::ImageLayout::eGeneral,
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
}
|
||||
|
||||
void Image::CopyMip(Image& source, uint32_t mip, uint32_t layer) {
|
||||
@@ -561,11 +534,9 @@ void Image::CopyMip(Image& source, uint32_t mip, uint32_t layer) {
|
||||
const auto width = std::max(backing.extent.width >> mip, 1u);
|
||||
const auto height = std::max(backing.extent.height >> mip, 1u);
|
||||
const auto depth = std::max(backing.extent.depth >> mip, 1u);
|
||||
EXIT_IF(width != source.backing.extent.width ||
|
||||
height != source.backing.extent.height);
|
||||
const auto [source_layers, destination_layers] =
|
||||
SanitizeCopyLayers(source, *this, depth);
|
||||
const auto aspects = FullAspectMask(source.backing.format);
|
||||
EXIT_IF(width != source.backing.extent.width || height != source.backing.extent.height);
|
||||
const auto [source_layers, destination_layers] = SanitizeCopyLayers(source, *this, depth);
|
||||
const auto aspects = FullAspectMask(source.backing.format);
|
||||
EXIT_IF(aspects != FullAspectMask(backing.format));
|
||||
std::array<vk::ImageCopy, 2> copies {};
|
||||
uint32_t copy_count = 0;
|
||||
@@ -580,16 +551,13 @@ void Image::CopyMip(Image& source, uint32_t mip, uint32_t layer) {
|
||||
copy.extent = {width, height, depth};
|
||||
}
|
||||
auto command = m_scheduler->Current().Handle();
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal,
|
||||
vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal,
|
||||
vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal,
|
||||
backing.image, vk::ImageLayout::eTransferDstOptimal, copy_count,
|
||||
copies.data());
|
||||
Transit(vk::ImageLayout::eTransferDstOptimal, vk::AccessFlagBits2::eTransferWrite, {}, command);
|
||||
source.Transit(vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
command.copyImage(source.backing.image, vk::ImageLayout::eTransferSrcOptimal, backing.image,
|
||||
vk::ImageLayout::eTransferDstOptimal, copy_count, copies.data());
|
||||
Transit(vk::ImageLayout::eGeneral,
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {},
|
||||
command);
|
||||
vk::AccessFlagBits2::eShaderRead | vk::AccessFlagBits2::eTransferRead, {}, command);
|
||||
}
|
||||
|
||||
namespace ImageOps {
|
||||
@@ -601,8 +569,7 @@ void Validate(const ImageInfo& info) {
|
||||
if (info.pixel_format == vk::Format::eUndefined) {
|
||||
const bool metadata_empty =
|
||||
info.metadata.range.address == 0 && info.metadata.range.size == 0 &&
|
||||
info.metadata.kind == ImageMetadataKind::None &&
|
||||
info.metadata.control == 0 &&
|
||||
info.metadata.kind == ImageMetadataKind::None && info.metadata.control == 0 &&
|
||||
info.metadata.compression == VideoOutCompression::Uncompressed &&
|
||||
!info.metadata.stencil_compressed;
|
||||
if (info.data.Empty() || info.HasStencil() || !metadata_empty || info.extent.width == 0 ||
|
||||
@@ -615,9 +582,9 @@ void Validate(const ImageInfo& info) {
|
||||
}
|
||||
|
||||
if (info.extent.width == 0 || info.extent.height == 0 || info.extent.depth == 0 ||
|
||||
info.resources.levels == 0 ||
|
||||
info.resources.levels > info.mip_layout.size() || info.resources.layers == 0 ||
|
||||
info.samples == 0 || vulkan_sample_count(info.samples) == vk::SampleCountFlagBits {} ||
|
||||
info.resources.levels == 0 || info.resources.levels > info.mip_layout.size() ||
|
||||
info.resources.layers == 0 || info.samples == 0 ||
|
||||
vulkan_sample_count(info.samples) == vk::SampleCountFlagBits {} ||
|
||||
info.bytes_per_block == 0 || (info.data.address != 0 && info.pitch == 0)) {
|
||||
EXIT("invalid image geometry or format\n");
|
||||
}
|
||||
@@ -688,11 +655,11 @@ uint32_t RenderTargetTransferFormat(uint32_t bytes_per_element) {
|
||||
|
||||
} // namespace ImageOps
|
||||
|
||||
Image::Image(GraphicContext& graphics, CommandScheduler& scheduler,
|
||||
const ImageInfo& image_info)
|
||||
Image::Image(GraphicContext& graphics, CommandScheduler& scheduler, const ImageInfo& image_info)
|
||||
: info(image_info), m_graphics(&graphics), m_scheduler(&scheduler) {
|
||||
KYTY_PROFILER_FUNCTION();
|
||||
ImageOps::Validate(info);
|
||||
m_cpu_dirty = !info.data.Empty();
|
||||
if (info.pixel_format == vk::Format::eUndefined) {
|
||||
return;
|
||||
}
|
||||
@@ -742,9 +709,9 @@ Image::Image(GraphicContext& graphics, CommandScheduler& scheduler,
|
||||
}
|
||||
|
||||
uint64_t Image::HashGuestEdges() const {
|
||||
constexpr uint64_t page_mask = TRACKER_PAGE_SIZE - 1;
|
||||
constexpr uint64_t page_mask = TRACKER_PAGE_SIZE - 1;
|
||||
std::array<uint8_t, TRACKER_PAGE_SIZE * 2> bytes {};
|
||||
const auto range = info.data;
|
||||
const auto range = info.data;
|
||||
const uint64_t head_end = std::min(range.End(), (range.address + page_mask) & ~page_mask);
|
||||
const uint64_t tail_begin = std::max(range.address, range.End() & ~page_mask);
|
||||
const uint64_t head_size = head_end - range.address;
|
||||
|
||||
@@ -66,16 +66,16 @@ public:
|
||||
[[nodiscard]] vk::ImageView FindView(const ImageViewInfo& view_info);
|
||||
void AssociateDepth(ImageId image_id) { depth_id = image_id; }
|
||||
using Barriers = std::vector<vk::ImageMemoryBarrier2>;
|
||||
[[nodiscard]] Barriers
|
||||
GetBarriers(vk::ImageLayout destination_layout, vk::AccessFlags2 destination_access,
|
||||
vk::PipelineStageFlags2 destination_stage,
|
||||
std::optional<ImageSubresourceRange> range);
|
||||
[[nodiscard]] Barriers GetBarriers(vk::ImageLayout destination_layout,
|
||||
vk::AccessFlags2 destination_access,
|
||||
vk::PipelineStageFlags2 destination_stage,
|
||||
std::optional<ImageSubresourceRange> range);
|
||||
void Transit(vk::ImageLayout destination_layout, vk::AccessFlags2 destination_access,
|
||||
std::optional<ImageSubresourceRange> range, vk::CommandBuffer command_buffer);
|
||||
void Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer,
|
||||
uint64_t offset, uint64_t size);
|
||||
void Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer,
|
||||
uint64_t offset, uint64_t size);
|
||||
void Upload(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer, uint64_t offset,
|
||||
uint64_t size);
|
||||
void Download(std::span<const vk::BufferImageCopy> copies, vk::Buffer buffer, uint64_t offset,
|
||||
uint64_t size);
|
||||
void CopyImage(Image& source);
|
||||
void Resolve(Image& source, const ImageSubresourceRange& source_range,
|
||||
const ImageSubresourceRange& destination_range);
|
||||
@@ -84,8 +84,8 @@ public:
|
||||
|
||||
void InvalidateCpuWrite(uint64_t vaddr, uint64_t size) {
|
||||
if (ImageRangeOverlaps(info.data.address, info.data.size, vaddr, size)) {
|
||||
m_cpu_dirty = true;
|
||||
m_maybe_cpu_dirty = false;
|
||||
m_cpu_dirty = true;
|
||||
m_maybe_cpu_dirty = false;
|
||||
m_maybe_hash_valid = false;
|
||||
} else if (ImagePageRangesOverlap(info.data.address, info.data.size, vaddr, size)) {
|
||||
m_maybe_cpu_dirty = true;
|
||||
@@ -95,6 +95,11 @@ public:
|
||||
[[nodiscard]] bool IsCpuDirty() const { return m_cpu_dirty || m_maybe_cpu_dirty; }
|
||||
[[nodiscard]] bool IsDefinitelyCpuDirty() const { return m_cpu_dirty; }
|
||||
[[nodiscard]] bool IsMaybeCpuDirty() const { return m_maybe_cpu_dirty; }
|
||||
void MarkMaybeCpuDirty() {
|
||||
if (!m_cpu_dirty) {
|
||||
m_maybe_cpu_dirty = true;
|
||||
}
|
||||
}
|
||||
[[nodiscard]] bool NeedsMaybeCpuHash() const {
|
||||
return m_maybe_cpu_dirty && !m_maybe_hash_valid;
|
||||
}
|
||||
@@ -102,14 +107,14 @@ public:
|
||||
if (!NeedsMaybeCpuHash()) {
|
||||
EXIT("image cannot initialize maybe-dirty hash\n");
|
||||
}
|
||||
m_maybe_cpu_hash = hash;
|
||||
m_maybe_cpu_hash = hash;
|
||||
m_maybe_hash_valid = true;
|
||||
}
|
||||
[[nodiscard]] bool ResolveMaybeCpuHash(uint64_t hash) {
|
||||
if (!m_maybe_cpu_dirty || !m_maybe_hash_valid || m_cpu_dirty) {
|
||||
EXIT("image cannot resolve maybe-dirty hash\n");
|
||||
}
|
||||
m_maybe_cpu_dirty = false;
|
||||
m_maybe_cpu_dirty = false;
|
||||
m_maybe_hash_valid = false;
|
||||
m_cpu_dirty |= hash != m_maybe_cpu_hash;
|
||||
return m_cpu_dirty;
|
||||
@@ -125,14 +130,15 @@ public:
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsGpuModified() const noexcept { return m_gpu_modified; }
|
||||
void MarkGpuModified() noexcept { m_gpu_modified = true; }
|
||||
void ClearGpuModified() noexcept { m_gpu_modified = false; }
|
||||
void MarkGpuModified() noexcept { m_gpu_modified = true; }
|
||||
void ClearGpuModified() noexcept { m_gpu_modified = false; }
|
||||
|
||||
[[nodiscard]] bool IsBufferModified() const noexcept { return m_buffer_modified; }
|
||||
void MarkBufferModified() noexcept { m_buffer_modified = true; }
|
||||
void ClearBufferModified() noexcept { m_buffer_modified = false; }
|
||||
void MarkBufferModified() noexcept { m_buffer_modified = true; }
|
||||
void ClearBufferModified() noexcept { m_buffer_modified = false; }
|
||||
|
||||
[[nodiscard]] bool Overlaps(uint64_t address, uint64_t size, bool pages = false) const noexcept {
|
||||
[[nodiscard]] bool Overlaps(uint64_t address, uint64_t size,
|
||||
bool pages = false) const noexcept {
|
||||
return pages ? ImagePageRangesOverlap(info.data.address, info.data.size, address, size)
|
||||
: ImageRangeOverlaps(info.data.address, info.data.size, address, size);
|
||||
}
|
||||
@@ -142,38 +148,41 @@ public:
|
||||
[[nodiscard]] bool SafeToDownload() const noexcept {
|
||||
return IsGpuModified() && !IsBufferModified() && !IsCpuDirty();
|
||||
}
|
||||
[[nodiscard]] bool IsTracked() const noexcept { return track_addr != 0 && track_addr_end != 0; }
|
||||
[[nodiscard]] uint64_t AccountedSize() const noexcept {
|
||||
return backing.image == nullptr ? 0 : (info.data.size + 1023) & ~uint64_t {1023};
|
||||
}
|
||||
[[nodiscard]] uint64_t HashGuestEdges() const;
|
||||
|
||||
ImageInfo info;
|
||||
VulkanImage backing;
|
||||
ImageViewCache views;
|
||||
ImageUsage usage;
|
||||
ImageBinding binding;
|
||||
bool registered = false;
|
||||
ImageId depth_id {};
|
||||
uint64_t tick_accessed_last = 0;
|
||||
size_t lru_id = 0;
|
||||
ImageInfo info;
|
||||
VulkanImage backing;
|
||||
ImageViewCache views;
|
||||
ImageUsage usage;
|
||||
ImageBinding binding;
|
||||
bool registered = false;
|
||||
uint64_t track_addr = 0;
|
||||
uint64_t track_addr_end = 0;
|
||||
ImageId depth_id {};
|
||||
uint64_t tick_accessed_last = 0;
|
||||
size_t lru_id = 0;
|
||||
|
||||
private:
|
||||
friend struct ImageTestAccess;
|
||||
|
||||
[[nodiscard]] static vk::ImageAspectFlags FullAspectMask(vk::Format format) noexcept;
|
||||
[[nodiscard]] static uint32_t CopyRows(uint64_t row_size, uint32_t rows,
|
||||
uint64_t capacity) noexcept;
|
||||
[[nodiscard]] static uint32_t CopyRows(uint64_t row_size, uint32_t rows,
|
||||
uint64_t capacity) noexcept;
|
||||
[[nodiscard]] static std::pair<uint32_t, uint32_t>
|
||||
SanitizeCopyLayers(const Image& source, const Image& destination, uint32_t depth);
|
||||
|
||||
GraphicContext* m_graphics = nullptr;
|
||||
CommandScheduler* m_scheduler = nullptr;
|
||||
uint64_t m_maybe_cpu_hash = 0;
|
||||
bool m_cpu_dirty = false;
|
||||
bool m_maybe_cpu_dirty = false;
|
||||
bool m_maybe_hash_valid = false;
|
||||
bool m_gpu_modified = false;
|
||||
bool m_buffer_modified = false;
|
||||
GraphicContext* m_graphics = nullptr;
|
||||
CommandScheduler* m_scheduler = nullptr;
|
||||
uint64_t m_maybe_cpu_hash = 0;
|
||||
bool m_cpu_dirty = false;
|
||||
bool m_maybe_cpu_dirty = false;
|
||||
bool m_maybe_hash_valid = false;
|
||||
bool m_gpu_modified = false;
|
||||
bool m_buffer_modified = false;
|
||||
};
|
||||
|
||||
namespace ImageOps {
|
||||
|
||||
Reference in New Issue
Block a user