Foundation
Loading...
Searching...
No Matches
Examples.hpp
Go to the documentation of this file.
1#pragma once
4#include <Math/Math.hpp>
6using namespace Foundation;
7using namespace Core;
8using namespace Math;
9using namespace RenderCore;
10
12 RHIResourceFormat::R8G8B8A8Unorm, RHIResourceFormat::B8G8R8A8Unrom, RHIResourceFormat::R8G8B8A8Srgb,
13 RHIResourceFormat::B8G8R8A8Srgb};
14
16 RHISwapchainPresentMode::Mailbox, RHISwapchainPresentMode::Tearing, RHISwapchainPresentMode::Fifo};
17
18namespace details
19{
20 inline void CreateSwapchain(SDL_Window* window, RHIDevice* device,
22 {
23 int w, h;
24 SDL_GetWindowSizeInPixels(window, &w, &h);
25 LOG(RenderApplication, LogDebug, "Creating swapchain ({}x{})", w, h);
26 device->WaitIdle();
27 if (outSwap)
28 outSwap.Reset();
29 auto format = Ranges::FirstOf(Views::all(kFormatPreferenceList) |
30 Views::filter(Ranges::ContainedBy(device->GetSwapchainSupportedFormats())));
31 auto present = Ranges::FirstOf(Views::all(kPresentModePreferenceList) |
33 CHECK_MSG(format.has_value(), "No supported swapchain format found!");
34 LOG(RenderApplication, LogDebug, "Selected swapchain format: {}", format.value());
35 CHECK_MSG(present.has_value(), "No supported presentation mode found!");
36 LOG(RenderApplication, LogDebug, "Selected swapchain present mode: {}", present.value());
38 .format = format.value(),
39 .extents = RHIExtent3D{w, h, 1},
40 .minBufferCount = 3,
41 .presentMode = present.value(),
42 });
43 }
44}
45// [renderer, app, device, swapchain]
46constexpr int Examples_SDLWindowFlagsVulkan = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_VULKAN;
47inline auto Examples_InitVulkan(SDL_Window* window, RendererDesc const& desc = {})
48{
50 auto device = app->CreateDevice({}, window);
54 return std::make_tuple(renderer, app, std::move(device), std::move(swap));
55}
56// Polls event, possibly resizing the swapchain, and returns true if the window should close.
58{
59 SDL_Event event{};
60 bool any = SDL_PollEvent(&event);
61 if (outEvent)
62 *outEvent = event;
63 if (!any)
64 return false;
65 if (event.window.windowID != SDL_GetWindowID(window))
66 return false;
67 if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) return true;
68 // Resize swapchain if necessary
70 {
72 renderer->SetSwapchain(swap);
73 }
74 return false;
75}
77{
78 renderer->BeginExecute();
79 renderer->ExecuteFrame();
80 renderer->EndExecute();
81}
92
93inline float Examples_GetTime()
94{
95 return static_cast<float>(SDL_GetTicks() / 1e3);
96}
97
98
100{
101 size_t lastTick{};
102 size_t frames{};
103 float fps{};
104 float Update()
105 {
106 size_t now = SDL_GetTicksNS();
107 size_t delta = now - lastTick;
108 if (delta >= 1e9)
109 {
110 fps = 1e9 * (static_cast<float>(frames) / delta);
111 frames = 0;
112 lastTick = now;
113 }
114 frames++;
115 return fps;
116 }
117};
118
119
121{
122 static constexpr char kControlsText[] = "Mouse Left: Rotate | Mouse Right: Pan | Mouse Wheel: Zoom";
123
125 float radius;
126 float pitch, yaw;
130 {
131 if (event.type == SDL_EVENT_MOUSE_MOTION)
132 {
133 if (event.motion.state & SDL_BUTTON_LMASK)
134 {
135 pitch -= event.motion.xrel * 1e-2f;
136 yaw -= event.motion.yrel * 1e-2f;
137 }
138 if (event.motion.state & SDL_BUTTON_RMASK)
139 {
140 quat rot = angleAxis(yaw, vec3(1, 0, 0)) * angleAxis(pitch, vec3(0, 1, 0));
141 vec3 right = rot * vec3(1, 0, 0);
142 vec3 up = rot * vec3(0, 1, 0);
143 center -= right * (event.motion.xrel * radius * 1e-3f);
144 center += up * (event.motion.yrel * radius * 1e-3f);
145 }
146 }
147 if (event.type == SDL_EVENT_MOUSE_WHEEL)
148 {
149 radius -= event.wheel.y * radius * 1e-1f;
150 radius = radius < 1e-3f ? 1e-3f : radius;
151 }
152 // ---
154 quat rot = angleAxis(yaw, vec3(1, 0, 0)) * angleAxis(pitch, vec3(0, 1, 0));
155 vec3 dir = rot * vec3(0, 0, 1);
158 return viewProj;
159 }
160};
#define GLOBAL_ALLOC
Definition Allocator.hpp:221
constexpr int Examples_SDLWindowFlagsVulkan
Definition Examples.hpp:46
constexpr RHISwapchainPresentMode kPresentModePreferenceList[]
Definition Examples.hpp:15
bool Examples_ShouldClose(SDL_Window *window, Renderer *renderer, RHIDeviceScopedHandle< RHISwapchain > &swap, SDL_Event *outEvent=nullptr)
Definition Examples.hpp:57
constexpr RHIResourceFormat kFormatPreferenceList[]
Definition Examples.hpp:11
float Examples_GetTime()
Definition Examples.hpp:93
void Examples_NewFrame(Renderer *renderer)
Definition Examples.hpp:76
auto Examples_InitVulkan(SDL_Window *window, RendererDesc const &desc={})
Definition Examples.hpp:47
auto Examples_DestroyVulkan(SDL_Window *window, Renderer *renderer, VulkanApplication *app, RHIApplicationScopedHandle< RHIDevice > &device, RHIDeviceScopedHandle< RHISwapchain > &swapchain)
Definition Examples.hpp:82
#define LOG(TAG, LEVEL, FORMAT,...)
Definition Logging.hpp:39
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:46
@ LogDebug
Definition Logging.hpp:6
Definition Device.hpp:189
virtual Span< RHISwapchainPresentMode const > GetSwapchainSupportedPresentModes() const =0
virtual Span< RHIResourceFormat const > GetSwapchainSupportedFormats() const =0
virtual void WaitIdle() const =0
virtual RHIDeviceScopedHandle< RHISwapchain > CreateSwapchain(RHISwapchain::SwapchainDesc const &desc)=0
Scoped move-only RAII handle wrapper for RHI Objects.
Definition Details.hpp:86
void Reset()
Destructs the underlying RHIObject, and invalidates the scoped handle.
Definition Details.hpp:127
Definition Application.hpp:16
Renderer implementing a Frame Graph system with automatic resource tracking and synchronization.
Definition Renderer.hpp:89
constexpr Optional< range_value_t< T > > FirstOf(T &&range)
Returns the first element of a range, or an empty Optional if the range is empty.
Definition Container.hpp:231
void Destruct(Allocator *resource, T *obj)
Convenience destructor for objects allocated with Construct or ConstructBase.
Definition Allocator.hpp:156
T * Construct(Allocator *resource, Args &&...args)
Convenience placement new with object of type T using a Foundation::Core::Allocator.
Definition Allocator.hpp:149
vec3 float3
Definition Math.hpp:26
mat4 infinitePerspectiveRHReverseZ(float fovY, float a, float zNear)
Definition ModelViewProjection.hpp:7
mat4 viewMatrixRHReverseZ(vec3 pos, quat rot)
Definition ModelViewProjection.hpp:26
RHISwapchainPresentMode
Definition Swapchain.hpp:11
glm::vec< 3, uint32_t > RHIExtent3D
Definition Common.hpp:11
RHIResourceFormat
Definition Common.hpp:20
Definition Allocator.hpp:5
Definition Examples.hpp:19
void CreateSwapchain(SDL_Window *window, RHIDevice *device, RHIDeviceScopedHandle< RHISwapchain > &outSwap)
Definition Examples.hpp:20
Definition Examples.hpp:100
size_t lastTick
Definition Examples.hpp:101
float fps
Definition Examples.hpp:103
float Update()
Definition Examples.hpp:104
size_t frames
Definition Examples.hpp:102
Definition Examples.hpp:121
float3 center
Definition Examples.hpp:124
float radius
Definition Examples.hpp:125
float yaw
Definition Examples.hpp:126
float zNear
Definition Examples.hpp:127
static constexpr char kControlsText[]
Definition Examples.hpp:122
float fovY
Definition Examples.hpp:127
float pitch
Definition Examples.hpp:126
mat4 view
Definition Examples.hpp:128
mat4 Update(SDL_Event const &event)
Definition Examples.hpp:129
mat4 proj
Definition Examples.hpp:128
float aspect
Definition Examples.hpp:127
Range predicate that checks if a value is contained within a given range.
Definition Container.hpp:219
RHIResourceFormat format
Definition Swapchain.hpp:25
Parameters for Renderer creation.
Definition Renderer.hpp:17