Combine render functions into single gpu_render()
This commit is contained in:
1 parent
bf2e49a1eb
commit
e7f4f4b51d
3 files changed
+62
-74
No files matched your search
@@ -795,8 +795,6 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
|
||||
ctx->dev, &transient_cmd_pool_config, nullptr,
|
||||
&ctx->transient_cmd_pool);
|
||||
assert(result == VK_SUCCESS);
|
||||
|
||||
ctx->active_img_index = UINT32_MAX;
|
||||
}
|
||||
|
||||
void gpu_cleanup(const gpu_ctx_t *ctx)
|
||||
@@ -957,7 +955,10 @@ void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model)
|
||||
vkFreeMemory(ctx->dev, model->vertex_mem, nullptr);
|
||||
}
|
||||
|
||||
void gpu_render_begin(gpu_ctx_t *ctx, vp_t vp)
|
||||
void gpu_render(
|
||||
const gpu_ctx_t *ctx, vp_t vp, size_t model_count,
|
||||
const gpu_model_t *models, const mat4_t **bone_matrices,
|
||||
const mat4_t *model_transforms)
|
||||
{
|
||||
VkResult result;
|
||||
|
||||
@@ -966,9 +967,10 @@ void gpu_render_begin(gpu_ctx_t *ctx, vp_t vp)
|
||||
vkResetFences(ctx->dev, 1, &ctx->in_flight);
|
||||
|
||||
// Aquire image from swapchain
|
||||
uint32_t curr_img;
|
||||
result = vkAcquireNextImageKHR(
|
||||
ctx->dev, ctx->swapchain, UINT64_MAX, ctx->img_avail, VK_NULL_HANDLE,
|
||||
&ctx->active_img_index);
|
||||
&curr_img);
|
||||
assert(result == VK_SUCCESS);
|
||||
|
||||
// Reset command buffer, then begin writing
|
||||
@@ -986,7 +988,7 @@ void gpu_render_begin(gpu_ctx_t *ctx, vp_t vp)
|
||||
VkRenderPassBeginInfo render_pass_begin = {
|
||||
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
||||
.renderPass = ctx->render_pass,
|
||||
.framebuffer = ctx->framebuffers[ctx->active_img_index],
|
||||
.framebuffer = ctx->framebuffers[curr_img],
|
||||
.renderArea.extent = ctx->swapchain_extent,
|
||||
.clearValueCount = 2,
|
||||
.pClearValues = clear_values,
|
||||
@@ -1002,40 +1004,31 @@ void gpu_render_begin(gpu_ctx_t *ctx, vp_t vp)
|
||||
0, sizeof(vp_t), &vp);
|
||||
vkCmdDraw(ctx->cmd_buf, 4, 1, 0, 0);
|
||||
|
||||
// Draw models
|
||||
vkCmdBindPipeline(
|
||||
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->model_pipeline);
|
||||
vkCmdPushConstants(
|
||||
ctx->cmd_buf, ctx->model_pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT,
|
||||
0, sizeof(vp_t), &vp);
|
||||
}
|
||||
for (unsigned i = 0; i < model_count; ++i) {
|
||||
models[i].transform_ptr[curr_img] = model_transforms[i];
|
||||
memcpy(
|
||||
models[i].bone_ptr + (models[i].bone_count * curr_img),
|
||||
bone_matrices[i], models[i].bone_count * sizeof(mat4_t));
|
||||
|
||||
void gpu_render_model(
|
||||
const gpu_ctx_t *ctx, const gpu_model_t *model,
|
||||
const mat4_t *bone_matrices, mat4_t transform)
|
||||
{
|
||||
assert(ctx->active_img_index < ctx->swapchain_img_count);
|
||||
vkCmdBindVertexBuffers(
|
||||
ctx->cmd_buf, 0, 1, &models[i].vertex_buf,
|
||||
&(VkDeviceSize) { 0 });
|
||||
vkCmdBindIndexBuffer(
|
||||
ctx->cmd_buf, models[i].index_buf, 0, VK_INDEX_TYPE_UINT16);
|
||||
|
||||
model->transform_ptr[ctx->active_img_index] = transform;
|
||||
memcpy(
|
||||
model->bone_ptr + (model->bone_count * ctx->active_img_index),
|
||||
bone_matrices, model->bone_count * sizeof(mat4_t));
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx->model_pipeline_layout, 0, 1, models[i].desc_sets + curr_img,
|
||||
0, nullptr);
|
||||
|
||||
vkCmdBindVertexBuffers(
|
||||
ctx->cmd_buf, 0, 1, &model->vertex_buf, &(VkDeviceSize) { 0 });
|
||||
vkCmdBindIndexBuffer(
|
||||
ctx->cmd_buf, model->index_buf, 0, VK_INDEX_TYPE_UINT16);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
||||
ctx->model_pipeline_layout, 0, 1,
|
||||
model->desc_sets + ctx->active_img_index, 0, nullptr);
|
||||
|
||||
vkCmdDrawIndexed(ctx->cmd_buf, model->index_count, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
void gpu_render_submit(gpu_ctx_t *ctx)
|
||||
{
|
||||
VkResult result;
|
||||
vkCmdDrawIndexed(ctx->cmd_buf, models[i].index_count, 1, 0, 0, 0);
|
||||
}
|
||||
|
||||
// Finish render pass and end writing to command buffer
|
||||
vkCmdEndRenderPass(ctx->cmd_buf);
|
||||
@@ -1065,10 +1058,8 @@ void gpu_render_submit(gpu_ctx_t *ctx)
|
||||
.pWaitSemaphores = &ctx->render_fin,
|
||||
.swapchainCount = 1,
|
||||
.pSwapchains = &ctx->swapchain,
|
||||
.pImageIndices = &ctx->active_img_index,
|
||||
.pImageIndices = &curr_img,
|
||||
};
|
||||
result = vkQueuePresentKHR(ctx->queue, &present_info);
|
||||
assert(result == VK_SUCCESS);
|
||||
|
||||
ctx->active_img_index = UINT32_MAX;
|
||||
}
|
||||
Reference in new issue
Block a user