diff --git a/common/src/main/java/org/vivecraft/client/extensions/RenderTargetExtension.java b/common/src/main/java/org/vivecraft/client/extensions/RenderTargetExtension.java index 3258c56c9..cc751cfcf 100644 --- a/common/src/main/java/org/vivecraft/client/extensions/RenderTargetExtension.java +++ b/common/src/main/java/org/vivecraft/client/extensions/RenderTargetExtension.java @@ -35,10 +35,4 @@ public interface RenderTargetExtension { * @return if the RenderTarget is set to use mipmaps */ boolean vivecraft$hasMipmaps(); - - /** - * Sets the color id - * @param colorid the color id to set - */ - void vivecraft$setColorid(int colorid); } diff --git a/common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java b/common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java index 782f8ce1e..3ebe8ea04 100644 --- a/common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java +++ b/common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java @@ -2,11 +2,13 @@ import com.mojang.blaze3d.pipeline.RenderTarget; import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.platform.TextureUtil; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.Minecraft; import org.lwjgl.opengl.GL30; import org.vivecraft.client.Xplat; import org.vivecraft.client.extensions.RenderTargetExtension; +import org.vivecraft.client_vr.render.helpers.RenderHelper; /** * extension of a regular RenderTarget that sets Vivecraft features on creation @@ -36,14 +38,22 @@ public VRTextureTarget(String name, int width, int height, boolean useDepth, int this.setClearColor(0, 0, 0, 0); } - public VRTextureTarget(String name, int width, int height, int colorid, int index) { - super(true); + public VRTextureTarget(String name, int width, int height, int colorId, int index) { + super(false); this.name = name; RenderSystem.assertOnGameThreadOrInit(); this.resize(width, height, Minecraft.ON_OSX); - ((RenderTargetExtension) this).vivecraft$setColorid(colorid); - GlStateManager._glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferId); - GL30.glFramebufferTextureLayer(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, colorid, 0, index); + + // free the old one when setting a new one + if (this.colorTextureId != -1) { + TextureUtil.releaseTextureId(this.colorTextureId); + } + this.colorTextureId = colorId; + + GlStateManager._glBindFramebuffer(GL30.GL_FRAMEBUFFER, this.frameBufferId); + // unset the old GL_COLOR_ATTACHMENT0 + GlStateManager._glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL30.GL_TEXTURE_2D, 0, 0); + GL30.glFramebufferTextureLayer(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, colorId, 0, index); this.setClearColor(0, 0, 0, 0); } diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java b/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java index 29bb0f4b7..c69fe0194 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/VRRenderer.java @@ -26,7 +26,6 @@ import org.vivecraft.client.utils.TextUtils; import org.vivecraft.client_vr.ClientDataHolderVR; import org.vivecraft.client_vr.VRTextureTarget; -import org.vivecraft.client_vr.extensions.GameRendererExtension; import org.vivecraft.client_vr.extensions.WindowExtension; import org.vivecraft.client_vr.gameplay.screenhandlers.GuiHandler; import org.vivecraft.client_vr.gameplay.screenhandlers.KeyboardHandler; @@ -37,6 +36,7 @@ import org.vivecraft.client_vr.render.VRShaders; import org.vivecraft.client_vr.render.helpers.RenderHelper; import org.vivecraft.client_vr.settings.VRSettings; +import org.vivecraft.client_xr.render_pass.RenderPassManager; import org.vivecraft.client_xr.render_pass.WorldRenderPass; import org.vivecraft.mod_compat_vr.ShadersHelper; import org.vivecraft.mod_compat_vr.resolutioncontrol.ResolutionControlHelper; @@ -55,8 +55,7 @@ public abstract class VRRenderer { private float lastFarClip = 0F; // render buffers - protected int LeftEyeTextureId = -1; - protected int RightEyeTextureId = -1; + protected boolean eyeFramebuffersCreated = false; public RenderTarget framebufferMR; public RenderTarget framebufferUndistorted; public RenderTarget framebufferVrRender; @@ -100,7 +99,7 @@ public VRRenderer(MCVR vr) { * @param width width of the texture * @param height height of the texture */ - public abstract void createRenderTexture(int width, int height) throws RenderConfigException; + public abstract void createRenderTexture(int width, int height); /** * gets the cached projection matrix if the farClip distance matches with the last, else gets a new one from the VR runtime @@ -570,11 +569,12 @@ public void setupRenderConfiguration(boolean render) throws RenderConfigExceptio minecraft.screen.init(minecraft, guiWidth, guiHeight); } } - } - //for OPENXR, it needs to reinit - this.eyeProj[0] = this.getProjectionMatrix(0, ((GameRendererExtension) minecraft.gameRenderer).vivecraft$getMinClipDistance(), lastFarClip); - this.eyeProj[1] = this.getProjectionMatrix(1, ((GameRendererExtension) minecraft.gameRenderer).vivecraft$getMinClipDistance(), lastFarClip); + RenderPassManager.INSTANCE.vanillaRenderTarget.resize( + ((WindowExtension) (Object) Minecraft.getInstance().getWindow()).vivecraft$getActualScreenWidth(), + ((WindowExtension) (Object) Minecraft.getInstance().getWindow()).vivecraft$getActualScreenHeight(), + Minecraft.ON_OSX); + } if (this.reinitFrameBuffers) { RenderHelper.checkGLError("Start Init"); @@ -617,7 +617,17 @@ public void setupRenderConfiguration(boolean render) throws RenderConfigExceptio destroyBuffers(); - this.createRenderTexture(eyew, eyeh); + if (!this.eyeFramebuffersCreated) { + VRSettings.LOGGER.info("Vivecraft: VR Provider supplied texture resolution: {} x {}", eyew, eyeh); + this.createRenderTexture(eyew, eyeh); + + if (!this.getLastError().isEmpty()) { + throw new RenderConfigException( + Component.translatable("vivecraft.messages.renderiniterror", this.getName()), + Component.literal(this.getLastError())); + } + this.eyeFramebuffersCreated = true; + } float resolutionScale = ResolutionControlHelper.isLoaded() ? ResolutionControlHelper.getCurrentScaleFactor() : 1.0F; @@ -726,8 +736,6 @@ public void setupRenderConfiguration(boolean render) throws RenderConfigExceptio try { minecraft.mainRenderTarget = this.framebufferVrRender; - VRShaders.setupBlitAspect(); - RenderHelper.checkGLError("init blit aspect shader"); VRShaders.setupDepthMask(); RenderHelper.checkGLError("init depth shader"); VRShaders.setupFOVReduction(); diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/nullvr/NullVRStereoRenderer.java b/common/src/main/java/org/vivecraft/client_vr/provider/nullvr/NullVRStereoRenderer.java index a1d975844..4cc593528 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/nullvr/NullVRStereoRenderer.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/nullvr/NullVRStereoRenderer.java @@ -8,9 +8,9 @@ import net.minecraft.util.Tuple; import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; +import org.vivecraft.client_vr.VRTextureTarget; import org.vivecraft.client_vr.provider.MCVR; import org.vivecraft.client_vr.provider.VRRenderer; -import org.vivecraft.client_vr.render.RenderConfigException; import org.vivecraft.client_vr.render.RenderPass; import org.vivecraft.client_vr.render.helpers.RenderHelper; import org.vivecraft.client_vr.settings.VRSettings; @@ -43,23 +43,33 @@ protected Matrix4f getProjectionMatrix(int eyeType, float nearClip, float farCli } @Override - public void createRenderTexture(int lwidth, int lheight) throws RenderConfigException { + public void createRenderTexture(int lwidth, int lheight) { + int boundTextureId = GlStateManager._getInteger(GL11.GL_TEXTURE_BINDING_2D); + this.LeftEyeTextureId = GlStateManager._genTexture(); - int i = GlStateManager._getInteger(GL11.GL_TEXTURE_BINDING_2D); RenderSystem.bindTexture(this.LeftEyeTextureId); RenderSystem.texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); RenderSystem.texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, lwidth, lheight, 0, GL11.GL_RGBA, GL11.GL_INT, null); - RenderSystem.bindTexture(i); this.RightEyeTextureId = GlStateManager._genTexture(); - i = GlStateManager._getInteger(GL11.GL_TEXTURE_BINDING_2D); RenderSystem.bindTexture(this.RightEyeTextureId); RenderSystem.texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); RenderSystem.texParameter(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); GlStateManager._texImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, lwidth, lheight, 0, GL11.GL_RGBA, GL11.GL_INT, null); - RenderSystem.bindTexture(i); - this.lastError = RenderHelper.checkGLError("create VR textures"); + + this.framebufferEye0 = new VRTextureTarget("L Eye", lwidth, lheight, false, this.LeftEyeTextureId, true, false, + false); + RenderHelper.checkGLError("Left Eye framebuffer setup"); + + this.framebufferEye1 = new VRTextureTarget("R Eye", lwidth, lheight, false, this.RightEyeTextureId, true, false, + false); + this.lastError = RenderHelper.checkGLError("Right Eye framebuffer setup"); + + VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye0); + VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye1); + + RenderSystem.bindTexture(boundTextureId); } @Override @@ -72,12 +82,12 @@ public boolean providesStencilMask() { @Override public RenderTarget getLeftEyeTarget() { - return framebufferEye0; + return this.framebufferEye0; } @Override public RenderTarget getRightEyeTarget() { - return framebufferEye1; + return this.framebufferEye1; } @@ -92,9 +102,8 @@ public String getName() { } @Override - protected void destroyBuffers() { - super.destroyBuffers(); - + public void destroy() { + super.destroy(); if (this.framebufferEye0 != null) { this.framebufferEye0.destroyBuffers(); this.framebufferEye0 = null; @@ -104,7 +113,6 @@ protected void destroyBuffers() { this.framebufferEye1.destroyBuffers(); this.framebufferEye1 = null; } - if (this.LeftEyeTextureId > -1) { TextureUtil.releaseTextureId(this.LeftEyeTextureId); this.LeftEyeTextureId = -1; diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/openvr_lwjgl/OpenVRStereoRenderer.java b/common/src/main/java/org/vivecraft/client_vr/provider/openvr_lwjgl/OpenVRStereoRenderer.java index 6db87596b..9f31a5fde 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/openvr_lwjgl/OpenVRStereoRenderer.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/openvr_lwjgl/OpenVRStereoRenderer.java @@ -36,8 +36,8 @@ public OpenVRStereoRenderer(MCOpenVR vr) { super(vr); this.openvr = vr; - hiddenMeshes[0] = HiddenAreaMesh.calloc(); - hiddenMeshes[1] = HiddenAreaMesh.calloc(); + this.hiddenMeshes[0] = HiddenAreaMesh.calloc(); + this.hiddenMeshes[1] = HiddenAreaMesh.calloc(); } @Override @@ -93,7 +93,7 @@ protected Matrix4f getProjectionMatrix(int eyeType, float nearClip, float farCli } @Override - public void createRenderTexture(int width, int height) throws RenderConfigException { + public void createRenderTexture(int width, int height) { int boundTextureId = GlStateManager._getInteger(GL11C.GL_TEXTURE_BINDING_2D); // generate left eye texture this.LeftEyeTextureId = GlStateManager._genTexture(); @@ -115,30 +115,19 @@ public void createRenderTexture(int width, int height) throws RenderConfigExcept this.openvr.texType1.eColorSpace(VR.EColorSpace_ColorSpace_Gamma); this.openvr.texType1.eType(VR.ETextureType_TextureType_OpenGL); - if (this.LeftEyeTextureId == -1) { - throw new RenderConfigException( - Component.translatable("vivecraft.messages.renderiniterror", this.getName()), - Component.literal(this.getLastError())); - } + this.framebufferEye0 = new VRTextureTarget("L Eye", width, height, false, this.LeftEyeTextureId, true, false, + false); + RenderHelper.checkGLError("Left Eye framebuffer setup"); - VRSettings.LOGGER.info("Vivecraft: VR Provider supplied render texture IDs: {}, {}", this.LeftEyeTextureId, this.RightEyeTextureId); - VRSettings.LOGGER.info("Vivecraft: VR Provider supplied texture resolution: {} x {}", width, height); + this.framebufferEye1 = new VRTextureTarget("R Eye", width, height, false, this.RightEyeTextureId, true, false, + false); - RenderHelper.checkGLError("Render Texture setup"); + this.lastError = RenderHelper.checkGLError("Right Eye framebuffer setup"); - if (this.framebufferEye0 == null) { - this.framebufferEye0 = new VRTextureTarget("L Eye", width, height, false, this.LeftEyeTextureId, false, true, false); - VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye0); - RenderHelper.checkGLError("Left Eye framebuffer setup"); - } + VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye0); + VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye1); - if (this.framebufferEye1 == null) { - this.framebufferEye1 = new VRTextureTarget("R Eye", width, height, false, this.RightEyeTextureId, false, true, false); - VRSettings.LOGGER.info("Vivecraft: {}", this.framebufferEye1); - RenderHelper.checkGLError("Right Eye framebuffer setup"); - } RenderSystem.bindTexture(boundTextureId); - this.lastError = RenderHelper.checkGLError("create VR textures"); } @Override @@ -184,12 +173,12 @@ public boolean providesStencilMask() { @Override public RenderTarget getLeftEyeTarget() { - return framebufferEye0; + return this.framebufferEye0; } @Override public RenderTarget getRightEyeTarget() { - return framebufferEye1; + return this.framebufferEye1; } public float[] getStencilMask(RenderPass eye) { @@ -205,17 +194,19 @@ public String getName() { } @Override - protected void destroyBuffers() { - super.destroyBuffers(); - if (this.framebufferEye0 != null) { - this.framebufferEye0.destroyBuffers(); - this.framebufferEye0 = null; - } + public void destroy() { + super.destroy(); + this.hiddenMeshes[0].free(); + this.hiddenMeshes[1].free(); + if (this.framebufferEye0 != null) { + this.framebufferEye0.destroyBuffers(); + this.framebufferEye0 = null; + } - if (this.framebufferEye1 != null) { - this.framebufferEye1.destroyBuffers(); - this.framebufferEye1 = null; - } + if (this.framebufferEye1 != null) { + this.framebufferEye1.destroyBuffers(); + this.framebufferEye1 = null; + } if (this.LeftEyeTextureId > -1) { TextureUtil.releaseTextureId(this.LeftEyeTextureId); this.LeftEyeTextureId = -1; @@ -226,11 +217,4 @@ protected void destroyBuffers() { this.RightEyeTextureId = -1; } } - - @Override - public void destroy() { - super.destroy(); - this.hiddenMeshes[0].free(); - this.hiddenMeshes[1].free(); - } } diff --git a/common/src/main/java/org/vivecraft/client_vr/provider/openxr/OpenXRStereoRenderer.java b/common/src/main/java/org/vivecraft/client_vr/provider/openxr/OpenXRStereoRenderer.java index 3cebe96cc..7f6290e61 100644 --- a/common/src/main/java/org/vivecraft/client_vr/provider/openxr/OpenXRStereoRenderer.java +++ b/common/src/main/java/org/vivecraft/client_vr/provider/openxr/OpenXRStereoRenderer.java @@ -4,10 +4,8 @@ import net.minecraft.util.Tuple; import org.joml.Matrix4f; import org.lwjgl.PointerBuffer; -import org.lwjgl.opengl.GL31; import org.lwjgl.openxr.*; import org.lwjgl.system.MemoryStack; -import org.vivecraft.client_vr.ClientDataHolderVR; import org.vivecraft.client_vr.VRTextureTarget; import org.vivecraft.client_vr.provider.VRRenderer; import org.vivecraft.client_vr.render.RenderConfigException; @@ -23,8 +21,6 @@ public class OpenXRStereoRenderer extends VRRenderer { private VRTextureTarget[] rightFramebuffers; private boolean render; private XrCompositionLayerProjectionView.Buffer projectionLayerViews; - private VRTextureTarget rightFramebuffer; - private VRTextureTarget leftFramebuffer; public OpenXRStereoRenderer(MCOpenXR vr) { @@ -33,19 +29,20 @@ public OpenXRStereoRenderer(MCOpenXR vr) { } @Override - public void createRenderTexture(int width, int height) throws RenderConfigException{ + public void createRenderTexture(int width, int height) { try (MemoryStack stack = MemoryStack.stackPush()) { - //Get amount of views in the swapchain IntBuffer intBuffer = stack.ints(0); //Set value to 0 - int error = XR10.xrEnumerateSwapchainImages(openxr.swapchain, intBuffer, null); + int error = XR10.xrEnumerateSwapchainImages(this.openxr.swapchain, intBuffer, null); this.openxr.logError(error, "xrEnumerateSwapchainImages", "get count"); //Now we know the amount, create the image buffer int imageCount = intBuffer.get(0); - XrSwapchainImageOpenGLKHR.Buffer swapchainImageBuffer = this.openxr.device.createImageBuffers(imageCount, stack); + XrSwapchainImageOpenGLKHR.Buffer swapchainImageBuffer = this.openxr.device.createImageBuffers(imageCount, + stack); - error = XR10.xrEnumerateSwapchainImages(openxr.swapchain, intBuffer, XrSwapchainImageBaseHeader.create(swapchainImageBuffer.address(), swapchainImageBuffer.capacity())); + error = XR10.xrEnumerateSwapchainImages(this.openxr.swapchain, intBuffer, + XrSwapchainImageBaseHeader.create(swapchainImageBuffer.address(), swapchainImageBuffer.capacity())); this.openxr.logError(error, "xrEnumerateSwapchainImages", "get images"); this.leftFramebuffers = new VRTextureTarget[imageCount]; @@ -53,36 +50,33 @@ public void createRenderTexture(int width, int height) throws RenderConfigExcept for (int i = 0; i < imageCount; i++) { XrSwapchainImageOpenGLKHR openxrImage = swapchainImageBuffer.get(i); - leftFramebuffers[i] = new VRTextureTarget("L Eye " + i, width, height, openxrImage.image(), 0); - RenderHelper.checkGLError("Left Eye framebuffer setup"); - rightFramebuffers[i] = new VRTextureTarget("R Eye " + i, width, height, openxrImage.image(), 1); - RenderHelper.checkGLError("Right Eye framebuffer setup"); + this.leftFramebuffers[i] = new VRTextureTarget("L Eye " + i, width, height, openxrImage.image(), 0); + RenderHelper.checkGLError("Left Eye " + i + " framebuffer setup"); + this.rightFramebuffers[i] = new VRTextureTarget("R Eye " + i, width, height, openxrImage.image(), 1); + this.lastError = RenderHelper.checkGLError("Right Eye " + i + " framebuffer setup"); } - - this.rightFramebuffer = new VRTextureTarget("R Eye mirror", width, height, true, -1, true, true, ClientDataHolderVR.getInstance().vrSettings.vrUseStencil); - this.leftFramebuffer = new VRTextureTarget("L Eye mirror", width, height, true, -1, true, true, ClientDataHolderVR.getInstance().vrSettings.vrUseStencil); } } @Override public void setupRenderConfiguration(boolean render) throws IOException, RenderConfigException { super.setupRenderConfiguration(render); - + if (!render) { return; } this.projectionLayerViews = XrCompositionLayerProjectionView.calloc(2); - try (MemoryStack stack = MemoryStack.stackPush()){ + try (MemoryStack stack = MemoryStack.stackPush()) { IntBuffer intBuf2 = stack.callocInt(1); int error = XR10.xrAcquireSwapchainImage( - openxr.swapchain, + this.openxr.swapchain, XrSwapchainImageAcquireInfo.calloc(stack).type(XR10.XR_TYPE_SWAPCHAIN_IMAGE_ACQUIRE_INFO), intBuf2); this.openxr.logError(error, "xrAcquireSwapchainImage", ""); - error = XR10.xrWaitSwapchainImage(openxr.swapchain, + error = XR10.xrWaitSwapchainImage(this.openxr.swapchain, XrSwapchainImageWaitInfo.calloc(stack) .type(XR10.XR_TYPE_SWAPCHAIN_IMAGE_WAIT_INFO) .timeout(XR10.XR_INFINITE_DURATION)); @@ -92,68 +86,57 @@ public void setupRenderConfiguration(boolean render) throws IOException, RenderC // Render view to the appropriate part of the swapchain image. for (int viewIndex = 0; viewIndex < 2; viewIndex++) { - - var subImage = projectionLayerViews.get(viewIndex) + var subImage = this.projectionLayerViews.get(viewIndex) .type(XR10.XR_TYPE_COMPOSITION_LAYER_PROJECTION_VIEW) - .pose(openxr.viewBuffer.get(viewIndex).pose()) - .fov(openxr.viewBuffer.get(viewIndex).fov()) + .pose(this.openxr.viewBuffer.get(viewIndex).pose()) + .fov(this.openxr.viewBuffer.get(viewIndex).fov()) .subImage(); - subImage.swapchain(openxr.swapchain); + subImage.swapchain(this.openxr.swapchain); subImage.imageRect().offset().set(0, 0); - subImage.imageRect().extent().set(openxr.width, openxr.height); + subImage.imageRect().extent().set(this.openxr.width, this.openxr.height); subImage.imageArrayIndex(viewIndex); - } } } @Override public Matrix4f getProjectionMatrix(int eyeType, float nearClip, float farClip) { - XrFovf fov = openxr.viewBuffer.get(eyeType).fov(); - return new Matrix4f().setPerspectiveOffCenterFov(fov.angleLeft(), fov.angleRight(), fov.angleDown(), fov.angleUp(), nearClip, farClip); + XrFovf fov = this.openxr.viewBuffer.get(eyeType).fov(); + return new Matrix4f().setPerspectiveOffCenterFov(fov.angleLeft(), fov.angleRight(), fov.angleDown(), + fov.angleUp(), nearClip, farClip); } @Override public void endFrame() throws RenderConfigException { - - GL31.glBindFramebuffer(GL31.GL_READ_FRAMEBUFFER, getLeftEyeTarget().frameBufferId); - GL31.glBindFramebuffer(GL31.GL_DRAW_FRAMEBUFFER, leftFramebuffers[swapIndex].frameBufferId); - GL31.glBlitFramebuffer(0,0, getLeftEyeTarget().viewWidth, getLeftEyeTarget().viewHeight, 0,0, leftFramebuffers[swapIndex].viewWidth, leftFramebuffers[swapIndex].viewHeight, GL31.GL_STENCIL_BUFFER_BIT | GL31.GL_COLOR_BUFFER_BIT, GL31.GL_NEAREST); - - GL31.glBindFramebuffer(GL31.GL_READ_FRAMEBUFFER, getRightEyeTarget().frameBufferId); - GL31.glBindFramebuffer(GL31.GL_DRAW_FRAMEBUFFER, rightFramebuffers[swapIndex].frameBufferId); - GL31.glBlitFramebuffer(0,0, getRightEyeTarget().viewWidth, getRightEyeTarget().viewHeight, 0,0, rightFramebuffers[swapIndex].viewWidth, rightFramebuffers[swapIndex].viewHeight, GL31.GL_STENCIL_BUFFER_BIT | GL31.GL_COLOR_BUFFER_BIT, GL31.GL_NEAREST); - - - try (MemoryStack stack = MemoryStack.stackPush()){ + try (MemoryStack stack = MemoryStack.stackPush()) { PointerBuffer layers = stack.callocPointer(1); int error; error = XR10.xrReleaseSwapchainImage( - openxr.swapchain, + this.openxr.swapchain, XrSwapchainImageReleaseInfo.calloc(stack) .type(XR10.XR_TYPE_SWAPCHAIN_IMAGE_RELEASE_INFO)); this.openxr.logError(error, "xrReleaseSwapchainImage", ""); XrCompositionLayerProjection compositionLayerProjection = XrCompositionLayerProjection.calloc(stack) .type(XR10.XR_TYPE_COMPOSITION_LAYER_PROJECTION) - .space(openxr.xrAppSpace) - .views(projectionLayerViews); + .space(this.openxr.xrAppSpace) + .views(this.projectionLayerViews); layers.put(compositionLayerProjection); layers.flip(); error = XR10.xrEndFrame( - openxr.session, + this.openxr.session, XrFrameEndInfo.calloc(stack) .type(XR10.XR_TYPE_FRAME_END_INFO) - .displayTime(openxr.time) + .displayTime(this.openxr.time) .environmentBlendMode(XR10.XR_ENVIRONMENT_BLEND_MODE_OPAQUE) .layers(layers)); this.openxr.logError(error, "xrEndFrame", ""); - projectionLayerViews.close(); + this.projectionLayerViews.close(); } } @@ -164,12 +147,12 @@ public boolean providesStencilMask() { @Override public RenderTarget getLeftEyeTarget() { - return leftFramebuffer; + return this.leftFramebuffers[this.swapIndex]; } @Override public RenderTarget getRightEyeTarget() { - return rightFramebuffer; + return this.rightFramebuffers[this.swapIndex]; } @Override @@ -179,6 +162,25 @@ public String getName() { @Override public Tuple getRenderTextureSizes() { - return new Tuple<>(openxr.width, openxr.height); + return new Tuple<>(this.openxr.width, this.openxr.height); + } + + @Override + public void destroy() { + super.destroy(); + + if (this.leftFramebuffers != null) { + for (VRTextureTarget leftFramebuffer : this.leftFramebuffers) { + leftFramebuffer.destroyBuffers(); + } + this.leftFramebuffers = null; + } + + if (this.rightFramebuffers != null) { + for (VRTextureTarget rightFramebuffer : this.rightFramebuffers) { + rightFramebuffer.destroyBuffers(); + } + this.rightFramebuffers = null; + } } } diff --git a/common/src/main/java/org/vivecraft/client_vr/render/MirrorNotification.java b/common/src/main/java/org/vivecraft/client_vr/render/MirrorNotification.java index c080435ce..cf9c86f94 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/MirrorNotification.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/MirrorNotification.java @@ -41,8 +41,8 @@ public static void notify(String text, boolean clear, int lengthMs) { */ public static void render() { if (System.currentTimeMillis() < MIRROR_NOTIFY_START + MIRROR_NOTIFY_LEN) { - int screenX = ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenWidth(); - int screenY = ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenHeight(); + int screenX = MC.mainRenderTarget.width; + int screenY = MC.mainRenderTarget.height; RenderSystem.viewport(0, 0, screenX, screenY); Matrix4f projection = new Matrix4f().setOrtho(0.0F, screenX, diff --git a/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java b/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java index 4e10ffa85..06cae4521 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/VRShaders.java @@ -37,9 +37,6 @@ public class VRShaders { public static AbstractUniform POST_PROCESSING_OVERLAY_BLACK_ALPHA_UNIFORM; public static AbstractUniform POST_PROCESSING_OVERLAY_EYE_UNIFORM; - // blit shader - public static ShaderInstance BLIT_VR_SHADER; - // end portal shaders public static ShaderInstance RENDERTYPE_END_PORTAL_VR_SHADER; public static ShaderInstance RENDERTYPE_END_GATEWAY_VR_SHADER; @@ -89,10 +86,6 @@ public static void setupFOVReduction() throws IOException { POST_PROCESSING_OVERLAY_BLACK_ALPHA_UNIFORM = POST_PROCESSING_SHADER.safeGetUniform("blackalpha"); } - public static void setupBlitAspect() throws Exception { - BLIT_VR_SHADER = new ShaderInstance(Minecraft.getInstance().getResourceManager(), "blit_vr", DefaultVertexFormat.POSITION_TEX); - } - public static void setupPortalShaders() throws IOException { RENDERTYPE_END_PORTAL_VR_SHADER = new ShaderInstance(Minecraft.getInstance().getResourceManager(), "rendertype_end_portal_vr", DefaultVertexFormat.POSITION); RENDERTYPE_END_GATEWAY_VR_SHADER = new ShaderInstance(Minecraft.getInstance().getResourceManager(), "rendertype_end_gateway_vr", DefaultVertexFormat.POSITION); diff --git a/common/src/main/java/org/vivecraft/client_vr/render/helpers/ShaderHelper.java b/common/src/main/java/org/vivecraft/client_vr/render/helpers/ShaderHelper.java index 601cd132a..73b03405c 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/helpers/ShaderHelper.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/helpers/ShaderHelper.java @@ -1,6 +1,7 @@ package org.vivecraft.client_vr.render.helpers; import com.mojang.blaze3d.pipeline.RenderTarget; +import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.*; import net.minecraft.Util; @@ -14,10 +15,11 @@ import org.jetbrains.annotations.NotNull; import org.joml.Matrix4f; import org.joml.Vector3f; +import org.lwjgl.opengl.GL11C; +import org.lwjgl.opengl.GL30C; import org.lwjgl.opengl.GL43; import org.vivecraft.client_vr.ClientDataHolderVR; import org.vivecraft.client_vr.extensions.GameRendererExtension; -import org.vivecraft.client_vr.extensions.WindowExtension; import org.vivecraft.client_vr.gameplay.screenhandlers.GuiHandler; import org.vivecraft.client_vr.render.MirrorNotification; import org.vivecraft.client_vr.render.RenderPass; @@ -81,7 +83,9 @@ private static void drawFullscreenQuad(VertexFormat format) { throw new IllegalStateException("Unexpected vertex format " + format); } + RenderSystem.disableDepthTest(); BufferUploader.draw(builder.end()); + RenderSystem.enableDepthTest(); } /** @@ -256,15 +260,15 @@ public static void drawMirror() { RenderTarget leftEye = DATA_HOLDER.vrRenderer.getLeftEyeTarget(); RenderTarget rightEye = DATA_HOLDER.vrRenderer.getRightEyeTarget(); - int screenWidth = ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenWidth() / 2; - int screenHeight = ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenHeight(); + int screenWidth = MC.mainRenderTarget.width / 2; + int screenHeight = MC.mainRenderTarget.height; if (leftEye != null) { - ShaderHelper.blitToScreen(leftEye, 0, screenWidth, screenHeight, 0, 0.0F, 0.0F, false); + blitFramebuffer(leftEye, 0, 0, screenWidth, screenHeight); } if (rightEye != null) { - ShaderHelper.blitToScreen(rightEye, screenWidth, screenWidth, screenHeight, 0, 0.0F, 0.0F, false); + blitFramebuffer(rightEye, screenWidth, 0, MC.mainRenderTarget.width, screenHeight); } } else { // general single buffer case @@ -303,9 +307,7 @@ public static void drawMirror() { // source = DataHolder.getInstance().vrRenderer.telescopeFramebufferR; // if (source != null) { - ShaderHelper.blitToScreen(source, - 0, ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenWidth(), - ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenHeight(), 0, + blitFramebufferCrop(source, 0, 0, MC.mainRenderTarget.width, MC.mainRenderTarget.height, xCrop, yCrop, keepAspect); } } @@ -316,9 +318,7 @@ public static void drawMirror() { public static void doMixedRealityMirror() { // set viewport to fullscreen, since it would be still on the one from the last pass - RenderSystem.viewport(0, 0, - ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenWidth(), - ((WindowExtension) (Object) MC.getWindow()).vivecraft$getActualScreenHeight()); + RenderSystem.viewport(0, 0, MC.mainRenderTarget.width, MC.mainRenderTarget.height); Vec3 camPlayer = DATA_HOLDER.vrPlayer.vrdata_room_pre.getHeadPivot() .subtract(DATA_HOLDER.vrPlayer.vrdata_room_pre.getEye(RenderPass.THIRD).getPosition()); @@ -355,6 +355,12 @@ public static void doMixedRealityMirror() { VRShaders.MIXED_REALITY_SHADER.setSampler("thirdPersonDepth", DATA_HOLDER.vrRenderer.framebufferMR.getDepthTextureId()); + VRShaders.MIXED_REALITY_SHADER.apply(); + + drawFullscreenQuad(VRShaders.MIXED_REALITY_SHADER.getVertexFormat()); + + VRShaders.MIXED_REALITY_SHADER.clear(); + if (DATA_HOLDER.vrSettings.mixedRealityUnityLike) { RenderTarget source; if (DATA_HOLDER.vrSettings.displayMirrorUseScreenshotCamera && DATA_HOLDER.cameraTracker.isVisible()) { @@ -368,14 +374,9 @@ public static void doMixedRealityMirror() { source = DATA_HOLDER.vrRenderer.getRightEyeTarget(); } } - VRShaders.MIXED_REALITY_SHADER.setSampler("firstPersonColor", source.getColorTextureId()); + blitFramebuffer(source, MC.mainRenderTarget.width / 2, 0, + MC.mainRenderTarget.width, MC.mainRenderTarget.height / 2); } - - VRShaders.MIXED_REALITY_SHADER.apply(); - - drawFullscreenQuad(VRShaders.MIXED_REALITY_SHADER.getVertexFormat()); - - VRShaders.MIXED_REALITY_SHADER.clear(); } /** @@ -425,66 +426,67 @@ public static void doFSAA(RenderTarget source, RenderTarget firstPass, RenderTar } /** - * blits the given {@code source} RenderTarget to the screen/bound buffer
- * the {@code source} is drawn to the rectangle at {@code left},{@code top} with a size of {@code width},{@code height}
+ * blits the given {@code source} RenderTarget to the bound framebuffer
* if {@code xCropFactor} or {@code yCropFactor} are non 0 the {@code source} gets zoomed in * @param source RenderTarget to draw to the screen * @param left left edge of the target area - * @param width width of the target area - * @param height height of the target area * @param top top edge of the target area + * @param right right edge width of the target area + * @param bottom bottom edge of the target area * @param xCropFactor vertical crop factor for the {@code source} * @param yCropFactor horizontal crop factor for the {@code source} * @param keepAspect keeps the aspect ratio in takt when cropping the buffer */ - public static void blitToScreen(RenderTarget source, int left, int width, int height, int top, float xCropFactor, float yCropFactor, boolean keepAspect) { - RenderSystem.assertOnRenderThread(); - RenderSystem.colorMask(true, true, true, false); - RenderSystem.disableDepthTest(); - RenderSystem.depthMask(false); - RenderSystem.viewport(left, top, width, height); - RenderSystem.disableBlend(); - - float drawAspect = (float) width / (float) height; - float bufferAspect = (float) source.viewWidth / (float) source.viewHeight; - - float xMin = xCropFactor; - float yMin = yCropFactor; - float xMax = 1.0F - xCropFactor; - float yMax = 1.0F - yCropFactor; + private static void blitFramebufferCrop( + RenderTarget source, int left, int top, int right, int bottom, + float xCropFactor, float yCropFactor, boolean keepAspect) + { if (keepAspect) { + float drawAspect = (float) MC.mainRenderTarget.width / (float) MC.mainRenderTarget.height; + float bufferAspect = (float) source.viewWidth / (float) source.viewHeight; if (drawAspect > bufferAspect) { // destination is wider than the buffer float heightAspect = (bufferAspect / drawAspect) * (0.5F - yCropFactor); - yMin = 0.5F - heightAspect; - yMax = 0.5F + heightAspect; + yCropFactor = 0.5F - heightAspect; } else { // destination is taller than the buffer float widthAspect = (drawAspect / bufferAspect) * (0.5F - xCropFactor); - xMin = 0.5F - widthAspect; - xMax = 0.5F + widthAspect; + xCropFactor = 0.5F - widthAspect; } } - ShaderInstance instance = VRShaders.BLIT_VR_SHADER; - instance.setSampler("DiffuseSampler", source.getColorTextureId()); - - instance.apply(); - - BufferBuilder bufferbuilder = RenderSystem.renderThreadTesselator().getBuilder(); - bufferbuilder.begin(VertexFormat.Mode.QUADS, instance.getVertexFormat()); - - bufferbuilder.vertex(-1.0F, -1.0F, 0.0F).uv(xMin, yMin).endVertex(); - bufferbuilder.vertex(1.0F, -1.0F, 0.0F).uv(xMax, yMin).endVertex(); - bufferbuilder.vertex(1.0F, 1.0F, 0.0F).uv(xMax, yMax).endVertex(); - bufferbuilder.vertex(-1.0F, 1.0F, 0.0F).uv(xMin, yMax).endVertex(); - BufferUploader.draw(bufferbuilder.end()); - instance.clear(); + int xMin = (int) (xCropFactor * source.width); + int yMin = (int) (yCropFactor * source.height); + int xMax = source.width - xMin; + int yMax = source.height - yMin; + + GlStateManager._glBindFramebuffer(GL30C.GL_READ_FRAMEBUFFER, source.frameBufferId); + GlStateManager._glBlitFrameBuffer( + xMin, yMin, xMax, yMax, + left, top, right, bottom, + GL11C.GL_COLOR_BUFFER_BIT, GL11C.GL_LINEAR); + GlStateManager._glBindFramebuffer(GL30C.GL_READ_FRAMEBUFFER, 0); + } - RenderSystem.depthMask(true); - RenderSystem.colorMask(true, true, true, true); + /** + * blits the given {@code source} RenderTarget to the bound framebuffer + * @param source RenderTarget to draw to the screen + * @param left left edge of the target area + * @param top top edge of the target area + * @param right right edge width of the target area + * @param bottom bottom edge of the target area + */ + private static void blitFramebuffer( + RenderTarget source, int left, int top, int right, int bottom) + { + GlStateManager._glBindFramebuffer(GL30C.GL_READ_FRAMEBUFFER, source.frameBufferId); + GlStateManager._glBlitFrameBuffer( + 0, 0, source.width, source.height, + left, top, right, bottom, + GL11C.GL_COLOR_BUFFER_BIT, GL11C.GL_LINEAR); + GlStateManager._glBindFramebuffer(GL30C.GL_READ_FRAMEBUFFER, 0); } } diff --git a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java index a173f1ac2..2d0d498cd 100644 --- a/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java +++ b/common/src/main/java/org/vivecraft/client_vr/render/helpers/VRPassHelper.java @@ -8,6 +8,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiGraphics; import org.joml.Matrix4f; +import org.lwjgl.opengl.GL11C; import org.lwjgl.opengl.GL13C; import org.lwjgl.opengl.GL30C; import org.vivecraft.client.utils.ClientUtils; @@ -243,7 +244,15 @@ public static void renderAndSubmit(boolean renderLevel, long nanoTime, float act MC.getProfiler().pop(); DATA_HOLDER.vrPlayer.postRender(actualPartialTick); - MC.getProfiler().push("Display/Reproject"); + + MC.getProfiler().push("vrMirror"); + // use the vanilla target for the mirror + RenderPassManager.setVanillaRenderPass(); + MC.mainRenderTarget.bindWrite(true); + ShaderHelper.drawMirror(); + RenderHelper.checkGLError("post-mirror"); + + MC.getProfiler().popPush("Display/Reproject"); try { DATA_HOLDER.vrRenderer.endFrame(); diff --git a/common/src/main/java/org/vivecraft/mixin/client/blaze3d/RenderTargetMixin.java b/common/src/main/java/org/vivecraft/mixin/client/blaze3d/RenderTargetMixin.java index e72594dbc..b3f9afa5e 100644 --- a/common/src/main/java/org/vivecraft/mixin/client/blaze3d/RenderTargetMixin.java +++ b/common/src/main/java/org/vivecraft/mixin/client/blaze3d/RenderTargetMixin.java @@ -23,8 +23,6 @@ public abstract class RenderTargetMixin implements RenderTargetExtension { public int width; @Shadow public int height; - @Shadow - protected int colorTextureId; @Unique private int vivecraft$texId = -1; @Unique @@ -54,11 +52,6 @@ public abstract class RenderTargetMixin implements RenderTargetExtension { this.vivecraft$texId = texId; } - @Override - public void vivecraft$setColorid(int colorid) { - this.colorTextureId = colorid; - } - @Unique public void vivecraft$setLinearFilter(boolean linearFilter) { this.vivecraft$linearFilter = linearFilter; diff --git a/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java b/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java index 7ab155cc5..b69ad825e 100644 --- a/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java +++ b/common/src/main/java/org/vivecraft/mixin/client_vr/MinecraftVRMixin.java @@ -73,7 +73,6 @@ import org.vivecraft.client_vr.render.VRFirstPersonArmSwing; import org.vivecraft.client_vr.render.MirrorNotification; import org.vivecraft.client_vr.render.helpers.RenderHelper; -import org.vivecraft.client_vr.render.helpers.ShaderHelper; import org.vivecraft.client_vr.settings.VRHotkeys; import org.vivecraft.client_vr.settings.VRSettings; import org.vivecraft.client_xr.render_pass.RenderPassManager; @@ -391,17 +390,6 @@ public abstract class MinecraftVRMixin implements MinecraftExtension { return VRState.VR_RUNNING ? null : original; } - @WrapOperation(method = "runTick", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/pipeline/RenderTarget;blitToScreen(II)V")) - private void vivecraft$blitMirror(RenderTarget instance, int width, int height, Operation original) { - if (!VRState.VR_RUNNING) { - original.call(instance, width, height); - } else { - this.profiler.popPush("vrMirror"); - ShaderHelper.drawMirror(); - RenderHelper.checkGLError("post-mirror"); - } - } - // the VR runtime handles the frame limit, no need to manually limit it 60fps @ModifyExpressionValue(method = "doWorldLoad", at = @At(value = "CONSTANT", args = "longValue=16")) private long vivecraft$noWaitOnLevelLoad(long original) { diff --git a/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.fsh b/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.fsh deleted file mode 100644 index d961ccfc5..000000000 --- a/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.fsh +++ /dev/null @@ -1,11 +0,0 @@ -#version 150 core - -uniform sampler2D DiffuseSampler; - -in vec2 texCoordinates; - -out vec4 fragColor; - -void main(){ - fragColor = texture(DiffuseSampler, texCoordinates.st); -} diff --git a/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.json b/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.json deleted file mode 100644 index bf37dbbc2..000000000 --- a/common/src/main/resources/assets/minecraft/shaders/core/blit_vr.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "vertex": "passthrough_vr", - "fragment": "blit_vr", - "attributes": ["Position", "UV0"], - "samplers": [ - { - "name": "DiffuseSampler" - } - ], - "uniforms": [] -} diff --git a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality_vr.fsh b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality_vr.fsh index 346fab2b2..44d5465b1 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/mixedreality_vr.fsh +++ b/common/src/main/resources/assets/minecraft/shaders/core/mixedreality_vr.fsh @@ -1,7 +1,5 @@ #version 330 core -uniform sampler2D firstPersonColor; - uniform sampler2D thirdPersonColor; uniform sampler2D thirdPersonDepth; @@ -63,9 +61,6 @@ void main(void) { out_Color.rgb = vec3(1.0); } } - } else if (texCoordinates.x >= 0.5 && texCoordinates.y < 0.5){ - // first person - out_Color.rgb = texture(firstPersonColor, sampleTexcCoord).rgb; } } else { // side by side