radv/gfx10: Only set HW edge flags with gs & tess disabled.
[mesa.git] / src / amd / vulkan / radv_descriptor_set.c
index 7374f94d260d73348ee91aafcf5a6bc7e2572018..bf926288aa025504da436c1581ec2dd1e5defa26 100644 (file)
@@ -30,6 +30,7 @@
 #include "util/mesa-sha1.h"
 #include "radv_private.h"
 #include "sid.h"
+#include "vk_format.h"
 #include "vk_util.h"
 
 
@@ -82,27 +83,51 @@ VkResult radv_CreateDescriptorSetLayout(
 
        uint32_t max_binding = 0;
        uint32_t immutable_sampler_count = 0;
+       uint32_t ycbcr_sampler_count = 0;
        for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
                max_binding = MAX2(max_binding, pCreateInfo->pBindings[j].binding);
                if ((pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
                     pCreateInfo->pBindings[j].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) &&
-                    pCreateInfo->pBindings[j].pImmutableSamplers)
+                    pCreateInfo->pBindings[j].pImmutableSamplers) {
                        immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
+
+                       bool has_ycbcr_sampler = false;
+                       for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
+                               if (radv_sampler_from_handle(pCreateInfo->pBindings[j].pImmutableSamplers[i])->ycbcr_sampler)
+                                       has_ycbcr_sampler = true;
+                       }
+
+                       if (has_ycbcr_sampler)
+                               ycbcr_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
+               }
        }
 
        uint32_t samplers_offset = sizeof(struct radv_descriptor_set_layout) +
                (max_binding + 1) * sizeof(set_layout->binding[0]);
        size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);
+       if (ycbcr_sampler_count > 0) {
+               size += ycbcr_sampler_count * sizeof(struct radv_sampler_ycbcr_conversion) + (max_binding + 1) * sizeof(uint32_t);
+       }
 
-       set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8,
-                                VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+       set_layout = vk_zalloc2(&device->alloc, pAllocator, size, 8,
+                               VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (!set_layout)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
        set_layout->flags = pCreateInfo->flags;
+       set_layout->layout_size = size;
 
        /* We just allocate all the samplers at the end of the struct */
        uint32_t *samplers = (uint32_t*)&set_layout->binding[max_binding + 1];
+       struct radv_sampler_ycbcr_conversion *ycbcr_samplers = NULL;
+       uint32_t *ycbcr_sampler_offsets = NULL;
+
+       if (ycbcr_sampler_count > 0) {
+               ycbcr_sampler_offsets = samplers + 4 * immutable_sampler_count;
+               set_layout->ycbcr_sampler_offsets_offset = (char*)ycbcr_sampler_offsets - (char*)set_layout;
+               ycbcr_samplers = (struct radv_sampler_ycbcr_conversion *)(ycbcr_sampler_offsets + max_binding + 1);
+       } else
+               set_layout->ycbcr_sampler_offsets_offset = 0;
 
        VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings,
                                                                        pCreateInfo->bindingCount);
@@ -128,6 +153,24 @@ VkResult radv_CreateDescriptorSetLayout(
                uint32_t alignment;
                unsigned binding_buffer_count = 0;
                uint32_t descriptor_count = binding->descriptorCount;
+               bool has_ycbcr_sampler = false;
+
+               /* main image + fmask */
+               uint32_t max_sampled_image_descriptors = 2;
+
+               if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&
+                   binding->pImmutableSamplers) {
+                       for (unsigned i = 0; i < binding->descriptorCount; ++i) {
+                               struct radv_sampler_ycbcr_conversion *conversion =
+                                       radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;
+
+                               if (conversion) {
+                                       has_ycbcr_sampler = true;
+                                       max_sampled_image_descriptors = MAX2(max_sampled_image_descriptors,
+                                                                            vk_format_get_plane_count(conversion->format));
+                               }
+                       }
+               }
 
                switch (binding->descriptorType) {
                case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
@@ -204,13 +247,25 @@ VkResult radv_CreateDescriptorSetLayout(
 
                        /* Don't reserve space for the samplers if they're not accessed. */
                        if (set_layout->binding[b].immutable_samplers_equal) {
-                               if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
+                               if (binding->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER &&
+                                   max_sampled_image_descriptors <= 2)
                                        set_layout->binding[b].size -= 32;
                                else if (binding->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)
                                        set_layout->binding[b].size -= 16;
                        }
                        samplers += 4 * binding->descriptorCount;
                        samplers_offset += 4 * sizeof(uint32_t) * binding->descriptorCount;
+
+                       if (has_ycbcr_sampler) {
+                               ycbcr_sampler_offsets[b] = (const char*)ycbcr_samplers - (const char*)set_layout;
+                               for (uint32_t i = 0; i < binding->descriptorCount; i++) {
+                                       if (radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler)
+                                               ycbcr_samplers[i] = *radv_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler;
+                                       else
+                                               ycbcr_samplers[i].format = VK_FORMAT_UNDEFINED;
+                               }
+                               ycbcr_samplers += binding->descriptorCount;
+                       }
                }
 
                set_layout->size += descriptor_count * set_layout->binding[b].size;
@@ -376,12 +431,8 @@ VkResult radv_CreatePipelineLayout(
                for (uint32_t b = 0; b < set_layout->binding_count; b++) {
                        dynamic_offset_count += set_layout->binding[b].array_size * set_layout->binding[b].dynamic_offset_count;
                        dynamic_shader_stages |= set_layout->dynamic_shader_stages;
-                       if (set_layout->binding[b].immutable_samplers_offset)
-                               _mesa_sha1_update(&ctx, radv_immutable_samplers(set_layout, set_layout->binding + b),
-                                                 set_layout->binding[b].array_size * 4 * sizeof(uint32_t));
                }
-               _mesa_sha1_update(&ctx, set_layout->binding,
-                                 sizeof(set_layout->binding[0]) * set_layout->binding_count);
+               _mesa_sha1_update(&ctx, set_layout, set_layout->layout_size);
        }
 
        layout->dynamic_offset_count = dynamic_offset_count;
@@ -452,7 +503,17 @@ radv_descriptor_set_create(struct radv_device *device,
        }
 
        set->layout = layout;
-       uint32_t layout_size = align_u32(layout->size, 32);
+       uint32_t layout_size = layout->size;
+       if (variable_count) {
+               assert(layout->has_variable_descriptors);
+               uint32_t stride = layout->binding[layout->binding_count - 1].size;
+               if (layout->binding[layout->binding_count - 1].type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT)
+                       stride = 1;
+
+               layout_size = layout->binding[layout->binding_count - 1].offset +
+                             *variable_count * stride;
+       }
+       layout_size = align_u32(layout_size, 32);
        if (layout_size) {
                set->size = layout_size;
 
@@ -510,7 +571,7 @@ radv_descriptor_set_create(struct radv_device *device,
 
                        unsigned offset = layout->binding[i].offset / 4;
                        if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
-                               offset += 16;
+                               offset += radv_combined_image_descriptor_sampler_offset(layout->binding + i) / 4;
 
                        const uint32_t *samplers = (const uint32_t*)((const char*)layout + layout->binding[i].immutable_samplers_offset);
                        for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
@@ -786,9 +847,16 @@ static void write_buffer_descriptor(struct radv_device *device,
        dst[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
                S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
                S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
-               S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
-               S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
-               S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
+               S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
+
+       if (device->physical_device->rad_info.chip_class >= GFX10) {
+               dst[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
+                         S_008F0C_OOB_SELECT(3) |
+                         S_008F0C_RESOURCE_LEVEL(1);
+       } else {
+               dst[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
+                         S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
+       }
 
        if (cmd_buffer)
                radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo);
@@ -829,21 +897,21 @@ static void write_dynamic_buffer_descriptor(struct radv_device *device,
 static void
 write_image_descriptor(struct radv_device *device,
                       struct radv_cmd_buffer *cmd_buffer,
-                      unsigned *dst,
+                      unsigned size, unsigned *dst,
                       struct radeon_winsys_bo **buffer_list,
                       VkDescriptorType descriptor_type,
                       const VkDescriptorImageInfo *image_info)
 {
        RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);
-       uint32_t *descriptor;
+       union radv_descriptor *descriptor;
 
        if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
-               descriptor = iview->storage_descriptor;
+               descriptor = &iview->storage_descriptor;
        } else {
-               descriptor = iview->descriptor;
+               descriptor = &iview->descriptor;
        }
 
-       memcpy(dst, descriptor, 16 * 4);
+       memcpy(dst, descriptor, size);
 
        if (cmd_buffer)
                radv_cs_add_buffer(device->ws, cmd_buffer->cs, iview->bo);
@@ -854,6 +922,7 @@ write_image_descriptor(struct radv_device *device,
 static void
 write_combined_image_sampler_descriptor(struct radv_device *device,
                                        struct radv_cmd_buffer *cmd_buffer,
+                                       unsigned sampler_offset,
                                        unsigned *dst,
                                        struct radeon_winsys_bo **buffer_list,
                                        VkDescriptorType descriptor_type,
@@ -862,10 +931,12 @@ write_combined_image_sampler_descriptor(struct radv_device *device,
 {
        RADV_FROM_HANDLE(radv_sampler, sampler, image_info->sampler);
 
-       write_image_descriptor(device, cmd_buffer, dst, buffer_list, descriptor_type, image_info);
+       write_image_descriptor(device, cmd_buffer, sampler_offset, dst, buffer_list,
+                              descriptor_type, image_info);
        /* copy over sampler state */
-       if (has_sampler)
-               memcpy(dst + 16, sampler->state, 16);
+       if (has_sampler) {
+               memcpy(dst + sampler_offset / sizeof(*dst), sampler->state, 16);
+       }
 }
 
 static void
@@ -938,20 +1009,23 @@ void radv_update_descriptor_sets(
                        case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
                        case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
                        case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
-                               write_image_descriptor(device, cmd_buffer, ptr, buffer_list,
+                               write_image_descriptor(device, cmd_buffer, 64, ptr, buffer_list,
                                                       writeset->descriptorType,
                                                       writeset->pImageInfo + j);
                                break;
-                       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
-                               write_combined_image_sampler_descriptor(device, cmd_buffer, ptr, buffer_list,
+                       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
+                               unsigned sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout);
+                               write_combined_image_sampler_descriptor(device, cmd_buffer, sampler_offset,
+                                                                       ptr, buffer_list,
                                                                        writeset->descriptorType,
                                                                        writeset->pImageInfo + j,
                                                                        !binding_layout->immutable_samplers_offset);
                                if (copy_immutable_samplers) {
                                        const unsigned idx = writeset->dstArrayElement + j;
-                                       memcpy(ptr + 16, samplers + 4 * idx, 16);
+                                       memcpy((char*)ptr + sampler_offset, samplers + 4 * idx, 16);
                                }
                                break;
+                       }
                        case VK_DESCRIPTOR_TYPE_SAMPLER:
                                if (!binding_layout->immutable_samplers_offset) {
                                        write_sampler_descriptor(device, ptr,
@@ -1110,6 +1184,7 @@ VkResult radv_CreateDescriptorUpdateTemplate(VkDevice _device,
                        .dst_stride = dst_stride,
                        .buffer_offset = buffer_offset,
                        .has_sampler = !binding_layout->immutable_samplers_offset,
+                       .sampler_offset = radv_combined_image_descriptor_sampler_offset(binding_layout),
                        .immutable_samplers = immutable_samplers
                };
        }
@@ -1174,17 +1249,18 @@ void radv_update_descriptor_set_with_template(struct radv_device *device,
                        case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
                        case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
                        case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
-                               write_image_descriptor(device, cmd_buffer, pDst, buffer_list,
+                               write_image_descriptor(device, cmd_buffer, 64, pDst, buffer_list,
                                                       templ->entry[i].descriptor_type,
                                                       (struct VkDescriptorImageInfo *) pSrc);
                                break;
                        case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
-                               write_combined_image_sampler_descriptor(device, cmd_buffer, pDst, buffer_list,
-                                                                       templ->entry[i].descriptor_type,
+                               write_combined_image_sampler_descriptor(device, cmd_buffer, templ->entry[i].sampler_offset,
+                                                                       pDst, buffer_list, templ->entry[i].descriptor_type,
                                                                        (struct VkDescriptorImageInfo *) pSrc,
                                                                        templ->entry[i].has_sampler);
-                               if (templ->entry[i].immutable_samplers)
-                                       memcpy(pDst + 16, templ->entry[i].immutable_samplers + 4 * j, 16);
+                               if (templ->entry[i].immutable_samplers) {
+                                       memcpy((char*)pDst + templ->entry[i].sampler_offset, templ->entry[i].immutable_samplers + 4 * j, 16);
+                               }
                                break;
                        case VK_DESCRIPTOR_TYPE_SAMPLER:
                                if (templ->entry[i].has_sampler)