Example #1
0
 public void Init(string name, MyFileTextureEnum type, bool waitTillLoaded, bool skipQualityReduction)
 {
     m_name                 = name;
     m_type                 = type;
     TextureState           = waitTillLoaded ? FileTextureState.Unloaded : FileTextureState.Requested;
     m_skipQualityReduction = skipQualityReduction;
 }
Example #2
0
 public void Init(string name, string localPath, MyFileTextureEnum type, bool waitTillLoaded, bool skipQualityReduction)
 {
     Debug.Assert(System.IO.Path.IsPathRooted(localPath), "Path must be rooted");
     m_name                 = name;
     m_path                 = System.IO.Path.GetFullPath(localPath);
     m_type                 = type;
     TextureState           = waitTillLoaded ? FileTextureState.Unloaded : FileTextureState.Requested;
     m_skipQualityReduction = skipQualityReduction;
 }
Example #3
0
 public void Destroy()
 {
     TextureState = FileTextureState.Unloaded;
     if (m_ownsData)
     {
         m_srv.Dispose();
         m_resource.Dispose();
     }
     m_srv      = null;
     m_resource = null;
 }
Example #4
0
            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;
            }
Example #5
0
 public void Destroy()
 {
     TextureState = FileTextureState.Unloaded;
     if (m_ownsData)
     {
         m_srv.Dispose();
         m_resource.Dispose();
     }
     m_srv = null;
     m_resource = null;
 }
Example #6
0
            public unsafe void Load()
            {
                if (TextureState == FileTextureState.Loaded)
                    return;

                Debug.Assert(m_resource == null);
                Debug.Assert(m_srv == null, "Texture " + Name + " in invalid state");

                Image img = null;

                if (MyFileSystem.FileExists(m_path))
                {
                    try
                    {
                        using (var s = MyFileSystem.OpenRead(m_path))
                        {
                            img = Image.Load(s);
                            ImageFormatInFile = img.Description.Format;
                        }
                    }
                    catch (Exception e)
                    {
                        MyRender11.Log.WriteLine("Error while loading texture: " + m_path + ", exception: " + e);
                    }
                }

                bool loaded = false;
                if (img != null)
                {
                    int skipMipmaps = 0;
                    if (m_type != MyFileTextureEnum.GUI && m_type != MyFileTextureEnum.GPUPARTICLES && img.Description.MipLevels > 1)
                        skipMipmaps = MyRender11.RenderSettings.TextureQuality.MipmapsToSkip(img.Description.Width, img.Description.Height);

                    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];

                    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 };

                            void* data = pixels.DataPointer.ToPointer();

                            lastSize = pixels.BufferStride;
                            totalSize += lastSize;
                        }
                    }

                    var targetWidth = img.Description.Width >> skipMipmaps;
                    var targetHeight = img.Description.Height >> skipMipmaps;


                    bool overwriteFormatToSrgb = false;
                    if (MyCompilationSymbols.ReinterpretFormatsStoredInFiles)
                        overwriteFormatToSrgb = (m_type != MyFileTextureEnum.NORMALMAP_GLOSS) && !FormatHelper.IsSRgb(img.Description.Format);

                    m_format = overwriteFormatToSrgb ? MyResourceUtils.MakeSrgb(img.Description.Format) : img.Description.Format;

                    var desc = new Texture2DDescription
                    {
                        MipLevels = targetMipmaps,
                        Format = m_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_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: " + m_path);

                    m_srv = replacingTexture.Srv;
                    m_resource = replacingTexture.Resource;
                    m_size = replacingTexture.Size;
                    m_ownsData = false;
                    m_byteSize = 0;
                    MyRender11.Log.WriteLine("Missing or invalid texture: " + Name);
                }

                TextureState = FileTextureState.Loaded;
            }
Example #7
0
 public void Init(string name, string localPath, MyFileTextureEnum type, bool waitTillLoaded, bool skipQualityReduction)
 {
     Debug.Assert(System.IO.Path.IsPathRooted(localPath), "Path must be rooted");
     m_name = name;
     m_path = System.IO.Path.GetFullPath(localPath);
     m_type = type;
     TextureState = waitTillLoaded ? FileTextureState.Unloaded : FileTextureState.Requested;
     m_skipQualityReduction = skipQualityReduction;
 }
Example #8
0
 public void Init(string name, MyFileTextureEnum type, bool waitTillLoaded, bool skipQualityReduction)
 {
     m_name = name;
     m_type = type;
     TextureState = waitTillLoaded ? FileTextureState.Unloaded : FileTextureState.Requested;
     m_skipQualityReduction = skipQualityReduction;
 }