[Vulkan] Initial support for mipmaps
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "xenia/base/logging.h"
|
||||
#include "xenia/base/math.h"
|
||||
#include "xenia/base/memory.h"
|
||||
|
||||
#include "third_party/xxhash/xxhash.h"
|
||||
|
||||
@@ -59,6 +60,8 @@ bool TextureInfo::Prepare(const xe_gpu_texture_fetch_t& fetch,
|
||||
info.endianness = static_cast<Endian>(fetch.endianness);
|
||||
info.is_tiled = fetch.tiled;
|
||||
info.has_packed_mips = fetch.packed_mips;
|
||||
info.mip_address = fetch.mip_address << 12;
|
||||
info.mip_levels = fetch.packed_mips ? fetch.mip_max_level + 1 : 1;
|
||||
info.input_length = 0; // Populated below.
|
||||
|
||||
if (info.format_info()->format == TextureFormat::kUnknown) {
|
||||
@@ -78,6 +81,7 @@ bool TextureInfo::Prepare(const xe_gpu_texture_fetch_t& fetch,
|
||||
} break;
|
||||
case Dimension::k3D: {
|
||||
// TODO(benvanik): calculate size.
|
||||
assert_always();
|
||||
return false;
|
||||
}
|
||||
case Dimension::kCube: {
|
||||
@@ -106,6 +110,8 @@ bool TextureInfo::PrepareResolve(uint32_t physical_address,
|
||||
info.endianness = endian;
|
||||
info.is_tiled = true;
|
||||
info.has_packed_mips = false;
|
||||
info.mip_address = 0;
|
||||
info.mip_levels = 1;
|
||||
info.input_length = 0;
|
||||
|
||||
if (info.format_info()->format == TextureFormat::kUnknown) {
|
||||
@@ -121,10 +127,6 @@ void TextureInfo::CalculateTextureSizes2D(uint32_t width, uint32_t height) {
|
||||
size_2d.logical_width = width;
|
||||
size_2d.logical_height = height;
|
||||
|
||||
// Here be dragons. The values here are used in texture_cache.cc to copy
|
||||
// images and create GL textures. Changes here will impact that code.
|
||||
// TODO(benvanik): generic texture copying utility.
|
||||
|
||||
auto format = format_info();
|
||||
|
||||
// w/h in blocks.
|
||||
@@ -135,11 +137,15 @@ void TextureInfo::CalculateTextureSizes2D(uint32_t width, uint32_t height) {
|
||||
xe::round_up(size_2d.logical_height, format->block_height) /
|
||||
format->block_height;
|
||||
|
||||
// Tiles are 32x32 blocks. The pitch of all textures must a multiple of tile
|
||||
// dimensions.
|
||||
uint32_t tile_width = xe::round_up(block_width, 32) / 32;
|
||||
size_2d.block_width = tile_width * 32;
|
||||
size_2d.block_height = block_height;
|
||||
if (is_tiled) {
|
||||
// If the texture is tiled, its dimensions must be a multiple of tile
|
||||
// dimensions (32x32 blocks).
|
||||
size_2d.block_width = xe::round_up(block_width, 32);
|
||||
size_2d.block_height = xe::round_up(block_height, 32);
|
||||
} else {
|
||||
size_2d.block_width = block_width;
|
||||
size_2d.block_height = block_height;
|
||||
}
|
||||
|
||||
uint32_t bytes_per_block =
|
||||
format->block_width * format->block_height * format->bits_per_pixel / 8;
|
||||
@@ -177,11 +183,15 @@ void TextureInfo::CalculateTextureSizesCube(uint32_t width, uint32_t height,
|
||||
xe::round_up(size_cube.logical_height, format->block_height) /
|
||||
format->block_height;
|
||||
|
||||
// Tiles are 32x32 blocks. All textures must be multiples of tile dimensions.
|
||||
uint32_t tile_width = xe::round_up(block_width, 32) / 32;
|
||||
uint32_t tile_height = xe::round_up(block_height, 32) / 32;
|
||||
size_cube.block_width = tile_width * 32;
|
||||
size_cube.block_height = tile_height * 32;
|
||||
if (is_tiled) {
|
||||
// If the texture is tiled, its dimensions must be a multiple of tile
|
||||
// dimensions (32x32 blocks).
|
||||
size_cube.block_width = xe::round_up(block_width, 32);
|
||||
size_cube.block_height = xe::round_up(block_height, 32);
|
||||
} else {
|
||||
size_cube.block_width = block_width;
|
||||
size_cube.block_height = block_height;
|
||||
}
|
||||
|
||||
uint32_t bytes_per_block =
|
||||
format->block_width * format->block_height * format->bits_per_pixel / 8;
|
||||
@@ -204,12 +214,154 @@ void TextureInfo::CalculateTextureSizesCube(uint32_t width, uint32_t height,
|
||||
input_length = size_cube.input_face_length * 6;
|
||||
}
|
||||
|
||||
static void TextureSwap(Endian endianness, void* dest, const void* src,
|
||||
size_t length) {
|
||||
switch (endianness) {
|
||||
case Endian::k8in16:
|
||||
xe::copy_and_swap_16_unaligned(dest, src, length / 2);
|
||||
break;
|
||||
case Endian::k8in32:
|
||||
xe::copy_and_swap_32_unaligned(dest, src, length / 4);
|
||||
break;
|
||||
case Endian::k16in32: // Swap high and low 16 bits within a 32 bit word
|
||||
xe::copy_and_swap_16_in_32_unaligned(dest, src, length);
|
||||
break;
|
||||
default:
|
||||
case Endian::kUnspecified:
|
||||
std::memcpy(dest, src, length);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void ConvertTexelCTX1(uint8_t* dest, size_t dest_pitch,
|
||||
const uint8_t* src, Endian src_endianness) {
|
||||
// http://fileadmin.cs.lth.se/cs/Personal/Michael_Doggett/talks/unc-xenos-doggett.pdf
|
||||
union {
|
||||
uint8_t data[8];
|
||||
struct {
|
||||
uint8_t r0, g0, r1, g1;
|
||||
uint32_t xx;
|
||||
};
|
||||
} block;
|
||||
static_assert(sizeof(block) == 8, "CTX1 block mismatch");
|
||||
|
||||
const uint32_t bytes_per_block = 8;
|
||||
TextureSwap(src_endianness, block.data, src, bytes_per_block);
|
||||
|
||||
uint8_t cr[4] = {
|
||||
block.r0, block.r1,
|
||||
static_cast<uint8_t>(2.f / 3.f * block.r0 + 1.f / 3.f * block.r1),
|
||||
static_cast<uint8_t>(1.f / 3.f * block.r0 + 2.f / 3.f * block.r1)};
|
||||
uint8_t cg[4] = {
|
||||
block.g0, block.g1,
|
||||
static_cast<uint8_t>(2.f / 3.f * block.g0 + 1.f / 3.f * block.g1),
|
||||
static_cast<uint8_t>(1.f / 3.f * block.g0 + 2.f / 3.f * block.g1)};
|
||||
|
||||
for (uint32_t oy = 0; oy < 4; ++oy) {
|
||||
for (uint32_t ox = 0; ox < 4; ++ox) {
|
||||
uint8_t xx = (block.xx >> (((ox + (oy * 4)) * 2))) & 3;
|
||||
dest[(oy * dest_pitch) + (ox * 2) + 0] = cr[xx];
|
||||
dest[(oy * dest_pitch) + (ox * 2) + 1] = cg[xx];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextureInfo::ConvertTiled(uint8_t* dest, const uint8_t* src, Endian endian,
|
||||
const FormatInfo* format_info, uint32_t offset_x,
|
||||
uint32_t offset_y, uint32_t block_pitch,
|
||||
uint32_t width, uint32_t height,
|
||||
uint32_t output_width) {
|
||||
// TODO(benvanik): optimize this inner loop (or work by tiles).
|
||||
uint32_t bytes_per_block = format_info->block_width *
|
||||
format_info->block_height *
|
||||
format_info->bits_per_pixel / 8;
|
||||
|
||||
uint32_t output_pitch =
|
||||
output_width * format_info->block_width * format_info->bits_per_pixel / 8;
|
||||
|
||||
uint32_t output_row_height = 1;
|
||||
if (format_info->format == TextureFormat::k_CTX1) {
|
||||
// TODO: Can we calculate this?
|
||||
output_row_height = 4;
|
||||
}
|
||||
|
||||
// logical w/h in blocks.
|
||||
uint32_t block_width =
|
||||
xe::round_up(width, format_info->block_width) / format_info->block_width;
|
||||
uint32_t block_height = xe::round_up(height, format_info->block_height) /
|
||||
format_info->block_height;
|
||||
|
||||
// Bytes per pixel
|
||||
auto log2_bpp =
|
||||
(bytes_per_block / 4) + ((bytes_per_block / 2) >> (bytes_per_block / 4));
|
||||
|
||||
// Offset to the current row, in bytes.
|
||||
uint32_t output_row_offset = 0;
|
||||
for (uint32_t y = 0; y < block_height; y++) {
|
||||
auto input_row_offset =
|
||||
TextureInfo::TiledOffset2DOuter(offset_y + y, block_pitch, log2_bpp);
|
||||
|
||||
// Go block-by-block on this row.
|
||||
uint32_t output_offset = output_row_offset;
|
||||
for (uint32_t x = 0; x < block_width; x++) {
|
||||
auto input_offset = TextureInfo::TiledOffset2DInner(
|
||||
offset_x + x, offset_y + y, log2_bpp, input_row_offset);
|
||||
input_offset >>= log2_bpp;
|
||||
|
||||
if (format_info->format == TextureFormat::k_CTX1) {
|
||||
// Convert to R8G8.
|
||||
ConvertTexelCTX1(&dest[output_offset], output_pitch, src, endian);
|
||||
} else {
|
||||
// Generic swap to destination.
|
||||
TextureSwap(endian, dest + output_offset,
|
||||
src + input_offset * bytes_per_block, bytes_per_block);
|
||||
}
|
||||
|
||||
output_offset += bytes_per_block;
|
||||
}
|
||||
|
||||
output_row_offset += output_pitch * output_row_height;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t TextureInfo::GetMaxMipLevels(uint32_t width, uint32_t height,
|
||||
uint32_t depth) {
|
||||
return 1 + xe::log2_floor(std::max({width, height, depth}));
|
||||
}
|
||||
|
||||
bool TextureInfo::GetPackedTileOffset(const TextureInfo& texture_info,
|
||||
uint32_t TextureInfo::GetMipLocation(const TextureInfo& src, uint32_t mip,
|
||||
uint32_t* offset_x, uint32_t* offset_y) {
|
||||
if (mip == 0) {
|
||||
// Short-circuit. Mip 0 is always stored in guest_address.
|
||||
GetPackedTileOffset(src, offset_x, offset_y);
|
||||
return src.guest_address;
|
||||
}
|
||||
|
||||
// Walk forward to find the address of the mip
|
||||
// If the texture is <= 16 pixels w/h, the mips are packed with the base
|
||||
// texture. Otherwise, they're stored beginning from mip_address.
|
||||
uint32_t address_base = std::min(src.width, src.height) < 16
|
||||
? src.guest_address
|
||||
: src.mip_address;
|
||||
uint32_t address_offset = 0;
|
||||
|
||||
for (uint32_t i = 1; i < mip; i++) {
|
||||
uint32_t logical_width = std::max((src.width + 1) >> mip, 1u);
|
||||
uint32_t logical_height = std::max((src.height + 1) >> mip, 1u);
|
||||
if (std::min(logical_width, logical_height) <= 16) {
|
||||
// We've reached the point where the mips are packed into a single tile.
|
||||
// TODO(DrChat): Figure out how to calculate the packed tile offset.
|
||||
continue;
|
||||
}
|
||||
|
||||
address_offset += src.input_length >> (i * 2);
|
||||
}
|
||||
|
||||
return address_base + address_offset;
|
||||
}
|
||||
|
||||
bool TextureInfo::GetPackedTileOffset(uint32_t width, uint32_t height,
|
||||
const FormatInfo* format_info,
|
||||
uint32_t* out_offset_x,
|
||||
uint32_t* out_offset_y) {
|
||||
// Tile size is 32x32, and once textures go <=16 they are packed into a
|
||||
@@ -231,6 +383,13 @@ bool TextureInfo::GetPackedTileOffset(const TextureInfo& texture_info,
|
||||
// This only works for square textures, or textures that are some non-pot
|
||||
// <= square. As soon as the aspect ratio goes weird, the textures start to
|
||||
// stretch across tiles.
|
||||
//
|
||||
// The 2x2 and 1x1 squares are packed in their specific positions because
|
||||
// each square is the size of at least one block (which is 4x4 pixels max)
|
||||
// 4x4: x = 4
|
||||
// 2x2: y = (x & 0x3) << 2
|
||||
// 1x1: y = (x & 0x3) << 2
|
||||
//
|
||||
// if (tile_aligned(w) > tile_aligned(h)) {
|
||||
// // wider than tall, so packed horizontally
|
||||
// } else if (tile_aligned(w) < tile_aligned(h)) {
|
||||
@@ -243,16 +402,14 @@ bool TextureInfo::GetPackedTileOffset(const TextureInfo& texture_info,
|
||||
// The minimum dimension is what matters most: if either width or height
|
||||
// is <= 16 this mode kicks in.
|
||||
|
||||
if (std::min(texture_info.size_2d.logical_width,
|
||||
texture_info.size_2d.logical_height) > 16) {
|
||||
if (std::min(width, height) > 16) {
|
||||
// Too big, not packed.
|
||||
*out_offset_x = 0;
|
||||
*out_offset_y = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (xe::log2_ceil(texture_info.size_2d.logical_width) >
|
||||
xe::log2_ceil(texture_info.size_2d.logical_height)) {
|
||||
if (xe::log2_ceil(width) > xe::log2_ceil(height)) {
|
||||
// Wider than tall. Laid out vertically.
|
||||
*out_offset_x = 0;
|
||||
*out_offset_y = 16;
|
||||
@@ -261,26 +418,37 @@ bool TextureInfo::GetPackedTileOffset(const TextureInfo& texture_info,
|
||||
*out_offset_x = 16;
|
||||
*out_offset_y = 0;
|
||||
}
|
||||
*out_offset_x /= texture_info.format_info()->block_width;
|
||||
*out_offset_y /= texture_info.format_info()->block_height;
|
||||
|
||||
*out_offset_x /= format_info->block_width;
|
||||
*out_offset_y /= format_info->block_height;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TextureInfo::GetPackedTileOffset(const TextureInfo& texture_info,
|
||||
uint32_t* out_offset_x,
|
||||
uint32_t* out_offset_y) {
|
||||
return GetPackedTileOffset(
|
||||
texture_info.size_2d.logical_width, texture_info.size_2d.logical_height,
|
||||
texture_info.format_info(), out_offset_x, out_offset_y);
|
||||
}
|
||||
|
||||
// https://github.com/BinomialLLC/crunch/blob/ea9b8d8c00c8329791256adafa8cf11e4e7942a2/inc/crn_decomp.h#L4108
|
||||
uint32_t TextureInfo::TiledOffset2DOuter(uint32_t y, uint32_t width,
|
||||
uint32_t log_bpp) {
|
||||
uint32_t macro = ((y >> 5) * (width >> 5)) << (log_bpp + 7);
|
||||
uint32_t micro = ((y & 6) << 2) << log_bpp;
|
||||
return macro + ((micro & ~15) << 1) + (micro & 15) +
|
||||
((y & 8) << (3 + log_bpp)) + ((y & 1) << 4);
|
||||
uint32_t log2_bpp) {
|
||||
uint32_t macro = ((y / 32) * (width / 32)) << (log2_bpp + 7);
|
||||
uint32_t micro = ((y & 6) << 2) << log2_bpp;
|
||||
return macro + ((micro & ~0xF) << 1) + (micro & 0xF) +
|
||||
((y & 8) << (3 + log2_bpp)) + ((y & 1) << 4);
|
||||
}
|
||||
|
||||
uint32_t TextureInfo::TiledOffset2DInner(uint32_t x, uint32_t y, uint32_t bpp,
|
||||
uint32_t TextureInfo::TiledOffset2DInner(uint32_t x, uint32_t y,
|
||||
uint32_t log2_bpp,
|
||||
uint32_t base_offset) {
|
||||
uint32_t macro = (x >> 5) << (bpp + 7);
|
||||
uint32_t micro = (x & 7) << bpp;
|
||||
uint32_t offset = base_offset + (macro + ((micro & ~15) << 1) + (micro & 15));
|
||||
return ((offset & ~511) << 3) + ((offset & 448) << 2) + (offset & 63) +
|
||||
uint32_t macro = (x / 32) << (log2_bpp + 7);
|
||||
uint32_t micro = (x & 7) << log2_bpp;
|
||||
uint32_t offset =
|
||||
base_offset + (macro + ((micro & ~0xF) << 1) + (micro & 0xF));
|
||||
return ((offset & ~0x1FF) << 3) + ((offset & 0x1C0) << 2) + (offset & 0x3F) +
|
||||
((y & 16) << 7) + (((((y & 8) >> 2) + (x >> 3)) & 3) << 6);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user