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
2 changes: 2 additions & 0 deletions core/kyty/graphics/host_gpu/graphicContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct GraphicContext {
bool maintenance8_enabled = false;
bool robust_buffer_access_enabled = false;
bool ray_query_enabled = false;
bool compute_derivative_quads_enabled = false;
bool compute_derivative_linear_enabled = false;
bool r8_srgb_views = false;
bool r8g8_srgb_views = false;
bool r4g4b4a4_render_targets = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ ShaderRecompiler::Spirv::Profile SpirvProfile(const GraphicContext& graphics) {
.r8g8_srgb_views = graphics.r8g8_srgb_views,
.relaxed_float = Config::RelaxedShaderFloatEnabled(),
.ray_query = graphics.ray_query_enabled,
.compute_derivative_quads = graphics.compute_derivative_quads_enabled,
.compute_derivative_linear = graphics.compute_derivative_linear_enabled,
.lds_lane_sync = LdsLaneSyncEnabled(),
.native_float16 = graphics.shader_float16_enabled,
.f32_product_no_contraction = F32ProductNoContractionEnabled(),
Expand Down
4 changes: 3 additions & 1 deletion core/kyty/graphics/presentation/window/vulkanWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics,
"Some games may render incorrectly or crash.\n");
}
graphics.shader_image_int64_atomics_enabled = image_atomic_int64.shaderImageInt64Atomics;
graphics.compute_derivative_quads_enabled = compute_derivatives.computeDerivativeGroupQuads == VK_TRUE;
graphics.compute_derivative_linear_enabled = compute_derivatives.computeDerivativeGroupLinear == VK_TRUE;
#if defined(ANYPS5_KYTY)
if (PipelineLibrariesWanted() && extended_dynamic_state &&
HasExtension(available_extensions, VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME) &&
Expand Down Expand Up @@ -800,7 +802,7 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics,
if (!graphics.conditional_rendering_enabled) {
create.unlink<vk::PhysicalDeviceConditionalRenderingFeaturesEXT>();
}
if (!compute_derivatives.computeDerivativeGroupQuads) {
if (!graphics.compute_derivative_quads_enabled && !graphics.compute_derivative_linear_enabled) {
create.unlink<vk::PhysicalDeviceComputeShaderDerivativesFeaturesKHR>();
}
if (!extended_dynamic_state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct Profile {
bool r8g8_srgb_views = false;
bool relaxed_float = false;
bool ray_query = false;
bool compute_derivative_quads = true;
bool compute_derivative_linear = true;
bool lds_lane_sync = true;
bool native_float16 = true;
bool f32_product_no_contraction = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,10 +1085,6 @@ void DefineModule(EmitterState& state) {
state.builder.RequireCapability(spv::CapabilityGroupNonUniformBallot);
state.builder.RequireCapability(spv::CapabilityGroupNonUniformShuffle);
}
if (state.program.info.compute_derivatives && state.program.stage == ShaderType::Compute) {
state.builder.RequireCapability(spv::CapabilityComputeDerivativeGroupQuadsKHR);
state.builder.RequireExtension("SPV_KHR_compute_shader_derivatives");
}
const bool fragment_barycentric =
state.program.stage == ShaderType::Pixel &&
std::any_of(state.inputs.begin(), state.inputs.end(), [](const InputBinding& input) {
Expand Down Expand Up @@ -1124,6 +1120,10 @@ void DefineModule(EmitterState& state) {
local_x = cs->threads_num[0] != 0u ? cs->threads_num[0] : local_x;
local_y = cs->threads_num[1] != 0u ? cs->threads_num[1] : local_y;
local_z = cs->threads_num[2] != 0u ? cs->threads_num[2] : local_z;
if (state.program.stage == ShaderType::Compute && state.program.info.compute_derivatives &&
(uint64_t {local_x} * local_y * local_z) % 4u != 0u) {
EXIT("compute derivatives require four-lane workgroups (got %ux%ux%u)", local_x, local_y, local_z);
}
if (state.lane_count == 2) {
local_x = ((local_x * local_y * local_z + 63u) / 64u) * 32u;
local_y = local_z = 1u;
Expand All @@ -1134,6 +1134,21 @@ void DefineModule(EmitterState& state) {
}
state.builder.AddExecutionMode(state.main_func, spv::ExecutionModeLocalSize, local_x,
local_y, local_z);
if (state.program.stage == ShaderType::Compute && state.program.info.compute_derivatives) {
const bool quads_layout = (local_x & 1u) == 0u && (local_y & 1u) == 0u;
const bool linear_layout = (uint64_t {local_x} * local_y * local_z) % 4u == 0u;
if (quads_layout && state.profile.compute_derivative_quads) {
state.builder.RequireCapability(spv::CapabilityComputeDerivativeGroupQuadsKHR);
state.builder.AddExecutionMode(state.main_func, spv::ExecutionModeDerivativeGroupQuadsKHR);
} else if (linear_layout && state.profile.compute_derivative_linear) {
state.builder.RequireCapability(spv::CapabilityComputeDerivativeGroupLinearKHR);
state.builder.AddExecutionMode(state.main_func, spv::ExecutionModeDerivativeGroupLinearKHR);
} else {
EXIT("compute derivative group feature unavailable for local size %ux%ux%u", local_x,
local_y, local_z);
}
state.builder.RequireExtension("SPV_KHR_compute_shader_derivatives");
}
}
if (state.program.stage == ShaderType::Pixel) {
state.builder.AddExecutionMode(state.main_func, spv::ExecutionModeOriginUpperLeft);
Expand All @@ -1155,9 +1170,6 @@ void DefineModule(EmitterState& state) {
}
#endif
}
if (state.program.info.compute_derivatives && state.program.stage == ShaderType::Compute) {
state.builder.AddExecutionMode(state.main_func, spv::ExecutionModeDerivativeGroupQuadsKHR);
}
state.builder.AddName(state.main_func, "main");
if (state.program.info.function_lds) {
state.builder.AddName(state.lds_variable, "lds_dwords");
Expand Down
2 changes: 1 addition & 1 deletion core/kyty/graphics/shader/recompiler/ir/Serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ KYTY_IO_FIELDS(BdaReadWord, a, b)
KYTY_IO_FIELDS(BdaReadSpan, a, b, c, d, e)
KYTY_IO_FIELDS(BdaReadPlan, a, b)
KYTY_IO_FIELDS(CompiledShaderInfo, a, b, c, d, e, f, g, h, i, j, k)
KYTY_IO_FIELDS(Spirv::Profile, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)
KYTY_IO_FIELDS(Spirv::Profile, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)
#if defined(ANYPS5_KYTY)
KYTY_IO_FIELDS(ShaderTessellationInputInfo, a, b, c, d, e, f, g, h, i, j)
#else
Expand Down
5 changes: 4 additions & 1 deletion core/kyty/tools/SpirvMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ bool ReadDump(const std::filesystem::path& path, Dump& dump) {
template <size_t Flags>
using EarlierProfile = std::tuple<bool, bool, uint32_t, std::array<bool, Flags>>;

using PreviousProfile = std::tuple<bool, bool, uint32_t, std::array<bool, 12>, uint32_t>;

bool ReadStoreHeader(std::istream& input) {
input.seekg(0, std::ios::end);
const auto end = input.tellg();
Expand All @@ -67,7 +69,8 @@ bool ReadStoreHeader(std::istream& input) {
input.seekg(size);
return bool(input);
};
return try_profile(ShaderRecompiler::Spirv::Profile{}) || try_profile(EarlierProfile<9>{}) ||
return try_profile(ShaderRecompiler::Spirv::Profile{}) || try_profile(PreviousProfile{}) ||
try_profile(EarlierProfile<9>{}) ||
try_profile(EarlierProfile<8>{}) || try_profile(EarlierProfile<7>{}) || try_profile(EarlierProfile<6>{});
}

Expand Down
1 change: 1 addition & 0 deletions core/kyty/tools/tests/SpirvMatch.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ string(CONCAT both "${vertex}"
"input=3 stage=pixel shader=0000000000000103 module=0000000000000300 variant=standard\n")
check_match(0 "${both}" "" fixture.shaders "モジュール.spv" plain.spv ps.spv)
check_match(0 "${vertex}" "" legacy.shaders vs.spv)
check_match(0 "${vertex}" "" previous.shaders vs.spv)
check_match(1 "${vertex}input=2 no match\n" "" fixture.shaders vs.spv missing.spv)
check_match(2 "" "Invalid or unreadable SPIR-V input 2\n" fixture.shaders vs.spv invalid.spv)
check_match(2 "" "Invalid or unreadable Kyty shader store header\n" bad-header.shaders vs.spv)
Expand Down
10 changes: 9 additions & 1 deletion core/kyty/tools/tests/SpirvMatchFixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ int main() {
Write(legacy, old_header.bytes);
WriteModule<LegacyWriter>(legacy, 0x102, 0x201, ShaderType::Vertex, vertex, {});
WriteModule<LegacyWriter>(legacy, 0x101, 0x201, ShaderType::Vertex, vertex, {});
std::ofstream previous("previous.shaders", std::ios::binary);
Writer previous_header;
const uint32_t all_stages = ~0u;
IoAll(previous_header, stamp, off, off, clock_period, off, off, off, off, off,
on, on, on, on, on, on, on, all_stages, shared_memory);
Write(previous, previous_header.bytes);
WriteModule(previous, 0x102, 0x201, ShaderType::Vertex, vertex, {});
WriteModule(previous, 0x101, 0x201, ShaderType::Vertex, vertex, {});

std::ofstream malformed("malformed.shaders", std::ios::binary);
Write(malformed, header.bytes);
Expand All @@ -93,5 +101,5 @@ int main() {
WriteDump("plain.spv", plain);
WriteDump("missing.spv", missing);
WriteDump("invalid.spv", {0, 0, 0, 0, 0});
return !store || !legacy || !malformed || !bad_header;
return !store || !legacy || !previous || !malformed || !bad_header;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ inline void ReportInvalid(const std::string& message, std::span<const uint32_t>
}

inline void Run(std::span<const uint32_t> original, uint8_t mode, uint8_t shape,
std::string* refusal = nullptr, bool* emitted = nullptr) {
std::string* refusal = nullptr, bool* emitted = nullptr,
const Recompiler::Spirv::Profile* device_profile = nullptr,
std::vector<uint32_t>* spirv_output = nullptr, uint32_t compute_local_x = 0u) {
if (emitted != nullptr) *emitted = false;
static const auto profile = Profile();
std::vector<uint32_t> storage(original.begin(), original.end());
Expand All @@ -84,7 +86,7 @@ inline void Run(std::span<const uint32_t> original, uint8_t mode, uint8_t shape,
options.emit_debug_info = false;
options.native_ray_tracing = true;
options.ray_stats = false;
options.profile = &profile;
options.profile = device_profile != nullptr ? device_profile : &profile;
const std::vector<uint32_t> user_data(16u + (shape & 15u), 0u);
options.user_data = user_data;

Expand All @@ -107,7 +109,7 @@ inline void Run(std::span<const uint32_t> original, uint8_t mode, uint8_t shape,
default:
compute.wave_size = wave;
compute.host_subgroup_size = 32;
compute.threads_num[0] = 1u << (shape & 3u);
compute.threads_num[0] = compute_local_x != 0u ? compute_local_x : 1u << (shape & 3u);
compute.threads_num[1] = 1u << ((shape >> 2) & 3u);
compute.threads_num[2] = 1;
compute.thread_ids_num = 2;
Expand Down Expand Up @@ -137,6 +139,7 @@ inline void Run(std::span<const uint32_t> original, uint8_t mode, uint8_t shape,
if (!IR::ResourceSpecializationFits(translated.program, specialization)) return;
auto compiled = Recompiler::CompileProgram(std::move(translated), options, specialization, 0);
if (emitted != nullptr) *emitted = !compiled.spirv.empty();
if (spirv_output != nullptr) *spirv_output = compiled.spirv;
const char* policy = std::getenv("KYTY_FUZZ_SPIRV");
if (!compiled.spirv.empty() && policy != nullptr && std::strcmp(policy, "off") != 0 && policy[0] != 0) {
std::string message;
Expand Down
33 changes: 26 additions & 7 deletions core/libs/prx/libSceAgcDriver/KytyShim/tests/HalfProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ void Require(bool condition, const char* message) {
std::exit(1);
}

std::vector<uint8_t> Header(bool native) {
struct Features {
bool native;
bool quads;
bool linear;
};

std::vector<uint8_t> Header(Features features) {
ShaderRecompiler::Spirv::Profile profile;
profile.native_float16 = native;
profile.native_float16 = features.native;
profile.compute_derivative_quads = features.quads;
profile.compute_derivative_linear = features.linear;
const uint64_t stamp = 1234;
const uint32_t limit = 65536;
ShaderRecompiler::IR::Writer writer;
Expand All @@ -27,19 +35,30 @@ std::vector<uint8_t> Header(bool native) {

int main() {
Require(ShaderRecompiler::Spirv::DefaultProfile.native_float16, "the default changed the native shader path");
const auto native = Header(true);
const auto software = Header(false);
Require(ShaderRecompiler::Spirv::DefaultProfile.compute_derivative_quads &&
ShaderRecompiler::Spirv::DefaultProfile.compute_derivative_linear,
"the default changed the derivative shader path");
const auto native = Header({true, true, true});
const auto software = Header({false, true, true});
const auto without_quads = Header({true, false, true});
const auto without_linear = Header({true, true, false});
Require(native != software, "native and software-half stores have the same header");
for (bool expected : {false, true}) {
Require(native != without_quads && native != without_linear,
"derivative feature profiles have the same store header");
const Features cases[] = {{false, true, true}, {true, true, true},
{true, false, true}, {true, true, false}};
for (const auto& expected : cases) {
const auto bytes = Header(expected);
ShaderRecompiler::IR::Reader reader {bytes};
uint64_t stamp = 0;
uint32_t limit = 0;
ShaderRecompiler::Spirv::Profile profile;
ShaderStoreFormat::IoStoreHeader(reader, stamp, profile, limit);
Require(reader.ok && reader.bytes.empty(), "a profile header did not decode exactly");
Require(stamp == 1234 && limit == 65536 && profile.native_float16 == expected,
"a store header lost its half-precision profile");
Require(stamp == 1234 && limit == 65536 && profile.native_float16 == expected.native &&
profile.compute_derivative_quads == expected.quads &&
profile.compute_derivative_linear == expected.linear,
"a store header lost its shader feature profile");
ShaderRecompiler::IR::Reader truncated {std::span(bytes).first(bytes.size() - 1)};
ShaderStoreFormat::IoStoreHeader(truncated, stamp, profile, limit);
Require(!truncated.ok, "a truncated profile header was accepted");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ShaderRecompilerRun.hpp"
#include "graphics/shader/recompiler/frontend/decode/ShaderDecoder.h"
#include "spirv/unified1/spirv.hpp"

#include <cstdint>
#include <cstdio>
Expand All @@ -15,9 +16,26 @@ struct Case {
uint8_t shape;
std::vector<uint32_t> words;
bool requires_spirv = false;
uint32_t execution_mode = 0;
bool quads_supported = true;
};

const std::vector<Case> Cases = {
{"compute-image-lod-with-2x2-workgroup", 0x01, 0x05,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u}, true,
spv::ExecutionModeDerivativeGroupQuadsKHR},
{"compute-image-lod-with-4x2-workgroup", 0x01, 0x06,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u}, true,
spv::ExecutionModeDerivativeGroupQuadsKHR},
{"compute-image-lod-with-8x1-workgroup", 0x01, 0x03,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u}, true,
spv::ExecutionModeDerivativeGroupLinearKHR},
{"compute-image-lod-with-8x1-wave64-host32", 0x00, 0x03,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u}, true,
spv::ExecutionModeDerivativeGroupQuadsKHR},
{"compute-image-lod-with-2x2-without-quads-feature", 0x01, 0x05,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u}, true,
spv::ExecutionModeDerivativeGroupLinearKHR, false},
{"lds-append-in-a-vertex-shader", 0x03, 0x00, {0xd8f80000u, 0u, 0xbf810000u}, true},
{"lds-consume-in-a-vertex-shader", 0x03, 0x00, {0xd8f40000u, 0u, 0xbf810000u}, true},
{"lds-append-in-a-pixel-shader", 0x04, 0x00, {0xd8f80000u, 0u, 0xbf810000u}, true},
Expand Down Expand Up @@ -47,9 +65,20 @@ struct Refusal {
uint8_t shape;
std::vector<uint32_t> words;
const char* reason;
bool linear_supported = true;
uint32_t compute_local_x = 0u;
};

const std::vector<Refusal> Refusals = {
{"compute-image-lod-with-1x1-workgroup", 0x01, 0x00,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u},
"compute derivatives require four-lane workgroups"},
{"compute-image-lod-with-3x1-workgroup", 0x01, 0x00,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u},
"compute derivatives require four-lane workgroups", true, 3u},
{"compute-image-lod-with-8x1-without-linear-feature", 0x01, 0x03,
{0xf1800000u, 0x00000000u, 0xe8040000u, 0x00000000u, 0xbf810000u},
"compute derivative group feature unavailable", false},
{"vop3p-without-its-second-word", 0x3b, 0x07, {0x520a0001u, 0xce600001u}, "the code ends after"},
{"literal-constant-without-its-word", 0x00, 0x00, {0x7e0002ffu}, "the code ends after"},
{"vop1-dpp-without-its-modifier-word", 0x00, 0x00, {0x7e0002fau}, "the code ends after"},
Expand Down Expand Up @@ -91,6 +120,19 @@ const std::vector<ModeWrite> ModeWrites = {
{0xb9001801u, 0x00u},
};

bool HasExecutionMode(const std::vector<uint32_t>& words, uint32_t expected) {
for (size_t offset = 5; offset < words.size();) {
const uint32_t count = words[offset] >> 16u;
if (count == 0u || count > words.size() - offset) return false;
if ((words[offset] & 0xffffu) == spv::OpExecutionMode && count >= 3u &&
words[offset + 2u] == expected) {
return true;
}
offset += count;
}
return false;
}

bool FloatModeBits() {
namespace Decoder = Libs::Graphics::ShaderRecompiler::Decoder;
bool ok = true;
Expand Down Expand Up @@ -159,18 +201,29 @@ int main(int argc, char** argv) {
if (argc >= 2 && std::string_view(argv[1]) != entry.name) continue;
found = true;
bool emitted = false;
KytyFuzz::Run(entry.words, entry.mode, entry.shape, nullptr, &emitted);
std::vector<uint32_t> spirv;
auto profile = KytyFuzz::Profile();
profile.compute_derivative_quads = entry.quads_supported;
KytyFuzz::Run(entry.words, entry.mode, entry.shape, nullptr, &emitted, &profile,
entry.execution_mode == 0u ? nullptr : &spirv);
if (entry.requires_spirv && !emitted) {
std::printf("%s: FAILED, did not emit SPIR-V\n", entry.name);
return 1;
}
if (entry.execution_mode != 0u && !HasExecutionMode(spirv, entry.execution_mode)) {
std::printf("%s: FAILED, missing execution mode %u\n", entry.name, entry.execution_mode);
return 1;
}
std::printf("%s: ok\n", entry.name);
}
for (const auto& entry: Refusals) {
if (argc >= 2 && std::string_view(argv[1]) != entry.name) continue;
found = true;
std::string refusal;
KytyFuzz::Run(entry.words, entry.mode, entry.shape, &refusal);
auto profile = KytyFuzz::Profile();
profile.compute_derivative_linear = entry.linear_supported;
KytyFuzz::Run(entry.words, entry.mode, entry.shape, &refusal, nullptr, &profile,
nullptr, entry.compute_local_x);
if (refusal.find(entry.reason) == std::string::npos) {
std::printf("%s: FAILED, refusal \"%s\" instead of \"%s\"\n", entry.name, refusal.c_str(), entry.reason);
return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# kyty

## add Functional

- [Compute quad derivatives](../../../core/kyty/graphics/shader/recompiler/backend/spirv/spirvEmitterModule.cpp) retain `DerivativeGroupQuadsKHR` on compatible 2D layouts and use `DerivativeGroupLinearKHR` for other complete four-lane groups when the device enables it. Incomplete guest groups and unavailable device features are refused. Whether Linear better matches console lane order for every compatible 2D layout remains unverified. Closes when: lane-order behavior is measured across layouts and a valid fallback for unsupported devices is tested.
Loading