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

View file

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

View file

@ -316,6 +316,7 @@ struct TextureCacheParams {
static constexpr bool FRAMEBUFFER_BLITS = false; static constexpr bool FRAMEBUFFER_BLITS = false;
static constexpr bool HAS_EMULATED_COPIES = false; static constexpr bool HAS_EMULATED_COPIES = false;
static constexpr bool HAS_DEVICE_MEMORY_INFO = true; static constexpr bool HAS_DEVICE_MEMORY_INFO = true;
static constexpr bool HAS_PIXEL_FORMAT_CONVERSIONS = false;
using Runtime = Vulkan::TextureCacheRuntime; using Runtime = Vulkan::TextureCacheRuntime;
using Image = Vulkan::Image; 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(dst.info.type != ImageType::e2D);
UNIMPLEMENTED_IF(src.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) { for (const ImageCopy& copy : copies) {
UNIMPLEMENTED_IF(copy.dst_subresource.num_layers != 1); UNIMPLEMENTED_IF(copy.dst_subresource.num_layers != 1);
UNIMPLEMENTED_IF(copy.src_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; static constexpr bool HAS_EMULATED_COPIES = P::HAS_EMULATED_COPIES;
/// True when the API can provide info about the memory of the device. /// 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; 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_EXPECTED_MEMORY = 1_GiB;
static constexpr u64 DEFAULT_CRITICAL_MEMORY = 2_GiB; static constexpr u64 DEFAULT_CRITICAL_MEMORY = 2_GiB;