From c3d41c26f7f22eaba7bd045d4b0660b6d0fd6534 Mon Sep 17 00:00:00 2001 From: Oliver-makes-code Date: Wed, 3 Apr 2024 23:38:44 -0500 Subject: [PATCH] A couple microoptimizations I did for fun --- Client/Content/shaders/common/filtering.glsl | 27 +++++++++++++------- Client/Content/shaders/terrain.v.glsl | 20 ++++++++------- Client/Rendering/World/ChunkRenderer.cs | 1 - 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Client/Content/shaders/common/filtering.glsl b/Client/Content/shaders/common/filtering.glsl index 46431ea..2b47b72 100644 --- a/Client/Content/shaders/common/filtering.glsl +++ b/Client/Content/shaders/common/filtering.glsl @@ -13,18 +13,19 @@ vec3 colorBlendUniform(vec3 colA, vec3 colB, float h) { ); // rgb to cone (arg of pow can't be negative) - vec3 lmsA = pow(kCONEtoLMS*colA, vec3(1.0/3.0)); - vec3 lmsB = pow(kCONEtoLMS*colB, vec3(1.0/3.0)); + const vec3 _1_3 = vec3(1.0f / 3.0f); + vec3 lmsA = pow(kCONEtoLMS*colA, _1_3); + vec3 lmsB = pow(kCONEtoLMS*colB, _1_3); // lerp vec3 lms = mix(lmsA, lmsB, h); // gain in the middle (no oaklab anymore, but looks better?) - lms *= 1.0+0.2*h*(1.0-h); + lms *= 1.0 + 0.2 * h * (1.0 - h); // cone to rgb - return kLMStoCONE*(lms*lms*lms); + return kLMStoCONE * (lms * lms * lms); } float weightedRatio(float n, float m) { - return 0.5 + 0.5 * (1 - min(n,m)/max(max(n,m),1.175494e-38)) * sign(m-n); + return 0.5 + 0.5 * (1 - min(n,m) / max(max(n,m), 1.175494e-38)) * sign(m-n); } vec4 colorBlendWeightedAverage(vec4[4] colorPoints) { @@ -44,7 +45,7 @@ vec4 colorBlendWeightedUniform(vec4[4] colorPoints) { float alphaAvg = ( colorPoints[0].a + colorPoints[1].a + colorPoints[2].a + colorPoints[3].a - ) / 4; + ) * 0.25; float aMix = weightedRatio(colorPoints[0].a, colorPoints[1].a); vec3 a = colorBlendUniform(colorPoints[0].rgb, colorPoints[1].rgb, aMix); @@ -62,7 +63,7 @@ vec4 colorBlendAverage(vec4[4] colorPoints) { colorPoints[1] + colorPoints[2] + colorPoints[3] - ) / 4; + ) * 0.25; } vec4 colorBlendUniform(vec4[4] colorPoints) { @@ -75,23 +76,31 @@ vec4 colorBlendUniform(vec4[4] colorPoints) { #AREA FRAGMENT vec4[4] interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sampler sam) { + // Get the size of the texture vec2 inverseTexSize = textureSize(sampler2D(tex, sam), 0); vec2 texSize = 1 / inverseTexSize; vec2 oldUv = uv; - vec2 boxSize = (abs(dFdx(oldUv)) + abs(dFdy(oldUv))) * inverseTexSize * 0.8; + // Get the size of the current pixel on the texture + vec2 boxSize = (abs(dFdxCoarse(oldUv)) + abs(dFdyCoarse(oldUv))) * inverseTexSize * 0.8; + // Get the functional center for interpolation vec2 tx = oldUv * inverseTexSize - 0.5 * boxSize; vec2 tfract = fract(tx); + + // Get the offset for the interpolation vec2 txOffset = smoothstep(1 - boxSize, vec2(1), tfract); + // Get the minimum and maximum coordinates for sampling vec2 tmin = uvMin + 0.5 * texSize; vec2 tmax = uvMax - 0.5 * texSize; + // Clamp the texture coordinates and rescale it to the texture vec2 newUvMin = clamp((tx - tfract + 0.5) * texSize, tmin, tmax); vec2 newUvMax = clamp((tx - tfract + 0.5 + txOffset) * texSize, tmin, tmax); + // Sample the colors vec4[4] sampledColors = { texture(sampler2D(tex, sam), newUvMin), texture(sampler2D(tex, sam), newUvMax), @@ -101,4 +110,4 @@ vec4[4] interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sample return sampledColors; } -#END \ No newline at end of file +#END diff --git a/Client/Content/shaders/terrain.v.glsl b/Client/Content/shaders/terrain.v.glsl index ab1e011..1c63570 100644 --- a/Client/Content/shaders/terrain.v.glsl +++ b/Client/Content/shaders/terrain.v.glsl @@ -4,19 +4,21 @@ layout (set = 2, binding = 0) uniform sampler TextureSampler; layout (set = 2, binding = 1) uniform texture2D Texture; -void UnpackUv(int packedUv, out vec2 uv) { +void UnpackUv(uint packedUv, out vec2 uv) { + const float _1_65535 = 1 / 65535.0f; uv = vec2( - (packedUv & 65535) / 65535.0f, - ((packedUv >> 16) & 65535) / 65535.0f + (packedUv & 0xFFFFu) * _1_65535, + ((packedUv >> 16u) & 0xFFFFu) * _1_65535 ); } -void Unpack(int packedColorAndAo, int packedUv, int packedUvMin, int packedUvMax, out vec4 colorAndAo, out vec2 uv, out vec2 uvMin, out vec2 uvMax){ +void Unpack(uint packedColorAndAo, uint packedUv, uint packedUvMin, uint packedUvMax, out vec4 colorAndAo, out vec2 uv, out vec2 uvMin, out vec2 uvMax){ + const float _1_255 = 1.0f / 255.0f; colorAndAo = vec4( - (packedColorAndAo & 255) / 255.0f, - ((packedColorAndAo >> 8) & 255) / 255.0f, - ((packedColorAndAo >> 16) & 255) / 255.0f, - ((packedColorAndAo >> 24) & 255) / 255.0f + (packedColorAndAo & 0xFFu) * _1_255, + ((packedColorAndAo >> 8u) & 0xFFu) * _1_255, + ((packedColorAndAo >> 16u) & 0xFFu) * _1_255, + ((packedColorAndAo >> 24u) & 0xFFu) * _1_255 ); UnpackUv(packedUv, uv); @@ -24,7 +26,7 @@ void Unpack(int packedColorAndAo, int packedUv, int packedUvMin, int packedUvMax UnpackUv(packedUvMax, uvMax); } -void vert(vec3 position, int packedColorAndAo, int packedUv, int packedUvMin, int packedUvMax, out vec3 o_color, out vec2 o_uv, out vec2 o_uvMin, out vec2 o_uvMax){ +void vert(vec3 position, uint packedColorAndAo, uint packedUv, uint packedUvMin, uint packedUvMax, out vec3 o_color, out vec2 o_uv, out vec2 o_uvMin, out vec2 o_uvMax){ vec4 colorAndAo; Unpack(packedColorAndAo, packedUv, packedUvMin, packedUvMax, colorAndAo, o_uv, o_uvMin, o_uvMax); o_color = colorBlendUniform(colorAndAo.rgb, vec3(0), colorAndAo.a); diff --git a/Client/Rendering/World/ChunkRenderer.cs b/Client/Rendering/World/ChunkRenderer.cs index bfffb5f..4455486 100644 --- a/Client/Rendering/World/ChunkRenderer.cs +++ b/Client/Rendering/World/ChunkRenderer.cs @@ -54,7 +54,6 @@ public override void CreatePipeline(MainFramebuffer framebuffer) { chunkPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() { BlendState = new() { AttachmentStates = [ - BlendAttachmentDescription.OverrideBlend, BlendAttachmentDescription.OverrideBlend ] },