faster floodfill sampling in rgb not hsv

This commit is contained in:
NULL511 2024-06-18 22:23:29 -04:00
parent ccf5ebd5d4
commit 41b2f443d9
3 changed files with 108 additions and 135 deletions

View File

@ -31,10 +31,8 @@ layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
int sumOf(ivec3 vec) {return vec.x + vec.y + vec.z;}
vec3 Lpv_RgbToHsv(const in vec3 lightColor, const in float lightRange) {
vec3 lightValue = RgbToHsv(lightColor);
lightValue.b = lightRange / LpvBlockSkyRange.x;
return lightValue;
int getSharedIndex(ivec3 pos) {
return sumOf(pos * lpvFlatten);
}
vec4 GetLpvValue(in ivec3 texCoord) {
@ -44,14 +42,25 @@ layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
? imageLoad(imgLpv2, texCoord)
: imageLoad(imgLpv1, texCoord);
lpvSample.ba = exp2(lpvSample.ba * LpvBlockSkyRange) - 1.0;
lpvSample.rgb = HsvToRgb(lpvSample.rgb);
vec4 hsv_sky = vec4(RgbToHsv(lpvSample.rgb), lpvSample.a);
hsv_sky.zw = exp2(hsv_sky.zw * LpvBlockSkyRange) - 1.0;
lpvSample = vec4(HsvToRgb(hsv_sky.xyz), hsv_sky.w);
return lpvSample;
}
int getSharedIndex(ivec3 pos) {
return sumOf(pos * lpvFlatten);
uint GetVoxelBlock(const in ivec3 voxelPos) {
if (clamp(voxelPos, ivec3(0), ivec3(VoxelSize3-1u)) != voxelPos)
return BLOCK_EMPTY;
return imageLoad(imgVoxelMask, voxelPos).r;
}
void PopulateSharedIndex(const in ivec3 imgCoordOffset, const in ivec3 workGroupOffset, const in uint i) {
ivec3 pos = workGroupOffset + ivec3(i / lpvFlatten) % 10;
lpvSharedData[i] = GetLpvValue(imgCoordOffset + pos);
voxelSharedData[i] = GetVoxelBlock(pos);
}
vec4 sampleShared(ivec3 pos, int mask_index) {
@ -83,20 +92,6 @@ layout (local_size_x = 8, local_size_y = 8, local_size_z = 8) in;
const vec4 avgFalloff = (1.0/6.0) * LpvBlockSkyFalloff.xxxy;
return (sX1 + sX2 + sY1 + sY2 + sZ1 + sZ2) * avgFalloff;
}
uint GetVoxelBlock(const in ivec3 voxelPos) {
if (clamp(voxelPos, ivec3(0), ivec3(VoxelSize3-1u)) != voxelPos)
return BLOCK_EMPTY;
return imageLoad(imgVoxelMask, voxelPos).r;
}
void PopulateSharedIndex(const in ivec3 imgCoordOffset, const in ivec3 workGroupOffset, const in uint i) {
ivec3 pos = workGroupOffset + ivec3(i / lpvFlatten) % 10;
lpvSharedData[i] = GetLpvValue(imgCoordOffset + pos);
voxelSharedData[i] = GetVoxelBlock(pos);
}
#endif
@ -107,6 +102,7 @@ void main() {
uvec3 chunkPos = gl_WorkGroupID * gl_WorkGroupSize;
if (any(greaterThanEqual(chunkPos, LpvSize3))) return;
// Pre-populate shared-memory buffer for improved sampling performance
uint i = uint(gl_LocalInvocationIndex) * 2u;
if (i < 1000u) {
ivec3 imgCoordOffset = ivec3(floor(cameraPosition) - floor(previousCameraPosition));
@ -118,42 +114,49 @@ void main() {
barrier();
// Exit early if outside LPV buffer size
ivec3 imgCoord = ivec3(gl_GlobalInvocationID);
if (any(greaterThanEqual(imgCoord, LpvSize3))) return;
uint blockId = voxelSharedData[getSharedIndex(ivec3(gl_LocalInvocationID) + 1)];
vec4 lightValue = vec4(0.0);
vec3 lightColor = vec3(0.0);
vec3 tintColor = vec3(1.0);
float lightRange = 0.0;
uint mixMask = 0xFFFF;
if (blockId > 0u) {
tintColor = vec3(0.0);
// Decode light data for current voxel
uint blockId = voxelSharedData[getSharedIndex(ivec3(gl_LocalInvocationID) + 1)];
if (blockId > 0u) {
uvec2 blockData = imageLoad(imgBlockData, int(blockId)).rg;
vec4 lightColorRange = unpackUnorm4x8(blockData.r);
lightColor = srgbToLinear(lightColorRange.rgb);
lightRange = lightColorRange.a * 255.0;
vec4 tintColorMask = unpackUnorm4x8(blockData.g);
tintColor = srgbToLinear(tintColorMask.rgb);
mixMask = (blockData.g >> 24) & 0xFFFF;
vec3 lightColor = srgbToLinear(lightColorRange.rgb);
float lightRange = lightColorRange.a * 255.0;
if (lightRange > 0.0) {
lightValue.rgb = Lpv_RgbToHsv(lightColor, lightRange);
lightValue.ba = exp2(lightValue.ba * LpvBlockSkyRange) - 1.0;
lightValue.rgb = HsvToRgb(lightValue.rgb);
}
}
// Mix neighbor voxel light values
if (any(greaterThan(tintColor, vec3(0.0)))) {
vec4 lightMixed = mixNeighbours(ivec3(gl_LocalInvocationID), mixMask);
lightMixed.rgb *= tintColor;
lightValue += lightMixed;
}
lightValue.rgb = RgbToHsv(lightValue.rgb);
lightValue.ba = log2(lightValue.ba + 1.0) / LpvBlockSkyRange;
// Add light for current voxel
if (lightRange > 0.0) {
vec3 hsv = RgbToHsv(lightColor);
hsv.z = exp2(lightRange) - 1.0;
lightValue.rgb += HsvToRgb(hsv);
}
// Convert back to linear RGB space
vec4 hsv_sky = vec4(RgbToHsv(lightValue.rgb), lightValue.a);
hsv_sky.zw = log2(hsv_sky.zw + 1.0) / LpvBlockSkyRange;
lightValue = vec4(HsvToRgb(hsv_sky.xyz), hsv_sky.w);
// Store final value
if (frameCounter % 2 == 0)
imageStore(imgLpv1, imgCoord, lightValue);
else

View File

@ -1,51 +1,23 @@
// LPV falloff curve
const float LpvBlockPower = 4.0;
// LPV block brightness scale
const float LpvBlockBrightness = 2.0;
float lpvCurve(float values) {
// return pow(values, LpvBlockPower) ;
return pow(1.0 - sqrt(1.0-values), 2.0);
// return values;
}
vec4 SampleLpvNearest(const in ivec3 lpvPos) {
vec4 lpvSample = (frameCounter % 2) == 0
? texelFetch(texLpv1, lpvPos, 0)
: texelFetch(texLpv2, lpvPos, 0);
lpvSample.b = lpvCurve(lpvSample.b) * LpvBlockSkyRange.x;
lpvSample.rgb = HsvToRgb(lpvSample.rgb);
return lpvSample;
}
vec4 SampleLpvLinear(const in vec3 lpvPos) {
vec3 pos = lpvPos - 0.5;
ivec3 lpvCoord = ivec3(floor(pos));
vec3 lpvF = fract(pos);
vec3 texcoord = lpvPos / LpvSize3;
vec4 sample_x1y1z1 = SampleLpvNearest(lpvCoord + ivec3(0, 0, 0));
vec4 sample_x2y1z1 = SampleLpvNearest(lpvCoord + ivec3(1, 0, 0));
vec4 sample_x1y2z1 = SampleLpvNearest(lpvCoord + ivec3(0, 1, 0));
vec4 sample_x2y2z1 = SampleLpvNearest(lpvCoord + ivec3(1, 1, 0));
vec4 lpvSample = (frameCounter % 2) == 0
? textureLod(texLpv1, texcoord, 0)
: textureLod(texLpv2, texcoord, 0);
vec4 sample_x1y1z2 = SampleLpvNearest(lpvCoord + ivec3(0, 0, 1));
vec4 sample_x2y1z2 = SampleLpvNearest(lpvCoord + ivec3(1, 0, 1));
vec4 sample_x1y2z2 = SampleLpvNearest(lpvCoord + ivec3(0, 1, 1));
vec4 sample_x2y2z2 = SampleLpvNearest(lpvCoord + ivec3(1, 1, 1));
vec3 hsv = RgbToHsv(lpvSample.rgb);
hsv.z = lpvCurve(hsv.b) * LpvBlockSkyRange.x;
lpvSample.rgb = HsvToRgb(hsv);
vec4 sample_y1z1 = mix(sample_x1y1z1, sample_x2y1z1, lpvF.x);
vec4 sample_y2z1 = mix(sample_x1y2z1, sample_x2y2z1, lpvF.x);
vec4 sample_y1z2 = mix(sample_x1y1z2, sample_x2y1z2, lpvF.x);
vec4 sample_y2z2 = mix(sample_x1y2z2, sample_x2y2z2, lpvF.x);
vec4 sample_z1 = mix(sample_y1z1, sample_y2z1, lpvF.y);
vec4 sample_z2 = mix(sample_y1z2, sample_y2z2, lpvF.y);
return mix(sample_z1, sample_z2, lpvF.z);
return lpvSample;
}
vec3 GetLpvBlockLight(const in vec4 lpvSample) {

View File

@ -33,8 +33,6 @@ void PopulateShadowVoxel(const in vec3 playerPos) {
if (
((renderStage == MC_RENDER_STAGE_ENTITIES && (currentRenderedItemId > 0 || entityId > 0)) || renderStage == MC_RENDER_STAGE_BLOCK_ENTITIES)
) {
voxelId = uint(mc_Entity.x + 0.5);
if (renderStage == MC_RENDER_STAGE_BLOCK_ENTITIES) {
if (blockEntityId > 0 && blockEntityId < 500)
voxelId = uint(blockEntityId);