diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 95d036381a..626d744596 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -849,7 +849,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 @@ -879,6 +879,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 diff --git a/docs/development/mali-venus.md b/docs/development/mali-venus.md index 8a722ff7e0..fa72294e42 100644 --- a/docs/development/mali-venus.md +++ b/docs/development/mali-venus.md @@ -122,11 +122,16 @@ What it does, per feature: - The source must be memory the application has mapped; DXVK's staging memory is. - 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. +- `dualSrcBlend`: reports maxFragmentDualSrcAttachments 1. DXVK 2.x and 3.x require it for D3D11. + - Fragment outputs with `Index 1` become Private, in shader modules and in inline stage code. + - SRC1 blend factors become source alpha: `SRC1_COLOR` and `SRC1_ALPHA` → `SRC_ALPHA`, their `ONE_MINUS_` forms → `ONE_MINUS_SRC_ALPHA`. + - 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. 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 second blend source. - `bc.c`: BC formats, images and uploads. - `VkLayer_droiddeck_mali_compat.json`: implicit layer manifest. @@ -142,7 +147,8 @@ 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 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`. - Performance: BC decoding on the CPU makes loads slow. A compute-shader decoder would be the next step. ## Changes on the branch diff --git a/tools/tests/mali_compat_layer_test.c b/tools/tests/mali_compat_layer_test.c new file mode 100644 index 0000000000..640460685c --- /dev/null +++ b/tools/tests/mali_compat_layer_test.c @@ -0,0 +1,490 @@ +#include "mali_compat.h" + +#include +#include +#include +#include +#include + +VKAPI_ATTR VkResult VKAPI_CALL vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *interface); + +struct object { void *dispatch; }; +static int instance_table, device_table; +static struct object mock_instance = {&instance_table}; +static struct object mock_lacking = {&instance_table}, mock_capable = {&instance_table}; +static struct object mock_device = {&device_table}, mock_command_buffer = {&device_table}; + +static VkBool32 driver_saw_dual_source; +static uint32_t *driver_code; +static size_t driver_code_count; +static VkPipelineColorBlendAttachmentState driver_attachments[4]; +static uint32_t driver_attachment_count; +static VkColorBlendEquationEXT driver_equations[4]; +static uint32_t driver_equation_count; + +#define MAIN 1 +#define VOID 2 +#define FUNCTION 3 +#define FLOAT 4 +#define VEC4 5 +#define OUTPUT_VEC4 6 +#define COLOR 7 +#define SECOND 8 +#define ONE 9 +#define ONES 10 +#define OUTPUT_FLOAT 11 +#define UINT 12 +#define ZERO 13 +#define LABEL 14 +#define CHANNEL 15 +#define BOUND 16 + +struct spirv { uint32_t w[256]; size_t n; }; + +static void op(struct spirv *s, SpvOp code, uint32_t count, const uint32_t *operands) +{ + s->w[s->n++] = (count + 1) << 16 | code; + for (uint32_t i = 0; i < count; i++) + s->w[s->n++] = operands[i]; +} + +#define OP(s, code, ...) \ + do { \ + const uint32_t operands_[] = {__VA_ARGS__}; \ + op(s, code, sizeof(operands_) / sizeof(operands_[0]), operands_); \ + } while (0) + +static void fragment_shader(struct spirv *s, uint32_t version, bool dual_source) +{ + s->n = 0; + const uint32_t header[5] = {SpvMagicNumber, version, 0, BOUND, 0}; + memcpy(s->w, header, sizeof(header)); + s->n = 5; + OP(s, SpvOpCapability, SpvCapabilityShader); + OP(s, SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450); + OP(s, SpvOpEntryPoint, SpvExecutionModelFragment, MAIN, 0x6e69616d, 0, COLOR, SECOND); + OP(s, SpvOpExecutionMode, MAIN, SpvExecutionModeOriginUpperLeft); + OP(s, SpvOpDecorate, COLOR, SpvDecorationLocation, 0); + if (dual_source) { + OP(s, SpvOpDecorate, COLOR, SpvDecorationIndex, 0); + OP(s, SpvOpDecorate, SECOND, SpvDecorationLocation, 0); + OP(s, SpvOpDecorate, SECOND, SpvDecorationIndex, 1); + } else { + OP(s, SpvOpDecorate, SECOND, SpvDecorationLocation, 1); + } + OP(s, SpvOpTypeVoid, VOID); + OP(s, SpvOpTypeFunction, FUNCTION, VOID); + OP(s, SpvOpTypeFloat, FLOAT, 32); + OP(s, SpvOpTypeVector, VEC4, FLOAT, 4); + OP(s, SpvOpTypePointer, OUTPUT_VEC4, SpvStorageClassOutput, VEC4); + OP(s, SpvOpTypePointer, OUTPUT_FLOAT, SpvStorageClassOutput, FLOAT); + OP(s, SpvOpTypeInt, UINT, 32, 0); + OP(s, SpvOpConstant, FLOAT, ONE, 0x3f800000); + OP(s, SpvOpConstantComposite, VEC4, ONES, ONE, ONE, ONE, ONE); + OP(s, SpvOpConstant, UINT, ZERO, 0); + OP(s, SpvOpVariable, OUTPUT_VEC4, COLOR, SpvStorageClassOutput); + OP(s, SpvOpVariable, OUTPUT_VEC4, SECOND, SpvStorageClassOutput); + OP(s, SpvOpFunction, VOID, MAIN, SpvFunctionControlMaskNone, FUNCTION); + OP(s, SpvOpLabel, LABEL); + OP(s, SpvOpStore, COLOR, ONES); + OP(s, SpvOpStore, SECOND, ONES); + OP(s, SpvOpAccessChain, OUTPUT_FLOAT, CHANNEL, SECOND, ZERO); + OP(s, SpvOpStore, CHANNEL, ONE); + s->w[s->n++] = 1u << 16 | SpvOpReturn; + s->w[s->n++] = 1u << 16 | SpvOpFunctionEnd; +} + +static const uint32_t *find(const uint32_t *w, size_t count, SpvOp code, uint32_t position, uint32_t value) +{ + for (size_t i = 5; i < count; i += w[i] >> 16) { + if ((w[i] & 0xffff) == code && (w[i] >> 16) > position && w[i + position] == value) + return &w[i]; + } + return NULL; +} + +static uint32_t storage_of(const uint32_t *w, size_t count, uint32_t pointer_type) +{ + const uint32_t *type = find(w, count, SpvOpTypePointer, 1, pointer_type); + assert(type); + return type[2]; +} + +static bool in_interface(const uint32_t *w, size_t count, uint32_t id) +{ + const uint32_t *entry = find(w, count, SpvOpEntryPoint, 2, MAIN); + assert(entry); + for (uint32_t i = 5; i < entry[0] >> 16; i++) { + if (entry[i] == id) + return true; + } + return false; +} + +static void save(const char *directory, const char *name, const uint32_t *words, size_t count) +{ + if (!directory) + return; + char path[512]; + snprintf(path, sizeof(path), "%s/%s", directory, name); + FILE *file = fopen(path, "wb"); + assert(file); + assert(fwrite(words, sizeof(uint32_t), count, file) == count); + fclose(file); +} + +static void check_stripped(const uint32_t *w, size_t count, bool second_listed) +{ + assert(!spirv_uses_dual_source(w, count)); + const uint32_t *second = find(w, count, SpvOpVariable, 2, SECOND); + const uint32_t *color = find(w, count, SpvOpVariable, 2, COLOR); + assert(second && second[3] == SpvStorageClassPrivate); + assert(storage_of(w, count, second[1]) == SpvStorageClassPrivate); + assert(color && color[3] == SpvStorageClassOutput && color[1] == OUTPUT_VEC4); + assert(!find(w, count, SpvOpDecorate, 1, SECOND)); + const uint32_t *location = find(w, count, SpvOpDecorate, 1, COLOR); + assert(location && location[2] == SpvDecorationLocation && location[3] == 0); + const uint32_t *channel = find(w, count, SpvOpAccessChain, 2, CHANNEL); + assert(channel && storage_of(w, count, channel[1]) == SpvStorageClassPrivate); + assert(storage_of(w, count, OUTPUT_FLOAT) == SpvStorageClassOutput); + assert(in_interface(w, count, COLOR)); + assert(in_interface(w, count, SECOND) == second_listed); +} + +static void test_spirv(const char *directory) +{ + struct spirv dual, plain; + struct spirv_code out; + + fragment_shader(&dual, 0x00010000, true); + save(directory, "dual-1.0.spv", dual.w, dual.n); + assert(spirv_uses_dual_source(dual.w, dual.n)); + assert(spirv_strip_dual_source(dual.w, dual.n, &out)); + check_stripped(out.words, out.count, false); + save(directory, "stripped-1.0.spv", out.words, out.count); + free(out.words); + + fragment_shader(&dual, 0x00010400, true); + assert(spirv_strip_dual_source(dual.w, dual.n, &out)); + check_stripped(out.words, out.count, true); + save(directory, "stripped-1.4.spv", out.words, out.count); + free(out.words); + + fragment_shader(&plain, 0x00010000, false); + save(directory, "plain-1.0.spv", plain.w, plain.n); + assert(!spirv_uses_dual_source(plain.w, plain.n)); + assert(spirv_strip_dual_source(plain.w, plain.n, &out)); + assert(out.count == plain.n && !memcmp(out.words, plain.w, plain.n * sizeof(uint32_t))); + free(out.words); + + uint32_t truncated[5] = {SpvMagicNumber, 0x00010000, 0, BOUND, 0}; + assert(!spirv_uses_dual_source(truncated, 5)); + assert(!spirv_strip_dual_source(truncated, 5, &out)); +} + +static VkResult VKAPI_CALL mock_create_instance(const VkInstanceCreateInfo *info, const VkAllocationCallbacks *allocator, + VkInstance *instance) +{ + *instance = (VkInstance)&mock_instance; + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_destroy_instance(VkInstance instance, const VkAllocationCallbacks *allocator) {} + +static VkResult VKAPI_CALL mock_extensions(VkPhysicalDevice physical, const char *layer, uint32_t *count, + VkExtensionProperties *properties) +{ + *count = 0; + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_features(VkPhysicalDevice physical, VkPhysicalDeviceFeatures *features) +{ + memset(features, 0, sizeof(*features)); + features->dualSrcBlend = physical == (VkPhysicalDevice)&mock_capable; +} + +static void VKAPI_CALL mock_features2(VkPhysicalDevice physical, VkPhysicalDeviceFeatures2 *features) +{ + mock_features(physical, &features->features); +} + +static void VKAPI_CALL mock_properties(VkPhysicalDevice physical, VkPhysicalDeviceProperties *properties) +{ + memset(properties, 0, sizeof(*properties)); + strcpy(properties->deviceName, "mock"); + properties->limits.maxFragmentDualSrcAttachments = physical == (VkPhysicalDevice)&mock_capable; +} + +static void VKAPI_CALL mock_properties2(VkPhysicalDevice physical, VkPhysicalDeviceProperties2 *properties) +{ + mock_properties(physical, &properties->properties); +} + +static void VKAPI_CALL mock_memory_properties(VkPhysicalDevice physical, VkPhysicalDeviceMemoryProperties *properties) +{ + memset(properties, 0, sizeof(*properties)); +} + +static void VKAPI_CALL mock_format_properties2(VkPhysicalDevice physical, VkFormat format, VkFormatProperties2 *out) +{ + memset(&out->formatProperties, 0, sizeof(out->formatProperties)); +} + +static VkResult VKAPI_CALL mock_image_format_properties2(VkPhysicalDevice physical, + const VkPhysicalDeviceImageFormatInfo2 *info, + VkImageFormatProperties2 *properties) +{ + return VK_ERROR_FORMAT_NOT_SUPPORTED; +} + +static VkResult VKAPI_CALL mock_create_device(VkPhysicalDevice physical, const VkDeviceCreateInfo *info, + const VkAllocationCallbacks *allocator, VkDevice *device) +{ + driver_saw_dual_source = info->pEnabledFeatures && info->pEnabledFeatures->dualSrcBlend; + for (const VkBaseInStructure *p = info->pNext; p; p = p->pNext) { + if (p->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 && + ((const VkPhysicalDeviceFeatures2 *)p)->features.dualSrcBlend) + driver_saw_dual_source = VK_TRUE; + } + VkPhysicalDeviceFeatures supported; + mock_features(physical, &supported); + if (driver_saw_dual_source && !supported.dualSrcBlend) + return VK_ERROR_FEATURE_NOT_PRESENT; + *device = (VkDevice)&mock_device; + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_destroy_device(VkDevice device, const VkAllocationCallbacks *allocator) {} + +static void capture_code(const uint32_t *words, size_t size) +{ + free(driver_code); + driver_code_count = size / sizeof(uint32_t); + driver_code = malloc(size); + memcpy(driver_code, words, size); +} + +static VkResult VKAPI_CALL mock_create_shader_module(VkDevice device, const VkShaderModuleCreateInfo *info, + const VkAllocationCallbacks *allocator, VkShaderModule *module) +{ + capture_code(info->pCode, info->codeSize); + *module = (VkShaderModule)0x1000; + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_destroy_shader_module(VkDevice device, VkShaderModule module, + const VkAllocationCallbacks *allocator) {} + +static VkResult VKAPI_CALL mock_create_graphics_pipelines(VkDevice device, VkPipelineCache cache, uint32_t count, + const VkGraphicsPipelineCreateInfo *infos, + const VkAllocationCallbacks *allocator, VkPipeline *pipelines) +{ + const VkPipelineColorBlendStateCreateInfo *blend = infos[0].pColorBlendState; + driver_attachment_count = blend->attachmentCount; + memcpy(driver_attachments, blend->pAttachments, blend->attachmentCount * sizeof(*driver_attachments)); + const VkShaderModuleCreateInfo *module = infos[0].pStages[0].pNext; + assert(module && module->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO); + capture_code(module->pCode, module->codeSize); + pipelines[0] = (VkPipeline)0x2000; + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_set_blend_equation(VkCommandBuffer cmd, uint32_t first, uint32_t count, + const VkColorBlendEquationEXT *equations) +{ + for (uint32_t i = 0; i < count; i++) + driver_equations[first + i] = equations[i]; + if (first + count > driver_equation_count) + driver_equation_count = first + count; +} + +#define MOCK(name, function) if (!strcmp(requested, name)) return (PFN_vkVoidFunction)function +static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mock_gipa(VkInstance instance, const char *requested) +{ + MOCK("vkCreateInstance", mock_create_instance); + MOCK("vkDestroyInstance", mock_destroy_instance); + MOCK("vkCreateDevice", mock_create_device); + MOCK("vkEnumerateDeviceExtensionProperties", mock_extensions); + MOCK("vkGetPhysicalDeviceFeatures", mock_features); + MOCK("vkGetPhysicalDeviceFeatures2", mock_features2); + MOCK("vkGetPhysicalDeviceProperties", mock_properties); + MOCK("vkGetPhysicalDeviceProperties2", mock_properties2); + MOCK("vkGetPhysicalDeviceMemoryProperties", mock_memory_properties); + MOCK("vkGetPhysicalDeviceFormatProperties2", mock_format_properties2); + MOCK("vkGetPhysicalDeviceImageFormatProperties2", mock_image_format_properties2); + return NULL; +} + +static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mock_gdpa(VkDevice device, const char *requested) +{ + MOCK("vkDestroyDevice", mock_destroy_device); + MOCK("vkCreateShaderModule", mock_create_shader_module); + MOCK("vkDestroyShaderModule", mock_destroy_shader_module); + MOCK("vkCreateGraphicsPipelines", mock_create_graphics_pipelines); + MOCK("vkCmdSetColorBlendEquationEXT", mock_set_blend_equation); + return NULL; +} + +static PFN_vkGetInstanceProcAddr layer_gipa; +static PFN_vkGetDeviceProcAddr layer_gdpa; + +static VkResult create_device(VkPhysicalDevice physical, const void *chain, const VkPhysicalDeviceFeatures *enabled, + VkDevice *device) +{ + VkLayerDeviceLink link = {NULL, mock_gipa, mock_gdpa}; + VkLayerDeviceCreateInfo loader = {VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO, chain, VK_LAYER_LINK_INFO, {&link}}; + VkDeviceCreateInfo info = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &loader, 0, 0, NULL, 0, NULL, 0, NULL, enabled}; + PFN_vkCreateDevice create = (PFN_vkCreateDevice)layer_gipa((VkInstance)&mock_instance, "vkCreateDevice"); + return create(physical, &info, NULL, device); +} + +static void destroy_device(VkDevice device) +{ + ((PFN_vkDestroyDevice)layer_gdpa(device, "vkDestroyDevice"))(device, NULL); +} + +static const VkPipelineColorBlendAttachmentState dual_attachment = { + VK_TRUE, VK_BLEND_FACTOR_ONE, VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, VK_BLEND_OP_ADD, + VK_BLEND_FACTOR_SRC1_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA, VK_BLEND_OP_ADD, 0xf, +}; + +static const VkPipelineColorBlendAttachmentState plain_attachment = { + VK_TRUE, VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_CONSTANT_ALPHA, VK_BLEND_OP_ADD, + VK_BLEND_FACTOR_SRC_ALPHA, VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA, VK_BLEND_OP_ADD, 0xf, +}; + +static void create_pipeline(VkDevice device, VkPipelineColorBlendAttachmentState *attachments, + VkShaderModuleCreateInfo *module) +{ + VkPipelineShaderStageCreateInfo stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, module, 0, + VK_SHADER_STAGE_FRAGMENT_BIT, VK_NULL_HANDLE, "main", NULL}; + VkPipelineColorBlendStateCreateInfo blend = {VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, NULL, 0, + VK_FALSE, VK_LOGIC_OP_COPY, 2, attachments, {0}}; + VkGraphicsPipelineCreateInfo info = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .stageCount = 1, + .pStages = &stage, .pColorBlendState = &blend}; + VkPipeline pipeline; + PFN_vkCreateGraphicsPipelines create = + (PFN_vkCreateGraphicsPipelines)layer_gdpa(device, "vkCreateGraphicsPipelines"); + assert(create(device, VK_NULL_HANDLE, 1, &info, NULL, &pipeline) == VK_SUCCESS); + assert(stage.pNext == module && blend.pAttachments == attachments); +} + +static bool same_attachment(const VkPipelineColorBlendAttachmentState *a, const VkPipelineColorBlendAttachmentState *b) +{ + return !memcmp(a, b, sizeof(*a)); +} + +static void test_lacking_device(void) +{ + VkPhysicalDevice physical = (VkPhysicalDevice)&mock_lacking; + VkPhysicalDeviceFeatures features; + ((PFN_vkGetPhysicalDeviceFeatures)layer_gipa((VkInstance)&mock_instance, "vkGetPhysicalDeviceFeatures"))(physical, + &features); + assert(features.dualSrcBlend); + VkPhysicalDeviceFeatures2 queried = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; + ((PFN_vkGetPhysicalDeviceFeatures2)layer_gipa((VkInstance)&mock_instance, "vkGetPhysicalDeviceFeatures2"))(physical, + &queried); + assert(queried.features.dualSrcBlend); + VkPhysicalDeviceProperties properties; + ((PFN_vkGetPhysicalDeviceProperties)layer_gipa((VkInstance)&mock_instance, "vkGetPhysicalDeviceProperties"))( + physical, &properties); + assert(properties.limits.maxFragmentDualSrcAttachments == 1); + + VkDevice device; + VkPhysicalDeviceFeatures enabled = {.dualSrcBlend = VK_TRUE}; + assert(create_device(physical, NULL, &enabled, &device) == VK_SUCCESS); + assert(!driver_saw_dual_source && enabled.dualSrcBlend); + destroy_device(device); + + VkPhysicalDeviceFeatures2 chained = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, NULL, {.dualSrcBlend = VK_TRUE}}; + assert(create_device(physical, &chained, NULL, &device) == VK_SUCCESS); + assert(!driver_saw_dual_source && chained.features.dualSrcBlend); + + struct spirv dual; + fragment_shader(&dual, 0x00010300, true); + VkShaderModuleCreateInfo module = {VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, NULL, 0, dual.n * sizeof(uint32_t), + dual.w}; + VkShaderModule handle; + assert(((PFN_vkCreateShaderModule)layer_gdpa(device, "vkCreateShaderModule"))(device, &module, NULL, &handle) == + VK_SUCCESS); + check_stripped(driver_code, driver_code_count, false); + ((PFN_vkDestroyShaderModule)layer_gdpa(device, "vkDestroyShaderModule"))(device, handle, NULL); + + VkPipelineColorBlendAttachmentState attachments[2] = {dual_attachment, plain_attachment}; + create_pipeline(device, attachments, &module); + check_stripped(driver_code, driver_code_count, false); + assert(module.pCode == dual.w && spirv_uses_dual_source(module.pCode, module.codeSize / sizeof(uint32_t))); + assert(driver_attachment_count == 2); + assert(driver_attachments[0].srcColorBlendFactor == VK_BLEND_FACTOR_ONE); + assert(driver_attachments[0].dstColorBlendFactor == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA); + assert(driver_attachments[0].srcAlphaBlendFactor == VK_BLEND_FACTOR_SRC_ALPHA); + assert(driver_attachments[0].dstAlphaBlendFactor == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA); + assert(driver_attachments[0].colorWriteMask == 0xf && driver_attachments[0].blendEnable); + assert(same_attachment(&driver_attachments[1], &plain_attachment)); + assert(same_attachment(&attachments[0], &dual_attachment)); + + VkColorBlendEquationEXT equations[2] = { + {VK_BLEND_FACTOR_SRC1_COLOR, VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR, VK_BLEND_OP_ADD, VK_BLEND_FACTOR_ONE, + VK_BLEND_FACTOR_ZERO, VK_BLEND_OP_ADD}, + {VK_BLEND_FACTOR_DST_COLOR, VK_BLEND_FACTOR_SRC_COLOR, VK_BLEND_OP_ADD, VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, + VK_BLEND_FACTOR_CONSTANT_COLOR, VK_BLEND_OP_ADD}, + }; + VkCommandBuffer cmd = (VkCommandBuffer)&mock_command_buffer; + PFN_vkCmdSetColorBlendEquationEXT set_equation = + (PFN_vkCmdSetColorBlendEquationEXT)layer_gdpa(device, "vkCmdSetColorBlendEquationEXT"); + assert(set_equation != (PFN_vkCmdSetColorBlendEquationEXT)mock_set_blend_equation); + set_equation(cmd, 1, 2, equations); + assert(driver_equation_count == 3); + assert(driver_equations[1].srcColorBlendFactor == VK_BLEND_FACTOR_SRC_ALPHA); + assert(driver_equations[1].dstColorBlendFactor == VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA); + assert(driver_equations[1].srcAlphaBlendFactor == VK_BLEND_FACTOR_ONE); + assert(!memcmp(&driver_equations[2], &equations[1], sizeof(equations[1]))); + assert(equations[0].srcColorBlendFactor == VK_BLEND_FACTOR_SRC1_COLOR); + destroy_device(device); +} + +static void test_capable_device(void) +{ + VkPhysicalDevice physical = (VkPhysicalDevice)&mock_capable; + VkPhysicalDeviceProperties properties; + ((PFN_vkGetPhysicalDeviceProperties)layer_gipa((VkInstance)&mock_instance, "vkGetPhysicalDeviceProperties"))( + physical, &properties); + assert(properties.limits.maxFragmentDualSrcAttachments == 1); + + VkDevice device; + VkPhysicalDeviceFeatures enabled = {.dualSrcBlend = VK_TRUE}; + assert(create_device(physical, NULL, &enabled, &device) == VK_SUCCESS && driver_saw_dual_source); + assert(layer_gdpa(device, "vkCreateGraphicsPipelines") == (PFN_vkVoidFunction)mock_create_graphics_pipelines); + assert(layer_gdpa(device, "vkCreateShaderModule") == (PFN_vkVoidFunction)mock_create_shader_module); + assert(layer_gdpa(device, "vkCmdSetColorBlendEquationEXT") == (PFN_vkVoidFunction)mock_set_blend_equation); + destroy_device(device); +} + +int main(int argc, char **argv) +{ + test_spirv(argc > 1 ? argv[1] : NULL); + + setenv("WINEPREFIX", "/nonexistent", 1); + unsetenv("DROIDDECK_MALI_COMPAT"); + unsetenv("DROIDDECK_MALI_COMPAT_TRACE"); + VkNegotiateLayerInterface negotiate = {LAYER_NEGOTIATE_INTERFACE_STRUCT, NULL, 2, NULL, NULL, NULL}; + assert(vkNegotiateLoaderLayerInterfaceVersion(&negotiate) == VK_SUCCESS); + layer_gipa = negotiate.pfnGetInstanceProcAddr; + layer_gdpa = negotiate.pfnGetDeviceProcAddr; + + VkLayerInstanceLink link = {NULL, mock_gipa, NULL}; + VkLayerInstanceCreateInfo loader = {VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO, NULL, VK_LAYER_LINK_INFO, {&link}}; + VkInstanceCreateInfo info = {.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pNext = &loader}; + VkInstance instance; + PFN_vkCreateInstance create_instance = (PFN_vkCreateInstance)layer_gipa(NULL, "vkCreateInstance"); + assert(create_instance(&info, NULL, &instance) == VK_SUCCESS); + + test_lacking_device(); + test_capable_device(); + + ((PFN_vkDestroyInstance)layer_gipa(instance, "vkDestroyInstance"))(instance, NULL); + free(driver_code); + printf("mali compat layer ok\n"); + return 0; +} diff --git a/tools/tests/test_mali_compat_layer.py b/tools/tests/test_mali_compat_layer.py new file mode 100644 index 0000000000..fb0b21f5ea --- /dev/null +++ b/tools/tests/test_mali_compat_layer.py @@ -0,0 +1,56 @@ +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" + + +def include(variable, default): + return Path(os.environ.get(variable, str(default))) + + +class MaliCompatLayerTest(unittest.TestCase): + def test_dual_source_blending_against_a_mock_loader_and_driver(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") + bcdec = include("MALI_COMPAT_BCDEC_INCLUDE", DEPS / "bcdec") + missing = [str(path) for path in (vulkan / "vulkan/vk_layer.h", spirv / "spirv/unified1/spirv.h", + bcdec / "bcdec.h") if not path.is_file()] + if missing: + message = "Vulkan 1.4, SPIR-V and bcdec headers are required: " + ", ".join(missing) + if os.environ.get("CI"): + self.fail(message) + self.skipTest(message) + with tempfile.TemporaryDirectory(prefix="droiddeck-mali-compat-") as tmp: + program = Path(tmp) / "mali-compat-layer" + built = subprocess.run([compiler, "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Wno-unused-parameter", + "-UNDEBUG", "-pthread", "-I", str(LAYER), "-I", str(vulkan), "-I", str(spirv), + "-I", str(bcdec), str(Path(__file__).with_name("mali_compat_layer_test.c")), + *sorted(str(source) for source in LAYER.glob("*.c")), "-o", str(program)], + capture_output=True, text=True) + self.assertEqual(built.returncode, 0, built.stdout + built.stderr) + ran = subprocess.run([str(program), tmp], capture_output=True, text=True, timeout=30) + self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr) + self.assertIn("mali compat layer ok", ran.stdout) + self.assertIn("emulating dualSrcBlend for mock", ran.stderr) + validator = shutil.which("spirv-val") + if not validator: + return + for name, environment in (("dual-1.0.spv", "vulkan1.0"), ("plain-1.0.spv", "vulkan1.0"), + ("stripped-1.0.spv", "vulkan1.0"), ("stripped-1.4.spv", "vulkan1.2")): + checked = subprocess.run([validator, "--target-env", environment, str(Path(tmp) / name)], + capture_output=True, text=True) + self.assertEqual(checked.returncode, 0, name + ": " + checked.stdout + checked.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/venus/layer/layer.c b/tools/venus/layer/layer.c index 4df8a54ad1..2f1e7ccd7d 100644 --- a/tools/venus/layer/layer.c +++ b/tools/venus/layer/layer.c @@ -212,6 +212,8 @@ uint32_t physical_device_gaps(struct instance *inst, VkPhysicalDevice physical_d gaps |= GAP_ROBUSTNESS2; if (!features.features.multiDrawIndirect) gaps |= GAP_MULTI_DRAW; + if (!features.features.dualSrcBlend) + gaps |= GAP_DUAL_SRC_BLEND; if (!has_extension(inst, physical_device, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME)) gaps |= GAP_DEPTH_CLIP; /* No robustness2 at all: the extension is offered with robustBufferAccess2 (as robustBufferAccess) @@ -284,6 +286,8 @@ static void report_features(VkPhysicalDeviceFeatures *features, uint32_t gaps) features->textureCompressionBC = VK_TRUE; if (gaps & GAP_MULTI_DRAW) features->multiDrawIndirect = VK_TRUE; + if (gaps & GAP_DUAL_SRC_BLEND) + features->dualSrcBlend = VK_TRUE; } /* Which made-up features the application asked for, and off with them before the driver sees them. */ @@ -314,6 +318,10 @@ static uint32_t take_features(VkPhysicalDeviceFeatures *features, uint32_t gaps) requested |= GAP_MULTI_DRAW; features->multiDrawIndirect = VK_FALSE; } + if ((gaps & GAP_DUAL_SRC_BLEND) && features->dualSrcBlend) { + requested |= GAP_DUAL_SRC_BLEND; + features->dualSrcBlend = VK_FALSE; + } return requested; } @@ -323,6 +331,8 @@ static void report_limits(VkPhysicalDeviceLimits *limits, uint32_t gaps) limits->maxViewports = 16; if ((gaps & GAP_MULTI_DRAW) && limits->maxDrawIndirectCount < (1u << 30)) limits->maxDrawIndirectCount = 1u << 30; + if ((gaps & GAP_DUAL_SRC_BLEND) && !limits->maxFragmentDualSrcAttachments) + limits->maxFragmentDualSrcAttachments = 1; if (gaps & GAPS_SHADER) { limits->maxClipDistances = 8; limits->maxCullDistances = 8; @@ -616,7 +626,7 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev pthread_mutex_unlock(&global_lock); if (requested) { - layer_log("emulating%s%s%s%s%s%s%s%s%s for %s", (requested & GAP_FILL_MODE) ? " fillModeNonSolid" : "", + layer_log("emulating%s%s%s%s%s%s%s%s%s%s for %s", (requested & GAP_FILL_MODE) ? " fillModeNonSolid" : "", (requested & GAP_MULTI_VIEWPORT) ? " multiViewport" : "", (requested & GAP_CLIP_DISTANCE) ? " shaderClipDistance" : "", (requested & GAP_CULL_DISTANCE) ? " shaderCullDistance" : "", @@ -624,7 +634,8 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev (requested & GAP_BC) ? " textureCompressionBC" : "", (requested & GAP_MULTI_DRAW) ? " multiDrawIndirect" : "", (requested & GAP_DEPTH_CLIP) ? " depthClipEnable" : "", - (requested & GAP_NULL_DESCRIPTOR) ? " nullDescriptor" : "", properties.deviceName); + (requested & GAP_NULL_DESCRIPTOR) ? " nullDescriptor" : "", + (requested & GAP_DUAL_SRC_BLEND) ? " dualSrcBlend" : "", properties.deviceName); } return VK_SUCCESS; } @@ -667,8 +678,8 @@ struct hook { #define HOOK_ALIAS(name, alias, gaps) {"vk" #alias, (PFN_vkVoidFunction)name, gaps} static const struct hook device_hooks[] = { - HOOK(CreateShaderModule, GAPS_SHADER), - HOOK(DestroyShaderModule, GAPS_SHADER), + HOOK(CreateShaderModule, GAPS_SHADER | GAP_DUAL_SRC_BLEND), + HOOK(DestroyShaderModule, GAPS_SHADER | GAP_DUAL_SRC_BLEND), HOOK(CreateGraphicsPipelines, GAPS_PIPELINE), HOOK(CmdSetViewport, GAP_MULTI_VIEWPORT), HOOK(CmdSetScissor, GAP_MULTI_VIEWPORT), @@ -676,6 +687,7 @@ static const struct hook device_hooks[] = { HOOK_ALIAS(CmdSetViewportWithCount, CmdSetViewportWithCountEXT, GAP_MULTI_VIEWPORT), HOOK(CmdSetScissorWithCount, GAP_MULTI_VIEWPORT), HOOK_ALIAS(CmdSetScissorWithCount, CmdSetScissorWithCountEXT, GAP_MULTI_VIEWPORT), + HOOK(CmdSetColorBlendEquationEXT, GAP_DUAL_SRC_BLEND), HOOK(CreateImage, GAP_BC), HOOK(DestroyImage, GAP_BC), HOOK(CreateImageView, GAP_BC), diff --git a/tools/venus/layer/mali_compat.h b/tools/venus/layer/mali_compat.h index 055034ab65..a785eaff4a 100644 --- a/tools/venus/layer/mali_compat.h +++ b/tools/venus/layer/mali_compat.h @@ -29,10 +29,11 @@ enum gap { GAP_MULTI_DRAW = 1u << 6, /* multiDrawIndirect: indirect draws looped one by one */ GAP_DEPTH_CLIP = 1u << 7, /* VK_EXT_depth_clip_enable: depthClipEnable off becomes depth clamp */ GAP_NULL_DESCRIPTOR = 1u << 8, /* VK_EXT_robustness2 missing entirely: nullDescriptor stand-ins */ + GAP_DUAL_SRC_BLEND = 1u << 9, }; #define GAPS_SHADER (GAP_CLIP_DISTANCE | GAP_CULL_DISTANCE) -#define GAPS_PIPELINE (GAP_FILL_MODE | GAP_MULTI_VIEWPORT | GAPS_SHADER | GAP_DEPTH_CLIP) +#define GAPS_PIPELINE (GAP_FILL_MODE | GAP_MULTI_VIEWPORT | GAPS_SHADER | GAP_DEPTH_CLIP | GAP_DUAL_SRC_BLEND) void layer_log(const char *format, ...) __attribute__((format(printf, 1, 2))); @@ -135,7 +136,8 @@ struct instance { X(UpdateDescriptorSetWithTemplate) \ X(CmdPushDescriptorSetWithTemplate) \ X(CmdBindVertexBuffers) \ - X(CmdBindVertexBuffers2) + X(CmdBindVertexBuffers2) \ + X(CmdSetColorBlendEquationEXT) struct staging_block; @@ -183,6 +185,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); +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); enum clip_role { CLIP_STRIP, CLIP_PRODUCER, CLIP_CONSUMER }; @@ -216,6 +220,8 @@ VKAPI_ATTR void VKAPI_CALL CmdSetScissor(VkCommandBuffer cmd, uint32_t first, ui VKAPI_ATTR void VKAPI_CALL CmdSetViewportWithCount(VkCommandBuffer cmd, uint32_t count, const VkViewport *viewports); VKAPI_ATTR void VKAPI_CALL CmdSetScissorWithCount(VkCommandBuffer cmd, uint32_t count, const VkRect2D *scissors); +VKAPI_ATTR void VKAPI_CALL CmdSetColorBlendEquationEXT(VkCommandBuffer cmd, uint32_t first, uint32_t count, + const VkColorBlendEquationEXT *equations); /* bc.c */ VkFormat bc_substitute(VkFormat format); diff --git a/tools/venus/layer/pipeline.c b/tools/venus/layer/pipeline.c index a749c21d3e..906ef27d26 100644 --- a/tools/venus/layer/pipeline.c +++ b/tools/venus/layer/pipeline.c @@ -12,27 +12,46 @@ 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); - if (!spirv_uses_distances(info->pCode, count)) - return dev->CreateShaderModule(device, info, allocator, module); + 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; + } 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, ©, allocator, module); + free(single.words); + return result; + } struct spirv_code stripped; - if (!spirv_strip_distances(info->pCode, count, &stripped)) { + if (!spirv_strip_distances(copy.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); + VkResult result = dev->CreateShaderModule(device, ©, allocator, module); + free(single.words); + return result; } - VkShaderModuleCreateInfo copy = *info; + 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); VkResult result = dev->CreateShaderModule(device, ©, allocator, module); free(stripped.words); - if (result != VK_SUCCESS) + if (result != VK_SUCCESS) { + free(original->words); + free(original); return result; + } - struct spirv_code *original = malloc(sizeof(*original)); - original->count = count; - original->words = malloc(info->codeSize); - memcpy(original->words, info->pCode, info->codeSize); pthread_mutex_lock(&dev->lock); map_put(&dev->shader_modules, HANDLE(*module), original); pthread_mutex_unlock(&dev->lock); @@ -87,12 +106,15 @@ static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreat struct pipeline_rewrite { VkPipelineRasterizationStateCreateInfo rasterization; + VkPipelineColorBlendStateCreateInfo blend; + VkPipelineColorBlendAttachmentState *attachments; /* VK_EXT_depth_clip_enable's state, taken out of the application's chain for the call. */ VkBaseOutStructure *clip_prev, *clip_node; VkPipelineViewportStateCreateInfo viewport; VkPipelineShaderStageCreateInfo stages[MAX_STAGES]; VkShaderModuleCreateInfo modules[MAX_STAGES]; 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]; @@ -154,8 +176,17 @@ static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *in bool any = false; 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); - any |= uses[i]; + if ((dev->emulated & GAP_DUAL_SRC_BLEND) && code[i].inline_info && + spirv_uses_dual_source(code[i].words, code[i].count)) { + if (spirv_strip_dual_source(code[i].words, code[i].count, &rw->single[i])) { + code[i].words = rw->single[i].words; + code[i].count = rw->single[i].count; + } else { + 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; } if (!any) return; @@ -196,11 +227,63 @@ 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 && rw->single[i].words) { + rw->code[i] = rw->single[i]; + rw->single[i] = (struct spirv_code){0}; + } if (rw->code[i].words) use_code(dev, info, rw, &code[i], i); } } +static bool single_source(VkBlendFactor *factor) +{ + switch (*factor) { + case VK_BLEND_FACTOR_SRC1_COLOR: + case VK_BLEND_FACTOR_SRC1_ALPHA: + *factor = VK_BLEND_FACTOR_SRC_ALPHA; + return true; + case VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR: + case VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA: + *factor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + return true; + default: + return false; + } +} + +static bool single_source_equation(VkBlendFactor *src_color, VkBlendFactor *dst_color, VkBlendFactor *src_alpha, + VkBlendFactor *dst_alpha) +{ + bool changed = single_source(src_color); + changed |= single_source(dst_color); + changed |= single_source(src_alpha); + changed |= single_source(dst_alpha); + return changed; +} + +static void rewrite_blend(const VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw) +{ + const VkPipelineColorBlendStateCreateInfo *blend = info->pColorBlendState; + if (!blend || !blend->pAttachments || !blend->attachmentCount) + return; + rw->attachments = malloc(blend->attachmentCount * sizeof(*rw->attachments)); + memcpy(rw->attachments, blend->pAttachments, blend->attachmentCount * sizeof(*rw->attachments)); + bool changed = false; + for (uint32_t i = 0; i < blend->attachmentCount; i++) { + VkPipelineColorBlendAttachmentState *a = &rw->attachments[i]; + changed |= single_source_equation(&a->srcColorBlendFactor, &a->dstColorBlendFactor, &a->srcAlphaBlendFactor, + &a->dstAlphaBlendFactor); + } + if (!changed) { + free(rw->attachments); + rw->attachments = NULL; + return; + } + rw->blend = *blend; + rw->blend.pAttachments = rw->attachments; +} + static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw) { if ((dev->emulated & GAP_FILL_MODE) && info->pRasterizationState && @@ -241,7 +324,12 @@ static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *i rw->viewport.scissorCount = 1; info->pViewportState = &rw->viewport; } - if (dev->emulated & GAPS_SHADER) + if (dev->emulated & GAP_DUAL_SRC_BLEND) { + rewrite_blend(info, rw); + if (rw->attachments) + info->pColorBlendState = &rw->blend; + } + if (dev->emulated & (GAPS_SHADER | GAP_DUAL_SRC_BLEND)) rewrite_shaders(dev, info, rw); } @@ -257,7 +345,9 @@ static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw) if (rw->temporary[i] != VK_NULL_HANDLE) dev->DestroyShaderModule(dev->handle, rw->temporary[i], NULL); free(rw->code[i].words); + free(rw->single[i].words); } + free(rw->attachments); } VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache cache, uint32_t count, @@ -306,3 +396,15 @@ VKAPI_ATTR void VKAPI_CALL CmdSetScissorWithCount(VkCommandBuffer cmd, uint32_t struct device *dev = device_from_handle(cmd); dev->CmdSetScissorWithCount(cmd, count > 1 ? 1 : count, scissors); } + +VKAPI_ATTR void VKAPI_CALL CmdSetColorBlendEquationEXT(VkCommandBuffer cmd, uint32_t first, uint32_t count, + const VkColorBlendEquationEXT *equations) +{ + struct device *dev = device_from_handle(cmd); + for (uint32_t i = 0; i < count; i++) { + VkColorBlendEquationEXT equation = equations[i]; + single_source_equation(&equation.srcColorBlendFactor, &equation.dstColorBlendFactor, + &equation.srcAlphaBlendFactor, &equation.dstAlphaBlendFactor); + dev->CmdSetColorBlendEquationEXT(cmd, first + i, 1, &equation); + } +} diff --git a/tools/venus/layer/spirv.c b/tools/venus/layer/spirv.c index 734d5ce2d9..9a9794d9ce 100644 --- a/tools/venus/layer/spirv.c +++ b/tools/venus/layer/spirv.c @@ -416,19 +416,19 @@ static void add_to_interface(struct module *m, uint32_t id) } /* A standalone distance variable becomes Private, and its decorations go. */ -static void privatize(struct module *m, const struct distance_var *var) +static void privatize(struct module *m, uint32_t id) { - size_t d = definition(m, var->id); + size_t d = definition(m, id); uint32_t storage, pointee; pointer_parts(m, m->w[d + 1], &storage, &pointee); m->w[d + 1] = pointer_type(m, SpvStorageClassPrivate, pointee); m->w[d + 3] = SpvStorageClassPrivate; - mark_private(m, var->id); + mark_private(m, id); /* Before SPIR-V 1.4 an entry point lists only its inputs and outputs. */ if (m->version < VERSION_1_4) - m->interface_remove[var->id] = 1; + m->interface_remove[id] = 1; for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) { - if (OPCODE(m->w[i]) == SpvOpDecorate && m->w[i + 1] == var->id) + if (OPCODE(m->w[i]) == SpvOpDecorate && m->w[i + 1] == id) edit(m, i, INSTEAD); } } @@ -667,7 +667,7 @@ static void strip(struct module *m, const struct analysis *a, uint32_t keep) { for (uint32_t i = 0; i < a->var_count; i++) { if (a->vars[i].id != keep) - privatize(m, &a->vars[i]); + privatize(m, a->vars[i].id); } struct stand_in stand_ins[16]; uint32_t count = make_stand_ins(m, a, stand_ins, 16); @@ -688,6 +688,50 @@ bool spirv_strip_distances(const uint32_t *words, size_t count, struct spirv_cod return done; } +static bool is_second_source(const uint32_t *w) +{ + return OPCODE(w[0]) == SpvOpDecorate && LENGTH(w[0]) == 4 && w[2] == SpvDecorationIndex && w[3] == 1; +} + +bool spirv_uses_dual_source(const uint32_t *words, size_t count) +{ + if (count <= 5 || words[0] != SpvMagicNumber) + return false; + for (size_t i = 5; i < count && LENGTH(words[i]) && i + LENGTH(words[i]) <= count; i += LENGTH(words[i])) { + if (is_declaration(OPCODE(words[i]))) + break; + if (is_second_source(&words[i])) + return true; + } + return false; +} + +bool spirv_strip_dual_source(const uint32_t *words, size_t count, struct spirv_code *out) +{ + struct module m; + if (!load(&m, words, count)) + return false; + uint32_t outputs[8]; + uint32_t output_count = 0; + for (size_t i = 5; i < m.first_declaration; i += LENGTH(m.w[i])) { + if (!is_second_source(&m.w[i])) + continue; + size_t d = definition(&m, m.w[i + 1]); + uint32_t storage, pointee; + if (output_count == 8 || !d || OPCODE(m.w[d]) != SpvOpVariable || + !pointer_parts(&m, m.w[d + 1], &storage, &pointee) || storage != SpvStorageClassOutput) + m.failed = true; + else + outputs[output_count++] = m.w[i + 1]; + } + for (uint32_t i = 0; i < output_count && !m.failed; i++) + privatize(&m, outputs[i]); + retarget_accesses(&m, NULL, 0); + bool done = finish(&m, out); + unload(&m); + return done; +} + static const struct distance_var *clip_output(const struct module *m, const struct analysis *a, uint32_t *length) { for (uint32_t i = 0; i < a->var_count; i++) { diff --git a/tools/venus/release.env b/tools/venus/release.env index 5da556c28b..91603af626 100644 --- a/tools/venus/release.env +++ b/tools/venus/release.env @@ -1,3 +1,3 @@ -VENUS_REPO=The412Banner/droiddeck-nonadreno-drivers -VENUS_TAG=venus-proto13-e24ed8ab -VENUS_SHA256=25f236bbc32c8490fc61c20a6016b0bea4a8c3267a69de6b91496da2afe6bc04 +VENUS_REPO=Droid-Deck/DroidDeck-Components +VENUS_TAG=venus-dualsrc-463ba9ef +VENUS_SHA256=ace6f4c3e62db0c7487859d8cebdcdd83656272951f84c00471fafc364ae30af