what the fuck opengl

This commit is contained in:
mcrcortex
2025-04-07 14:51:31 +10:00
parent 2ff6f4cb4e
commit 7027c34941
4 changed files with 75 additions and 29 deletions

View File

@@ -1,6 +1,9 @@
package me.cortex.voxy.client.core.model; package me.cortex.voxy.client.core.model;
import com.mojang.blaze3d.buffers.GpuBuffer; import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.opengl.GlConst;
import com.mojang.blaze3d.opengl.GlStateManager;
import com.mojang.blaze3d.pipeline.RenderPipeline; import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.platform.DepthTestFunction;
import com.mojang.blaze3d.systems.RenderPass; import com.mojang.blaze3d.systems.RenderPass;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.textures.GpuTexture; import com.mojang.blaze3d.textures.GpuTexture;
@@ -18,17 +21,28 @@ public class BudgetBufferRenderer {
.withFragmentShader(Identifier.of("voxy","bakery/position_tex")) .withFragmentShader(Identifier.of("voxy","bakery/position_tex"))
.withUniform("transform", UniformType.MATRIX4X4) .withUniform("transform", UniformType.MATRIX4X4)
.withSampler("tex") .withSampler("tex")
.withDepthTestFunction(DepthTestFunction.LEQUAL_DEPTH_TEST)
.withVertexFormat(VertexFormats.POSITION_TEXTURE_COLOR, VertexFormat.DrawMode.QUADS) .withVertexFormat(VertexFormats.POSITION_TEXTURE_COLOR, VertexFormat.DrawMode.QUADS)
.build(); .build();
public static void draw(BuiltBuffer buffer, GpuTexture tex, Matrix4f matrix) { public static void draw(BuiltBuffer buffer, GpuTexture tex, Matrix4f matrix) {
//Fuz the gpu sampler state
GlStateManager._activeTexture(GlConst.GL_TEXTURE0);
GlStateManager._bindTexture(0);
GlStateManager._activeTexture(GlConst.GL_TEXTURE1);
GlStateManager._bindTexture(0);
GlStateManager._activeTexture(GlConst.GL_TEXTURE2);
GlStateManager._bindTexture(0);
GlStateManager._activeTexture(GlConst.GL_TEXTURE2+1);
GlStateManager._bindTexture(0);
RenderSystem.ShapeIndexBuffer shapeIndexBuffer = RenderSystem.getSequentialBuffer(buffer.getDrawParameters().mode()); RenderSystem.ShapeIndexBuffer shapeIndexBuffer = RenderSystem.getSequentialBuffer(buffer.getDrawParameters().mode());
GpuBuffer gpuBuffer = buffer.getDrawParameters().format().uploadImmediateVertexBuffer(buffer.getBuffer()); GpuBuffer gpuBuffer = buffer.getDrawParameters().format().uploadImmediateVertexBuffer(buffer.getBuffer());
var res = (GlResourceManager)RenderSystem.getDevice() var res = (GlResourceManager)RenderSystem.getDevice()
.createCommandEncoder(); .createCommandEncoder();
res.currentProgram = null; res.currentProgram = null;
res.currentPipeline = RENDERER_THING; res.currentPipeline = null;
try (RenderPass renderPass = new RenderPassImpl(res, false)) { try (RenderPass renderPass = new RenderPassImpl(res, false)) {
renderPass.setPipeline(RENDERER_THING); renderPass.setPipeline(RENDERER_THING);
renderPass.setVertexBuffer(0, gpuBuffer); renderPass.setVertexBuffer(0, gpuBuffer);

View File

@@ -132,6 +132,7 @@ public class ModelFactory {
Arrays.fill(this.fluidStateLUT, -1); Arrays.fill(this.fluidStateLUT, -1);
this.modelTexture2id.defaultReturnValue(-1); this.modelTexture2id.defaultReturnValue(-1);
this.addEntry(0);//Add air as the first entry
} }

View File

@@ -34,6 +34,7 @@ import static org.lwjgl.opengl.ARBShaderImageLoadStore.glMemoryBarrier;
import static org.lwjgl.opengl.GL14C.glBlendFuncSeparate; import static org.lwjgl.opengl.GL14C.glBlendFuncSeparate;
import static org.lwjgl.opengl.GL20C.glUniformMatrix4fv; import static org.lwjgl.opengl.GL20C.glUniformMatrix4fv;
import static org.lwjgl.opengl.GL30.*; import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL45.glBlitNamedFramebuffer;
//Builds a texture for each face of a model //Builds a texture for each face of a model
public class ModelTextureBakery { public class ModelTextureBakery {
@@ -106,10 +107,11 @@ public class ModelTextureBakery {
//TODO: figure out why calling this makes minecraft render black //TODO: figure out why calling this makes minecraft render black
//renderLayer.startDrawing(); //renderLayer.startDrawing();
glBindFramebuffer(GL_FRAMEBUFFER, this.capture.framebuffer.id);
glClearColor(0,0,0,0); glClearColor(0,0,0,0);
glClearDepth(1); glClearDepth(1);
glClearStencil(0); glClearStencil(0);
glBindFramebuffer(GL_FRAMEBUFFER, this.capture.framebuffer.id);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST); glEnable(GL_STENCIL_TEST);
@@ -137,6 +139,9 @@ public class ModelTextureBakery {
glStencilFunc(GL_ALWAYS, 1, 0xFF); glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilMask(0xFF); glStencilMask(0xFF);
int[] viewdat = new int[4];
glGetIntegerv(GL_VIEWPORT, viewdat);
var tex = MinecraftClient.getInstance().getTextureManager().getTexture(Identifier.of("minecraft", "textures/atlas/blocks.png")).getGlTexture(); var tex = MinecraftClient.getInstance().getTextureManager().getTexture(Identifier.of("minecraft", "textures/atlas/blocks.png")).getGlTexture();
for (int i = 0; i < FACE_VIEWS.size(); i++) { for (int i = 0; i < FACE_VIEWS.size(); i++) {
glViewport((i%3)*this.width, (i/3)*this.height, this.width, this.height); glViewport((i%3)*this.width, (i/3)*this.height, this.width, this.height);
@@ -148,6 +153,9 @@ public class ModelTextureBakery {
this.rasterView(state, model, transform, randomValue, i, renderFluid, tex); this.rasterView(state, model, transform, randomValue, i, renderFluid, tex);
} }
glViewport(viewdat[0], viewdat[1], viewdat[2], viewdat[3]);
//renderLayer.endDrawing(); //renderLayer.endDrawing();
@@ -156,6 +164,21 @@ public class ModelTextureBakery {
glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT); glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT);
this.capture.emitToStream(streamBuffer, streamBaseOffset); 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*4,16*2*4, GL_COLOR_BUFFER_BIT, GL_NEAREST);
//SOMEBODY PLEASE FUCKING EXPLAIN TO ME WHY MUST CLEAR THE FRAMEBUFFER HERE WHEN IT IS LITERALLY CLEARED AT THE START OF THE FRAME
// WITHOUT THIS, WATER DOESNT RENDER
//TODO: FIXME, WHAT THE ACTUAL FUCK
glBindFramebuffer(GL_FRAMEBUFFER, this.capture.framebuffer.id);
glClearDepth(1);
glClear(GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
private final BufferAllocator allocator = new BufferAllocator(786432); private final BufferAllocator allocator = new BufferAllocator(786432);

View File

@@ -5,11 +5,14 @@ import com.mojang.blaze3d.systems.RenderSystem;
import me.cortex.voxy.client.config.VoxyConfig; import me.cortex.voxy.client.config.VoxyConfig;
import me.cortex.voxy.client.core.gl.Capabilities; import me.cortex.voxy.client.core.gl.Capabilities;
import me.cortex.voxy.client.core.gl.GlBuffer; import me.cortex.voxy.client.core.gl.GlBuffer;
import me.cortex.voxy.client.core.model.ColourDepthTextureData;
import me.cortex.voxy.client.core.model.ModelBakerySubsystem; import me.cortex.voxy.client.core.model.ModelBakerySubsystem;
import me.cortex.voxy.client.core.model.bakery.ModelTextureBakery;
import me.cortex.voxy.client.core.rendering.building.RenderDataFactory45; 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.building.RenderGenerationService;
import me.cortex.voxy.client.core.rendering.post.PostProcessing; 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.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.IrisUtil;
import me.cortex.voxy.common.Logger; import me.cortex.voxy.common.Logger;
import me.cortex.voxy.common.thread.ServiceThreadPool; import me.cortex.voxy.common.thread.ServiceThreadPool;
@@ -18,6 +21,7 @@ import me.cortex.voxy.common.world.WorldSection;
import me.cortex.voxy.common.world.other.Mapper; import me.cortex.voxy.common.world.other.Mapper;
import me.cortex.voxy.commonImpl.VoxyCommon; 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.DefaultTerrainRenderPasses;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.GlBackend; import net.minecraft.client.gl.GlBackend;
import net.minecraft.client.render.Camera; import net.minecraft.client.render.Camera;
@@ -25,6 +29,7 @@ import net.minecraft.client.render.Frustum;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -63,7 +68,37 @@ public class VoxyRenderSystem {
this.renderDistanceTracker.setRenderDistance(renderDistance); this.renderDistanceTracker.setRenderDistance(renderDistance);
} }
//private static final ModelTextureBakery mtb = new ModelTextureBakery(16, 16);
//private static final RawDownloadStream downstream = new RawDownloadStream(1<<20);
public void renderSetup(Frustum frustum, Camera camera) { public void renderSetup(Frustum frustum, Camera camera) {
/*
if (false) {
int allocation = downstream.download(2 * 4 * 6 * 16 * 16, ptr -> {
ColourDepthTextureData[] textureData = new ColourDepthTextureData[6];
final int FACE_SIZE = 16 * 16;
for (int face = 0; face < 6; face++) {
long faceDataPtr = ptr + (FACE_SIZE * 4) * face * 2;
int[] colour = new int[FACE_SIZE];
int[] depth = new int[FACE_SIZE];
//Copy out colour
for (int i = 0; i < FACE_SIZE; i++) {
//De-interpolate results
colour[i] = MemoryUtil.memGetInt(faceDataPtr + (i * 4 * 2));
depth[i] = MemoryUtil.memGetInt(faceDataPtr + (i * 4 * 2) + 4);
}
textureData[face] = new ColourDepthTextureData(colour, depth, 16, 16);
}
if (textureData[0].colour()[0] == 0) {
int a = 0;
}
});
mtb.renderFacesToStream(Blocks.AIR.getDefaultState(), 123456, false, downstream.getBufferId(), allocation);
downstream.submit();
downstream.tick();
}*/
this.renderDistanceTracker.setCenterAndProcess(camera.getBlockPos().getX(), camera.getBlockPos().getZ()); this.renderDistanceTracker.setCenterAndProcess(camera.getBlockPos().getX(), camera.getBlockPos().getZ());
this.renderer.setup(camera); this.renderer.setup(camera);
@@ -92,34 +127,7 @@ public class VoxyRenderSystem {
).mulLocal(makeProjectionMatrix(16, 16*3000)); ).mulLocal(makeProjectionMatrix(16, 16*3000));
} }
//private static final ModelTextureBakery2 mtb = new ModelTextureBakery2(16, 16);
//private static final RawDownloadStream downstream = new RawDownloadStream(1<<20);
public void renderOpaque(MatrixStack matrices, double cameraX, double cameraY, double cameraZ) { public void renderOpaque(MatrixStack matrices, double cameraX, double cameraY, double cameraZ) {
/*
int allocation = downstream.download(2*4*6*16*16, ptr->{
ColourDepthTextureData[] textureData = new ColourDepthTextureData[6];
final int FACE_SIZE = 16*16;
for (int face = 0; face < 6; face++) {
long faceDataPtr = ptr + (FACE_SIZE*4)*face*2;
int[] colour = new int[FACE_SIZE];
int[] depth = new int[FACE_SIZE];
//Copy out colour
for (int i = 0; i < FACE_SIZE; i++) {
//De-interpolate results
colour[i] = MemoryUtil.memGetInt(faceDataPtr+ (i*4*2));
depth[i] = MemoryUtil.memGetInt(faceDataPtr+ (i*4*2)+4);
}
textureData[face] = new ColourDepthTextureData(colour, depth, 16, 16);
}
int a = 0;
});
mtb.renderFacesToStream(Blocks.GRASS_BLOCK.getDefaultState(), 123456, false, downstream.getBufferId(), allocation);
downstream.submit();
downstream.tick();
*/
//if (true) return; //if (true) return;