private static void CreateBitmapTexture(D3D11Device device, string fileName, out D3D11ShaderResourceView textureView) { int width; int height; byte[] bytes; using (var file = new Bitmap(fileName)) { var rect = new Rectangle(0, 0, file.Width, file.Height); int length = file.Width * file.Height; width = file.Width; height = file.Height; bytes = new byte[length * 4]; using (var bitmap = file.Clone(rect, PixelFormat.Format32bppArgb)) { BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat); try { Marshal.Copy(data.Scan0, bytes, 0, length * 4); } finally { bitmap.UnlockBits(data); } } } D3D11SubResourceData[] textureSubResData = new[] { new D3D11SubResourceData(bytes, (uint)width * 4) }; var textureDesc = new D3D11Texture2DDesc(DxgiFormat.B8G8R8A8UNorm, (uint)width, (uint)height, 1, 1); using (var texture = device.CreateTexture2D(textureDesc, textureSubResData)) { var textureViewDesc = new D3D11ShaderResourceViewDesc { Format = textureDesc.Format, ViewDimension = D3D11SrvDimension.Texture2D, Texture2D = new D3D11Texture2DSrv { MipLevels = textureDesc.MipLevels, MostDetailedMip = 0 } }; textureView = device.CreateShaderResourceView(texture, textureViewDesc); } }
static D3D11ShaderResourceView CreateBufferSRV(D3D11Device pDevice, D3D11Buffer pBuffer) { var descBuf = pBuffer.Description; D3D11ShaderResourceViewDesc desc; if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferAllowRawViews)) { // This is a Raw Buffer desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.R32Typeless, 0, descBuf.ByteWidth / 4, D3D11BufferExSrvOptions.Raw); } else if (descBuf.MiscOptions.HasFlag(D3D11ResourceMiscOptions.BufferStructured)) { // This is a Structured Buffer desc = new D3D11ShaderResourceViewDesc(pBuffer, DxgiFormat.Unknown, 0, descBuf.ByteWidth / descBuf.StructureByteStride, D3D11BufferExSrvOptions.None); } else { throw new InvalidOperationException(); } return(pDevice.CreateShaderResourceView(pBuffer, desc)); }
private static void CreateD3DResources( D3D11Device device, D3D11ResourceDimension resDim, int width, int height, int depth, int mipCount, int arraySize, DxgiFormat format, D3D11Usage usage, D3D11BindOptions bindFlags, D3D11CpuAccessOptions cpuAccessFlags, D3D11ResourceMiscOptions miscFlags, bool forceSRGB, bool isCubeMap, D3D11SubResourceData[] initData, out D3D11Resource texture, out D3D11ShaderResourceView textureView) { texture = null; textureView = null; if (forceSRGB) { format = (DxgiFormat)DdsHelpers.MakeSrgb((DdsFormat)format); } switch (resDim) { case D3D11ResourceDimension.Texture1D: { var desc = new D3D11Texture1DDesc( format, (uint)width, (uint)arraySize, (uint)mipCount, bindFlags, usage, cpuAccessFlags, miscFlags & ~D3D11ResourceMiscOptions.TextureCube); texture = device.CreateTexture1D(desc, initData); try { var SRVDesc = new D3D11ShaderResourceViewDesc { Format = format }; if (arraySize > 1) { SRVDesc.ViewDimension = D3D11SrvDimension.Texture1DArray; SRVDesc.Texture1DArray = new D3D11Texture1DArraySrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, ArraySize = (uint)arraySize }; } else { SRVDesc.ViewDimension = D3D11SrvDimension.Texture1D; SRVDesc.Texture1D = new D3D11Texture1DSrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels }; } textureView = device.CreateShaderResourceView(texture, SRVDesc); } catch { D3D11Utils.DisposeAndNull(ref texture); throw; } break; } case D3D11ResourceDimension.Texture2D: { var desc = new D3D11Texture2DDesc( format, (uint)width, (uint)height, (uint)arraySize, (uint)mipCount, bindFlags, usage, cpuAccessFlags, 1, 0); if (isCubeMap) { desc.MiscOptions = miscFlags | D3D11ResourceMiscOptions.TextureCube; } else { desc.MiscOptions = miscFlags & ~D3D11ResourceMiscOptions.TextureCube; } if (format == DxgiFormat.BC1UNorm || format == DxgiFormat.BC2UNorm || format == DxgiFormat.BC3UNorm) { if ((width & 3) != 0 || (height & 3) != 0) { desc.Width = (uint)(width + 3) & ~3U; desc.Height = (uint)(height + 3) & ~3U; } } texture = device.CreateTexture2D(desc, initData); try { var SRVDesc = new D3D11ShaderResourceViewDesc { Format = format }; if (isCubeMap) { if (arraySize > 6) { SRVDesc.ViewDimension = D3D11SrvDimension.TextureCubeArray; SRVDesc.TextureCubeArray = new D3D11TextureCubeArraySrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, // Earlier we set arraySize to (NumCubes * 6) NumCubes = (uint)arraySize / 6 }; } else { SRVDesc.ViewDimension = D3D11SrvDimension.TextureCube; SRVDesc.TextureCube = new D3D11TextureCubeSrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, }; } } else if (arraySize > 1) { SRVDesc.ViewDimension = D3D11SrvDimension.Texture2DArray; SRVDesc.Texture2DArray = new D3D11Texture2DArraySrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, ArraySize = (uint)arraySize }; } else { SRVDesc.ViewDimension = D3D11SrvDimension.Texture2D; SRVDesc.Texture2D = new D3D11Texture2DSrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, }; } textureView = device.CreateShaderResourceView(texture, SRVDesc); } catch { D3D11Utils.DisposeAndNull(ref texture); throw; } break; } case D3D11ResourceDimension.Texture3D: { var desc = new D3D11Texture3DDesc( format, (uint)width, (uint)height, (uint)depth, (uint)mipCount, bindFlags, usage, cpuAccessFlags, miscFlags & ~D3D11ResourceMiscOptions.TextureCube); texture = device.CreateTexture3D(desc, initData); try { var SRVDesc = new D3D11ShaderResourceViewDesc { Format = format, ViewDimension = D3D11SrvDimension.Texture3D, Texture3D = new D3D11Texture3DSrv { MipLevels = (mipCount == 0) ? uint.MaxValue : desc.MipLevels, } }; textureView = device.CreateShaderResourceView(texture, SRVDesc); } catch { D3D11Utils.DisposeAndNull(ref texture); throw; } break; } } }