Skip to content

Commit

Permalink
Initial commit for quad pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
jellysquid3 committed Dec 10, 2023
1 parent d8a1fda commit 53dc0e8
Show file tree
Hide file tree
Showing 28 changed files with 337 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public GlShader(ShaderType type, Identifier name, String src) {
String log = GL20C.glGetShaderInfoLog(handle);

if (!log.isEmpty()) {
LOGGER.warn("Shader compilation log for " + this.name + ": " + log);
LOGGER.warn("""
Shader compilation messages for {}:
=== Shader Source ===
{}
=== Compilation Messages ===
{}""", this.name, src, log);
}

int result = GlStateManager.glGetShaderi(handle, GL20C.GL_COMPILE_STATUS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.jellysquid.mods.sodium.client.gl.util;

public record VertexRange(int vertexStart, int vertexCount) {
public record VertexRange(int offset, int count) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void calculate(ModelQuadView quad, BlockPos pos, QuadLightData out, Direc
this.applyNonParallelFace(neighborInfo, quad, pos, lightFace, out);
}

this.applySidedBrightness(out, lightFace, shade);
// this.applySidedBrightness(out, lightFace, shade);
}

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.jellysquid.mods.sodium.client.render.chunk;

import me.jellysquid.mods.sodium.client.SodiumClientMod;
import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexAttributeBinding;
import me.jellysquid.mods.sodium.client.gl.device.CommandList;
import me.jellysquid.mods.sodium.client.gl.device.DrawCommandList;
import me.jellysquid.mods.sodium.client.gl.device.MultiDrawBatch;
Expand All @@ -16,11 +15,9 @@
import me.jellysquid.mods.sodium.client.render.chunk.lists.ChunkRenderListIterable;
import me.jellysquid.mods.sodium.client.render.chunk.lists.ChunkRenderList;
import me.jellysquid.mods.sodium.client.render.chunk.region.RenderRegion;
import me.jellysquid.mods.sodium.client.render.chunk.shader.ChunkShaderBindingPoints;
import me.jellysquid.mods.sodium.client.render.chunk.shader.ChunkShaderInterface;
import me.jellysquid.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import me.jellysquid.mods.sodium.client.render.viewport.CameraTransform;
import me.jellysquid.mods.sodium.client.util.BitwiseMath;
import org.lwjgl.system.MemoryUtil;
Expand All @@ -32,11 +29,19 @@ public class DefaultChunkRenderer extends ShaderChunkRenderer {

private final SharedQuadIndexBuffer sharedIndexBuffer;

public DefaultChunkRenderer(RenderDevice device, ChunkVertexType vertexType) {
private final GlTessellation tessellation;

public DefaultChunkRenderer(RenderDevice device, ModelQuadFormat vertexType) {
super(device, vertexType);

this.batch = new MultiDrawBatch((ModelQuadFacing.COUNT * RenderRegion.REGION_SIZE) + 1);
this.sharedIndexBuffer = new SharedQuadIndexBuffer(device.createCommandList(), SharedQuadIndexBuffer.IndexType.INTEGER);

try (var commandList = device.createCommandList()) {
this.tessellation = commandList.createTessellation(GlPrimitiveType.TRIANGLES, new TessellationBinding[] {
TessellationBinding.forElementBuffer(this.sharedIndexBuffer.getBufferObject())
});
}
}

@Override
Expand Down Expand Up @@ -73,10 +78,10 @@ public void render(ChunkRenderMatrices matrices,

this.sharedIndexBuffer.ensureCapacity(commandList, this.batch.getIndexBufferSize());

var tessellation = this.prepareTessellation(commandList, region);
shader.setVertexBuffer(region.getResources().getVertexBuffer());

setModelMatrixUniforms(shader, region, camera);
executeDrawBatch(commandList, tessellation, this.batch);
executeDrawBatch(commandList, this.tessellation, this.batch);
}

super.end(renderPass);
Expand Down Expand Up @@ -206,27 +211,6 @@ private static float getCameraTranslation(int chunkBlockPos, int cameraBlockPos,
return (chunkBlockPos - cameraBlockPos) - cameraPos;
}

private GlTessellation prepareTessellation(CommandList commandList, RenderRegion region) {
var resources = region.getResources();
var tessellation = resources.getTessellation();

if (tessellation == null) {
resources.updateTessellation(commandList, tessellation = this.createRegionTessellation(commandList, resources));
}

return tessellation;
}

private GlTessellation createRegionTessellation(CommandList commandList, RenderRegion.DeviceResources resources) {
return commandList.createTessellation(GlPrimitiveType.TRIANGLES, new TessellationBinding[] {
TessellationBinding.forVertexBuffer(resources.getVertexBuffer(), new GlVertexAttributeBinding[] {
new GlVertexAttributeBinding(ChunkShaderBindingPoints.ATTRIBUTE_PACKED_DATA,
this.vertexFormat.getAttribute(ChunkMeshAttribute.VERTEX_DATA))
}),
TessellationBinding.forElementBuffer(this.sharedIndexBuffer.getBufferObject())
});
}

private static void executeDrawBatch(CommandList commandList, GlTessellation tessellation, MultiDrawBatch batch) {
try (DrawCommandList drawCommandList = commandList.beginTessellating(tessellation)) {
drawCommandList.multiDrawElementsBaseVertex(batch, GlIndexType.UNSIGNED_INT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public class RenderSectionManager {
private @Nullable BlockPos lastCameraPosition;

public RenderSectionManager(ClientWorld world, int renderDistance, CommandList commandList) {
this.chunkRenderer = new DefaultChunkRenderer(RenderDevice.INSTANCE, ChunkMeshFormats.COMPACT);
this.chunkRenderer = new DefaultChunkRenderer(RenderDevice.INSTANCE, ChunkMeshFormats.DEFAULT);

this.world = world;
this.builder = new ChunkBuilder(world, ChunkMeshFormats.COMPACT);
this.builder = new ChunkBuilder(world, ChunkMeshFormats.DEFAULT);

this.needsUpdate = true;
this.renderDistance = renderDistance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package me.jellysquid.mods.sodium.client.render.chunk;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import me.jellysquid.mods.sodium.client.gl.attribute.GlVertexFormat;
import me.jellysquid.mods.sodium.client.gl.device.CommandList;
import me.jellysquid.mods.sodium.client.gl.device.RenderDevice;
import me.jellysquid.mods.sodium.client.gl.shader.*;
import me.jellysquid.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import me.jellysquid.mods.sodium.client.render.chunk.shader.*;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkMeshAttribute;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import net.minecraft.util.Identifier;

import java.util.Map;

public abstract class ShaderChunkRenderer implements ChunkRenderer {
private final Map<ChunkShaderOptions, GlProgram<ChunkShaderInterface>> programs = new Object2ObjectOpenHashMap<>();

protected final ChunkVertexType vertexType;
protected final GlVertexFormat<ChunkMeshAttribute> vertexFormat;
protected final ModelQuadFormat vertexType;

protected final RenderDevice device;

protected GlProgram<ChunkShaderInterface> activeProgram;

public ShaderChunkRenderer(RenderDevice device, ChunkVertexType vertexType) {
public ShaderChunkRenderer(RenderDevice device, ModelQuadFormat vertexType) {
this.device = device;
this.vertexType = vertexType;
this.vertexFormat = vertexType.getVertexFormat();
}

protected GlProgram<ChunkShaderInterface> compileProgram(ChunkShaderOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import me.jellysquid.mods.sodium.client.render.chunk.terrain.TerrainRenderPass;
import me.jellysquid.mods.sodium.client.render.chunk.terrain.material.Material;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.builder.ChunkMeshBufferBuilder;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import me.jellysquid.mods.sodium.client.util.NativeBuffer;

import java.nio.ByteBuffer;
Expand All @@ -26,16 +26,16 @@
public class ChunkBuildBuffers {
private final Reference2ReferenceOpenHashMap<TerrainRenderPass, BakedChunkModelBuilder> builders = new Reference2ReferenceOpenHashMap<>();

private final ChunkVertexType vertexType;
private final ModelQuadFormat format;

public ChunkBuildBuffers(ChunkVertexType vertexType) {
this.vertexType = vertexType;
public ChunkBuildBuffers(ModelQuadFormat format) {
this.format = format;

for (TerrainRenderPass pass : DefaultTerrainRenderPasses.ALL) {
var vertexBuffers = new ChunkMeshBufferBuilder[ModelQuadFacing.COUNT];

for (int facing = 0; facing < ModelQuadFacing.COUNT; facing++) {
vertexBuffers[facing] = new ChunkMeshBufferBuilder(this.vertexType, 128 * 1024);
vertexBuffers[facing] = new ChunkMeshBufferBuilder(this.format, 128 * 1024);
}

this.builders.put(pass, new BakedChunkModelBuilder(vertexBuffers));
Expand All @@ -60,38 +60,38 @@ public ChunkModelBuilder get(Material material) {
public BuiltSectionMeshParts createMesh(TerrainRenderPass pass) {
var builder = this.builders.get(pass);

List<ByteBuffer> vertexBuffers = new ArrayList<>();
VertexRange[] vertexRanges = new VertexRange[ModelQuadFacing.COUNT];
List<ByteBuffer> meshBuffers = new ArrayList<>();
VertexRange[] meshRanges = new VertexRange[ModelQuadFacing.COUNT];

int vertexCount = 0;
int totalPrimitives = 0;

for (ModelQuadFacing facing : ModelQuadFacing.VALUES) {
var buffer = builder.getVertexBuffer(facing);
var buffer = builder.getMeshBuffer(facing);

if (buffer.isEmpty()) {
continue;
}

vertexBuffers.add(buffer.slice());
vertexRanges[facing.ordinal()] = new VertexRange(vertexCount, buffer.count());
meshBuffers.add(buffer.slice());
meshRanges[facing.ordinal()] = new VertexRange(totalPrimitives * 4, buffer.getPrimitiveCount() * 4);

vertexCount += buffer.count();
totalPrimitives += buffer.getPrimitiveCount();
}

if (vertexCount == 0) {
if (totalPrimitives == 0) {
return null;
}

var mergedBuffer = new NativeBuffer(vertexCount * this.vertexType.getVertexFormat().getStride());
var mergedBuffer = new NativeBuffer(totalPrimitives * this.format.getStride());
var mergedBufferBuilder = mergedBuffer.getDirectBuffer();

for (var buffer : vertexBuffers) {
for (var buffer : meshBuffers) {
mergedBufferBuilder.put(buffer);
}

mergedBufferBuilder.flip();

return new BuiltSectionMeshParts(mergedBuffer, vertexRanges);
return new BuiltSectionMeshParts(mergedBuffer, meshRanges);
}

public void destroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package me.jellysquid.mods.sodium.client.render.chunk.compile;

import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ChunkVertexType;
import me.jellysquid.mods.sodium.client.render.chunk.vertex.format.ModelQuadFormat;
import me.jellysquid.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderCache;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.world.World;

public class ChunkBuildContext {
public final ChunkBuildBuffers buffers;
public final BlockRenderCache cache;

public ChunkBuildContext(ClientWorld world, ChunkVertexType vertexType) {
public ChunkBuildContext(ClientWorld world, ModelQuadFormat vertexType) {
this.buffers = new ChunkBuildBuffers(vertexType);
this.cache = new BlockRenderCache(MinecraftClient.getInstance(), world);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import net.minecraft.client.texture.Sprite;

public class BakedChunkModelBuilder implements ChunkModelBuilder {
private final ChunkMeshBufferBuilder[] vertexBuffers;
private final ChunkMeshBufferBuilder[] meshBuffers;

private BuiltSectionInfo.Builder renderData;

public BakedChunkModelBuilder(ChunkMeshBufferBuilder[] vertexBuffers) {
this.vertexBuffers = vertexBuffers;
public BakedChunkModelBuilder(ChunkMeshBufferBuilder[] meshBuffers) {
this.meshBuffers = meshBuffers;
}

@Override
public ChunkMeshBufferBuilder getVertexBuffer(ModelQuadFacing facing) {
return this.vertexBuffers[facing.ordinal()];
public ChunkMeshBufferBuilder getMeshBuffer(ModelQuadFacing facing) {
return this.meshBuffers[facing.ordinal()];
}

@Override
Expand All @@ -25,15 +25,15 @@ public void addSprite(Sprite sprite) {
}

public void destroy() {
for (ChunkMeshBufferBuilder builder : this.vertexBuffers) {
for (ChunkMeshBufferBuilder builder : this.meshBuffers) {
builder.destroy();
}
}

public void begin(BuiltSectionInfo.Builder renderData, int sectionIndex) {
this.renderData = renderData;

for (var vertexBuffer : this.vertexBuffers) {
for (var vertexBuffer : this.meshBuffers) {
vertexBuffer.start(sectionIndex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.minecraft.client.texture.Sprite;

public interface ChunkModelBuilder {
ChunkMeshBufferBuilder getVertexBuffer(ModelQuadFacing facing);
ChunkMeshBufferBuilder getMeshBuffer(ModelQuadFacing facing);

void addSprite(Sprite sprite);
}
Loading

0 comments on commit 53dc0e8

Please sign in to comment.