• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  *
7  */
8 
9 //
10 //
11 //
12 
13 #include "event_pool.h"
14 #include "host_alloc.h"
15 #include "assert_vk.h"
16 
17 //
18 //
19 //
20 
21 struct vk_event_pool
22 {
23   VkEvent                     * events;
24   VkAllocationCallbacks const * allocator;
25   VkDevice                      device;
26   uint32_t                      resize;
27   uint32_t                      size;
28   uint32_t                      next;
29 };
30 
31 static
32 void
vk_event_pool_resize(struct vk_event_pool * const event_pool)33 vk_event_pool_resize(struct vk_event_pool * const event_pool)
34 {
35   static struct VkEventCreateInfo const eci = {
36     .sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO,
37     .pNext = NULL,
38     .flags = 0
39   };
40 
41   // FIXME -- respect allocator
42 
43   event_pool->size  += event_pool->resize;
44   event_pool->events = vk_host_realloc(event_pool->allocator,
45                                        event_pool->events,
46                                        sizeof(VkEvent) * event_pool->size);
47 
48   for (uint32_t ii=event_pool->next; ii<event_pool->size; ii++)
49     {
50       vk(CreateEvent(event_pool->device,
51                      &eci,
52                      event_pool->allocator,
53                      event_pool->events+ii));
54     }
55 }
56 
57 struct vk_event_pool *
vk_event_pool_create(VkDevice device,VkAllocationCallbacks const * allocator,uint32_t const resize)58 vk_event_pool_create(VkDevice device, VkAllocationCallbacks const * allocator, uint32_t const resize)
59 {
60   struct vk_event_pool * const event_pool = vk_host_alloc(allocator,sizeof(*event_pool));
61 
62   event_pool->events    = NULL;
63   event_pool->allocator = allocator;
64   event_pool->device    = device;
65   event_pool->resize    = resize;
66   event_pool->size      = 0;
67   event_pool->next      = 0;
68 
69   vk_event_pool_resize(event_pool);
70 
71   return event_pool;
72 }
73 
74 void
vk_event_pool_release(struct vk_event_pool * const event_pool)75 vk_event_pool_release(struct vk_event_pool * const event_pool)
76 {
77   for (uint32_t ii=0; ii<event_pool->size; ii++)
78     {
79       vkDestroyEvent(event_pool->device,
80                      event_pool->events[ii],
81                      event_pool->allocator);
82     }
83 
84   vk_host_free(event_pool->allocator,event_pool->events);
85   vk_host_free(event_pool->allocator,event_pool);
86 }
87 
88 void
vk_event_pool_reset(struct vk_event_pool * const event_pool)89 vk_event_pool_reset(struct vk_event_pool * const event_pool)
90 {
91   for (uint32_t ii=0; ii<event_pool->next; ii++)
92     vk(ResetEvent(event_pool->device,event_pool->events[ii]));
93 
94   event_pool->next = 0;
95 }
96 
97 VkEvent
vk_event_pool_acquire(struct vk_event_pool * const event_pool)98 vk_event_pool_acquire(struct vk_event_pool * const event_pool)
99 {
100   if (event_pool->next == event_pool->size)
101     vk_event_pool_resize(event_pool);
102 
103   return event_pool->events[event_pool->next++];
104 }
105 
106 //
107 //
108 //
109