turnip: enable 420_UNORM formats
[mesa.git] / src / freedreno / vulkan / tu_descriptor_set.c
index de1683c6bd7eeecdb8c85c473d5f9416a1186333..ec2a940e858fc7c01fe6b7e1acfc5858f1544a13 100644 (file)
@@ -84,6 +84,7 @@ descriptor_size(VkDescriptorType type)
    case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
       /* These are remapped to the special driver-managed descriptor set,
        * hence they don't take up any space in the original descriptor set:
+       * Input attachment doesn't use descriptor sets at all
        */
       return 0;
    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
@@ -119,20 +120,35 @@ tu_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) {
          immutable_sampler_count += pCreateInfo->pBindings[j].descriptorCount;
+
+         bool has_ycbcr_sampler = false;
+         for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
+            if (tu_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 tu_descriptor_set_layout) +
-      (max_binding + 1) * sizeof(set_layout->binding[0]);
-   uint32_t size = samplers_offset + immutable_sampler_count * A6XX_TEX_SAMP_DWORDS * 4;
+   uint32_t samplers_offset =
+         offsetof(struct tu_descriptor_set_layout, binding[max_binding + 1]);
+
+   /* note: only need to store TEX_SAMP_DWORDS for immutable samples,
+    * but using struct tu_sampler makes things simpler */
+   uint32_t size = samplers_offset +
+      immutable_sampler_count * sizeof(struct tu_sampler) +
+      ycbcr_sampler_count * sizeof(struct tu_sampler_ycbcr_conversion);
 
-   set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8,
+   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);
@@ -141,6 +157,8 @@ tu_CreateDescriptorSetLayout(
 
    /* We just allocate all the immutable samplers at the end of the struct */
    struct tu_sampler *samplers = (void*) &set_layout->binding[max_binding + 1];
+   struct tu_sampler_ycbcr_conversion *ycbcr_samplers =
+      (void*) &samplers[immutable_sampler_count];
 
    VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(
       pCreateInfo->pBindings, pCreateInfo->bindingCount);
@@ -155,11 +173,7 @@ tu_CreateDescriptorSetLayout(
    set_layout->size = 0;
    set_layout->dynamic_ubo = 0;
 
-   memset(set_layout->binding, 0,
-          size - sizeof(struct tu_descriptor_set_layout));
-
    uint32_t dynamic_offset_count = 0;
-   uint32_t input_attachment_count = 0;
    uint32_t buffer_count = 0;
 
    for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
@@ -171,8 +185,8 @@ tu_CreateDescriptorSetLayout(
       set_layout->binding[b].offset = set_layout->size;
       set_layout->binding[b].buffer_offset = buffer_count;
       set_layout->binding[b].dynamic_offset_offset = dynamic_offset_count;
-      set_layout->binding[b].input_attachment_offset = input_attachment_count;
       set_layout->binding[b].size = descriptor_size(binding->descriptorType);
+      set_layout->binding[b].shader_stages = binding->stageFlags;
 
       if (variable_flags && binding->binding < variable_flags->bindingCount &&
           (variable_flags->pBindingFlags[binding->binding] &
@@ -195,6 +209,27 @@ tu_CreateDescriptorSetLayout(
 
          samplers += binding->descriptorCount;
          samplers_offset += sizeof(struct tu_sampler) * binding->descriptorCount;
+
+         bool has_ycbcr_sampler = false;
+         for (unsigned i = 0; i < pCreateInfo->pBindings[j].descriptorCount; ++i) {
+            if (tu_sampler_from_handle(binding->pImmutableSamplers[i])->ycbcr_sampler)
+               has_ycbcr_sampler = true;
+         }
+
+         if (has_ycbcr_sampler) {
+            set_layout->binding[b].ycbcr_samplers_offset =
+               (const char*)ycbcr_samplers - (const char*)set_layout;
+            for (uint32_t i = 0; i < binding->descriptorCount; i++) {
+               struct tu_sampler *sampler = tu_sampler_from_handle(binding->pImmutableSamplers[i]);
+               if (sampler->ycbcr_sampler)
+                  ycbcr_samplers[i] = *sampler->ycbcr_sampler;
+               else
+                  ycbcr_samplers[i].ycbcr_model = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
+            }
+            ycbcr_samplers += binding->descriptorCount;
+         } else {
+            set_layout->binding[b].ycbcr_samplers_offset = 0;
+         }
       }
 
       set_layout->size +=
@@ -212,15 +247,13 @@ tu_CreateDescriptorSetLayout(
 
          dynamic_offset_count += binding->descriptorCount;
       }
-      if (binding->descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
-         input_attachment_count += binding->descriptorCount;
+
       set_layout->shader_stages |= binding->stageFlags;
    }
 
    free(bindings);
 
    set_layout->dynamic_offset_count = dynamic_offset_count;
-   set_layout->input_attachment_count = input_attachment_count;
    set_layout->buffer_count = buffer_count;
 
    *pSetLayout = tu_descriptor_set_layout_to_handle(set_layout);
@@ -275,10 +308,10 @@ tu_GetDescriptorSetLayoutSupport(
       uint64_t descriptor_sz = descriptor_size(binding->descriptorType);
       uint64_t descriptor_alignment = 8;
 
-      if (size && !align_u64(size, descriptor_alignment)) {
+      if (size && !ALIGN_POT(size, descriptor_alignment)) {
          supported = false;
       }
-      size = align_u64(size, descriptor_alignment);
+      size = ALIGN_POT(size, descriptor_alignment);
 
       uint64_t max_count = UINT64_MAX;
       if (descriptor_sz)
@@ -326,10 +359,9 @@ tu_CreatePipelineLayout(VkDevice _device,
       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
    layout->num_sets = pCreateInfo->setLayoutCount;
-   layout->input_attachment_count = 0;
    layout->dynamic_offset_count = 0;
 
-   unsigned dynamic_offset_count = 0, input_attachment_count = 0;
+   unsigned dynamic_offset_count = 0;
 
    _mesa_sha1_init(&ctx);
    for (uint32_t set = 0; set < pCreateInfo->setLayoutCount; set++) {
@@ -337,9 +369,7 @@ tu_CreatePipelineLayout(VkDevice _device,
                      pCreateInfo->pSetLayouts[set]);
       layout->set[set].layout = set_layout;
       layout->set[set].dynamic_offset_start = dynamic_offset_count;
-      layout->set[set].input_attachment_start = input_attachment_count;
       dynamic_offset_count += set_layout->dynamic_offset_count;
-      input_attachment_count += set_layout->input_attachment_count;
 
       for (uint32_t b = 0; b < set_layout->binding_count; b++) {
          if (set_layout->binding[b].immutable_samplers_offset)
@@ -354,7 +384,6 @@ tu_CreatePipelineLayout(VkDevice _device,
    }
 
    layout->dynamic_offset_count = dynamic_offset_count;
-   layout->input_attachment_count = input_attachment_count;
    layout->push_constant_size = 0;
 
    for (unsigned i = 0; i < pCreateInfo->pushConstantRangeCount; ++i) {
@@ -407,8 +436,7 @@ tu_descriptor_set_create(struct tu_device *device,
    unsigned dynamic_offset = sizeof(struct tu_descriptor_set) +
       sizeof(struct tu_bo *) * buffer_count;
    unsigned mem_size = dynamic_offset +
-      A6XX_TEX_CONST_DWORDS * 4 * (layout->dynamic_offset_count +
-                                   layout->input_attachment_count);;
+      A6XX_TEX_CONST_DWORDS * 4 * layout->dynamic_offset_count;
 
    if (pool->host_memory_base) {
       if (pool->host_memory_end - pool->host_memory_ptr < mem_size)
@@ -426,7 +454,7 @@ tu_descriptor_set_create(struct tu_device *device,
 
    memset(set, 0, mem_size);
 
-   if (layout->dynamic_offset_count + layout->input_attachment_count > 0) {
+   if (layout->dynamic_offset_count) {
       set->dynamic_descriptors = (uint32_t *)((uint8_t*)set + dynamic_offset);
    }
 
@@ -488,6 +516,26 @@ tu_descriptor_set_create(struct tu_device *device,
          return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
    }
 
+   if (layout->has_immutable_samplers) {
+      for (unsigned i = 0; i < layout->binding_count; ++i) {
+         if (!layout->binding[i].immutable_samplers_offset)
+            continue;
+
+         unsigned offset = layout->binding[i].offset / 4;
+         if (layout->binding[i].type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
+            offset += A6XX_TEX_CONST_DWORDS;
+
+         const struct tu_sampler *samplers =
+            (const struct tu_sampler *)((const char *)layout +
+                               layout->binding[i].immutable_samplers_offset);
+         for (unsigned j = 0; j < layout->binding[i].array_size; ++j) {
+            memcpy(set->mapped_ptr + offset, samplers[j].descriptor,
+                   sizeof(samplers[j].descriptor));
+            offset += layout->binding[i].size / 4;
+         }
+      }
+   }
+
    *out_set = set;
    return VK_SUCCESS;
 }
@@ -532,7 +580,6 @@ tu_CreateDescriptorPool(VkDevice _device,
       switch(pCreateInfo->pPoolSizes[i].type) {
       case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
       case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
-      case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
          dynamic_count += pCreateInfo->pPoolSizes[i].descriptorCount;
       default:
          break;
@@ -845,7 +892,7 @@ tu_update_descriptor_sets(struct tu_device *device,
          case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
             unsigned idx = writeset->dstArrayElement + j;
-            idx += set->layout->input_attachment_count + binding_layout->dynamic_offset_offset;
+            idx += binding_layout->dynamic_offset_offset;
             write_ubo_descriptor(device, cmd_buffer,
                                  set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
                                  buffer_list, writeset->pBufferInfo + j);
@@ -858,7 +905,7 @@ tu_update_descriptor_sets(struct tu_device *device,
          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
             assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
             unsigned idx = writeset->dstArrayElement + j;
-            idx += set->layout->input_attachment_count + binding_layout->dynamic_offset_offset;
+            idx += binding_layout->dynamic_offset_offset;
             write_buffer_descriptor(device, cmd_buffer,
                                     set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
                                     buffer_list, writeset->pBufferInfo + j);
@@ -879,15 +926,6 @@ tu_update_descriptor_sets(struct tu_device *device,
                                    writeset->descriptorType,
                                    writeset->pImageInfo + j);
             break;
-         case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
-            unsigned idx = writeset->dstArrayElement + j;
-            idx += binding_layout->input_attachment_offset;
-            write_image_descriptor(device, cmd_buffer,
-                                    set->dynamic_descriptors + A6XX_TEX_CONST_DWORDS * idx,
-                                    buffer_list, writeset->descriptorType,
-                                    writeset->pImageInfo + j);
-            break;
-         }
          case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
             write_combined_image_sampler_descriptor(device, cmd_buffer,
                                                     A6XX_TEX_CONST_DWORDS * 4,
@@ -899,6 +937,9 @@ tu_update_descriptor_sets(struct tu_device *device,
          case VK_DESCRIPTOR_TYPE_SAMPLER:
             write_sampler_descriptor(device, ptr, writeset->pImageInfo + j);
             break;
+         case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
+            /* nothing in descriptor set - framebuffer state is used instead */
+            break;
          default:
             unreachable("unimplemented descriptor type");
             break;
@@ -941,8 +982,6 @@ tu_update_descriptor_sets(struct tu_device *device,
          case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
             unsigned src_idx = copyset->srcArrayElement + j;
             unsigned dst_idx = copyset->dstArrayElement + j;
-            src_idx += src_set->layout->input_attachment_count;
-            dst_idx += dst_set->layout->input_attachment_count;
             src_idx += src_binding_layout->dynamic_offset_offset;
             dst_idx += dst_binding_layout->dynamic_offset_offset;
 
@@ -952,18 +991,6 @@ tu_update_descriptor_sets(struct tu_device *device,
             memcpy(dst_dynamic, src_dynamic, A6XX_TEX_CONST_DWORDS * 4);
             break;
          }
-         case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
-            unsigned src_idx = copyset->srcArrayElement + j;
-            unsigned dst_idx = copyset->dstArrayElement + j;
-            src_idx += src_binding_layout->input_attachment_offset;
-            dst_idx += dst_binding_layout->input_attachment_offset;
-
-            uint32_t *src_dynamic, *dst_dynamic;
-            src_dynamic = src_set->dynamic_descriptors + src_idx * A6XX_TEX_CONST_DWORDS;
-            dst_dynamic = dst_set->dynamic_descriptors + dst_idx * A6XX_TEX_CONST_DWORDS;
-            memcpy(dst_dynamic, src_dynamic, A6XX_TEX_CONST_DWORDS * 4);
-            break;
-         }
          default:
             memcpy(dst_ptr, src_ptr, src_binding_layout->size);
          }
@@ -1014,11 +1041,58 @@ tu_CreateDescriptorUpdateTemplate(
    if (!templ)
       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+   templ->entry_count = entry_count;
+
+   if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR) {
+      TU_FROM_HANDLE(tu_pipeline_layout, pipeline_layout, pCreateInfo->pipelineLayout);
+
+      /* descriptorSetLayout should be ignored for push descriptors
+       * and instead it refers to pipelineLayout and set.
+       */
+      assert(pCreateInfo->set < MAX_SETS);
+      set_layout = pipeline_layout->set[pCreateInfo->set].layout;
+   }
+
+   for (uint32_t i = 0; i < entry_count; i++) {
+      const VkDescriptorUpdateTemplateEntry *entry = &pCreateInfo->pDescriptorUpdateEntries[i];
+
+      const struct tu_descriptor_set_binding_layout *binding_layout =
+         set_layout->binding + entry->dstBinding;
+      const uint32_t buffer_offset = binding_layout->buffer_offset +
+         entry->dstArrayElement;
+      uint32_t dst_offset, dst_stride;
+
+      /* dst_offset is an offset into dynamic_descriptors when the descriptor 
+       * is dynamic, and an offset into mapped_ptr otherwise.
+       */
+      switch (entry->descriptorType) {
+      case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
+      case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
+         dst_offset = (binding_layout->dynamic_offset_offset +
+            entry->dstArrayElement) * A6XX_TEX_CONST_DWORDS;
+         dst_stride = A6XX_TEX_CONST_DWORDS;
+         break;
+      default:
+         dst_offset = binding_layout->offset / 4;
+         dst_offset += (binding_layout->size * entry->dstArrayElement) / 4;
+         dst_stride = binding_layout->size / 4;
+      }
+
+      templ->entry[i] = (struct tu_descriptor_update_template_entry) {
+         .descriptor_type = entry->descriptorType,
+         .descriptor_count = entry->descriptorCount,
+         .src_offset = entry->offset,
+         .src_stride = entry->stride,
+         .dst_offset = dst_offset,
+         .dst_stride = dst_stride,
+         .buffer_offset = buffer_offset,
+         .has_sampler = !binding_layout->immutable_samplers_offset,
+      };
+   }
+
    *pDescriptorUpdateTemplate =
       tu_descriptor_update_template_to_handle(templ);
 
-   tu_use_args(set_layout);
-   tu_stub();
    return VK_SUCCESS;
 }
 
@@ -1048,7 +1122,71 @@ tu_update_descriptor_set_with_template(
 {
    TU_FROM_HANDLE(tu_descriptor_update_template, templ,
                   descriptorUpdateTemplate);
-   tu_use_args(templ);
+
+   for (uint32_t i = 0; i < templ->entry_count; i++) {
+      uint32_t *ptr = set->mapped_ptr;
+      const void *src = ((const char *) pData) + templ->entry[i].src_offset;
+      struct tu_bo **buffer_list = set->buffers;
+
+      ptr += templ->entry[i].dst_offset;
+      buffer_list += templ->entry[i].buffer_offset;
+      unsigned dst_offset = templ->entry[i].dst_offset;
+      for (unsigned j = 0; j < templ->entry[i].descriptor_count; ++j) {
+         switch(templ->entry[i].descriptor_type) {
+         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
+            assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
+            write_ubo_descriptor(device, cmd_buffer,
+                                 set->dynamic_descriptors + dst_offset,
+                                 buffer_list, src);
+            break;
+         }
+         case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
+            write_ubo_descriptor(device, cmd_buffer, ptr, buffer_list, src);
+            break;
+         case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
+            assert(!(set->layout->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR));
+            write_buffer_descriptor(device, cmd_buffer,
+                                    set->dynamic_descriptors + dst_offset,
+                                    buffer_list, src);
+            break;
+         }
+         case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
+            write_buffer_descriptor(device, cmd_buffer, ptr, buffer_list, src);
+            break;
+         case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
+         case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
+            write_texel_buffer_descriptor(device, cmd_buffer, ptr,
+                                          buffer_list, *(VkBufferView *) src);
+            break;
+         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,
+                                   templ->entry[i].descriptor_type,
+                                   src);
+            break;
+         }
+         case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
+            write_combined_image_sampler_descriptor(device, cmd_buffer,
+                                                    A6XX_TEX_CONST_DWORDS * 4,
+                                                    ptr, buffer_list,
+                                                    templ->entry[i].descriptor_type,
+                                                    src,
+                                                    templ->entry[i].has_sampler);
+            break;
+         case VK_DESCRIPTOR_TYPE_SAMPLER:
+            write_sampler_descriptor(device, ptr, src);
+            break;
+         default:
+            unreachable("unimplemented descriptor type");
+            break;
+         }
+         src = (char *) src + templ->entry[i].src_stride;
+         ptr += templ->entry[i].dst_stride;
+         dst_offset += templ->entry[i].dst_stride;
+         ++buffer_list;
+      }
+   }
 }
 
 void
@@ -1067,19 +1205,39 @@ tu_UpdateDescriptorSetWithTemplate(
 
 VkResult
 tu_CreateSamplerYcbcrConversion(
-   VkDevice device,
+   VkDevice _device,
    const VkSamplerYcbcrConversionCreateInfo *pCreateInfo,
    const VkAllocationCallbacks *pAllocator,
    VkSamplerYcbcrConversion *pYcbcrConversion)
 {
-   *pYcbcrConversion = VK_NULL_HANDLE;
+   TU_FROM_HANDLE(tu_device, device, _device);
+   struct tu_sampler_ycbcr_conversion *conversion;
+
+   conversion = vk_alloc2(&device->alloc, pAllocator, sizeof(*conversion), 8,
+                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+   if (!conversion)
+      return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
+
+   conversion->format = pCreateInfo->format;
+   conversion->ycbcr_model = pCreateInfo->ycbcrModel;
+   conversion->ycbcr_range = pCreateInfo->ycbcrRange;
+   conversion->components = pCreateInfo->components;
+   conversion->chroma_offsets[0] = pCreateInfo->xChromaOffset;
+   conversion->chroma_offsets[1] = pCreateInfo->yChromaOffset;
+   conversion->chroma_filter = pCreateInfo->chromaFilter;
+
+   *pYcbcrConversion = tu_sampler_ycbcr_conversion_to_handle(conversion);
    return VK_SUCCESS;
 }
 
 void
-tu_DestroySamplerYcbcrConversion(VkDevice device,
+tu_DestroySamplerYcbcrConversion(VkDevice _device,
                                  VkSamplerYcbcrConversion ycbcrConversion,
                                  const VkAllocationCallbacks *pAllocator)
 {
-   /* Do nothing. */
+   TU_FROM_HANDLE(tu_device, device, _device);
+   TU_FROM_HANDLE(tu_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);
+
+   if (ycbcr_conversion)
+      vk_free2(&device->alloc, pAllocator, ycbcr_conversion);
 }