Skip to content

Commit

Permalink
Fix blocks not rendering below Y 0 (i.e. In Negative bounds )
Browse files Browse the repository at this point in the history
(Hopefully won't cause performance regressions due to the Int to Float Casts + Mixed Integer + Float Workloads)
  • Loading branch information
thr3343 committed Nov 20, 2023
1 parent ed50187 commit 62c2e6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 15 additions & 12 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ private AreaBuffer getAreaBuffer(TerrainRenderType r) {
}

private static int encodeSectionOffset(int xOffset, int yOffset, int zOffset) {
final int xOffset1 = (xOffset & 127)>>4;
final int zOffset1 = (zOffset & 127)>>4;
final int yOffset1 = yOffset >> 4;
return zOffset1 << 16 | yOffset1 << 8 | xOffset1;
final int xOffset1 = (xOffset & 127);
final int zOffset1 = (zOffset & 127);
return yOffset << 18 | zOffset1 << 9 | xOffset1;
}

private void updateChunkAreaOrigin(double camX, double camY, double camZ, VkCommandBuffer commandBuffer, long ptr) {
Expand Down Expand Up @@ -231,18 +230,22 @@ public void buildDrawBatchesDirect(TerrainRenderType terrainRenderType, double c

VkCommandBuffer commandBuffer = Renderer.getCommandBuffer();
try(MemoryStack stack = MemoryStack.stackPush()) {
nvkCmdBindVertexBuffers(commandBuffer, 0, 1, stack.npointer(getAreaBuffer(terrainRenderType).getId()), stack.npointer(0));
updateChunkAreaOrigin(camX, camY, camZ, commandBuffer, stack.nmalloc(16));
}

if(isTranslucent) {
vkCmdBindIndexBuffer(commandBuffer, this.indexBuffer.getId(), 0, VK_INDEX_TYPE_UINT16);
}

for (var iterator = this.sectionQueues.get(terrainRenderType).iterator(isTranslucent); iterator.hasNext(); ) {
final DrawParameters drawParameters = iterator.next();
vkCmdDrawIndexed(commandBuffer, drawParameters.indexCount, drawParameters.indexCount != 0 ? 1 : 0, drawParameters.firstIndex, drawParameters.vertexOffset, drawParameters.baseInstance);
if (isTranslucent) {
vkCmdBindIndexBuffer(commandBuffer, this.indexBuffer.getId(), 0, VK_INDEX_TYPE_UINT16);
}

long npointer = stack.npointer(getAreaBuffer(terrainRenderType).getId());
long npointer1 = stack.npointer(0);
for (var iterator = this.sectionQueues.get(terrainRenderType).iterator(isTranslucent); iterator.hasNext(); ) {
final DrawParameters drawParameters = iterator.next();
VUtil.UNSAFE.putLong(npointer1, drawParameters.vertexOffset*20L);
nvkCmdBindVertexBuffers(commandBuffer, 0, 1, npointer, npointer1);
vkCmdDrawIndexed(commandBuffer, drawParameters.indexCount, drawParameters.indexCount != 0 ? 1 : 0, drawParameters.firstIndex, 0, drawParameters.baseInstance);

}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ const vec4 UNPACK_FACTOR = vec4(127.*16.);
const vec3 POSITION_INV = vec3(1.0 / 1900.0);

void main() {
const vec4 baseOffset = vec4(fma(Position,POSITION_INV,ChunkOffset), 1);
const vec4 xyz = fma(unpackSnorm4x8(gl_InstanceIndex),UNPACK_FACTOR,baseOffset);
gl_Position = MVP * xyz;
const vec3 baseOffset = bitfieldExtract(ivec3(gl_InstanceIndex)>> ivec3(0, 18, 9), 0, 9);
const vec3 pos = baseOffset+fma(Position, vec3(POSITION_INV), ChunkOffset);
const vec4 a = vec4(pos, 1);
gl_Position = MVP * a;

vertexDistance = length((ModelViewMat * xyz).xyz);
vertexDistance = length((ModelViewMat * a).xyz);
vertexColor = Color * sample_lightmap(Sampler2, UV2);
texCoord0 = UV0 * UV_INV;
// normal = MVP * vec4(Normal, 0.0);
Expand Down

0 comments on commit 62c2e6a

Please sign in to comment.