Skip to content

Commit 466180e

Browse files
committed
[gltf] Setup gltf model descriptors.
Signed-off-by: Johannes <[email protected]>
1 parent 9dcb1fa commit 466180e

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

include/inexor/vulkan-renderer/gltf2/gltf_loader.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "inexor/vulkan-renderer/wrapper/device.hpp"
55
#include "inexor/vulkan-renderer/wrapper/gpu_texture.hpp"
66
#include "inexor/vulkan-renderer/wrapper/mesh_buffer.hpp"
7+
#include "inexor/vulkan-renderer/wrapper/uniform_buffer.hpp"
78

89
#include <glm/glm.hpp>
910

@@ -28,7 +29,7 @@ namespace inexor::vulkan_renderer::gltf2 {
2829
class Model {
2930

3031
const wrapper::Device &m_device;
31-
std::string m_name;
32+
std::string m_model_name;
3233
std::string m_file_name;
3334
tinygltf::Model m_gltf_model;
3435

@@ -79,6 +80,20 @@ class Model {
7980
std::vector<uint32_t> m_index_data;
8081
std::vector<ModelVertex> m_vertex_data;
8182

83+
const std::uint32_t m_swapchain_image_count;
84+
85+
struct ModelMatrixUBO {
86+
glm::mat4 projection;
87+
glm::mat4 model;
88+
glm::vec4 light = glm::vec4(5.0f, 5.0f, -5.0f, 1.0f);
89+
};
90+
91+
const ModelMatrixUBO m_matrices;
92+
93+
std::vector<wrapper::UniformBuffer> m_uniform_buffers;
94+
95+
std::vector<wrapper::ResourceDescriptor> m_descriptors;
96+
8297
void load_textures();
8398

8499
void load_texture_indices();
@@ -97,12 +112,15 @@ class Model {
97112
/// @brief
98113
void setup_descriptor_sets();
99114

115+
void create_mesh_buffer();
116+
100117
public:
101118
/// @brief
102119
/// @param device
103120
/// @param file_name
104121
/// @param model_name
105-
Model(const wrapper::Device &device, std::string &file_name, std::string &model_name);
122+
Model(const wrapper::Device &device, std::uint32_t swapchain_image_count, std::string &file_name,
123+
std::string &model_name);
106124

107125
~Model();
108126

src/vulkan-renderer/gltf2/gltf_loader.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include "inexor/vulkan-renderer/gltf2/gltf_loader.hpp"
2+
3+
#include "inexor/vulkan-renderer/wrapper/descriptor.hpp"
4+
#include "inexor/vulkan-renderer/wrapper/descriptor_builder.hpp"
5+
26
#include <glm/gtc/type_ptr.hpp>
37

48
namespace inexor::vulkan_renderer::gltf2 {
59

6-
Model::Model(const wrapper::Device &device, std::string &file_name, std::string &model_name)
7-
: m_device(device), m_name(model_name), m_file_name(file_name) {
10+
Model::Model(const wrapper::Device &device, std::uint32_t swapchain_image_count, std::string &file_name,
11+
std::string &model_name)
12+
: m_device(device), m_swapchain_image_count(swapchain_image_count), m_model_name(model_name),
13+
m_file_name(file_name) {
814
tinygltf::TinyGLTF gltf_context;
915

1016
std::string gltf_loader_error = "";
@@ -25,10 +31,9 @@ Model::Model(const wrapper::Device &device, std::string &file_name, std::string
2531
load_node(m_gltf_model.nodes[scene.nodes[i]], m_gltf_model, nullptr, m_index_data, m_vertex_data);
2632
}
2733

28-
// TODO: Implement!
2934
setup_descriptor_sets();
3035

31-
// TODO: Create mesh buffer!
36+
create_mesh_buffer();
3237
}
3338

3439
Model::~Model() {}
@@ -96,7 +101,6 @@ void Model::load_materials() {
96101

97102
void Model::load_node(const tinygltf::Node &inputNode, const tinygltf::Model &input, std::shared_ptr<ModelNode> parent,
98103
std::vector<uint32_t> &indexBuffer, std::vector<ModelVertex> &vertexBuffer) {
99-
100104
std::shared_ptr<ModelNode> node = std::make_shared<ModelNode>();
101105
node->matrix = glm::mat4(1.0f);
102106

@@ -245,6 +249,37 @@ void Model::load_node(const tinygltf::Node &inputNode, const tinygltf::Model &in
245249
}
246250
}
247251

252+
void Model::setup_descriptor_sets() {
253+
spdlog::debug("Setting up descriptor sets.");
254+
255+
m_uniform_buffers.emplace_back(m_device, "model matrices uniform buffer", sizeof(ModelMatrixUBO));
256+
257+
wrapper::DescriptorBuilder builder(m_device, m_swapchain_image_count);
258+
259+
std::string model_descriptor_name = "Descriptor for glTF model " + m_model_name;
260+
261+
const auto number_of_textures = m_textures.size();
262+
263+
// Gather parameters for descriptor builder.
264+
// It's important to pass the number of textures to the constructors to pre-allocate the required memory.
265+
std::vector<VkSampler> texture_images_sampler(number_of_textures);
266+
std::vector<VkImageView> texture_image_views(number_of_textures);
267+
268+
for (const auto &texture : m_textures) {
269+
texture_images_sampler.emplace_back(texture.sampler());
270+
texture_image_views.emplace_back(texture.image_view());
271+
}
272+
273+
const std::vector<std::uint32_t> texture_bindings(number_of_textures, 0);
274+
const std::vector<VkShaderStageFlagBits> texture_stage_flags(number_of_textures, VK_SHADER_STAGE_FRAGMENT_BIT);
275+
276+
m_descriptors.emplace_back(
277+
builder.add_uniform_buffer<ModelMatrixUBO>(m_uniform_buffers[0].buffer(), 0, VK_SHADER_STAGE_VERTEX_BIT)
278+
.add_combined_image_sampler_array(texture_images_sampler, texture_image_views, texture_bindings,
279+
texture_stage_flags)
280+
.build(model_descriptor_name));
281+
}
282+
248283
void Model::draw_node(VkCommandBuffer cmd_buffer, VkPipelineLayout layout, std::shared_ptr<ModelNode> node) {
249284
if (node->mesh.primitives.size() > 0) {
250285
// Pass the node's matrix via push constants

0 commit comments

Comments
 (0)