panfrost/midgard: Hit missed scheduling opportunity
[mesa.git] / src / freedreno / vulkan / tu_cs.c
index 02bb1bceac462edc79928ee68ad48e5082fcb14f..48242f813ada953826896797c6d3881b56a6a0e1 100644 (file)
  * Initialize a command stream.
  */
 void
-tu_cs_init(struct tu_cs *cs)
+tu_cs_init(struct tu_cs *cs, enum tu_cs_mode mode, uint32_t initial_size)
 {
-   cs->start = cs->cur = cs->end = NULL;
+   assert(mode != TU_CS_MODE_EXTERNAL);
 
-   cs->entry_count = cs->entry_capacity = 0;
-   cs->entries = NULL;
+   memset(cs, 0, sizeof(*cs));
 
-   cs->bo_count = cs->bo_capacity = 0;
-   cs->bos = NULL;
+   cs->mode = mode;
+   cs->next_bo_size = initial_size;
+}
+
+/**
+ * Initialize a command stream as a wrapper to an external buffer.
+ */
+void
+tu_cs_init_external(struct tu_cs *cs, uint32_t *start, uint32_t *end)
+{
+   memset(cs, 0, sizeof(*cs));
+
+   cs->mode = TU_CS_MODE_EXTERNAL;
+   cs->start = cs->reserved_end = cs->cur = start;
+   cs->end = end;
 }
 
 /**
@@ -54,88 +66,274 @@ tu_cs_finish(struct tu_device *dev, struct tu_cs *cs)
 }
 
 /**
- * Begin (or continue) command packet emission.  This will reserve space from
- * the command stream for at least \a reserve_size uint32_t values.
+ * Get the offset of the command packets emitted since the last call to
+ * tu_cs_add_entry.
  */
-VkResult
-tu_cs_begin(struct tu_device *dev, struct tu_cs *cs, uint32_t reserve_size)
+static uint32_t
+tu_cs_get_offset(const struct tu_cs *cs)
 {
-   assert(reserve_size);
+   assert(cs->bo_count);
+   return cs->start - (uint32_t *) cs->bos[cs->bo_count - 1]->map;
+}
 
-   if (cs->end - cs->cur < reserve_size) {
-      if (cs->bo_count == cs->bo_capacity) {
-         uint32_t new_capacity = MAX2(4, 2 * cs->bo_capacity);
-         struct tu_bo **new_bos =
-            realloc(cs->bos, new_capacity * sizeof(struct tu_bo *));
-         if (!new_bos)
-            abort();
+/**
+ * Get the size of the command packets emitted since the last call to
+ * tu_cs_add_entry.
+ */
+static uint32_t
+tu_cs_get_size(const struct tu_cs *cs)
+{
+   return cs->cur - cs->start;
+}
 
-         cs->bo_capacity = new_capacity;
-         cs->bos = new_bos;
-      }
+/**
+ * Get the size of the remaining space in the current BO.
+ */
+static uint32_t
+tu_cs_get_space(const struct tu_cs *cs)
+{
+   return cs->end - cs->cur;
+}
 
-      uint32_t new_size = MAX2(16384, reserve_size * sizeof(uint32_t));
-      if (cs->bo_count)
-         new_size = MAX2(new_size, cs->bos[cs->bo_count - 1]->size * 2);
+/**
+ * Return true if there is no command packet emitted since the last call to
+ * tu_cs_add_entry.
+ */
+static uint32_t
+tu_cs_is_empty(const struct tu_cs *cs)
+{
+   return tu_cs_get_size(cs) == 0;
+}
 
-      struct tu_bo *new_bo = malloc(sizeof(struct tu_bo));
-      if (!new_bo)
-         abort();
+/*
+ * Allocate and add a BO to a command stream.  Following command packets will
+ * be emitted to the new BO.
+ */
+static VkResult
+tu_cs_add_bo(struct tu_device *dev, struct tu_cs *cs, uint32_t size)
+{
+   /* no BO for TU_CS_MODE_EXTERNAL */
+   assert(cs->mode != TU_CS_MODE_EXTERNAL);
 
-      VkResult result = tu_bo_init_new(dev, new_bo, new_size);
-      if (result != VK_SUCCESS) {
-         free(new_bo);
-         return result;
-      }
+   /* no dangling command packet */
+   assert(tu_cs_is_empty(cs));
 
-      result = tu_bo_map(dev, new_bo);
-      if (result != VK_SUCCESS) {
-         tu_bo_finish(dev, new_bo);
-         free(new_bo);
-         return result;
-      }
+   /* grow cs->bos if needed */
+   if (cs->bo_count == cs->bo_capacity) {
+      uint32_t new_capacity = MAX2(4, 2 * cs->bo_capacity);
+      struct tu_bo **new_bos =
+         realloc(cs->bos, new_capacity * sizeof(struct tu_bo *));
+      if (!new_bos)
+         return VK_ERROR_OUT_OF_HOST_MEMORY;
 
-      cs->bos[cs->bo_count] = new_bo;
-      ++cs->bo_count;
+      cs->bo_capacity = new_capacity;
+      cs->bos = new_bos;
+   }
 
-      cs->start = cs->cur = (uint32_t *) new_bo->map;
-      cs->end = cs->start + new_bo->size / sizeof(uint32_t);
+   struct tu_bo *new_bo = malloc(sizeof(struct tu_bo));
+   if (!new_bo)
+      return VK_ERROR_OUT_OF_HOST_MEMORY;
+
+   VkResult result = tu_bo_init_new(dev, new_bo, size * sizeof(uint32_t));
+   if (result != VK_SUCCESS) {
+      free(new_bo);
+      return result;
    }
-   cs->start = cs->cur;
+
+   result = tu_bo_map(dev, new_bo);
+   if (result != VK_SUCCESS) {
+      tu_bo_finish(dev, new_bo);
+      free(new_bo);
+      return result;
+   }
+
+   cs->bos[cs->bo_count++] = new_bo;
+
+   cs->start = cs->cur = cs->reserved_end = (uint32_t *) new_bo->map;
+   cs->end = cs->start + new_bo->size / sizeof(uint32_t);
 
    return VK_SUCCESS;
 }
 
 /**
- * End command packet emission by adding an IB entry for the command packets
- * emitted since the last call to tu_cs_begin.
+ * Reserve an IB entry.
  */
-VkResult
-tu_cs_end(struct tu_cs *cs)
+static VkResult
+tu_cs_reserve_entry(struct tu_device *dev, struct tu_cs *cs)
 {
-   if (cs->start == cs->cur)
-      return VK_SUCCESS;
+   /* entries are only for TU_CS_MODE_GROW */
+   assert(cs->mode == TU_CS_MODE_GROW);
 
-   if (cs->entry_capacity == cs->entry_count) {
-      uint32_t new_capacity = MAX2(cs->entry_capacity * 2, 4);
+   /* grow cs->entries if needed */
+   if (cs->entry_count == cs->entry_capacity) {
+      uint32_t new_capacity = MAX2(4, cs->entry_capacity * 2);
       struct tu_cs_entry *new_entries =
          realloc(cs->entries, new_capacity * sizeof(struct tu_cs_entry));
       if (!new_entries)
-         abort(); /* TODO */
+         return VK_ERROR_OUT_OF_HOST_MEMORY;
 
-      cs->entries = new_entries;
       cs->entry_capacity = new_capacity;
+      cs->entries = new_entries;
    }
 
+   return VK_SUCCESS;
+}
+
+/**
+ * Add an IB entry for the command packets emitted since the last call to this
+ * function.
+ */
+static void
+tu_cs_add_entry(struct tu_cs *cs)
+{
+   /* entries are only for TU_CS_MODE_GROW */
+   assert(cs->mode == TU_CS_MODE_GROW);
+
+   /* disallow empty entry */
+   assert(!tu_cs_is_empty(cs));
+
+   /*
+    * because we disallow empty entry, tu_cs_add_bo and tu_cs_reserve_entry
+    * must both have been called
+    */
    assert(cs->bo_count);
+   assert(cs->entry_count < cs->entry_capacity);
 
-   struct tu_cs_entry entry;
-   entry.bo = cs->bos[cs->bo_count - 1];
-   entry.size = (cs->cur - cs->start) * sizeof(uint32_t);
-   entry.offset = (cs->start - (uint32_t *) entry.bo->map) * sizeof(uint32_t);
+   /* add an entry for [cs->start, cs->cur] */
+   cs->entries[cs->entry_count++] = (struct tu_cs_entry) {
+      .bo = cs->bos[cs->bo_count - 1],
+      .size = tu_cs_get_size(cs) * sizeof(uint32_t),
+      .offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
+   };
 
-   cs->entries[cs->entry_count] = entry;
-   ++cs->entry_count;
+   cs->start = cs->cur;
+}
+
+/**
+ * Begin (or continue) command packet emission.  This does nothing but sanity
+ * checks currently.  \a cs must not be in TU_CS_MODE_SUB_STREAM mode.
+ */
+void
+tu_cs_begin(struct tu_cs *cs)
+{
+   assert(cs->mode != TU_CS_MODE_SUB_STREAM);
+   assert(tu_cs_is_empty(cs));
+}
+
+/**
+ * End command packet emission.  This adds an IB entry when \a cs is in
+ * TU_CS_MODE_GROW mode.
+ */
+void
+tu_cs_end(struct tu_cs *cs)
+{
+   assert(cs->mode != TU_CS_MODE_SUB_STREAM);
+
+   if (cs->mode == TU_CS_MODE_GROW && !tu_cs_is_empty(cs))
+      tu_cs_add_entry(cs);
+}
+
+/**
+ * Begin command packet emission to a sub-stream.  \a cs must be in
+ * TU_CS_MODE_SUB_STREAM mode.
+ *
+ * Return \a sub_cs which is in TU_CS_MODE_EXTERNAL mode.  tu_cs_begin and
+ * tu_cs_reserve_space are implied and \a sub_cs is ready for command packet
+ * emission.
+ */
+VkResult
+tu_cs_begin_sub_stream(struct tu_device *dev,
+                       struct tu_cs *cs,
+                       uint32_t size,
+                       struct tu_cs *sub_cs)
+{
+   assert(cs->mode == TU_CS_MODE_SUB_STREAM);
+   assert(size);
+
+   VkResult result = tu_cs_reserve_space(dev, cs, size);
+   if (result != VK_SUCCESS)
+      return result;
+
+   tu_cs_init_external(sub_cs, cs->cur, cs->reserved_end);
+   tu_cs_begin(sub_cs);
+   result = tu_cs_reserve_space(dev, sub_cs, size);
+   assert(result == VK_SUCCESS);
+
+   return VK_SUCCESS;
+}
+
+/**
+ * End command packet emission to a sub-stream.  \a sub_cs becomes invalid
+ * after this call.
+ *
+ * Return an IB entry for the sub-stream.  The entry has the same lifetime as
+ * \a cs.
+ */
+struct tu_cs_entry
+tu_cs_end_sub_stream(struct tu_cs *cs, struct tu_cs *sub_cs)
+{
+   assert(cs->mode == TU_CS_MODE_SUB_STREAM);
+   assert(cs->bo_count);
+   assert(sub_cs->start == cs->cur && sub_cs->end == cs->reserved_end);
+   tu_cs_sanity_check(sub_cs);
+
+   tu_cs_end(sub_cs);
+
+   cs->cur = sub_cs->cur;
+
+   struct tu_cs_entry entry = {
+      .bo = cs->bos[cs->bo_count - 1],
+      .size = tu_cs_get_size(cs) * sizeof(uint32_t),
+      .offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
+   };
+
+   cs->start = cs->cur;
+
+   return entry;
+}
+
+/**
+ * Reserve space from a command stream for \a reserved_size uint32_t values.
+ * This never fails when \a cs has mode TU_CS_MODE_EXTERNAL.
+ */
+VkResult
+tu_cs_reserve_space(struct tu_device *dev,
+                    struct tu_cs *cs,
+                    uint32_t reserved_size)
+{
+   if (tu_cs_get_space(cs) < reserved_size) {
+      if (cs->mode == TU_CS_MODE_EXTERNAL) {
+         unreachable("cannot grow external buffer");
+         return VK_ERROR_OUT_OF_HOST_MEMORY;
+      }
+
+      /* add an entry for the exiting command packets */
+      if (!tu_cs_is_empty(cs)) {
+         /* no direct command packet for TU_CS_MODE_SUB_STREAM */
+         assert(cs->mode != TU_CS_MODE_SUB_STREAM);
+
+         tu_cs_add_entry(cs);
+      }
+
+      /* switch to a new BO */
+      uint32_t new_size = MAX2(cs->next_bo_size, reserved_size);
+      VkResult result = tu_cs_add_bo(dev, cs, new_size);
+      if (result != VK_SUCCESS)
+         return result;
+
+      /* double the size for the next bo */
+      new_size <<= 1;
+      if (cs->next_bo_size < new_size)
+         cs->next_bo_size = new_size;
+   }
+
+   assert(tu_cs_get_space(cs) >= reserved_size);
+   cs->reserved_end = cs->cur + reserved_size;
+
+   if (cs->mode == TU_CS_MODE_GROW) {
+      /* reserve an entry for the next call to this function or tu_cs_end */
+      return tu_cs_reserve_entry(dev, cs);
+   }
 
    return VK_SUCCESS;
 }
@@ -147,6 +345,12 @@ tu_cs_end(struct tu_cs *cs)
 void
 tu_cs_reset(struct tu_device *dev, struct tu_cs *cs)
 {
+   if (cs->mode == TU_CS_MODE_EXTERNAL) {
+      assert(!cs->bo_count && !cs->entry_count);
+      cs->reserved_end = cs->cur = cs->start;
+      return;
+   }
+
    for (uint32_t i = 0; i + 1 < cs->bo_count; ++i) {
       tu_bo_finish(dev, cs->bos[i]);
       free(cs->bos[i]);
@@ -156,25 +360,9 @@ tu_cs_reset(struct tu_device *dev, struct tu_cs *cs)
       cs->bos[0] = cs->bos[cs->bo_count - 1];
       cs->bo_count = 1;
 
-      cs->start = cs->cur = (uint32_t *) cs->bos[0]->map;
+      cs->start = cs->cur = cs->reserved_end = (uint32_t *) cs->bos[0]->map;
       cs->end = cs->start + cs->bos[0]->size / sizeof(uint32_t);
    }
 
    cs->entry_count = 0;
 }
-
-/**
- * Reserve space from a command stream for \a size uint32_t values.
- */
-VkResult
-tu_cs_check_space(struct tu_device *dev, struct tu_cs *cs, size_t size)
-{
-   if (cs->end - cs->cur >= size)
-      return VK_SUCCESS;
-
-   VkResult result = tu_cs_end(cs);
-   if (result != VK_SUCCESS)
-      return result;
-
-   return tu_cs_begin(dev, cs, size);
-}