2012-09-15 13:43:31 +00:00
|
|
|
#region Using Statements
|
2011-12-14 11:49:04 +00:00
|
|
|
using System;
|
2012-09-07 09:48:45 +00:00
|
|
|
using System.IO;
|
2011-12-14 11:49:04 +00:00
|
|
|
using ANX.Framework.Graphics;
|
|
|
|
using ANX.Framework.NonXNA.RenderSystem;
|
2012-09-07 09:48:45 +00:00
|
|
|
using SharpDX;
|
2012-09-15 13:43:31 +00:00
|
|
|
|
|
|
|
#endregion
|
2011-12-14 11:49:04 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-12-14 11:49:04 +00:00
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
using Dx11 = SharpDX.Direct3D11;
|
|
|
|
|
2011-12-14 11:49:04 +00:00
|
|
|
namespace ANX.RenderSystem.Windows.DX11
|
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
public partial class DxTexture2D : INativeTexture2D
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2015-09-30 21:31:15 +02:00
|
|
|
private Dx11.Texture2D _nativeTexture;
|
|
|
|
|
|
|
|
public Dx11.ShaderResourceView NativeShaderResourceView { get; private set; }
|
|
|
|
|
|
|
|
public Dx11.Texture2D NativeTexture
|
|
|
|
{
|
|
|
|
get { return _nativeTexture; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (NativeShaderResourceView != null)
|
|
|
|
{
|
|
|
|
NativeShaderResourceView.Dispose();
|
|
|
|
NativeShaderResourceView = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_nativeTexture = value;
|
|
|
|
|
|
|
|
if (_nativeTexture != null)
|
|
|
|
{
|
|
|
|
NativeShaderResourceView = new Dx11.ShaderResourceView(this.GraphicsDevice.NativeDevice.Device, NativeTexture);
|
|
|
|
#if DEBUG
|
|
|
|
NativeShaderResourceView.DebugName = _nativeTexture.DebugName + "_ShaderView";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region Constructor
|
2015-09-30 21:31:15 +02:00
|
|
|
protected DxTexture2D(GraphicsDeviceDX graphicsDevice)
|
|
|
|
{
|
|
|
|
this.GraphicsDevice = graphicsDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DxTexture2D(GraphicsDeviceDX graphicsDevice, Dx11.Texture2D nativeTexture)
|
|
|
|
: this(graphicsDevice)
|
|
|
|
{
|
|
|
|
if (nativeTexture == null)
|
|
|
|
throw new ArgumentNullException("nativeTexture");
|
|
|
|
|
|
|
|
this.Width = nativeTexture.Description.Width;
|
|
|
|
this.Height = nativeTexture.Description.Height;
|
|
|
|
#if DEBUG
|
|
|
|
if (string.IsNullOrEmpty(nativeTexture.DebugName))
|
|
|
|
nativeTexture.DebugName = "Texture_" + textureCount++;
|
|
|
|
#endif
|
|
|
|
this.NativeTexture = nativeTexture;
|
|
|
|
|
|
|
|
this.surfaceFormat = DxFormatConverter.Translate(nativeTexture.Description.Format);
|
|
|
|
}
|
|
|
|
|
|
|
|
public DxTexture2D(GraphicsDeviceDX graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount)
|
|
|
|
: this(graphicsDevice)
|
|
|
|
{
|
|
|
|
this.surfaceFormat = surfaceFormat;
|
|
|
|
|
|
|
|
Dx11.Device device = graphicsDevice.NativeDevice.Device;
|
|
|
|
|
|
|
|
var description = new Dx11.Texture2DDescription()
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
MipLevels = mipCount,
|
|
|
|
ArraySize = mipCount,
|
|
|
|
Format = DxFormatConverter.Translate(surfaceFormat),
|
|
|
|
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
|
|
|
|
Usage = Dx11.ResourceUsage.Default,
|
|
|
|
BindFlags = Dx11.BindFlags.ShaderResource,
|
|
|
|
CpuAccessFlags = Dx11.CpuAccessFlags.None,
|
|
|
|
OptionFlags = Dx11.ResourceOptionFlags.None,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.Width = width;
|
|
|
|
this.Height = height;
|
|
|
|
var texture = new Dx11.Texture2D(device, description);
|
|
|
|
#if DEBUG
|
|
|
|
texture.DebugName = "Texture_" + textureCount++;
|
|
|
|
#endif
|
|
|
|
this.NativeTexture = texture;
|
|
|
|
}
|
2012-09-07 09:48:45 +00:00
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region GetHashCode
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return NativeTexture.NativePointer.ToInt32();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region SaveAsJpeg (TODO)
|
|
|
|
public void SaveAsJpeg(Stream stream, int width, int height)
|
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
// TODO: handle width and height?
|
|
|
|
Dx11.Texture2D.ToStream(NativeTexture.Device.ImmediateContext, NativeTexture, Dx11.ImageFileFormat.Jpg, stream);
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region SaveAsPng (TODO)
|
|
|
|
public void SaveAsPng(Stream stream, int width, int height)
|
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
// TODO: handle width and height?
|
|
|
|
Dx11.Texture2D.ToStream(NativeTexture.Device.ImmediateContext, NativeTexture, Dx11.ImageFileFormat.Png, stream);
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
protected DataStream Map(Dx11.Texture2D texture, int subresource, int level, ResourceMapping mapping, out int pitch)
|
|
|
|
{
|
|
|
|
DataStream stream;
|
|
|
|
var box = GraphicsDevice.NativeDevice.MapSubresource(texture, subresource, mapping.ToMapMode(), Dx11.MapFlags.None, out stream);
|
|
|
|
|
|
|
|
pitch = box.RowPitch;
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected int GetSubresource(int level)
|
|
|
|
{
|
|
|
|
return Dx11.Texture2D.CalculateSubResourceIndex(level, 0, NativeTexture.Description.MipLevels);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void UpdateSubresource<T>(T[] data, Dx11.Texture2D texture, int level, Framework.Rectangle rect) where T : struct
|
|
|
|
{
|
|
|
|
GraphicsDevice.NativeDevice.UpdateSubresource(data, _nativeTexture, this.GetSubresource(level), GetPitch(level), 0, rect.ToResourceRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Dx11.Texture2D CreateStagingTexture(ResourceMapping mapping)
|
|
|
|
{
|
|
|
|
var description = new Dx11.Texture2DDescription()
|
|
|
|
{
|
|
|
|
CpuAccessFlags = mapping.ToCpuAccessFlags(),
|
|
|
|
Usage = Dx11.ResourceUsage.Staging,
|
|
|
|
Format = NativeTexture.Description.Format,
|
|
|
|
Width = this.Width,
|
|
|
|
Height = this.Height,
|
|
|
|
MipLevels = this.NativeTexture.Description.MipLevels,
|
|
|
|
SampleDescription = NativeTexture.Description.SampleDescription,
|
|
|
|
ArraySize = NativeTexture.Description.ArraySize,
|
|
|
|
OptionFlags = Dx11.ResourceOptionFlags.None,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var texture = new Dx11.Texture2D(GraphicsDevice.NativeDevice.Device, description);
|
|
|
|
#if DEBUG
|
|
|
|
texture.DebugName = NativeTexture.DebugName + "_Staging";
|
|
|
|
#endif
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Unmap(Dx11.Texture2D texture, int subresource)
|
|
|
|
{
|
|
|
|
GraphicsDevice.NativeDevice.UnmapSubresource(texture, subresource);
|
|
|
|
}
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
2011-12-14 11:49:04 +00:00
|
|
|
}
|