2011-12-14 11:49:04 +00:00
|
|
|
using System;
|
2012-09-07 09:48:45 +00:00
|
|
|
using System.IO;
|
|
|
|
using ANX.BaseDirectX;
|
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;
|
|
|
|
using Dx11 = SharpDX.Direct3D11;
|
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
|
|
|
|
|
|
|
namespace ANX.RenderSystem.Windows.DX11
|
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
public class Texture2D_DX11 : BaseTexture2D<Dx11.Texture2D>, INativeTexture2D
|
|
|
|
{
|
|
|
|
#region Public
|
|
|
|
public override int Width
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return NativeTexture != null ? NativeTexture.Description.Width : 0;
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 11:24:23 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
public override int Height
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return NativeTexture != null ? NativeTexture.Description.Height : 0;
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
protected internal Dx11.ShaderResourceView NativeShaderResourceView { get; protected set; }
|
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region Constructor
|
|
|
|
internal Texture2D_DX11(GraphicsDevice graphicsDevice, SurfaceFormat surfaceFormat)
|
2012-09-08 09:07:23 +00:00
|
|
|
: base(graphicsDevice, surfaceFormat, 1)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
public Texture2D_DX11(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount)
|
2012-09-08 09:07:23 +00:00
|
|
|
: base(graphicsDevice, surfaceFormat, mipCount)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
Dx11.Device device = (graphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice.Device;
|
|
|
|
var sampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);
|
|
|
|
|
|
|
|
if (useRenderTexture)
|
|
|
|
{
|
|
|
|
var descriptionStaging = new Dx11.Texture2DDescription()
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
MipLevels = mipCount,
|
|
|
|
ArraySize = mipCount,
|
|
|
|
Format = BaseFormatConverter.Translate(surfaceFormat),
|
|
|
|
SampleDescription = sampleDescription,
|
|
|
|
Usage = Dx11.ResourceUsage.Staging,
|
|
|
|
CpuAccessFlags = Dx11.CpuAccessFlags.Write,
|
|
|
|
};
|
|
|
|
NativeTextureStaging = new Dx11.Texture2D(device, descriptionStaging);
|
|
|
|
}
|
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
var description = new Dx11.Texture2DDescription()
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
MipLevels = mipCount,
|
|
|
|
ArraySize = mipCount,
|
|
|
|
Format = BaseFormatConverter.Translate(surfaceFormat),
|
2012-09-08 09:07:23 +00:00
|
|
|
SampleDescription = sampleDescription,
|
|
|
|
Usage = useRenderTexture ? Dx11.ResourceUsage.Default : Dx11.ResourceUsage.Dynamic,
|
2012-09-07 09:48:45 +00:00
|
|
|
BindFlags = Dx11.BindFlags.ShaderResource,
|
2012-09-08 09:07:23 +00:00
|
|
|
CpuAccessFlags = useRenderTexture ? Dx11.CpuAccessFlags.None : Dx11.CpuAccessFlags.Write,
|
2012-09-07 09:48:45 +00:00
|
|
|
};
|
|
|
|
|
2012-09-08 09:07:23 +00:00
|
|
|
NativeTexture = new Dx11.Texture2D(device, description);
|
|
|
|
NativeShaderResourceView = new Dx11.ShaderResourceView(device, NativeTexture);
|
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 Dispose
|
|
|
|
public override void Dispose()
|
|
|
|
{
|
|
|
|
if (NativeShaderResourceView != null)
|
|
|
|
{
|
|
|
|
NativeShaderResourceView.Dispose();
|
|
|
|
NativeShaderResourceView = null;
|
|
|
|
}
|
|
|
|
|
2012-09-08 09:07:23 +00:00
|
|
|
if (NativeTextureStaging != null)
|
|
|
|
{
|
|
|
|
NativeTextureStaging.Dispose();
|
|
|
|
NativeTextureStaging = null;
|
|
|
|
}
|
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
base.Dispose();
|
|
|
|
}
|
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#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
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region GetData (TODO)
|
|
|
|
public void GetData<T>(T[] data) where T : struct
|
|
|
|
{
|
|
|
|
GetData(data, 0, data.Length);
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
public void GetData<T>(int level, Framework.Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region MapWrite
|
2012-09-08 09:07:23 +00:00
|
|
|
protected override IntPtr MapWrite(int level)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
|
|
|
Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice;
|
2012-09-08 09:07:23 +00:00
|
|
|
tempSubresource = Dx11.Texture2D.CalculateSubResourceIndex(level, 0, mipCount);
|
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
DataBox box = context.MapSubresource(texture, tempSubresource,
|
|
|
|
useRenderTexture ? Dx11.MapMode.Write : Dx11.MapMode.WriteDiscard, Dx11.MapFlags.None);
|
2012-09-07 09:48:45 +00:00
|
|
|
pitch = box.RowPitch;
|
|
|
|
return box.DataPointer;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region MapRead
|
2012-09-08 09:07:23 +00:00
|
|
|
protected override IntPtr MapRead(int level)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
|
|
|
Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice;
|
2012-09-08 09:07:23 +00:00
|
|
|
tempSubresource = Dx11.Texture2D.CalculateSubResourceIndex(level, 0, mipCount);
|
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
DataBox box = context.MapSubresource(texture, tempSubresource, Dx11.MapMode.Read, Dx11.MapFlags.None);
|
2012-09-07 09:48:45 +00:00
|
|
|
pitch = box.RowPitch;
|
|
|
|
return box.DataPointer;
|
|
|
|
}
|
|
|
|
#endregion
|
2012-02-19 13:41:02 +00:00
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
#region Unmap
|
|
|
|
protected override void Unmap()
|
|
|
|
{
|
|
|
|
Dx11.DeviceContext context = (GraphicsDevice.NativeDevice as GraphicsDeviceWindowsDX11).NativeDevice;
|
2012-09-08 09:07:23 +00:00
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
context.UnmapSubresource(texture, tempSubresource);
|
|
|
|
|
|
|
|
if(useRenderTexture)
|
|
|
|
context.CopyResource(NativeTextureStaging, NativeTexture);
|
2012-02-19 13:41:02 +00:00
|
|
|
}
|
2012-09-07 09:48:45 +00:00
|
|
|
#endregion
|
|
|
|
}
|
2011-12-14 11:49:04 +00:00
|
|
|
}
|