Merge pull request #355 from Sergio0694/dev/fix-resource-texture-reuse
Fix resource texture manager reuse
このコミットが含まれているのは:
6ファイル変更
+126
-16
検索条件に一致するファイルはありません
+10
-1
@@ -170,6 +170,8 @@ partial struct D2D1ResourceTextureManagerImpl
|
||||
return E.E_NOT_VALID_STATE;
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1ResourceTexture> d2D1ResourceTexture = default;
|
||||
|
||||
// Create the resource now, as it hasn't been created yet
|
||||
int result = @this->d2D1EffectContext->CreateResourceTexture(
|
||||
resourceId: @this->resourceId,
|
||||
@@ -177,12 +179,19 @@ partial struct D2D1ResourceTextureManagerImpl
|
||||
data: @this->data,
|
||||
strides: @this->strides,
|
||||
dataSize: @this->dataSize,
|
||||
resourceTexture: resourceTexture);
|
||||
resourceTexture: d2D1ResourceTexture.GetAddressOf());
|
||||
|
||||
// If creation was successful, release the buffered data. Going forwards,
|
||||
// the resource texture will be used directly for all updates requested.
|
||||
if (result == S.S_OK)
|
||||
{
|
||||
// Store the resource texture for later
|
||||
d2D1ResourceTexture.CopyTo(&@this->d2D1ResourceTexture);
|
||||
|
||||
// Also return it to callers
|
||||
d2D1ResourceTexture.CopyTo(resourceTexture);
|
||||
|
||||
// Free the staging buffers
|
||||
NativeMemory.Free(@this->resourceTextureProperties.extents);
|
||||
NativeMemory.Free(@this->resourceTextureProperties.extendModes);
|
||||
NativeMemory.Free(@this->data);
|
||||
|
||||
+7
-1
@@ -234,7 +234,6 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
|
||||
// Enter the lock, and then free the effect context. That is guaranteed to
|
||||
// not be null here, as it is only ever set if ID2D1Multithread is retrieved.
|
||||
this.d2D1Multithread->Enter();
|
||||
this.d2D1EffectContext->Release();
|
||||
|
||||
// Release the resource too if it has been created
|
||||
if (this.d2D1ResourceTexture is not null)
|
||||
@@ -242,7 +241,14 @@ internal unsafe partial struct D2D1ResourceTextureManagerImpl
|
||||
this.d2D1ResourceTexture->Release();
|
||||
}
|
||||
|
||||
// Now that the resource, if any, has been released, the effect context can also
|
||||
// be released. This must be done only after any associated resource textures have
|
||||
// been released. Releasing an effect context first, in case it was the last reference
|
||||
// to it, will otherwise cause the Release() call on the resource texture to explode.
|
||||
this.d2D1EffectContext->Release();
|
||||
|
||||
this.d2D1Multithread->Leave();
|
||||
this.d2D1Multithread->Release();
|
||||
}
|
||||
|
||||
if (this.resourceId is not null)
|
||||
|
||||
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 444 KiB |
バイナリファイルは表示されません。
|
変更後 幅: | 高さ: | サイズ: 328 KiB |
@@ -38,16 +38,10 @@
|
||||
<None Update="Assets\Landscape_Pixelate.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\nature_part0.png">
|
||||
<None Update="Assets\Wallpaper0.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\nature_part1.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\nature_part2.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\nature_part3.png">
|
||||
<None Update="Assets\Wallpaper1.png">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="Assets\WallpapersStack.png">
|
||||
|
||||
@@ -214,7 +214,9 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadPixelsFromResourceTexture2D_CreateBeforeGettingEffectContext()
|
||||
[DataRow(1)]
|
||||
[DataRow(2)]
|
||||
public unsafe void LoadPixelsFromResourceTexture2D_CreateBeforeGettingEffectContext(int drawCount)
|
||||
{
|
||||
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
|
||||
using ComPtr<ID2D1Device> d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
|
||||
@@ -242,6 +244,8 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height);
|
||||
|
||||
D2D1ResourceTextureManager.Initialize(
|
||||
resourceTextureManager: resourceTextureManager.Get(),
|
||||
resourceId: Guid.NewGuid(),
|
||||
@@ -253,15 +257,20 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
data: MemoryMarshal.AsBytes(pixels.Span),
|
||||
strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected.Width) });
|
||||
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0);
|
||||
for (int i = 0; i < drawCount; i++)
|
||||
{
|
||||
// We need to reassign the resource texture manager before each draw, so the D2D runtime can see
|
||||
// some effect property has changed, and it will call PrepareForRender. That's where the resource
|
||||
// texture will be retrieved. When drawCount is more than 1, this test is used to validate that
|
||||
// getting the resource texture multiple times from the same manager also works correctly.
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0);
|
||||
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected.Width, (uint)expected.Height);
|
||||
|
||||
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
|
||||
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
|
||||
}
|
||||
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
string destinationPath = Path.Combine(assemblyPath, "temp", "IndexedFromResourceTexture2D_Before.png");
|
||||
string destinationPath = Path.Combine(assemblyPath, "temp", $"IndexedFromResourceTexture2D_Before_Draw{drawCount}.png");
|
||||
|
||||
_ = Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
|
||||
|
||||
@@ -328,6 +337,98 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public unsafe void LoadPixelsFromResourceTexture2D_AlternatingResourceTextureManagers()
|
||||
{
|
||||
using ComPtr<ID2D1Factory2> d2D1Factory2 = D2D1Helper.CreateD2D1Factory2();
|
||||
using ComPtr<ID2D1Device> d2D1Device = D2D1Helper.CreateD2D1Device(d2D1Factory2.Get());
|
||||
using ComPtr<ID2D1DeviceContext> d2D1DeviceContext = D2D1Helper.CreateD2D1DeviceContext(d2D1Device.Get());
|
||||
|
||||
D2D1PixelShaderEffect.RegisterForD2D1Factory1<IndexFrom2DResourceTextureShader>(d2D1Factory2.Get(), null, out _);
|
||||
|
||||
string assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
|
||||
string wallpaper0Path = Path.Combine(assemblyPath, "Assets", "Wallpaper0.png");
|
||||
string wallpaper1Path = Path.Combine(assemblyPath, "Assets", "Wallpaper1.png");
|
||||
|
||||
using Image<Rgba32> expected0 = Image.Load<Rgba32>(wallpaper0Path);
|
||||
using Image<Rgba32> expected1 = Image.Load<Rgba32>(wallpaper1Path);
|
||||
|
||||
Memory<Rgba32> pixels0 = Memory<Rgba32>.Empty;
|
||||
Memory<Rgba32> pixels1 = Memory<Rgba32>.Empty;
|
||||
|
||||
if (!expected0.DangerousTryGetSinglePixelMemory(out pixels0) ||
|
||||
!expected1.DangerousTryGetSinglePixelMemory(out pixels1))
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
using ComPtr<IUnknown> resourceTextureManager0 = default;
|
||||
using ComPtr<IUnknown> resourceTextureManager1 = default;
|
||||
|
||||
D2D1ResourceTextureManager.Create((void**)resourceTextureManager0.GetAddressOf());
|
||||
D2D1ResourceTextureManager.Create((void**)resourceTextureManager1.GetAddressOf());
|
||||
|
||||
D2D1ResourceTextureManager.Initialize(
|
||||
resourceTextureManager: resourceTextureManager0.Get(),
|
||||
resourceId: Guid.NewGuid(),
|
||||
extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height },
|
||||
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
|
||||
channelDepth: D2D1ChannelDepth.Four,
|
||||
filter: D2D1Filter.MinMagMipPoint,
|
||||
extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp },
|
||||
data: MemoryMarshal.AsBytes(pixels0.Span),
|
||||
strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) });
|
||||
|
||||
D2D1ResourceTextureManager.Initialize(
|
||||
resourceTextureManager: resourceTextureManager1.Get(),
|
||||
resourceId: Guid.NewGuid(),
|
||||
extents: stackalloc[] { (uint)expected0.Width, (uint)expected0.Height },
|
||||
bufferPrecision: D2D1BufferPrecision.UInt8Normalized,
|
||||
channelDepth: D2D1ChannelDepth.Four,
|
||||
filter: D2D1Filter.MinMagMipPoint,
|
||||
extendModes: stackalloc[] { D2D1ExtendMode.Clamp, D2D1ExtendMode.Clamp },
|
||||
data: MemoryMarshal.AsBytes(pixels1.Span),
|
||||
strides: stackalloc[] { (uint)(sizeof(Rgba32) * expected0.Width) });
|
||||
|
||||
using (ComPtr<ID2D1Effect> d2D1Effect = default)
|
||||
{
|
||||
D2D1PixelShaderEffect.CreateFromD2D1DeviceContext<IndexFrom2DResourceTextureShader>(d2D1DeviceContext.Get(), (void**)d2D1Effect.GetAddressOf());
|
||||
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), (uint)expected0.Width, (uint)expected0.Height);
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
ref readonly ComPtr<IUnknown> resourceTextureManager = ref i % 2 == 0
|
||||
? ref resourceTextureManager0
|
||||
: ref resourceTextureManager1;
|
||||
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(d2D1Effect.Get(), resourceTextureManager.Get(), 0);
|
||||
|
||||
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
|
||||
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
string destinationPath = Path.Combine(assemblyPath, "temp", $"LoadPixelsFromResourceTexture2D_Alternating{i}.png");
|
||||
|
||||
_ = Directory.CreateDirectory(Path.GetDirectoryName(destinationPath)!);
|
||||
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, (uint)expected0.Width, (uint)expected0.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
|
||||
string expectedPath = i % 2 == 0 ? wallpaper0Path : wallpaper1Path;
|
||||
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
}
|
||||
}
|
||||
|
||||
_ = resourceTextureManager0.Get()->AddRef();
|
||||
|
||||
Assert.AreEqual(1u, resourceTextureManager0.Get()->Release());
|
||||
|
||||
_ = resourceTextureManager1.Get()->AddRef();
|
||||
|
||||
Assert.AreEqual(1u, resourceTextureManager1.Get()->Release());
|
||||
}
|
||||
|
||||
[D2DInputCount(0)]
|
||||
[D2DRequiresScenePosition]
|
||||
private partial struct IndexFrom2DResourceTextureShader : ID2D1PixelShader
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする