Skip to content

Commit 97b4cf8

Browse files
committed
Merge fixes
1 parent a8d885a commit 97b4cf8

File tree

10 files changed

+125
-123
lines changed

10 files changed

+125
-123
lines changed

common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.mojang.blaze3d.opengl.GlDevice;
44
import com.mojang.blaze3d.pipeline.RenderTarget;
5-
import com.mojang.blaze3d.platform.GlStateManager;
65
import com.mojang.blaze3d.platform.TextureUtil;
76
import com.mojang.blaze3d.systems.RenderSystem;
87
import com.mojang.blaze3d.textures.AddressMode;

common/src/main/java/org/vivecraft/client_vr/provider/nullvr/NullVRStereoRenderer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mojang.blaze3d.pipeline.RenderTarget;
44
import com.mojang.blaze3d.opengl.GlStateManager;
5+
import com.mojang.blaze3d.systems.RenderSystem;
56
import net.minecraft.util.Mth;
67
import net.minecraft.util.Tuple;
78
import org.joml.Matrix4f;
@@ -51,7 +52,7 @@ public void createRenderTexture(int width, int height) {
5152
GlStateManager._bindTexture(this.LeftEyeTextureId);
5253
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
5354
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
54-
GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, lwidth, lheight, 0, GL11.GL_RGBA, GL11.GL_INT,
55+
GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_INT,
5556
null);
5657

5758
GlStateManager._bindTexture(i);
@@ -60,7 +61,7 @@ public void createRenderTexture(int width, int height) {
6061
GlStateManager._bindTexture(this.RightEyeTextureId);
6162
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
6263
GlStateManager._texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
63-
GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, lwidth, lheight, 0, GL11.GL_RGBA, GL11.GL_INT,
64+
GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_INT,
6465
null);
6566
GlStateManager._bindTexture(i);
6667
this.lastError = RenderHelper.checkGLError("create VR textures");
@@ -83,7 +84,7 @@ public void createRenderTexture(int width, int height) {
8384
this.lastError = !leftError.isEmpty() ? leftError : rightError;
8485
}
8586

86-
RenderSystem.bindTexture(boundTextureId);
87+
GlStateManager._bindTexture(boundTextureId);
8788
}
8889

8990
@Override

common/src/main/java/org/vivecraft/client_vr/provider/openvr_lwjgl/OpenVRStereoRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void createRenderTexture(int width, int height) {
142142
this.lastError = !leftError.isEmpty() ? leftError : rightError;
143143
}
144144

145-
RenderSystem.bindTexture(boundTextureId);
145+
GlStateManager._bindTexture(boundTextureId);
146146
}
147147

148148
@Override

common/src/main/java/org/vivecraft/client_vr/provider/openxr/MCOpenXR.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MCOpenXR extends MCVR {
4949
public XrSession session;
5050
public XrSpace xrAppSpace;
5151
public XrSpace xrViewSpace;
52-
public XrSwapchain swapchain;
52+
public XrSwapchain[] swapchain;
5353
public final XrEventDataBuffer eventDataBuffer = XrEventDataBuffer.calloc();
5454
public long time;
5555
private boolean tried;
@@ -96,8 +96,8 @@ public void destroy() {
9696
error = XR10.xrDestroyActionSet(new XrActionSet(inputActionSet, this.instance));
9797
logError(error, "xrDestroyActionSet", "");
9898
}
99-
if (this.swapchain != null) {
100-
error = XR10.xrDestroySwapchain(this.swapchain);
99+
for (XrSwapchain xrSwapchain : this.swapchain) {
100+
error = XR10.xrDestroySwapchain(xrSwapchain);
101101
logError(error, "xrDestroySwapchain", "");
102102
}
103103
if (this.viewBuffer != null) {
@@ -870,27 +870,30 @@ private void initializeOpenXRSwapChain() {
870870
throw new RuntimeException("No compatible swapchain / framebuffer format available: " + formats);
871871
}
872872

873+
this.swapchain = new XrSwapchain[2];
873874
// Make swapchain
874-
XrViewConfigurationView viewConfig = views.get(0);
875-
XrSwapchainCreateInfo swapchainCreateInfo = XrSwapchainCreateInfo.calloc(stack);
876-
swapchainCreateInfo.type(XR10.XR_TYPE_SWAPCHAIN_CREATE_INFO);
877-
swapchainCreateInfo.next(NULL);
878-
swapchainCreateInfo.createFlags(0);
879-
swapchainCreateInfo.usageFlags(XR10.XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT);
880-
swapchainCreateInfo.format(chosenFormat);
881-
swapchainCreateInfo.sampleCount(1);
882-
swapchainCreateInfo.width(viewConfig.recommendedImageRectWidth());
883-
swapchainCreateInfo.height(viewConfig.recommendedImageRectHeight());
884-
swapchainCreateInfo.faceCount(1);
885-
swapchainCreateInfo.arraySize(2);
886-
swapchainCreateInfo.mipCount(1);
887-
888-
PointerBuffer handlePointer = stack.callocPointer(1);
889-
error = XR10.xrCreateSwapchain(this.session, swapchainCreateInfo, handlePointer);
890-
logError(error, "xrCreateSwapchain", "format: " + chosenFormat);
891-
this.swapchain = new XrSwapchain(handlePointer.get(0), this.session);
892-
this.width = swapchainCreateInfo.width();
893-
this.height = swapchainCreateInfo.height();
875+
for(int i = 0; i < 2; i++) {
876+
XrViewConfigurationView viewConfig = views.get(0);
877+
XrSwapchainCreateInfo swapchainCreateInfo = XrSwapchainCreateInfo.calloc(stack);
878+
swapchainCreateInfo.type(XR10.XR_TYPE_SWAPCHAIN_CREATE_INFO);
879+
swapchainCreateInfo.next(NULL);
880+
swapchainCreateInfo.createFlags(0);
881+
swapchainCreateInfo.usageFlags(XR10.XR_SWAPCHAIN_USAGE_COLOR_ATTACHMENT_BIT);
882+
swapchainCreateInfo.format(chosenFormat);
883+
swapchainCreateInfo.sampleCount(1);
884+
swapchainCreateInfo.width(viewConfig.recommendedImageRectWidth());
885+
swapchainCreateInfo.height(viewConfig.recommendedImageRectHeight());
886+
swapchainCreateInfo.faceCount(1);
887+
swapchainCreateInfo.arraySize(1);
888+
swapchainCreateInfo.mipCount(1);
889+
890+
PointerBuffer handlePointer = stack.callocPointer(1);
891+
error = XR10.xrCreateSwapchain(this.session, swapchainCreateInfo, handlePointer);
892+
logError(error, "xrCreateSwapchain", "format: " + chosenFormat);
893+
this.swapchain[i] = new XrSwapchain(handlePointer.get(0), this.session);
894+
this.width = swapchainCreateInfo.width();
895+
this.height = swapchainCreateInfo.height();
896+
}
894897
}
895898
}
896899

common/src/main/java/org/vivecraft/client_vr/provider/openxr/OpenXRStereoRenderer.java

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public 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

common/src/main/java/org/vivecraft/client_vr/render/helpers/ShaderHelper.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.mojang.blaze3d.buffers.GpuBuffer;
66
import com.mojang.blaze3d.pipeline.RenderPipeline;
77
import com.mojang.blaze3d.pipeline.RenderTarget;
8-
import com.mojang.blaze3d.platform.GlStateManager;
98
import com.mojang.blaze3d.systems.RenderSystem;
109
import com.mojang.blaze3d.textures.GpuTexture;
1110
import com.mojang.blaze3d.vertex.*;
@@ -274,11 +273,11 @@ public static void drawMirror() {
274273
int screenHeight = MC.mainRenderTarget.height;
275274

276275
if (leftEye != null) {
277-
blitFramebuffer(leftEye, 0, 0, screenWidth, screenHeight);
276+
ShaderHelper.blitToScreen(leftEye, 0, screenWidth, screenHeight, 0, 0.0F, 0.0F, false);
278277
}
279278

280279
if (rightEye != null) {
281-
blitFramebuffer(rightEye, screenWidth, 0, MC.mainRenderTarget.width, screenHeight);
280+
ShaderHelper.blitToScreen(rightEye, screenWidth, screenWidth, screenHeight, 0, 0.0F, 0.0F, false);
282281
}
283282
} else {
284283
// general single buffer case
@@ -317,7 +316,9 @@ public static void drawMirror() {
317316
// source = DataHolder.getInstance().vrRenderer.telescopeFramebufferR;
318317
//
319318
if (source != null) {
320-
blitFramebufferCrop(source, 0, 0, MC.mainRenderTarget.width, MC.mainRenderTarget.height,
319+
ShaderHelper.blitToScreen(source,
320+
0, MC.mainRenderTarget.width,
321+
MC.mainRenderTarget.height, 0,
321322
xCrop, yCrop, keepAspect);
322323
}
323324
}
@@ -376,9 +377,9 @@ public static void doMixedRealityMirror() {
376377
source = DATA_HOLDER.vrRenderer.framebufferUndistorted;
377378
} else {
378379
if (DATA_HOLDER.vrSettings.displayMirrorLeftEye) {
379-
source = DATA_HOLDER.vrRenderer.framebufferEye0;
380+
source = DATA_HOLDER.vrRenderer.getLeftEyeTarget();
380381
} else {
381-
source = DATA_HOLDER.vrRenderer.framebufferEye1;
382+
source = DATA_HOLDER.vrRenderer.getRightEyeTarget();
382383
}
383384
}
384385
renderPass.bindSampler(VRShaders.MIXED_REALITY_FIRST_COLOR_SAMPLER, source.getColorTexture());
@@ -418,21 +419,22 @@ public static void doFSAA(RenderTarget source, RenderTarget firstPass, RenderTar
418419
}
419420

420421
/**
421-
* blits the given {@code source} RenderTarget to the bound framebuffer<br>
422+
* blits the given {@code source} RenderTarget to the screen/bound buffer<br>
422423
* the {@code source} is drawn to the rectangle at {@code left},{@code top} with a size of {@code width},{@code height}<br>
423424
* if {@code xCropFactor} or {@code yCropFactor} are non 0 the {@code source} gets zoomed in
424-
* @param source RenderTarget to draw to the screen
425-
* @param left left edge of the target area
426-
* @param top top edge of the target area
427-
* @param right right edge width of the target area
428-
* @param bottom bottom edge of the target area
425+
*
426+
* @param source RenderTarget to draw to the screen
427+
* @param left left edge of the target area
428+
* @param width width of the target area
429+
* @param height height of the target area
430+
* @param top top edge of the target area
429431
* @param xCropFactor vertical crop factor for the {@code source}
430432
* @param yCropFactor horizontal crop factor for the {@code source}
431-
* @param keepAspect keeps the aspect ratio in takt when cropping the buffer
433+
* @param keepAspect keeps the aspect ratio in takt when cropping the buffer
432434
*/
433-
private static void blitFramebufferCrop(
434-
RenderTarget source, int left, int top, int right, int bottom,
435-
float xCropFactor, float yCropFactor, boolean keepAspect)
435+
public static void blitToScreen(
436+
RenderTarget source, int left, int width, int height, int top, float xCropFactor, float yCropFactor,
437+
boolean keepAspect)
436438
{
437439
RenderSystem.assertOnRenderThread();
438440

@@ -449,12 +451,14 @@ private static void blitFramebufferCrop(
449451
// destination is wider than the buffer
450452
float heightAspect = (bufferAspect / drawAspect) * (0.5F - yCropFactor);
451453

452-
yCropFactor = 0.5F - heightAspect;
454+
yMin = 0.5F - heightAspect;
455+
yMax = 0.5F + heightAspect;
453456
} else {
454457
// destination is taller than the buffer
455458
float widthAspect = (drawAspect / bufferAspect) * (0.5F - xCropFactor);
456459

457-
xCropFactor = 0.5F - widthAspect;
460+
xMin = 0.5F - widthAspect;
461+
xMax = 0.5F + widthAspect;
458462
}
459463
}
460464

0 commit comments

Comments
 (0)