Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mirror changes, and some other stuff #322

Open
wants to merge 2 commits into
base: openxr
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
20 changes: 15 additions & 5 deletions common/src/main/java/org/vivecraft/client_vr/VRTextureTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -570,11 +569,17 @@ 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);
if (((WindowExtension) (Object) Minecraft.getInstance().getWindow()).vivecraft$getActualScreenWidth() > 0 &&
((WindowExtension) (Object) Minecraft.getInstance().getWindow()).vivecraft$getActualScreenHeight() > 0)
{
// we use the vanilla target to draw the mirror, so need to keep its size up to date
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");
Expand Down Expand Up @@ -617,7 +622,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;

Expand Down Expand Up @@ -726,8 +741,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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}


Expand All @@ -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;
Expand All @@ -104,7 +113,6 @@ protected void destroyBuffers() {
this.framebufferEye1.destroyBuffers();
this.framebufferEye1 = null;
}

if (this.LeftEyeTextureId > -1) {
TextureUtil.releaseTextureId(this.LeftEyeTextureId);
this.LeftEyeTextureId = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -226,11 +217,4 @@ protected void destroyBuffers() {
this.RightEyeTextureId = -1;
}
}

@Override
public void destroy() {
super.destroy();
this.hiddenMeshes[0].free();
this.hiddenMeshes[1].free();
}
}
Loading