Beispiel #1
0
        public override void SetDefaultRenderTarget(int width, int height, bool doClearColor)
        {
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type  = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags = SharpDX.Direct3D12.ResourceBarrierFlags.None;
            if (_sampleDesc.Count > 1)
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffersAA[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveSource,
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget)
                {
                    Subresource = 0
                };
            }
            else
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.Present,
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget)
                {
                    Subresource = 0
                };
            }
            _commands.ResourceBarrier(barrier);

            SharpDX.Direct3D12.CpuDescriptorHandle rtv = _heapRTV.CPUDescriptorHandleForHeapStart;
            rtv.Ptr += _frameIndex * _sizeRTV;

            if (doClearColor)
            {
                SharpDX.Mathematics.Interop.RawColor4 clearColor = new SharpDX.Mathematics.Interop.RawColor4(0.0f, 0.0f, 0.0f, 0.0f);
                _commands.ClearRenderTargetView(rtv, clearColor, 0, null);
            }

            SharpDX.Direct3D12.CpuDescriptorHandle dsv = _heapDSV.CPUDescriptorHandleForHeapStart;
            _commands.ClearDepthStencilView(dsv, SharpDX.Direct3D12.ClearFlags.FlagsStencil, 0.0f, 0, 0, null);
            _commands.SetRenderTargets(1, rtv, dsv);
            _commands.SetViewports(1, _viewport);
        }
Beispiel #2
0
        /// <unmanaged>void ID3D12CommandList::ResourceBarrier([In] unsigned int Count,[In, Buffer] const D3D12_RESOURCE_BARRIER_DESC* pDesc)</unmanaged>
        /// <unmanaged-short>ID3D12CommandList::ResourceBarrier</unmanaged-short>
        public unsafe void ResourceBarrierAliasing(Resource resourceBefore, Resource resourceAfter)
        {
            var barrier = new ResourceBarrier(new ResourceAliasingBarrier(resourceBefore, resourceAfter));

            ResourceBarrier(1, new IntPtr(&barrier));
        }
Beispiel #3
0
        /// <unmanaged>void ID3D12CommandList::ResourceBarrier([In] unsigned int Count,[In, Buffer] const D3D12_RESOURCE_BARRIER_DESC* pDesc)</unmanaged>
        /// <unmanaged-short>ID3D12CommandList::ResourceBarrier</unmanaged-short>
        public unsafe void ResourceBarrierTransition(Resource resource, int subresource, ResourceStates stateBefore, ResourceStates stateAfter)
        {
            var barrier = new ResourceBarrier(new ResourceTransitionBarrier(resource, subresource, stateBefore, stateAfter));

            ResourceBarrier(1, new IntPtr(&barrier));
        }
 /// <summary>	
 /// <p> Notifies the driver that it needs to synchronize multiple accesses to resources. </p>	
 /// </summary>	
 /// <param name="numBarriers"><dd>  <p> The number of submitted barrier descriptions. </p> </dd></param>	
 /// <param name="barriersRef"><dd>  <p> Pointer to an array of barrier descriptions. </p> </dd></param>	
 /// <remarks>	
 /// <p>There are three types of barrier descriptions:</p><ul> <li> <strong><see cref="SharpDX.Direct3D12.ResourceTransitionBarrier"/></strong> -  Transition barriers  indicate that a set of subresources transition between different usages.  The caller must specify the <em>before</em> and <em>after</em> usages of the subresources.  The D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES flag is used to transition all subresources in a resource at the same time. </li> <li> <strong><see cref="SharpDX.Direct3D12.ResourceAliasingBarrier"/></strong> - Aliasing barriers indicate a transition between usages of two different resources which have mappings into the same heap.  The application can specify both the before and the after resource.  Note that one or both resources can be <c>null</c> (indicating that any tiled resource could cause aliasing). </li> <li> <strong><see cref="SharpDX.Direct3D12.ResourceUnorderedAccessViewBarrier"/></strong> - Unordered access view barriers indicate all UAV accesses (read or writes) to a particular resource must complete before any future UAV accesses (read or write) can begin.  The specified resource cannot be <c>null</c>.  It is not necessary to insert a UAV barrier between two draw or dispatch calls which only read a UAV.  Additionally, it is not necessary to insert a UAV barrier between two draw or dispatch calls which write to the same UAV if the application knows that it is safe to execute the UAV accesses in any order.  The resource can be <c>null</c> (indicating that any UAV access could require the barrier). </li> </ul><p> When <strong><see cref="SharpDX.Direct3D12.GraphicsCommandList.ResourceBarrier"/></strong> is passed an array of resource barrier descriptions, the API behaves as if it was called N times (1 for each array element), in the specified order. </p><p> For descriptions of the usage states a subresource can be in, see the <strong><see cref="SharpDX.Direct3D12.ResourceStates"/></strong> enumeration and the Using Resource Barriers to Synchronize Resource States in Direct3D 12 section. </p><p> A subresource can be in any state when <strong><see cref="SharpDX.Direct3D12.GraphicsCommandList.DiscardResource"/></strong> is called. </p><p> When a back buffer is presented, it must be in the <see cref="SharpDX.Direct3D12.ResourceStates.Present"/> state.  If <strong>Present</strong> is called on a resource which is not in the PRESENT state, a debug layer warning will be emitted. </p><p>The resource usage bits are group into two categories, read-only and read/write.</p><p> The following usage bits are read-only: </p><ul> <li><see cref="SharpDX.Direct3D12.ResourceStates.VertexAndConstantBuffer"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.IndexBuffer"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.NonPixelShaderResource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.PixelShaderResource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.IndirectArgument"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.CopySource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.DepthRead"/></li> </ul><p>The following usage bits are read/write:</p><ul> <li><see cref="SharpDX.Direct3D12.ResourceStates.CopyDestination"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.RenderTarget"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.UnorderedAccess"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.DepthWrite"/></li> <li>D3D12_RESOURCE_STATE_GENERATE_MIPS</li> <li><see cref="SharpDX.Direct3D12.ResourceStates.StreamOut"/></li> </ul><p> At most one write bit can be set. If any write bit is set, then no read bit may be set. If no write bit is set, then any number of read bits may be set.  </p><p> At any given time, a subresource is in exactly one  state (determined by a set of flags).  The application must ensure that the states are matched when making a sequence of <strong>ResourceBarrier</strong> calls. In other words, the before and after states in consecutive calls to <strong>ResourceBarrier</strong> must agree. </p><p>To transition all subresources within a resource, the application can set the subresource index to D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, which implies that all subresources are changed.</p><p> For improved performance, applications should use split barriers (refer to Synchronization and Multi-Engine). Applications should also batch multiple transitions into a single call whenever possible. </p>	
 /// </remarks>	
 /// <include file='.\..\Documentation\CodeComments.xml' path="/comments/comment[@id='ID3D12GraphicsCommandList::ResourceBarrier']/*"/>	
 /// <msdn-id>dn903898</msdn-id>	
 /// <unmanaged>void ID3D12GraphicsCommandList::ResourceBarrier([In] unsigned int NumBarriers,[In, Buffer] const void* pBarriers)</unmanaged>	
 /// <unmanaged-short>ID3D12GraphicsCommandList::ResourceBarrier</unmanaged-short>	
 public unsafe void ResourceBarrierTransition(Resource resource, int subresource, ResourceStates stateBefore, ResourceStates stateAfter)
 {
     var barrier = new ResourceBarrier(new ResourceTransitionBarrier(resource, subresource, stateBefore, stateAfter));
     ResourceBarrier(1, new IntPtr(&barrier));
 }
 /// <summary>	
 /// <p> Notifies the driver that it needs to synchronize multiple accesses to resources. </p>	
 /// </summary>	
 /// <param name="numBarriers"><dd>  <p> The number of submitted barrier descriptions. </p> </dd></param>	
 /// <param name="barriersRef"><dd>  <p> Pointer to an array of barrier descriptions. </p> </dd></param>	
 /// <remarks>	
 /// <p>There are three types of barrier descriptions:</p><ul> <li> <strong><see cref="SharpDX.Direct3D12.ResourceTransitionBarrier"/></strong> -  Transition barriers  indicate that a set of subresources transition between different usages.  The caller must specify the <em>before</em> and <em>after</em> usages of the subresources.  The D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES flag is used to transition all subresources in a resource at the same time. </li> <li> <strong><see cref="SharpDX.Direct3D12.ResourceAliasingBarrier"/></strong> - Aliasing barriers indicate a transition between usages of two different resources which have mappings into the same heap.  The application can specify both the before and the after resource.  Note that one or both resources can be <c>null</c> (indicating that any tiled resource could cause aliasing). </li> <li> <strong><see cref="SharpDX.Direct3D12.ResourceUnorderedAccessViewBarrier"/></strong> - Unordered access view barriers indicate all UAV accesses (read or writes) to a particular resource must complete before any future UAV accesses (read or write) can begin.  The specified resource cannot be <c>null</c>.  It is not necessary to insert a UAV barrier between two draw or dispatch calls which only read a UAV.  Additionally, it is not necessary to insert a UAV barrier between two draw or dispatch calls which write to the same UAV if the application knows that it is safe to execute the UAV accesses in any order.  The resource can be <c>null</c> (indicating that any UAV access could require the barrier). </li> </ul><p> When <strong><see cref="SharpDX.Direct3D12.GraphicsCommandList.ResourceBarrier"/></strong> is passed an array of resource barrier descriptions, the API behaves as if it was called N times (1 for each array element), in the specified order. </p><p> For descriptions of the usage states a subresource can be in, see the <strong><see cref="SharpDX.Direct3D12.ResourceStates"/></strong> enumeration and the Using Resource Barriers to Synchronize Resource States in Direct3D 12 section. </p><p> A subresource can be in any state when <strong><see cref="SharpDX.Direct3D12.GraphicsCommandList.DiscardResource"/></strong> is called. </p><p> When a back buffer is presented, it must be in the <see cref="SharpDX.Direct3D12.ResourceStates.Present"/> state.  If <strong>Present</strong> is called on a resource which is not in the PRESENT state, a debug layer warning will be emitted. </p><p>The resource usage bits are group into two categories, read-only and read/write.</p><p> The following usage bits are read-only: </p><ul> <li><see cref="SharpDX.Direct3D12.ResourceStates.VertexAndConstantBuffer"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.IndexBuffer"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.NonPixelShaderResource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.PixelShaderResource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.IndirectArgument"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.CopySource"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.DepthRead"/></li> </ul><p>The following usage bits are read/write:</p><ul> <li><see cref="SharpDX.Direct3D12.ResourceStates.CopyDestination"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.RenderTarget"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.UnorderedAccess"/></li> <li><see cref="SharpDX.Direct3D12.ResourceStates.DepthWrite"/></li> <li>D3D12_RESOURCE_STATE_GENERATE_MIPS</li> <li><see cref="SharpDX.Direct3D12.ResourceStates.StreamOut"/></li> </ul><p> At most one write bit can be set. If any write bit is set, then no read bit may be set. If no write bit is set, then any number of read bits may be set.  </p><p> At any given time, a subresource is in exactly one  state (determined by a set of flags).  The application must ensure that the states are matched when making a sequence of <strong>ResourceBarrier</strong> calls. In other words, the before and after states in consecutive calls to <strong>ResourceBarrier</strong> must agree. </p><p>To transition all subresources within a resource, the application can set the subresource index to D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, which implies that all subresources are changed.</p><p> For improved performance, applications should use split barriers (refer to Synchronization and Multi-Engine). Applications should also batch multiple transitions into a single call whenever possible. </p>	
 /// </remarks>	
 /// <include file='.\..\Documentation\CodeComments.xml' path="/comments/comment[@id='ID3D12GraphicsCommandList::ResourceBarrier']/*"/>	
 /// <msdn-id>dn903898</msdn-id>	
 /// <unmanaged>void ID3D12GraphicsCommandList::ResourceBarrier([In] unsigned int NumBarriers,[In, Buffer] const void* pBarriers)</unmanaged>	
 /// <unmanaged-short>ID3D12GraphicsCommandList::ResourceBarrier</unmanaged-short>	
 public unsafe void ResourceBarrierAliasing(Resource resourceBefore, Resource resourceAfter)
 {
     var barrier = new ResourceBarrier(new ResourceAliasingBarrier(resourceBefore, resourceAfter));
     ResourceBarrier(1, new IntPtr(&barrier));
 }
Beispiel #6
0
        public override void Swap()
        {
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type  = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags = SharpDX.Direct3D12.ResourceBarrierFlags.None;

            if (_sampleDesc.Count > 1)
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffersAA[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget,
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveSource)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);

                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.Present,
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveDestination)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);

                _commands.ResolveSubresource(_backBuffers[_frameIndex], 0, _backBuffersAA[_frameIndex], 0, _format);

                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveDestination,
                                                                                      SharpDX.Direct3D12.ResourceStates.Present)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);
            }
            else
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget,
                                                                                      SharpDX.Direct3D12.ResourceStates.Present)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);
            }

            // TODO: transition video textures

            _commands.Close();
            _queue.ExecuteCommandList(_commands);

            _fenceValue++;
            _queue.Signal(_fence, _fenceValue);
            _frameFenceValues[_frameIndex] = _fenceValue;

            _swapChain.Present(_syncInterval, _presentFlags);
            _frameIndex = _swapChain.CurrentBackBufferIndex;
        }
Beispiel #7
0
        public override ImageCapture CaptureRenderTarget(RenderTarget surface)
        {
            IntPtr texNativePtr = RenderDeviceD3D11.GetTextureNativePointer(surface.Texture);

            SharpDX.Direct3D12.Resource            texture = new SharpDX.Direct3D12.Resource(texNativePtr);
            SharpDX.Direct3D12.ResourceDescription texDesc = texture.Description;

            // Transition PIXEL_SHADER_RESOURCE -> COPY_SOURCE
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type       = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags      = SharpDX.Direct3D12.ResourceBarrierFlags.None;
            barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(texture,
                                                                                  SharpDX.Direct3D12.ResourceStates.PixelShaderResource,
                                                                                  SharpDX.Direct3D12.ResourceStates.CopySource)
            {
                Subresource = 0
            };

            _commands.ResourceBarrier(barrier);

            long totalBytes;

            long[] rowSize = { 0 };
            _dev.GetCopyableFootprints(ref texDesc, 0, 1, 0, null, null, rowSize, out totalBytes);
            long pitch = (rowSize[0] + 255) & ~255;

            // Create staging texture
            SharpDX.Direct3D12.HeapProperties heap;
            heap.Type                 = SharpDX.Direct3D12.HeapType.Readback;
            heap.CPUPageProperty      = SharpDX.Direct3D12.CpuPageProperty.Unknown;
            heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown;
            heap.CreationNodeMask     = 0;
            heap.VisibleNodeMask      = 0;

            SharpDX.Direct3D12.ResourceDescription desc;
            desc.Dimension         = SharpDX.Direct3D12.ResourceDimension.Buffer;
            desc.Alignment         = 0;
            desc.Width             = pitch * texDesc.Height * 4;
            desc.Height            = 1;
            desc.DepthOrArraySize  = 1;
            desc.MipLevels         = 1;
            desc.Format            = SharpDX.DXGI.Format.Unknown;
            desc.SampleDescription = new SampleDescription {
                Count = 1, Quality = 0
            };
            desc.Layout = SharpDX.Direct3D12.TextureLayout.RowMajor;
            desc.Flags  = SharpDX.Direct3D12.ResourceFlags.None;

            SharpDX.Direct3D12.Resource stagingTex = _dev.CreateCommittedResource(heap, SharpDX.Direct3D12.HeapFlags.None,
                                                                                  desc, SharpDX.Direct3D12.ResourceStates.CopyDestination, null);

            SharpDX.Direct3D12.TextureCopyLocation copySrc = new SharpDX.Direct3D12.TextureCopyLocation(texture, 0);
            SharpDX.Direct3D12.TextureCopyLocation copyDst = new SharpDX.Direct3D12.TextureCopyLocation(texture,
                                                                                                        new SharpDX.Direct3D12.PlacedSubResourceFootprint
            {
                Offset    = 0,
                Footprint = new SharpDX.Direct3D12.SubResourceFootprint
                {
                    Width    = (int)texDesc.Width,
                    Height   = (int)texDesc.Height,
                    Depth    = 1,
                    RowPitch = (int)pitch,
                    Format   = texDesc.Format
                }
            });

            // Wait GPU completion
            _commands.CopyTextureRegion(copyDst, 0, 0, 0, copySrc, null);
            _commands.Close();
            _queue.ExecuteCommandList(_commands);
            WaitForGpu();

            // Ensure image capture object is created with the appropriate size
            if (_imageCapture == null || _imageCapture.Width != desc.Width || _imageCapture.Height != desc.Height)
            {
                _imageCapture = new ImageCapture((uint)desc.Width, (uint)desc.Height);
            }

            // Map and copy to image
            byte[] dst = _imageCapture.Pixels;
            IntPtr src = stagingTex.Map(0);

            for (int i = 0; i < desc.Height; ++i)
            {
                int dstRow = i * (int)_imageCapture.Stride;
                int srcRow = i * (int)pitch;

                for (int j = 0; j < desc.Width; j++)
                {
                    // RGBA -> BGRA
                    dst[dstRow + 4 * j + 2] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 0);
                    dst[dstRow + 4 * j + 1] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 1);
                    dst[dstRow + 4 * j + 0] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 2);
                    dst[dstRow + 4 * j + 3] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 3);
                }
            }

            stagingTex.Unmap(0);

            return(_imageCapture);
        }