c/render: Refactor gfx mesh shader allocation and dispatch

This commit is contained in:
Jakob Bornecrantz 2023-10-06 21:56:38 +01:00
parent bc5727faf2
commit ff51653ffd
3 changed files with 94 additions and 44 deletions

View file

@ -810,11 +810,12 @@ do_gfx_mesh(struct comp_renderer *r,
VkResult ret; VkResult ret;
/* /*
* Reserve UBOs and fill in any data a head of time, if we ever want to * Reserve UBOs, create descriptor sets, and fill in any data a head of
* copy UBO data this lets us do that easily write a copy command before * time, if we ever want to copy UBO data this lets us do that easily
* the other gfx commands. * write a copy command before the other gfx commands.
*/ */
struct render_sub_alloc ubos[2];
VkDescriptorSet descriptor_sets[2] = XRT_STRUCT_INIT;
for (uint32_t i = 0; i < 2; i++) { for (uint32_t i = 0; i < 2; i++) {
struct render_gfx_mesh_ubo_data data = { struct render_gfx_mesh_ubo_data data = {
@ -822,13 +823,13 @@ do_gfx_mesh(struct comp_renderer *r,
.post_transform = src_norm_rects[i], .post_transform = src_norm_rects[i],
}; };
ret = render_sub_alloc_ubo_alloc_and_write( // ret = render_gfx_mesh_alloc_and_write( //
vk, // vk_bundle rr, //
&rr->ubo_tracker, // rsat &data, //
&data, // ptr src_samplers[i], //
sizeof(data), // size src_image_views[i], //
&ubos[i]); // out_rsa &descriptor_sets[i]); //
VK_CHK_WITH_GOTO(ret, "render_sub_alloc_ubo_alloc_and_write", err_no_memory); VK_CHK_WITH_GOTO(ret, "render_gfx_mesh_alloc", err_no_memory);
} }
@ -846,12 +847,10 @@ do_gfx_mesh(struct comp_renderer *r,
i, // view_index i, // view_index
&viewport_datas[i]); // viewport_data &viewport_datas[i]); // viewport_data
render_gfx_distortion( // render_gfx_mesh_draw( //
rr, // rr, // rr
i, // i, // mesh_index
&ubos[i], // descriptor_sets[i]); // descriptor_set
src_samplers[i], //
src_image_views[i]); //
render_gfx_end_view(rr); render_gfx_end_view(rr);
} }

View file

@ -714,35 +714,71 @@ render_gfx_end_view(struct render_gfx *rr)
assert(rr->rtr != NULL); assert(rr->rtr != NULL);
} }
XRT_CHECK_RESULT VkResult
render_gfx_mesh_alloc_and_write(struct render_gfx *rr,
const struct render_gfx_mesh_ubo_data *data,
VkSampler src_sampler,
VkImageView src_image_view,
VkDescriptorSet *out_descriptor_set)
{
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
struct render_sub_alloc ubo = XRT_STRUCT_INIT;
struct vk_bundle *vk = vk_from_rr(rr);
struct render_resources *r = rr->r;
VkResult ret;
/*
* Allocate and upload data.
*/
ret = render_sub_alloc_ubo_alloc_and_write( //
vk, // vk_bundle
&rr->ubo_tracker, // rsat
data, // ptr
sizeof(*data), // size
&ubo); // out_rsa
VK_CHK_AND_RET(ret, "render_sub_alloc_ubo_alloc_and_write");
/*
* Create and fill out destriptor.
*/
ret = vk_create_descriptor_set( //
vk, // vk_bundle
r->mesh.descriptor_pool, // descriptor_pool
r->mesh.descriptor_set_layout, // descriptor_set_layout
&descriptor_set); // descriptor_set
VK_CHK_AND_RET(ret, "vk_create_descriptor_set");
update_mesh_discriptor_set( //
vk, // vk_bundle
r->mesh.src_binding, // src_binding
src_sampler, // sampler
src_image_view, // image_view
r->mesh.ubo_binding, // ubo_binding
ubo.buffer, // buffer
ubo.offset, // offset
ubo.size, // size
descriptor_set); // descriptor_set
*out_descriptor_set = descriptor_set;
return VK_SUCCESS;
}
void void
render_gfx_distortion( render_gfx_mesh_draw(struct render_gfx *rr, uint32_t mesh_index, VkDescriptorSet descriptor_set)
struct render_gfx *rr, uint32_t view_index, struct render_sub_alloc *ubo, VkSampler sampler, VkImageView image_view)
{ {
struct vk_bundle *vk = vk_from_rr(rr); struct vk_bundle *vk = vk_from_rr(rr);
struct render_resources *r = rr->r; struct render_resources *r = rr->r;
assert(view_index == rr->current_view);
struct render_gfx_view *v = &rr->views[view_index];
VkDescriptorSet descriptor_set = v->mesh.descriptor_set;
/* /*
* Descriptors and pipeline. * Descriptors and pipeline.
*/ */
update_mesh_discriptor_set( //
vk, // vk_bundle
r->mesh.src_binding, // src_binding
sampler, // sampler
image_view, // image_view
r->mesh.ubo_binding, // ubo_binding
ubo->buffer, // buffer
ubo->offset, // offset
ubo->size, // size
descriptor_set); // descriptor_set
VkDescriptorSet descriptor_sets[1] = {descriptor_set}; VkDescriptorSet descriptor_sets[1] = {descriptor_set};
vk->vkCmdBindDescriptorSets( // vk->vkCmdBindDescriptorSets( //
r->cmd, // commandBuffer r->cmd, // commandBuffer
@ -789,9 +825,9 @@ render_gfx_distortion(
vk->vkCmdDrawIndexed( // vk->vkCmdDrawIndexed( //
r->cmd, // commandBuffer r->cmd, // commandBuffer
r->mesh.index_counts[view_index], // indexCount r->mesh.index_counts[mesh_index], // indexCount
1, // instanceCount 1, // instanceCount
r->mesh.index_offsets[view_index], // firstIndex r->mesh.index_offsets[mesh_index], // firstIndex
0, // vertexOffset 0, // vertexOffset
0); // firstInstance 0); // firstInstance
} else { } else {

View file

@ -891,16 +891,31 @@ void
render_gfx_end_view(struct render_gfx *rr); render_gfx_end_view(struct render_gfx *rr);
/*! /*!
* Dispatch one mesh shader instance into the current view. * Allocate needed resources for one mesh shader dispatch, will also update the
* descriptor set, ubo will be filled out with the given @p data argument.
*
* Uses the @ref render_sub_alloc_tracker of the @ref render_gfx and the
* descriptor pool of @ref render_resources, both of which will be reset once
* closed, so don't save any reference to these objects beyond the frame.
*
* @public @memberof render_gfx
*/
XRT_CHECK_RESULT VkResult
render_gfx_mesh_alloc_and_write(struct render_gfx *rr,
const struct render_gfx_mesh_ubo_data *data,
VkSampler src_sampler,
VkImageView src_image_view,
VkDescriptorSet *out_descriptor_set);
/*!
* Dispatch one mesh shader instance, using the give @p mesh_index as source for
* mesh geometry.
* *
* @public @memberof render_gfx * @public @memberof render_gfx
*/ */
void void
render_gfx_distortion(struct render_gfx *rr, render_gfx_mesh_draw(struct render_gfx *rr, uint32_t mesh_index, VkDescriptorSet descriptor_set);
uint32_t view_index,
struct render_sub_alloc *ubo,
VkSampler sampler,
VkImageView image_view);
/*! /*!
* @} * @}