Skip to content
Closed
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: 4 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -4947,7 +4947,10 @@ struct entry { // 10 uints = 40 bytes
first layer, whose slot records that image as its owner so another dimension's image with the same slot number does
not inherit it). One layout serves the device: any module whose meta says `tagged` makes the table tagged, because
the kind word is additive (a plain module ignores the word and reads the index, which the tagged layout keeps at
the same place), so a plain majority never turns it off; no tagged module is the old plain layout.
the same place), so a plain majority never turns it off; no tagged module is the old plain layout. With no
library loaded at all (the GPU tests and tools, which bring their own modules) the table is tagged, the default
of `tools/rdna2spirv.py --descriptor-table` (`kNoLibraryDescriptorTableLayout`, kept equal to it by
`tests/test_descriptor_table_default.py`); a plain table there gave `cs_texture_uav` an all-zero kind word.
`LEONIDA_DESCRIPTOR_TABLE=plain|tagged` overrides, and the runtime says so loudly when an override forces plain
while tagged modules exist.
- The layer must keep the table sparse enough that no live key needs more than eight probes. When all eight slots are occupied,
Expand Down
15 changes: 10 additions & 5 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15609,12 +15609,14 @@ class Device final
// each index and uses it to tell which array that index is bound in (a 3D T# read by a 2D op is not another
// image), and the inserts below fill it. A plain library reads only the index, exactly as before. The kind
// word is additive, so one tagged module needs the tagged layout whatever the plain count (the library's
// vote); chosen once here, like the hash kind, and LEONIDA_DESCRIPTOR_TABLE overrides.
// vote); chosen once here, like the hash kind, and LEONIDA_DESCRIPTOR_TABLE overrides. With no library the
// modules come from the caller (GPU tests, tools), translated with rdna2spirv.py's default layout.
leonida::DescriptorTableCensus table_modules{};
leonida::DescriptorTableLayout descriptor_table = leonida::DescriptorTableLayout::plain;
if (const leonida::ShaderLibrary* library = state_.library()) {
table_modules = library->descriptor_table_census();
descriptor_table = library->descriptor_table_layout();
const leonida::ShaderLibrary* table_library = state_.library();
leonida::DescriptorTableLayout descriptor_table = leonida::kNoLibraryDescriptorTableLayout;
if (table_library) {
table_modules = table_library->descriptor_table_census();
descriptor_table = table_library->descriptor_table_layout();
}
leonida::DescriptorTableLayout override_table = descriptor_table;
if (leonida::descriptor_table_layout_from_env(std::getenv("LEONIDA_DESCRIPTOR_TABLE"), &override_table)) {
Expand All @@ -15635,6 +15637,9 @@ class Device final
std::fprintf(stderr, "[rt] d3d12: descriptor table: %zu tagged / %zu plain modules; using %s\n",
table_modules.tagged, table_modules.plain,
leonida::descriptor_table_layout_name(descriptor_table));
} else if (!table_library) {
std::fprintf(stderr, "[rt] d3d12: descriptor table: %s (no shader library; the translator's default)\n",
leonida::descriptor_table_layout_name(descriptor_table));
} else {
std::fprintf(stderr, "[rt] d3d12: descriptor table: %s (all %zu modules)\n",
leonida::descriptor_table_layout_name(descriptor_table), table_modules.total());
Expand Down
7 changes: 7 additions & 0 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,13 @@ inline DescriptorTableLayout majority_descriptor_table_layout(const DescriptorTa
return census.tagged > 0 ? DescriptorTableLayout::tagged : DescriptorTableLayout::plain;
}

// The layout with no library loaded (the GPU tests and tools create pipelines from modules they bring themselves):
// tools/rdna2spirv.py's default --descriptor-table, which is what those modules were translated with. Tagged is
// also safe for a plain module, which reads the index and ignores the kind word. A plain default handed every
// tagged module an all-zero kind, so cs_texture_uav.spv stored to the fallback image. Kept equal to the translator
// by tests/test_descriptor_table_default.py.
inline constexpr DescriptorTableLayout kNoLibraryDescriptorTableLayout = DescriptorTableLayout::tagged;

// LEONIDA_DESCRIPTOR_TABLE=plain|tagged overrides the modules' vote. False when `value` is null, empty or not one
// of the two layouts (an unrecognised value is ignored rather than failing startup).
inline bool descriptor_table_layout_from_env(const char* value, DescriptorTableLayout* layout) {
Expand Down
5 changes: 5 additions & 0 deletions runtime/test/shader_library_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,11 @@ int main() {
CHECK(!descriptor_table_layout_from_env("Plain", &chosen));
CHECK(!descriptor_table_layout_from_env("other", &chosen));
CHECK(chosen == DescriptorTableLayout::tagged);

// No library loaded (the GPU tests, the tools): the translator's default layout, tagged, as
// tools/rdna2spirv.py DESCRIPTOR_TABLE (tests/test_descriptor_table_default.py keeps the two equal). A plain
// table there handed cs_texture_uav.spv an all-zero kind, so its stores went to the fallback image.
CHECK(kNoLibraryDescriptorTableLayout == DescriptorTableLayout::tagged);
std::filesystem::remove_all(dir);
}

Expand Down
64 changes: 64 additions & 0 deletions tests/test_descriptor_table_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""The runtime's descriptor-table layout with no shader library loaded is the translator's default.

`runtime/shims/d3d12_vulkan_device.cpp` takes the layout from the loaded library's vote. With no library (the GPU
tests, the tools) it uses `kNoLibraryDescriptorTableLayout` (`runtime/src/shader_library.h`). The modules those
paths create pipelines from are translated with `tools/rdna2spirv.py`'s default `--descriptor-table`, so the two
must name the same layout: when the runtime said plain and the translator tagged, `cs_texture_uav.spv` read an
all-zero kind word and its stores went to the fallback image.

Reads both files as text; needs no build, GPU or game files.
"""

import os
import re
import unittest

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def read(*parts):
with open(os.path.join(ROOT, *parts), encoding="utf-8") as f:
return f.read()


def runtime_no_library_layout(header):
m = re.search(r"kNoLibraryDescriptorTableLayout\s*=\s*DescriptorTableLayout::(\w+)\s*;", header)
return m.group(1) if m else None


def translator_defaults(source):
constant = re.search(r'^DESCRIPTOR_TABLE\s*=\s*"(\w+)"', source, re.M)
option = re.search(r'"--descriptor-table"\s*,\s*choices=DESCRIPTOR_TABLES\s*,\s*default=("(\w+)"|DESCRIPTOR_TABLE)',
source)
constant = constant.group(1) if constant else None
if not option:
return constant, None
return constant, option.group(2) if option.group(2) else constant


class DescriptorTableDefault(unittest.TestCase):
def test_runtime_matches_translator(self):
runtime = runtime_no_library_layout(read("runtime", "src", "shader_library.h"))
constant, option = translator_defaults(read("tools", "rdna2spirv.py"))
self.assertIsNotNone(runtime, "kNoLibraryDescriptorTableLayout not found in runtime/src/shader_library.h")
self.assertIsNotNone(constant, "DESCRIPTOR_TABLE not found in tools/rdna2spirv.py")
self.assertIsNotNone(option, "--descriptor-table's default not found in tools/rdna2spirv.py")
self.assertEqual(constant, option, "rdna2spirv.py's DESCRIPTOR_TABLE and --descriptor-table default differ")
self.assertEqual(runtime, constant,
"the runtime's no-library descriptor table must be the translator's default layout")

def test_parsers(self):
self.assertEqual(runtime_no_library_layout(
"inline constexpr DescriptorTableLayout kNoLibraryDescriptorTableLayout = DescriptorTableLayout::plain;"),
"plain")
self.assertIsNone(runtime_no_library_layout("DescriptorTableLayout descriptor_table = plain;"))
self.assertEqual(translator_defaults('DESCRIPTOR_TABLE = "plain"\n'
'ap.add_argument("--descriptor-table", choices=DESCRIPTOR_TABLES, '
'default=DESCRIPTOR_TABLE)'), ("plain", "plain"))
self.assertEqual(translator_defaults('DESCRIPTOR_TABLE = "tagged"\n'
'ap.add_argument("--descriptor-table", choices=DESCRIPTOR_TABLES, '
'default="plain")'), ("tagged", "plain"))


if __name__ == "__main__":
unittest.main()