anv: Add initial support for cube maps
[mesa.git] / src / vulkan / anv_meta.c
index 419667a9f306d1f3477a867c83d13a8a5be0d108..63976cdfe4238aa4b535f9294db19c96636e0e8c 100644 (file)
@@ -145,19 +145,15 @@ anv_meta_restore(const struct anv_meta_saved_state *state,
    cmd_buffer->state.dirty |= state->dynamic_mask;
 }
 
-static VkImageViewType
-meta_blit_get_src_image_view_type(const struct anv_image *src_image)
+VkImageViewType
+anv_meta_get_view_type(const struct anv_image *image)
 {
-   switch (src_image->type) {
-   case VK_IMAGE_TYPE_1D:
-      return VK_IMAGE_VIEW_TYPE_1D;
-   case VK_IMAGE_TYPE_2D:
-      return VK_IMAGE_VIEW_TYPE_2D;
-   case VK_IMAGE_TYPE_3D:
-      return VK_IMAGE_VIEW_TYPE_3D;
+   switch (image->type) {
+   case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
+   case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
+   case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
    default:
-      assert(!"bad VkImageType");
-      return 0;
+      unreachable("bad VkImageViewType");
    }
 }
 
@@ -199,67 +195,43 @@ anv_device_init_meta_blit_state(struct anv_device *device)
          .subpassCount = 1,
          .pSubpasses = &(VkSubpassDescription) {
             .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
-            .inputCount = 0,
-            .colorCount = 1,
+            .inputAttachmentCount = 0,
+            .colorAttachmentCount = 1,
             .pColorAttachments = &(VkAttachmentReference) {
                .attachment = 0,
                .layout = VK_IMAGE_LAYOUT_GENERAL,
             },
             .pResolveAttachments = NULL,
-            .depthStencilAttachment = (VkAttachmentReference) {
+            .pDepthStencilAttachment = &(VkAttachmentReference) {
                .attachment = VK_ATTACHMENT_UNUSED,
                .layout = VK_IMAGE_LAYOUT_GENERAL,
             },
-            .preserveCount = 1,
+            .preserveAttachmentCount = 1,
             .pPreserveAttachments = &(VkAttachmentReference) {
                .attachment = 0,
                .layout = VK_IMAGE_LAYOUT_GENERAL,
             },
          },
          .dependencyCount = 0,
-      }, &device->meta_state.blit.render_pass);
+      }, NULL, &device->meta_state.blit.render_pass);
 
    /* We don't use a vertex shader for clearing, but instead build and pass
     * the VUEs directly to the rasterization backend.  However, we do need
     * to provide GLSL source for the vertex shader so that the compiler
     * does not dead-code our inputs.
     */
-   struct anv_shader_module vsm = {
+   struct anv_shader_module vs = {
       .nir = build_nir_vertex_shader(false),
    };
 
-   struct anv_shader_module fsm_2d = {
+   struct anv_shader_module fs_2d = {
       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D),
    };
 
-   struct anv_shader_module fsm_3d = {
+   struct anv_shader_module fs_3d = {
       .nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D),
    };
 
-   VkShader vs;
-   anv_CreateShader(anv_device_to_handle(device),
-      &(VkShaderCreateInfo) {
-         .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
-         .module = anv_shader_module_to_handle(&vsm),
-         .pName = "main",
-      }, &vs);
-
-   VkShader fs_2d;
-   anv_CreateShader(anv_device_to_handle(device),
-      &(VkShaderCreateInfo) {
-         .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
-         .module = anv_shader_module_to_handle(&fsm_2d),
-         .pName = "main",
-      }, &fs_2d);
-
-   VkShader fs_3d;
-   anv_CreateShader(anv_device_to_handle(device),
-      &(VkShaderCreateInfo) {
-         .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
-         .module = anv_shader_module_to_handle(&fsm_3d),
-         .pName = "main",
-      }, &fs_3d);
-
    VkPipelineVertexInputStateCreateInfo vi_create_info = {
       .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
       .vertexBindingDescriptionCount = 2,
@@ -315,7 +287,7 @@ anv_device_init_meta_blit_state(struct anv_device *device)
       }
    };
    anv_CreateDescriptorSetLayout(anv_device_to_handle(device), &ds_layout_info,
-                                 &device->meta_state.blit.ds_layout);
+                                 NULL, &device->meta_state.blit.ds_layout);
 
    anv_CreatePipelineLayout(anv_device_to_handle(device),
       &(VkPipelineLayoutCreateInfo) {
@@ -323,18 +295,20 @@ anv_device_init_meta_blit_state(struct anv_device *device)
          .setLayoutCount = 1,
          .pSetLayouts = &device->meta_state.blit.ds_layout,
       },
-      &device->meta_state.blit.pipeline_layout);
+      NULL, &device->meta_state.blit.pipeline_layout);
 
    VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
       {
          .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-         .stage = VK_SHADER_STAGE_VERTEX,
-         .shader = vs,
+         .stage = VK_SHADER_STAGE_VERTEX_BIT,
+         .module = anv_shader_module_to_handle(&vs),
+         .pName = "main",
          .pSpecializationInfo = NULL
       }, {
          .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-         .stage = VK_SHADER_STAGE_FRAGMENT,
-         .shader = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
+         .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
+         .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
+         .pName = "main",
          .pSpecializationInfo = NULL
       },
    };
@@ -356,7 +330,6 @@ anv_device_init_meta_blit_state(struct anv_device *device)
       },
       .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
          .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
-         .depthClipEnable = true,
          .rasterizerDiscardEnable = false,
          .polygonMode = VK_POLYGON_MODE_FILL,
          .cullMode = VK_CULL_MODE_NONE,
@@ -408,22 +381,19 @@ anv_device_init_meta_blit_state(struct anv_device *device)
       .use_rectlist = true
    };
 
-   pipeline_shader_stages[1].shader = fs_2d;
+   pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_2d);
    anv_graphics_pipeline_create(anv_device_to_handle(device),
       &vk_pipeline_info, &anv_pipeline_info,
-      &device->meta_state.blit.pipeline_2d_src);
+      NULL, &device->meta_state.blit.pipeline_2d_src);
 
-   pipeline_shader_stages[1].shader = fs_3d;
+   pipeline_shader_stages[1].module = anv_shader_module_to_handle(&fs_3d);
    anv_graphics_pipeline_create(anv_device_to_handle(device),
       &vk_pipeline_info, &anv_pipeline_info,
-      &device->meta_state.blit.pipeline_3d_src);
-
-   anv_DestroyShader(anv_device_to_handle(device), vs);
-   anv_DestroyShader(anv_device_to_handle(device), fs_2d);
-   anv_DestroyShader(anv_device_to_handle(device), fs_3d);
-   ralloc_free(vsm.nir);
-   ralloc_free(fsm_2d.nir);
-   ralloc_free(fsm_3d.nir);
+      NULL, &device->meta_state.blit.pipeline_3d_src);
+
+   ralloc_free(vs.nir);
+   ralloc_free(fs_2d.nir);
+   ralloc_free(fs_3d.nir);
 }
 
 static void
@@ -504,6 +474,8 @@ meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
       },
    };
 
+   anv_state_clflush(vb_state);
+
    struct anv_buffer vertex_buffer = {
       .device = device,
       .size = vb_size,
@@ -527,7 +499,7 @@ meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
          .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
          .magFilter = blit_filter,
          .minFilter = blit_filter,
-      }, &sampler);
+      }, &cmd_buffer->pool->alloc, &sampler);
 
    VkDescriptorSet set;
    anv_AllocateDescriptorSets(anv_device_to_handle(device),
@@ -568,7 +540,7 @@ meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
          .width = dest_iview->extent.width,
          .height = dest_iview->extent.height,
          .layers = 1
-      }, &fb);
+      }, &cmd_buffer->pool->alloc, &fb);
 
    ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
       &(VkRenderPassBeginInfo) {
@@ -628,8 +600,10 @@ meta_emit_blit(struct anv_cmd_buffer *cmd_buffer,
     * descriptor sets, etc. has been used.  We are free to delete it.
     */
    anv_descriptor_set_destroy(device, anv_descriptor_set_from_handle(set));
-   anv_DestroySampler(anv_device_to_handle(device), sampler);
-   anv_DestroyFramebuffer(anv_device_to_handle(device), fb);
+   anv_DestroySampler(anv_device_to_handle(device), sampler,
+                      &cmd_buffer->pool->alloc);
+   anv_DestroyFramebuffer(anv_device_to_handle(device), fb,
+                          &cmd_buffer->pool->alloc);
 }
 
 static void
@@ -683,11 +657,13 @@ do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
 
    VkImage src_image;
    image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
-   anv_CreateImage(vk_device, &image_info, &src_image);
+   anv_CreateImage(vk_device, &image_info,
+                   &cmd_buffer->pool->alloc, &src_image);
 
    VkImage dest_image;
    image_info.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
-   anv_CreateImage(vk_device, &image_info, &dest_image);
+   anv_CreateImage(vk_device, &image_info,
+                   &cmd_buffer->pool->alloc, &dest_image);
 
    /* We could use a vk call to bind memory, but that would require
     * creating a dummy memory object etc. so there's really no point.
@@ -742,8 +718,8 @@ do_buffer_copy(struct anv_cmd_buffer *cmd_buffer,
                   (VkExtent3D) { width, height, 1 },
                   VK_FILTER_NEAREST);
 
-   anv_DestroyImage(vk_device, src_image);
-   anv_DestroyImage(vk_device, dest_image);
+   anv_DestroyImage(vk_device, src_image, &cmd_buffer->pool->alloc);
+   anv_DestroyImage(vk_device, dest_image, &cmd_buffer->pool->alloc);
 }
 
 void anv_CmdCopyBuffer(
@@ -837,9 +813,6 @@ void anv_CmdCopyImage(
    ANV_FROM_HANDLE(anv_image, src_image, srcImage);
    ANV_FROM_HANDLE(anv_image, dest_image, destImage);
 
-   const VkImageViewType src_iview_type =
-      meta_blit_get_src_image_view_type(src_image);
-
    struct anv_meta_saved_state saved_state;
 
    meta_prepare_blit(cmd_buffer, &saved_state);
@@ -850,7 +823,7 @@ void anv_CmdCopyImage(
          &(VkImageViewCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
             .image = srcImage,
-            .viewType = src_iview_type,
+            .viewType = anv_meta_get_view_type(src_image),
             .format = src_image->format->vk_format,
             .subresourceRange = {
                .aspectMask = pRegions[r].srcSubresource.aspectMask,
@@ -894,7 +867,7 @@ void anv_CmdCopyImage(
             &(VkImageViewCreateInfo) {
                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
                .image = destImage,
-               .viewType = VK_IMAGE_VIEW_TYPE_2D,
+               .viewType = anv_meta_get_view_type(dest_image),
                .format = dest_image->format->vk_format,
                .subresourceRange = {
                   .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
@@ -935,9 +908,6 @@ void anv_CmdBlitImage(
    ANV_FROM_HANDLE(anv_image, src_image, srcImage);
    ANV_FROM_HANDLE(anv_image, dest_image, destImage);
 
-   const VkImageViewType src_iview_type =
-      meta_blit_get_src_image_view_type(src_image);
-
    struct anv_meta_saved_state saved_state;
 
    anv_finishme("respect VkFilter");
@@ -950,7 +920,7 @@ void anv_CmdBlitImage(
          &(VkImageViewCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
             .image = srcImage,
-            .viewType = src_iview_type,
+            .viewType = anv_meta_get_view_type(src_image),
             .format = src_image->format->vk_format,
             .subresourceRange = {
                .aspectMask = pRegions[r].srcSubresource.aspectMask,
@@ -984,7 +954,7 @@ void anv_CmdBlitImage(
          &(VkImageViewCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
             .image = destImage,
-            .viewType = VK_IMAGE_VIEW_TYPE_2D,
+            .viewType = anv_meta_get_view_type(dest_image),
             .format = dest_image->format->vk_format,
             .subresourceRange = {
                .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
@@ -1013,6 +983,7 @@ static struct anv_image *
 make_image_for_buffer(VkDevice vk_device, VkBuffer vk_buffer, VkFormat format,
                       VkImageUsageFlags usage,
                       VkImageType image_type,
+                      const VkAllocationCallbacks *alloc,
                       const VkBufferImageCopy *copy)
 {
    ANV_FROM_HANDLE(anv_buffer, buffer, vk_buffer);
@@ -1037,7 +1008,7 @@ make_image_for_buffer(VkDevice vk_device, VkBuffer vk_buffer, VkFormat format,
          .tiling = VK_IMAGE_TILING_LINEAR,
          .usage = usage,
          .flags = 0,
-      }, &vk_image);
+      }, alloc, &vk_image);
    assert(result == VK_SUCCESS);
 
    ANV_FROM_HANDLE(anv_image, image, vk_image);
@@ -1079,7 +1050,8 @@ void anv_CmdCopyBufferToImage(
       struct anv_image *src_image =
          make_image_for_buffer(vk_device, srcBuffer, proxy_format,
                                VK_IMAGE_USAGE_SAMPLED_BIT,
-                               dest_image->type, &pRegions[r]);
+                               dest_image->type, &cmd_buffer->pool->alloc,
+                               &pRegions[r]);
 
       const uint32_t dest_base_array_slice =
          meta_blit_get_dest_view_base_array_slice(dest_image,
@@ -1118,7 +1090,7 @@ void anv_CmdCopyBufferToImage(
             &(VkImageViewCreateInfo) {
                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
                .image = anv_image_to_handle(dest_image),
-               .viewType = VK_IMAGE_VIEW_TYPE_2D,
+               .viewType = anv_meta_get_view_type(dest_image),
                .format = proxy_format,
                .subresourceRange = {
                   .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
@@ -1159,7 +1131,8 @@ void anv_CmdCopyBufferToImage(
                               src_image->extent.height * 4;
       }
 
-      anv_DestroyImage(vk_device, anv_image_to_handle(src_image));
+      anv_DestroyImage(vk_device, anv_image_to_handle(src_image),
+                       &cmd_buffer->pool->alloc);
    }
 
    meta_finish_blit(cmd_buffer, &saved_state);
@@ -1178,9 +1151,6 @@ void anv_CmdCopyImageToBuffer(
    VkDevice vk_device = anv_device_to_handle(cmd_buffer->device);
    struct anv_meta_saved_state saved_state;
 
-   const VkImageViewType src_iview_type =
-      meta_blit_get_src_image_view_type(src_image);
-
    meta_prepare_blit(cmd_buffer, &saved_state);
 
    for (unsigned r = 0; r < regionCount; r++) {
@@ -1189,7 +1159,7 @@ void anv_CmdCopyImageToBuffer(
          &(VkImageViewCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
             .image = srcImage,
-            .viewType = src_iview_type,
+            .viewType = anv_meta_get_view_type(src_image),
             .format = src_image->format->vk_format,
             .subresourceRange = {
                .aspectMask = pRegions[r].imageSubresource.aspectMask,
@@ -1209,7 +1179,8 @@ void anv_CmdCopyImageToBuffer(
       struct anv_image *dest_image =
          make_image_for_buffer(vk_device, destBuffer, dest_format,
                                VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
-                               src_image->type, &pRegions[r]);
+                               src_image->type, &cmd_buffer->pool->alloc,
+                               &pRegions[r]);
 
       unsigned num_slices;
       if (src_image->type == VK_IMAGE_TYPE_3D) {
@@ -1262,7 +1233,8 @@ void anv_CmdCopyImageToBuffer(
                                dest_image->extent.height * 4;
       }
 
-      anv_DestroyImage(vk_device, anv_image_to_handle(dest_image));
+      anv_DestroyImage(vk_device, anv_image_to_handle(dest_image),
+                       &cmd_buffer->pool->alloc);
    }
 
    meta_finish_blit(cmd_buffer, &saved_state);
@@ -1314,13 +1286,13 @@ anv_device_finish_meta(struct anv_device *device)
 
    /* Blit */
    anv_DestroyRenderPass(anv_device_to_handle(device),
-                         device->meta_state.blit.render_pass);
+                         device->meta_state.blit.render_pass, NULL);
    anv_DestroyPipeline(anv_device_to_handle(device),
-                       device->meta_state.blit.pipeline_2d_src);
+                       device->meta_state.blit.pipeline_2d_src, NULL);
    anv_DestroyPipeline(anv_device_to_handle(device),
-                       device->meta_state.blit.pipeline_3d_src);
+                       device->meta_state.blit.pipeline_3d_src, NULL);
    anv_DestroyPipelineLayout(anv_device_to_handle(device),
-                             device->meta_state.blit.pipeline_layout);
+                             device->meta_state.blit.pipeline_layout, NULL);
    anv_DestroyDescriptorSetLayout(anv_device_to_handle(device),
-                                  device->meta_state.blit.ds_layout);
+                                  device->meta_state.blit.ds_layout, NULL);
 }