Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Headset.h
Go to the documentation of this file.
1#pragma once
2#include <glm/ext/matrix_float4x4.hpp>
3#include <openxr/openxr.h>
4#include <vector>
5#include <memory>
6#include <vulkan/vulkan_core.h>
7
9
10namespace EngineCore
11{
13 class Renderer;
14}
15
16namespace EngineCore
17{
18
26{
27 VkImage image = nullptr;
28 VkImageView imageView = nullptr;
29 VkFramebuffer framebuffer = nullptr;
30
31 void cleanup(const ApplicationContext* context);
32
33private:
34 VkImageLayout imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
35};
36
38{
39 public:
40 Headset(const ApplicationContext* context = nullptr);
41 ~Headset();
42
43 void cleanup();
44
46 {
47 Error, // An error occurred
48 RenderFully, // Render this frame normally
49 SkipRender, // Skip rendering the frame but end it
50 SkipFully, // Skip processing this frame entirely without ending it
51 ComputeOnly // Run compute shaders but skip XR/mesh rendering (COMPUTE_DEBUG mode)
52 };
53
58 {
60 bool shouldCallBeginFrame; // Whether beginXrFrameAfterWait should be called
61 };
62
69 void updateViewMatrix();
70
79
87 BeginFrameResult beginXrFrameAfterWait(uint32_t& swapchainImageIndex);
88
93 BeginFrameResult beginXrFrame(uint32_t& swapchainImageIndex, Renderer *renderer);
94 void endXrFrame();
95 void endXrFrameNoRender();
96
97 void beginXrSession() const;
105 [[nodiscard]] VkImage getSwapchainImage(size_t swapchainImageIndex) const;
106
107 std::vector<RenderTarget> getSwapchainRenderTargets();
108
115 void endXrSession() const;
116
122 void requestExitSession();
123
132 [[nodiscard]] uint32_t getEyeCount() const;
133
144 [[nodiscard]] VkExtent2D getEyeResolution(size_t eyeIndex) const;
145
154 [[nodiscard]] VkRenderPass getRenderPass() const;
155
156
157
168 [[nodiscard]] glm::mat4 getEyeViewMatrix(size_t eyeIndex) const;
169
174 void setPlayerPosition(const glm::vec3& position) { playerPosition_ = position; }
175
180 [[nodiscard]] glm::vec3 getPlayerPosition() const { return playerPosition_; }
181
192 [[nodiscard]] glm::mat4 getEyeProjectionMatrix(size_t eyeIndex) const;
193
204 [[nodiscard]] glm::mat4 getViewProjectionMatrix(size_t eyeIndex) const;
205
214 [[nodiscard]] XrSpace getReferenceSpace() const;
215
224 [[nodiscard]] XrSession getSession() const;
225
234 [[nodiscard]] bool getIsExitRequested() const;
235
244 [[nodiscard]] XrFrameState getXrFrameState() const;
245
246 private:
247 XrViewState xrViewState = {};
248 XrFrameState xrFrameState = {};
249
250 XrSessionState xrSessionState = XR_SESSION_STATE_UNKNOWN;
251
252 bool isExistRequested = false;
253
254 const ApplicationContext* context = nullptr;
255
259 VkRenderPass vkRenderPass = VK_NULL_HANDLE;
260
264 XrSession xrSession = XR_NULL_HANDLE;
265
269 XrSpace xrReferenceSpace = XR_NULL_HANDLE;
273 XrReferenceSpaceType xrReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL;
274
275 uint32_t eyeCount = 0u;
276
277 std::vector<XrViewConfigurationView> eyeImageInfos;
278
279 std::vector<XrView> eyePoses;
280
282 {
283 VkImage image = nullptr;
284 VkDeviceMemory deviceMemory = nullptr;
285 VkImageView imageView = nullptr;
286
288 };
289
292
293 // Hi-Z Sampler (shared across all frames - the sampler configuration doesn't change)
294 // Note: Hi-Z pyramids are now per-frame in RenderProcess to avoid cross-frame hazards
295 VkSampler hiZSampler_ = VK_NULL_HANDLE;
296
297 void createHiZSampler(VkDevice device);
298
299 XrSwapchain xrSwapchain = nullptr;
300
301 // Named thread pool for xrWaitFrame timeout handling (visible in Tracy as "XR Wait 1")
302 std::unique_ptr<NamedThreadPool> xrWaitThreadPool_;
303
304 private:
308 std::vector<RenderTarget> swapchainRenderTargets;
309
310 std::vector<XrCompositionLayerProjectionView> eyeRenderInfos;
311 struct Eye
312 {
313 glm::mat4 eyeViewMatrix{};
315 };
316 std::vector<Eye> eyes = {};
317
318 XrTime lastUpdateTime = 0;
319
320 // Player position offset applied to view matrices
321 glm::vec3 playerPosition_{0.0f, 0.0f, 0.0f};
322
323 public:
324 const RenderTarget* getRenderTarget(size_t swapchainImageIndex) const;
325
326 // Hi-Z sampler accessor (sampler is shared, pyramids are now per-frame in RenderProcess)
327 [[nodiscard]] VkSampler getHiZSampler() const { return hiZSampler_; }
328 [[nodiscard]] VkImage getDepthBufferImage() const { return depthBuffer.image; }
329 [[nodiscard]] VkImageView getDepthBufferView() const { return depthBuffer.imageView; }
330 [[nodiscard]] VkImage getColorBufferImage() const { return colorBuffer.image; }
331 [[nodiscard]] VkImageView getColorBufferView() const { return colorBuffer.imageView; }
332};
333} // namespace EngineCore
The application context is the core class which stores the basic openxr and vulkan objects.
VkSampler getHiZSampler() const
Definition Headset.h:327
VkExtent2D getEyeResolution(size_t eyeIndex) const
Gets eye resolution in x and y for a specified eye index. 0 is left.
Definition Headset.cpp:953
std::vector< XrView > eyePoses
Definition Headset.h:279
VkSampler hiZSampler_
Definition Headset.h:295
void beginXrSession() const
Definition Headset.cpp:911
glm::mat4 getEyeViewMatrix(size_t eyeIndex) const
Returns the view matrix of the specified eye.
Definition Headset.cpp:981
std::vector< RenderTarget > swapchainRenderTargets
The swapchain render targets. The images get rendered onto those render targets.
Definition Headset.h:308
std::unique_ptr< NamedThreadPool > xrWaitThreadPool_
Definition Headset.h:302
glm::mat4 getViewProjectionMatrix(size_t eyeIndex) const
Gets you a matrix of the multiplied view and projection matrix.
Definition Headset.cpp:994
VkImageView getColorBufferView() const
Definition Headset.h:331
XrSession getSession() const
Gets the active xr session.
Definition Headset.cpp:1004
XrSpace xrReferenceSpace
The xr reference space.
Definition Headset.h:269
std::vector< XrCompositionLayerProjectionView > eyeRenderInfos
Definition Headset.h:310
void endXrSession() const
ends the xr session and thus terminating the xr window on the headset
Definition Headset.cpp:926
uint32_t eyeCount
Definition Headset.h:275
void setPlayerPosition(const glm::vec3 &position)
Sets the player world position offset. This offset is applied to all view matrices.
Definition Headset.h:174
XrReferenceSpaceType xrReferenceSpaceType
Type of the xr reference space, if it is room scale or just sitting vr.
Definition Headset.h:273
void endXrFrameNoRender()
Definition Headset.cpp:899
ImageBuffer colorBuffer
Definition Headset.h:290
std::vector< XrViewConfigurationView > eyeImageInfos
Definition Headset.h:277
XrViewState xrViewState
Definition Headset.h:247
XrSessionState xrSessionState
Definition Headset.h:250
void requestExitSession()
Requests the XR runtime to exit the session gracefully. This should be called when the application wa...
Definition Headset.cpp:931
VkImageView getDepthBufferView() const
Definition Headset.h:329
VkImage getColorBufferImage() const
Definition Headset.h:330
XrTime lastUpdateTime
Definition Headset.h:318
std::vector< Eye > eyes
Definition Headset.h:316
void updateViewMatrix()
Updates the eye matrix for the headset.
Definition Headset.cpp:587
XrSession xrSession
The xr session.
Definition Headset.h:264
std::vector< RenderTarget > getSwapchainRenderTargets()
Definition Headset.cpp:922
Headset(const ApplicationContext *context=nullptr)
Definition Headset.cpp:28
WaitFrameResult waitForXrFrame(Renderer *renderer)
First part of frame setup: calls xrWaitFrame. Can be called before previous xrEndFrame completes....
Definition Headset.cpp:604
glm::mat4 getEyeProjectionMatrix(size_t eyeIndex) const
Gets the projection matrix for an eye.
Definition Headset.cpp:989
BeginFrameResult beginXrFrame(uint32_t &swapchainImageIndex, Renderer *renderer)
Legacy function that combines waitForXrFrame and beginXrFrameAfterWait. Kept for compatibility but pr...
Definition Headset.cpp:853
bool getIsExitRequested() const
Gets whether the headset has requested for the application to stop.
Definition Headset.cpp:1009
XrSwapchain xrSwapchain
Definition Headset.h:299
XrSpace getReferenceSpace() const
Gets reference space for the xr headset.
Definition Headset.cpp:999
ImageBuffer depthBuffer
Definition Headset.h:291
void createHiZSampler(VkDevice device)
Definition Headset.cpp:1023
BeginFrameResult beginXrFrameAfterWait(uint32_t &swapchainImageIndex)
Second part of frame setup: calls xrBeginFrame, acquires swapchain image. MUST be called after previo...
Definition Headset.cpp:814
const RenderTarget * getRenderTarget(size_t swapchainImageIndex) const
Definition Headset.cpp:1018
uint32_t getEyeCount() const
Gets eye count.
Definition Headset.cpp:948
VkImage getSwapchainImage(size_t swapchainImageIndex) const
Gets the swapchain image for the given index.
Definition Headset.cpp:918
XrFrameState xrFrameState
Definition Headset.h:248
const ApplicationContext * context
Definition Headset.h:254
glm::vec3 playerPosition_
Definition Headset.h:321
XrFrameState getXrFrameState() const
gets the current state of the frame
Definition Headset.cpp:1013
VkImage getDepthBufferImage() const
Definition Headset.h:328
glm::vec3 getPlayerPosition() const
Gets the current player world position.
Definition Headset.h:180
VkRenderPass getRenderPass() const
Gets render pass.
Definition Headset.cpp:959
VkRenderPass vkRenderPass
The vulkan render pass.
Definition Headset.h:259
The renderer is the main class for rendering. It owns all data which is used any time in any frame....
Definition Renderer.h:72
Tracy-named thread pool using BS_tracy::tracy_thread_pool.
Log category system implementation.
glm::mat4 eyeProjectionMatrix
Definition Headset.h:314
Result from waitForXrFrame indicating what the main loop should do.
Definition Headset.h:58
A render target which contains all resources to access the rendered image.
Definition Headset.h:26
VkImageLayout imageLayout
Definition Headset.h:34
void cleanup(const ApplicationContext *context)
Definition Headset.cpp:964
VkFramebuffer framebuffer
Definition Headset.h:29
VkImageView imageView
Definition Headset.h:28