Skip to content

Commit

Permalink
Merge pull request #586 from Sergio0694/dev/only-valid-xml-props
Browse files Browse the repository at this point in the history
Only declare valid resource texture manager properties in effect XML
  • Loading branch information
Sergio0694 authored Oct 1, 2023
2 parents 678f197 + be17050 commit 4ab55e8
Show file tree
Hide file tree
Showing 8 changed files with 368 additions and 351 deletions.
300 changes: 172 additions & 128 deletions src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,115 +17,124 @@ public static class D2D1PixelShaderEffectProperty
/// </remarks>
public const uint ConstantBuffer = 0;

/// <summary>
/// The index for the <c>ID2D1TransformMapper</c> property of a <see cref="D2D1PixelShaderEffect"/> object.
/// </summary>
/// <remarks>
/// <para>The value of this property is of type <c>D2D1_PROPERTY_TYPE_IUNKNOWN</c>.</para>
/// <para>This value can be passed when calling <c>ID2D1Effect::SetValue</c>.</para>
/// <para>For more info, see <see href="https://learn.microsoft.com/en-us/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1properties-setvalue(uint32_d2d1_property_type_constbyte_uint32)"/>.</para>
/// </remarks>
public const uint TransformMapper = 1;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 0.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager0 = 1;
/// <remarks>
/// <para>For more info, see <see cref="D2D1ResourceTextureManager"/>.</para>
/// <para>This property is only available if a shader declares at least N + 1 resource textures.</para>
/// </remarks>
public const uint ResourceTextureManager0 = 2;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 1.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager1 = 2;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager1 = 3;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 2.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager2 = 3;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager2 = 4;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 3.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager3 = 4;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager3 = 5;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 4.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager4 = 5;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager4 = 6;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 5.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager5 = 6;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager5 = 7;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 6.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager6 = 7;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager6 = 8;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 7.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager7 = 8;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager7 = 9;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 8.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager8 = 9;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager8 = 10;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 9.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager9 = 10;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager9 = 11;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 10.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager10 = 11;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager10 = 12;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 11.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager11 = 12;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager11 = 13;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 12.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager12 = 13;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager12 = 14;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 13.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager13 = 14;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager13 = 15;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 14.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager14 = 15;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager14 = 16;

/// <summary>
/// The index for the <c>ID2D1ResourceTextureManager</c> instance for the resource texture at index 15.
/// </summary>
/// <remarks>For more info, see <see cref="D2D1ResourceTextureManager"/>.</remarks>
public const uint ResourceTextureManager15 = 16;
/// <remarks><inheritdoc cref="ResourceTextureManager0" path="/remarks/node()"/></remarks>
public const uint ResourceTextureManager15 = 17;

/// <summary>
/// The index for the <c>ID2D1TransformMapper</c> property of a <see cref="D2D1PixelShaderEffect"/> object.
/// The maximum total number of properties.
/// </summary>
/// <remarks>
/// <para>The value of this property is of type <c>D2D1_PROPERTY_TYPE_IUNKNOWN</c>.</para>
/// <para>This value can be passed when calling <c>ID2D1Effect::SetValue</c>.</para>
/// <para>For more info, see <see href="https://learn.microsoft.com/en-us/windows/win32/api/d2d1_1/nf-d2d1_1-id2d1properties-setvalue(uint32_d2d1_property_type_constbyte_uint32)"/>.</para>
/// </remarks>
public const uint TransformMapper = 17;
/// <remarks>This should always be the value of the last property above, plus 1.</remarks>
internal const int MaximumNumberOfAvailableProperties = 18;

/// <summary>
/// The total number of properties.
/// The number of always available properties.
/// </summary>
/// <remarks>This should always be the value of the last property above, plus 1.</remarks>
internal const uint NumberOfProperties = 18;
/// <remarks>These are <see cref="ConstantBuffer"/> and <see cref="TransformMapper"/>.</remarks>
internal const int NumberOfAlwaysAvailableProperties = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ public static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE ch

if (Windows.SUCCEEDED(hresult))
{
foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan<D2D1ResourceTextureDescription>(@this->resourceTextureDescriptions, @this->resourceTextureDescriptionCount))
for (int i = 0; i < @this->resourceTextureDescriptionCount; i++)
{
using ComPtr<ID2D1ResourceTextureManager> resourceTextureManager = @this->resourceTextureManagerBuffer[resourceTextureDescription.Index];
using ComPtr<ID2D1ResourceTextureManager> resourceTextureManager = @this->resourceTextureManagerBuffer[i];

// If the current resource texture manager is not set, we cannot render, as there's an unbound resource texture
if (resourceTextureManager.Get() is null)
Expand Down Expand Up @@ -180,6 +180,8 @@ public static int PrepareForRender(PixelShaderEffect* @this, D2D1_CHANGE_TYPE ch
break;
}

ref readonly D2D1ResourceTextureDescription resourceTextureDescription = ref @this->resourceTextureDescriptions[i];

// Set the ID2D1ResourceTexture object to the current index in the ID2D1DrawInfo object in use
hresult = @this->d2D1DrawInfo->SetResourceTexture(
textureIndex: (uint)resourceTextureDescription.Index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ private static int SetTransformMapperImpl(IUnknown* effect, byte* data, uint dat
/// <summary>
/// Gets the resource texture manager for a given index.
/// </summary>
/// <param name="resourceTextureIndex">The index of the resource texture to get the manager for.</param>
/// <param name="index">The index of the resource texture manager to assign the resource texture manager to (this might not match the resource texture index).</param>
/// <param name="data">A pointer to a variable that stores the data that this function retrieves on the property.</param>
/// <param name="dataSize">The number of bytes in the property to retrieve.</param>
/// <param name="actualSize">A optional pointer to a variable that stores the actual number of bytes retrieved on the property.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
private int GetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* data, uint dataSize, uint* actualSize)
private int GetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize, uint* actualSize)
{
if (!IsResourceTextureManagerIndexValid(resourceTextureIndex, out _))
if (index >= this.resourceTextureDescriptionCount)
{
return E.E_INVALIDARG;
}
Expand All @@ -299,7 +299,7 @@ private int GetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* dat
return E.E_INVALIDARG;
}

using ComPtr<ID2D1ResourceTextureManager> resourceTextureManager = this.resourceTextureManagerBuffer[resourceTextureIndex];
using ComPtr<ID2D1ResourceTextureManager> resourceTextureManager = this.resourceTextureManagerBuffer[index];

HRESULT hresult = resourceTextureManager.CopyTo((ID2D1ResourceTextureManager**)data);

Expand All @@ -319,13 +319,13 @@ private int GetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* dat
/// <summary>
/// Sets the resource texture manager for a given index.
/// </summary>
/// <param name="resourceTextureIndex">The index of the resource texture to set the manager for.</param>
/// <param name="index">The index of the resource texture manager to assign the resource texture manager to (this might not match the resource texture index).</param>
/// <param name="data">A pointer to the data to be set on the property.</param>
/// <param name="dataSize">The number of bytes in the property set by the function.</param>
/// <returns>The <see cref="HRESULT"/> for the operation.</returns>
private int SetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* data, uint dataSize)
private int SetResourceTextureManagerAtIndex(int index, byte* data, uint dataSize)
{
if (!IsResourceTextureManagerIndexValid(resourceTextureIndex, out uint dimensions))
if (index >= this.resourceTextureDescriptionCount)
{
return E.E_INVALIDARG;
}
Expand Down Expand Up @@ -371,6 +371,8 @@ private int SetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* dat
// Initialize the resource texture manager, if an effect context is available
if (this.d2D1EffectContext is not null)
{
uint dimensions = (uint)this.resourceTextureDescriptions[index].Dimensions;

result = resourceTextureManagerInternal.Get()->Initialize(this.d2D1EffectContext, &dimensions);

// ID2D1ResourceTextureManager::Initialize should generally return either S_OK for first
Expand All @@ -384,7 +386,7 @@ private int SetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* dat
}
}

ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[resourceTextureIndex];
ref ID2D1ResourceTextureManager* currentResourceTextureManager = ref this.resourceTextureManagerBuffer[index];

// If there's already an existing manager at this index, release it
if (currentResourceTextureManager is not null)
Expand All @@ -397,27 +399,4 @@ private int SetResourceTextureManagerAtIndex(int resourceTextureIndex, byte* dat

return S.S_OK;
}

/// <summary>
/// Checks whether a given index for a resource texture manager is valid for the current effect.
/// </summary>
/// <param name="resourceTextureIndex">The index of the resource texture to validate.</param>
/// <param name="dimensions">The number of dimensions for the resource texture at the gven index.</param>
/// <returns>Whether or not <paramref name="resourceTextureIndex"/> is valid for the current effect.</returns>
private readonly bool IsResourceTextureManagerIndexValid(int resourceTextureIndex, out uint dimensions)
{
foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan<D2D1ResourceTextureDescription>(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount))
{
if (resourceTextureDescription.Index == resourceTextureIndex)
{
dimensions = (uint)resourceTextureDescription.Dimensions;

return true;
}
}

dimensions = 0;

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,10 @@ private uint Release()
_ = this.d2D1EffectContext->Release();
}

// Use the list of resource texture descriptions to see the indices that might have accepted a resource texture manager.
// Then, retrieve all of them and release the ones that had been assigned (from one of the property bindings).
foreach (ref readonly D2D1ResourceTextureDescription resourceTextureDescription in new ReadOnlySpan<D2D1ResourceTextureDescription>(this.resourceTextureDescriptions, this.resourceTextureDescriptionCount))
// Retrieve all possible resource texture managers in use and release the ones that had been assigned (from one of the property bindings)
for (int i = 0; i < this.resourceTextureDescriptionCount; i++)
{
ID2D1ResourceTextureManager* resourceTextureManager = this.resourceTextureManagerBuffer[resourceTextureDescription.Index];
ID2D1ResourceTextureManager* resourceTextureManager = this.resourceTextureManagerBuffer[i];

if (resourceTextureManager is not null)
{
Expand Down
Loading

0 comments on commit 4ab55e8

Please sign in to comment.