public static DX11Texture2D LoadFromMemory(DxDevice device, IntPtr dataPointer, int dataLength) { IntPtr resource; NativeMethods.LoadTextureFromMemory(device.Device.NativePointer, dataPointer, dataLength, out resource); if (resource != IntPtr.Zero) { Texture2D texture = Texture2D.FromPointer <Texture2D>(resource); ShaderResourceView srv = new ShaderResourceView(device, texture); return(DX11Texture2D.FromReference(device, texture, srv)); } else { throw new Exception("Failed to load texture"); } }
public static DX11Texture2D LoadFromFile(DxDevice device, string path, bool mips = true) { IntPtr resource; int levels = mips ? 0 : 1; NativeMethods.LoadTextureFromFile(device.Device.NativePointer, path, out resource, levels); if (resource != IntPtr.Zero) { Texture2D texture = Texture2D.FromPointer <Texture2D>(resource); ShaderResourceView srv = new ShaderResourceView(device, texture); return(DX11Texture2D.FromReference(device, texture, srv)); } else { throw new Exception("Failed to load texture"); } }
public DX11DepthStencil(DxDevice device, int w, int h, SampleDescription sd, eDepthFormat depthformat = eDepthFormat.d24s8) { this.device = device; var depthBufferDesc = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, Format = depthformat.GetTypeLessFormat(), Height = h, Width = w, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = sd, Usage = ResourceUsage.Default }; this.Texture = new Texture2D(device.Device, depthBufferDesc); this.resourceDesc = this.Texture.Description; this.DepthFormat = depthformat; ShaderResourceViewDescription srvd = new ShaderResourceViewDescription() { Format = depthformat.GetSRVFormat(), Dimension = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled, }; if (sd.Count == 1) { srvd.Texture2D.MipLevels = 1; srvd.Texture2D.MostDetailedMip = 0; } this.ShaderView = new ShaderResourceView(device.Device, this.Texture, srvd); if (depthformat.HasStencil()) { ShaderResourceViewDescription stencild = new ShaderResourceViewDescription() { Dimension = sd.Count == 1 ? ShaderResourceViewDimension.Texture2D : ShaderResourceViewDimension.Texture2DMultisampled, Format = depthformat.GetStencilSRVFormat(), }; if (sd.Count == 1) { stencild.Texture2D.MipLevels = 1; stencild.Texture2D.MostDetailedMip = 0; } ShaderResourceView stencilview = new ShaderResourceView(this.device.Device, this.Texture, stencild); this.Stencil = DX11Texture2D.FromReference(this.device, this.Texture, stencilview); } else { //Just pass depth instead this.Stencil = null; } DepthStencilViewDescription dsvd = new DepthStencilViewDescription() { Format = depthformat.GetDepthFormat(), Dimension = sd.Count == 1 ? DepthStencilViewDimension.Texture2D : DepthStencilViewDimension.Texture2DMultisampled, }; this.DepthView = new DepthStencilView(device.Device, this.Texture, dsvd); //Read only dsv only supported in dx11 minimum if (device.IsFeatureLevel11) { dsvd.Flags = DepthStencilViewFlags.ReadOnlyDepth; if (depthformat.HasStencil()) { dsvd.Flags |= DepthStencilViewFlags.ReadOnlyStencil; } this.ReadOnlyView = new DepthStencilView(device.Device, this.Texture, dsvd); } }