public DescriptorHeapObjects(Device device, int renderTargetViewDescriptorCount, int depthStencilViewDescriptorCount) { var rtvHeapDesc = new DescriptorHeapDescription { DescriptorCount = renderTargetViewDescriptorCount, Type = DescriptorHeapType.RenderTargetView }; this.RenderTargetViewDescriptorHeap = device.CreateDescriptorHeap(rtvHeapDesc); var dsvHeapDesc = new DescriptorHeapDescription { DescriptorCount = depthStencilViewDescriptorCount, Type = DescriptorHeapType.DepthStencilView }; this.DepthStencilViewDescriptorHeap = device.CreateDescriptorHeap(dsvHeapDesc); var cbvHeapDesc = new DescriptorHeapDescription { DescriptorCount = 1, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, Flags = DescriptorHeapFlags.ShaderVisible, NodeMask = 0 }; this.ConstantBufferViewShaderResourceViewUnorderedAccessViewDescriptorHeap = device.CreateDescriptorHeap(cbvHeapDesc); this.DescriptorHeaps = new[] { ConstantBufferViewShaderResourceViewUnorderedAccessViewDescriptorHeap }; }
void CreateHeaps() { _sizeRTV = _dev.GetDescriptorHandleIncrementSize(SharpDX.Direct3D12.DescriptorHeapType.RenderTargetView); { SharpDX.Direct3D12.DescriptorHeapDescription desc; desc.Type = SharpDX.Direct3D12.DescriptorHeapType.RenderTargetView; desc.DescriptorCount = FrameCount; desc.Flags = SharpDX.Direct3D12.DescriptorHeapFlags.None; desc.NodeMask = 0; _heapRTV = _dev.CreateDescriptorHeap(desc); } { SharpDX.Direct3D12.DescriptorHeapDescription desc; desc.Type = SharpDX.Direct3D12.DescriptorHeapType.DepthStencilView; desc.DescriptorCount = 1; desc.Flags = SharpDX.Direct3D12.DescriptorHeapFlags.None; desc.NodeMask = 0; _heapDSV = _dev.CreateDescriptorHeap(desc); } }
private void CreateRtvAndDsvDescriptorHeaps() { var rtvHeapDesc = new DescriptorHeapDescription { DescriptorCount = RtvDescriptorCount, Type = DescriptorHeapType.RenderTargetView }; RtvHeap = Device.CreateDescriptorHeap(rtvHeapDesc); var dsvHeapDesc = new DescriptorHeapDescription { DescriptorCount = DsvDescriptorCount, Type = DescriptorHeapType.DepthStencilView }; DsvHeap = Device.CreateDescriptorHeap(dsvHeapDesc); }
private void LoadPipeline(RenderForm form) { 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 Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription 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 }; SwapChain 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. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); DescriptorHeapDescription srvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. CpuDescriptorHandle 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); }
/// <summary> /// Setup resources for rendering /// </summary> void LoadAssets() { // Create the main command list commandList = Collect(device.CreateCommandList(CommandListType.Direct, commandListAllocator, pipelineState)); // Create the descriptor heap for the render target view descriptorHeapRT = Collect(device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.RenderTargetView, DescriptorCount = 1 })); #if USE_DEPTH descriptorHeapDS = Collect(device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.DepthStencilView, DescriptorCount = 1 })); #endif #if USE_TEXTURE descriptorHeapCB = Collect(device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, DescriptorCount = 2, Flags = DescriptorHeapFlags.ShaderVisible, })); descriptorHeapS = Collect(device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.Sampler, DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, })); descriptorsHeaps[0] = descriptorHeapCB; descriptorsHeaps[1] = descriptorHeapS; #else descriptorHeapCB = Collect(device.CreateDescriptorHeap(new DescriptorHeapDescription() { Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, })); descriptorsHeaps[0] = descriptorHeapCB; #endif #if true // root signature in code var rsparams = new RootParameter[] { new RootParameter(ShaderVisibility.Vertex, new RootDescriptor(), RootParameterType.ConstantBufferView), new RootParameter(ShaderVisibility.Vertex, new DescriptorRange { RangeType = DescriptorRangeType.ConstantBufferView, BaseShaderRegister = 1, DescriptorCount = 1, }), #if USE_TEXTURE new RootParameter(ShaderVisibility.Pixel, new DescriptorRange { RangeType = DescriptorRangeType.ShaderResourceView, BaseShaderRegister = 0, DescriptorCount = 1, }), new RootParameter(ShaderVisibility.Pixel, new DescriptorRange { RangeType = DescriptorRangeType.Sampler, BaseShaderRegister = 0, DescriptorCount = 1, }), #endif }; var rs = new RootSignatureDescription(RootSignatureFlags.AllowInputAssemblerInputLayout, rsparams); rootSignature = Collect(device.CreateRootSignature(rs.Serialize())); #else var rootSignatureByteCode = Utilities.ReadStream(assembly.GetManifestResourceStream("Shaders.Cube" + shaderNameSuffix + ".rs")); using (var bufferRootSignature = DataBuffer.Create(rootSignatureByteCode)) rootSignature = Collect(device.CreateRootSignature(bufferRootSignature)); #endif byte[] vertexShaderByteCode = GetResourceBytes("Cube" + shaderNameSuffix + ".vso"); byte[] pixelShaderByteCode = GetResourceBytes("Cube" + shaderNameSuffix + ".pso"); var layout = new InputLayoutDescription(new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0), #if USE_INSTANCES new InputElement("OFFSET", 0, Format.R32G32B32_Float, 0, 1, InputClassification.PerInstanceData, 1), #endif }); #region pipeline state var psd = new GraphicsPipelineStateDescription { InputLayout = layout, VertexShader = vertexShaderByteCode, PixelShader = pixelShaderByteCode, RootSignature = rootSignature, DepthStencilState = DepthStencilStateDescription.Default(), DepthStencilFormat = Format.Unknown, BlendState = BlendStateDescription.Default(), RasterizerState = RasterizerStateDescription.Default(), SampleDescription = new SampleDescription(1, 0), RenderTargetCount = 1, PrimitiveTopologyType = PrimitiveTopologyType.Triangle, SampleMask = -1, StreamOutput = new StreamOutputDescription() }; psd.RenderTargetFormats[0] = Format.R8G8B8A8_UNorm; #if USE_DEPTH psd.DepthStencilFormat = Format.D32_Float; #else psd.DepthStencilState.IsDepthEnabled = false; #endif //psd.RasterizerState.CullMode = CullMode.None; pipelineState = Collect(device.CreateGraphicsPipelineState(psd)); #endregion pipeline state #region vertices var vertices = new[] { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Front -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // BACK -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // BACK 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Top -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Bottom -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Bottom 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Left -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Left -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Right 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, // Right 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, }; #endregion vertices #region vertex buffer // Instantiate Vertex buiffer from vertex data int sizeOfFloat = sizeof(float); int sizeInBytes = vertices.Length * sizeOfFloat; vertexBuffer = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription(ResourceDimension.Buffer, 0, sizeInBytes, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead)); vertexBufferView = new[] { new VertexBufferView { BufferLocation = vertexBuffer.GPUVirtualAddress, SizeInBytes = sizeInBytes, StrideInBytes = sizeOfFloat * 8, } }; var ptr = vertexBuffer.Map(0); Utilities.Write(ptr, vertices, 0, vertices.Length); vertexBuffer.Unmap(0); #endregion vertex buffer #region instances #if USE_INSTANCES int instanceSizeInBytes = sizeOfFloat * instances.Length; instancesBuffer = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription(ResourceDimension.Buffer, 0, instanceSizeInBytes, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead)); instancesBufferView = new[] { new VertexBufferView { BufferLocation = instancesBuffer.GPUVirtualAddress, SizeInBytes = instanceSizeInBytes, StrideInBytes = sizeOfFloat * 3, } }; ptr = instancesBuffer.Map(0); Utilities.Write(ptr, instances, 0, instances.Length); instancesBuffer.Unmap(0); #endif #endregion instances #region indices #if USE_INDICES var indexData = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 }; sizeInBytes = indexData.Length * sizeof(int); indexBuffer = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription(ResourceDimension.Buffer, 0, sizeInBytes, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead)); ptr = indexBuffer.Map(0); Utilities.Write(ptr, indexData, 0, indexData.Length); indexBuffer.Unmap(0); indexBufferView = new IndexBufferView { BufferLocation = indexBuffer.GPUVirtualAddress, SizeInBytes = sizeInBytes, Format = Format.R32_UInt }; #endif #endregion indices #region transform transWorld = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription(ResourceDimension.Buffer, 0, 16 * sizeOfMatrix, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead)); transWorldPtr = transWorld.Map(0); transViewProj = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription(ResourceDimension.Buffer, 0, sizeOfMatrix, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead)); device.CreateConstantBufferView(new ConstantBufferViewDescription { BufferLocation = transViewProj.GPUVirtualAddress, SizeInBytes = sizeOfMatrix, }, descriptorHeapCB.CPUDescriptorHandleForHeapStart); var view = Matrix.LookAtLH(new Vector3(5, 5, -5), Vector3.Zero, Vector3.UnitY); var proj = Matrix.PerspectiveFovLH(MathUtil.Pi / 4, (float)width / height, 0.1f, 100); var vpT = view * proj; vpT.Transpose(); ptr = transViewProj.Map(0); Utilities.Write(ptr, ref vpT); transViewProj.Unmap(0); #endregion transform #if USE_TEXTURE #region texture Resource buf; using (var tl = new TextureLoader("GeneticaMortarlessBlocks.jpg")) { int w = tl.Width, h = tl.Height; var descrs = new[] { new ResourceDescription(ResourceDimension.Texture2D, 0, w, h, 1, 1, Format.B8G8R8A8_UNorm, 1, 0, TextureLayout.Unknown, ResourceFlags.None), }; texture = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, descrs[0], ResourceStates.CopyDestination) ); var resAllocInfo = device.GetResourceAllocationInfo(1, 1, descrs); buf = device.CreateCommittedResource( new HeapProperties(HeapType.Upload), HeapFlags.None, new ResourceDescription( ResourceDimension.Buffer, 0, resAllocInfo.SizeInBytes, 1, 1, 1, Format.Unknown, 1, 0, TextureLayout.RowMajor, ResourceFlags.None), ResourceStates.GenericRead); var ptrBuf = buf.Map(0); int rowPitch = tl.CopyImageData(ptrBuf); buf.Unmap(0); var src = new TextureCopyLocation(buf, new PlacedSubResourceFootprint { Offset = 0, Footprint = new SubResourceFootprint { Format = Format.B8G8R8A8_UNorm_SRgb, Width = w, Height = h, Depth = 1, RowPitch = rowPitch } } ); var dst = new TextureCopyLocation(texture, 0); // record copy commandList.CopyTextureRegion(dst, 0, 0, 0, src, null); commandList.ResourceBarrierTransition(texture, ResourceStates.CopyDestination, ResourceStates.GenericRead); } descrOffsetCB = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView); device.CreateShaderResourceView(texture, null, descriptorHeapCB.CPUDescriptorHandleForHeapStart + descrOffsetCB); #endregion texture #region sampler device.CreateSampler(new SamplerStateDescription { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, Filter = Filter.MaximumMinMagMipLinear, }, descriptorHeapS.CPUDescriptorHandleForHeapStart); #endregion sampler #endif // Get the backbuffer and creates the render target view renderTarget = Collect(swapChain.GetBackBuffer <Resource>(0)); device.CreateRenderTargetView(renderTarget, null, descriptorHeapRT.CPUDescriptorHandleForHeapStart); #if USE_DEPTH depthBuffer = Collect(device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 1, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil), ResourceStates.Present, new ClearValue { Format = Format.D32_Float, DepthStencil = new DepthStencilValue { Depth = 1, Stencil = 0, } })); device.CreateDepthStencilView(depthBuffer, null, descriptorHeapDS.CPUDescriptorHandleForHeapStart); #endif // Create the viewport viewPort = new ViewportF(0, 0, width, height); // Create the scissor scissorRectangle = new Rectangle(0, 0, width, height); // Create a fence to wait for next frame fence = Collect(device.CreateFence(0, FenceFlags.None)); currentFence = 1; // Close command list commandList.Close(); commandQueue.ExecuteCommandList(commandList); // Create an event handle use for VTBL CreateWaitEvent(); // Wait the command list to complete WaitForPrevFrame(); #if USE_TEXTURE buf.Dispose(); #endif }
private void LoadPipeline(RenderForm form) { 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; device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { var queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); 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, SampleDescription = new SampleDescription(1, 0), IsWindowed = true }; var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface<SwapChain3>(); frameIndex = swapChain.CurrentBackBufferIndex; } var rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); var srvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 200, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc); var terrainHeapDesc = new DescriptorHeapDescription() { DescriptorCount = 2, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; terrainHeap = device.CreateDescriptorHeap(terrainHeapDesc); var meshCtrCbvDesc = new DescriptorHeapDescription() { DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView }; meshCtrBufferViewHeap = device.CreateDescriptorHeap(meshCtrCbvDesc); var rtcHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtcHandle); rtcHandle += rtvDescriptorSize; } var svHeapDesc = new DescriptorHeapDescription() { Type = DescriptorHeapType.Sampler, DescriptorCount = 1, Flags = DescriptorHeapFlags.ShaderVisible, NodeMask = 0 }; samplerViewHeap = device.CreateDescriptorHeap(svHeapDesc); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); }
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); }
private void LoadPipeline(RenderForm form) { 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 Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription 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 }; SwapChain 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. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; } //create depth buffer; DescriptorHeapDescription dsvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.DepthStencilView }; depthStencilViewHeap = device.CreateDescriptorHeap(dsvHeapDesc); CpuDescriptorHandle dsvHandle = depthStencilViewHeap.CPUDescriptorHandleForHeapStart; ClearValue depthOptimizedClearValue = new ClearValue() { Format = Format.D32_Float, DepthStencil = new DepthStencilValue() { Depth = 1.0F, Stencil = 0 }, }; depthTarget = device.CreateCommittedResource( new HeapProperties(HeapType.Default), HeapFlags.None, new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 0, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil), ResourceStates.DepthWrite, depthOptimizedClearValue); var depthView = new DepthStencilViewDescription() { Format = Format.D32_Float, Dimension = DepthStencilViewDimension.Texture2D, Flags = DepthStencilViewFlags.None, }; //bind depth buffer device.CreateDepthStencilView(depthTarget, null, dsvHandle); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle); }
private void LoadPipeline(RenderForm form) { int width = form.ClientSize.Width; int height = form.ClientSize.Height; #if DEBUG // Enable the D3D12 debug layer. { DebugInterface.Get().EnableDebugLayer(); } #endif device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription 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 }; SwapChain 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. renderTargetViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); //create depth buffer; depthStencilViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.DepthStencilView }); //constant buffer view heap constantBufferViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = 100, Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView, Flags = DescriptorHeapFlags.ShaderVisible }); //Create targets CreateTargets(width, height); //sampler buffer view heap samplerViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription() { DescriptorCount = 10, Type = DescriptorHeapType.Sampler, Flags = DescriptorHeapFlags.ShaderVisible }); //bind sampler device.CreateSampler(new SamplerStateDescription() { Filter = Filter.ComparisonMinMagMipLinear, AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, MinimumLod = float.MinValue, MaximumLod = float.MaxValue, MipLodBias = 0, MaximumAnisotropy = 0, ComparisonFunction = Comparison.Never }, samplerViewHeap.CPUDescriptorHandleForHeapStart); commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle); form.UserResized += (sender, e) => { isResizing = true; }; }
private void LoadPipeline(RenderForm form) { 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 Device(null, SharpDX.Direct3D.FeatureLevel.Level_12_0); using (var factory = new Factory4()) { // Describe and create the command queue. CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct); commandQueue = device.CreateCommandQueue(queueDesc); // Describe and create the swap chain. SwapChainDescription 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 }; SwapChain 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. DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Flags = DescriptorHeapFlags.None, Type = DescriptorHeapType.RenderTargetView }; renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc); rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); //Init Direct3D11 device from Direct3D12 device device11 = SharpDX.Direct3D11.Device.CreateFromDirect3D12(device, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, null, null, commandQueue); deviceContext11 = device11.ImmediateContext; device11on12 = device11.QueryInterface<SharpDX.Direct3D11.ID3D11On12Device>(); var d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded); // Create frame resources. CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart; for (int n = 0; n < FrameCount; n++) { renderTargets[n] = swapChain.GetBackBuffer<Resource>(n); device.CreateRenderTargetView(renderTargets[n], null, rtvHandle); rtvHandle += rtvDescriptorSize; //init Direct2D surfaces SharpDX.Direct3D11.D3D11ResourceFlags format = new SharpDX.Direct3D11.D3D11ResourceFlags() { BindFlags = (int)SharpDX.Direct3D11.BindFlags.RenderTarget, CPUAccessFlags = (int)SharpDX.Direct3D11.CpuAccessFlags.None }; device11on12.CreateWrappedResource( renderTargets[n], format, (int)ResourceStates.Present, (int)ResourceStates.RenderTarget, typeof(SharpDX.Direct3D11.Resource).GUID, out wrappedBackBuffers[n]); //Init direct2D surface var d2dSurface = wrappedBackBuffers[n].QueryInterface<Surface>(); direct2DRenderTarget[n] = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied))); d2dSurface.Dispose(); } commandAllocator = device.CreateCommandAllocator(CommandListType.Direct); d2dFactory.Dispose(); //Init font var directWriteFactory = new SharpDX.DirectWrite.Factory(); textFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold, SharpDX.DirectWrite.FontStyle.Normal, 48) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near }; textBrush = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget[0], Color.White); directWriteFactory.Dispose(); }
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(); }