Пример #1
0
        public Device(ref Descriptor desc, FeatureLevel featureLevel = FeatureLevel.Level_12_0)
            : base(ref desc)
        {
            if (desc.DebugDevice)
            {
                var debugInterface = SharpDX.Direct3D12.DebugInterface.Get();
                if (debugInterface != null)
                {
                    debugInterface.EnableDebugLayer();
                }
                else
                {
                    Log.Error("Failed to obtain DX12 debug layer.");
                }
            }

            // Use first adapter that is supported.
            using (SharpDX.DXGI.Factory dxgiFactory = new Factory4())
            {
                for (int adapterIndex = 0;; ++adapterIndex)
                {
                    var adapter = dxgiFactory.GetAdapter(adapterIndex);
                    if (adapter == null)
                    {
                        // TODO: Throw exception
                        return;
                    }

                    try
                    {
                        DeviceD3D12 = new SharpDX.Direct3D12.Device(adapter, (SharpDX.Direct3D.FeatureLevel) featureLevel);
                        Adapter = adapter;
                        break;
                    }

                    catch (Exception)
                    {
                        DeviceD3D12 = null;
                        adapter.Dispose();
                    }
                }
            }

            // Get Resource handle increment sizes.
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.Sampler] = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.DepthStencil] = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.RenderTarget] = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.ShaderResource] = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            Memory.Enums.InitLookupTables();

            Log.Info("Successfully created a DX12 device with adapter \"{0}\"", Adapter.Description.Description);
        }
Пример #2
0
        private void CreateCommandObjects()
        {
            var queueDesc = new CommandQueueDescription(CommandListType.Direct);

            CommandQueue = Device.CreateCommandQueue(queueDesc);

            DirectCmdListAlloc = Device.CreateCommandAllocator(CommandListType.Direct);

            CommandList = Device.CreateCommandList(
                0,
                CommandListType.Direct,
                DirectCmdListAlloc, // Associated command allocator.
                null);              // Initial PipelineStateObject.

            // Start off in a closed state.  This is because the first time we refer
            // to the command list we will Reset it, and it needs to be closed before
            // calling Reset.
            CommandList.Close();
        }
Пример #3
0
        public static List <Mesh> New(Device device,
                                      GraphicsCommandList commandList,
                                      PrimitiveTopology primitiveTopology,
                                      ref int index, Vector3 start, Vector3 stop)
        {
            var meshes = new List <Mesh>();
            var radius = 2;

            var x = new Cylinder(device, commandList, primitiveTopology, start, new Vector3(stop.X, start.Y, start.Z), radius, radius, 10, 10, Color.Red, ref index);

            meshes.Add(x);
            x = new Cylinder(device, commandList, primitiveTopology, new Vector3(stop.X, start.Y, start.Z), stop, radius, radius, 10, 10, Color.Red, ref index);
            meshes.Add(x);
            x = new Cylinder(device, commandList, primitiveTopology, stop, new Vector3(start.X, stop.Y, stop.Z), radius, radius, 10, 10, Color.Red, ref index);
            meshes.Add(x);
            x = new Cylinder(device, commandList, primitiveTopology, new Vector3(start.X, stop.Y, stop.Z), start, radius, radius, 10, 10, Color.Red, ref index);
            meshes.Add(x);

            return(meshes);
        }
Пример #4
0
        /// <summary>
        /// Gets the maximum multisample count for a particular <see cref="PixelFormat" />.
        /// </summary>
        /// <param name="device">The device.</param>
        /// <param name="pixelFormat">The pixelFormat.</param>
        /// <returns>The maximum multisample count for this pixel pixelFormat</returns>
        private static MultisampleCount GetMaximumMultisampleCount(SharpDX.Direct3D12.Device device, SharpDX.DXGI.Format pixelFormat)
        {
            SharpDX.Direct3D12.FeatureDataMultisampleQualityLevels qualityLevels;
            qualityLevels.Format            = pixelFormat;
            qualityLevels.Flags             = MultisampleQualityLevelFlags.None;
            qualityLevels.QualityLevelCount = 0;

            int maxCount = 1;

            for (int i = 8; i >= 1; i /= 2)
            {
                qualityLevels.SampleCount = i;
                if (device.CheckFeatureSupport(SharpDX.Direct3D12.Feature.MultisampleQualityLevels, ref qualityLevels))
                {
                    maxCount = i;
                    break;
                }
            }
            return((MultisampleCount)maxCount);
        }
Пример #5
0
        /// <summary>
        /// Creates the rendering pipeline.
        /// </summary>
        void LoadPipeline()
        {
            // create swap chain descriptor
            var swapChainDescription1 = new SwapChainDescription1()
            {
                AlphaMode         = AlphaMode.Unspecified,
                BufferCount       = SwapBufferCount,
                Usage             = Usage.RenderTargetOutput,
                SwapEffect        = SwapEffect.FlipSequential,
                SampleDescription = new SampleDescription(1, 0),
                Format            = Format.R8G8B8A8_UNorm,
                Width             = width,
                Height            = height
            };

            // enable debug layer
            using (var debugInterface = DebugInterface.Get())
                debugInterface.EnableDebugLayer();

            // create device
            using (var factory = new Factory4())
            {
#if USE_WARP
                using (var warpAdapter = factory.GetWarpAdapter())
                {
                    device = Collect(new Device(warpAdapter, FeatureLevel.Level_12_0));
                }
#else
                using (var adapter = factory.Adapters[1])
                {
                    device = Collect(new Device(adapter, FeatureLevel.Level_11_0));
                }
#endif
                commandQueue = Collect(device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct)));

                CreateSwapChain(ref swapChainDescription1, factory);
            }

            // create command queue and allocator objects
            commandListAllocator = Collect(device.CreateCommandAllocator(CommandListType.Direct));
        }
        // Below are helper factory methods in order to make use generic type inference.
        // Note that constructors do not support such inference.

        public static MeshGeometry New <TVertex, TIndex>(
            Device device,
            GraphicsCommandList commandList,
            IEnumerable <TVertex> vertices,
            IEnumerable <TIndex> indices,
            string name = "Default")
            where TVertex : struct
            where TIndex : struct
        {
            TVertex[] vertexArray = vertices.ToArray();
            TIndex[]  indexArray  = indices.ToArray();

            int      vertexBufferByteSize = Utilities.SizeOf(vertexArray);
            Resource vertexBufferUploader;
            Resource vertexBuffer = D3DUtil.CreateDefaultBuffer(device, commandList, vertexArray, vertexBufferByteSize, out vertexBufferUploader);

            int      indexBufferByteSize = Utilities.SizeOf(indexArray);
            Resource indexBufferUploader;
            Resource indexBuffer = D3DUtil.CreateDefaultBuffer(device, commandList, indexArray, indexBufferByteSize, out indexBufferUploader);

            return(new MeshGeometry
            {
                Name = name,
                VertexByteStride = Utilities.SizeOf <TVertex>(),
                VertexBufferByteSize = vertexBufferByteSize,
                VertexBufferGPU = vertexBuffer,
                VertexBufferCPU = vertexArray,
                IndexCount = indexArray.Length,
                IndexFormat = GetIndexFormat <TIndex>(),
                IndexBufferByteSize = indexBufferByteSize,
                IndexBufferGPU = indexBuffer,
                IndexBufferCPU = indexArray,
                _toDispose =
                {
                    vertexBuffer, vertexBufferUploader,
                    indexBuffer,  indexBufferUploader
                }
            });
        }
Пример #7
0
        protected void Initialize(Device device, ref int index, IEnumerable <TVertex> vertices = null,
                                  IEnumerable <TIndex> indices = null)
        {
            this.meshIndex = index;
            index++;

            var vertexArray = vertices?.ToArray();
            var indexArray  = indices?.ToArray();

            var      vertexBufferByteSize = Utilities.SizeOf(vertexArray);
            Resource vertexBufferUploader;
            var      vertexBuffer = BufferHelper.CreateDefaultBuffer(device, CommandList, vertexArray, vertexBufferByteSize,
                                                                     out vertexBufferUploader);

            var      indexBufferByteSize = Utilities.SizeOf(indexArray);
            Resource indexBufferUploader;
            var      indexBuffer = BufferHelper.CreateDefaultBuffer(device, CommandList, indexArray, indexBufferByteSize,
                                                                    out indexBufferUploader);

            this.VertexByteStride     = Utilities.SizeOf <TVertex>();
            this.VertexBufferByteSize = vertexBufferByteSize;
            this.VertexBufferGpu      = vertexBuffer;

            if (indexArray != null)
            {
                this.IndexCount          = indexArray.Length;
                this.IndexFormat         = GetIndexFormat();
                this.IndexBufferByteSize = indexBufferByteSize;
                this.IndexBufferGpu      = indexBuffer;
            }

            this.toDispose = new List <IDisposable>
            {
                vertexBuffer,
                vertexBufferUploader,
                indexBuffer,
                indexBufferUploader
            };
        }
        public static Resource CreateDefaultBuffer <T>(
            Device device,
            GraphicsCommandList cmdList,
            T[] initData,
            long byteSize,
            out Resource uploadBuffer) where T : struct
        {
            // Create the actual default buffer resource.
            Resource defaultBuffer = device.CreateCommittedResource(
                new HeapProperties(HeapType.Default),
                HeapFlags.None,
                ResourceDescription.Buffer(byteSize),
                ResourceStates.Common);

            // In order to copy CPU memory data into our default buffer, we need to create
            // an intermediate upload heap.
            uploadBuffer = device.CreateCommittedResource(
                new HeapProperties(HeapType.Upload),
                HeapFlags.None,
                ResourceDescription.Buffer(byteSize),
                ResourceStates.GenericRead);

            // Copy the data to the upload buffer.
            IntPtr ptr = uploadBuffer.Map(0);

            Utilities.Write(ptr, initData, 0, initData.Length);
            uploadBuffer.Unmap(0);

            // Schedule to copy the data to the default buffer resource.
            cmdList.ResourceBarrierTransition(defaultBuffer, ResourceStates.Common, ResourceStates.CopyDestination);
            cmdList.CopyResource(defaultBuffer, uploadBuffer);
            cmdList.ResourceBarrierTransition(defaultBuffer, ResourceStates.CopyDestination, ResourceStates.GenericRead);

            // Note: uploadBuffer has to be kept alive after the above function calls because
            // the command list has not been executed yet that performs the actual copy.
            // The caller can Release the uploadBuffer after it knows the copy has been executed.

            return(defaultBuffer);
        }
Пример #9
0
        protected virtual void Dispose(bool disposing)
        {
            // Implements the basic dispose pattern.
            // Ref: https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
            if (disposing)
            {
                FlushCommandQueue();

                RtvHeap?.Dispose();
                DsvHeap?.Dispose();
                SwapChain?.Dispose();
                foreach (Resource buffer in _swapChainBuffers)
                {
                    buffer?.Dispose();
                }
                DepthStencilBuffer?.Dispose();
                CommandList?.Dispose();
                DirectCmdListAlloc?.Dispose();
                CommandQueue?.Dispose();
                Fence?.Dispose();
                Device?.Dispose();
            }
        }
Пример #10
0
        /// <summary>
        /// Create texture from bmp
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="filename">Filename</param>
        /// <returns></returns>
        public static Resource CreateTextureFromBitmap(Device device, string filename)
        {
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(filename);

            int width  = bitmap.Width;
            int height = bitmap.Height;

            // Describe and create a Texture2D.
            ResourceDescription textureDesc = new ResourceDescription()
            {
                MipLevels         = 1,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                Flags             = ResourceFlags.None,
                DepthOrArraySize  = 1,
                SampleDescription = new SampleDescription(1, 0),
                Dimension         = ResourceDimension.Texture2D,
            };

            var buffer = device.CreateCommittedResource(new HeapProperties(HeapType.Upload), HeapFlags.None, textureDesc, ResourceStates.GenericRead);


            System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            buffer.WriteToSubresource(0, new ResourceRegion()
            {
                Back   = 1,
                Bottom = height,
                Right  = width
            }, data.Scan0, 4 * width, 4 * width * height);
            int bufferSize = data.Height * data.Stride;

            bitmap.UnlockBits(data);

            return(buffer);
        }
Пример #11
0
 internal CommandQueue(ref Descriptor desc, Device device, string label)
     : base(ref desc, device, label)
 {
 }
Пример #12
0
        protected virtual void OnResize()
        {
            Debug.Assert(Device != null);
            Debug.Assert(SwapChain != null);
            Debug.Assert(DirectCmdListAlloc != null);

            // Flush before changing any resources.
            FlushCommandQueue();

            CommandList.Reset(DirectCmdListAlloc, null);

            // Release the previous resources we will be recreating.
            foreach (Resource buffer in _swapChainBuffers)
            {
                buffer?.Dispose();
            }
            DepthStencilBuffer?.Dispose();

            // Resize the swap chain.
            SwapChain.ResizeBuffers(
                SwapChainBufferCount,
                ClientWidth, ClientHeight,
                BackBufferFormat,
                SwapChainFlags.AllowModeSwitch);

            CpuDescriptorHandle rtvHeapHandle = RtvHeap.CPUDescriptorHandleForHeapStart;

            for (int i = 0; i < SwapChainBufferCount; i++)
            {
                Resource backBuffer = SwapChain.GetBackBuffer <Resource>(i);
                _swapChainBuffers[i] = backBuffer;
                Device.CreateRenderTargetView(backBuffer, null, rtvHeapHandle);
                rtvHeapHandle += RtvDescriptorSize;
            }

            // Create the depth/stencil buffer and view.
            var depthStencilDesc = new ResourceDescription
            {
                Dimension         = ResourceDimension.Texture2D,
                Alignment         = 0,
                Width             = ClientWidth,
                Height            = ClientHeight,
                DepthOrArraySize  = 1,
                MipLevels         = 1,
                Format            = Format.R24G8_Typeless,
                SampleDescription = new SampleDescription
                {
                    Count   = MsaaCount,
                    Quality = MsaaQuality
                },
                Layout = TextureLayout.Unknown,
                Flags  = ResourceFlags.AllowDepthStencil
            };
            var optClear = new ClearValue
            {
                Format       = DepthStencilFormat,
                DepthStencil = new DepthStencilValue
                {
                    Depth   = 1.0f,
                    Stencil = 0
                }
            };

            DepthStencilBuffer = Device.CreateCommittedResource(
                new HeapProperties(HeapType.Default),
                HeapFlags.None,
                depthStencilDesc,
                ResourceStates.Common,
                optClear);

            var depthStencilViewDesc = new DepthStencilViewDescription
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Format    = DepthStencilFormat
            };
            // Create descriptor to mip level 0 of entire resource using a depth stencil format.
            CpuDescriptorHandle dsvHeapHandle = DsvHeap.CPUDescriptorHandleForHeapStart;

            Device.CreateDepthStencilView(DepthStencilBuffer, depthStencilViewDesc, dsvHeapHandle);

            // Transition the resource from its initial state to be used as a depth buffer.
            CommandList.ResourceBarrierTransition(DepthStencilBuffer, ResourceStates.Common, ResourceStates.DepthWrite);

            // Execute the resize commands.
            CommandList.Close();
            CommandQueue.ExecuteCommandList(CommandList);

            // Wait until resize is complete.
            FlushCommandQueue();

            Viewport         = new ViewportF(0, 0, ClientWidth, ClientHeight, 0.0f, 1.0f);
            ScissorRectangle = new RectangleF(0, 0, ClientWidth, ClientHeight);
        }
Пример #13
0
        /// <summary>
        ///     Initializes the specified device.
        /// </summary>
        /// <param name="graphicsProfiles">The graphics profiles.</param>
        /// <param name="deviceCreationFlags">The device creation flags.</param>
        /// <param name="windowHandle">The window handle.</param>
        private void InitializePlatformDevice(GraphicsProfile[] graphicsProfiles, DeviceCreationFlags deviceCreationFlags, object windowHandle)
        {
            if (nativeDevice != null)
            {
                // Destroy previous device
                ReleaseDevice();
            }

            rendererName = Adapter.NativeAdapter.Description.Description;

            // Profiling is supported through pix markers
            IsProfilingSupported = true;

            if ((deviceCreationFlags & DeviceCreationFlags.Debug) != 0)
            {
                SharpDX.Direct3D12.DebugInterface.Get().EnableDebugLayer();
            }

            // Default fallback
            if (graphicsProfiles.Length == 0)
            {
                graphicsProfiles = new[] { GraphicsProfile.Level_11_0 }
            }
            ;

            // Create Device D3D12 with feature Level based on profile
            for (int index = 0; index < graphicsProfiles.Length; index++)
            {
                var graphicsProfile = graphicsProfiles[index];
                try
                {
                    // D3D12 supports only feature level 11+
                    var level = graphicsProfile.ToFeatureLevel();
                    if (level < SharpDX.Direct3D.FeatureLevel.Level_11_0)
                    {
                        level = SharpDX.Direct3D.FeatureLevel.Level_11_0;
                    }

                    nativeDevice = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, level);

                    RequestedProfile    = graphicsProfile;
                    CurrentFeatureLevel = level;
                    break;
                }
                catch (Exception)
                {
                    if (index == graphicsProfiles.Length - 1)
                    {
                        throw;
                    }
                }
            }

            // Describe and create the command queue.
            var queueDesc = new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct);

            NativeCommandQueue = nativeDevice.CreateCommandQueue(queueDesc);
            //queueDesc.Type = CommandListType.Copy;
            NativeCopyCommandQueue = nativeDevice.CreateCommandQueue(queueDesc);

            SrvHandleIncrementSize     = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            SamplerHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler);

            // Prepare pools
            CommandAllocators = new CommandAllocatorPool(this);
            SrvHeaps          = new HeapPool(this, SrvHeapSize, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            SamplerHeaps      = new HeapPool(this, SamplerHeapSize, DescriptorHeapType.Sampler);

            // Prepare descriptor allocators
            SamplerAllocator            = new DescriptorAllocator(this, DescriptorHeapType.Sampler);
            ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            DepthStencilViewAllocator   = new DescriptorAllocator(this, DescriptorHeapType.DepthStencilView);
            RenderTargetViewAllocator   = new DescriptorAllocator(this, DescriptorHeapType.RenderTargetView);

            // Prepare copy command list (start it closed, so that every new use start with a Reset)
            NativeCopyCommandAllocator = NativeDevice.CreateCommandAllocator(CommandListType.Direct);
            NativeCopyCommandList      = NativeDevice.CreateCommandList(CommandListType.Direct, NativeCopyCommandAllocator, null);
            NativeCopyCommandList.Close();

            // Fence for next frame and resource cleaning
            nativeFence     = NativeDevice.CreateFence(0, FenceFlags.None);
            nativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None);
        }
Пример #14
0
        public GraphicsHost(RenderForm window, bool hidden = false)
        {
            if (window == null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            this.window = window;
            if (!hidden)
            {
                this.window.Visible = true;
            }

#if DEBUG
            Configuration.EnableObjectTracking      = true;
            Configuration.ThrowOnShaderCompileError = false;
#endif

            var swapChainDescription = new SwapChainDescription()
            {
                BufferCount       = SwapBufferCount,
                ModeDescription   = new ModeDescription(Format.R8G8B8A8_UNorm),
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = window.Handle,
                SwapEffect        = SwapEffect.FlipDiscard,
                SampleDescription = new SampleDescription(1, 0),
                IsWindowed        = true
            };

#if DEBUG
            // Enable the D3D12 debug layer.
            // DebugInterface.Get().EnableDebugLayer();
#endif

            try
            {
                // null == DriverType.Hardware
                device       = new Device(null, FeatureLevel.Level_11_0);
                commandQueue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct));
            }
            catch (SharpDXException)
            {
                using (var factory = new Factory4())
                {
                    device       = new Device(factory.GetWarpAdapter(), FeatureLevel.Level_11_0);
                    commandQueue = device.CreateCommandQueue(new CommandQueueDescription(CommandListType.Direct));
                }
            }

            using (var factory = new Factory1())
                swapChain = new SwapChain(factory, commandQueue, swapChainDescription);

            commandListAllocator = device.CreateCommandAllocator(CommandListType.Direct);

            descriptorHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
            {
                Type            = DescriptorHeapType.RenderTargetView,
                DescriptorCount = 1
            });

            commandList = device.CreateCommandList(CommandListType.Direct, commandListAllocator, null);

            renderTarget = swapChain.GetBackBuffer <Resource>(0);
            device.CreateRenderTargetView(renderTarget, null, descriptorHeap.CPUDescriptorHandleForHeapStart);

            viewport         = new ViewportF(0, 0, this.Width, this.Height);
            scissorRectangle = new Rectangle(0, 0, this.Width, this.Height);

            fence        = device.CreateFence(0, FenceFlags.None);
            currentFence = 1;

            commandList.Close();

            eventHandle = new AutoResetEvent(false);

            WaitForPrevFrame();
        }
Пример #15
0
        static Resource CreateTextureFromDDS(Device d3dDevice, DDS_HEADER header, DDS_HEADER_DXT10?header10, byte[] bitData, int offset, int maxsize, out bool isCubeMap)
        {
            int width  = header.width;
            int height = header.height;
            int depth  = header.depth;

            ResourceDimension resDim = ResourceDimension.Unknown;
            int    arraySize         = 1;
            Format format            = Format.Unknown;

            isCubeMap = false;

            int mipCount = header.mipMapCount;

            if (0 == mipCount)
            {
                mipCount = 1;
            }

            if (((header.ddspf.flags & DDS_FOURCC) > 0) && (MAKEFOURCC('D', 'X', '1', '0') == header.ddspf.fourCC))
            {
                DDS_HEADER_DXT10 d3d10ext = header10.Value;

                arraySize = d3d10ext.arraySize;
                if (arraySize == 0)
                {
                    throw new Exception();
                }

                if (BitsPerPixel(d3d10ext.dxgiFormat) == 0)
                {
                    throw new Exception();
                }

                format = d3d10ext.dxgiFormat;

                switch ((ResourceDimension)d3d10ext.resourceDimension)
                {
                case ResourceDimension.Texture1D:
                    // D3DX writes 1D textures with a fixed Height of 1
                    if ((header.flags & DDS_HEIGHT) > 0 && height != 1)
                    {
                        throw new Exception();
                    }
                    height = depth = 1;
                    break;

                case ResourceDimension.Texture2D:
                    //D3D11_RESOURCE_MISC_TEXTURECUBE
                    if ((d3d10ext.miscFlag & 0x4) > 0)
                    {
                        arraySize *= 6;
                        isCubeMap  = true;
                    }
                    depth = 1;
                    break;

                case ResourceDimension.Texture3D:
                    if (!((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0))
                    {
                        throw new Exception();
                    }

                    if (arraySize > 1)
                    {
                        throw new Exception();
                    }
                    break;

                default:
                    throw new Exception();
                }

                resDim = (ResourceDimension)d3d10ext.resourceDimension;
            }
            else
            {
                format = GetDXGIFormat(header.ddspf);

                if (format == Format.Unknown)
                {
                    throw new Exception();
                }

                if ((header.flags & DDS_HEADER_FLAGS_VOLUME) > 0)
                {
                    resDim = ResourceDimension.Texture3D;
                }
                else
                {
                    if ((header.caps2 & DDS_CUBEMAP) > 0)
                    {
                        // We require all six faces to be defined
                        if ((header.caps2 & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
                        {
                            throw new Exception();
                        }

                        arraySize = 6;
                        isCubeMap = true;
                    }

                    depth  = 1;
                    resDim = ResourceDimension.Texture2D;
                }
            }
            var resource = d3dDevice.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None,
                                                             new ResourceDescription()
            {
                //Alignment = -1,
                Dimension         = resDim,
                DepthOrArraySize  = (short)arraySize,
                Flags             = ResourceFlags.None,
                Format            = format,
                Height            = height,
                Layout            = TextureLayout.Unknown,
                MipLevels         = (short)mipCount,
                SampleDescription = new SampleDescription(1, 0),
                Width             = width,
            },
                                                             ResourceStates.GenericRead);

            FillInitData(resource, width, height, depth, mipCount, arraySize, format, 0, 0, bitData, offset);

            return(resource);
        }
Пример #16
0
        public Cylinder(Device device,
                        GraphicsCommandList commandList,
                        PrimitiveTopology primitiveTopology,
                        Vector3 startPoint,
                        Vector3 endPoint,
                        float bottomRadius,
                        float topRadius,
                        int sliceCount,
                        int stackCount,
                        Color color,
                        ref int index,
                        string name = "Default")
        {
            base.CommandList       = commandList;
            base.PrimitiveTopology = primitiveTopology;
            base.Name = name;

            var vertices = new List <Vertex>();
            var indices  = new List <short>();

            var height      = MathHelper.DistanceBetweenVector(startPoint, endPoint);
            var stackHeight = height / stackCount;
            var radiusStep  = (topRadius - bottomRadius) / stackCount;
            var ringCount   = stackCount + 1;

            for (int i = 0; i < ringCount; i++)
            {
                var y      = -0.5f * height + i * stackHeight;
                var r      = bottomRadius + i * radiusStep;
                var dTheta = 2.0f * MathUtil.Pi / sliceCount;

                for (var j = 0; j <= sliceCount; j++)
                {
                    var c = MathHelper.Cosf(j * dTheta);
                    var s = MathHelper.Sinf(j * dTheta);

                    var pos     = new Vector3(r * c, y, r * s);
                    var uv      = new Vector2((float)j / sliceCount, 1f - (float)i / stackCount);
                    var tangent = new Vector3(-s, 0.0f, c);

                    var dr        = bottomRadius - topRadius;
                    var bitangent = new Vector3(dr * c, -height, dr * s);

                    var normal = Vector3.Cross(tangent, bitangent);
                    normal.Normalize();
                    vertices.Add(new Vertex {
                        Position = pos, Color = color.ToColor4()
                    });
                }
            }

            var ringVertexCount = sliceCount + 1;

            for (var i = 0; i < stackCount; i++)
            {
                for (var j = 0; j < sliceCount; j++)
                {
                    indices.Add((short)(i * ringVertexCount + j));
                    indices.Add((short)((i + 1) * ringVertexCount + j));
                    indices.Add((short)((i + 1) * ringVertexCount + j + 1));

                    indices.Add((short)(i * ringVertexCount + j));
                    indices.Add((short)((i + 1) * ringVertexCount + j + 1));
                    indices.Add((short)(i * ringVertexCount + j + 1));
                }
            }

            var baseIndex = vertices.Count;
            var dtheta    = 2.0f * MathUtil.Pi / sliceCount;

            for (var i = 0; i <= sliceCount; i++)
            {
                var x = topRadius * MathHelper.Cosf(i * dtheta);
                var z = topRadius * MathHelper.Sinf(i * dtheta);
                var u = x / height + 0.5f;
                var v = z / height + 0.5f;

                vertices.Add(new Vertex {
                    Position = new Vector3(x, 0.5f * height, z), Color = color.ToColor4()
                });
            }

            vertices.Add(new Vertex {
                Position = new Vector3(0, 0.5f * height, 0), Color = color.ToColor4()
            });

            var centerIndex = vertices.Count - 1;

            for (int i = 0; i < sliceCount; i++)
            {
                indices.Add((short)centerIndex);
                indices.Add((short)(baseIndex + i + 1));
                indices.Add((short)(baseIndex + i));
            }

            baseIndex = vertices.Count;
            for (var i = 0; i <= sliceCount; i++)
            {
                var x = bottomRadius * MathHelper.Cosf(i * dtheta);
                var z = bottomRadius * MathHelper.Sinf(i * dtheta);
                var u = x / height + 0.5f;
                var v = z / height + 0.5f;

                vertices.Add(new Vertex {
                    Position = new Vector3(x, -0.5f * height, z), Color = color.ToColor4()
                });
            }


            vertices.Add(new Vertex {
                Position = new Vector3(0, -0.5f * height, 0), Color = color.ToColor4()
            });

            centerIndex = vertices.Count - 1;

            for (var i = 0; i < sliceCount; i++)
            {
                indices.Add((short)centerIndex);
                indices.Add((short)(baseIndex + i + 1));
                indices.Add((short)(baseIndex + i));
            }

            var vz = new Vector3(0, 1, 0);

            var p = startPoint - endPoint;

            p.Normalize();
            var t = Vector3.Cross(vz, p);

            var angle  = 180 / Math.PI * Math.Acos(Vector3.Dot(vz, p) / p.Length());
            var middle = MathHelper.Middle2Vector(startPoint, endPoint);

            World = Matrix.RotationAxis(new Vector3(t.X, t.Y, t.Z), MathHelper.DegreeToRadian((float)angle)) *
                    Matrix.Translation(middle);

            this.Initialize(device, ref index, vertices, indices);
        }
Пример #17
0
        public static RootSignature New(Device device)
        {
            var rootSigDesc = BuildRootSignatureDescription();

            return(device.CreateRootSignature(rootSigDesc.Serialize()));
        }
Пример #18
0
        private List<IntPtr> GetProcAddress()
        {
            var address = new List<IntPtr>();

            _device12 = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
            using (var renderForm = new Form())
            {
                using (var factory = new SharpDX.DXGI.Factory4())
                {
                    _commandQueue
                        = _device12.CreateCommandQueue(new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct));

                    _commandAllocator
                        = _device12.CreateCommandAllocator(CommandListType.Direct);

                    _commandList
                        = _device12.CreateCommandList(CommandListType.Direct, _commandAllocator, null);

                    var swapChainDesc = new SharpDX.DXGI.SwapChainDescription()
                    {
                        BufferCount = 2,
                        ModeDescription = new SharpDX.DXGI.ModeDescription(100, 100, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                        Usage = SharpDX.DXGI.Usage.RenderTargetOutput,
                        SwapEffect = SharpDX.DXGI.SwapEffect.FlipDiscard,
                        OutputHandle = renderForm.Handle,
                        Flags = SwapChainFlags.AllowModeSwitch,
                        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                        IsWindowed = true
                    };

                    var tempSwapChain = new SharpDX.DXGI.SwapChain(factory, _commandQueue, swapChainDesc);
                    _swapChain = tempSwapChain.QueryInterface<SharpDX.DXGI.SwapChain3>();
                    tempSwapChain.Dispose();
                }

                if (_device12 != null && _swapChain != null)
                {
                    address.AddRange(GetVTblAddresses(_device12.NativePointer, 44));
                    address.AddRange(GetVTblAddresses(_commandQueue.NativePointer, 19));
                    address.AddRange(GetVTblAddresses(_commandAllocator.NativePointer, 9));
                    address.AddRange(GetVTblAddresses(_commandList.NativePointer, 60));
                    address.AddRange(GetVTblAddresses(_swapChain.NativePointer, 18));

                    _device12.Dispose();
                    _device12 = null;

                    _commandQueue.Dispose();
                    _commandQueue = null;

                    _commandAllocator.Dispose();
                    _commandAllocator = null;

                    _commandList.Dispose();
                    _commandList = null;

                    _swapChain.Dispose();
                    _swapChain = null;
                }
            }

            return address;
        }
Пример #19
0
        protected void InitDirect3D()
        {
#if DEBUG
            // The Direct3D 12 debug layer may or may not be installed. It's installation can be
            // managed through settings page "Manage optional features" with a feature called
            // "Graphics Tools".
            // There may be a better solution to check for it instead of try/catch. If you happen
            // to know, please consider opening an issue or PR in the repo.
            try
            {
                DebugInterface.Get().EnableDebugLayer();
            }
            catch (SharpDXException ex) when(ex.Descriptor.NativeApiCode == "DXGI_ERROR_SDK_COMPONENT_MISSING")
            {
                Debug.WriteLine("Failed to enable debug layer. Please ensure \"Graphics Tools\" feature is enabled in Windows \"Manage optional feature\" settings page");
            }
#endif

            _factory = new Factory4();

            try
            {
                // Try to create hardware device.
                // Pass NULL to use the default adapter which is the first adapter that is enumerated by Factory.Adapters.
                // Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/dn770336(v=vs.85).aspx
                Device = new Device(null, FeatureLevel.Level_11_0);
            }
            catch (SharpDXException)
            {
                // Fallback to WARP device.
                Adapter warpAdapter = _factory.GetWarpAdapter();
                Device = new Device(warpAdapter, FeatureLevel.Level_11_0);
            }

            Fence       = Device.CreateFence(0, FenceFlags.None);
            _fenceEvent = new AutoResetEvent(false);

            RtvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            DsvDescriptorSize       = Device.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView);
            CbvSrvUavDescriptorSize = Device.GetDescriptorHandleIncrementSize(
                DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            // Check 4X MSAA quality support for our back buffer format.
            // All Direct3D 11 capable devices support 4X MSAA for all render
            // target formats, so we only need to check quality support.

            FeatureDataMultisampleQualityLevels msQualityLevels;
            msQualityLevels.Format            = BackBufferFormat;
            msQualityLevels.SampleCount       = 4;
            msQualityLevels.Flags             = MultisampleQualityLevelFlags.None;
            msQualityLevels.QualityLevelCount = 0;
            Debug.Assert(Device.CheckFeatureSupport(Feature.MultisampleQualityLevels, ref msQualityLevels));
            _m4xMsaaQuality = msQualityLevels.QualityLevelCount;

#if DEBUG
            LogAdapters();
#endif

            CreateCommandObjects();
            CreateSwapChain();
            CreateRtvAndDsvDescriptorHeaps();
        }
Пример #20
0
        /// <summary>
        /// Load texture from DDS file
        /// </summary>
        /// <param name="device">Device</param>
        /// <param name="filename">Filename</param>
        /// <returns></returns>
        public static Resource CreateTextureFromDDS(Device device, string filename)
        {
            bool isCube;

            return(CreateTextureFromDDS(device, System.IO.File.ReadAllBytes(filename), out isCube));
        }
Пример #21
0
        /// <summary>
        ///     Initializes the specified device.
        /// </summary>
        /// <param name="graphicsProfiles">The graphics profiles.</param>
        /// <param name="deviceCreationFlags">The device creation flags.</param>
        /// <param name="windowHandle">The window handle.</param>
        private void InitializePlatformDevice(GraphicsProfile[] graphicsProfiles, DeviceCreationFlags deviceCreationFlags, object windowHandle)
        {
            if (nativeDevice != null)
            {
                // Destroy previous device
                ReleaseDevice();
            }

            rendererName = Adapter.NativeAdapter.Description.Description;

            // Profiling is supported through pix markers
            IsProfilingSupported = true;

            // Command lists are thread-safe and execute deferred
            IsDeferred = true;

            bool isDebug = (deviceCreationFlags & DeviceCreationFlags.Debug) != 0;

            if (isDebug)
            {
                SharpDX.Direct3D12.DebugInterface.Get().EnableDebugLayer();
            }

            // Create Device D3D12 with feature Level based on profile
            for (int index = 0; index < graphicsProfiles.Length; index++)
            {
                var graphicsProfile = graphicsProfiles[index];
                try
                {
                    // D3D12 supports only feature level 11+
                    var level = graphicsProfile.ToFeatureLevel();
                    if (level < SharpDX.Direct3D.FeatureLevel.Level_11_0)
                    {
                        level = SharpDX.Direct3D.FeatureLevel.Level_11_0;
                    }

                    nativeDevice = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, level);

                    RequestedProfile    = graphicsProfile;
                    CurrentFeatureLevel = level;
                    break;
                }
                catch (Exception)
                {
                    if (index == graphicsProfiles.Length - 1)
                    {
                        throw;
                    }
                }
            }

            // Describe and create the command queue.
            var queueDesc = new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct);

            NativeCommandQueue = nativeDevice.CreateCommandQueue(queueDesc);
            //queueDesc.Type = CommandListType.Copy;
            NativeCopyCommandQueue = nativeDevice.CreateCommandQueue(queueDesc);
            TimestampFrequency     = NativeCommandQueue.TimestampFrequency;

            SrvHandleIncrementSize     = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            SamplerHandleIncrementSize = NativeDevice.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler);

            if (isDebug)
            {
                var debugDevice = nativeDevice.QueryInterfaceOrNull <DebugDevice>();
                if (debugDevice != null)
                {
                    var infoQueue = debugDevice.QueryInterfaceOrNull <InfoQueue>();
                    if (infoQueue != null)
                    {
                        MessageId[] disabledMessages =
                        {
                            // This happens when render target or depth stencil clear value is diffrent
                            // than provided during resource allocation.
                            MessageId.CleardepthstencilviewMismatchingclearvalue,
                            MessageId.ClearrendertargetviewMismatchingclearvalue,

                            // This occurs when there are uninitialized descriptors in a descriptor table,
                            // even when a shader does not access the missing descriptors.
                            MessageId.InvalidDescriptorHandle,

                            // These happen when capturing with VS diagnostics
                            MessageId.MapInvalidNullRange,
                            MessageId.UnmapInvalidNullRange,
                        };

                        // Disable irrelevant debug layer warnings
                        InfoQueueFilter filter = new InfoQueueFilter
                        {
                            DenyList = new InfoQueueFilterDescription
                            {
                                Ids = disabledMessages
                            }
                        };
                        infoQueue.AddStorageFilterEntries(filter);

                        //infoQueue.SetBreakOnSeverity(MessageSeverity.Error, true);
                        //infoQueue.SetBreakOnSeverity(MessageSeverity.Warning, true);

                        infoQueue.Dispose();
                    }
                    debugDevice.Dispose();
                }
            }

            // Prepare pools
            CommandAllocators = new CommandAllocatorPool(this);
            SrvHeaps          = new HeapPool(this, SrvHeapSize, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            SamplerHeaps      = new HeapPool(this, SamplerHeapSize, DescriptorHeapType.Sampler);

            // Prepare descriptor allocators
            SamplerAllocator            = new DescriptorAllocator(this, DescriptorHeapType.Sampler);
            ShaderResourceViewAllocator = new DescriptorAllocator(this, DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);
            DepthStencilViewAllocator   = new DescriptorAllocator(this, DescriptorHeapType.DepthStencilView);
            RenderTargetViewAllocator   = new DescriptorAllocator(this, DescriptorHeapType.RenderTargetView);

            // Prepare copy command list (start it closed, so that every new use start with a Reset)
            NativeCopyCommandAllocator = NativeDevice.CreateCommandAllocator(CommandListType.Direct);
            NativeCopyCommandList      = NativeDevice.CreateCommandList(CommandListType.Direct, NativeCopyCommandAllocator, null);
            NativeCopyCommandList.Close();

            // Fence for next frame and resource cleaning
            nativeFence     = NativeDevice.CreateFence(0, FenceFlags.None);
            nativeCopyFence = NativeDevice.CreateFence(0, FenceFlags.None);
        }
Пример #22
0
        private void LoadPipeline(RenderForm form)
        {
            SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericMouse, DeviceFlags.None);
            SharpDX.RawInput.Device.MouseInput += MouseEvent;

            SharpDX.RawInput.Device.RegisterDevice(UsagePage.Generic, UsageId.GenericKeyboard, DeviceFlags.None);
            SharpDX.RawInput.Device.KeyboardInput += KeyboardEvent;

            label = new Label()
            {
                Text = "Text", Location = new System.Drawing.Point(16, 16)
            };
            form.Controls.Add(label);


            int width  = form.ClientSize.Width;
            int height = form.ClientSize.Height;

            viewport.Width    = width;
            viewport.Height   = height;
            viewport.MaxDepth = 1.0f;

            scissorRect.Right  = width;
            scissorRect.Bottom = height;

#if DEBUG
            // Enable the D3D12 debug layer.
            {
                DebugInterface.Get().EnableDebugLayer();
            }
#endif
            device = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
            using (var factory = new Factory4())
            {
                // Describe and create the command queue.
                var queueDesc = new CommandQueueDescription(CommandListType.Direct);
                commandQueue = device.CreateCommandQueue(queueDesc);


                // Describe and create the swap chain.
                var swapChainDesc = new SwapChainDescription()
                {
                    BufferCount     = FrameCount,
                    ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                    Usage           = Usage.RenderTargetOutput,
                    SwapEffect      = SwapEffect.FlipDiscard,
                    OutputHandle    = form.Handle,
                    //Flags = SwapChainFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                    IsWindowed        = true
                };

                var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
                swapChain = tempSwapChain.QueryInterface <SwapChain3>();
                tempSwapChain.Dispose();
                frameIndex = swapChain.CurrentBackBufferIndex;
            }

            // Create descriptor heaps.
            // Describe and create a render target view (RTV) descriptor heap.
            var rtvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = FrameCount,
                Flags           = DescriptorHeapFlags.None,
                Type            = DescriptorHeapType.RenderTargetView
            };

            renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);

            rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            // Create frame resources.
            var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            for (int n = 0; n < FrameCount; n++)
            {
                renderTargets[n] = swapChain.GetBackBuffer <Resource>(n);
                device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
                rtvHandle += rtvDescriptorSize;
            }

            commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
        }