v3dv: initial support for input attachments

We are treating them as a special case of texture, so the commit is
mostly about integrating them with the existing
SAMPLER/SAMPLER_IMAGE/COMBINED_IMAGE_SAMPLER infrastructure.

This commit doesn't use in any special way the render pass
information, including the dependencies, so it is possible that we
would need to do something else. But this commit gets several CTS
tests, and two Sascha Willem Vulkan demos, so let's start with this
commit and handle any other use case for following commits.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6766>
diff --git a/src/broadcom/vulkan/v3dv_cmd_buffer.c b/src/broadcom/vulkan/v3dv_cmd_buffer.c
index 0a9d790..2ab2b4f 100644
--- a/src/broadcom/vulkan/v3dv_cmd_buffer.c
+++ b/src/broadcom/vulkan/v3dv_cmd_buffer.c
@@ -2068,8 +2068,7 @@
                            subpass->color_count,
                            internal_bpp);
 
-      /* FIXME: we don't support input/resolve attachments yet */
-      assert(subpass->input_count == 0);
+      /* FIXME: we don't suppport resolve attachments yet */
       assert(subpass->resolve_attachments == NULL);
    }
 
diff --git a/src/broadcom/vulkan/v3dv_descriptor_set.c b/src/broadcom/vulkan/v3dv_descriptor_set.c
index d9a0002..9bc41d9 100644
--- a/src/broadcom/vulkan/v3dv_descriptor_set.c
+++ b/src/broadcom/vulkan/v3dv_descriptor_set.c
@@ -38,6 +38,7 @@
    case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
       return sizeof(struct v3dv_combined_image_sampler_descriptor);
    case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
+   case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
       return sizeof(struct v3dv_sampled_image_descriptor);
    default:
       return 0;
@@ -248,7 +249,8 @@
                                             index, &type);
 
    assert(type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE ||
-          type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
+          type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
+          type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
 
    if (type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) {
       reloc.offset += offsetof(struct v3dv_combined_image_sampler_descriptor,
@@ -272,7 +274,9 @@
 
    assert(image_descriptor);
    assert(image_descriptor->type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE ||
-          image_descriptor->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
+          image_descriptor->type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
+          image_descriptor->type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT);
+
    assert(image_descriptor->image_view);
    assert(image_descriptor->image_view->image);
 
@@ -376,6 +380,7 @@
       case VK_DESCRIPTOR_TYPE_SAMPLER:
       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
+      case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
          break;
       default:
          unreachable("Unimplemented descriptor type");
@@ -620,6 +625,7 @@
       case VK_DESCRIPTOR_TYPE_SAMPLER:
       case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
       case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
+      case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
          /* Nothing here, just to keep the descriptor type filtering below */
          break;
       default:
@@ -950,6 +956,7 @@
 
             break;
          }
+         case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
          case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
             const VkDescriptorImageInfo *image_info = writeset->pImageInfo + j;
             V3DV_FROM_HANDLE(v3dv_image_view, iview, image_info->imageView);
diff --git a/src/broadcom/vulkan/v3dv_pass.c b/src/broadcom/vulkan/v3dv_pass.c
index 4cf41be..b90425e 100644
--- a/src/broadcom/vulkan/v3dv_pass.c
+++ b/src/broadcom/vulkan/v3dv_pass.c
@@ -63,7 +63,17 @@
             pass->attachments[ds_attachment_idx].last_subpass = i;
       }
 
-      /* FIXME: input/resolve attachments */
+      for (uint32_t j = 0; j < subpass->input_count; j++) {
+         uint32_t input_attachment_idx = subpass->input_attachments[j].attachment;
+         if (input_attachment_idx == VK_ATTACHMENT_UNUSED)
+            continue;
+         if (i < pass->attachments[input_attachment_idx].first_subpass)
+            pass->attachments[input_attachment_idx].first_subpass = i;
+         if (i > pass->attachments[input_attachment_idx].last_subpass)
+            pass->attachments[input_attachment_idx].last_subpass = i;
+      }
+      /* FIXME: we don't support resolve attachments yet */
+      assert(subpass->resolve_attachments == NULL);
    }
 }
 
diff --git a/src/broadcom/vulkan/v3dv_pipeline.c b/src/broadcom/vulkan/v3dv_pipeline.c
index a71a9a0..5581c18 100644
--- a/src/broadcom/vulkan/v3dv_pipeline.c
+++ b/src/broadcom/vulkan/v3dv_pipeline.c
@@ -668,12 +668,19 @@
    struct v3dv_descriptor_set_binding_layout *binding_layout =
       &set_layout->binding[binding];
 
+   /* For input attachments, the shader includes the attachment_idx. As we are
+    * treating them as a texture, we only want the base_index
+    */
+   uint32_t array_index = binding_layout->type != VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ?
+      deref->var->data.index + base_index :
+      base_index;
+
    int desc_index =
       descriptor_map_add(is_sampler ?
                          &pipeline->sampler_map : &pipeline->texture_map,
                          deref->var->data.descriptor_set,
                          deref->var->data.binding,
-                         deref->var->data.index + base_index,
+                         array_index,
                          binding_layout->array_size);
    if (is_sampler)
       instr->sampler_index = desc_index;