attempted to improve mipping by computing dx,dy accross entire uv instead of local uv

This commit is contained in:
mcrcortex
2025-06-10 13:08:26 +10:00
parent cb599eea0b
commit 22553eb1f9
2 changed files with 75 additions and 23 deletions

View File

@@ -1,4 +1,8 @@
#version 460 core #version 460 core
//Use quad shuffling to compute fragment mip
//#extension GL_KHR_shader_subgroup_quad: enable
layout(binding = 0) uniform sampler2D blockModelAtlas; layout(binding = 0) uniform sampler2D blockModelAtlas;
layout(binding = 2) uniform sampler2D depthTex; layout(binding = 2) uniform sampler2D depthTex;
@@ -13,22 +17,50 @@ layout(location = 2) in flat vec4 tinting;
layout(location = 3) in flat vec4 addin; layout(location = 3) in flat vec4 addin;
layout(location = 4) in flat uint flags; layout(location = 4) in flat uint flags;
layout(location = 5) in flat vec4 conditionalTinting; layout(location = 5) in flat vec4 conditionalTinting;
layout(location = 6) in flat vec2 quadSize;
#ifdef DEBUG_RENDER #ifdef DEBUG_RENDER
layout(location = 6) in flat uint quadDebug; layout(location = 7) in flat uint quadDebug;
#endif #endif
layout(location = 0) out vec4 outColour; layout(location = 0) out vec4 outColour;
vec4 computeColour(vec4 colour) {
//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;
}
return (colour * tinting) + addin;
}
bool useMipmaps() {
return ((flags>>1)&1u)==0u;
}
void main() { void main() {
//Tile is the tile we are in
vec2 tile;
vec2 uv2 = modf(uv, tile)*(1.0/(vec2(3.0,2.0)*256.0));
vec4 colour = vec4(1);
vec2 texPos = uv2 + baseUV;
if (useMipmaps()) {
vec2 uvSmol = uv*(1.0/(vec2(3.0,2.0)*256.0));
vec2 dx = dFdx(uvSmol);//vec2(lDx, dDx);
vec2 dy = dFdy(uvSmol);//vec2(lDy, dDy);
colour = textureGrad(blockModelAtlas, texPos, dx, dy);
} else {
colour = texture(blockModelAtlas, texPos, -5.0);
}
if (any(notEqual(clamp(tile, vec2(0), quadSize), tile))) {
discard;
}
//Check the minimum bounding texture and ensure we are greater than it //Check the minimum bounding texture and ensure we are greater than it
if (gl_FragCoord.z < texelFetch(depthTex, ivec2(gl_FragCoord.xy), 0).r) { if (gl_FragCoord.z < texelFetch(depthTex, ivec2(gl_FragCoord.xy), 0).r) {
discard; discard;
} }
vec2 uv = mod(uv, vec2(1.0))*(1.0/(vec2(3.0,2.0)*256.0));
vec2 texPos = uv + baseUV;
//vec4 colour = solidColour;
//TODO: FIXME, need to manually compute the mip colour
vec4 colour = texture(blockModelAtlas, texPos, ((flags>>1)&1u)*-5.0);//TODO: FIXME mipping needs to be fixed so that it doesnt go cross model bounds
//Also, small quad is really fking over the mipping level somehow //Also, small quad is really fking over the mipping level somehow
if ((flags&1u) == 1 && (texture(blockModelAtlas, texPos, -16.0).a <= 0.1f)) { if ((flags&1u) == 1 && (texture(blockModelAtlas, texPos, -16.0).a <= 0.1f)) {
//This is stupidly stupidly bad for divergence //This is stupidly stupidly bad for divergence
@@ -38,14 +70,7 @@ void main() {
#endif #endif
} }
//Conditional tinting, TODO: FIXME: REPLACE WITH MASK OR SOMETHING, like encode data into the top bit of alpha outColour = computeColour(colour);
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);
#ifdef DEBUG_RENDER #ifdef DEBUG_RENDER
@@ -56,4 +81,30 @@ void main() {
hash = hash * 1827364925 + 123325621; hash = hash * 1827364925 + 123325621;
outColour = vec4(float(hash&15u)/15, float((hash>>4)&15u)/15, float((hash>>8)&15u)/15, 1); outColour = vec4(float(hash&15u)/15, float((hash>>4)&15u)/15, float((hash>>8)&15u)/15, 1);
#endif #endif
} }
//#ifdef GL_KHR_shader_subgroup_quad
/*
uint hash = (uint(tile.x)*(1<<16))^uint(tile.y);
uint horiz = subgroupQuadSwapHorizontal(hash);
bool sameTile = horiz==hash;
uint sv = mix(uint(-1), hash, sameTile);
uint vert = subgroupQuadSwapVertical(sv);
sameTile = sameTile&&vert==hash;
mipBias = sameTile?0:-5.0;
*/
/*
vec2 uvSmol = uv*(1.0/(vec2(3.0,2.0)*256.0));
float lDx = subgroupQuadSwapHorizontal(uvSmol.x)-uvSmol.x;
float lDy = subgroupQuadSwapVertical(uvSmol.y)-uvSmol.y;
float dDx = subgroupQuadSwapDiagonal(lDx);
float dDy = subgroupQuadSwapDiagonal(lDy);
vec2 dx = vec2(lDx, dDx);
vec2 dy = vec2(lDy, dDy);
colour = textureGrad(blockModelAtlas, texPos, dx, dy);
*/
//#else
//colour = texture(blockModelAtlas, texPos);
//#endif

View File

@@ -20,9 +20,10 @@ layout(location = 2) out flat vec4 tinting;
layout(location = 3) out flat vec4 addin; layout(location = 3) out flat vec4 addin;
layout(location = 4) out flat uint flags; layout(location = 4) out flat uint flags;
layout(location = 5) out flat vec4 conditionalTinting; layout(location = 5) out flat vec4 conditionalTinting;
layout(location = 6) out flat vec2 size;
#ifdef DEBUG_RENDER #ifdef DEBUG_RENDER
layout(location = 6) out flat uint quadDebug; layout(location = 7) out flat uint quadDebug;
#endif #endif
/* /*
@@ -41,17 +42,17 @@ vec4 uint2vec4RGBA(uint colour) {
} }
vec4 getFaceSize(uint faceData) { vec4 getFaceSize(uint faceData) {
float EPSILON = 0.0005f; float EPSILON = 0.00005f;
vec4 faceOffsetsSizes = extractFaceSizes(faceData); vec4 faceOffsetsSizes = extractFaceSizes(faceData);
//Make the end relative to the start
faceOffsetsSizes.yw -= faceOffsetsSizes.xz;
//Expand the quads by a very small amount //Expand the quads by a very small amount
faceOffsetsSizes.xz -= vec2(EPSILON); faceOffsetsSizes.xz -= vec2(EPSILON);
faceOffsetsSizes.yw += vec2(EPSILON); faceOffsetsSizes.yw += vec2(EPSILON);
//Make the end relative to the start
faceOffsetsSizes.yw -= faceOffsetsSizes.xz;
return faceOffsetsSizes; return faceOffsetsSizes;
} }
@@ -105,10 +106,10 @@ void main() {
ivec2 quadSize = extractSize(quad); ivec2 quadSize = extractSize(quad);
if (cornerIdx == 1) //Only if we are the provoking vertex if (cornerIdx == 1) //Only if we are the provoking vertex
{ {
size = vec2(quadSize-1);
vec2 modelUV = vec2(modelId&0xFFu, (modelId>>8)&0xFFu)*(1.0/(256.0)); vec2 modelUV = vec2(modelId&0xFFu, (modelId>>8)&0xFFu)*(1.0/(256.0));
baseUV = modelUV + (vec2(face>>1, face&1u) * (1.0/(vec2(3.0, 2.0)*256.0))); baseUV = modelUV + (vec2(face>>1, face&1u) * (1.0/(vec2(3.0, 2.0)*256.0)));