Antialiasing (SMAA) implementation

This commit is contained in:
Anuken
2026-09-21 01:57:43 -04:00
parent 65da207421
commit d7d0506e3f
13 changed files with 697 additions and 0 deletions
+2
View File
@@ -1461,6 +1461,8 @@ setting.fpscap.name = Max FPS
setting.fpscap.description = Sets the maximum frames per second the game will run at.\nHigher values may increase power consumption.
setting.fpscap.none = None
setting.fpscap.text = {0} FPS
setting.smaa.name = Antialiasing
setting.smaa.description = Enables SMAAA; smoothes out harsh edges of pixels.\nMaybe have significant performance impact.
setting.uiscale.name = UI Scaling
setting.uiscale.description = Modifies the size of the user interface.\nRestart required to apply changes.
setting.uiEdgePadding.name = UI Edge Padding
Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

+46
View File
@@ -0,0 +1,46 @@
//SMAA pass 3 (neighborhood blending) - fragment stage. Port of SMAANeighborhoodBlendingPS (no reprojection).
//Inputs: u_texture = original scene color (LINEAR filtering, clamp to edge - the blend uses bilinear taps)
// u_blend = blend weights from pass 2 on texture unit 1
//Output: final anti-aliased color.
#define HIGHP
uniform sampler2D u_texture;
uniform sampler2D u_blend;
uniform vec4 u_metrics;
varying vec2 v_texCoords;
varying vec4 v_offset;
out vec4 fragColor;
void main(){
vec2 texcoord = v_texCoords;
//fetch the blending weights for the current pixel
vec4 a;
a.x = texture2D(u_blend, v_offset.xy).a; //right
a.y = texture2D(u_blend, v_offset.zw).g; //bottom
a.wz = texture2D(u_blend, texcoord).xz; //left, top
//is there any blending weight with a value greater than 0.0?
if(dot(a, vec4(1.0)) < 1e-5){
fragColor = textureLod(u_texture, texcoord, 0.0);
}else{
bool h = max(a.x, a.z) > max(a.y, a.w); //max(horizontal) > max(vertical)
//calculate the blending offsets
vec4 blendingOffset = h ? vec4(a.x, 0.0, a.z, 0.0) : vec4(0.0, a.y, 0.0, a.w);
vec2 blendingWeight = h ? a.xz : a.yw;
blendingWeight /= dot(blendingWeight, vec2(1.0));
//calculate the texture coordinates
vec4 blendingCoord = blendingOffset * vec4(u_metrics.xy, -u_metrics.xy) + texcoord.xyxy;
//two bilinear taps blend across the edge
vec4 color = blendingWeight.x * textureLod(u_texture, blendingCoord.xy, 0.0);
color += blendingWeight.y * textureLod(u_texture, blendingCoord.zw, 0.0);
fragColor = color;
}
}
+17
View File
@@ -0,0 +1,17 @@
//SMAA pass 3 (neighborhood blending) - vertex stage. Port of SMAANeighborhoodBlendingVS.
//u_metrics = (1/width, 1/height, width, height).
attribute vec4 a_position;
attribute vec2 a_texCoord0;
uniform vec4 u_metrics;
varying vec2 v_texCoords;
varying vec4 v_offset;
void main(){
v_texCoords = a_texCoord0;
v_offset = u_metrics.xyxy * vec4(1.0, 0.0, 0.0, 1.0) + a_texCoord0.xyxy;
gl_Position = a_position;
}
+63
View File
@@ -0,0 +1,63 @@
//SMAA pass 1 (luma edge detection) - fragment stage. Port of SMAALumaEdgeDetectionPS (no predication).
//Input: u_texture = scene color (LINEAR filtering, clamp to edge)
//Output: RG = edge flags (left, top). Pixels with no edge are discarded, so the target MUST be cleared to 0 first.
#define HIGHP
//SMAA_PRESET_HIGH values
#define SMAA_THRESHOLD 0.1
#define SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR 2.0
uniform sampler2D u_texture;
varying vec2 v_texCoords;
varying vec4 v_offset0;
varying vec4 v_offset1;
varying vec4 v_offset2;
//explicit declaration so the Arc prelude does not add a lowp one (edge data needs full precision on GLES)
out vec4 fragColor;
const vec3 lumaWeights = vec3(0.2126, 0.7152, 0.0722);
float luma(vec2 coord){
return dot(texture2D(u_texture, coord).rgb, lumaWeights);
}
void main(){
vec2 threshold = vec2(SMAA_THRESHOLD);
//left and top deltas
float L = luma(v_texCoords);
float Lleft = luma(v_offset0.xy);
float Ltop = luma(v_offset0.zw);
vec4 delta;
delta.xy = abs(L - vec2(Lleft, Ltop));
vec2 edges = step(threshold, delta.xy);
//early out if there is no edge
if(dot(edges, vec2(1.0)) == 0.0){
discard;
}
//right and bottom deltas
float Lright = luma(v_offset1.xy);
float Lbottom = luma(v_offset1.zw);
delta.zw = abs(L - vec2(Lright, Lbottom));
vec2 maxDelta = max(delta.xy, delta.zw);
//next-to-left and next-to-top deltas
float Lleftleft = luma(v_offset2.xy);
float Ltoptop = luma(v_offset2.zw);
delta.zw = abs(vec2(Lleft, Ltop) - vec2(Lleftleft, Ltoptop));
maxDelta = max(maxDelta.xy, delta.zw);
float finalDelta = max(maxDelta.x, maxDelta.y);
//local contrast adaptation
edges.xy *= step(finalDelta, SMAA_LOCAL_CONTRAST_ADAPTATION_FACTOR * delta.xy);
fragColor = vec4(edges, 0.0, 0.0);
}
+22
View File
@@ -0,0 +1,22 @@
//SMAA pass 1 (luma edge detection) - vertex stage. Port of SMAAEdgeDetectionVS.
//u_metrics = (1/width, 1/height, width, height) of the buffer being processed.
attribute vec4 a_position;
attribute vec2 a_texCoord0;
uniform vec4 u_metrics;
varying vec2 v_texCoords;
varying vec4 v_offset0;
varying vec4 v_offset1;
varying vec4 v_offset2;
void main(){
v_texCoords = a_texCoord0;
v_offset0 = u_metrics.xyxy * vec4(-1.0, 0.0, 0.0, -1.0) + a_texCoord0.xyxy;
v_offset1 = u_metrics.xyxy * vec4( 1.0, 0.0, 0.0, 1.0) + a_texCoord0.xyxy;
v_offset2 = u_metrics.xyxy * vec4(-2.0, 0.0, 0.0, -2.0) + a_texCoord0.xyxy;
gl_Position = a_position;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

+350
View File
@@ -0,0 +1,350 @@
//SMAA pass 2 (blending weight calculation) - fragment stage. Port of SMAABlendingWeightCalculationPS,
//including the diagonal and corner pattern handling. Non-temporal, so subsample indices are all zero.
//
//Inputs: u_texture = edges from pass 1 (LINEAR filtering, clamp to edge - the searches rely on bilinear fetches)
// u_area = AreaTex (160x560, RG8, LINEAR, clamp) on texture unit 1
// u_search = SearchTex (64x16, R8, point/linear, clamp) on texture unit 2
//Output: RGBA = blend weights.
#define HIGHP
//SMAA_PRESET_HIGH values
#define SMAA_MAX_SEARCH_STEPS 16
#define SMAA_MAX_SEARCH_STEPS_DIAG 8
#define SMAA_CORNER_ROUNDING 25
//uncomment to disable diagonal / corner handling (PRESET_LOW / MEDIUM do this)
//#define SMAA_DISABLE_DIAG_DETECTION
//#define SMAA_DISABLE_CORNER_DETECTION
#define SMAA_AREATEX_MAX_DISTANCE 16
#define SMAA_AREATEX_MAX_DISTANCE_DIAG 20
#define SMAA_AREATEX_PIXEL_SIZE (1.0 / vec2(160.0, 560.0))
#define SMAA_AREATEX_SUBTEX_SIZE (1.0 / 7.0)
#define SMAA_SEARCHTEX_SIZE vec2(66.0, 33.0)
#define SMAA_SEARCHTEX_PACKED_SIZE vec2(64.0, 16.0)
#define SMAA_CORNER_ROUNDING_NORM (float(SMAA_CORNER_ROUNDING) / 100.0)
//offsets to textureLodOffset must be compile-time constants, so this has to be a macro
#define sampleOffset(tex, coord, off) textureLodOffset(tex, coord, 0.0, off)
uniform sampler2D u_texture; //edges
uniform sampler2D u_area;
uniform sampler2D u_search;
uniform vec4 u_metrics;
varying vec2 v_texCoords;
varying vec2 v_pixcoord;
varying vec4 v_offset0;
varying vec4 v_offset1;
varying vec4 v_offset2;
out vec4 fragColor;
//---- diagonal pattern handling ----
#ifndef SMAA_DISABLE_DIAG_DETECTION
//conditionally decodes edges that were fetched with a bilinear tap at a diagonal
vec2 decodeDiag(vec2 e){
e.r = e.r * abs(5.0 * e.r - 5.0 * 0.75);
return round(e);
}
vec4 decodeDiag(vec4 e){
e.rb = e.rb * abs(5.0 * e.rb - 5.0 * 0.75);
return round(e);
}
vec2 searchDiag1(vec2 texcoord, vec2 dir, out vec2 e){
vec4 coord = vec4(texcoord, -1.0, 1.0);
vec3 t = vec3(u_metrics.xy, 1.0);
while(coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9){
coord.xyz = t * vec3(dir, 1.0) + coord.xyz;
e = textureLod(u_texture, coord.xy, 0.0).rg;
coord.w = dot(e, vec2(0.5));
}
return coord.zw;
}
vec2 searchDiag2(vec2 texcoord, vec2 dir, out vec2 e){
vec4 coord = vec4(texcoord, -1.0, 1.0);
coord.x += 0.25 * u_metrics.x; //see @SearchDiag2Optimization in the original
vec3 t = vec3(u_metrics.xy, 1.0);
while(coord.z < float(SMAA_MAX_SEARCH_STEPS_DIAG - 1) && coord.w > 0.9){
coord.xyz = t * vec3(dir, 1.0) + coord.xyz;
//bilinear fetch is used here, so the result must be decoded
e = textureLod(u_texture, coord.xy, 0.0).rg;
e = decodeDiag(e);
coord.w = dot(e, vec2(0.5));
}
return coord.zw;
}
//area lookup for diagonal patterns
vec2 areaDiag(vec2 dist, vec2 e, float offset){
vec2 texcoord = vec2(SMAA_AREATEX_MAX_DISTANCE_DIAG) * e + dist;
//move to texel centers
texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
//diagonal areas are on the second half of the texture
texcoord.x += 0.5;
//subsample offset
texcoord.y += SMAA_AREATEX_SUBTEX_SIZE * offset;
return textureLod(u_area, texcoord, 0.0).rg;
}
vec2 calculateDiagWeights(vec2 texcoord, vec2 e){
vec2 weights = vec2(0.0);
//subsample indices are zero for non-temporal use: z = w = 0.0
vec4 d;
vec2 end;
if(e.r > 0.0){
d.xz = searchDiag1(texcoord, vec2(-1.0, 1.0), end);
d.x += float(end.y > 0.9);
}else{
d.xz = vec2(0.0);
}
d.yw = searchDiag1(texcoord, vec2(1.0, -1.0), end);
if(d.x + d.y > 2.0){ //d.x + d.y + 1 > 3
//fetch the crossing edges
vec4 coords = vec4(-d.x + 0.25, d.x, d.y, -d.y - 0.25) * u_metrics.xyxy + texcoord.xyxy;
vec4 c;
c.xy = sampleOffset(u_texture, coords.xy, ivec2(-1, 0)).rg;
c.zw = sampleOffset(u_texture, coords.zw, ivec2( 1, 0)).rg;
c.yxwz = decodeDiag(c.xyzw);
//merge crossing edges at each side into a single value
vec2 cc = vec2(2.0) * c.xz + c.yw;
//remove the crossing edge if we didn't find the end of the line
if(d.z >= 0.9) cc.x = 0.0;
if(d.w >= 0.9) cc.y = 0.0;
weights += areaDiag(d.xy, cc, 0.0);
}
//second diagonal
d.xz = searchDiag2(texcoord, vec2(-1.0, -1.0), end);
if(sampleOffset(u_texture, texcoord, ivec2(1, 0)).r > 0.0){
d.yw = searchDiag2(texcoord, vec2(1.0, 1.0), end);
d.y += float(end.y > 0.9);
}else{
d.yw = vec2(0.0);
}
if(d.x + d.y > 2.0){ //d.x + d.y + 1 > 3
vec4 coords = vec4(-d.x, -d.x, d.y, d.y) * u_metrics.xyxy + texcoord.xyxy;
vec4 c;
c.x = sampleOffset(u_texture, coords.xy, ivec2(-1, 0)).g;
c.y = sampleOffset(u_texture, coords.xy, ivec2( 0, -1)).r;
c.zw = sampleOffset(u_texture, coords.zw, ivec2( 1, 0)).gr;
vec2 cc = vec2(2.0) * c.xz + c.yw;
if(d.z >= 0.9) cc.x = 0.0;
if(d.w >= 0.9) cc.y = 0.0;
weights += areaDiag(d.xy, cc, 0.0).gr;
}
return weights;
}
#endif
//---- horizontal / vertical searches ----
//scan the search texture for the real end of the line
float searchLength(vec2 e, float offset){
//the texture is 66x33 but packed into 64x16, the two halves store left and right variants
vec2 scale = SMAA_SEARCHTEX_SIZE * vec2(0.5, -1.0);
vec2 bias = SMAA_SEARCHTEX_SIZE * vec2(offset, 1.0);
//scale and bias to access texel centers
scale += vec2(-1.0, 1.0);
bias += vec2(0.5, -0.5);
//convert from pixel coordinates to texcoords
scale *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
bias *= 1.0 / SMAA_SEARCHTEX_PACKED_SIZE;
return textureLod(u_search, scale * e + bias, 0.0).r;
}
float searchXLeft(vec2 texcoord, float end){
//bilinear filtering lets us check two edges per fetch, hence the step of 2 pixels
vec2 e = vec2(0.0, 1.0);
while(texcoord.x > end && e.g > 0.8281 && e.r == 0.0){
e = textureLod(u_texture, texcoord, 0.0).rg;
texcoord = -vec2(2.0, 0.0) * u_metrics.xy + texcoord;
}
float offset = -(255.0 / 127.0) * searchLength(e, 0.0) + 3.25;
return u_metrics.x * offset + texcoord.x;
}
float searchXRight(vec2 texcoord, float end){
vec2 e = vec2(0.0, 1.0);
while(texcoord.x < end && e.g > 0.8281 && e.r == 0.0){
e = textureLod(u_texture, texcoord, 0.0).rg;
texcoord = vec2(2.0, 0.0) * u_metrics.xy + texcoord;
}
float offset = -(255.0 / 127.0) * searchLength(e, 0.5) + 3.25;
return -u_metrics.x * offset + texcoord.x;
}
float searchYUp(vec2 texcoord, float end){
vec2 e = vec2(1.0, 0.0);
while(texcoord.y > end && e.r > 0.8281 && e.g == 0.0){
e = textureLod(u_texture, texcoord, 0.0).rg;
texcoord = -vec2(0.0, 2.0) * u_metrics.xy + texcoord;
}
float offset = -(255.0 / 127.0) * searchLength(e.gr, 0.0) + 3.25;
return u_metrics.y * offset + texcoord.y;
}
float searchYDown(vec2 texcoord, float end){
vec2 e = vec2(1.0, 0.0);
while(texcoord.y < end && e.r > 0.8281 && e.g == 0.0){
e = textureLod(u_texture, texcoord, 0.0).rg;
texcoord = vec2(0.0, 2.0) * u_metrics.xy + texcoord;
}
float offset = -(255.0 / 127.0) * searchLength(e.gr, 0.5) + 3.25;
return -u_metrics.y * offset + texcoord.y;
}
//area lookup for orthogonal patterns
vec2 area(vec2 dist, float e1, float e2, float offset){
//rounding prevents precision errors of bilinear filtering
vec2 texcoord = vec2(SMAA_AREATEX_MAX_DISTANCE) * round(4.0 * vec2(e1, e2)) + dist;
//move to texel centers
texcoord = SMAA_AREATEX_PIXEL_SIZE * texcoord + 0.5 * SMAA_AREATEX_PIXEL_SIZE;
//subsample offset
texcoord.y = SMAA_AREATEX_SUBTEX_SIZE * offset + texcoord.y;
return textureLod(u_area, texcoord, 0.0).rg;
}
//---- corner detection ----
void detectHorizontalCornerPattern(inout vec2 weights, vec4 texcoord, vec2 d){
#ifndef SMAA_DISABLE_CORNER_DETECTION
vec2 leftRight = step(d.xy, d.yx);
vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
rounding /= leftRight.x + leftRight.y; //reduce blending for pixels in the center of a line
vec2 factor = vec2(1.0);
factor.x -= rounding.x * sampleOffset(u_texture, texcoord.xy, ivec2(0, 1)).r;
factor.x -= rounding.y * sampleOffset(u_texture, texcoord.zw, ivec2(1, 1)).r;
factor.y -= rounding.x * sampleOffset(u_texture, texcoord.xy, ivec2(0, -2)).r;
factor.y -= rounding.y * sampleOffset(u_texture, texcoord.zw, ivec2(1, -2)).r;
weights *= clamp(factor, 0.0, 1.0);
#endif
}
void detectVerticalCornerPattern(inout vec2 weights, vec4 texcoord, vec2 d){
#ifndef SMAA_DISABLE_CORNER_DETECTION
vec2 leftRight = step(d.xy, d.yx);
vec2 rounding = (1.0 - SMAA_CORNER_ROUNDING_NORM) * leftRight;
rounding /= leftRight.x + leftRight.y;
vec2 factor = vec2(1.0);
factor.x -= rounding.x * sampleOffset(u_texture, texcoord.xy, ivec2( 1, 0)).g;
factor.x -= rounding.y * sampleOffset(u_texture, texcoord.zw, ivec2( 1, 1)).g;
factor.y -= rounding.x * sampleOffset(u_texture, texcoord.xy, ivec2(-2, 0)).g;
factor.y -= rounding.y * sampleOffset(u_texture, texcoord.zw, ivec2(-2, 1)).g;
weights *= clamp(factor, 0.0, 1.0);
#endif
}
void main(){
vec2 texcoord = v_texCoords;
vec4 weights = vec4(0.0);
vec2 e = texture2D(u_texture, texcoord).rg;
if(e.g > 0.0){ //edge at north
#ifndef SMAA_DISABLE_DIAG_DETECTION
//diagonals have both north and west edges, so searching for them in one of the boundaries is enough
weights.rg = calculateDiagWeights(texcoord, e);
//skip horizontal/vertical processing if a diagonal pattern was found
if(weights.r == -weights.g){ //weights.r + weights.g == 0.0
#endif
vec2 d;
//find the distance to the left
vec3 coords;
coords.x = searchXLeft(v_offset0.xy, v_offset2.x);
coords.y = v_offset1.y; //offset[1].y = texcoord.y - 0.25 * texel.y (interpolated by hardware)
d.x = coords.x;
//fetch the left crossing edges, use bilinear filtering to fetch two edges in a single tap
float e1 = textureLod(u_texture, coords.xy, 0.0).r;
//find the distance to the right
coords.z = searchXRight(v_offset0.zw, v_offset2.y);
d.y = coords.z;
//convert distances from texcoords to pixels; abs() because of the round() offsets
d = abs(round(u_metrics.zz * d - v_pixcoord.xx));
//square root to get more accuracy for small distances (the area texture is compressed)
vec2 sqrt_d = sqrt(d);
//fetch the right crossing edges
float e2 = sampleOffset(u_texture, coords.zy, ivec2(1, 0)).r;
//pattern-specific area
weights.rg = area(sqrt_d, e1, e2, 0.0);
//fix corners
coords.y = texcoord.y;
detectHorizontalCornerPattern(weights.rg, coords.xyzy, d);
#ifndef SMAA_DISABLE_DIAG_DETECTION
}else{
e.r = 0.0; //skip vertical processing
}
#endif
}
if(e.r > 0.0){ //edge at west
vec2 d;
//find the distance to the top
vec3 coords;
coords.y = searchYUp(v_offset1.xy, v_offset2.z);
coords.x = v_offset0.x; //offset[0].x = texcoord.x - 0.25 * texel.x
d.x = coords.y;
float e1 = textureLod(u_texture, coords.xy, 0.0).g;
//find the distance to the bottom
coords.z = searchYDown(v_offset1.zw, v_offset2.w);
d.y = coords.z;
d = abs(round(u_metrics.ww * d - v_pixcoord.yy));
vec2 sqrt_d = sqrt(d);
float e2 = sampleOffset(u_texture, coords.xz, ivec2(0, 1)).g;
weights.ba = area(sqrt_d, e1, e2, 0.0);
coords.x = texcoord.x;
detectVerticalCornerPattern(weights.ba, coords.xyxz, d);
}
fragColor = weights;
}
+31
View File
@@ -0,0 +1,31 @@
//SMAA pass 2 (blending weight calculation) - vertex stage. Port of SMAABlendingWeightCalculationVS.
//u_metrics = (1/width, 1/height, width, height).
//SMAA_MAX_SEARCH_STEPS must match the value in smaa-weights.frag.
#define SMAA_MAX_SEARCH_STEPS 16
attribute vec4 a_position;
attribute vec2 a_texCoord0;
uniform vec4 u_metrics;
varying vec2 v_texCoords;
varying vec2 v_pixcoord;
varying vec4 v_offset0;
varying vec4 v_offset1;
varying vec4 v_offset2;
void main(){
v_texCoords = a_texCoord0;
v_pixcoord = a_texCoord0 * u_metrics.zw;
//search offsets, quarter/eighth pixel biases are part of the algorithm
v_offset0 = u_metrics.xyxy * vec4(-0.25, -0.125, 1.25, -0.125) + a_texCoord0.xyxy;
v_offset1 = u_metrics.xyxy * vec4(-0.125, -0.25, -0.125, 1.25) + a_texCoord0.xyxy;
//search limits
v_offset2 = u_metrics.xxyy * (vec4(-2.0, 2.0, -2.0, 2.0) * float(SMAA_MAX_SEARCH_STEPS))
+ vec4(v_offset0.xz, v_offset1.yw);
gl_Position = a_position;
}
+6
View File
@@ -33,6 +33,7 @@ public class Renderer implements ApplicationListener{
public final OverlayRenderer overlays = new OverlayRenderer();
public final LightRenderer lights = new LightRenderer();
public final Pixelator pixelator = new Pixelator();
public final SMAA smaa = new SMAA();
public PlanetRenderer planets;
public @Nullable Bloom bloom;
@@ -218,6 +219,10 @@ public class Renderer implements ApplicationListener{
if(renderer.pixelate){
pixelator.drawPixelate();
}else if(smaa.enabled()){
smaa.begin();
draw();
smaa.end();
}else{
draw();
}
@@ -262,6 +267,7 @@ public class Renderer implements ApplicationListener{
@Override
public void dispose(){
smaa.dispose();
Events.fire(new DisposeEvent());
}
+155
View File
@@ -0,0 +1,155 @@
package mindustry.graphics;
import arc.graphics.*;
import arc.graphics.gl.*;
import arc.util.*;
import static arc.Core.*;
/**
* Subpixel Morphological Anti-Aliasing (SMAA 1x), post-process anti-aliasing.
* Ported from https://github.com/iryoku/smaa (MIT), see assets/shaders/smaa-*.
* <p>
* Usage: call {@link #begin()} before rendering the scene and {@link #end()} afterwards.
*/
public class SMAA implements Disposable{
private boolean initialized, failed, active;
private FrameBuffer sceneBuffer, edgesBuffer, weightsBuffer;
private Texture areaTex, searchTex;
private Shader edgesShader, weightsShader, blendShader;
/** @return whether SMAA should be applied this frame. */
public boolean enabled(){
return settings.getBool("smaa") && !failed;
}
private void init(){
initialized = true;
try{
sceneBuffer = new FrameBuffer();
edgesBuffer = new FrameBuffer();
weightsBuffer = new FrameBuffer();
//all three buffers are sampled with bilinear filtering by the shaders (the searches and the final blend rely on it)
for(FrameBuffer buffer : new FrameBuffer[]{sceneBuffer, edgesBuffer, weightsBuffer}){
buffer.texture.setFilter(TextureFilter.linear, TextureFilter.linear);
buffer.texture.setWrap(TextureWrap.clampToEdge);
}
areaTex = new Texture(Shaders.getShaderFi("smaa-area.png"));
areaTex.setFilter(TextureFilter.linear, TextureFilter.linear);
areaTex.setWrap(TextureWrap.clampToEdge);
searchTex = new Texture(Shaders.getShaderFi("smaa-search.png"));
searchTex.setFilter(TextureFilter.linear, TextureFilter.linear);
searchTex.setWrap(TextureWrap.clampToEdge);
edgesShader = new SMAAShader("smaa-edges"){
@Override
public void apply(){
setUniformi("u_texture", 0);
applyMetrics(this, sceneBuffer);
}
};
weightsShader = new SMAAShader("smaa-weights"){
@Override
public void apply(){
areaTex.bind(1);
searchTex.bind(2);
Gl.activeTexture(Gl.texture0);
setUniformi("u_texture", 0);
setUniformi("u_area", 1);
setUniformi("u_search", 2);
applyMetrics(this, edgesBuffer);
}
};
blendShader = new SMAAShader("smaa-blend"){
@Override
public void apply(){
weightsBuffer.texture.bind(1);
Gl.activeTexture(Gl.texture0);
setUniformi("u_texture", 0);
setUniformi("u_blend", 1);
applyMetrics(this, sceneBuffer);
}
};
}catch(Throwable t){
failed = true;
Log.err("Failed to initialize SMAA, disabling it.", t);
dispose();
}
}
private static void applyMetrics(Shader shader, FrameBuffer buffer){
shader.setUniformf("u_metrics", 1f / buffer.width, 1f / buffer.height, buffer.width, buffer.height);
}
/** Starts rendering the scene into the SMAA input buffer. Must be paired with {@link #end()}. */
public void begin(){
if(failed) return;
if(!initialized) init();
if(failed) return;
int w = graphics.getWidth(), h = graphics.getHeight();
sceneBuffer.resize(w, h);
edgesBuffer.resize(w, h);
weightsBuffer.resize(w, h);
active = true;
sceneBuffer.begin(Color.clear);
}
/** Stops rendering the scene, runs all SMAA passes and draws the result to the previously bound framebuffer. */
public void end(){
if(!active) return;
active = false;
sceneBuffer.end();
//every pass writes raw data, so blending must be off
Blending.disabled.apply();
//pass 1: edges. this pass discards pixels without edges, so the target has to be cleared.
edgesBuffer.begin(Color.clear);
sceneBuffer.blit(edgesShader);
edgesBuffer.end();
//pass 2: blending weights
weightsBuffer.begin(Color.clear);
edgesBuffer.blit(weightsShader);
weightsBuffer.end();
//pass 3: blend the scene using the weights, straight to the screen
sceneBuffer.blit(blendShader);
Blending.normal.apply();
}
@Override
public void dispose(){
if(sceneBuffer != null) sceneBuffer.dispose();
if(edgesBuffer != null) edgesBuffer.dispose();
if(weightsBuffer != null) weightsBuffer.dispose();
if(areaTex != null) areaTex.dispose();
if(searchTex != null) searchTex.dispose();
if(edgesShader != null) edgesShader.dispose();
if(weightsShader != null) weightsShader.dispose();
if(blendShader != null) blendShader.dispose();
sceneBuffer = edgesBuffer = weightsBuffer = null;
areaTex = searchTex = null;
edgesShader = weightsShader = blendShader = null;
initialized = false;
}
/** Loads smaa-[name].vert and smaa-[name].frag from the shaders folder. */
private static class SMAAShader extends Shader{
SMAAShader(String name){
super(Shaders.getShaderFi(name + ".vert"), Shaders.getShaderFi(name + ".frag"));
}
}
}
@@ -8,6 +8,7 @@ import arc.graphics.gl.*;
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.*;
import mindustry.game.EventType.*;
import mindustry.graphics.*;
import mindustry.graphics.g3d.PlanetGrid.*;
@@ -52,6 +53,8 @@ public class PlanetRenderer implements Disposable{
/** Render the entire planet scene to the screen. */
public void render(PlanetParams params){
boolean aa = Vars.renderer.smaa.enabled();
if(aa) Vars.renderer.smaa.begin();
Draw.flush();
Gl.clear(Gl.depthBufferBit);
Gl.enable(Gl.depthTest);
@@ -142,6 +145,7 @@ public class PlanetRenderer implements Disposable{
Gl.disable(Gl.depthTest);
cam.update();
if(aa) Vars.renderer.smaa.end();
}
public void renderPlanet(Planet planet, PlanetParams params){
@@ -501,6 +501,7 @@ public class SettingsMenuDialog extends BaseDialog{
}
}
graphics.checkPref("smaa", false);
graphics.checkPref("effects", true);
graphics.checkPref("atmosphere", true);
graphics.checkPref("drawlight", true);