Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualjsedlak

Posted 20 January 2012 - 03:48 AM

I'm working on a shader that utilizes a Texture2DArray<float4> variable, but am having trouble setting it up in the C# code. I have a list of textures I want to load into the array, but am not sure how to proceed with actually putting them in the resource. Right now I have the following.


Log.BeginSection("Level.LoadTextureArray");

int mipLevels = tilesets[0].Texture.Description.MipLevels;
SlimDX.DXGI.Format format = tilesets[0].Texture.Description.Format;

Log.WriteLine("Mip Levels: {0}", mipLevels);
Log.WriteLine("Format: {0}", format);

Texture2DDescription stagingDescription = new Texture2DDescription
{
ArraySize = tilesets.Count,
Width = 512,
Height = 512,
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Write,
Format = format,
Usage = ResourceUsage.Staging,
MipLevels = mipLevels,
SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0)
};

//TextureCache = new Texture2D(App.Device, desc);
var stagingTexture = new Texture2D(App.Device, stagingDescription);

Log.BeginSection("Tileset Loop");
for (int i = 0; i < tilesets.Count; i++)
{
Tileset tileset = tilesets[i];

Log.WriteLine("Loading tileset {1} ({0})", tileset.Filename, i);

tileset.EnsureAssets();

var box = App.Device.ImmediateContext.MapSubresource(stagingTexture, 0, i, MapMode.Write, MapFlags.None);
App.Device.ImmediateContext.UpdateSubresource(box, tileset.Texture, 0);
App.Device.ImmediateContext.UnmapSubresource(stagingTexture, 0);


tileset.DisposeAssets();
}
Log.EndSection("Tileset Loop");

var destinationDescription = new Texture2DDescription
{
ArraySize = tilesets.Count,
MipLevels = mipLevels,
Format = format,
Width = 512,
Height = 512,
Usage = ResourceUsage.Default,
BindFlags = SlimDX.Direct3D11.BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0)
};

TextureCache = new Texture2D(App.Device, destinationDescription);

App.Device.ImmediateContext.CopyResource(stagingTexture, TextureCache);

TextureCacheView = new ShaderResourceView(App.Device, TextureCache);

Log.EndSection("Level.LoadTextureArray");

This returns a "A first chance exception of type 'SlimDX.Direct3D11.Direct3D11Exception' occurred in SlimDX.dll" when I create the ShaderResourceView. Am I missing something, or doing something very wrong? TIA.

#2jsedlak

Posted 20 January 2012 - 03:47 AM

I'm working on a shader that utilizes a Texture2DArray<float4> variable, but am having trouble setting it up in the C# code. I have a list of textures I want to load into the array, but am not sure how to proceed with actually putting them in the resource. Right now I have the following.


Log.BeginSection("Level.LoadTextureArray");

int mipLevels = tilesets[0].Texture.Description.MipLevels;
SlimDX.DXGI.Format format = tilesets[0].Texture.Description.Format;

Log.WriteLine("Mip Levels: {0}", mipLevels);
Log.WriteLine("Format: {0}", format);

Texture2DDescription stagingDescription = new Texture2DDescription
{
ArraySize = tilesets.Count,
Width = 512,
Height = 512,
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Write,
Format = format,
Usage = ResourceUsage.Staging,
MipLevels = mipLevels,
SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0)
};

var stagingTexture = new Texture2D(App.Device, stagingDescription);

Log.BeginSection("Tileset Loop");
for (int i = 0; i < tilesets.Count; i++)
{
Tileset tileset = tilesets[i];

Log.WriteLine("Loading tileset {1} ({0})", tileset.Filename, i);

tileset.EnsureAssets();

var box = App.Device.ImmediateContext.MapSubresource(stagingTexture, 0, i, MapMode.Write, MapFlags.None);
App.Device.ImmediateContext.UpdateSubresource(box, tileset.Texture, 0);
App.Device.ImmediateContext.UnmapSubresource(stagingTexture, 0);

tileset.DisposeAssets();
}
Log.EndSection("Tileset Loop");

var destinationDescription = new Texture2DDescription
{
ArraySize = tilesets.Count,
MipLevels = mipLevels,
Format = format,
Width = 512,
Height = 512,
Usage = ResourceUsage.Default,
BindFlags = SlimDX.Direct3D11.BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0)
};

TextureCache = new Texture2D(App.Device, destinationDescription);
App.Device.ImmediateContext.CopyResource(stagingTexture, TextureCache);
TextureCacheView = new ShaderResourceView(App.Device, TextureCache);

Log.EndSection("Level.LoadTextureArray");

This returns a "A first chance exception of type 'SlimDX.Direct3D11.Direct3D11Exception' occurred in SlimDX.dll" when I create the ShaderResourceView. Am I missing something, or doing something very wrong? TIA.

#1jsedlak

Posted 19 January 2012 - 07:03 AM

I'm working on a shader that utilizes a Texture2DArray<float4> variable, but am having trouble setting it up in the C# code. I have a list of textures I want to load into the array, but am not sure how to proceed with actually putting them in the resource. Right now I have the following.


Texture2DDescription desc = new Texture2DDescription
{
    ArraySize = textures.Count,
    Width = 1024,
    Height = 1024,
    BindFlags = BindFlags.ShaderResource,
    CpuAccessFlags = CpuAccessFlags.Write | CpuAccessFlags.Read,
    Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
    OptionFlags = ResourceOptionFlags.None,
    Usage = ResourceUsage.Default,
    MipLevels = 1,
    SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0)
};
		   
TextureCache = new Texture2D(App.Device, desc);
int index = 0;
foreach (TextureData textureData in textureDataCollection)
{
    App.Device.ImmediateContext.CopySubresourceRegion(
	    textureData.Texture,
	    0,
	    new ResourceRegion { Front = 0, Back = 1, Top = 0, Left = 0, Bottom = textureData.Height, Right = textureData.Width },
	    TextureCache,
	    index,
	    0, 0, 0
    );
    index++;
}
TextureCacheView = new ShaderResourceView(App.Device, TextureCache, new ShaderResourceViewDescription
{
    Dimension = ShaderResourceViewDimension.Texture2DArray,
    ArraySize = textures.Count,
    MipLevels = 1,
    Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
    Flags = ShaderResourceViewExtendedBufferFlags.None
});

This returns a "A first chance exception of type 'SlimDX.Direct3D11.Direct3D11Exception' occurred in SlimDX.dll" when I create the ShaderResourceView. Am I missing something, or doing something very wrong? TIA.

PARTNERS