From 704858045b4f7defb977adb8344ae4428cd02081 Mon Sep 17 00:00:00 2001 From: Frederico Linhares Date: Thu, 12 Jan 2023 15:23:16 -0300 Subject: refa Use Push constants for 3D models --- src/vk/graphics_pipeline_3d.cpp | 57 +++++++++++++++------------------- src/vk/graphics_pipeline_3d_layout.cpp | 24 +++++++------- src/vk/model.cpp | 20 +++--------- src/vk/uniform_data_object.hpp | 2 +- 4 files changed, 43 insertions(+), 60 deletions(-) (limited to 'src/vk') diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp index f915c00..4eacc9d 100644 --- a/src/vk/graphics_pipeline_3d.cpp +++ b/src/vk/graphics_pipeline_3d.cpp @@ -538,8 +538,7 @@ GraphicsPipeline3D::draw( std::shared_ptr view, const VkCommandBuffer draw_command_buffer, const size_t current_frame, const uint32_t image_index) { - // Set viewport - { + { // Set viewport VkViewport vk_viewport{}; vk_viewport.x = view->region.x; vk_viewport.y = view->region.y; @@ -561,8 +560,6 @@ GraphicsPipeline3D::draw( for(auto& [model, instances]: cg_core.vk_renderer->models_to_draw[current_frame]) { - // Commands - { std::array vk_descriptor_sets{ this->descriptor_sets_world[image_index], view->descriptor_sets_3d[image_index], @@ -582,34 +579,31 @@ GraphicsPipeline3D::draw( vkCmdBindIndexBuffer( draw_command_buffer, model->mesh->index_buffer->buffer, 0, VK_INDEX_TYPE_UINT32); - vkCmdDrawIndexed( - draw_command_buffer, model->mesh->index_count, instances.size(), - 0, 0, 0); - } - - VK::ODOModelInstance ubo_model_instance; - - for(int i{0}; i < instances.size(); i++) - { - // Object matrix. - glm::mat4 instance_matrix{1.0f}; - instance_matrix = glm::translate( - instance_matrix, instances[i].position); - instance_matrix = glm::rotate( - instance_matrix, instances[i].rotation.x, glm::vec3{1.0, 0.0, 0.0}); - instance_matrix = glm::rotate( - instance_matrix, instances[i].rotation.y, glm::vec3{0.0, 1.0, 0.0}); - instance_matrix = glm::rotate( - instance_matrix, instances[i].rotation.z, glm::vec3{0.0, 0.0, 1.0}); - - ubo_model_instance.instances[i] = instance_matrix; - } - - model->ub_model_instance[image_index].copy_data(&ubo_model_instance); + + for(auto &instance: instances) + { // Object matrix. + glm::mat4 instance_matrix{1.0f}; + instance_matrix = glm::translate( + instance_matrix, instance.position); + instance_matrix = glm::rotate( + instance_matrix, instance.rotation.x, glm::vec3{1.0, 0.0, 0.0}); + instance_matrix = glm::rotate( + instance_matrix, instance.rotation.y, glm::vec3{0.0, 1.0, 0.0}); + instance_matrix = glm::rotate( + instance_matrix, instance.rotation.z, glm::vec3{0.0, 0.0, 1.0}); + + ODOModelInstance model_instance{instance_matrix}; + vkCmdPushConstants( + draw_command_buffer, + cg_core.vk_graphics_pipeline_3d_layout->pipeline, + VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ODOModelInstance), + &model_instance); + vkCmdDrawIndexed( + draw_command_buffer, model->mesh->index_count, 1, 0, 0, 0); + } } - // Update view uniform buffers - { + { // Update view uniform buffers VK::ODOView3D ubo_view_3d{}; // View matrix. @@ -633,8 +627,7 @@ GraphicsPipeline3D::draw( view->ub_3d[image_index].copy_data(&ubo_view_3d); } - // Update world uniform buffer - { + { // Update world uniform buffer ODOWorld3D_Vert ubo_world_3d_vert{}; ubo_world_3d_vert.ambient_light_color = glm::vec4{0.25, 0.25, 0.25, 1.0}; this->ub_world_vert[image_index].copy_data(&ubo_world_3d_vert); diff --git a/src/vk/graphics_pipeline_3d_layout.cpp b/src/vk/graphics_pipeline_3d_layout.cpp index 4374ab9..db5d210 100644 --- a/src/vk/graphics_pipeline_3d_layout.cpp +++ b/src/vk/graphics_pipeline_3d_layout.cpp @@ -19,6 +19,7 @@ #include #include "../core.hpp" +#include "uniform_data_object.hpp" namespace { @@ -106,21 +107,15 @@ load_descriptor_set_model_instance(void *obj) { auto self = static_cast(obj); - std::array layout_bindings{}; + std::array layout_bindings{}; layout_bindings[0].binding = 0; - layout_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + layout_bindings[0].descriptorType = + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; layout_bindings[0].descriptorCount = 1; - layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; layout_bindings[0].pImmutableSamplers = nullptr; - layout_bindings[1].binding = 1; - layout_bindings[1].descriptorType = - VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - layout_bindings[1].descriptorCount = 1; - layout_bindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; - layout_bindings[1].pImmutableSamplers = nullptr; - VkDescriptorSetLayoutCreateInfo layout_info{}; layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; layout_info.pNext = nullptr; @@ -155,12 +150,17 @@ load_pipeline(void *obj) self->descriptor_set_view, self->descriptor_set_model_instance}; + std::array push_constants; + push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + push_constants[0].offset = 0; + push_constants[0].size = sizeof(VK::ODOModelInstance); + VkPipelineLayoutCreateInfo pipeline_layout_info{}; pipeline_layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipeline_layout_info.setLayoutCount = set_layouts.size(); pipeline_layout_info.pSetLayouts = set_layouts.data(); - pipeline_layout_info.pushConstantRangeCount = 0; - pipeline_layout_info.pPushConstantRanges = nullptr; + pipeline_layout_info.pushConstantRangeCount = push_constants.size(); + pipeline_layout_info.pPushConstantRanges = push_constants.data(); if(vkCreatePipelineLayout( cg_core.vk_device_with_swapchain->device, diff --git a/src/vk/model.cpp b/src/vk/model.cpp index 9f3b373..4f63ddb 100644 --- a/src/vk/model.cpp +++ b/src/vk/model.cpp @@ -118,27 +118,17 @@ load_buffers_to_descriptor_sets(void *obj) image_info.imageView = self->texture->view; image_info.sampler = self->texture->sampler; - std::array write_descriptors{}; + std::array write_descriptors{}; write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; write_descriptors[0].dstSet = self->descriptor_sets[i]; write_descriptors[0].dstBinding = 0; write_descriptors[0].dstArrayElement = 0; write_descriptors[0].descriptorCount = 1; - write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write_descriptors[0].pBufferInfo = &buffer_info; - write_descriptors[0].pImageInfo = nullptr; - write_descriptors[0].pTexelBufferView = nullptr; - - write_descriptors[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write_descriptors[1].dstSet = self->descriptor_sets[i]; - write_descriptors[1].dstBinding = 1; - write_descriptors[1].dstArrayElement = 0; - write_descriptors[1].descriptorCount = 1; - write_descriptors[1].descriptorType = + write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - write_descriptors[1].pBufferInfo = nullptr; - write_descriptors[1].pImageInfo = &image_info; - write_descriptors[1].pTexelBufferView = nullptr; + write_descriptors[0].pBufferInfo = nullptr; + write_descriptors[0].pImageInfo = &image_info; + write_descriptors[0].pTexelBufferView = nullptr; vkUpdateDescriptorSets( cg_core.vk_device_with_swapchain->device, write_descriptors.size(), diff --git a/src/vk/uniform_data_object.hpp b/src/vk/uniform_data_object.hpp index d040e4e..102b82d 100644 --- a/src/vk/uniform_data_object.hpp +++ b/src/vk/uniform_data_object.hpp @@ -46,7 +46,7 @@ struct ODOWorld3D_Frag struct ODOModelInstance { - glm::mat4 instances[128]; + glm::mat4 matrix; }; struct ODOVector4D -- cgit v1.2.3