Skip to content

Commit

Permalink
Optimise Chunk Shader
Browse files Browse the repository at this point in the history
(May either have no performance improvement and/or have possible Shader/Visual artefacts)
  • Loading branch information
thr3343 committed Nov 14, 2023
1 parent 7c3755e commit 556301a
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 28 deletions.
7 changes: 4 additions & 3 deletions src/main/java/net/vulkanmod/render/chunk/DrawBuffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ public DrawParameters upload(int xOffset, int yOffset, int zOffset, UploadBuffer
}

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

public void buildDrawBatchesIndirect(IndirectBuffer indirectBuffer, TerrainRenderType terrainRenderType, double camX, double camY, double camZ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CustomVertexFormat {

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

public static final VertexFormat COMPRESSED_TERRAIN = new VertexFormat(new ImmutableMap.Builder<String, VertexFormatElement>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ else if(type == VertexFormatElement.Type.USHORT){

offset += 4;
}
else if(type == VertexFormatElement.Type.UINT){
posDescription.format(VK_FORMAT_R32_UINT);
posDescription.offset(offset);

offset += 4;
}
break;

case NORMAL:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
; SPIR-V
; Version: 1.0
; Generator: Google Shaderc over Glslang; 11
; Bound: 72
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Vertex %4 "main" %gl_InstanceIndex %32 %43 %62 %64 %71
OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
OpMemberDecorate %_struct_21 0 Offset 0
OpDecorate %_struct_21 Block
OpDecorate %32 Location 0
OpMemberDecorate %_struct_41 0 BuiltIn Position
OpMemberDecorate %_struct_41 1 BuiltIn PointSize
OpMemberDecorate %_struct_41 2 BuiltIn ClipDistance
OpMemberDecorate %_struct_41 3 BuiltIn CullDistance
OpDecorate %_struct_41 Block
OpMemberDecorate %_struct_45 0 ColMajor
OpMemberDecorate %_struct_45 0 Offset 0
OpMemberDecorate %_struct_45 0 MatrixStride 16
OpDecorate %_struct_45 Block
OpDecorate %47 DescriptorSet 0
OpDecorate %47 Binding 0
OpDecorate %62 Location 1
OpDecorate %64 Location 2
OpDecorate %71 Location 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
%int = OpTypeInt 32 1
%_ptr_Input_int = OpTypePointer Input %int
%gl_InstanceIndex = OpVariable %_ptr_Input_int Input
%uint = OpTypeInt 32 0
%v4float = OpTypeVector %float 4
%float_2032 = OpConstant %float 2032
%20 = OpConstantComposite %v3float %float_2032 %float_2032 %float_2032
%_struct_21 = OpTypeStruct %v3float
%_ptr_PushConstant__struct_21 = OpTypePointer PushConstant %_struct_21
%23 = OpVariable %_ptr_PushConstant__struct_21 PushConstant
%int_0 = OpConstant %int 0
%_ptr_PushConstant_v3float = OpTypePointer PushConstant %v3float
%v3int = OpTypeVector %int 3
%_ptr_Input_v3int = OpTypePointer Input %v3int
%32 = OpVariable %_ptr_Input_v3int Input
%float_0_000526315765 = OpConstant %float 0.000526315765
%36 = OpConstantComposite %v3float %float_0_000526315765 %float_0_000526315765 %float_0_000526315765
%uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%_struct_41 = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output__struct_41 = OpTypePointer Output %_struct_41
%43 = OpVariable %_ptr_Output__struct_41 Output
%mat4v4float = OpTypeMatrix %v4float 4
%_struct_45 = OpTypeStruct %mat4v4float
%_ptr_Uniform__struct_45 = OpTypePointer Uniform %_struct_45
%47 = OpVariable %_ptr_Uniform__struct_45 Uniform
%_ptr_Uniform_mat4v4float = OpTypePointer Uniform %mat4v4float
%float_1 = OpConstant %float 1
%_ptr_Output_v4float = OpTypePointer Output %v4float
%v2float = OpTypeVector %float 2
%_ptr_Output_v2float = OpTypePointer Output %v2float
%62 = OpVariable %_ptr_Output_v2float Output
%_ptr_Input_uint = OpTypePointer Input %uint
%64 = OpVariable %_ptr_Input_uint Input
%71 = OpVariable %_ptr_Output_v4float Output
%4 = OpFunction %void None %3
%5 = OpLabel
%13 = OpLoad %int %gl_InstanceIndex
%15 = OpBitcast %uint %13
%17 = OpExtInst %v4float %1 UnpackSnorm4x8 %15
%18 = OpVectorShuffle %v3float %17 %17 0 1 2
%26 = OpAccessChain %_ptr_PushConstant_v3float %23 %int_0
%27 = OpLoad %v3float %26
%28 = OpExtInst %v3float %1 Fma %18 %20 %27
%33 = OpLoad %v3int %32
%34 = OpConvertSToF %v3float %33
%38 = OpExtInst %v3float %1 Fma %34 %36 %28
%49 = OpAccessChain %_ptr_Uniform_mat4v4float %47 %int_0
%50 = OpLoad %mat4v4float %49
%53 = OpCompositeExtract %float %38 0
%54 = OpCompositeExtract %float %38 1
%55 = OpCompositeExtract %float %38 2
%56 = OpCompositeConstruct %v4float %53 %54 %55 %float_1
%57 = OpMatrixTimesVector %v4float %50 %56
%59 = OpAccessChain %_ptr_Output_v4float %43 %int_0
OpStore %59 %57
%65 = OpLoad %uint %64
%66 = OpExtInst %v2float %1 UnpackUnorm2x16 %65
OpStore %62 %66
OpReturn
OpFunctionEnd
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ layout(location = 2) out vec2 texCoord0;
//Compressed Vertex
layout(location = 0) in ivec3 Position;
layout(location = 1) in vec4 Color;
layout(location = 2) in uvec2 UV0;
layout(location = 2) in uint UV0;
layout(location = 3) in ivec2 UV2;
//layout(location = 4) in vec3 Normal;

const float UV_INV = 1.0 / 65536.0;
const float POSITION_INV = 1.0 / 1900.0;

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

vertexDistance = length((ModelViewMat * vec4(pos + a, 1.0)).xyz);
vertexDistance = length((ModelViewMat * vec4(pos, 1.0)).xyz);
vertexColor = Color * sample_lightmap(Sampler2, UV2);
texCoord0 = UV0 * UV_INV;
texCoord0 = unpackUnorm2x16(UV0);
// normal = MVP * vec4(Normal, 0.0);
}

Expand All @@ -53,4 +52,4 @@ void main() {
// vertexColor = Color * minecraft_sample_lightmap(Sampler2, UV2);
// texCoord0 = UV0;
// // normal = MVP * vec4(Normal, 0.0);
//}
//}
24 changes: 9 additions & 15 deletions src/main/resources/assets/vulkanmod/shaders/include/light.glsl
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
//light.glsl
#define MINECRAFT_LIGHT_POWER (0.6)
#define MINECRAFT_AMBIENT_LIGHT (0.4)
//#pragma once
const float MINECRAFT_LIGHT_POWER = (0.6);
const float MINECRAFT_AMBIENT_LIGHT = (0.4);

vec4 minecraft_mix_light(vec3 lightDir0, vec3 lightDir1, vec3 normal, vec4 color) {
lightDir0 = normalize(lightDir0);
lightDir1 = normalize(lightDir1);
float light0 = max(0.0, dot(lightDir0, normal));
float light1 = max(0.0, dot(lightDir1, normal));
float lightAccum = min(1.0, (light0 + light1) * MINECRAFT_LIGHT_POWER + MINECRAFT_AMBIENT_LIGHT);
return vec4(color.rgb * lightAccum, color.a);
float light0 = max(0.0, dot(normalize(lightDir0), normal));
float light1 = max(0.0, dot(normalize(lightDir1), normal));
color.rgb *= fma(light0 + light1, MINECRAFT_LIGHT_POWER, MINECRAFT_AMBIENT_LIGHT); //lightAccum
return color;
}

vec4 minecraft_sample_lightmap(sampler2D lightMap, ivec2 uv) {
return texture(lightMap, clamp(uv / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)));
return texelFetch(lightMap, bitfieldExtract(uv, 4, 8), 0); //return texture(lightMap, clamp(uv / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)));
}

vec4 sample_lightmap(sampler2D lightMap, ivec2 uv) {
return texelFetch(lightMap, bitfieldExtract(uv, 4, 8), 0);
}

vec4 linear_fog(vec4 inColor, float vertexDistance, float fogStart, float fogEnd, vec4 fogColor) {
if (vertexDistance <= fogStart) {
return inColor;
}

float fogValue = vertexDistance < fogEnd ? smoothstep(fogStart, fogEnd, vertexDistance) : 1.0;
return vec4(mix(inColor.rgb, fogColor.rgb, fogValue * fogColor.a), inColor.a);
return (vertexDistance <= fogStart) ? inColor : mix(inColor, fogColor, min(smoothstep(fogStart, fogEnd, vertexDistance), 1.0) * fogColor.a);
}

0 comments on commit 556301a

Please sign in to comment.