public void OnUnloadData() { MyFileArrayTexture[] array = new MyFileArrayTexture[m_texturesOnAutoDisposal.Count]; m_texturesOnAutoDisposal.CopyTo(array); foreach (var myTex in array) { IFileArrayTexture tex = myTex; DisposeTex(ref tex); } m_texturesOnAutoDisposal.Clear(); }
public void DisposeTex(ref IFileArrayTexture texture) { if (texture == null) { return; } MyFileArrayTexture textureInternal = (MyFileArrayTexture)texture; if (m_isDeviceInit) { textureInternal.OnDeviceEnd(); } m_fileTextureArrays.Deallocate(textureInternal); texture = null; }
public void DisposeTex(ref IFileArrayTexture texture) { if (texture == null) { return; } MyFileArrayTexture textureInternal = (MyFileArrayTexture)texture; if (m_isDeviceInit) { textureInternal.OnDeviceEnd(); } if (m_texturesOnAutoDisposal.Contains(textureInternal)) { m_texturesOnAutoDisposal.Remove(textureInternal); } m_fileTextureArrays.Deallocate(textureInternal); texture = null; }
// if texture cannot be loaded, it will be used byte pattern to generate substition texture public IFileArrayTexture CreateFromFiles(string resourceName, string[] inputFiles, MyFileTextureEnum type, byte[] bytePatternFor4x4, Format formatBytePattern, bool autoDisposeOnUnload, MyFileArrayTexture source = null) { MyFileArrayTexture array; m_fileTextureArrays.AllocateOrCreate(out array); array.Load(resourceName, inputFiles, type, bytePatternFor4x4, formatBytePattern); if (m_isDeviceInit) { array.OnDeviceInit(source); } if (autoDisposeOnUnload) { m_texturesOnAutoDisposal.Add(array); } m_statistics.Add(array); return(array); }
private static IResource GetResource(string filepath, MyFileTextureEnum type, MyFileArrayTexture referenceArray, int referenceSlice, Vector2I textureSize, out Texture2DDescription description, out int sourceSlice) { if (referenceArray != null && referenceSlice < referenceArray.Size3.Z && referenceArray.Size == textureSize && filepath == referenceArray.SubresourceFilenames[referenceSlice]) { var tex2d = referenceArray.Resource as Texture2D; description = tex2d.Description; sourceSlice = referenceSlice; return(referenceArray); } else { var texture = MyManagers.FileTextures.GetTexture(filepath, type, true, temporary: true); var tex2d = texture.Resource as Texture2D; if (tex2d == null) { var tex2D = texture.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null, "MyFileArrayTexture supports only 2D textures. Inconsistent texture: " + filepath); description = new Texture2DDescription(); sourceSlice = -1; return(null); } description = tex2d.Description; sourceSlice = 0; return(texture); } }
public void OnDeviceInit(MyFileArrayTexture source = null) { MyFileTextureParams fileTexParams; bool ret = GetCorrectedFileTextureParams(out fileTexParams); //MyRenderProxy.Assert(ret, "It is not implemented mechanism, what to do, when none of the textures exist"); m_size = fileTexParams.Resolution; Texture2DDescription desc = new Texture2DDescription(); desc.ArraySize = m_listSubresourceFilenames.Count; desc.BindFlags = BindFlags.ShaderResource; desc.CpuAccessFlags = CpuAccessFlags.None; desc.Format = fileTexParams.Format == Format.Unknown ? Format.BC1_UNorm_SRgb : fileTexParams.Format; desc.Height = (int)Size.Y; desc.Width = (int)Size.X; var mipmaps = desc.MipLevels = fileTexParams.Mipmaps; desc.SampleDescription.Count = 1; desc.SampleDescription.Quality = 0; desc.Usage = ResourceUsage.Default; m_resource = new Texture2D(MyRender11.Device, desc); m_resource.DebugName = m_resourceName; Format = desc.Format; MipmapCount = fileTexParams.Mipmaps; // foreach mip int i = 0; foreach (var path in m_listSubresourceFilenames) { Texture2DDescription description; int sourceSlice; IResource resource = GetResource(path, m_type, source, i, new Vector2I(desc.Width, desc.Height), out description, out sourceSlice); bool consistent = MyResourceUtils.CheckTexturesConsistency(desc, description); if (!consistent) { if (!string.IsNullOrEmpty(path) && MyFileSystem.FileExists(path)) { string msg = string.Format( "Texture {0} cannot be loaded. If this message is displayed on reloading textures, please restart the game. If it is not, please notify developers.", path); MyRenderProxy.Fail(msg); } } if (!consistent && m_recoverySystem.UseErrorTexture) // if the texture cannot be used, error texture will be used { var texture = MyManagers.FileTextures.GetTexture(m_recoverySystem.TextureFilepath, m_type, true, temporary: true); var tex2D = texture.Resource as Texture2D; MyRenderProxy.Assert(tex2D != null, "MyFileArrayTexture supports only 2D textures. Inconsistent texture: " + m_recoverySystem.TextureFilepath); description = tex2D.Description; sourceSlice = 0; consistent = MyResourceUtils.CheckTexturesConsistency(desc, description); } IGeneratedTexture generatedTexture = null; if (!consistent && m_recoverySystem.UseBytePattern) // if the texture cannot be used, byte pattern will be used to generate texture { generatedTexture = MyManagers.GeneratedTextures.CreateFromBytePattern("MyFileArrayTexture.Tmp", desc.Width, desc.Height, m_recoverySystem.FormatBytePattern, m_recoverySystem.BytePattern); resource = generatedTexture; var tex2D = generatedTexture.Resource as Texture2D; description = tex2D.Description; sourceSlice = 0; MyRenderProxy.Assert(tex2D != null, "MyFileArrayTexture supports only 2D textures"); consistent = MyResourceUtils.CheckTexturesConsistency(desc, description); } if (!consistent) { Texture2DDescription desc1 = desc; Texture2DDescription desc2 = description; string errorMsg = string.Format("Textures ({0}) is not compatible within array texture! Width: ({1},{2}) Height: ({3},{4}) Mipmaps: ({5},{6}) Format: ({7},{8})", path, desc1.Width, desc2.Width, desc1.Height, desc2.Height, desc1.MipLevels, desc2.MipLevels, desc1.Format, desc2.Format); MyRenderProxy.Error(errorMsg); MyRender11.Log.WriteLine(errorMsg); } if (consistent) { for (int m = 0; m < mipmaps; m++) { MyRender11.RC.CopySubresourceRegion(resource, Resource.CalculateSubResourceIndex(m, sourceSlice, mipmaps), null, Resource, Resource.CalculateSubResourceIndex(m, i, mipmaps)); int sizeX = Resource.CalculateMipSize(m, Size.X); int sizeY = Resource.CalculateMipSize(m, Size.Y); ByteSize += FormatHelper.ComputeScanlineCount(Format, sizeX) * 4 * FormatHelper.ComputeScanlineCount(Format, sizeY) * 4 * FormatHelper.SizeOfInBytes(Format); } } if (generatedTexture != null) { MyManagers.GeneratedTextures.DisposeTex(generatedTexture); } i++; } m_srv = new ShaderResourceView(MyRender11.Device, Resource); }