Skip to content

Commit

Permalink
improved type info
Browse files Browse the repository at this point in the history
  • Loading branch information
YukinoHayakawa committed Mar 6, 2022
1 parent 30fa99e commit 7ecf834
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
28 changes: 28 additions & 0 deletions DemoHelloSwapchain/AppConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <Usagi/Modules/Resources/ResWindowManager/HeapWindowManager.hpp>
#include <Usagi/Modules/Runtime/HeapManager/HeapManagerStatic.hpp>
#include <Usagi/Runtime/Service/LazyInitService.hpp>
#include <Usagi/Runtime/Service/ServiceAccess.hpp>
#include <Usagi/Runtime/Service/Service.hpp>

#include <Usagi/Modules/Platforms/Vulkan/VulkanGpuDevice.hpp>
#include <Usagi/Modules/Resources/ResGraphicsVulkan/HeapVulkanObjectManager.hpp>
#include <Usagi/Modules/Resources/ResVulkanWSI/RbVulkanSwapchain.hpp>

namespace usagi
{
USAGI_DECL_ALIAS_SERVICE(
ServiceHardwareGraphics,
LazyInitService<VulkanGpuDevice>
);

USAGI_DECL_SERVICE_ALIAS(ServiceHardwareGraphics, graphics);

using AppServiceHeapManager = SimpleService<HeapManagerStatic<
HeapWindowManager,
HeapVulkanObjectManager // todo this should be platform independent name
>>;

USAGI_DECL_SERVICE_ALIAS(AppServiceHeapManager, heap_manager);
}
1 change: 1 addition & 0 deletions DemoHelloSwapchain/DemoHelloSwapchain.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AppConfig.hpp" />
<ClInclude Include="ServiceColorChoice.hpp" />
<ClInclude Include="SystemClearSwapchainImage.hpp" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions DemoHelloSwapchain/DemoHelloSwapchain.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
<ClInclude Include="ServiceColorChoice.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AppConfig.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions DemoHelloSwapchain/ServiceColorChoice.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <Usagi/Runtime/Service/ServiceAccess.hpp>

namespace usagi
{
struct ServiceColorChoice
Expand All @@ -10,4 +12,6 @@ struct ServiceColorChoice

int color_choice = 0;
};

USAGI_DECL_SERVICE_ALIAS(ServiceColorChoice, color_choice);
}
43 changes: 24 additions & 19 deletions DemoHelloSwapchain/SystemClearSwapchainImage.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#pragma once

#include <Usagi/Entity/EntityDatabase.hpp>
#include <Usagi/Modules/Common/Color/Color.hpp>
#include <Usagi/Runtime/Service/Service.hpp>
#include <Usagi/Runtime/Service/LazyInitService.hpp>

#include <Usagi/Modules/Platforms/Vulkan/VulkanGpuDevice.hpp>
#include <Usagi/Runtime/Service/ServiceAccess.hpp>
#include <Usagi/Modules/IO/Graphics/Enum.hpp>
#include <Usagi/Modules/Resources/ResVulkanWSI/RbVulkanSwapchain.hpp>
#include <Usagi/Modules/Runtime/Executive/ServiceAsyncWorker.hpp>

#include "ServiceColorChoice.hpp"

namespace usagi
{
USAGI_DECL_ALIAS_SERVICE(
ServiceHardwareGraphics,
LazyInitService<VulkanGpuDevice>
);

struct SystemClearSwapchainImage
{
using WriteAccess = C<>;
using ReadAccess = C<>;

void update(auto &&rt, auto &&db);
using ServiceAccessT = ServiceAccess<
ServiceHardwareGraphics,
ServiceColorChoice,
ServiceAsyncWorker,
AppServiceHeapManager
>;

void update(ServiceAccessT rt, auto &&db);
};

void SystemClearSwapchainImage::update(auto &&rt, auto &&db)
void SystemClearSwapchainImage::update(ServiceAccessT rt, auto &&db)
{
// The color used to fill the swapchain image
static const Color4f Colors[] {
Expand All @@ -37,11 +37,10 @@ void SystemClearSwapchainImage::update(auto &&rt, auto &&db)
// todo: need a way to allow different impl while providing an unified service name
// This service provides access to GPU resources with automatic lifetime
// management.
auto &gfx = USAGI_SERVICE(rt, ServiceHardwareGraphics);
auto &gfx = rt.graphics();
// This services synchronizes window entities with the operating system.
// auto &wnd_mgr = USAGI_SERVICE(rt, ServiceNativeWindowManager);
auto &color = USAGI_SERVICE(rt, ServiceColorChoice);
auto &worker = USAGI_SERVICE(rt, ServiceAsyncWorker);
const auto &color = rt.color_choice();
auto &worker = rt.executor();
auto &heap_mgr = rt.heap_manager();

// todo this is ugly
Expand All @@ -53,7 +52,10 @@ void SystemClearSwapchainImage::update(auto &&rt, auto &&db)
// todo: how to know when the window is destroyed so the resource can be released.
// todo: how to specify the swapchain spec
// todo this is hell difficult to debug with when it doesn't compile
auto swapchain_accessor = heap_mgr.template resource<typename GpuDevice::RbSwapchain>(
// todo: no type info for returned resource
// auto swapchain_accessor = heap_mgr.resource<GpuDevice::RbSwapchain>(
ResourceAccessor<GpuDevice::RbSwapchain> swapchain_accessor =
heap_mgr.resource<GpuDevice::RbSwapchain>(
{ },
&worker,
[] {
Expand All @@ -66,10 +68,13 @@ void SystemClearSwapchainImage::update(auto &&rt, auto &&db)
NativeWindowState::NORMAL
);
}
).make_request();
).make_request(); // return type of make_request can be deduced if templated param db is removed
if(!swapchain_accessor.ready()) return;

// todo help auto completion infer return type
auto swapchain = swapchain_accessor.get();
// swapchain->
// RefCounted<VulkanSwapchain> swapchain = swapchain_accessor.get();

// prepare synchronization primitives
auto sem_image_avail = gfx.allocate_semaphore();
Expand All @@ -78,7 +83,7 @@ void SystemClearSwapchainImage::update(auto &&rt, auto &&db)

const auto image_info = swapchain->acquire_next_image(
sem_image_avail.get());

// The graphics service internally manages a command list allocator
// for each thread.
// todo: how to know when the thread is destroyed so we can release the resource (perhaps needs some common mechanism as the window)
Expand Down
37 changes: 9 additions & 28 deletions DemoHelloSwapchain/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@

// #define NDEBUG

#ifdef _DEBUG
#pragma comment(lib, "fmtd.lib")
#else
#pragma comment(lib, "fmt.lib")
#endif

#include <fmt/ostream.h>

#include <Usagi/Modules/Runtime/Executive/AppHost.hpp>
#include <Usagi/Modules/Runtime/Executive/SystemTaskList.hpp>
#include <Usagi/Modules/Common/Math/ComponentRegion2D.hpp>
Expand All @@ -24,32 +16,29 @@

#include <Usagi/Modules/Platforms/WinCommon/Input/InputEventSourceWin32RawInput.hpp>
#include <Usagi/Modules/Platforms/WinCommon/Windowing/NativeWindowManagerWin32.hpp>
#include <Usagi/Modules/Resources/ResWindowManager/HeapWindowManager.hpp>
#include <Usagi/Modules/Runtime/Executive/ServiceAsyncWorker.hpp>
#include <Usagi/Modules/Runtime/HeapManager/HeapManagerStatic.hpp>

using namespace usagi;

#include "AppConfig.hpp"

#include "ServiceColorChoice.hpp"
#include "SystemClearSwapchainImage.hpp"

using namespace usagi;

struct Services
: ServiceInputSource
, ServiceNativeWindowManager
, ServiceStateTransitionGraph
, ServiceHardwareGraphics
, ServiceColorChoice
, HeapManagerStatic<
HeapWindowManager,
HeapVulkanObjectManager
>
, AppServiceHeapManager
, ServiceAsyncWorker
{
Services()
: ServiceInputSource(Tag<InputEventSourceWin32RawInput>())
// todo: use templatized type to help devirtualization (but how to change service provider during runtime?)
, ServiceNativeWindowManager(Tag<NativeWindowManagerWin32>())
, HeapManagerStatic(
, AppServiceHeapManager(
std::forward_as_tuple(), // Window Manager
std::forward_as_tuple(&ServiceHardwareGraphics::get_service()) // Vulkan
)
Expand Down Expand Up @@ -80,8 +69,7 @@ int main(int argc, char *argv[])
InputEventQueue input_event_queue;
SystemInputEventPump pump;

/*
if(std::ranges::distance(db.create_access<ComponentAccessReadOnly>()
/*if(std::ranges::distance(db.create_access<ComponentAccessReadOnly>()
.view<ComponentNativeWindow>()) == 0)
{
auto &c_wnd = gArchetypeWindow.component<ComponentNativeWindow>();
Expand All @@ -91,22 +79,14 @@ int main(int argc, char *argv[])
c_region.size = { 1280, 720 };
c_region.position = { 100, 100 };
db.insert(gArchetypeWindow);
}
*/
}*/

// auto &wnd_mgr = USAGI_SERVICE(gServices, ServiceNativeWindowManager);
[[maybe_unused]]
auto &rt = app.services();
auto &tg = USAGI_SERVICE(app.services(), ServiceStateTransitionGraph);
[[maybe_unused]]
auto &gfx = USAGI_SERVICE(app.services(), ServiceHardwareGraphics);
auto &color = USAGI_SERVICE(app.services(), ServiceColorChoice);
// gfx.set_thread_resource_pool_size(1);
[[maybe_unused]]
auto &heap_mgr = app.services().heap_manager();
// using GpuDevice = std::remove_reference_t<decltype(*&gfx)>;
[[maybe_unused]]
auto &worker = USAGI_SERVICE(rt, ServiceAsyncWorker);

while(!tg.should_exit)
{
Expand All @@ -120,6 +100,7 @@ int main(int argc, char *argv[])
ComponentAccessAllowAll
>();

// bug window not responsive
for(auto &&e : view_access.view<ComponentInputEvent>())
{
auto &msg = e.component<ComponentInputEvent>();
Expand Down

0 comments on commit 7ecf834

Please sign in to comment.