using System;
using System.Runtime.InteropServices;
using TerraFX.Interop.DirectX;
using TerraFX.Interop.Windows;
#if !NET6_0_OR_GREATER
using UnmanagedCallersOnlyAttribute = ComputeSharp.NetStandard.System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute;
#endif
namespace ComputeSharp.D2D1.Shaders.Interop.Effects.ResourceManagers;
///
partial struct ResourceTextureManager
{
///
/// The implementation for ID2D1ResourceTextureManager.
///
private static unsafe class ID2D1ResourceTextureManagerMethods
{
#if !NET6_0_OR_GREATER
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int InitializeDelegate(
ResourceTextureManager* @this,
Guid* resourceId,
D2D1_RESOURCE_TEXTURE_PROPERTIES* resourceTextureProperties,
byte* data,
uint* strides,
uint dataSize);
///
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int UpdateDelegate(
ResourceTextureManager* @this,
uint* minimumExtents,
uint* maximumExtents,
uint* strides,
uint dimensions,
byte* data,
uint dataCount);
///
/// A cached instance wrapping .
///
public static readonly QueryInterfaceDelegate QueryInterfaceWrapper = QueryInterface;
///
/// A cached instance wrapping .
///
public static readonly AddRefDelegate AddRefWrapper = AddRef;
///
/// A cached instance wrapping .
///
public static readonly ReleaseDelegate ReleaseWrapper = Release;
///
/// A cached instance wrapping .
///
public static readonly InitializeDelegate InitializeWrapper = Initialize;
///
/// A cached instance wrapping .
///
public static readonly UpdateDelegate UpdateWrapper = Update;
#endif
///
[UnmanagedCallersOnly]
public static int QueryInterface(ResourceTextureManager* @this, Guid* riid, void** ppvObject)
{
return @this->QueryInterface(riid, ppvObject);
}
///
[UnmanagedCallersOnly]
public static uint AddRef(ResourceTextureManager* @this)
{
return @this->AddRef();
}
///
[UnmanagedCallersOnly]
public static uint Release(ResourceTextureManager* @this)
{
return @this->Release();
}
///
/// Creates or finds the given resource texture, depending on whether a resource id is specified.
/// It also optionally initializes the texture with the specified data.
///
/// The current ID2D1ResourceTextureManager instance.
/// An optional pointer to the unique id that identifies the resource texture.
/// The properties used to create the resource texture.
/// The optional data to be loaded into the resource texture.
/// An optional pointer to the stride to advance through the resource texture, according to dimension.
/// The size, in bytes, of the data.
/// An for the operation.
[UnmanagedCallersOnly]
public static int Initialize(
ResourceTextureManager* @this,
Guid* resourceId,
D2D1_RESOURCE_TEXTURE_PROPERTIES* resourceTextureProperties,
byte* data,
uint* strides,
uint dataSize)
{
// Return E_POINTER if any input pointer is null
if (resourceTextureProperties is null ||
data is null ||
strides is null ||
resourceTextureProperties->extents is null ||
resourceTextureProperties->extendModes is null)
{
return E.E_POINTER;
}
// If the method has already been called, fail
if (@this->d2D1ResourceTexture is not null ||
@this->data is not null)
{
return E.E_NOT_VALID_STATE;
}
// If Initialize has already been called, just forward the call
if (@this->d2D1EffectContext is not null)
{
return @this->d2D1EffectContext->CreateResourceTexture(
resourceId: resourceId,
resourceTextureProperties: resourceTextureProperties,
data: data,
strides: strides,
dataSize: dataSize,
resourceTexture: &@this->d2D1ResourceTexture);
}
@this->resourceId = (Guid*)NativeMemory.Alloc((nuint)sizeof(Guid));
*@this->resourceId = *resourceId;
uint* extents = (uint*)NativeMemory.Alloc(sizeof(uint) * resourceTextureProperties->dimensions);
D2D1_EXTEND_MODE* extendModes = (D2D1_EXTEND_MODE*)NativeMemory.Alloc(sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions);
Buffer.MemoryCopy(
source: resourceTextureProperties->extents,
destination: extents,
destinationSizeInBytes: sizeof(uint) * resourceTextureProperties->dimensions,
sourceBytesToCopy: sizeof(uint) * resourceTextureProperties->dimensions);
Buffer.MemoryCopy(
source: resourceTextureProperties->extendModes,
destination: extendModes,
destinationSizeInBytes: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions,
sourceBytesToCopy: sizeof(D2D1_EXTEND_MODE) * resourceTextureProperties->dimensions);
@this->resourceTextureProperties.extents = extents;
@this->resourceTextureProperties.dimensions = resourceTextureProperties->dimensions;
@this->resourceTextureProperties.bufferPrecision = resourceTextureProperties->bufferPrecision;
@this->resourceTextureProperties.channelDepth = resourceTextureProperties->channelDepth;
@this->resourceTextureProperties.filter = resourceTextureProperties->filter;
@this->resourceTextureProperties.extendModes = extendModes;
@this->data = (byte*)NativeMemory.Alloc(dataSize);
Buffer.MemoryCopy(data, @this->data, dataSize, dataSize);
@this->strides = (uint*)NativeMemory.Alloc(sizeof(uint) * resourceTextureProperties->dimensions);
Buffer.MemoryCopy(
source: strides,
destination: @this->strides,
destinationSizeInBytes: sizeof(uint) * resourceTextureProperties->dimensions,
sourceBytesToCopy: sizeof(uint) * resourceTextureProperties->dimensions);
return 0;
}
///
/// Updates the specific resource texture inside the specific range or box using the supplied data.
///
/// The current ID2D1ResourceTextureManager instance.
/// The "left" extent of the updates if specified. If , the entire texture is updated.
/// The "right" extent of the updates if specified. If , the entire texture is updated.
/// The stride to advance through the input data, according to dimension.
/// The number of dimensions in the resource texture. This must match the number used to load the texture.
/// The data to be placed into the resource texture.
/// The size of the data buffer to be used to update the resource texture.
/// An for the operation.
[UnmanagedCallersOnly]
public static int Update(
ResourceTextureManager* @this,
uint* minimumExtents,
uint* maximumExtents,
uint* strides,
uint dimensions,
byte* data,
uint dataCount)
{
// If a resource texture already exists, just forward the call
if (@this->d2D1ResourceTexture is not null)
{
return @this->d2D1ResourceTexture->Update(
minimumExtents: minimumExtents,
maximimumExtents: maximumExtents,
strides: strides,
dimensions: dimensions,
data: data,
dataCount: dataCount);
}
// TODO: implement Update over buffered data
return 0;
}
}
}