Skip to content

Commit

Permalink
zzre: Migrate to Veldrid fork of TechPizzaDev (#323)
Browse files Browse the repository at this point in the history
* Initial migration to fork

* zzre: Fix one case of unused vertex input

* Rebase fork and add portability_enumeration fix

* Ignore more .DS_Store files

* Use collection expression

* Fix validation due to VkImageView not disposed before Framebuffer

* Fix another vertex input not consumed
although I am not sure why this fixes it.
  • Loading branch information
Helco authored Feb 23, 2024
1 parent 624f4b6 commit 41ac5a5
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ nuget-feed
**/*.user
**/BuildOutput.sarif
sarif-output
**/.DS_Store
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- Common Dependency versioning -->
<PropertyGroup>
<RestoreSources>$(RestoreSources);../nuget-feed;https://api.nuget.org/v3/index.json</RestoreSources>
<VeldridHash>g603c7f2521</VeldridHash>
<VeldridHash>g9c18bddece</VeldridHash>
<DefaultEcsHash>0e92bb5</DefaultEcsHash>
<ImguiHash>9b51cce</ImguiHash>
<MlangHash>331cf83</MlangHash>
Expand Down
2 changes: 1 addition & 1 deletion extern/Veldrid
Submodule Veldrid updated 451 files
8 changes: 4 additions & 4 deletions get-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ MlangHash=`git -C extern/Mlang rev-parse --short HEAD`
NLayerHash=`git -C extern/NLayer rev-parse --short HEAD`
Configuration=Release
ConfigSuffix=
VeldridHash=notactuallyused
VeldridHash=4.9.0-717ab09d
CommonFlags="--include-symbols -p:EmbedAllSources=true -p:DebugType=portable -p:SymbolPackageFormat=snupkg -clp:ErrorsOnly -o nuget-feed"

dotnet pack extern/DefaultEcs/source/DefaultEcs/DefaultEcs.csproj -c SafeDebug $CommonFlags "-p:TEST=true" --version-suffix safe-$DefaultEcsHash
dotnet pack extern/DefaultEcs/source/DefaultEcs/DefaultEcs.csproj -c $Configuration $CommonFlags "-p:TEST=true" --version-suffix $DefaultEcsHash$ConfigSuffix
dotnet pack extern/Veldrid/src/Veldrid/Veldrid.csproj -c $Configuration $CommonFlags "-p:ExcludeOpenGL=true" "-p:ExcludeD3D11=true" --version-suffix $VeldridHash$ConfigSuffix
dotnet pack extern/Veldrid/src/Veldrid.MetalBindings/Veldrid.MetalBindings.csproj -c $Configuration $CommonFlags --version-suffix $VeldridHash$ConfigSuffix
dotnet pack extern/Veldrid/src/Veldrid.RenderDoc/Veldrid.RenderDoc.csproj -c $Configuration $CommonFlags --version-suffix $VeldridHash$ConfigSuffix
dotnet pack extern/Veldrid/src/Veldrid/Veldrid.csproj -c $Configuration $CommonFlags "-p:ExcludeOpenGL=true" "-p:ExcludeD3D11=true" "-p:ExcludeMetal=true"
dotnet pack extern/Veldrid/src/Veldrid.MetalBindings/Veldrid.MetalBindings.csproj -c $Configuration $CommonFlags
dotnet pack extern/Veldrid/src/Veldrid.RenderDoc/Veldrid.RenderDoc.csproj -c $Configuration $CommonFlags
dotnet pack extern/ImGui.NET/src/ImGui.NET/ImGui.NET.csproj -c $Configuration $CommonFlags -p:PackagePrereleaseIdentifier=-$ImGuiNETHash$ConfigSuffix
dotnet pack extern/ImGui.NET/src/ImGuizmo.NET/ImGuizmo.NET.csproj -c $Configuration $CommonFlags -p:PackagePrereleaseIdentifier=-$ImGuiNETHash$ConfigSuffix
dotnet pack extern/Mlang/Mlang/Mlang.csproj -c $Configuration $CommonFlags --version-suffix $MlangHash
Expand Down
8 changes: 4 additions & 4 deletions zzre.core/imgui/FramebufferArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public FramebufferArea(Window parent, GraphicsDevice device)
protected override void DisposeManaged()
{
base.DisposeManaged();
if (bindingHandle != IntPtr.Zero)
ImGuiRenderer.RemoveImGuiBinding(targetColor);
WindowContainer.OnceBeforeUpdate += () =>
{
// Delay disposal so we do not attempt to render the ImGui commands
// with the framebuffer texture already disposed
if (bindingHandle != IntPtr.Zero)
ImGuiRenderer.RemoveImGuiBinding(targetColor);
// Delay disposal so we do not attempt to dispose resources
// that are still in-flight
bindingHandle = IntPtr.Zero;
targetColor.Dispose();
targetDepth.Dispose();
Expand Down
28 changes: 12 additions & 16 deletions zzre.core/imgui/ImGuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDes
_gd = gd;
_colorSpaceHandling = colorSpaceHandling;
ResourceFactory factory = gd.ResourceFactory;
_vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.Dynamic));
_vertexBuffer = factory.CreateBuffer(new BufferDescription(10000, BufferUsage.VertexBuffer | BufferUsage.DynamicWrite));
_vertexBuffer.Name = "ImGui.NET Vertex Buffer";
_indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.Dynamic));
_indexBuffer = factory.CreateBuffer(new BufferDescription(2000, BufferUsage.IndexBuffer | BufferUsage.DynamicWrite));
_indexBuffer.Name = "ImGui.NET Index Buffer";

_projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
_projMatrixBuffer = factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.DynamicWrite));
_projMatrixBuffer.Name = "ImGui.NET Projection Buffer";

byte[] vertexShaderBytes = GetEmbeddedResourceBytes("imgui-vertex");
Expand Down Expand Up @@ -179,7 +179,7 @@ public void CreateDeviceResources(GraphicsDevice gd, OutputDescription outputDes
[_layout, _textureLayout],
outputDescription,
ResourceBindingModel.Default);
_pipeline = factory.CreateGraphicsPipeline(ref pd);
_pipeline = factory.CreateGraphicsPipeline(pd);
_pipeline.Name = "ImGui.NET Pipeline";

_mainResourceSet = factory.CreateResourceSet(new ResourceSetDescription(_layout,
Expand Down Expand Up @@ -258,15 +258,8 @@ public void RemoveImGuiBinding(Texture texture)
/// <summary>
/// Retrieves the shader texture binding for the given helper handle.
/// </summary>
public ResourceSet GetImageResourceSet(IntPtr imGuiBinding)
{
if (!_viewsById.TryGetValue(imGuiBinding, out ResourceSetInfo rsi))
{
throw new InvalidOperationException("No registered ImGui binding with id " + imGuiBinding.ToString());
}

return rsi.ResourceSet;
}
private ResourceSet GetImageResourceSet(IntPtr imGuiBinding) =>
_viewsById.GetValueOrDefault(imGuiBinding).ResourceSet;

public void ClearCachedImageResources()
{
Expand Down Expand Up @@ -628,15 +621,15 @@ private unsafe void RenderImDrawData(ImDrawDataPtr draw_data, GraphicsDevice gd,
if (totalVBSize > _vertexBuffer.SizeInBytes)
{
_vertexBuffer.Dispose();
_vertexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalVBSize * 1.5f), BufferUsage.VertexBuffer | BufferUsage.Dynamic));
_vertexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalVBSize * 1.5f), BufferUsage.VertexBuffer | BufferUsage.DynamicWrite));
_vertexBuffer.Name = $"ImGui.NET Vertex Buffer";
}

uint totalIBSize = (uint)(draw_data.TotalIdxCount * sizeof(ushort));
if (totalIBSize > _indexBuffer.SizeInBytes)
{
_indexBuffer.Dispose();
_indexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalIBSize * 1.5f), BufferUsage.IndexBuffer | BufferUsage.Dynamic));
_indexBuffer = gd.ResourceFactory.CreateBuffer(new BufferDescription((uint)(totalIBSize * 1.5f), BufferUsage.IndexBuffer | BufferUsage.DynamicWrite));
_indexBuffer.Name = $"ImGui.NET Index Buffer";
}

Expand Down Expand Up @@ -705,7 +698,10 @@ private unsafe void RenderImDrawData(ImDrawDataPtr draw_data, GraphicsDevice gd,
}
else
{
cl.SetGraphicsResourceSet(1, GetImageResourceSet(pcmd.TextureId));
var resourceSet = GetImageResourceSet(pcmd.TextureId);
if (resourceSet == null)
continue;
cl.SetGraphicsResourceSet(1, resourceSet);
}
}

Expand Down
4 changes: 2 additions & 2 deletions zzre.core/imgui/WindowContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public WindowContainer(SdlWindow window, GraphicsDevice device)
{
Device = device;

var fb = device.MainSwapchain.Framebuffer;
var fb = device.MainSwapchain!.Framebuffer;
ImGuiRenderer = new(device, fb.OutputDescription, (int)fb.Width, (int)fb.Height, ColorSpaceHandling.Legacy, callNewFrame: false);
ImGuizmoNET.ImGuizmo.SetImGuiContext(ImGui.GetCurrentContext());
ImGuizmoNET.ImGuizmo.AllowAxisFlip(false);
Expand Down Expand Up @@ -186,7 +186,7 @@ public void Render()
Device.WaitForFence(fence);
fence.Reset();
commandList.Begin();
commandList.SetFramebuffer(Device.MainSwapchain.Framebuffer);
commandList.SetFramebuffer(Device.MainSwapchain!.Framebuffer);
commandList.ClearColorTarget(0, RgbaFloat.Cyan);
ImGuiRenderer.Render(Device, commandList);
commandList.End();
Expand Down
2 changes: 1 addition & 1 deletion zzre.core/rendering/BaseMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void Apply(CommandList cl)
resourceSet = factory.CreateResourceSet(new ResourceSetDescription()
{
Layout = layout,
BoundResources = Bindings.Select(b => b.Resource).ToArray()
BoundResources = Bindings.Select(b => b.Resource).ToArray()!
});
resourceSet.Name = $"{parentName} Set {index}";
}
Expand Down
2 changes: 1 addition & 1 deletion zzre.core/rendering/DynamicGraphicsBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void Update(CommandList cl)
foreach (var range in dirtyBytes)
{
var offset = range.GetOffset(capacityInBytes);
cl.UpdateBuffer(OptionalBuffer, (uint)offset, bytes.AsSpan(range));
cl.UpdateBuffer(OptionalBuffer!, (uint)offset, bytes.AsSpan(range));
}
dirtyBytes.Clear();
}
Expand Down
4 changes: 2 additions & 2 deletions zzre.core/rendering/DynamicMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public unsafe Attribute(GraphicsDevice device, bool dynamic, string meshName, st
{
Name = name;
buffer = new(device,
BufferUsage.VertexBuffer | (dynamic ? BufferUsage.Dynamic : default),
BufferUsage.VertexBuffer | (dynamic ? BufferUsage.DynamicWrite : default),
$"{meshName} {name}",
minGrowFactor)
{
Expand Down Expand Up @@ -82,7 +82,7 @@ public DynamicMesh(ITagContainer diContainer,
this.minGrowFactor = minGrowFactor;

indexBuffer = new(graphicsDevice,
BufferUsage.IndexBuffer | (dynamic ? BufferUsage.Dynamic : default),
BufferUsage.IndexBuffer | (dynamic ? BufferUsage.DynamicWrite : default),
name + " Indices",
minGrowFactor)
{
Expand Down
2 changes: 1 addition & 1 deletion zzre.core/rendering/MlangMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void ApplyBindings(CommandList cl)
throw new InvalidOperationException($"Binding {bindingInfo.Name} is not set");
setDescriptions[bindingInfo.SetIndex].BoundResources[bindingInfo.BindingIndex] = binding.Resource;
}
resourceSets = setDescriptions.Select(Device.ResourceFactory.CreateResourceSet).ToArray();
resourceSets = setDescriptions.Select(d => Device.ResourceFactory.CreateResourceSet(d)).ToArray();
}
for (int i = 0; i < resourceSets.Length; i++)
cl.SetGraphicsResourceSet((uint)i, resourceSets[i]);
Expand Down
2 changes: 1 addition & 1 deletion zzre.core/rendering/ShaderVariantCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private ResourceLayout[] CreateResourceLayouts(ShaderVariant variant)
layouts[binding.SetIndex].Elements[binding.BindingIndex] = new(
binding.Name, kind, ShaderStages.Fragment | ShaderStages.Vertex);
}
return layouts.Select(Factory.CreateResourceLayout).ToArray();
return layouts.Select(l => Factory.CreateResourceLayout(l)).ToArray();
}

private BlendStateDescription CreateBlendState(PipelineState state) => new()
Expand Down
2 changes: 1 addition & 1 deletion zzre.core/rendering/UniformBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public UniformBuffer(ResourceFactory factory, bool dynamic = false)
uint alignedSize = (uint)Marshal.SizeOf<T>();
alignedSize = (alignedSize + 15) / 16 * 16;
Buffer = factory.CreateBuffer(new BufferDescription(alignedSize, BufferUsage.UniformBuffer |
(dynamic ? BufferUsage.Dynamic : default)));
(dynamic ? BufferUsage.DynamicWrite : default)));
Buffer.Name = $"{GetType().Name} {GetHashCode()}";
}

Expand Down
6 changes: 5 additions & 1 deletion zzre/game/systems/model/ModelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ private void Update(
else
clumpCounts[^1] = clumpCounts[^1].Increment();

var m = entity.TryGet<components.TexShift>().GetValueOrDefault(components.TexShift.Default).Matrix;
instanceArena!.Add(new()
{
tint = materialInfo.Color,
world = location.LocalToWorld,
texShift = entity.TryGet<components.TexShift>().GetValueOrDefault(components.TexShift.Default).Matrix
texShift = new(
m.M11, m.M12, 0f,
m.M21, m.M22, 0f,
m.M31, m.M32, 1f)
});
}

Expand Down
2 changes: 1 addition & 1 deletion zzre/game/systems/sound/SoundEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void Dispose()

// just to be safe: also delete all sources
using (context.EnsureIsCurrent())
device.AL.DeleteSources(sourcePool.ToArray());
device.AL.DeleteSources([.. sourcePool]);
sourcePool.Clear();

spawnEmitterSubscription?.Dispose();
Expand Down
7 changes: 4 additions & 3 deletions zzre/materials/ModelMaterial.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using Silk.NET.Maths;
using zzio;
using zzre.rendering;

Expand Down Expand Up @@ -28,7 +29,7 @@ public struct ModelFactors
public struct ModelInstance
{
public Matrix4x4 world;
public Matrix3x2 texShift;
public Matrix3X3<float> texShift;
public IColor tint;
}

Expand Down Expand Up @@ -111,7 +112,7 @@ public ModelMaterial(ITagContainer diContainer) : base(diContainer, "model")
public sealed class ModelInstanceBuffer : DynamicMesh
{
private readonly Attribute<Matrix4x4> attrWorld;
private readonly Attribute<Matrix3x2> attrTexShift;
private readonly Attribute<Matrix3X3<float>> attrTexShift;
private readonly Attribute<IColor> attrTint;

public ModelInstanceBuffer(ITagContainer diContainer,
Expand All @@ -121,7 +122,7 @@ public ModelInstanceBuffer(ITagContainer diContainer,
: base(diContainer, dynamic, name)
{
attrWorld = AddAttribute<Matrix4x4>("world");
attrTexShift = AddAttribute<Matrix3x2>("inTexShift");
attrTexShift = AddAttribute<Matrix3X3<float>>("inTexShift");
attrTint = AddAttribute<IColor>("inTint");
Preallocate(preallocateInstances, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion zzre/rendering/SkeletonPoseBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Skeleton? Skeleton
poseBuffer?.Dispose();
poseBuffer = Parent.Device.ResourceFactory.CreateBuffer(new BufferDescription(
MaxBoneCount * 4 * 4 * sizeof(float),
BufferUsage.StructuredBufferReadOnly | BufferUsage.Dynamic,
BufferUsage.StructuredBufferReadOnly | BufferUsage.DynamicWrite,
4 * 4 * sizeof(float)));
poseBuffer.Name = $"{skeleton.Name} Pose {GetHashCode()}";
poseBufferRange = new DeviceBufferRange(PoseBuffer, 0, poseBuffer.SizeInBytes);
Expand Down
11 changes: 4 additions & 7 deletions zzre/shaders/model.mlang
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ option HasFog;
variants exclude if (IsSkinned && (IsInstanced || HasEnvMap || HasTexShift));
variants exclude if (!IsInstanced && (HasTexShift || HasEnvMap || !DepthWrite || !DepthTest || Blend != IsOpaque)); // we only use opaque materials for non-instanced currently

attributes
{
float3 inPos;
byte4_norm inColor;
}
attributes float3 inPos;
attributes if (!IsSkinned) byte4_norm inColor;
attributes if (HasEnvMap) float3 inNormal;
attributes if (!HasEnvMap) float2 inUV;
attributes if (IsSkinned)
Expand All @@ -24,7 +21,7 @@ attributes if (IsSkinned)
}

instances mat4 world;
instances if (HasTexShift) mat3x2 inTexShift;
instances if (HasTexShift) mat3 inTexShift;
instances byte4_norm inTint;

varying
Expand Down Expand Up @@ -130,7 +127,7 @@ vertex
else
uv = inUV;
if (HasTexShift)
uv = inTexShift * vec3(uv, 1);
uv = vec3(inTexShift * vec3(uv, 1)).xy;
varUV = uv;

if (IsSkinned)
Expand Down
3 changes: 1 addition & 2 deletions zzre/tools/TestRaycaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Veldrid;
using zzre.imgui;
using zzre.rendering;
using System.Linq;
using Quaternion = System.Numerics.Quaternion;
using zzio.vfs;
using zzio.rwbs;
Expand Down Expand Up @@ -154,7 +153,7 @@ private void OnRender()
: IColor.Black;
});

device.UpdateTexture(fbArea.Framebuffer.ColorTargets.First().Target, pixels, 0, 0, 0,
device.UpdateTexture(fbArea.Framebuffer.ColorTargets[0].Target, pixels!, 0, 0, 0,
fbArea.Framebuffer.Width, fbArea.Framebuffer.Height, 1,
0, 0);
}
Expand Down

0 comments on commit 41ac5a5

Please sign in to comment.