From 62c2e6a1394e4ffb871d044f17c8ad3ee88fd9ed Mon Sep 17 00:00:00 2001 From: thr3343 <125277899+thr3343@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:54:40 +0000 Subject: [PATCH] Fix blocks not rendering below Y 0 (i.e. In Negative bounds ) (Hopefully won't cause performance regressions due to the Int to Float Casts + Mixed Integer + Float Workloads) --- .../vulkanmod/render/chunk/DrawBuffers.java | 27 ++++++++++--------- .../shaders/basic/terrain/terrain.vsh | 9 ++++--- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java b/src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java index 79351f7afa..2f61fd3e82 100644 --- a/src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java +++ b/src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java @@ -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) { @@ -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); + } } diff --git a/src/main/resources/assets/vulkanmod/shaders/basic/terrain/terrain.vsh b/src/main/resources/assets/vulkanmod/shaders/basic/terrain/terrain.vsh index d3e6de24ae..a205f97ae5 100644 --- a/src/main/resources/assets/vulkanmod/shaders/basic/terrain/terrain.vsh +++ b/src/main/resources/assets/vulkanmod/shaders/basic/terrain/terrain.vsh @@ -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);