Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use double for compositor types #11768

Merged
merged 2 commits into from
Jun 16, 2023
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
10 changes: 5 additions & 5 deletions samples/ControlCatalog/Pages/CompositionPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ void Update()
{
if(_solidVisual == null)
return;
_solidVisual.Size = new Vector2((float)v.Bounds.Width / 3, (float)v.Bounds.Height / 3);
_solidVisual.Offset = new Vector3((float)v.Bounds.Width / 3, (float)v.Bounds.Height / 3, 0);
_solidVisual.Size = new (v.Bounds.Width / 3, v.Bounds.Height / 3);
_solidVisual.Offset = new (v.Bounds.Width / 3, v.Bounds.Height / 3, 0);
}
v.AttachedToVisualTree += delegate
{
Expand All @@ -164,7 +164,7 @@ void Update()
animation.Direction = PlaybackDirection.Alternate;
_solidVisual.StartAnimation("Color", animation);

_solidVisual.AnchorPoint = new Vector2(0, 0);
_solidVisual.AnchorPoint = new (0, 0);

var scale = _solidVisual.Compositor.CreateVector3KeyFrameAnimation();
scale.Duration = TimeSpan.FromSeconds(5);
Expand Down Expand Up @@ -195,8 +195,8 @@ void Update()
if (_customVisual == null)
return;
var h = (float)Math.Min(v.Bounds.Height, v.Bounds.Width / 3);
_customVisual.Size = new Vector2((float)v.Bounds.Width, h);
_customVisual.Offset = new Vector3(0, (float)(v.Bounds.Height - h) / 2, 0);
_customVisual.Size = new (v.Bounds.Width, h);
_customVisual.Offset = new (0, (v.Bounds.Height - h) / 2, 0);
}
v.AttachedToVisualTree += delegate
{
Expand Down
16 changes: 8 additions & 8 deletions samples/ControlCatalog/Pages/GesturePage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ControlCatalog.Pages
public class GesturePage : UserControl
{
private bool _isInit;
private float _currentScale;
private double _currentScale;

public GesturePage()
{
Expand Down Expand Up @@ -53,7 +53,7 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
if(compositionVisual!= null)
{
_currentScale = 1;
compositionVisual.Scale = new Vector3(1,1,1);
compositionVisual.Scale = new (1,1,1);
compositionVisual.Offset = default;
image.InvalidateMeasure();
}
Expand All @@ -69,7 +69,7 @@ private void SetPinchHandlers(Control? control)
}

_currentScale = 1;
Vector3 currentOffset = default;
Vector3D currentOffset = default;

CompositionVisual? compositionVisual = null;

Expand Down Expand Up @@ -133,11 +133,11 @@ void InitComposition(Control visual)

if (compositionVisual != null && _currentScale != 1)
{
currentOffset += new Vector3((float)e.Delta.X, (float)e.Delta.Y, 0);
currentOffset += new Vector3D(e.Delta.X, e.Delta.Y, 0);

var currentSize = control.Bounds.Size * _currentScale;

currentOffset = new Vector3((float)MathUtilities.Clamp(currentOffset.X, 0, currentSize.Width - control.Bounds.Width),
currentOffset = new Vector3D(MathUtilities.Clamp(currentOffset.X, 0, currentSize.Width - control.Bounds.Width),
(float)MathUtilities.Clamp(currentOffset.Y, 0, currentSize.Height - control.Bounds.Height),
0);

Expand All @@ -157,7 +157,7 @@ private void SetPullHandlers(Control? control, bool inverse)

var ball = control.FindLogicalDescendantOfType<Border>();

Vector3 defaultOffset = default;
Vector3D defaultOffset = default;

CompositionVisual? ballCompositionVisual = null;

Expand All @@ -181,11 +181,11 @@ private void SetPullHandlers(Control? control, bool inverse)

control.AddHandler(Gestures.PullGestureEvent, (s, e) =>
{
Vector3 center = new((float)control.Bounds.Center.X, (float)control.Bounds.Center.Y, 0);
Vector3D center = new((float)control.Bounds.Center.X, (float)control.Bounds.Center.Y, 0);
InitComposition(ball!);
if (ballCompositionVisual != null)
{
ballCompositionVisual.Offset = defaultOffset + new System.Numerics.Vector3((float)e.Delta.X * 0.4f, (float)e.Delta.Y * 0.4f, 0) * (inverse ? -1 : 1);
ballCompositionVisual.Offset = defaultOffset + new Vector3D(e.Delta.X * 0.4f, e.Delta.Y * 0.4f, 0) * (inverse ? -1 : 1);

e.Handled = true;
}
Expand Down
4 changes: 2 additions & 2 deletions samples/GpuInterop/DrawingSurfaceDemoBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async void Initialize()

Surface = _compositor.CreateDrawingSurface();
_visual = _compositor.CreateSurfaceVisual();
_visual.Size = new Vector2((float)Bounds.Width, (float)Bounds.Height);
_visual.Size = new (Bounds.Width, Bounds.Height);
_visual.Surface = Surface;
ElementComposition.SetElementChildVisual(this, _visual);
var (res, info) = await DoInitialize(_compositor, Surface);
Expand All @@ -72,7 +72,7 @@ void UpdateFrame()
if (root == null)
return;

_visual!.Size = new Vector2((float)Bounds.Width, (float)Bounds.Height);
_visual!.Size = new (Bounds.Width, Bounds.Height);
var size = PixelSize.FromSize(Bounds.Size, root.RenderScaling);
RenderFrame(size);
if (SupportsDisco && Disco > 0)
Expand Down
7 changes: 7 additions & 0 deletions samples/Sandbox/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
x:Class="Sandbox.MainWindow">

<ScrollViewer>
<StackPanel>
<Button Margin="0 100000000000000000 0 0">0</Button>
<Button>1</Button>
</StackPanel>
</ScrollViewer>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class ScalarInterpolator : IInterpolator<float>

public static ScalarInterpolator Instance { get; } = new ScalarInterpolator();
}
class DoubleInterpolator : IInterpolator<double>
{
public double Interpolate(double @from, double to, float progress) => @from + (to - @from) * progress;

public static DoubleInterpolator Instance { get; } = new ();
}

class Vector2Interpolator : IInterpolator<Vector2>
{
Expand All @@ -28,6 +34,15 @@ public Vector2 Interpolate(Vector2 @from, Vector2 to, float progress)
public static Vector2Interpolator Instance { get; } = new Vector2Interpolator();
}

class VectorInterpolator : IInterpolator<Vector>
{
public Vector Interpolate(Vector @from, Vector to, float progress)
=> new(DoubleInterpolator.Instance.Interpolate(from.X, to.X, progress),
DoubleInterpolator.Instance.Interpolate(from.Y, to.Y, progress));

public static VectorInterpolator Instance { get; } = new ();
}

class Vector3Interpolator : IInterpolator<Vector3>
{
public Vector3 Interpolate(Vector3 @from, Vector3 to, float progress)
Expand All @@ -36,6 +51,16 @@ public Vector3 Interpolate(Vector3 @from, Vector3 to, float progress)
public static Vector3Interpolator Instance { get; } = new Vector3Interpolator();
}

class Vector3DInterpolator : IInterpolator<Vector3D>
{
public Vector3D Interpolate(Vector3D @from, Vector3D to, float progress)
=> new(DoubleInterpolator.Instance.Interpolate(from.X, to.X, progress),
DoubleInterpolator.Instance.Interpolate(from.Y, to.Y, progress),
DoubleInterpolator.Instance.Interpolate(from.Z, to.Z, progress));

public static Vector3DInterpolator Instance { get; } = new ();
}

class Vector4Interpolator : IInterpolator<Vector4>
{
public Vector4 Interpolate(Vector4 @from, Vector4 to, float progress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ private void UpdateCore()
continue;

// TODO: Optimize all of that by moving to the Visual itself, so we won't have to recalculate every time
comp.Offset = new Vector3((float)visual.Bounds.Left, (float)visual.Bounds.Top, 0);
comp.Size = new Vector2((float)visual.Bounds.Width, (float)visual.Bounds.Height);
comp.Offset = new (visual.Bounds.Left, visual.Bounds.Top, 0);
comp.Size = new (visual.Bounds.Width, visual.Bounds.Height);
comp.Visible = visual.IsVisible;
comp.Opacity = (float)visual.Opacity;
comp.ClipToBounds = visual.ClipToBounds;
Expand All @@ -269,7 +269,7 @@ private void UpdateCore()
renderTransform *= (-offset) * visual.RenderTransform.Value * (offset);
}

comp.TransformMatrix = MatrixUtils.ToMatrix4x4(renderTransform);
comp.TransformMatrix = renderTransform;

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void VerifyAccess()
_host.Compositor.VerifyAccess();
}

protected Vector2 EffectiveSize
protected Vector EffectiveSize
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static bool TryGetInvertedTransform(CompositionVisual visual, out Matrix matrix)
return false;
}

var m33 = MatrixUtils.ToMatrix(m.Value);
var m33 = m.Value;
return m33.TryInvert(out matrix);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class BuiltInExpressionFfi : IExpressionForeignFunctionInterface
private readonly DelegateExpressionFfi _registry;

static float Lerp(float a, float b, float p) => p * (b - a) + a;
static double Lerp(double a, double b, double p) => p * (b - a) + a;

static Matrix3x2 Inverse(Matrix3x2 m)
{
Expand All @@ -34,6 +35,13 @@ static float SmoothStep(float edge0, float edge1, float x)
var t = MathUtilities.Clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}

static double SmoothStep(double edge0, double edge1, double x)
{
var t = MathUtilities.Clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
return t * t * (3.0f - 2.0f * t);
}


static Vector2 SmoothStep(Vector2 edge0, Vector2 edge1, Vector2 x)
{
Expand All @@ -43,6 +51,15 @@ static Vector2 SmoothStep(Vector2 edge0, Vector2 edge1, Vector2 x)

);
}

static Vector SmoothStep(Vector edge0, Vector edge1, Vector x)
{
return new (
SmoothStep(edge0.X, edge1.X, x.X),
SmoothStep(edge0.Y, edge1.Y, x.Y)
);
}

static Vector3 SmoothStep(Vector3 edge0, Vector3 edge1, Vector3 x)
{
return new Vector3(
Expand All @@ -52,6 +69,15 @@ static Vector3 SmoothStep(Vector3 edge0, Vector3 edge1, Vector3 x)

);
}

static Vector3D SmoothStep(Vector3D edge0, Vector3D edge1, Vector3D x)
{
return new (
SmoothStep(edge0.X, edge1.X, x.X),
SmoothStep(edge0.Y, edge1.Y, x.Y),
SmoothStep(edge0.Z, edge1.Z, x.Z)
);
}

static Vector4 SmoothStep(Vector4 edge0, Vector4 edge1, Vector4 x)
{
Expand All @@ -69,7 +95,9 @@ private BuiltInExpressionFfi()
{
{"Abs", (float f) => Math.Abs(f)},
{"Abs", (Vector2 v) => Vector2.Abs(v)},
{"Abs", (Vector v) => v.Abs()},
{"Abs", (Vector3 v) => Vector3.Abs(v)},
{"Abs", (Vector3D v) => v.Abs()},
{"Abs", (Vector4 v) => Vector4.Abs(v)},

{"ACos", (float f) => (float) Math.Acos(f)},
Expand All @@ -79,7 +107,9 @@ private BuiltInExpressionFfi()

{"Clamp", (float a1, float a2, float a3) => MathUtilities.Clamp(a1, a2, a3)},
{"Clamp", (Vector2 a1, Vector2 a2, Vector2 a3) => Vector2.Clamp(a1, a2, a3)},
{"Clamp", (Vector a1, Vector a2, Vector a3) => Vector.Clamp(a1, a2, a3)},
{"Clamp", (Vector3 a1, Vector3 a2, Vector3 a3) => Vector3.Clamp(a1, a2, a3)},
{"Clamp", (Vector3D a1, Vector3D a2, Vector3D a3) => Vector3D.Clamp(a1, a2, a3)},
{"Clamp", (Vector4 a1, Vector4 a2, Vector4 a3) => Vector4.Clamp(a1, a2, a3)},

{"Concatenate", (Quaternion a1, Quaternion a2) => Quaternion.Concatenate(a1, a2)},
Expand Down Expand Up @@ -109,11 +139,15 @@ private BuiltInExpressionFfi()
},

{"Distance", (Vector2 a1, Vector2 a2) => Vector2.Distance(a1, a2)},
{"Distance", (Vector a1, Vector a2) => Vector.Distance(a1, a2)},
{"Distance", (Vector3 a1, Vector3 a2) => Vector3.Distance(a1, a2)},
{"Distance", (Vector3D a1, Vector3D a2) => Vector3D.Distance(a1, a2)},
{"Distance", (Vector4 a1, Vector4 a2) => Vector4.Distance(a1, a2)},

{"DistanceSquared", (Vector2 a1, Vector2 a2) => Vector2.DistanceSquared(a1, a2)},
{"DistanceSquared", (Vector a1, Vector a2) => Vector.DistanceSquared(a1, a2)},
{"DistanceSquared", (Vector3 a1, Vector3 a2) => Vector3.DistanceSquared(a1, a2)},
{"DistanceSquared", (Vector3D a1, Vector3D a2) => Vector3D.DistanceSquared(a1, a2)},
{"DistanceSquared", (Vector4 a1, Vector4 a2) => Vector4.DistanceSquared(a1, a2)},

{"Floor", (float v) => (float) Math.Floor(v)},
Expand All @@ -123,18 +157,24 @@ private BuiltInExpressionFfi()


{"Length", (Vector2 a1) => a1.Length()},
{"Length", (Vector a1) => a1.Length},
{"Length", (Vector3 a1) => a1.Length()},
{"Length", (Vector3D a1) => a1.Length},
{"Length", (Vector4 a1) => a1.Length()},
{"Length", (Quaternion a1) => a1.Length()},

{"LengthSquared", (Vector2 a1) => a1.LengthSquared()},
{"LengthSquared", (Vector a1) => a1*a1},
{"LengthSquared", (Vector3 a1) => a1.LengthSquared()},
{"LengthSquared", (Vector3D a1) => Vector3D.Dot(a1, a1)},
{"LengthSquared", (Vector4 a1) => a1.LengthSquared()},
{"LengthSquared", (Quaternion a1) => a1.LengthSquared()},

{"Lerp", (float a1, float a2, float a3) => Lerp(a1, a2, a3)},
{"Lerp", (Vector2 a1, Vector2 a2, float a3) => Vector2.Lerp(a1, a2, a3)},
{"Lerp", (Vector a1, Vector a2, float a3) => new Vector(Lerp(a1.X, a2.X, a3), Lerp(a1.Y, a2.Y, a3))},
{"Lerp", (Vector3 a1, Vector3 a2, float a3) => Vector3.Lerp(a1, a2, a3)},
{"Lerp", (Vector3D a1, Vector3D a2, float a3) => new Vector3D(Lerp(a1.X, a2.X, a3), Lerp(a1.Y, a2.Y, a3), Lerp(a1.Z, a2.Z, a3))},
{"Lerp", (Vector4 a1, Vector4 a2, float a3) => Vector4.Lerp(a1, a2, a3)},


Expand Down Expand Up @@ -173,39 +213,50 @@ private BuiltInExpressionFfi()

{"Max", (float a1, float a2) => Math.Max(a1, a2)},
{"Max", (Vector2 a1, Vector2 a2) => Vector2.Max(a1, a2)},
{"Max", (Vector a1, Vector a2) => Vector.Max(a1, a2)},
{"Max", (Vector3 a1, Vector3 a2) => Vector3.Max(a1, a2)},
{"Max", (Vector3D a1, Vector3D a2) => Vector3D.Max(a1, a2)},
{"Max", (Vector4 a1, Vector4 a2) => Vector4.Max(a1, a2)},


{"Min", (float a1, float a2) => Math.Min(a1, a2)},
{"Min", (Vector2 a1, Vector2 a2) => Vector2.Min(a1, a2)},
{"Min", (Vector a1, Vector a2) => Vector.Min(a1, a2)},
{"Min", (Vector3 a1, Vector3 a2) => Vector3.Min(a1, a2)},
{"Min", (Vector3D a1, Vector3D a2) => Vector3D.Min(a1, a2)},
{"Min", (Vector4 a1, Vector4 a2) => Vector4.Min(a1, a2)},

{"Mod", (float a, float b) => a % b},

{"Normalize", (Quaternion a) => Quaternion.Normalize(a)},
{"Normalize", (Vector2 a) => Vector2.Normalize(a)},
{"Normalize", (Vector a) => Vector.Normalize(a)},
{"Normalize", (Vector3 a) => Vector3.Normalize(a)},
{"Normalize", (Vector3D a) => Vector3D.Normalize(a)},
{"Normalize", (Vector4 a) => Vector4.Normalize(a)},

{"Pow", (float a, float b) => (float) Math.Pow(a, b)},
{"Quaternion.CreateFromAxisAngle", (Vector3 a, float b) => Quaternion.CreateFromAxisAngle(a, b)},
{"Quaternion.CreateFromAxisAngle", (Vector3D a, float b) => Quaternion.CreateFromAxisAngle(a.ToVector3(), b)},
{"Quaternion", (float a, float b, float c, float d) => new Quaternion(a, b, c, d)},

{"Round", (float a) => (float) Math.Round(a)},

{"Scale", (Matrix3x2 a, float b) => a * b},
{"Scale", (Matrix4x4 a, float b) => a * b},
{"Scale", (Vector2 a, float b) => a * b},
{"Scale", (Vector a, float b) => a * b},
{"Scale", (Vector3 a, float b) => a * b},
{"Scale", (Vector3D a, float b) => Vector3D.Multiply(a, b)},
{"Scale", (Vector4 a, float b) => a * b},

{"Sin", (float a) => (float) Math.Sin(a)},

{"SmoothStep", (float a1, float a2, float a3) => SmoothStep(a1, a2, a3)},
{"SmoothStep", (Vector2 a1, Vector2 a2, Vector2 a3) => SmoothStep(a1, a2, a3)},
{"SmoothStep", (Vector a1, Vector a2, Vector a3) => SmoothStep(a1, a2, a3)},
{"SmoothStep", (Vector3 a1, Vector3 a2, Vector3 a3) => SmoothStep(a1, a2, a3)},
{"SmoothStep", (Vector3D a1, Vector3D a2, Vector3D a3) => SmoothStep(a1, a2, a3)},
{"SmoothStep", (Vector4 a1, Vector4 a2, Vector4 a3) => SmoothStep(a1, a2, a3)},

// I have no idea how to do a spherical interpolation for a scalar value, so we are doing a linear one
Expand All @@ -222,9 +273,13 @@ private BuiltInExpressionFfi()
{"Transform", (Vector2 a, Matrix3x2 b) => Vector2.Transform(a, b)},
{"Transform", (Vector3 a, Matrix4x4 b) => Vector3.Transform(a, b)},

{"Vector2", (float a, float b) => new Vector2(a, b)},
{"Vector3", (float a, float b, float c) => new Vector3(a, b, c)},
{"Vector2", (float a, float b) => new Vector(a, b)},
{"Vector2", (double a, double b) => new Vector(a, b)},
{"Vector3", (float a, float b, float c) => new Vector3D(a, b, c)},
{"Vector3", (double a, double b, double c) => new Vector3D(a, b, c)},
{"Vector3", (Vector2 v2, float z) => new Vector3(v2, z)},
{"Vector3", (Vector v2, float z) => new Vector3D(v2.X, v2.Y, z)},
{"Vector3", (Vector v2, double z) => new Vector3D(v2.X, v2.Y, z)},
{"Vector4", (float a, float b, float c, float d) => new Vector4(a, b, c, d)},
{"Vector4", (Vector2 v2, float z, float w) => new Vector4(v2, z, w)},
{"Vector4", (Vector3 v3, float w) => new Vector4(v3, w)},
Expand Down
Loading