summaryrefslogtreecommitdiff
path: root/src/vk
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2023-01-12 15:23:16 -0300
committerFrederico Linhares <fred@linhares.blue>2023-01-12 15:23:16 -0300
commit704858045b4f7defb977adb8344ae4428cd02081 (patch)
treea21dfcdbd7836b0a55bdeaf99001e615ada98e0a /src/vk
parent62367a0bcca75c45eb3e7d1582c4fff18827ddc7 (diff)
refa Use Push constants for 3D models
Diffstat (limited to 'src/vk')
-rw-r--r--src/vk/graphics_pipeline_3d.cpp57
-rw-r--r--src/vk/graphics_pipeline_3d_layout.cpp24
-rw-r--r--src/vk/model.cpp20
-rw-r--r--src/vk/uniform_data_object.hpp2
4 files changed, 43 insertions, 60 deletions
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<View3D> 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<VkDescriptorSet, 3> 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 <array>
#include "../core.hpp"
+#include "uniform_data_object.hpp"
namespace
{
@@ -106,21 +107,15 @@ load_descriptor_set_model_instance(void *obj)
{
auto self = static_cast<VK::GraphicsPipeline3DLayout*>(obj);
- std::array<VkDescriptorSetLayoutBinding, 2> layout_bindings{};
+ std::array<VkDescriptorSetLayoutBinding, 1> 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<VkPushConstantRange, 1> 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<VkWriteDescriptorSet, 2> write_descriptors{};
+ std::array<VkWriteDescriptorSet, 1> 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