History

vulkan: move shader data execution state to execution pools

This finally permits using fully compiled shaders across multiple execution contexts.

Lynne committed 2 years ago

Commit d233d991 parent 0a37d5a2 files · +153 −85Browse files

Unified · Split

2 files
  1. libavutil/vulkan.c +103 −54
  2. libavutil/vulkan.h +50 −31
libavutil/vulkan.c+103−54
@@ -262,6 +262,28 @@ void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool) · 261 unmodified lines
262262 av_free(e->sem_wait);
263263 }
264264
265+ /* Free shader-specific data */
266+ for (int i = 0; i < pool->nb_reg_shd; i++) {
267+ FFVulkanShaderData *sd = &pool->reg_shd[i];
268+
269+ if (s->extensions & FF_VK_EXT_DESCRIPTOR_BUFFER) {
270+ for (int j = 0; j < sd->nb_descriptor_sets; j++) {
271+ FFVulkanDescriptorSetData *set_data = &sd->desc_set_buf[j];
272+ if (set_data->buf.mem)
273+ ff_vk_unmap_buffer(s, &set_data->buf, 0);
274+ ff_vk_free_buf(s, &set_data->buf);
275+ }
276+ }
277+
278+ if (sd->desc_pool)
279+ vk->DestroyDescriptorPool(s->hwctx->act_dev, sd->desc_pool,
280+ s->hwctx->alloc);
281+
282+ av_freep(&sd->desc_set_buf);
283+ av_freep(&sd->desc_bind);
284+ av_freep(&sd->desc_sets);
285+ }
286+
265287 if (pool->cmd_bufs)
266288 vk->FreeCommandBuffers(s->hwctx->act_dev, pool->cmd_buf_pool,
267289 pool->pool_size, pool->cmd_bufs);
@@ -1655,6 +1677,16 @@ int ff_vk_shader_link(FFVulkanContext *s, FFVulkanShader *shd, · 1387 unmodified lines
16551677 if (err < 0)
16561678 return err;
16571679
1680+ if (s->extensions & FF_VK_EXT_DESCRIPTOR_BUFFER) {
1681+ shd->bound_buffer_indices = av_calloc(shd->nb_descriptor_sets,
1682+ sizeof(*shd->bound_buffer_indices));
1683+ if (!shd->bound_buffer_indices)
1684+ return AVERROR(ENOMEM);
1685+
1686+ for (int i = 0; i < shd->nb_descriptor_sets; i++)
1687+ shd->bound_buffer_indices[i] = i;
1688+ }
1689+
16581690 {
16591691 VkShaderModule mod;
16601692 err = create_shader_module(s, shd, &mod, spirv, spirv_len);
@@ -1817,25 +1849,40 @@ int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, · 156 unmodified lines
18171849 FFVulkanShader *shd)
18181850 {
18191851 int err;
1852+ FFVulkanShaderData *sd;
18201853
18211854 if (!shd->nb_descriptor_sets)
18221855 return 0;
18231856
1857+ sd = av_realloc_array(pool->reg_shd,
1858+ sizeof(*pool->reg_shd),
1859+ pool->nb_reg_shd + 1);
1860+ if (!sd)
1861+ return AVERROR(ENOMEM);
1862+
1863+ pool->reg_shd = sd;
1864+ sd = &sd[pool->nb_reg_shd++];
1865+ memset(sd, 0, sizeof(*sd));
1866+
1867+ sd->shd = shd;
1868+ sd->nb_descriptor_sets = shd->nb_descriptor_sets;
1869+
18241870 if (s->extensions & FF_VK_EXT_DESCRIPTOR_BUFFER) {
1825− shd->desc_bind = av_calloc(shd->nb_descriptor_sets, sizeof(*shd->desc_bind));
1826− if (!shd->desc_bind)
1871+ sd->desc_bind = av_malloc_array(sd->nb_descriptor_sets, sizeof(*sd->desc_bind));
1872+ if (!sd->desc_bind)
18271873 return AVERROR(ENOMEM);
18281874
1829− shd->bound_buffer_indices = av_calloc(shd->nb_descriptor_sets,
1830− sizeof(*shd->bound_buffer_indices));
1831− if (!shd->bound_buffer_indices)
1875+ sd->desc_set_buf = av_calloc(sd->nb_descriptor_sets, sizeof(*sd->desc_set_buf));
1876+ if (!sd->desc_set_buf)
18321877 return AVERROR(ENOMEM);
18331878
1834− for (int i = 0; i < shd->nb_descriptor_sets; i++) {
1879+ for (int i = 0; i < sd->nb_descriptor_sets; i++) {
18351880 FFVulkanDescriptorSet *set = &shd->desc_set[i];
1881+ FFVulkanDescriptorSetData *sdb = &sd->desc_set_buf[i];
18361882 int nb = set->singular ? 1 : pool->pool_size;
18371883
1838− err = ff_vk_create_buf(s, &set->buf, set->aligned_size*nb,
1884+ err = ff_vk_create_buf(s, &sdb->buf,
1885+ set->aligned_size*nb,
18391886 NULL, NULL, set->usage,
18401887 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
18411888 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
@@ -1843,17 +1890,15 @@ int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, · 1 unmodified line
18431890 if (err < 0)
18441891 return err;
18451892
1846− err = ff_vk_map_buffer(s, &set->buf, &set->desc_mem, 0);
1893+ err = ff_vk_map_buffer(s, &sdb->buf, &sdb->desc_mem, 0);
18471894 if (err < 0)
18481895 return err;
18491896
1850− shd->desc_bind[i] = (VkDescriptorBufferBindingInfoEXT) {
1897+ sd->desc_bind[i] = (VkDescriptorBufferBindingInfoEXT) {
18511898 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_BUFFER_BINDING_INFO_EXT,
18521899 .usage = set->usage,
1853− .address = set->buf.address,
1900+ .address = sdb->buf.address,
18541901 };
1855−
1856− shd->bound_buffer_indices[i] = i;
18571902 }
18581903 } else if (!shd->use_push) {
18591904 VkResult ret;
@@ -1870,11 +1915,11 @@ int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, · 10 unmodified lines
18701915 .flags = 0,
18711916 .pPoolSizes = shd->desc_pool_size,
18721917 .poolSizeCount = shd->nb_desc_pool_size,
1873− .maxSets = shd->nb_descriptor_sets*pool->pool_size,
1918+ .maxSets = sd->nb_descriptor_sets*pool->pool_size,
18741919 };
18751920
18761921 ret = vk->CreateDescriptorPool(s->hwctx->act_dev, &pool_create_info,
1877− s->hwctx->alloc, &shd->desc_pool);
1922+ s->hwctx->alloc, &sd->desc_pool);
18781923 if (ret != VK_SUCCESS) {
18791924 av_log(s, AV_LOG_ERROR, "Unable to create descriptor pool: %s\n",
18801925 ff_vk_ret2str(ret));
@@ -1887,63 +1932,75 @@ int ff_vk_shader_register_exec(FFVulkanContext *s, FFVkExecPool *pool, · 6 unmodified lines
18871932
18881933 /* Colate each execution context's descriptor set layouts */
18891934 for (int i = 0; i < pool->pool_size; i++)
1890− for (int j = 0; j < shd->nb_descriptor_sets; j++)
1891− tmp_layouts[i*shd->nb_descriptor_sets + j] = shd->desc_layout[j];
1935+ for (int j = 0; j < sd->nb_descriptor_sets; j++)
1936+ tmp_layouts[i*sd->nb_descriptor_sets + j] = shd->desc_layout[j];
18921937
18931938 set_alloc_info = (VkDescriptorSetAllocateInfo) {
18941939 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1895− .descriptorPool = shd->desc_pool,
1940+ .descriptorPool = sd->desc_pool,
18961941 .pSetLayouts = tmp_layouts,
18971942 .descriptorSetCount = pool_create_info.maxSets,
18981943 };
18991944
1900− shd->desc_sets = av_malloc_array(pool_create_info.maxSets,
1945+ sd->desc_sets = av_malloc_array(pool_create_info.maxSets,
19011946 sizeof(*tmp_layouts));
1902− if (!shd->desc_sets) {
1947+ if (!sd->desc_sets) {
19031948 av_free(tmp_layouts);
19041949 return AVERROR(ENOMEM);
19051950 }
19061951 ret = vk->AllocateDescriptorSets(s->hwctx->act_dev, &set_alloc_info,
1907− shd->desc_sets);
1952+ sd->desc_sets);
19081953 av_free(tmp_layouts);
19091954 if (ret != VK_SUCCESS) {
19101955 av_log(s, AV_LOG_ERROR, "Unable to allocate descriptor set: %s\n",
19111956 ff_vk_ret2str(ret));
1912− av_freep(&shd->desc_sets);
1957+ av_freep(&sd->desc_sets);
19131958 return AVERROR_EXTERNAL;
19141959 }
1915−
1916− shd->assoc_pool = pool;
19171960 }
19181961
19191962 return 0;
19201963 }
19211964
1965+static inline FFVulkanShaderData *get_shd_data(FFVkExecContext *e,
1966+ FFVulkanShader *shd)
1967+{
1968+ for (int i = 0; i < e->parent->nb_reg_shd; i++)
1969+ if (e->parent->reg_shd[i].shd == shd)
1970+ return &e->parent->reg_shd[i];
1971+ av_assert0(0);
1972+}
1973+
19221974 static inline void update_set_descriptor(FFVulkanContext *s, FFVkExecContext *e,
1923− FFVulkanDescriptorSet *set,
1975+ FFVulkanShader *shd, int set,
19241976 int bind_idx, int array_idx,
19251977 VkDescriptorGetInfoEXT *desc_get_info,
19261978 size_t desc_size)
19271979 {
19281980 FFVulkanFunctions *vk = &s->vkfn;
1929− const size_t exec_offset = set->singular ? 0 : set->aligned_size*e->idx;
1930− void *desc = set->desc_mem + /* Base */
1931− exec_offset + /* Execution context */
1932− set->binding_offset[bind_idx] + /* Descriptor binding */
1933− array_idx*desc_size; /* Array position */
1981+ FFVulkanDescriptorSet *desc_set = &shd->desc_set[set];
1982+ FFVulkanShaderData *sd = get_shd_data(e, shd);
1983+ const size_t exec_offset = desc_set->singular ? 0 : desc_set->aligned_size*e->idx;
1984+
1985+ void *desc = sd->desc_set_buf[set].desc_mem + /* Base */
1986+ exec_offset + /* Execution context */
1987+ desc_set->binding_offset[bind_idx] + /* Descriptor binding */
1988+ array_idx*desc_size; /* Array position */
19341989
19351990 vk->GetDescriptorEXT(s->hwctx->act_dev, desc_get_info, desc_size, desc);
19361991 }
19371992
1938−static inline void update_set_pool_write(FFVulkanContext *s, FFVulkanShader *shd,
1939− FFVkExecContext *e,
1940− FFVulkanDescriptorSet *desc_set, int set,
1993+static inline void update_set_pool_write(FFVulkanContext *s, FFVkExecContext *e,
1994+ FFVulkanShader *shd, int set,
19411995 VkWriteDescriptorSet *write_info)
19421996 {
19431997 FFVulkanFunctions *vk = &s->vkfn;
1998+ FFVulkanDescriptorSet *desc_set = &shd->desc_set[set];
1999+ FFVulkanShaderData *sd = get_shd_data(e, shd);
2000+
19442001 if (desc_set->singular) {
1945− for (int i = 0; i < shd->assoc_pool->pool_size; i++) {
1946− write_info->dstSet = shd->desc_sets[i*shd->nb_descriptor_sets + set];
2002+ for (int i = 0; i < e->parent->pool_size; i++) {
2003+ write_info->dstSet = sd->desc_sets[i*sd->nb_descriptor_sets + set];
19472004 vk->UpdateDescriptorSets(s->hwctx->act_dev, 1, write_info, 0, NULL);
19482005 }
19492006 } else {
@@ -1954,7 +2011,7 @@ static inline void update_set_pool_write(FFVulkanContext *s, FFVulkanShader *shd · 4 unmodified lines
19542011 set, 1,
19552012 write_info);
19562013 } else {
1957− write_info->dstSet = shd->desc_sets[e->idx*shd->nb_descriptor_sets + set];
2014+ write_info->dstSet = sd->desc_sets[e->idx*sd->nb_descriptor_sets + set];
19582015 vk->UpdateDescriptorSets(s->hwctx->act_dev, 1, write_info, 0, NULL);
19592016 }
19602017 }
@@ -2003,7 +2060,7 @@ static int vk_set_descriptor_image(FFVulkanContext *s, FFVulkanShader *shd, · 42 unmodified lines
20032060 break;
20042061 };
20052062
2006− update_set_descriptor(s, e, desc_set, bind, offs,
2063+ update_set_descriptor(s, e, shd, set, bind, offs,
20072064 &desc_get_info, desc_size);
20082065 } else {
20092066 VkDescriptorImageInfo desc_pool_write_info_img = {
@@ -2019,7 +2076,7 @@ static int vk_set_descriptor_image(FFVulkanContext *s, FFVulkanShader *shd, · 9 unmodified lines
20192076 .descriptorType = desc_set->binding[bind].descriptorType,
20202077 .pImageInfo = &desc_pool_write_info_img,
20212078 };
2022− update_set_pool_write(s, shd, e, desc_set, set, &desc_pool_write_info);
2079+ update_set_pool_write(s, e, shd, set, &desc_pool_write_info);
20232080 }
20242081
20252082 return 0;
@@ -2070,7 +2127,7 @@ int ff_vk_shader_update_desc_buffer(FFVulkanContext *s, FFVkExecContext *e, · 44 unmodified lines
20702127 break;
20712128 };
20722129
2073− update_set_descriptor(s, e, desc_set, bind, elem, &desc_get_info, desc_size);
2130+ update_set_descriptor(s, e, shd, set, bind, elem, &desc_get_info, desc_size);
20742131 } else {
20752132 VkDescriptorBufferInfo desc_pool_write_info_buf = {
20762133 .buffer = buf->buf,
@@ -2085,7 +2142,7 @@ int ff_vk_shader_update_desc_buffer(FFVulkanContext *s, FFVkExecContext *e, · 8 unmodified lines
20852142 .descriptorType = desc_set->binding[bind].descriptorType,
20862143 .pBufferInfo = &desc_pool_write_info_buf,
20872144 };
2088− update_set_pool_write(s, shd, e, desc_set, set, &desc_pool_write_info);
2145+ update_set_pool_write(s, e, shd, set, &desc_pool_write_info);
20892146 }
20902147
20912148 return 0;
@@ -2119,25 +2176,26 @@ void ff_vk_exec_bind_shader(FFVulkanContext *s, FFVkExecContext *e, · 27 unmodified lines
21192176 {
21202177 FFVulkanFunctions *vk = &s->vkfn;
21212178 VkDeviceSize offsets[1024];
2179+ FFVulkanShaderData *sd = get_shd_data(e, shd);
21222180
21232181 /* Bind pipeline */
21242182 vk->CmdBindPipeline(e->buf, shd->bind_point, shd->pipeline);
21252183
2126− if (shd->nb_descriptor_sets) {
2184+ if (sd->nb_descriptor_sets) {
21272185 if (s->extensions & FF_VK_EXT_DESCRIPTOR_BUFFER) {
2128− for (int i = 0; i < shd->nb_descriptor_sets; i++)
2186+ for (int i = 0; i < sd->nb_descriptor_sets; i++)
21292187 offsets[i] = shd->desc_set[i].singular ? 0 : shd->desc_set[i].aligned_size*e->idx;
21302188
21312189 /* Bind descriptor buffers */
2132− vk->CmdBindDescriptorBuffersEXT(e->buf, shd->nb_descriptor_sets, shd->desc_bind);
2190+ vk->CmdBindDescriptorBuffersEXT(e->buf, sd->nb_descriptor_sets, sd->desc_bind);
21332191 /* Binding offsets */
21342192 vk->CmdSetDescriptorBufferOffsetsEXT(e->buf, shd->bind_point, shd->pipeline_layout,
2135− 0, shd->nb_descriptor_sets,
2193+ 0, sd->nb_descriptor_sets,
21362194 shd->bound_buffer_indices, offsets);
21372195 } else if (!shd->use_push) {
21382196 vk->CmdBindDescriptorSets(e->buf, shd->bind_point, shd->pipeline_layout,
2139− 0, shd->nb_descriptor_sets,
2140− &shd->desc_sets[e->idx*shd->nb_descriptor_sets],
2197+ 0, sd->nb_descriptor_sets,
2198+ &sd->desc_sets[e->idx*sd->nb_descriptor_sets],
21412199 0, NULL);
21422200 }
21432201 }
@@ -2163,9 +2221,6 @@ void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd) · 19 unmodified lines
21632221
21642222 for (int i = 0; i < shd->nb_descriptor_sets; i++) {
21652223 FFVulkanDescriptorSet *set = &shd->desc_set[i];
2166− if (set->buf.mem)
2167− ff_vk_unmap_buffer(s, &set->buf, 0);
2168− ff_vk_free_buf(s, &set->buf);
21692224 av_free(set->binding);
21702225 av_free(set->binding_offset);
21712226 }
@@ -2175,15 +2230,9 @@ void ff_vk_shader_free(FFVulkanContext *s, FFVulkanShader *shd) · 3 unmodified lines
21752230 vk->DestroyDescriptorSetLayout(s->hwctx->act_dev, shd->desc_layout[i],
21762231 s->hwctx->alloc);
21772232
2178− if (shd->desc_pool)
2179− vk->DestroyDescriptorPool(s->hwctx->act_dev, shd->desc_pool,
2180− s->hwctx->alloc);
2181−
21822233 av_freep(&shd->desc_pool_size);
21832234 av_freep(&shd->desc_layout);
2184− av_freep(&shd->desc_sets);
21852235 av_freep(&shd->desc_set);
2186− av_freep(&shd->desc_bind);
21872236 av_freep(&shd->bound_buffer_indices);
21882237 av_freep(&shd->push_consts);
21892238 shd->push_consts_num = 0;
libavutil/vulkan.h+50−31
@@ -162,31 +162,11 @@ typedef struct FFVkExecContext { · 161 unmodified lines
162162 unsigned int frame_update_alloc_size;
163163 } FFVkExecContext;
164164
165−typedef struct FFVkExecPool {
166− FFVkExecContext *contexts;
167− atomic_int_least64_t idx;
168−
169− VkCommandPool cmd_buf_pool;
170− VkCommandBuffer *cmd_bufs;
171− int pool_size;
172−
173− VkQueryPool query_pool;
174− void *query_data;
175− int query_results;
176− int query_statuses;
177− int query_64bit;
178− int query_status_stride;
179− int nb_queries;
180− size_t qd_size;
181−} FFVkExecPool;
182−
183165 typedef struct FFVulkanDescriptorSet {
184− FFVkBuffer buf;
185− uint8_t *desc_mem;
186− VkDeviceSize layout_size;
187− VkDeviceSize aligned_size; /* descriptorBufferOffsetAlignment */
188− VkDeviceSize total_size; /* Once registered to an exec context */
189− VkBufferUsageFlags usage;
166+ /* Descriptor buffer */
167+ VkDeviceSize layout_size;
168+ VkDeviceSize aligned_size; /* descriptorBufferOffsetAlignment */
169+ VkBufferUsageFlags usage;
190170
191171 VkDescriptorSetLayoutBinding *binding;
192172 VkDeviceSize *binding_offset;
@@ -225,23 +205,62 @@ typedef struct FFVulkanShader { · 32 unmodified lines
225205 VkPushConstantRange *push_consts;
226206 int push_consts_num;
227207
208+ /* Descriptor sets */
209+ FFVulkanDescriptorSet *desc_set;
210+ int nb_descriptor_sets;
211+
228212 /* Descriptor buffer */
229213 VkDescriptorSetLayout *desc_layout;
230− FFVulkanDescriptorSet *desc_set;
231− VkDescriptorBufferBindingInfoEXT *desc_bind;
232214 uint32_t *bound_buffer_indices;
233− int nb_descriptor_sets;
234215
235216 /* Descriptor pool */
236217 int use_push;
237− VkDescriptorSet *desc_sets;
238− VkDescriptorPool desc_pool;
239218 VkDescriptorPoolSize *desc_pool_size;
240219 int nb_desc_pool_size;
241− int total_desc_sets;
242− FFVkExecPool *assoc_pool;
243220 } FFVulkanShader;
244221
222+typedef struct FFVulkanDescriptorSetData {
223+ /* Descriptor buffer */
224+ FFVkBuffer buf;
225+ uint8_t *desc_mem;
226+} FFVulkanDescriptorSetData;
227+
228+typedef struct FFVulkanShaderData {
229+ /* Shader to which this data belongs to */
230+ FFVulkanShader *shd;
231+ int nb_descriptor_sets;
232+
233+ /* Descriptor buffer */
234+ FFVulkanDescriptorSetData *desc_set_buf;
235+ VkDescriptorBufferBindingInfoEXT *desc_bind;
236+
237+ /* Descriptor pools */
238+ VkDescriptorSet *desc_sets;
239+ VkDescriptorPool desc_pool;
240+} FFVulkanShaderData;
241+
242+typedef struct FFVkExecPool {
243+ FFVkExecContext *contexts;
244+ atomic_int_least64_t idx;
245+
246+ VkCommandPool cmd_buf_pool;
247+ VkCommandBuffer *cmd_bufs;
248+ int pool_size;
249+
250+ VkQueryPool query_pool;
251+ void *query_data;
252+ int query_results;
253+ int query_statuses;
254+ int query_64bit;
255+ int query_status_stride;
256+ int nb_queries;
257+ size_t qd_size;
258+
259+ /* Registered shaders' data */
260+ FFVulkanShaderData *reg_shd;
261+ int nb_reg_shd;
262+} FFVkExecPool;
263+
245264 typedef struct FFVulkanContext {
246265 const AVClass *class;
247266 void *log_parent;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t