From aa21ceb0a69f09d7d90af260f242f16ff7b6ea95 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 11 Aug 2022 13:38:12 +0200 Subject: [PATCH] Implement ID2D1ResourceTextureManagerInternal --- ...ger.ID2D1ResourceTextureManagerInternal.cs | 65 ++++++++++++++++++- .../Windows/shared/winerror/E.cs | 3 + .../Windows/shared/winerror/S.cs | 6 -- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs index e0a101b6..4e30520d 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Effects/ResourceManagers/ResourceTextureManager.ID2D1ResourceTextureManagerInternal.cs @@ -87,7 +87,25 @@ partial struct ResourceTextureManager [UnmanagedCallersOnly] public static int Initialize(ResourceTextureManager* @this, ID2D1EffectContext* effectContext) { - return 0; + if (effectContext is null) + { + return E.E_POINTER; + } + + effectContext->AddRef(); + + // If this is the first time the method is called, just store the context + if (@this->d2D1EffectContext is null) + { + @this->d2D1EffectContext = effectContext; + + return S.S_OK; + } + + // Otherwise, just do nothing and return S_FALSE. This allows an existing resource texture + // manager to be shared across effects. If the resource cannot be shared, it will just + // return an error when effect is actually set, with ID2D1DrawInfo::SetResourceTexture. + return S.S_FALSE; } /// @@ -99,7 +117,50 @@ partial struct ResourceTextureManager [UnmanagedCallersOnly] public static int GetResourceTexture(ResourceTextureManager* @this, ID2D1ResourceTexture** resourceTexture) { - return 0; + if (*resourceTexture is not null) + { + return E.E_POINTER; + } + + // If the effect context is null, it means Initialize has not been called yet + if (@this->d2D1EffectContext is null) + { + return E.E_NOT_VALID_STATE; + } + + // If the texture has already been created, just return it + if (@this->d2D1ResourceTexture is not null) + { + @this->d2D1ResourceTexture->AddRef(); + + *resourceTexture = @this->d2D1ResourceTexture; + } + + // If the data is null at this point, it means CreateResourceTexture has not been called yet + if (@this->data is null) + { + return E.E_NOT_VALID_STATE; + } + + // Create the resource now, as it hasn't been created yet + int result = @this->d2D1EffectContext->CreateResourceTexture( + resourceId: @this->resourceId, + resourceTextureProperties: &@this->resourceTextureProperties, + data: @this->data, + strides: @this->strides, + dataSize: @this->dataSize, + resourceTexture: resourceTexture); + + // If creation was successful, release the buffered data + if (result == S.S_OK) + { + NativeMemory.Free(@this->resourceTextureProperties.extents); + NativeMemory.Free(@this->resourceTextureProperties.extendModes); + NativeMemory.Free(@this->data); + NativeMemory.Free(@this->strides); + } + + return S.S_OK; } } } diff --git a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs index a8da79de..6190c538 100644 --- a/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs +++ b/src/TerraFX.Interop.Windows.D2D1/Windows/shared/winerror/E.cs @@ -7,6 +7,9 @@ namespace TerraFX.Interop.Windows { internal static class E { + [NativeTypeName("#define E_NOT_VALID_STATE HRESULT_FROM_WIN32(ERROR_INVALID_STATE)")] + public const int E_NOT_VALID_STATE = -2147019873; + [NativeTypeName("#define E_NOTIMPL _HRESULT_TYPEDEF_(0x80004001L)")] public const int E_NOTIMPL = unchecked((int)(0x80004001)); diff --git a/src/TerraFX.Interop.Windows/Windows/shared/winerror/S.cs b/src/TerraFX.Interop.Windows/Windows/shared/winerror/S.cs index 04be0f93..ed02e8c5 100644 --- a/src/TerraFX.Interop.Windows/Windows/shared/winerror/S.cs +++ b/src/TerraFX.Interop.Windows/Windows/shared/winerror/S.cs @@ -12,11 +12,5 @@ namespace TerraFX.Interop.Windows [NativeTypeName("#define S_FALSE ((HRESULT)1L)")] public const int S_FALSE = ((int)(1)); - - [NativeTypeName("#define S_STORE_LAUNCHED_FOR_REMEDIATION _HRESULT_TYPEDEF_(0x00270258L)")] - public const int S_STORE_LAUNCHED_FOR_REMEDIATION = ((int)(0x00270258)); - - [NativeTypeName("#define S_APPLICATION_ACTIVATION_ERROR_HANDLED_BY_DIALOG _HRESULT_TYPEDEF_(0x00270259L)")] - public const int S_APPLICATION_ACTIVATION_ERROR_HANDLED_BY_DIALOG = ((int)(0x00270259)); } }