From 2b8efbfb8f859bb7021baa866214b84ca9933acd Mon Sep 17 00:00:00 2001 From: mcrcortex <{ID}+{username}@users.noreply.github.com> Date: Sun, 14 Apr 2024 03:34:00 +1000 Subject: [PATCH] it work --- .../rendering/building/RenderDataFactory.java | 2 +- .../mixin/minecraft/MixinMinecraftClient.java | 2 +- .../voxy/shaders/lod/gl46mesh/cmdgen.comp | 2 +- .../voxy/shaders/lod/gl46mesh/quads.frag | 29 +++- .../voxy/shaders/lod/gl46mesh/quads.vert | 133 ++++++++++++++++-- 5 files changed, 156 insertions(+), 12 deletions(-) diff --git a/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java b/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java index 0cf3dc75..724e8d36 100644 --- a/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java +++ b/src/main/java/me/cortex/voxy/client/core/rendering/building/RenderDataFactory.java @@ -111,7 +111,7 @@ public class RenderDataFactory { long key = section.key; buff = new MemoryBuffer(bufferSize * 8L); long ptr = buff.address; - MemoryUtil.memSet(ptr, -1, bufferSize * 8L); + MemoryUtil.memSet(ptr, 0,bufferSize * 8L); int meshlet = 0; int innerQuadCount = 0; diff --git a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinMinecraftClient.java b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinMinecraftClient.java index 8c3f0159..7d611b1f 100644 --- a/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinMinecraftClient.java +++ b/src/main/java/me/cortex/voxy/client/mixin/minecraft/MixinMinecraftClient.java @@ -11,6 +11,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinMinecraftClient { @Inject(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resource/PeriodicNotificationManager;(Lnet/minecraft/util/Identifier;Lit/unimi/dsi/fastutil/objects/Object2BooleanFunction;)V", shift = At.Shift.AFTER)) private void injectRenderDoc(RunArgs args, CallbackInfo ci) { - //System.load("C:\\Program Files\\RenderDoc\\renderdoc.dll"); + System.load("C:\\Program Files\\RenderDoc\\renderdoc.dll"); } } diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/cmdgen.comp b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/cmdgen.comp index 05c1c4d3..a00d0047 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/cmdgen.comp +++ b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/cmdgen.comp @@ -48,7 +48,7 @@ void main() { uint mli = atomicAdd(drawCmd.instanceCount, total);//meshletListIndex - uint meshletPtr = extractMeshletStart(meta); + uint meshletPtr = extractMeshletStart(meta) + (meta.cntA&0xFFFF); emitMeshlets(mli, meshletPtr, a, a); emitMeshlets(mli, meshletPtr, u, (meta.cntB &0xFFFF)); diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.frag b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.frag index 36836acc..7732974e 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.frag +++ b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.frag @@ -1,5 +1,32 @@ #version 460 core +layout(binding = 0) uniform sampler2D blockModelAtlas; + +//TODO: need to fix when merged quads have discardAlpha set to false but they span multiple tiles +// however they are not a full block + +layout(location = 0) in vec2 uv; +layout(location = 1) in flat vec2 baseUV; +layout(location = 2) in flat vec4 tinting; +layout(location = 3) in flat vec4 addin; +layout(location = 4) in flat uint flags; +layout(location = 5) in flat vec4 conditionalTinting; +//layout(location = 6) in flat vec4 solidColour; + layout(location = 0) out vec4 outColour; void main() { - outColour = vec4(1,0,0,1); + vec2 uv = mod(uv, vec2(1.0))*(1.0/(vec2(3.0,2.0)*256.0)); + //vec4 colour = solidColour; + vec4 colour = texture(blockModelAtlas, uv + baseUV, ((flags>>1)&1u)*-4.0); + if ((flags&1u) == 1 && colour.a <= 0.25f) { + discard; + } + + //Conditional tinting, TODO: FIXME: REPLACE WITH MASK OR SOMETHING, like encode data into the top bit of alpha + if ((flags&(1u<<2)) != 0 && abs(colour.r-colour.g) < 0.02f && abs(colour.g-colour.b) < 0.02f) { + colour *= conditionalTinting; + } + + outColour = (colour * tinting) + addin; + + //outColour = vec4(uv + baseUV, 0, 1); } \ No newline at end of file diff --git a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.vert b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.vert index ae9021d1..8db9ceb7 100644 --- a/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.vert +++ b/src/main/resources/assets/voxy/shaders/lod/gl46mesh/quads.vert @@ -7,6 +7,7 @@ #define MESHLET_SIZE (QUADS_PER_MESHLET+2) #import #import +#import #define PosHeader Quad @@ -59,22 +60,138 @@ bool setupMeshlet() { return false; } + + + +layout(location = 0) out vec2 uv; +layout(location = 1) out flat vec2 baseUV; +layout(location = 2) out flat vec4 tinting; +layout(location = 3) out flat vec4 addin; +layout(location = 4) out flat uint flags; +layout(location = 5) out flat vec4 conditionalTinting; + +vec4 uint2vec4RGBA(uint colour) { + return vec4((uvec4(colour)>>uvec4(24,16,8,0))&uvec4(0xFF))/255.0; +} + +vec4 getFaceSize(uint faceData) { + float EPSILON = 0.001f; + vec4 faceOffsetsSizes = extractFaceSizes(faceData); + //Expand the quads by a very small amount + faceOffsetsSizes.xz -= vec2(EPSILON); + faceOffsetsSizes.yw += vec2(EPSILON); + + //Make the end relative to the start + faceOffsetsSizes.yw -= faceOffsetsSizes.xz; + + return faceOffsetsSizes; +} + +//TODO: make branchless by using ternaries i think +vec3 swizzelDataAxis(uint axis, vec3 data) { + if (axis == 0) { //Up/down + data = data.xzy; + } + //Not needed, here for readability + //if (axis == 1) {//north/south + // offset = offset.xyz; + //} + if (axis == 2) { //west/east + data = data.zxy; + } + return data; +} + void main() { if (setupMeshlet()) { gl_Position = vec4(1.0f/0.0f); return; } - if ((gl_VertexID>>2)!=0) { - gl_Position = vec4(1.0f/0.0f); - return; - } - uint detail = extractDetail(meshletPosition); + uint lodLevel = extractDetail(meshletPosition); ivec3 sectionPos = extractPosition(meshletPosition); - ivec3 pos = (((sectionPos<>1)&1)*(1<>8)&0xFFu)*(1.0/(256.0)); + baseUV = modelUV + (vec2(face>>1, face&1u) * (1.0/(vec2(3.0, 2.0)*256.0))); + + ivec2 quadSize = extractSize(quad); + + { //Generate tinting and flag data + flags = faceHasAlphaCuttout(faceData); + + //We need to have a conditional override based on if the model size is < a full face + quadSize > 1 + flags |= uint(any(greaterThan(quadSize, ivec2(1)))) & faceHasAlphaCuttoutOverride(faceData); + + flags |= uint(!modelHasMipmaps(model))<<1; + + //Compute lighting + tinting = getLighting(extractLightId(quad)); + + //Apply model colour tinting + uint tintColour = model.colourTint; + if (modelHasBiomeLUT(model)) { + tintColour = colourData[tintColour + extractBiomeId(quad)]; + } + + conditionalTinting = vec4(0); + if (tintColour != uint(-1)) { + flags |= 1u<<2; + conditionalTinting = uint2vec4RGBA(tintColour).yzwx; + } + + addin = vec4(0.0); + if (!isTranslucent) { + tinting.w = 0.0; + //Encode the face, the lod level and + uint encodedData = 0; + encodedData |= face; + encodedData |= (lodLevel<<3); + encodedData |= uint(hasAO)<<6; + addin.w = float(encodedData)/255.0; + } + + //Apply face tint + if (isShaded) { + //TODO: make branchless, infact apply ahead of time to the texture itself in ModelManager since that is + // per face + if ((face>>1) == 1) { + tinting.xyz *= 0.8f; + } else if ((face>>1) == 2) { + tinting.xyz *= 0.6f; + } else if (face == 0){ + tinting.xyz *= 0.5f; + } + } + } + + + + + + vec4 faceSize = getFaceSize(faceData); + + vec2 cQuadSize = (faceSize.yw + quadSize - 1) * vec2((cornerIdx>>1)&1, cornerIdx&1); + uv = faceSize.xz + cQuadSize; + + vec3 cornerPos = extractPos(quad); + float depthOffset = extractFaceIndentation(faceData); + cornerPos += swizzelDataAxis(face>>1, vec3(faceSize.xz, mix(depthOffset, 1-depthOffset, float(face&1u)))); + + vec3 origin = vec3(((sectionPos<>1,vec3(cQuadSize,0)))*(1<