Skip to content

Commit 0b04026

Browse files
committed
save
1 parent 6a61a99 commit 0b04026

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+893
-180
lines changed

Coplt.Graphics/Coplt.Graphics.csproj renamed to Coplt.Graphics.Core/Coplt.Graphics.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<NoWarn>CS8500</NoWarn>
99
<IsAotCompatible>true</IsAotCompatible>
10+
<RootNamespace>Coplt.Graphics</RootNamespace>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Coplt.Dropping;
2+
using Coplt.Graphics.Native;
3+
4+
namespace Coplt.Graphics.Core;
5+
6+
public record struct GpuBufferCreateOptions()
7+
{
8+
public Str8or16 Name;
9+
public required ResourcePurpose Purpose;
10+
public CpuAccess CpuAccess = CpuAccess.None;
11+
public LifeTime LifeTime = LifeTime.Common;
12+
public required ulong Size;
13+
public uint Count;
14+
public uint Stride;
15+
public BufferUsage Usage = BufferUsage.Raw;
16+
}
17+
18+
[Dropping(Unmanaged = true)]
19+
public sealed unsafe partial class GpuBuffer : GpuResource, ICbv, ISrv, IUav, IRtv, IIbv, IVbv
20+
{
21+
#region Fields
22+
23+
internal new FGpuBuffer* m_ptr;
24+
25+
#endregion
26+
27+
#region Props
28+
29+
public new FGpuBuffer* Ptr => m_ptr;
30+
public ulong Size => m_ptr->m_size;
31+
ulong IView.LongSize => Size;
32+
uint3 IView.Size => new((uint)Size, 1, 1);
33+
public uint Count => m_ptr->m_count;
34+
public uint Stride => m_ptr->m_stride;
35+
public BufferUsage Usage => (BufferUsage)m_ptr->m_usage;
36+
37+
#endregion
38+
39+
#region Ctor
40+
41+
internal GpuBuffer(FGpuBuffer* ptr, string? name, GpuQueue queue) : base(&ptr->Base, name, queue)
42+
{
43+
m_ptr = ptr;
44+
}
45+
46+
#endregion
47+
48+
#region Drop
49+
50+
[Drop]
51+
private void Drop()
52+
{
53+
m_ptr = null;
54+
}
55+
56+
#endregion
57+
58+
#region ToString
59+
60+
public override string ToString() =>
61+
m_name is null
62+
? $"{nameof(GpuBuffer)}(0x{(nuint)m_ptr:X})"
63+
: $"{nameof(GpuBuffer)}(0x{(nuint)m_ptr:X} \"{m_name}\")";
64+
65+
#endregion
66+
67+
#region View
68+
69+
FResourceMeta IView.GetMeta() => new()
70+
{
71+
CurrentState = m_ptr->Base.m_state,
72+
Type = FResourceRefType.Buffer,
73+
Buffer = m_ptr,
74+
};
75+
void IView.UnsafeChangeState(FResourceState state) => UnsafeChangeState(state);
76+
internal void UnsafeChangeState(FResourceState state) => m_ptr->Base.m_state.ChangeState(state);
77+
78+
public bool TryVbv() => Purpose.HasFlags(ResourcePurpose.VertexBuffer);
79+
public bool TryIbv() => Purpose.HasFlags(ResourcePurpose.IndexBuffer);
80+
public bool TryRtv() => Purpose.HasFlags(ResourcePurpose.RenderTarget);
81+
public bool TryUav() => Purpose.HasFlags(ResourcePurpose.UnorderedAccess);
82+
public bool TrySrv() => Purpose.HasFlags(ResourcePurpose.ShaderResource);
83+
public bool TryCbv() => Purpose.HasFlags(ResourcePurpose.ConstantBuffer);
84+
85+
#endregion
86+
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public GpuQueue CreateMainQueue(string? Name = null, ReadOnlySpan<byte> Name8 =
145145
};
146146
FGpuQueue* ptr;
147147
m_ptr->CreateMainQueue(&f_options, &ptr).TryThrow();
148-
return new(ptr, Name);
148+
return new(ptr, Name, this);
149149
}
150150
}
151151

@@ -357,4 +357,13 @@ public GraphicsShaderPipeline CreateGraphicsShaderPipeline(
357357
}
358358

359359
#endregion
360+
361+
#region CreateBuffer
362+
363+
public GpuBuffer CreateBuffer(
364+
in GpuBufferCreateOptions options,
365+
string? Name = null, ReadOnlySpan<byte> Name8 = default
366+
) => MainQueue.CreateBuffer(in options, Name, Name8);
367+
368+
#endregion
360369
}
Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using Coplt.Dropping;
1+
using Coplt.Dropping;
32
using Coplt.Graphics.Native;
43

54
namespace Coplt.Graphics.Core;
@@ -25,6 +24,7 @@ public sealed unsafe partial class GpuQueue
2524

2625
internal FGpuQueue* m_ptr;
2726
internal string? m_name;
27+
internal readonly GpuDevice m_device;
2828
internal readonly Lock m_lock = new();
2929
internal readonly CommandList m_cmd;
3030

@@ -33,19 +33,19 @@ public sealed unsafe partial class GpuQueue
3333
#region Props
3434

3535
public FGpuQueue* Ptr => m_ptr;
36-
36+
public GpuDevice Device => m_device;
3737
public GpuQueueType QueueType => m_ptr->m_queue_type.FromFFI();
38-
3938
public CommandList CommandList => m_cmd;
4039

4140
#endregion
4241

4342
#region Ctor
4443

45-
internal GpuQueue(FGpuQueue* ptr, string? name)
44+
internal GpuQueue(FGpuQueue* ptr, string? name, GpuDevice device)
4645
{
4746
m_name = name;
4847
m_ptr = ptr;
48+
m_device = device;
4949
m_cmd = new(this);
5050
}
5151

@@ -129,4 +129,36 @@ public GpuOutput CreateOutputForHwnd(
129129
}
130130

131131
#endregion
132+
133+
#region CreateBuffer
134+
135+
public GpuBuffer CreateBuffer(
136+
in GpuBufferCreateOptions options,
137+
string? Name = null, ReadOnlySpan<byte> Name8 = default
138+
)
139+
{
140+
fixed (char* p_name = Name)
141+
fixed (byte* p_name8 = Name8)
142+
{
143+
FGpuBufferCreateOptions f_options = new()
144+
{
145+
Base = new()
146+
{
147+
Name = new(Name, Name8, p_name, p_name8),
148+
Purpose = options.Purpose.ToFFI(),
149+
CpuAccess = options.CpuAccess.ToFFI(),
150+
LifeTime = options.LifeTime.ToFFI(),
151+
},
152+
Size = options.Size,
153+
Count = options.Count,
154+
Stride = options.Stride,
155+
Usage = options.Usage.ToFFI(),
156+
};
157+
FGpuBuffer* ptr;
158+
m_device.m_ptr->CreateBuffer(&f_options, &ptr).TryThrow();
159+
return new(ptr, Name, this);
160+
}
161+
}
162+
163+
#endregion
132164
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Runtime.CompilerServices;
2+
using Coplt.Dropping;
3+
using Coplt.Graphics.Native;
4+
5+
namespace Coplt.Graphics.Core;
6+
7+
[Dropping(Unmanaged = true)]
8+
public abstract unsafe partial class GpuResource
9+
{
10+
#region Fields
11+
12+
internal FGpuResource* m_ptr;
13+
internal string? m_name;
14+
internal GpuQueue m_queue;
15+
16+
#endregion
17+
18+
#region Props
19+
20+
public FGpuResource* Ptr => m_ptr;
21+
public GpuDevice Device => m_queue.m_device;
22+
public GpuQueue Queue => m_queue;
23+
public ResourceState State => m_ptr->m_state.FromFFI();
24+
public ResourcePurpose Purpose => (ResourcePurpose)m_ptr->m_purpose;
25+
public CpuAccess CpuAccess => (CpuAccess)m_ptr->m_cpu_access;
26+
public LifeTime LifeTime => (LifeTime)m_ptr->m_life_time;
27+
28+
#endregion
29+
30+
#region Ctor
31+
32+
internal GpuResource(FGpuResource* ptr, string? name, GpuQueue queue)
33+
{
34+
m_ptr = ptr;
35+
m_name = name;
36+
m_queue = queue;
37+
}
38+
39+
#endregion
40+
41+
#region Drop
42+
43+
[Drop]
44+
private void Drop()
45+
{
46+
if (ExchangeUtils.ExchangePtr(ref m_ptr, null, out var ptr) is null) return;
47+
ptr->Release();
48+
}
49+
50+
#endregion
51+
52+
#region SetName
53+
54+
public void SetName(string name)
55+
{
56+
m_name = name;
57+
fixed (char* ptr = name)
58+
{
59+
Str8or16 str = new() { str16 = ptr, len = name.Length };
60+
m_ptr->SetName(&str).TryThrow();
61+
}
62+
}
63+
64+
public void SetName(ReadOnlySpan<byte> name)
65+
{
66+
fixed (byte* ptr = name)
67+
{
68+
Str8or16 str = new() { str8 = ptr, len = name.Length };
69+
m_ptr->SetName(&str).TryThrow();
70+
}
71+
m_name = null;
72+
}
73+
74+
#endregion
75+
76+
#region ToString
77+
78+
public override string ToString() =>
79+
m_name is null
80+
? $"{nameof(GpuResource)}(0x{(nuint)m_ptr:X})"
81+
: $"{nameof(GpuResource)}(0x{(nuint)m_ptr:X} \"{m_name}\")";
82+
83+
#endregion
84+
}
File renamed without changes.

0 commit comments

Comments
 (0)