Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
A couple microoptimizations I did for fun
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Apr 4, 2024
1 parent b94c2ab commit c3d41c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
27 changes: 18 additions & 9 deletions Client/Content/shaders/common/filtering.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -62,7 +63,7 @@ vec4 colorBlendAverage(vec4[4] colorPoints) {
colorPoints[1] +
colorPoints[2] +
colorPoints[3]
) / 4;
) * 0.25;
}

vec4 colorBlendUniform(vec4[4] colorPoints) {
Expand All @@ -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),
Expand All @@ -101,4 +110,4 @@ vec4[4] interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sample
return sampledColors;
}

#END
#END
20 changes: 11 additions & 9 deletions Client/Content/shaders/terrain.v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
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);
UnpackUv(packedUvMin, uvMin);
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);
Expand Down
1 change: 0 additions & 1 deletion Client/Rendering/World/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public override void CreatePipeline(MainFramebuffer framebuffer) {
chunkPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() {
BlendState = new() {
AttachmentStates = [
BlendAttachmentDescription.OverrideBlend,
BlendAttachmentDescription.OverrideBlend
]
},
Expand Down

0 comments on commit c3d41c2

Please sign in to comment.