Skip to content

Commit 7132193

Browse files
Use enhanced barriers on Small Resources sample (#936)
* Converted Small Resources to use enhanced barriers * Update readme * Simplyfied readme * Removed aliasing barrier
1 parent f8d07c1 commit 7132193

4 files changed

Lines changed: 200 additions & 54 deletions

File tree

Samples/Desktop/D3D12Multithreading/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ This sample demonstrates the use of multiple threads with Direct3D 12. An app ca
2020
### Optional features
2121
This sample has been updated to build against the Windows 10 Anniversary Update SDK. In this SDK a new revision of Root Signatures is available for Direct3D 12 apps to use. Root Signature 1.1 allows for apps to declare when descriptors in a descriptor heap won't change or the data descriptors point to won't change. This allows the option for drivers to make optimizations that might be possible knowing that something (like a descriptor or the memory it points to) is static for some period of time.
2222

23-
This sample is built with Enhanced Barriers. The sample will use Enhanced Barriers if the hardware supports it. If not, Legacy Barriers will be used instead.
23+
This sample uses Enhanced Barriers if the hardware supports it. Otherwise, Legacy Barriers will be used instead.

Samples/Desktop/D3D12SmallResources/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ This sample demonstrates the use of small placed resources in Direct3D 12. The s
2121
SPACE bar - toggles between using placed and committed resources.
2222

2323
### Optional features
24-
This sample has been updated to build against the Windows 10 Anniversary Update SDK. In this SDK a new revision of Root Signatures is available for Direct3D 12 apps to use. Root Signature 1.1 allows for apps to declare when descriptors in a descriptor heap won't change or the data descriptors point to won't change. This allows the option for drivers to make optimizations that might be possible knowing that something (like a descriptor or the memory it points to) is static for some period of time.
24+
This sample has been updated to build against the Windows 10 Anniversary Update SDK. In this SDK a new revision of Root Signatures is available for Direct3D 12 apps to use. Root Signature 1.1 allows for apps to declare when descriptors in a descriptor heap won't change or the data descriptors point to won't change. This allows the option for drivers to make optimizations that might be possible knowing that something (like a descriptor or the memory it points to) is static for some period of time.
25+
26+
This sample uses Enhanced Barriers if the hardware supports it. Otherwise, Legacy Barriers will be used instead.

Samples/Desktop/D3D12SmallResources/src/D3D12SmallResources.cpp

Lines changed: 191 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ void D3D12SmallResources::LoadPipeline()
8080
));
8181
}
8282

83+
D3D12_FEATURE_DATA_D3D12_OPTIONS12 options12 = {};
84+
ThrowIfFailed(m_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS12, &options12, sizeof(options12)));
85+
m_bIsEnhancedBarriersEnabled = static_cast<bool>(options12.EnhancedBarriersSupported);
86+
8387
// Describe and create the command queue.
8488
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
8589
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
@@ -284,21 +288,48 @@ void D3D12SmallResources::LoadAssets()
284288
}
285289
const UINT vertexBufferSize = sizeof(quadVertices);
286290

287-
ThrowIfFailed(m_device->CreateCommittedResource(
288-
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
289-
D3D12_HEAP_FLAG_NONE,
290-
&CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
291-
D3D12_RESOURCE_STATE_COPY_DEST,
292-
nullptr,
293-
IID_PPV_ARGS(&m_vertexBuffer)));
294-
295-
ThrowIfFailed(m_device->CreateCommittedResource(
296-
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
297-
D3D12_HEAP_FLAG_NONE,
298-
&CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
299-
D3D12_RESOURCE_STATE_GENERIC_READ,
300-
nullptr,
301-
IID_PPV_ARGS(&vertexBufferUpload)));
291+
if (m_bIsEnhancedBarriersEnabled)
292+
{
293+
ThrowIfFailed(m_device->CreateCommittedResource3(
294+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
295+
D3D12_HEAP_FLAG_NONE,
296+
&CD3DX12_RESOURCE_DESC1::Buffer(vertexBufferSize),
297+
D3D12_BARRIER_LAYOUT_UNDEFINED,
298+
nullptr,
299+
nullptr,
300+
0,
301+
nullptr,
302+
IID_PPV_ARGS(&m_vertexBuffer)));
303+
304+
ThrowIfFailed(m_device->CreateCommittedResource3(
305+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
306+
D3D12_HEAP_FLAG_NONE,
307+
&CD3DX12_RESOURCE_DESC1::Buffer(vertexBufferSize),
308+
D3D12_BARRIER_LAYOUT_UNDEFINED,
309+
nullptr,
310+
nullptr,
311+
0,
312+
nullptr,
313+
IID_PPV_ARGS(&vertexBufferUpload)));
314+
}
315+
else
316+
{
317+
ThrowIfFailed(m_device->CreateCommittedResource(
318+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
319+
D3D12_HEAP_FLAG_NONE,
320+
&CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
321+
D3D12_RESOURCE_STATE_COPY_DEST,
322+
nullptr,
323+
IID_PPV_ARGS(&m_vertexBuffer)));
324+
325+
ThrowIfFailed(m_device->CreateCommittedResource(
326+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
327+
D3D12_HEAP_FLAG_NONE,
328+
&CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize),
329+
D3D12_RESOURCE_STATE_GENERIC_READ,
330+
nullptr,
331+
IID_PPV_ARGS(&vertexBufferUpload)));
332+
}
302333

303334
NAME_D3D12_OBJECT(m_vertexBuffer);
304335

@@ -310,7 +341,25 @@ void D3D12SmallResources::LoadAssets()
310341
vertexData.SlicePitch = vertexData.RowPitch;
311342

312343
UpdateSubresources<1>(m_commandList.Get(), m_vertexBuffer.Get(), vertexBufferUpload.Get(), 0, 0, 1, &vertexData);
313-
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_vertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
344+
if (m_bIsEnhancedBarriersEnabled)
345+
{
346+
D3D12_BUFFER_BARRIER VertexBufBarriers[] =
347+
{
348+
CD3DX12_BUFFER_BARRIER(
349+
D3D12_BARRIER_SYNC_COPY, // SyncBefore
350+
D3D12_BARRIER_SYNC_VERTEX_SHADING, // SyncAfter
351+
D3D12_BARRIER_ACCESS_COPY_DEST, // AccessBefore
352+
D3D12_BARRIER_ACCESS_VERTEX_BUFFER, // AccessAfter
353+
m_vertexBuffer.Get()
354+
)
355+
};
356+
D3D12_BARRIER_GROUP VertexBufBarrierGroups[] = { CD3DX12_BARRIER_GROUP(_countof(VertexBufBarriers), VertexBufBarriers) };
357+
m_commandList->Barrier(_countof(VertexBufBarrierGroups), VertexBufBarrierGroups);
358+
}
359+
else
360+
{
361+
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_vertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
362+
}
314363

315364
// Initialize the vertex buffer view.
316365
m_vertexBufferView.BufferLocation = m_vertexBuffer->GetGPUVirtualAddress();
@@ -380,34 +429,65 @@ void D3D12SmallResources::CreateTextures()
380429
CD3DX12_HEAP_DESC heapDesc(heapSize, D3D12_HEAP_TYPE_DEFAULT, 0, D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_DENY_RT_DS_TEXTURES);
381430
ThrowIfFailed(m_device->CreateHeap(&heapDesc, IID_PPV_ARGS(&m_textureHeap)));
382431

383-
std::vector<D3D12_RESOURCE_BARRIER> barriers;
384-
barriers.resize(TextureCount);
385-
for (UINT n = 0; n < TextureCount; n++)
432+
if (m_bIsEnhancedBarriersEnabled)
386433
{
387-
ThrowIfFailed(m_device->CreatePlacedResource(
388-
m_textureHeap.Get(),
389-
n * info.SizeInBytes,
390-
&textureDesc,
391-
D3D12_RESOURCE_STATE_COMMON,
392-
nullptr,
393-
IID_PPV_ARGS(&m_textures[n])));
394-
395-
barriers[n] = CD3DX12_RESOURCE_BARRIER::Aliasing(nullptr, m_textures[n].Get());
434+
for (UINT n = 0; n < TextureCount; n++)
435+
{
436+
ThrowIfFailed(m_device->CreatePlacedResource2(
437+
m_textureHeap.Get(),
438+
n * info.SizeInBytes,
439+
&CD3DX12_RESOURCE_DESC1(textureDesc),
440+
D3D12_BARRIER_LAYOUT_COMMON,
441+
nullptr,
442+
0,
443+
nullptr,
444+
IID_PPV_ARGS(&m_textures[n])));
445+
}
446+
}
447+
else
448+
{
449+
for (UINT n = 0; n < TextureCount; n++)
450+
{
451+
ThrowIfFailed(m_device->CreatePlacedResource(
452+
m_textureHeap.Get(),
453+
n * info.SizeInBytes,
454+
&textureDesc,
455+
D3D12_RESOURCE_STATE_COMMON,
456+
nullptr,
457+
IID_PPV_ARGS(&m_textures[n])));
458+
}
396459
}
397-
398-
m_copyCommandList->ResourceBarrier(static_cast<UINT>(barriers.size()), barriers.data());
399460
}
400461
else
401462
{
402-
for (UINT n = 0; n < TextureCount; n++)
463+
if (m_bIsEnhancedBarriersEnabled)
403464
{
404-
ThrowIfFailed(m_device->CreateCommittedResource(
405-
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
406-
D3D12_HEAP_FLAG_NONE,
407-
&textureDesc,
408-
D3D12_RESOURCE_STATE_COMMON,
409-
nullptr,
410-
IID_PPV_ARGS(&m_textures[n])));
465+
for (UINT n = 0; n < TextureCount; n++)
466+
{
467+
ThrowIfFailed(m_device->CreateCommittedResource3(
468+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
469+
D3D12_HEAP_FLAG_NONE,
470+
&CD3DX12_RESOURCE_DESC1(textureDesc),
471+
D3D12_BARRIER_LAYOUT_COMMON,
472+
nullptr,
473+
nullptr,
474+
0,
475+
nullptr,
476+
IID_PPV_ARGS(&m_textures[n])));
477+
}
478+
}
479+
else
480+
{
481+
for (UINT n = 0; n < TextureCount; n++)
482+
{
483+
ThrowIfFailed(m_device->CreateCommittedResource(
484+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
485+
D3D12_HEAP_FLAG_NONE,
486+
&textureDesc,
487+
D3D12_RESOURCE_STATE_COMMON,
488+
nullptr,
489+
IID_PPV_ARGS(&m_textures[n])));
490+
}
411491
}
412492
}
413493

@@ -427,13 +507,29 @@ void D3D12SmallResources::CreateTextures()
427507
{
428508
const UINT64 uploadBufferSize = GetRequiredIntermediateSize(m_textures[n].Get(), 0, 1) + D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
429509

430-
ThrowIfFailed(m_device->CreateCommittedResource(
431-
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
432-
D3D12_HEAP_FLAG_NONE,
433-
&CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
434-
D3D12_RESOURCE_STATE_GENERIC_READ,
435-
nullptr,
436-
IID_PPV_ARGS(&uploadResources[n])));
510+
if (m_bIsEnhancedBarriersEnabled)
511+
{
512+
ThrowIfFailed(m_device->CreateCommittedResource3(
513+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
514+
D3D12_HEAP_FLAG_NONE,
515+
&CD3DX12_RESOURCE_DESC1::Buffer(uploadBufferSize),
516+
D3D12_BARRIER_LAYOUT_UNDEFINED,
517+
nullptr,
518+
nullptr,
519+
0,
520+
nullptr,
521+
IID_PPV_ARGS(&uploadResources[n])));
522+
}
523+
else
524+
{
525+
ThrowIfFailed(m_device->CreateCommittedResource(
526+
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
527+
D3D12_HEAP_FLAG_NONE,
528+
&CD3DX12_RESOURCE_DESC::Buffer(uploadBufferSize),
529+
D3D12_RESOURCE_STATE_GENERIC_READ,
530+
nullptr,
531+
IID_PPV_ARGS(&uploadResources[n])));
532+
}
437533

438534
auto texture = GenerateTexture();
439535

@@ -611,8 +707,32 @@ void D3D12SmallResources::PopulateCommandList()
611707
m_commandList->RSSetViewports(1, &m_viewport);
612708
m_commandList->RSSetScissorRects(1, &m_scissorRect);
613709

614-
// Indicate that the back buffer will be used as a render target.
615-
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
710+
711+
if (m_bIsEnhancedBarriersEnabled)
712+
{
713+
D3D12_TEXTURE_BARRIER PresentToRTBarriers[] =
714+
{
715+
CD3DX12_TEXTURE_BARRIER(
716+
D3D12_BARRIER_SYNC_NONE, // SyncBefore
717+
D3D12_BARRIER_SYNC_RENDER_TARGET, // SyncAfter
718+
D3D12_BARRIER_ACCESS_NO_ACCESS, // AccessBefore
719+
D3D12_BARRIER_ACCESS_RENDER_TARGET, // AccessAfter
720+
D3D12_BARRIER_LAYOUT_PRESENT, // LayoutBefore
721+
D3D12_BARRIER_LAYOUT_RENDER_TARGET, // LayoutAfter
722+
m_renderTargets[m_frameIndex].Get(),
723+
CD3DX12_BARRIER_SUBRESOURCE_RANGE(0xffffffff), // All subresources
724+
D3D12_TEXTURE_BARRIER_FLAG_NONE
725+
)
726+
};
727+
728+
D3D12_BARRIER_GROUP PresentToRTBarriersGroups[] = { CD3DX12_BARRIER_GROUP(_countof(PresentToRTBarriers), PresentToRTBarriers) };
729+
m_commandList->Barrier(_countof(PresentToRTBarriersGroups), PresentToRTBarriersGroups);
730+
}
731+
else
732+
{
733+
// Indicate that the back buffer will be used as a render target.
734+
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
735+
}
616736

617737
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
618738
m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
@@ -640,9 +760,31 @@ void D3D12SmallResources::PopulateCommandList()
640760
PIXEndEvent(m_commandList.Get());
641761
}
642762

643-
// Indicate that the back buffer will now be used to present.
644-
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
763+
if (m_bIsEnhancedBarriersEnabled)
764+
{
765+
D3D12_TEXTURE_BARRIER RTToPresentBarriers[] =
766+
{
767+
CD3DX12_TEXTURE_BARRIER(
768+
D3D12_BARRIER_SYNC_RENDER_TARGET, // SyncBefore
769+
D3D12_BARRIER_SYNC_NONE, // SyncAfter
770+
D3D12_BARRIER_ACCESS_RENDER_TARGET, // AccessBefore
771+
D3D12_BARRIER_ACCESS_NO_ACCESS, // AccessAfter
772+
D3D12_BARRIER_LAYOUT_RENDER_TARGET, // LayoutBefore
773+
D3D12_BARRIER_LAYOUT_PRESENT, // LayoutAfter
774+
m_renderTargets[m_frameIndex].Get(),
775+
CD3DX12_BARRIER_SUBRESOURCE_RANGE(0xffffffff), // All subresources
776+
D3D12_TEXTURE_BARRIER_FLAG_NONE
777+
)
778+
};
645779

780+
D3D12_BARRIER_GROUP RTToPresentBarriersGroups[] = { CD3DX12_BARRIER_GROUP(_countof(RTToPresentBarriers), RTToPresentBarriers) };
781+
m_commandList->Barrier(_countof(RTToPresentBarriersGroups), RTToPresentBarriersGroups);
782+
}
783+
else
784+
{
785+
// Indicate that the back buffer will now be used to present.
786+
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
787+
}
646788
ThrowIfFailed(m_commandList->Close());
647789
}
648790

Samples/Desktop/D3D12SmallResources/src/D3D12SmallResources.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class D3D12SmallResources : public DXSample
5656
CD3DX12_RECT m_scissorRect;
5757
ComPtr<IDXGISwapChain3> m_swapChain;
5858
ComPtr<IDXGIAdapter3> m_adapter;
59-
ComPtr<ID3D12Device> m_device;
59+
ComPtr<ID3D12Device10> m_device;
6060
ComPtr<ID3D12Resource> m_renderTargets[FrameCount];
6161
ComPtr<ID3D12CommandAllocator> m_commandAllocators[FrameCount];
6262
ComPtr<ID3D12CommandAllocator> m_copyCommandAllocator;
@@ -76,14 +76,16 @@ class D3D12SmallResources : public DXSample
7676

7777
// Asset objects.
7878
ComPtr<ID3D12PipelineState> m_pipelineState;
79-
ComPtr<ID3D12GraphicsCommandList> m_commandList;
80-
ComPtr<ID3D12GraphicsCommandList> m_copyCommandList;
79+
ComPtr<ID3D12GraphicsCommandList8> m_commandList;
80+
ComPtr<ID3D12GraphicsCommandList8> m_copyCommandList;
8181
ComPtr<ID3D12Resource> m_vertexBuffer;
8282
std::vector<ComPtr<ID3D12Resource>> m_textures;
8383
ComPtr<ID3D12Heap> m_textureHeap; // Only used when the resources being drawn are placed resources.
8484
bool m_usePlacedResources;
8585
D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
8686

87+
bool m_bIsEnhancedBarriersEnabled;
88+
8789
void LoadPipeline();
8890
void LoadAssets();
8991
std::vector<UINT8> GenerateTexture();

0 commit comments

Comments
 (0)