Send view and project matrices in push constant
This commit is contained in:
1 parent
f3aa7435ec
commit
ea82627695
4 files changed
+67
-59
No files matched your search
@@ -540,10 +540,17 @@ void gpu_init(gpu_ctx_t *ctx, GLFWwindow *window, const char *name)
|
||||
};
|
||||
|
||||
// Create pipeline layout
|
||||
const VkPushConstantRange push_constant_range = {
|
||||
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
|
||||
.offset = 0,
|
||||
.size = sizeof(vp_t),
|
||||
};
|
||||
const VkPipelineLayoutCreateInfo pipeline_layout_config = {
|
||||
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
|
||||
.setLayoutCount = 1,
|
||||
.pSetLayouts = &ctx->desc_set_layout,
|
||||
.pushConstantRangeCount = 1,
|
||||
.pPushConstantRanges = &push_constant_range,
|
||||
};
|
||||
result = vkCreatePipelineLayout(
|
||||
ctx->dev, &pipeline_layout_config, nullptr, &ctx->pipeline_layout);
|
||||
@@ -807,15 +814,16 @@ void gpu_model_load(
|
||||
ctx->dev, &desc_sets_alloc_config, out->desc_sets);
|
||||
assert(result == VK_SUCCESS);
|
||||
|
||||
// Create and map MVP buffer
|
||||
const size_t mvp_size = ctx->swapchain_img_count * sizeof(mvp_t);
|
||||
// Create and map transform buffer
|
||||
const size_t transform_size = ctx->swapchain_img_count * sizeof(mat4_t);
|
||||
create_buffer(
|
||||
ctx, mvp_size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
ctx, transform_size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
|
||||
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
||||
&out->mvp_buf, &out->mvp_mem);
|
||||
&out->transform_buf, &out->transform_mem);
|
||||
vkMapMemory(
|
||||
ctx->dev, out->mvp_mem, 0, mvp_size, 0, (void **)&out->mvp_ptr);
|
||||
ctx->dev, out->transform_mem, 0, transform_size, 0,
|
||||
(void **)&out->transform_ptr);
|
||||
|
||||
// Create and map bone matrix buffer
|
||||
const size_t bone_size
|
||||
@@ -828,7 +836,7 @@ void gpu_model_load(
|
||||
vkMapMemory(
|
||||
ctx->dev, out->bone_mem, 0, bone_size, 0, (void **)&out->bone_ptr);
|
||||
|
||||
// Configure descriptor sets to use MVP and bone matrix buffers
|
||||
// Configure descriptor sets to use transform and bone matrix buffers
|
||||
for (unsigned i = 0; i < ctx->swapchain_img_count; ++i) {
|
||||
const VkWriteDescriptorSet desc_writes[] = {
|
||||
{
|
||||
@@ -839,9 +847,9 @@ void gpu_model_load(
|
||||
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
||||
.descriptorCount = 1,
|
||||
.pBufferInfo = &(VkDescriptorBufferInfo) {
|
||||
.buffer = out->mvp_buf,
|
||||
.offset = i * sizeof(mvp_t),
|
||||
.range = sizeof(mvp_t),
|
||||
.buffer = out->transform_buf,
|
||||
.offset = i * sizeof(mat4_t),
|
||||
.range = sizeof(mat4_t),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -870,8 +878,8 @@ void gpu_model_cleanup(const gpu_ctx_t *ctx, const gpu_model_t *model)
|
||||
vkDestroyBuffer(ctx->dev, model->bone_buf, nullptr);
|
||||
vkFreeMemory(ctx->dev, model->bone_mem, nullptr);
|
||||
|
||||
vkDestroyBuffer(ctx->dev, model->mvp_buf, nullptr);
|
||||
vkFreeMemory(ctx->dev, model->mvp_mem, nullptr);
|
||||
vkDestroyBuffer(ctx->dev, model->transform_buf, nullptr);
|
||||
vkFreeMemory(ctx->dev, model->transform_mem, nullptr);
|
||||
|
||||
vkDestroyBuffer(ctx->dev, model->index_buf, nullptr);
|
||||
vkFreeMemory(ctx->dev, model->index_mem, nullptr);
|
||||
@@ -879,7 +887,7 @@ 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)
|
||||
void gpu_render_begin(gpu_ctx_t *ctx, vp_t vp)
|
||||
{
|
||||
VkResult result;
|
||||
|
||||
@@ -901,7 +909,6 @@ void gpu_render_begin(gpu_ctx_t *ctx)
|
||||
result = vkBeginCommandBuffer(ctx->cmd_buf, &cmd_begin);
|
||||
assert(result == VK_SUCCESS);
|
||||
|
||||
// Start render pass
|
||||
VkClearValue clear_values[] = {
|
||||
[0] = { .color = { { 0.0, 0.0, 0.0, 1.0 } } },
|
||||
[1] = { .depthStencil = { 1.0, 0.0 } },
|
||||
@@ -918,15 +925,19 @@ void gpu_render_begin(gpu_ctx_t *ctx)
|
||||
ctx->cmd_buf, &render_pass_begin, VK_SUBPASS_CONTENTS_INLINE);
|
||||
vkCmdBindPipeline(
|
||||
ctx->cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, ctx->pipeline);
|
||||
|
||||
vkCmdPushConstants(
|
||||
ctx->cmd_buf, ctx->pipeline_layout, VK_SHADER_STAGE_VERTEX_BIT, 0,
|
||||
sizeof(vp_t), &vp);
|
||||
}
|
||||
|
||||
void gpu_render_model(
|
||||
const gpu_ctx_t *ctx, const gpu_model_t *model, const mvp_t *mvp,
|
||||
const mat4_t *bone_matrices)
|
||||
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);
|
||||
|
||||
memcpy(model->mvp_ptr + ctx->active_img_index, mvp, sizeof(mvp_t));
|
||||
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));
|
||||
|
||||
Reference in new issue
Block a user