//internal FeaturesPerFormat(PixelFormat format, MultisampleCount maximumMultisampleCount, ComputeShaderFormatSupport computeShaderFormatSupport, FormatSupport formatSupport) internal FeaturesPerFormat(PixelFormat format, MultisampleCount maximumMultisampleCount, FormatSupport formatSupport) { Format = format; this.MultisampleCountMax = maximumMultisampleCount; //ComputeShaderFormatSupport = computeShaderFormatSupport; FormatSupport = formatSupport; }
/// <summary> /// Creates a <see cref="RenderTargetView"/> with the specified width, height, and <see cref="Format"/>. /// The render target is also bound as a shader resource (BindFlags.ShaderResource) and a <see cref="ShaderResourceView"/> is created. /// </summary> /// <param name="device">The current <see cref="Device"/> being used.</param> /// <param name="width">The width of the <see cref="RenderTargetView"/> being created.</param> /// <param name="height">The height of the <see cref="RenderTargetView"/> being created.</param> /// <param name="format"> /// The <see cref="Format"/> of the <see cref="RenderTargetView"/> being created. The device is queried for support of the given format. /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown. /// </param> /// <param name="targetTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="RenderTargetView"/>.</param> /// <param name="shaderView">The newly created <see cref="ShaderResourceView"/> that can be used to bind the render target as input to a shader.</param> /// <returns>The newly created <see cref="RenderTargetView"/> object.</returns> public static RenderTargetView CreateRenderTarget(Device device, int width, int height, Format format, out Texture2D targetTexture, out ShaderResourceView shaderView) { FormatSupport support = device.CheckFormatSupport(format); if ((support & FormatSupport.RenderTarget) != FormatSupport.RenderTarget) { throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a RenderTarget on this GPU."); } targetTexture = CreateTexture2D(device, width, height, format, BindFlags.RenderTarget | BindFlags.ShaderResource); shaderView = new ShaderResourceView(device, targetTexture); // Is this necessary? It seems to work fine without it. //RenderTargetViewDescription rtViewDesc = new RenderTargetViewDescription(); //rtViewDesc.ArraySize = 1; //rtViewDesc.DepthSliceCount = 1; //rtViewDesc.Dimension = RenderTargetViewDimension.Texture2D; //rtViewDesc.ElementOffset = 0; //rtViewDesc.ElementWidth = 0; //rtViewDesc.FirstArraySlice = 0; //rtViewDesc.FirstDepthSlice = 0; //rtViewDesc.Format = format; //rtViewDesc.MipSlice = 0; return(new RenderTargetView(device, targetTexture)); }
//internal FeaturesPerFormat(PixelFormat format, MSAALevel maximumMSAALevel, ComputeShaderFormatSupport computeShaderFormatSupport, FormatSupport formatSupport) internal FeaturesPerFormat(PixelFormat format, MSAALevel maximumMSAALevel, FormatSupport formatSupport) { Format = format; this.MSAALevelMax = maximumMSAALevel; //ComputeShaderFormatSupport = computeShaderFormatSupport; FormatSupport = formatSupport; }
private Texture2D CaptureTexture(DeviceContext deviceContext, Texture2D source, out Texture2DDescription desc) { Device d3dDevice = deviceContext.Device; // debug: i got it! //D3D11.Texture2D texture = source.QueryInterface<D3D11.Texture2D>(); Texture2D staging = null; desc = source.Description; if (desc.SampleDescription.Count > 1) { desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; Texture2D temp = new Texture2D(d3dDevice, desc); DXGI.Format fmt = EnsureNotTypeless(desc.Format); FormatSupport support = d3dDevice.CheckFormatSupport(fmt); if ((support & FormatSupport.MultisampleResolve) == 0) { return(null); } for (int item = 0; item < desc.ArraySize; ++item) { for (int level = 0; level < desc.MipLevels; ++level) { int index = Resource.CalculateSubResourceIndex(level, item, desc.MipLevels); deviceContext.ResolveSubresource(temp, index, source, index, fmt); } } desc.BindFlags = 0; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; staging = new Texture2D(d3dDevice, desc); deviceContext.CopyResource(temp, staging); } else if (desc.Usage == ResourceUsage.Staging && desc.CpuAccessFlags == CpuAccessFlags.Read) { staging = source; } else { desc.BindFlags = 0; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; staging = new Texture2D(d3dDevice, desc); deviceContext.CopyResource(source, staging); } return(staging); }
internal FeaturesPerFormat(Format format, MSAALevel maximumMSAALevel, ComputeShaderFormatSupport computeShaderFormatSupport, FormatSupport formatSupport) { Format = format; this.MSAALevelMax = maximumMSAALevel; ComputeShaderFormatSupport = computeShaderFormatSupport; FormatSupport = formatSupport; }
/// <summary> /// Register supported formats for specified usage /// </summary> /// <param name="usage">Usage to register</param> private void RegisterFormats(FormatSupport usage) { //Shouldn't happen but just in case if (!this.usageformats.ContainsKey(usage)) { this.usageformats.Add(usage, FormatHelper.Instance.SupportedFormats(this.device, usage)); } }
/// <summary> /// Register supported formats for specified usage /// </summary> /// <param name="usage">Usage to register</param> private void RegisterFormats(FormatSupport usage) { //Shouldn't happen but just in case if (!this.usageformats.ContainsKey(usage)) { this.usageformats.Add(usage, FormatHelper.Instance.SupportedFormats(this.device, usage)); } }
private void BuildFormatSupport() { foreach (object o in Enum.GetValues(typeof(FormatSupport))) { FormatSupport usage = (FormatSupport)o; this.usageformats[usage] = this.SupportedFormats(usage); } }
/// <summary> /// Lists supported DXGI formats for a given usage /// </summary> /// <param name="dev">Device to check format support for</param> /// <param name="usage">Requested Usage</param> /// <returns>List of Supported formats</returns> public List<string> SupportedFormats(Device dev, FormatSupport usage) { List<string> result = new List<string>(); foreach (string s in Enum.GetNames(typeof(Format))) { if (IsSupported(dev, usage, (Format)Enum.Parse(typeof(Format), s))) { result.Add(s); } } return result; }
/// <summary> /// Lists supported DXGI formats for a given usage /// </summary> /// <param name="dev">Device to check format support for</param> /// <param name="usage">Requested Usage</param> /// <returns>List of Supported formats</returns> private List<Format> SupportedFormats(FormatSupport usage) { List<Format> result = new List<Format>(); foreach (object o in Enum.GetValues(typeof(Format))) { Format f = (Format)o; if (IsSupported(usage,f)) { result.Add(f); } } return result; }
/// <summary> /// Lists supported DXGI formats for a given usage /// </summary> /// <param name="dev">Device to check format support for</param> /// <param name="usage">Requested Usage</param> /// <returns>List of Supported formats</returns> public List <string> SupportedFormats(Device dev, FormatSupport usage) { List <string> result = new List <string>(); foreach (string s in Enum.GetNames(typeof(Format))) { if (IsSupported(dev, usage, (Format)Enum.Parse(typeof(Format), s))) { result.Add(s); } } return(result); }
/// <summary> /// Lists supported DXGI formats for a given usage /// </summary> /// <param name="dev">Device to check format support for</param> /// <param name="usage">Requested Usage</param> /// <returns>List of Supported formats</returns> private List <Format> SupportedFormats(FormatSupport usage) { List <Format> result = new List <Format>(); foreach (object o in Enum.GetValues(typeof(Format))) { Format f = (Format)o; if (IsSupported(usage, f)) { result.Add(f); } } return(result); }
public DeviceFormatHelper(Device device) { this.device = device; foreach (object o in Enum.GetValues(typeof(FormatSupport))) { FormatSupport usage = (FormatSupport)o; this.RegisterFormats(usage); string[] fmts = this.usageformats[usage].ToArray(); //host.UpdateEnum(this.GetEnumName(usage), fmts[0], fmts); EnumManager.UpdateEnum(this.GetEnumName(usage), fmts[0], fmts); } }
/// <summary> /// Creates an <see cref="UnorderedAccessView"/> with the specified width, height, and <see cref="Format"/>. /// </summary> /// <param name="device">The current <see cref="Device"/> being used.</param> /// <param name="width">The width of the <see cref="UnorderedAccessView"/> being created.</param> /// <param name="height">The height of the <see cref="UnorderedAccessView"/> being created.</param> /// <param name="format"> /// The <see cref="Format"/> of the <see cref="UnorderedAccessView"/> being created. The device is queried for support of the given format. /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown. /// </param> /// <param name="unorderedAccessViewTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="UnorderedAccessView"/>.</param> /// <returns>The newly created <see cref="UnorderedAccessView"/> object.</returns> public static UnorderedAccessView CreateUnorderedAccessView(Device device, int width, int height, Format format, out Texture2D unorderedAccessViewTexture) { FormatSupport support = device.CheckFormatSupport(format); if ((support & FormatSupport.UnorderedAccessView) != FormatSupport.UnorderedAccessView) { throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as an UnorderedAccessView on this GPU."); } unorderedAccessViewTexture = CreateTexture2D(device, width, height, format, BindFlags.UnorderedAccess); // Is this necessary? //UnorderedAccessViewDescription uavViewDesc = new UnorderedAccessViewDescription(); //uavViewDesc.ArraySize = 1; //uavViewDesc.Dimension = DepthStencilViewDimension.Texture2D; //uavViewDesc.FirstArraySlice = 0; //uavViewDesc.Flags = DepthStencilViewFlags.None; //uavViewDesc.Format = format; //uavViewDesc.MipSlice = 0; return(new UnorderedAccessView(device, unorderedAccessViewTexture)); }
/// <summary> /// Creates a <see cref="DepthStencilView"/> with the specified width, height, and <see cref="Format"/>. /// </summary> /// <param name="device">The current <see cref="Device"/> being used.</param> /// <param name="width">The width of the <see cref="DepthStencilView"/> being created.</param> /// <param name="height">The height of the <see cref="DepthStencilView"/> being created.</param> /// <param name="format"> /// The <see cref="Format"/> of the <see cref="DepthStencilView"/> being created. The device is queried for support of the given format. /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown. /// </param> /// <param name="depthStencilTexture">The newly created <see cref="Texture2D"/> that is the backing resource for the <see cref="DepthStencilView"/>.</param> /// <returns>The newly created <see cref="DepthStencilView"/> object.</returns> public static DepthStencilView CreateDepthStencilView(Device device, int width, int height, Format format, out Texture2D depthStencilTexture) { FormatSupport support = device.CheckFormatSupport(format); if ((support & FormatSupport.DepthStencil) != FormatSupport.DepthStencil) { throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a DepthStencil on this GPU."); } depthStencilTexture = CreateTexture2D(device, width, height, format, BindFlags.DepthStencil); // Is this necessary? It seems to work fine without it. //DepthStencilViewDescription dsViewDesc = new DepthStencilViewDescription(); //dsViewDesc.ArraySize = 1; //dsViewDesc.Dimension = DepthStencilViewDimension.Texture2D; //dsViewDesc.FirstArraySlice = 0; //dsViewDesc.Flags = DepthStencilViewFlags.None; //dsViewDesc.Format = format; //dsViewDesc.MipSlice = 0; return(new DepthStencilView(device, depthStencilTexture)); }
/// <summary> /// Creates a <see cref="Texture2D"/> with the specified width, height, <see cref="Format"/>, and <see cref="BindFlags"/>. /// It uses default values of CpuAccessFlags.None, ResourceOptionFlags.None, and ResourceUsage.Default /// </summary> /// <param name="device">The current <see cref="Device"/> being used.</param> /// <param name="width">The width of the <see cref="Texture2D"/> being created.</param> /// <param name="height">The height of the <see cref="Texture2D"/> being created.</param> /// <param name="format"> /// The <see cref="Format"/> of the <see cref="Texture2D"/> being created. The device is queried for support of the given format. /// If the current hardware doesn't support the format, then a <see cref="NotSupportedException"/> is thrown. /// </param> /// <param name="bindFlags">The <see cref="BindFlags"/> of the <see cref="Texture2D"/> being created.</param> /// <returns>The newly created <see cref="Texture2D"/> object.</returns> public static Texture2D CreateTexture2D(Device device, int width, int height, Format format, BindFlags bindFlags) { FormatSupport support = device.CheckFormatSupport(format); if ((support & FormatSupport.Texture2D) != FormatSupport.Texture2D) { throw new NotSupportedException("The given format (" + format.ToString() + ") is not supported as a Texture2D on this GPU."); } Texture2DDescription texDesc = new Texture2DDescription(); texDesc.ArraySize = 1; texDesc.BindFlags = bindFlags; texDesc.CpuAccessFlags = CpuAccessFlags.None; texDesc.Format = format; texDesc.Height = height; texDesc.MipLevels = 1; texDesc.OptionFlags = ResourceOptionFlags.None; texDesc.SampleDescription = new SampleDescription(1, 0); texDesc.Usage = ResourceUsage.Default; texDesc.Width = width; return(new Texture2D(device, texDesc)); }
/// <summary> /// Get the VVVV enum name for a specified usage /// </summary> /// <param name="usage">Format usage</param> /// <returns>VVVV Enum name</returns> public string GetEnumName(FormatSupport usage) { return usage.ToString() + "_Formats_DX11_" +device.ComPointer.ToInt32().ToString(); }
public List<string> GetAllowedFormats(FormatSupport usage) { return this.usageformats[usage]; }
public ID3D11Texture2D CaptureTexture(ID3D11Texture2D source) { ID3D11Texture2D stagingTexture; Texture2DDescription desc = source.Description; if (desc.ArraySize > 1 || desc.MipLevels > 1) { Console.WriteLine("WARNING: ScreenGrab does not support 2D arrays, cubemaps, or mipmaps; only the first surface is written. Consider using DirectXTex instead."); return(null); } if (desc.SampleDescription.Count > 1) { // MSAA content must be resolved before being copied to a staging texture desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; ID3D11Texture2D temp = Device.CreateTexture2D(desc); Format format = desc.Format; FormatSupport formatSupport = Device.CheckFormatSupport(format); if ((formatSupport & FormatSupport.MultisampleResolve) == FormatSupport.None) { throw new NotSupportedException($"Format {format} doesn't support multisample resolve"); } for (int item = 0; item < desc.ArraySize; ++item) { for (int level = 0; level < desc.MipLevels; ++level) { int index = CalculateSubResourceIndex(level, item, desc.MipLevels); DeviceContext.ResolveSubresource(temp, index, source, index, format); } } desc.BindFlags = BindFlags.None; desc.MiscFlags &= ResourceOptionFlags.TextureCube; desc.CPUAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; stagingTexture = Device.CreateTexture2D(desc); DeviceContext.CopyResource(stagingTexture, temp); } else if ((desc.Usage == ResourceUsage.Staging) && ((desc.CPUAccessFlags & CpuAccessFlags.Read) != CpuAccessFlags.None)) { // Handle case where the source is already a staging texture we can use directly stagingTexture = source; } else { // Otherwise, create a staging texture from the non-MSAA source desc.BindFlags = 0; desc.MiscFlags &= ResourceOptionFlags.TextureCube; desc.CPUAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; stagingTexture = Device.CreateTexture2D(desc); DeviceContext.CopyResource(stagingTexture, source); } return(stagingTexture); }
/// <summary> /// Checks if a format is supported for a specific usage /// </summary> /// <param name="dev">Device to check</param> /// <param name="usage">Desired format usage</param> /// <param name="format">Desired format</param> /// <returns>true if format supported, false otherwise</returns> public bool IsSupported(FormatSupport usage, Format format) { FormatSupport support = this.Device.CheckFormatSupport(format); return (support | usage) == support; }
/// <summary> /// Checks if a format is supported for a specific usage /// </summary> /// <param name="dev">Device to check</param> /// <param name="usage">Desired format usage</param> /// <param name="format">Desired format</param> /// <returns>true if format supported, false otherwise</returns> public bool IsSupported(Device dev, FormatSupport usage, Format format) { FormatSupport support = dev.CheckFormatSupport(format); return (support | usage) == support; }
public List <string> GetAllowedFormats(FormatSupport usage) { return(this.usageformats[usage]); }
public IEnumerable<IFormatInfo> GetSupportedFormats(FormatSupport requiredSupport) { return FormatInfos.Where(fi => (formatSupports[(Format)fi.ID] & requiredSupport) == requiredSupport); }
private Result CaptureTextureFix(DeviceContext deviceContext, Texture2D source, out Texture2DDescription desc, out Texture2D staging) { desc = new Texture2DDescription(); staging = null; if (deviceContext == null || source == null) { return(Result.InvalidArg); } ResourceDimension resType = source.Dimension; if (resType != ResourceDimension.Texture2D) { //string message = SharpDX.Diagnostics.ErrorManager.GetErrorMessage(0); //return Result.GetResultFromWin32Error(ERROR_NOT_SUPPORTED) } desc = source.Description; Device d3dDevice = deviceContext.Device; //Texture2D staging = null; if (desc.SampleDescription.Count > 1) { desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; Texture2D temp; try { temp = new Texture2D(d3dDevice, desc); } catch (SharpDXException e) { return(e.ResultCode); } DXGI.Format fmt = EnsureNotTypeless(desc.Format); FormatSupport support = FormatSupport.None; try { support = d3dDevice.CheckFormatSupport(fmt); } catch (SharpDXException e) { return(e.ResultCode); } if ((support & FormatSupport.MultisampleResolve) == 0) { return(Result.Fail); } for (int item = 0; item < desc.ArraySize; ++item) { for (int level = 0; level < desc.MipLevels; ++level) { int index = Resource.CalculateSubResourceIndex(level, item, desc.MipLevels); deviceContext.ResolveSubresource(temp, index, source, index, fmt); } } desc.BindFlags = 0; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; try { staging = new Texture2D(d3dDevice, desc); deviceContext.CopyResource(temp, staging); } catch (SharpDXException e) { return(e.ResultCode); } } else if (desc.Usage == ResourceUsage.Staging && desc.CpuAccessFlags == CpuAccessFlags.Read) { staging = source; } else { desc.BindFlags = 0; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; try { staging = new Texture2D(d3dDevice, desc); deviceContext.CopyResource(source, staging); } catch (SharpDXException e) { return(e.ResultCode); } } return(Result.Ok); }
public List<Format> GetAllowedFormats(FormatSupport format) { return usageformats[format]; }
protected override bool GetPixelFormatSupportCore( PixelFormat format, TextureType type, TextureUsage usage, out PixelFormatProperties properties) { if (D3D11Formats.IsUnsupportedFormat(format)) { properties = default(PixelFormatProperties); return(false); } Format dxgiFormat = D3D11Formats.ToDxgiFormat(format, (usage & TextureUsage.DepthStencil) != 0); FormatSupport fs = _device.CheckFormatSupport(dxgiFormat); if ((usage & TextureUsage.RenderTarget) != 0 && (fs & FormatSupport.RenderTarget) == 0 || (usage & TextureUsage.DepthStencil) != 0 && (fs & FormatSupport.DepthStencil) == 0 || (usage & TextureUsage.Sampled) != 0 && (fs & FormatSupport.ShaderSample) == 0 || (usage & TextureUsage.Cubemap) != 0 && (fs & FormatSupport.TextureCube) == 0 || (usage & TextureUsage.Storage) != 0 && (fs & FormatSupport.TypedUnorderedAccessView) == 0) { properties = default(PixelFormatProperties); return(false); } const uint MaxTextureDimension = 16384; const uint MaxVolumeExtent = 2048; uint sampleCounts = 0; if (CheckFormatMultisample(dxgiFormat, 1)) { sampleCounts |= (1 << 0); } if (CheckFormatMultisample(dxgiFormat, 2)) { sampleCounts |= (1 << 1); } if (CheckFormatMultisample(dxgiFormat, 4)) { sampleCounts |= (1 << 2); } if (CheckFormatMultisample(dxgiFormat, 8)) { sampleCounts |= (1 << 3); } if (CheckFormatMultisample(dxgiFormat, 16)) { sampleCounts |= (1 << 4); } if (CheckFormatMultisample(dxgiFormat, 32)) { sampleCounts |= (1 << 5); } properties = new PixelFormatProperties( MaxTextureDimension, type == TextureType.Texture1D ? 1 : MaxTextureDimension, type != TextureType.Texture3D ? 1 : MaxVolumeExtent, uint.MaxValue, type == TextureType.Texture3D ? 1 : MaxVolumeExtent, sampleCounts); return(true); }
/// <summary> /// Checks if a format is supported for a specific usage /// </summary> /// <param name="dev">Device to check</param> /// <param name="usage">Desired format usage</param> /// <param name="format">Desired format</param> /// <returns>true if format supported, false otherwise</returns> public bool IsSupported(Device dev, FormatSupport usage, Format format) { FormatSupport support = dev.CheckFormatSupport(format); return((support | usage) == support); }
public List <Format> GetAllowedFormats(FormatSupport format) { return(usageformats[format]); }
/* public IEnumerable<IDepthStencilFormatInfo> GetSupportedDepthStencilFormats() { return DepthStencilFormatInfos; }*/ public IEnumerable<IFormatInfo> GetSupportedFormats(FormatSupport requiredSupport) { // todo: rewrite var resultingFormatInfos = AllColorFormatInfos; if ((requiredSupport & FormatSupport.Texture1D) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(Texture1DFormatInfos); if ((requiredSupport & FormatSupport.Texture2D) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(Texture2DFormatInfos); if ((requiredSupport & FormatSupport.Texture3D) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(Texture3DFormatInfos); if ((requiredSupport & FormatSupport.TextureCube) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(TextureCubeFormatInfos); if ((requiredSupport & FormatSupport.Mip) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(MipFormatInfos); if ((requiredSupport & FormatSupport.MipAutogen) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(MipAutogenFormatInfos); if ((requiredSupport & FormatSupport.RenderTarget) != 0) resultingFormatInfos = resultingFormatInfos.Intersect(RenderbufferAndTextureFormatInfos); if (requiredSupport.HasFlag(FormatSupport.DepthStencil)) resultingFormatInfos = resultingFormatInfos.Intersect(DepthStencilFormatInfos); return resultingFormatInfos; }
/// <summary> /// Get the VVVV enum name for a specified usage /// </summary> /// <param name="usage">Format usage</param> /// <returns>VVVV Enum name</returns> public string GetEnumName(FormatSupport usage) { return(usage.ToString() + "_Formats_DX11"); }
/// <summary> /// Get the VVVV enum name for a specified usage /// </summary> /// <param name="usage">Format usage</param> /// <returns>VVVV Enum name</returns> public string GetEnumName(FormatSupport usage) { return(usage.ToString() + "_Formats_DX11_" + device.ComPointer.ToInt32().ToString()); }
/// <summary> /// 建立一個臨時的 Texture 以便擷取資源 /// </summary> /// <param name="source">來源texture</param> /// <param name="staging">複本texture</param> /// <returns></returns> private static Result CreateStagingTexture(DeviceContext deviceContext, Resource source, out Texture2DDescription desc, out Texture2D staging) { desc = new Texture2DDescription(); staging = null; if (deviceContext == null && source == null) { return(Result.InvalidArg); } ResourceDimension resourceDimension = source.Dimension; if (resourceDimension != ResourceDimension.Texture2D) { return(Result.InvalidArg); } if (!(source.QueryInterface <Texture2D>() is Texture2D src)) { return(Result.Fail); } desc = src.Description; var d3dDevice = deviceContext.Device; if (desc.SampleDescription.Count > 1) { desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; Texture2D temp; try { temp = new Texture2D(d3dDevice, desc); } catch (SharpDXException e) { return(e.ResultCode); } DXGI.Format fmt = desc.Format.EnsureNotTypeless(); FormatSupport support = FormatSupport.None; try { support = d3dDevice.CheckFormatSupport(fmt); } catch (SharpDXException e) { return(e.ResultCode); } if ((support & FormatSupport.MultisampleResolve) == 0) { return(Result.Fail); } for (int item = 0; item < desc.ArraySize; ++item) { for (int level = 0; level < desc.MipLevels; ++level) { int index = Resource.CalculateSubResourceIndex(level, item, desc.MipLevels); deviceContext.ResolveSubresource(temp, index, source, index, fmt); } } desc.BindFlags = BindFlags.None; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; try { staging = new Texture2D(d3dDevice, desc); deviceContext.CopyResource(temp, staging); } catch (SharpDXException e) { return(e.ResultCode); } } else if (desc.Usage == ResourceUsage.Staging && desc.CpuAccessFlags == CpuAccessFlags.Read) { staging = source.QueryInterface <Texture2D>(); } else { desc.BindFlags = BindFlags.None; desc.OptionFlags &= ResourceOptionFlags.TextureCube; desc.CpuAccessFlags = CpuAccessFlags.Read; desc.Usage = ResourceUsage.Staging; try { staging = new Texture2D(d3dDevice, desc); if (staging != null) { deviceContext.CopyResource(source, staging); } else { return(Result.Fail); } } catch (SharpDXException e) { return(e.ResultCode); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); return(Result.Fail); } } return(Result.Ok); }
private void Load(BitmapSource bitmapSource) { Width = bitmapSource.Size.Width; Height = bitmapSource.Size.Height; int mipLevels = 1; { int width = Width, height = Height; while (width > 1 && height > 1) { width = Math.Max(width / 2, 1); height = Math.Max(height / 2, 1); mipLevels++; } } FormatSupport fmtSupport = Context.Instance.Dev.CheckFormatSupport(PixelFormat); Debug.Assert((fmtSupport & FormatSupport.MipAutogen) != 0); m_Handle.TextureDesc = new Texture2DDescription { Width = Width, Height = Height, MipLevels = 1, ArraySize = 1, Format = PixelFormat, CpuAccessFlags = CpuAccessFlags.None, Usage = ResourceUsage.Default, // BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, BindFlags = BindFlags.ShaderResource, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription { Count = 1, Quality = 0 }, }; DataStream data = new DataStream(Width * Height * 4, true, true); bitmapSource.CopyPixels(Width * 4, data); var dataRectangle = new DataRectangle(data.DataPointer, Width * 4); m_Handle.Texture = new Texture2D(Context.Instance.Dev, m_Handle.TextureDesc, dataRectangle); var srvDesc = new ShaderResourceViewDescription { Format = m_Handle.TextureDesc.Format, Dimension = ShaderResourceViewDimension.Texture2D, Texture2D = new ShaderResourceViewDescription.Texture2DResource { MipLevels = m_Handle.TextureDesc.MipLevels } }; m_Handle.ResourceView = new ShaderResourceView(Context.Instance.Dev, m_Handle.Texture, srvDesc); // Context.Instance.DevCon.UpdateSubresource(ref dataRectangle, m_Handle.Texture); // Context.Instance.DevCon.GenerateMips(m_Handle.ResourceView); m_Handle.SamplerDesc = new SamplerStateDescription { AddressU = TextureAddressMode.Wrap, AddressV = TextureAddressMode.Wrap, AddressW = TextureAddressMode.Wrap, MinimumLod = 0, MaximumLod = 3.402823466e+38f, Filter = (Filter)TextureFilter, ComparisonFunction = Comparison.Never }; m_Handle.SamplerState = new SamplerState(Context.Instance.Dev, m_Handle.SamplerDesc); }
public bool IsSupported(FormatSupport usage, Format format) { FormatSupport support = this.Device.CheckFormatSupport(format); return((support | usage) == support); }