using System; using System.Globalization; using System.Runtime.CompilerServices; using System.Text; using ComputeSharp.D2D1.Extensions; using ComputeSharp.D2D1.Helpers; using ComputeSharp.D2D1.Interop.Effects; using ComputeSharp.D2D1.Shaders.Interop.Buffers; using ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers; using ComputeSharp.D2D1.Shaders.Loaders; using TerraFX.Interop.DirectX; using TerraFX.Interop.Windows; #pragma warning disable CS0618 namespace ComputeSharp.D2D1.Interop; /// /// An implementation of a D2D1 pixel shader effect that can be used to instantiate ID2D1Effect objects. /// public static unsafe class D2D1PixelShaderEffect { /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { if (d2D1Factory1 is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); } RegisterForD2D1Factory1(d2D1Factory1, null, out effectId); } /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to register. /// The type of implementation to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { if (d2D1Factory1 is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); } RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); } /// /// Registers an effect from an input D2D1 pixel shader, by calling ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to register. /// A pointer to the ID2D1Factory1 instance to use. /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { if (d2D1Factory1 is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); } effectId = default; PixelShaderEffect.For.Initialize(mapperFactory); // Setup the input string StringBuilder effectInputsBuilder = new(); for (int i = 0; i < PixelShaderEffect.For.InputCount; i++) { effectInputsBuilder.Append(""); } // Prepare the XML with a variable number of inputs string xml = @$" {effectInputsBuilder} "; fixed (char* pXml = xml) fixed (char* pBufferPropertyName = "Buffer") fixed (char* pResourceTextureManager0PropertyName = "ResourceTextureManager0") fixed (char* pResourceTextureManager1PropertyName = "ResourceTextureManager1") fixed (char* pResourceTextureManager2PropertyName = "ResourceTextureManager2") fixed (char* pResourceTextureManager3PropertyName = "ResourceTextureManager3") fixed (char* pResourceTextureManager4PropertyName = "ResourceTextureManager4") fixed (char* pResourceTextureManager5PropertyName = "ResourceTextureManager5") fixed (char* pResourceTextureManager6PropertyName = "ResourceTextureManager6") fixed (char* pResourceTextureManager7PropertyName = "ResourceTextureManager7") fixed (char* pResourceTextureManager8PropertyName = "ResourceTextureManager8") fixed (char* pResourceTextureManager9PropertyName = "ResourceTextureManager9") fixed (char* pResourceTextureManager10PropertyName = "ResourceTextureManager10") fixed (char* pResourceTextureManager11PropertyName = "ResourceTextureManager11") fixed (char* pResourceTextureManager12PropertyName = "ResourceTextureManager12") fixed (char* pResourceTextureManager13PropertyName = "ResourceTextureManager13") fixed (char* pResourceTextureManager14PropertyName = "ResourceTextureManager14") fixed (char* pResourceTextureManager15PropertyName = "ResourceTextureManager15") { // Prepare the effect binding functions D2D1_PROPERTY_BINDING* d2D1PropertyBinding = stackalloc D2D1_PROPERTY_BINDING[17]; // Property names d2D1PropertyBinding[0].propertyName = (ushort*)pBufferPropertyName; d2D1PropertyBinding[1].propertyName = (ushort*)pResourceTextureManager0PropertyName; d2D1PropertyBinding[2].propertyName = (ushort*)pResourceTextureManager1PropertyName; d2D1PropertyBinding[3].propertyName = (ushort*)pResourceTextureManager2PropertyName; d2D1PropertyBinding[4].propertyName = (ushort*)pResourceTextureManager3PropertyName; d2D1PropertyBinding[5].propertyName = (ushort*)pResourceTextureManager4PropertyName; d2D1PropertyBinding[6].propertyName = (ushort*)pResourceTextureManager5PropertyName; d2D1PropertyBinding[7].propertyName = (ushort*)pResourceTextureManager6PropertyName; d2D1PropertyBinding[8].propertyName = (ushort*)pResourceTextureManager7PropertyName; d2D1PropertyBinding[9].propertyName = (ushort*)pResourceTextureManager8PropertyName; d2D1PropertyBinding[10].propertyName = (ushort*)pResourceTextureManager9PropertyName; d2D1PropertyBinding[11].propertyName = (ushort*)pResourceTextureManager10PropertyName; d2D1PropertyBinding[12].propertyName = (ushort*)pResourceTextureManager11PropertyName; d2D1PropertyBinding[13].propertyName = (ushort*)pResourceTextureManager12PropertyName; d2D1PropertyBinding[14].propertyName = (ushort*)pResourceTextureManager13PropertyName; d2D1PropertyBinding[15].propertyName = (ushort*)pResourceTextureManager14PropertyName; d2D1PropertyBinding[16].propertyName = (ushort*)pResourceTextureManager15PropertyName; // Property getters d2D1PropertyBinding[0].getFunction = PixelShaderEffect.GetConstantBuffer; d2D1PropertyBinding[1].getFunction = PixelShaderEffect.GetResourceTextureManager0; d2D1PropertyBinding[2].getFunction = PixelShaderEffect.GetResourceTextureManager1; d2D1PropertyBinding[3].getFunction = PixelShaderEffect.GetResourceTextureManager2; d2D1PropertyBinding[4].getFunction = PixelShaderEffect.GetResourceTextureManager3; d2D1PropertyBinding[5].getFunction = PixelShaderEffect.GetResourceTextureManager4; d2D1PropertyBinding[6].getFunction = PixelShaderEffect.GetResourceTextureManager5; d2D1PropertyBinding[7].getFunction = PixelShaderEffect.GetResourceTextureManager6; d2D1PropertyBinding[8].getFunction = PixelShaderEffect.GetResourceTextureManager7; d2D1PropertyBinding[9].getFunction = PixelShaderEffect.GetResourceTextureManager8; d2D1PropertyBinding[10].getFunction = PixelShaderEffect.GetResourceTextureManager9; d2D1PropertyBinding[11].getFunction = PixelShaderEffect.GetResourceTextureManager10; d2D1PropertyBinding[12].getFunction = PixelShaderEffect.GetResourceTextureManager11; d2D1PropertyBinding[13].getFunction = PixelShaderEffect.GetResourceTextureManager12; d2D1PropertyBinding[14].getFunction = PixelShaderEffect.GetResourceTextureManager13; d2D1PropertyBinding[15].getFunction = PixelShaderEffect.GetResourceTextureManager14; d2D1PropertyBinding[16].getFunction = PixelShaderEffect.GetResourceTextureManager15; // Property setters d2D1PropertyBinding[0].setFunction = PixelShaderEffect.SetConstantBuffer; d2D1PropertyBinding[1].setFunction = PixelShaderEffect.SetResourceTextureManager0; d2D1PropertyBinding[2].setFunction = PixelShaderEffect.SetResourceTextureManager1; d2D1PropertyBinding[3].setFunction = PixelShaderEffect.SetResourceTextureManager2; d2D1PropertyBinding[4].setFunction = PixelShaderEffect.SetResourceTextureManager3; d2D1PropertyBinding[5].setFunction = PixelShaderEffect.SetResourceTextureManager4; d2D1PropertyBinding[6].setFunction = PixelShaderEffect.SetResourceTextureManager5; d2D1PropertyBinding[7].setFunction = PixelShaderEffect.SetResourceTextureManager6; d2D1PropertyBinding[8].setFunction = PixelShaderEffect.SetResourceTextureManager7; d2D1PropertyBinding[9].setFunction = PixelShaderEffect.SetResourceTextureManager8; d2D1PropertyBinding[10].setFunction = PixelShaderEffect.SetResourceTextureManager9; d2D1PropertyBinding[11].setFunction = PixelShaderEffect.SetResourceTextureManager10; d2D1PropertyBinding[12].setFunction = PixelShaderEffect.SetResourceTextureManager11; d2D1PropertyBinding[13].setFunction = PixelShaderEffect.SetResourceTextureManager12; d2D1PropertyBinding[14].setFunction = PixelShaderEffect.SetResourceTextureManager13; d2D1PropertyBinding[15].setFunction = PixelShaderEffect.SetResourceTextureManager14; d2D1PropertyBinding[16].setFunction = PixelShaderEffect.SetResourceTextureManager15; fixed (Guid* pGuid = &PixelShaderEffect.For.Id) { // Register the effect ((ID2D1Factory1*)d2D1Factory1)->RegisterEffectFromString( classId: pGuid, propertyXml: (ushort*)pXml, bindings: d2D1PropertyBinding, bindingsCount: 17, effectFactory: PixelShaderEffect.For.Factory).Assert(); } effectId = PixelShaderEffect.For.Id; } } /// /// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to register. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader { return GetRegistrationBlob(null, out effectId); } /// /// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to register. /// The type of implementation to register. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// For more info and for details on the binary format, see . public static ReadOnlyMemory GetRegistrationBlob(out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { return GetRegistrationBlob(static () => new TMapper(), out effectId); } /// /// Gets a binary blob containing serialized information that can be used to register an effect, by using ID2D1Factory1::RegisterEffectFromString. /// /// The type of D2D1 pixel shader to get the registration blob for. /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. /// A blob containing serialized information that can be used to register an effect. /// Thrown if an effect is registered multiple times with different properties. /// /// /// This blob can be useful to marshal registration data across an app domain boundary without having assembly references cross this boundary as well. /// /// /// For instance, an application could allow plugins to create effects through the ComputeSharp.D2D1 APIs, and to then create an effect registration /// blob to pass to the main application, which could then use it to register the effect. This way, even if a plugin was loaded with a separate assembly load /// context and using a different (potentially incompatible) version of ComputeSharp.D2D1, the two could still work side by side with no issues. /// /// /// The binary blob contains information with the following format: /// /// The blob version id (a ). /// The effect id (a ). /// The number of inputs for the effect (an ). /// The effect XML description (as null-terminated UTF8 text). /// The number of property bindings (an ). /// The property name (as null-terminated UTF8 text). /// The property getter (a ). /// The property setter (a ). /// The effect factory (a ). /// /// The property name, getter and setter are grouped together after the number of bindings. /// /// /// To make the deserialization easier, the type can be used to read and validate the returned blob. /// The leading blob id will determine what subtype should be used to deserialize the blob (eg. ). /// /// /// For more info, see . /// /// public static ReadOnlyMemory GetRegistrationBlob(Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { effectId = default; PixelShaderEffect.For.Initialize(mapperFactory); using ArrayPoolBinaryWriter writer = new(ArrayPoolBinaryWriter.DefaultInitialBufferSize); // Blob id writer.Write(D2D1EffectRegistrationData.V1.BlobId); // Effect id and number of inputs writer.Write(PixelShaderEffect.For.Id); writer.Write(PixelShaderEffect.For.InputCount); // Build the XML text writer.WriteAsUtf8(@" "); // Add the input nodes for (int i = 0; i < PixelShaderEffect.For.InputCount; i++) { writer.WriteAsUtf8(""); } // Write the last part of the XML (including the buffer property) writer.WriteAsUtf8(@" "); // Null terminator for the text writer.Write((byte)'\0'); // Bindings writer.Write(17); writer.WriteAsUtf8("Buffer"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetConstantBuffer); writer.Write((nint)PixelShaderEffect.SetConstantBuffer); writer.WriteAsUtf8("ResourceTextureManager0"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager0); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager0); writer.WriteAsUtf8("ResourceTextureManager1"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager1); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager1); writer.WriteAsUtf8("ResourceTextureManager2"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager2); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager2); writer.WriteAsUtf8("ResourceTextureManager3"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager3); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager3); writer.WriteAsUtf8("ResourceTextureManager4"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager4); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager4); writer.WriteAsUtf8("ResourceTextureManager5"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager5); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager5); writer.WriteAsUtf8("ResourceTextureManager6"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager6); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager6); writer.WriteAsUtf8("ResourceTextureManager7"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager7); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager7); writer.WriteAsUtf8("ResourceTextureManager8"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager8); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager8); writer.WriteAsUtf8("ResourceTextureManager9"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager9); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager9); writer.WriteAsUtf8("ResourceTextureManager10"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager10); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager10); writer.WriteAsUtf8("ResourceTextureManager11"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager11); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager11); writer.WriteAsUtf8("ResourceTextureManager12"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager12); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager12); writer.WriteAsUtf8("ResourceTextureManager13"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager13); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager13); writer.WriteAsUtf8("ResourceTextureManager14"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager14); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager14); writer.WriteAsUtf8("ResourceTextureManager15"); writer.Write((byte)'\0'); writer.Write((nint)PixelShaderEffect.GetResourceTextureManager15); writer.Write((nint)PixelShaderEffect.SetResourceTextureManager15); // Effect factory writer.Write((nint)PixelShaderEffect.For.Factory); effectId = PixelShaderEffect.For.Id; return writer.WrittenSpan.ToArray(); } /// /// Creates an effect wrapping an input D2D1 pixel shader, by calling ID2D1DeviceContext::CreateEffect. /// /// The type of D2D1 pixel shader to create an effect for. /// A pointer to the ID2D1DeviceContext instance to use. /// A pointer to the resulting ID2D1Effect* pointer to produce. /// Thrown if or are . /// /// Thrown if this method is called before initializing a shader effect for the shader type . /// To do so, make sure to call either (or an overload), or /// to use (or an overload), and use that blob to register an effect. /// /// For more info, see . public static void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { if (d2D1DeviceContext is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1DeviceContext), "The input ID2D1DeviceContext object cannot be null."); } if (d2D1Effect is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The pointer to the target ID2D1Effect result cannot be null."); } if (!PixelShaderEffect.For.TryGetId(out Guid id)) { ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet."); } ((ID2D1DeviceContext*)d2D1DeviceContext)->CreateEffect( effectId: &id, effect: (ID2D1Effect**)d2D1Effect).Assert(); } /// /// Sets the constant buffer from an input D2D1 effect, by calling ID2D1Effect::SetValue. /// /// The type of D2D1 pixel shader to set the constant buffer for. /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1Effect instance to use. /// Thrown if is . /// For more info, see . public static void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { if (d2D1Effect is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1DeviceContext object cannot be null."); } D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); } /// /// Sets the resource texture manager from an input D2D1 effect, by calling ID2D1Effect::SetValue. /// /// A pointer to the ID2D1Effect instance to use. /// The input ID2D1ResourceTextureManager object (see ). /// The index of the resource texture to assign the resource texture manager to. /// Thrown if or are . /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, void* resourceTextureManager, int resourceTextureIndex) { if (d2D1Effect is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); } if (resourceTextureManager is null) { ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input ID2D1ResourceTextureManager object cannot be null."); } ((ID2D1Effect*)d2D1Effect)->SetValue( index: D2D1PixelShaderEffectProperty.ResourceTextureManager0 + (uint)resourceTextureIndex, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)&resourceTextureManager, dataSize: (uint)sizeof(void*)).Assert(); } /// /// Sets the resource texture manager from an input D2D1 effect, by calling ID2D1Effect::SetValue. /// /// A pointer to the ID2D1Effect instance to use. /// The input instance.. /// The index of the resource texture to assign the resource texture manager to. /// Thrown if or are . /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, D2D1ResourceTextureManager resourceTextureManager, int resourceTextureIndex) { if (d2D1Effect is null) { ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); } if (resourceTextureManager is null) { ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input D2D1ResourceTextureManager object cannot be null."); } using ComPtr resourceTextureManager2 = default; resourceTextureManager.GetD2D1ResourceTextureManager(resourceTextureManager2.GetAddressOf()); ((ID2D1Effect*)d2D1Effect)->SetValue( index: D2D1PixelShaderEffectProperty.ResourceTextureManager0 + (uint)resourceTextureIndex, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)resourceTextureManager2.GetAddressOf(), dataSize: (uint)sizeof(void*)).Assert(); } }