Пример #1
0
        public void DdsDirectXCubemapCompability()
        {
            // load cubemap
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "cubemap.dds");
            model.Apply();

            // export
            var desc = new ExportDescription(model.Pipelines[0].Image, ExportDir + "tmp", "dds");

            desc.FileFormat = GliFormat.RGBA8_SRGB;
            model.Export.Export(desc);

            // try loading with dds loader
            DDSTextureLoader.CreateDDSTextureFromFile(Device.Get().Handle, Device.Get().ContextHandle, ExportDir + "tmp.dds",
                                                      out var resource, out var resourceView, 0, out var alphaMode);

            Assert.AreEqual(ResourceDimension.Texture2D, resource.Dimension);
            Assert.IsTrue(resource is Texture2D);
            var tex2D = resource as Texture2D;

            Assert.AreEqual(model.Images.Size.Width, tex2D.Description.Width);
            Assert.AreEqual(model.Images.Size.Height, tex2D.Description.Height);
            Assert.AreEqual(model.Images.NumLayers, tex2D.Description.ArraySize);
            Assert.AreEqual(model.Images.NumMipmaps, tex2D.Description.MipLevels);
        }
Пример #2
0
        public static ShaderResourceView ShaderResourceViewFromFile(Device device, string filename)
        {
            ShaderResourceView srv;

            SharpDX.Direct3D11.Resource     texture;
            DDSTextureLoader.DDS_ALPHA_MODE alphaMode;

            if ((Path.GetExtension(filename) ?? "").ToLower() == ".dds")
            {
                DDSTextureLoader.CreateDDSTextureFromFile(device, device.ImmediateContext, filename, out texture, out srv, 0, out alphaMode);
                texture.Dispose();
            }
            else
            {
                using (var tex = CreateTexture2DFromFile(device, filename))
                {
                    tex.DebugName = filename;

                    srv = new ShaderResourceView(device, tex);

                    srv.DebugName = filename;
                }
            }

            return(srv);
        }
Пример #3
0
        public void Dds3DDirectXCompability()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers3d.dds");
            model.Apply();
            model.Export.Quality = 100;
            var origTex    = (Texture3D)model.Pipelines[0].Image;
            var origColors = origTex.GetPixelColors(0, 0);

            Color[] newColors = null;

            var    eFmt      = model.Export.Formats.First(fmt => fmt.Extension == "dds");
            string errors    = "";
            int    numErrors = 0;
            int    nFormats  = 0;

            foreach (var format in eFmt.Formats)
            {
                if (format.IsExcludedFrom3DExport())
                {
                    continue;
                }
                nFormats++;
                try
                {
                    // export to dds
                    var desc = new ExportDescription(ExportDir + "tmp", "dds", model.Export);
                    desc.FileFormat = format;
                    model.Export.Export(origTex, desc);

                    // load with directx dds loader
                    DDSTextureLoader.CreateDDSTextureFromFile(Device.Get().Handle, Device.Get().ContextHandle, ExportDir + "tmp.dds",
                                                              out var resource, out var resourceView, 0, out var alphaMode);

                    // convert to obtain color data
                    using (var newTex = model.Export.convert.ConvertFromRaw3D(resourceView, origTex.Size, Format.R32G32B32A32_Float))
                    {
                        resourceView.Dispose();
                        resource.Dispose();

                        newColors = newTex.GetPixelColors(0, 0);
                        // only compare with red channel since some formats only store red
                        TestData.CompareColors(origColors, newColors, Color.Channel.R, 0.1f);
                    }
                }
                catch (Exception e)
                {
                    errors += $"{format}: {e.Message}\n";
                    ++numErrors;
                }
            }

            if (errors.Length > 0)
            {
                throw new Exception($"directX compability failed for {numErrors}/{nFormats} formats:\n" + errors);
            }
        }
Пример #4
0
        public static ShaderResourceView LoadTexture(Device d3d, DeviceContext d3dContext, string fileName)
        {
            Resource           ddsResource;
            ShaderResourceView _temp;

            DDSTextureLoader.DDS_ALPHA_MODE mode;
            string texturePath = GetTextureFromPath(fileName);

            DDSTextureLoader.CreateDDSTextureFromFile(d3d, d3dContext, texturePath, out ddsResource, out _temp, 4096, out mode);
            return(_temp);
        }
Пример #5
0
 public static ID3D11ShaderResourceView LoadTexture(ID3D11Device d3d, ID3D11DeviceContext d3dContext, string fileName)
 {
     try
     {
         ID3D11Resource                  ddsResource;
         ID3D11ShaderResourceView        _temp;
         DDSTextureLoader.DDS_ALPHA_MODE mode;
         string texturePath = GetTextureFromPath(fileName);
         DDSTextureLoader.CreateDDSTextureFromFile(d3d, d3dContext, texturePath, out ddsResource, out _temp, 4096, out mode);
         return(_temp);
     }
     catch
     {
         Log.WriteLine(string.Format("Failed to load file: {0}", fileName), LoggingTypes.FATAL, LogCategoryTypes.IO);
         return(null);
     }
 }
Пример #6
0
        public void Dds3DDirectXCompability()
        {
            var model = new Models(1);

            model.AddImageFromFile(TestData.Directory + "checkers3d.dds");
            model.Apply();
            var origTex    = (Texture3D)model.Pipelines[0].Image;
            var origColors = origTex.GetPixelColors(LayerMipmapSlice.Mip0);

            Color[] newColors = null;

            // colors multiplied by 100 for integer precision formats
            model.Pipelines[0].Color.Formula = "I0 * 100";
            model.Apply();
            var integerTex    = (Texture3D)model.Pipelines[0].Image;
            var origColors100 = integerTex.GetPixelColors(LayerMipmapSlice.Mip0);

            var    eFmt      = ExportDescription.GetExportFormat("dds");
            string errors    = "";
            int    numErrors = 0;
            int    nFormats  = 0;

            foreach (var format in eFmt.Formats)
            {
                if (format.IsExcludedFrom3DExport())
                {
                    continue;
                }
                nFormats++;
                try
                {
                    // export to dds
                    var isIntegerPrecision = IsIntegerPrecisionFormat(format);
                    var desc = new ExportDescription(origTex, ExportDir + "tmp", "dds");
                    desc.FileFormat = format;
                    desc.Quality    = 100;
                    if (isIntegerPrecision)
                    {
                        desc.Multiplier = 100.0f;
                    }
                    model.Export.Export(desc);

                    // load with directx dds loader
                    DDSTextureLoader.CreateDDSTextureFromFile(Device.Get().Handle, Device.Get().ContextHandle, ExportDir + "tmp.dds",
                                                              out var resource, out var resourceView, 0, out var alphaMode);

                    // convert to obtain color data
                    using (var newTex = model.SharedModel.Convert.ConvertFromRaw3D(resourceView, origTex.Size, Format.R32G32B32A32_Float, isIntegerPrecision))
                    {
                        resourceView.Dispose();
                        resource.Dispose();

                        newColors = newTex.GetPixelColors(LayerMipmapSlice.Mip0);
                        // only compare with red channel since some formats only store red
                        if (isIntegerPrecision)
                        {
                            TestData.CompareColors(origColors100, newColors, Color.Channel.R, 1.0f);
                        }
                        else
                        {
                            TestData.CompareColors(origColors, newColors, Color.Channel.R, 0.1f);
                        }
                    }
                }
                catch (Exception e)
                {
                    errors += $"{format}: {e.Message}\n";
                    ++numErrors;
                }
            }

            if (errors.Length > 0)
            {
                throw new Exception($"directX compability failed for {numErrors}/{nFormats} formats:\n" + errors);
            }
        }