Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Editor/UIWidgetsPanelEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public class UIWidgetsPanelEditor : RawImageEditor {
public override void OnInspectorGUI() {
base.OnInspectorGUI();
var pixelRatioProperty = this.serializedObject.FindProperty("devicePixelRatioOverride");
var antiAliasingProperty = this.serializedObject.FindProperty("hardwareAntiAliasing");
EditorGUILayout.PropertyField(pixelRatioProperty);
EditorGUILayout.PropertyField(antiAliasingProperty);
this.serializedObject.ApplyModifiedProperties();
}
}
Expand Down
12 changes: 12 additions & 0 deletions Runtime/editor/editor_window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public override GUIContent titleContent {
protected override float queryDevicePixelRatio() {
return EditorGUIUtility.pixelsPerPoint;
}

protected override int queryAntiAliasing() {
return defaultAntiAliasing;
}

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

public void OnEnable() {
this._devicePixelRatio = this.queryDevicePixelRatio();
this._antiAliasing = this.queryAntiAliasing();
this.updatePhysicalSize();
this.updateSafeArea();
D.assert(this._surface == null);
Expand Down Expand Up @@ -270,6 +275,10 @@ protected bool displayMetricsChanged() {
if (this._devicePixelRatio != this.queryDevicePixelRatio()) {
return true;
}

if (this._antiAliasing != this.queryAntiAliasing()) {
return true;
}

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

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

protected abstract float queryDevicePixelRatio();
protected abstract int queryAntiAliasing();
protected abstract Vector2 queryWindowSize();

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

layerTree.frameSize = this._physicalSize;
layerTree.devicePixelRatio = this._devicePixelRatio;
layerTree.antiAliasing = this._antiAliasing;
this._rasterizer.draw(layerTree);
}

Expand Down
2 changes: 1 addition & 1 deletion Runtime/editor/rasterizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ bool _drawToSurface(LayerTree layerTree) {
D.assert(this._surface != null);

var frame = this._surface.acquireFrame(
layerTree.frameSize, layerTree.devicePixelRatio);
layerTree.frameSize, layerTree.devicePixelRatio, layerTree.antiAliasing);
if (frame == null) {
return false;
}
Expand Down
20 changes: 14 additions & 6 deletions Runtime/editor/surface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool _performSubmit() {
}

public interface Surface : IDisposable {
SurfaceFrame acquireFrame(Size size, float devicePixelRatio);
SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing);

MeshPool getMeshPool();
}
Expand Down Expand Up @@ -104,8 +104,8 @@ public WindowSurfaceImpl(DrawToTargetFunc drawToTargetFunc = null) {
this._drawToTargetFunc = drawToTargetFunc;
}

public SurfaceFrame acquireFrame(Size size, float devicePixelRatio) {
this._createOrUpdateRenderTexture(size, devicePixelRatio);
public SurfaceFrame acquireFrame(Size size, float devicePixelRatio, int antiAliasing) {
this._createOrUpdateRenderTexture(size, devicePixelRatio, antiAliasing);

return new SurfaceFrame(this._surface,
(frame, canvas) => this._presentSurface(canvas));
Expand Down Expand Up @@ -151,10 +151,11 @@ protected bool _presentSurface(Canvas canvas) {
return true;
}

void _createOrUpdateRenderTexture(Size size, float devicePixelRatio) {
void _createOrUpdateRenderTexture(Size size, float devicePixelRatio, int antiAliasing) {
if (this._surface != null
&& this._surface.size == size
&& this._surface.devicePixelRatio == devicePixelRatio
&& this._surface.antiAliasing == antiAliasing
&& this._surface.getRenderTexture() != null) {
return;
}
Expand All @@ -164,14 +165,16 @@ void _createOrUpdateRenderTexture(Size size, float devicePixelRatio) {
this._surface = null;
}

this._surface = new GrSurface(size, devicePixelRatio, this._meshPool);
this._surface = new GrSurface(size, devicePixelRatio, antiAliasing, this._meshPool);
}
}

public class GrSurface : IDisposable {
public readonly Size size;

public readonly float devicePixelRatio;

public readonly int antiAliasing;

readonly MeshPool _meshPool;

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

public GrSurface(Size size, float devicePixelRatio, MeshPool meshPool) {
public GrSurface(Size size, float devicePixelRatio, int antiAliasing, MeshPool meshPool) {
this.size = size;
this.devicePixelRatio = devicePixelRatio;
this.antiAliasing = antiAliasing;

var desc = new RenderTextureDescriptor(
(int) this.size.width, (int) this.size.height,
RenderTextureFormat.Default, 24) {
useMipMap = false,
autoGenerateMips = false,
};

if (antiAliasing != 0) {
desc.msaaSamples = antiAliasing;
}

this._renderTexture = new RenderTexture(desc);
this._renderTexture.hideFlags = HideFlags.HideAndDontSave;
Expand Down
14 changes: 14 additions & 0 deletions Runtime/engine/UIWidgetsPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public override GUIContent titleContent {
protected override float queryDevicePixelRatio() {
return this._uiWidgetsPanel.devicePixelRatio;
}

protected override int queryAntiAliasing() {
return this._uiWidgetsPanel.antiAliasing;
}

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

[Tooltip("set to zero if you want to use the default device pixel ratio of the target platforms; otherwise the " +
"device pixel ratio will be forced to the given value on all devices.")]
[SerializeField] protected float devicePixelRatioOverride;

[Tooltip("set to true will enable the hardware anti-alias feature, which will improve the appearance of the UI greatly but " +
"making it much slower. Enable it only when seriously required.")]
[SerializeField] protected bool hardwareAntiAliasing = false;
WindowAdapter _windowAdapter;
Texture _texture;
Vector2 _lastMouseMove;
Expand Down Expand Up @@ -164,6 +174,10 @@ public float devicePixelRatio {
: this._displayMetrics.devicePixelRatio;
}
}

public int antiAliasing {
get { return this.hardwareAntiAliasing ? Window.defaultAntiAliasing : 0; }
}

public WindowPadding viewPadding {
get { return this._displayMetrics.viewPadding; }
Expand Down
13 changes: 10 additions & 3 deletions Runtime/ui/renderer/cmdbufferCanvas/rendering/canvas_impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ RenderLayer _createMaskLayer(RenderLayer parentLayer, uiRect maskBounds,
width: textureWidth,
height: textureHeight,
layerBounds: maskBounds,
filterMode: FilterMode.Bilinear
filterMode: FilterMode.Bilinear,
noMSAA: true
);

parentLayer.addLayer(maskLayer);
Expand Down Expand Up @@ -452,7 +453,8 @@ RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigmaX, float sigmaY,
width: textureWidth,
height: textureHeight,
layerBounds: maskLayer.layerBounds,
filterMode: FilterMode.Bilinear
filterMode: FilterMode.Bilinear,
noMSAA: true
);

parentLayer.addLayer(blurXLayer);
Expand All @@ -462,7 +464,8 @@ RenderLayer _createBlurLayer(RenderLayer maskLayer, float sigmaX, float sigmaY,
width: textureWidth,
height: textureHeight,
layerBounds: maskLayer.layerBounds,
filterMode: FilterMode.Bilinear
filterMode: FilterMode.Bilinear,
noMSAA: true
);

parentLayer.addLayer(blurYLayer);
Expand Down Expand Up @@ -1115,6 +1118,10 @@ void _drawLayer(RenderLayer layer, CommandBuffer cmdBuf) {
useMipMap = false,
autoGenerateMips = false
};

if (this._renderTexture.antiAliasing != 0 && !subLayer.noMSAA) {
desc.msaaSamples = this._renderTexture.antiAliasing;
}

cmdBuf.GetTemporaryRT(subLayer.rtID, desc, subLayer.filterMode);
this._drawLayer(subLayer, cmdBuf);
Expand Down
3 changes: 3 additions & 0 deletions Runtime/ui/renderer/cmdbufferCanvas/rendering/render_layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal class RenderLayer : PoolObject {
public int width;
public int height;
public FilterMode filterMode = FilterMode.Bilinear;
public bool noMSAA = false;
public uiRect layerBounds;
public uiPaint? layerPaint;
public readonly List<RenderCmd> draws = new List<RenderCmd>(128);
Expand Down Expand Up @@ -38,13 +39,15 @@ public Vector4 viewport {

public static RenderLayer create(int rtID = 0, int width = 0, int height = 0,
FilterMode filterMode = FilterMode.Bilinear,
bool noMSAA = false,
uiRect? layerBounds = null, uiPaint? layerPaint = null, bool ignoreClip = true) {
D.assert(layerBounds != null);
var newLayer = ObjectPool<RenderLayer>.alloc();
newLayer.rtID = rtID;
newLayer.width = width;
newLayer.height = height;
newLayer.filterMode = filterMode;
newLayer.noMSAA = noMSAA;
newLayer.layerBounds = layerBounds.Value;
newLayer.layerPaint = layerPaint;
newLayer.ignoreClip = ignoreClip;
Expand Down
1 change: 1 addition & 0 deletions Runtime/ui/renderer/compositeCanvas/flow/layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Unity.UIWidgets.flow {
public class PrerollContext {
public RasterCache rasterCache;
public float devicePixelRatio;
public int antiAliasing;
public Rect cullRect;
public Stopwatch frameTime;
}
Expand Down
8 changes: 8 additions & 0 deletions Runtime/ui/renderer/compositeCanvas/flow/layer_tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ public float devicePixelRatio {
get { return this._devicePixelRatio; }
set { this._devicePixelRatio = value; }
}

int _antiAliasing;

public int antiAliasing {
get { return this._antiAliasing; }
set { this._antiAliasing = value; }
}

static readonly Matrix3 _identityMatrix = Matrix3.I();

public void preroll(CompositorContext.ScopedFrame frame, bool ignoreRasterCache = false) {
var prerollContext = new PrerollContext {
rasterCache = ignoreRasterCache ? null : frame.context().rasterCache(),
devicePixelRatio = frame.canvas().getDevicePixelRatio(),
antiAliasing = this.antiAliasing,
cullRect = Rect.largest,
frameTime = frame.context().frameTime()
};
Expand Down
2 changes: 1 addition & 1 deletion Runtime/ui/renderer/compositeCanvas/flow/picture_layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void preroll(PrerollContext context, Matrix3 matrix) {
ctm[5] = ctm[5].alignToPixel(context.devicePixelRatio);

this._rasterCacheResult = context.rasterCache.getPrerolledImage(
this._picture, ctm, context.devicePixelRatio, this._isComplex,
this._picture, ctm, context.devicePixelRatio, context.antiAliasing, this._isComplex,
this._willChange);
}
else {
Expand Down
21 changes: 15 additions & 6 deletions Runtime/ui/renderer/compositeCanvas/flow/raster_cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void draw(Canvas canvas) {
}

class _RasterCacheKey : IEquatable<_RasterCacheKey> {
internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio) {
internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio, int antiAliasing) {
D.assert(picture != null);
D.assert(matrix != null);
this.picture = picture;
Expand All @@ -70,13 +70,16 @@ internal _RasterCacheKey(Picture picture, Matrix3 matrix, float devicePixelRatio
this.matrix[2] = 0.0f;
this.matrix[5] = 0.0f;
this.devicePixelRatio = devicePixelRatio;
this.antiAliasing = antiAliasing;
}

public readonly Picture picture;

public readonly Matrix3 matrix;

public readonly float devicePixelRatio;

public readonly int antiAliasing;

public bool Equals(_RasterCacheKey other) {
if (ReferenceEquals(null, other)) {
Expand All @@ -89,7 +92,8 @@ public bool Equals(_RasterCacheKey other) {

return Equals(this.picture, other.picture) &&
Equals(this.matrix, other.matrix) &&
this.devicePixelRatio.Equals(other.devicePixelRatio);
this.devicePixelRatio.Equals(other.devicePixelRatio) &&
this.antiAliasing.Equals(other.antiAliasing);
}

public override bool Equals(object obj) {
Expand All @@ -113,6 +117,7 @@ public override int GetHashCode() {
var hashCode = (this.picture != null ? this.picture.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (this.matrix != null ? this.matrix.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ this.devicePixelRatio.GetHashCode();
hashCode = (hashCode * 397) ^ this.antiAliasing.GetHashCode();
return hashCode;
}
}
Expand Down Expand Up @@ -149,7 +154,7 @@ public MeshPool meshPool {
}

public RasterCacheResult getPrerolledImage(
Picture picture, Matrix3 transform, float devicePixelRatio, bool isComplex,
Picture picture, Matrix3 transform, float devicePixelRatio, int antiAliasing, bool isComplex,
bool willChange) {
if (this.threshold == 0) {
return null;
Expand All @@ -163,7 +168,7 @@ public RasterCacheResult getPrerolledImage(
return null;
}

_RasterCacheKey cacheKey = new _RasterCacheKey(picture, transform, devicePixelRatio);
_RasterCacheKey cacheKey = new _RasterCacheKey(picture, transform, devicePixelRatio, antiAliasing);

var entry = this._cache.putIfAbsent(cacheKey, () => new _RasterCacheEntry());

Expand All @@ -177,7 +182,7 @@ public RasterCacheResult getPrerolledImage(
if (entry.image == null) {
D.assert(this._meshPool != null);
entry.image =
this._rasterizePicture(picture, transform, devicePixelRatio, this._meshPool);
this._rasterizePicture(picture, transform, devicePixelRatio, antiAliasing, this._meshPool);
}

return entry.image;
Expand Down Expand Up @@ -222,7 +227,7 @@ static bool _canRasterizePicture(Picture picture) {
}

RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float devicePixelRatio,
MeshPool meshPool) {
int antiAliasing, MeshPool meshPool) {
var bounds = transform.mapRect(picture.paintBounds).roundOut(devicePixelRatio);

var desc = new RenderTextureDescriptor(
Expand All @@ -232,6 +237,10 @@ RasterCacheResult _rasterizePicture(Picture picture, Matrix3 transform, float de
useMipMap = false,
autoGenerateMips = false,
};

if (antiAliasing != 0) {
desc.msaaSamples = antiAliasing;
}

var renderTexture = new RenderTexture(desc);
renderTexture.hideFlags = HideFlags.HideAndDontSave;
Expand Down
9 changes: 9 additions & 0 deletions Runtime/ui/window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,21 @@ public static bool hasInstance {
}

internal static Window _instance;

public const int defaultAntiAliasing = 4;

public float devicePixelRatio {
get { return this._devicePixelRatio; }
}

protected float _devicePixelRatio = 1.0f;

public int antiAliasing {
get { return this._antiAliasing; }
}

protected int _antiAliasing = defaultAntiAliasing;


public Size physicalSize {
get { return this._physicalSize; }
Expand Down