Ejemplo n.º 1
0
    public static void Main()
    {
        using ID3D12Device device = D3D12CreateDevice <ID3D12Device>(null, FeatureLevel.Level_12_1);

        using IDStorageFactory factory = DStorageGetFactory <IDStorageFactory>();

        string fileToLoad = "Test.txt";
        Result result     = factory.OpenFile(fileToLoad, out IDStorageFile? file);

        if (result.Failure)
        {
            Console.WriteLine($"The file '{fileToLoad}' could not be opened. HRESULT={result}");
            //ShowHelpText();
            return;
        }

        ByHandleFileInformation info = file !.FileInformation;
        uint fileSize = info.FileSizeLow;

        // Create a DirectStorage queue which will be used to load data into a buffer on the GPU.
        QueueDesc queueDesc = new QueueDesc
        {
            Capacity   = MaxQueueCapacity,
            Priority   = Priority.Normal,
            SourceType = RequestSourceType.File,
            Device     = device
        };

        using IDStorageQueue queue = factory.CreateQueue(queueDesc);

        // Create the ID3D12Resource buffer which will be populated with the file's contents
        HeapProperties      bufferHeapProps = new HeapProperties(HeapType.Default);
        ResourceDescription bufferDesc      = ResourceDescription.Buffer(fileSize);

        using ID3D12Resource bufferResource = device.CreateCommittedResource(
                  bufferHeapProps,
                  HeapFlags.None,
                  bufferDesc,
                  ResourceStates.Common
                  );

        // Enqueue a request to read the file contents into a destination D3D12 buffer resource.
        // Note: The example request below is performing a single read of the entire file contents.
        Request request = new Request();

        request.Options.SourceType          = RequestSourceType.File;
        request.Options.DestinationType     = RequestDestinationType.Buffer;
        request.Source.File.Source          = file;
        request.Source.File.Offset          = 0;
        request.Source.File.Size            = fileSize;
        request.UncompressedSize            = fileSize;
        request.Destination.Buffer.Resource = bufferResource;
        request.Destination.Buffer.Offset   = 0;
        request.Destination.Buffer.Size     = request.Source.File.Size;
        queue.EnqueueRequest(request);

        // Configure a fence to be signaled when the request is completed
        using ID3D12Fence fence         = device.CreateFence();
        using AutoResetEvent fenceEvent = new AutoResetEvent(false);

        ulong fenceValue = 1;

        fence.SetEventOnCompletion(fenceValue, fenceEvent).CheckError();
        queue.EnqueueSignal(fence, fenceValue);

        // Tell DirectStorage to start executing all queued items.
        queue.Submit();

        // Wait for the submitted work to complete
        Console.WriteLine("Waiting for the DirectStorage request to complete...");
        fenceEvent.WaitOne();

        // Check the status array for errors.
        // If an error was detected the first failure record
        // can be retrieved to get more details.
        ErrorRecord errorRecord = queue.RetrieveErrorRecord();

        if (errorRecord.FirstFailure.HResult.Failure)
        {
            //
            // errorRecord.FailureCount - The number of failed requests in the queue since the last
            //                            RetrieveErrorRecord call.
            // errorRecord.FirstFailure - Detailed record about the first failed command in the enqueue order.
            //
            Console.WriteLine($"The DirectStorage request failed! HRESULT={errorRecord.FirstFailure.HResult}");
        }
        else
        {
            Console.WriteLine("The DirectStorage request completed successfully!");
        }
    }
Ejemplo n.º 2
0
 public uint SubmitCommandList(ID3D12GraphicsCommandList4 pCmdList, ID3D12CommandQueue pCmdQueue, ID3D12Fence pFence, uint fenceValue)
 {
     pCmdList.Close();
     pCmdQueue.ExecuteCommandList(pCmdList);
     fenceValue++;
     pCmdQueue.Signal(pFence, fenceValue);
     return(fenceValue);
 }
 /// <summary>
 /// Asynchronously makes objects resident for the device.
 /// </summary>
 public Result EnqueueMakeResident(ResidencyFlags flags, ID3D12Pageable[] objects, ID3D12Fence fenceToSignal, ulong fenceValueToSignal)
 {
     return(EnqueueMakeResident(flags, objects?.Length ?? 0, objects, fenceToSignal, fenceValueToSignal));
 }
Ejemplo n.º 4
0
 internal bool IsFenceComplete(ID3D12Fence fence, long fenceValue)
 {
     return(fence.CompletedValue >= fenceValue);
 }
Ejemplo n.º 5
0
        protected Application(bool useDirect3D12)
        {
            _wndProc = ProcessWindowMessage;
            var wndClassEx = new WNDCLASSEX
            {
                Size                  = Unsafe.SizeOf <WNDCLASSEX>(),
                Styles                = WindowClassStyles.CS_HREDRAW | WindowClassStyles.CS_VREDRAW | WindowClassStyles.CS_OWNDC,
                WindowProc            = _wndProc,
                InstanceHandle        = HInstance,
                CursorHandle          = LoadCursor(IntPtr.Zero, SystemCursor.IDC_ARROW),
                BackgroundBrushHandle = IntPtr.Zero,
                IconHandle            = IntPtr.Zero,
                ClassName             = WndClassName,
            };

            var atom = RegisterClassEx(ref wndClassEx);

            if (atom == 0)
            {
                throw new InvalidOperationException(
                          $"Failed to register window class. Error: {Marshal.GetLastWin32Error()}"
                          );
            }

            Window = new Window("Vortice", 800, 600);

            if (useDirect3D12 &&
                !ID3D12Device.IsSupported(null, FeatureLevel.Level_11_0))
            {
                useDirect3D12 = false;
            }

            var debugFactory = false;

#if DEBUG
            if (useDirect3D12)
            {
                if (D3D12GetDebugInterface <ID3D12Debug>(out var debug).Success)
                {
                    debug.EnableDebugLayer();
                    debugFactory = true;
                }
            }
#endif

            if (useDirect3D12)
            {
                if (CreateDXGIFactory2(debugFactory, out IDXGIFactory4 dxgiFactory4).Failure)
                {
                    throw new InvalidOperationException("Cannot create IDXGIFactory4");
                }

                _dxgiFactory = dxgiFactory4;
            }
            else
            {
                if (CreateDXGIFactory2(debugFactory, out _dxgiFactory).Failure)
                {
                    throw new InvalidOperationException("Cannot create IDXGIFactory4");
                }
            }

            if (useDirect3D12)
            {
                Debug.Assert(D3D12CreateDevice(null, FeatureLevel.Level_11_0, out _d3d12Device).Success);

                _d3d12CommandQueue = _d3d12Device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct, CommandQueuePriority.Normal));
            }
            else
            {
                var featureLevels = new FeatureLevel[]
                {
                    FeatureLevel.Level_11_1,
                    FeatureLevel.Level_11_0
                };
                Debug.Assert(ID3D11Device.TryCreate(
                                 null,
                                 DriverType.Hardware,
                                 DeviceCreationFlags.BgraSupport,
                                 featureLevels,
                                 out _d3d11Device,
                                 out _d3d11DeviceContext).Success);
            }

            var swapChainDesc = new SwapChainDescription1
            {
                BufferCount       = FrameCount,
                Width             = Window.Width,
                Height            = Window.Height,
                Format            = Format.B8G8R8A8_UNorm,
                Usage             = Vortice.Usage.RenderTargetOutput,
                SwapEffect        = SwapEffect.FlipDiscard,
                SampleDescription = new SampleDescription(1, 0)
            };

            SwapChain = DXGIFactory.CreateSwapChainForHwnd(_d3d12CommandQueue, Window.Handle, swapChainDesc);
            DXGIFactory.MakeWindowAssociation(Window.Handle, WindowAssociationFlags.IgnoreAltEnter);

            if (useDirect3D12)
            {
                SwapChain3  = SwapChain.QueryInterface <IDXGISwapChain3>();
                _frameIndex = SwapChain3.GetCurrentBackBufferIndex();
            }

            _rtvHeap           = _d3d12Device.CreateDescriptorHeap(new DescriptorHeapDescription(DescriptorHeapType.RenderTargetView, FrameCount));
            _rtvDescriptorSize = _d3d12Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            // Create frame resources.
            {
                var rtvHandle = _rtvHeap.GetCPUDescriptorHandleForHeapStart();

                // Create a RTV for each frame.
                _renderTargets = new ID3D12Resource[FrameCount];
                for (var i = 0; i < FrameCount; i++)
                {
                    _renderTargets[i] = SwapChain.GetBuffer <ID3D12Resource>(i);
                    _d3d12Device.CreateRenderTargetView(_renderTargets[i], null, rtvHandle);
                    rtvHandle += _rtvDescriptorSize;
                }
            }

            _commandAllocator = _d3d12Device.CreateCommandAllocator(CommandListType.Direct);
            _commandList      = _d3d12Device.CreateCommandList(CommandListType.Direct, _commandAllocator);
            _commandList.Close();

            // Create synchronization objects.
            _d3d12Fence = _d3d12Device.CreateFence(0);
            _fenceValue = 1;
            _fenceEvent = new AutoResetEvent(false);
        }