Skip to content

Commit 808c05f

Browse files
committed
Graphics: Improvements in BindGroupLayout and BindGroup, binding using IGraphicsBindableResource interface, handle UAV under D3D12.
1 parent 374310d commit 808c05f

21 files changed

+479
-241
lines changed

src/Alimer/DisposableObject.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
namespace Alimer;
88

9+
/// <summary>
10+
/// Base interface for a <see cref="IDisposable"/> interface.
11+
/// </summary>
12+
public interface IDisposableObject : IDisposable
13+
{
14+
/// <summary>
15+
/// Gets <c>true</c> if the object has been disposed; otherwise, <c>false</c>.
16+
/// </summary>
17+
bool IsDisposed { get; }
18+
}
19+
920
/// <summary>
1021
/// Base class for a <see cref="IDisposable"/> interface.
1122
/// </summary>
12-
public abstract class DisposableObject : IDisposable
23+
public abstract class DisposableObject : IDisposableObject
1324
{
1425
private volatile uint _isDisposed = 0;
1526

@@ -21,9 +32,7 @@ protected DisposableObject()
2132
_isDisposed = 0;
2233
}
2334

24-
/// <summary>
25-
/// Gets <c>true</c> if the object has been disposed; otherwise, <c>false</c>.
26-
/// </summary>
35+
/// <inheritdoc />
2736
public bool IsDisposed => _isDisposed != 0;
2837

2938
/// <summary>

src/Alimer/Graphics/AccelerationStructure.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Alimer.Graphics;
55

6-
public abstract class AccelerationStructure : GraphicsObject
6+
public abstract class AccelerationStructure : GraphicsObject, IGraphicsBindableResource
77
{
88
protected AccelerationStructure(in AccelerationStructureDescriptor descriptor)
99
: base(descriptor.Label)

src/Alimer/Graphics/BindGroup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ protected BindGroup(in BindGroupDescriptor descriptor)
2222
/// A read-only span containing the bind group entries to apply. Each entry specifies a resource to bind; the span
2323
/// must not be empty.
2424
/// </param>
25-
public abstract void Update(ReadOnlySpan<BindGroupEntry> entries);
25+
public abstract void Update(Span<BindGroupEntry> entries);
2626
}

src/Alimer/Graphics/BindGroupDescriptor.cs

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Amer Koleci and Contributors.
22
// Licensed under the MIT License (MIT). See LICENSE in the repository root for more information.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using CommunityToolkit.Diagnostics;
56
using static Alimer.Graphics.Constants;
67

@@ -17,70 +18,35 @@ public struct BindGroupEntry
1718
public uint Binding;
1819

1920
/// <summary>
20-
/// The <see cref="GraphicsBuffer"/> bound.
21+
/// Specifies the index in a binding array.
2122
/// </summary>
22-
public GraphicsBuffer? Buffer;
23-
24-
public ulong Offset = 0;
25-
public ulong Size = WholeSize;
23+
public uint ArrayElement = 0;
2624

2725
/// <summary>
28-
/// The <see cref="Graphics.TextureView"/> bound.
26+
/// The <see cref="IGraphicsBindableResource"/> bound.
2927
/// </summary>
30-
public TextureView? TextureView;
28+
public IGraphicsBindableResource Resource;
3129

3230
/// <summary>
33-
/// The <see cref="Graphics.Sampler"/> bound.
31+
/// If the binding is a buffer, this is the byte offset of the binding range, ignored otherwise.
3432
/// </summary>
35-
public Sampler? Sampler;
33+
public ulong Offset = 0;
3634

3735
/// <summary>
38-
/// The <see cref="Graphics.AccelerationStructure"/> bound.
36+
/// If the binding is a buffer, this is the byte size of the binding range
37+
/// (<see cref="WholeSize"/> means the binding ends at the end of the buffer), ignored otherwise.
3938
/// </summary>
40-
public AccelerationStructure? AccelerationStructure;
39+
public ulong Size = WholeSize;
4140

42-
public BindGroupEntry(uint binding, GraphicsBuffer buffer, ulong offset = 0, ulong size = WholeSize)
41+
public BindGroupEntry(uint binding, IGraphicsBindableResource resource, ulong offset = 0, ulong size = WholeSize)
4342
{
44-
Guard.IsNotNull(buffer, nameof(buffer));
43+
Guard.IsNotNull(resource, nameof(resource));
4544

4645
Binding = binding;
47-
Buffer = buffer;
46+
Resource = resource;
4847
Offset = offset;
4948
Size = size;
5049
}
51-
52-
public BindGroupEntry(uint binding, Texture texture)
53-
{
54-
Guard.IsNotNull(texture, nameof(texture));
55-
Guard.IsNotNull(texture.DefaultView, nameof(texture.DefaultView));
56-
57-
Binding = binding;
58-
TextureView = texture.DefaultView;
59-
}
60-
61-
public BindGroupEntry(uint binding, TextureView textureView)
62-
{
63-
Guard.IsNotNull(textureView, nameof(textureView));
64-
65-
Binding = binding;
66-
TextureView = textureView;
67-
}
68-
69-
public BindGroupEntry(uint binding, Sampler sampler)
70-
{
71-
Guard.IsNotNull(sampler, nameof(sampler));
72-
73-
Binding = binding;
74-
Sampler = sampler;
75-
}
76-
77-
public BindGroupEntry(uint binding, AccelerationStructure accelerationStructure)
78-
{
79-
Guard.IsNotNull(accelerationStructure, nameof(accelerationStructure));
80-
81-
Binding = binding;
82-
AccelerationStructure = accelerationStructure;
83-
}
8450
}
8551

8652
/// <summary>
@@ -89,17 +55,18 @@ public BindGroupEntry(uint binding, AccelerationStructure accelerationStructure)
8955
public ref struct BindGroupDescriptor
9056
{
9157
// TODO: Separate per type (buffers/textures/samplers etc)
92-
public ReadOnlySpan<BindGroupEntry> Entries;
58+
public Span<BindGroupEntry> Entries;
9359

9460
/// <summary>
9561
/// The label of <see cref="BindGroup"/>.
9662
/// </summary>
9763
public string? Label;
9864

99-
public BindGroupDescriptor(ReadOnlySpan<BindGroupEntry> entries, string? label = default)
65+
public BindGroupDescriptor(Span<BindGroupEntry> entries,string? label = default)
10066
{
10167
Guard.IsGreaterThan(entries.Length, 0, nameof(entries));
10268

69+
//ConstantBufferEntries = constantBufferEntries;
10370
Entries = entries;
10471
Label = label;
10572
}

src/Alimer/Graphics/BindGroupLayout.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ public abstract class BindGroupLayout : GraphicsObject
1010
protected BindGroupLayout(in BindGroupLayoutDescriptor descriptor)
1111
: base(descriptor.Label)
1212
{
13+
Entries = descriptor.Entries.ToArray();
1314
}
1415

15-
public BindGroup CreateBindGroup(params ReadOnlySpan<BindGroupEntry> entries)
16+
public BindGroupLayoutEntry[] Entries { get; }
17+
18+
public BindGroup CreateBindGroup(params Span<BindGroupEntry> entries)
1619
{
1720
return CreateBindGroup(new BindGroupDescriptor(entries));
1821
}

src/Alimer/Graphics/BindGroupLayoutDescriptor.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ public BindGroupLayoutDescriptor()
2121
{
2222
}
2323

24-
public BindGroupLayoutDescriptor(Span<BindGroupLayoutEntry> entries)
24+
public BindGroupLayoutDescriptor(Span<BindGroupLayoutEntry> entries, string? label = default)
2525
{
2626
Guard.IsGreaterThan(entries.Length, 0, nameof(entries));
2727

2828
Entries = entries;
29-
}
30-
31-
public BindGroupLayoutDescriptor(Span<BindGroupLayoutEntry> entries, string label)
32-
: this(entries)
33-
{
3429
Label = label;
3530
}
3631
}

src/Alimer/Graphics/BindGroupLayoutEntry.cs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,56 @@ namespace Alimer.Graphics;
55

66
public readonly struct BufferBindingLayout
77
{
8-
public readonly BufferBindingType Type;
9-
public readonly bool HasDynamicOffset;
10-
public readonly ulong MinBindingSize;
8+
public readonly BufferBindingType Type = BufferBindingType.Constant;
9+
public readonly bool HasDynamicOffset = false;
1110

1211
public BufferBindingLayout()
1312
{
14-
Type = BufferBindingType.Constant;
15-
HasDynamicOffset = false;
16-
MinBindingSize = 0;
1713
}
1814

19-
public BufferBindingLayout(BufferBindingType type, bool hasDynamicOffset = false, ulong minBindingSize = 0)
15+
public BufferBindingLayout(BufferBindingType type, bool hasDynamicOffset = false)
2016
{
2117
Type = type;
2218
HasDynamicOffset = hasDynamicOffset;
23-
MinBindingSize = minBindingSize;
2419
}
2520
}
2621

2722
public readonly struct SamplerBindingLayout
2823
{
29-
public readonly SamplerBindingType Type;
24+
public readonly SamplerBindingType Type = SamplerBindingType.Filtering;
3025

3126
public SamplerBindingLayout()
3227
{
33-
Type = SamplerBindingType.Filtering;
3428
}
3529

36-
public SamplerBindingLayout(SamplerBindingType type)
30+
public SamplerBindingLayout(SamplerBindingType type = SamplerBindingType.Filtering)
3731
{
3832
Type = type;
3933
}
4034
}
4135

4236
public readonly struct TextureBindingLayout
4337
{
44-
public readonly TextureSampleType SampleType;
45-
//public TextureViewDimension ViewDimension;
46-
public readonly bool Multisampled;
38+
public readonly TextureSampleType SampleType = TextureSampleType.Float;
39+
public readonly TextureViewDimension ViewDimension = TextureViewDimension.View2D;
40+
public readonly bool Multisampled = false;
4741

4842
public TextureBindingLayout()
4943
{
50-
SampleType = TextureSampleType.Float;
51-
Multisampled = false;
5244
}
5345

54-
public TextureBindingLayout(TextureSampleType sampleType, bool multisampled = false)
46+
public TextureBindingLayout(TextureSampleType sampleType = TextureSampleType.Float, bool multisampled = false)
5547
{
5648
SampleType = sampleType;
5749
Multisampled = multisampled;
5850
}
5951
}
6052

61-
public struct StorageTextureBindingLayout
53+
public readonly struct StorageTextureBindingLayout
6254
{
63-
public StorageTextureAccess Access = StorageTextureAccess.WriteOnly;
64-
public PixelFormat Format = PixelFormat.Undefined;
65-
//public TextureViewDimension viewDimension;
55+
public readonly StorageTextureAccess Access = StorageTextureAccess.WriteOnly;
56+
public readonly PixelFormat Format = PixelFormat.Undefined;
57+
public readonly TextureViewDimension ViewDimension = TextureViewDimension.View2D;
6658

6759
public StorageTextureBindingLayout()
6860
{
@@ -75,9 +67,9 @@ public StorageTextureBindingLayout(StorageTextureAccess access, PixelFormat form
7567
}
7668
}
7769

78-
public struct AccelerationStructureBindingLayout
70+
public readonly struct AccelerationStructureBindingLayout
7971
{
80-
public PixelFormat Format = PixelFormat.Undefined;
72+
public readonly PixelFormat Format = PixelFormat.Undefined;
8173

8274
public AccelerationStructureBindingLayout()
8375
{
@@ -89,34 +81,38 @@ public AccelerationStructureBindingLayout(PixelFormat format)
8981
}
9082
}
9183

92-
9384
/// <summary>
9485
/// Single entry for <see cref="BindGroupLayout"/>.
9586
/// </summary>
96-
public record struct BindGroupLayoutEntry
87+
public readonly struct BindGroupLayoutEntry
9788
{
9889
/// <summary>
9990
/// Register index to bind to (supplied in shader).
10091
/// </summary>
101-
public uint Binding;
92+
public readonly uint Binding;
10293

10394
/// <summary>
10495
/// The shader stage the resources will be accessible to.
10596
/// </summary>
106-
public ShaderStages Visibility;
97+
public readonly ShaderStages Visibility;
98+
99+
/// <summary>
100+
/// The number of descriptors contained in the binding, accessed in a shader as an array.
101+
/// </summary>
102+
public readonly uint Count = 1u;
107103

108104
/// <summary>
109105
/// Gets the buffer binding.
110106
/// </summary>
111-
public BufferBindingLayout Buffer;
107+
public readonly BufferBindingLayout Buffer;
112108

113-
public SamplerBindingLayout Sampler;
109+
public readonly SamplerBindingLayout Sampler;
114110

115-
public SamplerDescriptor? StaticSampler;
111+
public readonly SamplerDescriptor? StaticSampler;
116112

117-
public TextureBindingLayout Texture;
118-
public StorageTextureBindingLayout StorageTexture;
119-
public AccelerationStructureBindingLayout AccelerationStructure;
113+
public readonly TextureBindingLayout Texture;
114+
public readonly StorageTextureBindingLayout StorageTexture;
115+
public readonly AccelerationStructureBindingLayout AccelerationStructure;
120116

121117
public BindGroupLayoutEntry(BufferBindingLayout buffer, uint binding, ShaderStages visibility = ShaderStages.All)
122118
{

0 commit comments

Comments
 (0)