private Texture( GraphicsDevice graphicsDevice, PixelFormat pixelFormat, int arraySize, int width, int height, TextureBindFlags bindFlags, int mipMapCount, TextureMipMapData[] mipMapData) : base(graphicsDevice) { Width = width; Height = height; MipMapCount = mipMapCount; PlatformConstruct( graphicsDevice, pixelFormat, arraySize, width, height, bindFlags, mipMapCount, mipMapData); }
private void PlatformConstruct( GraphicsDevice graphicsDevice, PixelFormat pixelFormat, int arraySize, int width, int height, TextureBindFlags bindFlags, int mipMapCount, TextureMipMapData[] mipMapData) { var descriptor = new MTLTextureDescriptor { ArrayLength = (nuint) arraySize, CpuCacheMode = MTLCpuCacheMode.DefaultCache, Depth = 1, Height = (nuint) height, MipmapLevelCount = (nuint) mipMapCount, PixelFormat = pixelFormat.ToMTLPixelFormat(), ResourceOptions = MTLResourceOptions.StorageModeManaged, SampleCount = 1, StorageMode = MTLStorageMode.Managed, TextureType = arraySize > 1 ? MTLTextureType.k2DArray : MTLTextureType.k2D, Width = (nuint) width, Usage = bindFlags.ToMTLTextureUsage() }; DeviceTexture = AddDisposable(graphicsDevice.Device.CreateTexture(descriptor)); if (mipMapData != null) { for (var i = 0; i < mipMapData.Length; i++) { var mipWidth = CalculateMipMapSize(i, width); var mipHeight = CalculateMipMapSize(i, height); var region = MTLRegion.Create2D(0, 0, mipWidth, mipHeight); var dataHandle = GCHandle.Alloc(mipMapData[i].Data, GCHandleType.Pinned); try { DeviceTexture.ReplaceRegion( region, (nuint) i, dataHandle.AddrOfPinnedObject(), (nuint) mipMapData[i].BytesPerRow); } finally { dataHandle.Free(); } } } }
private void PlatformConstruct( GraphicsDevice graphicsDevice, PixelFormat pixelFormat, int arraySize, int width, int height, TextureBindFlags bindFlags, int mipMapCount, TextureMipMapData[] mipMapData) { var resourceDescription = new Texture2DDescription { ArraySize = arraySize, BindFlags = bindFlags.ToBindFlags(), CpuAccessFlags = CpuAccessFlags.None, Format = pixelFormat.ToDxgiFormat(), Height = height, MipLevels = mipMapCount, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), Usage = mipMapData != null ? D3D11.ResourceUsage.Immutable : D3D11.ResourceUsage.Default, Width = width }; if (mipMapData != null) { var dataStreams = new DataStream[mipMapData.Length]; var dataBoxes = new DataBox[mipMapData.Length]; for (var i = 0; i < mipMapData.Length; i++) { dataStreams[i] = DataStream.Create(mipMapData[i].Data, true, false); dataBoxes[i] = new DataBox(dataStreams[i].DataPointer, mipMapData[i].BytesPerRow, 0); } DeviceResource = AddDisposable(new Texture2D( graphicsDevice.Device, resourceDescription, dataBoxes)); foreach (var dataStream in dataStreams) { dataStream.Dispose(); } } else { DeviceResource = AddDisposable(new Texture2D( graphicsDevice.Device, resourceDescription)); } }
public static BindFlags ToBindFlags(this TextureBindFlags value) { var result = BindFlags.None; if (value.HasFlag(TextureBindFlags.ShaderResource)) { result |= BindFlags.ShaderResource; } if (value.HasFlag(TextureBindFlags.RenderTarget)) { result |= BindFlags.RenderTarget; } return(result); }
public static MTLTextureUsage ToMTLTextureUsage(this TextureBindFlags value) { switch (value) { case TextureBindFlags.None: return(MTLTextureUsage.Unknown); case TextureBindFlags.ShaderResource: return(MTLTextureUsage.ShaderRead); case TextureBindFlags.RenderTarget: return(MTLTextureUsage.RenderTarget); default: throw new ArgumentOutOfRangeException(); } }
public static Texture CreateTexture2D( GraphicsDevice graphicsDevice, PixelFormat pixelFormat, int width, int height, TextureBindFlags bindFlags) { var result = new Texture( graphicsDevice, pixelFormat, 1, width, height, bindFlags, 1, null); return(result); }
/// <summary> /// Retrieves a list of flags matching the specified DirectX texture binding flags. /// </summary> /// <param name="bindFlags">DirectX texture binding flags to translate into Oculus SDK texture binding flags.</param> /// <returns>Oculus SDK texture binding flags matching the specified bindFlags.</returns> public static TextureBindFlags GetTextureBindFlags(SharpDX.Direct3D11.BindFlags bindFlags) { TextureBindFlags result = TextureBindFlags.None; if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.DepthStencil)) { result |= TextureBindFlags.DX_DepthStencil; } if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.RenderTarget)) { result |= TextureBindFlags.DX_RenderTarget; } if (bindFlags.HasFlag(SharpDX.Direct3D11.BindFlags.UnorderedAccess)) { result |= TextureBindFlags.DX_DepthStencil; } return(result); }