Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8a554f9

Browse files
committed
Add SoA raytracer as a CQ test for Intel hardware intrinsic
1 parent f3ab7e0 commit 8a554f9

22 files changed

+1401
-0
lines changed

tests/build.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\JIT\config\benchmark+roslyn\benchmark+roslyn.csproj" />
3131
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\JIT\config\benchmark+serialize\benchmark+serialize.csproj" />
3232
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\JIT\config\benchmark\benchmark.csproj" />
33+
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\JIT\config\benchmark+intrinsic\benchmark+intrinsic.csproj" />
3334
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\performance\performance.csproj" />
3435
<RestoreProjects Include="$(MSBuildThisFileDirectory)src\TestWrappersConfig\TestWrappersConfig.csproj" />
3536
</ItemGroup>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using System.Runtime.Intrinsics.X86;
7+
using static System.Runtime.Intrinsics.X86.Avx;
8+
using System.Runtime.Intrinsics;
9+
internal class Camera
10+
{
11+
12+
public Camera(VectorPacket256 pos, VectorPacket256 forward, VectorPacket256 up, VectorPacket256 right) { Pos = pos; Forward = forward; Up = up; Right = right; }
13+
14+
public VectorPacket256 Pos;
15+
public VectorPacket256 Forward;
16+
public VectorPacket256 Up;
17+
public VectorPacket256 Right;
18+
19+
public static Camera Create(VectorPacket256 pos, VectorPacket256 lookAt)
20+
{
21+
VectorPacket256 forward = (lookAt - pos).Normalize();
22+
VectorPacket256 down = new VectorPacket256(SetZeroVector256<float>(), SetAllVector256<float>(-1), SetZeroVector256<float>());
23+
Vector256<float> OnePointFive = SetAllVector256<float>(1.5f);
24+
VectorPacket256 right = OnePointFive * VectorPacket256.CrossProduct(forward, down).Normalize();
25+
VectorPacket256 up = OnePointFive * VectorPacket256.CrossProduct(forward, right).Normalize();
26+
27+
return new Camera(pos, forward, up, right);
28+
}
29+
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
internal struct Color
7+
{
8+
public float R { get; }
9+
public float G { get; }
10+
public float B { get; }
11+
12+
public static readonly Color Background = new Color(0, 0, 0);
13+
14+
public Color(float _r, float _g, float _b)
15+
{
16+
R = _r;
17+
G = _g;
18+
B = _b;
19+
}
20+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using static System.Runtime.Intrinsics.X86.Avx;
7+
using System.Runtime.Intrinsics.X86;
8+
using System.Runtime.Intrinsics;
9+
using System.Runtime.CompilerServices;
10+
11+
using ColorPacket256 = VectorPacket256;
12+
13+
internal static class ColorPacket256Helper
14+
{
15+
16+
private static readonly Vector256<float> One = SetAllVector256<float>(1.0f);
17+
private static readonly Vector256<float> Max = SetAllVector256<float>(255.0f);
18+
public static Int32RGBPacket256 ConvertToIntRGB(this VectorPacket256 colors)
19+
{
20+
21+
var rsMask = Compare(colors.Xs, One, FloatComparisonMode.GreaterThanOrderedNonSignaling);
22+
var gsMask = Compare(colors.Ys, One, FloatComparisonMode.GreaterThanOrderedNonSignaling);
23+
var bsMask = Compare(colors.Zs, One, FloatComparisonMode.GreaterThanOrderedNonSignaling);
24+
25+
var rs = BlendVariable(colors.Xs, One, rsMask);
26+
var gs = BlendVariable(colors.Ys, One, gsMask);
27+
var bs = BlendVariable(colors.Zs, One, bsMask);
28+
29+
var rsInt = ConvertToVector256Int32(Multiply(rs, Max));
30+
var gsInt = ConvertToVector256Int32(Multiply(gs, Max));
31+
var bsInt = ConvertToVector256Int32(Multiply(bs, Max));
32+
33+
return new Int32RGBPacket256(rsInt, gsInt, bsInt);
34+
}
35+
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
public static ColorPacket256 Times(ColorPacket256 left, ColorPacket256 right)
38+
{
39+
return new VectorPacket256(Multiply(left.Xs, right.Xs), Multiply(left.Ys, right.Ys), Multiply(left.Zs, right.Zs));
40+
}
41+
42+
public static readonly ColorPacket256 BackgroundColor = new ColorPacket256(SetZeroVector256<float>());
43+
public static readonly ColorPacket256 DefaultColor = new ColorPacket256(SetZeroVector256<float>());
44+
}
45+
46+
internal struct Int32RGBPacket256
47+
{
48+
public Vector256<int> Rs;
49+
public Vector256<int> Gs;
50+
public Vector256<int> Bs;
51+
52+
public Int32RGBPacket256(Vector256<int> rs, Vector256<int> gs, Vector256<int> bs)
53+
{
54+
Rs = rs;
55+
Gs = gs;
56+
Bs = bs;
57+
}
58+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using static System.Runtime.Intrinsics.X86.Avx;
7+
using static System.Runtime.Intrinsics.X86.Avx2;
8+
using System.Runtime.Intrinsics.X86;
9+
using System.Runtime.Intrinsics;
10+
using System.Runtime.CompilerServices;
11+
using System;
12+
13+
internal struct Intersections
14+
{
15+
public Vector256<float> Distances;
16+
public Vector256<int> ThingIndices;
17+
18+
public static readonly Vector256<float> NullDistance = SetAllVector256<float>(float.MaxValue);
19+
public static readonly Vector256<int> NullIndex = SetAllVector256<int>(-1);
20+
21+
public Intersections(Vector256<float> dis, Vector256<int> things)
22+
{
23+
Distances = dis;
24+
ThingIndices = things;
25+
}
26+
27+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
28+
public bool AllNullIntersections()
29+
{
30+
return AllNullIntersections(Distances);
31+
}
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public static bool AllNullIntersections(Vector256<float> dis)
35+
{
36+
var cmp = Compare(dis, NullDistance, FloatComparisonMode.EqualOrderedNonSignaling);
37+
var zero = SetZeroVector256<int>();
38+
var mask = Avx2.CompareEqual(zero, zero);
39+
return TestC(cmp, StaticCast<int, float>(mask));
40+
}
41+
42+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using System.Runtime.CompilerServices;
7+
using ColorPacket256 = VectorPacket256;
8+
9+
internal class LightPacket256
10+
{
11+
public VectorPacket256 Positions;
12+
public ColorPacket256 Colors;
13+
14+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15+
public LightPacket256(Vector pos, Color col)
16+
{
17+
Positions = new VectorPacket256(pos.X, pos.Y, pos.Z);
18+
Colors = new ColorPacket256(col.R, col.G, col.B);
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using System.Runtime.Intrinsics;
7+
8+
internal abstract class ObjectPacket256
9+
{
10+
public Surface Surface { get; }
11+
public abstract Vector256<float> Intersect(RayPacket256 rayPacket256);
12+
public abstract VectorPacket256 Normals(VectorPacket256 pos);
13+
14+
public ObjectPacket256(Surface surface)
15+
{
16+
Surface = surface;
17+
}
18+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
//
5+
6+
using System.Collections.Generic;
7+
using System.Diagnostics;
8+
9+
namespace System.Collections.Concurrent
10+
{
11+
/// <summary>Provides a thread-safe object pool.</summary>
12+
/// <typeparam name="T">Specifies the type of the elements stored in the pool.</typeparam>
13+
[DebuggerDisplay("Count={Count}")]
14+
[DebuggerTypeProxy(typeof(IProducerConsumerCollection_DebugView<>))]
15+
public sealed class ObjectPool<T> : ProducerConsumerCollectionBase<T>
16+
{
17+
private readonly Func<T> _generator;
18+
19+
/// <summary>Initializes an instance of the ObjectPool class.</summary>
20+
/// <param name="generator">The function used to create items when no items exist in the pool.</param>
21+
public ObjectPool(Func<T> generator) : this(generator, new ConcurrentQueue<T>()) { }
22+
23+
/// <summary>Initializes an instance of the ObjectPool class.</summary>
24+
/// <param name="generator">The function used to create items when no items exist in the pool.</param>
25+
/// <param name="collection">The collection used to store the elements of the pool.</param>
26+
public ObjectPool(Func<T> generator, IProducerConsumerCollection<T> collection)
27+
: base(collection)
28+
{
29+
if (generator == null) throw new ArgumentNullException("generator");
30+
_generator = generator;
31+
}
32+
33+
/// <summary>Adds the provided item into the pool.</summary>
34+
/// <param name="item">The item to be added.</param>
35+
public void PutObject(T item) { base.TryAdd(item); }
36+
37+
/// <summary>Gets an item from the pool.</summary>
38+
/// <returns>The removed or created item.</returns>
39+
/// <remarks>If the pool is empty, a new item will be created and returned.</remarks>
40+
public T GetObject()
41+
{
42+
T value;
43+
return base.TryTake(out value) ? value : _generator();
44+
}
45+
46+
/// <summary>Clears the object pool, returning all of the data that was in the pool.</summary>
47+
/// <returns>An array containing all of the elements in the pool.</returns>
48+
public T[] ToArrayAndClear()
49+
{
50+
var items = new List<T>();
51+
T value;
52+
while (base.TryTake(out value)) items.Add(value);
53+
return items.ToArray();
54+
}
55+
56+
protected override bool TryAdd(T item)
57+
{
58+
PutObject(item);
59+
return true;
60+
}
61+
62+
protected override bool TryTake(out T item)
63+
{
64+
item = GetObject();
65+
return true;
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)