fix(venus): lower the guest's SPIR-V to 1.3 for the phone's shader compiler (#2587)

* fix(venus): lower the guest's SPIR-V to 1.3 for the phone's shader compiler

* fix(venus): lower SPIR-V to 1.3 for Mali drivers

* 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.

* fix(venus): keep application pipeline chains read-only and check non-semantic imports

Inline stage code is handed to the driver through a copy of the stage's
pNext chain instead of being written into the application's create info,
pipeline hooks return VK_ERROR_OUT_OF_HOST_MEMORY when their allocations
fail, and a NonSemantic import is only lowered to SPIR-V 1.3 when the
module declares SPV_KHR_non_semantic_info.

* fix(venus): preserve valid pipeline and shader behavior

* fix(venus): lower compatible read-only shader parameters

* fix(venus): install SPIR-V 1.3 lowering hooks on Vulkan 1.2+ devices or emulated gaps
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-21 23:52:56 -05:00
1 parent bc11653c15
commit 6de3c59d7c
7 files changed
+1183 -101

No files matched your search

+24 -1
View File
@@ -128,10 +128,32 @@ What it does, per feature:
- Pipelines and `vkCmdSetColorBlendEquationEXT` both.
- Why alpha: output 0's color is the paint, its alpha is the usual coverage. Per-channel coverage collapses to one value; blending stays plain alpha blending, not tinted by the paint.
The SPIR-V version the driver gets:
- The phone's own driver compiles the guest's SPIR-V, and its compiler is old: on the MT6897
Mali-G615 (r44p1, "Vulkan 1.3.247") it dies with a null read in `cmpbe_v2_compile_multiple_shaders`
while compiling a pipeline, and the render server dies with it, so every guest client that
draws loses its device.
- Zink writes SPIR-V 1.6 for any Vulkan 1.3 device (`zink_screen.c`: `spirv_version` from the
device's version), and SPIR-V 1.4 changed the entry point's interface: it has to list every
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`). 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.
Files:
- `layer.c`: loader plumbing, feature and limit reporting, device creation, the hook table.
- `pipeline.c`: pipelines, shader modules, viewports.
- `spirv.c`: the SPIR-V rewriter for clip and cull distances and the second blend source.
- `spirv.c`: the SPIR-V rewriter for clip and cull distances and the second blend source, and the version cap.
- `bc.c`: BC formats, images and uploads.
- `VkLayer_droiddeck_mali_compat.json`: implicit layer manifest.
@@ -149,6 +171,7 @@ How it ships:
Not done yet:
- Unit tests for the clip and cull distance rewriter. It is untested beyond DXVK's shaders, and unknown whether Silksong's shaders used clip distances at all.
- The dual-source rewrite has them: `tools/tests/test_mali_compat_layer.py`.
- The version cap has them: `tools/tests/test_mali_compat_spirv.py`.
- Performance: BC decoding on the CPU makes loads slow. A compute-shader decoder would be the next step.
## Changes on the branch
+548
View File
@@ -0,0 +1,548 @@
#include "mali_compat.h"
#include <spirv/unified1/spirv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OPCODE(word) ((word) & 0xffffu)
#define LENGTH(word) ((word) >> 16)
static const uint32_t newer[] = {
0x07230203, 0x00010600, 0x00070000, 0x0000000a, 0x00000000, 0x00020011, 0x00000001, 0x0003000e,
0x00000000, 0x00000001, 0x0007000f, 0x00000004, 0x00000001, 0x6e69616d, 0x00000000, 0x00000002,
0x00000003, 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, 0x00000000,
0x00020013, 0x00000004, 0x00030016, 0x00000005, 0x00000020, 0x00040020, 0x00000006, 0x00000003,
0x00000005, 0x0004003b, 0x00000006, 0x00000002, 0x00000003, 0x00040020, 0x00000007, 0x00000006,
0x00000005, 0x0004003b, 0x00000007, 0x00000003, 0x00000006, 0x00030021, 0x00000008, 0x00000004,
0x00050036, 0x00000004, 0x00000001, 0x00000000, 0x00000008, 0x000200f8, 0x00000009, 0x00011140,
0x00010038,
};
static const size_t newer_count = sizeof(newer) / sizeof(uint32_t);
static const uint32_t readonly_parameter[] = {
SpvMagicNumber, 0x00010600, 0, 13, 0,
(2u << 16) | SpvOpCapability, SpvCapabilityShader,
(3u << 16) | SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450,
(5u << 16) | SpvOpEntryPoint, SpvExecutionModelFragment, 7, 0x6e69616d, 0,
(3u << 16) | SpvOpExecutionMode, 7, SpvExecutionModeOriginUpperLeft,
(3u << 16) | SpvOpDecorate, 3, SpvDecorationBlock,
(5u << 16) | SpvOpMemberDecorate, 3, 0, SpvDecorationOffset, 0,
(3u << 16) | SpvOpDecorate, 10, SpvDecorationNonWritable,
(4u << 16) | SpvOpDecorate, 12, SpvDecorationDescriptorSet, 0,
(4u << 16) | SpvOpDecorate, 12, SpvDecorationBinding, 0,
(2u << 16) | SpvOpTypeVoid, 1,
(3u << 16) | SpvOpTypeFloat, 2, 32,
(3u << 16) | SpvOpTypeStruct, 3, 2,
(4u << 16) | SpvOpTypePointer, 4, SpvStorageClassStorageBuffer, 3,
(4u << 16) | SpvOpVariable, 4, 12, SpvStorageClassStorageBuffer,
(3u << 16) | SpvOpTypeFunction, 5, 1,
(4u << 16) | SpvOpTypeFunction, 6, 1, 4,
(5u << 16) | SpvOpFunction, 1, 7, 0, 5,
(2u << 16) | SpvOpLabel, 8,
(1u << 16) | SpvOpReturn,
(1u << 16) | SpvOpFunctionEnd,
(5u << 16) | SpvOpFunction, 1, 9, 0, 6,
(3u << 16) | SpvOpFunctionParameter, 4, 10,
(2u << 16) | SpvOpLabel, 11,
(1u << 16) | SpvOpReturn,
(1u << 16) | SpvOpFunctionEnd,
};
static const uint32_t older[] = {
0x07230203, 0x00010300, 0x00070000, 0x00000008, 0x00000000, 0x00020011, 0x00000001, 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, 0x00030021, 0x00000006, 0x00000003, 0x00050036,
0x00000003, 0x00000001, 0x00000000, 0x00000006, 0x000200f8, 0x00000007, 0x000100fd, 0x00010038,
};
static const size_t older_count = sizeof(older) / sizeof(uint32_t);
static const uint32_t capability[] = {
0x07230203, 0x00010600, 0x00070000, 0x00000008, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
0x00000046, 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, 0x00030021, 0x00000006,
0x00000003, 0x00050036, 0x00000003, 0x00000001, 0x00000000, 0x00000006, 0x000200f8, 0x00000007,
0x000100fd, 0x00010038,
};
static const size_t capability_count = sizeof(capability) / sizeof(uint32_t);
static const uint32_t memory[] = {
0x07230203, 0x00010600, 0x00070000, 0x00000008, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
0x000014e1, 0x0003000e, 0x00000000, 0x00000003, 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, 0x00030021, 0x00000006,
0x00000003, 0x00050036, 0x00000003, 0x00000001, 0x00000000, 0x00000006, 0x000200f8, 0x00000007,
0x000100fd, 0x00010038,
};
static const size_t memory_count = sizeof(memory) / sizeof(uint32_t);
static const uint32_t zinklike[] = {
0x07230203, 0x00010600, 0x00070000, 0x00000017, 0x00000000, 0x00020011, 0x00000001, 0x0003000e,
0x00000000, 0x00000001, 0x0007000f, 0x00000004, 0x00000001, 0x6e69616d, 0x00000000, 0x00000002,
0x00000003, 0x00030010, 0x00000001, 0x00000007, 0x00040047, 0x00000002, 0x0000001e, 0x00000000,
0x00020013, 0x00000004, 0x00030016, 0x00000005, 0x00000020, 0x00040015, 0x00000006, 0x00000020,
0x00000001, 0x00040017, 0x00000007, 0x00000005, 0x00000004, 0x0003001e, 0x00000008, 0x00000007,
0x00040020, 0x00000009, 0x00000003, 0x00000005, 0x0004003b, 0x00000009, 0x00000002, 0x00000003,
0x00040020, 0x0000000a, 0x00000006, 0x00000008, 0x00040020, 0x0000000b, 0x00000007, 0x00000008,
0x0004003b, 0x0000000a, 0x00000003, 0x00000006, 0x00040020, 0x0000000c, 0x00000006, 0x00000005,
0x00040020, 0x0000000d, 0x00000007, 0x00000005, 0x0004002b, 0x00000005, 0x0000000e, 0x3f800000,
0x0004002b, 0x00000006, 0x0000000f, 0x00000000, 0x00030021, 0x00000010, 0x00000004, 0x00050036,
0x00000004, 0x00000001, 0x00000000, 0x00000010, 0x000200f8, 0x00000011, 0x0004003b, 0x0000000b,
0x00000012, 0x00000007, 0x00060041, 0x0000000d, 0x00000013, 0x00000012, 0x0000000f, 0x0000000f,
0x0003003e, 0x00000013, 0x0000000e, 0x0004003d, 0x00000005, 0x00000014, 0x00000013, 0x00070050,
0x00000007, 0x00000015, 0x00000014, 0x00000014, 0x00000014, 0x00000014, 0x00040041, 0x00000009,
0x00000016, 0x00000002, 0x0003003e, 0x00000016, 0x00000014, 0x000100fd, 0x00010038,
};
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 const uint32_t nonsemantic[] = {
0x07230203, 0x00010600, 0x00070000, 0x0000000c, 0x00000000, 0x00020011, 0x00000001, 0x0008000b,
0x00000001, 0x536e6f4e, 0x6e616d65, 0x2e636974, 0x75626544, 0x69725067, 0x0066746e, 0x0003000e,
0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003,
0x00030010, 0x00000002, 0x00000007, 0x00030007, 0x00000004, 0x00000078, 0x00040047, 0x00000003,
0x0000001e, 0x00000000, 0x00020013, 0x00000005, 0x00030016, 0x00000006, 0x00000020, 0x00040020,
0x00000007, 0x00000003, 0x00000006, 0x0004003b, 0x00000007, 0x00000003, 0x00000003, 0x0004002b,
0x00000006, 0x00000008, 0x3f800000, 0x00030021, 0x00000009, 0x00000005, 0x00050036, 0x00000005,
0x00000002, 0x00000000, 0x00000009, 0x000200f8, 0x0000000a, 0x0006000c, 0x00000005, 0x0000000b,
0x00000001, 0x00000001, 0x00000004, 0x0003003e, 0x00000003, 0x00000008, 0x000100fd, 0x00010038,
};
static const size_t nonsemantic_count = sizeof(nonsemantic) / sizeof(uint32_t);
static const uint32_t nonsemantic_extension[] = {
0x07230203, 0x00010600, 0x00070000, 0x0000000c, 0x00000000, 0x00020011, 0x00000001, 0x0008000a,
0x5f565053, 0x5f52484b, 0x5f6e6f6e, 0x616d6573, 0x6369746e, 0x666e695f, 0x0000006f, 0x0008000b,
0x00000001, 0x536e6f4e, 0x6e616d65, 0x2e636974, 0x75626544, 0x69725067, 0x0066746e, 0x0003000e,
0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000002, 0x6e69616d, 0x00000000, 0x00000003,
0x00030010, 0x00000002, 0x00000007, 0x00030007, 0x00000004, 0x00000078, 0x00040047, 0x00000003,
0x0000001e, 0x00000000, 0x00020013, 0x00000005, 0x00030016, 0x00000006, 0x00000020, 0x00040020,
0x00000007, 0x00000003, 0x00000006, 0x0004003b, 0x00000007, 0x00000003, 0x00000003, 0x0004002b,
0x00000006, 0x00000008, 0x3f800000, 0x00030021, 0x00000009, 0x00000005, 0x00050036, 0x00000005,
0x00000002, 0x00000000, 0x00000009, 0x000200f8, 0x0000000a, 0x0006000c, 0x00000005, 0x0000000b,
0x00000001, 0x00000001, 0x00000004, 0x0003003e, 0x00000003, 0x00000008, 0x000100fd, 0x00010038,
};
static const size_t nonsemantic_extension_count = sizeof(nonsemantic_extension) / sizeof(uint32_t);
static int failures;
static void check(int condition, const char *what)
{
if (!condition) {
printf("FAIL: %s\n", what);
failures++;
}
}
static size_t count_opcodes(const uint32_t *words, size_t count, uint32_t opcode)
{
size_t found = 0;
for (size_t i = 5; i < count; i += LENGTH(words[i])) {
if (OPCODE(words[i]) == opcode)
found++;
}
return found;
}
static size_t interface_size(const uint32_t *words, size_t count)
{
for (size_t i = 5; i < count; i += LENGTH(words[i])) {
if (OPCODE(words[i]) != SpvOpEntryPoint)
continue;
size_t length = LENGTH(words[i]);
size_t name = 3;
while (name < length) {
uint32_t word = words[i + name];
if (!(word & 0xff) || !(word & 0xff00) || !(word & 0xff0000) || !(word & 0xff000000))
break;
name++;
}
return length - (name + 1);
}
return 0;
}
static void write_module(const char *path, const struct spirv_code *code)
{
FILE *out = fopen(path, "wb");
if (!out) {
printf("FAIL: %s cannot be written\n", path);
failures++;
return;
}
fwrite(code->words, sizeof(uint32_t), code->count, out);
fclose(out);
}
static struct device test_device;
static int failed_allocations;
static bool fail_allocations;
static int driver_calls;
static bool chain_was_layer_owned;
static VkStructureType first_link;
static uint32_t driver_version;
static uint32_t driver_subgroup_size;
void *test_calloc(size_t count, size_t size)
{
if (fail_allocations) {
failed_allocations++;
return NULL;
}
if (size && count > SIZE_MAX / size)
return NULL;
size_t bytes = count * size;
void *memory = malloc(bytes > 0 ? bytes : 1);
if (memory)
memset(memory, 0, bytes);
return memory;
}
struct device *device_from_handle(const void *dispatchable)
{
return &test_device;
}
void layer_log(const char *format, ...)
{
}
void *map_get(const struct handle_map *map, uint64_t key)
{
return NULL;
}
void map_put(struct handle_map *map, uint64_t key, void *value)
{
}
void *map_remove(struct handle_map *map, uint64_t key)
{
return NULL;
}
static void read_stage(const VkPipelineShaderStageCreateInfo *stage, const void *app_chain)
{
driver_calls++;
chain_was_layer_owned = stage->pNext != app_chain;
first_link = stage->pNext ? ((const VkBaseInStructure *)stage->pNext)->sType : VK_STRUCTURE_TYPE_MAX_ENUM;
driver_version = 0;
driver_subgroup_size = 0;
for (const VkBaseInStructure *s = stage->pNext; s; s = s->pNext) {
if (s->sType == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO)
driver_subgroup_size = ((const VkPipelineShaderStageRequiredSubgroupSizeCreateInfo *)s)->requiredSubgroupSize;
if (s->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) {
const VkShaderModuleCreateInfo *info = (const VkShaderModuleCreateInfo *)s;
driver_version = spirv_version(info->pCode, info->codeSize / sizeof(uint32_t));
}
}
}
static const void *expected_chain;
static VkResult VKAPI_CALL driver_compute(VkDevice device, VkPipelineCache cache, uint32_t count,
const VkComputePipelineCreateInfo *infos,
const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
{
read_stage(&infos[0].stage, expected_chain);
return VK_SUCCESS;
}
static VkResult VKAPI_CALL driver_graphics(VkDevice device, VkPipelineCache cache, uint32_t count,
const VkGraphicsPipelineCreateInfo *infos,
const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
{
read_stage(&infos[0].pStages[0], expected_chain);
return VK_SUCCESS;
}
static const VkShaderModuleCreateInfo app_module = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = sizeof(newer),
.pCode = newer,
};
static const VkPipelineShaderStageRequiredSubgroupSizeCreateInfo app_subgroup = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO,
.pNext = &app_module,
.requiredSubgroupSize = 16,
};
static const VkApplicationInfo app_unknown = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = &app_module,
};
static const VkPipelineShaderStageCreateInfo app_fragment = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = &app_subgroup,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT,
.pName = "main",
};
static void check_pipelines(void)
{
VkDevice device = (VkDevice)&test_device;
VkPipeline pipeline = VK_NULL_HANDLE;
pthread_mutex_init(&test_device.lock, NULL);
test_device.CreateComputePipelines = driver_compute;
test_device.CreateGraphicsPipelines = driver_graphics;
VkComputePipelineCreateInfo compute = {
.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
.stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = &app_subgroup,
.stage = VK_SHADER_STAGE_COMPUTE_BIT,
.pName = "main"},
};
expected_chain = &app_subgroup;
check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS,
"a compute pipeline with inline code behind another structure is created");
check(chain_was_layer_owned, "the compute stage gets the layer's own chain");
check(first_link == VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO,
"the compute stage keeps its chain order");
check(driver_subgroup_size == 16, "the compute stage keeps its required subgroup size");
check(driver_version == 0x00010300u, "the compute stage's inline code reaches the driver as SPIR-V 1.3");
check(app_module.pCode == newer && app_module.codeSize == sizeof(newer),
"the application's shader module create info is untouched");
check(compute.stage.pNext == &app_subgroup, "the application's compute stage is untouched");
compute.stage.pNext = &app_module;
expected_chain = &app_module;
check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS,
"a compute pipeline with inline code first in its chain is created");
check(chain_was_layer_owned && driver_version == 0x00010300u,
"inline code first in the chain reaches the driver as SPIR-V 1.3");
compute.stage.pNext = &app_unknown;
expected_chain = &app_unknown;
check(CreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute, NULL, &pipeline) == VK_SUCCESS,
"a compute pipeline behind an unknown structure is created");
check(!chain_was_layer_owned && driver_version == 0x00010600u,
"a chain the layer cannot copy reaches the driver as the application wrote it");
VkGraphicsPipelineCreateInfo graphics = {
.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
.stageCount = 1,
.pStages = &app_fragment,
};
expected_chain = &app_subgroup;
check(CreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics, NULL, &pipeline) == VK_SUCCESS,
"a graphics pipeline with inline code behind another structure is created");
check(chain_was_layer_owned && driver_subgroup_size == 16 && driver_version == 0x00010300u,
"the graphics stage reaches the driver capped with its chain intact");
check(app_fragment.pNext == &app_subgroup && graphics.pStages == &app_fragment,
"the application's graphics stages are untouched");
driver_calls = 0;
failed_allocations = 0;
fail_allocations = true;
VkPipeline compute_outputs[2] = {(VkPipeline)(uintptr_t)1, (VkPipeline)(uintptr_t)2};
VkPipeline graphics_outputs[2] = {(VkPipeline)(uintptr_t)1, (VkPipeline)(uintptr_t)2};
VkResult compute_result = CreateComputePipelines(device, VK_NULL_HANDLE, 2, &compute, NULL, compute_outputs);
VkResult graphics_result = CreateGraphicsPipelines(device, VK_NULL_HANDLE, 2, &graphics, NULL, graphics_outputs);
fail_allocations = false;
check(failed_allocations > 0, "the allocations were made to fail");
check(compute_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a compute pipeline without memory reports it");
check(graphics_result == VK_ERROR_OUT_OF_HOST_MEMORY, "a graphics pipeline without memory reports it");
check(compute_outputs[0] == VK_NULL_HANDLE && compute_outputs[1] == VK_NULL_HANDLE,
"failed compute pipeline outputs are cleared");
check(graphics_outputs[0] == VK_NULL_HANDLE && graphics_outputs[1] == VK_NULL_HANDLE,
"failed graphics pipeline outputs are cleared");
check(driver_calls == 0, "the driver is not called without memory");
pthread_mutex_destroy(&test_device.lock);
}
int main(int argc, char **argv)
{
struct spirv_code capped = {0};
check(spirv_cap_version(newer, newer_count, &capped), "a SPIR-V 1.6 module is capped");
check(capped.words != NULL, "the capped module is there");
if (capped.words) {
check(capped.words[1] == 0x00010300u, "the capped module is SPIR-V 1.3");
check(interface_size(capped.words, capped.count) == 1,
"the entry point lists only the input and output variables");
check(count_opcodes(capped.words, capped.count, SpvOpKill) == 1, "the discard is an OpKill");
check(count_opcodes(capped.words, capped.count, SpvOpTerminateInvocation) == 0,
"no OpTerminateInvocation is left");
if (argc > 1)
write_module(argv[1], &capped);
free(capped.words);
}
struct spirv_code kept = {0};
check(!spirv_cap_version(older, older_count, &kept), "a SPIR-V 1.3 module is left alone");
check(kept.words == NULL, "the untouched module is not handed back");
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(!spirv_cap_version(nonsemantic, nonsemantic_count, &kept),
"a module importing a NonSemantic set without SPV_KHR_non_semantic_info is left alone");
uint32_t private_readonly[sizeof(zinklike) / sizeof(uint32_t) + 3];
size_t declaration = 5;
while (OPCODE(zinklike[declaration]) != SpvOpTypeVoid)
declaration += LENGTH(zinklike[declaration]);
memcpy(private_readonly, zinklike, declaration * sizeof(uint32_t));
private_readonly[declaration] = (3u << 16) | SpvOpDecorate;
private_readonly[declaration + 1] = 3;
private_readonly[declaration + 2] = SpvDecorationNonWritable;
memcpy(private_readonly + declaration + 3, zinklike + declaration,
(zinklike_count - declaration) * sizeof(uint32_t));
check(!spirv_cap_version(private_readonly, zinklike_count + 3, &kept),
"a private NonWritable variable is left alone");
if (argc > 6) {
struct spirv_code source = {.words = private_readonly, .count = zinklike_count + 3};
write_module(argv[6], &source);
}
uint32_t converted[sizeof(zinklike) / sizeof(uint32_t) + 9];
size_t function = declaration;
while (OPCODE(zinklike[function]) != SpvOpFunction)
function += LENGTH(zinklike[function]);
memcpy(converted, zinklike, function * sizeof(uint32_t));
const uint32_t conversion[] = {(4u << 16) | SpvOpTypeInt, 23, 32, 0,
(5u << 16) | SpvOpSpecConstantOp, 23, 24, SpvOpUConvert, 15};
memcpy(converted + function, conversion, sizeof(conversion));
memcpy(converted + function + 9, zinklike + function, (zinklike_count - function) * sizeof(uint32_t));
converted[3] = 25;
check(!spirv_cap_version(converted, zinklike_count + 9, &kept),
"a specialization UConvert without its extension is left alone");
if (argc > 7) {
struct spirv_code source = {.words = converted, .count = zinklike_count + 9};
write_module(argv[7], &source);
}
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 debug = {0};
check(spirv_cap_version(nonsemantic_extension, nonsemantic_extension_count, &debug),
"a module declaring SPV_KHR_non_semantic_info for its NonSemantic import is capped");
if (debug.words) {
check(debug.words[1] == 0x00010300u, "the module with the NonSemantic import is SPIR-V 1.3");
if (argc > 5)
write_module(argv[5], &debug);
free(debug.words);
}
struct spirv_code guest = {0};
check(spirv_cap_version(zinklike, zinklike_count, &guest), "a guest module with a private global is capped");
if (guest.words) {
check(guest.words[1] == 0x00010300u, "the guest module is SPIR-V 1.3");
check(interface_size(guest.words, guest.count) == 1,
"the guest module lists only the input and output variables");
check(count_opcodes(guest.words, guest.count, SpvOpFunction) == 1, "the guest module keeps its function");
if (argc > 2)
write_module(argv[2], &guest);
free(guest.words);
}
struct spirv_code parameter = {0};
size_t parameter_count = sizeof(readonly_parameter) / sizeof(uint32_t);
check(spirv_cap_version(readonly_parameter, parameter_count, &parameter),
"a NonWritable storage buffer parameter is capped");
if (parameter.words) {
check(count_opcodes(parameter.words, parameter.count, SpvOpFunctionParameter) == 1,
"the storage buffer parameter is preserved");
if (argc > 8)
write_module(argv[8], &parameter);
free(parameter.words);
}
uint32_t incompatible_parameter[sizeof(readonly_parameter) / sizeof(uint32_t)];
memcpy(incompatible_parameter, readonly_parameter, sizeof(readonly_parameter));
for (size_t i = 5; i < parameter_count; i += LENGTH(incompatible_parameter[i])) {
if (OPCODE(incompatible_parameter[i]) == SpvOpTypePointer) {
incompatible_parameter[i + 2] = SpvStorageClassPrivate;
check(!spirv_cap_version(incompatible_parameter, parameter_count, &kept),
"a NonWritable Private parameter is left alone");
incompatible_parameter[i + 2] = SpvStorageClassFunction;
check(!spirv_cap_version(incompatible_parameter, parameter_count, &kept),
"a NonWritable Function parameter is left alone");
}
}
check_pipelines();
if (failures)
printf("%d check(s) failed\n", failures);
else
printf("mali compat spirv ok\n");
return failures ? 1 : 0;
}
+90
View File
@@ -0,0 +1,90 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[2]
LAYER = ROOT / "tools/venus/layer"
DEPS = ROOT / "build/layer-deps"
HARNESS = Path(__file__).with_name("mali_compat_spirv_test.c")
def include(variable, default):
return Path(os.environ.get(variable, str(default)))
class MaliCompatSpirvTest(unittest.TestCase):
def test_the_layer_lowers_a_module_to_the_version_the_driver_knows(self):
compiler = shutil.which(os.environ.get("CC", "cc"))
if not compiler:
self.fail("A C compiler is required for the Mali compatibility layer test")
vulkan = include("MALI_COMPAT_VULKAN_INCLUDE", DEPS / "vulkan/include")
spirv = include("MALI_COMPAT_SPIRV_INCLUDE", DEPS / "spirv/include")
missing = [str(path) for path in (vulkan / "vulkan/vk_layer.h", spirv / "spirv/unified1/spirv.h")
if not path.is_file()]
if missing:
message = "Vulkan 1.4 and SPIR-V headers are required: " + ", ".join(missing)
if os.environ.get("CI"):
self.fail(message)
self.skipTest(message)
with tempfile.TemporaryDirectory(prefix="droiddeck-mali-spirv-") as tmp:
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"
debug = Path(tmp) / "debug.spv"
private_readonly = Path(tmp) / "private-readonly.spv"
parameter = Path(tmp) / "parameter.spv"
converted = Path(tmp) / "converted.spv"
built = subprocess.run(
[compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", "-UNDEBUG",
"-pthread", "-Dcalloc=test_calloc", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv),
str(HARNESS), str(LAYER / "spirv.c"), str(LAYER / "pipeline.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), str(extension), str(scalar), str(debug),
str(private_readonly), str(converted), str(parameter)],
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())
self.assertTrue(debug.is_file())
validator = shutil.which("spirv-val")
if not validator:
return
for original in (private_readonly, converted):
checked = subprocess.run([validator, "--target-env", "vulkan1.3", str(original)],
capture_output=True, text=True)
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
downgraded = bytearray(original.read_bytes())
downgraded[4:8] = (0x00010300).to_bytes(4, "little")
offset = 20
while True:
instruction = int.from_bytes(downgraded[offset:offset + 4], "little")
length, opcode = instruction >> 16, instruction & 0xffff
if opcode == 15:
downgraded[offset:offset + 4] = (((length - 1) << 16) | opcode).to_bytes(4, "little")
del downgraded[offset + (length - 1) * 4:offset + length * 4]
break
offset += length * 4
original.write_bytes(downgraded)
checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(original)],
capture_output=True, text=True)
self.assertNotEqual(checked.returncode, 0, "the 1.3 form must be rejected")
expected = "NonWritable" if original == private_readonly else "UConvert"
self.assertIn(expected, checked.stdout + checked.stderr)
for lowered in (module, guest, extension, scalar, debug, parameter):
checked = subprocess.run([validator, "--target-env", "vulkan1.1", str(lowered)],
capture_output=True, text=True)
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
if __name__ == "__main__":
unittest.main()
+11 -6
View File
@@ -615,6 +615,7 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev
inst->GetPhysicalDeviceMemoryProperties(physical_device, &dev->memory_properties);
VkPhysicalDeviceProperties properties;
inst->GetPhysicalDeviceProperties(physical_device, &properties);
dev->active = inst->active && (requested != 0 || properties.apiVersion >= VK_API_VERSION_1_2);
uint32_t components = properties.limits.maxVertexOutputComponents;
if (properties.limits.maxFragmentInputComponents < components)
components = properties.limits.maxFragmentInputComponents;
@@ -672,15 +673,18 @@ struct hook {
const char *name;
PFN_vkVoidFunction function;
uint32_t gaps;
bool always;
};
#define HOOK(name, gaps) {"vk" #name, (PFN_vkVoidFunction)name, gaps}
#define HOOK_ALIAS(name, alias, gaps) {"vk" #alias, (PFN_vkVoidFunction)name, gaps}
#define HOOK(name, gaps) {"vk" #name, (PFN_vkVoidFunction)name, gaps, false}
#define HOOK_ALIAS(name, alias, gaps) {"vk" #alias, (PFN_vkVoidFunction)name, gaps, false}
#define HOOK_ALWAYS(name) {"vk" #name, (PFN_vkVoidFunction)name, 0, true}
static const struct hook device_hooks[] = {
HOOK(CreateShaderModule, GAPS_SHADER | GAP_DUAL_SRC_BLEND),
HOOK(DestroyShaderModule, GAPS_SHADER | GAP_DUAL_SRC_BLEND),
HOOK(CreateGraphicsPipelines, GAPS_PIPELINE),
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),
@@ -756,7 +760,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 ((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;
+7
View File
@@ -89,6 +89,7 @@ struct instance {
X(CreateShaderModule) \
X(DestroyShaderModule) \
X(CreateGraphicsPipelines) \
X(CreateComputePipelines) \
X(CmdSetViewport) \
X(CmdSetScissor) \
X(CmdSetViewportWithCount) \
@@ -145,6 +146,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. */
@@ -185,6 +187,8 @@ struct spirv_code {
bool spirv_uses_distances(const uint32_t *words, size_t count);
/* The module with clip and cull distances taken out; false when it cannot be rewritten. */
bool spirv_strip_distances(const uint32_t *words, size_t count, struct spirv_code *out);
uint32_t spirv_version(const uint32_t *words, size_t count);
bool spirv_cap_version(const uint32_t *words, size_t count, struct spirv_code *out);
bool spirv_uses_dual_source(const uint32_t *words, size_t count);
bool spirv_strip_dual_source(const uint32_t *words, size_t count, struct spirv_code *out);
@@ -213,6 +217,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,
+204 -94
View File
@@ -5,6 +5,13 @@
#define HANDLE(h) ((uint64_t)(uintptr_t)(h))
#define MAX_STAGES 8
#define MAX_LINKS 8
static void log_spirv_version(const char *what, const uint32_t *words, size_t count)
{
uint32_t version = spirv_version(words, count);
layer_log("shader module: SPIR-V %u.%u %s (%zu words)", (version >> 16) & 0xff, (version >> 8) & 0xff, what, count);
}
/* A module with distances goes to the driver without them; its original stays here, for the
* pipeline that can emulate its clip distances. */
@@ -12,49 +19,57 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade
const VkAllocationCallbacks *allocator, VkShaderModule *module)
{
struct device *dev = device_from_handle(device);
VkShaderModuleCreateInfo copy = *info;
size_t count = info->codeSize / sizeof(uint32_t);
struct spirv_code capped = {0};
bool have_capped = spirv_cap_version(info->pCode, count, &capped);
if (have_capped)
log_spirv_version("lowered to 1.3", info->pCode, count);
else if (spirv_version(info->pCode, count) > 0x00010300u)
log_spirv_version("left as it is", info->pCode, count);
const uint32_t *code = have_capped ? capped.words : info->pCode;
size_t words = have_capped ? capped.count : count;
struct spirv_code single = {0};
if ((dev->emulated & GAP_DUAL_SRC_BLEND) && spirv_uses_dual_source(info->pCode, count)) {
if (spirv_strip_dual_source(info->pCode, count, &single)) {
copy.pCode = single.words;
copy.codeSize = single.count * sizeof(uint32_t);
count = single.count;
if ((dev->emulated & GAP_DUAL_SRC_BLEND) && spirv_uses_dual_source(code, words)) {
if (spirv_strip_dual_source(code, words, &single)) {
code = single.words;
words = single.count;
} else {
layer_log("could not take the second blend source out of a shader module");
}
}
if (!(dev->emulated & GAPS_SHADER) || !spirv_uses_distances(copy.pCode, count)) {
VkResult result = dev->CreateShaderModule(device, &copy, allocator, module);
free(single.words);
return result;
}
struct spirv_code stripped;
if (!spirv_strip_distances(copy.pCode, count, &stripped)) {
layer_log("could not take the clip and cull distances out of a shader module");
VkResult result = dev->CreateShaderModule(device, &copy, allocator, module);
free(single.words);
return result;
struct spirv_code stripped = {0};
bool have_stripped = false;
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");
}
struct spirv_code *original = malloc(sizeof(*original));
original->count = count;
original->words = malloc(copy.codeSize);
memcpy(original->words, copy.pCode, copy.codeSize);
free(single.words);
copy.pCode = stripped.words;
copy.codeSize = stripped.count * sizeof(uint32_t);
if (!have_stripped && code == info->pCode)
return dev->CreateShaderModule(device, info, allocator, module);
VkShaderModuleCreateInfo copy = *info;
copy.pCode = have_stripped ? stripped.words : code;
copy.codeSize = (have_stripped ? stripped.count : words) * sizeof(uint32_t);
VkResult result = dev->CreateShaderModule(device, &copy, allocator, module);
free(stripped.words);
if (result != VK_SUCCESS) {
free(original->words);
free(original);
if (result != VK_SUCCESS || !have_stripped) {
free(capped.words);
free(single.words);
free(stripped.words);
return result;
}
struct spirv_code *original = malloc(sizeof(*original));
original->count = words;
original->words = malloc(words * sizeof(uint32_t));
memcpy(original->words, code, words * sizeof(uint32_t));
pthread_mutex_lock(&dev->lock);
map_put(&dev->shader_modules, HANDLE(*module), original);
pthread_mutex_unlock(&dev->lock);
free(capped.words);
free(single.words);
free(stripped.words);
return VK_SUCCESS;
}
@@ -76,9 +91,10 @@ struct stage_code {
const uint32_t *words;
size_t count;
/* The application's own, when the code comes inline in the stage. */
VkShaderModuleCreateInfo *inline_info;
const VkShaderModuleCreateInfo *inline_info;
/* A module whose distances were already taken out at creation. */
bool stripped_module;
struct spirv_code capped;
};
static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreateInfo *stage, struct stage_code *code)
@@ -86,9 +102,16 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat
memset(code, 0, sizeof(*code));
for (const VkBaseInStructure *s = stage->pNext; s; s = s->pNext) {
if (s->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) {
code->inline_info = (VkShaderModuleCreateInfo *)s;
code->inline_info = (const VkShaderModuleCreateInfo *)s;
code->words = code->inline_info->pCode;
code->count = code->inline_info->codeSize / sizeof(uint32_t);
if (spirv_cap_version(code->words, code->count, &code->capped)) {
log_spirv_version("lowered to 1.3", code->words, code->count);
code->words = code->capped.words;
code->count = code->capped.count;
} else if (spirv_version(code->words, code->count) > 0x00010300u) {
log_spirv_version("left as it is", code->words, code->count);
}
return;
}
}
@@ -104,6 +127,15 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat
}
}
union stage_link {
VkBaseInStructure base;
VkDebugUtilsObjectNameInfoEXT name;
VkPipelineRobustnessCreateInfo robustness;
VkPipelineShaderStageModuleIdentifierCreateInfoEXT identifier;
VkPipelineShaderStageRequiredSubgroupSizeCreateInfo subgroup;
VkShaderModuleValidationCacheCreateInfoEXT validation_cache;
};
struct pipeline_rewrite {
VkPipelineRasterizationStateCreateInfo rasterization;
VkPipelineColorBlendStateCreateInfo blend;
@@ -116,12 +148,71 @@ struct pipeline_rewrite {
struct spirv_code code[MAX_STAGES];
struct spirv_code single[MAX_STAGES];
VkShaderModule temporary[MAX_STAGES];
/* Rewritten where they are, when they are not first in their stage's chain. */
VkShaderModuleCreateInfo *in_place[MAX_STAGES];
const uint32_t *in_place_code[MAX_STAGES];
size_t in_place_size[MAX_STAGES];
union stage_link links[MAX_STAGES][MAX_LINKS];
};
static size_t link_size(VkStructureType type)
{
switch (type) {
case VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT:
return sizeof(VkDebugUtilsObjectNameInfoEXT);
case VK_STRUCTURE_TYPE_PIPELINE_ROBUSTNESS_CREATE_INFO:
return sizeof(VkPipelineRobustnessCreateInfo);
case VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_MODULE_IDENTIFIER_CREATE_INFO_EXT:
return sizeof(VkPipelineShaderStageModuleIdentifierCreateInfoEXT);
case VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_REQUIRED_SUBGROUP_SIZE_CREATE_INFO:
return sizeof(VkPipelineShaderStageRequiredSubgroupSizeCreateInfo);
case VK_STRUCTURE_TYPE_SHADER_MODULE_VALIDATION_CACHE_CREATE_INFO_EXT:
return sizeof(VkShaderModuleValidationCacheCreateInfoEXT);
default:
return 0;
}
}
static bool link_inline_code(VkPipelineShaderStageCreateInfo *stage, struct pipeline_rewrite *rw,
const struct stage_code *code, uint32_t i)
{
union stage_link *links = rw->links[i];
const VkBaseInStructure **next = (const VkBaseInStructure **)&stage->pNext;
uint32_t used = 0;
for (const VkBaseInStructure *s = stage->pNext; s != (const VkBaseInStructure *)code->inline_info;
s = s->pNext) {
size_t size = link_size(s->sType);
if (!size || used == MAX_LINKS)
return false;
memcpy(&links[used], s, size);
*next = &links[used].base;
next = (const VkBaseInStructure **)&links[used].base.pNext;
used++;
}
rw->modules[i] = *code->inline_info;
rw->modules[i].pCode = rw->code[i].words;
rw->modules[i].codeSize = rw->code[i].count * sizeof(uint32_t);
*next = (const VkBaseInStructure *)&rw->modules[i];
return true;
}
static void use_stage_code(struct device *dev, VkPipelineShaderStageCreateInfo *stage, struct pipeline_rewrite *rw,
const struct stage_code *code, uint32_t i)
{
const struct spirv_code *out = &rw->code[i];
if (code->inline_info) {
const void *app_chain = stage->pNext;
if (!link_inline_code(stage, rw, code, i)) {
stage->pNext = app_chain;
layer_log("could not rewrite a pipeline stage's inline shader code");
}
} else {
VkShaderModuleCreateInfo module_info = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = out->count * sizeof(uint32_t),
.pCode = out->words,
};
if (dev->CreateShaderModule(dev->handle, &module_info, NULL, &rw->temporary[i]) == VK_SUCCESS)
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)
{
@@ -129,27 +220,7 @@ static void use_code(struct device *dev, VkGraphicsPipelineCreateInfo *info, str
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) {
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];
} else if (code->inline_info) {
rw->in_place[i] = code->inline_info;
rw->in_place_code[i] = code->inline_info->pCode;
rw->in_place_size[i] = code->inline_info->codeSize;
code->inline_info->pCode = out->words;
code->inline_info->codeSize = out->count * sizeof(uint32_t);
} else {
VkShaderModuleCreateInfo module_info = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = out->count * sizeof(uint32_t),
.pCode = out->words,
};
if (dev->CreateShaderModule(dev->handle, &module_info, NULL, &rw->temporary[i]) == VK_SUCCESS)
rw->stages[i].module = rw->temporary[i];
}
use_stage_code(dev, &rw->stages[i], rw, code, i);
}
static int stage_index(const VkGraphicsPipelineCreateInfo *info, VkShaderStageFlagBits stage)
@@ -174,6 +245,7 @@ static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *in
struct stage_code code[MAX_STAGES];
bool uses[MAX_STAGES];
bool any = false;
bool distances = (dev->emulated & GAPS_SHADER) != 0;
for (uint32_t i = 0; i < n; i++) {
find_stage_code(dev, &info->pStages[i], &code[i]);
if ((dev->emulated & GAP_DUAL_SRC_BLEND) && code[i].inline_info &&
@@ -185,40 +257,40 @@ static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *in
layer_log("could not take the second blend source out of a pipeline's shader");
}
}
uses[i] = (dev->emulated & GAPS_SHADER) && code[i].words && spirv_uses_distances(code[i].words, code[i].count);
any |= uses[i] || rw->single[i].words;
uses[i] = distances && code[i].words && spirv_uses_distances(code[i].words, code[i].count);
any |= uses[i];
}
if (!any)
return;
int producer = stage_index(info, VK_SHADER_STAGE_GEOMETRY_BIT);
if (producer < 0)
producer = stage_index(info, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT);
if (producer < 0)
producer = stage_index(info, VK_SHADER_STAGE_VERTEX_BIT);
int consumer = stage_index(info, VK_SHADER_STAGE_FRAGMENT_BIT);
bool emulate = (dev->emulated & GAP_CLIP_DISTANCE) && producer >= 0 && consumer >= 0 && uses[producer] &&
code[consumer].words;
for (uint32_t i = 0; i < n && emulate; i++) {
if ((int)i != producer && (int)i != consumer && uses[i])
emulate = false;
}
if (emulate) {
struct clip_info produced, consumed;
spirv_clip_info(code[producer].words, code[producer].count, CLIP_PRODUCER, &produced);
spirv_clip_info(code[consumer].words, code[consumer].count, CLIP_CONSUMER, &consumed);
uint32_t location = produced.locations_used > consumed.locations_used ? produced.locations_used
: consumed.locations_used;
emulate = produced.has_clip && location != UINT32_MAX &&
location + produced.clip_count <= dev->max_varying_locations &&
spirv_emulate_clip(code[producer].words, code[producer].count, CLIP_PRODUCER, location,
produced.clip_count, &rw->code[producer]) &&
spirv_emulate_clip(code[consumer].words, code[consumer].count, CLIP_CONSUMER, location,
produced.clip_count, &rw->code[consumer]);
if (!emulate) {
free(rw->code[producer].words);
free(rw->code[consumer].words);
rw->code[producer] = rw->code[consumer] = (struct spirv_code){0};
if (any) {
int producer = stage_index(info, VK_SHADER_STAGE_GEOMETRY_BIT);
if (producer < 0)
producer = stage_index(info, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT);
if (producer < 0)
producer = stage_index(info, VK_SHADER_STAGE_VERTEX_BIT);
int consumer = stage_index(info, VK_SHADER_STAGE_FRAGMENT_BIT);
bool emulate = (dev->emulated & GAP_CLIP_DISTANCE) && producer >= 0 && consumer >= 0 && uses[producer] &&
code[consumer].words;
for (uint32_t i = 0; i < n && emulate; i++) {
if ((int)i != producer && (int)i != consumer && uses[i])
emulate = false;
}
if (emulate) {
struct clip_info produced, consumed;
spirv_clip_info(code[producer].words, code[producer].count, CLIP_PRODUCER, &produced);
spirv_clip_info(code[consumer].words, code[consumer].count, CLIP_CONSUMER, &consumed);
uint32_t location = produced.locations_used > consumed.locations_used ? produced.locations_used
: consumed.locations_used;
emulate = produced.has_clip && location != UINT32_MAX &&
location + produced.clip_count <= dev->max_varying_locations &&
spirv_emulate_clip(code[producer].words, code[producer].count, CLIP_PRODUCER, location,
produced.clip_count, &rw->code[producer]) &&
spirv_emulate_clip(code[consumer].words, code[consumer].count, CLIP_CONSUMER, location,
produced.clip_count, &rw->code[consumer]);
if (!emulate) {
free(rw->code[producer].words);
free(rw->code[consumer].words);
rw->code[producer] = rw->code[consumer] = (struct spirv_code){0};
}
}
}
@@ -231,8 +303,13 @@ static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *in
rw->code[i] = rw->single[i];
rw->single[i] = (struct spirv_code){0};
}
if (!rw->code[i].words && code[i].capped.words) {
rw->code[i] = code[i].capped;
code[i].capped.words = NULL;
}
if (rw->code[i].words)
use_code(dev, info, rw, &code[i], i);
free(code[i].capped.words);
}
}
@@ -329,8 +406,7 @@ static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *i
if (rw->attachments)
info->pColorBlendState = &rw->blend;
}
if (dev->emulated & (GAPS_SHADER | GAP_DUAL_SRC_BLEND))
rewrite_shaders(dev, info, rw);
rewrite_shaders(dev, info, rw);
}
static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw)
@@ -338,10 +414,6 @@ static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw)
if (rw->clip_prev)
rw->clip_prev->pNext = rw->clip_node;
for (uint32_t i = 0; i < MAX_STAGES; i++) {
if (rw->in_place[i]) {
rw->in_place[i]->pCode = rw->in_place_code[i];
rw->in_place[i]->codeSize = rw->in_place_size[i];
}
if (rw->temporary[i] != VK_NULL_HANDLE)
dev->DestroyShaderModule(dev->handle, rw->temporary[i], NULL);
free(rw->code[i].words);
@@ -355,8 +427,15 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipeli
const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
{
struct device *dev = device_from_handle(device);
VkGraphicsPipelineCreateInfo *copies = malloc(count * sizeof(*copies));
VkGraphicsPipelineCreateInfo *copies = calloc(count, sizeof(*copies));
struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites));
if (count && (!copies || !rewrites)) {
for (uint32_t i = 0; i < count; i++)
pipelines[i] = VK_NULL_HANDLE;
free(copies);
free(rewrites);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
for (uint32_t i = 0; i < count; i++) {
copies[i] = infos[i];
rewrite_pipeline(dev, &copies[i], &rewrites[i]);
@@ -369,6 +448,37 @@ 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 = calloc(count, sizeof(*copies));
struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites));
if (count && (!copies || !rewrites)) {
for (uint32_t i = 0; i < count; i++)
pipelines[i] = VK_NULL_HANDLE;
free(copies);
free(rewrites);
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
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)
+299
View File
@@ -15,6 +15,7 @@
#define OPCODE(word) ((word) & 0xffffu)
#define LENGTH(word) ((word) >> 16)
#define VERSION_1_3 0x00010300u
#define VERSION_1_4 0x00010400u
struct words {
@@ -103,6 +104,7 @@ static bool declares_result_second(uint32_t op)
case SpvOpUndef:
case SpvOpVariable:
case SpvOpFunction:
case SpvOpFunctionParameter:
return true;
default:
return false;
@@ -875,3 +877,300 @@ bool spirv_emulate_clip(const uint32_t *words, size_t count, enum clip_role role
unload(&m);
return done;
}
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 names_prefix(const uint32_t *words, uint32_t count, const char *prefix)
{
size_t size = strlen(prefix);
return count * sizeof(uint32_t) > size && !memcmp(words, prefix, size);
}
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 bool declares_capability(const struct module *m, uint32_t capability)
{
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
if (OPCODE(m->w[i]) == SpvOpCapability && LENGTH(m->w[i]) >= 2 && m->w[i + 1] == capability)
return true;
}
return false;
}
static uint32_t image_operands_at(uint32_t op)
{
switch (op) {
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 valid_in_1_3(const struct module *m, size_t i)
{
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:
if (length < 3 || !AVAILABLE(m, newer_decorations, m->w[i + 2]))
return false;
if (m->w[i + 2] == SpvDecorationNonWritable) {
size_t d = definition(m, m->w[i + 1]);
if (!d || (OPCODE(m->w[d]) == SpvOpVariable &&
(LENGTH(m->w[d]) < 4 || m->w[d + 3] == SpvStorageClassPrivate ||
m->w[d + 3] == SpvStorageClassFunction)))
return false;
if (OPCODE(m->w[d]) == SpvOpFunctionParameter) {
size_t type = LENGTH(m->w[d]) >= 3 ? definition(m, m->w[d + 1]) : 0;
if (!type || OPCODE(m->w[type]) != SpvOpTypePointer || LENGTH(m->w[type]) < 4 ||
m->w[type + 2] == SpvStorageClassPrivate || m->w[type + 2] == SpvStorageClassFunction)
return false;
}
}
return true;
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 SpvOpSpecConstantOp:
return length >= 5 && (m->w[i + 3] != SpvOpUConvert ||
declares_capability(m, SpvCapabilityKernel) ||
declares_extension(m, "SPV_AMD_gpu_shader_int16"));
case SpvOpExtInstImport:
return length >= 3 && (!names_prefix(&m->w[i + 2], length - 2, "NonSemantic.") ||
declares_extension(m, "SPV_KHR_non_semantic_info"));
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);
}
}
}
static bool variable_storage(const struct module *m, uint32_t id, uint32_t *storage)
{
size_t d = definition(m, id);
if (!d || OPCODE(m->w[d]) != SpvOpVariable)
return false;
*storage = m->w[d + 3];
return true;
}
static void trim_entry_interfaces(struct module *m)
{
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
if (OPCODE(m->w[i]) != SpvOpEntryPoint)
continue;
uint32_t length = LENGTH(m->w[i]);
if (length < 4)
continue;
uint32_t first = 3 + string_words(&m->w[i + 3], length - 3);
for (uint32_t k = first; k < length; k++) {
uint32_t id = m->w[i + k];
uint32_t storage;
if (id < m->original_bound && variable_storage(m, id, &storage) &&
storage != SpvStorageClassInput && storage != SpvStorageClassOutput)
m->interface_remove[id] = 1;
}
}
}
uint32_t spirv_version(const uint32_t *words, size_t count)
{
if (count < 5 || words[0] != SpvMagicNumber)
return 0;
return words[1];
}
bool spirv_cap_version(const uint32_t *words, size_t count, struct spirv_code *out)
{
if (count < 5 || words[0] != SpvMagicNumber || words[1] <= VERSION_1_3)
return false;
struct module m;
if (!load(&m, words, count))
return false;
for (size_t i = 5; i < m.count; i += LENGTH(m.w[i])) {
if (!valid_in_1_3(&m, i)) {
unload(&m);
return false;
}
}
for (size_t i = 5; i < m.count; i += LENGTH(m.w[i])) {
if (OPCODE(m.w[i]) == SpvOpTerminateInvocation)
m.w[i] = (LENGTH(m.w[i]) << 16) | SpvOpKill;
}
trim_entry_interfaces(&m);
m.w[1] = VERSION_1_3;
bool done = finish(&m, out);
unload(&m);
return done;
}