Skip to content

Commit

Permalink
shader reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNumbat committed Jan 14, 2024
1 parent 1d27b2d commit 435d121
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 89 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,4 @@ For faster parallel builds, you can instead generate [ninja](https://ninja-build
# ToDo

Finish porting
- [ ] shader hot reloading
- [ ] descriptor gen
17 changes: 10 additions & 7 deletions rvk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ cmake_minimum_required(VERSION 3.17)
project(rvk LANGUAGES CXX)

set(SOURCES_RVK
"fwd.h"
"drop.h"
"execute.h"
"fwd.h"
"fwd.cpp"
"rvk.h"
"rvk.cpp"
"instance.cpp"
"instance.h"
"device.cpp"
"instance.cpp"
"device.h"
"device.cpp"
"swapchain.h"
"swapchain.cpp"
"memory.cpp"
"memory.h"
"descriptors.cpp"
"memory.cpp"
"descriptors.h"
"descriptors.cpp"
"commands.h"
"commands.cpp"
"pipeline.h"
"pipeline.cpp"
"acceleration.cpp"
"acceleration.h"
"imgui_impl_vulkan.cpp"
"acceleration.cpp"
"shader_loader.h"
"shader_loader.cpp"
"imgui_impl_vulkan.h"
"imgui_impl_vulkan.cpp"
)

if(UNIX AND NOT APPLE)
Expand Down
87 changes: 87 additions & 0 deletions rvk/fwd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

#include "fwd.h"

namespace rvk::impl {

void check(VkResult result) {
RVK_CHECK(result);
}

String_View describe(VkResult result) {
switch(result) {
#define STR(r) \
case VK_##r: return #r##_v
STR(SUCCESS);
STR(NOT_READY);
STR(TIMEOUT);
STR(EVENT_SET);
STR(EVENT_RESET);
STR(INCOMPLETE);
STR(ERROR_OUT_OF_HOST_MEMORY);
STR(ERROR_OUT_OF_DEVICE_MEMORY);
STR(ERROR_INITIALIZATION_FAILED);
STR(ERROR_DEVICE_LOST);
STR(ERROR_MEMORY_MAP_FAILED);
STR(ERROR_LAYER_NOT_PRESENT);
STR(ERROR_EXTENSION_NOT_PRESENT);
STR(ERROR_FEATURE_NOT_PRESENT);
STR(ERROR_INCOMPATIBLE_DRIVER);
STR(ERROR_TOO_MANY_OBJECTS);
STR(ERROR_FORMAT_NOT_SUPPORTED);
STR(ERROR_SURFACE_LOST_KHR);
STR(ERROR_NATIVE_WINDOW_IN_USE_KHR);
STR(SUBOPTIMAL_KHR);
STR(ERROR_OUT_OF_DATE_KHR);
STR(ERROR_INCOMPATIBLE_DISPLAY_KHR);
STR(ERROR_VALIDATION_FAILED_EXT);
STR(ERROR_INVALID_SHADER_NV);
#undef STR
default: return "UNKNOWN_ERROR"_v;
}
}

[[nodiscard]] String_View describe(VkObjectType type) {
switch(type) {
#define STR(r) \
case VK_OBJECT_TYPE_##r: return #r##_v
STR(UNKNOWN);
STR(INSTANCE);
STR(PHYSICAL_DEVICE);
STR(DEVICE);
STR(QUEUE);
STR(SEMAPHORE);
STR(COMMAND_BUFFER);
STR(FENCE);
STR(DEVICE_MEMORY);
STR(BUFFER);
STR(IMAGE);
STR(EVENT);
STR(QUERY_POOL);
STR(BUFFER_VIEW);
STR(IMAGE_VIEW);
STR(SHADER_MODULE);
STR(PIPELINE_CACHE);
STR(PIPELINE_LAYOUT);
STR(RENDER_PASS);
STR(PIPELINE);
STR(DESCRIPTOR_SET_LAYOUT);
STR(SAMPLER);
STR(DESCRIPTOR_POOL);
STR(DESCRIPTOR_SET);
STR(FRAMEBUFFER);
STR(COMMAND_POOL);
STR(SAMPLER_YCBCR_CONVERSION);
STR(DESCRIPTOR_UPDATE_TEMPLATE);
STR(SURFACE_KHR);
STR(SWAPCHAIN_KHR);
STR(DISPLAY_KHR);
STR(DISPLAY_MODE_KHR);
STR(DEBUG_REPORT_CALLBACK_EXT);
STR(DEBUG_UTILS_MESSENGER_EXT);
STR(VALIDATION_CACHE_EXT);
#undef STR
default: return "UNKNOWN_OBJECT"_v;
}
}

} // namespace rvk
85 changes: 4 additions & 81 deletions rvk/rvk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,6 @@ using namespace rpp;

namespace impl {

void check(VkResult result) {
RVK_CHECK(result);
}

String_View describe(VkResult result) {
switch(result) {
#define STR(r) \
case VK_##r: return #r##_v
STR(SUCCESS);
STR(NOT_READY);
STR(TIMEOUT);
STR(EVENT_SET);
STR(EVENT_RESET);
STR(INCOMPLETE);
STR(ERROR_OUT_OF_HOST_MEMORY);
STR(ERROR_OUT_OF_DEVICE_MEMORY);
STR(ERROR_INITIALIZATION_FAILED);
STR(ERROR_DEVICE_LOST);
STR(ERROR_MEMORY_MAP_FAILED);
STR(ERROR_LAYER_NOT_PRESENT);
STR(ERROR_EXTENSION_NOT_PRESENT);
STR(ERROR_FEATURE_NOT_PRESENT);
STR(ERROR_INCOMPATIBLE_DRIVER);
STR(ERROR_TOO_MANY_OBJECTS);
STR(ERROR_FORMAT_NOT_SUPPORTED);
STR(ERROR_SURFACE_LOST_KHR);
STR(ERROR_NATIVE_WINDOW_IN_USE_KHR);
STR(SUBOPTIMAL_KHR);
STR(ERROR_OUT_OF_DATE_KHR);
STR(ERROR_INCOMPATIBLE_DISPLAY_KHR);
STR(ERROR_VALIDATION_FAILED_EXT);
STR(ERROR_INVALID_SHADER_NV);
#undef STR
default: return "UNKNOWN_ERROR"_v;
}
}

[[nodiscard]] String_View describe(VkObjectType type) {
switch(type) {
#define STR(r) \
case VK_OBJECT_TYPE_##r: return #r##_v
STR(UNKNOWN);
STR(INSTANCE);
STR(PHYSICAL_DEVICE);
STR(DEVICE);
STR(QUEUE);
STR(SEMAPHORE);
STR(COMMAND_BUFFER);
STR(FENCE);
STR(DEVICE_MEMORY);
STR(BUFFER);
STR(IMAGE);
STR(EVENT);
STR(QUERY_POOL);
STR(BUFFER_VIEW);
STR(IMAGE_VIEW);
STR(SHADER_MODULE);
STR(PIPELINE_CACHE);
STR(PIPELINE_LAYOUT);
STR(RENDER_PASS);
STR(PIPELINE);
STR(DESCRIPTOR_SET_LAYOUT);
STR(SAMPLER);
STR(DESCRIPTOR_POOL);
STR(DESCRIPTOR_SET);
STR(FRAMEBUFFER);
STR(COMMAND_POOL);
STR(SAMPLER_YCBCR_CONVERSION);
STR(DESCRIPTOR_UPDATE_TEMPLATE);
STR(SURFACE_KHR);
STR(SWAPCHAIN_KHR);
STR(DISPLAY_KHR);
STR(DISPLAY_MODE_KHR);
STR(DEBUG_REPORT_CALLBACK_EXT);
STR(DEBUG_UTILS_MESSENGER_EXT);
STR(VALIDATION_CACHE_EXT);
#undef STR
default: return "UNKNOWN_OBJECT"_v;
}
}

struct Deletion_Queue {

explicit Deletion_Queue() = default;
Expand Down Expand Up @@ -594,6 +513,10 @@ Opt<BLAS::Staged> make_blas(Buffer& geometry, Vec<BLAS::Offsets, Alloc> offsets)
return BLAS::make(impl::singleton->device_memory.dup(), geometry, move(offsets));
}

Shader_Loader make_shader_loader() {
return Shader_Loader{impl::singleton->device.dup()};
}

void submit(Commands& cmds, u32 index) {
impl::singleton->device->submit(cmds, index);
}
Expand Down
9 changes: 9 additions & 0 deletions rvk/rvk.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#include "acceleration.h"
#include "commands.h"
#include "descriptors.h"
#include "drop.h"
#include "memory.h"
#include "pipeline.h"
#include "shader_loader.h"

namespace rvk {

Expand All @@ -21,8 +24,10 @@ using impl::Commands;
using impl::Fence;
using impl::Image;
using impl::Image_View;
using impl::Pipeline;
using impl::Sem_Ref;
using impl::Semaphore;
using impl::Shader;
using impl::TLAS;

using Finalizer = FunctionN<8, void()>;
Expand Down Expand Up @@ -77,6 +82,10 @@ Opt<Image> make_image(VkExtent3D extent, VkFormat format, VkImageUsageFlags usag
Opt<TLAS::Staged> make_tlas(Buffer& instances, u32 n_instances);
Opt<BLAS::Staged> make_blas(Buffer& geometry, Vec<BLAS::Offsets, Alloc> offsets);

// Pipelines

Shader_Loader make_shader_loader();

// Command execution

void submit(Commands& cmds, u32 index);
Expand Down
86 changes: 86 additions & 0 deletions rvk/shader_loader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@

#include "shader_loader.h"

#include <rpp/asyncio.h>

namespace rvk {

impl::Shader& Shader_Loader::get(Token token) {
return shaders.get(token).first;
}

Shader_Loader::Token Shader_Loader::compile(String_View path) {

if(Opt<Vec<u8, Files::Alloc>> data = Files::read(path)) {

Shader shader{device.dup(), data->slice()};
Files::Write_Watcher watcher{path};

Token token = next_token.incr();
{
Thread::Lock lock{mutex};
shaders.insert(token, Pair{move(shader), move(watcher)});
}

return token;
}

die("[rvk] Failed to read shader from %!", path);
}

Async::Task<Shader_Loader::Token> Shader_Loader::compile_async(Async::Pool<>& pool,
String_View path) {

if(Opt<Vec<u8, Files::Alloc>> data = co_await Async::read(pool, path)) {

// Will compile on another thread
Shader shader{device.dup(), data->slice()};
Files::Write_Watcher watcher{path};

Token token = next_token.incr();
{
Thread::Lock lock{mutex};
shaders.insert(token, Pair{move(shader), move(watcher)});
}

co_return token;
}

die("[rvk] Failed to read shader from %!", path);
}

void Shader_Loader::try_reload() {
Thread::Lock lock{mutex};

Region(R) {
Map<Reload_Token, Empty<>, Mregion<R>> callbacks_to_run;

for(auto& [token, shader] : shaders) {
if(shader.second.poll()) {
if(Opt<Vec<u8, Files::Alloc>> data = shader.second.read()) {
shader.first = Shader{device.dup(), data->slice()};
callbacks_to_run.insert(reloads.get(token), {});
}
}
}

for(auto [token, _] : callbacks_to_run) {
static_cast<void>(_);
callbacks.get(token)(*this);
}
}
}

void Shader_Loader::on_reload(Slice<Shader_Loader::Token> tokens,
Function<void(Shader_Loader&)> callback) {
Thread::Lock lock{mutex};

Reload_Token reload_token = next_reload_token++;
callbacks.insert(reload_token, move(callback));

for(auto token : tokens) {
reloads.insert(token, reload_token);
}
}

} // namespace rvk
Loading

0 comments on commit 435d121

Please sign in to comment.