IDynamicFileArrayTexture GetArrayTextureFromFilepath(string filepath, MyChannel channel, Vector2I defaultResolution) { MyFileTextureParams textureParams; MyArrayTextureKey key; key.Channel = channel; bool isLoaded = MyFileTextureParamsManager.LoadFromFile(filepath, out textureParams); if (isLoaded) { Format format; format = textureParams.Format; if (MyCompilationSymbols.ReinterpretFormatsStoredInFiles) { if (channel != MyChannel.NormalGloss) { format = MyResourceUtils.MakeSrgb(format); } } key.Format = format; key.ResolutionInFile = textureParams.Resolution; key.MipmapsCount = textureParams.Mipmaps; } else { Format format; switch (channel) { case MyChannel.ColorMetal: format = Format.BC7_UNorm_SRgb; break; case MyChannel.NormalGloss: format = Format.BC7_UNorm; break; case MyChannel.Extension: format = Format.BC7_UNorm_SRgb; break; case MyChannel.Alphamask: format = Format.BC4_UNorm; break; default: MyRenderProxy.Assert(false); format = Format.Unknown; break; } key.Format = format; key.ResolutionInFile = defaultResolution; key.MipmapsCount = MyResourceUtils.GetMipmapsCount(Math.Max(key.ResolutionInFile.X, key.ResolutionInFile.Y)); } return(GetArrayTextureFromKey(key)); }
// if no file texture can be loaded, the function will return false and default value in parameters bool GetCorrectedFileTextureParams(out MyFileTextureParams parameters) { //parameters = new MyFileTextureParams(); foreach (string filepath in m_listSubresourceFilenames) { if (MyFileTextureParamsManager.LoadFromFile(filepath, out parameters)) { if (MyCompilationSymbols.ReinterpretFormatsStoredInFiles) { if (m_type != MyFileTextureEnum.NORMALMAP_GLOSS) { parameters.Format = MyResourceUtils.MakeSrgb(parameters.Format); } } int skipMipmaps = 0; if (m_type != MyFileTextureEnum.GUI && m_type != MyFileTextureEnum.GPUPARTICLES) { skipMipmaps = MyRender11.RenderSettings.TextureQuality.MipmapsToSkip(parameters.Resolution.X, parameters.Resolution.Y); } if (parameters.Mipmaps > 1) { parameters.Mipmaps -= skipMipmaps; parameters.Resolution.X = MyResourceUtils.GetMipmapSize(parameters.Resolution.X, skipMipmaps); parameters.Resolution.Y = MyResourceUtils.GetMipmapSize(parameters.Resolution.Y, skipMipmaps); } return(true); } } parameters.Format = m_recoverySystem.FormatBytePattern; parameters.Mipmaps = 3; parameters.Resolution = new Vector2I(4, 4); parameters.ArraySize = 1; return(false); }
public void Load() { if (TextureState == FileTextureState.Loaded) { return; } string path = Path.Combine(MyFileSystem.ContentPath, Name); Debug.Assert(m_resource == null); Debug.Assert(m_srv == null, "Texture " + Name + " in invalid state"); Image img = null; if (MyFileSystem.FileExists(path)) { try { using (var s = MyFileSystem.OpenRead(path)) { img = Image.Load(s); m_imageFormatInFile = img.Description.Format; } } catch (Exception e) { MyRender11.Log.WriteLine("Error while loading texture: " + path + ", exception: " + e); } } bool loaded = false; if (img != null) { int skipMipmaps = (m_type != MyFileTextureEnum.GUI && m_type != MyFileTextureEnum.GPUPARTICLES && img.Description.MipLevels > 1) ? MyRender11.RenderSettings.TextureQuality.MipmapsToSkip(img.Description.Width, img.Description.Height) : 0; if (m_skipQualityReduction) { skipMipmaps = 0; } int totalSize = 0; int targetMipmaps = img.Description.MipLevels - skipMipmaps; var mipmapsData = new DataBox[(img.Description.MipLevels - skipMipmaps) * img.Description.ArraySize]; long delta = 0; int lastSize = 0; for (int z = 0; z < img.Description.ArraySize; z++) { for (int i = 0; i < targetMipmaps; i++) { var pixels = img.GetPixelBuffer(z, i + skipMipmaps); mipmapsData[Resource.CalculateSubResourceIndex(i, z, targetMipmaps)] = new DataBox { DataPointer = pixels.DataPointer, RowPitch = pixels.RowStride }; delta = pixels.DataPointer.ToInt64() - img.DataPointer.ToInt64(); lastSize = pixels.BufferStride; totalSize += lastSize; } } var targetWidth = img.Description.Width >> skipMipmaps; var targetHeight = img.Description.Height >> skipMipmaps; bool overwriteFormatToSrgb = (m_type != MyFileTextureEnum.NORMALMAP_GLOSS) && !FormatHelper.IsSRgb(img.Description.Format); var desc = new Texture2DDescription { MipLevels = targetMipmaps, Format = overwriteFormatToSrgb ? MyResourceUtils.MakeSrgb(img.Description.Format) : img.Description.Format, Height = targetHeight, Width = targetWidth, ArraySize = img.Description.ArraySize, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, Usage = ResourceUsage.Immutable, SampleDescription = new SampleDescription { Count = 1, Quality = 0 }, OptionFlags = img.Description.Dimension == TextureDimension.TextureCube ? ResourceOptionFlags.TextureCube : ResourceOptionFlags.None }; try { m_resource = new Texture2D(MyRender11.Device, desc, mipmapsData); m_size = new Vector2I(targetWidth, targetHeight); //m_skippedMipmaps = skipMipmaps; m_fileExists = true; m_byteSize = totalSize; m_ownsData = true; m_srv = new ShaderResourceView(MyRender11.Device, m_resource); m_resource.DebugName = m_name; m_srv.DebugName = m_name; img.Dispose(); loaded = true; } catch (SharpDXException) { img.Dispose(); } } if (!loaded) { ISrvBindable replacingTexture = MyGeneratedTextureManager.ZeroTex; switch (m_type) { case MyFileTextureEnum.NORMALMAP_GLOSS: replacingTexture = MyGeneratedTextureManager.MissingNormalGlossTex; break; case MyFileTextureEnum.EXTENSIONS: replacingTexture = MyGeneratedTextureManager.MissingExtensionTex; break; case MyFileTextureEnum.ALPHAMASK: replacingTexture = MyGeneratedTextureManager.MissingAlphamaskTex; break; case MyFileTextureEnum.CUBEMAP: replacingTexture = MyGeneratedTextureManager.MissingCubeTex; break; } MyRender11.Log.WriteLine("Could not load texture: " + path); m_srv = replacingTexture.Srv; m_resource = replacingTexture.Resource; m_size = replacingTexture.Size; m_ownsData = false; m_fileExists = false; m_byteSize = 0; MyRender11.Log.WriteLine("Missing or invalid texture: " + Name); } TextureState = FileTextureState.Loaded; }