Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vulkan: Workaround broken push constant support on Ivy Bridge #2733

Merged
merged 2 commits into from
Feb 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions gfx/common/vulkan_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,22 +331,36 @@ void vulkan_destroy_texture(VkDevice device, struct vk_texture *tex)

static void vulkan_write_quad_descriptors(VkDevice device,
VkDescriptorSet set,
VkBuffer buffer,
VkDeviceSize offset,
VkDeviceSize range,
const struct vk_texture *texture,
VkSampler sampler)
{
VkWriteDescriptorSet write = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
VkDescriptorImageInfo image_info;
VkDescriptorBufferInfo buffer_info;

image_info.sampler = sampler;
image_info.imageView = texture->view;
image_info.imageLayout = texture->layout;

buffer_info.buffer = buffer;
buffer_info.offset = offset;
buffer_info.range = range;

write.dstSet = set;
write.dstBinding = 0;
write.descriptorCount = 1;
write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
write.pBufferInfo = &buffer_info;
vkUpdateDescriptorSets(device, 1, &write, 0, NULL);

write.dstSet = set;
write.dstBinding = 1;
write.descriptorCount = 1;
write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write.pImageInfo = &image_info;

vkUpdateDescriptorSets(device, 1, &write, 0, NULL);
}

Expand Down Expand Up @@ -395,20 +409,25 @@ void vulkan_draw_triangles(vk_t *vk, const struct vk_draw_triangles *call)

/* Upload descriptors */
{
struct vk_draw_uniform ubo;
VkDescriptorSet set;

ubo.mvp = *call->mvp;
ubo.texsize[0] = call->texture->width;
ubo.texsize[1] = call->texture->height;
ubo.texsize[2] = 1.0f / call->texture->width;
ubo.texsize[3] = 1.0f / call->texture->height;

if (call->texture->view != vk->tracker.view || call->sampler != vk->tracker.sampler)
if (memcmp(call->mvp, &vk->tracker.mvp, sizeof(*call->mvp)) ||
call->texture->view != vk->tracker.view ||
call->sampler != vk->tracker.sampler)
{
/* Upload UBO */
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*call->mvp), &range))
return;
memcpy(range.data, call->mvp, sizeof(*call->mvp));

set = vulkan_descriptor_manager_alloc(vk->context->device, &vk->chain->descriptor_manager);
vulkan_write_quad_descriptors(vk->context->device,
set,
range.buffer,
range.offset,
sizeof(*call->mvp),
call->texture,
call->sampler);

Expand All @@ -418,10 +437,8 @@ void vulkan_draw_triangles(vk_t *vk, const struct vk_draw_triangles *call)

vk->tracker.view = call->texture->view;
vk->tracker.sampler = call->sampler;
vk->tracker.mvp = *call->mvp;
}

vkCmdPushConstants(vk->cmd, vk->pipelines.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0, sizeof(struct vk_draw_uniform), &ubo);
}

/* VBO is already uploaded. */
Expand Down Expand Up @@ -449,24 +466,29 @@ void vulkan_draw_quad(vk_t *vk, const struct vk_draw_quad *quad)

/* Upload descriptors */
{
struct vk_draw_uniform ubo;
VkDescriptorSet set;
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(struct vk_draw_uniform), &range))
sizeof(*quad->mvp), &range))
return;

ubo.mvp = *quad->mvp;
ubo.texsize[0] = quad->texture->width;
ubo.texsize[1] = quad->texture->height;
ubo.texsize[2] = 1.0f / quad->texture->width;
ubo.texsize[3] = 1.0f / quad->texture->height;

if (quad->texture->view != vk->tracker.view || quad->sampler != vk->tracker.sampler)
if (memcmp(quad->mvp, &vk->tracker.mvp, sizeof(*quad->mvp)) ||
quad->texture->view != vk->tracker.view ||
quad->sampler != vk->tracker.sampler)
{
/* Upload UBO */
struct vk_buffer_range range;
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->ubo,
sizeof(*quad->mvp), &range))
return;
memcpy(range.data, quad->mvp, sizeof(*quad->mvp));

set = vulkan_descriptor_manager_alloc(vk->context->device, &vk->chain->descriptor_manager);
vulkan_write_quad_descriptors(vk->context->device,
set,
range.buffer,
range.offset,
sizeof(*quad->mvp),
quad->texture,
quad->sampler);

Expand All @@ -476,10 +498,8 @@ void vulkan_draw_quad(vk_t *vk, const struct vk_draw_quad *quad)

vk->tracker.view = quad->texture->view;
vk->tracker.sampler = quad->sampler;
vk->tracker.mvp = *quad->mvp;
}

vkCmdPushConstants(vk->cmd, vk->pipelines.layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0, sizeof(struct vk_draw_uniform), &ubo);
}

/* Upload VBO */
Expand Down
7 changes: 1 addition & 6 deletions gfx/common/vulkan_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ typedef struct vulkan_context
bool swapchain_is_srgb;
} vulkan_context_t;

struct vk_draw_uniform
{
math_matrix_4x4 mvp;
float texsize[4];
};

struct vk_color
{
float r, g, b, a;
Expand Down Expand Up @@ -308,6 +302,7 @@ typedef struct vk
VkPipeline pipeline;
VkImageView view;
VkSampler sampler;
math_matrix_4x4 mvp;
uint64_t dirty;
} tracker;

Expand Down
33 changes: 17 additions & 16 deletions gfx/drivers/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,28 @@ static void vulkan_init_pipeline_layout(vk_t *vk)
{
VkDescriptorSetLayoutCreateInfo set_layout_info = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
VkPipelineLayoutCreateInfo layout_info = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
VkDescriptorSetLayoutBinding binding = {0};
VkPushConstantRange push_range = {0};
VkDescriptorSetLayoutBinding bindings[2] = {{0}};

binding.binding = 0;
binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
binding.descriptorCount = 1;
binding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
binding.pImmutableSamplers = NULL;
bindings[0].binding = 0;
bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
bindings[0].descriptorCount = 1;
bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
bindings[0].pImmutableSamplers = NULL;

bindings[1].binding = 1;
bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
bindings[1].descriptorCount = 1;
bindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
bindings[1].pImmutableSamplers = NULL;

set_layout_info.bindingCount = 1;
set_layout_info.pBindings = &binding;
set_layout_info.bindingCount = 2;
set_layout_info.pBindings = bindings;

vkCreateDescriptorSetLayout(vk->context->device, &set_layout_info, NULL, &vk->pipelines.set_layout);

layout_info.setLayoutCount = 1;
layout_info.pSetLayouts = &vk->pipelines.set_layout;
layout_info.pushConstantRangeCount = 1;
layout_info.pPushConstantRanges = &push_range;

push_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
push_range.offset = 0;
push_range.size = sizeof(struct vk_draw_uniform);
vkCreatePipelineLayout(vk->context->device, &layout_info, NULL, &vk->pipelines.layout);
}

Expand Down Expand Up @@ -396,14 +396,15 @@ static void vulkan_init_descriptor_pool(vk_t *vk)
{
unsigned i;
VkDescriptorPoolCreateInfo pool_info = { VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO };
static const VkDescriptorPoolSize pool_sizes[1] = {
static const VkDescriptorPoolSize pool_sizes[2] = {
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1 },
{ VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1 },
};

for (i = 0; i < vk->num_swapchain_images; i++)
{
vk->swapchain[i].descriptor_manager = vulkan_create_descriptor_manager(vk->context->device,
pool_sizes, 1, vk->pipelines.set_layout);
pool_sizes, 2, vk->pipelines.set_layout);
}
}

Expand Down
2 changes: 1 addition & 1 deletion gfx/drivers/vulkan_shaders/alpha_blend.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 = 0) uniform highp sampler2D uTex;
layout(set = 0, binding = 1) uniform highp sampler2D uTex;

void main()
{
Expand Down
4 changes: 2 additions & 2 deletions gfx/drivers/vulkan_shaders/alpha_blend.frag.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ unsigned char alpha_blend_frag_spv[] = {
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, 0x14, 0x00, 0x00, 0x00,
0x10, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
0x10, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 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,
Expand All @@ -21,7 +21,7 @@ unsigned char alpha_blend_frag_spv[] = {
0x0b, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x47, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00, 0x10, 0x00, 0x00, 0x00,
0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
0x21, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x47, 0x00, 0x04, 0x00,
0x14, 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,
Expand Down
3 changes: 1 addition & 2 deletions gfx/drivers/vulkan_shaders/alpha_blend.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ layout(location = 2) in vec4 Color;
layout(location = 0) out vec2 vTexCoord;
layout(location = 1) out vec4 vColor;

layout(push_constant, std140) uniform UBO
layout(set = 0, binding = 0, std140) uniform UBO
{
mat4 MVP;
vec4 texsize;
} global;

void main()
Expand Down
Loading