Skip to content

Commit d31dcda

Browse files
[Editor] Fix ImGui external texture handling;
1 parent afec35f commit d31dcda

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

Engine/Staple.Editor/ImGuiProxy.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ internal class ImGuiProxy
2626
public ImDrawVert[] vertices = [];
2727
public ushort[] indices = [];
2828
public readonly Dictionary<int, (Texture, byte[], int)> textures = [];
29+
public readonly Dictionary<int, Texture> registeredTextures = [];
30+
public readonly Dictionary<Texture, int> registeredTexturesInverse = [];
2931
private int textureCounter = 1;
3032

31-
private Texture[] emptyTexture = [];
32-
private Texture[] singleTexture = new Texture[1];
33+
private readonly Texture[] emptyTexture = [];
34+
private readonly Texture[] singleTexture = new Texture[1];
3335

3436
public ImFontPtr editorFont;
3537

@@ -605,6 +607,10 @@ private void Render(ImDrawDataPtr drawData)
605607
{
606608
singleTexture[0] = item.Item1;
607609
}
610+
else if(registeredTextures.TryGetValue(index, out var t))
611+
{
612+
singleTexture[0] = t;
613+
}
608614

609615
textures = singleTexture;
610616
}
@@ -657,21 +663,35 @@ private void Render(ImDrawDataPtr drawData)
657663
}
658664
}
659665

666+
public int RegisterTexture(Texture texture)
667+
{
668+
if(registeredTexturesInverse.TryGetValue(texture, out var ID))
669+
{
670+
return ID;
671+
}
672+
673+
var index = textureCounter++;
674+
675+
registeredTextures.Add(index, texture);
676+
registeredTexturesInverse.Add(texture, index);
677+
678+
return index;
679+
}
680+
660681
[MethodImpl(MethodImplOptions.AggressiveInlining)]
661682
public static ImTextureRef GetImGuiTexture(Texture texture)
662683
{
663684
unsafe
664685
{
665686
if (texture == null ||
666687
texture.Disposed ||
667-
texture.metadata.readBack)
688+
texture.metadata.readBack ||
689+
instance == null)
668690
{
669691
return new ImTextureRef(texId: ImTextureID.Null);
670692
}
671693

672-
//return new ImTextureRef(texId: new ImTextureID((ulong)texture.handle.idx));
673-
674-
return new ImTextureRef(texId: ImTextureID.Null);
694+
return new ImTextureRef(texId: new ImTextureID(instance.RegisterTexture(texture)));
675695
}
676696
}
677697

0 commit comments

Comments
 (0)