From 32c4adc4dca5be89101d7caa3f8998468c435168 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Sun, 10 Nov 2024 22:16:01 +0000 Subject: [PATCH 1/9] make surface optional --- example/example0.cpp | 11 +++++++++-- src/rndr/types/globals.cpp | 9 ++++++++- src/rndr/types/globals.h | 5 ++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/example/example0.cpp b/example/example0.cpp index b8b8aed..ccb3bb2 100644 --- a/example/example0.cpp +++ b/example/example0.cpp @@ -79,8 +79,15 @@ ustd::result renderFrame(rndr::Application &program_gpu) program_gpu.processEvents(); /* get current texture to write to */ + auto surface_optional = program_gpu.getSurface(); + if (!surface_optional) { + return ustd::unexpected(surface_optional.message()); + } + + const wgpu::Surface &surface = *surface_optional; + wgpu::SurfaceTexture surface_tex; - program_gpu.getSurface().GetCurrentTexture(&surface_tex); + surface.GetCurrentTexture(&surface_tex); if (surface_tex.status != wgpu::SurfaceGetCurrentTextureStatus::Success) { std::ostringstream oss; @@ -148,7 +155,7 @@ ustd::result renderFrame(rndr::Application &program_gpu) /* program_gpu.blockOnFuture(work_future); */ /* finally, present the next texture */ - program_gpu.getSurface().Present(); + surface.Present(); return {}; } diff --git a/src/rndr/types/globals.cpp b/src/rndr/types/globals.cpp index b52f4be..c838a8b 100644 --- a/src/rndr/types/globals.cpp +++ b/src/rndr/types/globals.cpp @@ -20,6 +20,10 @@ namespace rndr { +Globals::Globals(bool uses_surface) : uses_surface_(uses_surface) +{ +} + Globals::~Globals() { /* manually release wgpu surface-related assets */ @@ -258,8 +262,11 @@ const wgpu::Limits &Globals::getLimits() return limits_; } -const wgpu::Surface &Globals::getSurface() +const ustd::expected Globals::getSurface() { + if (!uses_surface_) { + return ustd::unexpected("Context was not configured to use a surface."); + } return surface_; } diff --git a/src/rndr/types/globals.h b/src/rndr/types/globals.h index 427dfd2..3f84f6c 100644 --- a/src/rndr/types/globals.h +++ b/src/rndr/types/globals.h @@ -27,6 +27,8 @@ class Globals { */ void setRequiredFeatures(std::vector required_features); + Globals(bool uses_surface = true); + virtual ~Globals(); /** @@ -48,7 +50,7 @@ class Globals { const wgpu::Device &getDevice(); const wgpu::Limits &getLimits(); - const wgpu::Surface &getSurface(); + const ustd::expected getSurface(); const std::vector &getFeatures(); const wgpu::Queue &getQueue(); GLFWwindow *getWindow(); @@ -85,6 +87,7 @@ class Globals { ustd::result initializeWebGPU(); ustd::result initializeGLFW(); + const bool uses_surface_; bool initialized_ = false; }; From 89192a3893705d1b3e568c928ef48429625a1d17 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Sun, 10 Nov 2024 22:43:34 +0000 Subject: [PATCH 2/9] rename file to context --- example/example0.cpp | 10 +-- src/rndr/CMakeLists.txt | 4 +- src/rndr/application.cpp | 68 ------------------- src/rndr/application.h | 63 ----------------- src/rndr/{types/globals.cpp => context.cpp} | 24 ++++--- src/rndr/{types/globals.h => context.h} | 16 ++--- src/rndr/resources/material.h | 4 +- src/rndr/types/CMakeLists.txt | 2 - src/rndr/utils/helpers.h | 2 +- ...pplication.tests.cpp => context.tests.cpp} | 8 +-- 10 files changed, 36 insertions(+), 165 deletions(-) delete mode 100644 src/rndr/application.cpp delete mode 100644 src/rndr/application.h rename src/rndr/{types/globals.cpp => context.cpp} (93%) rename src/rndr/{types/globals.h => context.h} (89%) rename tests/sanity/{application.tests.cpp => context.tests.cpp} (63%) diff --git a/example/example0.cpp b/example/example0.cpp index ccb3bb2..f0377af 100644 --- a/example/example0.cpp +++ b/example/example0.cpp @@ -1,4 +1,4 @@ -#include "rndr/application.h" +#include "rndr/context.h" #include "rndr/math/ops.h" #include "rndr/utils/helpers.h" #include @@ -8,7 +8,7 @@ constexpr int w = 640; constexpr int h = 480; -ustd::result performBufferCopies(rndr::Application &program_gpu) +ustd::result performBufferCopies(rndr::Globals &program_gpu) { const wgpu::Device &device = program_gpu.getDevice(); @@ -68,7 +68,7 @@ ustd::result performBufferCopies(rndr::Application &program_gpu) return {}; } -ustd::result renderFrame(rndr::Application &program_gpu) +ustd::result renderFrame(rndr::Globals &program_gpu) { const wgpu::Device &device = program_gpu.getDevice(); @@ -162,8 +162,8 @@ ustd::result renderFrame(rndr::Application &program_gpu) int main() { - rndr::Application program_gpu; - ustd::result init_result = program_gpu.initialize(); + rndr::Globals program_gpu; + ustd::result init_result = program_gpu.initialize(); if (!init_result.ok()) { std::cerr << "Initialization failed for reason: " << init_result; diff --git a/src/rndr/CMakeLists.txt b/src/rndr/CMakeLists.txt index ed848d6..f2fd7e9 100644 --- a/src/rndr/CMakeLists.txt +++ b/src/rndr/CMakeLists.txt @@ -1,6 +1,6 @@ add_library(rndr STATIC - ${CMAKE_CURRENT_SOURCE_DIR}/application.h - ${CMAKE_CURRENT_SOURCE_DIR}/application.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/context.h + ${CMAKE_CURRENT_SOURCE_DIR}/context.cpp ) Include(FetchContent) diff --git a/src/rndr/application.cpp b/src/rndr/application.cpp deleted file mode 100644 index a50564a..0000000 --- a/src/rndr/application.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/** - * @file application.cpp - * @author Jackson Wyatt Kaplan (JwyattK@gmail.com) - * @brief - * @version 0.1 - * @date 2023-07-15 - * - * @copyright Copyright (c) 2023 - * - */ - -#include "application.h" -#include "rndr/utils/helpers.h" - -#include -#include -#include -#include - -namespace rndr { - -using namespace wgpu; - -Application::Application() - : support_dir_(RNDR_SUPPORT_DIR), shader_dir_(support_dir_ / "shaders") -{ - /** @todo DELETE! */ - /* - assert(support_dir_.is_absolute() && std::filesystem::exists(support_dir_)); - assert(shader_dir_.is_absolute() && std::filesystem::exists(shader_dir_)); - - collectShaderSource_(true); - */ -} - -bool Application::addShaderSource(std::filesystem::path shader_path) -{ - using namespace std::filesystem; - - if (!exists(shader_path) || shader_path.extension() != ".wgsl") { - return false; - } - - std::stringstream out; - std::ifstream ifs(shader_path); - out << ifs.rdbuf(); - - auto name = shader_path.string().substr(shader_dir_.string().size() + 1); - shader_code_[name] = {name, shader_path, out.str()}; - return true; -} - -void Application::collectShaderSource_(bool rescan) -{ - - using namespace std::filesystem; - - if (rescan) { - shader_code_.clear(); - }; - - for (auto const &dir_entry : recursive_directory_iterator{shader_dir_}) { - auto p = dir_entry.path(); - addShaderSource(p); - } -} - -} // namespace rndr diff --git a/src/rndr/application.h b/src/rndr/application.h deleted file mode 100644 index 17a5999..0000000 --- a/src/rndr/application.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * @file application.h - * @author Jackson Wyatt Kaplan (JwyattK@gmail.com) - * @brief - * @version 0.1 - * @date 2023-07-15 - * - * @copyright Copyright (c) 2023 - * - */ - -#ifndef RNDR_APPLICATION_H_ -#define RNDR_APPLICATION_H_ - -#include "rndr/types/globals.h" - -#include "webgpu/webgpu_cpp.h" -#include -#include - -#ifndef RNDR_SUPPORT_DIR -#error "Must define rndr support directory." -#endif - -namespace rndr { - -class Application : public Globals { -public: - Application(); - ~Application() = default; - - /* non-copyable */ - Application(const Application &) = delete; - Application &operator=(const Application &) = delete; - - /* non-movable */ - Application(Application &&) = delete; - Application &operator=(Application &&) = delete; - - bool addShaderSource(std::filesystem::path shader_path); - - wgpu::TextureFormat get_preferred_texture_format(); - -private: - void collectShaderSource_(bool rescan = false); - - /* Shader storage */ - struct ShaderSource { - std::string name; - std::filesystem::path path; - std::string source; - }; - std::map shader_code_ = {}; - - std::filesystem::path support_dir_ = {}; - std::filesystem::path shader_dir_ = {}; - - std::mutex frame_lock; -}; - -} // namespace rndr - -#endif diff --git a/src/rndr/types/globals.cpp b/src/rndr/context.cpp similarity index 93% rename from src/rndr/types/globals.cpp rename to src/rndr/context.cpp index c838a8b..a6f6728 100644 --- a/src/rndr/types/globals.cpp +++ b/src/rndr/context.cpp @@ -1,5 +1,5 @@ /** - * @file globals.h + * @file context.cpp * @author Jackson Wyatt Kaplan (JwyattK@gmail.com) * @brief * @version 0.1 @@ -11,8 +11,8 @@ #include "rndr/utils/helpers.h" +#include "context.h" #include "glfw3webgpu.h" -#include "globals.h" #include #include @@ -27,7 +27,7 @@ Globals::Globals(bool uses_surface) : uses_surface_(uses_surface) Globals::~Globals() { /* manually release wgpu surface-related assets */ - wgpuSurfaceRelease(surface_.MoveToCHandle()); + wgpuSurfaceRelease(surface_->MoveToCHandle()); glfwDestroyWindow(getWindow()); glfwTerminate(); @@ -61,17 +61,19 @@ ustd::result Globals::initializeWebGPU() return ustd::unexpected("Failed to retrieve instance"); } - surface_ - = wgpu::Surface::Acquire(glfwGetWGPUSurface(instance_.Get(), window_)); + if (uses_surface_) { + surface_ + = wgpu::Surface::Acquire(glfwGetWGPUSurface(instance_.Get(), window_)); - if (surface_.Get() == nullptr) { - return ustd::unexpected("Failed to retrieve surface"); + if (surface_->Get() == nullptr) { + return ustd::unexpected("Failed to retrieve surface"); + } } /* Request adapter */ wgpu::RequestAdapterOptions adapter_opts = {}; adapter_opts.powerPreference = wgpu::PowerPreference::HighPerformance; - adapter_opts.compatibleSurface = surface_; + adapter_opts.compatibleSurface = surface_ ? *surface_ : nullptr; wgpu::Adapter adapter_; @@ -165,7 +167,9 @@ ustd::result Globals::initializeWebGPU() surface_config.height = height_; surface_config.presentMode = wgpu::PresentMode::Fifo; - surface_.Configure(&surface_config); + if (uses_surface_) { + surface_->Configure(&surface_config); + } return {}; } @@ -267,7 +271,7 @@ const ustd::expected Globals::getSurface() if (!uses_surface_) { return ustd::unexpected("Context was not configured to use a surface."); } - return surface_; + return *surface_; } const std::vector &Globals::getFeatures() diff --git a/src/rndr/types/globals.h b/src/rndr/context.h similarity index 89% rename from src/rndr/types/globals.h rename to src/rndr/context.h index 3f84f6c..3564bae 100644 --- a/src/rndr/types/globals.h +++ b/src/rndr/context.h @@ -1,5 +1,5 @@ /** - * @file globals.h + * @file context.h * @author Jackson Wyatt Kaplan (JwyattK@gmail.com) * @brief * @version 0.1 @@ -9,8 +9,8 @@ * */ -#ifndef RNDR_GLOBALS_H_ -#define RNDR_GLOBALS_H_ +#ifndef RNDR_CONTEXT_H_ +#define RNDR_CONTEXT_H_ #include "ustd/expected.h" @@ -66,12 +66,12 @@ class Globals { void processEvents(); protected: - wgpu::Instance instance_ = {}; - wgpu::Device device_ = {}; - wgpu::Queue queue_ = {}; + wgpu::Instance instance_ = {}; + wgpu::Device device_ = {}; + wgpu::Queue queue_ = {}; - wgpu::Surface surface_ = {}; - GLFWwindow *window_ = nullptr; + std::optional surface_ = {}; + GLFWwindow *window_ = nullptr; /* Features and limits */ std::vector features_ = {}; diff --git a/src/rndr/resources/material.h b/src/rndr/resources/material.h index 3db9fb8..23233b7 100644 --- a/src/rndr/resources/material.h +++ b/src/rndr/resources/material.h @@ -13,7 +13,7 @@ #define RNDR_MATERIAL_H_ #include "renderable_mesh.h" -#include "rndr/types/globals.h" +#include "rndr/context.h" namespace rndr { @@ -36,4 +36,4 @@ class Material : public GlobalAccess { } // namespace rndr -#endif \ No newline at end of file +#endif diff --git a/src/rndr/types/CMakeLists.txt b/src/rndr/types/CMakeLists.txt index 45a9698..2edda30 100644 --- a/src/rndr/types/CMakeLists.txt +++ b/src/rndr/types/CMakeLists.txt @@ -1,5 +1,3 @@ target_sources(rndr PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/globals.h - ${CMAKE_CURRENT_SOURCE_DIR}/globals.cpp ${CMAKE_CURRENT_SOURCE_DIR}/types.h ) diff --git a/src/rndr/utils/helpers.h b/src/rndr/utils/helpers.h index 2e6734a..1733b19 100644 --- a/src/rndr/utils/helpers.h +++ b/src/rndr/utils/helpers.h @@ -9,7 +9,7 @@ * */ -#include +#include "rndr/context.h" #include "webgpu/webgpu_cpp.h" #include diff --git a/tests/sanity/application.tests.cpp b/tests/sanity/context.tests.cpp similarity index 63% rename from tests/sanity/application.tests.cpp rename to tests/sanity/context.tests.cpp index 454eb58..2a0d140 100644 --- a/tests/sanity/application.tests.cpp +++ b/tests/sanity/context.tests.cpp @@ -1,4 +1,4 @@ -#include "rndr/application.h" +#include "rndr/context.h" #include "ustd/expected.h" #include #include @@ -6,9 +6,9 @@ /** TODO unhide this test when we are able to initialize without a surface */ TEST_CASE("Application initializes and destructs without fault", "[.sanity]") { - auto app = std::make_unique(); - ustd::expected init_result = app->initialize(); + auto context = std::make_unique(); + ustd::expected init_result = context->initialize(); INFO("Failed to initialize: " << init_result); REQUIRE(!init_result.ok()); - REQUIRE(app->isInitialized()); + REQUIRE(context->isInitialized()); } From 18218209f45e1e063b928c677d88634e76de14c5 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Sun, 10 Nov 2024 22:45:30 +0000 Subject: [PATCH 3/9] rename globals to context --- example/example0.cpp | 6 +++--- src/rndr/context.cpp | 38 ++++++++++++++++----------------- src/rndr/context.h | 12 +++++------ src/rndr/resources/material.cpp | 4 ++-- src/rndr/resources/material.h | 2 +- src/rndr/utils/helpers.cpp | 2 +- src/rndr/utils/helpers.h | 2 +- tests/sanity/CMakeLists.txt | 2 +- tests/sanity/context.tests.cpp | 2 +- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/example/example0.cpp b/example/example0.cpp index f0377af..48019d9 100644 --- a/example/example0.cpp +++ b/example/example0.cpp @@ -8,7 +8,7 @@ constexpr int w = 640; constexpr int h = 480; -ustd::result performBufferCopies(rndr::Globals &program_gpu) +ustd::result performBufferCopies(rndr::Context &program_gpu) { const wgpu::Device &device = program_gpu.getDevice(); @@ -68,7 +68,7 @@ ustd::result performBufferCopies(rndr::Globals &program_gpu) return {}; } -ustd::result renderFrame(rndr::Globals &program_gpu) +ustd::result renderFrame(rndr::Context &program_gpu) { const wgpu::Device &device = program_gpu.getDevice(); @@ -162,7 +162,7 @@ ustd::result renderFrame(rndr::Globals &program_gpu) int main() { - rndr::Globals program_gpu; + rndr::Context program_gpu; ustd::result init_result = program_gpu.initialize(); if (!init_result.ok()) { diff --git a/src/rndr/context.cpp b/src/rndr/context.cpp index a6f6728..68b5346 100644 --- a/src/rndr/context.cpp +++ b/src/rndr/context.cpp @@ -20,11 +20,11 @@ namespace rndr { -Globals::Globals(bool uses_surface) : uses_surface_(uses_surface) +Context::Context(bool uses_surface) : uses_surface_(uses_surface) { } -Globals::~Globals() +Context::~Context() { /* manually release wgpu surface-related assets */ wgpuSurfaceRelease(surface_->MoveToCHandle()); @@ -39,18 +39,18 @@ Globals::~Globals() } /* PRE-INIT SETTERS */ -void Globals::setRequiredFeatures( +void Context::setRequiredFeatures( std::vector required_features) { required_features_ = std::move(required_features); } -void Globals::setRequiredLimits(wgpu::Limits required_limits) +void Context::setRequiredLimits(wgpu::Limits required_limits) { required_limits_.limits = std::move(required_limits); } -ustd::result Globals::initializeWebGPU() +ustd::result Context::initializeWebGPU() { wgpu::InstanceDescriptor instance_desc = {}; instance_desc.features.timedWaitAnyEnable = true; @@ -174,7 +174,7 @@ ustd::result Globals::initializeWebGPU() return {}; } -ustd::result Globals::initializeGLFW() +ustd::result Context::initializeGLFW() { /* GLFW init */ glfwInitHint(GLFW_CLIENT_API, GLFW_NO_API); @@ -188,7 +188,7 @@ ustd::result Globals::initializeGLFW() return {}; } -ustd::result Globals::initialize(int width, int height) +ustd::result Context::initialize(int width, int height) { width_ = width; height_ = height; @@ -206,7 +206,7 @@ ustd::result Globals::initialize(int width, int height) return {}; } -bool Globals::blockOnFuture(wgpu::Future future) +bool Context::blockOnFuture(wgpu::Future future) { assert(instance_.Get() != nullptr); constexpr auto timeout = std::chrono::milliseconds(100); @@ -219,7 +219,7 @@ bool Globals::blockOnFuture(wgpu::Future future) return wait_info.completed && wait_success; } -wgpu::Future Globals::getSubmittedWorkFuture() +wgpu::Future Context::getSubmittedWorkFuture() { assert(isInitialized()); wgpu::Future future = getQueue().OnSubmittedWorkDone( @@ -231,42 +231,42 @@ wgpu::Future Globals::getSubmittedWorkFuture() return future; } -void Globals::blockOnSubmittedWork() +void Context::blockOnSubmittedWork() { blockOnFuture(getSubmittedWorkFuture()); } -void Globals::processEvents() +void Context::processEvents() { instance_.ProcessEvents(); } -bool Globals::isInitialized() +bool Context::isInitialized() { return initialized_; } -const wgpu::Device &Globals::getDevice() +const wgpu::Device &Context::getDevice() { return device_; } -const wgpu::Queue &Globals::getQueue() +const wgpu::Queue &Context::getQueue() { return queue_; } -GLFWwindow *Globals::getWindow() +GLFWwindow *Context::getWindow() { return window_; } -const wgpu::Limits &Globals::getLimits() +const wgpu::Limits &Context::getLimits() { return limits_; } -const ustd::expected Globals::getSurface() +const ustd::expected Context::getSurface() { if (!uses_surface_) { return ustd::unexpected("Context was not configured to use a surface."); @@ -274,12 +274,12 @@ const ustd::expected Globals::getSurface() return *surface_; } -const std::vector &Globals::getFeatures() +const std::vector &Context::getFeatures() { return features_; } -bool Globals::hasFeature(wgpu::FeatureName feature) +bool Context::hasFeature(wgpu::FeatureName feature) { return std::find(features_.begin(), features_.end(), feature) != features_.end(); diff --git a/src/rndr/context.h b/src/rndr/context.h index 3564bae..1d4c2b8 100644 --- a/src/rndr/context.h +++ b/src/rndr/context.h @@ -20,16 +20,16 @@ namespace rndr { -class Globals { +class Context { public: /** * @brief Set the required features. Call this before a call to `initialize`. */ void setRequiredFeatures(std::vector required_features); - Globals(bool uses_surface = true); + Context(bool uses_surface = true); - virtual ~Globals(); + virtual ~Context(); /** * @brief Set the required limits. Call this before a call to `initialize`. @@ -94,12 +94,12 @@ class Globals { class GlobalAccess { public: - GlobalAccess(Globals &globals) : globals_(globals) + GlobalAccess(Context &globals) : globals_(globals) { } protected: - Globals &getGlobals() + Context &getGlobals() { return globals_; } @@ -110,7 +110,7 @@ class GlobalAccess { } private: - Globals &globals_; + Context &globals_; }; } // namespace rndr diff --git a/src/rndr/resources/material.cpp b/src/rndr/resources/material.cpp index 3a2b6ce..55ea643 100644 --- a/src/rndr/resources/material.cpp +++ b/src/rndr/resources/material.cpp @@ -13,7 +13,7 @@ namespace rndr { -Material::Material(Globals &globals) : GlobalAccess(globals) +Material::Material(Context &globals) : GlobalAccess(globals) { const char *shaderSource = R"( @vertex @@ -103,4 +103,4 @@ fn fs_main() -> @location(0) vec4 { pipeline_ = getDevice().CreateRenderPipeline(&pipeline_desc); } -} // namespace rndr \ No newline at end of file +} // namespace rndr diff --git a/src/rndr/resources/material.h b/src/rndr/resources/material.h index 23233b7..b1efff0 100644 --- a/src/rndr/resources/material.h +++ b/src/rndr/resources/material.h @@ -19,7 +19,7 @@ namespace rndr { class Material : public GlobalAccess { - Material(Globals &globals); + Material(Context &globals); Material(const Material &) = delete; Material &operator=(const Material &) = delete; diff --git a/src/rndr/utils/helpers.cpp b/src/rndr/utils/helpers.cpp index 6ff4b76..0afb9fb 100644 --- a/src/rndr/utils/helpers.cpp +++ b/src/rndr/utils/helpers.cpp @@ -107,7 +107,7 @@ wgpu::Device requestWGPUDevice(wgpu::Adapter adapter) return std::move(udata.device); } -wgpu::RenderPipeline createRenderPipeline(Globals &globals) +wgpu::RenderPipeline createRenderPipeline(Context &globals) { const char *shaderSource = R"( diff --git a/src/rndr/utils/helpers.h b/src/rndr/utils/helpers.h index 1733b19..e094409 100644 --- a/src/rndr/utils/helpers.h +++ b/src/rndr/utils/helpers.h @@ -19,7 +19,7 @@ namespace rndr { -wgpu::RenderPipeline createRenderPipeline(Globals &globals); +wgpu::RenderPipeline createRenderPipeline(Context &globals); namespace helpers { diff --git a/tests/sanity/CMakeLists.txt b/tests/sanity/CMakeLists.txt index 73a277a..0db5a2d 100644 --- a/tests/sanity/CMakeLists.txt +++ b/tests/sanity/CMakeLists.txt @@ -1,3 +1,3 @@ target_sources(tests PRIVATE - application.tests.cpp + context.tests.cpp ) diff --git a/tests/sanity/context.tests.cpp b/tests/sanity/context.tests.cpp index 2a0d140..7e793b8 100644 --- a/tests/sanity/context.tests.cpp +++ b/tests/sanity/context.tests.cpp @@ -6,7 +6,7 @@ /** TODO unhide this test when we are able to initialize without a surface */ TEST_CASE("Application initializes and destructs without fault", "[.sanity]") { - auto context = std::make_unique(); + auto context = std::make_unique(); ustd::expected init_result = context->initialize(); INFO("Failed to initialize: " << init_result); REQUIRE(!init_result.ok()); From 599c1a0362631040e75c4fa6ed6df4f3ed5ced4b Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Sun, 10 Nov 2024 22:52:04 +0000 Subject: [PATCH 4/9] finish renaming and adjust tests --- src/rndr/context.cpp | 4 +++- src/rndr/context.h | 10 +++++----- src/rndr/resources/material.cpp | 2 +- src/rndr/resources/material.h | 2 +- src/rndr/utils/helpers.cpp | 6 +++--- src/rndr/utils/helpers.h | 2 +- tests/sanity/context.tests.cpp | 7 ++++--- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/rndr/context.cpp b/src/rndr/context.cpp index 68b5346..89367bc 100644 --- a/src/rndr/context.cpp +++ b/src/rndr/context.cpp @@ -27,7 +27,9 @@ Context::Context(bool uses_surface) : uses_surface_(uses_surface) Context::~Context() { /* manually release wgpu surface-related assets */ - wgpuSurfaceRelease(surface_->MoveToCHandle()); + if (surface_) { + wgpuSurfaceRelease(surface_->MoveToCHandle()); + } glfwDestroyWindow(getWindow()); glfwTerminate(); diff --git a/src/rndr/context.h b/src/rndr/context.h index 1d4c2b8..a05fd9f 100644 --- a/src/rndr/context.h +++ b/src/rndr/context.h @@ -94,23 +94,23 @@ class Context { class GlobalAccess { public: - GlobalAccess(Context &globals) : globals_(globals) + GlobalAccess(Context &context) : context_(context) { } protected: - Context &getGlobals() + Context &getContext() { - return globals_; + return context_; } const wgpu::Device &getDevice() { - return globals_.getDevice(); + return context_.getDevice(); } private: - Context &globals_; + Context &context_; }; } // namespace rndr diff --git a/src/rndr/resources/material.cpp b/src/rndr/resources/material.cpp index 55ea643..115ca75 100644 --- a/src/rndr/resources/material.cpp +++ b/src/rndr/resources/material.cpp @@ -13,7 +13,7 @@ namespace rndr { -Material::Material(Context &globals) : GlobalAccess(globals) +Material::Material(Context &context) : GlobalAccess(context) { const char *shaderSource = R"( @vertex diff --git a/src/rndr/resources/material.h b/src/rndr/resources/material.h index b1efff0..63b5e63 100644 --- a/src/rndr/resources/material.h +++ b/src/rndr/resources/material.h @@ -19,7 +19,7 @@ namespace rndr { class Material : public GlobalAccess { - Material(Context &globals); + Material(Context &context); Material(const Material &) = delete; Material &operator=(const Material &) = delete; diff --git a/src/rndr/utils/helpers.cpp b/src/rndr/utils/helpers.cpp index 0afb9fb..6bfcb72 100644 --- a/src/rndr/utils/helpers.cpp +++ b/src/rndr/utils/helpers.cpp @@ -107,7 +107,7 @@ wgpu::Device requestWGPUDevice(wgpu::Adapter adapter) return std::move(udata.device); } -wgpu::RenderPipeline createRenderPipeline(Context &globals) +wgpu::RenderPipeline createRenderPipeline(Context &context) { const char *shaderSource = R"( @@ -141,7 +141,7 @@ fn fs_main() -> @location(0) vec4 { shader_module_desc.label = "Default Shader Module"; shader_module_desc.nextInChain = &shader_code_desc; wgpu::ShaderModule shader_module - = globals.getDevice().CreateShaderModule(&shader_module_desc); + = context.getDevice().CreateShaderModule(&shader_module_desc); wgpu::RenderPipelineDescriptor pipeline_desc = {}; pipeline_desc.nextInChain = nullptr; @@ -196,7 +196,7 @@ fn fs_main() -> @location(0) vec4 { pipeline_desc.layout = nullptr; wgpu::RenderPipeline ret - = globals.getDevice().CreateRenderPipeline(&pipeline_desc); + = context.getDevice().CreateRenderPipeline(&pipeline_desc); return ret; } diff --git a/src/rndr/utils/helpers.h b/src/rndr/utils/helpers.h index e094409..dd60cfd 100644 --- a/src/rndr/utils/helpers.h +++ b/src/rndr/utils/helpers.h @@ -19,7 +19,7 @@ namespace rndr { -wgpu::RenderPipeline createRenderPipeline(Context &globals); +wgpu::RenderPipeline createRenderPipeline(Context &context); namespace helpers { diff --git a/tests/sanity/context.tests.cpp b/tests/sanity/context.tests.cpp index 7e793b8..5c9fab2 100644 --- a/tests/sanity/context.tests.cpp +++ b/tests/sanity/context.tests.cpp @@ -4,11 +4,12 @@ #include /** TODO unhide this test when we are able to initialize without a surface */ -TEST_CASE("Application initializes and destructs without fault", "[.sanity]") +TEST_CASE("Application initializes and destructs without fault", "[sanity]") { - auto context = std::make_unique(); + auto context = std::make_unique(false); ustd::expected init_result = context->initialize(); INFO("Failed to initialize: " << init_result); - REQUIRE(!init_result.ok()); + REQUIRE(init_result.ok()); + REQUIRE(!context->getSurface()); REQUIRE(context->isInitialized()); } From 439a0e2da5f3d1796fac8a13cbcae361e7449306 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Mon, 11 Nov 2024 09:26:18 +0000 Subject: [PATCH 5/9] add necessary include for gcc --- src/rndr/context.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rndr/context.h b/src/rndr/context.h index a05fd9f..e533c42 100644 --- a/src/rndr/context.h +++ b/src/rndr/context.h @@ -15,6 +15,7 @@ #include "ustd/expected.h" #include +#include #include #include From 8fd8d9ea194bcb3374ff790e714c671a926a31d0 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Mon, 11 Nov 2024 09:34:33 +0000 Subject: [PATCH 6/9] protect dtor releases --- src/rndr/context.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/rndr/context.cpp b/src/rndr/context.cpp index 89367bc..89e050d 100644 --- a/src/rndr/context.cpp +++ b/src/rndr/context.cpp @@ -34,10 +34,16 @@ Context::~Context() glfwDestroyWindow(getWindow()); glfwTerminate(); - wgpuQueueRelease(queue_.MoveToCHandle()); - wgpuDeviceRelease(device_.MoveToCHandle()); - instance_.ProcessEvents(); - wgpuInstanceRelease(instance_.MoveToCHandle()); + if (queue_) { + wgpuQueueRelease(queue_.MoveToCHandle()); + } + if (device_) { + wgpuDeviceRelease(device_.MoveToCHandle()); + } + if (instance_) { + instance_.ProcessEvents(); + wgpuInstanceRelease(instance_.MoveToCHandle()); + } } /* PRE-INIT SETTERS */ From 124323862da9030a3ad8c872c2a106a1e9c2c330 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Mon, 11 Nov 2024 09:37:32 +0000 Subject: [PATCH 7/9] dont init window if not using surface, duh --- src/rndr/context.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/rndr/context.cpp b/src/rndr/context.cpp index 89e050d..01a6ceb 100644 --- a/src/rndr/context.cpp +++ b/src/rndr/context.cpp @@ -31,8 +31,10 @@ Context::~Context() wgpuSurfaceRelease(surface_->MoveToCHandle()); } - glfwDestroyWindow(getWindow()); - glfwTerminate(); + if (window_) { + glfwDestroyWindow(window_); + glfwTerminate(); + } if (queue_) { wgpuQueueRelease(queue_.MoveToCHandle()); @@ -201,8 +203,10 @@ ustd::result Context::initialize(int width, int height) width_ = width; height_ = height; - if (auto result = initializeGLFW(); !result) { - return result; + if (uses_surface_) { + if (auto result = initializeGLFW(); !result) { + return result; + } } if (auto result = initializeWebGPU(); !result) { From 41d7aae9cc9a5e0ae9e6d0022e254cc658a56d06 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Mon, 11 Nov 2024 14:50:32 +0000 Subject: [PATCH 8/9] fix unit tests --- .github/workflows/unit_tests.yml | 1 + tests/sanity/CMakeLists.txt | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index bd4f11a..6341356 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -11,6 +11,7 @@ on: jobs: test: runs-on: ubuntu-latest + env: ACTIONS_UNIT_TESTS: "1" steps: - name: Apt Install Dependencies diff --git a/tests/sanity/CMakeLists.txt b/tests/sanity/CMakeLists.txt index 0db5a2d..73f5f37 100644 --- a/tests/sanity/CMakeLists.txt +++ b/tests/sanity/CMakeLists.txt @@ -1,3 +1,8 @@ +# Include tests that cannot run on GH actions due to lack of GPU here +if(NOT DEFINED ENV{ACTIONS_UNIT_TESTS}) + target_sources(tests PRIVATE context.tests.cpp ) + +endif() From 4d70b6d6731a642a87b6a5366c35b0a3ea6c56f3 Mon Sep 17 00:00:00 2001 From: Jackson Kaplan Date: Mon, 11 Nov 2024 14:53:15 +0000 Subject: [PATCH 9/9] fixup yaml syntax --- .github/workflows/unit_tests.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 6341356..ae91629 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -11,7 +11,8 @@ on: jobs: test: runs-on: ubuntu-latest - env: ACTIONS_UNIT_TESTS: "1" + env: + ACTIONS_UNIT_TESTS: "1" steps: - name: Apt Install Dependencies