turnip: create a less dummy pipeline
authorChia-I Wu <olvaffe@gmail.com>
Thu, 21 Feb 2019 17:22:17 +0000 (09:22 -0800)
committerChia-I Wu <olvaffe@gmail.com>
Mon, 11 Mar 2019 17:02:13 +0000 (10:02 -0700)
Still dummy, but at least it is created from tu_pipeline_builder.

src/freedreno/vulkan/tu_pipeline.c
src/freedreno/vulkan/tu_private.h

index b7598960748c8361021071805d0313ae5c286f34..9df7c20b45503d44a79d225cac6bb24dab86f1d7 100644 (file)
 #include "vk_format.h"
 #include "vk_util.h"
 
-VkResult
-tu_graphics_pipeline_create(
-   VkDevice _device,
-   VkPipelineCache _cache,
-   const VkGraphicsPipelineCreateInfo *pCreateInfo,
-   const struct tu_graphics_pipeline_create_info *extra,
-   const VkAllocationCallbacks *pAllocator,
-   VkPipeline *pPipeline)
+#include "tu_cs.h"
+
+struct tu_pipeline_builder
+{
+   struct tu_device *device;
+   struct tu_pipeline_cache *cache;
+   const VkAllocationCallbacks *alloc;
+   const VkGraphicsPipelineCreateInfo *create_info;
+};
+
+static VkResult
+tu_pipeline_builder_create_pipeline(struct tu_pipeline_builder *builder,
+                                    struct tu_pipeline **out_pipeline)
 {
+   struct tu_device *dev = builder->device;
+
+   struct tu_pipeline *pipeline =
+      vk_zalloc2(&dev->alloc, builder->alloc, sizeof(*pipeline), 8,
+                 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+   if (!pipeline)
+      return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   tu_cs_init(&pipeline->cs, TU_CS_MODE_SUB_STREAM, 2048);
+
+   /* reserve the space now such that tu_cs_begin_sub_stream never fails */
+   VkResult result = tu_cs_reserve_space(dev, &pipeline->cs, 2048);
+   if (result != VK_SUCCESS) {
+      vk_free2(&dev->alloc, builder->alloc, pipeline);
+      return result;
+   }
+
+   *out_pipeline = pipeline;
 
    return VK_SUCCESS;
 }
 
+static void
+tu_pipeline_finish(struct tu_pipeline *pipeline,
+                   struct tu_device *dev,
+                   const VkAllocationCallbacks *alloc)
+{
+   tu_cs_finish(dev, &pipeline->cs);
+}
+
+static VkResult
+tu_pipeline_builder_build(struct tu_pipeline_builder *builder,
+                          struct tu_pipeline **pipeline)
+{
+   VkResult result = tu_pipeline_builder_create_pipeline(builder, pipeline);
+   if (result != VK_SUCCESS)
+      return result;
+
+   /* we should have reserved enough space upfront such that the CS never
+    * grows
+    */
+   assert((*pipeline)->cs.bo_count == 1);
+
+   return VK_SUCCESS;
+}
+
+static void
+tu_pipeline_builder_init_graphics(
+   struct tu_pipeline_builder *builder,
+   struct tu_device *dev,
+   struct tu_pipeline_cache *cache,
+   const VkGraphicsPipelineCreateInfo *create_info,
+   const VkAllocationCallbacks *alloc)
+{
+   *builder = (struct tu_pipeline_builder) {
+      .device = dev,
+      .cache = cache,
+      .create_info = create_info,
+      .alloc = alloc,
+   };
+}
+
 VkResult
-tu_CreateGraphicsPipelines(VkDevice _device,
+tu_CreateGraphicsPipelines(VkDevice device,
                            VkPipelineCache pipelineCache,
                            uint32_t count,
                            const VkGraphicsPipelineCreateInfo *pCreateInfos,
                            const VkAllocationCallbacks *pAllocator,
                            VkPipeline *pPipelines)
 {
-   VkResult result = VK_SUCCESS;
-   unsigned i = 0;
+   TU_FROM_HANDLE(tu_device, dev, device);
+   TU_FROM_HANDLE(tu_pipeline_cache, cache, pipelineCache);
 
-   for (; i < count; i++) {
-      VkResult r;
-      r =
-         tu_graphics_pipeline_create(_device, pipelineCache, &pCreateInfos[i],
-                                     NULL, pAllocator, &pPipelines[i]);
-      if (r != VK_SUCCESS) {
-         result = r;
-         pPipelines[i] = VK_NULL_HANDLE;
+   for (uint32_t i = 0; i < count; i++) {
+      struct tu_pipeline_builder builder;
+      tu_pipeline_builder_init_graphics(&builder, dev, cache,
+                                        &pCreateInfos[i], pAllocator);
+
+      struct tu_pipeline *pipeline;
+      VkResult result = tu_pipeline_builder_build(&builder, &pipeline);
+
+      if (result != VK_SUCCESS) {
+         for (uint32_t j = 0; j < i; j++) {
+            tu_DestroyPipeline(device, pPipelines[j], pAllocator);
+            pPipelines[j] = VK_NULL_HANDLE;
+         }
+
+         return result;
       }
+
+      pPipelines[i] = tu_pipeline_to_handle(pipeline);
    }
 
-   return result;
+   return VK_SUCCESS;
 }
 
 static VkResult
@@ -108,3 +180,18 @@ tu_CreateComputePipelines(VkDevice _device,
 
    return result;
 }
+
+void
+tu_DestroyPipeline(VkDevice _device,
+                   VkPipeline _pipeline,
+                   const VkAllocationCallbacks *pAllocator)
+{
+   TU_FROM_HANDLE(tu_device, dev, _device);
+   TU_FROM_HANDLE(tu_pipeline, pipeline, _pipeline);
+
+   if (!_pipeline)
+      return;
+
+   tu_pipeline_finish(pipeline, dev, pAllocator);
+   vk_free2(&dev->alloc, pAllocator, pipeline);
+}
index f4c455d0d9dce7a059961a621f2b1a24d951007e..9a5a1a7170159dcfad91bd7cffb85a9ecc004c31 100644 (file)
@@ -977,7 +977,8 @@ struct tu_shader_module
 
 struct tu_pipeline
 {
-   struct tu_device *device;
+   struct tu_cs cs;
+
    struct tu_dynamic_state dynamic_state;
 
    struct tu_pipeline_layout *layout;
@@ -1007,15 +1008,6 @@ struct tu_graphics_pipeline_create_info
    uint32_t custom_blend_mode;
 };
 
-VkResult
-tu_graphics_pipeline_create(
-   VkDevice device,
-   VkPipelineCache cache,
-   const VkGraphicsPipelineCreateInfo *pCreateInfo,
-   const struct tu_graphics_pipeline_create_info *extra,
-   const VkAllocationCallbacks *alloc,
-   VkPipeline *pPipeline);
-
 struct tu_native_format
 {
    int vtx;      /* VFMTn_xxx or -1 */