diff --git a/app/src/main/cpp/vkbridge/server_emu.c b/app/src/main/cpp/vkbridge/server_emu.c index 7a1961b8c..3e8300599 100644 --- a/app/src/main/cpp/vkbridge/server_emu.c +++ b/app/src/main/cpp/vkbridge/server_emu.c @@ -25,7 +25,7 @@ static int env_off(const char *name) return v && !strcmp(v, "0"); } -uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd) +uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd, uint32_t *missing) { VkPhysicalDeviceFeatures f; idt->vkGetPhysicalDeviceFeatures(pd, &f); @@ -65,6 +65,14 @@ uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd) if (!depth_clip && f.depthClamp && !env_off("VKBRIDGE_EMU_DEPTHCLIP")) emu |= VKB_EMU_DEPTH_CLIP; /* PointSize in geometry/tessellation stages: stripped when the feature is missing. */ if (!f.shaderTessellationAndGeometryPointSize && (f.geometryShader || f.tessellationShader)) emu |= VKB_EMU_POINT_SIZE; + uint32_t miss = 0; + if (!f.textureCompressionBC) miss |= VKB_EMU_BCN; + if (!divisor) miss |= VKB_EMU_DIVISOR; + if (!f.shaderClipDistance) miss |= VKB_EMU_CLIP_DISTANCE; + if (!f.shaderCullDistance) miss |= VKB_EMU_CULL_DISTANCE; + if (!depth_clip) miss |= VKB_EMU_DEPTH_CLIP; + if (emu & VKB_EMU_MAINT5) miss |= VKB_EMU_MAINT5; + *missing = miss; return emu & VKB_EMU_IMPLEMENTED; } diff --git a/app/src/main/cpp/vkbridge/server_emu.h b/app/src/main/cpp/vkbridge/server_emu.h index a10d6eda3..e227f6e11 100644 --- a/app/src/main/cpp/vkbridge/server_emu.h +++ b/app/src/main/cpp/vkbridge/server_emu.h @@ -5,7 +5,7 @@ #include "vkb_server.h" /* Emulations that are implemented (the rest are never offered to the client). */ -#define VKB_EMU_IMPLEMENTED (VKB_EMU_MAINT5) +#define VKB_EMU_IMPLEMENTED (VKB_EMU_MAINT5 | VKB_EMU_CLIP_DISTANCE | VKB_EMU_CULL_DISTANCE | VKB_EMU_POINT_SIZE) #define VKB_EMU_BUCKETS 1024 @@ -51,7 +51,11 @@ void vkb_emu_install_features(vkb_srv_table *dev); void vkb_emu_uninstall_features(vkb_srv_table *dev); void vkb_emu_image_create_info(vkb_emu_device *e, VkImageCreateInfo *ci); void vkb_emu_image_release(vkb_emu_device *e, vkb_emu_image *img); -void vkb_emu_graphics_pipeline_info(vkb_emu_device *e, VkGraphicsPipelineCreateInfo *ci); +/* Rewrites a graphics pipeline's shaders; temporary modules are appended to mods. */ +void vkb_emu_shader_pipeline(vkb_emu_device *e, VkDevice device, VkGraphicsPipelineCreateInfo *ci, VkShaderModule *mods, + uint32_t *nmods, uint32_t cap); +void vkb_emu_shader_install(vkb_srv_table *dev); +void vkb_emu_shader_uninstall(vkb_srv_table *dev); /* 1 = handled (the emulation recorded its own commands instead). */ int vkb_emu_copy_image(vkb_emu_device *e, VkCommandBuffer cb, VkImage src, VkImageLayout sl, VkImage dst, VkImageLayout dl, uint32_t n, const VkImageCopy *regions); diff --git a/app/src/main/cpp/vkbridge/server_emu_features.c b/app/src/main/cpp/vkbridge/server_emu_features.c index 90be0cd2b..3b07b0e84 100644 --- a/app/src/main/cpp/vkbridge/server_emu_features.c +++ b/app/src/main/cpp/vkbridge/server_emu_features.c @@ -6,11 +6,10 @@ #include "server_emu.h" int vkb_emu_bcn_supported(const vkb_dispatch *idt, VkPhysicalDevice pd) { (void)idt; (void)pd; return 0; } -void vkb_emu_install_features(vkb_srv_table *dev) { (void)dev; } -void vkb_emu_uninstall_features(vkb_srv_table *dev) { (void)dev; } +void vkb_emu_install_features(vkb_srv_table *dev) { vkb_emu_shader_install(dev); } +void vkb_emu_uninstall_features(vkb_srv_table *dev) { vkb_emu_shader_uninstall(dev); } void vkb_emu_image_create_info(vkb_emu_device *e, VkImageCreateInfo *ci) { (void)e; (void)ci; } void vkb_emu_image_release(vkb_emu_device *e, vkb_emu_image *img) { (void)e; (void)img; } -void vkb_emu_graphics_pipeline_info(vkb_emu_device *e, VkGraphicsPipelineCreateInfo *ci) { (void)e; (void)ci; } int vkb_emu_copy_image(vkb_emu_device *e, VkCommandBuffer cb, VkImage src, VkImageLayout sl, VkImage dst, VkImageLayout dl, uint32_t n, const VkImageCopy *regions) { diff --git a/app/src/main/cpp/vkbridge/server_emu_layer.c b/app/src/main/cpp/vkbridge/server_emu_layer.c index 94732828e..c75f8989f 100644 --- a/app/src/main/cpp/vkbridge/server_emu_layer.c +++ b/app/src/main/cpp/vkbridge/server_emu_layer.c @@ -200,13 +200,14 @@ static VKAPI_ATTR VkResult VKAPI_CALL emu_CreateGraphicsPipelines(VkDevice devic uint32_t nmods = 0; for (uint32_t i = 0; i < n; i++) { VkGraphicsPipelineCreateInfo *ci = (VkGraphicsPipelineCreateInfo *)&infos[i]; + /* Shader rewrites first: they read inline SPIR-V before it becomes a module. */ + vkb_emu_shader_pipeline(e, device, ci, mods, &nmods, 64); if (e->flags & VKB_EMU_MAINT5) { VkPipelineCreateFlags2CreateInfo *f2 = vkb_emu_chain_take(ci, VK_STRUCTURE_TYPE_PIPELINE_CREATE_FLAGS_2_CREATE_INFO); if (f2) ci->flags = (VkPipelineCreateFlags)f2->flags; for (uint32_t s = 0; s < ci->stageCount; s++) prep_stage(device, (VkPipelineShaderStageCreateInfo *)&ci->pStages[s], mods, &nmods, 64); } - vkb_emu_graphics_pipeline_info(e, ci); } VkResult r = vkb_emu_real()->vkCreateGraphicsPipelines(device, cache, n, infos, pAllocator, pPipelines); for (uint32_t i = 0; i < nmods; i++) vkb_emu_real()->vkDestroyShaderModule(device, mods[i], NULL); diff --git a/app/src/main/cpp/vkbridge/server_emu_shader.c b/app/src/main/cpp/vkbridge/server_emu_shader.c new file mode 100644 index 000000000..4fe7dbc13 --- /dev/null +++ b/app/src/main/cpp/vkbridge/server_emu_shader.c @@ -0,0 +1,387 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * + * Shader-side emulations, done when a graphics pipeline is created (both ends of the vertex -> + * fragment interface are known then): + * + * clip distance (shaderClipDistance missing): the last pre-rasterization stage's ClipDistance + * output becomes an ordinary varying at a free location; the fragment shader reads it and + * discards the fragment when any distance is negative. For planar clip distances this is the + * same set of pixels the hardware clipper would keep. + * cull distance (shaderCullDistance missing): the output becomes an unread varying - nothing is + * culled, which costs fragments but never drops geometry that should be visible. + * point size in geometry/tessellation (shaderTessellationAndGeometryPointSize missing): same. + * + * Shader modules are kept as SPIR-V so a pipeline can build rewritten variants; the driver's own + * module is created from a variant that needs none of the missing features. + */ +#define _GNU_SOURCE +#include "server_emu.h" +#include "spirv_edit.h" + +#include +#include +#include +#include + +typedef struct shader_rec { + VkShaderModule module; + uint32_t *code; + size_t size; + struct shader_rec *next; +} shader_rec; + +typedef struct shader_state { + pthread_mutex_t lock; + shader_rec *mods[1024]; +} shader_state; + +#define SHADER_EMU (VKB_EMU_CLIP_DISTANCE | VKB_EMU_CULL_DISTANCE | VKB_EMU_POINT_SIZE) + +static unsigned mh(VkShaderModule m) +{ + uint64_t k = (uint64_t)m; + k ^= k >> 31; + k *= 0x94d049bb133111ebull; + return (unsigned)(k >> 54); +} + +static shader_state *state(vkb_emu_device *e) { return e->shaders; } + +static const shader_rec *find_module(vkb_emu_device *e, VkShaderModule m) +{ + shader_state *s = state(e); + pthread_mutex_lock(&s->lock); + shader_rec *r = s->mods[mh(m)]; + while (r && r->module != m) r = r->next; + pthread_mutex_unlock(&s->lock); + return r; +} + +/* ------------------------------------------------------------------ rewrites */ + +/* Finds a standalone Output/Input variable decorated with a builtin; 0 if none. 2 = in a block. */ +static uint32_t builtin_var(const spv_mod *m, uint32_t builtin, uint32_t storage, int *in_block) +{ + *in_block = 0; + for (uint32_t i = 0; i < m->count; i++) { + const spv_inst *in = &m->ins[i]; + if (in->op == SpvOpDecorate && in->w[1] == SpvDecorationBuiltIn && in->w[2] == builtin) { + int d = spv_find_def(m, in->w[0]); + if (d >= 0 && m->ins[d].op == SpvOpVariable && m->ins[d].w[2] == storage) return in->w[0]; + } + if (in->op == SpvOpMemberDecorate && in->w[2] == SpvDecorationBuiltIn && in->w[3] == builtin) *in_block = 1; + } + return 0; +} + +static uint32_t var_pointee(const spv_mod *m, uint32_t var) +{ + int d = spv_find_def(m, var); + if (d < 0) return 0; + int p = spv_find_def(m, m->ins[d].w[0]); + return p < 0 ? 0 : m->ins[p].w[2]; +} + +static void add_decoration(spv_mod *m, uint32_t id, uint32_t dec, uint32_t value) +{ + uint32_t ops[3] = {id, dec, value}; + spv_insert(m, spv_annotations_end(m), SpvOpDecorate, ops, 3); +} + +/* Builtin output -> plain varying at *loc (advanced). Returns the element count, 0 if absent. */ +static uint32_t demote_builtin(spv_mod *m, uint32_t builtin, uint32_t *loc, int *unsupported) +{ + int in_block; + uint32_t var = builtin_var(m, builtin, SpvStorageClassOutput, &in_block); + if (!var) { + if (in_block) *unsupported = 1; + return 0; + } + uint32_t pointee = var_pointee(m, var); + uint32_t n = spv_array_length(m, pointee); + if (!n) n = 1; + spv_remove_decoration(m, var, SpvDecorationBuiltIn); + add_decoration(m, var, SpvDecorationLocation, *loc); + *loc += n; + return n; +} + +/* The pre-rasterization side. Returns the clip distance count (0: none), -1 if it cannot. */ +static int rewrite_pre_raster(spv_mod *m, uint32_t emu, uint32_t clip_loc, int last_stage) +{ + int unsupported = 0; + uint32_t loc = clip_loc; + int clip = 0; + if ((emu & VKB_EMU_CLIP_DISTANCE) && spv_has_capability(m, SpvCapabilityClipDistance)) { + clip = (int)demote_builtin(m, SpvBuiltInClipDistance, &loc, &unsupported); + spv_remove_capability(m, SpvCapabilityClipDistance); + } + if ((emu & VKB_EMU_CULL_DISTANCE) && spv_has_capability(m, SpvCapabilityCullDistance)) { + demote_builtin(m, SpvBuiltInCullDistance, &loc, &unsupported); + spv_remove_capability(m, SpvCapabilityCullDistance); + } + int model = spv_execution_model(m); + if ((emu & VKB_EMU_POINT_SIZE) && (model == SpvExecutionModelGeometry || model == SpvExecutionModelTessellationEvaluation) && + (spv_has_capability(m, SpvCapabilityGeometryPointSize) || spv_has_capability(m, SpvCapabilityTessellationPointSize))) { + demote_builtin(m, SpvBuiltInPointSize, &loc, &unsupported); + spv_remove_capability(m, SpvCapabilityGeometryPointSize); + spv_remove_capability(m, SpvCapabilityTessellationPointSize); + } + (void)last_stage; + if (unsupported) VKB_ONCE(VKB_LOG_WARN, "emulation: clip/cull distance inside an output block is not handled (logged once)"); + return unsupported && !clip ? -1 : clip; +} + +/* The fragment side: read the distances at `loc` and discard when any is negative. */ +static int rewrite_fragment(spv_mod *m, uint32_t loc, uint32_t n) +{ + uint32_t f32 = spv_type_float(m, 32); + uint32_t len = spv_const_u32(m, n); + uint32_t arr = spv_type_array(m, f32, len); + uint32_t parr = spv_type_pointer(m, SpvStorageClassInput, arr); + uint32_t pf = spv_type_pointer(m, SpvStorageClassInput, f32); + uint32_t tbool = spv_type_bool(m); + uint32_t zero = spv_const_f32(m, 0.0f); + uint32_t idx[16]; + for (uint32_t i = 0; i < n && i < 16; i++) idx[i] = spv_const_u32(m, i); + uint32_t var = spv_new_id(m); + uint32_t vops[3] = {parr, var, SpvStorageClassInput}; + spv_insert(m, spv_types_end(m), SpvOpVariable, vops, 3); + add_decoration(m, var, SpvDecorationLocation, loc); + spv_add_interface(m, var); + + int ep = spv_find_op(m, SpvOpEntryPoint, 0); + if (ep < 0) return -1; + uint32_t fn = m->ins[ep].w[1]; + int fi = -1; + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpFunction && m->ins[i].w[1] == fn) fi = (int)i; + if (fi < 0) return -1; + int li = spv_find_op(m, SpvOpLabel, (uint32_t)fi); + if (li < 0) return -1; + uint32_t first_label = m->ins[li].w[0]; + uint32_t at = (uint32_t)li + 1; + while (at < m->count && (m->ins[at].op == SpvOpVariable || m->ins[at].op == SpvOpLine || m->ins[at].op == SpvOpNoLine)) at++; + + uint32_t any = 0; + for (uint32_t i = 0; i < n && i < 16; i++) { + uint32_t p = spv_new_id(m), v = spv_new_id(m), lt = spv_new_id(m); + uint32_t ac[4] = {pf, p, var, idx[i]}; + at = spv_insert(m, at, SpvOpAccessChain, ac, 4) + 1; + uint32_t ld[3] = {f32, v, p}; + at = spv_insert(m, at, SpvOpLoad, ld, 3) + 1; + uint32_t cmp[4] = {tbool, lt, v, zero}; + at = spv_insert(m, at, SpvOpFOrdLessThan, cmp, 4) + 1; + if (any) { + uint32_t o = spv_new_id(m), orr[4] = {tbool, o, any, lt}; + at = spv_insert(m, at, SpvOpLogicalOr, orr, 4) + 1; + any = o; + } else { + any = lt; + } + } + uint32_t kill = spv_new_id(m), merge = spv_new_id(m); + uint32_t sm[2] = {merge, 0}; + at = spv_insert(m, at, SpvOpSelectionMerge, sm, 2) + 1; + uint32_t bc[3] = {any, kill, merge}; + at = spv_insert(m, at, SpvOpBranchConditional, bc, 3) + 1; + at = spv_insert(m, at, SpvOpLabel, &kill, 1) + 1; + at = spv_insert(m, at, spv_version(m) >= 0x00010600 ? SpvOpTerminateInvocation : SpvOpKill, NULL, 0) + 1; + at = spv_insert(m, at, SpvOpLabel, &merge, 1) + 1; + /* Phis that named the old entry block as a predecessor now come from the merge block. */ + for (uint32_t i = at; i < m->count; i++) { + spv_inst *in = &m->ins[i]; + if (in->op != 245 /* OpPhi */) continue; + for (uint32_t k = 3; k < (uint32_t)in->n - 1; k += 2) + if (in->w[k] == first_label) in->w[k] = merge; + } + return m->err ? -1 : 0; +} + +/* ------------------------------------------------------------------ module store */ + +static VkResult make_module(VkDevice device, const spv_mod *m, VkShaderModule *out) +{ + uint32_t *code; + size_t size = spv_write(m, &code); + if (!size) return VK_ERROR_OUT_OF_HOST_MEMORY; + const char *dump = getenv("VKBRIDGE_DUMP_SPIRV"); + if (dump && *dump) { + static int seq; + char path[512]; + snprintf(path, sizeof(path), "%s/vkbridge-%d-%d.spv", dump, (int)getpid(), __atomic_fetch_add(&seq, 1, __ATOMIC_RELAXED)); + FILE *f = fopen(path, "wb"); + if (f) { + fwrite(code, 1, size, f); + fclose(f); + } + } + VkShaderModuleCreateInfo ci = {VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, NULL, 0, size, code}; + VkResult r = vkb_emu_real()->vkCreateShaderModule(device, &ci, NULL, out); + free(code); + return r; +} + +static VKAPI_ATTR VkResult VKAPI_CALL emu_CreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *ci, + const VkAllocationCallbacks *pAllocator, VkShaderModule *pModule) +{ + vkb_emu_device *e = vkb_emu_cur(); + spv_mod m; + VkResult r; + int needs = 0; + if (spv_parse(&m, ci->pCode, ci->codeSize) == 0) { + needs = spv_has_capability(&m, SpvCapabilityClipDistance) || spv_has_capability(&m, SpvCapabilityCullDistance) || + spv_has_capability(&m, SpvCapabilityGeometryPointSize) || spv_has_capability(&m, SpvCapabilityTessellationPointSize); + } + if (needs) { + /* The driver's module must not ask for what it lacks; pipelines use rewritten variants. */ + int maxo = spv_max_location(&m, SpvStorageClassOutput); + rewrite_pre_raster(&m, e->flags, (uint32_t)(maxo + 1), 1); + r = make_module(device, &m, pModule); + } else { + r = vkb_emu_real()->vkCreateShaderModule(device, ci, pAllocator, pModule); + } + spv_free(&m); + if (r != VK_SUCCESS) return r; + shader_rec *rec = calloc(1, sizeof(*rec)); + rec->module = *pModule; + rec->size = ci->codeSize; + rec->code = malloc(ci->codeSize); + memcpy(rec->code, ci->pCode, ci->codeSize); + shader_state *s = state(e); + pthread_mutex_lock(&s->lock); + unsigned h = mh(*pModule); + rec->next = s->mods[h]; + s->mods[h] = rec; + pthread_mutex_unlock(&s->lock); + return r; +} + +static VKAPI_ATTR void VKAPI_CALL emu_DestroyShaderModule(VkDevice device, VkShaderModule module, const VkAllocationCallbacks *pAllocator) +{ + vkb_emu_device *e = vkb_emu_cur(); + if (module) { + shader_state *s = state(e); + pthread_mutex_lock(&s->lock); + shader_rec **pp = &s->mods[mh(module)]; + while (*pp && (*pp)->module != module) pp = &(*pp)->next; + shader_rec *r = *pp; + if (r) *pp = r->next; + pthread_mutex_unlock(&s->lock); + if (r) { + free(r->code); + free(r); + } + } + vkb_emu_real()->vkDestroyShaderModule(device, module, pAllocator); +} + +/* The SPIR-V of a stage: its module's (kept here) or the inline VkShaderModuleCreateInfo's. */ +static int stage_code(vkb_emu_device *e, const VkPipelineShaderStageCreateInfo *st, const uint32_t **code, size_t *size) +{ + if (st->module) { + const shader_rec *r = find_module(e, st->module); + if (!r) return 0; + *code = r->code; + *size = r->size; + return 1; + } + for (const VkBaseInStructure *b = st->pNext; b; b = b->pNext) { + if (b->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) { + *code = ((const VkShaderModuleCreateInfo *)b)->pCode; + *size = ((const VkShaderModuleCreateInfo *)b)->codeSize; + return 1; + } + } + return 0; +} + +/* Replace a stage's shader by a rewritten module (recorded for destruction after the call). */ +static void set_stage_module(VkDevice device, VkPipelineShaderStageCreateInfo *st, const spv_mod *m, VkShaderModule *mods, + uint32_t *nmods, uint32_t cap) +{ + VkShaderModule mod; + if (*nmods >= cap || make_module(device, m, &mod) != VK_SUCCESS) return; + mods[(*nmods)++] = mod; + st->module = mod; + vkb_emu_chain_take(st, VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO); +} + +void vkb_emu_shader_pipeline(vkb_emu_device *e, VkDevice device, VkGraphicsPipelineCreateInfo *ci, VkShaderModule *mods, + uint32_t *nmods, uint32_t cap) +{ + if (!(e->flags & SHADER_EMU) || !ci->pStages) return; + VkPipelineShaderStageCreateInfo *pre = NULL, *frag = NULL; + int pre_rank = -1; + for (uint32_t s = 0; s < ci->stageCount; s++) { + VkPipelineShaderStageCreateInfo *st = (VkPipelineShaderStageCreateInfo *)&ci->pStages[s]; + int rank = st->stage == VK_SHADER_STAGE_VERTEX_BIT ? 0 : st->stage == VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT ? 1 + : st->stage == VK_SHADER_STAGE_GEOMETRY_BIT ? 2 : -1; + if (rank > pre_rank) { + pre_rank = rank; + pre = st; + } + if (st->stage == VK_SHADER_STAGE_FRAGMENT_BIT) frag = st; + } + if (!pre) return; + const uint32_t *code; + size_t size; + if (!stage_code(e, pre, &code, &size)) return; + spv_mod pm; + if (spv_parse(&pm, code, size) != 0) return; + if (!spv_has_capability(&pm, SpvCapabilityClipDistance) && !spv_has_capability(&pm, SpvCapabilityCullDistance) && + !spv_has_capability(&pm, SpvCapabilityGeometryPointSize) && !spv_has_capability(&pm, SpvCapabilityTessellationPointSize)) { + spv_free(&pm); + return; + } + spv_mod fm; + int have_frag = 0; + const uint32_t *fcode; + size_t fsize; + if (frag && stage_code(e, frag, &fcode, &fsize) && spv_parse(&fm, fcode, fsize) == 0) have_frag = 1; + int loc = spv_max_location(&pm, SpvStorageClassOutput); + if (have_frag) { + int fl = spv_max_location(&fm, SpvStorageClassInput); + if (fl > loc) loc = fl; + } + int clip = rewrite_pre_raster(&pm, e->flags, (uint32_t)(loc + 1), 1); + if (clip >= 0) set_stage_module(device, pre, &pm, mods, nmods, cap); + if (clip > 0) { + if (have_frag) { + if (rewrite_fragment(&fm, (uint32_t)(loc + 1), (uint32_t)clip) == 0) set_stage_module(device, frag, &fm, mods, nmods, cap); + } else { + VKB_ONCE(VKB_LOG_WARN, "emulation: clip distances in a pipeline without a fragment shader are not applied (logged once)"); + } + } + VKB_DBG("emulation: pipeline with %d clip distance(s) rewritten at location %d", clip, loc + 1); + spv_free(&pm); + if (have_frag) spv_free(&fm); +} + +void vkb_emu_shader_install(vkb_srv_table *dev) +{ + vkb_emu_device *e = dev->emu; + if (!(e->flags & SHADER_EMU)) return; + shader_state *s = calloc(1, sizeof(*s)); + pthread_mutex_init(&s->lock, NULL); + e->shaders = s; + dev->dt.vkCreateShaderModule = emu_CreateShaderModule; + dev->dt.vkDestroyShaderModule = emu_DestroyShaderModule; +} + +void vkb_emu_shader_uninstall(vkb_srv_table *dev) +{ + shader_state *s = dev->emu->shaders; + if (!s) return; + for (int i = 0; i < 1024; i++) { + while (s->mods[i]) { + shader_rec *r = s->mods[i]; + s->mods[i] = r->next; + free(r->code); + free(r); + } + } + pthread_mutex_destroy(&s->lock); + free(s); + dev->emu->shaders = NULL; +} diff --git a/app/src/main/cpp/vkbridge/server_instance.c b/app/src/main/cpp/vkbridge/server_instance.c index 6cf6844f3..866ffbfea 100644 --- a/app/src/main/cpp/vkbridge/server_instance.c +++ b/app/src/main/cpp/vkbridge/server_instance.c @@ -119,12 +119,13 @@ void vkb_selftest_all(void) snprintf(k->name, sizeof(k->name), "%s", p2.properties.deviceName); k->info.real_api_version = p2.properties.apiVersion; vkb_mem_selftest(k, inst, &dt, pds[i]); - k->info.emu_available = vkb_emu_detect(&dt, pds[i]); + k->info.emu_available = vkb_emu_detect(&dt, pds[i], &k->info.missing); k->info.emu = k->info.emu_available; const char *emu_env = getenv("VKBRIDGE_EMULATE"); if (emu_env && !strcmp(emu_env, "0")) k->info.emu = 0; k->valid = 1; - VKB_INFO("%s: memory sharing = %s, emulating 0x%x", k->name, vkb_mem_strategy_name(k->info.strategy), k->info.emu); + VKB_INFO("%s: memory sharing = %s, missing 0x%x, emulating 0x%x", k->name, vkb_mem_strategy_name(k->info.strategy), + k->info.missing, k->info.emu); } dt.vkDestroyInstance(inst, NULL); } diff --git a/app/src/main/cpp/vkbridge/spirv_edit.c b/app/src/main/cpp/vkbridge/spirv_edit.c new file mode 100644 index 000000000..cbf926c01 --- /dev/null +++ b/app/src/main/cpp/vkbridge/spirv_edit.c @@ -0,0 +1,328 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "spirv_edit.h" + +#include +#include + +int spv_parse(spv_mod *m, const uint32_t *code, size_t bytes) +{ + memset(m, 0, sizeof(*m)); + if (bytes < 20 || (bytes & 3) || code[0] != 0x07230203u) return -1; + memcpy(m->header, code, 20); + size_t words = bytes / 4, pos = 5; + while (pos < words) { + uint32_t n = code[pos] >> 16, op = code[pos] & 0xffff; + if (n == 0 || pos + n > words) return -1; + spv_insert(m, m->count, (uint16_t)op, code + pos + 1, (uint16_t)(n - 1)); + if (m->err) return -1; + pos += n; + } + return 0; +} + +size_t spv_write(const spv_mod *m, uint32_t **out) +{ + size_t words = 5; + for (uint32_t i = 0; i < m->count; i++) words += m->ins[i].n; + uint32_t *buf = malloc(words * 4); + if (!buf) return 0; + memcpy(buf, m->header, 20); + size_t pos = 5; + for (uint32_t i = 0; i < m->count; i++) { + const spv_inst *in = &m->ins[i]; + buf[pos++] = ((uint32_t)in->n << 16) | in->op; + memcpy(buf + pos, in->w, (size_t)(in->n - 1) * 4); + pos += in->n - 1; + } + *out = buf; + return words * 4; +} + +void spv_free(spv_mod *m) +{ + for (uint32_t i = 0; i < m->count; i++) free(m->ins[i].w); + free(m->ins); + memset(m, 0, sizeof(*m)); +} + +uint32_t spv_insert(spv_mod *m, uint32_t at, uint16_t op, const uint32_t *ops, uint16_t nops) +{ + if (m->count == m->cap) { + uint32_t nc = m->cap ? m->cap * 2 : 1024; + spv_inst *ni = realloc(m->ins, nc * sizeof(*ni)); + if (!ni) { + m->err = 1; + return 0; + } + m->ins = ni; + m->cap = nc; + } + if (at > m->count) at = m->count; + memmove(&m->ins[at + 1], &m->ins[at], (m->count - at) * sizeof(spv_inst)); + spv_inst *in = &m->ins[at]; + in->op = op; + in->n = (uint16_t)(nops + 1); + in->w = malloc((size_t)(nops ? nops : 1) * 4); + if (!in->w) m->err = 1; + else if (nops) memcpy(in->w, ops, (size_t)nops * 4); + m->count++; + return at; +} + +void spv_remove(spv_mod *m, uint32_t at) +{ + if (at >= m->count) return; + free(m->ins[at].w); + memmove(&m->ins[at], &m->ins[at + 1], (m->count - at - 1) * sizeof(spv_inst)); + m->count--; +} + +int spv_find_op(const spv_mod *m, uint16_t op, uint32_t from) +{ + for (uint32_t i = from; i < m->count; i++) + if (m->ins[i].op == op) return (int)i; + return -1; +} + +static int is_type(uint16_t op) { return op >= 19 && op <= 38; } +static int is_const(uint16_t op) { return (op >= 41 && op <= 46) || (op >= 48 && op <= 52); } + +uint32_t spv_result_id(const spv_inst *in) +{ + if (in->n < 2) return 0; + if (is_type(in->op) || in->op == SpvOpLabel || in->op == SpvOpString || in->op == SpvOpExtInstImport || in->op == 73) + return in->w[0]; + switch (in->op) { + case SpvOpName: case SpvOpMemberName: case SpvOpDecorate: case SpvOpMemberDecorate: case SpvOpEntryPoint: + case SpvOpExecutionMode: case SpvOpCapability: case SpvOpExtension: case SpvOpMemoryModel: case SpvOpStore: + case SpvOpBranch: case SpvOpBranchConditional: case SpvOpSelectionMerge: case SpvOpReturn: case SpvOpKill: + case SpvOpFunctionEnd: case SpvOpLine: case SpvOpNoLine: case SpvOpSource: case SpvOpSourceExtension: + case SpvOpDecorateId: case SpvOpDecorateString: case SpvOpMemberDecorateString: case SpvOpModuleProcessed: + case SpvOpExecutionModeId: case 246 /* LoopMerge */: case 251 /* Switch */: case 254 /* ReturnValue */: + case 255 /* Unreachable */: case 74: case 75: case 224 /* ControlBarrier */: case 225 /* MemoryBarrier */: + case SpvOpTerminateInvocation: + return 0; + default: + return in->n >= 3 ? in->w[1] : 0; + } +} + +int spv_find_def(const spv_mod *m, uint32_t id) +{ + for (uint32_t i = 0; i < m->count; i++) + if (spv_result_id(&m->ins[i]) == id) return (int)i; + return -1; +} + +uint32_t spv_types_end(const spv_mod *m) +{ + int f = spv_find_op(m, SpvOpFunction, 0); + return f < 0 ? m->count : (uint32_t)f; +} + +uint32_t spv_annotations_end(const spv_mod *m) +{ + uint32_t end = spv_types_end(m); + for (uint32_t i = 0; i < end; i++) { + uint16_t op = m->ins[i].op; + if (is_type(op) || is_const(op) || op == SpvOpVariable || op == SpvOpUndef) return i; + } + return end; +} + +static uint32_t add_global(spv_mod *m, uint16_t op, const uint32_t *ops, uint16_t n) +{ + uint32_t at = spv_types_end(m); + spv_insert(m, at, op, ops, n); + return at; +} + +uint32_t spv_type_float(spv_mod *m, uint32_t width) +{ + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpTypeFloat && m->ins[i].w[1] == width && m->ins[i].n == 3) return m->ins[i].w[0]; + uint32_t id = spv_new_id(m), ops[2] = {id, width}; + add_global(m, SpvOpTypeFloat, ops, 2); + return id; +} + +uint32_t spv_type_int(spv_mod *m, uint32_t width, uint32_t sign) +{ + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpTypeInt && m->ins[i].w[1] == width && m->ins[i].w[2] == sign) return m->ins[i].w[0]; + uint32_t id = spv_new_id(m), ops[3] = {id, width, sign}; + add_global(m, SpvOpTypeInt, ops, 3); + return id; +} + +uint32_t spv_type_bool(spv_mod *m) +{ + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpTypeBool) return m->ins[i].w[0]; + uint32_t id = spv_new_id(m); + add_global(m, SpvOpTypeBool, &id, 1); + return id; +} + +uint32_t spv_type_pointer(spv_mod *m, uint32_t storage, uint32_t pointee) +{ + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpTypePointer && m->ins[i].w[1] == storage && m->ins[i].w[2] == pointee) return m->ins[i].w[0]; + uint32_t id = spv_new_id(m), ops[3] = {id, storage, pointee}; + add_global(m, SpvOpTypePointer, ops, 3); + return id; +} + +uint32_t spv_type_array(spv_mod *m, uint32_t elem, uint32_t len_const) +{ + for (uint32_t i = 0; i < m->count; i++) { + if (m->ins[i].op == SpvOpTypeArray && m->ins[i].w[1] == elem && m->ins[i].w[2] == len_const && + spv_get_decoration(m, m->ins[i].w[0], 6 /* ArrayStride */) < 0) + return m->ins[i].w[0]; + } + uint32_t id = spv_new_id(m), ops[3] = {id, elem, len_const}; + add_global(m, SpvOpTypeArray, ops, 3); + return id; +} + +uint32_t spv_const_u32(spv_mod *m, uint32_t v) +{ + uint32_t t = spv_type_int(m, 32, 0); + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpConstant && m->ins[i].w[0] == t && m->ins[i].n == 4 && m->ins[i].w[2] == v) return m->ins[i].w[1]; + uint32_t id = spv_new_id(m), ops[3] = {t, id, v}; + add_global(m, SpvOpConstant, ops, 3); + return id; +} + +uint32_t spv_const_f32(spv_mod *m, float v) +{ + uint32_t t = spv_type_float(m, 32), bits; + memcpy(&bits, &v, 4); + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpConstant && m->ins[i].w[0] == t && m->ins[i].n == 4 && m->ins[i].w[2] == bits) return m->ins[i].w[1]; + uint32_t id = spv_new_id(m), ops[3] = {t, id, bits}; + add_global(m, SpvOpConstant, ops, 3); + return id; +} + +int64_t spv_get_decoration(const spv_mod *m, uint32_t id, uint32_t decoration) +{ + for (uint32_t i = 0; i < m->count; i++) { + const spv_inst *in = &m->ins[i]; + if (in->op == SpvOpDecorate && in->w[0] == id && in->w[1] == decoration) return in->n > 3 ? (int64_t)in->w[2] : 0; + } + return -1; +} + +void spv_remove_decoration(spv_mod *m, uint32_t id, uint32_t decoration) +{ + for (uint32_t i = 0; i < m->count;) { + const spv_inst *in = &m->ins[i]; + if (in->op == SpvOpDecorate && in->w[0] == id && in->w[1] == decoration) spv_remove(m, i); + else i++; + } +} + +int spv_has_capability(const spv_mod *m, uint32_t cap) +{ + for (uint32_t i = 0; i < m->count; i++) + if (m->ins[i].op == SpvOpCapability && m->ins[i].w[0] == cap) return 1; + return 0; +} + +void spv_remove_capability(spv_mod *m, uint32_t cap) +{ + for (uint32_t i = 0; i < m->count;) { + if (m->ins[i].op == SpvOpCapability && m->ins[i].w[0] == cap) spv_remove(m, i); + else i++; + } +} + +void spv_add_capability(spv_mod *m, uint32_t cap) +{ + if (!spv_has_capability(m, cap)) spv_insert(m, 0, SpvOpCapability, &cap, 1); +} + +int spv_execution_model(const spv_mod *m) +{ + int e = spv_find_op(m, SpvOpEntryPoint, 0); + return e < 0 ? -1 : (int)m->ins[e].w[0]; +} + +void spv_add_interface(spv_mod *m, uint32_t var) +{ + for (uint32_t i = 0; i < m->count; i++) { + spv_inst *in = &m->ins[i]; + if (in->op != SpvOpEntryPoint) continue; + uint32_t *nw = realloc(in->w, (size_t)in->n * 4); + if (!nw) { + m->err = 1; + return; + } + nw[in->n - 1] = var; + in->w = nw; + in->n++; + } +} + +uint32_t spv_array_length(const spv_mod *m, uint32_t array_type) +{ + int d = spv_find_def(m, array_type); + if (d < 0 || m->ins[d].op != SpvOpTypeArray) return 0; + int c = spv_find_def(m, m->ins[d].w[2]); + if (c < 0 || m->ins[c].op != SpvOpConstant) return 0; + return m->ins[c].w[2]; +} + +/* Number of locations a type occupies. */ +static uint32_t loc_count(const spv_mod *m, uint32_t type, int depth) +{ + int d = spv_find_def(m, type); + if (d < 0 || depth > 16) return 1; + const spv_inst *in = &m->ins[d]; + switch (in->op) { + case SpvOpTypeArray: return spv_array_length(m, type) * loc_count(m, in->w[1], depth + 1); + case 24 /* Matrix */: return in->w[2] * loc_count(m, in->w[1], depth + 1); + case SpvOpTypeStruct: { + uint32_t s = 0; + for (uint32_t k = 1; k < (uint32_t)in->n - 1; k++) s += loc_count(m, in->w[k], depth + 1); + return s; + } + case SpvOpTypeVector: { + int e = spv_find_def(m, in->w[1]); + uint32_t width = e >= 0 ? m->ins[e].w[1] : 32; + return (width == 64 && in->w[2] > 2) ? 2 : 1; + } + default: return 1; + } +} + +int spv_max_location(const spv_mod *m, uint32_t storage) +{ + int max = -1; + for (uint32_t i = 0; i < m->count; i++) { + const spv_inst *in = &m->ins[i]; + if (in->op != SpvOpVariable || in->w[2] != storage) continue; + int p = spv_find_def(m, in->w[0]); + if (p < 0) continue; + uint32_t pointee = m->ins[p].w[2]; + int64_t loc = spv_get_decoration(m, in->w[1], SpvDecorationLocation); + if (loc >= 0) { + int top = (int)loc + (int)loc_count(m, pointee, 0) - 1; + if (top > max) max = top; + continue; + } + /* A block with member locations. */ + for (uint32_t k = 0; k < m->count; k++) { + const spv_inst *md = &m->ins[k]; + if (md->op == SpvOpMemberDecorate && md->w[0] == pointee && md->w[2] == SpvDecorationLocation) { + int st = spv_find_def(m, pointee); + uint32_t mt = st >= 0 && md->w[1] + 1 < m->ins[st].n ? m->ins[st].w[1 + md->w[1]] : 0; + int top = (int)md->w[3] + (int)loc_count(m, mt, 0) - 1; + if (top > max) max = top; + } + } + } + return max; +} diff --git a/app/src/main/cpp/vkbridge/spirv_edit.h b/app/src/main/cpp/vkbridge/spirv_edit.h new file mode 100644 index 000000000..a9387b014 --- /dev/null +++ b/app/src/main/cpp/vkbridge/spirv_edit.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * + * A small SPIR-V editor for the bridge's shader emulations: parse a module into instructions, + * look things up, append/insert/remove instructions, and write it back. + */ +#ifndef VKB_SPIRV_EDIT_H +#define VKB_SPIRV_EDIT_H + +#include +#include + +/* Opcodes and enums used (from the SPIR-V spec). */ +enum { + SpvOpName = 5, SpvOpMemberName = 6, SpvOpExtension = 10, SpvOpExtInstImport = 11, SpvOpMemoryModel = 14, + SpvOpEntryPoint = 15, SpvOpExecutionMode = 16, SpvOpCapability = 17, SpvOpTypeVoid = 19, SpvOpTypeBool = 20, + SpvOpTypeInt = 21, SpvOpTypeFloat = 22, SpvOpTypeVector = 23, SpvOpTypeArray = 28, SpvOpTypeRuntimeArray = 29, + SpvOpTypeStruct = 30, SpvOpTypePointer = 32, SpvOpTypeFunction = 33, SpvOpConstantTrue = 41, + SpvOpConstantFalse = 42, SpvOpConstant = 43, SpvOpConstantComposite = 44, SpvOpFunction = 54, + SpvOpFunctionParameter = 55, SpvOpFunctionEnd = 56, SpvOpVariable = 59, SpvOpLoad = 61, SpvOpStore = 62, + SpvOpAccessChain = 65, SpvOpInBoundsAccessChain = 66, SpvOpDecorate = 71, SpvOpMemberDecorate = 72, + SpvOpCompositeExtract = 81, SpvOpFOrdLessThan = 184, SpvOpLogicalOr = 166, SpvOpSelectionMerge = 247, + SpvOpLabel = 248, SpvOpBranch = 249, SpvOpBranchConditional = 250, SpvOpReturn = 253, SpvOpKill = 252, + SpvOpTerminateInvocation = 4416, SpvOpLine = 8, SpvOpNoLine = 317, SpvOpModuleProcessed = 330, + SpvOpString = 7, SpvOpSource = 3, SpvOpSourceExtension = 4, SpvOpSourceContinued = 2, SpvOpExecutionModeId = 331, + SpvOpDecorateId = 332, SpvOpDecorateString = 5632, SpvOpMemberDecorateString = 5633, SpvOpUndef = 1, + SpvOpExtInst = 12, SpvOpIAdd = 128, SpvOpUDiv = 134, SpvOpISub = 130, +}; +enum { SpvDecorationBuiltIn = 11, SpvDecorationLocation = 30, SpvDecorationComponent = 31, SpvDecorationBlock = 2, + SpvDecorationFlat = 14, SpvDecorationPatch = 15 }; +enum { SpvBuiltInPosition = 0, SpvBuiltInPointSize = 1, SpvBuiltInClipDistance = 3, SpvBuiltInCullDistance = 4, + SpvBuiltInInstanceIndex = 43, SpvBuiltInBaseInstance = 4425 }; +enum { SpvStorageClassInput = 1, SpvStorageClassOutput = 3, SpvStorageClassPrivate = 6, SpvStorageClassFunction = 7 }; +enum { SpvCapabilityClipDistance = 32, SpvCapabilityCullDistance = 33, SpvCapabilityTessellationPointSize = 23, + SpvCapabilityGeometryPointSize = 24, SpvCapabilityDrawParameters = 4427 }; +enum { SpvExecutionModelVertex = 0, SpvExecutionModelTessellationControl = 1, SpvExecutionModelTessellationEvaluation = 2, + SpvExecutionModelGeometry = 3, SpvExecutionModelFragment = 4, SpvExecutionModelGLCompute = 5 }; + +typedef struct spv_inst { + uint16_t op; + uint16_t n; /* word count including the first word */ + uint32_t *w; /* operand words (w[0] = first operand), n - 1 of them */ +} spv_inst; + +typedef struct spv_mod { + uint32_t header[5]; + spv_inst *ins; + uint32_t count, cap; + int err; +} spv_mod; + +int spv_parse(spv_mod *m, const uint32_t *code, size_t bytes); +/* Writes the module into a malloc'd buffer; returns its size in bytes (0 on failure). */ +size_t spv_write(const spv_mod *m, uint32_t **out); +void spv_free(spv_mod *m); + +static inline uint32_t spv_version(const spv_mod *m) { return m->header[1]; } +static inline uint32_t spv_new_id(spv_mod *m) { return m->header[3]++; } + +/* Insert an instruction before index `at` (at = count appends). Operands copied. Returns index. */ +uint32_t spv_insert(spv_mod *m, uint32_t at, uint16_t op, const uint32_t *ops, uint16_t nops); +void spv_remove(spv_mod *m, uint32_t at); + +/* Index of the first instruction with this opcode (or of the defining instruction of `id`), or -1. */ +int spv_find_op(const spv_mod *m, uint16_t op, uint32_t from); +int spv_find_def(const spv_mod *m, uint32_t id); +/* The id an instruction defines (result id), or 0. */ +uint32_t spv_result_id(const spv_inst *in); + +/* Section boundaries: index where new decorations / types / function code may be inserted. */ +uint32_t spv_types_end(const spv_mod *m); /* end of the types/constants/globals section */ +uint32_t spv_annotations_end(const spv_mod *m); /* end of the decoration section */ + +/* Types and constants: found when they already exist (non-aggregate types must be unique). */ +uint32_t spv_type_float(spv_mod *m, uint32_t width); +uint32_t spv_type_int(spv_mod *m, uint32_t width, uint32_t sign); +uint32_t spv_type_bool(spv_mod *m); +uint32_t spv_type_pointer(spv_mod *m, uint32_t storage, uint32_t pointee); +uint32_t spv_type_array(spv_mod *m, uint32_t elem, uint32_t len_const); +uint32_t spv_const_u32(spv_mod *m, uint32_t v); +uint32_t spv_const_f32(spv_mod *m, float v); + +/* Decorations of an id: returns the operand (after the decoration) or -1. */ +int64_t spv_get_decoration(const spv_mod *m, uint32_t id, uint32_t decoration); +void spv_remove_decoration(spv_mod *m, uint32_t id, uint32_t decoration); +int spv_has_capability(const spv_mod *m, uint32_t cap); +void spv_remove_capability(spv_mod *m, uint32_t cap); +void spv_add_capability(spv_mod *m, uint32_t cap); +/* The execution model of the (first) entry point, or -1. */ +int spv_execution_model(const spv_mod *m); +/* Adds an interface variable to every entry point. */ +void spv_add_interface(spv_mod *m, uint32_t var); +/* Highest Location used by variables of a storage class (members included), or -1. */ +int spv_max_location(const spv_mod *m, uint32_t storage); +/* Length of an OpTypeArray (via its constant), or 0. */ +uint32_t spv_array_length(const spv_mod *m, uint32_t array_type); + +#endif diff --git a/app/src/main/cpp/vkbridge/vkb_server.h b/app/src/main/cpp/vkbridge/vkb_server.h index 67541968b..331cd8616 100644 --- a/app/src/main/cpp/vkbridge/vkb_server.h +++ b/app/src/main/cpp/vkbridge/vkb_server.h @@ -107,7 +107,7 @@ void vkb_mem_release(vkb_srv_table *dev, vkb_srv_mem *m); void vkb_mem_free_all(vkb_srv_table *dev); /* emulation (server_emu*.c) */ -uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd); +uint32_t vkb_emu_detect(const vkb_dispatch *idt, VkPhysicalDevice pd, uint32_t *missing); void vkb_emu_device_create_info(vkb_srv_table *inst, VkPhysicalDevice pd, uint32_t emu, VkDeviceCreateInfo *ci, vkb_arena *a); void vkb_emu_device_init(vkb_srv_table *dev, uint32_t emu); diff --git a/tools/vkbridge/client/emu.c b/tools/vkbridge/client/emu.c index 90ca1594b..39f69384a 100644 --- a/tools/vkbridge/client/emu.c +++ b/tools/vkbridge/client/emu.c @@ -1,45 +1,141 @@ /* SPDX-License-Identifier: GPL-3.0-or-later * - * Client side of feature emulation (Phase 4 fills this in). + * Client side of feature emulation: the app sees a feature when the GPU has it or the server + * emulates it (vkb_server_pd_info.emu), and does not see one the GPU lacks (.missing). */ #define _GNU_SOURCE #include "emu.h" +#include "gen/vkb_client_gen.h" +#include "vkb_emu_formats.h" + +#include + +#define EMU(pd, f) (((pd)->info.emu & (f)) != 0) +#define MISSING(pd, f) (((pd)->info.missing & (f)) != 0 && !EMU(pd, f)) const VkExtensionProperties *vkb_emu_extensions(vkb_physdev *pd, uint32_t *n) { - static const VkExtensionProperties maint5 = {"VK_KHR_maintenance5", 1}; static __thread VkExtensionProperties list[8]; uint32_t c = 0; - if (pd->info.emu & VKB_EMU_MAINT5) list[c++] = maint5; + if (EMU(pd, VKB_EMU_MAINT5)) list[c++] = (VkExtensionProperties){"VK_KHR_maintenance5", 1}; + if (EMU(pd, VKB_EMU_DIVISOR)) { + list[c++] = (VkExtensionProperties){"VK_EXT_vertex_attribute_divisor", 3}; + list[c++] = (VkExtensionProperties){"VK_KHR_vertex_attribute_divisor", 1}; + } + if (EMU(pd, VKB_EMU_DEPTH_CLIP)) list[c++] = (VkExtensionProperties){"VK_EXT_depth_clip_enable", 1}; *n = c; return list; } -void vkb_emu_patch_limits(vkb_physdev *pd, VkPhysicalDeviceProperties *p) { (void)pd; (void)p; } -void vkb_emu_patch_properties_chain(vkb_physdev *pd, VkBaseOutStructure *b) { (void)pd; (void)b; } -void vkb_emu_patch_features(vkb_physdev *pd, VkPhysicalDeviceFeatures *f) { (void)pd; (void)f; } +void vkb_emu_patch_limits(vkb_physdev *pd, VkPhysicalDeviceProperties *p) +{ + (void)pd; + (void)p; +} + +void vkb_emu_patch_properties_chain(vkb_physdev *pd, VkBaseOutStructure *b) +{ + if (EMU(pd, VKB_EMU_DIVISOR)) { + if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT) + ((VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)b)->maxVertexAttribDivisor = UINT32_MAX; + if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES) { + VkPhysicalDeviceVertexAttributeDivisorProperties *p = (void *)b; + p->maxVertexAttribDivisor = UINT32_MAX; + p->supportsNonZeroFirstInstance = VK_TRUE; + } + } +} + +static void set_feature(vkb_physdev *pd, uint32_t flag, VkBool32 *f) +{ + if (EMU(pd, flag)) *f = VK_TRUE; + else if (MISSING(pd, flag)) *f = VK_FALSE; +} + +void vkb_emu_patch_features(vkb_physdev *pd, VkPhysicalDeviceFeatures *f) +{ + set_feature(pd, VKB_EMU_BCN, &f->textureCompressionBC); + set_feature(pd, VKB_EMU_CLIP_DISTANCE, &f->shaderClipDistance); + set_feature(pd, VKB_EMU_CULL_DISTANCE, &f->shaderCullDistance); +} + void vkb_emu_patch_features_chain(vkb_physdev *pd, VkBaseOutStructure *b) { - if ((pd->info.emu & VKB_EMU_MAINT5) && b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES) - ((VkPhysicalDeviceMaintenance5Features *)b)->maintenance5 = VK_TRUE; - /* Host image copy hands the driver host pointers: never offered across the bridge. */ - if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES) ((VkPhysicalDeviceVulkan14Features *)b)->hostImageCopy = VK_FALSE; - if (b->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES) ((VkPhysicalDeviceHostImageCopyFeatures *)b)->hostImageCopy = VK_FALSE; + switch ((int)b->sType) { + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES: + if (EMU(pd, VKB_EMU_MAINT5)) ((VkPhysicalDeviceMaintenance5Features *)b)->maintenance5 = VK_TRUE; + break; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES: { + VkPhysicalDeviceVertexAttributeDivisorFeatures *d = (void *)b; + set_feature(pd, VKB_EMU_DIVISOR, &d->vertexAttributeInstanceRateDivisor); + set_feature(pd, VKB_EMU_DIVISOR, &d->vertexAttributeInstanceRateZeroDivisor); + break; + } + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT: + set_feature(pd, VKB_EMU_DEPTH_CLIP, &((VkPhysicalDeviceDepthClipEnableFeaturesEXT *)b)->depthClipEnable); + break; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES: { + VkPhysicalDeviceVulkan14Features *v = (void *)b; + v->hostImageCopy = VK_FALSE; + set_feature(pd, VKB_EMU_DIVISOR, &v->vertexAttributeInstanceRateDivisor); + set_feature(pd, VKB_EMU_DIVISOR, &v->vertexAttributeInstanceRateZeroDivisor); + break; + } + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES: + /* Host image copy hands the driver host pointers: never offered across the bridge. */ + ((VkPhysicalDeviceHostImageCopyFeatures *)b)->hostImageCopy = VK_FALSE; + break; + default: + break; + } +} + +int vkb_emu_is_emulated_format(vkb_physdev *pd, VkFormat format) +{ + return vkb_is_bc(format) && EMU(pd, VKB_EMU_BCN); } int vkb_emu_format_properties(vkb_physdev *pd, VkFormat format, VkFormatProperties *fp, VkFormatProperties2 *fp2) { - (void)pd; (void)format; (void)fp; (void)fp2; - return 0; + if (!vkb_is_bc(format) || !(pd->info.missing & VKB_EMU_BCN)) return 0; + memset(fp, 0, sizeof(*fp)); + if (EMU(pd, VKB_EMU_BCN)) { + /* What the decoded image can do, limited to what a compressed texture is used for. */ + VkFormatProperties real; + vkb_wire_vkGetPhysicalDeviceFormatProperties((VkPhysicalDevice)pd, vkb_bc_view_format(format), &real); + fp->optimalTilingFeatures = real.optimalTilingFeatures & + (VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | + VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT | + VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT); + } + if (fp2) { + for (VkBaseOutStructure *b = (VkBaseOutStructure *)fp2->pNext; b; b = b->pNext) { + if (b->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3) { + VkFormatProperties3 *p3 = (void *)b; + p3->linearTilingFeatures = 0; + p3->optimalTilingFeatures = fp->optimalTilingFeatures; + p3->bufferFeatures = 0; + } else if (b->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT) { + ((VkDrmFormatModifierPropertiesListEXT *)b)->drmFormatModifierCount = 0; + } else if (b->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT) { + ((VkDrmFormatModifierPropertiesList2EXT *)b)->drmFormatModifierCount = 0; + } + } + } + return 1; } VkFormat vkb_emu_real_format(vkb_physdev *pd, VkFormat format, VkImageUsageFlags *usage, VkImageCreateFlags *flags) { - (void)pd; (void)usage; (void)flags; - return format; + if (!vkb_emu_is_emulated_format(pd, format)) return format; + /* The server creates the decoded image as storage-capable UNORM, mutable to the view format. */ + *flags &= ~(VkImageCreateFlags)(VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT); + *flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; + *usage |= VK_IMAGE_USAGE_STORAGE_BIT; + return vkb_bc_storage_format(format); } -int vkb_emu_is_emulated_format(vkb_physdev *pd, VkFormat format) { (void)pd; (void)format; return 0; } +/* Image creation is rewritten on the server (it knows the decode); nothing to do here. */ void vkb_emu_image_create_info(vkb_device *dev, VkImageCreateInfo *ci) { (void)dev; (void)ci; } void vkb_emu_image_created(vkb_device *dev, const VkImageCreateInfo *ci, VkImage image) { (void)dev; (void)ci; (void)image; } void vkb_emu_image_destroyed(vkb_device *dev, VkImage image) { (void)dev; (void)image; } diff --git a/tools/vkbridge/client/physdev.c b/tools/vkbridge/client/physdev.c index 378226e4d..0bb8647c3 100644 --- a/tools/vkbridge/client/physdev.c +++ b/tools/vkbridge/client/physdev.c @@ -144,6 +144,16 @@ static int hidden_ext(const char *name) return 0; } +static int bridged_device_ext_for(vkb_physdev *pd, const char *name) +{ + /* Clip distance emulation rewrites the vertex and fragment shaders together, at pipeline + * creation: pipeline libraries and shader identifiers would split them. */ + if ((pd->info.emu & VKB_EMU_CLIP_DISTANCE) && + (!strcmp(name, "VK_EXT_graphics_pipeline_library") || !strcmp(name, "VK_EXT_shader_module_identifier"))) + return 0; + return 1; +} + static int bridged_device_ext(const char *name) { if (hidden_ext(name)) return 0; @@ -189,7 +199,7 @@ int vkb_physdev_ready(vkb_physdev *pd) pd->exts = calloc(n + ncl + nemu + 2, sizeof(VkExtensionProperties)); pd->next = 0; for (uint32_t i = 0; i < n; i++) - if (bridged_device_ext(srv[i].extensionName)) pd->exts[pd->next++] = srv[i]; + if (bridged_device_ext(srv[i].extensionName) && bridged_device_ext_for(pd, srv[i].extensionName)) pd->exts[pd->next++] = srv[i]; for (uint32_t i = 0; i < nemu; i++) { int dup = 0; for (uint32_t j = 0; j < pd->next; j++) dup |= !strcmp(pd->exts[j].extensionName, emu[i].extensionName); diff --git a/tools/vkbridge/common/vkb_emu_formats.h b/tools/vkbridge/common/vkb_emu_formats.h new file mode 100644 index 000000000..1704adf98 --- /dev/null +++ b/tools/vkbridge/common/vkb_emu_formats.h @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * + * BCn emulation: what each block-compressed format is decoded into. Shared by the client (format + * queries) and the server (image creation, decode). The decoded image is created with the UNORM + * base format and MUTABLE_FORMAT so the decoder can write it through a storage view, and the + * app's sRGB views still work. + */ +#ifndef VKB_EMU_FORMATS_H +#define VKB_EMU_FORMATS_H + +#include + +static inline int vkb_is_bc(VkFormat f) +{ + return f >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && f <= VK_FORMAT_BC7_SRGB_BLOCK; +} + +/* The format an app's view of a BC format becomes (keeps sRGB/SNORM meaning). */ +static inline VkFormat vkb_bc_view_format(VkFormat f) +{ + switch (f) { + case VK_FORMAT_BC1_RGB_UNORM_BLOCK: + case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: + case VK_FORMAT_BC2_UNORM_BLOCK: + case VK_FORMAT_BC3_UNORM_BLOCK: + case VK_FORMAT_BC7_UNORM_BLOCK: return VK_FORMAT_R8G8B8A8_UNORM; + case VK_FORMAT_BC1_RGB_SRGB_BLOCK: + case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: + case VK_FORMAT_BC2_SRGB_BLOCK: + case VK_FORMAT_BC3_SRGB_BLOCK: + case VK_FORMAT_BC7_SRGB_BLOCK: return VK_FORMAT_R8G8B8A8_SRGB; + case VK_FORMAT_BC4_UNORM_BLOCK: return VK_FORMAT_R8_UNORM; + case VK_FORMAT_BC4_SNORM_BLOCK: return VK_FORMAT_R8_SNORM; + case VK_FORMAT_BC5_UNORM_BLOCK: return VK_FORMAT_R8G8_UNORM; + case VK_FORMAT_BC5_SNORM_BLOCK: return VK_FORMAT_R8G8_SNORM; + case VK_FORMAT_BC6H_UFLOAT_BLOCK: + case VK_FORMAT_BC6H_SFLOAT_BLOCK: return VK_FORMAT_R16G16B16A16_SFLOAT; + default: return f; + } +} + +/* The format the decoded image is created with and written through (storage-capable). */ +static inline VkFormat vkb_bc_storage_format(VkFormat f) +{ + switch (vkb_bc_view_format(f)) { + case VK_FORMAT_R8G8B8A8_SRGB: + case VK_FORMAT_R8G8B8A8_UNORM: return VK_FORMAT_R8G8B8A8_UNORM; + case VK_FORMAT_R8_SNORM: + case VK_FORMAT_R8_UNORM: return VK_FORMAT_R8_UNORM; + case VK_FORMAT_R8G8_SNORM: + case VK_FORMAT_R8G8_UNORM: return VK_FORMAT_R8G8_UNORM; + case VK_FORMAT_R16G16B16A16_SFLOAT: return VK_FORMAT_R16G16B16A16_SFLOAT; + default: return f; + } +} + +/* Bytes per 4x4 block. */ +static inline uint32_t vkb_bc_block_bytes(VkFormat f) +{ + switch (f) { + case VK_FORMAT_BC1_RGB_UNORM_BLOCK: + case VK_FORMAT_BC1_RGB_SRGB_BLOCK: + case VK_FORMAT_BC1_RGBA_UNORM_BLOCK: + case VK_FORMAT_BC1_RGBA_SRGB_BLOCK: + case VK_FORMAT_BC4_UNORM_BLOCK: + case VK_FORMAT_BC4_SNORM_BLOCK: return 8; + default: return 16; + } +} + +#endif diff --git a/tools/vkbridge/common/vkb_proto.h b/tools/vkbridge/common/vkb_proto.h index 0fe0d8358..ed4b877a8 100644 --- a/tools/vkbridge/common/vkb_proto.h +++ b/tools/vkbridge/common/vkb_proto.h @@ -46,7 +46,8 @@ typedef struct vkb_server_pd_info { uint32_t emu; /* VKB_EMU_* the server will perform */ uint32_t emu_available; /* VKB_EMU_* the server could perform */ uint32_t real_api_version; - uint32_t reserved[7]; + uint32_t missing; /* VKB_EMU_* features the GPU lacks (emulated or not) */ + uint32_t reserved[6]; } vkb_server_pd_info; /* vkAllocateMemory wire extras (after VkMemoryAllocateInfo). */ diff --git a/tools/vkbridge/host/mali_env.sh b/tools/vkbridge/host/mali_env.sh new file mode 100755 index 000000000..7ec508894 --- /dev/null +++ b/tools/vkbridge/host/mali_env.sh @@ -0,0 +1,15 @@ +#!/bin/bash +# Source this: a host bridge server that behaves like the Mali-G720 driver as far as Phase 0 +# found (no BCn, clip/cull distance, vertex attribute divisor, maintenance5; Vulkan 1.3). +_vkb_host=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +export VKBRIDGE_SOCKET=${XDG_RUNTIME_DIR:-/tmp}/vkbridge-host.sock +pgrep -x vkbridge-server >/dev/null && kill $(pgrep -x vkbridge-server) 2>/dev/null; sleep 0.3 +rm -f "$VKBRIDGE_SOCKET" +VKBRIDGE_FAKE_MISSING=${VKB_MISSING:-bc,clip,cull,divisor,maint5} VK_DRIVER_FILES=${1:-/usr/share/vulkan/icd.d/radeon_icd.json} \ + nohup "$_vkb_host/out/vkbridge-server" --socket "$VKBRIDGE_SOCKET" --log "$_vkb_host/out/server.log" >/dev/null 2>&1 & +for _i in $(seq 50); do [ -S "$VKBRIDGE_SOCKET" ] && break; sleep 0.1; done +chmod 666 "$VKBRIDGE_SOCKET" +export VK_DRIVER_FILES=$_vkb_host/out/vkbridge_icd.json +export VKBRIDGE_HIDE_EXTS=VK_KHR_maintenance5,VK_EXT_vertex_attribute_divisor,VK_KHR_vertex_attribute_divisor +export VKBRIDGE_MAX_API=1.3 +echo "vkbridge server up, imitating Mali's gaps"