Skip to content

Commit ea1d56a

Browse files
committed
pix
1 parent 8ba4c5f commit ea1d56a

File tree

26 files changed

+3613
-28
lines changed

26 files changed

+3613
-28
lines changed

Coplt.Graphics.Core/Core/CommandList.cs

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public void ReqState(
3535
FResAccess Access,
3636
FShaderStageFlags Stages,
3737
FLegacyState Legacy,
38-
bool? CrossQueue = null
38+
bool? CrossQueue = null,
39+
FImageBarrierFlags ImageFlags = FImageBarrierFlags.None
3940
) => ReqState(
4041
self, new FResState
4142
{
@@ -44,9 +45,12 @@ public void ReqState(
4445
Stages = Stages,
4546
Legacy = Legacy,
4647
CrossQueue = CrossQueue ?? Resource.NativeState.CrossQueue,
47-
}
48+
}, ImageFlags
4849
);
49-
private void ReqState(CommandList self, FResState NewState)
50+
private void ReqState(
51+
CommandList self, FResState NewState,
52+
FImageBarrierFlags ImageFlags = FImageBarrierFlags.None
53+
)
5054
{
5155
ref var OldState = ref Resource.NativeState;
5256
if (First)
@@ -99,7 +103,10 @@ private void ReqState(CommandList self, FResState NewState)
99103
return;
100104
}
101105

102-
private FBarrier BuildBarrier(in FResState OldState, in FResState NewState)
106+
private FBarrier BuildBarrier(
107+
in FResState OldState, in FResState NewState,
108+
FImageBarrierFlags ImageFlags = FImageBarrierFlags.None
109+
)
103110
{
104111
var barrier = new FBarrier();
105112
switch (Resource.Type)
@@ -151,7 +158,7 @@ private FBarrier BuildBarrier(in FResState OldState, in FResState NewState)
151158
FirstPlane = 0,
152159
NumPlanes = Resource.Planes,
153160
},
154-
Flags = FImageBarrierFlags.None,
161+
Flags = ImageFlags,
155162
};
156163
if (OldState.CrossQueue || NewState.CrossQueue)
157164
{
@@ -377,18 +384,20 @@ internal FResourceRef AddResource(IGpuResource resource)
377384
internal uint AddString(string str)
378385
{
379386
var index = m_string16.Count;
380-
CollectionsMarshal.SetCount(m_string16, index + str.Length);
381-
var dst = CollectionsMarshal.AsSpan(m_string16).Slice(index, str.Length);
387+
CollectionsMarshal.SetCount(m_string16, index + str.Length + 1);
388+
var dst = CollectionsMarshal.AsSpan(m_string16).Slice(index, str.Length + 1);
382389
str.CopyTo(dst);
390+
dst[str.Length] = '\0';
383391
return (uint)index;
384392
}
385393

386394
internal uint AddString(ReadOnlySpan<byte> str)
387395
{
388396
var index = m_string8.Count;
389-
CollectionsMarshal.SetCount(m_string8, index + str.Length);
390-
var dst = CollectionsMarshal.AsSpan(m_string8).Slice(index, str.Length);
397+
CollectionsMarshal.SetCount(m_string8, index + str.Length + 1);
398+
var dst = CollectionsMarshal.AsSpan(m_string8).Slice(index, str.Length + 1);
391399
str.CopyTo(dst);
400+
dst[str.Length] = 0;
392401
return (uint)index;
393402
}
394403

@@ -1048,15 +1057,9 @@ public RenderScope Render(
10481057

10491058
#endregion
10501059

1051-
#region Add
1052-
1053-
m_commands.Add(new() { Render = cmd });
1054-
1055-
#endregion
1056-
10571060
#region Scope
10581061

1059-
RenderScope scope = new(this, info_index, m_render_commands.Count, m_render_commands, debug_scope);
1062+
RenderScope scope = new(this, info_index, cmd, m_render_commands, debug_scope);
10601063

10611064
#endregion
10621065

@@ -1290,7 +1293,7 @@ public RtvInfo(IRtv View, LoadOp<float4> Load) : this(View, Load, StoreOp.Store)
12901293
public unsafe struct RenderScope(
12911294
CommandList self,
12921295
int info_index,
1293-
int cmd_index,
1296+
FCommandRender render_cmd,
12941297
List<FRenderCommandItem> m_commands,
12951298
bool debug_scope
12961299
) : IDisposable
@@ -1300,6 +1303,10 @@ bool debug_scope
13001303
private ShaderPipeline? m_current_pipeline;
13011304
private ShaderBinding? m_current_binding;
13021305
private bool m_disposed;
1306+
private bool m_has_pixel_shader;
1307+
private bool m_has_vertex_shader;
1308+
private bool m_has_mesh_shader;
1309+
private bool m_has_task_shader;
13031310

13041311
#endregion
13051312

@@ -1319,7 +1326,35 @@ public void Dispose()
13191326
throw new InvalidOperationException(
13201327
"There is still Debug scope not ended, please check whether Dispose is missed."
13211328
);
1322-
Info.CommandCount = (uint)m_commands.Count - (uint)cmd_index;
1329+
ref var info = ref Info;
1330+
if (self.AutoBarrierEnabled)
1331+
{
1332+
var stages = m_has_pixel_shader ? FShaderStageFlags.Pixel : FShaderStageFlags.None;
1333+
if (info.Dsv.ResourceIndex != 0)
1334+
{
1335+
ref var state = ref self.StateAt(info.Dsv);
1336+
state.ReqState(
1337+
self,
1338+
FResLayout.DepthStencilWrite,
1339+
FResAccess.DepthStencilWrite,
1340+
stages,
1341+
FLegacyState.DepthWrite
1342+
);
1343+
}
1344+
for (var i = 0; i < info.NumRtv; i++)
1345+
{
1346+
ref var state = ref self.StateAt(info.Rtv[i]);
1347+
state.ReqState(
1348+
self,
1349+
FResLayout.RenderTarget,
1350+
FResAccess.RenderTarget,
1351+
stages,
1352+
FLegacyState.RenderTarget
1353+
);
1354+
}
1355+
}
1356+
self.m_commands.Add(new() { Render = render_cmd });
1357+
Info.CommandCount = (uint)m_commands.Count - render_cmd.CommandStartIndex;
13231358
self.m_render_commands.Add(new() { Type = FCommandType.End });
13241359
self.m_in_render_or_compute_scope = false;
13251360
if (debug_scope) new DebugScope(self).Dispose();
@@ -1598,6 +1633,8 @@ public void Draw(
15981633
Indexed = Indexed,
15991634
};
16001635
m_commands.Add(new() { Draw = cmd });
1636+
m_has_pixel_shader = true;
1637+
m_has_vertex_shader = true;
16011638
}
16021639

16031640
#endregion
@@ -1634,6 +1671,9 @@ public void DispatchMesh(
16341671
Type = FDispatchType.Mesh,
16351672
};
16361673
m_commands.Add(new() { Dispatch = cmd });
1674+
m_has_pixel_shader = true;
1675+
m_has_mesh_shader = true;
1676+
m_has_task_shader = m_current_pipeline!.Shader.Stages.HasFlags(ShaderStageFlags.Task);
16371677
}
16381678

16391679
#endregion

Coplt.Graphics.Core/Native/Native.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,6 +3194,18 @@ public partial struct FCommandLabel
31943194

31953195
[NativeTypeName("Coplt::FStrType")]
31963196
public FStrType StrType;
3197+
3198+
[NativeTypeName("u8[3]")]
3199+
public _Color_e__FixedBuffer Color;
3200+
3201+
[NativeTypeName("Coplt::b8")]
3202+
public B8 HasColor;
3203+
3204+
[InlineArray(3)]
3205+
public partial struct _Color_e__FixedBuffer
3206+
{
3207+
public byte e0;
3208+
}
31973209
}
31983210

31993211
[NativeTypeName("struct FCommandBeginScope : Coplt::FCommandBase")]
@@ -3209,6 +3221,18 @@ public partial struct FCommandBeginScope
32093221

32103222
[NativeTypeName("Coplt::FStrType")]
32113223
public FStrType StrType;
3224+
3225+
[NativeTypeName("u8[3]")]
3226+
public _Color_e__FixedBuffer Color;
3227+
3228+
[NativeTypeName("Coplt::b8")]
3229+
public B8 HasColor;
3230+
3231+
[InlineArray(3)]
3232+
public partial struct _Color_e__FixedBuffer
3233+
{
3234+
public byte e0;
3235+
}
32123236
}
32133237

32143238
[NativeTypeName("struct FCommandEndScope : Coplt::FCommandBase")]
@@ -3441,7 +3465,7 @@ public partial struct FCommandDispatch
34413465

34423466
public partial struct FCommandItem
34433467
{
3444-
[NativeTypeName("__AnonymousRecord_Command_L486_C9")]
3468+
[NativeTypeName("__AnonymousRecord_Command_L490_C9")]
34453469
public _Anonymous_e__Union Anonymous;
34463470

34473471
[UnscopedRef]
@@ -3639,7 +3663,7 @@ public partial struct __pad_e__FixedBuffer
36393663

36403664
public partial struct FRenderCommandItem
36413665
{
3642-
[NativeTypeName("__AnonymousRecord_Command_L514_C9")]
3666+
[NativeTypeName("__AnonymousRecord_Command_L518_C9")]
36433667
public _Anonymous_e__Union Anonymous;
36443668

36453669
[UnscopedRef]
@@ -3809,7 +3833,7 @@ public partial struct __pad_e__FixedBuffer
38093833

38103834
public partial struct FComputeCommandItem
38113835
{
3812-
[NativeTypeName("__AnonymousRecord_Command_L537_C9")]
3836+
[NativeTypeName("__AnonymousRecord_Command_L541_C9")]
38133837
public _Anonymous_e__Union Anonymous;
38143838

38153839
[UnscopedRef]

Coplt.Graphics.Native.D3d12/Coplt.Graphics.Native.D3d12.csproj renamed to Coplt.Graphics.Native.D3d12.Win64/Coplt.Graphics.Native.D3d12.Win64.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<IncludeBuildOutput>false</IncludeBuildOutput>
1010
<NoWarn>NU5128</NoWarn>
11-
<AssemblyName>_d3d12_native_stub_</AssemblyName>
1211
</PropertyGroup>
1312

1413
<ItemGroup>
15-
<ProjectReference Include="..\Coplt.Graphics.Core\Coplt.Graphics.Core.csproj" />
14+
<ProjectReference Include="..\Coplt.Graphics.Core\Coplt.Graphics.Core.csproj"/>
1615
</ItemGroup>
1716

1817
<Target Name="NativeBuildDebug" BeforeTargets="Build" Condition="'$(Configuration)' == 'Debug'" Outputs="../build/debug/bin/**/*">
@@ -32,5 +31,12 @@
3231
</ItemGroup>
3332
<Copy SourceFiles="@(NativeOutputs)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" UseHardlinksIfPossible="true"/>
3433
</Target>
34+
35+
<ItemGroup>
36+
<None Include="../Coplt.Graphics.Native/D3d12/ThirdParty/WinPixEventRuntime/bin/x64/WinPixEventRuntime.dll" Visible="false">
37+
<Pack>true</Pack>
38+
<PackagePath>runtimes\win-x64\native\</PackagePath>
39+
</None>
40+
</ItemGroup>
3541

3642
</Project>

Coplt.Graphics.Native/Api/FFI/Command.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,17 @@ namespace Coplt
330330
u32 StringIndex{};
331331
u32 StringLength{};
332332
FStrType StrType{};
333+
u8 Color[3]{};
334+
b8 HasColor{};
333335
};
334336

335337
struct FCommandBeginScope : FCommandBase
336338
{
337339
u32 StringIndex{};
338340
u32 StringLength{};
339341
FStrType StrType{};
342+
u8 Color[3]{};
343+
b8 HasColor{};
340344
};
341345

342346
struct FCommandEndScope : FCommandBase

Coplt.Graphics.Native/D3d12/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set_target_properties(${target_name} PROPERTIES LINKER_LANGUAGE CXX)
5252
set_target_properties(${target_name} PROPERTIES OUTPUT_NAME "Coplt.Graphics.Native.D3d12")
5353
add_library(Coplt::Graphics::D3d12 ALIAS ${target_name})
5454
target_compile_definitions(${target_name} PRIVATE -DFFI_SRC -DNOMINMAX)
55+
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/WinPixEventRuntime/Include/WinPixEventRuntime)
5556

5657
set(BUILD_XAUDIO_WIN10 OFF)
5758
set(BUILD_DXIL_SHADERS OFF)
@@ -65,3 +66,8 @@ target_link_libraries(${target_name}
6566
Microsoft::DirectX-Headers
6667
D3D12MemoryAllocator
6768
)
69+
70+
target_link_libraries(${target_name}
71+
PUBLIC
72+
${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/WinPixEventRuntime/bin/x64/WinPixEventRuntime.lib
73+
)

Coplt.Graphics.Native/D3d12/Src/Command.cc

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "../Include/PipelineState.h"
1616
#include "../ThirdParty/DirectXTK12/Src/d3dx12.h"
1717

18+
#include "pix3.h"
19+
1820
using namespace Coplt;
1921

2022
void D3d12CommandInterpreter::BarrierContext::Reset()
@@ -121,11 +123,14 @@ void D3d12CommandInterpreter::Translate(const FCommandSubmit& submit)
121123
case FCommandType::Present:
122124
continue;
123125
case FCommandType::Label:
124-
continue; // todo
126+
Label(submit, i, item.Label);
127+
continue;
125128
case FCommandType::BeginScope:
126-
continue; // todo
129+
BeginScope(submit, i, item.BeginScope);
130+
continue;
127131
case FCommandType::EndScope:
128-
continue; // todo
132+
EndScope(submit, i, item.EndScope);
133+
continue;
129134
case FCommandType::Barrier:
130135
Barrier(submit, i, item.Barrier);
131136
continue;
@@ -159,6 +164,42 @@ void D3d12CommandInterpreter::Translate(const FCommandSubmit& submit)
159164
}
160165
}
161166

167+
void D3d12CommandInterpreter::Label(const FCommandSubmit& submit, u32 i, const FCommandLabel& cmd) const
168+
{
169+
if (!m_queue->m_device->Debug()) return;
170+
auto color = PIX_COLOR_DEFAULT;
171+
if (cmd.HasColor) color = PIX_COLOR(cmd.Color[0], cmd.Color[1], cmd.Color[2]);
172+
if (cmd.StrType == FStrType::Str8)
173+
{
174+
PIXSetMarker(m_queue->m_cmd.m_list0.Get(), color, "%s", reinterpret_cast<const char*>(submit.Str8 + cmd.StringIndex));
175+
}
176+
else
177+
{
178+
PIXSetMarker(m_queue->m_cmd.m_list0.Get(), color, L"%s", reinterpret_cast<const wchar_t*>(submit.Str16 + cmd.StringIndex));
179+
}
180+
}
181+
182+
void D3d12CommandInterpreter::BeginScope(const FCommandSubmit& submit, u32 i, const FCommandBeginScope& cmd) const
183+
{
184+
if (!m_queue->m_device->Debug()) return;
185+
auto color = PIX_COLOR_DEFAULT;
186+
if (cmd.HasColor) color = PIX_COLOR(cmd.Color[0], cmd.Color[1], cmd.Color[2]);
187+
if (cmd.StrType == FStrType::Str8)
188+
{
189+
PIXBeginEvent(m_queue->m_cmd.m_list0.Get(), color, "%s", reinterpret_cast<const char*>(submit.Str8 + cmd.StringIndex));
190+
}
191+
else
192+
{
193+
PIXBeginEvent(m_queue->m_cmd.m_list0.Get(), color, L"%s", reinterpret_cast<const wchar_t*>(submit.Str16 + cmd.StringIndex));
194+
}
195+
}
196+
197+
void D3d12CommandInterpreter::EndScope(const FCommandSubmit& submit, u32 i, const FCommandEndScope& cmd) const
198+
{
199+
if (!m_queue->m_device->Debug()) return;
200+
PIXEndEvent(m_queue->m_cmd.m_list0.Get());
201+
}
202+
162203
void D3d12CommandInterpreter::Barrier(const FCommandSubmit& submit, u32 i, const FCommandBarrier& cmd)
163204
{
164205
m_barrier_context.Reset();

Coplt.Graphics.Native/D3d12/Src/Command.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ namespace Coplt
6969

7070
void Translate(const FCommandSubmit& submit);
7171

72+
void Label(const FCommandSubmit& submit, u32 i, const FCommandLabel& cmd) const;
73+
void BeginScope(const FCommandSubmit& submit, u32 i, const FCommandBeginScope& cmd) const;
74+
void EndScope(const FCommandSubmit& submit, u32 i, const FCommandEndScope& cmd) const;
7275
void Barrier(const FCommandSubmit& submit, u32 i, const FCommandBarrier& cmd);
7376
void Barrier(const FCommandSubmit& submit, const FGlobalBarrier& item);
7477
void Barrier(const FCommandSubmit& submit, const FBufferBarrier& item);

0 commit comments

Comments
 (0)