summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChuyan Zhang <chuyan@ucsb.edu>2024-10-10 18:51:05 -0700
committerChuyan Zhang <chuyan@ucsb.edu>2024-10-10 18:51:05 -0700
commit6271f5ce797bf12be64c710b66b1b9e93a239829 (patch)
tree44cc6a5ab5a20f7e55829f18b68a9f1191dac81d /src
parentba27f3c22e79b91a2573b7efd00c5a3bbdb96dbc (diff)
downloadiris-main.tar.gz
iris-main.zip
move to new resource abstractionHEADmain
Diffstat (limited to 'src')
-rw-r--r--src/gltf_loader.h9
-rw-r--r--src/render_assets.h94
-rw-r--r--src/render_graph.h41
-rw-r--r--src/render_pass.h23
-rw-r--r--src/resources/buffer.cpp56
-rw-r--r--src/resources/image.cpp43
-rw-r--r--src/shader.h39
-rw-r--r--src/vulkan_helper.h118
-rw-r--r--src/vulkan_swapchain.h47
9 files changed, 99 insertions, 371 deletions
diff --git a/src/gltf_loader.h b/src/gltf_loader.h
deleted file mode 100644
index 4b3190b..0000000
--- a/src/gltf_loader.h
+++ /dev/null
@@ -1,9 +0,0 @@
-#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/src/render_assets.h b/src/render_assets.h
deleted file mode 100644
index 1819271..0000000
--- a/src/render_assets.h
+++ /dev/null
@@ -1,94 +0,0 @@
-#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/src/render_graph.h b/src/render_graph.h
deleted file mode 100644
index f09d6cb..0000000
--- a/src/render_graph.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#pragma once
-
-#include "render_pass.h"
-#include <cstdint>
-#include <vector>
-
-namespace iris {
-
-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/src/render_pass.h b/src/render_pass.h
deleted file mode 100644
index b01bcae..0000000
--- a/src/render_pass.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#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/src/resources/buffer.cpp b/src/resources/buffer.cpp
new file mode 100644
index 0000000..8568466
--- /dev/null
+++ b/src/resources/buffer.cpp
@@ -0,0 +1,56 @@
+#include "resources/buffer.h"
+#include "vulkan_helper.h"
+#include <memory>
+
+namespace iris {
+
+void *Buffer_tt::map() {
+ if (!mapped_data.has_value()) {
+ CHECK_VULKAN(vmaMapMemory(allocator, allocation, &mapped_data.value()));
+ }
+ return mapped_data.value();
+}
+
+void Buffer_tt::unmap() {
+ if (mapped_data.has_value()) {
+ vmaUnmapMemory(allocator, allocation);
+ mapped_data.reset();
+ } else {
+ spdlog::warn("Buffer is not mapped, skipping");
+ }
+}
+
+void Buffer_tt::release() {
+ if (handle != VK_NULL_HANDLE) {
+ spdlog::debug("Destroying buffer: 0x{:x}", (uint64_t)handle);
+ vkDeviceWaitIdle(device);
+ vmaDestroyBuffer(allocator, handle, allocation);
+ handle = VK_NULL_HANDLE;
+ }
+}
+
+std::shared_ptr<Buffer_tt> Device::create_buffer(BufferDesc desc) {
+ VkBufferCreateInfo buffer_create_info = {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+ .size = desc.size,
+ .usage = desc.buffer_usage,
+ };
+ VmaAllocationCreateInfo alloc_create_info = {
+ .usage = desc.memory_usage,
+ };
+ std::shared_ptr<Buffer_tt> buffer = std::make_shared<Buffer_tt>();
+ buffer->device = this->device;
+ buffer->allocator = this->allocator;
+ buffer->desc = desc;
+ CHECK_VULKAN(vmaCreateBuffer(
+ allocator,
+ &buffer_create_info,
+ &alloc_create_info,
+ &buffer->handle,
+ &buffer->allocation,
+ VK_NULL_HANDLE));
+ spdlog::debug("Created buffer: 0x{:x}", (uint64_t)buffer->handle);
+ return buffer;
+}
+
+} // namespace iris \ No newline at end of file
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
new file mode 100644
index 0000000..0c51b2d
--- /dev/null
+++ b/src/resources/image.cpp
@@ -0,0 +1,43 @@
+#include "resources/image.h"
+#include "vulkan_helper.h"
+#include <vulkan/vulkan_core.h>
+
+namespace iris {
+
+VkImageView Image_tt::get_image_view(const ImageViewDesc &desc) {
+ auto it = image_views.find(desc);
+ if (it != image_views.end()) {
+ return it->second;
+ }
+
+ // Not found, create the image view
+ VkImageViewCreateInfo view_create_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
+ .image = handle,
+ .viewType = desc.view_type,
+ .format = desc.format,
+ .components = {
+ .r = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .g = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .b = VK_COMPONENT_SWIZZLE_IDENTITY,
+ .a = VK_COMPONENT_SWIZZLE_IDENTITY,
+ },
+ .subresourceRange = {
+ .aspectMask = desc.aspect_mask,
+ .baseMipLevel = desc.base_mip_level,
+ .levelCount = desc.mip_levels,
+ .baseArrayLayer = desc.base_array_layer,
+ .layerCount = desc.array_layers,
+ },
+ };
+ VkImageView view;
+ CHECK_VULKAN(vkCreateImageView(
+ device,
+ &view_create_info,
+ nullptr,
+ &view));
+ image_views[desc] = view;
+ return view;
+}
+
+} // namespace iris \ No newline at end of file
diff --git a/src/shader.h b/src/shader.h
deleted file mode 100644
index b34fd92..0000000
--- a/src/shader.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#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/src/vulkan_helper.h b/src/vulkan_helper.h
deleted file mode 100644
index 04fcead..0000000
--- a/src/vulkan_helper.h
+++ /dev/null
@@ -1,118 +0,0 @@
-#pragma once
-
-#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,
- });
-
- CommandBuffer create_command_buffer();
-};
-
-} // namespace iris \ No newline at end of file
diff --git a/src/vulkan_swapchain.h b/src/vulkan_swapchain.h
deleted file mode 100644
index 8d4604e..0000000
--- a/src/vulkan_swapchain.h
+++ /dev/null
@@ -1,47 +0,0 @@
-#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