Added exception type and check for double types

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-16 14:26:08 +01:00
1 親 cd8456a39f
コミット 977ba2297b
3ファイル変更
+99

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

+49
ファイルの表示
@@ -0,0 +1,49 @@
using System;
using System.Diagnostics.Contracts;
using System.Text;
namespace ComputeSharp.Exceptions
{
/// <summary>
/// A custom <see cref="InvalidOperationException"/> that indicates when a resource was attempted to be created with an unsupported type.
/// </summary>
public sealed class UnsupportedDoubleOperationsException : InvalidOperationException
{
/// <summary>
/// Creates a new <see cref="UnsupportedDoubleOperationsException"/> instance.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public UnsupportedDoubleOperationsException(string message)
: base(message)
{
}
/// <summary>
/// Creates a new <see cref="UnsupportedTextureTypeException"/> instance from the specified parameters.
/// </summary>
/// <param name="type">The element type of resource that was being created.</param>
/// <returns>A new <see cref="UnsupportedTextureTypeException"/> instance with a formatted error message.</returns>
[Pure]
private static UnsupportedDoubleOperationsException Create(Type type)
{
StringBuilder builder = new(256);
builder.AppendLine($"The device in use does not support creating resources of type {type}, as it does not support using double precision floating point numbers.");
builder.AppendLine("As a possible workaround, consider replacing usage of the double type with single precision floating point numbers.");
builder.AppendLine("Note that double precision operations are only partially supported on GPU devices, and are far slower than single precision ones.");
builder.ToString();
return new(builder.ToString());
}
/// <summary>
/// Throws a new <see cref="UnsupportedDoubleOperationsException"/> instance from the specified parameters.
/// </summary>
/// <typeparam name="T">The type of values in the resource that couldn't be created.</typeparam>
internal static void Throw<T>()
where T : unmanaged
{
throw Create(typeof(T));
}
}
}
+42
ファイルの表示
@@ -0,0 +1,42 @@
using System;
using System.Diagnostics.Contracts;
using System.Reflection;
namespace ComputeSharp.Graphics.Helpers
{
/// <summary>
/// A class providing info on specific types.
/// </summary>
/// <typeparam name="T">The input type to extract info for.</typeparam>
internal static class TypeInfo<T>
where T : unmanaged
{
/// <summary>
/// Whether or not type <typeparamref name="T"/> is <see cref="double"/> or contains a <see cref="double"/> field.
/// </summary>
public static readonly bool IsDoubleOrContainsDoubles;
/// <summary>
/// Checks the value for <see cref="IsDoubleOrContainsDoubles"/> for type <typeparamref name="T"/>.
/// </summary>
/// <param name="type">The current type to check.</param>
/// <returns>Whether or not <paramref name="type"/> is <see cref="double"/> or contains a <see cref="double"/> field.</returns>
[Pure]
private static bool ChecksIsDoubleOrContainsDoubles(Type type)
{
if (type == typeof(double)) return true;
if (type.IsPrimitive) return false;
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (ChecksIsDoubleOrContainsDoubles(fieldInfo.FieldType))
{
return true;
}
}
return false;
}
}
}
+8
ファイルの表示
@@ -3,9 +3,11 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using ComputeSharp.Exceptions;
using ComputeSharp.Graphics.Extensions;
using ComputeSharp.Graphics.Helpers;
using ComputeSharp.Interop;
using Microsoft.Toolkit.Diagnostics;
using TerraFX.Interop;
using static TerraFX.Interop.D3D12_FEATURE;
using FX = TerraFX.Interop.Windows;
using ResourceType = ComputeSharp.Graphics.Resources.Enums.ResourceType;
@@ -66,6 +68,12 @@ namespace ComputeSharp.Resources
Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255, nameof(length));
}
if (TypeInfo<T>.IsDoubleOrContainsDoubles &&
device.D3D12Device->CheckFeatureSupport<D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
{
UnsupportedDoubleOperationsException.Throw<T>();
}
SizeInBytes = checked((nint)(length * elementSizeInBytes));
GraphicsDevice = device;
Length = length;