public void Dispose() { DXDebug.CheckAccess(graphicsHost.Device); constantBuffer.Dispose(); pipelineState.Dispose(); vertexBuffer.Dispose(); }
public override Texture <SharpDX.Direct3D12.Resource> CreateTexture(TextureContent data) { DXDebug.CheckAccess(graphicsHost.Device); SharpDX.Direct3D12.Resource texture = null; var textureDesc = ResourceDescription.Texture2D(Format.R8G8B8A8_UNorm, data.Width, data.Height); texture = graphicsHost.Device.CreateCommittedResource(new HeapProperties(HeapType.Default), HeapFlags.None, textureDesc, ResourceStates.CopyDestination); long uploadBufferSize = GetRequiredIntermediateSize(texture, 0, 1); // Create the GPU upload buffer. var textureUploadHeap = graphicsHost.Device.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, ResourceDescription.Texture2D(Format.R8G8B8A8_UNorm, data.Width, data.Height), ResourceStates.GenericRead); // Copy data to the intermediate upload heap and then schedule a copy // from the upload heap to the Texture2D. var handle = GCHandle.Alloc(data.Pixels, GCHandleType.Pinned); var ptr = Marshal.UnsafeAddrOfPinnedArrayElement(data.Pixels, 0); textureUploadHeap.WriteToSubresource(0, null, ptr, (sizeof(byte) * 4) * data.Width, data.Pixels.Length); handle.Free(); // TODO: Runtime invalid call error below //var commandList = graphicsHost.RequestCommandList(); //commandList.CopyTextureRegion(new TextureCopyLocation(texture, 0), 0, 0, 0, new TextureCopyLocation(textureUploadHeap, 0), null); //commandList.ResourceBarrierTransition(texture, ResourceStates.CopyDestination, ResourceStates.PixelShaderResource); //// Describe and create a SRV for the texture. //var srvDesc = new ShaderResourceViewDescription //{ // Shader4ComponentMapping = DXHelper.DefaultComponentMapping(), // Format = textureDesc.Format, // Dimension = ShaderResourceViewDimension.Texture2D, // Texture2D = { MipLevels = 1 }, //}; //graphicsHost.Device.CreateShaderResourceView(texture, srvDesc, graphicsHost.SRVHeap.CPUDescriptorHandleForHeapStart); //// Command lists are created in the recording state, but there is nothing //// to record yet. The main loop expects it to be closed, so close it now. //commandList.Close(); //graphicsHost.CommandQueue.ExecuteCommandList(commandList); textureUploadHeap.Dispose(); return(new Texture <SharpDX.Direct3D12.Resource>(texture, data.Width, data.Height, data.Left, data.Right, data.Top, data.Bottom, data.IsTransparent)); }
protected override unsafe void BeginDraw(ref Matrix4x4 projection, ushort *pIndex, int indexCount) { DXDebug.CheckAccess(graphicsHost.Device); // Update constant buffer resource. constantBufferData.transform = projection; SharpDX.Utilities.Write(mappedConstantBuffer, ref constantBufferData); // Request a new bundle. commandList = graphicsHost.RequestBundle(pipelineState); // Record the commands. commandList.SetGraphicsRootDescriptorTable(0, graphicsHost.CBVHeap.GPUDescriptorHandleForHeapStart); commandList.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleStrip; }
protected override unsafe void Draw(Vertex2D *pVertex, int vertexCount, SharpDX.Direct3D12.Resource texture, bool isTransparent) { DXDebug.CheckAccess(graphicsHost.Device); // TODO: texture, isTransparent commandList.SetGraphicsRootDescriptorTable(1, graphicsHost.SRVHeap.GPUDescriptorHandleForHeapStart); // Update the vertex buffer IntPtr dataBegin = vertexBuffer.Map(0); SharpDX.Utilities.CopyMemory(dataBegin, new IntPtr(pVertex), vertexCount * Vertex2D.SizeInBytes); vertexBuffer.Unmap(0); // Record the commands. commandList.SetVertexBuffer(0, vertexBufferView); // Call draw. commandList.DrawInstanced(vertexCount / 4 * 6, 1, 0, 0); }
private void CreateWindowResources() { // Wait until all previous GPU work is complete. WaitForGPU(); // Clear the previous window size specific content. for (int i = 0; i < FrameCount; i++) { renderTargets[i] = null; } // Calculate the necessary render target size in pixels. var outputSize = new Size2(); outputSize.Width = window.ClientSize.Width; outputSize.Height = window.ClientSize.Height; // Prevent zero size DirectX content from being created. outputSize.Width = Math.Max(outputSize.Width, 640); outputSize.Height = Math.Max(outputSize.Width, 480); if (swapChain != null) { // If the swap chain already exists, resize it. swapChain.ResizeBuffers(FrameCount, outputSize.Width, outputSize.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None); if (!DXDebug.ValidateDevice(device)) { throw new ArgumentNullException(nameof(device)); } } else { using (var factory = new Factory4()) { // Otherwise, create a new one using the same adapter as the existing Direct3D device. SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = FrameCount, ModeDescription = new ModeDescription(outputSize.Width, outputSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, SwapEffect = SwapEffect.FlipDiscard, OutputHandle = window.Handle, SampleDescription = new SampleDescription(1, 0), IsWindowed = true, }; var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc); swapChain = tempSwapChain.QueryInterface <SwapChain3>(); swapChain.DebugName = "SwapChain"; tempSwapChain.Dispose(); } } // Create a render target view of the swap chain back buffer. var descriptorHeapDesc = new DescriptorHeapDescription() { DescriptorCount = FrameCount, Type = DescriptorHeapType.RenderTargetView, Flags = DescriptorHeapFlags.None }; rtvHeap = device.CreateDescriptorHeap(descriptorHeapDesc); rtvHeap.Name = "Render Target View Descriptor Heap"; // All pending GPU work was already finished. Update the tracked fence values // to the last value signaled. for (int i = 0; i < FrameCount; i++) { fenceValues[i] = fenceValues[currentFrame]; } currentFrame = 0; var rtvDescriptor = rtvHeap.CPUDescriptorHandleForHeapStart; rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView); for (int i = 0; i < FrameCount; i++) { renderTargets[i] = swapChain.GetBackBuffer <Resource>(i); device.CreateRenderTargetView(renderTargets[i], null, rtvDescriptor + (rtvDescriptorSize * i)); renderTargets[i].Name = $"Render Target {i}"; } viewport = new ViewportF(); viewport.Width = outputSize.Width; viewport.Height = outputSize.Height; viewport.MaxDepth = 1.0f; scissorRect = new Rectangle(); scissorRect.Right = outputSize.Width; scissorRect.Bottom = outputSize.Height; }