@@ -33,7 +33,7 @@ XrViewConfigurationType VR_GetBestViewConfiguration(XrInstance instance, XrSyste
3333 return XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO ;
3434 }
3535 }
36-
36+
3737 free (viewConfigurations );
3838 return XR_VIEW_CONFIGURATION_TYPE_MAX_ENUM ;
3939}
@@ -42,7 +42,7 @@ uint32_t VR_GetViewConfigurationViews(XrInstance instance, XrSystemId systemId,
4242{
4343 uint32_t viewCount = 0 ;
4444 XR_CHECK (
45- xrEnumerateViewConfigurationViews (instance , systemId , viewConfig , 0 , & viewCount , NULL ),
45+ xrEnumerateViewConfigurationViews (instance , systemId , viewConfig , 0 , & viewCount , NULL ),
4646 "Failed to count ViewConfiguration Views" );
4747
4848 * views = calloc (viewCount , sizeof (XrViewConfigurationView ));
@@ -51,7 +51,7 @@ uint32_t VR_GetViewConfigurationViews(XrInstance instance, XrSystemId systemId,
5151 (* views )[idx ].type = XR_TYPE_VIEW_CONFIGURATION_VIEW ;
5252 }
5353 XR_CHECK (
54- xrEnumerateViewConfigurationViews (instance , systemId , viewConfig , viewCount , & viewCount , * views ),
54+ xrEnumerateViewConfigurationViews (instance , systemId , viewConfig , viewCount , & viewCount , * views ),
5555 "Failed to enumerate ViewConfiguration Views" );
5656
5757 return viewCount ;
@@ -61,9 +61,9 @@ uint32_t VR_GetSwapchainFormats(XrSession session, int64_t** formats)
6161{
6262 uint32_t formatCount = 0 ;
6363 XR_CHECK (
64- xrEnumerateSwapchainFormats (session , 0 , & formatCount , NULL ),
64+ xrEnumerateSwapchainFormats (session , 0 , & formatCount , NULL ),
6565 "Failed to count Swapchain formats" );
66-
66+
6767 * formats = calloc (formatCount , sizeof (int64_t ));
6868 XR_CHECK (
6969 xrEnumerateSwapchainFormats (session , formatCount , & formatCount , * formats ),
@@ -158,7 +158,7 @@ GLuint VR_CreateImageView(GLuint colorImage, GLuint depthImage, uint32_t viewCou
158158
159159 GLenum result = qglCheckFramebufferStatus (GL_DRAW_FRAMEBUFFER );
160160 CHECK (result == GL_FRAMEBUFFER_COMPLETE , "Failed to create complete Framebuffer" );
161-
161+
162162 qglBindFramebuffer (GL_FRAMEBUFFER , 0 );
163163 return framebuffer ;
164164}
@@ -290,31 +290,24 @@ GLuint VR_CreateScreenOverlayFramebuffer(GLuint colorImage)
290290 return framebuffer ;
291291}
292292
293- void VR_CreateSwapchain (XrSession session , XrBool32 isColor , int64_t format , const XrViewConfigurationView * view , uint32_t viewCount , VR_SwapchainInfo * swapchain_info , int supersampledWidth , int supersampledHeight )
293+ void VR_CreateColorSwapchain (XrSession session , int64_t format , const XrViewConfigurationView * view , uint32_t viewCount , VR_SwapchainInfo * swapchain_info , int supersampledWidth , int supersampledHeight )
294294{
295295 XrSwapchainCreateInfo swapchainCI ;
296296 swapchainCI .type = XR_TYPE_SWAPCHAIN_CREATE_INFO ;
297297 swapchainCI .next = NULL ;
298298 swapchainCI .createFlags = 0 ;
299- if (isColor )
300- {
301- swapchainCI .usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT ;
302- }
303- else
304- {
305- swapchainCI .usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ;
306- }
299+ swapchainCI .usageFlags = XR_SWAPCHAIN_USAGE_SAMPLED_BIT | XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT ;
307300 swapchainCI .format = format ;
308301 swapchainCI .sampleCount = 1 ;
309302 swapchainCI .width = supersampledWidth ;
310303 swapchainCI .height = supersampledHeight ;
311304 swapchainCI .faceCount = 1 ;
312305 swapchainCI .arraySize = viewCount ;
313306 swapchainCI .mipCount = 1 ;
314-
307+
315308 XR_CHECK (
316- xrCreateSwapchain (session , & swapchainCI , & swapchain_info -> swapchain ),
317- ( isColor ? "Failed to create color Swapchain" : "Failed to create depth Swapchain" ) );
309+ xrCreateSwapchain (session , & swapchainCI , & swapchain_info -> swapchain ),
310+ "Failed to create color Swapchain" );
318311 swapchain_info -> swapchainFormat = swapchainCI .format ;
319312 swapchain_info -> width = swapchainCI .width ;
320313 swapchain_info -> height = swapchainCI .height ;
@@ -325,7 +318,7 @@ void VR_CreateSwapchain(XrSession session, XrBool32 isColor, int64_t format, con
325318
326319 uint32_t imageCount = 0 ;
327320 XR_CHECK (
328- xrEnumerateSwapchainImages (swapchain_info -> swapchain , 0 , & imageCount , NULL ),
321+ xrEnumerateSwapchainImages (swapchain_info -> swapchain , 0 , & imageCount , NULL ),
329322 "Failed to count Swapchain Images" );
330323
331324 XrSwapchainImageOpenGLKHR * swapchainImagesGL = calloc (imageCount , sizeof (XrSwapchainImageOpenGLKHR ));
@@ -347,14 +340,44 @@ void VR_CreateSwapchain(XrSession session, XrBool32 isColor, int64_t format, con
347340 }
348341
349342 // Create a side texture that we will render menus etc. to and then we will use as source for actual frames in VR
350- if (isColor )
351- {
352- swapchain_info -> virtualScreenImage = VR_CreateImage2D (format , supersampledWidth , supersampledHeight );
353- }
343+ swapchain_info -> virtualScreenImage = VR_CreateImage2D (format , supersampledWidth , supersampledHeight );
354344
355345 free (swapchainImagesGL );
356346}
357347
348+ void VR_CreateDepthSwapchain (XrSession session , int64_t format , const XrViewConfigurationView * view , uint32_t viewCount , VR_SwapchainInfo * swapchain_info , int supersampledWidth , int supersampledHeight , int imageCount )
349+ {
350+ swapchain_info -> swapchain = XR_NULL_HANDLE ;
351+ swapchain_info -> swapchainFormat = format ;
352+ swapchain_info -> width = supersampledWidth ;
353+ swapchain_info -> height = supersampledHeight ;
354+
355+ swapchain_info -> imageCount = imageCount ;
356+ swapchain_info -> images = calloc (imageCount , sizeof (uint32_t ));
357+ for (uint32_t idx = 0 ; idx < imageCount ; ++ idx )
358+ {
359+ qglGenTextures (1 , & swapchain_info -> images [idx ]);
360+ qglBindTexture (GL_TEXTURE_2D_ARRAY , swapchain_info -> images [idx ]);
361+
362+ qglTexStorage3D (
363+ GL_TEXTURE_2D_ARRAY ,
364+ 1 ,
365+ format ,
366+ supersampledWidth ,
367+ supersampledHeight ,
368+ viewCount
369+ );
370+
371+ // Required parameters
372+ qglTexParameteri (GL_TEXTURE_2D_ARRAY , GL_TEXTURE_MIN_FILTER , GL_NEAREST );
373+ qglTexParameteri (GL_TEXTURE_2D_ARRAY , GL_TEXTURE_MAG_FILTER , GL_NEAREST );
374+ qglTexParameteri (GL_TEXTURE_2D_ARRAY , GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE );
375+ qglTexParameteri (GL_TEXTURE_2D_ARRAY , GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE );
376+ }
377+
378+ swapchain_info -> virtualScreenImage = 0 ;
379+ }
380+
358381
359382//
360383// Usable
@@ -378,7 +401,7 @@ void VR_GetRecommendedResolution(XrInstance instance, XrSystemId systemId, int*
378401 CHECK (
379402 viewConfigurationType != XR_VIEW_CONFIGURATION_TYPE_MAX_ENUM ,
380403 "No required view configuration type supported" );
381-
404+
382405 XrViewConfigurationView * views = NULL ;
383406 VR_GetViewConfigurationViews (instance , systemId , viewConfigurationType , & views );
384407
@@ -455,8 +478,8 @@ VR_SwapchainInfos VR_CreateSwapchains(XrInstance instance, XrSystemId systemId,
455478 // Swapchains
456479 //
457480 VR_SwapchainInfos swapchains = {.viewCount = viewCount };
458- VR_CreateSwapchain (session , XR_TRUE , colorFormat , & views [0 ], viewCount , & swapchains .color , supersampledWidth , supersampledHeight );
459- VR_CreateSwapchain (session , XR_FALSE , depthFormat , & views [0 ], viewCount , & swapchains .depth , supersampledWidth , supersampledHeight );
481+ VR_CreateColorSwapchain (session , colorFormat , & views [0 ], viewCount , & swapchains .color , supersampledWidth , supersampledHeight );
482+ VR_CreateDepthSwapchain (session , depthFormat , & views [0 ], viewCount , & swapchains .depth , supersampledWidth , supersampledHeight , swapchains . color . imageCount );
460483 fprintf (stderr , "[OpenXR] Created color and depth swapchains: %dx%d, %u images\n" , swapchains .color .width , swapchains .color .height , swapchains .color .imageCount );
461484
462485 // Create screen overlay swapchain (single-layer for quad layer)
@@ -492,12 +515,12 @@ VR_SwapchainInfos VR_CreateSwapchains(XrInstance instance, XrSystemId systemId,
492515 return swapchains ;
493516}
494517
495- void VR_DestroySwapchain (VR_SwapchainInfo * swapchain )
518+ void VR_DestroyColorSwapchain (VR_SwapchainInfo * swapchain )
496519{
497520 free (swapchain -> images );
498521 swapchain -> imageCount = 0 ;
499522 swapchain -> images = NULL ;
500-
523+
501524 glDeleteTextures (1 , & swapchain -> virtualScreenImage );
502525 swapchain -> virtualScreenImage = 0 ;
503526
@@ -507,6 +530,20 @@ void VR_DestroySwapchain(VR_SwapchainInfo* swapchain)
507530 swapchain -> swapchain = XR_NULL_HANDLE ;
508531}
509532
533+ void VR_DestroyDepthSwapchain (VR_SwapchainInfo * swapchain )
534+ {
535+ for (uint32_t idx = 0 ; idx < swapchain -> imageCount ; ++ idx ) {
536+ qglDeleteTextures (1 , & swapchain -> images [idx ]);
537+ swapchain -> images [idx ] = 0 ;
538+ }
539+
540+ free (swapchain -> images );
541+ swapchain -> images = NULL ;
542+ swapchain -> imageCount = 0 ;
543+ swapchain -> virtualScreenImage = 0 ;
544+ swapchain -> swapchain = XR_NULL_HANDLE ;
545+ }
546+
510547void VR_DestroySwapchains (VR_SwapchainInfos * swapchains )
511548{
512549 for (uint32_t idx = 0 ; idx < swapchains -> color .imageCount ; ++ idx )
@@ -533,12 +570,12 @@ void VR_DestroySwapchains(VR_SwapchainInfos* swapchains)
533570 swapchains -> screenOverlayFramebuffer = 0 ;
534571 }
535572
536- VR_DestroySwapchain (& swapchains -> color );
537- VR_DestroySwapchain (& swapchains -> depth );
538- VR_DestroySwapchain (& swapchains -> screenOverlay );
573+ VR_DestroyColorSwapchain (& swapchains -> color );
574+ VR_DestroyDepthSwapchain (& swapchains -> depth );
575+ VR_DestroyColorSwapchain (& swapchains -> screenOverlay );
539576}
540577
541- void VR_Swapchains_BindFramebuffers (VR_SwapchainInfos * swapchains , uint32_t swapchainColorIndex , uint32_t swapchainDepthIndex )
578+ void VR_Swapchains_BindFramebuffers (VR_SwapchainInfos * swapchains , uint32_t swapchainColorIndex )
542579{
543580 if (!swapchains )
544581 {
@@ -549,13 +586,10 @@ void VR_Swapchains_BindFramebuffers(VR_SwapchainInfos* swapchains, uint32_t swap
549586 CHECK (
550587 swapchainColorIndex < swapchains -> color .imageCount ,
551588 "Invalid swapchainImageIndex value - out of bounds" );
552- CHECK (
553- swapchainDepthIndex < swapchains -> depth .imageCount ,
554- "Invalid swapchainImageIndex value - out of bounds" );
555589
556590 qglBindFramebuffer (GL_DRAW_FRAMEBUFFER , swapchains -> framebuffers [swapchainColorIndex ]);
557591 qglFramebufferTextureMultiviewOVR (GL_DRAW_FRAMEBUFFER , GL_COLOR_ATTACHMENT0 , swapchains -> color .images [swapchainColorIndex ], 0 , 0 , 2 );
558- qglFramebufferTextureMultiviewOVR (GL_DRAW_FRAMEBUFFER , GL_DEPTH_ATTACHMENT , swapchains -> depth .images [swapchainDepthIndex ], 0 , 0 , 2 );
592+ qglFramebufferTextureMultiviewOVR (GL_DRAW_FRAMEBUFFER , GL_DEPTH_ATTACHMENT , swapchains -> depth .images [swapchainColorIndex ], 0 , 0 , 2 );
559593}
560594
561595void VR_Swapchains_BlitXRToMainFbo (VR_SwapchainInfos * swapchains , uint32_t swapchainImageIndex , XrDesktopViewConfiguration viewConfig , qboolean useVirtualScreen )
@@ -663,7 +697,7 @@ void VR_Swapchains_BlitXRToMainFbo(VR_SwapchainInfos* swapchains, uint32_t swapc
663697
664698 qglBlitNamedFramebuffer (
665699 swapchains -> eyeFramebuffers [part ][swapchainImageIndex ],
666- defaultFBO ,
700+ defaultFBO ,
667701 offsetX , offsetY , offsetX + cutX , offsetY + cutY ,
668702 leftOffset , 0 , leftOffset + engine -> window .width / parts , engine -> window .height ,
669703 GL_COLOR_BUFFER_BIT ,
@@ -792,39 +826,29 @@ void VR_Swapchains_BlitXRToVirtualScreen(VR_SwapchainInfos* swapchains, uint32_t
792826 GL_NEAREST );
793827}
794828
795- void VR_Swapchains_Acquire (VR_SwapchainInfos * swapchainInfos , uint32_t * colorIndex , uint32_t * depthIndex )
829+ void VR_Swapchains_Acquire (VR_SwapchainInfos * swapchainInfos , uint32_t * colorIndex )
796830{
797- XrSwapchain swapchains [2 ] = {swapchainInfos -> color .swapchain , swapchainInfos -> depth .swapchain };
798- uint32_t * indexPtrs [2 ] = {colorIndex , depthIndex };
799- for (uint32_t idx = 0 ; idx < 2 ; ++ idx )
800- {
801- XrSwapchainImageAcquireInfo acquireInfo = {XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO , NULL };
831+ XrSwapchainImageAcquireInfo acquireInfo = {XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO , NULL };
802832
803- XR_CHECK (
804- xrAcquireSwapchainImage (swapchains [ idx ] , & acquireInfo , indexPtrs [ idx ] ),
805- "Failed to acquire swapchain image" );
833+ XR_CHECK (
834+ xrAcquireSwapchainImage (swapchainInfos -> color . swapchain , & acquireInfo , colorIndex ),
835+ "Failed to acquire swapchain image" );
806836
807- XrSwapchainImageWaitInfo waitInfo ;
808- waitInfo .type = XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO ;
809- waitInfo .next = NULL ;
810- waitInfo .timeout = XR_INFINITE_DURATION ;
811- CHECK (
812- !XR_FAILED (xrWaitSwapchainImage (swapchains [idx ], & waitInfo )),
813- "Failed to wait for swapchain image" );
814- }
837+ XrSwapchainImageWaitInfo waitInfo ;
838+ waitInfo .type = XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO ;
839+ waitInfo .next = NULL ;
840+ waitInfo .timeout = XR_INFINITE_DURATION ;
841+ CHECK (
842+ !XR_FAILED (xrWaitSwapchainImage (swapchainInfos -> color .swapchain , & waitInfo )),
843+ "Failed to wait for swapchain image" );
815844}
816845
817846void VR_Swapchains_Release (VR_SwapchainInfos * swapchainInfos )
818847{
819- XrSwapchain swapchains [2 ] = {swapchainInfos -> color .swapchain , swapchainInfos -> depth .swapchain };
820-
821- for (uint32_t idx = 0 ; idx < 2 ; ++ idx )
822- {
823- XrSwapchainImageReleaseInfo releaseInfo = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO , NULL };
824- XR_CHECK (
825- xrReleaseSwapchainImage (swapchains [idx ], & releaseInfo ),
826- "Failed to release swapchain image" );
827- }
848+ XrSwapchainImageReleaseInfo releaseInfo = {XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO , NULL };
849+ XR_CHECK (
850+ xrReleaseSwapchainImage (swapchainInfos -> color .swapchain , & releaseInfo ),
851+ "Failed to release swapchain image" );
828852}
829853
830854//
0 commit comments