From 2e11c5a156eea519ff751ebb10e007410c08ad53 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Fri, 13 Nov 2026 18:31:00 -0500 Subject: [PATCH] fix(venus): keep the SPIR-V cap valid and scoped to active layers Cap inline compute pipeline code too, leave the shader hooks off when the layer is turned off, strip clip and cull distances only where they are emulated, and leave modules alone whose capabilities, decorations or OpSelect results need a newer version or an undeclared extension. --- docs/development/mali-venus.md | 12 +- tools/tests/mali_compat_spirv_test.c | 78 +++++++++ tools/tests/test_mali_compat_spirv.py | 9 +- tools/venus/layer/layer.c | 5 +- tools/venus/layer/mali_compat.h | 5 + tools/venus/layer/pipeline.c | 50 ++++-- tools/venus/layer/spirv.c | 218 ++++++++++++++++++++++---- 7 files changed, 333 insertions(+), 44 deletions(-) diff --git a/docs/development/mali-venus.md b/docs/development/mali-venus.md index d87f9b3b72..5094c593de 100644 --- a/docs/development/mali-venus.md +++ b/docs/development/mali-venus.md @@ -133,9 +133,15 @@ The SPIR-V version the driver gets: global variable, not only the input and output ones. - The layer therefore lowers every module to SPIR-V 1.3 before the driver sees it: the version word, the interface list (back to the input and output variables) and `OpTerminateInvocation` - (SPIR-V 1.6's name for `OpKill`). A module that uses something 1.3 cannot express (a - `ShaderViewportIndex` capability, the Vulkan memory model, `OpCopyLogical`) is left alone and - the layer says so in its log. + (SPIR-V 1.6's name for `OpKill`). This covers shader modules, and code given inline to + graphics and compute pipelines. +- A module that uses something 1.3 cannot express is left alone and the layer says so in its + log: a `ShaderViewportIndex` capability, the Vulkan memory model, `OpCopyLogical`, an + `OpSelect` of a structure or array, the newer loop controls and image operands, or a + capability, decoration or instruction that became core later without the extension 1.3 + needs for it (`ShaderNonUniform` without `SPV_EXT_descriptor_indexing`). +- The version cap runs on every device the layer is active on; clip and cull distances are + only taken out where the layer emulates them. `DROIDDECK_MALI_COMPAT=0` turns both off. - This is what Dawn ("Disable SPIR-V 1.4 on Android ARM"), ANGLE (`supportsSPIRV14`) and naga (`AVOID_SPIRV_COPY_LOGICAL`) do for a proprietary compiler they cannot trust. diff --git a/tools/tests/mali_compat_spirv_test.c b/tools/tests/mali_compat_spirv_test.c index 90a0e8d427..28b95eb6da 100644 --- a/tools/tests/mali_compat_spirv_test.c +++ b/tools/tests/mali_compat_spirv_test.c @@ -71,6 +71,60 @@ static const uint32_t zinklike[] = { }; static const size_t zinklike_count = sizeof(zinklike) / sizeof(uint32_t); +static const uint32_t nonuniform[] = { + 0x07230203, 0x00010600, 0x00070000, 0x00000009, 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x000014b5, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000001, 0x6e69616d, + 0x00000000, 0x00000002, 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, + 0x00000000, 0x00020013, 0x00000003, 0x00030016, 0x00000004, 0x00000020, 0x00040020, 0x00000005, + 0x00000003, 0x00000004, 0x0004003b, 0x00000005, 0x00000002, 0x00000003, 0x0004002b, 0x00000004, + 0x00000006, 0x3f800000, 0x00030021, 0x00000007, 0x00000003, 0x00050036, 0x00000003, 0x00000001, + 0x00000000, 0x00000007, 0x000200f8, 0x00000008, 0x0003003e, 0x00000002, 0x00000006, 0x000100fd, + 0x00010038, +}; +static const size_t nonuniform_count = sizeof(nonuniform) / sizeof(uint32_t); + +static const uint32_t nonuniform_extension[] = { + 0x07230203, 0x00010600, 0x00070000, 0x00000009, 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x000014b5, 0x0008000a, 0x5f565053, 0x5f545845, 0x63736564, 0x74706972, 0x695f726f, 0x7865646e, + 0x00676e69, 0x0003000e, 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000001, 0x6e69616d, + 0x00000000, 0x00000002, 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, + 0x00000000, 0x00020013, 0x00000003, 0x00030016, 0x00000004, 0x00000020, 0x00040020, 0x00000005, + 0x00000003, 0x00000004, 0x0004003b, 0x00000005, 0x00000002, 0x00000003, 0x0004002b, 0x00000004, + 0x00000006, 0x3f800000, 0x00030021, 0x00000007, 0x00000003, 0x00050036, 0x00000003, 0x00000001, + 0x00000000, 0x00000007, 0x000200f8, 0x00000008, 0x0003003e, 0x00000002, 0x00000006, 0x000100fd, + 0x00010038, +}; +static const size_t nonuniform_extension_count = sizeof(nonuniform_extension) / sizeof(uint32_t); + +static const uint32_t select_struct[] = { + 0x07230203, 0x00010600, 0x00070000, 0x00000011, 0x00000000, 0x00020011, 0x00000001, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000001, 0x6e69616d, 0x00000000, 0x00000002, + 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, 0x00000000, 0x00020013, + 0x00000003, 0x00020014, 0x00000004, 0x00030016, 0x00000005, 0x00000020, 0x0004001e, 0x00000006, + 0x00000005, 0x00000005, 0x00040020, 0x00000007, 0x00000003, 0x00000005, 0x0004003b, 0x00000007, + 0x00000002, 0x00000003, 0x0004002b, 0x00000005, 0x00000008, 0x3f800000, 0x0004002b, 0x00000005, + 0x00000009, 0x00000000, 0x00030029, 0x00000004, 0x0000000a, 0x0005002c, 0x00000006, 0x0000000b, + 0x00000008, 0x00000009, 0x0005002c, 0x00000006, 0x0000000c, 0x00000009, 0x00000008, 0x00030021, + 0x0000000d, 0x00000003, 0x00050036, 0x00000003, 0x00000001, 0x00000000, 0x0000000d, 0x000200f8, + 0x0000000e, 0x000600a9, 0x00000006, 0x0000000f, 0x0000000a, 0x0000000b, 0x0000000c, 0x00050051, + 0x00000005, 0x00000010, 0x0000000f, 0x00000000, 0x0003003e, 0x00000002, 0x00000010, 0x000100fd, + 0x00010038, +}; +static const size_t select_struct_count = sizeof(select_struct) / sizeof(uint32_t); + +static const uint32_t select_scalar[] = { + 0x07230203, 0x00010600, 0x00070000, 0x0000000d, 0x00000000, 0x00020011, 0x00000001, 0x0003000e, + 0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000001, 0x6e69616d, 0x00000000, 0x00000002, + 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, 0x00000000, 0x00020013, + 0x00000003, 0x00020014, 0x00000004, 0x00030016, 0x00000005, 0x00000020, 0x00040020, 0x00000006, + 0x00000003, 0x00000005, 0x0004003b, 0x00000006, 0x00000002, 0x00000003, 0x0004002b, 0x00000005, + 0x00000007, 0x3f800000, 0x0004002b, 0x00000005, 0x00000008, 0x00000000, 0x00030029, 0x00000004, + 0x00000009, 0x00030021, 0x0000000a, 0x00000003, 0x00050036, 0x00000003, 0x00000001, 0x00000000, + 0x0000000a, 0x000200f8, 0x0000000b, 0x000600a9, 0x00000005, 0x0000000c, 0x00000009, 0x00000007, + 0x00000008, 0x0003003e, 0x00000002, 0x0000000c, 0x000100fd, 0x00010038, +}; +static const size_t select_scalar_count = sizeof(select_scalar) / sizeof(uint32_t); + static int failures; static void check(int condition, const char *what) @@ -144,6 +198,30 @@ int main(int argc, char **argv) check(!spirv_cap_version(capability, capability_count, &kept), "a module with a capability from SPIR-V 1.5 is left alone"); check(!spirv_cap_version(memory, memory_count, &kept), "a module with the Vulkan memory model is left alone"); + check(!spirv_cap_version(nonuniform, nonuniform_count, &kept), + "a module with ShaderNonUniform and no SPV_EXT_descriptor_indexing is left alone"); + check(!spirv_cap_version(select_struct, select_struct_count, &kept), + "a module selecting a structure is left alone"); + check(kept.words == NULL, "no rejected module is handed back"); + + struct spirv_code extension = {0}; + check(spirv_cap_version(nonuniform_extension, nonuniform_extension_count, &extension), + "a module declaring SPV_EXT_descriptor_indexing for ShaderNonUniform is capped"); + if (extension.words) { + check(extension.words[1] == 0x00010300u, "the module with the extension is SPIR-V 1.3"); + if (argc > 3) + write_module(argv[3], &extension); + free(extension.words); + } + + struct spirv_code scalar = {0}; + check(spirv_cap_version(select_scalar, select_scalar_count, &scalar), "a module selecting a scalar is capped"); + if (scalar.words) { + check(count_opcodes(scalar.words, scalar.count, SpvOpSelect) == 1, "the scalar select is kept"); + if (argc > 4) + write_module(argv[4], &scalar); + free(scalar.words); + } struct spirv_code guest = {0}; check(spirv_cap_version(zinklike, zinklike_count, &guest), "a guest module with a private global is capped"); diff --git a/tools/tests/test_mali_compat_spirv.py b/tools/tests/test_mali_compat_spirv.py index 2e10d0290e..011c95a58f 100644 --- a/tools/tests/test_mali_compat_spirv.py +++ b/tools/tests/test_mali_compat_spirv.py @@ -34,21 +34,26 @@ class MaliCompatSpirvTest(unittest.TestCase): program = Path(tmp) / "mali-compat-spirv" module = Path(tmp) / "capped.spv" guest = Path(tmp) / "guest.spv" + extension = Path(tmp) / "extension.spv" + scalar = Path(tmp) / "scalar.spv" built = subprocess.run( [compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG", "-pthread", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv), str(HARNESS), str(LAYER / "spirv.c"), "-o", str(program)], capture_output=True, text=True) self.assertEqual(built.returncode, 0, built.stdout + built.stderr) - ran = subprocess.run([str(program), str(module), str(guest)], capture_output=True, text=True, timeout=30) + ran = subprocess.run([str(program), str(module), str(guest), str(extension), str(scalar)], + capture_output=True, text=True, timeout=30) self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr) self.assertIn("mali compat spirv ok", ran.stdout) self.assertTrue(module.is_file()) self.assertTrue(guest.is_file()) + self.assertTrue(extension.is_file()) + self.assertTrue(scalar.is_file()) validator = shutil.which("spirv-val") if not validator: return - for lowered in (module, guest): + for lowered in (module, guest, extension, scalar): checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(lowered)], capture_output=True, text=True) self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr) diff --git a/tools/venus/layer/layer.c b/tools/venus/layer/layer.c index cfc118bbd3..1a6cb8f3e5 100644 --- a/tools/venus/layer/layer.c +++ b/tools/venus/layer/layer.c @@ -593,6 +593,7 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev dev->handle = *device; dev->gdpa = gdpa; dev->emulated = requested; + dev->active = inst->active; pthread_mutex_init(&dev->lock, NULL); #define LOAD(name) \ dev->name = (PFN_vk##name)gdpa(*device, "vk" #name); \ @@ -672,6 +673,7 @@ static const struct hook device_hooks[] = { HOOK_ALWAYS(CreateShaderModule), HOOK_ALWAYS(DestroyShaderModule), HOOK_ALWAYS(CreateGraphicsPipelines), + HOOK_ALWAYS(CreateComputePipelines), HOOK(CmdSetViewport, GAP_MULTI_VIEWPORT), HOOK(CmdSetScissor, GAP_MULTI_VIEWPORT), HOOK(CmdSetViewportWithCount, GAP_MULTI_VIEWPORT), @@ -746,7 +748,8 @@ static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice devic if (!next) return NULL; for (size_t i = 0; i < sizeof(device_hooks) / sizeof(device_hooks[0]); i++) { - if ((device_hooks[i].always || (dev->emulated & device_hooks[i].gaps)) && !strcmp(name, device_hooks[i].name)) + bool installed = device_hooks[i].always ? dev->active : (dev->emulated & device_hooks[i].gaps) != 0; + if (installed && !strcmp(name, device_hooks[i].name)) return device_hooks[i].function; } return next; diff --git a/tools/venus/layer/mali_compat.h b/tools/venus/layer/mali_compat.h index 32383eac5f..0582432e63 100644 --- a/tools/venus/layer/mali_compat.h +++ b/tools/venus/layer/mali_compat.h @@ -88,6 +88,7 @@ struct instance { X(CreateShaderModule) \ X(DestroyShaderModule) \ X(CreateGraphicsPipelines) \ + X(CreateComputePipelines) \ X(CmdSetViewport) \ X(CmdSetScissor) \ X(CmdSetViewportWithCount) \ @@ -143,6 +144,7 @@ struct device { void *key; VkDevice handle; PFN_vkGetDeviceProcAddr gdpa; + bool active; /* The gaps the application enabled a made-up feature for. */ uint32_t emulated; /* Locations a clip distance varying may use, from the vertex output and fragment input limits. */ @@ -211,6 +213,9 @@ VKAPI_ATTR void VKAPI_CALL DestroyShaderModule(VkDevice device, VkShaderModule m VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache cache, uint32_t count, const VkGraphicsPipelineCreateInfo *infos, const VkAllocationCallbacks *allocator, VkPipeline *pipelines); +VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache cache, uint32_t count, + const VkComputePipelineCreateInfo *infos, + const VkAllocationCallbacks *allocator, VkPipeline *pipelines); VKAPI_ATTR void VKAPI_CALL CmdSetViewport(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkViewport *viewports); VKAPI_ATTR void VKAPI_CALL CmdSetScissor(VkCommandBuffer cmd, uint32_t first, uint32_t count, diff --git a/tools/venus/layer/pipeline.c b/tools/venus/layer/pipeline.c index 1b1d4bc3fc..bae468ad7f 100644 --- a/tools/venus/layer/pipeline.c +++ b/tools/venus/layer/pipeline.c @@ -30,7 +30,7 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade struct spirv_code stripped = {0}; bool have_stripped = false; - if (spirv_uses_distances(code, words)) { + if ((dev->emulated & GAPS_SHADER) && spirv_uses_distances(code, words)) { have_stripped = spirv_strip_distances(code, words, &stripped); if (!have_stripped) layer_log("could not take the clip and cull distances out of a shader module"); @@ -135,19 +135,15 @@ struct pipeline_rewrite { size_t in_place_size[MAX_STAGES]; }; -static void use_code(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw, - const struct stage_code *code, uint32_t i) +static void use_stage_code(struct device *dev, VkPipelineShaderStageCreateInfo *stage, struct pipeline_rewrite *rw, + const struct stage_code *code, uint32_t i) { - if (info->pStages != rw->stages) { - memcpy(rw->stages, info->pStages, info->stageCount * sizeof(*rw->stages)); - info->pStages = rw->stages; - } const struct spirv_code *out = &rw->code[i]; - if (code->inline_info && rw->stages[i].pNext == code->inline_info) { + if (code->inline_info && stage->pNext == code->inline_info) { rw->modules[i] = *code->inline_info; rw->modules[i].pCode = out->words; rw->modules[i].codeSize = out->count * sizeof(uint32_t); - rw->stages[i].pNext = &rw->modules[i]; + stage->pNext = &rw->modules[i]; } else if (code->inline_info) { rw->in_place[i] = code->inline_info; rw->in_place_code[i] = code->inline_info->pCode; @@ -161,10 +157,20 @@ static void use_code(struct device *dev, VkGraphicsPipelineCreateInfo *info, str .pCode = out->words, }; if (dev->CreateShaderModule(dev->handle, &module_info, NULL, &rw->temporary[i]) == VK_SUCCESS) - rw->stages[i].module = rw->temporary[i]; + stage->module = rw->temporary[i]; } } +static void use_code(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw, + const struct stage_code *code, uint32_t i) +{ + if (info->pStages != rw->stages) { + memcpy(rw->stages, info->pStages, info->stageCount * sizeof(*rw->stages)); + info->pStages = rw->stages; + } + use_stage_code(dev, &rw->stages[i], rw, code, i); +} + static int stage_index(const VkGraphicsPipelineCreateInfo *info, VkShaderStageFlagBits stage) { for (uint32_t i = 0; i < info->stageCount; i++) { @@ -319,6 +325,30 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipeli return result; } +VKAPI_ATTR VkResult VKAPI_CALL CreateComputePipelines(VkDevice device, VkPipelineCache cache, uint32_t count, + const VkComputePipelineCreateInfo *infos, + const VkAllocationCallbacks *allocator, VkPipeline *pipelines) +{ + struct device *dev = device_from_handle(device); + VkComputePipelineCreateInfo *copies = malloc(count * sizeof(*copies)); + struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites)); + for (uint32_t i = 0; i < count; i++) { + copies[i] = infos[i]; + struct stage_code code; + find_stage_code(dev, &copies[i].stage, &code); + if (code.capped.words) { + rewrites[i].code[0] = code.capped; + use_stage_code(dev, &copies[i].stage, &rewrites[i], &code, 0); + } + } + VkResult result = dev->CreateComputePipelines(device, cache, count, copies, allocator, pipelines); + for (uint32_t i = 0; i < count; i++) + undo_rewrite(dev, &rewrites[i]); + free(rewrites); + free(copies); + return result; +} + /* The driver has one viewport and one scissor; anything past the first is left out. */ VKAPI_ATTR void VKAPI_CALL CmdSetViewport(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkViewport *viewports) diff --git a/tools/venus/layer/spirv.c b/tools/venus/layer/spirv.c index 44f9011fea..4fa75e8608 100644 --- a/tools/venus/layer/spirv.c +++ b/tools/venus/layer/spirv.c @@ -833,29 +833,205 @@ bool spirv_emulate_clip(const uint32_t *words, size_t count, enum clip_role role return done; } -static bool needs_newer_version(uint32_t op) +struct newer_feature { + uint32_t value; + const char *extension; + const char *alternative; +}; + +#define PHYSICAL_STORAGE_BUFFER "SPV_EXT_physical_storage_buffer", "SPV_KHR_physical_storage_buffer" + +static const struct newer_feature newer_opcodes[] = { + {SpvOpCopyLogical, NULL, NULL}, + {SpvOpPtrEqual, NULL, NULL}, + {SpvOpPtrNotEqual, NULL, NULL}, + {SpvOpPtrDiff, NULL, NULL}, + {SpvOpDemoteToHelperInvocation, "SPV_EXT_demote_to_helper_invocation", NULL}, + {SpvOpSDot, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpUDot, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpSUDot, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpSDotAccSat, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpUDotAccSat, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpSUDotAccSat, "SPV_KHR_integer_dot_product", NULL}, + {SpvOpDecorateString, "SPV_GOOGLE_decorate_string", "SPV_GOOGLE_hlsl_functionality1"}, + {SpvOpMemberDecorateString, "SPV_GOOGLE_decorate_string", "SPV_GOOGLE_hlsl_functionality1"}, +}; + +static const struct newer_feature newer_capabilities[] = { + {SpvCapabilityShaderLayer, NULL, NULL}, + {SpvCapabilityShaderViewportIndex, NULL, NULL}, + {SpvCapabilityUniformDecoration, NULL, NULL}, + {SpvCapabilityStorageBuffer8BitAccess, "SPV_KHR_8bit_storage", NULL}, + {SpvCapabilityUniformAndStorageBuffer8BitAccess, "SPV_KHR_8bit_storage", NULL}, + {SpvCapabilityStoragePushConstant8, "SPV_KHR_8bit_storage", NULL}, + {SpvCapabilityDenormPreserve, "SPV_KHR_float_controls", NULL}, + {SpvCapabilityDenormFlushToZero, "SPV_KHR_float_controls", NULL}, + {SpvCapabilitySignedZeroInfNanPreserve, "SPV_KHR_float_controls", NULL}, + {SpvCapabilityRoundingModeRTE, "SPV_KHR_float_controls", NULL}, + {SpvCapabilityRoundingModeRTZ, "SPV_KHR_float_controls", NULL}, + {SpvCapabilityShaderNonUniform, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityRuntimeDescriptorArray, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityInputAttachmentArrayDynamicIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityUniformTexelBufferArrayDynamicIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityStorageTexelBufferArrayDynamicIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityUniformBufferArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilitySampledImageArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityStorageBufferArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityStorageImageArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityInputAttachmentArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityUniformTexelBufferArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityStorageTexelBufferArrayNonUniformIndexing, "SPV_EXT_descriptor_indexing", NULL}, + {SpvCapabilityVulkanMemoryModel, "SPV_KHR_vulkan_memory_model", NULL}, + {SpvCapabilityVulkanMemoryModelDeviceScope, "SPV_KHR_vulkan_memory_model", NULL}, + {SpvCapabilityPhysicalStorageBufferAddresses, PHYSICAL_STORAGE_BUFFER}, + {SpvCapabilityDemoteToHelperInvocation, "SPV_EXT_demote_to_helper_invocation", NULL}, + {SpvCapabilityDotProductInputAll, "SPV_KHR_integer_dot_product", NULL}, + {SpvCapabilityDotProductInput4x8Bit, "SPV_KHR_integer_dot_product", NULL}, + {SpvCapabilityDotProductInput4x8BitPacked, "SPV_KHR_integer_dot_product", NULL}, + {SpvCapabilityDotProduct, "SPV_KHR_integer_dot_product", NULL}, +}; + +static const struct newer_feature newer_decorations[] = { + {SpvDecorationUniformId, NULL, NULL}, + {SpvDecorationNoSignedWrap, "SPV_KHR_no_integer_wrap_decoration", NULL}, + {SpvDecorationNoUnsignedWrap, "SPV_KHR_no_integer_wrap_decoration", NULL}, + {SpvDecorationNonUniform, "SPV_EXT_descriptor_indexing", NULL}, + {SpvDecorationRestrictPointer, PHYSICAL_STORAGE_BUFFER}, + {SpvDecorationAliasedPointer, PHYSICAL_STORAGE_BUFFER}, + {SpvDecorationCounterBuffer, "SPV_GOOGLE_hlsl_functionality1", NULL}, + {SpvDecorationUserSemantic, "SPV_GOOGLE_hlsl_functionality1", NULL}, +}; + +#define NEWER_LOOP_CONTROLS \ + (SpvLoopControlMinIterationsMask | SpvLoopControlMaxIterationsMask | SpvLoopControlIterationMultipleMask | \ + SpvLoopControlPeelCountMask | SpvLoopControlPartialCountMask) +#define NEWER_IMAGE_OPERANDS \ + (SpvImageOperandsSignExtendMask | SpvImageOperandsZeroExtendMask | SpvImageOperandsNontemporalMask) + +static bool declares_extension(const struct module *m, const char *name) +{ + if (!name) + return false; + size_t size = strlen(name) + 1; + for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) { + uint32_t length = LENGTH(m->w[i]); + if (OPCODE(m->w[i]) == SpvOpExtension && (length - 1) * sizeof(uint32_t) >= size && + !memcmp(&m->w[i + 1], name, size)) + return true; + } + return false; +} + +static bool available_in_1_3(const struct module *m, const struct newer_feature *features, size_t count, + uint32_t value) +{ + for (size_t i = 0; i < count; i++) { + if (features[i].value == value) + return declares_extension(m, features[i].extension) || declares_extension(m, features[i].alternative); + } + return true; +} + +#define AVAILABLE(m, table, value) available_in_1_3(m, table, sizeof(table) / sizeof(table[0]), value) + +static uint32_t image_operands_at(uint32_t op) { switch (op) { - case SpvOpCopyLogical: - case SpvOpPtrEqual: - case SpvOpPtrNotEqual: - case SpvOpPtrDiff: - case SpvOpDemoteToHelperInvocation: + case SpvOpImageWrite: + return 4; + case SpvOpImageSampleImplicitLod: + case SpvOpImageSampleExplicitLod: + case SpvOpImageSampleProjImplicitLod: + case SpvOpImageSampleProjExplicitLod: + case SpvOpImageFetch: + case SpvOpImageRead: + case SpvOpImageSparseSampleImplicitLod: + case SpvOpImageSparseSampleExplicitLod: + case SpvOpImageSparseSampleProjImplicitLod: + case SpvOpImageSparseSampleProjExplicitLod: + case SpvOpImageSparseFetch: + case SpvOpImageSparseRead: + return 5; + case SpvOpImageSampleDrefImplicitLod: + case SpvOpImageSampleDrefExplicitLod: + case SpvOpImageSampleProjDrefImplicitLod: + case SpvOpImageSampleProjDrefExplicitLod: + case SpvOpImageGather: + case SpvOpImageDrefGather: + case SpvOpImageSparseSampleDrefImplicitLod: + case SpvOpImageSparseSampleDrefExplicitLod: + case SpvOpImageSparseSampleProjDrefImplicitLod: + case SpvOpImageSparseSampleProjDrefExplicitLod: + case SpvOpImageSparseGather: + case SpvOpImageSparseDrefGather: + return 6; + default: + return 0; + } +} + +static uint32_t memory_access_words(uint32_t mask) +{ + uint32_t words = 1; + if (mask & SpvMemoryAccessAlignedMask) + words++; + if (mask & SpvMemoryAccessMakePointerAvailableMask) + words++; + if (mask & SpvMemoryAccessMakePointerVisibleMask) + words++; + return words; +} + +static bool selects_scalar_vector_or_pointer(const struct module *m, uint32_t type) +{ + size_t d = definition(m, type); + if (!d) + return false; + switch (OPCODE(m->w[d])) { + case SpvOpTypeBool: + case SpvOpTypeInt: + case SpvOpTypeFloat: + case SpvOpTypeVector: + case SpvOpTypePointer: return true; default: return false; } } -static bool needs_newer_capability(uint32_t capability) +static bool valid_in_1_3(const struct module *m, size_t i) { - switch (capability) { - case SpvCapabilityShaderLayer: - case SpvCapabilityShaderViewportIndex: - case SpvCapabilityUniformDecoration: - return true; - default: + uint32_t op = OPCODE(m->w[i]); + uint32_t length = LENGTH(m->w[i]); + if (!AVAILABLE(m, newer_opcodes, op)) return false; + switch (op) { + case SpvOpMemoryModel: + return length >= 3 && m->w[i + 2] == SpvMemoryModelGLSL450; + case SpvOpCapability: + return length >= 2 && AVAILABLE(m, newer_capabilities, m->w[i + 1]); + case SpvOpExecutionModeId: + return length >= 3 && m->w[i + 2] != SpvExecutionModeLocalSizeId; + case SpvOpDecorate: + case SpvOpDecorateId: + case SpvOpDecorateString: + return length >= 3 && AVAILABLE(m, newer_decorations, m->w[i + 2]); + case SpvOpMemberDecorate: + case SpvOpMemberDecorateString: + return length >= 4 && AVAILABLE(m, newer_decorations, m->w[i + 3]); + case SpvOpLoopMerge: + return length >= 4 && !(m->w[i + 3] & NEWER_LOOP_CONTROLS); + case SpvOpSelect: + return length >= 6 && selects_scalar_vector_or_pointer(m, m->w[i + 1]); + case SpvOpCopyMemory: + case SpvOpCopyMemorySized: { + uint32_t at = op == SpvOpCopyMemory ? 3 : 4; + return length <= at || at + memory_access_words(m->w[i + at]) >= length; + } + default: { + uint32_t at = image_operands_at(op); + return !at || length <= at || !(m->w[i + at] & NEWER_IMAGE_OPERANDS); + } } } @@ -901,21 +1077,7 @@ bool spirv_cap_version(const uint32_t *words, size_t count, struct spirv_code *o if (!load(&m, words, count)) return false; for (size_t i = 5; i < m.count; i += LENGTH(m.w[i])) { - uint32_t op = OPCODE(m.w[i]); - uint32_t length = LENGTH(m.w[i]); - if (op == SpvOpMemoryModel && (length < 3 || m.w[i + 2] != SpvMemoryModelGLSL450)) { - unload(&m); - return false; - } - if (op == SpvOpCapability && (length < 2 || needs_newer_capability(m.w[i + 1]))) { - unload(&m); - return false; - } - if (op == SpvOpExecutionModeId && (length < 3 || m.w[i + 2] == SpvExecutionModeLocalSizeId)) { - unload(&m); - return false; - } - if (needs_newer_version(op)) { + if (!valid_in_1_3(&m, i)) { unload(&m); return false; }