2012-09-15 13:43:31 +00:00
|
|
|
#region Using Statements
|
2011-11-16 14:27:53 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2012-09-06 09:58:13 +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-11-16 14:27:53 +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-11-16 14:27:53 +00:00
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
using Dx10 = SharpDX.Direct3D10;
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
namespace ANX.RenderSystem.Windows.DX10
|
2011-11-16 14:27:53 +00:00
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
public partial class DxTexture2D : INativeTexture2D
|
2012-09-04 21:36:46 +00:00
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
private Dx10.Texture2D NativeTextureStaging;
|
|
|
|
|
|
|
|
public int Width
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
return NativeTexture != null ? NativeTexture.Description.Width : 0;
|
2012-09-06 09:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
public int Height
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
return NativeTexture != null ? NativeTexture.Description.Height : 0;
|
2012-09-06 09:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
internal Dx10.ShaderResourceView NativeShaderResourceView { get; set; }
|
|
|
|
|
|
|
|
internal Dx10.Texture2D NativeTexture { get; set; }
|
2012-09-06 09:58:13 +00:00
|
|
|
|
|
|
|
#region Constructor
|
2012-09-15 13:43:31 +00:00
|
|
|
internal DxTexture2D(GraphicsDevice graphicsDevice, SurfaceFormat surfaceFormat)
|
|
|
|
: this(graphicsDevice, surfaceFormat, 1)
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-15 13:43:31 +00:00
|
|
|
public DxTexture2D(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat surfaceFormat, int mipCount)
|
|
|
|
: this(graphicsDevice, surfaceFormat, mipCount)
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
2012-09-15 13:43:31 +00:00
|
|
|
Dx10.Device device = (graphicsDevice.NativeDevice as GraphicsDeviceDX).NativeDevice;
|
2012-09-08 09:07:23 +00:00
|
|
|
|
|
|
|
if (useRenderTexture)
|
|
|
|
{
|
|
|
|
var descriptionStaging = new Dx10.Texture2DDescription()
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
MipLevels = mipCount,
|
|
|
|
ArraySize = mipCount,
|
2012-09-15 13:43:31 +00:00
|
|
|
Format = DxFormatConverter.Translate(surfaceFormat),
|
2012-09-08 09:07:23 +00:00
|
|
|
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
|
|
|
|
Usage = Dx10.ResourceUsage.Staging,
|
|
|
|
CpuAccessFlags = Dx10.CpuAccessFlags.Write,
|
|
|
|
};
|
|
|
|
NativeTextureStaging = new Dx10.Texture2D(device, descriptionStaging);
|
|
|
|
}
|
2012-09-06 09:58:13 +00:00
|
|
|
|
2012-09-08 09:07:23 +00:00
|
|
|
var description = new Dx10.Texture2DDescription()
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
MipLevels = mipCount,
|
|
|
|
ArraySize = mipCount,
|
2012-09-15 13:43:31 +00:00
|
|
|
Format = DxFormatConverter.Translate(surfaceFormat),
|
2012-09-06 09:58:13 +00:00
|
|
|
SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
|
2012-09-08 09:07:23 +00:00
|
|
|
Usage = useRenderTexture ? Dx10.ResourceUsage.Default : Dx10.ResourceUsage.Dynamic,
|
2012-09-06 09:58:13 +00:00
|
|
|
BindFlags = Dx10.BindFlags.ShaderResource,
|
2012-09-08 09:07:23 +00:00
|
|
|
CpuAccessFlags = useRenderTexture ? Dx10.CpuAccessFlags.None : Dx10.CpuAccessFlags.Write,
|
2012-09-06 09:58:13 +00:00
|
|
|
OptionFlags = Dx10.ResourceOptionFlags.None,
|
|
|
|
};
|
|
|
|
|
2012-09-07 09:48:45 +00:00
|
|
|
NativeTexture = new Dx10.Texture2D(device, description);
|
|
|
|
NativeShaderResourceView = new Dx10.ShaderResourceView(device, NativeTexture);
|
2012-09-06 09:58:13 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region GetHashCode
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return NativeTexture.NativePointer.ToInt32();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-09-04 21:36:46 +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?
|
|
|
|
Dx10.Texture2D.ToStream(NativeTexture, Dx10.ImageFileFormat.Jpg, stream);
|
2012-09-04 21:36:46 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#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?
|
|
|
|
Dx10.Texture2D.ToStream(NativeTexture, Dx10.ImageFileFormat.Png, stream);
|
2012-09-04 21:36:46 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-09-06 09:58:13 +00:00
|
|
|
#region GetData (TODO)
|
|
|
|
public void GetData<T>(T[] data) where T : struct
|
2012-09-04 21:36:46 +00:00
|
|
|
{
|
2012-09-07 09:48:45 +00:00
|
|
|
GetData(data, 0, data.Length);
|
2012-09-04 21:36:46 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 09:58:13 +00:00
|
|
|
public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
|
2012-09-04 21:36:46 +00:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
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
|
2012-09-04 21:36:46 +00:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
#endregion
|
2012-09-07 09:48:45 +00:00
|
|
|
|
|
|
|
#region MapWrite
|
2012-09-15 13:43:31 +00:00
|
|
|
protected IntPtr MapWrite(int level)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
tempSubresource = Dx10.Texture2D.CalculateSubResourceIndex(level, 0, mipCount);
|
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
DataRectangle rect = texture.Map(tempSubresource, useRenderTexture ? Dx10.MapMode.Write : Dx10.MapMode.WriteDiscard,
|
|
|
|
Dx10.MapFlags.None);
|
2012-09-07 09:48:45 +00:00
|
|
|
pitch = rect.Pitch;
|
|
|
|
return rect.DataPointer;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region MapRead
|
2012-09-15 13:43:31 +00:00
|
|
|
protected IntPtr MapRead(int level)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
tempSubresource = Dx10.Texture2D.CalculateSubResourceIndex(level, 0, mipCount);
|
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
DataRectangle rect = texture.Map(tempSubresource, Dx10.MapMode.Read, Dx10.MapFlags.None);
|
2012-09-07 09:48:45 +00:00
|
|
|
pitch = rect.Pitch;
|
|
|
|
return rect.DataPointer;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Unmap
|
2012-09-15 13:43:31 +00:00
|
|
|
protected void Unmap()
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2012-09-08 09:07:23 +00:00
|
|
|
var texture = useRenderTexture ? NativeTextureStaging : NativeTexture;
|
|
|
|
texture.Unmap(tempSubresource);
|
|
|
|
if (useRenderTexture)
|
|
|
|
texture.Device.CopyResource(NativeTextureStaging, NativeTexture);
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
|
|
|
#endregion
|
2012-09-04 21:36:46 +00:00
|
|
|
}
|
2011-11-16 14:27:53 +00:00
|
|
|
}
|