Merge pull request #7349 from ameerj/ogl-convert-image

gl_texture_cache: Implement pixel format conversions for copies
This commit is contained in:
bunnei 2021-11-18 11:30:47 -08:00 committed by GitHub
commit 0bc46fedd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 28 deletions

View file

@ -396,6 +396,10 @@ OGLTexture MakeImage(const VideoCommon::ImageInfo& info, GLenum gl_internal_form
UNREACHABLE_MSG("Invalid image format={}", format);
return GL_R32UI;
}
[[nodiscard]] u32 NextPow2(u32 value) {
return 1U << (32U - std::countl_zero(value - 1U));
}
} // Anonymous namespace
ImageBufferMap::~ImageBufferMap() {
@ -522,6 +526,12 @@ void TextureCacheRuntime::CopyImage(Image& dst_image, Image& src_image,
}
}
void TextureCacheRuntime::ConvertImage(Image& dst, Image& src,
std::span<const VideoCommon::ImageCopy> copies) {
LOG_DEBUG(Render_OpenGL, "Converting {} to {}", src.info.format, dst.info.format);
format_conversion_pass.ConvertImage(dst, src, copies);
}
bool TextureCacheRuntime::CanImageBeCopied(const Image& dst, const Image& src) {
if (dst.info.type == ImageType::e3D && dst.info.format == PixelFormat::BC4_UNORM) {
return false;
@ -538,7 +548,7 @@ void TextureCacheRuntime::EmulateCopyImage(Image& dst, Image& src,
ASSERT(src.info.type == ImageType::e3D);
util_shaders.CopyBC4(dst, src, copies);
} else if (IsPixelFormatBGR(dst.info.format) || IsPixelFormatBGR(src.info.format)) {
bgr_copy_pass.CopyBGR(dst, src, copies);
format_conversion_pass.ConvertImage(dst, src, copies);
} else {
UNREACHABLE();
}
@ -1286,35 +1296,37 @@ Framebuffer::Framebuffer(TextureCacheRuntime& runtime, std::span<ImageView*, NUM
Framebuffer::~Framebuffer() = default;
void BGRCopyPass::CopyBGR(Image& dst_image, Image& src_image,
std::span<const VideoCommon::ImageCopy> copies) {
static constexpr VideoCommon::Offset3D zero_offset{0, 0, 0};
void FormatConversionPass::ConvertImage(Image& dst_image, Image& src_image,
std::span<const VideoCommon::ImageCopy> copies) {
const GLenum dst_target = ImageTarget(dst_image.info);
const GLenum src_target = ImageTarget(src_image.info);
const u32 img_bpp = BytesPerBlock(src_image.info.format);
for (const ImageCopy& copy : copies) {
ASSERT(copy.src_offset == zero_offset);
ASSERT(copy.dst_offset == zero_offset);
const u32 num_src_layers = static_cast<u32>(copy.src_subresource.num_layers);
const u32 copy_size = copy.extent.width * copy.extent.height * num_src_layers * img_bpp;
if (bgr_pbo_size < copy_size) {
bgr_pbo.Create();
bgr_pbo_size = copy_size;
glNamedBufferData(bgr_pbo.handle, bgr_pbo_size, nullptr, GL_STREAM_COPY);
const auto src_origin = MakeCopyOrigin(copy.src_offset, copy.src_subresource, src_target);
const auto dst_origin = MakeCopyOrigin(copy.dst_offset, copy.dst_subresource, dst_target);
const auto region = MakeCopyRegion(copy.extent, copy.dst_subresource, dst_target);
const u32 copy_size = region.width * region.height * region.depth * img_bpp;
if (pbo_size < copy_size) {
intermediate_pbo.Create();
pbo_size = NextPow2(copy_size);
glNamedBufferData(intermediate_pbo.handle, pbo_size, nullptr, GL_STREAM_COPY);
}
// Copy from source to PBO
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ROW_LENGTH, copy.extent.width);
glBindBuffer(GL_PIXEL_PACK_BUFFER, bgr_pbo.handle);
glGetTextureSubImage(src_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height,
num_src_layers, src_image.GlFormat(), src_image.GlType(),
static_cast<GLsizei>(bgr_pbo_size), nullptr);
glBindBuffer(GL_PIXEL_PACK_BUFFER, intermediate_pbo.handle);
glGetTextureSubImage(src_image.Handle(), src_origin.level, src_origin.x, src_origin.y,
src_origin.z, region.width, region.height, region.depth,
src_image.GlFormat(), src_image.GlType(),
static_cast<GLsizei>(pbo_size), nullptr);
// Copy from PBO to destination in desired GL format
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, copy.extent.width);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bgr_pbo.handle);
glTextureSubImage3D(dst_image.Handle(), 0, 0, 0, 0, copy.extent.width, copy.extent.height,
copy.dst_subresource.num_layers, dst_image.GlFormat(),
dst_image.GlType(), nullptr);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, intermediate_pbo.handle);
glTextureSubImage3D(dst_image.Handle(), dst_origin.level, dst_origin.x, dst_origin.y,
dst_origin.z, region.width, region.height, region.depth,
dst_image.GlFormat(), dst_image.GlType(), nullptr);
}
}

View file

@ -52,17 +52,17 @@ struct FormatProperties {
bool is_compressed;
};
class BGRCopyPass {
class FormatConversionPass {
public:
BGRCopyPass() = default;
~BGRCopyPass() = default;
FormatConversionPass() = default;
~FormatConversionPass() = default;
void CopyBGR(Image& dst_image, Image& src_image,
std::span<const VideoCommon::ImageCopy> copies);
void ConvertImage(Image& dst_image, Image& src_image,
std::span<const VideoCommon::ImageCopy> copies);
private:
OGLBuffer bgr_pbo;
size_t bgr_pbo_size{};
OGLBuffer intermediate_pbo;
size_t pbo_size{};
};
class TextureCacheRuntime {
@ -86,6 +86,8 @@ public:
void CopyImage(Image& dst, Image& src, std::span<const VideoCommon::ImageCopy> copies);
void ConvertImage(Image& dst, Image& src, std::span<const VideoCommon::ImageCopy> copies);
void ConvertImage(Framebuffer* dst, ImageView& dst_view, ImageView& src_view, bool rescaled) {
UNIMPLEMENTED();
}
@ -144,7 +146,7 @@ private:
const Device& device;
StateTracker& state_tracker;
UtilShaders util_shaders;
BGRCopyPass bgr_copy_pass;
FormatConversionPass format_conversion_pass;
std::array<std::unordered_map<GLenum, FormatProperties>, 3> format_properties;
bool has_broken_texture_view_formats = false;
@ -336,6 +338,7 @@ struct TextureCacheParams {
static constexpr bool FRAMEBUFFER_BLITS = true;
static constexpr bool HAS_EMULATED_COPIES = true;
static constexpr bool HAS_DEVICE_MEMORY_INFO = true;
static constexpr bool HAS_PIXEL_FORMAT_CONVERSIONS = true;
using Runtime = OpenGL::TextureCacheRuntime;
using Image = OpenGL::Image;

View file

@ -316,6 +316,7 @@ struct TextureCacheParams {
static constexpr bool FRAMEBUFFER_BLITS = false;
static constexpr bool HAS_EMULATED_COPIES = false;
static constexpr bool HAS_DEVICE_MEMORY_INFO = true;
static constexpr bool HAS_PIXEL_FORMAT_CONVERSIONS = false;
using Runtime = Vulkan::TextureCacheRuntime;
using Image = Vulkan::Image;

View file

@ -1759,6 +1759,9 @@ void TextureCache<P>::CopyImage(ImageId dst_id, ImageId src_id, std::vector<Imag
}
UNIMPLEMENTED_IF(dst.info.type != ImageType::e2D);
UNIMPLEMENTED_IF(src.info.type != ImageType::e2D);
if constexpr (HAS_PIXEL_FORMAT_CONVERSIONS) {
return runtime.ConvertImage(dst, src, copies);
}
for (const ImageCopy& copy : copies) {
UNIMPLEMENTED_IF(copy.dst_subresource.num_layers != 1);
UNIMPLEMENTED_IF(copy.src_subresource.num_layers != 1);

View file

@ -59,6 +59,8 @@ class TextureCache {
static constexpr bool HAS_EMULATED_COPIES = P::HAS_EMULATED_COPIES;
/// True when the API can provide info about the memory of the device.
static constexpr bool HAS_DEVICE_MEMORY_INFO = P::HAS_DEVICE_MEMORY_INFO;
/// True when the API provides utilities for pixel format conversions.
static constexpr bool HAS_PIXEL_FORMAT_CONVERSIONS = P::HAS_PIXEL_FORMAT_CONVERSIONS;
static constexpr u64 DEFAULT_EXPECTED_MEMORY = 1_GiB;
static constexpr u64 DEFAULT_CRITICAL_MEMORY = 2_GiB;