Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2967,6 +2967,11 @@ if(TARGET Vulkan::Headers)
add_executable(gfx10_descriptors_test test/gfx10_descriptors_test.cpp)
target_link_libraries(gfx10_descriptors_test PRIVATE Vulkan::Headers)
add_test(NAME gfx10_descriptors COMMAND gfx10_descriptors_test)
# CPU cost of the GFX10 format-table lookups an SRV makes, indexed against the scans they replaced; ctest runs
# its self-test (both agree on every format and code), run it without arguments for the numbers.
add_executable(gfx10_format_bench test/gfx10_format_bench.cpp)
target_link_libraries(gfx10_format_bench PRIVATE Vulkan::Headers)
add_test(NAME gfx10_format_bench_selftest COMMAND gfx10_format_bench --self-test)
# CPU cost of the D3D12 -> Vulkan replay per command (record, prepare, emit) with no-op Vulkan entry points:
# no GPU or driver. ctest runs its quick self-test; run it without arguments for the numbers.
add_executable(replay_cpu_bench test/replay_cpu_bench.cpp)
Expand Down
61 changes: 56 additions & 5 deletions runtime/src/gfx10_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,54 @@ inline constexpr FormatRow format_table[] = {
// The format of the console's separate stencil surface: what a T# of a depth-stencil texture's stencil plane names.
inline constexpr std::uint32_t stencil_format = 5; // GFX10_FORMAT_8_UINT

// The table indexed both ways, built at compile time, so the lookups below cost one load instead of a scan of
// the table (they run for every view a descriptor names: CreateShaderResourceView, the T# -> set 0 binding).
// Each keeps the scan's answer: the first row in table order. Codes are 9 bits (T# word 1 bits 28:20); the
// Vulkan formats of the core range are below 256, and the few above it (VK_EXT_4444_formats) fall back to the scan.
namespace detail {
inline constexpr std::uint8_t no_row = 0xFF;
inline constexpr std::uint32_t code_count = 512, dense_vulkan_formats = 256;
static_assert(std::size(format_table) < no_row, "a row index must fit in a byte");

struct FormatIndex {
std::array<std::uint8_t, dense_vulkan_formats> by_vulkan{}; // first row of a format
std::array<std::uint8_t, code_count> first{}; // first row of a code
std::array<std::uint8_t, code_count> count{}; // rows of the code, all adjacent to `first`
};

inline constexpr FormatIndex build_format_index() {
FormatIndex index{};
index.by_vulkan.fill(no_row);
index.first.fill(no_row);
for (std::size_t i = 0; i < std::size(format_table); ++i) {
const FormatRow& row = format_table[i];
if (std::uint32_t(row.vulkan) < dense_vulkan_formats && index.by_vulkan[row.vulkan] == no_row)
index.by_vulkan[row.vulkan] = std::uint8_t(i);
if (index.first[row.gfx10] == no_row) index.first[row.gfx10] = std::uint8_t(i);
++index.count[row.gfx10];
}
return index;
}
inline constexpr FormatIndex format_index = build_format_index();

// The rows of one code are adjacent, so [first, first + count) is every row of the code, in table order.
inline constexpr bool codes_adjacent() {
for (std::size_t i = 0; i < std::size(format_table); ++i) {
const std::uint32_t code = format_table[i].gfx10;
if (code >= code_count) return false;
if (i < format_index.first[code] || i >= std::size_t(format_index.first[code]) + format_index.count[code])
return false;
}
return true;
}
static_assert(codes_adjacent(), "keep the rows of a GFX10 code together in format_table");
} // namespace detail

inline constexpr const FormatRow* format_row(VkFormat format) {
if (std::uint32_t(format) < detail::dense_vulkan_formats) {
const std::uint8_t i = detail::format_index.by_vulkan[std::uint32_t(format)];
return i == detail::no_row ? nullptr : &format_table[i];
}
for (const FormatRow& row : format_table)
if (row.vulkan == format) return &row;
return nullptr;
Expand All @@ -334,9 +381,9 @@ inline constexpr const FormatRow* format_row(VkFormat format) {
// The Vulkan format with the code's bits, in the hardware's channel order where Vulkan has such a format.
// VK_FORMAT_UNDEFINED: not in the table. A code that depth and colour formats share gives the colour format.
inline constexpr VkFormat vulkan_format(std::uint32_t gfx10_format) {
for (const FormatRow& row : format_table)
if (row.gfx10 == gfx10_format) return row.vulkan;
return VK_FORMAT_UNDEFINED;
if (gfx10_format >= detail::code_count) return VK_FORMAT_UNDEFINED;
const std::uint8_t i = detail::format_index.first[gfx10_format];
return i == detail::no_row ? VK_FORMAT_UNDEFINED : format_table[i].vulkan;
}

// 0 (GFX10_FORMAT_INVALID): not in the table.
Expand Down Expand Up @@ -380,8 +427,12 @@ inline constexpr VkFormat vulkan_view_format(VkFormat image_format, std::uint32_
if (image->gfx10 == gfx10_format) return image_format;
if (is_depth_stencil_format(image_format)) return VK_FORMAT_UNDEFINED;
const FormatRow* fallback = nullptr;
for (const FormatRow& row : format_table) {
if (row.gfx10 != gfx10_format || is_depth_stencil_format(row.vulkan)) continue;
if (gfx10_format >= detail::code_count) return VK_FORMAT_UNDEFINED;
const std::size_t first = detail::format_index.first[gfx10_format];
const std::size_t end = first + detail::format_index.count[gfx10_format];
for (std::size_t i = first; i < end; ++i) { // empty for a code not in the table
const FormatRow& row = format_table[i];
if (is_depth_stencil_format(row.vulkan)) continue;
if (row.channels == image->channels) return row.vulkan;
if (!fallback) fallback = &row;
}
Expand Down
39 changes: 38 additions & 1 deletion runtime/test/gfx10_descriptors_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,45 @@ int main() {
}
}

// The indexed lookups give the first row in table order, as a scan would, for every format and code; the
// codes are 9 bits, the formats above the dense range (VK_EXT_4444_formats) and unknown ones still resolve.
for (std::size_t i = 0; i < std::size(format_table); ++i) {
const FormatRow& row = format_table[i];
const FormatRow* first_of_format = nullptr;
const FormatRow* first_of_code = nullptr;
for (const FormatRow& other : format_table) {
if (!first_of_format && other.vulkan == row.vulkan) first_of_format = &other;
if (!first_of_code && other.gfx10 == row.gfx10) first_of_code = &other;
}
CHECK(format_row(row.vulkan) == first_of_format);
CHECK(vulkan_format(row.gfx10) == first_of_code->vulkan);
}
for (std::uint32_t code = 0; code < 1024; ++code) {
bool listed = false;
for (const FormatRow& row : format_table) listed = listed || row.gfx10 == code;
if (!listed) CHECK(vulkan_format(code) == VK_FORMAT_UNDEFINED);
if (!listed) CHECK(vulkan_view_format(VK_FORMAT_R8G8B8A8_UNORM, code, true) == VK_FORMAT_UNDEFINED);
}
for (std::uint32_t f = 0; f < 1024; ++f) {
bool listed = false;
for (const FormatRow& row : format_table) listed = listed || std::uint32_t(row.vulkan) == f;
if (!listed) CHECK(format_row(VkFormat(f)) == nullptr);
}
CHECK(vulkan_format(5) == VK_FORMAT_R8_UINT); // not S8_UINT, the second row of the code
CHECK(vulkan_format(22) == VK_FORMAT_R32_SFLOAT);
CHECK(vulkan_format(0xFFFFFFFFu) == VK_FORMAT_UNDEFINED);
CHECK(format_row(VK_FORMAT_A4R4G4B4_UNORM_PACK16) && format_row(VK_FORMAT_A4R4G4B4_UNORM_PACK16)->gfx10 == 136);
CHECK(format_from_vulkan(VK_FORMAT_A4B4G4R4_UNORM_PACK16) == 136);
CHECK(format_row(VkFormat(1000340002)) == nullptr);
CHECK(format_row(VkFormat(0x7FFFFFFF)) == nullptr);
CHECK(vulkan_view_format(VK_FORMAT_A4R4G4B4_UNORM_PACK16, 136) == VK_FORMAT_A4R4G4B4_UNORM_PACK16);
CHECK(vulkan_view_format(VK_FORMAT_R4G4B4A4_UNORM_PACK16, 136) == VK_FORMAT_R4G4B4A4_UNORM_PACK16);
CHECK(vulkan_view_format(VK_FORMAT_B8G8R8A8_UNORM, 130) == VK_FORMAT_B8G8R8A8_SRGB); // second row of 130
CHECK(vulkan_view_format(VK_FORMAT_R8G8B8A8_UNORM, 0x1FF, true) == VK_FORMAT_UNDEFINED);
CHECK(vulkan_view_format(VK_FORMAT_R8G8B8A8_UNORM, 0xFFFFFFFFu, true) == VK_FORMAT_UNDEFINED);

// DST_SEL as a component mapping.
const auto same = [](const VkComponentMapping& m, VkComponentSwizzle r, VkComponentSwizzle g, VkComponentSwizzle b,
const auto same =[](const VkComponentMapping& m, VkComponentSwizzle r, VkComponentSwizzle g, VkComponentSwizzle b,
VkComponentSwizzle a) { return m.r == r && m.g == g && m.b == b && m.a == a; };
constexpr auto R = VK_COMPONENT_SWIZZLE_R, G = VK_COMPONENT_SWIZZLE_G, B = VK_COMPONENT_SWIZZLE_B,
A = VK_COMPONENT_SWIZZLE_A, zero = VK_COMPONENT_SWIZZLE_ZERO, one = VK_COMPONENT_SWIZZLE_ONE;
Expand Down
232 changes: 232 additions & 0 deletions runtime/test/gfx10_format_bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// CPU benchmark of the GFX10 format-table lookups (src/gfx10_descriptors.h): format_row, vulkan_format,
// format_from_vulkan, format_swizzle and vulkan_view_format, which run for every view a descriptor names
// (CreateShaderResourceView / CreateUnorderedAccessView encode a T#, and binding a T# into set 0 picks its view
// format and component mapping under the descriptor lock). It times the indexed lookups against the linear scans of
// the table they replaced, kept here as the reference, on the sequence of calls one SRV makes, over a mix of the
// formats a frame's textures use. No GPU, driver or game files.
//
// Usage:
// gfx10_format_bench [--iters N] [--reps R] [--self-test]
// --self-test checks that both give the same answers for every format and code, quickly, and prints no timings.
#ifndef VK_NO_PROTOTYPES
#define VK_NO_PROTOTYPES
#endif
#include "../src/gfx10_descriptors.h"

#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>

namespace g = leonida::gfx10;

namespace reference { // the lookups as they were: a scan of format_table each

const g::FormatRow* format_row(VkFormat format) {
for (const g::FormatRow& row : g::format_table)
if (row.vulkan == format) return &row;
return nullptr;
}
VkFormat vulkan_format(std::uint32_t code) {
for (const g::FormatRow& row : g::format_table)
if (row.gfx10 == code) return row.vulkan;
return VK_FORMAT_UNDEFINED;
}
std::uint32_t format_from_vulkan(VkFormat format) {
const g::FormatRow* row = format_row(format);
return row ? row->gfx10 : 0;
}
g::Swizzle format_swizzle(VkFormat format) {
const g::FormatRow* row = format_row(format);
return row ? row->channels : g::identity_swizzle;
}
VkFormat vulkan_view_format(VkFormat image_format, std::uint32_t code, bool allow_channel_cast) {
const g::FormatRow* image = format_row(image_format);
if (!image) return VK_FORMAT_UNDEFINED;
if (image->gfx10 == code) return image_format;
if (g::is_depth_stencil_format(image_format)) return VK_FORMAT_UNDEFINED;
const g::FormatRow* fallback = nullptr;
for (const g::FormatRow& row : g::format_table) {
if (row.gfx10 != code || g::is_depth_stencil_format(row.vulkan)) continue;
if (row.channels == image->channels) return row.vulkan;
if (!fallback) fallback = &row;
}
return allow_channel_cast && fallback ? fallback->vulkan : VK_FORMAT_UNDEFINED;
}
VkComponentMapping vulkan_swizzle(const g::Swizzle& swizzle, VkFormat view_format) {
const g::Swizzle channels = format_swizzle(view_format);
const auto component = [&](std::uint8_t select) {
if (select == g::select_one) return VK_COMPONENT_SWIZZLE_ONE;
if (select >= g::select_x && select <= g::select_w)
for (unsigned i = 0; i < 4; ++i)
if (channels[i] == select) return VkComponentSwizzle(VK_COMPONENT_SWIZZLE_R + i);
return VK_COMPONENT_SWIZZLE_ZERO;
};
return {component(swizzle[0]), component(swizzle[1]), component(swizzle[2]), component(swizzle[3])};
}

} // namespace reference

namespace {

// Formats probed: every one in the table, the ones around them, and a few that are not there.
std::vector<VkFormat> probe_formats() {
std::vector<VkFormat> out;
for (std::uint32_t f = 0; f < 400; ++f) out.push_back(VkFormat(f));
for (const g::FormatRow& row : g::format_table) out.push_back(row.vulkan);
for (const std::uint32_t f : {1000340000u, 1000340001u, 1000340002u, 1000156000u, 1000066000u, 0x7FFFFFFFu,
0x80000000u, 0xFFFFFFFFu})
out.push_back(VkFormat(f));
return out;
}

bool same(const g::Swizzle& a, const g::Swizzle& b) { return a == b; }

// Every format and code, both ways; 0 when they agree.
int self_test() {
const std::vector<VkFormat> formats = probe_formats();
for (const VkFormat f : formats) {
if (g::format_row(f) != reference::format_row(f)) {
std::fprintf(stderr, "format_row(%u) differs\n", unsigned(f));
return 1;
}
if (g::format_from_vulkan(f) != reference::format_from_vulkan(f) ||
!same(g::format_swizzle(f), reference::format_swizzle(f))) {
std::fprintf(stderr, "format_from_vulkan/format_swizzle(%u) differs\n", unsigned(f));
return 1;
}
}
std::vector<std::uint32_t> codes;
for (std::uint32_t c = 0; c < 1100; ++c) codes.push_back(c);
for (const std::uint32_t c : {0x7FFFFFFFu, 0x80000000u, 0xFFFFFFFFu}) codes.push_back(c);
for (const std::uint32_t c : codes) {
if (g::vulkan_format(c) != reference::vulkan_format(c)) {
std::fprintf(stderr, "vulkan_format(%u) differs\n", c);
return 1;
}
}
std::size_t pairs = 0;
for (const VkFormat f : formats)
for (const std::uint32_t c : codes)
for (const bool cast : {false, true}) {
++pairs;
if (g::vulkan_view_format(f, c, cast) != reference::vulkan_view_format(f, c, cast)) {
std::fprintf(stderr, "vulkan_view_format(%u, %u, %d) differs\n", unsigned(f), c, int(cast));
return 1;
}
}
std::printf("gfx10_format_bench self-test: %zu formats, %zu codes, %zu view-format queries agree\n",
formats.size(), codes.size(), pairs);
return 0;
}

// What one SRV of each texture does (d3d12_vulkan_device.cpp, image_descriptor and bind_image): the code of the
// named format, the view format of the image for that code, the channels of the view format, and the mapping.
// Formats as a frame mixes them: block-compressed albedo and normals, render targets, depth, a few odd ones.
struct View {
VkFormat image; // the resource's format
VkFormat named; // the view's format
};
const View mix[] = {
{VK_FORMAT_BC7_SRGB_BLOCK, VK_FORMAT_BC7_SRGB_BLOCK},
{VK_FORMAT_BC7_UNORM_BLOCK, VK_FORMAT_BC7_SRGB_BLOCK},
{VK_FORMAT_BC1_RGBA_SRGB_BLOCK, VK_FORMAT_BC1_RGBA_SRGB_BLOCK},
{VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_BC5_UNORM_BLOCK},
{VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_BC4_UNORM_BLOCK},
{VK_FORMAT_BC3_UNORM_BLOCK, VK_FORMAT_BC3_UNORM_BLOCK},
{VK_FORMAT_BC6H_UFLOAT_BLOCK, VK_FORMAT_BC6H_UFLOAT_BLOCK},
{VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB},
{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM},
{VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT},
{VK_FORMAT_B10G11R11_UFLOAT_PACK32, VK_FORMAT_B10G11R11_UFLOAT_PACK32},
{VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_A2B10G10R10_UNORM_PACK32},
{VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT},
{VK_FORMAT_D32_SFLOAT, VK_FORMAT_R32_SFLOAT},
{VK_FORMAT_R16G16_SFLOAT, VK_FORMAT_R16G16_SFLOAT},
{VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM},
{VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT},
{VK_FORMAT_R8G8B8A8_UINT, VK_FORMAT_R32_UINT},
};

template<typename Lookups>
std::uint64_t run(std::size_t iters, Lookups&& lookups) {
std::uint64_t sink = 0;
for (std::size_t i = 0; i < iters; ++i) {
const View& v = mix[i % std::size(mix)];
sink += lookups(v);
}
return sink;
}

struct Indexed {
std::uint64_t operator()(const View& v) const {
const std::uint32_t code = g::format_from_vulkan(v.named);
const VkFormat view = g::vulkan_view_format(v.image, code, true);
const g::Swizzle channels = g::format_swizzle(view);
const VkComponentMapping m = g::vulkan_swizzle(g::identity_swizzle, view);
return std::uint64_t(code) + std::uint64_t(view) + channels[0] + std::uint64_t(m.r) +
std::uint64_t(g::vulkan_format(code));
}
};
struct Scanned {
std::uint64_t operator()(const View& v) const {
const std::uint32_t code = reference::format_from_vulkan(v.named);
const VkFormat view = reference::vulkan_view_format(v.image, code, true);
const g::Swizzle channels = reference::format_swizzle(view);
const VkComponentMapping m = reference::vulkan_swizzle(g::identity_swizzle, view);
return std::uint64_t(code) + std::uint64_t(view) + channels[0] + std::uint64_t(m.r) +
std::uint64_t(reference::vulkan_format(code));
}
};

template<typename Lookups>
double median_ns(std::size_t iters, std::size_t reps, Lookups lookups, std::uint64_t& sink) {
std::vector<double> times;
for (std::size_t r = 0; r < reps + 2; ++r) {
const auto start = std::chrono::steady_clock::now();
sink += run(iters, lookups);
const auto stop = std::chrono::steady_clock::now();
if (r >= 2) times.push_back(std::chrono::duration<double, std::nano>(stop - start).count() / double(iters));
}
std::sort(times.begin(), times.end());
return times[times.size() / 2];
}

int usage() {
std::fprintf(stderr, "usage: gfx10_format_bench [--iters N] [--reps R] [--self-test]\n");
return 2;
}

} // namespace

int main(int argc, char** argv) {
std::size_t iters = 2000000, reps = 15;
bool self = false;
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
if (arg == "--self-test") self = true;
else if (arg == "--iters" && i + 1 < argc) iters = std::strtoull(argv[++i], nullptr, 10);
else if (arg == "--reps" && i + 1 < argc) reps = std::strtoull(argv[++i], nullptr, 10);
else return usage();
}
if (const int r = self_test(); r || self) return r;
if (!iters || !reps) return usage();

std::uint64_t sink_indexed = 0, sink_scanned = 0;
const double scanned = median_ns(iters, reps, Scanned{}, sink_scanned);
const double indexed = median_ns(iters, reps, Indexed{}, sink_indexed);
// Both compute the same values for the mix, so the sums agree.
if (sink_indexed != sink_scanned) {
std::fprintf(stderr, "the two lookups disagree on the benchmark mix\n");
return 1;
}
std::printf("per SRV view (format_from_vulkan + vulkan_view_format + format_swizzle + vulkan_swizzle + "
"vulkan_format), median of %zu runs of %zu:\n", reps, iters);
std::printf(" table scan %8.2f ns\n", scanned);
std::printf(" indexed %8.2f ns (%.1fx, %.0f%% less)\n", indexed, scanned / indexed,
100.0 * (1.0 - indexed / scanned));
return 0;
}