Compare commits
1 Commits
mmap-fixew
...
macroify-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c303e8a1d1 |
@@ -13,17 +13,12 @@
|
||||
#include "common/fs/fs_android.h"
|
||||
#endif
|
||||
#include "common/logging/log.h"
|
||||
#include "common/literals.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#include <share.h>
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
@@ -41,13 +36,13 @@ namespace {
|
||||
#ifdef _WIN32
|
||||
|
||||
/**
|
||||
* Converts the file access mode and file type enums to a file access mode wide string.
|
||||
*
|
||||
* @param mode File access mode
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A pointer to a wide string representing the file access mode.
|
||||
*/
|
||||
* Converts the file access mode and file type enums to a file access mode wide string.
|
||||
*
|
||||
* @param mode File access mode
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A pointer to a wide string representing the file access mode.
|
||||
*/
|
||||
[[nodiscard]] constexpr const wchar_t* AccessModeToWStr(FileAccessMode mode, FileType type) {
|
||||
switch (type) {
|
||||
case FileType::BinaryFile:
|
||||
@@ -84,12 +79,12 @@ namespace {
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the file-share access flag enum to a Windows defined file-share access flag.
|
||||
*
|
||||
* @param flag File-share access flag
|
||||
*
|
||||
* @returns Windows defined file-share access flag.
|
||||
*/
|
||||
* Converts the file-share access flag enum to a Windows defined file-share access flag.
|
||||
*
|
||||
* @param flag File-share access flag
|
||||
*
|
||||
* @returns Windows defined file-share access flag.
|
||||
*/
|
||||
[[nodiscard]] constexpr int ToWindowsFileShareFlag(FileShareFlag flag) {
|
||||
switch (flag) {
|
||||
case FileShareFlag::ShareNone:
|
||||
@@ -107,13 +102,13 @@ namespace {
|
||||
#else
|
||||
|
||||
/**
|
||||
* Converts the file access mode and file type enums to a file access mode string.
|
||||
*
|
||||
* @param mode File access mode
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A pointer to a string representing the file access mode.
|
||||
*/
|
||||
* Converts the file access mode and file type enums to a file access mode string.
|
||||
*
|
||||
* @param mode File access mode
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A pointer to a string representing the file access mode.
|
||||
*/
|
||||
[[nodiscard]] constexpr const char* AccessModeToStr(FileAccessMode mode, FileType type) {
|
||||
switch (type) {
|
||||
case FileType::BinaryFile:
|
||||
@@ -152,12 +147,12 @@ namespace {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Converts the seek origin enum to a seek origin integer.
|
||||
*
|
||||
* @param origin Seek origin
|
||||
*
|
||||
* @returns Seek origin integer.
|
||||
*/
|
||||
* Converts the seek origin enum to a seek origin integer.
|
||||
*
|
||||
* @param origin Seek origin
|
||||
*
|
||||
* @returns Seek origin integer.
|
||||
*/
|
||||
[[nodiscard]] constexpr int ToSeekOrigin(SeekOrigin origin) {
|
||||
switch (origin) {
|
||||
case SeekOrigin::SetOrigin:
|
||||
@@ -183,7 +178,7 @@ std::string ReadStringFromFile(const std::filesystem::path& path, FileType type)
|
||||
}
|
||||
|
||||
size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
|
||||
std::string_view string) {
|
||||
std::string_view string) {
|
||||
if (Exists(path) && !IsFile(path)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -194,7 +189,7 @@ size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
|
||||
}
|
||||
|
||||
size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
|
||||
std::string_view string) {
|
||||
std::string_view string) {
|
||||
if (Exists(path) && !IsFile(path)) {
|
||||
return 0;
|
||||
}
|
||||
@@ -249,103 +244,15 @@ FileType IOFile::GetType() const {
|
||||
return file_type;
|
||||
}
|
||||
|
||||
#ifdef __unix__
|
||||
int PlatformMapReadOnly(IOFile& io, const char* path) {
|
||||
io.mmap_fd = open(path., O_RDONLY);
|
||||
if (io.mmap_fd > 0) {
|
||||
struct stat st;
|
||||
fstat(io.mmap_fd, &st);
|
||||
io.mmap_size = st.st_size;
|
||||
|
||||
int map_flags = MAP_PRIVATE;
|
||||
#ifdef MAP_PREFAULT_READ
|
||||
// Prefaults reads so the final resulting pagetable from this big stupid mmap()
|
||||
// isn't comically lazily loaded, we just coalesce everything in-place for our
|
||||
// lovely mmap flags; if we didn't prefault the reads the page table will be
|
||||
// constructed in-place (i.e on a read-by-read basis) causing lovely soft-faults
|
||||
// which would nuke any performance gains.
|
||||
//
|
||||
// This of course incurs a cost in the initial mmap(2) call, but that is fine.
|
||||
map_flags |= MAP_PREFAULT_READ;
|
||||
#endif
|
||||
#ifdef MAP_NOSYNC
|
||||
// This causes physical media to not be synched to our file/memory
|
||||
// This means that if the read-only file is written to, we won't see changes
|
||||
// or we may see changes which are just funnily scattered, in any case
|
||||
// this presumes the files won't be changed during execution
|
||||
//
|
||||
// Do not ever use this on write files (if we ever support that); this will create
|
||||
// a fun amount of fragmentation on the disk.
|
||||
map_flags |= MAP_NOSYNC;
|
||||
#endif
|
||||
#ifdef MAP_ALIGNED_SUPER
|
||||
// File must be big enough that it's worth to super align. We can't just super-align every
|
||||
// file otherwise we will run out of alignments for actually important files :)
|
||||
// System doesn't guarantee a super alignment, but if it's available it will delete
|
||||
// about 3 layers(?) of the TLB tree for each read/write.
|
||||
// Again the cost of faults may make this negligible gains, but hey, we gotta work
|
||||
// what we gotta work with.
|
||||
using namespace Common::Literals;
|
||||
u64 big_file_threshold = 512_MiB;
|
||||
map_flags |= u64(st.st_size) >= big_file_threshold ? MAP_ALIGNED_SUPER : 0;
|
||||
#endif
|
||||
io.mmap_base = (u8*)mmap(nullptr, io.mmap_size, PROT_READ, map_flags, io.mmap_fd, 0);
|
||||
if (io.mmap_base == MAP_FAILED) {
|
||||
close(io.mmap_fd);
|
||||
io.mmap_fd = -1;
|
||||
} else {
|
||||
posix_madvise(mmap_base, io.mmap_size, POSIX_MADV_WILLNEED);
|
||||
}
|
||||
}
|
||||
return io.mmap_fd;
|
||||
}
|
||||
void PlatformUnmap(IOFile& io) {
|
||||
if (io.mmap_fd != -1) {
|
||||
munmap(io.mmap_base, io.mmap_size);
|
||||
close(io.mmap_fd);
|
||||
io.mmap_fd = -1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
int PlatformMapReadOnly(IOFile& io, const char* path) {
|
||||
io.file_handle = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, nullptr);
|
||||
if (HANDLE(io.file_handle) != INVALID_HANDLE_VALUE) {
|
||||
io.mapping_handle = CreateFileMappingW(file_handle, nullptr, PAGE_READONLY, 0, 0, nullptr);
|
||||
if (io.mapping_handle) {
|
||||
io.mmap_base = (char const*)MapViewOfFile(HANDLE(io.mapping_handle), FILE_MAP_READ, 0, 0, 0);
|
||||
if (io.mmap_base) {
|
||||
_LARGE_INTEGER pvalue;
|
||||
GetFileSizeEx(file_handle, &pvalue);
|
||||
io.mmap_size = uint32_t(pvalue.QuadPart);
|
||||
} else {
|
||||
CloseHandle(io.mapping_handle);
|
||||
CloseHandle(io.file_handle);
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
CloseHandle(io.file_handle);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void PlatformUnmap(IOFile& io) {
|
||||
if(io.mapping_handle) {
|
||||
if(io.mmap_base)
|
||||
UnmapViewOfFile(HANDLE(io.mmap_base));
|
||||
CloseHandle(HANDLE(io.mapping_handle));
|
||||
}
|
||||
if(io.file_handle != INVALID_HANDLE_VALUE)
|
||||
CloseHandle(HANDLE(io.file_handle));
|
||||
}
|
||||
#endif
|
||||
|
||||
void IOFile::Open(const fs::path& path, FileAccessMode mode, FileType type, FileShareFlag flag) {
|
||||
Close();
|
||||
|
||||
file_path = path;
|
||||
file_access_mode = mode;
|
||||
file_type = type;
|
||||
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
if (flag != FileShareFlag::ShareNone) {
|
||||
file = _wfsopen(path.c_str(), AccessModeToWStr(mode, type), ToWindowsFileShareFlag(flag));
|
||||
@@ -355,58 +262,51 @@ void IOFile::Open(const fs::path& path, FileAccessMode mode, FileType type, File
|
||||
#elif ANDROID
|
||||
if (Android::IsContentUri(path)) {
|
||||
ASSERT_MSG(mode == FileAccessMode::Read, "Content URI file access is for read-only!");
|
||||
if (PlatformMapReadOnly(*this, path.c_str()) == -1) {
|
||||
LOG_ERROR(Common_Filesystem, "Error mmap'ing file: {}", path.c_str());
|
||||
int const fd = Android::OpenContentUri(path, Android::OpenMode::Read);
|
||||
if (fd != -1) {
|
||||
file = fdopen(fd, "r");
|
||||
if (errno != 0 && file == nullptr)
|
||||
LOG_ERROR(Common_Filesystem, "Error opening file: {}, error: {}", path.c_str(), strerror(errno));
|
||||
} else {
|
||||
LOG_ERROR(Common_Filesystem, "Error opening file: {}", path.c_str());
|
||||
const auto fd = Android::OpenContentUri(path, Android::OpenMode::Read);
|
||||
if (fd != -1) {
|
||||
file = fdopen(fd, "r");
|
||||
const auto error_num = errno;
|
||||
if (error_num != 0 && file == nullptr) {
|
||||
LOG_ERROR(Common_Filesystem, "Error opening file: {}, error: {}", path.c_str(),
|
||||
strerror(error_num));
|
||||
}
|
||||
} else {
|
||||
LOG_ERROR(Common_Filesystem, "Error opening file: {}", path.c_str());
|
||||
}
|
||||
} else {
|
||||
file = std::fopen(path.c_str(), AccessModeToStr(mode, type));
|
||||
}
|
||||
#elif defined(__HAIKU__) || defined(__managarm__) || defined(__OPENORBIS__) || defined(__APPLE__)
|
||||
file = std::fopen(path.c_str(), AccessModeToStr(mode, type));
|
||||
#elif defined(__unix__)
|
||||
if (type == FileType::BinaryFile && mode == FileAccessMode::Read) {
|
||||
if (PlatformMapReadOnly(*this, path.c_str()) == -1) {
|
||||
LOG_ERROR(Common_Filesystem, "Error mmap'ing file: {}", path.c_str());
|
||||
}
|
||||
}
|
||||
if (mmap_fd == -1) {
|
||||
file = std::fopen(path.c_str(), AccessModeToStr(mode, type)); // mmap(2) failed or simply we can't use it
|
||||
}
|
||||
#else
|
||||
// Some other fancy OS (ahem fucking Darwin/Mac OSX)
|
||||
file = std::fopen(path.c_str(), AccessModeToStr(mode, type));
|
||||
#endif
|
||||
|
||||
if (!IsOpen()) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to open the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void IOFile::Close() {
|
||||
PlatformUnmap(*this);
|
||||
if (file) {
|
||||
errno = 0;
|
||||
const auto close_result = std::fclose(file) == 0;
|
||||
if (!close_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to close the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
file = nullptr;
|
||||
if (!IsOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
|
||||
const auto close_result = std::fclose(file) == 0;
|
||||
|
||||
if (!close_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to close the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
|
||||
file = nullptr;
|
||||
}
|
||||
|
||||
bool IOFile::IsOpen() const {
|
||||
return file != nullptr || IsMappedFile();
|
||||
return file != nullptr;
|
||||
}
|
||||
|
||||
std::string IOFile::ReadString(size_t length) const {
|
||||
@@ -423,132 +323,137 @@ size_t IOFile::WriteString(std::span<const char> string) const {
|
||||
}
|
||||
|
||||
bool IOFile::Flush() const {
|
||||
ASSERT(!IsMappedFile());
|
||||
if (file) {
|
||||
errno = 0;
|
||||
auto const flush_result = std::fflush(file) == 0;
|
||||
if (!flush_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to flush the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
return flush_result;
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
const auto flush_result = std::fflush(file) == 0;
|
||||
#else
|
||||
const auto flush_result = std::fflush(file) == 0;
|
||||
#endif
|
||||
|
||||
if (!flush_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to flush the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
|
||||
return flush_result;
|
||||
}
|
||||
|
||||
bool IOFile::Commit() const {
|
||||
ASSERT(!IsMappedFile());
|
||||
if (file) {
|
||||
errno = 0;
|
||||
#ifdef _WIN32
|
||||
const auto commit_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0;
|
||||
#else
|
||||
const auto commit_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0;
|
||||
#endif
|
||||
if (!commit_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to commit the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
return commit_result;
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
const auto commit_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0;
|
||||
#else
|
||||
const auto commit_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0;
|
||||
#endif
|
||||
|
||||
if (!commit_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to commit the file at path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
}
|
||||
|
||||
return commit_result;
|
||||
}
|
||||
|
||||
bool IOFile::SetSize(u64 size) const {
|
||||
ASSERT(!IsMappedFile());
|
||||
if (file) {
|
||||
errno = 0;
|
||||
#ifdef _WIN32
|
||||
const auto set_size_result = _chsize_s(fileno(file), s64(size)) == 0;
|
||||
#else
|
||||
const auto set_size_result = ftruncate(fileno(file), s64(size)) == 0;
|
||||
#endif
|
||||
if (!set_size_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to resize the file at path={}, size={}, ec_message={}",
|
||||
PathToUTF8String(file_path), size, ec.message());
|
||||
}
|
||||
return set_size_result;
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
errno = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
const auto set_size_result = _chsize_s(fileno(file), static_cast<s64>(size)) == 0;
|
||||
#else
|
||||
const auto set_size_result = ftruncate(fileno(file), static_cast<s64>(size)) == 0;
|
||||
#endif
|
||||
|
||||
if (!set_size_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to resize the file at path={}, size={}, ec_message={}",
|
||||
PathToUTF8String(file_path), size, ec.message());
|
||||
}
|
||||
|
||||
return set_size_result;
|
||||
}
|
||||
|
||||
u64 IOFile::GetSize() const {
|
||||
if (IsMappedFile())
|
||||
return mmap_size;
|
||||
if (file) {
|
||||
// Flush any unwritten buffered data into the file prior to retrieving the file mmap_size.
|
||||
std::fflush(file);
|
||||
if (!IsOpen()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Flush any unwritten buffered data into the file prior to retrieving the file size.
|
||||
std::fflush(file);
|
||||
|
||||
#if ANDROID
|
||||
u64 file_size = 0;
|
||||
if (Android::IsContentUri(file_path)) {
|
||||
file_size = Android::GetSize(file_path);
|
||||
} else {
|
||||
std::error_code ec;
|
||||
|
||||
file_size = fs::file_size(file_path, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERROR(Common_Filesystem, "Failed to retrieve the file mmap_size of path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#else
|
||||
u64 file_size = 0;
|
||||
if (Android::IsContentUri(file_path)) {
|
||||
file_size = Android::GetSize(file_path);
|
||||
} else {
|
||||
std::error_code ec;
|
||||
auto const file_size = fs::file_size(file_path, ec);
|
||||
|
||||
file_size = fs::file_size(file_path, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERROR(Common_Filesystem, "Failed to retrieve the file mmap_size of path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
LOG_ERROR(Common_Filesystem,
|
||||
"Failed to retrieve the file size of path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return file_size;
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
std::error_code ec;
|
||||
|
||||
const auto file_size = fs::file_size(file_path, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERROR(Common_Filesystem, "Failed to retrieve the file size of path={}, ec_message={}",
|
||||
PathToUTF8String(file_path), ec.message());
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
return file_size;
|
||||
}
|
||||
|
||||
bool IOFile::Seek(s64 offset, SeekOrigin origin) const {
|
||||
if (IsMappedFile()) {
|
||||
// fuck you to whoever made this method const
|
||||
switch (origin) {
|
||||
case SeekOrigin::SetOrigin:
|
||||
mmap_offset = off_t(offset);
|
||||
break;
|
||||
case SeekOrigin::CurrentPosition:
|
||||
mmap_offset += off_t(offset);
|
||||
break;
|
||||
case SeekOrigin::End:
|
||||
mmap_offset = off_t(mmap_size) + off_t(offset);
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
if (file) {
|
||||
errno = 0;
|
||||
const auto seek_result = fseeko(file, offset, ToSeekOrigin(origin)) == 0;
|
||||
if (!seek_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem, "Failed to seek the file at path={}, offset={}, origin={}, ec_message={}",
|
||||
PathToUTF8String(file_path), offset, origin, ec.message());
|
||||
}
|
||||
return seek_result;
|
||||
|
||||
errno = 0;
|
||||
|
||||
const auto seek_result = fseeko(file, offset, ToSeekOrigin(origin)) == 0;
|
||||
|
||||
if (!seek_result) {
|
||||
const auto ec = std::error_code{errno, std::generic_category()};
|
||||
LOG_ERROR(Common_Filesystem,
|
||||
"Failed to seek the file at path={}, offset={}, origin={}, ec_message={}",
|
||||
PathToUTF8String(file_path), offset, origin, ec.message());
|
||||
}
|
||||
return false;
|
||||
|
||||
return seek_result;
|
||||
}
|
||||
|
||||
s64 IOFile::Tell() const {
|
||||
if (IsMappedFile()) {
|
||||
errno = 0;
|
||||
return s64(mmap_offset);
|
||||
if (!IsOpen()) {
|
||||
return 0;
|
||||
}
|
||||
if (file) {
|
||||
errno = 0;
|
||||
return ftello(file);
|
||||
}
|
||||
return 0;
|
||||
|
||||
errno = 0;
|
||||
|
||||
return ftello(file);
|
||||
}
|
||||
|
||||
} // namespace Common::FS
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <span>
|
||||
#include <type_traits>
|
||||
@@ -25,12 +21,12 @@ enum class SeekOrigin {
|
||||
};
|
||||
|
||||
/**
|
||||
* Opens a file stream at path with the specified open mode.
|
||||
*
|
||||
* @param file_stream Reference to file stream
|
||||
* @param path Filesystem path
|
||||
* @param open_mode File stream open mode
|
||||
*/
|
||||
* Opens a file stream at path with the specified open mode.
|
||||
*
|
||||
* @param file_stream Reference to file stream
|
||||
* @param path Filesystem path
|
||||
* @param open_mode File stream open mode
|
||||
*/
|
||||
template <typename FileStream>
|
||||
void OpenFileStream(FileStream& file_stream, const std::filesystem::path& path,
|
||||
std::ios_base::openmode open_mode) {
|
||||
@@ -49,14 +45,14 @@ void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::op
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Reads an entire file at path and returns a string of the contents read from the file.
|
||||
* If the filesystem object at path is not a regular file, this function returns an empty string.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A string of the contents read from the file.
|
||||
*/
|
||||
* Reads an entire file at path and returns a string of the contents read from the file.
|
||||
* If the filesystem object at path is not a regular file, this function returns an empty string.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns A string of the contents read from the file.
|
||||
*/
|
||||
[[nodiscard]] std::string ReadStringFromFile(const std::filesystem::path& path, FileType type);
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -71,18 +67,18 @@ template <typename Path>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Writes a string to a file at path and returns the number of characters successfully written.
|
||||
* If a file already exists at path, its contents will be erased.
|
||||
* If a file does not exist at path, it creates and opens a new empty file for writing.
|
||||
* If the filesystem object at path exists and is not a regular file, this function returns 0.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
* Writes a string to a file at path and returns the number of characters successfully written.
|
||||
* If a file already exists at path, its contents will be erased.
|
||||
* If a file does not exist at path, it creates and opens a new empty file for writing.
|
||||
* If the filesystem object at path exists and is not a regular file, this function returns 0.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
[[nodiscard]] size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
|
||||
std::string_view string);
|
||||
std::string_view string);
|
||||
|
||||
#ifdef _WIN32
|
||||
template <typename Path>
|
||||
@@ -96,15 +92,15 @@ template <typename Path>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Appends a string to a file at path and returns the number of characters successfully written.
|
||||
* If a file does not exist at path, it creates and opens a new empty file for appending.
|
||||
* If the filesystem object at path exists and is not a regular file, this function returns 0.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
* Appends a string to a file at path and returns the number of characters successfully written.
|
||||
* If a file does not exist at path, it creates and opens a new empty file for appending.
|
||||
* If the filesystem object at path exists and is not a regular file, this function returns 0.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param type File type
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
[[nodiscard]] size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
|
||||
std::string_view string);
|
||||
|
||||
@@ -132,14 +128,14 @@ public:
|
||||
FileShareFlag flag = FileShareFlag::ShareReadOnly);
|
||||
|
||||
/**
|
||||
* An IOFile is a lightweight wrapper on C Library file operations.
|
||||
* Automatically closes an open file on the destruction of an IOFile object.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param mode File access mode
|
||||
* @param type File type, default is BinaryFile. Use TextFile to open the file as a text file
|
||||
* @param flag (Windows only) File-share access flag, default is ShareReadOnly
|
||||
*/
|
||||
* An IOFile is a lightweight wrapper on C Library file operations.
|
||||
* Automatically closes an open file on the destruction of an IOFile object.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param mode File access mode
|
||||
* @param type File type, default is BinaryFile. Use TextFile to open the file as a text file
|
||||
* @param flag (Windows only) File-share access flag, default is ShareReadOnly
|
||||
*/
|
||||
explicit IOFile(const std::filesystem::path& path, FileAccessMode mode,
|
||||
FileType type = FileType::BinaryFile,
|
||||
FileShareFlag flag = FileShareFlag::ShareReadOnly);
|
||||
@@ -153,70 +149,84 @@ public:
|
||||
IOFile& operator=(IOFile&& other) noexcept;
|
||||
|
||||
/**
|
||||
* Gets the path of the file.
|
||||
*
|
||||
* @returns The path of the file.
|
||||
*/
|
||||
* Gets the path of the file.
|
||||
*
|
||||
* @returns The path of the file.
|
||||
*/
|
||||
[[nodiscard]] std::filesystem::path GetPath() const;
|
||||
|
||||
/**
|
||||
* Gets the access mode of the file.
|
||||
*
|
||||
* @returns The access mode of the file.
|
||||
*/
|
||||
* Gets the access mode of the file.
|
||||
*
|
||||
* @returns The access mode of the file.
|
||||
*/
|
||||
[[nodiscard]] FileAccessMode GetAccessMode() const;
|
||||
|
||||
/**
|
||||
* Gets the type of the file.
|
||||
*
|
||||
* @returns The type of the file.
|
||||
*/
|
||||
* Gets the type of the file.
|
||||
*
|
||||
* @returns The type of the file.
|
||||
*/
|
||||
[[nodiscard]] FileType GetType() const;
|
||||
|
||||
/**
|
||||
* Opens a file at path with the specified file access mode.
|
||||
* This function behaves differently depending on the FileAccessMode.
|
||||
* These behaviors are documented in each enum value of FileAccessMode.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param mode File access mode
|
||||
* @param type File type, default is BinaryFile. Use TextFile to open the file as a text file
|
||||
* @param flag (Windows only) File-share access flag, default is ShareReadOnly
|
||||
*/
|
||||
* Opens a file at path with the specified file access mode.
|
||||
* This function behaves differently depending on the FileAccessMode.
|
||||
* These behaviors are documented in each enum value of FileAccessMode.
|
||||
*
|
||||
* @param path Filesystem path
|
||||
* @param mode File access mode
|
||||
* @param type File type, default is BinaryFile. Use TextFile to open the file as a text file
|
||||
* @param flag (Windows only) File-share access flag, default is ShareReadOnly
|
||||
*/
|
||||
void Open(const std::filesystem::path& path, FileAccessMode mode,
|
||||
FileType type = FileType::BinaryFile,
|
||||
FileShareFlag flag = FileShareFlag::ShareReadOnly);
|
||||
FileType type = FileType::BinaryFile,
|
||||
FileShareFlag flag = FileShareFlag::ShareReadOnly);
|
||||
|
||||
// #ifdef _WIN32
|
||||
// template <typename Path>
|
||||
// void Open(const Path& path, FileAccessMode mode, FileType type = FileType::BinaryFile,
|
||||
// FileShareFlag flag = FileShareFlag::ShareReadOnly) {
|
||||
// using ValueType = typename Path::value_type;
|
||||
// if constexpr (IsChar<ValueType>) {
|
||||
// Open(ToU8String(path), mode, type, flag);
|
||||
// } else {
|
||||
// Open(std::filesystem::path{path}, mode, type, flag);
|
||||
// }
|
||||
// }
|
||||
// #endif
|
||||
|
||||
/// Closes the file if it is opened.
|
||||
void Close();
|
||||
|
||||
/**
|
||||
* Checks whether the file is open.
|
||||
* Use this to check whether the calls to Open() or Close() succeeded.
|
||||
*
|
||||
* @returns True if the file is open, false otherwise.
|
||||
*/
|
||||
* Checks whether the file is open.
|
||||
* Use this to check whether the calls to Open() or Close() succeeded.
|
||||
*
|
||||
* @returns True if the file is open, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool IsOpen() const;
|
||||
|
||||
/**
|
||||
* Helper function which deduces the value type of a contiguous STL container used in ReadSpan.
|
||||
* If T is not a contiguous container as defined by the concept IsContiguousContainer, this
|
||||
* calls ReadObject and T must be a trivially copyable object.
|
||||
*
|
||||
* See ReadSpan for more details if T is a contiguous container.
|
||||
* See ReadObject for more details if T is a trivially copyable object.
|
||||
*
|
||||
* @tparam T Contiguous container or trivially copyable object
|
||||
*
|
||||
* @param data Container of T::value_type data or reference to object
|
||||
*
|
||||
* @returns Count of T::value_type data or objects successfully read.
|
||||
*/
|
||||
* Helper function which deduces the value type of a contiguous STL container used in ReadSpan.
|
||||
* If T is not a contiguous container as defined by the concept IsContiguousContainer, this
|
||||
* calls ReadObject and T must be a trivially copyable object.
|
||||
*
|
||||
* See ReadSpan for more details if T is a contiguous container.
|
||||
* See ReadObject for more details if T is a trivially copyable object.
|
||||
*
|
||||
* @tparam T Contiguous container or trivially copyable object
|
||||
*
|
||||
* @param data Container of T::value_type data or reference to object
|
||||
*
|
||||
* @returns Count of T::value_type data or objects successfully read.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] size_t Read(T& data) const {
|
||||
if constexpr (IsContiguousContainer<T>) {
|
||||
using ContiguousType = typename T::value_type;
|
||||
static_assert(std::is_trivially_copyable_v<ContiguousType>, "Data type must be trivially copyable.");
|
||||
static_assert(std::is_trivially_copyable_v<ContiguousType>,
|
||||
"Data type must be trivially copyable.");
|
||||
return ReadSpan<ContiguousType>(data);
|
||||
} else {
|
||||
return ReadObject(data) ? 1 : 0;
|
||||
@@ -224,24 +234,25 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function which deduces the value type of a contiguous STL container used in WriteSpan.
|
||||
* If T is not a contiguous STL container as defined by the concept IsContiguousContainer, this
|
||||
* calls WriteObject and T must be a trivially copyable object.
|
||||
*
|
||||
* See WriteSpan for more details if T is a contiguous container.
|
||||
* See WriteObject for more details if T is a trivially copyable object.
|
||||
*
|
||||
* @tparam T Contiguous container or trivially copyable object
|
||||
*
|
||||
* @param data Container of T::value_type data or const reference to object
|
||||
*
|
||||
* @returns Count of T::value_type data or objects successfully written.
|
||||
*/
|
||||
* Helper function which deduces the value type of a contiguous STL container used in WriteSpan.
|
||||
* If T is not a contiguous STL container as defined by the concept IsContiguousContainer, this
|
||||
* calls WriteObject and T must be a trivially copyable object.
|
||||
*
|
||||
* See WriteSpan for more details if T is a contiguous container.
|
||||
* See WriteObject for more details if T is a trivially copyable object.
|
||||
*
|
||||
* @tparam T Contiguous container or trivially copyable object
|
||||
*
|
||||
* @param data Container of T::value_type data or const reference to object
|
||||
*
|
||||
* @returns Count of T::value_type data or objects successfully written.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] size_t Write(const T& data) const {
|
||||
if constexpr (IsContiguousContainer<T>) {
|
||||
using ContiguousType = typename T::value_type;
|
||||
static_assert(std::is_trivially_copyable_v<ContiguousType>, "Data type must be trivially copyable.");
|
||||
static_assert(std::is_trivially_copyable_v<ContiguousType>,
|
||||
"Data type must be trivially copyable.");
|
||||
return WriteSpan<ContiguousType>(data);
|
||||
} else {
|
||||
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
|
||||
@@ -250,209 +261,199 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a span of T data from a file sequentially.
|
||||
* This function reads from the current position of the file pointer and
|
||||
* advances it by the (count of T * sizeof(T)) bytes successfully read.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks read permissions
|
||||
* - Attempting to read beyond the end-of-file
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param data Span of T data
|
||||
*
|
||||
* @returns Count of T data successfully read.
|
||||
*/
|
||||
* Reads a span of T data from a file sequentially.
|
||||
* This function reads from the current position of the file pointer and
|
||||
* advances it by the (count of T * sizeof(T)) bytes successfully read.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks read permissions
|
||||
* - Attempting to read beyond the end-of-file
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param data Span of T data
|
||||
*
|
||||
* @returns Count of T data successfully read.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] size_t ReadSpan(std::span<T> data) const {
|
||||
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
|
||||
if (IsMappedFile()) {
|
||||
std::memcpy(data.data(), mmap_base + mmap_offset, sizeof(T) * data.size());
|
||||
return data.size();
|
||||
|
||||
if (!IsOpen()) {
|
||||
return 0;
|
||||
}
|
||||
return IsOpen() ? std::fread(data.data(), sizeof(T), data.size(), file) : 0;
|
||||
|
||||
return std::fread(data.data(), sizeof(T), data.size(), file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a span of T data to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the (count of T * sizeof(T)) bytes successfully written.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks write permissions
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param data Span of T data
|
||||
*
|
||||
* @returns Count of T data successfully written.
|
||||
*/
|
||||
* Writes a span of T data to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the (count of T * sizeof(T)) bytes successfully written.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks write permissions
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param data Span of T data
|
||||
*
|
||||
* @returns Count of T data successfully written.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] size_t WriteSpan(std::span<const T> data) const {
|
||||
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
|
||||
if (IsMappedFile()) {
|
||||
std::memcpy(mmap_base + mmap_offset, data.data(), sizeof(T) * data.size());
|
||||
return data.size();
|
||||
|
||||
if (!IsOpen()) {
|
||||
return 0;
|
||||
}
|
||||
return IsOpen() ? std::fwrite(data.data(), sizeof(T), data.size(), file) : 0;
|
||||
|
||||
return std::fwrite(data.data(), sizeof(T), data.size(), file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a T object from a file sequentially.
|
||||
* This function reads from the current position of the file pointer and
|
||||
* advances it by the sizeof(T) bytes successfully read.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks read permissions
|
||||
* - Attempting to read beyond the end-of-file
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param object Reference to object
|
||||
*
|
||||
* @returns True if the object is successfully read from the file, false otherwise.
|
||||
*/
|
||||
* Reads a T object from a file sequentially.
|
||||
* This function reads from the current position of the file pointer and
|
||||
* advances it by the sizeof(T) bytes successfully read.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks read permissions
|
||||
* - Attempting to read beyond the end-of-file
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param object Reference to object
|
||||
*
|
||||
* @returns True if the object is successfully read from the file, false otherwise.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] bool ReadObject(T& object) const {
|
||||
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
|
||||
static_assert(!std::is_pointer_v<T>, "T must not be a pointer to an object.");
|
||||
if (IsMappedFile()) {
|
||||
std::memcpy(&object, mmap_base + mmap_offset, sizeof(T));
|
||||
return sizeof(T);
|
||||
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
return IsOpen() ? std::fread(&object, sizeof(T), 1, file) == 1 : false;
|
||||
|
||||
return std::fread(&object, sizeof(T), 1, file) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a T object to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the sizeof(T) bytes successfully written.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks write permissions
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param object Const reference to object
|
||||
*
|
||||
* @returns True if the object is successfully written to the file, false otherwise.
|
||||
*/
|
||||
* Writes a T object to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the sizeof(T) bytes successfully written.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
* - The opened file lacks write permissions
|
||||
*
|
||||
* @tparam T Data type
|
||||
*
|
||||
* @param object Const reference to object
|
||||
*
|
||||
* @returns True if the object is successfully written to the file, false otherwise.
|
||||
*/
|
||||
template <typename T>
|
||||
[[nodiscard]] bool WriteObject(const T& object) const {
|
||||
static_assert(std::is_trivially_copyable_v<T>, "Data type must be trivially copyable.");
|
||||
static_assert(!std::is_pointer_v<T>, "T must not be a pointer to an object.");
|
||||
if (IsMappedFile()) {
|
||||
std::memcpy(mmap_base + mmap_offset, &object, sizeof(T));
|
||||
return sizeof(T);
|
||||
|
||||
if (!IsOpen()) {
|
||||
return false;
|
||||
}
|
||||
return IsOpen() ? std::fwrite(&object, sizeof(T), 1, file) == 1 : false;
|
||||
|
||||
return std::fwrite(&object, sizeof(T), 1, file) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialized function to read a string of a given length from a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the number of characters successfully read.
|
||||
* The size of the returned string may not match length if not all bytes are successfully read.
|
||||
*
|
||||
* @param length Length of the string
|
||||
*
|
||||
* @returns A string read from the file.
|
||||
*/
|
||||
* Specialized function to read a string of a given length from a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the number of characters successfully read.
|
||||
* The size of the returned string may not match length if not all bytes are successfully read.
|
||||
*
|
||||
* @param length Length of the string
|
||||
*
|
||||
* @returns A string read from the file.
|
||||
*/
|
||||
[[nodiscard]] std::string ReadString(size_t length) const;
|
||||
|
||||
/**
|
||||
* Specialized function to write a string to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the number of characters successfully written.
|
||||
*
|
||||
* @param string Span of const char backed std::string or std::string_view
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
* Specialized function to write a string to a file sequentially.
|
||||
* This function writes from the current position of the file pointer and
|
||||
* advances it by the number of characters successfully written.
|
||||
*
|
||||
* @param string Span of const char backed std::string or std::string_view
|
||||
*
|
||||
* @returns Number of characters successfully written.
|
||||
*/
|
||||
[[nodiscard]] size_t WriteString(std::span<const char> string) const;
|
||||
|
||||
/**
|
||||
* Attempts to flush any unwritten buffered data into the file.
|
||||
*
|
||||
* @returns True if the flush was successful, false otherwise.
|
||||
*/
|
||||
* Attempts to flush any unwritten buffered data into the file.
|
||||
*
|
||||
* @returns True if the flush was successful, false otherwise.
|
||||
*/
|
||||
bool Flush() const;
|
||||
|
||||
/**
|
||||
* Attempts to commit the file into the disk.
|
||||
* Note that this is an expensive operation as this forces the operating system to write
|
||||
* the contents of the file associated with the file descriptor into the disk.
|
||||
*
|
||||
* @returns True if the commit was successful, false otherwise.
|
||||
*/
|
||||
* Attempts to commit the file into the disk.
|
||||
* Note that this is an expensive operation as this forces the operating system to write
|
||||
* the contents of the file associated with the file descriptor into the disk.
|
||||
*
|
||||
* @returns True if the commit was successful, false otherwise.
|
||||
*/
|
||||
bool Commit() const;
|
||||
|
||||
/**
|
||||
* Resizes the file to a given size.
|
||||
* If the file is resized to a smaller size, the remainder of the file is discarded.
|
||||
* If the file is resized to a larger size, the new area appears as if zero-filled.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
*
|
||||
* @param size File size in bytes
|
||||
*
|
||||
* @returns True if the file resize succeeded, false otherwise.
|
||||
*/
|
||||
* Resizes the file to a given size.
|
||||
* If the file is resized to a smaller size, the remainder of the file is discarded.
|
||||
* If the file is resized to a larger size, the new area appears as if zero-filled.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
*
|
||||
* @param size File size in bytes
|
||||
*
|
||||
* @returns True if the file resize succeeded, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool SetSize(u64 size) const;
|
||||
|
||||
/**
|
||||
* Gets the size of the file.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
*
|
||||
* @returns The file size in bytes of the file. Returns 0 on failure.
|
||||
*/
|
||||
* Gets the size of the file.
|
||||
*
|
||||
* Failures occur when:
|
||||
* - The file is not open
|
||||
*
|
||||
* @returns The file size in bytes of the file. Returns 0 on failure.
|
||||
*/
|
||||
[[nodiscard]] u64 GetSize() const;
|
||||
|
||||
/**
|
||||
* Moves the current position of the file pointer with the specified offset and seek origin.
|
||||
*
|
||||
* @param offset Offset from seek origin
|
||||
* @param origin Seek origin
|
||||
*
|
||||
* @returns True if the file pointer has moved to the specified offset, false otherwise.
|
||||
*/
|
||||
* Moves the current position of the file pointer with the specified offset and seek origin.
|
||||
*
|
||||
* @param offset Offset from seek origin
|
||||
* @param origin Seek origin
|
||||
*
|
||||
* @returns True if the file pointer has moved to the specified offset, false otherwise.
|
||||
*/
|
||||
[[nodiscard]] bool Seek(s64 offset, SeekOrigin origin = SeekOrigin::SetOrigin) const;
|
||||
|
||||
/**
|
||||
* Gets the current position of the file pointer.
|
||||
*
|
||||
* @returns The current position of the file pointer.
|
||||
*/
|
||||
* Gets the current position of the file pointer.
|
||||
*
|
||||
* @returns The current position of the file pointer.
|
||||
*/
|
||||
[[nodiscard]] s64 Tell() const;
|
||||
|
||||
private:
|
||||
std::filesystem::path file_path;
|
||||
FileAccessMode file_access_mode{};
|
||||
FileType file_type{};
|
||||
std::FILE* file = nullptr;
|
||||
|
||||
// Any decent system should have mmap() for files
|
||||
// Systems with artifical mmap() limitations should simply change the logic within file.cpp
|
||||
// and reduce the threshold for which the mmap() is set to
|
||||
#ifdef _WIN32
|
||||
void *mapping_handle = nullptr;
|
||||
void *file_handle = nullptr;
|
||||
bool IsMappedFile() { return file_handle != nullptr; }
|
||||
#else // POSIX
|
||||
int mmap_fd = -1;
|
||||
bool IsMappedFile() { return mmap_fd != -1; }
|
||||
#endif
|
||||
u8* mmap_base = nullptr;
|
||||
size_t mmap_size = 0;
|
||||
mutable off_t mmap_offset = 0; // fuck you
|
||||
std::FILE* file = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Common::FS
|
||||
|
||||
@@ -20,116 +20,122 @@ struct FormatTuple {
|
||||
GLenum format = GL_NONE;
|
||||
GLenum type = GL_NONE;
|
||||
};
|
||||
|
||||
#define SURFACE_FORMAT_LIST \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, A8B8G8R8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA8_SNORM, GL_RGBA, GL_BYTE, A8B8G8R8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE, A8B8G8R8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, A8B8G8R8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, R5G6B5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, B5G6R5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, A1R5G5B5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, A2B10G10R10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, A2B10G10R10_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB10_A2, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV, A2R10G10B10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, A1B5G5R5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, A5B5G5R1_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R8, GL_RED, GL_UNSIGNED_BYTE, R8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R8_SNORM, GL_RED, GL_BYTE, R8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R8I, GL_RED_INTEGER, GL_BYTE, R8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, R8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, R16G16B16A16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, R16G16B16A16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA16_SNORM, GL_RGBA, GL_SHORT, R16G16B16A16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT, R16G16B16A16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, R16G16B16A16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, B10G11R11_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, R32G32B32A32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_NONE, GL_NONE, BC1_RGBA_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_NONE, GL_NONE, BC2_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_NONE, GL_NONE, BC3_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RED_RGTC1, GL_NONE, GL_NONE, BC4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SIGNED_RED_RGTC1, GL_NONE, GL_NONE, BC4_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RG_RGTC2, GL_NONE, GL_NONE, BC5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SIGNED_RG_RGTC2, GL_NONE, GL_NONE, BC5_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_BPTC_UNORM, GL_NONE, GL_NONE, BC7_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, GL_NONE, GL_NONE, BC6H_UFLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_NONE, GL_NONE, BC6H_SFLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_4x4_KHR, GL_NONE, GL_NONE, ASTC_2D_4X4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, B8G8R8A8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA32F, GL_RGBA, GL_FLOAT, R32G32B32A32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA32I, GL_RGBA_INTEGER, GL_INT, R32G32B32A32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG32F, GL_RG, GL_FLOAT, R32G32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG32I, GL_RG_INTEGER, GL_INT, R32G32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R32F, GL_RED, GL_FLOAT, R32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R16F, GL_RED, GL_HALF_FLOAT, R16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R16, GL_RED, GL_UNSIGNED_SHORT, R16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R16_SNORM, GL_RED, GL_SHORT, R16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT, R16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R16I, GL_RED_INTEGER, GL_SHORT, R16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG16, GL_RG, GL_UNSIGNED_SHORT, R16G16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG16F, GL_RG, GL_HALF_FLOAT, R16G16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT, R16G16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG16I, GL_RG_INTEGER, GL_SHORT, R16G16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG16_SNORM, GL_RG, GL_SHORT, R16G16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB32F, GL_RGB, GL_FLOAT, R32G32B32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, A8B8G8R8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, R8G8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG8_SNORM, GL_RG, GL_BYTE, R8G8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG8I, GL_RG_INTEGER, GL_BYTE, R8G8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE, R8G8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT, R32G32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB16F, GL_RGBA, GL_HALF_FLOAT, R16G16B16X16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, R32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_R32I, GL_RED_INTEGER, GL_INT, R32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_8x8_KHR, GL_NONE, GL_NONE, ASTC_2D_8X8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_8x5_KHR, GL_NONE, GL_NONE, ASTC_2D_8X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_5x4_KHR, GL_NONE, GL_NONE, ASTC_2D_5X4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_SRGB8_ALPHA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, B8G8R8A8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_NONE, GL_NONE, BC1_RGBA_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_NONE, GL_NONE, BC2_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_NONE, GL_NONE, BC3_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM, GL_NONE, GL_NONE, BC7_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, A4B4G4R4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_R8, GL_RED, GL_UNSIGNED_BYTE, G4R4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, GL_NONE, GL_NONE, ASTC_2D_4X4_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, GL_NONE, GL_NONE, ASTC_2D_8X8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, GL_NONE, GL_NONE, ASTC_2D_8X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, GL_NONE, GL_NONE, ASTC_2D_5X4_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_5x5_KHR, GL_NONE, GL_NONE, ASTC_2D_5X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, GL_NONE, GL_NONE, ASTC_2D_5X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_10x8_KHR, GL_NONE, GL_NONE, ASTC_2D_10X8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, GL_NONE, GL_NONE, ASTC_2D_10X8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_6x6_KHR, GL_NONE, GL_NONE, ASTC_2D_6X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, GL_NONE, GL_NONE, ASTC_2D_6X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_10x6_KHR, GL_NONE, GL_NONE, ASTC_2D_10X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, GL_NONE, GL_NONE, ASTC_2D_10X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_10x5_KHR, GL_NONE, GL_NONE, ASTC_2D_10X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, GL_NONE, GL_NONE, ASTC_2D_10X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_10x10_KHR, GL_NONE, GL_NONE, ASTC_2D_10X10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, GL_NONE, GL_NONE, ASTC_2D_10X10_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_12x10_KHR, GL_NONE, GL_NONE, ASTC_2D_12X10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, GL_NONE, GL_NONE, ASTC_2D_12X10_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_12x12_KHR, GL_NONE, GL_NONE, ASTC_2D_12X12_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, GL_NONE, GL_NONE, ASTC_2D_12X12_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_8x6_KHR, GL_NONE, GL_NONE, ASTC_2D_8X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, GL_NONE, GL_NONE, ASTC_2D_8X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_RGBA_ASTC_6x5_KHR, GL_NONE, GL_NONE, ASTC_2D_6X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, GL_NONE, GL_NONE, ASTC_2D_6X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, E5B9G9R9_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, D32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, D16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8, X8_D24_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_STENCIL_INDEX8, GL_STENCIL, GL_UNSIGNED_BYTE, S8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, D24_UNORM_S8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, S8_UINT_D24_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, D32_FLOAT_S8_UINT)
|
||||
constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> FORMAT_TABLE = {{
|
||||
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV}, // A8B8G8R8_UNORM
|
||||
{GL_RGBA8_SNORM, GL_RGBA, GL_BYTE}, // A8B8G8R8_SNORM
|
||||
{GL_RGBA8I, GL_RGBA_INTEGER, GL_BYTE}, // A8B8G8R8_SINT
|
||||
{GL_RGBA8UI, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE}, // A8B8G8R8_UINT
|
||||
{GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5}, // R5G6B5_UNORM
|
||||
{GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV}, // B5G6R5_UNORM
|
||||
{GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV}, // A1R5G5B5_UNORM
|
||||
{GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV}, // A2B10G10R10_UNORM
|
||||
{GL_RGB10_A2UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV}, // A2B10G10R10_UINT
|
||||
{GL_RGB10_A2, GL_BGRA, GL_UNSIGNED_INT_2_10_10_10_REV}, // A2R10G10B10_UNORM
|
||||
{GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV}, // A1B5G5R5_UNORM
|
||||
{GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1}, // A5B5G5R1_UNORM
|
||||
{GL_R8, GL_RED, GL_UNSIGNED_BYTE}, // R8_UNORM
|
||||
{GL_R8_SNORM, GL_RED, GL_BYTE}, // R8_SNORM
|
||||
{GL_R8I, GL_RED_INTEGER, GL_BYTE}, // R8_SINT
|
||||
{GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE}, // R8_UINT
|
||||
{GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT}, // R16G16B16A16_FLOAT
|
||||
{GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT}, // R16G16B16A16_UNORM
|
||||
{GL_RGBA16_SNORM, GL_RGBA, GL_SHORT}, // R16G16B16A16_SNORM
|
||||
{GL_RGBA16I, GL_RGBA_INTEGER, GL_SHORT}, // R16G16B16A16_SINT
|
||||
{GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT}, // R16G16B16A16_UINT
|
||||
{GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV}, // B10G11R11_FLOAT
|
||||
{GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT}, // R32G32B32A32_UINT
|
||||
{GL_COMPRESSED_RGBA_S3TC_DXT1_EXT}, // BC1_RGBA_UNORM
|
||||
{GL_COMPRESSED_RGBA_S3TC_DXT3_EXT}, // BC2_UNORM
|
||||
{GL_COMPRESSED_RGBA_S3TC_DXT5_EXT}, // BC3_UNORM
|
||||
{GL_COMPRESSED_RED_RGTC1}, // BC4_UNORM
|
||||
{GL_COMPRESSED_SIGNED_RED_RGTC1}, // BC4_SNORM
|
||||
{GL_COMPRESSED_RG_RGTC2}, // BC5_UNORM
|
||||
{GL_COMPRESSED_SIGNED_RG_RGTC2}, // BC5_SNORM
|
||||
{GL_COMPRESSED_RGBA_BPTC_UNORM}, // BC7_UNORM
|
||||
{GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT}, // BC6H_UFLOAT
|
||||
{GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT}, // BC6H_SFLOAT
|
||||
{GL_COMPRESSED_RGBA_ASTC_4x4_KHR}, // ASTC_2D_4X4_UNORM
|
||||
{GL_RGBA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV}, // B8G8R8A8_UNORM
|
||||
{GL_RGBA32F, GL_RGBA, GL_FLOAT}, // R32G32B32A32_FLOAT
|
||||
{GL_RGBA32I, GL_RGBA_INTEGER, GL_INT}, // R32G32B32A32_SINT
|
||||
{GL_RG32F, GL_RG, GL_FLOAT}, // R32G32_FLOAT
|
||||
{GL_RG32I, GL_RG_INTEGER, GL_INT}, // R32G32_SINT
|
||||
{GL_R32F, GL_RED, GL_FLOAT}, // R32_FLOAT
|
||||
{GL_R16F, GL_RED, GL_HALF_FLOAT}, // R16_FLOAT
|
||||
{GL_R16, GL_RED, GL_UNSIGNED_SHORT}, // R16_UNORM
|
||||
{GL_R16_SNORM, GL_RED, GL_SHORT}, // R16_SNORM
|
||||
{GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT}, // R16_UINT
|
||||
{GL_R16I, GL_RED_INTEGER, GL_SHORT}, // R16_SINT
|
||||
{GL_RG16, GL_RG, GL_UNSIGNED_SHORT}, // R16G16_UNORM
|
||||
{GL_RG16F, GL_RG, GL_HALF_FLOAT}, // R16G16_FLOAT
|
||||
{GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT}, // R16G16_UINT
|
||||
{GL_RG16I, GL_RG_INTEGER, GL_SHORT}, // R16G16_SINT
|
||||
{GL_RG16_SNORM, GL_RG, GL_SHORT}, // R16G16_SNORM
|
||||
{GL_RGB32F, GL_RGB, GL_FLOAT}, // R32G32B32_FLOAT
|
||||
{GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV}, // A8B8G8R8_SRGB
|
||||
{GL_RG8, GL_RG, GL_UNSIGNED_BYTE}, // R8G8_UNORM
|
||||
{GL_RG8_SNORM, GL_RG, GL_BYTE}, // R8G8_SNORM
|
||||
{GL_RG8I, GL_RG_INTEGER, GL_BYTE}, // R8G8_SINT
|
||||
{GL_RG8UI, GL_RG_INTEGER, GL_UNSIGNED_BYTE}, // R8G8_UINT
|
||||
{GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT}, // R32G32_UINT
|
||||
{GL_RGB16F, GL_RGBA, GL_HALF_FLOAT}, // R16G16B16X16_FLOAT
|
||||
{GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT}, // R32_UINT
|
||||
{GL_R32I, GL_RED_INTEGER, GL_INT}, // R32_SINT
|
||||
{GL_COMPRESSED_RGBA_ASTC_8x8_KHR}, // ASTC_2D_8X8_UNORM
|
||||
{GL_COMPRESSED_RGBA_ASTC_8x5_KHR}, // ASTC_2D_8X5_UNORM
|
||||
{GL_COMPRESSED_RGBA_ASTC_5x4_KHR}, // ASTC_2D_5X4_UNORM
|
||||
{GL_SRGB8_ALPHA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV}, // B8G8R8A8_SRGB
|
||||
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT}, // BC1_RGBA_SRGB
|
||||
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT}, // BC2_SRGB
|
||||
{GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT}, // BC3_SRGB
|
||||
{GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM}, // BC7_SRGB
|
||||
{GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV}, // A4B4G4R4_UNORM
|
||||
{GL_R8, GL_RED, GL_UNSIGNED_BYTE}, // G4R4_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR}, // ASTC_2D_4X4_SRGB
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR}, // ASTC_2D_8X8_SRGB
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR}, // ASTC_2D_8X5_SRGB
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR}, // ASTC_2D_5X4_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_5x5_KHR}, // ASTC_2D_5X5_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR}, // ASTC_2D_5X5_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_10x8_KHR}, // ASTC_2D_10X8_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR}, // ASTC_2D_10X8_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_6x6_KHR}, // ASTC_2D_6X6_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR}, // ASTC_2D_6X6_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_10x6_KHR}, // ASTC_2D_10X6_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR}, // ASTC_2D_10X6_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_10x5_KHR}, // ASTC_2D_10X5_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR}, // ASTC_2D_10X5_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_10x10_KHR}, // ASTC_2D_10X10_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR}, // ASTC_2D_10X10_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_12x10_KHR}, // ASTC_2D_12X10_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR}, // ASTC_2D_12X10_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_12x12_KHR}, // ASTC_2D_12X12_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR}, // ASTC_2D_12X12_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_8x6_KHR}, // ASTC_2D_8X6_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR}, // ASTC_2D_8X6_SRGB
|
||||
{GL_COMPRESSED_RGBA_ASTC_6x5_KHR}, // ASTC_2D_6X5_UNORM
|
||||
{GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR}, // ASTC_2D_6X5_SRGB
|
||||
{GL_RGB9_E5, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV}, // E5B9G9R9_FLOAT
|
||||
{GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT}, // D32_FLOAT
|
||||
{GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT}, // D16_UNORM
|
||||
{GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT_24_8}, // X8_D24_UNORM
|
||||
{GL_STENCIL_INDEX8, GL_STENCIL, GL_UNSIGNED_BYTE}, // S8_UINT
|
||||
{GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8}, // D24_UNORM_S8_UINT
|
||||
{GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8}, // S8_UINT_D24_UNORM
|
||||
{GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL,
|
||||
GL_FLOAT_32_UNSIGNED_INT_24_8_REV}, // D32_FLOAT_S8_UINT
|
||||
#define SURFACE_FORMAT_ELEM(a1, a2, a3, name) {a1, a2, a3},
|
||||
SURFACE_FORMAT_LIST
|
||||
#undef SURFACE_FORMAT_ELEM
|
||||
}};
|
||||
|
||||
inline const FormatTuple& GetFormatTuple(VideoCore::Surface::PixelFormat pixel_format) {
|
||||
ASSERT(static_cast<size_t>(pixel_format) < FORMAT_TABLE.size());
|
||||
return FORMAT_TABLE[static_cast<size_t>(pixel_format)];
|
||||
constexpr FormatTuple GetFormatTuple(VideoCore::Surface::PixelFormat pixel_format) noexcept {
|
||||
switch (pixel_format) {
|
||||
#define SURFACE_FORMAT_ELEM(a1, a2, a3, name) case VideoCore::Surface::PixelFormat::name: return {a1, a2, a3};
|
||||
SURFACE_FORMAT_LIST
|
||||
#undef SURFACE_FORMAT_ELEM
|
||||
#undef SURFACE_FORMAT_LIST
|
||||
default: UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
inline GLenum VertexFormat(Maxwell::VertexAttribute attrib) {
|
||||
|
||||
@@ -105,148 +105,141 @@ VkCompareOp DepthCompareFunction(Tegra::Texture::DepthCompareFunc depth_compare_
|
||||
}
|
||||
|
||||
} // namespace Sampler
|
||||
|
||||
namespace {
|
||||
constexpr u32 Attachable = 1 << 0;
|
||||
constexpr u32 Storage = 1 << 1;
|
||||
|
||||
struct FormatTuple {
|
||||
VkFormat format; ///< Vulkan format
|
||||
int usage = 0; ///< Describes image format usage
|
||||
} constexpr tex_format_tuples[] = {
|
||||
{VK_FORMAT_A8B8G8R8_UNORM_PACK32, Attachable | Storage}, // A8B8G8R8_UNORM
|
||||
{VK_FORMAT_A8B8G8R8_SNORM_PACK32, Attachable | Storage}, // A8B8G8R8_SNORM
|
||||
{VK_FORMAT_A8B8G8R8_SINT_PACK32, Attachable | Storage}, // A8B8G8R8_SINT
|
||||
{VK_FORMAT_A8B8G8R8_UINT_PACK32, Attachable | Storage}, // A8B8G8R8_UINT
|
||||
{VK_FORMAT_R5G6B5_UNORM_PACK16, Attachable}, // R5G6B5_UNORM
|
||||
{VK_FORMAT_B5G6R5_UNORM_PACK16}, // B5G6R5_UNORM
|
||||
{VK_FORMAT_A1R5G5B5_UNORM_PACK16, Attachable}, // A1R5G5B5_UNORM
|
||||
{VK_FORMAT_A2B10G10R10_UNORM_PACK32, Attachable | Storage}, // A2B10G10R10_UNORM
|
||||
{VK_FORMAT_A2B10G10R10_UINT_PACK32, Attachable | Storage}, // A2B10G10R10_UINT
|
||||
{VK_FORMAT_A2R10G10B10_UNORM_PACK32, Attachable}, // A2R10G10B10_UNORM
|
||||
{VK_FORMAT_A1R5G5B5_UNORM_PACK16, Attachable}, // A1B5G5R5_UNORM (flipped with swizzle)
|
||||
{VK_FORMAT_R5G5B5A1_UNORM_PACK16}, // A5B5G5R1_UNORM (specially swizzled)
|
||||
{VK_FORMAT_R8_UNORM, Attachable | Storage}, // R8_UNORM
|
||||
{VK_FORMAT_R8_SNORM, Attachable | Storage}, // R8_SNORM
|
||||
{VK_FORMAT_R8_SINT, Attachable | Storage}, // R8_SINT
|
||||
{VK_FORMAT_R8_UINT, Attachable | Storage}, // R8_UINT
|
||||
{VK_FORMAT_R16G16B16A16_SFLOAT, Attachable | Storage}, // R16G16B16A16_FLOAT
|
||||
{VK_FORMAT_R16G16B16A16_UNORM, Attachable | Storage}, // R16G16B16A16_UNORM
|
||||
{VK_FORMAT_R16G16B16A16_SNORM, Attachable | Storage}, // R16G16B16A16_SNORM
|
||||
{VK_FORMAT_R16G16B16A16_SINT, Attachable | Storage}, // R16G16B16A16_SINT
|
||||
{VK_FORMAT_R16G16B16A16_UINT, Attachable | Storage}, // R16G16B16A16_UINT
|
||||
{VK_FORMAT_B10G11R11_UFLOAT_PACK32, Attachable | Storage}, // B10G11R11_FLOAT
|
||||
{VK_FORMAT_R32G32B32A32_UINT, Attachable | Storage}, // R32G32B32A32_UINT
|
||||
{VK_FORMAT_BC1_RGBA_UNORM_BLOCK}, // BC1_RGBA_UNORM
|
||||
{VK_FORMAT_BC2_UNORM_BLOCK}, // BC2_UNORM
|
||||
{VK_FORMAT_BC3_UNORM_BLOCK}, // BC3_UNORM
|
||||
{VK_FORMAT_BC4_UNORM_BLOCK}, // BC4_UNORM
|
||||
{VK_FORMAT_BC4_SNORM_BLOCK}, // BC4_SNORM
|
||||
{VK_FORMAT_BC5_UNORM_BLOCK}, // BC5_UNORM
|
||||
{VK_FORMAT_BC5_SNORM_BLOCK}, // BC5_SNORM
|
||||
{VK_FORMAT_BC7_UNORM_BLOCK}, // BC7_UNORM
|
||||
{VK_FORMAT_BC6H_UFLOAT_BLOCK}, // BC6H_UFLOAT
|
||||
{VK_FORMAT_BC6H_SFLOAT_BLOCK}, // BC6H_SFLOAT
|
||||
{VK_FORMAT_ASTC_4x4_UNORM_BLOCK}, // ASTC_2D_4X4_UNORM
|
||||
{VK_FORMAT_B8G8R8A8_UNORM, Attachable | Storage}, // B8G8R8A8_UNORM
|
||||
{VK_FORMAT_R32G32B32A32_SFLOAT, Attachable | Storage}, // R32G32B32A32_FLOAT
|
||||
{VK_FORMAT_R32G32B32A32_SINT, Attachable | Storage}, // R32G32B32A32_SINT
|
||||
{VK_FORMAT_R32G32_SFLOAT, Attachable | Storage}, // R32G32_FLOAT
|
||||
{VK_FORMAT_R32G32_SINT, Attachable | Storage}, // R32G32_SINT
|
||||
{VK_FORMAT_R32_SFLOAT, Attachable | Storage}, // R32_FLOAT
|
||||
{VK_FORMAT_R16_SFLOAT, Attachable | Storage}, // R16_FLOAT
|
||||
{VK_FORMAT_R16_UNORM, Attachable | Storage}, // R16_UNORM
|
||||
{VK_FORMAT_R16_SNORM, Attachable | Storage}, // R16_SNORM
|
||||
{VK_FORMAT_R16_UINT, Attachable | Storage}, // R16_UINT
|
||||
{VK_FORMAT_R16_SINT, Attachable | Storage}, // R16_SINT
|
||||
{VK_FORMAT_R16G16_UNORM, Attachable | Storage}, // R16G16_UNORM
|
||||
{VK_FORMAT_R16G16_SFLOAT, Attachable | Storage}, // R16G16_FLOAT
|
||||
{VK_FORMAT_R16G16_UINT, Attachable | Storage}, // R16G16_UINT
|
||||
{VK_FORMAT_R16G16_SINT, Attachable | Storage}, // R16G16_SINT
|
||||
{VK_FORMAT_R16G16_SNORM, Attachable | Storage}, // R16G16_SNORM
|
||||
{VK_FORMAT_R32G32B32_SFLOAT}, // R32G32B32_FLOAT
|
||||
{VK_FORMAT_A8B8G8R8_SRGB_PACK32, Attachable}, // A8B8G8R8_SRGB
|
||||
{VK_FORMAT_R8G8_UNORM, Attachable | Storage}, // R8G8_UNORM
|
||||
{VK_FORMAT_R8G8_SNORM, Attachable | Storage}, // R8G8_SNORM
|
||||
{VK_FORMAT_R8G8_SINT, Attachable | Storage}, // R8G8_SINT
|
||||
{VK_FORMAT_R8G8_UINT, Attachable | Storage}, // R8G8_UINT
|
||||
{VK_FORMAT_R32G32_UINT, Attachable | Storage}, // R32G32_UINT
|
||||
{VK_FORMAT_R16G16B16A16_SFLOAT, Attachable | Storage}, // R16G16B16X16_FLOAT
|
||||
{VK_FORMAT_R32_UINT, Attachable | Storage}, // R32_UINT
|
||||
{VK_FORMAT_R32_SINT, Attachable | Storage}, // R32_SINT
|
||||
{VK_FORMAT_ASTC_8x8_UNORM_BLOCK}, // ASTC_2D_8X8_UNORM
|
||||
{VK_FORMAT_ASTC_8x5_UNORM_BLOCK}, // ASTC_2D_8X5_UNORM
|
||||
{VK_FORMAT_ASTC_5x4_UNORM_BLOCK}, // ASTC_2D_5X4_UNORM
|
||||
{VK_FORMAT_B8G8R8A8_SRGB, Attachable}, // B8G8R8A8_SRGB
|
||||
{VK_FORMAT_BC1_RGBA_SRGB_BLOCK}, // BC1_RGBA_SRGB
|
||||
{VK_FORMAT_BC2_SRGB_BLOCK}, // BC2_SRGB
|
||||
{VK_FORMAT_BC3_SRGB_BLOCK}, // BC3_SRGB
|
||||
{VK_FORMAT_BC7_SRGB_BLOCK}, // BC7_SRGB
|
||||
{VK_FORMAT_A4B4G4R4_UNORM_PACK16_EXT}, // A4B4G4R4_UNORM
|
||||
{VK_FORMAT_R4G4_UNORM_PACK8}, // G4R4_UNORM
|
||||
{VK_FORMAT_ASTC_4x4_SRGB_BLOCK}, // ASTC_2D_4X4_SRGB
|
||||
{VK_FORMAT_ASTC_8x8_SRGB_BLOCK}, // ASTC_2D_8X8_SRGB
|
||||
{VK_FORMAT_ASTC_8x5_SRGB_BLOCK}, // ASTC_2D_8X5_SRGB
|
||||
{VK_FORMAT_ASTC_5x4_SRGB_BLOCK}, // ASTC_2D_5X4_SRGB
|
||||
{VK_FORMAT_ASTC_5x5_UNORM_BLOCK}, // ASTC_2D_5X5_UNORM
|
||||
{VK_FORMAT_ASTC_5x5_SRGB_BLOCK}, // ASTC_2D_5X5_SRGB
|
||||
{VK_FORMAT_ASTC_10x8_UNORM_BLOCK}, // ASTC_2D_10X8_UNORM
|
||||
{VK_FORMAT_ASTC_10x8_SRGB_BLOCK}, // ASTC_2D_10X8_SRGB
|
||||
{VK_FORMAT_ASTC_6x6_UNORM_BLOCK}, // ASTC_2D_6X6_UNORM
|
||||
{VK_FORMAT_ASTC_6x6_SRGB_BLOCK}, // ASTC_2D_6X6_SRGB
|
||||
{VK_FORMAT_ASTC_10x6_UNORM_BLOCK}, // ASTC_2D_10X6_UNORM
|
||||
{VK_FORMAT_ASTC_10x6_SRGB_BLOCK}, // ASTC_2D_10X6_SRGB
|
||||
{VK_FORMAT_ASTC_10x5_UNORM_BLOCK}, // ASTC_2D_10X5_UNORM
|
||||
{VK_FORMAT_ASTC_10x5_SRGB_BLOCK}, // ASTC_2D_10X5_SRGB
|
||||
{VK_FORMAT_ASTC_10x10_UNORM_BLOCK}, // ASTC_2D_10X10_UNORM
|
||||
{VK_FORMAT_ASTC_10x10_SRGB_BLOCK}, // ASTC_2D_10X10_SRGB
|
||||
{VK_FORMAT_ASTC_12x10_UNORM_BLOCK}, // ASTC_2D_12X10_UNORM
|
||||
{VK_FORMAT_ASTC_12x10_SRGB_BLOCK}, // ASTC_2D_12X10_SRGB
|
||||
{VK_FORMAT_ASTC_12x12_UNORM_BLOCK}, // ASTC_2D_12X12_UNORM
|
||||
{VK_FORMAT_ASTC_12x12_SRGB_BLOCK}, // ASTC_2D_12X12_SRGB
|
||||
{VK_FORMAT_ASTC_8x6_UNORM_BLOCK}, // ASTC_2D_8X6_UNORM
|
||||
{VK_FORMAT_ASTC_8x6_SRGB_BLOCK}, // ASTC_2D_8X6_SRGB
|
||||
{VK_FORMAT_ASTC_6x5_UNORM_BLOCK}, // ASTC_2D_6X5_UNORM
|
||||
{VK_FORMAT_ASTC_6x5_SRGB_BLOCK}, // ASTC_2D_6X5_SRGB
|
||||
{VK_FORMAT_E5B9G9R9_UFLOAT_PACK32}, // E5B9G9R9_FLOAT
|
||||
|
||||
// Depth formats
|
||||
{VK_FORMAT_D32_SFLOAT, Attachable}, // D32_FLOAT
|
||||
{VK_FORMAT_D16_UNORM, Attachable}, // D16_UNORM
|
||||
{VK_FORMAT_X8_D24_UNORM_PACK32, Attachable}, // X8_D24_UNORM
|
||||
|
||||
// Stencil formats
|
||||
{VK_FORMAT_S8_UINT, Attachable}, // S8_UINT
|
||||
|
||||
// DepthStencil formats
|
||||
{VK_FORMAT_D24_UNORM_S8_UINT, Attachable}, // D24_UNORM_S8_UINT
|
||||
{VK_FORMAT_D24_UNORM_S8_UINT, Attachable}, // S8_UINT_D24_UNORM (emulated)
|
||||
{VK_FORMAT_D32_SFLOAT_S8_UINT, Attachable}, // D32_FLOAT_S8_UINT
|
||||
VkFormat format{}; ///< Vulkan format
|
||||
s32 usage = 0; ///< Describes image format usage
|
||||
};
|
||||
static_assert(std::size(tex_format_tuples) == VideoCore::Surface::MaxPixelFormat);
|
||||
|
||||
constexpr bool IsZetaFormat(PixelFormat pixel_format) {
|
||||
return pixel_format >= PixelFormat::MaxColorFormat &&
|
||||
pixel_format < PixelFormat::MaxDepthStencilFormat;
|
||||
return pixel_format >= PixelFormat::MaxColorFormat && pixel_format < PixelFormat::MaxDepthStencilFormat;
|
||||
}
|
||||
|
||||
} // Anonymous namespace
|
||||
|
||||
FormatInfo SurfaceFormat(const Device& device, FormatType format_type, bool with_srgb,
|
||||
PixelFormat pixel_format) {
|
||||
ASSERT(static_cast<size_t>(pixel_format) < std::size(tex_format_tuples));
|
||||
FormatTuple tuple = tex_format_tuples[static_cast<size_t>(pixel_format)];
|
||||
FormatInfo SurfaceFormat(const Device& device, FormatType format_type, bool with_srgb, PixelFormat pixel_format) {
|
||||
u32 const usage_attachable = 1 << 0;
|
||||
u32 const usage_storage = 1 << 1;
|
||||
FormatTuple tuple;
|
||||
switch (pixel_format) {
|
||||
#define SURFACE_FORMAT_LIST \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A8B8G8R8_UNORM_PACK32, usage_attachable | usage_storage, A8B8G8R8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A8B8G8R8_SNORM_PACK32, usage_attachable | usage_storage, A8B8G8R8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A8B8G8R8_SINT_PACK32, usage_attachable | usage_storage, A8B8G8R8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A8B8G8R8_UINT_PACK32, usage_attachable | usage_storage, A8B8G8R8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R5G6B5_UNORM_PACK16, usage_attachable, R5G6B5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_B5G6R5_UNORM_PACK16, 0, B5G6R5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A1R5G5B5_UNORM_PACK16, usage_attachable, A1R5G5B5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A2B10G10R10_UNORM_PACK32, usage_attachable | usage_storage, A2B10G10R10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A2B10G10R10_UINT_PACK32, usage_attachable | usage_storage, A2B10G10R10_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A2R10G10B10_UNORM_PACK32, usage_attachable, A2R10G10B10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A1R5G5B5_UNORM_PACK16, usage_attachable, A1B5G5R5_UNORM) /*flipped with swizzle*/ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R5G5B5A1_UNORM_PACK16, 0, A5B5G5R1_UNORM) /*specially swizzled*/ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8_UNORM, usage_attachable | usage_storage, R8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8_SNORM, usage_attachable | usage_storage, R8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8_SINT, usage_attachable | usage_storage, R8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8_UINT, usage_attachable | usage_storage, R8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_SFLOAT, usage_attachable | usage_storage, R16G16B16A16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_UNORM, usage_attachable | usage_storage, R16G16B16A16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_SNORM, usage_attachable | usage_storage, R16G16B16A16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_SINT, usage_attachable | usage_storage, R16G16B16A16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_UINT, usage_attachable | usage_storage, R16G16B16A16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_B10G11R11_UFLOAT_PACK32, usage_attachable | usage_storage, B10G11R11_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32B32A32_UINT, usage_attachable | usage_storage, R32G32B32A32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC1_RGBA_UNORM_BLOCK, 0, BC1_RGBA_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC2_UNORM_BLOCK, 0, BC2_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC3_UNORM_BLOCK, 0, BC3_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC4_UNORM_BLOCK, 0, BC4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC4_SNORM_BLOCK, 0, BC4_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC5_UNORM_BLOCK, 0, BC5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC5_SNORM_BLOCK, 0, BC5_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC7_UNORM_BLOCK, 0, BC7_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC6H_UFLOAT_BLOCK, 0, BC6H_UFLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC6H_SFLOAT_BLOCK, 0, BC6H_SFLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_4x4_UNORM_BLOCK, 0, ASTC_2D_4X4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_B8G8R8A8_UNORM, usage_attachable | usage_storage, B8G8R8A8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32B32A32_SFLOAT, usage_attachable | usage_storage, R32G32B32A32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32B32A32_SINT, usage_attachable | usage_storage, R32G32B32A32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32_SFLOAT, usage_attachable | usage_storage, R32G32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32_SINT, usage_attachable | usage_storage, R32G32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32_SFLOAT, usage_attachable | usage_storage, R32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16_SFLOAT, usage_attachable | usage_storage, R16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16_UNORM, usage_attachable | usage_storage, R16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16_SNORM, usage_attachable | usage_storage, R16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16_UINT, usage_attachable | usage_storage, R16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16_SINT, usage_attachable | usage_storage, R16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16_UNORM, usage_attachable | usage_storage, R16G16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16_SFLOAT, usage_attachable | usage_storage, R16G16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16_UINT, usage_attachable | usage_storage, R16G16_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16_SINT, usage_attachable | usage_storage, R16G16_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16_SNORM, usage_attachable | usage_storage, R16G16_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32B32_SFLOAT, 0, R32G32B32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A8B8G8R8_SRGB_PACK32, usage_attachable, A8B8G8R8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8G8_UNORM, usage_attachable | usage_storage, R8G8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8G8_SNORM, usage_attachable | usage_storage, R8G8_SNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8G8_SINT, usage_attachable | usage_storage, R8G8_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R8G8_UINT, usage_attachable | usage_storage, R8G8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32G32_UINT, usage_attachable | usage_storage, R32G32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R16G16B16A16_SFLOAT, usage_attachable | usage_storage, R16G16B16X16_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32_UINT, usage_attachable | usage_storage, R32_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R32_SINT, usage_attachable | usage_storage, R32_SINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x8_UNORM_BLOCK, 0, ASTC_2D_8X8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x5_UNORM_BLOCK, 0, ASTC_2D_8X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_5x4_UNORM_BLOCK, 0, ASTC_2D_5X4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_B8G8R8A8_SRGB, usage_attachable, B8G8R8A8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC1_RGBA_SRGB_BLOCK, 0, BC1_RGBA_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC2_SRGB_BLOCK, 0, BC2_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC3_SRGB_BLOCK, 0, BC3_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_BC7_SRGB_BLOCK, 0, BC7_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_A4B4G4R4_UNORM_PACK16_EXT, 0, A4B4G4R4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_R4G4_UNORM_PACK8, 0, G4R4_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_4x4_SRGB_BLOCK, 0, ASTC_2D_4X4_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x8_SRGB_BLOCK, 0, ASTC_2D_8X8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x5_SRGB_BLOCK, 0, ASTC_2D_8X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_5x4_SRGB_BLOCK, 0, ASTC_2D_5X4_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_5x5_UNORM_BLOCK, 0, ASTC_2D_5X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_5x5_SRGB_BLOCK, 0, ASTC_2D_5X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x8_UNORM_BLOCK, 0, ASTC_2D_10X8_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x8_SRGB_BLOCK, 0, ASTC_2D_10X8_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_6x6_UNORM_BLOCK, 0, ASTC_2D_6X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_6x6_SRGB_BLOCK, 0, ASTC_2D_6X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x6_UNORM_BLOCK, 0, ASTC_2D_10X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x6_SRGB_BLOCK, 0, ASTC_2D_10X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x5_UNORM_BLOCK, 0, ASTC_2D_10X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x5_SRGB_BLOCK, 0, ASTC_2D_10X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x10_UNORM_BLOCK, 0, ASTC_2D_10X10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_10x10_SRGB_BLOCK, 0, ASTC_2D_10X10_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_12x10_UNORM_BLOCK, 0, ASTC_2D_12X10_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_12x10_SRGB_BLOCK, 0, ASTC_2D_12X10_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_12x12_UNORM_BLOCK, 0, ASTC_2D_12X12_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_12x12_SRGB_BLOCK, 0, ASTC_2D_12X12_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x6_UNORM_BLOCK, 0, ASTC_2D_8X6_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_8x6_SRGB_BLOCK, 0, ASTC_2D_8X6_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_6x5_UNORM_BLOCK, 0, ASTC_2D_6X5_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_ASTC_6x5_SRGB_BLOCK, 0, ASTC_2D_6X5_SRGB) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_E5B9G9R9_UFLOAT_PACK32, 0, E5B9G9R9_FLOAT) \
|
||||
/* Depth formats */ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_D32_SFLOAT, usage_attachable, D32_FLOAT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_D16_UNORM, usage_attachable, D16_UNORM) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_X8_D24_UNORM_PACK32, usage_attachable, X8_D24_UNORM) \
|
||||
/* Stencil formats */ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_S8_UINT, usage_attachable, S8_UINT) \
|
||||
/* DepthStencil formats */ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_D24_UNORM_S8_UINT, usage_attachable, D24_UNORM_S8_UINT) \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_D24_UNORM_S8_UINT, usage_attachable, S8_UINT_D24_UNORM) /* emulated */ \
|
||||
SURFACE_FORMAT_ELEM(VK_FORMAT_D32_SFLOAT_S8_UINT, usage_attachable, D32_FLOAT_S8_UINT)
|
||||
#define SURFACE_FORMAT_ELEM(res, usage, pixel) case PixelFormat::pixel: tuple = {res, usage}; break;
|
||||
SURFACE_FORMAT_LIST
|
||||
default: UNREACHABLE_MSG("unknown format {}", pixel_format);
|
||||
#undef SURFACE_FORMAT_ELEM
|
||||
#undef SURFACE_FORMAT_LIST
|
||||
}
|
||||
bool const is_srgb = with_srgb && VideoCore::Surface::IsPixelFormatSRGB(pixel_format);
|
||||
// Transcode on hardware that doesn't support ASTC natively
|
||||
if (!device.IsOptimalAstcSupported() && VideoCore::Surface::IsPixelFormatASTC(pixel_format)) {
|
||||
const bool is_srgb = with_srgb && VideoCore::Surface::IsPixelFormatSRGB(pixel_format);
|
||||
|
||||
switch (Settings::values.astc_recompression.GetValue()) {
|
||||
case Settings::AstcRecompression::Uncompressed:
|
||||
if (is_srgb) {
|
||||
tuple.format = VK_FORMAT_A8B8G8R8_SRGB_PACK32;
|
||||
} else {
|
||||
tuple.format = VK_FORMAT_A8B8G8R8_UNORM_PACK32;
|
||||
tuple.usage |= Storage;
|
||||
tuple.usage |= usage_storage;
|
||||
}
|
||||
break;
|
||||
case Settings::AstcRecompression::Bc1:
|
||||
@@ -259,7 +252,6 @@ FormatInfo SurfaceFormat(const Device& device, FormatType format_type, bool with
|
||||
}
|
||||
// Transcode on hardware that doesn't support BCn natively
|
||||
if (!device.IsOptimalBcnSupported() && VideoCore::Surface::IsPixelFormatBCn(pixel_format)) {
|
||||
const bool is_srgb = with_srgb && VideoCore::Surface::IsPixelFormatSRGB(pixel_format);
|
||||
if (pixel_format == PixelFormat::BC4_SNORM) {
|
||||
tuple.format = VK_FORMAT_R8_SNORM;
|
||||
} else if (pixel_format == PixelFormat::BC4_UNORM) {
|
||||
@@ -268,8 +260,7 @@ FormatInfo SurfaceFormat(const Device& device, FormatType format_type, bool with
|
||||
tuple.format = VK_FORMAT_R8G8_SNORM;
|
||||
} else if (pixel_format == PixelFormat::BC5_UNORM) {
|
||||
tuple.format = VK_FORMAT_R8G8_UNORM;
|
||||
} else if (pixel_format == PixelFormat::BC6H_SFLOAT ||
|
||||
pixel_format == PixelFormat::BC6H_UFLOAT) {
|
||||
} else if (pixel_format == PixelFormat::BC6H_SFLOAT || pixel_format == PixelFormat::BC6H_UFLOAT) {
|
||||
tuple.format = VK_FORMAT_R16G16B16A16_SFLOAT;
|
||||
} else if (is_srgb) {
|
||||
tuple.format = VK_FORMAT_A8B8G8R8_SRGB_PACK32;
|
||||
@@ -277,9 +268,8 @@ FormatInfo SurfaceFormat(const Device& device, FormatType format_type, bool with
|
||||
tuple.format = VK_FORMAT_A8B8G8R8_UNORM_PACK32;
|
||||
}
|
||||
}
|
||||
const bool attachable = (tuple.usage & Attachable) != 0;
|
||||
const bool storage = (tuple.usage & Storage) != 0;
|
||||
|
||||
bool const attachable = (tuple.usage & usage_attachable) != 0;
|
||||
bool const storage = (tuple.usage & usage_storage) != 0;
|
||||
VkFormatFeatureFlags usage{};
|
||||
switch (format_type) {
|
||||
case FormatType::Buffer:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// SPDX-FileCopyrightText: 2014 Citra Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
@@ -13,127 +15,125 @@
|
||||
|
||||
namespace VideoCore::Surface {
|
||||
|
||||
#define PIXEL_FORMAT_LIST \
|
||||
PIXEL_FORMAT_ELEM(A8B8G8R8_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A8B8G8R8_SNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A8B8G8R8_SINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A8B8G8R8_UINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R5G6B5_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(B5G6R5_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(A1R5G5B5_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(A2B10G10R10_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A2B10G10R10_UINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A2R10G10B10_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(A1B5G5R5_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(A5B5G5R1_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R8_UNORM, 1, 1, 8) \
|
||||
PIXEL_FORMAT_ELEM(R8_SNORM, 1, 1, 8) \
|
||||
PIXEL_FORMAT_ELEM(R8_SINT, 1, 1, 8) \
|
||||
PIXEL_FORMAT_ELEM(R8_UINT, 1, 1, 8) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16A16_FLOAT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16A16_UNORM, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16A16_SNORM, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16A16_SINT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16A16_UINT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(B10G11R11_FLOAT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R32G32B32A32_UINT, 1, 1, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC1_RGBA_UNORM, 4, 4, 64) \
|
||||
PIXEL_FORMAT_ELEM(BC2_UNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC3_UNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC4_UNORM, 4, 4, 64) \
|
||||
PIXEL_FORMAT_ELEM(BC4_SNORM, 4, 4, 64) \
|
||||
PIXEL_FORMAT_ELEM(BC5_UNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC5_SNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC7_UNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC6H_UFLOAT, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC6H_SFLOAT, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_4X4_UNORM, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(B8G8R8A8_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R32G32B32A32_FLOAT, 1, 1, 128) \
|
||||
PIXEL_FORMAT_ELEM(R32G32B32A32_SINT, 1, 1, 128) \
|
||||
PIXEL_FORMAT_ELEM(R32G32_FLOAT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R32G32_SINT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R32_FLOAT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R16_FLOAT, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R16_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R16_SNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R16_UINT, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R16_SINT, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R16G16_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R16G16_FLOAT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R16G16_UINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R16G16_SINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R16G16_SNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R32G32B32_FLOAT, 1, 1, 96) \
|
||||
PIXEL_FORMAT_ELEM(A8B8G8R8_SRGB, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R8G8_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R8G8_SNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R8G8_SINT, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R8G8_UINT, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(R32G32_UINT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R16G16B16X16_FLOAT, 1, 1, 64) \
|
||||
PIXEL_FORMAT_ELEM(R32_UINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(R32_SINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X8_UNORM, 8, 8, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X5_UNORM, 8, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_5X4_UNORM, 5, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(B8G8R8A8_SRGB, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(BC1_RGBA_SRGB, 4, 4, 64) \
|
||||
PIXEL_FORMAT_ELEM(BC2_SRGB, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC3_SRGB, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(BC7_SRGB, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(A4B4G4R4_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(G4R4_UNORM, 1, 1, 8) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_4X4_SRGB, 4, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X8_SRGB, 8, 8, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X5_SRGB, 8, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_5X4_SRGB, 5, 4, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_5X5_UNORM, 5, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_5X5_SRGB, 5, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X8_UNORM, 10, 8, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X8_SRGB, 10, 8, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_6X6_UNORM, 6, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_6X6_SRGB, 6, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X6_UNORM, 10, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X6_SRGB, 10, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X5_UNORM, 10, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X5_SRGB, 10, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X10_UNORM, 10, 10, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_10X10_SRGB, 10, 10, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_12X10_UNORM, 12, 10, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_12X10_SRGB, 12, 10, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_12X12_UNORM, 12, 12, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_12X12_SRGB, 12, 12, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X6_UNORM, 8, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_8X6_SRGB, 8, 6, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_6X5_UNORM, 6, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(ASTC_2D_6X5_SRGB, 6, 5, 128) \
|
||||
PIXEL_FORMAT_ELEM(E5B9G9R9_FLOAT, 1, 1, 32) \
|
||||
/* Depth formats */ \
|
||||
PIXEL_FORMAT_ELEM(D32_FLOAT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(D16_UNORM, 1, 1, 16) \
|
||||
PIXEL_FORMAT_ELEM(X8_D24_UNORM, 1, 1, 32) \
|
||||
/* Stencil formats */ \
|
||||
PIXEL_FORMAT_ELEM(S8_UINT, 1, 1, 8) \
|
||||
/* DepthStencil formats */ \
|
||||
PIXEL_FORMAT_ELEM(D24_UNORM_S8_UINT, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(S8_UINT_D24_UNORM, 1, 1, 32) \
|
||||
PIXEL_FORMAT_ELEM(D32_FLOAT_S8_UINT, 1, 1, 64)
|
||||
|
||||
enum class PixelFormat {
|
||||
A8B8G8R8_UNORM,
|
||||
A8B8G8R8_SNORM,
|
||||
A8B8G8R8_SINT,
|
||||
A8B8G8R8_UINT,
|
||||
R5G6B5_UNORM,
|
||||
B5G6R5_UNORM,
|
||||
A1R5G5B5_UNORM,
|
||||
A2B10G10R10_UNORM,
|
||||
A2B10G10R10_UINT,
|
||||
A2R10G10B10_UNORM,
|
||||
A1B5G5R5_UNORM,
|
||||
A5B5G5R1_UNORM,
|
||||
R8_UNORM,
|
||||
R8_SNORM,
|
||||
R8_SINT,
|
||||
R8_UINT,
|
||||
R16G16B16A16_FLOAT,
|
||||
R16G16B16A16_UNORM,
|
||||
R16G16B16A16_SNORM,
|
||||
R16G16B16A16_SINT,
|
||||
R16G16B16A16_UINT,
|
||||
B10G11R11_FLOAT,
|
||||
R32G32B32A32_UINT,
|
||||
BC1_RGBA_UNORM,
|
||||
BC2_UNORM,
|
||||
BC3_UNORM,
|
||||
BC4_UNORM,
|
||||
BC4_SNORM,
|
||||
BC5_UNORM,
|
||||
BC5_SNORM,
|
||||
BC7_UNORM,
|
||||
BC6H_UFLOAT,
|
||||
BC6H_SFLOAT,
|
||||
ASTC_2D_4X4_UNORM,
|
||||
B8G8R8A8_UNORM,
|
||||
R32G32B32A32_FLOAT,
|
||||
R32G32B32A32_SINT,
|
||||
R32G32_FLOAT,
|
||||
R32G32_SINT,
|
||||
R32_FLOAT,
|
||||
R16_FLOAT,
|
||||
R16_UNORM,
|
||||
R16_SNORM,
|
||||
R16_UINT,
|
||||
R16_SINT,
|
||||
R16G16_UNORM,
|
||||
R16G16_FLOAT,
|
||||
R16G16_UINT,
|
||||
R16G16_SINT,
|
||||
R16G16_SNORM,
|
||||
R32G32B32_FLOAT,
|
||||
A8B8G8R8_SRGB,
|
||||
R8G8_UNORM,
|
||||
R8G8_SNORM,
|
||||
R8G8_SINT,
|
||||
R8G8_UINT,
|
||||
R32G32_UINT,
|
||||
R16G16B16X16_FLOAT,
|
||||
R32_UINT,
|
||||
R32_SINT,
|
||||
ASTC_2D_8X8_UNORM,
|
||||
ASTC_2D_8X5_UNORM,
|
||||
ASTC_2D_5X4_UNORM,
|
||||
B8G8R8A8_SRGB,
|
||||
BC1_RGBA_SRGB,
|
||||
BC2_SRGB,
|
||||
BC3_SRGB,
|
||||
BC7_SRGB,
|
||||
A4B4G4R4_UNORM,
|
||||
G4R4_UNORM,
|
||||
ASTC_2D_4X4_SRGB,
|
||||
ASTC_2D_8X8_SRGB,
|
||||
ASTC_2D_8X5_SRGB,
|
||||
ASTC_2D_5X4_SRGB,
|
||||
ASTC_2D_5X5_UNORM,
|
||||
ASTC_2D_5X5_SRGB,
|
||||
ASTC_2D_10X8_UNORM,
|
||||
ASTC_2D_10X8_SRGB,
|
||||
ASTC_2D_6X6_UNORM,
|
||||
ASTC_2D_6X6_SRGB,
|
||||
ASTC_2D_10X6_UNORM,
|
||||
ASTC_2D_10X6_SRGB,
|
||||
ASTC_2D_10X5_UNORM,
|
||||
ASTC_2D_10X5_SRGB,
|
||||
ASTC_2D_10X10_UNORM,
|
||||
ASTC_2D_10X10_SRGB,
|
||||
ASTC_2D_12X10_UNORM,
|
||||
ASTC_2D_12X10_SRGB,
|
||||
ASTC_2D_12X12_UNORM,
|
||||
ASTC_2D_12X12_SRGB,
|
||||
ASTC_2D_8X6_UNORM,
|
||||
ASTC_2D_8X6_SRGB,
|
||||
ASTC_2D_6X5_UNORM,
|
||||
ASTC_2D_6X5_SRGB,
|
||||
E5B9G9R9_FLOAT,
|
||||
|
||||
MaxColorFormat,
|
||||
|
||||
// Depth formats
|
||||
D32_FLOAT = MaxColorFormat,
|
||||
D16_UNORM,
|
||||
X8_D24_UNORM,
|
||||
|
||||
MaxDepthFormat,
|
||||
|
||||
// Stencil formats
|
||||
S8_UINT = MaxDepthFormat,
|
||||
MaxStencilFormat,
|
||||
|
||||
// DepthStencil formats
|
||||
D24_UNORM_S8_UINT = MaxStencilFormat,
|
||||
S8_UINT_D24_UNORM,
|
||||
D32_FLOAT_S8_UINT,
|
||||
|
||||
MaxDepthStencilFormat,
|
||||
|
||||
#define PIXEL_FORMAT_ELEM(name, ...) name,
|
||||
PIXEL_FORMAT_LIST
|
||||
#undef PIXEL_FORMAT_ELEM
|
||||
MaxColorFormat = D32_FLOAT,
|
||||
MaxDepthFormat = S8_UINT,
|
||||
MaxStencilFormat = D24_UNORM_S8_UINT,
|
||||
MaxDepthStencilFormat = u8(D32_FLOAT_S8_UINT) + 1,
|
||||
Max = MaxDepthStencilFormat,
|
||||
Invalid = 255,
|
||||
};
|
||||
constexpr std::size_t MaxPixelFormat = static_cast<std::size_t>(PixelFormat::Max);
|
||||
constexpr std::size_t MaxPixelFormat = std::size_t(PixelFormat::Max);
|
||||
|
||||
enum class SurfaceType {
|
||||
ColorTexture = 0,
|
||||
@@ -154,371 +154,55 @@ enum class SurfaceTarget {
|
||||
TextureCubeArray,
|
||||
};
|
||||
|
||||
constexpr std::array<u8, MaxPixelFormat> BLOCK_WIDTH_TABLE = {{
|
||||
1, // A8B8G8R8_UNORM
|
||||
1, // A8B8G8R8_SNORM
|
||||
1, // A8B8G8R8_SINT
|
||||
1, // A8B8G8R8_UINT
|
||||
1, // R5G6B5_UNORM
|
||||
1, // B5G6R5_UNORM
|
||||
1, // A1R5G5B5_UNORM
|
||||
1, // A2B10G10R10_UNORM
|
||||
1, // A2B10G10R10_UINT
|
||||
1, // A2R10G10B10_UNORM
|
||||
1, // A1B5G5R5_UNORM
|
||||
1, // A5B5G5R1_UNORM
|
||||
1, // R8_UNORM
|
||||
1, // R8_SNORM
|
||||
1, // R8_SINT
|
||||
1, // R8_UINT
|
||||
1, // R16G16B16A16_FLOAT
|
||||
1, // R16G16B16A16_UNORM
|
||||
1, // R16G16B16A16_SNORM
|
||||
1, // R16G16B16A16_SINT
|
||||
1, // R16G16B16A16_UINT
|
||||
1, // B10G11R11_FLOAT
|
||||
1, // R32G32B32A32_UINT
|
||||
4, // BC1_RGBA_UNORM
|
||||
4, // BC2_UNORM
|
||||
4, // BC3_UNORM
|
||||
4, // BC4_UNORM
|
||||
4, // BC4_SNORM
|
||||
4, // BC5_UNORM
|
||||
4, // BC5_SNORM
|
||||
4, // BC7_UNORM
|
||||
4, // BC6H_UFLOAT
|
||||
4, // BC6H_SFLOAT
|
||||
4, // ASTC_2D_4X4_UNORM
|
||||
1, // B8G8R8A8_UNORM
|
||||
1, // R32G32B32A32_FLOAT
|
||||
1, // R32G32B32A32_SINT
|
||||
1, // R32G32_FLOAT
|
||||
1, // R32G32_SINT
|
||||
1, // R32_FLOAT
|
||||
1, // R16_FLOAT
|
||||
1, // R16_UNORM
|
||||
1, // R16_SNORM
|
||||
1, // R16_UINT
|
||||
1, // R16_SINT
|
||||
1, // R16G16_UNORM
|
||||
1, // R16G16_FLOAT
|
||||
1, // R16G16_UINT
|
||||
1, // R16G16_SINT
|
||||
1, // R16G16_SNORM
|
||||
1, // R32G32B32_FLOAT
|
||||
1, // A8B8G8R8_SRGB
|
||||
1, // R8G8_UNORM
|
||||
1, // R8G8_SNORM
|
||||
1, // R8G8_SINT
|
||||
1, // R8G8_UINT
|
||||
1, // R32G32_UINT
|
||||
1, // R16G16B16X16_FLOAT
|
||||
1, // R32_UINT
|
||||
1, // R32_SINT
|
||||
8, // ASTC_2D_8X8_UNORM
|
||||
8, // ASTC_2D_8X5_UNORM
|
||||
5, // ASTC_2D_5X4_UNORM
|
||||
1, // B8G8R8A8_SRGB
|
||||
4, // BC1_RGBA_SRGB
|
||||
4, // BC2_SRGB
|
||||
4, // BC3_SRGB
|
||||
4, // BC7_SRGB
|
||||
1, // A4B4G4R4_UNORM
|
||||
1, // G4R4_UNORM
|
||||
4, // ASTC_2D_4X4_SRGB
|
||||
8, // ASTC_2D_8X8_SRGB
|
||||
8, // ASTC_2D_8X5_SRGB
|
||||
5, // ASTC_2D_5X4_SRGB
|
||||
5, // ASTC_2D_5X5_UNORM
|
||||
5, // ASTC_2D_5X5_SRGB
|
||||
10, // ASTC_2D_10X8_UNORM
|
||||
10, // ASTC_2D_10X8_SRGB
|
||||
6, // ASTC_2D_6X6_UNORM
|
||||
6, // ASTC_2D_6X6_SRGB
|
||||
10, // ASTC_2D_10X6_UNORM
|
||||
10, // ASTC_2D_10X6_SRGB
|
||||
10, // ASTC_2D_10X5_UNORM
|
||||
10, // ASTC_2D_10X5_SRGB
|
||||
10, // ASTC_2D_10X10_UNORM
|
||||
10, // ASTC_2D_10X10_SRGB
|
||||
12, // ASTC_2D_12X10_UNORM
|
||||
12, // ASTC_2D_12X10_SRGB
|
||||
12, // ASTC_2D_12X12_UNORM
|
||||
12, // ASTC_2D_12X12_SRGB
|
||||
8, // ASTC_2D_8X6_UNORM
|
||||
8, // ASTC_2D_8X6_SRGB
|
||||
6, // ASTC_2D_6X5_UNORM
|
||||
6, // ASTC_2D_6X5_SRGB
|
||||
1, // E5B9G9R9_FLOAT
|
||||
1, // D32_FLOAT
|
||||
1, // D16_UNORM
|
||||
1, // X8_D24_UNORM
|
||||
1, // S8_UINT
|
||||
1, // D24_UNORM_S8_UINT
|
||||
1, // S8_UINT_D24_UNORM
|
||||
1, // D32_FLOAT_S8_UINT
|
||||
}};
|
||||
|
||||
constexpr u32 DefaultBlockWidth(PixelFormat format) {
|
||||
ASSERT(static_cast<std::size_t>(format) < BLOCK_WIDTH_TABLE.size());
|
||||
return BLOCK_WIDTH_TABLE[static_cast<std::size_t>(format)];
|
||||
constexpr u32 DefaultBlockWidth(PixelFormat format) noexcept {
|
||||
switch (format) {
|
||||
#define PIXEL_FORMAT_ELEM(name, width, height, bits) case PixelFormat::name: return width;
|
||||
PIXEL_FORMAT_LIST
|
||||
#undef PIXEL_FORMAT_ELEM
|
||||
default: UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
constexpr std::array<u8, MaxPixelFormat> BLOCK_HEIGHT_TABLE = {{
|
||||
1, // A8B8G8R8_UNORM
|
||||
1, // A8B8G8R8_SNORM
|
||||
1, // A8B8G8R8_SINT
|
||||
1, // A8B8G8R8_UINT
|
||||
1, // R5G6B5_UNORM
|
||||
1, // B5G6R5_UNORM
|
||||
1, // A1R5G5B5_UNORM
|
||||
1, // A2B10G10R10_UNORM
|
||||
1, // A2B10G10R10_UINT
|
||||
1, // A2R10G10B10_UNORM
|
||||
1, // A1B5G5R5_UNORM
|
||||
1, // A5B5G5R1_UNORM
|
||||
1, // R8_UNORM
|
||||
1, // R8_SNORM
|
||||
1, // R8_SINT
|
||||
1, // R8_UINT
|
||||
1, // R16G16B16A16_FLOAT
|
||||
1, // R16G16B16A16_UNORM
|
||||
1, // R16G16B16A16_SNORM
|
||||
1, // R16G16B16A16_SINT
|
||||
1, // R16G16B16A16_UINT
|
||||
1, // B10G11R11_FLOAT
|
||||
1, // R32G32B32A32_UINT
|
||||
4, // BC1_RGBA_UNORM
|
||||
4, // BC2_UNORM
|
||||
4, // BC3_UNORM
|
||||
4, // BC4_UNORM
|
||||
4, // BC4_SNORM
|
||||
4, // BC5_UNORM
|
||||
4, // BC5_SNORM
|
||||
4, // BC7_UNORM
|
||||
4, // BC6H_UFLOAT
|
||||
4, // BC6H_SFLOAT
|
||||
4, // ASTC_2D_4X4_UNORM
|
||||
1, // B8G8R8A8_UNORM
|
||||
1, // R32G32B32A32_FLOAT
|
||||
1, // R32G32B32A32_SINT
|
||||
1, // R32G32_FLOAT
|
||||
1, // R32G32_SINT
|
||||
1, // R32_FLOAT
|
||||
1, // R16_FLOAT
|
||||
1, // R16_UNORM
|
||||
1, // R16_SNORM
|
||||
1, // R16_UINT
|
||||
1, // R16_SINT
|
||||
1, // R16G16_UNORM
|
||||
1, // R16G16_FLOAT
|
||||
1, // R16G16_UINT
|
||||
1, // R16G16_SINT
|
||||
1, // R16G16_SNORM
|
||||
1, // R32G32B32_FLOAT
|
||||
1, // A8B8G8R8_SRGB
|
||||
1, // R8G8_UNORM
|
||||
1, // R8G8_SNORM
|
||||
1, // R8G8_SINT
|
||||
1, // R8G8_UINT
|
||||
1, // R32G32_UINT
|
||||
1, // R16G16B16X16_FLOAT
|
||||
1, // R32_UINT
|
||||
1, // R32_SINT
|
||||
8, // ASTC_2D_8X8_UNORM
|
||||
5, // ASTC_2D_8X5_UNORM
|
||||
4, // ASTC_2D_5X4_UNORM
|
||||
1, // B8G8R8A8_SRGB
|
||||
4, // BC1_RGBA_SRGB
|
||||
4, // BC2_SRGB
|
||||
4, // BC3_SRGB
|
||||
4, // BC7_SRGB
|
||||
1, // A4B4G4R4_UNORM
|
||||
1, // G4R4_UNORM
|
||||
4, // ASTC_2D_4X4_SRGB
|
||||
8, // ASTC_2D_8X8_SRGB
|
||||
5, // ASTC_2D_8X5_SRGB
|
||||
4, // ASTC_2D_5X4_SRGB
|
||||
5, // ASTC_2D_5X5_UNORM
|
||||
5, // ASTC_2D_5X5_SRGB
|
||||
8, // ASTC_2D_10X8_UNORM
|
||||
8, // ASTC_2D_10X8_SRGB
|
||||
6, // ASTC_2D_6X6_UNORM
|
||||
6, // ASTC_2D_6X6_SRGB
|
||||
6, // ASTC_2D_10X6_UNORM
|
||||
6, // ASTC_2D_10X6_SRGB
|
||||
5, // ASTC_2D_10X5_UNORM
|
||||
5, // ASTC_2D_10X5_SRGB
|
||||
10, // ASTC_2D_10X10_UNORM
|
||||
10, // ASTC_2D_10X10_SRGB
|
||||
10, // ASTC_2D_12X10_UNORM
|
||||
10, // ASTC_2D_12X10_SRGB
|
||||
12, // ASTC_2D_12X12_UNORM
|
||||
12, // ASTC_2D_12X12_SRGB
|
||||
6, // ASTC_2D_8X6_UNORM
|
||||
6, // ASTC_2D_8X6_SRGB
|
||||
5, // ASTC_2D_6X5_UNORM
|
||||
5, // ASTC_2D_6X5_SRGB
|
||||
1, // E5B9G9R9_FLOAT
|
||||
1, // D32_FLOAT
|
||||
1, // D16_UNORM
|
||||
1, // X8_D24_UNORM
|
||||
1, // S8_UINT
|
||||
1, // D24_UNORM_S8_UINT
|
||||
1, // S8_UINT_D24_UNORM
|
||||
1, // D32_FLOAT_S8_UINT
|
||||
}};
|
||||
|
||||
constexpr u32 DefaultBlockHeight(PixelFormat format) {
|
||||
ASSERT(static_cast<std::size_t>(format) < BLOCK_HEIGHT_TABLE.size());
|
||||
return BLOCK_HEIGHT_TABLE[static_cast<std::size_t>(format)];
|
||||
constexpr u32 DefaultBlockHeight(PixelFormat format) noexcept {
|
||||
switch (format) {
|
||||
#define PIXEL_FORMAT_ELEM(name, width, height, bits) case PixelFormat::name: return height;
|
||||
PIXEL_FORMAT_LIST
|
||||
#undef PIXEL_FORMAT_ELEM
|
||||
default: UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
constexpr std::array<u8, MaxPixelFormat> BITS_PER_BLOCK_TABLE = {{
|
||||
32, // A8B8G8R8_UNORM
|
||||
32, // A8B8G8R8_SNORM
|
||||
32, // A8B8G8R8_SINT
|
||||
32, // A8B8G8R8_UINT
|
||||
16, // R5G6B5_UNORM
|
||||
16, // B5G6R5_UNORM
|
||||
16, // A1R5G5B5_UNORM
|
||||
32, // A2B10G10R10_UNORM
|
||||
32, // A2B10G10R10_UINT
|
||||
32, // A2R10G10B10_UNORM
|
||||
16, // A1B5G5R5_UNORM
|
||||
16, // A5B5G5R1_UNORM
|
||||
8, // R8_UNORM
|
||||
8, // R8_SNORM
|
||||
8, // R8_SINT
|
||||
8, // R8_UINT
|
||||
64, // R16G16B16A16_FLOAT
|
||||
64, // R16G16B16A16_UNORM
|
||||
64, // R16G16B16A16_SNORM
|
||||
64, // R16G16B16A16_SINT
|
||||
64, // R16G16B16A16_UINT
|
||||
32, // B10G11R11_FLOAT
|
||||
128, // R32G32B32A32_UINT
|
||||
64, // BC1_RGBA_UNORM
|
||||
128, // BC2_UNORM
|
||||
128, // BC3_UNORM
|
||||
64, // BC4_UNORM
|
||||
64, // BC4_SNORM
|
||||
128, // BC5_UNORM
|
||||
128, // BC5_SNORM
|
||||
128, // BC7_UNORM
|
||||
128, // BC6H_UFLOAT
|
||||
128, // BC6H_SFLOAT
|
||||
128, // ASTC_2D_4X4_UNORM
|
||||
32, // B8G8R8A8_UNORM
|
||||
128, // R32G32B32A32_FLOAT
|
||||
128, // R32G32B32A32_SINT
|
||||
64, // R32G32_FLOAT
|
||||
64, // R32G32_SINT
|
||||
32, // R32_FLOAT
|
||||
16, // R16_FLOAT
|
||||
16, // R16_UNORM
|
||||
16, // R16_SNORM
|
||||
16, // R16_UINT
|
||||
16, // R16_SINT
|
||||
32, // R16G16_UNORM
|
||||
32, // R16G16_FLOAT
|
||||
32, // R16G16_UINT
|
||||
32, // R16G16_SINT
|
||||
32, // R16G16_SNORM
|
||||
96, // R32G32B32_FLOAT
|
||||
32, // A8B8G8R8_SRGB
|
||||
16, // R8G8_UNORM
|
||||
16, // R8G8_SNORM
|
||||
16, // R8G8_SINT
|
||||
16, // R8G8_UINT
|
||||
64, // R32G32_UINT
|
||||
64, // R16G16B16X16_FLOAT
|
||||
32, // R32_UINT
|
||||
32, // R32_SINT
|
||||
128, // ASTC_2D_8X8_UNORM
|
||||
128, // ASTC_2D_8X5_UNORM
|
||||
128, // ASTC_2D_5X4_UNORM
|
||||
32, // B8G8R8A8_SRGB
|
||||
64, // BC1_RGBA_SRGB
|
||||
128, // BC2_SRGB
|
||||
128, // BC3_SRGB
|
||||
128, // BC7_UNORM
|
||||
16, // A4B4G4R4_UNORM
|
||||
8, // G4R4_UNORM
|
||||
128, // ASTC_2D_4X4_SRGB
|
||||
128, // ASTC_2D_8X8_SRGB
|
||||
128, // ASTC_2D_8X5_SRGB
|
||||
128, // ASTC_2D_5X4_SRGB
|
||||
128, // ASTC_2D_5X5_UNORM
|
||||
128, // ASTC_2D_5X5_SRGB
|
||||
128, // ASTC_2D_10X8_UNORM
|
||||
128, // ASTC_2D_10X8_SRGB
|
||||
128, // ASTC_2D_6X6_UNORM
|
||||
128, // ASTC_2D_6X6_SRGB
|
||||
128, // ASTC_2D_10X6_UNORM
|
||||
128, // ASTC_2D_10X6_SRGB
|
||||
128, // ASTC_2D_10X5_UNORM
|
||||
128, // ASTC_2D_10X5_SRGB
|
||||
128, // ASTC_2D_10X10_UNORM
|
||||
128, // ASTC_2D_10X10_SRGB
|
||||
128, // ASTC_2D_12X10_UNORM
|
||||
128, // ASTC_2D_12X10_SRGB
|
||||
128, // ASTC_2D_12X12_UNORM
|
||||
128, // ASTC_2D_12X12_SRGB
|
||||
128, // ASTC_2D_8X6_UNORM
|
||||
128, // ASTC_2D_8X6_SRGB
|
||||
128, // ASTC_2D_6X5_UNORM
|
||||
128, // ASTC_2D_6X5_SRGB
|
||||
32, // E5B9G9R9_FLOAT
|
||||
32, // D32_FLOAT
|
||||
16, // D16_UNORM
|
||||
32, // X8_D24_UNORM
|
||||
8, // S8_UINT
|
||||
32, // D24_UNORM_S8_UINT
|
||||
32, // S8_UINT_D24_UNORM
|
||||
64, // D32_FLOAT_S8_UINT
|
||||
}};
|
||||
|
||||
constexpr u32 BitsPerBlock(PixelFormat format) {
|
||||
ASSERT(static_cast<std::size_t>(format) < BITS_PER_BLOCK_TABLE.size());
|
||||
return BITS_PER_BLOCK_TABLE[static_cast<std::size_t>(format)];
|
||||
constexpr u32 BitsPerBlock(PixelFormat format) noexcept {
|
||||
switch (format) {
|
||||
#define PIXEL_FORMAT_ELEM(name, width, height, bits) case PixelFormat::name: return bits;
|
||||
PIXEL_FORMAT_LIST
|
||||
#undef PIXEL_FORMAT_ELEM
|
||||
default: UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
#undef PIXEL_FORMAT_LIST
|
||||
|
||||
/// Returns the sizer in bytes of the specified pixel format
|
||||
constexpr u32 BytesPerBlock(PixelFormat pixel_format) {
|
||||
return BitsPerBlock(pixel_format) / CHAR_BIT;
|
||||
}
|
||||
|
||||
SurfaceTarget SurfaceTargetFromTextureType(Tegra::Texture::TextureType texture_type);
|
||||
|
||||
bool SurfaceTargetIsLayered(SurfaceTarget target);
|
||||
|
||||
bool SurfaceTargetIsArray(SurfaceTarget target);
|
||||
|
||||
PixelFormat PixelFormatFromDepthFormat(Tegra::DepthFormat format);
|
||||
|
||||
PixelFormat PixelFormatFromRenderTargetFormat(Tegra::RenderTargetFormat format);
|
||||
|
||||
PixelFormat PixelFormatFromGPUPixelFormat(Service::android::PixelFormat format);
|
||||
|
||||
SurfaceType GetFormatType(PixelFormat pixel_format);
|
||||
|
||||
bool HasAlpha(PixelFormat pixel_format);
|
||||
|
||||
bool IsPixelFormatASTC(PixelFormat format);
|
||||
|
||||
bool IsPixelFormatBCn(PixelFormat format);
|
||||
|
||||
bool IsPixelFormatSRGB(PixelFormat format);
|
||||
|
||||
bool IsPixelFormatInteger(PixelFormat format);
|
||||
|
||||
bool IsPixelFormatSignedInteger(PixelFormat format);
|
||||
|
||||
size_t PixelComponentSizeBitsInteger(PixelFormat format);
|
||||
|
||||
std::pair<u32, u32> GetASTCBlockSize(PixelFormat format);
|
||||
|
||||
u64 TranscodedAstcSize(u64 base_size, PixelFormat format);
|
||||
|
||||
} // namespace VideoCore::Surface
|
||||
|
||||
Reference in New Issue
Block a user