diff options
author | Chuyan Zhang <chuyan@ucsb.edu> | 2024-10-10 18:51:05 -0700 |
---|---|---|
committer | Chuyan Zhang <chuyan@ucsb.edu> | 2024-10-10 18:51:05 -0700 |
commit | 6271f5ce797bf12be64c710b66b1b9e93a239829 (patch) | |
tree | 44cc6a5ab5a20f7e55829f18b68a9f1191dac81d /include | |
parent | ba27f3c22e79b91a2573b7efd00c5a3bbdb96dbc (diff) | |
download | iris-6271f5ce797bf12be64c710b66b1b9e93a239829.tar.gz iris-6271f5ce797bf12be64c710b66b1b9e93a239829.zip |
Diffstat (limited to 'include')
-rw-r--r-- | include/gltf_loader.h | 9 | ||||
-rw-r--r-- | include/render_assets.h | 94 | ||||
-rw-r--r-- | include/render_graph.h | 57 | ||||
-rw-r--r-- | include/render_pass.h | 23 | ||||
-rw-r--r-- | include/resources/buffer.h | 37 | ||||
-rw-r--r-- | include/resources/image.h | 71 | ||||
-rw-r--r-- | include/shader.h | 39 | ||||
-rw-r--r-- | include/vulkan_helper.h | 126 | ||||
-rw-r--r-- | include/vulkan_swapchain.h | 47 |
9 files changed, 503 insertions, 0 deletions
diff --git a/include/gltf_loader.h b/include/gltf_loader.h new file mode 100644 index 0000000..4b3190b --- /dev/null +++ b/include/gltf_loader.h @@ -0,0 +1,9 @@ +#ifndef GLTF_LOADER_H + +#define GLTF_LOADER_H +#include <string_view> +#include "render_assets.h" +#include "tiny_gltf.h" + +bool load_gltf(const std::string_view path, iris::Scene &scene); +#endif
\ No newline at end of file diff --git a/include/render_assets.h b/include/render_assets.h new file mode 100644 index 0000000..1819271 --- /dev/null +++ b/include/render_assets.h @@ -0,0 +1,94 @@ +#pragma once + +#include <cstdint> +#include <string> +#include <vector> +#include <glm/glm.hpp> + +namespace iris { + +struct Mesh { + std::string name; + std::vector<glm::vec3> vertices; + std::vector<glm::vec3> normals; + std::vector<glm::vec2> texcoords; + // Note: w component is a sign, 1.0 or -1.0 + std::vector<glm::vec4> tangents; + std::vector<uint32_t> indices; + int32_t material_index; + + // AABB + glm::vec3 p_min; + glm::vec3 p_max; +}; + +struct Material { + std::string name; + glm::vec4 base_color; + float metallic; + float roughness; + + int32_t base_color_texture; + int32_t metallic_roughness_texture; + int32_t normal_texture; + int32_t occulsion_texture; + int32_t emissive_texture; +}; + + +struct Image { + std::vector<uint8_t> data; + glm::uvec3 extent; + int32_t bits_per_channel; +}; + +struct Sampler2D { + int32_t min_filter; + int32_t mag_filter; + int32_t wrap_s; + int32_t wrap_t; +}; + +struct Texture { + std::string name; + Image image; + Sampler2D sampler; +}; + +struct Camera { + enum struct Tag { + Perspective, + Orthographic, + } intrinsic_tag; + struct PerspectiveCamera { + float fovx; + float fovy; + float aspect; + float znear; + float zfar; + }; + + struct OrthographicCamera { + float xmag; + float ymag; + float znear; + float zfar; + }; + union { + PerspectiveCamera perspective; + OrthographicCamera orthographic; + } intrinsic; + glm::vec3 position; + glm::vec3 direction; + glm::vec3 up; +}; + +struct Scene { + std::vector<Mesh> meshes; + std::vector<Material> materials; + std::vector<Texture> textures; + + Camera camera; +}; + +} // namespace iris
\ No newline at end of file diff --git a/include/render_graph.h b/include/render_graph.h new file mode 100644 index 0000000..b32d3eb --- /dev/null +++ b/include/render_graph.h @@ -0,0 +1,57 @@ +#pragma once + +#include "render_pass.h" +#include <cstdint> +#include <vector> + +namespace iris { + +struct BufferDesc { + +}; + +struct ImageDesc { + +}; + +struct ImageWithSamplerDesc { + +}; + +struct AccelerationStructureDesc { + +}; + +struct CreatedRenderResource { + enum struct Type { + Buffer, + Image, + ImageWithSampler, + AccelerationStructure, + } type; + + +}; + +struct ImportedRenderResource { + +}; + +struct RenderResource { + enum struct Type { + Created, + Imported, + } type; + + union { + CreatedRenderResource created; + ImportedRenderResource imported; + } inner; +}; + +struct RenderGraph { + std::vector<RenderPass> passes; + std::vector<RenderResource> resources; +}; + +} // namespace iris
\ No newline at end of file diff --git a/include/render_pass.h b/include/render_pass.h new file mode 100644 index 0000000..b01bcae --- /dev/null +++ b/include/render_pass.h @@ -0,0 +1,23 @@ +#pragma once + +enum struct RenderPassType { + RayTracing, + Compute, + Rasterization // Not intended to implement, want to make a pure ray tracer +}; + +struct RayTracingPass { + +}; + +struct ComputePass { + +}; + +struct RenderPass { + RenderPassType type; + union { + RayTracingPass ray_tracing; + ComputePass compute; + } inner; +}; diff --git a/include/resources/buffer.h b/include/resources/buffer.h new file mode 100644 index 0000000..7c6fe0e --- /dev/null +++ b/include/resources/buffer.h @@ -0,0 +1,37 @@ +#pragma once + +#include <cstdint> +#include <memory> +#include <optional> +#include <vk_mem_alloc.h> +#include <vulkan/vulkan_core.h> +namespace iris { + +struct BufferDesc { + uint64_t size; + std::optional<uint64_t> alignment; + VkBufferUsageFlags buffer_usage; + VmaMemoryUsage memory_usage; +}; + +struct Buffer_tt { + VkBuffer handle; + BufferDesc desc; + + // Used during release + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + void release(); + + // Used during map/unmap + std::optional<void *> mapped_data; + void *map(); + void unmap(); + + ~Buffer_tt() { release(); } +}; + +typedef std::shared_ptr<Buffer_tt> Buffer_s; + +} // namespace iris
\ No newline at end of file diff --git a/include/resources/image.h b/include/resources/image.h new file mode 100644 index 0000000..391c023 --- /dev/null +++ b/include/resources/image.h @@ -0,0 +1,71 @@ +#pragma once + +#include <unordered_map> +#include <vk_mem_alloc.h> +#include <vulkan/vulkan_core.h> +namespace iris { + +enum struct ImageType { + Texture1D, + Texture1DArray, + Texture2D, + Texture2DArray, + Texture3D, + TextureCube, + TextureCubeArray, +}; + +struct ImageDesc { + ImageType image_type; + VkImageUsageFlags image_usage; + VkImageCreateFlags create_flags; + VmaAllocationCreateFlags alloc_flags; + + VkFormat format; + VkExtent3D extent; + uint32_t mip_levels; + uint32_t array_layers; +}; + +struct ImageViewDesc { + VkImageViewType view_type; + VkFormat format; + VkImageAspectFlags aspect_mask; + uint32_t base_mip_level; + uint32_t mip_levels; + uint32_t base_array_layer; + uint32_t array_layers; + + bool operator==(const ImageViewDesc& other) const = default; +}; + +struct ImageViewDescHash { + std::size_t operator()(const ImageViewDesc& desc) const noexcept { + auto h = std::hash<int>{}(desc.view_type); + h ^= std::hash<int>{}(desc.format) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.aspect_mask) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.base_mip_level) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.mip_levels) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.base_array_layer) + 0x9e3779b9 + (h << 6) + (h >> 2); + h ^= std::hash<int>{}(desc.array_layers) + 0x9e3779b9 + (h << 6) + (h >> 2); + return h; + } +}; + +struct Image_tt { + VkImage handle; + ImageDesc desc; + + // Used during release + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + + // store all the image views + std::unordered_map<ImageViewDesc, VkImageView, ImageViewDescHash> image_views; + VkImageView get_image_view(const ImageViewDesc &desc); + + void release(); +}; + +} // namespace iris
\ No newline at end of file diff --git a/include/shader.h b/include/shader.h new file mode 100644 index 0000000..b34fd92 --- /dev/null +++ b/include/shader.h @@ -0,0 +1,39 @@ +#pragma once +#include <cstdint> +#include <cstdlib> +#include <string> +#include <string_view> +#include <vector> + +namespace iris { + +struct ShaderDesc { + // Name of the shader, filename if loaded from file + std::string name; + // Source code of the shader + std::string source; + // Entry name of the shader + std::string entry_point; + + // Shader type + enum struct Type { + eRayGen, + eMiss, + eClosestHit, + eAnyHit, + eIntersection, + eCallable, + eCompute, + } type; + + // Compiled binary + std::vector<uint32_t> compiled_binary; + + static Type shader_type_from_string(std::string_view type); + ShaderDesc( + const std::string_view path, + std::vector<std::string> defines, + std::vector<std::pair<std::string, std::string>> valued_defines); +}; + +} // namespace iris
\ No newline at end of file diff --git a/include/vulkan_helper.h b/include/vulkan_helper.h new file mode 100644 index 0000000..ec115fa --- /dev/null +++ b/include/vulkan_helper.h @@ -0,0 +1,126 @@ +#pragma once + +#include "resources/buffer.h" +#include "resources/image.h" +#include <memory> +#include <vulkan/vulkan_core.h> +#include <vulkan/vk_enum_string_helper.h> +#include <vk_mem_alloc.h> +#include <spdlog/spdlog.h> + +#include <cstdint> +#include <vector> +#include <string> + +#define CHECK_VULKAN(result) \ + do { \ + VkResult res = result; \ + if (res != VK_SUCCESS) { \ + spdlog::error("Vulkan error: {}", string_VkResult(res)); \ + abort(); \ + } \ + } while (0) + +namespace iris { + +struct Buffer_t { + VkBuffer buffer; + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + VkBufferUsageFlags flags; + VkDeviceSize size; + void *mapped_data = nullptr; + + void* map(); + void unmap(); + void release(); + ~Buffer_t() { release(); } +}; + +typedef std::shared_ptr<Buffer_t> Buffer; + +struct Texture2D_t { + VkImage image; + VkDevice device; + VmaAllocator allocator; + VmaAllocation allocation; + VkImageView image_view; + VkImageLayout layout; + + VkImageUsageFlags flags; + VkExtent2D extent; + + void release(); + ~Texture2D_t() { release(); } +}; + +typedef std::shared_ptr<Texture2D_t> Texture2D; + +// This is a really brute-force implementation, +// every command pool contains only 1 command buffer +struct CommandBuffer { + VkDevice device; + VkCommandPool pool; + VkCommandBuffer buffer; + VkFence fence; + VkQueue queue; + + CommandBuffer(VkDevice device, uint32_t queue_family_index, VkQueue queue); + void release(); + void begin(VkCommandBufferUsageFlags flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT); + void submit_sync(); + ~CommandBuffer() { release(); } +}; + +struct Device { + VkInstance instance; + VkPhysicalDevice physical_device; + VkDevice device; + uint32_t main_queue_family_index; + VkQueue graphics_queue; + VmaAllocator allocator; +#ifdef USE_VULKAN_VALIDATION_LAYERS + VkDebugReportCallbackEXT debugReportCallback; + VkDebugUtilsMessengerEXT debugUtilsMessenger; +#endif + + Device() = delete; + Device( + std::vector<std::string> layers, + std::vector<std::string> instance_extensions); + void destroy(); + + Buffer create_buffer( + VkDeviceSize size, + VkBufferUsageFlags usage, + VmaAllocationCreateInfo create_info = { + .usage = VMA_MEMORY_USAGE_AUTO, + }); + + Texture2D create_texture( + VkExtent2D extent, + VkFormat format, + VkImageUsageFlags usage, + VmaAllocationCreateInfo create_info = { + .usage = VMA_MEMORY_USAGE_AUTO, + }); + + Texture2D create_texture_from_image( + const char* filename, + VkFormat format, + VkImageUsageFlags usage, + VmaAllocationCreateInfo create_info = { + .usage = VMA_MEMORY_USAGE_AUTO, + }); + + std::shared_ptr<Buffer_tt> create_buffer( + BufferDesc desc); + + std::shared_ptr<Image_tt> create_image( + ImageDesc desc); + + CommandBuffer create_command_buffer(); +}; + +} // namespace iris
\ No newline at end of file diff --git a/include/vulkan_swapchain.h b/include/vulkan_swapchain.h new file mode 100644 index 0000000..8d4604e --- /dev/null +++ b/include/vulkan_swapchain.h @@ -0,0 +1,47 @@ +#pragma once +#include "imgui_impl_glfw.h" +#include "vulkan_helper.h" +#include "vulkan/vulkan_core.h" +#include <cstdint> +#include <sys/types.h> +#include <vector> +#include <string> + +std::vector<std::string> get_glfw_instance_extensions(); + +namespace iris { + +struct Swapchain { + Device device; + + VkSurfaceKHR surface = VK_NULL_HANDLE; + VkSwapchainKHR swapchain = VK_NULL_HANDLE; + VkRenderPass render_pass = VK_NULL_HANDLE; + VkDescriptorPool descriptor_pool = VK_NULL_HANDLE; + GLFWwindow *window = nullptr; + uint32_t width = -1; + uint32_t height = -1; + bool needs_recreate = false; + + static constexpr uint32_t SWAPCHAIN_IMAGE_COUNT = 3; + VkImage swapchain_images[SWAPCHAIN_IMAGE_COUNT]; + VkImageView swapchain_image_views[SWAPCHAIN_IMAGE_COUNT]; + VkFramebuffer framebuffers[SWAPCHAIN_IMAGE_COUNT]; + VkSemaphore image_available_semaphores[SWAPCHAIN_IMAGE_COUNT]; + VkSemaphore render_finished_semaphores[SWAPCHAIN_IMAGE_COUNT]; + CommandBuffer cmd_buf; + + Texture2D upload_texture; + + ImGuiContext *imgui_context = nullptr; + uint32_t semaphore_index = 0; + uint32_t frame_index = 0; + + void resize(uint32_t new_width, uint32_t new_height); + void start_frame(); + void display(Texture2D &texture); + void release(); + Swapchain(GLFWwindow *window, iris::Device device, uint32_t width, uint32_t height); +}; + +} // namespace iris
\ No newline at end of file |