Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/NotAPenguin0/Phobos
Browse files Browse the repository at this point in the history
  • Loading branch information
NotAPenguin0 committed Apr 17, 2023
2 parents acd17d6 + cab116e commit 1fbf98f
Show file tree
Hide file tree
Showing 31 changed files with 694 additions and 187 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CMake

on: [push, pull_request]

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release

jobs:
windows-build:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: suisei-cn/actions-download-file@v1
id: glslc-download
name: Download glslc
with:
url: https://storage.googleapis.com/shaderc/artifacts/prod/graphics_shader_compiler/shaderc/windows/continuous_release_2017/386/20220602-094729/install.zip
target: glslc-dir
auto-match: false

- name: Extract glslc archive
run: Expand-Archive glslc-dir/install.zip glslc-dir/install/bin

- name: Setup Vulkan SDK
uses: sjcobb2022/setup-vulkan-sdk@c2612401009bbce8002630e838bf91cc67f8b3c3
with:
vulkan-query-version: 1.2.198.1
vulkan-components: Vulkan-Headers, Vulkan-Loader
vulkan-use-cache: true

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -GNinja -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DGLSLC_DIR=glslc-dir/install/bin -DPHOBOS_ENABLE_TEST_APP=OFF

- name: Build
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Phobos Vulkan Renderer

This project is being continued in Rust, [here](https://github.com/NotAPenguin0/phobos-rs)

[![Build](https://github.com/NotAPenguin0/Phobos/actions/workflows/cmake.yml/badge.svg?branch=master)](https://github.com/NotAPenguin0/Phobos/actions/workflows/cmake.yml)

Phobos is a Vulkan renderer written in C++. It supports custom rendering commands, as well as a fixed pipeline implementing deferred
rendering. This fixed pipeline is aimed at being used for 3D scene viewing/editing.
rendering. This fixed pipeline is aimed at being used for 3D scene viewing/editing.
27 changes: 22 additions & 5 deletions include/phobos/acceleration_structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,36 @@ struct AccelerationStructureInstance {
class AccelerationStructureBuilder {
public:
static AccelerationStructureBuilder create(ph::Context& ctx);
// Update an acceleration structure. This will keep the BLAS, but clear the TLAS so new
// or different instances can be added.
// static AccelerationStructureBuilder from(ph::Context& ctx, AccelerationStructure to_update)

// Add a mesh to the acceleration structure. This mesh will be added to the list of meshes in the bottom-level acceleration structure.
// Returns the index of the mesh (used to create instances of it).
uint32_t add_mesh(AccelerationStructureMesh const& mesh);

void add_instance(AccelerationStructureInstance const& instance);

// Remove all instances in the TLAS builder (but keeps the buffer around, so it can be reused).
// Doesn't actually change the acceleration structure.
void clear_instances();

// Remove all meshes in the BLAS builder.
// Doesn't actually change the underlying acceleration structure object.
void clear_meshes();

// Builds only the BLAS
void build_blas_only(uint32_t thread_index = 0);

// Builds only the TLAS using the same BLAS structures.
void build_tlas_only(uint32_t thread_index = 0);

// Builds the acceleration structure. If done on a different thread, you must supply the thread index to ensure
// queue access is properly synchronized.
AccelerationStructure build(uint32_t thread_index = 0);

// Gets resulting AS.
AccelerationStructure get();

~AccelerationStructureBuilder();

private:
ph::Context& ctx;
std::vector<AccelerationStructureMesh> meshes;
Expand All @@ -93,9 +110,9 @@ class AccelerationStructureBuilder {
// Query pool containing the compacted BLAS size.
VkQueryPool compacted_blas_size_qp{};

AccelerationStructureBuilder(ph::Context& ctx);
explicit AccelerationStructureBuilder(ph::Context& ctx);

// Creates a single DedicatedAccelerationStructure from a create info. This means a buffer is created and
// Creates a single DedicatedAccelerationStructure from a CreateInfo. This means a buffer is created and
// bound to the acceleration structure
DedicatedAccelerationStructure create_acceleration_structure(VkAccelerationStructureCreateInfoKHR create_info);

Expand Down
8 changes: 7 additions & 1 deletion include/phobos/attachment.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#pragma once

#include <phobos/image.hpp>
#include <optional>

namespace ph {

struct Attachment {
ph::ImageView view{ };
ph::ImageView view {};
std::optional<ph::RawImage> image = std::nullopt;

explicit inline operator bool() const {
return view && true; // cast view to bool
}
};

}
4 changes: 4 additions & 0 deletions include/phobos/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ class CommandBuffer {

CommandBuffer& copy_buffer(BufferSlice src, BufferSlice dst);
CommandBuffer& copy_buffer_to_image(BufferSlice src, ph::ImageView dst, VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
CommandBuffer& copy_image_to_buffer(ph::ImageView src, BufferSlice dst, VkImageLayout layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);

CommandBuffer& blit_image(ph::RawImage const& src, VkImageLayout src_layout, ph::RawImage const& dst, VkImageLayout dst_layout, VkImageBlit blit, VkFilter filter = VK_FILTER_LINEAR);

#if PHOBOS_ENABLE_RAY_TRACING
CommandBuffer& bind_ray_tracing_pipeline(std::string_view name);

CommandBuffer& build_acceleration_structure(VkAccelerationStructureBuildGeometryInfoKHR const& info, VkAccelerationStructureBuildRangeInfoKHR const* ranges);
CommandBuffer& write_acceleration_structure_properties(VkAccelerationStructureKHR as, VkQueryType query_type, VkQueryPool query_pool, uint32_t index);
CommandBuffer& write_acceleration_structure_properties(std::span<VkAccelerationStructureKHR> handles, VkQueryType query_type, VkQueryPool query_pool, uint32_t first);
CommandBuffer& copy_acceleration_structure(VkAccelerationStructureKHR src, VkAccelerationStructureKHR dst, VkCopyAccelerationStructureModeKHR mode);
CommandBuffer& compact_acceleration_structure(VkAccelerationStructureKHR src, VkAccelerationStructureKHR dst);

Expand Down
55 changes: 34 additions & 21 deletions include/phobos/context.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

#include <phobos/version.hpp>
#include <phobos/window_interface.hpp>
#include <phobos/log_interface.hpp>
Expand Down Expand Up @@ -112,6 +111,7 @@ struct PhysicalDevice {
std::optional<SurfaceInfo> surface = std::nullopt;
#if PHOBOS_ENABLE_RAY_TRACING
VkPhysicalDeviceRayTracingPipelinePropertiesKHR ray_tracing_properties{};
VkPhysicalDeviceAccelerationStructurePropertiesKHR accel_structure_properties{};
#endif
};

Expand All @@ -131,6 +131,11 @@ struct Swapchain {
uint32_t image_index = 0;
};

struct WaitSemaphore {
VkSemaphore handle = nullptr;
plib::bit_flag<ph::PipelineStage> stage_flags = {};
};

struct PerThreadContext {
ph::ScratchAllocator vbo_allocator;
ph::ScratchAllocator ibo_allocator;
Expand Down Expand Up @@ -237,13 +242,14 @@ namespace impl {

class Context {
public:
Context(AppSettings settings);
Context(AppSettings const& settings);
~Context();

bool is_headless() const;
bool validation_enabled() const;
uint32_t thread_count() const;

VkDevice device();
PhysicalDevice const& get_physical_device() const;

Queue* get_queue(QueueType type);
Expand All @@ -267,10 +273,14 @@ class Context {
void name_object(VkQueue queue, std::string const& name);
void name_object(VkCommandPool pool, std::string const& name);
void name_object(ph::CommandBuffer const& cmd_buf, std::string const& name);
#if PHOBOS_ENABLE_RAY_TRACING
void name_object(VkAccelerationStructureKHR as, std::string const& name);
#endif

size_t max_frames_in_flight() const;
[[nodiscard]] InFlightContext wait_for_frame();
void submit_frame_commands(Queue& queue, CommandBuffer& cmd_buf);
void submit_frame_commands(Queue& queue, CommandBuffer& cmd_buf, std::vector<WaitSemaphore> const& wait_semaphores);
void present(Queue& queue);

// These must be called at the start and end of a thread context. Note that end_thread must be called when all work on the thread is complete, so be sure to add
Expand All @@ -294,8 +304,10 @@ class Context {
VkQueryPool create_query_pool(VkQueryType type, uint32_t count);
void destroy_query_pool(VkQueryPool pool);

Attachment* get_attachment(std::string_view name);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format);
Attachment get_attachment(std::string_view name);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, ImageType type);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, ImageType type);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, uint32_t layers, ImageType type);
// Does not actually resize if the new size is identical to the old size.
void resize_attachment(std::string_view name, VkExtent2D new_size);
bool is_swapchain_attachment(std::string const& name);
Expand Down Expand Up @@ -323,8 +335,12 @@ class Context {
#endif

RawImage create_image(ImageType type, VkExtent2D size, VkFormat format, uint32_t mips = 1);
RawImage create_image(ImageType type, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, uint32_t mips = 1);
RawImage create_image(ImageType type, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, uint32_t mips, uint32_t layers);
void destroy_image(RawImage& image);
ImageView create_image_view(RawImage const& target, ImageAspect aspect = ImageAspect::Color);
ImageView create_image_view(RawImage const& target, uint32_t mip, ImageAspect aspect = ImageAspect::Color);
ImageView create_image_view(RawImage const& target, uint32_t mip, uint32_t layer, ImageAspect aspect = ImageAspect::Color);
void destroy_image_view(ImageView& view);
ImageView get_image_view(uint64_t id);

Expand All @@ -347,7 +363,20 @@ class Context {
VkDeviceAddress get_device_address(BufferSlice slice);

#if PHOBOS_ENABLE_RAY_TRACING
void destroy_acceleration_structure(VkAccelerationStructureKHR handle);
void destroy_acceleration_structure(AccelerationStructure& as);

struct RTXExtensionFunctions {
PFN_vkCreateAccelerationStructureKHR _vkCreateAccelerationStructureKHR = nullptr;
PFN_vkDestroyAccelerationStructureKHR _vkDestroyAccelerationStructureKHR = nullptr;
PFN_vkGetAccelerationStructureBuildSizesKHR _vkGetAccelerationStructureBuildSizesKHR = nullptr;
PFN_vkCmdBuildAccelerationStructuresKHR _vkCmdBuildAccelerationStructuresKHR = nullptr;
PFN_vkCmdWriteAccelerationStructuresPropertiesKHR _vkCmdWriteAccelerationStructuresPropertiesKHR = nullptr;
PFN_vkCmdCopyAccelerationStructureKHR _vkCmdCopyAccelerationStructureKHR = nullptr;
PFN_vkGetAccelerationStructureDeviceAddressKHR _vkGetAccelerationStructureDeviceAddressKHR = nullptr;
PFN_vkCreateRayTracingPipelinesKHR _vkCreateRayTracingPipelinesKHR = nullptr;
PFN_vkCmdTraceRaysKHR _vkCmdTraceRaysKHR = nullptr;
} rtx_fun;
#endif

private:
Expand Down Expand Up @@ -377,8 +406,6 @@ class Context {
#endif
friend class impl::CacheImpl;

VkDevice device();

VkFramebuffer get_or_create(VkFramebufferCreateInfo const& info, std::string const& name = "");
VkRenderPass get_or_create(VkRenderPassCreateInfo const& info, std::string const& name = "");
VkDescriptorSetLayout get_or_create(DescriptorSetLayoutCreateInfo const& dslci);
Expand All @@ -388,27 +415,13 @@ class Context {
#if PHOBOS_ENABLE_RAY_TRACING
Pipeline get_or_create_ray_tracing_pipeline(std::string_view name);
#endif
VkDescriptorSet get_or_create(DescriptorSetBinding set_binding, Pipeline const& pipeline, void* pNext = nullptr);
VkDescriptorSet get_or_create(DescriptorSetBinding const& set_binding, Pipeline const& pipeline, void* pNext = nullptr);

ShaderMeta const& get_shader_meta(std::string_view pipeline_name);
ShaderMeta const& get_compute_shader_meta(std::string_view pipeline_name);
#if PHOBOS_ENABLE_RAY_TRACING
ShaderMeta const& get_ray_tracing_shader_meta(std::string_view pipeline_name);
#endif

#if PHOBOS_ENABLE_RAY_TRACING
struct RTXExtensionFunctions {
PFN_vkCreateAccelerationStructureKHR _vkCreateAccelerationStructureKHR = nullptr;
PFN_vkDestroyAccelerationStructureKHR _vkDestroyAccelerationStructureKHR = nullptr;
PFN_vkGetAccelerationStructureBuildSizesKHR _vkGetAccelerationStructureBuildSizesKHR = nullptr;
PFN_vkCmdBuildAccelerationStructuresKHR _vkCmdBuildAccelerationStructuresKHR = nullptr;
PFN_vkCmdWriteAccelerationStructuresPropertiesKHR _vkCmdWriteAccelerationStructuresPropertiesKHR = nullptr;
PFN_vkCmdCopyAccelerationStructureKHR _vkCmdCopyAccelerationStructureKHR = nullptr;
PFN_vkGetAccelerationStructureDeviceAddressKHR _vkGetAccelerationStructureDeviceAddressKHR = nullptr;
PFN_vkCreateRayTracingPipelinesKHR _vkCreateRayTracingPipelinesKHR = nullptr;
PFN_vkCmdTraceRaysKHR _vkCmdTraceRaysKHR = nullptr;
} rtx_fun;
#endif
};

}
23 changes: 21 additions & 2 deletions include/phobos/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,36 @@ struct hash<ph::DescriptorBufferInfo> {
}
};

#if PHOBOS_ENABLE_RAY_TRACING
template<>
struct hash<ph::DescriptorAccelerationStructureInfo> {
size_t operator()(ph::DescriptorAccelerationStructureInfo const& x) const noexcept {
size_t h = 0;
ph::hash_combine(h, x.structure);
return h;
}
};
#endif

template<>
struct hash<ph::DescriptorBinding> {
size_t operator()(ph::DescriptorBinding const& x) const noexcept {
size_t h = 0;
ph::hash_combine(h, x.binding, x.type);
for (auto const& d : x.descriptors) {
if (x.type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
if (x.type == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER || x.type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
|| x.type == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) {
ph::hash_combine(h, d.image);
}
else { ph::hash_combine(h, d.buffer); }
else if (x.type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER || x.type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
|| x.type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC || x.type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
ph::hash_combine(h, d.buffer);
}
#if PHOBOS_ENABLE_RAY_TRACING
else if (x.type == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR) {
ph::hash_combine(h, d.accel_structure);
}
#endif
}
return h;
}
Expand Down
4 changes: 4 additions & 0 deletions include/phobos/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ struct ImageView {
inline bool operator==(ImageView const& rhs) const {
return id == rhs.id;
}

inline explicit operator bool() const {
return id != static_cast<uint64_t>(-1) && image != nullptr && handle != nullptr;
}
};

// Common utilities
Expand Down
9 changes: 6 additions & 3 deletions include/phobos/impl/attachment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class AttachmentImpl {

// PUBLIC API FUNCTIONS

Attachment* get_attachment(std::string_view name);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format);
Attachment get_attachment(std::string_view name);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, ImageType type);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, ImageType type);
void create_attachment(std::string_view name, VkExtent2D size, VkFormat format, VkSampleCountFlagBits samples, uint32_t layers, ImageType type);
void resize_attachment(std::string_view name, VkExtent2D new_size);
bool is_swapchain_attachment(std::string const& name);
bool is_attachment(ImageView view);
Expand All @@ -31,8 +33,9 @@ class AttachmentImpl {

static inline std::string swapchain_attachment_name = "swapchain";

// Note that the only difference between this and Attachment is the const-ness of the image handle.
struct InternalAttachment {
Attachment attachment;
ph::ImageView view;
std::optional<RawImage> image;
};
std::unordered_map<std::string, InternalAttachment> attachments{};
Expand Down
2 changes: 1 addition & 1 deletion include/phobos/impl/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CacheImpl {
#if PHOBOS_ENABLE_RAY_TRACING
Pipeline get_or_create_ray_tracing_pipeline(ph::RayTracingPipelineCreateInfo& pci);
#endif
VkDescriptorSet get_or_create_descriptor_set(DescriptorSetBinding set_binding, Pipeline const& pipeline, void* pNext = nullptr);
VkDescriptorSet get_or_create_descriptor_set(DescriptorSetBinding const& set_binding, Pipeline const& pipeline, void* pNext = nullptr);

void next_frame();

Expand Down
5 changes: 4 additions & 1 deletion include/phobos/impl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ImageImpl;

class ContextImpl {
public:
ContextImpl(AppSettings settings);
ContextImpl(AppSettings const& settings);
~ContextImpl();

bool is_headless() const;
Expand Down Expand Up @@ -39,6 +39,9 @@ class ContextImpl {
void name_object(VkQueue queue, std::string const& name);
void name_object(VkCommandPool pool, std::string const& name);
void name_object(ph::CommandBuffer const& cmd_buf, std::string const& name);
#if PHOBOS_ENABLE_RAY_TRACING
void name_object(VkAccelerationStructureKHR as, std::string const& name);
#endif

VkFence create_fence();
VkResult wait_for_fence(VkFence fence, uint64_t timeout);
Expand Down

0 comments on commit 1fbf98f

Please sign in to comment.