public void Add(TextureSection textureSection) { Texture texture = null; CubeTexture cube = null; try { TextureHolder texHolder = new TextureHolder(); int texWidth = textureSection.Gtex.Header.Width; int texHeight = textureSection.Gtex.Header.Height; if (textureSection.Gtex.Header.IsCubeMap) { cube = new CubeTexture(device, texWidth, 0, Usage.None, textureSection.Gtex.Format, Pool.Managed); texHolder.Texture = cube; texHolder.Name = textureSection.ResourceId; texHolder.Gtex = textureSection.Gtex; CubeMapFace[] faces = new CubeMapFace[] { CubeMapFace.NegativeX, CubeMapFace.NegativeY, CubeMapFace.NegativeZ, CubeMapFace.PositiveX, CubeMapFace.PositiveY, CubeMapFace.PositiveZ }; for (var i = 0; i < 6; i++) { var texData = cube.LockRectangle(faces[i], 0, LockFlags.None); texData.Data.Write(texHolder.Gtex.TextureData[i], 0, texHolder.Gtex.TextureData[i].Length); cube.UnlockRectangle(faces[i], 0); } } else { int numMipMaps = textureSection.Gtex.Header.MipMapCount; Format format = textureSection.Gtex.Format; texture = new Texture(device, texWidth, texHeight, numMipMaps, Usage.None, format, Pool.Managed); texHolder.Texture = texture; texHolder.Name = textureSection.ResourceId; texHolder.Gtex = textureSection.Gtex; for (var i = 0; i < numMipMaps; i++) { var texData = texture.LockRectangle(i, LockFlags.None); texData.Data.Write(texHolder.Gtex.TextureData[i], 0, texHolder.Gtex.TextureData[i].Length); texture.UnlockRectangle(i); } } TextureHolder removeMe; if (this.textureLookup.TryGetValue(texHolder.Name, out removeMe)) { removeMe.Texture.Dispose(); this.textures.Remove(removeMe); this.textureLookup.Remove(texHolder.Name); } this.textures.Add(texHolder); this.textureLookup.Add(texHolder.Name, texHolder); } catch { if (texture != null) { texture.Dispose(); } if (cube != null) { cube.Dispose(); } throw; } }