Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

Commit

Permalink
[Toolkit.Graphics] Change API for selecting a Texture.TextureView by …
Browse files Browse the repository at this point in the history
…passing the ViewFormat. Internally we are now using a dictionary to store TextureView
  • Loading branch information
xoofx committed Dec 7, 2013
1 parent 8bf3955 commit 2a52b1f
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 48 deletions.
Expand Up @@ -37,10 +37,11 @@ internal ShaderResourceViewSelector(Texture thisTexture)
/// <summary>
/// Gets a specific <see cref="ShaderResourceView" /> from this texture.
/// </summary>
/// <param name="viewType">Type of the view slice.</param>
/// <param name="arrayOrDepthSlice">The texture array slice index.</param>
/// <param name="mipIndex">The mip map slice index.</param>
/// <param name="viewFormat">The view format.</param>
/// <param name="viewType">Type of the view.</param>
/// <param name="arrayOrDepthSlice">The array or depth slice.</param>
/// <param name="mipIndex">Index of the mip.</param>
/// <returns>An <see cref="ShaderResourceView" /></returns>
public TextureView this[ViewType viewType, int arrayOrDepthSlice, int mipIndex] { get { return this.texture.GetShaderResourceView(viewType, arrayOrDepthSlice, mipIndex); } }
public TextureView this[DXGI.Format viewFormat, ViewType viewType, int arrayOrDepthSlice, int mipIndex] { get { return this.texture.GetShaderResourceView(viewFormat, viewType, arrayOrDepthSlice, mipIndex); } }
}
}
67 changes: 62 additions & 5 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Texture.cs
Expand Up @@ -19,6 +19,7 @@
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.IO;
using SharpDX.DXGI;
using SharpDX.Direct3D11;
Expand Down Expand Up @@ -72,7 +73,8 @@ public abstract class Texture : GraphicsResource, IComparable<Texture>
/// </summary>
internal readonly int DepthStride;

internal TextureView[] shaderResourceViews;
internal TextureView defaultShaderResourceView;
internal Dictionary<TextureViewKey, TextureView> shaderResourceViews;
internal TextureView[] renderTargetViews;
internal UnorderedAccessView[] unorderedAccessViews;
private MipMapDescription[] mipmapDescriptions;
Expand Down Expand Up @@ -811,11 +813,12 @@ public void SetData(GraphicsDevice graphicsDevice, Image image)
/// <summary>
/// Gets a specific <see cref="ShaderResourceView" /> from this texture.
/// </summary>
/// <param name="viewFormat"></param>
/// <param name="viewType">Type of the view slice.</param>
/// <param name="arrayOrDepthSlice">The texture array slice index.</param>
/// <param name="mipIndex">The mip map slice index.</param>
/// <returns>An <see cref="ShaderResourceView" /></returns>
internal abstract TextureView GetShaderResourceView(ViewType viewType, int arrayOrDepthSlice, int mipIndex);
internal abstract TextureView GetShaderResourceView(Format viewFormat, ViewType viewType, int arrayOrDepthSlice, int mipIndex);

/// <summary>
/// Gets a specific <see cref="RenderTargetView" /> from this texture.
Expand All @@ -840,7 +843,7 @@ public void SetData(GraphicsDevice graphicsDevice, Image image)
/// <param name="from">Source for the.</param>
public static implicit operator ShaderResourceView(Texture from)
{
return @from == null ? null : @from.shaderResourceViews != null ? @from.shaderResourceViews[0] : null;
return @from == null ? null : from.defaultShaderResourceView;
}

/// <summary>
Expand Down Expand Up @@ -1093,10 +1096,12 @@ protected override void OnPropertyChanged(string propertyName)
{
if (this.shaderResourceViews != null)
{
for (int i = 0; i < this.shaderResourceViews.Length; i++)
int i = 0;
foreach(var shaderResourceViewItem in shaderResourceViews)
{
var shaderResourceView = this.shaderResourceViews[i];
var shaderResourceView = shaderResourceViewItem.Value;
if (shaderResourceView != null) shaderResourceView.View.DebugName = Name == null ? null : String.Format("{0} SRV[{1}]", i, Name);
i++;
}
}

Expand Down Expand Up @@ -1199,5 +1204,57 @@ internal static BindFlags GetBindFlagsFromTextureFlags(TextureFlags flags)

return bindFlags;
}

internal struct TextureViewKey : IEquatable<TextureViewKey>
{
public TextureViewKey(Format viewFormat, ViewType viewType, int arrayOrDepthSlice, int mipIndex)
{
ViewFormat = viewFormat;
ViewType = viewType;
ArrayOrDepthSlice = arrayOrDepthSlice;
MipIndex = mipIndex;
}

public readonly DXGI.Format ViewFormat;

public readonly ViewType ViewType;

public readonly int ArrayOrDepthSlice;

public readonly int MipIndex;

public bool Equals(TextureViewKey other)
{
return ViewFormat == other.ViewFormat && ViewType == other.ViewType && ArrayOrDepthSlice == other.ArrayOrDepthSlice && MipIndex == other.MipIndex;
}

public override bool Equals(object obj)
{
if(ReferenceEquals(null, obj)) return false;
return obj is TextureViewKey && Equals((TextureViewKey)obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (int)ViewFormat;
hashCode = (hashCode * 397) ^ (int)ViewType;
hashCode = (hashCode * 397) ^ ArrayOrDepthSlice;
hashCode = (hashCode * 397) ^ MipIndex;
return hashCode;
}
}

public static bool operator ==(TextureViewKey left, TextureViewKey right)
{
return left.Equals(right);
}

public static bool operator !=(TextureViewKey left, TextureViewKey right)
{
return !left.Equals(right);
}
}
}
}
8 changes: 4 additions & 4 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Texture1D.cs
Expand Up @@ -129,7 +129,7 @@ public static Texture1D New(GraphicsDevice device, int width, PixelFormat format
/// <unmanaged-short>ID3D11Device::CreateTexture1D</unmanaged-short>
public static Texture1D New(GraphicsDevice device, int width, MipMapCount mipCount, PixelFormat format, TextureFlags flags = TextureFlags.ShaderResource, int arraySize = 1, ResourceUsage usage = ResourceUsage.Default)
{
return new Texture1D(device, NewDescription(width, format, flags | TextureFlags.ShaderResource, mipCount, arraySize, usage));
return new Texture1D(device, NewDescription(width, format, flags, mipCount, arraySize, usage));
}

/// <summary>
Expand All @@ -151,7 +151,7 @@ public static Texture1D New(GraphicsDevice device, int width, MipMapCount mipCou
/// </remarks>
public unsafe static Texture1D New<T>(GraphicsDevice device, int width, PixelFormat format, T[] textureData, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable) where T : struct
{
return new Texture1D(device, NewDescription(width, format, flags | TextureFlags.ShaderResource, 1, 1, usage), GetDataBox(format, width, 1, 1, textureData, (IntPtr)Interop.Fixed(textureData)));
return new Texture1D(device, NewDescription(width, format, flags, 1, 1, usage), GetDataBox(format, width, 1, 1, textureData, (IntPtr)Interop.Fixed(textureData)));
}

/// <summary>
Expand All @@ -171,7 +171,7 @@ public static Texture1D New(GraphicsDevice device, Image image, TextureFlags fla
if (image.Description.Dimension != TextureDimension.Texture1D)
throw new ArgumentException("Invalid image. Must be 1D", "image");

return new Texture1D(device, CreateTextureDescriptionFromImage(image, flags | TextureFlags.ShaderResource, usage), image.ToDataBox());
return new Texture1D(device, CreateTextureDescriptionFromImage(image, flags, usage), image.ToDataBox());
}

/// <summary>
Expand All @@ -185,7 +185,7 @@ public static Texture1D New(GraphicsDevice device, Image image, TextureFlags fla
/// <returns>A texture</returns>
public static new Texture1D Load(GraphicsDevice device, Stream stream, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable)
{
var texture = Texture.Load(device, stream, flags | TextureFlags.ShaderResource, usage);
var texture = Texture.Load(device, stream, flags, usage);
if (!(texture is Texture1D))
throw new ArgumentException(string.Format("Texture is not type of [Texture1D] but [{0}]", texture.GetType().Name));
return (Texture1D)texture;
Expand Down
22 changes: 14 additions & 8 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Texture1DBase.cs
Expand Up @@ -18,8 +18,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Collections.Generic;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;

namespace SharpDX.Toolkit.Graphics
{
Expand Down Expand Up @@ -86,7 +88,7 @@ public override Texture ToStaging()
return new Texture1D(this.GraphicsDevice, this.Description.ToStagingDescription());
}

internal override TextureView GetShaderResourceView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
internal override TextureView GetShaderResourceView(Format viewFormat, ViewType viewType, int arrayOrDepthSlice, int mipIndex)
{
if ((this.Description.BindFlags & BindFlags.ShaderResource) == 0)
return null;
Expand All @@ -95,14 +97,13 @@ internal override TextureView GetShaderResourceView(ViewType viewType, int array
int mipCount;
GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);

var srvIndex = GetViewIndex(viewType, arrayOrDepthSlice, mipIndex);
var textureViewKey = new TextureViewKey(viewFormat, viewType, arrayOrDepthSlice, mipIndex);

lock (this.shaderResourceViews)
{
var srv = this.shaderResourceViews[srvIndex];

TextureView srv;
// Creates the shader resource view
if (srv == null)
if (!shaderResourceViews.TryGetValue(textureViewKey, out srv))
{
// Create the view
var srvDescription = new ShaderResourceViewDescription() { Format = this.Description.Format };
Expand All @@ -125,7 +126,7 @@ internal override TextureView GetShaderResourceView(ViewType viewType, int array
}

srv = new TextureView(this, new ShaderResourceView(this.GraphicsDevice, this.Resource, srvDescription));
this.shaderResourceViews[srvIndex] = ToDispose(srv);
this.shaderResourceViews.Add(textureViewKey, ToDispose(srv));
}

return srv;
Expand Down Expand Up @@ -187,10 +188,15 @@ protected override void InitializeViews()
// Creates the shader resource view
if ((this.Description.BindFlags & BindFlags.ShaderResource) != 0)
{
this.shaderResourceViews = new TextureView[GetViewCount()];
shaderResourceViews = new Dictionary<TextureViewKey, TextureView>();

// Pre initialize by default the view on the first array/mipmap
GetShaderResourceView(ViewType.Full, 0, 0);
var viewFormat = Format;
if (!FormatHelper.IsTypeless(viewFormat))
{
// Only valid for non-typeless viewformat
defaultShaderResourceView = GetShaderResourceView(viewFormat, ViewType.Full, 0, 0);
}
}

// Creates the unordered access view
Expand Down
10 changes: 5 additions & 5 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Texture2D.cs
Expand Up @@ -128,7 +128,7 @@ public static Texture2D New(GraphicsDevice device, int width, int height, PixelF
/// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short>
public static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags flags = TextureFlags.ShaderResource, int arraySize = 1, ResourceUsage usage = ResourceUsage.Default)
{
return new Texture2D(device, NewDescription(width, height, format, flags | TextureFlags.ShaderResource, mipCount, arraySize, usage));
return new Texture2D(device, NewDescription(width, height, format, flags, mipCount, arraySize, usage));
}

/// <summary>
Expand Down Expand Up @@ -172,7 +172,7 @@ public static Texture2D New(GraphicsDevice device, int width, int height, MipMap
/// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short>
public static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags flags = TextureFlags.ShaderResource, int arraySize = 1, ResourceUsage usage = ResourceUsage.Default)
{
return new Texture2D(device, NewDescription(width, height, format, flags | TextureFlags.ShaderResource, mipCount, arraySize, usage), textureData);
return new Texture2D(device, NewDescription(width, height, format, flags, mipCount, arraySize, usage), textureData);
}

/// <summary>
Expand All @@ -192,7 +192,7 @@ public static Texture2D New(GraphicsDevice device, Image image, TextureFlags fla
if (image.Description.Dimension != TextureDimension.Texture2D)
throw new ArgumentException("Invalid image. Must be 2D", "image");

return new Texture2D(device, CreateTextureDescriptionFromImage(image, flags | TextureFlags.ShaderResource, usage), image.ToDataBox());
return new Texture2D(device, CreateTextureDescriptionFromImage(image, flags, usage), image.ToDataBox());
}

/// <summary>
Expand All @@ -206,7 +206,7 @@ public static Texture2D New(GraphicsDevice device, Image image, TextureFlags fla
/// <returns>A texture</returns>
public static new Texture2D Load(GraphicsDevice device, Stream stream, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable)
{
var texture = Texture.Load(device, stream, flags | TextureFlags.ShaderResource, usage);
var texture = Texture.Load(device, stream, flags, usage);
if (!(texture is Texture2D))
throw new ArgumentException(string.Format("Texture is not type of [Texture2D] but [{0}]", texture.GetType().Name));
return (Texture2D)texture;
Expand All @@ -224,7 +224,7 @@ public static new Texture2D Load(GraphicsDevice device, Stream stream, TextureFl
public static new Texture2D Load(GraphicsDevice device, string filePath, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable)
{
using (var stream = new NativeFileStream(filePath, NativeFileMode.Open, NativeFileAccess.Read))
return Load(device, stream, flags | TextureFlags.ShaderResource, usage);
return Load(device, stream, flags, usage);
}
}
}
24 changes: 15 additions & 9 deletions Source/Toolkit/SharpDX.Toolkit.Graphics/Texture2DBase.cs
Expand Up @@ -18,8 +18,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System.Collections.Generic;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;

namespace SharpDX.Toolkit.Graphics
{
Expand Down Expand Up @@ -88,7 +90,7 @@ protected virtual DXGI.Format GetDefaultViewFormat()
return this.Description.Format;
}

internal override TextureView GetShaderResourceView(ViewType viewType, int arrayOrDepthSlice, int mipIndex)
internal override TextureView GetShaderResourceView(Format viewFormat, ViewType viewType, int arrayOrDepthSlice, int mipIndex)
{
if ((this.Description.BindFlags & BindFlags.ShaderResource) == 0)
return null;
Expand All @@ -97,17 +99,16 @@ internal override TextureView GetShaderResourceView(ViewType viewType, int array
int mipCount;
GetViewSliceBounds(viewType, ref arrayOrDepthSlice, ref mipIndex, out arrayCount, out mipCount);

var srvIndex = GetViewIndex(viewType, arrayOrDepthSlice, mipIndex);
var textureViewKey = new TextureViewKey(viewFormat, viewType, arrayOrDepthSlice, mipIndex);

lock (this.shaderResourceViews)
{
var srv = this.shaderResourceViews[srvIndex];

TextureView srv;
// Creates the shader resource view
if (srv == null)
if (!shaderResourceViews.TryGetValue(textureViewKey, out srv))
{
// Create the view
var srvDescription = new ShaderResourceViewDescription() { Format = GetDefaultViewFormat() };
var srvDescription = new ShaderResourceViewDescription() { Format = viewFormat };

// Initialize for texture arrays or texture cube
if (this.Description.ArraySize > 1)
Expand Down Expand Up @@ -150,7 +151,7 @@ internal override TextureView GetShaderResourceView(ViewType viewType, int array
}

srv = new TextureView(this, new ShaderResourceView(this.GraphicsDevice, this.Resource, srvDescription));
this.shaderResourceViews[srvIndex] = ToDispose(srv);
this.shaderResourceViews.Add(textureViewKey, ToDispose(srv));
}

return srv;
Expand Down Expand Up @@ -213,10 +214,15 @@ protected override void InitializeViews()
// Creates the shader resource view
if ((this.Description.BindFlags & BindFlags.ShaderResource) != 0)
{
this.shaderResourceViews = new TextureView[GetViewCount()];
this.shaderResourceViews = new Dictionary<TextureViewKey, TextureView>();

// Pre initialize by default the view on the first array/mipmap
GetShaderResourceView(ViewType.Full, 0, 0);
var viewFormat = GetDefaultViewFormat();
if(!FormatHelper.IsTypeless(viewFormat))
{
// Only valid for non-typeless viewformat
defaultShaderResourceView = GetShaderResourceView(viewFormat, ViewType.Full, 0, 0);
}
}

// Creates the unordered access view
Expand Down

0 comments on commit 2a52b1f

Please sign in to comment.