Foundation
Loading...
Searching...
No Matches
Examples.hpp
Go to the documentation of this file.
1#pragma once
4#include <Math/Math.hpp>
5using namespace Foundation;
6using namespace Core;
7using namespace Math;
8using namespace RenderCore;
9
11 RHIResourceFormat::R8G8B8A8Unorm, RHIResourceFormat::B8G8R8A8Unrom, RHIResourceFormat::R8G8B8A8Srgb,
12 RHIResourceFormat::B8G8R8A8Srgb};
13
15 RHISwapchainPresentMode::Mailbox, RHISwapchainPresentMode::Tearing, RHISwapchainPresentMode::Fifo};
16
17namespace details
18{
19 inline void CreateSwapchain(SDL_Window* window, RHIDevice* device,
21 {
22 int w, h;
23 SDL_GetWindowSizeInPixels(window, &w, &h);
24 LOG(RenderApplication, LogDebug, "Creating swapchain ({}x{})", w, h);
25 device->WaitIdle();
26 if (outSwap)
27 outSwap.Reset();
28 auto format = Ranges::FirstOf(Views::all(kFormatPreferenceList) |
29 Views::filter(Ranges::ContainedBy(device->GetSwapchainSupportedFormats())));
30 auto present = Ranges::FirstOf(Views::all(kPresentModePreferenceList) |
32 CHECK_MSG(format.has_value(), "No supported swapchain format found!");
33 LOG(RenderApplication, LogDebug, "Selected swapchain format: {}", format.value());
34 CHECK_MSG(present.has_value(), "No supported presentation mode found!");
35 LOG(RenderApplication, LogDebug, "Selected swapchain present mode: {}", present.value());
37 .format = format.value(),
38 .extents = RHIExtent3D{w, h, 1},
39 .minBufferCount = 3,
40 .presentMode = present.value(),
41 });
42 }
43}
44// [renderer, app, device, swapchain]
45constexpr int Examples_SDLWindowFlagsVulkan = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_VULKAN;
46inline auto Examples_InitVulkan(SDL_Window* window, RendererDesc const& desc = {})
47{
49 auto device = app->CreateDevice({}, window);
53 return std::make_tuple(renderer, app, std::move(device), std::move(swap));
54}
55// Polls event, possibly resizing the swapchain, and returns true if the window should close.
57{
58 SDL_Event event{};
59 bool any = SDL_PollEvent(&event);
60 if (outEvent)
61 *outEvent = event;
62 if (!any)
63 return false;
64 if (event.window.windowID != SDL_GetWindowID(window))
65 return false;
66 if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) return true;
67 // Resize swapchain if necessary
69 {
71 renderer->SetSwapchain(swap);
72 }
73 return false;
74}
76{
77 renderer->BeginExecute();
78 renderer->ExecuteFrame();
79 renderer->EndExecute();
80}
91
92inline float Examples_GetTime()
93{
94 return static_cast<float>(SDL_GetTicks() / 1e3);
95}
96
97
99{
100 size_t lastTick{};
101 size_t frames{};
102 float fps{};
103 float Update()
104 {
105 size_t now = SDL_GetTicksNS();
106 size_t delta = now - lastTick;
107 if (delta >= 1e9)
108 {
109 fps = 1e9 * (static_cast<float>(frames) / delta);
110 frames = 0;
111 lastTick = now;
112 }
113 frames++;
114 return fps;
115 }
116};
117
118
120{
121 static constexpr char kControlsText[] = "Mouse Left: Rotate | Mouse Right: Pan | Mouse Wheel: Zoom";
122
124 float radius;
125 float pitch, yaw;
129 {
130 if (event.type == SDL_EVENT_MOUSE_MOTION)
131 {
132 if (event.motion.state & SDL_BUTTON_LMASK)
133 {
134 pitch -= event.motion.xrel * 1e-2f;
135 yaw -= event.motion.yrel * 1e-2f;
136 }
137 if (event.motion.state & SDL_BUTTON_RMASK)
138 {
139 quat rot = angleAxis(yaw, vec3(1, 0, 0)) * angleAxis(pitch, vec3(0, 1, 0));
140 vec3 right = rot * vec3(1, 0, 0);
141 vec3 up = rot * vec3(0, 1, 0);
142 center -= right * (event.motion.xrel * radius * 1e-3f);
143 center += up * (event.motion.yrel * radius * 1e-3f);
144 }
145 }
146 if (event.type == SDL_EVENT_MOUSE_WHEEL)
147 {
148 radius -= event.wheel.y * radius * 1e-1f;
149 radius = radius < 1e-3f ? 1e-3f : radius;
150 }
151 // ---
153 quat rot = angleAxis(yaw, vec3(1, 0, 0)) * angleAxis(pitch, vec3(0, 1, 0));
154 vec3 dir = rot * vec3(0, 0, 1);
157 return viewProj;
158 }
159};
#define GLOBAL_ALLOC
Definition Allocator.hpp:221
constexpr int Examples_SDLWindowFlagsVulkan
Definition Examples.hpp:45
constexpr RHISwapchainPresentMode kPresentModePreferenceList[]
Definition Examples.hpp:14
bool Examples_ShouldClose(SDL_Window *window, Renderer *renderer, RHIDeviceScopedHandle< RHISwapchain > &swap, SDL_Event *outEvent=nullptr)
Definition Examples.hpp:56
constexpr RHIResourceFormat kFormatPreferenceList[]
Definition Examples.hpp:10
float Examples_GetTime()
Definition Examples.hpp:92
void Examples_NewFrame(Renderer *renderer)
Definition Examples.hpp:75
auto Examples_InitVulkan(SDL_Window *window, RendererDesc const &desc={})
Definition Examples.hpp:46
auto Examples_DestroyVulkan(SDL_Window *window, Renderer *renderer, VulkanApplication *app, RHIApplicationScopedHandle< RHIDevice > &device, RHIDeviceScopedHandle< RHISwapchain > &swapchain)
Definition Examples.hpp:81
#define LOG(TAG, LEVEL, FORMAT,...)
Definition Logging.hpp:43
#define CHECK_MSG(expr, format_str,...)
Definition Logging.hpp:50
@ LogDebug
Definition Logging.hpp:10
Definition Device.hpp:162
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:78
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:18
void CreateSwapchain(SDL_Window *window, RHIDevice *device, RHIDeviceScopedHandle< RHISwapchain > &outSwap)
Definition Examples.hpp:19
Definition Examples.hpp:99
size_t lastTick
Definition Examples.hpp:100
float fps
Definition Examples.hpp:102
float Update()
Definition Examples.hpp:103
size_t frames
Definition Examples.hpp:101
Definition Examples.hpp:120
float3 center
Definition Examples.hpp:123
float radius
Definition Examples.hpp:124
float yaw
Definition Examples.hpp:125
float zNear
Definition Examples.hpp:126
static constexpr char kControlsText[]
Definition Examples.hpp:121
float fovY
Definition Examples.hpp:126
float pitch
Definition Examples.hpp:125
mat4 view
Definition Examples.hpp:127
mat4 Update(SDL_Event const &event)
Definition Examples.hpp:128
mat4 proj
Definition Examples.hpp:127
float aspect
Definition Examples.hpp:126
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:16