private void SetShaderLineColor(ColorRgba color) { LineColorBufferElement bufElem = new LineColorBufferElement { LineColor = color }; if (lcBuffer == null) { lcBuffer = device.CreateBuffer( new BufferDescription { BindFlags = BindFlag.ConstantBuffer, CpuAccessFlags = CpuAccessFlag.Write, MiscFlags = ResourceMiscFlag.Undefined, Usage = Usage.Dynamic, ByteWidth = (uint)Marshal.SizeOf(bufElem) }); } MappedSubresource mappedResource = (MappedSubresource)deviceContext.Map(lcBuffer, 0, Map.WriteDiscard, MapFlag.Unspecified); Marshal.StructureToPtr(bufElem, mappedResource.Data, true); deviceContext.Unmap(lcBuffer, 0); deviceContext.PS.SetConstantBuffers(0, new D3DBuffer[] { lcBuffer }); }
private void SetShaderLineColor(ColorRgba color) { LineColorBufferElement bufElem = new LineColorBufferElement { LineColor = color }; if (lcBuffer == null) { lcBuffer = device.CreateBuffer( new BufferDescription { BindingOptions = BindingOptions.ConstantBuffer, CpuAccessOptions = CpuAccessOptions.Write, MiscellaneousResourceOptions = MiscellaneousResourceOptions.None, Usage = Usage.Dynamic, ByteWidth = (uint)Marshal.SizeOf(bufElem) }); } MappedSubresource mappedResource = (MappedSubresource)deviceContext.Map(lcBuffer, 0, Map.WriteDiscard, WindowsAPICodePack.DirectX.Direct3D11.MapOptions.None); Marshal.StructureToPtr(bufElem, mappedResource.Data, true); deviceContext.Unmap(lcBuffer, 0); deviceContext.PS.SetConstantBuffers(0, new D3DBuffer[] { lcBuffer }); }
private void SetShaderMatrices() { VSMatrix vsm = new VSMatrix(); vsm.wvp = DXMatrixHelper.Transpose(MatrixExtensions.ToMatrix4x4F(worldMatrix)); if (wvpBuffer == null) { wvpBuffer = device.CreateBuffer( new BufferDescription { BindFlags = BindFlag.ConstantBuffer, CpuAccessFlags = CpuAccessFlag.Write, MiscFlags = ResourceMiscFlag.Undefined, Usage = Usage.Dynamic, ByteWidth = (uint)Marshal.SizeOf(vsm) }); } MappedSubresource mappedResource = (MappedSubresource)deviceContext.Map(wvpBuffer, 0, Map.WriteDiscard, MapFlag.Unspecified); Marshal.StructureToPtr(vsm, mappedResource.Data, true); deviceContext.Unmap(wvpBuffer, 0); deviceContext.VS.SetConstantBuffers(0, new D3DBuffer[] { wvpBuffer }); }
private void SetShaderMatrices() { VSMatrix vsm = new VSMatrix(); vsm.wvp = DXMatrixHelper.Transpose(DXMatrixHelper.ToMatrix4x4F(worldMatrix)); if (wvpBuffer == null) { var bd = new BufferDescription { BindingOptions = BindingOptions.ConstantBuffer, CpuAccessOptions = CpuAccessOptions.Write, //MiscellaneousResourceOptions = MiscellaneousResourceOptions.None, //MiscFlags = ResourceMiscFlag.Undefined, Usage = Usage.Dynamic, ByteWidth = (uint)Marshal.SizeOf(vsm) }; wvpBuffer = device.CreateBuffer(bd); } MappedSubresource mappedResource = (MappedSubresource)deviceContext.Map(wvpBuffer, 0, Map.WriteDiscard, WindowsAPICodePack.DirectX.Direct3D11.MapOptions.None); Marshal.StructureToPtr(vsm, mappedResource.Data, true); deviceContext.Unmap(wvpBuffer, 0); deviceContext.VS.SetConstantBuffers(0, new D3DBuffer[] { wvpBuffer }); }
unsafe public static void UpdateVertexBuffer <T>(ID3D11DeviceContext context, ID3D11Buffer buffer, int slot, T data) where T : struct { MappedSubresource mappedResource = context.Map(buffer, MapMode.WriteDiscard, MapFlags.None); Unsafe.Write((void *)mappedResource.DataPointer, data); context.Unmap(buffer); context.VSSetConstantBuffer(slot, buffer); }
private void UpdateBBox(ID3D11Device InDevice, ID3D11DeviceContext InDContext) { // Update vertex buffer VertexLayouts.BasicLayout.Vertex[] BBoxVertices = new VertexLayouts.BasicLayout.Vertex[Objects.Count * 8]; int CurrentBBoxIndex = 0; foreach (RenderBoundingBox BBox in Objects.Values) { VertexLayouts.BasicLayout.Vertex[] Cached = BBox.GetTransformVertices(); System.Array.Copy(Cached, 0, BBoxVertices, CurrentBBoxIndex * Cached.Length, Cached.Length); CurrentBBoxIndex++; } // Update index buffer uint[] BBoxIndices = new uint[Objects.Count * 24]; CurrentBBoxIndex = 0; foreach (RenderBoundingBox BBox in Objects.Values) { uint[] CopiedIndices = new uint[BBox.Indices.Length]; for (int i = 0; i < CopiedIndices.Length; i++) { int BBoxOffset = (CurrentBBoxIndex * 8); CopiedIndices[i] = (ushort)(BBox.Indices[i] + BBoxOffset); } System.Array.Copy(CopiedIndices, 0, BBoxIndices, CurrentBBoxIndex * BBox.Indices.Length, BBox.Indices.Length); CurrentBBoxIndex++; } SizeToRender = Objects.Count * 24; if (VertexBuffer == null && IndexBuffer == null) { VertexBuffer = InDevice.CreateBuffer(BindFlags.VertexBuffer, BBoxVertices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); IndexBuffer = InDevice.CreateBuffer(BindFlags.IndexBuffer, BBoxIndices, 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); } else { // TODO: Templatize this MappedSubresource mappedResource = InDContext.Map(VertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, BBoxVertices); } InDContext.Unmap(VertexBuffer); mappedResource = InDContext.Map(IndexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, BBoxIndices); } InDContext.Unmap(IndexBuffer); } }
public override void UpdateBuffers(ID3D11Device device, ID3D11DeviceContext deviceContext) { if (bIsUpdatedNeeded) { MappedSubresource mappedResource = deviceContext.Map(vertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, vertices); } deviceContext.Unmap(vertexBuffer, 0); bIsUpdatedNeeded = false; } }
private void UpdateLines(ID3D11Device InDevice, ID3D11DeviceContext InDContext) { // Update buffers List <VertexLayouts.BasicLayout.Vertex> TempLineVertices = new List <VertexLayouts.BasicLayout.Vertex>(); List <ushort> TempLineIndices = new List <ushort>(); int CurrentVertexCount = 0; foreach (RenderLine Line in Objects.Values) { TempLineVertices.AddRange(Line.GetVertices); ushort[] CopiedIndices = Line.GetStrippedIndices; for (uint x = 0; x < CopiedIndices.Length; x++) { CopiedIndices[x] += (ushort)(CurrentVertexCount); } TempLineIndices.AddRange(CopiedIndices.ToList()); TempLineIndices.Add(ushort.MaxValue); CurrentVertexCount = TempLineVertices.Count; } SizeToRender = TempLineIndices.Count; if (VertexBuffer == null && IndexBuffer == null) { VertexBuffer = InDevice.CreateBuffer(BindFlags.VertexBuffer, TempLineVertices.ToArray(), 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); IndexBuffer = InDevice.CreateBuffer(BindFlags.IndexBuffer, TempLineIndices.ToArray(), 0, ResourceUsage.Dynamic, CpuAccessFlags.Write); } else { // TODO: Templatize this MappedSubresource mappedResource = InDContext.Map(VertexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, TempLineVertices.ToArray()); } InDContext.Unmap(VertexBuffer); mappedResource = InDContext.Map(IndexBuffer, MapMode.WriteDiscard, MapFlags.None); unsafe { UnsafeUtilities.Write(mappedResource.DataPointer, TempLineIndices.ToArray()); } InDContext.Unmap(IndexBuffer); } }
private void ProcessDataIntoSKPixmap(MappedSubresource dataBox) { int width = dataBox.RowPitch / 4; int height = dataBox.DepthPitch / width / 4; var skInfo = new SKImageInfo { ColorType = SKColorType.Bgra8888, AlphaType = SKAlphaType.Premul, Height = height, Width = width }; lock (pixmapLock) { pixmap = new SKPixmap(skInfo, dataBox.DataPointer); } }
private D3D11GraphicsDevice(Window?window, SizeI size, Format depthStencilFormat = Format.D32_Float) { Window = window; Size = size; Factory = CreateDXGIFactory1 <IDXGIFactory2>(); using (IDXGIAdapter1 adapter = GetHardwareAdapter()) { DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport; #if DEBUG if (SdkLayersAvailable()) { creationFlags |= DeviceCreationFlags.Debug; } #endif if (D3D11CreateDevice( adapter, DriverType.Unknown, creationFlags, s_featureLevels, out ID3D11Device tempDevice, out FeatureLevel, out ID3D11DeviceContext tempContext).Failure) { // If the initialization fails, fall back to the WARP device. // For more information on WARP, see: // http://go.microsoft.com/fwlink/?LinkId=286690 D3D11CreateDevice( IntPtr.Zero, DriverType.Warp, creationFlags, s_featureLevels, out tempDevice, out FeatureLevel, out tempContext).CheckError(); } Device = tempDevice.QueryInterface <ID3D11Device1>(); DeviceContext = tempContext.QueryInterface <ID3D11DeviceContext1>(); tempContext.Dispose(); tempDevice.Dispose(); } if (window != null) { IntPtr hwnd = window.Handle; SwapChainDescription1 swapChainDescription = new() { Width = window.ClientSize.Width, Height = window.ClientSize.Height, Format = Format.R8G8B8A8_UNorm, BufferCount = FrameCount, BufferUsage = Usage.RenderTargetOutput, SampleDescription = SampleDescription.Default, Scaling = Scaling.Stretch, SwapEffect = SwapEffect.FlipDiscard, AlphaMode = AlphaMode.Ignore }; SwapChainFullscreenDescription fullscreenDescription = new SwapChainFullscreenDescription { Windowed = true }; SwapChain = Factory.CreateSwapChainForHwnd(Device, hwnd, swapChainDescription, fullscreenDescription); Factory.MakeWindowAssociation(hwnd, WindowAssociationFlags.IgnoreAltEnter); BackBufferTexture = SwapChain.GetBuffer <ID3D11Texture2D>(0); RenderTargetView = Device.CreateRenderTargetView(BackBufferTexture); } else { // Create offscreen texture OffscreenTexture = Device.CreateTexture2D(Format.R8G8B8A8_UNorm, Size.Width, Size.Height, 1, 1, null, BindFlags.ShaderResource | BindFlags.RenderTarget); RenderTargetView = Device.CreateRenderTargetView(OffscreenTexture); } if (depthStencilFormat != Format.Unknown) { DepthStencilTexture = Device.CreateTexture2D(depthStencilFormat, Size.Width, Size.Height, 1, 1, null, BindFlags.DepthStencil); DepthStencilView = Device.CreateDepthStencilView(DepthStencilTexture !, new DepthStencilViewDescription(DepthStencilTexture, DepthStencilViewDimension.Texture2D)); } ReadOnlySpan <VertexPositionColor> triangleVertices = stackalloc VertexPositionColor[] { new VertexPositionColor(new Vector3(0f, 0.5f, 0.0f), new Color4(1.0f, 0.0f, 0.0f, 1.0f)), new VertexPositionColor(new Vector3(0.5f, -0.5f, 0.0f), new Color4(0.0f, 1.0f, 0.0f, 1.0f)), new VertexPositionColor(new Vector3(-0.5f, -0.5f, 0.0f), new Color4(0.0f, 0.0f, 1.0f, 1.0f)) }; bool dynamic = false; if (dynamic) { _vertexBuffer = Device.CreateBuffer(VertexPositionColor.SizeInBytes * 3, BindFlags.VertexBuffer, ResourceUsage.Dynamic, CpuAccessFlags.Write); MappedSubresource mappedSubresource = DeviceContext.Map(_vertexBuffer, 0, MapMode.WriteDiscard); triangleVertices.CopyTo(mappedSubresource.AsSpan <VertexPositionColor>(3)); DeviceContext.Unmap(_vertexBuffer, 0); } else { _vertexBuffer = Device.CreateBuffer(triangleVertices, BindFlags.VertexBuffer); } InputElementDescription[] inputElementDescs = new[] { new InputElementDescription("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElementDescription("COLOR", 0, Format.R32G32B32A32_Float, 12, 0) }; Span <byte> vertexShaderByteCode = CompileBytecode("Triangle.hlsl", "VSMain", "vs_4_0"); Span <byte> pixelShaderByteCode = CompileBytecode("Triangle.hlsl", "PSMain", "ps_4_0"); _vertexShader = Device.CreateVertexShader(vertexShaderByteCode); _pixelShader = Device.CreatePixelShader(pixelShaderByteCode); _inputLayout = Device.CreateInputLayout(inputElementDescs, vertexShaderByteCode); }
private unsafe void SaveScreenshot(string path, ContainerFormat format = ContainerFormat.Jpeg) { var d3d11GraphicsDevice = ((D3D11GraphicsDevice)_graphicsDevice !); ID3D11Texture2D source = Headless ? d3d11GraphicsDevice !.OffscreenTexture : d3d11GraphicsDevice !.BackBufferTexture; path = Path.Combine(AppContext.BaseDirectory, path); using (ID3D11Texture2D staging = d3d11GraphicsDevice !.CaptureTexture(source)) { staging.DebugName = "STAGING"; var textureDesc = staging !.Description; // Determine source format's WIC equivalent Guid pfGuid = default; bool sRGB = false; switch (textureDesc.Format) { case Format.R32G32B32A32_Float: pfGuid = WICPixelFormat.Format128bppRGBAFloat; break; case Format.R16G16B16A16_Float: pfGuid = WICPixelFormat.Format64bppRGBAHalf; break; case Format.R16G16B16A16_UNorm: pfGuid = WICPixelFormat.Format64bppRGBA; break; // DXGI 1.1 case Format.R10G10B10_Xr_Bias_A2_UNorm: pfGuid = WICPixelFormat.Format32bppRGBA1010102XR; break; case Format.R10G10B10A2_UNorm: pfGuid = WICPixelFormat.Format32bppRGBA1010102; break; case Format.B5G5R5A1_UNorm: pfGuid = WICPixelFormat.Format16bppBGRA5551; break; case Format.B5G6R5_UNorm: pfGuid = WICPixelFormat.Format16bppBGR565; break; case Format.R32_Float: pfGuid = WICPixelFormat.Format32bppGrayFloat; break; case Format.R16_Float: pfGuid = WICPixelFormat.Format16bppGrayHalf; break; case Format.R16_UNorm: pfGuid = WICPixelFormat.Format16bppGray; break; case Format.R8_UNorm: pfGuid = WICPixelFormat.Format8bppGray; break; case Format.A8_UNorm: pfGuid = WICPixelFormat.Format8bppAlpha; break; case Format.R8G8B8A8_UNorm: pfGuid = WICPixelFormat.Format32bppRGBA; break; case Format.R8G8B8A8_UNorm_SRgb: pfGuid = WICPixelFormat.Format32bppRGBA; sRGB = true; break; case Format.B8G8R8A8_UNorm: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGRA; break; case Format.B8G8R8A8_UNorm_SRgb: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGRA; sRGB = true; break; case Format.B8G8R8X8_UNorm: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGR; break; case Format.B8G8R8X8_UNorm_SRgb: // DXGI 1.1 pfGuid = WICPixelFormat.Format32bppBGR; sRGB = true; break; default: Console.WriteLine($"ERROR: ScreenGrab does not support all DXGI formats ({textureDesc.Format})"); return; } using var wicFactory = new IWICImagingFactory2(); //using IWICBitmapDecoder decoder = wicFactory.CreateDecoderFromFileName(path); //using Stream stream = File.OpenWrite(path); //using IWICStream wicStream = wicFactory.CreateStream(stream); using IWICStream wicStream = wicFactory.CreateStream(path, FileAccess.Write); using IWICBitmapEncoder encoder = wicFactory.CreateEncoder(format, wicStream); // Create a Frame encoder using IWICBitmapFrameEncode frame = encoder.CreateNewFrame(out SharpGen.Runtime.Win32.IPropertyBag2? props); frame.Initialize(props); frame.SetSize(textureDesc.Width, textureDesc.Height); frame.SetResolution(72, 72); // Screenshots don't typically include the alpha channel of the render target Guid targetGuid; switch (textureDesc.Format) { case Format.R32G32B32A32_Float: case Format.R16G16B16A16_Float: //if (IsWIC2()) { targetGuid = WICPixelFormat.Format96bppRGBFloat; } //else //{ // targetGuid = WICPixelFormat.Format24bppBGR; //} break; case Format.R16G16B16A16_UNorm: targetGuid = WICPixelFormat.Format48bppBGR; break; case Format.B5G5R5A1_UNorm: targetGuid = WICPixelFormat.Format16bppBGR555; break; case Format.B5G6R5_UNorm: targetGuid = WICPixelFormat.Format16bppBGR565; break; case Format.R32_Float: case Format.R16_Float: case Format.R16_UNorm: case Format.R8_UNorm: case Format.A8_UNorm: targetGuid = WICPixelFormat.Format8bppGray; break; default: targetGuid = WICPixelFormat.Format24bppBGR; break; } frame.SetPixelFormat(targetGuid); ID3D11DeviceContext1 context = d3d11GraphicsDevice !.DeviceContext; const bool native = false; if (native) { MappedSubresource mappedSubresource = context.Map(staging, 0, MapMode.Read); int imageSize = mappedSubresource.RowPitch * textureDesc.Height; if (targetGuid != pfGuid) { // Conversion required to write using (IWICBitmap bitmapSource = wicFactory.CreateBitmapFromMemory( textureDesc.Width, textureDesc.Height, pfGuid, mappedSubresource.RowPitch, imageSize, mappedSubresource.DataPointer)) { using (IWICFormatConverter formatConverter = wicFactory.CreateFormatConverter()) { if (!formatConverter.CanConvert(pfGuid, targetGuid)) { context.Unmap(staging, 0); return; } formatConverter.Initialize(bitmapSource, targetGuid, BitmapDitherType.None, null, 0, BitmapPaletteType.MedianCut); frame.WriteSource(formatConverter, new RectI(textureDesc.Width, textureDesc.Height)); } } } else { // No conversion required frame.WritePixels(textureDesc.Height, mappedSubresource.RowPitch, imageSize, mappedSubresource.DataPointer); } } else { int stride = WICPixelFormat.GetStride(pfGuid, textureDesc.Width); ReadOnlySpan <Color> colors = context.MapReadOnly <Color>(staging); if (targetGuid != pfGuid) { // Conversion required to write using (IWICBitmap bitmapSource = wicFactory.CreateBitmapFromMemory( textureDesc.Width, textureDesc.Height, pfGuid, colors, stride)) { using (IWICFormatConverter formatConverter = wicFactory.CreateFormatConverter()) { if (!formatConverter.CanConvert(pfGuid, targetGuid)) { context.Unmap(staging, 0); return; } formatConverter.Initialize(bitmapSource, targetGuid, BitmapDitherType.None, null, 0, BitmapPaletteType.MedianCut); frame.WriteSource(formatConverter, new RectI(textureDesc.Width, textureDesc.Height)); } } } else { // No conversion required frame.WritePixels(textureDesc.Height, stride, colors); } } context.Unmap(staging, 0); frame.Commit(); encoder.Commit(); } }
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource) { MappedResourceCacheKey key = new MappedResourceCacheKey(resource, subresource); lock (_mappedResourceLock) { if (_mappedResources.TryGetValue(key, out MappedResourceInfo info)) { if (info.Mode != mode) { throw new VeldridException("The given resource was already mapped with a different MapMode."); } info.RefCount += 1; _mappedResources[key] = info; } else { // No current mapping exists -- create one. if (resource is D3D11Buffer buffer) { lock (_immediateContextLock) { MappedSubresource msr = _immediateContext.Map( buffer.Buffer, 0, D3D11Formats.VdToD3D11MapMode((buffer.Usage & BufferUsage.Dynamic) == BufferUsage.Dynamic, mode), Vortice.Direct3D11.MapFlags.None); info.MappedResource = new MappedResource(resource, mode, msr.DataPointer, buffer.SizeInBytes); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } else { D3D11Texture texture = Util.AssertSubtype <MappableResource, D3D11Texture>(resource); lock (_immediateContextLock) { MappedSubresource msr = _immediateContext.Map( texture.DeviceTexture, 0, (int)subresource, D3D11Formats.VdToD3D11MapMode(false, mode), Vortice.Direct3D11.MapFlags.None, out int mipSize); info.MappedResource = new MappedResource( resource, mode, msr.DataPointer, texture.Height * (uint)msr.RowPitch, subresource, (uint)msr.RowPitch, (uint)msr.DepthPitch); info.RefCount = 1; info.Mode = mode; _mappedResources.Add(key, info); } } } return(info.MappedResource); } }