Added local ref/nuget sample projects

このコミットが含まれているのは:
Sergio Pedri committed 2020-12-14 18:03:35 +01:00
1 親 202df86227
コミット 4f92a04d54
7ファイル変更
+75 -15

検索条件に一致するファイルはありません

+14
ファイルの表示
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>8cefc324-ca6c-4845-9ad9-70b92bd87057</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>ComputeSharp.Sample.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Program.cs" />
</ItemGroup>
</Project>
+13
ファイルの表示
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>8cefc324-ca6c-4845-9ad9-70b92bd87057</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="ComputeSharp.Sample.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
+79
ファイルの表示
@@ -0,0 +1,79 @@
using System;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
namespace ComputeSharp.Sample
{
partial class Program
{
static void Main()
{
// Create the graphics buffer
int width = 10;
int height = 10;
float[] array = new float[width * height];
for (int i = 0; i < array.Length; i++)
{
array[i] = i + 1;
}
// Print the initial matrix
Console.WriteLine("===================== BEFORE =====================");
PrintMatrix(array, width, height);
Console.WriteLine("==================================================");
Console.WriteLine();
using ReadWriteBuffer<float> gpuBuffer = Gpu.Default.AllocateReadWriteBuffer(array);
// Run the shader
Gpu.Default.For(100, new MainKernel(gpuBuffer));
// Get the data back
gpuBuffer.GetData(array);
// Print the updated matrix
Console.WriteLine("===================== AFTER ======================");
PrintMatrix(array, width, height);
Console.WriteLine("==================================================");
}
/// <summary>
/// Kernel for <see cref="Main"/>.
/// </summary>
[AutoConstructor]
private readonly partial struct MainKernel : IComputeShader
{
private readonly ReadWriteBuffer<float> buffer;
/// <inheritdoc/>
public void Execute(ThreadIds ids)
{
buffer[ids.X] *= 2;
}
}
/// <summary>
/// Prints a matrix in a properly formatted way.
/// </summary>
/// <param name="array">The input <see langword="float"/> array representing the matrix to print.</param>
/// <param name="width">The width of the array to print.</param>
/// <param name="height">The height of the array to print.</param>
private static void PrintMatrix(float[] array, int width, int height)
{
int length = width * height;
int numberWidth = array.Max().ToString(CultureInfo.InvariantCulture).Length;
ref float r = ref array[0];
for (int i = 0; i < length; i++)
{
Console.Write(r.ToString(CultureInfo.InvariantCulture).PadLeft(numberWidth));
r = ref Unsafe.Add(ref r, 1);
if (i < length - 1) Console.Write(", ");
if (i > 0 && (i + 1) % width == 0) Console.WriteLine();
}
}
}
}