33#ifdef USE_DIRECTX_RENDERER
44#include " Renderer/CommandBuffer.h"
55
6- #include " DirectXTransitionManager.h"
76#include " DirectXDevice.h"
87#include " DirectXUtils.h"
98
@@ -57,15 +56,12 @@ void CommandBuffer::BeginRenderPass(const RenderPassDescriptor& renderPassDesc,
5756
5857 auto resource = static_cast <ID3D12Resource*>(colorAttachmentDesc.texture .GetHandle ());
5958 colorAttachments[i].cpuDescriptor = colorAttachmentDesc.texture .GetRenderTargetView ();
60- DirectXTransitionManager::TransitionLayout (mHandle ->commandList ,
61- resource, D3D12_RESOURCE_STATE_RENDER_TARGET );
6259 }
6360
6461 if (renderPassDesc.depthAttachment .texture .IsValid ())
6562 {
6663 auto format = renderPassDesc.depthAttachment .texture .GetDescriptor ().format ;
6764 auto resource = static_cast <ID3D12Resource*>(renderPassDesc.depthAttachment .texture .GetHandle ());
68- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , resource, D3D12_RESOURCE_STATE_DEPTH_WRITE );
6965
7066 D3D12_RENDER_PASS_DEPTH_STENCIL_DESC depthAttachment{};
7167 depthAttachment.cpuDescriptor = renderPassDesc.depthAttachment .texture .GetRenderTargetView ();
@@ -171,14 +167,7 @@ void CommandBuffer::CopyBuffer(const NativeGraphicsHandle src, const NativeGraph
171167{
172168 auto srcBuffer = static_cast <ID3D12Resource*>(src);
173169 auto dstBuffer = static_cast <ID3D12Resource*>(dst);
174-
175- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , dstBuffer, D3D12_RESOURCE_STATE_COPY_DEST );
176- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , srcBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE );
177-
178170 mHandle ->commandList ->CopyBufferRegion (dstBuffer, dstOffset, srcBuffer, srcOffset, size);
179-
180- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , srcBuffer, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE );
181- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , dstBuffer, D3D12_RESOURCE_STATE_ALL_SHADER_RESOURCE );
182171}
183172
184173void CommandBuffer::Blit (const Texture& source, const Texture& destination) const
@@ -187,9 +176,6 @@ void CommandBuffer::Blit(const Texture& source, const Texture& destination) cons
187176 auto srcTexture = static_cast <ID3D12Resource*>(source.GetHandle ());
188177 auto dstTexture = static_cast <ID3D12Resource*>(destination.GetHandle ());
189178
190- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , dstTexture, D3D12_RESOURCE_STATE_COPY_DEST );
191- DirectXTransitionManager::TransitionLayout (mHandle ->commandList , srcTexture, D3D12_RESOURCE_STATE_COPY_SOURCE );
192-
193179 D3D12_TEXTURE_COPY_LOCATION dst{};
194180 dst.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX ;
195181 dst.pResource = dstTexture;
@@ -202,6 +188,68 @@ void CommandBuffer::Blit(const Texture& source, const Texture& destination) cons
202188 mHandle ->commandList ->CopyTextureRegion (&dst, 0 , 0 , 0 , &src, nullptr );
203189}
204190
191+ void CommandBuffer::Barrier (const BarrierGroup& barrier) const
192+ {
193+ TArray<D3D12_BUFFER_BARRIER > d3d12BufferBarriers;
194+ TArray<D3D12_TEXTURE_BARRIER > d3d12TextureBarriers;
195+
196+ d3d12BufferBarriers.reserve (barrier.bufferBarriers .size ());
197+ d3d12TextureBarriers.reserve (barrier.textureBarriers .size ());
198+
199+ for (const BufferBarrier& bufferBarrier : barrier.bufferBarriers )
200+ {
201+ D3D12_BUFFER_BARRIER d3d12Barrier = {};
202+ d3d12Barrier.SyncBefore = BarrierStageToD3D12_BARRIER_SYNC (bufferBarrier.srcStage );
203+ d3d12Barrier.SyncAfter = BarrierStageToD3D12_BARRIER_SYNC (bufferBarrier.dstStage );
204+ d3d12Barrier.AccessBefore = BarrierAccessToD3D12_BARRIER_ACCESS (bufferBarrier.srcAccess );
205+ d3d12Barrier.AccessAfter = BarrierAccessToD3D12_BARRIER_ACCESS (bufferBarrier.dstAccess );
206+ d3d12Barrier.pResource = static_cast <ID3D12Resource*>(bufferBarrier.resource );
207+ d3d12Barrier.Offset = 0 ;
208+ d3d12Barrier.Size = UINT64_MAX ;
209+ d3d12BufferBarriers.emplace_back (d3d12Barrier);
210+ }
211+
212+ for (const TextureBarrier& textureBarrier : barrier.textureBarriers )
213+ {
214+ D3D12_TEXTURE_BARRIER d3d12Barrier = {};
215+ d3d12Barrier.SyncBefore = BarrierStageToD3D12_BARRIER_SYNC (textureBarrier.srcStage );
216+ d3d12Barrier.SyncAfter = BarrierStageToD3D12_BARRIER_SYNC (textureBarrier.dstStage );
217+ d3d12Barrier.AccessBefore = BarrierAccessToD3D12_BARRIER_ACCESS (textureBarrier.srcAccess );
218+ d3d12Barrier.AccessAfter = BarrierAccessToD3D12_BARRIER_ACCESS (textureBarrier.dstAccess );
219+ d3d12Barrier.LayoutBefore = BarrierLayoutToD3D12_BARRIER_LAYOUT (textureBarrier.oldLayout );
220+ d3d12Barrier.LayoutAfter = BarrierLayoutToD3D12_BARRIER_LAYOUT (textureBarrier.newLayout );
221+ d3d12Barrier.pResource = static_cast <ID3D12Resource*>(textureBarrier.resource );
222+ d3d12Barrier.Subresources .IndexOrFirstMipLevel = 0xffffffff ;
223+ d3d12Barrier.Subresources .NumMipLevels = 0 ;
224+ d3d12Barrier.Subresources .FirstArraySlice = 0 ;
225+ d3d12Barrier.Subresources .NumArraySlices = 0 ;
226+ d3d12Barrier.Subresources .FirstPlane = 0 ;
227+ d3d12Barrier.Subresources .NumPlanes = 0 ;
228+ d3d12Barrier.Flags = D3D12_TEXTURE_BARRIER_FLAG_NONE ;
229+ d3d12TextureBarriers.emplace_back (d3d12Barrier);
230+ }
231+
232+ TArray<D3D12_BARRIER_GROUP > barrierGroups;
233+ if (not d3d12BufferBarriers.empty ())
234+ {
235+ D3D12_BARRIER_GROUP bufferGroup = {};
236+ bufferGroup.Type = D3D12_BARRIER_TYPE_BUFFER ;
237+ bufferGroup.NumBarriers = static_cast <UINT32 >(d3d12BufferBarriers.size ());
238+ bufferGroup.pBufferBarriers = d3d12BufferBarriers.data ();
239+ barrierGroups.emplace_back (bufferGroup);
240+ }
241+
242+ if (not d3d12TextureBarriers.empty ())
243+ {
244+ D3D12_BARRIER_GROUP textureGroup = {};
245+ textureGroup.Type = D3D12_BARRIER_TYPE_TEXTURE ;
246+ textureGroup.NumBarriers = static_cast <UINT32 >(d3d12TextureBarriers.size ());
247+ textureGroup.pTextureBarriers = d3d12TextureBarriers.data ();
248+ barrierGroups.emplace_back (textureGroup);
249+ }
250+ mHandle ->commandList ->Barrier (static_cast <UINT32 >(barrierGroups.size ()), barrierGroups.data ());
251+ }
252+
205253void CommandBuffer::Begin (const TStringView debugName) const
206254{
207255 TWString debugNameW = StringUtils::Convert (debugName);
0 commit comments