using System; using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Interop.Effects.TransformMappers; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #pragma warning disable CS0618 namespace ComputeSharp.D2D1.Uwp.Extensions; /// /// A with extensions for the type. /// internal static unsafe class ID2D1EffectExtensions { /// /// Gets the constant buffer of type from a given object. /// /// The type of shader being used. /// The input instance. /// The constant buffer of type for . public static T GetConstantBuffer(this ref ID2D1Effect d2D1Effect) where T : unmanaged, ID2D1PixelShader { int constantBufferSize = D2D1PixelShader.GetConstantBufferSize(); byte[] buffer = ArrayPool.Shared.Rent(constantBufferSize); fixed (byte* p = buffer) { // Get the raw constant buffer from the effect d2D1Effect.GetValue( index: D2D1PixelShaderEffectProperty.ConstantBuffer, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BLOB, data: p, dataSize: (uint)constantBufferSize).Assert(); } Unsafe.SkipInit(out T shader); shader.InitializeFromDispatchData(buffer.AsSpan(0, constantBufferSize)); return shader; } /// /// Gets the instance from a given object. /// /// The type of shader being used. /// The input instance. /// The instance for . public static D2D1TransformMapper? GetTransformMapper(this ref ID2D1Effect d2D1Effect) where T : unmanaged, ID2D1PixelShader { using ComPtr d2D1TransformMapper = default; // Get the ID2D1TransformMapper object from the effect d2D1Effect.GetValue( index: D2D1PixelShaderEffectProperty.TransformMapper, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)d2D1TransformMapper.GetAddressOf(), dataSize: (uint)sizeof(void*)).Assert(); // If there is no transform, just return null (this is the default state) if (d2D1TransformMapper.Get() is null) { return null; } // Otherwise, check if it's an internal CCW and retrieve the managed wrapper using ComPtr d2D1TransformMapperInternal = default; d2D1TransformMapper.CopyTo(d2D1TransformMapperInternal.GetAddressOf()).Assert(); IntPtr handlePtr; // Get the underlying GCHandle value d2D1TransformMapperInternal.Get()->GetManagedWrapperHandle((void**)&handlePtr).Assert(); // Retrieve the managed wrapper from the GCHandle return (D2D1TransformMapper)GCHandle.FromIntPtr(handlePtr).Target!; } /// /// Gets the instance from a given object. /// /// The input instance. /// The current cached source instance. /// The index of the resource texture manager to retrieve. /// The instance for . public static D2D1ResourceTextureManager? GetResourceTextureManager( this ref ID2D1Effect d2D1Effect, D2D1ResourceTextureManager? source, int index) { using ComPtr d2D1ResourceTextureManager = default; // Get the ID2D1ResourceTextureManager object from the effect d2D1Effect.GetValue( index: (uint)(D2D1PixelShaderEffectProperty.ResourceTextureManager0 + index), type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)d2D1ResourceTextureManager.GetAddressOf(), dataSize: (uint)sizeof(void*)).Assert(); // If there is no resource texture manager, just return null (this is the default state) if (d2D1ResourceTextureManager.Get() is null) { return null; } // Otherwise, the source must not be null and wrapping the same CCW if (source is not null) { using ComPtr d2D1ResourceTextureManagerSource = default; source.GetD2D1ResourceTextureManager(d2D1ResourceTextureManagerSource.GetAddressOf()); // Check if the instances match, and return the managed wrapper we already have if (d2D1ResourceTextureManager.Get()->IsSameInstance(d2D1ResourceTextureManagerSource.Get())) { return source; } } // There was no way to retrieve the correct managed wrapper ThrowHelper.ThrowInvalidOperationException("Cannot retrieve a D2D1ResourceTextureManager instance wrapping a native object that was set externally."); return null; } /// /// Gets the property for a given object. /// /// The input instance. /// The value for . public static bool GetCachedProperty(this ref ID2D1Effect d2D1Effect) { int cacheOutput; d2D1Effect.GetValue( index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, data: (byte*)&cacheOutput, dataSize: sizeof(int)).Assert(); return cacheOutput != 0; } /// /// Sets the property for a given object. /// /// The input instance. /// The property value to set. public static void SetCachedProperty(this ref ID2D1Effect d2D1Effect, bool value) { int cacheOutput = value ? 1 : 0; d2D1Effect.SetValue( index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_CACHED, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_BOOL, data: (byte*)&cacheOutput, dataSize: sizeof(int)).Assert(); } /// /// Gets the property for a given object. /// /// The input instance. /// The value for . public static D2D1_BUFFER_PRECISION GetPrecisionProperty(this ref ID2D1Effect d2D1Effect) { D2D1_BUFFER_PRECISION d2D1BufferPrecision; d2D1Effect.GetValue( index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, data: (byte*)&d2D1BufferPrecision, dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); return d2D1BufferPrecision; } /// /// Sets the property for a given object. /// /// The input instance. /// The property value to set. public static void SetPrecisionProperty(this ref ID2D1Effect d2D1Effect, D2D1_BUFFER_PRECISION value) { d2D1Effect.SetValue( index: (uint)D2D1_PROPERTY.D2D1_PROPERTY_PRECISION, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_UNKNOWN, data: (byte*)&value, dataSize: sizeof(D2D1_BUFFER_PRECISION)).Assert(); } }