From cece4e5ae250374f9a080d1e150a477c5dc6daab Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Sun, 21 Feb 2016 13:52:30 +0100 Subject: [PATCH] Vulkan: Add workaround for broken VkImageView swizzling. Certain early drivers fail to apply swizzle mask. --- gfx/common/vulkan_common.h | 1 + gfx/drivers/vulkan.c | 30 ++++++--- gfx/drivers/vulkan_shaders/font.frag | 12 ++++ gfx/drivers/vulkan_shaders/font.frag.inc | 80 ++++++++++++++++++++++++ gfx/drivers_font/vulkan_raster_font.c | 7 ++- 5 files changed, 120 insertions(+), 10 deletions(-) create mode 100644 gfx/drivers/vulkan_shaders/font.frag create mode 100644 gfx/drivers/vulkan_shaders/font.frag.inc diff --git a/gfx/common/vulkan_common.h b/gfx/common/vulkan_common.h index 1a952d2ff6..9e981e0de7 100644 --- a/gfx/common/vulkan_common.h +++ b/gfx/common/vulkan_common.h @@ -357,6 +357,7 @@ typedef struct vk struct { VkPipeline alpha_blend; + VkPipeline font; VkDescriptorSetLayout set_layout; VkPipelineLayout layout; VkPipelineCache cache; diff --git a/gfx/drivers/vulkan.c b/gfx/drivers/vulkan.c index b65cc8fd8a..7de2c4ae1e 100644 --- a/gfx/drivers/vulkan.c +++ b/gfx/drivers/vulkan.c @@ -16,6 +16,7 @@ #include "../common/vulkan_common.h" #include "vulkan_shaders/alpha_blend.vert.inc" #include "vulkan_shaders/alpha_blend.frag.inc" +#include "vulkan_shaders/font.frag.inc" #include #include @@ -301,7 +302,6 @@ static void vulkan_init_pipelines(vk_t *vk) pipe.renderPass = vk->render_pass; pipe.layout = vk->pipelines.layout; - /* Alpha-blended pipeline. */ module_info.codeSize = alpha_blend_vert_spv_len; module_info.pCode = (const uint32_t*)alpha_blend_vert_spv; shader_stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT; @@ -309,13 +309,6 @@ static void vulkan_init_pipelines(vk_t *vk) vkCreateShaderModule(vk->context->device, &module_info, NULL, &shader_stages[0].module); - module_info.codeSize = alpha_blend_frag_spv_len; - module_info.pCode = (const uint32_t*)alpha_blend_frag_spv; - shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; - shader_stages[1].pName = "main"; - vkCreateShaderModule(vk->context->device, - &module_info, NULL, &shader_stages[1].module); - blend_attachment.blendEnable = true; blend_attachment.colorWriteMask = 0xf; blend_attachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; @@ -325,6 +318,26 @@ static void vulkan_init_pipelines(vk_t *vk) blend_attachment.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; blend_attachment.alphaBlendOp = VK_BLEND_OP_ADD; + /* Glyph pipeline */ + module_info.codeSize = font_frag_spv_len; + module_info.pCode = (const uint32_t*)font_frag_spv; + shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; + shader_stages[1].pName = "main"; + vkCreateShaderModule(vk->context->device, + &module_info, NULL, &shader_stages[1].module); + + vkCreateGraphicsPipelines(vk->context->device, vk->pipelines.cache, + 1, &pipe, NULL, &vk->pipelines.font); + vkDestroyShaderModule(vk->context->device, shader_stages[1].module, NULL); + + /* Alpha-blended pipeline. */ + module_info.codeSize = alpha_blend_frag_spv_len; + module_info.pCode = (const uint32_t*)alpha_blend_frag_spv; + shader_stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT; + shader_stages[1].pName = "main"; + vkCreateShaderModule(vk->context->device, + &module_info, NULL, &shader_stages[1].module); + vkCreateGraphicsPipelines(vk->context->device, vk->pipelines.cache, 1, &pipe, NULL, &vk->pipelines.alpha_blend); @@ -513,6 +526,7 @@ static void vulkan_deinit_pipelines(vk_t *vk) unsigned i; vulkan_deinit_pipeline_layout(vk); vkDestroyPipeline(vk->context->device, vk->pipelines.alpha_blend, NULL); + vkDestroyPipeline(vk->context->device, vk->pipelines.font, NULL); for (i = 0; i < 4; i++) vkDestroyPipeline(vk->context->device, vk->display.pipelines[i], NULL); } diff --git a/gfx/drivers/vulkan_shaders/font.frag b/gfx/drivers/vulkan_shaders/font.frag new file mode 100644 index 0000000000..8aacb6fa39 --- /dev/null +++ b/gfx/drivers/vulkan_shaders/font.frag @@ -0,0 +1,12 @@ +#version 310 es +precision highp float; +layout(location = 0) in vec2 vTexCoord; +layout(location = 1) in vec4 vColor; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 1) uniform highp sampler2D uTex; + +void main() +{ + // Workaround for early drivers which fail to apply swizzle in VkImageView. + FragColor = vec4(vColor.rgb, vColor.a * texture(uTex, vTexCoord).x); +} diff --git a/gfx/drivers/vulkan_shaders/font.frag.inc b/gfx/drivers/vulkan_shaders/font.frag.inc new file mode 100644 index 0000000000..4adb43b6df --- /dev/null +++ b/gfx/drivers/vulkan_shaders/font.frag.inc @@ -0,0 +1,80 @@ +unsigned char font_frag_spv[] = { + 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, + 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x47, 0x4c, 0x53, 0x4c, 0x2e, 0x73, 0x74, 0x64, 0x2e, 0x34, 0x35, 0x30, + 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x36, 0x01, 0x00, 0x00, + 0x05, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, + 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x04, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x76, 0x43, 0x6f, 0x6c, + 0x6f, 0x72, 0x00, 0x00, 0x05, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00, + 0x75, 0x54, 0x65, 0x78, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x00, + 0x1b, 0x00, 0x00, 0x00, 0x76, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, + 0x64, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00, 0x00, + 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x47, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x17, 0x00, 0x00, 0x00, + 0x21, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, + 0x1b, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x21, 0x00, 0x03, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x16, 0x00, 0x03, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x3b, 0x00, 0x04, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x0a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, + 0x3b, 0x00, 0x04, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x15, 0x00, 0x04, 0x00, + 0x0f, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x2b, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, 0x11, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x19, 0x00, 0x09, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x03, 0x00, + 0x15, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, + 0x3b, 0x00, 0x04, 0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x17, 0x00, 0x04, 0x00, 0x19, 0x00, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x04, 0x00, + 0x1a, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, + 0x3b, 0x00, 0x04, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00, + 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x05, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, 0x05, 0x00, 0x00, 0x00, + 0x3d, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, + 0x41, 0x00, 0x05, 0x00, 0x11, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, + 0x0b, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, + 0x3d, 0x00, 0x04, 0x00, 0x15, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x17, 0x00, 0x00, 0x00, 0x3d, 0x00, 0x04, 0x00, 0x19, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x57, 0x00, 0x05, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, + 0x1c, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x1f, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x85, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x13, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, + 0x06, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, + 0x22, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x51, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, + 0x0e, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x07, 0x00, + 0x07, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00, + 0x22, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, + 0x3e, 0x00, 0x03, 0x00, 0x09, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, + 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00 +}; +unsigned int font_frag_spv_len = 920; diff --git a/gfx/drivers_font/vulkan_raster_font.c b/gfx/drivers_font/vulkan_raster_font.c index 5cd0585b62..465c94e8d6 100644 --- a/gfx/drivers_font/vulkan_raster_font.c +++ b/gfx/drivers_font/vulkan_raster_font.c @@ -36,12 +36,14 @@ static void *vulkan_raster_font_init_font(void *data, const struct font_atlas *atlas = NULL; vulkan_raster_t *font = (vulkan_raster_t*)calloc(1, sizeof(*font)); +#if 0 VkComponentMapping swizzle = { VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_ONE, VK_COMPONENT_SWIZZLE_R, }; +#endif if (!font) return NULL; @@ -58,7 +60,8 @@ static void *vulkan_raster_font_init_font(void *data, atlas = font->font_driver->get_atlas(font->font_data); font->texture = vulkan_create_texture(font->vk, NULL, - atlas->width, atlas->height, VK_FORMAT_R8_UNORM, atlas->buffer, &swizzle, VULKAN_TEXTURE_STATIC); + atlas->width, atlas->height, VK_FORMAT_R8_UNORM, atlas->buffer, + NULL /*&swizzle*/, VULKAN_TEXTURE_STATIC); return font; } @@ -224,7 +227,7 @@ static void vulkan_raster_font_setup_viewport(vulkan_raster_t *font, bool full_s static void vulkan_raster_font_flush(vulkan_raster_t *font) { const struct vk_draw_triangles call = { - font->vk->pipelines.alpha_blend, + font->vk->pipelines.font, &font->texture, font->vk->samplers.nearest, &font->vk->mvp,