Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 482acf3

Browse files
authored
Merge pull request #348 from UnityTech/hardware_antialias
hardware antialias re-supporting
2 parents eb7037d + bff6d04 commit 482acf3

File tree

12 files changed

+90
-17
lines changed

12 files changed

+90
-17
lines changed

Editor/UIWidgetsPanelEditor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class UIWidgetsPanelEditor : RawImageEditor {
99
public override void OnInspectorGUI() {
1010
base.OnInspectorGUI();
1111
var pixelRatioProperty = this.serializedObject.FindProperty("devicePixelRatioOverride");
12+
var antiAliasingProperty = this.serializedObject.FindProperty("hardwareAntiAliasing");
1213
EditorGUILayout.PropertyField(pixelRatioProperty);
14+
EditorGUILayout.PropertyField(antiAliasingProperty);
1315
this.serializedObject.ApplyModifiedProperties();
1416
}
1517
}

Runtime/editor/editor_window.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public override GUIContent titleContent {
9191
protected override float queryDevicePixelRatio() {
9292
return EditorGUIUtility.pixelsPerPoint;
9393
}
94+
95+
protected override int queryAntiAliasing() {
96+
return defaultAntiAliasing;
97+
}
9498

9599
protected override Vector2 queryWindowSize() {
96100
return this.editorWindow.position.size;
@@ -188,6 +192,7 @@ public void onViewMetricsChanged() {
188192

189193
public void OnEnable() {
190194
this._devicePixelRatio = this.queryDevicePixelRatio();
195+
this._antiAliasing = this.queryAntiAliasing();
191196
this.updatePhysicalSize();
192197
this.updateSafeArea();
193198
D.assert(this._surface == null);
@@ -270,6 +275,10 @@ protected bool displayMetricsChanged() {
270275
if (this._devicePixelRatio != this.queryDevicePixelRatio()) {
271276
return true;
272277
}
278+
279+
if (this._antiAliasing != this.queryAntiAliasing()) {
280+
return true;
281+
}
273282

274283
var size = this.queryWindowSize();
275284
if (this._lastWindowWidth != size.x
@@ -289,6 +298,7 @@ public virtual void OnGUI(Event evt = null) {
289298
using (this.getScope()) {
290299
if (this.displayMetricsChanged()) {
291300
this._devicePixelRatio = this.queryDevicePixelRatio();
301+
this._antiAliasing = this.queryAntiAliasing();
292302

293303
var size = this.queryWindowSize();
294304
this._lastWindowWidth = size.x;
@@ -313,6 +323,7 @@ public virtual GUIContent titleContent {
313323
}
314324

315325
protected abstract float queryDevicePixelRatio();
326+
protected abstract int queryAntiAliasing();
316327
protected abstract Vector2 queryWindowSize();
317328

318329
protected virtual Surface createSurface() {
@@ -509,6 +520,7 @@ public override void render(Scene scene) {
509520

510521
layerTree.frameSize = this._physicalSize;
511522
layerTree.devicePixelRatio = this._devicePixelRatio;
523+
layerTree.antiAliasing = this._antiAliasing;
512524
this._rasterizer.draw(layerTree);
513525
}
514526

Runtime/editor/rasterizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool _drawToSurface(LayerTree layerTree) {
6363
D.assert(this._surface != null);
6464

6565
var frame = this._surface.acquireFrame(
66-
layerTree.frameSize, layerTree.devicePixelRatio);
66+
layerTree.frameSize, layerTree.devicePixelRatio, layerTree.antiAliasing);
6767
if (frame == null) {
6868
return false;
6969
}

Runtime/editor/surface.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool _performSubmit() {
5454
}
5555

5656
public interface Surface : IDisposable {
57-
SurfaceFrame acquireFrame(Size size, float devicePixelRatio);
57+
SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing);
5858

5959
MeshPool getMeshPool();
6060
}
@@ -104,8 +104,8 @@ public WindowSurfaceImpl(DrawToTargetFunc drawToTargetFunc = null) {
104104
this._drawToTargetFunc = drawToTargetFunc;
105105
}
106106

107-
public SurfaceFrame acquireFrame(Size size, float devicePixelRatio) {
108-
this._createOrUpdateRenderTexture(size, devicePixelRatio);
107+
public SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing) {
108+
this._createOrUpdateRenderTexture(size, devicePixelRatio, antiAliasing);
109109

110110
return new SurfaceFrame(this._surface,
111111
(frame, canvas) => this._presentSurface(canvas));
@@ -151,10 +151,11 @@ protected bool _presentSurface(Canvas canvas) {
151151
return true;
152152
}
153153

154-
void _createOrUpdateRenderTexture(Size size, float devicePixelRatio) {
154+
void _createOrUpdateRenderTexture(Size size, float devicePixelRatio, int antiAliasing) {
155155
if (this._surface != null
156156
&& this._surface.size == size
157157
&& this._surface.devicePixelRatio == devicePixelRatio
158+
&& this._surface.antiAliasing == antiAliasing
158159
&& this._surface.getRenderTexture() != null) {
159160
return;
160161
}
@@ -164,14 +165,16 @@ void _createOrUpdateRenderTexture(Size size, float devicePixelRatio) {
164165
this._surface = null;
165166
}
166167

167-
this._surface = new GrSurface(size, devicePixelRatio, this._meshPool);
168+
this._surface = new GrSurface(size, devicePixelRatio, antiAliasing, this._meshPool);
168169
}
169170
}
170171

171172
public class GrSurface : IDisposable {
172173
public readonly Size size;
173174

174175
public readonly float devicePixelRatio;
176+
177+
public readonly int antiAliasing;
175178

176179
readonly MeshPool _meshPool;
177180

@@ -192,16 +195,21 @@ public Canvas getCanvas() {
192195
return this._canvas;
193196
}
194197

195-
public GrSurface(Size size, float devicePixelRatio, MeshPool meshPool) {
198+
public GrSurface(Size size, float devicePixelRatio, int antiAliasing, MeshPool meshPool) {
196199
this.size = size;
197200
this.devicePixelRatio = devicePixelRatio;
201+
this.antiAliasing = antiAliasing;
198202

199203
var desc = new RenderTextureDescriptor(
200204
(int) this.size.width, (int) this.size.height,
201205
RenderTextureFormat.Default, 24) {
202206
useMipMap = false,
203207
autoGenerateMips = false,
204208
};
209+
210+
if (antiAliasing != 0) {
211+
desc.msaaSamples = antiAliasing;
212+
}
205213

206214
this._renderTexture = new RenderTexture(desc);
207215
this._renderTexture.hideFlags = HideFlags.HideAndDontSave;

Runtime/engine/UIWidgetsPanel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public override GUIContent titleContent {
6565
protected override float queryDevicePixelRatio() {
6666
return this._uiWidgetsPanel.devicePixelRatio;
6767
}
68+
69+
protected override int queryAntiAliasing() {
70+
return this._uiWidgetsPanel.antiAliasing;
71+
}
6872

6973
protected override Vector2 queryWindowSize() {
7074
var rect = this._uiWidgetsPanel.rectTransform.rect;
@@ -106,7 +110,13 @@ public class UIWidgetsPanel : RawImage, IPointerDownHandler, IPointerUpHandler,
106110
IPointerEnterHandler, IPointerExitHandler, WindowHost {
107111
static Event _repaintEvent;
108112

113+
[Tooltip("set to zero if you want to use the default device pixel ratio of the target platforms; otherwise the " +
114+
"device pixel ratio will be forced to the given value on all devices.")]
109115
[SerializeField] protected float devicePixelRatioOverride;
116+
117+
[Tooltip("set to true will enable the hardware anti-alias feature, which will improve the appearance of the UI greatly but " +
118+
"making it much slower. Enable it only when seriously required.")]
119+
[SerializeField] protected bool hardwareAntiAliasing = false;
110120
WindowAdapter _windowAdapter;
111121
Texture _texture;
112122
Vector2 _lastMouseMove;
@@ -164,6 +174,10 @@ public float devicePixelRatio {
164174
: this._displayMetrics.devicePixelRatio;
165175
}
166176
}
177+
178+
public int antiAliasing {
179+
get { return this.hardwareAntiAliasing ? Window.defaultAntiAliasing : 0; }
180+
}
167181

168182
public WindowPadding viewPadding {
169183
get { return this._displayMetrics.viewPadding; }

Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ RenderLayer _createMaskLayer(RenderLayer parentLayer, uiRect maskBounds,
412412
width: textureWidth,
413413
height: textureHeight,
414414
layerBounds: maskBounds,
415-
filterMode: FilterMode.Bilinear
415+
filterMode: FilterMode.Bilinear,
416+
noMSAA: true
416417
);
417418

418419
parentLayer.addLayer(maskLayer);
@@ -452,7 +453,8 @@ RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigmaX, float sigmaY,
452453
width: textureWidth,
453454
height: textureHeight,
454455
layerBounds: maskLayer.layerBounds,
455-
filterMode: FilterMode.Bilinear
456+
filterMode: FilterMode.Bilinear,
457+
noMSAA: true
456458
);
457459

458460
parentLayer.addLayer(blurXLayer);
@@ -462,7 +464,8 @@ RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigmaX, float sigmaY,
462464
width: textureWidth,
463465
height: textureHeight,
464466
layerBounds: maskLayer.layerBounds,
465-
filterMode: FilterMode.Bilinear
467+
filterMode: FilterMode.Bilinear,
468+
noMSAA: true
466469
);
467470

468471
parentLayer.addLayer(blurYLayer);
@@ -1115,6 +1118,10 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
11151118
useMipMap = false,
11161119
autoGenerateMips = false
11171120
};
1121+
1122+
if (this._renderTexture.antiAliasing != 0 && !subLayer.noMSAA) {
1123+
desc.msaaSamples = this._renderTexture.antiAliasing;
1124+
}
11181125

11191126
cmdBuf.GetTemporaryRT(subLayer.rtID, desc, subLayer.filterMode);
11201127
this._drawLayer(subLayer, cmdBuf);

Runtime/ui/renderer/cmdbufferCanvas/rendering/render_layer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class RenderLayer : PoolObject {
99
public int width;
1010
public int height;
1111
public FilterMode filterMode = FilterMode.Bilinear;
12+
public bool noMSAA = false;
1213
public uiRect layerBounds;
1314
public uiPaint? layerPaint;
1415
public readonly List<RenderCmd> draws = new List<RenderCmd>(128);
@@ -38,13 +39,15 @@ public Vector4 viewport {
3839

3940
public static RenderLayer create(int rtID = 0, int width = 0, int height = 0,
4041
FilterMode filterMode = FilterMode.Bilinear,
42+
bool noMSAA = false,
4143
uiRect? layerBounds = null, uiPaint? layerPaint = null, bool ignoreClip = true) {
4244
D.assert(layerBounds != null);
4345
var newLayer = ObjectPool<RenderLayer>.alloc();
4446
newLayer.rtID = rtID;
4547
newLayer.width = width;
4648
newLayer.height = height;
4749
newLayer.filterMode = filterMode;
50+
newLayer.noMSAA = noMSAA;
4851
newLayer.layerBounds = layerBounds.Value;
4952
newLayer.layerPaint = layerPaint;
5053
newLayer.ignoreClip = ignoreClip;

Runtime/ui/renderer/compositeCanvas/flow/layer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Unity.UIWidgets.flow {
44
public class PrerollContext {
55
public RasterCache rasterCache;
66
public float devicePixelRatio;
7+
public int antiAliasing;
78
public Rect cullRect;
89
public Stopwatch frameTime;
910
}

Runtime/ui/renderer/compositeCanvas/flow/layer_tree.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ public float devicePixelRatio {
2222
get { return this._devicePixelRatio; }
2323
set { this._devicePixelRatio = value; }
2424
}
25+
26+
int _antiAliasing;
27+
28+
public int antiAliasing {
29+
get { return this._antiAliasing; }
30+
set { this._antiAliasing = value; }
31+
}
2532

2633
static readonly Matrix3 _identityMatrix = Matrix3.I();
2734

2835
public void preroll(CompositorContext.ScopedFrame frame, bool ignoreRasterCache = false) {
2936
var prerollContext = new PrerollContext {
3037
rasterCache = ignoreRasterCache ? null : frame.context().rasterCache(),
3138
devicePixelRatio = frame.canvas().getDevicePixelRatio(),
39+
antiAliasing = this.antiAliasing,
3240
cullRect = Rect.largest,
3341
frameTime = frame.context().frameTime()
3442
};

Runtime/ui/renderer/compositeCanvas/flow/picture_layer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public override void preroll(PrerollContext context, Matrix3 matrix) {
3838
ctm[5] = ctm[5].alignToPixel(context.devicePixelRatio);
3939

4040
this._rasterCacheResult = context.rasterCache.getPrerolledImage(
41-
this._picture, ctm, context.devicePixelRatio, this._isComplex,
41+
this._picture, ctm, context.devicePixelRatio, context.antiAliasing, this._isComplex,
4242
this._willChange);
4343
}
4444
else {

0 commit comments

Comments
 (0)