diff --git a/src/main/java/me/cortex/voxy/client/core/model/BakedBlockEntityModel.java b/src/main/java/me/cortex/voxy/client/core/model/BakedBlockEntityModel.java index ed0d4572..317e862a 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/BakedBlockEntityModel.java +++ b/src/main/java/me/cortex/voxy/client/core/model/BakedBlockEntityModel.java @@ -1,5 +1,6 @@ package me.cortex.voxy.client.core.model; +import com.mojang.blaze3d.textures.GpuTexture; import com.mojang.blaze3d.vertex.VertexFormat; import me.cortex.voxy.common.Logger; import net.minecraft.block.BlockEntityProvider; @@ -11,6 +12,7 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; +import org.joml.Matrix4f; import java.util.ArrayList; import java.util.HashMap; @@ -104,27 +106,24 @@ public class BakedBlockEntityModel { this.layers = layers; } - public void renderOut() { - //TODO:FIXME: CANT RUN ON RENDER THREAD - if (false) { - System.err.println("Model entity baking not yet supported offthread baking"); - } else { - var vc = Tessellator.getInstance(); - for (var layer : this.layers) { - if (layer.isEmpty()) continue; - var bb = vc.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR); - if (layer.layer instanceof RenderLayer.MultiPhase mp) { - Identifier textureId = mp.phases.texture.getId().orElse(null); - if (textureId == null) { - System.err.println("ERROR: Empty texture id for layer: " + layer); - } else { - var texture = MinecraftClient.getInstance().getTextureManager().getTexture(textureId); - glBindTexture(GL_TEXTURE_2D, ((GlTexture)texture.getGlTexture()).getGlId()); - } + public void renderOut(Matrix4f matrix, GpuTexture texture) { + var vc = Tessellator.getInstance(); + for (var layer : this.layers) { + if (layer.isEmpty()) continue; + if (layer.layer instanceof RenderLayer.MultiPhase mp) { + Identifier textureId = mp.phases.texture.getId().orElse(null); + if (textureId == null) { + System.err.println("ERROR: Empty texture id for layer: " + layer); + } else { + texture = MinecraftClient.getInstance().getTextureManager().getTexture(textureId).getGlTexture(); } - layer.putInto(bb); - //BudgetBufferRenderer.draw(bb.end()); } + + var bb = vc.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR); + layer.putInto(bb); + var mesh = bb.endNullable(); + if (mesh!=null) + BudgetBufferRenderer.draw(mesh, texture, matrix); } } diff --git a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java index 5ca01f73..c756598f 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java +++ b/src/main/java/me/cortex/voxy/client/core/model/ModelFactory.java @@ -5,7 +5,7 @@ import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; import it.unimi.dsi.fastutil.objects.ObjectSet; import me.cortex.voxy.client.core.gl.Capabilities; -import me.cortex.voxy.client.core.model.bakery.ModelTextureBakery2; +import me.cortex.voxy.client.core.model.bakery.ModelTextureBakery; import me.cortex.voxy.client.core.rendering.util.RawDownloadStream; import me.cortex.voxy.client.core.rendering.util.UploadStream; import me.cortex.voxy.common.Logger; @@ -66,7 +66,7 @@ public class ModelFactory { private final Biome DEFAULT_BIOME = MinecraftClient.getInstance().world.getRegistryManager().getOrThrow(RegistryKeys.BIOME).get(BiomeKeys.PLAINS); - public final ModelTextureBakery2 bakery; + public final ModelTextureBakery bakery; //Model data might also contain a constant colour if the colour resolver produces a constant colour, this saves space in the @@ -123,7 +123,7 @@ public class ModelFactory { this.mapper = mapper; this.storage = storage; this.downstream = downstream; - this.bakery = new ModelTextureBakery2(MODEL_TEXTURE_SIZE, MODEL_TEXTURE_SIZE); + this.bakery = new ModelTextureBakery(MODEL_TEXTURE_SIZE, MODEL_TEXTURE_SIZE); this.metadataCache = new long[1<<16]; this.fluidStateLUT = new int[1<<16]; diff --git a/src/main/java/me/cortex/voxy/client/core/model/ModelTextureBakery.java b/src/main/java/me/cortex/voxy/client/core/model/ModelTextureBakery.java deleted file mode 100644 index 97914240..00000000 --- a/src/main/java/me/cortex/voxy/client/core/model/ModelTextureBakery.java +++ /dev/null @@ -1,381 +0,0 @@ -package me.cortex.voxy.client.core.model; - -import com.mojang.blaze3d.vertex.VertexFormat; -import me.cortex.voxy.client.core.gl.GlFramebuffer; -import me.cortex.voxy.client.core.gl.GlTexture; -import me.cortex.voxy.client.core.gl.shader.Shader; -import me.cortex.voxy.client.core.gl.shader.ShaderType; -import me.cortex.voxy.client.core.rendering.util.GlStateCapture; -import net.minecraft.block.*; -import net.minecraft.block.entity.BlockEntity; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gl.GlUniform; -import net.minecraft.client.render.*; -import net.minecraft.client.render.model.BlockStateModel; -import net.minecraft.client.util.BufferAllocator; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.fluid.FluidState; -import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.ColorHelper; -import net.minecraft.util.math.Direction; -import net.minecraft.util.math.RotationAxis; -import net.minecraft.util.math.random.LocalRandom; -import net.minecraft.world.BlockRenderView; -import net.minecraft.world.LightType; -import net.minecraft.world.biome.ColorResolver; -import net.minecraft.world.chunk.light.LightingProvider; -import org.jetbrains.annotations.Nullable; -import org.joml.Matrix4f; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL11C; - -import java.util.ArrayList; -import java.util.List; - -import static org.lwjgl.opengl.ARBDirectStateAccess.*; -import static org.lwjgl.opengl.ARBShaderImageLoadStore.GL_FRAMEBUFFER_BARRIER_BIT; -import static org.lwjgl.opengl.ARBShaderImageLoadStore.glMemoryBarrier; -import static org.lwjgl.opengl.GL11C.GL_TEXTURE_2D; -import static org.lwjgl.opengl.GL14C.glBlendFuncSeparate; -import static org.lwjgl.opengl.GL15C.glBindBuffer; -import static org.lwjgl.opengl.GL20C.glUniformMatrix4fv; -import static org.lwjgl.opengl.GL21C.GL_PIXEL_PACK_BUFFER; -import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL43.*; - -//Builds a texture for each face of a model -public class ModelTextureBakery { - private final int width; - private final int height; - private final GlTexture colourTex; - private final GlTexture depthTex; - private final GlTexture depthTexView; - private final GlFramebuffer framebuffer; - private final GlStateCapture glState = GlStateCapture.make() - .addCapability(GL_DEPTH_TEST) - .addCapability(GL_STENCIL_TEST) - .addCapability(GL_BLEND) - .addCapability(GL_CULL_FACE) - .addTexture(GL_TEXTURE0) - .addTexture(GL_TEXTURE1) - .addTexture(GL_TEXTURE2) - .build() - ; - private final Shader rasterShader = Shader.make() - .add(ShaderType.VERTEX, "voxy:bakery/position_tex.vsh") - .add(ShaderType.FRAGMENT, "voxy:bakery/position_tex.fsh") - .compile() - .name("ModelBaker"); - - private final Shader copyOutShader; - - private static final List FACE_VIEWS = new ArrayList<>(); - - - public ModelTextureBakery(int width, int height) { - //TODO: Make this run in a seperate opengl context so that it can run in a seperate thread - - this.width = width; - this.height = height; - this.colourTex = new GlTexture().store(GL_RGBA8, 1, width, height).name("ModelBakeryColour"); - this.depthTex = new GlTexture().store(GL_DEPTH24_STENCIL8, 1, width, height).name("ModelBakeryDepth"); - glTextureParameteri(this.depthTex.id, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX); - this.depthTexView = this.depthTex.createView(); - glTextureParameteri(this.depthTex.id, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT); - - this.framebuffer = new GlFramebuffer().bind(GL_COLOR_ATTACHMENT0, this.colourTex).bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthTex).verify().name("ModelFramebuffer"); - - glTextureParameteri(this.depthTexView.id, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX); - - this.copyOutShader = Shader.make() - .define("WIDTH", width) - .define("HEIGHT", height) - .add(ShaderType.COMPUTE, "voxy:bakery/buffercopy.comp") - .compile() - .name("ModelBakeryOut"); - - //This is done to help make debugging easier - FACE_VIEWS.clear(); - AddViews(); - } - - private static void AddViews() { - //TODO: FIXME: need to bake in the correct orientation, HOWEVER some orientations require a flipped winding order!!!! - - addView(-90,0, 0, false);//Direction.DOWN - addView(90,0, 0, false);//Direction.UP - addView(0,180, 0, true);//Direction.NORTH - addView(0,0, 0, false);//Direction.SOUTH - //TODO: check these arnt the wrong way round - addView(0,90, 270, false);//Direction.EAST - addView(0,270, 270, false);//Direction.WEST - } - - private static void addView(float pitch, float yaw, float rotation, boolean flipX) { - var stack = new MatrixStack(); - stack.translate(0.5f,0.5f,0.5f); - stack.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(rotation)); - stack.multiply(RotationAxis.POSITIVE_X.rotationDegrees(pitch)); - stack.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(yaw)); - stack.translate(-0.5f,-0.5f,-0.5f); - FACE_VIEWS.add(stack); - } - - - - //TODO: For block entities, also somehow attempt to render the default block entity, e.g. chests and stuff - // cause that will result in ok looking micro details in the terrain - public void renderFacesToStream(BlockState state, long randomValue, boolean renderFluid, int streamBuffer, int streamBaseOffset) { - this.glState.capture(); - var model = MinecraftClient.getInstance() - .getBakedModelManager() - .getBlockModels() - .getModel(state); - - BakedBlockEntityModel entityModel = state.hasBlockEntity()?BakedBlockEntityModel.bake(state):null; - - int oldFB = GlStateManager.getBoundFramebuffer(); - GL11C.glViewport(0, 0, this.width, this.height); - - var projection = new Matrix4f().identity().set(new float[]{ - 2,0,0,0, - 0, 2,0,0, - 0,0, -1f,0, - -1,-1,0,1, - }); - - - - RenderLayer renderLayer = null; - if (!renderFluid) { - renderLayer = RenderLayers.getBlockLayer(state); - } else { - renderLayer = RenderLayers.getFluidLayer(state.getFluidState()); - } - - - //TODO: figure out why calling this makes minecraft render black - //renderLayer.startDrawing(); - - glClearColor(0,0,0,0); - glClearDepth(1); - glClearStencil(0); - glBindFramebuffer(GL_FRAMEBUFFER, this.framebuffer.id); - - glEnable(GL_STENCIL_TEST); - glDepthRange(0, 1); - glDepthMask(true); - glEnable(GL_BLEND); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - //glDepthFunc(GL_LESS); - - - //TODO: Find a better solution - if (renderLayer == RenderLayer.getTranslucent()) { - //Very hacky blend function to retain the effect of the applied alpha since we dont really want to apply alpha - // this is because we apply the alpha again when rendering the terrain meaning the alpha is being double applied - glBlendFuncSeparate(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - } else { - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - } - - - //glBlendFunc(GL_ONE, GL_ONE); - - glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); - glStencilFunc(GL_ALWAYS, 1, 0xFF); - glStencilMask(0xFF); - - int texId = ((net.minecraft.client.texture.GlTexture)MinecraftClient.getInstance().getTextureManager().getTexture(Identifier.of("minecraft", "textures/atlas/blocks.png")).getGlTexture()).getGlId(); - - final int TEXTURE_SIZE = this.width*this.height *4;//NOTE! assume here that both depth and colour are 4 bytes in size - for (int i = 0; i < FACE_VIEWS.size(); i++) { - int faceOffset = streamBaseOffset + TEXTURE_SIZE*i*2; - captureViewToStream(state, model, entityModel, FACE_VIEWS.get(i), randomValue, i, renderFluid, texId, projection, streamBuffer, faceOffset); - - if (false) { - int SIZE = 128; - int x = (i % 3) * SIZE; - int y = (i / 3) * SIZE; - glBlitNamedFramebuffer(this.framebuffer.id, oldFB, 0, 0, 16, 16, x, y, x + SIZE, y + SIZE, GL_COLOR_BUFFER_BIT, GL_NEAREST); - } - } - - renderLayer.endDrawing(); - - glDisable(GL_STENCIL_TEST); - glDisable(GL_BLEND); - - glBindFramebuffer(GL_FRAMEBUFFER, oldFB); - GL11C.glViewport(GlStateManager.Viewport.getX(), GlStateManager.Viewport.getY(), GlStateManager.Viewport.getWidth(), GlStateManager.Viewport.getHeight()); - - //TODO: FIXME: fully revert the state of opengl - - this.glState.restore(); - } - - private final BufferAllocator allocator = new BufferAllocator(786432); - private void captureViewToStream(BlockState state, BlockStateModel model, BakedBlockEntityModel blockEntityModel, MatrixStack stack, long randomValue, int face, boolean renderFluid, int textureId, Matrix4f projection, int streamBuffer, int streamOffset) { - this.rasterShader.bind(); - glActiveTexture(GL_TEXTURE0); - glUniform1i(0, 0); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - float[] mat = new float[4*4]; - new Matrix4f(projection).mul(stack.peek().getPositionMatrix()).get(mat); - glUniformMatrix4fv(1, false, mat); - - - if (blockEntityModel != null && !renderFluid) { - blockEntityModel.renderOut(); - } - - var bb = new BufferBuilder(this.allocator, VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR) { - @Override - public void vertex(float x, float y, float z, int color, float u, float v, int overlay, int light, float normalX, float normalY, float normalZ) { - super.vertex(x, y, z, ColorHelper.getArgb(0,0,1), u, v, overlay, light, normalX, normalY, normalZ); - } - - @Override - public VertexConsumer color(int argb) { - return super.color(ColorHelper.getArgb(0,0,1)); - } - - @Override - public VertexConsumer color(int red, int green, int blue, int alpha) { - return super.color(0, 0, 1, 255); - } - }; - if (!renderFluid) { - //TODO: need to do 2 variants for quads, one which have coloured, ones that dont, might be able to pull a spare bit - // at the end whether or not a pixel should be mixed with texture - renderQuads(bb, model, new MatrixStack(), randomValue); - } else { - MinecraftClient.getInstance().getBlockRenderManager().renderFluid(BlockPos.ORIGIN, new BlockRenderView() { - @Override - public float getBrightness(Direction direction, boolean shaded) { - return 0; - } - - @Override - public LightingProvider getLightingProvider() { - return null; - } - - @Override - public int getLightLevel(LightType type, BlockPos pos) { - return 0; - } - - @Override - public int getColor(BlockPos pos, ColorResolver colorResolver) { - return 0; - } - - @Nullable - @Override - public BlockEntity getBlockEntity(BlockPos pos) { - return null; - } - - @Override - public BlockState getBlockState(BlockPos pos) { - if (pos.equals(Direction.byIndex(face).getVector())) { - return Blocks.AIR.getDefaultState(); - } - - //Fixme: - // This makes it so that the top face of water is always air, if this is commented out - // the up block will be a liquid state which makes the sides full - // if this is uncommented, that issue is fixed but e.g. stacking water layers ontop of eachother - // doesnt fill the side of the block - - //if (pos.getY() == 1) { - // return Blocks.AIR.getDefaultState(); - //} - return state; - } - - @Override - public FluidState getFluidState(BlockPos pos) { - if (pos.equals(Direction.byIndex(face).getVector())) { - return Blocks.AIR.getDefaultState().getFluidState(); - } - //if (pos.getY() == 1) { - // return Blocks.AIR.getDefaultState().getFluidState(); - //} - return state.getFluidState(); - } - - @Override - public int getHeight() { - return 0; - } - - @Override - public int getBottomY() { - return 0; - } - }, bb, state, state.getFluidState()); - } - - glBindTexture(GL_TEXTURE_2D, textureId); - try { - //System.err.println("REPLACE THE UPLOADING WITH THREAD SAFE VARIENT"); - BudgetBufferRenderer.draw(bb.end()); - } catch (IllegalStateException e) { - //System.err.println("Got empty buffer builder! for block " + state); - } - - glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT); - - this.emitToStream(streamBuffer, streamOffset); - } - - //TODO: FIXME: Mesa is broken when trying to read from a sampler of GL_STENCIL_INDEX - // it seems to just ignore the value set in GL_DEPTH_STENCIL_TEXTURE_MODE - private void emitToStream(int streamBuffer, int streamOffset) { - if (streamOffset%4 != 0) { - throw new IllegalArgumentException(); - } - this.copyOutShader.bind(); - glActiveTexture(GL_TEXTURE0); - GL11C.glBindTexture(GL11.GL_TEXTURE_2D, this.colourTex.id); - glBindSampler(0, 0); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL11.GL_TEXTURE_2D, this.depthTex.id); - glBindSampler(1, 0); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL11.GL_TEXTURE_2D, this.depthTexView.id); - glBindSampler(2, 0); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, streamBuffer); - glUniform1ui(4, streamOffset/4); - - glDispatchCompute(1,1,1); - } - - private static void renderQuads(BufferBuilder builder, BlockStateModel model, MatrixStack stack, long randomValue) { - for (Direction direction : new Direction[]{Direction.DOWN, Direction.UP, Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST, null}) { - for (var part : model.getParts(new LocalRandom(randomValue))) { - var quads = part.getQuads(direction); - for (var quad : quads) { - //TODO: mark pixels that have - int meta = 1; - builder.quad(stack.peek(), quad, ((meta >> 16) & 0xff) / 255f, ((meta >> 8) & 0xff) / 255f, (meta & 0xff) / 255f, 1.0f, 0, 0); - } - } - } - } - - public void free() { - this.framebuffer.free(); - this.colourTex.free(); - this.depthTexView.free(); - this.depthTex.free(); - this.rasterShader.free(); - this.copyOutShader.free(); - this.allocator.close(); - } -} diff --git a/src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery2.java b/src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery.java similarity index 79% rename from src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery2.java rename to src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery.java index 6bc1bde8..b3bd7685 100644 --- a/src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery2.java +++ b/src/main/java/me/cortex/voxy/client/core/model/bakery/ModelTextureBakery.java @@ -1,21 +1,13 @@ package me.cortex.voxy.client.core.model.bakery; -import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.textures.GpuTexture; import com.mojang.blaze3d.vertex.VertexFormat; -import me.cortex.voxy.client.core.gl.GlFramebuffer; -import me.cortex.voxy.client.core.gl.GlTexture; -import me.cortex.voxy.client.core.gl.shader.Shader; -import me.cortex.voxy.client.core.gl.shader.ShaderType; import me.cortex.voxy.client.core.model.BakedBlockEntityModel; import me.cortex.voxy.client.core.model.BudgetBufferRenderer; -import me.cortex.voxy.client.core.rendering.util.GlStateCapture; -import net.caffeinemc.mods.sodium.client.render.chunk.terrain.DefaultTerrainRenderPasses; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gl.GlBackend; import net.minecraft.client.render.*; import net.minecraft.client.render.model.BlockStateModel; import net.minecraft.client.util.BufferAllocator; @@ -33,30 +25,24 @@ import net.minecraft.world.biome.ColorResolver; import net.minecraft.world.chunk.light.LightingProvider; import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; -import org.lwjgl.opengl.GL11; -import org.lwjgl.opengl.GL11C; import java.util.ArrayList; import java.util.List; -import static org.lwjgl.opengl.ARBDirectStateAccess.glBlitNamedFramebuffer; -import static org.lwjgl.opengl.ARBDirectStateAccess.glTextureParameteri; import static org.lwjgl.opengl.ARBShaderImageLoadStore.GL_FRAMEBUFFER_BARRIER_BIT; import static org.lwjgl.opengl.ARBShaderImageLoadStore.glMemoryBarrier; -import static org.lwjgl.opengl.GL11C.GL_TEXTURE_2D; import static org.lwjgl.opengl.GL14C.glBlendFuncSeparate; import static org.lwjgl.opengl.GL20C.glUniformMatrix4fv; import static org.lwjgl.opengl.GL30.*; -import static org.lwjgl.opengl.GL43.*; //Builds a texture for each face of a model -public class ModelTextureBakery2 { +public class ModelTextureBakery { private static final List FACE_VIEWS = new ArrayList<>(); private final int width; private final int height; private final GlViewCapture capture; - public ModelTextureBakery2(int width, int height) { + public ModelTextureBakery(int width, int height) { this.width = width; this.height = height; this.capture = new GlViewCapture(width, height); @@ -93,11 +79,6 @@ public class ModelTextureBakery2 { //TODO: For block entities, also somehow attempt to render the default block entity, e.g. chests and stuff // cause that will result in ok looking micro details in the terrain public void renderFacesToStream(BlockState state, long randomValue, boolean renderFluid, int streamBuffer, int streamBaseOffset) { - int[] viewport = new int[4]; - glGetIntegerv(GL_VIEWPORT, viewport); - int oldFB = glGetInteger(GL_DRAW_FRAMEBUFFER_BINDING); - - var model = MinecraftClient.getInstance() .getBakedModelManager() .getBlockModels() @@ -160,7 +141,11 @@ public class ModelTextureBakery2 { for (int i = 0; i < FACE_VIEWS.size(); i++) { glViewport((i%3)*this.width, (i/3)*this.height, this.width, this.height); glBindFramebuffer(GL_FRAMEBUFFER, this.capture.framebuffer.id); - captureViewToStream(state, model, entityModel, new Matrix4f(projection).mul(FACE_VIEWS.get(i).peek().getPositionMatrix()), randomValue, i, renderFluid, tex); + var transform = new Matrix4f(projection).mul(FACE_VIEWS.get(i).peek().getPositionMatrix()); + if (entityModel!=null&&!renderFluid) { + entityModel.renderOut(transform, tex); + } + this.rasterView(state, model, transform, randomValue, i, renderFluid, tex); } @@ -169,35 +154,12 @@ public class ModelTextureBakery2 { glDisable(GL_STENCIL_TEST); glDisable(GL_BLEND); - glBindFramebuffer(GL_FRAMEBUFFER, oldFB); - - GL11C.glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); - glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT); this.capture.emitToStream(streamBuffer, streamBaseOffset); - - /* - var target = DefaultTerrainRenderPasses.CUTOUT.getTarget(); - int boundFB = ((net.minecraft.client.texture.GlTexture) target.getColorAttachment()).getOrCreateFramebuffer(((GlBackend) RenderSystem.getDevice()).getFramebufferManager(), target.getDepthAttachment()); - glBlitNamedFramebuffer(this.capture.framebuffer.id, boundFB, 0, 0, 16*3, 16*2, 0, 0,16*3*16, 16*2*16, GL_COLOR_BUFFER_BIT, GL_NEAREST); - */ - //TODO: FIXME: fully revert the state of opengl } private final BufferAllocator allocator = new BufferAllocator(786432); - private void captureViewToStream(BlockState state, BlockStateModel model, BakedBlockEntityModel blockEntityModel, Matrix4f transform, long randomValue, int face, boolean renderFluid, GpuTexture texture) { - //glActiveTexture(GL_TEXTURE0); - //glUniform1i(0, 0); - - //float[] mat = new float[4*4]; - //transform.get(mat); - //glUniformMatrix4fv(1, false, mat); - - - if (blockEntityModel != null && !renderFluid) { - blockEntityModel.renderOut(); - } - + private void rasterView(BlockState state, BlockStateModel model, Matrix4f transform, long randomValue, int face, boolean renderFluid, GpuTexture texture) { var bb = new BufferBuilder(this.allocator, VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR) { @Override public void vertex(float x, float y, float z, int color, float u, float v, int overlay, int light, float normalX, float normalY, float normalZ) { @@ -287,12 +249,9 @@ public class ModelTextureBakery2 { }, bb, state, state.getFluidState()); } - try { - //System.err.println("REPLACE THE UPLOADING WITH THREAD SAFE VARIENT"); - BudgetBufferRenderer.draw(bb.end(), texture, transform); - } catch (IllegalStateException e) { - //System.err.println("Got empty buffer builder! for block " + state); - } + var mesh = bb.endNullable(); + if (mesh != null) + BudgetBufferRenderer.draw(mesh, texture, transform); } private static void renderQuads(BufferBuilder builder, BlockStateModel model, MatrixStack stack, long randomValue) { diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/LightMapHelper.java b/src/main/java/me/cortex/voxy/client/core/rendering/LightMapHelper.java index 1eafcf07..0f9bf9c0 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/LightMapHelper.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/LightMapHelper.java @@ -8,35 +8,12 @@ import org.lwjgl.system.MemoryUtil; import static org.lwjgl.opengl.ARBUniformBufferObject.glBindBufferBase; import static org.lwjgl.opengl.GL11.glBindTexture; -import static org.lwjgl.opengl.GL33.glBindSampler; -import static org.lwjgl.opengl.GL33.glGenSamplers; +import static org.lwjgl.opengl.GL33.*; import static org.lwjgl.opengl.GL43.GL_SHADER_STORAGE_BUFFER; import static org.lwjgl.opengl.GL45.glBindTextureUnit; public class LightMapHelper { - private static final int SAMPLER = glGenSamplers(); - - static { - //SAMPLER - } - /* - private static final GlBuffer LIGHT_MAP_BUFFER = new GlBuffer(256*4); - public static void tickLightmap() { - long upload = UploadStream.INSTANCE.upload(LIGHT_MAP_BUFFER, 0, 256*4); - var lmt = MinecraftClient.getInstance().gameRenderer.getLightmapTextureManager().lightmapFramebuffer.getImage(); - for (int light = 0; light < 256; light++) { - int x = light&0xF; - int y = ((light>>4)&0xF); - int sample = lmt.getColor(x,y); - sample = ((sample&0xFF0000)>>16)|(sample&0xFF00)|((sample&0xFF)<<16); - MemoryUtil.memPutInt(upload + (((x<<4)|(y))*4), sample|(0xFF<<28)); - } - //TODO: CRITICAL FIXME - }*/ - public static void bind(int lightingIndex) { - //glBindBufferBase(GL_SHADER_STORAGE_BUFFER, lightingBufferIndex, LIGHT_MAP_BUFFER.id); - //glBindSampler(lightingIndex, SAMPLER); glBindTextureUnit(lightingIndex, ((net.minecraft.client.texture.GlTexture)(MinecraftClient.getInstance().gameRenderer.getLightmapTextureManager().getGlTexture())).getGlId()); } } \ No newline at end of file diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/VoxyRenderSystem.java b/src/main/java/me/cortex/voxy/client/core/rendering/VoxyRenderSystem.java index f0af1def..a2d9fd9c 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/VoxyRenderSystem.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/VoxyRenderSystem.java @@ -1,23 +1,16 @@ package me.cortex.voxy.client.core.rendering; import com.mojang.blaze3d.opengl.GlConst; -import com.mojang.blaze3d.opengl.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; import me.cortex.voxy.client.config.VoxyConfig; import me.cortex.voxy.client.core.gl.Capabilities; import me.cortex.voxy.client.core.gl.GlBuffer; -import me.cortex.voxy.client.core.gl.GlTexture; -import me.cortex.voxy.client.core.model.ColourDepthTextureData; import me.cortex.voxy.client.core.model.ModelBakerySubsystem; -import me.cortex.voxy.client.core.model.ModelTextureBakery; -import me.cortex.voxy.client.core.model.bakery.ModelTextureBakery2; import me.cortex.voxy.client.core.rendering.building.RenderDataFactory45; import me.cortex.voxy.client.core.rendering.building.RenderGenerationService; import me.cortex.voxy.client.core.rendering.post.PostProcessing; import me.cortex.voxy.client.core.rendering.util.DownloadStream; -import me.cortex.voxy.client.core.rendering.util.RawDownloadStream; import me.cortex.voxy.client.core.util.IrisUtil; -import me.cortex.voxy.client.core.util.RingTracker; import me.cortex.voxy.common.Logger; import me.cortex.voxy.common.thread.ServiceThreadPool; import me.cortex.voxy.common.world.WorldEngine; @@ -25,18 +18,13 @@ import me.cortex.voxy.common.world.WorldSection; import me.cortex.voxy.common.world.other.Mapper; import me.cortex.voxy.commonImpl.VoxyCommon; import net.caffeinemc.mods.sodium.client.render.chunk.terrain.DefaultTerrainRenderPasses; -import net.caffeinemc.mods.sodium.client.render.chunk.terrain.TerrainRenderPass; -import net.irisshaders.iris.targets.RenderTarget; -import net.minecraft.block.Blocks; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gl.GlBackend; import net.minecraft.client.render.Camera; import net.minecraft.client.render.Frustum; -import net.minecraft.client.render.LightmapTextureManager; import net.minecraft.client.util.math.MatrixStack; import org.joml.Matrix4f; import org.lwjgl.opengl.GL11; -import org.lwjgl.system.MemoryUtil; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/post/PostProcessing.java b/src/main/java/me/cortex/voxy/client/core/rendering/post/PostProcessing.java index 739b6fba..0dcd894b 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/post/PostProcessing.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/post/PostProcessing.java @@ -10,18 +10,13 @@ import org.joml.Matrix4f; import org.lwjgl.opengl.GL11C; import static org.lwjgl.opengl.ARBComputeShader.glDispatchCompute; -import static org.lwjgl.opengl.ARBFramebufferObject.*; import static org.lwjgl.opengl.ARBShaderImageLoadStore.glBindImageTexture; -import static org.lwjgl.opengl.GL11.*; -import static org.lwjgl.opengl.GL13.*; import static org.lwjgl.opengl.GL15.GL_READ_WRITE; import static org.lwjgl.opengl.GL20.glUniformMatrix4fv; -import static org.lwjgl.opengl.GL20C.glGetUniformLocation; -import static org.lwjgl.opengl.GL20C.glGetUniformfv; +import static org.lwjgl.opengl.GL30C.*; +import static org.lwjgl.opengl.GL30C.GL_DEPTH24_STENCIL8; import static org.lwjgl.opengl.GL43.GL_DEPTH_STENCIL_TEXTURE_MODE; -import static org.lwjgl.opengl.GL44C.glBindImageTextures; -import static org.lwjgl.opengl.GL45C.glBlitNamedFramebuffer; -import static org.lwjgl.opengl.GL45C.glTextureParameterf; +import static org.lwjgl.opengl.GL45C.*; public class PostProcessing { private final GlFramebuffer framebuffer; @@ -72,16 +67,15 @@ public class PostProcessing { glTextureParameterf(this.colour.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTextureParameterf(this.colourSSAO.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTextureParameterf(this.colourSSAO.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - //glTextureParameterf(this.depthStencil.id, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - //glTextureParameterf(this.depthStencil.id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTextureParameterf(this.depthStencil.id, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT); - this.framebuffer.bind(GL_COLOR_ATTACHMENT0, this.colour); - this.framebuffer.bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthStencil); - this.framebuffer.verify(); + this.framebuffer.bind(GL_COLOR_ATTACHMENT0, this.colour) + .bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthStencil) + .verify(); - this.framebufferSSAO.bind(GL_COLOR_ATTACHMENT0, this.colourSSAO); - this.framebufferSSAO.bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthStencil); - this.framebufferSSAO.verify(); + this.framebufferSSAO.bind(GL_COLOR_ATTACHMENT0, this.colourSSAO) + .bind(GL_DEPTH_STENCIL_ATTACHMENT, this.depthStencil) + .verify(); } } @@ -154,13 +148,9 @@ public class PostProcessing { mat.get(data); glUniformMatrix4fv(4, false, data);//invMVP - glActiveTexture(GL_TEXTURE0); glBindImageTexture(0, this.colourSSAO.id, 0, false,0, GL_READ_WRITE, GL_RGBA8); - glActiveTexture(GL_TEXTURE1); - GL11C.glBindTexture(GL_TEXTURE_2D, this.depthStencil.id); - glTexParameteri (GL_TEXTURE_2D, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_DEPTH_COMPONENT); - glActiveTexture(GL_TEXTURE2); - GL11C.glBindTexture(GL_TEXTURE_2D, this.colour.id); + glBindTextureUnit(1, this.depthStencil.id); + glBindTextureUnit(2, this.colour.id); glDispatchCompute((this.width+31)/32, (this.height+31)/32, 1); diff --git a/src/main/java/me/cortex/voxy/common/world/other/Mapper.java b/src/main/java/me/cortex/voxy/common/world/other/Mapper.java index 160ae8e4..94f613aa 100644 --- a/src/main/java/me/cortex/voxy/common/world/other/Mapper.java +++ b/src/main/java/me/cortex/voxy/common/world/other/Mapper.java @@ -71,7 +71,7 @@ public class Mapper { } public static long withLight(long id, int light) { - return (id&(~(0xFFL<<56)))|(Integer.toUnsignedLong(light)<<56); + return (id&(~(0xFFL<<56)))|(Integer.toUnsignedLong(light&0xFF)<<56); } public void setStateCallback(Consumer stateCallback) { @@ -181,7 +181,7 @@ public class Mapper { //TODO:FIXME: IS VERY SLOW NEED TO MAKE IT LOCK FREE, or at minimum use a concurrent map public long getBaseId(byte light, BlockState state, RegistryEntry biome) { - if (state.isAir()) return ((long)light)<<56;//Special case and fast return for air, dont care about the biome + if (state.isAir()) return Byte.toUnsignedLong(light) <<56;//Special case and fast return for air, dont care about the biome return composeMappingId(light, this.getIdForBlockState(state), this.getIdForBiome(biome)); } diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl index 5640b33b..4a0b0db3 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl +++ b/src/main/resources/assets/voxy/shaders/lod/gl46/bindings.glsl @@ -118,7 +118,7 @@ layout(binding = LIGHTING_SAMPLER_BINDING) uniform sampler2D lightSampler; vec4 getLighting(uint index) { int i2 = int(index); - return texture(lightSampler, vec2((i2>>4)&0xF, i2&0xF)/16.0f); + return texelFetch(lightSampler, ivec2((i2>>4)&0xF, i2&0xF), 0); } #endif