1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2021 Google LLC
3bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT
4bf215546Sopenharmony_ci */
5bf215546Sopenharmony_ci
6bf215546Sopenharmony_ci#include "vn_renderer_util.h"
7bf215546Sopenharmony_ci
8bf215546Sopenharmony_ciVkResult
9bf215546Sopenharmony_civn_renderer_submit_simple_sync(struct vn_renderer *renderer,
10bf215546Sopenharmony_ci                               const void *cs_data,
11bf215546Sopenharmony_ci                               size_t cs_size)
12bf215546Sopenharmony_ci{
13bf215546Sopenharmony_ci   struct vn_renderer_sync *sync;
14bf215546Sopenharmony_ci   VkResult result =
15bf215546Sopenharmony_ci      vn_renderer_sync_create(renderer, 0, VN_RENDERER_SYNC_BINARY, &sync);
16bf215546Sopenharmony_ci   if (result != VK_SUCCESS)
17bf215546Sopenharmony_ci      return result;
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_ci   const struct vn_renderer_submit submit = {
20bf215546Sopenharmony_ci      .batches =
21bf215546Sopenharmony_ci         &(const struct vn_renderer_submit_batch){
22bf215546Sopenharmony_ci            .cs_data = cs_data,
23bf215546Sopenharmony_ci            .cs_size = cs_size,
24bf215546Sopenharmony_ci            .sync_queue_cpu = true,
25bf215546Sopenharmony_ci            .syncs = &sync,
26bf215546Sopenharmony_ci            .sync_values = &(const uint64_t){ 1 },
27bf215546Sopenharmony_ci            .sync_count = 1,
28bf215546Sopenharmony_ci         },
29bf215546Sopenharmony_ci      .batch_count = 1,
30bf215546Sopenharmony_ci   };
31bf215546Sopenharmony_ci   const struct vn_renderer_wait wait = {
32bf215546Sopenharmony_ci      .timeout = UINT64_MAX,
33bf215546Sopenharmony_ci      .syncs = &sync,
34bf215546Sopenharmony_ci      .sync_values = &(const uint64_t){ 1 },
35bf215546Sopenharmony_ci      .sync_count = 1,
36bf215546Sopenharmony_ci   };
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   result = vn_renderer_submit(renderer, &submit);
39bf215546Sopenharmony_ci   if (result == VK_SUCCESS)
40bf215546Sopenharmony_ci      result = vn_renderer_wait(renderer, &wait);
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   vn_renderer_sync_destroy(renderer, sync);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   return result;
45bf215546Sopenharmony_ci}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_civoid
48bf215546Sopenharmony_civn_renderer_shmem_pool_init(UNUSED struct vn_renderer *renderer,
49bf215546Sopenharmony_ci                            struct vn_renderer_shmem_pool *pool,
50bf215546Sopenharmony_ci                            size_t min_alloc_size)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci   *pool = (struct vn_renderer_shmem_pool){
53bf215546Sopenharmony_ci      /* power-of-two to hit shmem cache */
54bf215546Sopenharmony_ci      .min_alloc_size = util_next_power_of_two(min_alloc_size),
55bf215546Sopenharmony_ci   };
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_civoid
59bf215546Sopenharmony_civn_renderer_shmem_pool_fini(struct vn_renderer *renderer,
60bf215546Sopenharmony_ci                            struct vn_renderer_shmem_pool *pool)
61bf215546Sopenharmony_ci{
62bf215546Sopenharmony_ci   if (pool->shmem)
63bf215546Sopenharmony_ci      vn_renderer_shmem_unref(renderer, pool->shmem);
64bf215546Sopenharmony_ci}
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_cistatic bool
67bf215546Sopenharmony_civn_renderer_shmem_pool_grow(struct vn_renderer *renderer,
68bf215546Sopenharmony_ci                            struct vn_renderer_shmem_pool *pool,
69bf215546Sopenharmony_ci                            size_t size)
70bf215546Sopenharmony_ci{
71bf215546Sopenharmony_ci   VN_TRACE_FUNC();
72bf215546Sopenharmony_ci   /* power-of-two to hit shmem cache */
73bf215546Sopenharmony_ci   size_t alloc_size = pool->min_alloc_size;
74bf215546Sopenharmony_ci   while (alloc_size < size) {
75bf215546Sopenharmony_ci      alloc_size <<= 1;
76bf215546Sopenharmony_ci      if (!alloc_size)
77bf215546Sopenharmony_ci         return false;
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci   struct vn_renderer_shmem *shmem =
81bf215546Sopenharmony_ci      vn_renderer_shmem_create(renderer, alloc_size);
82bf215546Sopenharmony_ci   if (!shmem)
83bf215546Sopenharmony_ci      return false;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   if (pool->shmem)
86bf215546Sopenharmony_ci      vn_renderer_shmem_unref(renderer, pool->shmem);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   pool->shmem = shmem;
89bf215546Sopenharmony_ci   pool->size = alloc_size;
90bf215546Sopenharmony_ci   pool->used = 0;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   return true;
93bf215546Sopenharmony_ci}
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_cistruct vn_renderer_shmem *
96bf215546Sopenharmony_civn_renderer_shmem_pool_alloc(struct vn_renderer *renderer,
97bf215546Sopenharmony_ci                             struct vn_renderer_shmem_pool *pool,
98bf215546Sopenharmony_ci                             size_t size,
99bf215546Sopenharmony_ci                             size_t *out_offset)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   if (unlikely(size > pool->size - pool->used)) {
102bf215546Sopenharmony_ci      if (!vn_renderer_shmem_pool_grow(renderer, pool, size))
103bf215546Sopenharmony_ci         return NULL;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      assert(size <= pool->size - pool->used);
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   struct vn_renderer_shmem *shmem =
109bf215546Sopenharmony_ci      vn_renderer_shmem_ref(renderer, pool->shmem);
110bf215546Sopenharmony_ci   *out_offset = pool->used;
111bf215546Sopenharmony_ci   pool->used += size;
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   return shmem;
114bf215546Sopenharmony_ci}
115