Skip to content

Commit

Permalink
more feature implementation and fixes
Browse files Browse the repository at this point in the history
Implemented the requirements to render to the screen in an agnostic manner
- cleaned up some of the includes
- fixed a potential incomplete type being deleted (gfx::render_graph)
- psl::library now supports being initialized without physical backing
  • Loading branch information
JessyDL committed Feb 4, 2024
1 parent 2859c2c commit cccfab5
Show file tree
Hide file tree
Showing 18 changed files with 286 additions and 40 deletions.
12 changes: 12 additions & 0 deletions core/fwd/core/fwd/gfx/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class swapchain;
}
#endif

#if defined(PE_WEBGPU)
namespace core::iwgpu {
class swapchain;
}
#endif

namespace core::gfx {
class swapchain;

Expand All @@ -30,4 +36,10 @@ struct backend_type<swapchain, graphics_backend::gles> {
using type = core::igles::swapchain;
};
#endif
#if defined(PE_WEBGPU)
template <>
struct backend_type<swapchain, graphics_backend::webgpu> {
using type = core::iwgpu::swapchain;
};
#endif
} // namespace core::gfx
2 changes: 2 additions & 0 deletions core/inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ if(${PE_WEBGPU})
set(INC_WEBGPU
iwgpu
context
computepass
drawpass
swapchain
)

Expand Down
29 changes: 26 additions & 3 deletions core/inc/core/gfx/drawpass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "core/gfx/types.hpp"
#include "core/resource/resource.hpp"
#include "psl/view_ptr.hpp"
#include <variant>
#ifdef PE_GLES
namespace core::igles {
class drawpass;
Expand All @@ -13,6 +12,11 @@ namespace core::ivk {
class drawpass;
}
#endif
#if defined(PE_WEBGPU)
namespace core::iwgpu {
class drawpass;
}
#endif

namespace core::gfx {
class context;
Expand All @@ -36,6 +40,12 @@ struct backend_type<drawpass, graphics_backend::gles> {
using type = core::igles::drawpass;
};
#endif
#if defined(PE_WEBGPU)
template <>
struct backend_type<drawpass, graphics_backend::webgpu> {
using type = core::iwgpu::drawpass;
};
#endif

class drawpass {
public:
Expand All @@ -45,9 +55,13 @@ class drawpass {
#ifdef PE_GLES
explicit drawpass(core::igles::drawpass* handle);
#endif
#if defined(PE_WEBGPU)
explicit drawpass(core::iwgpu::drawpass* handle);
#endif

drawpass(core::resource::handle<context> context, core::resource::handle<framebuffer_t> framebuffer);
drawpass(core::resource::handle<context> context, core::resource::handle<swapchain> swapchain);
drawpass([[maybe_unused]] core::resource::handle<context> context,
core::resource::handle<framebuffer_t> framebuffer);
drawpass([[maybe_unused]] core::resource::handle<context> context, core::resource::handle<swapchain> swapchain);
~drawpass();

drawpass(const drawpass& other) = delete;
Expand Down Expand Up @@ -89,6 +103,12 @@ class drawpass {
if constexpr(backend == graphics_backend::gles)
return m_GLESHandle;
#endif
#if defined(PE_WEBGPU)
if constexpr(backend == graphics_backend::webgpu)
return m_WGPUHandle;
#endif

return nullptr;
};

private:
Expand All @@ -98,6 +118,9 @@ class drawpass {
#endif
#ifdef PE_GLES
core::igles::drawpass* m_GLESHandle {nullptr};
#endif
#if defined(PE_WEBGPU)
core::iwgpu::drawpass* m_WGPUHandle {nullptr};
#endif
bool m_Dirty {true};
};
Expand Down
8 changes: 2 additions & 6 deletions core/inc/core/gfx/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ class graph {
};

public:
~graph() {
for(auto& [node, list] : m_Edges) {
delete(node);
}
}
~graph();
template <typename... Args>
T* emplace(Args&&... args) {
m_Edges.emplace_back(std::pair<node*, psl::array<node*>> {new node {T {std::forward<Args>(args)...}}, {}});
Expand Down Expand Up @@ -188,7 +184,7 @@ class render_graph {
using unique_var_t = std::variant<psl::unique_ptr<core::gfx::drawpass>, psl::unique_ptr<core::gfx::computepass>>;

public:
~render_graph() = default;
~render_graph();

psl::view_ptr<core::gfx::drawpass> create_drawpass(core::resource::handle<core::gfx::context> context,
core::resource::handle<core::gfx::swapchain> swapchain);
Expand Down
10 changes: 10 additions & 0 deletions core/inc/core/gfx/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class swapchain {
#ifdef PE_GLES
explicit swapchain(core::resource::handle<core::igles::swapchain>& handle);
#endif
#if defined(PE_WEBGPU)
explicit swapchain(core::resource::handle<core::iwgpu::swapchain>& handle);
#endif

swapchain(core::resource::cache_t& cache,
const core::resource::metadata& metaData,
Expand All @@ -44,6 +47,10 @@ class swapchain {
#ifdef PE_GLES
if constexpr(backend == graphics_backend::gles)
return m_GLESHandle;
#endif
#if defined(PE_WEBGPU)
if constexpr(backend == graphics_backend::webgpu)
return m_WGPUHandle;
#endif
};

Expand All @@ -55,5 +62,8 @@ class swapchain {
#ifdef PE_GLES
core::resource::handle<core::igles::swapchain> m_GLESHandle;
#endif
#if defined(PE_WEBGPU)
core::resource::handle<core::iwgpu::swapchain> m_WGPUHandle;
#endif
};
} // namespace core::gfx
13 changes: 13 additions & 0 deletions core/inc/core/gfx/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
namespace core::gfx {
enum class graphics_backend { undefined = 0, vulkan = 1 << 0, gles = 1 << 1, webgpu = 1 << 2 };

constexpr auto graphics_backend_str(graphics_backend backend) noexcept {
switch(backend) {
case graphics_backend::vulkan:
return "vulkan";
case graphics_backend::gles:
return "gles";
case graphics_backend::webgpu:
return "webgpu";
default:
return "undefined";
}
}

template <typename T, graphics_backend backend>
struct backend_type {};

Expand Down
44 changes: 44 additions & 0 deletions core/inc/core/wgpu/drawpass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include "core/gfx/drawgroup.hpp"
#include "core/gfx/types.hpp"
#include "core/resource/resource.hpp"

namespace core::iwgpu {
class context;
class swapchain;
class framebuffer;
class computepass;

class drawpass {
public:
drawpass(core::resource::handle<context> context, core::resource::handle<swapchain> swapchain);
// drawpass(core::resource::handle<context> context, core::resource::handle<framebuffer> framebuffer);
~drawpass() = default;

drawpass(const drawpass& other) = default;
drawpass(drawpass&& other) noexcept = default;

drawpass& operator=(const drawpass& other) = default;
drawpass& operator=(drawpass&& other) noexcept = default;

void clear();
void prepare();
bool build(bool force = false);
void present();

bool is_swapchain() const noexcept;

void add(core::gfx::drawgroup& group) noexcept;

void connect(psl::view_ptr<drawpass> pass) noexcept;
void connect(psl::view_ptr<computepass> pass) noexcept;
void disconnect(psl::view_ptr<drawpass> pass) noexcept;
void disconnect(psl::view_ptr<computepass> pass) noexcept;

private:
core::resource::handle<context> m_Context {};
core::resource::handle<swapchain> m_Swapchain {};
// core::resource::handle<framebuffer> m_Framebuffer {};
psl::array<core::gfx::drawgroup> m_DrawGroups {};
};
} // namespace core::iwgpu
5 changes: 5 additions & 0 deletions core/inc/core/wgpu/framebuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace core::iwgpu {
class framebuffer {};
} // namespace core::iwgpu
2 changes: 2 additions & 0 deletions core/inc/core/wgpu/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class swapchain {
auto texture() const noexcept -> wgpu::Texture { return m_SwapChain.GetCurrentTexture(); }
auto view() const noexcept -> wgpu::TextureView { return m_SwapChain.GetCurrentTextureView(); }

auto descriptor() noexcept -> wgpu::RenderPassDescriptor;

private:
psl::vec4 m_ClearColor {0.25f, 0.4f, 0.95f, 1.0f};
float m_ClearDepth {1.0f};
Expand Down
38 changes: 14 additions & 24 deletions core/main/main_webgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "core/os/surface.hpp" // the OS surface to draw one

#include "core/gfx/context.hpp"
#include "core/gfx/render_graph.hpp"
#include "core/gfx/swapchain.hpp"

#include <core/wgpu/iwgpu.hpp>
Expand All @@ -36,40 +37,30 @@ using namespace core::gfx;

int entry_agnostic(gfx::graphics_backend backend, core::os::context& os_context) {
core::log->info("Starting the application");

psl::string libraryPath {psl::utility::application::path::library + "resources.metalib"};
memory::region resource_region {20_mb, 4u, new memory::default_allocator()};
psl::string8_t environment = "";
switch(backend) {
case graphics_backend::gles:
environment = "gles";
break;
case graphics_backend::vulkan:
environment = "vulkan";
break;
case graphics_backend::webgpu:
environment = "webgpu";
break;
}

core::log->info("creating a '{}' backend", environment);
core::log->info("creating a '{}' backend", gfx::graphics_backend_str(backend));

core::log->info("creating cache");
cache_t cache {psl::meta::library {psl::to_string8_t(libraryPath), {{environment}}}};
cache_t cache {psl::meta::library {}};
core::log->info("cache created");

auto window_data = cache.instantiate<data::window>("cd61ad53-5ac8-41e9-a8a2-1d20b43376d9"_uid);
window_data->name(APPLICATION_FULL_NAME + " { " + environment + " }");
auto window_data = cache.create<data::window>();
window_data->name(APPLICATION_FULL_NAME + " { " + gfx::graphics_backend_str(backend) + " }");
auto surface_handle = cache.create<core::os::surface>(window_data);
if(!surface_handle) {
core::log->critical("Could not create a OS surface to draw on.");
return -1;
}

auto context_handle = cache.create<core::gfx::context>(backend, psl::string8_t {APPLICATION_NAME}, surface_handle);

auto swapchain_handle = cache.create<core::gfx::swapchain>(surface_handle, context_handle, os_context);

core::gfx::render_graph renderGraph {};


auto swapchain_pass = renderGraph.create_drawpass(context_handle, swapchain_handle);

while(os_context.tick() && surface_handle->tick()) {
renderGraph.present();
}
return 0;
}
Expand Down Expand Up @@ -106,7 +97,6 @@ int entry(gfx::graphics_backend backend, core::os::context& os_context) {
return -1;
}


wgpu::InstanceDescriptor desc = {};
desc.nextInChain = nullptr;
wgpu::Instance instance = wgpu::CreateInstance(&desc);
Expand Down Expand Up @@ -256,7 +246,6 @@ int main(int argc, char** argv) {
throw std::runtime_error("Requested a WebGPU backend, but application does not support WebGPU");
#endif
}
return graphics_backend::undefined;
}
#if defined(PE_VULKAN)
return graphics_backend::vulkan;
Expand All @@ -265,7 +254,8 @@ int main(int argc, char** argv) {
#elif defined(PE_GLES)
return graphics_backend::gles;
#endif
return graphics_backend::undefined;
}(argc, argv);
core::os::context context {};
return entry(backend, context);
return entry_agnostic(backend, context);
}
4 changes: 3 additions & 1 deletion core/src.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ endif()
if(${PE_WEBGPU})
set(SRC_WEBGPU
context
computepass
drawpass
swapchain
)
list(TRANSFORM SRC_WEBGPU PREPEND src/wgpu/)
list(TRANSFORM SRC_WEBGPU APPEND .cpp)
endif()
endif()

0 comments on commit cccfab5

Please sign in to comment.