1616
1717public class OpenXRStereoRenderer extends VRRenderer {
1818 private final MCOpenXR openxr ;
19- private int swapIndex ;
19+ private int [] swapIndex = new int [ 2 ] ;
2020 private VRTextureTarget [] leftFramebuffers ;
2121 private VRTextureTarget [] rightFramebuffers ;
2222 private boolean render ;
@@ -32,31 +32,36 @@ public OpenXRStereoRenderer(MCOpenXR vr) {
3232 @ Override
3333 public void createRenderTexture (int width , int height ) {
3434 try (MemoryStack stack = MemoryStack .stackPush ()) {
35-
36- // Get amount of views in the swapchain
37- IntBuffer intBuffer = stack .ints (0 ); //Set value to 0
38- int error = XR10 .xrEnumerateSwapchainImages (this .openxr .swapchain , intBuffer , null );
39- this .openxr .logError (error , "xrEnumerateSwapchainImages" , "get count" );
40-
41- // Now we know the amount, create the image buffer
42- int imageCount = intBuffer .get (0 );
43- XrSwapchainImageOpenGLKHR .Buffer swapchainImageBuffer = this .openxr .device .createImageBuffers (imageCount ,
44- stack );
45-
46- error = XR10 .xrEnumerateSwapchainImages (this .openxr .swapchain , intBuffer ,
47- XrSwapchainImageBaseHeader .create (swapchainImageBuffer .address (), swapchainImageBuffer .capacity ()));
48- this .openxr .logError (error , "xrEnumerateSwapchainImages" , "get images" );
49-
50- this .leftFramebuffers = new VRTextureTarget [imageCount ];
51- this .rightFramebuffers = new VRTextureTarget [imageCount ];
52-
53- for (int i = 0 ; i < imageCount ; i ++) {
54- XrSwapchainImageOpenGLKHR openxrImage = swapchainImageBuffer .get (i );
55- this .leftFramebuffers [i ] = new VRTextureTarget ("L Eye " + i , width , height , openxrImage .image (), 0 );
56- String leftError = RenderHelper .checkGLError ("Left Eye " + i + " framebuffer setup" );
57- this .rightFramebuffers [i ] = new VRTextureTarget ("R Eye " + i , width , height , openxrImage .image (), 1 );
58- String rightError = RenderHelper .checkGLError ("Right Eye " + i + " framebuffer setup" );
59-
35+ for (int i = 0 ; i < 2 ; i ++) {
36+ // Get amount of views in the swapchain
37+ IntBuffer intBuffer = stack .ints (0 ); //Set value to 0
38+ int error = XR10 .xrEnumerateSwapchainImages (this .openxr .swapchain [i ], intBuffer , null );
39+ this .openxr .logError (error , "xrEnumerateSwapchainImages" , "get count" );
40+
41+ // Now we know the amount, create the image buffer
42+ int imageCount = intBuffer .get (0 );
43+ XrSwapchainImageOpenGLKHR .Buffer swapchainImageBuffer = this .openxr .device .createImageBuffers (imageCount ,
44+ stack );
45+
46+ error = XR10 .xrEnumerateSwapchainImages (this .openxr .swapchain [i ], intBuffer ,
47+ XrSwapchainImageBaseHeader .create (swapchainImageBuffer .address (), swapchainImageBuffer .capacity ()));
48+ this .openxr .logError (error , "xrEnumerateSwapchainImages" , "get images" );
49+
50+ this .leftFramebuffers = new VRTextureTarget [imageCount ];
51+ this .rightFramebuffers = new VRTextureTarget [imageCount ];
52+
53+ String leftError = "" ;
54+ String rightError = "" ;
55+ for (int i1 = 0 ; i1 < imageCount ; i1 ++) {
56+ XrSwapchainImageOpenGLKHR openxrImage = swapchainImageBuffer .get (i );
57+ if (i == 0 ) {
58+ this .leftFramebuffers [i ] = new VRTextureTarget ("L Eye " + i , width , height , true , openxrImage .image (), true , false , false );
59+ leftError = RenderHelper .checkGLError ("Left Eye " + i + " framebuffer setup" );
60+ } else {
61+ this .rightFramebuffers [i ] = new VRTextureTarget ("R Eye " + i , width , height , true , openxrImage .image (), true , false , false );
62+ rightError = RenderHelper .checkGLError ("Right Eye " + i + " framebuffer setup" );
63+ }
64+ }
6065 if (this .lastError .isEmpty ()) {
6166 this .lastError = !leftError .isEmpty () ? leftError : rightError ;
6267 }
@@ -72,34 +77,35 @@ public void setupRenderConfiguration(boolean render) throws IOException, RenderC
7277
7378 this .projectionLayerViews = XrCompositionLayerProjectionView .calloc (2 );
7479 try (MemoryStack stack = MemoryStack .stackPush ()) {
75-
76- IntBuffer intBuf2 = stack .callocInt (1 );
77-
78- int error = XR10 .xrAcquireSwapchainImage (
79- this .openxr .swapchain ,
80- XrSwapchainImageAcquireInfo .calloc (stack ).type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO ),
81- intBuf2 );
82- this .openxr .logError (error , "xrAcquireSwapchainImage" , "" );
83-
84- error = XR10 .xrWaitSwapchainImage (this .openxr .swapchain ,
85- XrSwapchainImageWaitInfo .calloc (stack )
86- .type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO )
87- .timeout (XR10 .XR_INFINITE_DURATION ));
88- this .openxr .logError (error , "xrWaitSwapchainImage" , "" );
89-
90- this .swapIndex = intBuf2 .get (0 );
91-
92- // Render view to the appropriate part of the swapchain image.
93- for (int viewIndex = 0 ; viewIndex < 2 ; viewIndex ++) {
94- XrSwapchainSubImage subImage = this .projectionLayerViews .get (viewIndex )
95- .type (XR10 .XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW )
96- .pose (this .openxr .viewBuffer .get (viewIndex ).pose ())
97- .fov (this .openxr .viewBuffer .get (viewIndex ).fov ())
98- .subImage ();
99- subImage .swapchain (this .openxr .swapchain );
100- subImage .imageRect ().offset ().set (0 , 0 );
101- subImage .imageRect ().extent ().set (this .openxr .width , this .openxr .height );
102- subImage .imageArrayIndex (viewIndex );
80+ for (int i = 0 ; i < 2 ; i ++) {
81+ IntBuffer intBuf2 = stack .callocInt (1 );
82+
83+ int error = XR10 .xrAcquireSwapchainImage (
84+ this .openxr .swapchain [i ],
85+ XrSwapchainImageAcquireInfo .calloc (stack ).type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO ),
86+ intBuf2 );
87+ this .openxr .logError (error , "xrAcquireSwapchainImage" , "" );
88+
89+ error = XR10 .xrWaitSwapchainImage (this .openxr .swapchain [i ],
90+ XrSwapchainImageWaitInfo .calloc (stack )
91+ .type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO )
92+ .timeout (XR10 .XR_INFINITE_DURATION ));
93+ this .openxr .logError (error , "xrWaitSwapchainImage" , "" );
94+
95+ this .swapIndex [i ] = intBuf2 .get (0 );
96+
97+ // Render view to the appropriate part of the swapchain image.
98+ for (int viewIndex = 0 ; viewIndex < 2 ; viewIndex ++) {
99+ XrSwapchainSubImage subImage = this .projectionLayerViews .get (viewIndex )
100+ .type (XR10 .XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW )
101+ .pose (this .openxr .viewBuffer .get (viewIndex ).pose ())
102+ .fov (this .openxr .viewBuffer .get (viewIndex ).fov ())
103+ .subImage ();
104+ subImage .swapchain (this .openxr .swapchain [i ]);
105+ subImage .imageRect ().offset ().set (0 , 0 );
106+ subImage .imageRect ().extent ().set (this .openxr .width , this .openxr .height );
107+ subImage .imageArrayIndex (viewIndex );
108+ }
103109 }
104110 this .recalculateProjectionMatrix = true ;
105111 }
@@ -131,12 +137,13 @@ public void endFrame() throws RenderConfigException {
131137 try (MemoryStack stack = MemoryStack .stackPush ()) {
132138 PointerBuffer layers = stack .callocPointer (1 );
133139 int error ;
134-
135- error = XR10 .xrReleaseSwapchainImage (
136- this .openxr .swapchain ,
137- XrSwapchainImageReleaseInfo .calloc (stack )
138- .type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO ));
139- this .openxr .logError (error , "xrReleaseSwapchainImage" , "" );
140+ for (int i = 0 ; i < 2 ; i ++) {
141+ error = XR10 .xrReleaseSwapchainImage (
142+ this .openxr .swapchain [i ],
143+ XrSwapchainImageReleaseInfo .calloc (stack )
144+ .type (XR10 .XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO ));
145+ this .openxr .logError (error , "xrReleaseSwapchainImage" , "" );
146+ }
140147
141148 XrCompositionLayerProjection compositionLayerProjection = XrCompositionLayerProjection .calloc (stack )
142149 .type (XR10 .XR_TYPE_COMPOSITION_LAYER_PROJECTION )
@@ -167,12 +174,12 @@ public boolean providesStencilMask() {
167174
168175 @ Override
169176 public RenderTarget getLeftEyeTarget () {
170- return this .leftFramebuffers [this .swapIndex ];
177+ return this .leftFramebuffers [this .swapIndex [ 0 ] ];
171178 }
172179
173180 @ Override
174181 public RenderTarget getRightEyeTarget () {
175- return this .rightFramebuffers [this .swapIndex ];
182+ return this .rightFramebuffers [this .swapIndex [ 1 ] ];
176183 }
177184
178185 @ Override
0 commit comments