Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance + VRAM] Re-implement 16-byte Vertex Format + use 4 Byte alignment #487

Draft
wants to merge 4 commits into
base: 1.21
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexFormatElement;
import net.vulkanmod.vulkan.Vulkan;

public class CustomVertexFormat {

public static final VertexFormatElement ELEMENT_POSITION = new VertexFormatElement(0, 0,VertexFormatElement.Type.SHORT, VertexFormatElement.Usage.POSITION, 4);
public static final VertexFormatElement ELEMENT_COLOR = new VertexFormatElement(1, 0, VertexFormatElement.Type.UBYTE, VertexFormatElement.Usage.COLOR, 4);
public static final VertexFormatElement ELEMENT_UV0 = new VertexFormatElement(2, 0, VertexFormatElement.Type.USHORT, VertexFormatElement.Usage.UV, 2);
public static final VertexFormatElement ELEMENT_UV2 = new VertexFormatElement(3, 2, VertexFormatElement.Type.SHORT, VertexFormatElement.Usage.UV, 2);
public static final VertexFormatElement ELEMENT_UV0 = Vulkan.getDevice().isAMD() ? new VertexFormatElement(2, 0, VertexFormatElement.Type.UINT, VertexFormatElement.Usage.UV, 1) : new VertexFormatElement(2, 0, VertexFormatElement.Type.USHORT, VertexFormatElement.Usage.UV, 2);

public static final VertexFormat COMPRESSED_TERRAIN = VertexFormat.builder()
.add("Position", ELEMENT_POSITION)
.add("Color", ELEMENT_COLOR)
.add("UV0", ELEMENT_UV0)
.add("UV2", ELEMENT_UV2)
.build();

public static final VertexFormat NONE = VertexFormat.builder().build();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/vulkanmod/render/vertex/VertexBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public int getStride() {
}

class CompressedVertexBuilder implements VertexBuilder {
private static final int VERTEX_SIZE = 20;
private static final int VERTEX_SIZE = 16;

public static final float POS_CONV_MUL = 2048.0f;
public static final float POS_OFFSET = -4.0f;
public static final float POS_OFFSET_CONV = POS_OFFSET * POS_CONV_MUL;

public static final float UV_CONV_MUL = 32768.0f;
public static final float UV_CONV_MUL = 65536.f;

public void vertex(long ptr, float x, float y, float z, int color, float u, float v, int light, int packedNormal) {
final short sX = (short) (x * POS_CONV_MUL + POS_OFFSET_CONV);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ private static VkVertexInputAttributeDescription.Buffer getAttributeDescriptions

offset += 8;
}
case UINT -> {
posDescription.format(VK_FORMAT_R32_UINT);
posDescription.offset(offset);

offset += 4;
}
case SHORT -> {
posDescription.format(VK_FORMAT_R16G16_SINT);
posDescription.offset(offset);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/vulkanmod/vulkan/shader/SPIRVUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.vulkanmod.vulkan.shader;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.vulkanmod.vulkan.Vulkan;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.NativeResource;
Expand All @@ -22,8 +23,10 @@
import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.system.MemoryUtil.memASCII;
import static org.lwjgl.util.shaderc.Shaderc.*;
import static org.lwjgl.util.shaderc.Shaderc.shaderc_compile_options_set_optimization_level;

public class SPIRVUtils {
private static final boolean use4ByteAlignFormat = Vulkan.getDevice().isAMD();
private static final boolean DEBUG = false;
private static final boolean OPTIMIZATIONS = true;

Expand Down Expand Up @@ -55,6 +58,9 @@ private static void initCompiler() {
if(options == NULL) {
throw new RuntimeException("Failed to create compiler options");
}
//Use the optimal most performant vertex format based on architecture: 4 byte alignment if on AMD GCN, otherwise defaults to 2 Bytes (including Nvidia)
if(use4ByteAlignFormat)
shaderc_compile_options_add_macro_definition(options, "GCN_FIX", null);

if(OPTIMIZATIONS)
shaderc_compile_options_set_optimization_level(options, shaderc_optimization_level_performance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ layout (location = 1) out vec4 vertexColor;
layout (location = 2) out vec2 texCoord0;

//Compressed Vertex
layout (location = 0) in ivec4 Position;
layout (location = 1) in vec4 Color;
layout (location = 2) in uvec2 UV0;
#ifndef GCN_FIX
layout (location = 0) in ivec4 Position;
layout (location = 1) in vec4 Color;
layout (location = 2) in uvec2 UV0;
#else
layout (location = 0) in ivec4 Position;
layout (location = 1) in vec4 Color;
layout (location = 2) in uint UV0;
#endif


const float UV_INV = 1.0 / 32768.0;
//const vec3 POSITION_INV = vec3(1.0 / 1024.0);
Expand All @@ -35,7 +42,12 @@ void main() {

vertexDistance = fog_distance(pos.xyz, 0);
vertexColor = Color * sample_lightmap2(Sampler2, Position.a);
texCoord0 = UV0 * UV_INV;

#ifndef GCN_FIX
texCoord0 = UV0 * UV_INV;
#else
texCoord0 = unpackUnorm2x16(UV0);
#endif
}

////Default Vertex
Expand Down