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

This commit is contained in:
maxjivi05 committed 2026-11-13 17:58:55 -05:00
1 parent 0b8a3f1ee8
commit 9746392a8d
8 files changed
+440 -54

No files matched your search

+11 -1
View File
@@ -842,7 +842,7 @@ jobs:
run: test "$(git rev-parse 'HEAD^{tree}')" = "$XBOX_SOURCE_TREE"
- name: Install Python test dependencies
timeout-minutes: 7
run: bash tools/ci/install-ubuntu-packages.sh python3-yaml gcc-aarch64-linux-gnu libc6-dev-arm64-cross qemu-user libvulkan-dev mesa-vulkan-drivers
run: bash tools/ci/install-ubuntu-packages.sh python3-yaml gcc-aarch64-linux-gnu libc6-dev-arm64-cross qemu-user libvulkan-dev mesa-vulkan-drivers spirv-tools
- name: Resolve the pinned Android NDK
run: bash tools/ci/resolve-ndk.sh
@@ -872,6 +872,16 @@ jobs:
-ldl -o /tmp/test-presenter-shutdown
timeout 10 /tmp/test-presenter-shutdown
- name: Fetch the Mali compatibility layer's headers
run: |
set -euo pipefail
sdk=$(sed -n 's/^SDK=//p' tools/venus/ci-build.sh)
bcdec=$(grep -o 'https://raw.githubusercontent.com/iOrange/bcdec/[^ ]*' tools/venus/ci-build.sh)
mkdir -p build/layer-deps/bcdec
git -c advice.detachedHead=false clone -q --depth 1 -b "$sdk" https://github.com/KhronosGroup/Vulkan-Headers.git build/layer-deps/vulkan
git -c advice.detachedHead=false clone -q --depth 1 -b "$sdk" https://github.com/KhronosGroup/SPIRV-Headers.git build/layer-deps/spirv
curl -sSfL -o build/layer-deps/bcdec/bcdec.h "$bcdec"
- name: Steam import and game environment tests
run: bash tools/ci/run-python-tests.sh
+20 -2
View File
@@ -123,10 +123,26 @@ What it does, per feature:
- Decoded texels go through the layer's own staging buffers, recycled when the command buffer is reset.
- Copies from a BC image into a buffer, and between BC and uncompressed images, are skipped with a warning.
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`). 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.
- 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.
- `spirv.c`: the SPIR-V rewriter for clip and cull distances, and the version cap.
- `bc.c`: BC formats, images and uploads.
- `VkLayer_droiddeck_mali_compat.json`: implicit layer manifest.
@@ -142,7 +158,9 @@ How it ships:
- Guest paths: `usr/local/lib/droiddeck-mali/` and `usr/local/share/vulkan/implicit_layer.d/`.
Not done yet:
- Unit tests for the SPIR-V rewriter. It is untested beyond DXVK's shaders, and unknown whether Silksong's shaders used clip distances at all.
- Unit tests for the clip and cull distance rewrite. The version cap has one
(`tools/tests/test_mali_compat_spirv.py`); the distance rewrite is untested beyond DXVK's shaders,
and unknown whether Silksong's shaders used clip distances at all.
- Performance: BC decoding on the CPU makes loads slow. A compute-shader decoder would be the next step.
## Changes on the branch
+165
View File
@@ -0,0 +1,165 @@
#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 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 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);
}
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");
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);
}
if (failures)
printf("%d check(s) failed\n", failures);
else
printf("mali compat spirv ok\n");
return failures ? 1 : 0;
}
+58
View File
@@ -0,0 +1,58 @@
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"
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)
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())
validator = shutil.which("spirv-val")
if not validator:
return
for lowered in (module, guest):
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()
+8 -6
View File
@@ -661,15 +661,17 @@ 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),
HOOK(DestroyShaderModule, GAPS_SHADER),
HOOK(CreateGraphicsPipelines, GAPS_PIPELINE),
HOOK_ALWAYS(CreateShaderModule),
HOOK_ALWAYS(DestroyShaderModule),
HOOK_ALWAYS(CreateGraphicsPipelines),
HOOK(CmdSetViewport, GAP_MULTI_VIEWPORT),
HOOK(CmdSetScissor, GAP_MULTI_VIEWPORT),
HOOK(CmdSetViewportWithCount, GAP_MULTI_VIEWPORT),
@@ -744,7 +746,7 @@ 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))
if ((device_hooks[i].always || (dev->emulated & device_hooks[i].gaps)) && !strcmp(name, device_hooks[i].name))
return device_hooks[i].function;
}
return next;
+2
View File
@@ -183,6 +183,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);
enum clip_role { CLIP_STRIP, CLIP_PRODUCER, CLIP_CONSUMER };
+77 -45
View File
@@ -6,6 +6,12 @@
#define HANDLE(h) ((uint64_t)(uintptr_t)(h))
#define MAX_STAGES 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. */
VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *info,
@@ -13,26 +19,39 @@ VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShade
{
struct device *dev = device_from_handle(device);
size_t count = info->codeSize / sizeof(uint32_t);
if (!spirv_uses_distances(info->pCode, count))
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 stripped = {0};
bool have_stripped = false;
if (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");
}
if (!have_stripped && !have_capped)
return dev->CreateShaderModule(device, info, allocator, module);
struct spirv_code stripped;
if (!spirv_strip_distances(info->pCode, count, &stripped)) {
layer_log("could not take the clip and cull distances out of a shader module");
return dev->CreateShaderModule(device, info, allocator, module);
}
struct spirv_code final = have_stripped ? stripped : capped;
VkShaderModuleCreateInfo copy = *info;
copy.pCode = stripped.words;
copy.codeSize = stripped.count * sizeof(uint32_t);
copy.pCode = final.words;
copy.codeSize = final.count * sizeof(uint32_t);
VkResult result = dev->CreateShaderModule(device, &copy, allocator, module);
free(capped.words);
free(stripped.words);
if (result != VK_SUCCESS)
if (result != VK_SUCCESS || !have_stripped)
return result;
struct spirv_code *original = malloc(sizeof(*original));
original->count = count;
original->words = malloc(info->codeSize);
memcpy(original->words, info->pCode, info->codeSize);
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);
@@ -60,6 +79,7 @@ struct stage_code {
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)
@@ -70,6 +90,13 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat
code->inline_info = (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;
}
}
@@ -152,42 +179,43 @@ 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]);
uses[i] = code[i].words && spirv_uses_distances(code[i].words, code[i].count);
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};
}
}
}
@@ -196,8 +224,13 @@ static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *in
!spirv_strip_distances(code[i].words, code[i].count, &rw->code[i])) {
layer_log("could not take the clip and cull distances out of a pipeline's shader");
}
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);
}
}
@@ -241,8 +274,7 @@ static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *i
rw->viewport.scissorCount = 1;
info->pViewportState = &rw->viewport;
}
if (dev->emulated & GAPS_SHADER)
rewrite_shaders(dev, info, rw);
rewrite_shaders(dev, info, rw);
}
static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw)
+99
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 {
@@ -831,3 +832,101 @@ bool spirv_emulate_clip(const uint32_t *words, size_t count, enum clip_role role
unload(&m);
return done;
}
static bool needs_newer_version(uint32_t op)
{
switch (op) {
case SpvOpCopyLogical:
case SpvOpPtrEqual:
case SpvOpPtrNotEqual:
case SpvOpPtrDiff:
case SpvOpDemoteToHelperInvocation:
return true;
default:
return false;
}
}
static bool needs_newer_capability(uint32_t capability)
{
switch (capability) {
case SpvCapabilityShaderLayer:
case SpvCapabilityShaderViewportIndex:
case SpvCapabilityUniformDecoration:
return true;
default:
return false;
}
}
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])) {
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)) {
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;
}