#include "scene.hpp" #include #include #ifdef API_TEST #include #else #include "vk-renderer.hpp" #endif // API_TEST #include "gltf.hpp" #include "bvh.hpp" // #include "material.hpp" // void scene::random_scene() { // material ground_material = { { 0.5, 0.5, 0.5 } }; // spheres.emplace_back(vec3 { 0, -1000, 0 }, ground_material, 1000); // // int32_t spheres_count = 11; // for (int a = -spheres_count; a < spheres_count; a++) { // for (int b = -spheres_count; b < spheres_count; b++) { // auto choose_mat = randd(); // point3 center(a + 0.9 * randd(), 0.2, b + 0.9 * randd()); // // if ((center - point3{ 4, 0.2, 0 }).length() > 0.9) { // material sphere_material = {}; // // if (choose_mat < 0.8) { // // diffuse // sphere_material.base_color = color::random() * color::random(); // } else if (choose_mat < 0.95) { // // metal // sphere_material.base_color = color::random(0.5, 1); // sphere_material.metalness = randd(0, 0.5); // sphere_material.roughness = 0.f; // } else { // // glass // // sphere_material.ior = 1.5; // sphere_material.metalness = 1.f; // sphere_material.roughness = 0.f; // } // // spheres.emplace_back(center, sphere_material, 0.2); // } // } // } // // material material1 = {}; // // material1.ior = 1.5; // spheres.emplace_back(vec3 { 0, 1, 0 }, material1, 1.0); // // material material2 = {}; // material2.base_color = color{ 0.4, 0.2, 0.1 }; // spheres.emplace_back(vec3 { -4, 1, 0 }, material2, 1.0); // // material material3 = {}; // material3.base_color = color{ 0.7, 0.6, 0.5 }; // material3.roughness = 0.0; // spheres.emplace_back(vec3 { 4, 1, 0 }, material3, 1.0); // // spheres_count = spheres.size(); // } scene::metadata::metadata(const camera &cam, uint32_t width, uint32_t height) : cam(cam), width(width), height(height) {} scene::scene(const camera& cam, uint32_t width, uint32_t height) :meta(cam, width, height){ #ifdef API_TEST auto& device = vkdevice::get_render_device(); #endif // Geometry & BVH std::vector indices; std::vector positions; std::vector normals; std::vector uvs; std::vector materials; std::vector packed_nodes; // auto root_node = gltf::load("../models/BistroInterior", "BistroInterior.gltf"); auto gltf_model = gltf("../models/sponza/Sponza.gltf"); std::vector triangles; size_t triangles_offset = 0; size_t vertices_offset = 0; std::queue nodes_to_load {}; nodes_to_load.push(gltf_model.root_node); while(!nodes_to_load.empty()) { auto &node = nodes_to_load.front(); if (node.mesh != nullptr) { const auto* mesh = node.mesh; const auto& mesh_positions = mesh->get_attribute(ATTRIBUTE_TYPE::POSITION); const auto& mesh_normals = mesh->get_attribute(ATTRIBUTE_TYPE::NORMAL); const auto& mesh_uvs_0 = mesh->get_attribute(ATTRIBUTE_TYPE::UV_0); const auto& mesh_indices = mesh->get_indices(); indices.resize(indices.size() + mesh_indices.size()); positions.resize(vertices_offset + mesh_positions.size()); normals.resize(vertices_offset + mesh_normals.size()); uvs.resize(vertices_offset + mesh_uvs_0.size()); std::memcpy( positions.data() + vertices_offset * Mesh::attribute_components(ATTRIBUTE_TYPE::POSITION), mesh_positions.data(), mesh_positions.size() * sizeof(float) ); std::memcpy( normals.data() + vertices_offset * Mesh::attribute_components(ATTRIBUTE_TYPE::NORMAL), mesh_normals.data(), mesh_normals.size() * sizeof(float) ); std::memcpy( uvs.data() + vertices_offset * Mesh::attribute_components(ATTRIBUTE_TYPE::UV_0), mesh_uvs_0.data(), mesh_uvs_0.size() * sizeof(float) ); auto index_offset { 0U }; // 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]; size_t submesh_level_index_2 = mesh_indices[index_offset + 1]; size_t submesh_level_index_3 = mesh_indices[index_offset + 2]; size_t mesh_level_index_1 = (submesh.vertex_offset + submesh_level_index_1) * 3; size_t mesh_level_index_2 = (submesh.vertex_offset + submesh_level_index_2) * 3; size_t mesh_level_index_3 = (submesh.vertex_offset + submesh_level_index_3) * 3; triangles.emplace_back( vec3 { mesh_positions[mesh_level_index_1], mesh_positions[mesh_level_index_1 + 1], mesh_positions[mesh_level_index_1 + 2] }, vec3 { mesh_positions[mesh_level_index_2], mesh_positions[mesh_level_index_2 + 1], mesh_positions[mesh_level_index_2 + 2] }, vec3 { mesh_positions[mesh_level_index_3], mesh_positions[mesh_level_index_3 + 1], mesh_positions[mesh_level_index_3 + 2] } ); auto triangle_offset = triangles_offset * 3 + index_offset; indices[triangle_offset] = static_cast((submesh_level_index_1 + vertex_offset) | (0xff000000 & (materials.size() << 8))); indices[triangle_offset + 1] = static_cast((submesh_level_index_2 + vertex_offset) | (0xff000000 & (materials.size() << 16))); indices[triangle_offset + 2] = static_cast((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); auto& mat = 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 }); #ifdef API_TEST if (material.base_color_texture.id != handle::invalid_id) { mat.albedo_texture_id = device.get_texture(material.base_color_texture).get_sampled_index(); // mat.albedo_texture_sampler_id = device.get_sampler(0); } if (material.metallic_roughness_texture.id != handle::invalid_id) { mat.metallic_roughness_texture_id = device.get_texture(material.metallic_roughness_texture).get_sampled_index(); // mat.metallic_roughness_texture_sampler_id = device.get_sampler(0); } #else 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; } #endif } triangles_offset += mesh->triangle_count(); vertices_offset += mesh->vertices_count(); } for (auto &new_node: node.children) { nodes_to_load.push(new_node); } nodes_to_load.pop(); } // random_scene(); bvh builder(triangles, packed_nodes); // bvh builder(spheres, packed_nodes); #ifdef API_TEST { scene_buffer_handle = device.create_buffer({ .size = sizeof(meta) * 3U, .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& scene_buffer = device.get_buffer(scene_buffer_handle); uint8_t* ptr = static_cast(scene_buffer.mapped_ptr); memcpy(static_cast(ptr), &meta, sizeof(meta)); memcpy(static_cast(ptr + sizeof(meta)), &meta, sizeof(meta)); memcpy(static_cast(ptr + sizeof(meta) * 2U), &meta, sizeof(meta)); } { indices_buffer_handle = device.create_buffer({ .size = indices.size() * sizeof(indices[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& indices_buffer = device.get_buffer(indices_buffer_handle); void* ptr = indices_buffer.mapped_ptr; memcpy(ptr, indices.data(), indices.size() * sizeof(indices[0])); } { positions_buffer_handle = device.create_buffer({ .size = positions.size() * sizeof(positions[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& positions_buffer = device.get_buffer(positions_buffer_handle); void* ptr = positions_buffer.mapped_ptr; memcpy(ptr, positions.data(), positions.size() * sizeof(positions[0])); } { normals_buffer_handle = device.create_buffer({ .size = normals.size() * sizeof(normals[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& normals_buffer = device.get_buffer(normals_buffer_handle); void* ptr = normals_buffer.mapped_ptr; memcpy(ptr, normals.data(), normals.size() * sizeof(normals[0])); } { uvs_buffer_handle = device.create_buffer({ .size = uvs.size() * sizeof(uvs[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& uvs_buffer = device.get_buffer(uvs_buffer_handle); void* ptr = uvs_buffer.mapped_ptr; memcpy(ptr, uvs.data(), uvs.size() * sizeof(uvs[0])); } { bvh_buffer_handle = device.create_buffer({ .size = packed_nodes.size() * sizeof(packed_nodes[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& bvh_buffer = device.get_buffer(bvh_buffer_handle); void* ptr = bvh_buffer.mapped_ptr; memcpy(ptr, packed_nodes.data(), packed_nodes.size() * sizeof(packed_nodes[0])); } { materials_buffer_handle = device.create_buffer({ .size = materials.size() * sizeof(materials[0]), .usages = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, .memory_properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, .memory_usage = VMA_MEMORY_USAGE_CPU_TO_GPU, }); const auto& materials_buffer = device.get_buffer(materials_buffer_handle); void* ptr = materials_buffer.mapped_ptr; memcpy(ptr, materials.data(), materials.size() * sizeof(materials[0])); } #else scene_buffer = vkrenderer::create_buffer(sizeof(meta) * vkrenderer::virtual_frames_count); scene_buffer->write(&meta, 0, sizeof(meta)); scene_buffer->write(&meta, sizeof(meta), sizeof(meta)); scene_buffer->write(&meta, sizeof(meta) * 2, sizeof(meta)); indices_buffer = vkrenderer::create_buffer(indices.size() * sizeof(indices[0])); indices_buffer->write(indices.data(), 0, indices.size() * sizeof(indices[0])); positions_buffer = vkrenderer::create_buffer(positions.size() * sizeof(positions[0])); positions_buffer->write(positions.data(), 0, positions.size() * sizeof(positions[0])); normals_buffer = vkrenderer::create_buffer(normals.size() * sizeof(normals[0])); normals_buffer->write(normals.data(), 0, normals.size() * sizeof(normals[0])); uvs_buffer = vkrenderer::create_buffer(uvs.size() * sizeof(uvs[0])); uvs_buffer->write(uvs.data(), 0, uvs.size() * sizeof(uvs[0])); bvh_buffer = vkrenderer::create_buffer(packed_nodes.size() * sizeof(packed_nodes[0])); bvh_buffer->write(packed_nodes.data(), 0, packed_nodes.size() * sizeof(packed_nodes[0])); materials_buffer = vkrenderer::create_buffer(materials.size() * sizeof(materials[0])); materials_buffer->write(materials.data(), 0, materials.size() * sizeof(materials[0])); #endif // API_TEST }