From 5692f616c6201b39184e4353b6a4cb850a84a450 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Wed, 15 Jun 2022 23:02:29 +0200 Subject: Fix formatting and linting --- CMakeLists.txt | 5 +- include/aabb.hpp | 16 ++-- include/gltf.hpp | 32 ++++---- include/scene.hpp | 5 +- include/utils.hpp | 6 +- include/vec3.hpp | 1 + src/gltf.cpp | 85 ++++++++++----------- src/main.cpp | 215 ++++++++++++++++++++++++++++++------------------------ src/scene.cpp | 26 +++---- src/utils.cpp | 2 +- src/vec3.cpp | 111 ++++++++++++---------------- 11 files changed, 253 insertions(+), 251 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b18938..d884e9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.10) -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -10,7 +10,8 @@ project(path-tracer VERSION 1.0 LANGUAGES CXX) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") add_compile_options( - -msse4.1 -fcolor-diagnostics -fansi-escape-codes # colored output when using ninja + -msse4.1 + -fcolor-diagnostics -fansi-escape-codes # colored output when using ninja # https://github.com/cpp-best-practices/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#gcc--clang -Wall -Wextra diff --git a/include/aabb.hpp b/include/aabb.hpp index 475c04e..85b3c21 100644 --- a/include/aabb.hpp +++ b/include/aabb.hpp @@ -4,7 +4,7 @@ #include "vec3.hpp" #include "triangle.hpp" #include -#include +#include class aabb { public: @@ -36,27 +36,27 @@ class aabb { maximum.max(point); } - int32_t maximum_axis() { + int32_t maximum_axis() const { vec3 diag = diagonal(); if (diag.v[0] > diag.v[1] && diag.v[0] > diag.v[2]) { return 0; - } else if (diag.v[1] > diag.v[2]) { + } + if (diag.v[1] > diag.v[2]) { return 1; - } else { - return 2; } + return 2; } - vec3 diagonal() { + vec3 diagonal() const { return maximum - minimum; } - vec3 center() { + vec3 center() const { return diagonal() / 2.f; } - float surface_area() { + float surface_area() const { auto diag = diagonal(); return 2.f * (diag.v[0] * diag.v[1] + diag.v[0] * diag.v[2] + diag.v[1] * diag.v[2]); } diff --git a/include/gltf.hpp b/include/gltf.hpp index 7df5e7a..0364ca8 100644 --- a/include/gltf.hpp +++ b/include/gltf.hpp @@ -10,46 +10,46 @@ using json = nlohmann::json; #include "material.hpp" -#include "vec3.hpp" #include "vk-renderer.hpp" struct mesh_part { - std::vector indices; - std::vector positions; - std::vector normals; - std::vector uvs; - material mat; - uint32_t triangles_count = 0; - uint32_t vertices_count = 0; + std::vector indices; + std::vector positions; + std::vector normals; + std::vector uvs; + material mat; + uint32_t triangles_count = 0; + uint32_t vertices_count = 0; }; struct mesh { std::vector parts; - uint32_t triangles_count = 0; - uint32_t vertices_count = 0; + uint32_t triangles_count = 0; + uint32_t vertices_count = 0; }; struct node { - std::vector children; + std::vector children; std::optional mesh; }; class gltf { -public: + public: static node load(std::filesystem::path &path, std::string &filename, vkrenderer &renderer); -private: - + private: static void load_node(json &gltf_json, uint32_t index, node &parent, std::vector &meshes); - static std::vector load_meshes(json &gltf_json, std::vector> &buffers_content, std::vector &materials); + static std::vector load_meshes( + json &gltf_json, std::vector> &buffers_content, std::vector &materials); static std::vector load_textures(json &gltf_json, std::filesystem::path &path, vkrenderer &renderer); static std::vector load_materials(json &gltf_json, std::vector &textures); - static mesh_part load_primitive(json &gltf_json, json &primitive, std::vector> &buffers_content, std::vector &materials); + static mesh_part load_primitive(json &gltf_json, json &primitive, + std::vector> &buffers_content, std::vector &materials); }; #endif diff --git a/include/scene.hpp b/include/scene.hpp index 4bce1a7..8e47ab0 100644 --- a/include/scene.hpp +++ b/include/scene.hpp @@ -1,9 +1,6 @@ #ifndef __SCENE_HPP_ #define __SCENE_HPP_ -#include "vec3.hpp" -#include "sphere.hpp" -#include "triangle.hpp" #include "camera.hpp" #include "bvh-node.hpp" @@ -26,7 +23,7 @@ class scene { int32_t downscale_factor = 1; metadata(camera cam, uint32_t width, uint32_t height) - : cam(cam), width(width), height(height) {} + : cam(std::move(cam)), width(width), height(height) {} }; // void random_scene(); diff --git a/include/utils.hpp b/include/utils.hpp index 8548a5f..201976f 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -12,11 +12,11 @@ std::vector read_file(std::string& path); #define EPSILON 0.000001 inline float deg_to_rad(float deg) { - return deg * PI / 180.0; + return deg * PI / 180.f; } inline float randd() { - return rand() / (RAND_MAX + 1.0); + return rand() / (RAND_MAX + 1.f); } inline float randd(float min, float max) { @@ -24,7 +24,7 @@ inline float randd(float min, float max) { } inline int32_t randi(int32_t min, int32_t max) { - return static_cast(randd(min, max + 1)); + return (int32_t)(randd(min, max + 1.f)); } inline float clamp(float x, float min, float max) { diff --git a/include/vec3.hpp b/include/vec3.hpp index 429d747..0250345 100644 --- a/include/vec3.hpp +++ b/include/vec3.hpp @@ -13,6 +13,7 @@ class vec3 { vec3(__m128& v); vec3(__m128&& v); vec3(const vec3& vec); + vec3(const vec3&& vec); vec3 operator-() const; diff --git a/src/gltf.cpp b/src/gltf.cpp index 2f37a3c..982e76d 100644 --- a/src/gltf.cpp +++ b/src/gltf.cpp @@ -1,3 +1,4 @@ + #include "gltf.hpp" #include @@ -19,7 +20,7 @@ node gltf::load(std::filesystem::path &path, std::string &filename, vkrenderer & auto &buffers = gltf_json["buffers"]; auto buffer_index = 0; auto buffers_content = std::vector>(buffers.size()); - for (auto &buffer: buffers) { + for (auto &buffer : buffers) { auto buffer_path = (path / buffer["uri"].get()).string(); buffers_content[buffer_index] = read_file(buffer_path); buffer_index++; @@ -39,7 +40,7 @@ node gltf::load(std::filesystem::path &path, std::string &filename, vkrenderer & load_node(gltf_json, root_children[child_index].get(), root_node.children[child_index], meshes); } - return std::move(root_node); + return root_node; } void gltf::load_node(json &gltf_json, uint32_t index, node &parent, std::vector &meshes) { @@ -55,54 +56,54 @@ void gltf::load_node(json &gltf_json, uint32_t index, node &parent, std::vector< } } - if (gltf_node.contains("mesh")) { - parent.mesh = std::make_optional(meshes[gltf_node["mesh"].get()]); - } + if (gltf_node.contains("mesh")) { parent.mesh = std::make_optional(meshes[gltf_node["mesh"].get()]); } } -std::vector gltf::load_meshes(json &gltf_json, std::vector> &buffers_content, std::vector &materials) { +std::vector gltf::load_meshes( + json &gltf_json, std::vector> &buffers_content, std::vector &materials) { auto &gltf_meshes = gltf_json["meshes"]; auto meshes_count = gltf_meshes.size(); - std::vector meshes { meshes_count }; + std::vector meshes{meshes_count}; - for(size_t mesh_index = 0; mesh_index < meshes_count; mesh_index++) { + for (size_t mesh_index = 0; mesh_index < meshes_count; mesh_index++) { auto &gltf_mesh = gltf_meshes[mesh_index]; auto &primitives = gltf_mesh["primitives"]; auto &mesh = meshes[mesh_index]; - for (auto& primitive: primitives) { - auto &&part = load_primitive(gltf_json, primitive, buffers_content, materials); + for (auto &primitive : primitives) { + auto part = load_primitive(gltf_json, primitive, buffers_content, materials); mesh.vertices_count += part.vertices_count; mesh.triangles_count += part.triangles_count; mesh.parts.emplace_back(std::move(part)); } } - return std::move(meshes); + return meshes; } std::vector gltf::load_textures(json &gltf_json, std::filesystem::path &path, vkrenderer &renderer) { auto &gltf_images = gltf_json["images"]; auto images_count = gltf_images.size(); - std::vector images { images_count }; + std::vector images{images_count}; for (size_t image_index = 0; image_index < images_count; image_index++) { auto &gltf_image = gltf_images[image_index]; - int x,y,n; + int x, y, n; auto filepath = (path / gltf_image["uri"].get()).string(); unsigned char *data = stbi_load(filepath.c_str(), &x, &y, &n, 4); - auto img = renderer.create_2d_texture(static_cast(x), static_cast(y), VK_FORMAT_R8G8B8A8_UNORM); + auto *img = + renderer.create_2d_texture(static_cast(x), static_cast(y), VK_FORMAT_R8G8B8A8_UNORM); renderer.update_image(img, data); stbi_image_free(data); - images[image_index] = std::move(img); + images[image_index] = img; } auto &gltf_samplers = gltf_json["samplers"]; auto samplers_count = gltf_samplers.size(); - std::vector samplers { samplers_count }; + std::vector samplers{samplers_count}; for (size_t sampler_index = 0; sampler_index < samplers_count; sampler_index++) { samplers[sampler_index] = renderer.create_sampler(VK_FILTER_LINEAR, VK_SAMPLER_ADDRESS_MODE_REPEAT); @@ -110,7 +111,7 @@ std::vector gltf::load_textures(json &gltf_json, std::filesystem::path auto &gltf_textures = gltf_json["textures"]; auto textures_count = gltf_textures.size(); - std::vector textures { textures_count }; + std::vector textures{textures_count}; for (size_t texture_index = 0; texture_index < textures_count; texture_index++) { auto gltf_texture = gltf_textures[texture_index]; @@ -125,23 +126,21 @@ std::vector gltf::load_textures(json &gltf_json, std::filesystem::path // sampler_id = sampler->device_sampler.bindless_index; // } - // textures[texture_index] = texture { - // .image_id = image->device_image.bindless_sampled_index, - // .sampler_id = sampler_id - // }; + // textures[texture_index] = + // texture{.image_id = image->device_image.bindless_sampled_index, .sampler_id = sampler_id}; } - return std::move(textures); + return textures; } std::vector gltf::load_materials(json &gltf_json, std::vector &textures) { auto &gltf_materials = gltf_json["materials"]; auto materials_count = gltf_materials.size(); - std::vector materials { materials_count }; + std::vector materials{materials_count}; - for(size_t material_index = 0; material_index < materials_count; material_index++) { - auto& gltf_material = gltf_materials[material_index]; - auto& material = materials[material_index]; + for (size_t material_index = 0; material_index < materials_count; material_index++) { + auto &gltf_material = gltf_materials[material_index]; + auto &material = materials[material_index]; if (gltf_material.contains("pbrMetallicRoughness")) { auto &pbr_params = gltf_material["pbrMetallicRoughness"]; @@ -152,23 +151,26 @@ std::vector gltf::load_materials(json &gltf_json, std::vector material.base_color.v[1] = base_color[1].get(); material.base_color.v[2] = base_color[2].get(); material.base_color.v[3] = base_color[3].get(); - } else + } else { material.base_color = vec3(1.f); + } if (pbr_params.contains("baseColorTexture")) { auto base_color_index = pbr_params["baseColorTexture"]["index"].get(); material.base_color_texture = textures[base_color_index]; } - if (pbr_params.contains("metallicFactor")) + if (pbr_params.contains("metallicFactor")) { material.metalness = pbr_params["metallicFactor"].get(); - else + } else { material.metalness = 1.0; + } - if (pbr_params.contains("roughnessFactor")) + if (pbr_params.contains("roughnessFactor")) { material.roughness = pbr_params["roughnessFactor"].get(); - else + } else { material.roughness = 1.0; + } if (pbr_params.contains("metallicRoughnessTexture")) { auto metallic_roughness_index = pbr_params["metallicRoughnessTexture"]["index"].get(); @@ -177,10 +179,11 @@ std::vector gltf::load_materials(json &gltf_json, std::vector } } - return std::move(materials); + return materials; } -mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector> &buffers_content, std::vector &materials) { +mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector> &buffers_content, + std::vector &materials) { auto part = mesh_part(); auto &accessors = gltf_json["accessors"]; @@ -194,7 +197,7 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto &indices_buffer = buffers_content[indices_buffer_index]; - uint16_t* indices_start = (uint16_t*)(indices_buffer.data() + indices_offset); + auto *indices_start = (uint16_t *)(indices_buffer.data() + indices_offset); part.indices.resize(indices_length / sizeof(uint16_t)); std::memcpy(part.indices.data(), indices_start, indices_length); @@ -210,9 +213,9 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto &positions_buffer = buffers_content[positions_buffer_index]; - float* positions_start = (float*)(positions_buffer.data() + positions_offset); + auto *positions_start = (float *)(positions_buffer.data() + positions_offset); - part.positions.resize(positions_length / sizeof(float)); + part.positions.resize(positions_length / sizeof(float)); std::memcpy(part.positions.data(), positions_start, positions_length); } @@ -226,9 +229,9 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto &normals_buffer = buffers_content[normals_buffer_index]; - float* normals_start = (float*)(normals_buffer.data() + normals_offset); + auto *normals_start = (float *)(normals_buffer.data() + normals_offset); - part.normals.resize(normals_length / sizeof(float)); + part.normals.resize(normals_length / sizeof(float)); std::memcpy(part.normals.data(), normals_start, normals_length); } @@ -242,9 +245,9 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vector(); auto &uvs_buffer = buffers_content[uvs_buffer_index]; - float* uvs_start = (float*)(uvs_buffer.data() + uvs_offset); + auto *uvs_start = (float *)(uvs_buffer.data() + uvs_offset); - part.uvs.resize(uvs_length / sizeof(float)); + part.uvs.resize(uvs_length / sizeof(float)); std::memcpy(part.uvs.data(), uvs_start, uvs_length); } @@ -252,5 +255,5 @@ mesh_part gltf::load_primitive(json &gltf_json, json &primitive, std::vectorset_pipeline(raytracing_shader_name); auto *accumulation_texture = renderer.create_2d_texture(width, height, VK_FORMAT_R32G32B32A32_SFLOAT); @@ -128,118 +126,141 @@ int main() { for (auto event : wnd.events) { switch (event.type) { - case EVENT_TYPES::RESIZE: { - width = event.width; - height = event.height; - - main_scene.meta.width = width; - main_scene.meta.height = height; + case EVENT_TYPES::RESIZE: { + width = event.width; + height = event.height; + + main_scene.meta.width = width; + main_scene.meta.height = height; + + io.DisplaySize.x = (float)width; + io.DisplaySize.y = (float)height; + + if (event.width == 0 && event.height == 0) { + can_render = false; + } else { + can_render = true; + renderer.recreate_swapchain(); + main_scene.meta.cam.set_aspect_ratio((float)event.width / (float)event.height); + main_scene.meta.sample_index = 1; - io.DisplaySize.x = (float)width; - io.DisplaySize.y = (float)height; + accumulation_texture = renderer.create_2d_texture(width, height, VK_FORMAT_R32G32B32A32_SFLOAT); + output_texture = renderer.create_2d_texture(width, height, VK_FORMAT_R32G32B32A32_SFLOAT); + raytracing_pass->set_dispatch_size(event.width / 8 + 1, event.height / 8 + 1, 1); + } - if (event.width == 0 && event.height == 0) { - can_render = false; - } else { - can_render = true; - renderer.recreate_swapchain(); - main_scene.meta.cam.set_aspect_ratio((float)event.width / (float)event.height); - main_scene.meta.sample_index = 1; + main_scene.meta.sample_index = 1; - accumulation_texture = renderer.create_2d_texture(width, height, VK_FORMAT_R32G32B32A32_SFLOAT); - output_texture = renderer.create_2d_texture(width, height, VK_FORMAT_R32G32B32A32_SFLOAT); - raytracing_pass->set_dispatch_size(event.width / 8 + 1, event.height / 8 + 1, 1); - } + break; + } + case EVENT_TYPES::KEY_PRESS: { + auto move_speed = 1.f * delta_time; + if (wnd.keyboard[KEYS::LSHIFT] || wnd.keyboard[KEYS::RSHIFT]) { move_speed *= 10.f; } - main_scene.meta.sample_index = 1; + vec3 move_vec{}; + switch (event.key) { + case KEYS::W: { + move_vec = main_scene.meta.cam.forward * move_speed; break; } - case EVENT_TYPES::KEY_PRESS: { - auto move_speed = 1.f * delta_time; - if (wnd.keyboard[KEYS::LSHIFT] || wnd.keyboard[KEYS::RSHIFT]) { - move_speed *= 10.f; - } - - vec3 move_vec{}; - - switch (event.key) { - case KEYS::W: { - move_vec = main_scene.meta.cam.forward * move_speed; - break; - } - case KEYS::S: { - move_vec = -main_scene.meta.cam.forward * move_speed; - break; - } - case KEYS::D: { - move_vec = main_scene.meta.cam.right * move_speed; - break; - } - case KEYS::A: { - move_vec = -main_scene.meta.cam.right * move_speed; - break; - } - case KEYS::E: { - main_scene.meta.cam.rotate_y(0.001f * delta_time); - main_scene.meta.sample_index = 1; - break; - } - case KEYS::Q: { - main_scene.meta.cam.rotate_y(-0.001f * delta_time); - main_scene.meta.sample_index = 1; - break; - } - case KEYS::SPACE: { - move_vec = main_scene.meta.cam.up * move_speed; - break; - } - case KEYS::LCTRL: { - move_vec = -main_scene.meta.cam.up * move_speed; - break; - } - default: - break; - } - - main_scene.meta.cam.move(move_vec); - - if (!move_vec.near_zero()) { - main_scene.meta.sample_index = 1; - } + case KEYS::S: { + move_vec = -main_scene.meta.cam.forward * move_speed; + break; + } + case KEYS::D: { + move_vec = main_scene.meta.cam.right * move_speed; break; } - case EVENT_TYPES::BUTTON_PRESS: { - if (event.button == BUTTONS::LEFT) { - io.MouseDown[0] = true; - } - if (event.button == BUTTONS::RIGHT) { - io.MouseDown[1] = true; - } + case KEYS::A: { + move_vec = -main_scene.meta.cam.right * move_speed; break; } - case EVENT_TYPES::BUTTON_RELEASE: { - if (event.button == BUTTONS::LEFT) { - io.MouseDown[0] = false; - } - if (event.button == BUTTONS::RIGHT) { - io.MouseDown[1] = false; - } + case KEYS::E: { + main_scene.meta.cam.rotate_y(0.001f * delta_time); + main_scene.meta.sample_index = 1; + break; + } + case KEYS::Q: { + main_scene.meta.cam.rotate_y(-0.001f * delta_time); + main_scene.meta.sample_index = 1; break; } - case EVENT_TYPES::MOUSE_MOVE: { - io.MousePos = ImVec2{(float)event.x, (float)event.y}; + case KEYS::SPACE: { + move_vec = main_scene.meta.cam.up * move_speed; + break; + } + case KEYS::LCTRL: { + move_vec = -main_scene.meta.cam.up * move_speed; break; } default: break; + } + + main_scene.meta.cam.move(move_vec); + + if (!move_vec.near_zero()) { main_scene.meta.sample_index = 1; } + + break; + } + case EVENT_TYPES::BUTTON_PRESS: { + if (event.button == BUTTONS::LEFT) { io.MouseDown[0] = true; } + if (event.button == BUTTONS::RIGHT) { io.MouseDown[1] = true; } + break; + } + case EVENT_TYPES::BUTTON_RELEASE: { + if (event.button == BUTTONS::LEFT) { io.MouseDown[0] = false; } + if (event.button == BUTTONS::RIGHT) { io.MouseDown[1] = false; } + break; + } + case EVENT_TYPES::MOUSE_MOVE: { + io.MousePos = ImVec2{(float)event.x, (float)event.y}; + break; + } + default: + break; } } wnd.events.clear(); // GUI + ImGui::NewFrame(); + + ImGui::SetNextWindowPos({0.f, 0.f}); + ImGui::SetNextWindowSize({0.f, 0.f}); + ImGui::Begin("Debug", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar); + + ImGui::Text("frame per second %u\n", static_cast(1000.f / delta_time)); + ImGui::Text("frame time %f ms\n", delta_time); + ImGui::Text("frame count %u\n", main_scene.meta.sample_index); + + if (ImGui::Checkbox("depth of field", (bool *)&main_scene.meta.enable_dof)) { + main_scene.meta.sample_index = 1; + } + + ImGui::SliderInt("max bounces", (int32_t *)&main_scene.meta.max_bounce, 1, 250); + ImGui::SliderInt("min bounces", (int32_t *)&main_scene.meta.min_bounce, 1, main_scene.meta.max_bounce); + if (ImGui::SliderInt("downscale factor", (int32_t *)&main_scene.meta.downscale_factor, 1, 32)) { + main_scene.meta.sample_index = 1; + } + + if (ImGui::Checkbox("debug bvh", (bool *)&main_scene.meta.debug_bvh)) { main_scene.meta.sample_index = 1; } + + ImGui::End(); + + ImGui::EndFrame(); + + // To start a frame capture, call StartFrameCapture. + // You can specify NULL, NULL for the device to capture on if you have only one device and + // either no windows at all or only one window, and it will capture from that device. + // See the documentation below for a longer explanation + // if(rdoc_api) rdoc_api->StartFrameCapture(NULL, NULL); + + // renderer.ui(); + if (can_render) { ImGui::NewFrame(); diff --git a/src/scene.cpp b/src/scene.cpp index 871ba68..acac051 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -56,7 +56,7 @@ // } scene::scene(camera cam, uint32_t width, uint32_t height, vkrenderer &renderer) - :meta(cam, width, height){ + :meta(std::move(cam), width, height){ // auto bistro_interior_path = std::filesystem::path("../models/BistroInterior"); // auto bistro_interior_filename = std::string("BistroInterior.gltf"); @@ -67,21 +67,21 @@ scene::scene(camera cam, uint32_t width, uint32_t height, vkrenderer &renderer) auto root_node = gltf::load(sponza_path, sponza_filename, renderer); std::vector triangles; - auto triangles_offset = 0; - auto vertices_offset = 0; + size_t triangles_offset = 0; + size_t vertices_offset = 0; std::queue nodes_to_load {}; nodes_to_load.push(root_node); - while(nodes_to_load.size() != 0) { + while(!nodes_to_load.empty()) { auto &node = nodes_to_load.front(); if (node.mesh) { auto &mesh = node.mesh.value(); - indices.resize(indices.size() + mesh.triangles_count * 3); - positions.resize(positions.size() + mesh.vertices_count * 3); - normals.resize(normals.size() + mesh.vertices_count * 3); - uvs.resize(uvs.size() + mesh.vertices_count * 2); + indices.resize(indices.size() + (size_t)mesh.triangles_count * 3); + positions.resize(positions.size() + (size_t)mesh.vertices_count * 3); + normals.resize(normals.size() + (size_t)mesh.vertices_count * 3); + uvs.resize(uvs.size() + (size_t)mesh.vertices_count * 2); for (auto& mesh_part: mesh.parts) { @@ -90,9 +90,9 @@ scene::scene(camera cam, uint32_t width, uint32_t height, vkrenderer &renderer) std::memcpy(uvs.data() + vertices_offset * 2, mesh_part.uvs.data(), mesh_part.uvs.size() * sizeof(float)); for (size_t i = 0; i < mesh_part.indices.size(); i += 3) { - auto i1 = mesh_part.indices[i]; - auto i2 = mesh_part.indices[i + 1]; - auto i3 = mesh_part.indices[i + 2]; + size_t i1 = mesh_part.indices[i]; + size_t i2 = mesh_part.indices[i + 1]; + size_t i3 = mesh_part.indices[i + 2]; auto p1 = vec3(mesh_part.positions[i1 * 3], mesh_part.positions[i1 * 3 + 1], mesh_part.positions[i1 * 3 + 2]); auto p2 = vec3(mesh_part.positions[i2 * 3], mesh_part.positions[i2 * 3 + 1], mesh_part.positions[i2 * 3 + 2]); @@ -111,8 +111,8 @@ scene::scene(camera cam, uint32_t width, uint32_t height, vkrenderer &renderer) } } - for (auto &node: node.children) { - nodes_to_load.push(node); + for (auto &new_node: node.children) { + nodes_to_load.push(new_node); } nodes_to_load.pop(); diff --git a/src/utils.cpp b/src/utils.cpp index 2a9bdf5..6557ebc 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -37,7 +37,7 @@ std::vector read_file(const char* path) { return {}; } - return std::move(file_content); + return file_content; } std::vector read_file(std::string& path) { diff --git a/src/vec3.cpp b/src/vec3.cpp index 1e4a183..5cf04f0 100644 --- a/src/vec3.cpp +++ b/src/vec3.cpp @@ -1,44 +1,43 @@ +#include #include -#include #include -#include "vec3.hpp" #include "utils.hpp" +#include "vec3.hpp" -vec3::vec3(): v(_mm_setzero_ps()) {} -vec3::vec3(float x): v(_mm_set1_ps(x)) {} -vec3::vec3(float x, float y, float z): v(_mm_set_ps(0.f, z, y, x)) {} +vec3::vec3() : v(_mm_setzero_ps()) {} +vec3::vec3(float x) : v(_mm_set1_ps(x)) {} +vec3::vec3(float x, float y, float z) : v(_mm_set_ps(0.f, z, y, x)) {} -vec3::vec3(__m128& v): v(v) {} -vec3::vec3(__m128&& v): v(std::move(v)) {} -vec3::vec3(const vec3& vec) { - v = _mm_load_ps((float*)&vec.v); -} +vec3::vec3(__m128 &v) : v(v) {} +vec3::vec3(__m128 &&v) : v(v) {} -vec3 vec3::operator-() const { - return vec3(_mm_xor_ps(v, _mm_set1_ps(-0.0f))); -} +vec3::vec3(const vec3 &vec) { v = _mm_load_ps((float *)&vec.v); } -vec3& vec3::operator=(const vec3& vec) { - v = _mm_load_ps((float*)&vec.v); +vec3::vec3(const vec3 &&vec) { v = _mm_load_ps((float *)&vec.v); } + +vec3 vec3::operator-() const { return vec3(_mm_xor_ps(v, _mm_set1_ps(-0.0f))); } + +vec3 &vec3::operator=(const vec3 &vec) { + v = _mm_load_ps((float *)&vec.v); return *this; } -vec3& vec3::operator+=(const vec3& vec) { +vec3 &vec3::operator+=(const vec3 &vec) { v = _mm_add_ps(v, vec.v); return *this; } -vec3& vec3::operator*=(float scale) { +vec3 &vec3::operator*=(float scale) { __m128 s = _mm_set1_ps(scale); v = _mm_mul_ps(v, s); return *this; } -vec3& vec3::operator/=(float numerator) { +vec3 &vec3::operator/=(float numerator) { __m128 s = _mm_set1_ps(numerator); v = _mm_div_ps(v, s); @@ -46,9 +45,7 @@ vec3& vec3::operator/=(float numerator) { } float vec3::operator[](int axis) { - if (axis > 2) { - return std::numeric_limits::infinity(); - } + if (axis > 2) { return std::numeric_limits::infinity(); } return v[axis]; } @@ -58,36 +55,32 @@ float vec3::length() const { return std::sqrt(t[0]); } -float vec3::length_sq() const { - return (_mm_dp_ps(v, v, 0b11110001))[0]; -} +float vec3::length_sq() const { return (_mm_dp_ps(v, v, 0b11110001))[0]; } -float vec3::dot(const vec3& vec) const { - return (_mm_dp_ps(v, vec.v, 0b11110001))[0]; -} +float vec3::dot(const vec3 &vec) const { return (_mm_dp_ps(v, vec.v, 0b11110001))[0]; } -vec3 vec3::cross(const vec3& vec) const { - __m128 tmp0 = _mm_shuffle_ps(v,v,_MM_SHUFFLE(3,0,2,1)); - __m128 tmp1 = _mm_shuffle_ps(vec.v,vec.v,_MM_SHUFFLE(3,1,0,2)); - __m128 tmp2 = _mm_mul_ps(tmp0,vec.v); - __m128 tmp3 = _mm_mul_ps(tmp0,tmp1); - __m128 tmp4 = _mm_shuffle_ps(tmp2,tmp2,_MM_SHUFFLE(3,0,2,1)); - return _mm_sub_ps(tmp3,tmp4); +vec3 vec3::cross(const vec3 &vec) const { + __m128 tmp0 = _mm_shuffle_ps(v, v, _MM_SHUFFLE(3, 0, 2, 1)); + __m128 tmp1 = _mm_shuffle_ps(vec.v, vec.v, _MM_SHUFFLE(3, 1, 0, 2)); + __m128 tmp2 = _mm_mul_ps(tmp0, vec.v); + __m128 tmp3 = _mm_mul_ps(tmp0, tmp1); + __m128 tmp4 = _mm_shuffle_ps(tmp2, tmp2, _MM_SHUFFLE(3, 0, 2, 1)); + return _mm_sub_ps(tmp3, tmp4); } -vec3& vec3::min(const vec3& vec) { +vec3 &vec3::min(const vec3 &vec) { v = _mm_min_ps(v, vec.v); return *this; } -vec3& vec3::max(const vec3& vec) { +vec3 &vec3::max(const vec3 &vec) { v = _mm_max_ps(v, vec.v); return *this; } -vec3& vec3::normalize() { +vec3 &vec3::normalize() { v = _mm_div_ps(v, _mm_sqrt_ps(_mm_dp_ps(v, v, 0xff))); return *this; @@ -99,61 +92,47 @@ bool vec3::near_zero() const { return (fabs(v[0]) < EPSILON) && (fabs(v[1]) < EPSILON) && (fabs(v[2]) < EPSILON); } -vec3 vec3::random() { - return vec3 { randd(), randd(), randd() }; -} +vec3 vec3::random() { return vec3{randd(), randd(), randd()}; } -vec3 vec3::random(float min, float max) { - return vec3 { randd(min, max), randd(min, max), randd(min, max) }; -} +vec3 vec3::random(float min, float max) { return vec3{randd(min, max), randd(min, max), randd(min, max)}; } // TODO: Use simd instructions -vec3 lerp(const vec3& u, const vec3& v, float t) { - return (1.0 - t) * u + t * v; -} +vec3 lerp(const vec3 &u, const vec3 &v, float t) { return (1.f - t) * u + t * v; } -vec3 operator+(const vec3& u, const vec3& v) { - return vec3(_mm_add_ps(u.v, v.v)); -} +vec3 operator+(const vec3 &u, const vec3 &v) { return vec3(_mm_add_ps(u.v, v.v)); } -vec3 operator-(const vec3& u, const vec3& v) { - return vec3(_mm_sub_ps(u.v, v.v)); -} +vec3 operator-(const vec3 &u, const vec3 &v) { return vec3(_mm_sub_ps(u.v, v.v)); } -vec3 operator*(const vec3& u, const vec3& v) { - return vec3(_mm_mul_ps(u.v, v.v)); -} +vec3 operator*(const vec3 &u, const vec3 &v) { return vec3(_mm_mul_ps(u.v, v.v)); } -vec3 operator*(const vec3& u, float scale) { +vec3 operator*(const vec3 &u, float scale) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_mul_ps(u.v, s)); } -vec3 operator*(float scale, const vec3& u) { +vec3 operator*(float scale, const vec3 &u) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_mul_ps(u.v, s)); } -vec3 operator/(const vec3& u, float scale) { +vec3 operator/(const vec3 &u, float scale) { __m128 s = _mm_set1_ps(scale); return vec3(_mm_div_ps(u.v, s)); } // TODO: Use simd instructions -vec3 reflect(const vec3& v, const vec3& n) { - return v - 2 * v.dot(n) * n; -} +vec3 reflect(const vec3 &v, const vec3 &n) { return v - 2 * v.dot(n) * n; } // TODO: Use simd instructions -vec3 refract(const vec3& uv, const vec3& n, float etai_over_etat) { - auto cos_theta = fmin(n.dot(-uv), 1.0); - vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n); - vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_sq())) * n; +vec3 refract(const vec3 &uv, const vec3 &n, float etai_over_etat) { + auto cos_theta = fmin(n.dot(-uv), 1.f); + vec3 r_out_perp = etai_over_etat * (uv + cos_theta * n); + vec3 r_out_parallel = -sqrt(fabs(1.f - r_out_perp.length_sq())) * n; return r_out_perp + r_out_parallel; } -std::ostream& operator<<(std::ostream& out, vec3 vec) { +std::ostream &operator<<(std::ostream &out, vec3 vec) { return out << vec[0] << ' ' << vec[1] << ' ' << vec[2] << std::endl; } -- cgit v1.3