From 1bb6caa5c8cb97f23f84ac9f3d4aa6c38b6d901c Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 11 Sep 2022 23:43:54 +0200 Subject: Fix materials --- include/gltf.hpp | 2 +- include/vk-api.hpp | 2 +- include/vk-renderer.hpp | 14 ++++++++------ shaders/compute.comp | 1 - src/gltf.cpp | 2 +- src/scene.cpp | 49 +++++++++++++++++++++++++------------------------ src/vk-api.cpp | 2 +- src/vk-renderer.cpp | 13 +++++++++---- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/include/gltf.hpp b/include/gltf.hpp index 59189d4..2f7377a 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -18,7 +18,7 @@ struct node { struct raw_image { int32_t x; int32_t y; - int32_t n; + int32_t channels; uint8_t *data; }; diff --git a/include/vk-api.hpp b/include/vk-api.hpp index 2318eff..b50e751 100644 --- a/include/vk-api.hpp +++ b/include/vk-api.hpp @@ -115,7 +115,7 @@ class vkapi { handle create_buffer(size_t data_size, VkBufferUsageFlags buffer_usage, uint32_t mem_usage); void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, size_t size); - void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, off_t buffer_offset = 0); + void copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, size_t size, size_t buffer_offset = 0); void destroy_buffer(handle buffer); diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp index 614569a..6c6d2f0 100644 --- a/include/vk-renderer.hpp +++ b/include/vk-renderer.hpp @@ -124,20 +124,22 @@ class vkrenderer { class RingBuffer { public: - static const off_t invalid_alloc = -1; + static const size_t invalid_alloc = -1; RingBuffer(size_t size); - [[nodiscard]]off_t alloc(size_t alloc_size) const { - assert(alloc_size > 0); + [[nodiscard]]size_t alloc(size_t alloc_size) { + assert(alloc_size > 0 && alloc_size <= buffer_size); - if (ptr + alloc_size < buffer_size) { + if (ptr <= buffer_size - alloc_size) { + ptr += alloc_size; return ptr; } + return invalid_alloc; } - void write(void* data, off_t alloc_offset, size_t data_size) const { + void write(void* data, size_t alloc_offset, size_t data_size) const { auto api_buffer = vkrenderer::api.get_buffer(device_buffer); std::memcpy((uint8_t*)api_buffer.device_ptr + alloc_offset, data, data_size); @@ -150,7 +152,7 @@ class RingBuffer { handle device_buffer; size_t buffer_size; - off_t ptr = 0; + size_t ptr = 0; }; class Sampler { diff --git a/shaders/compute.comp b/shaders/compute.comp index 0f85da6..92fa9db 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -16,7 +16,6 @@ layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; struct tex { uint texture_id; uint sampler_id; - uint[2] padding; }; struct material { diff --git a/src/gltf.cpp b/src/gltf.cpp index 64e5644..537e4e1 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -118,7 +118,7 @@ void gltf::load_textures(const std::filesystem::path &path) { const auto &gltf_image = gltf_images[image_index]; auto filepath = (path / gltf_image["uri"].get()).string(); auto& image = images[image_index]; - image.data = stbi_load(filepath.c_str(), &image.x, &image.y, &image.n, 4); + image.data = stbi_load(filepath.c_str(), &image.x, &image.y, &image.channels, 4); } // const auto &gltf_samplers = gltf_json["samplers"]; diff --git a/src/scene.cpp b/src/scene.cpp index dcbafe3..f560d03 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -118,6 +118,7 @@ scene::scene(const camera& cam, uint32_t width, uint32_t height) // Add offset to indices in order to have absolute indices for (const auto& submesh: mesh->get_submeshes()) { auto vertex_offset = vertices_offset + submesh.vertex_offset; + const auto& material = submesh.material; for (size_t element_index { 0U }; element_index < submesh.element_count; element_index++, index_offset += 3) { size_t submesh_level_index_1 = mesh_indices[index_offset]; @@ -136,31 +137,31 @@ scene::scene(const camera& cam, uint32_t width, uint32_t height) auto triangle_offset = triangles_offset * 3 + index_offset; - indices[triangle_offset] = (submesh_level_index_1 + vertex_offset); // | (0xff000000 & (materials.size() << 8)); - indices[triangle_offset + 1] = (submesh_level_index_2 + vertex_offset); // | (0xff000000 & (materials.size() << 16)); - indices[triangle_offset + 2] = (submesh_level_index_3 + vertex_offset); // | (0xff000000 & (materials.size() << 24)); - - const auto& material = submesh.material; - const auto& albedo_image = vkrenderer::api.get_image(material.base_color_texture->device_image); - const auto& albedo_sampler = vkrenderer::api.get_sampler(material.base_color_texture->sampler->device_sampler); - - materials.emplace_back(gpu_material { - .base_color = material.base_color, - .albedo_texture_id = albedo_image.bindless_sampled_index, - .albedo_texture_sampler_id = albedo_sampler.bindless_index, - .metalness = material.metalness, - .roughness = material.roughness - }); - - if (material.metallic_roughness_texture != nullptr) { - auto& gpu_material = materials.back(); - const auto& metallic_roughness_image = vkrenderer::api.get_image(material.metallic_roughness_texture->device_image); - const auto& metallic_roughness_sampler = vkrenderer::api.get_sampler(material.metallic_roughness_texture->sampler->device_sampler); - - gpu_material.metallic_roughness_texture_id = metallic_roughness_image.bindless_sampled_index; - gpu_material.metallic_roughness_texture_sampler_id = metallic_roughness_sampler.bindless_index; - } + indices[triangle_offset] = (submesh_level_index_1 + vertex_offset) | (0xff000000 & (materials.size() << 8)); + indices[triangle_offset + 1] = (submesh_level_index_2 + vertex_offset) | (0xff000000 & (materials.size() << 16)); + indices[triangle_offset + 2] = (submesh_level_index_3 + vertex_offset) | (0xff000000 & (materials.size() << 24)); } + + const auto& albedo_image = vkrenderer::api.get_image(material.base_color_texture->device_image); + const auto& albedo_sampler = vkrenderer::api.get_sampler(material.base_color_texture->sampler->device_sampler); + + materials.emplace_back(gpu_material { + .base_color = material.base_color, + .albedo_texture_id = albedo_image.bindless_sampled_index, + .albedo_texture_sampler_id = albedo_sampler.bindless_index, + .metalness = material.metalness, + .roughness = material.roughness + }); + + if (material.metallic_roughness_texture != nullptr) { + auto& gpu_material = materials.back(); + const auto& metallic_roughness_image = vkrenderer::api.get_image(material.metallic_roughness_texture->device_image); + const auto& metallic_roughness_sampler = vkrenderer::api.get_sampler(material.metallic_roughness_texture->sampler->device_sampler); + + gpu_material.metallic_roughness_texture_id = metallic_roughness_image.bindless_sampled_index; + gpu_material.metallic_roughness_texture_sampler_id = metallic_roughness_sampler.bindless_index; + } + } triangles_offset += mesh->triangle_count(); diff --git a/src/vk-api.cpp b/src/vk-api.cpp index cb9ec0a..60ab6d4 100644 --- a/src/vk-api.cpp +++ b/src/vk-api.cpp @@ -201,7 +201,7 @@ void vkapi::copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, size_t } // TODO: Change handle type, here we don't know what type of ressources we are working with -void vkapi::copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, off_t buffer_offset) { +void vkapi::copy_buffer(VkCommandBuffer cmd_buf, handle src, handle dst, size_t size, size_t buffer_offset) { auto& dst_image = images[dst]; VkImageSubresourceLayers image_subresource_layers = {}; diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index 1cbf0be..54c667f 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -106,26 +106,31 @@ void vkrenderer::queue_image_update(Texture* texture) { } void vkrenderer::update_images() { + if(upload_queue.empty()) + return; + auto* staging_buffer = staging_buffers[virtual_frame_index]; + staging_buffer->reset(); VkCommandBuffer command_buffer = copy_command_buffers[virtual_frame_index]; vkrenderer::api.start_record(command_buffer); while(!upload_queue.empty()) { auto* texture = upload_queue.back(); - upload_queue.pop_back(); auto texture_size = texture->size(); auto offset = staging_buffer->alloc(texture_size); - if (offset == RingBuffer::invalid_alloc) { + if (offset == RingBuffer::invalid_alloc) break; - } + + upload_queue.pop_back(); staging_buffer->write(texture->data, offset, texture_size); vkrenderer::api.image_barrier(command_buffer, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, texture->device_image); - vkrenderer::api.copy_buffer(command_buffer, staging_buffer->device_buffer, texture->device_image, offset); + vkrenderer::api.copy_buffer(command_buffer, staging_buffer->device_buffer, texture->device_image, 0, offset); vkrenderer::api.image_barrier(command_buffer, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, texture->device_image); + break; } vkrenderer::api.end_record(command_buffer); -- cgit v1.3