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;
|
2015-09-30 21:31:15 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2012-09-15 13:43:31 +00:00
|
|
|
|
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
|
|
|
{
|
2015-09-30 21:31:15 +02:00
|
|
|
private Dx10.Texture2D _nativeTexture;
|
|
|
|
|
|
|
|
public Dx10.ShaderResourceView NativeShaderResourceView { get; private set; }
|
|
|
|
|
|
|
|
public Dx10.Texture2D NativeTexture
|
|
|
|
{
|
|
|
|
get { return _nativeTexture; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (NativeShaderResourceView != null)
|
|
|
|
{
|
|
|
|
NativeShaderResourceView.Dispose();
|
|
|
|
NativeShaderResourceView = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_nativeTexture = value;
|
|
|
|
|
|
|
|
if (_nativeTexture != null)
|
|
|
|
{
|
|
|
|
NativeShaderResourceView = new Dx10.ShaderResourceView(this.GraphicsDevice.NativeDevice, NativeTexture);
|
|
|
|
#if DEBUG
|
|
|
|
NativeShaderResourceView.DebugName = _nativeTexture.DebugName + "_ShaderView";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected DxTexture2D(GraphicsDeviceDX graphicsDevice)
|
|
|
|
{
|
|
|
|
this.GraphicsDevice = graphicsDevice;
|
|
|
|
}
|
|
|
|
|
2015-10-04 21:30:00 +02:00
|
|
|
internal DxTexture2D(GraphicsDeviceDX graphicsDevice, Dx10.Texture2D nativeTexture)
|
2015-09-30 21:31:15 +02:00
|
|
|
: 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)
|
2012-09-06 09:58:13 +00:00
|
|
|
{
|
2015-09-30 21:31:15 +02:00
|
|
|
this.surfaceFormat = surfaceFormat;
|
2012-09-08 09:07:23 +00:00
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
Dx10.Device device = graphicsDevice.NativeDevice;
|
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),
|
2015-09-30 21:31:15 +02:00
|
|
|
Usage = Dx10.ResourceUsage.Default,
|
2012-09-06 09:58:13 +00:00
|
|
|
BindFlags = Dx10.BindFlags.ShaderResource,
|
2015-09-30 21:31:15 +02:00
|
|
|
CpuAccessFlags = Dx10.CpuAccessFlags.None,
|
2012-09-06 09:58:13 +00:00
|
|
|
OptionFlags = Dx10.ResourceOptionFlags.None,
|
|
|
|
};
|
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
this.Width = width;
|
|
|
|
this.Height = height;
|
|
|
|
var texture = new Dx10.Texture2D(device, description);
|
|
|
|
#if DEBUG
|
|
|
|
texture.DebugName = "Texture_" + textureCount++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NativeTexture = texture;
|
2012-09-06 09:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
return NativeTexture.NativePointer.ToInt32();
|
|
|
|
}
|
|
|
|
|
2012-09-04 21:36:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
protected DataStream Map(Dx10.Texture2D texture, int subresource, int level, ResourceMapping mapping, out int pitch)
|
2012-09-04 21:36:46 +00:00
|
|
|
{
|
2015-09-30 21:31:15 +02:00
|
|
|
var dataRect = texture.Map(subresource, mapping.ToMapMode(), Dx10.MapFlags.None);
|
2012-09-04 21:36:46 +00:00
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
pitch = dataRect.Pitch;
|
2012-09-07 09:48:45 +00:00
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
bool read = (mapping & ResourceMapping.Read) != 0;
|
|
|
|
bool write = (mapping & ResourceMapping.Write) != 0;
|
2012-09-07 09:48:45 +00:00
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
return new DataStream(dataRect.DataPointer, SizeInBytes(level), read, write);
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 21:31:15 +02:00
|
|
|
protected int GetSubresource(int level)
|
|
|
|
{
|
|
|
|
return Dx10.Texture2D.CalculateSubResourceIndex(level, 0, NativeTexture.Description.MipLevels);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void UpdateSubresource<T>(T[] data, Dx10.Texture2D texture, int level, Framework.Rectangle rect) where T : struct
|
|
|
|
{
|
|
|
|
var handle = GCHandle.Alloc(data, GCHandleType.Pinned);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
GraphicsDevice.NativeDevice.UpdateSubresource(texture, this.GetSubresource(level), rect.ToResourceRegion(), handle.AddrOfPinnedObject(), GetPitch(level), 0);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
handle.Free();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Dx10.Texture2D CreateStagingTexture(ResourceMapping mapping)
|
|
|
|
{
|
|
|
|
var description = new Dx10.Texture2DDescription()
|
|
|
|
{
|
|
|
|
CpuAccessFlags = mapping.ToCpuAccessFlags(),
|
|
|
|
Usage = Dx10.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 = Dx10.ResourceOptionFlags.None,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var texture = new Dx10.Texture2D(GraphicsDevice.NativeDevice, description);
|
|
|
|
#if DEBUG
|
|
|
|
texture.DebugName = NativeTexture.DebugName + "_Staging";
|
|
|
|
#endif
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Unmap(Dx10.Texture2D texture, int subresource)
|
2012-09-07 09:48:45 +00:00
|
|
|
{
|
2015-09-30 21:31:15 +02:00
|
|
|
texture.Unmap(subresource);
|
2012-09-07 09:48:45 +00:00
|
|
|
}
|
2012-09-04 21:36:46 +00:00
|
|
|
}
|
2011-11-16 14:27:53 +00:00
|
|
|
}
|