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

Commit c3d41c2

Browse files
A couple microoptimizations I did for fun
1 parent b94c2ab commit c3d41c2

3 files changed

Lines changed: 29 additions & 19 deletions

File tree

Client/Content/shaders/common/filtering.glsl

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ vec3 colorBlendUniform(vec3 colA, vec3 colB, float h) {
1313
);
1414

1515
// rgb to cone (arg of pow can't be negative)
16-
vec3 lmsA = pow(kCONEtoLMS*colA, vec3(1.0/3.0));
17-
vec3 lmsB = pow(kCONEtoLMS*colB, vec3(1.0/3.0));
16+
const vec3 _1_3 = vec3(1.0f / 3.0f);
17+
vec3 lmsA = pow(kCONEtoLMS*colA, _1_3);
18+
vec3 lmsB = pow(kCONEtoLMS*colB, _1_3);
1819
// lerp
1920
vec3 lms = mix(lmsA, lmsB, h);
2021
// gain in the middle (no oaklab anymore, but looks better?)
21-
lms *= 1.0+0.2*h*(1.0-h);
22+
lms *= 1.0 + 0.2 * h * (1.0 - h);
2223
// cone to rgb
23-
return kLMStoCONE*(lms*lms*lms);
24+
return kLMStoCONE * (lms * lms * lms);
2425
}
2526

2627
float weightedRatio(float n, float m) {
27-
return 0.5 + 0.5 * (1 - min(n,m)/max(max(n,m),1.175494e-38)) * sign(m-n);
28+
return 0.5 + 0.5 * (1 - min(n,m) / max(max(n,m), 1.175494e-38)) * sign(m-n);
2829
}
2930

3031
vec4 colorBlendWeightedAverage(vec4[4] colorPoints) {
@@ -44,7 +45,7 @@ vec4 colorBlendWeightedUniform(vec4[4] colorPoints) {
4445
float alphaAvg = (
4546
colorPoints[0].a + colorPoints[1].a +
4647
colorPoints[2].a + colorPoints[3].a
47-
) / 4;
48+
) * 0.25;
4849

4950
float aMix = weightedRatio(colorPoints[0].a, colorPoints[1].a);
5051
vec3 a = colorBlendUniform(colorPoints[0].rgb, colorPoints[1].rgb, aMix);
@@ -62,7 +63,7 @@ vec4 colorBlendAverage(vec4[4] colorPoints) {
6263
colorPoints[1] +
6364
colorPoints[2] +
6465
colorPoints[3]
65-
) / 4;
66+
) * 0.25;
6667
}
6768

6869
vec4 colorBlendUniform(vec4[4] colorPoints) {
@@ -75,23 +76,31 @@ vec4 colorBlendUniform(vec4[4] colorPoints) {
7576

7677
#AREA FRAGMENT
7778
vec4[4] interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sampler sam) {
79+
// Get the size of the texture
7880
vec2 inverseTexSize = textureSize(sampler2D(tex, sam), 0);
7981
vec2 texSize = 1 / inverseTexSize;
8082

8183
vec2 oldUv = uv;
8284

83-
vec2 boxSize = (abs(dFdx(oldUv)) + abs(dFdy(oldUv))) * inverseTexSize * 0.8;
85+
// Get the size of the current pixel on the texture
86+
vec2 boxSize = (abs(dFdxCoarse(oldUv)) + abs(dFdyCoarse(oldUv))) * inverseTexSize * 0.8;
8487

88+
// Get the functional center for interpolation
8589
vec2 tx = oldUv * inverseTexSize - 0.5 * boxSize;
8690
vec2 tfract = fract(tx);
91+
92+
// Get the offset for the interpolation
8793
vec2 txOffset = smoothstep(1 - boxSize, vec2(1), tfract);
8894

95+
// Get the minimum and maximum coordinates for sampling
8996
vec2 tmin = uvMin + 0.5 * texSize;
9097
vec2 tmax = uvMax - 0.5 * texSize;
9198

99+
// Clamp the texture coordinates and rescale it to the texture
92100
vec2 newUvMin = clamp((tx - tfract + 0.5) * texSize, tmin, tmax);
93101
vec2 newUvMax = clamp((tx - tfract + 0.5 + txOffset) * texSize, tmin, tmax);
94102

103+
// Sample the colors
95104
vec4[4] sampledColors = {
96105
texture(sampler2D(tex, sam), newUvMin),
97106
texture(sampler2D(tex, sam), newUvMax),
@@ -101,4 +110,4 @@ vec4[4] interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sample
101110
return sampledColors;
102111
}
103112

104-
#END
113+
#END

Client/Content/shaders/terrain.v.glsl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44
layout (set = 2, binding = 0) uniform sampler TextureSampler;
55
layout (set = 2, binding = 1) uniform texture2D Texture;
66

7-
void UnpackUv(int packedUv, out vec2 uv) {
7+
void UnpackUv(uint packedUv, out vec2 uv) {
8+
const float _1_65535 = 1 / 65535.0f;
89
uv = vec2(
9-
(packedUv & 65535) / 65535.0f,
10-
((packedUv >> 16) & 65535) / 65535.0f
10+
(packedUv & 0xFFFFu) * _1_65535,
11+
((packedUv >> 16u) & 0xFFFFu) * _1_65535
1112
);
1213
}
1314

14-
void Unpack(int packedColorAndAo, int packedUv, int packedUvMin, int packedUvMax, out vec4 colorAndAo, out vec2 uv, out vec2 uvMin, out vec2 uvMax){
15+
void Unpack(uint packedColorAndAo, uint packedUv, uint packedUvMin, uint packedUvMax, out vec4 colorAndAo, out vec2 uv, out vec2 uvMin, out vec2 uvMax){
16+
const float _1_255 = 1.0f / 255.0f;
1517
colorAndAo = vec4(
16-
(packedColorAndAo & 255) / 255.0f,
17-
((packedColorAndAo >> 8) & 255) / 255.0f,
18-
((packedColorAndAo >> 16) & 255) / 255.0f,
19-
((packedColorAndAo >> 24) & 255) / 255.0f
18+
(packedColorAndAo & 0xFFu) * _1_255,
19+
((packedColorAndAo >> 8u) & 0xFFu) * _1_255,
20+
((packedColorAndAo >> 16u) & 0xFFu) * _1_255,
21+
((packedColorAndAo >> 24u) & 0xFFu) * _1_255
2022
);
2123

2224
UnpackUv(packedUv, uv);
2325
UnpackUv(packedUvMin, uvMin);
2426
UnpackUv(packedUvMax, uvMax);
2527
}
2628

27-
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){
29+
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){
2830
vec4 colorAndAo;
2931
Unpack(packedColorAndAo, packedUv, packedUvMin, packedUvMax, colorAndAo, o_uv, o_uvMin, o_uvMax);
3032
o_color = colorBlendUniform(colorAndAo.rgb, vec3(0), colorAndAo.a);

Client/Rendering/World/ChunkRenderer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public override void CreatePipeline(MainFramebuffer framebuffer) {
5454
chunkPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() {
5555
BlendState = new() {
5656
AttachmentStates = [
57-
BlendAttachmentDescription.OverrideBlend,
5857
BlendAttachmentDescription.OverrideBlend
5958
]
6059
},

0 commit comments

Comments
 (0)