Exemple #1
0
            public byte[] LoadTexture(string textureName, ReadAheadBinaryReader reader, int textureSize)
            {
                if (textureSize > 4e6)
                {
                    AcToolsLogging.Write($"{textureName}: {(double)textureSize / 1024 / 1024:F1} MB");
                }

                MemoryChunk.Bytes(textureSize).Execute(() => {
                    var bytes = reader.ReadBytes(textureSize);

                    // FromStream simply reads Stream to byte[] underneath, so we could just do it here in
                    // a more controlled manner

                    try {
                        lock (_device) {
                            if (OptionLoadView)
                            {
                                var view            = ShaderResourceView.FromMemory(_device, bytes); // new ShaderResourceView(_device, texture);
                                _ready[textureName] = new Tuple <Texture2D, ShaderResourceView>(null, view);
                            }
                            else
                            {
                                var texture         = Texture2D.FromMemory(_device, bytes);
                                var view            = new ShaderResourceView(_device, texture);
                                _ready[textureName] = new Tuple <Texture2D, ShaderResourceView>(texture, view);
                            }
                        }
                    } catch (SEHException e) {
                        AcToolsLogging.NonFatalErrorNotify("Can’t load texture", "Try again?", e);
                    }
                });

                return(null);
            }
Exemple #2
0
 public static void SaveAsDds([NotNull] Stream stream, [NotNull] byte[] imageData, PreferredDdsFormat format, [CanBeNull] IProgress <double> progress)
 {
     try {
         SaveAsDds_Compressor(stream, imageData, format, progress);
     } catch (Exception e) {
         AcToolsLogging.Write(e);
         try {
             SaveAsDds_Magick(stream, imageData, format);
             AcToolsLogging.NonFatalErrorNotify("Can’t encode DDS texture using NVIDIA Texture Tools", null, e);
         } catch (Exception ex) {
             AcToolsLogging.NonFatalErrorNotify("Can’t encode DDS texture using NVIDIA Texture Tools or Magick.NET", null, ex);
         }
     }
 }
        private SourceReady GetOriginal(ref Dictionary <int, ShaderResourceView> storage, [NotNull] PaintShopSource source, int maxSize,
                                        Func <ShaderResourceView, ShaderResourceView> preparation = null)
        {
            if (MainSlot.Kn5 == null)
            {
                return(null);
            }

            try {
                if (storage == null)
                {
                    storage = new Dictionary <int, ShaderResourceView>(2);
                    if (_sizes == null)
                    {
                        _sizes = new Dictionary <int, Size>();
                    }
                }

                ShaderResourceView original;
                var sourceHashCode = source.GetHashCode();
                var hashCode       = (sourceHashCode * 397) ^ maxSize.GetHashCode();
                if (!storage.TryGetValue(hashCode, out original))
                {
                    Size size;

                    if (source.ByChannels)
                    {
                        var red   = source.RedChannelSource == null ? null : GetOriginal(ref storage, source.RedChannelSource, maxSize);
                        var green = source.GreenChannelSource == null ? null : GetOriginal(ref storage, source.GreenChannelSource, maxSize);
                        var blue  = source.BlueChannelSource == null ? null : GetOriginal(ref storage, source.BlueChannelSource, maxSize);
                        var alpha = source.AlphaChannelSource == null ? null : GetOriginal(ref storage, source.AlphaChannelSource, maxSize);

                        var redSize   = source.RedChannelSource == null ? null : GetSize(source.RedChannelSource);
                        var greenSize = source.GreenChannelSource == null ? null : GetSize(source.GreenChannelSource);
                        var blueSize  = source.BlueChannelSource == null ? null : GetSize(source.BlueChannelSource);
                        var alphaSize = source.AlphaChannelSource == null ? null : GetSize(source.AlphaChannelSource);

                        size = Max(redSize, Max(greenSize, Max(blueSize, alphaSize))) ?? new Size(16, 16);
                        _sizes[sourceHashCode] = size;

                        if (size.Width > maxSize || size.Height > maxSize)
                        {
                            size = new Size(maxSize, maxSize);
                        }

                        using (var combined = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                            combined.Resize(DeviceContextHolder, size.Width, size.Height, null);
                            UseEffect(e => {
                                red.Set(e.FxAoMap, e.FxAoMapChannels);
                                green.Set(e.FxInputMap, e.FxInputMapChannels);
                                blue.Set(e.FxMaskMap, e.FxMaskMapChannels);
                                alpha.Set(e.FxOverlayMap, e.FxOverlayMapChannels);
                                e.TechCombineChannels.DrawAllPasses(DeviceContext, 6);
                            }, combined);

                            combined.KeepView = true;
                            original          = combined.View;
                        }
                    }
                    else
                    {
                        var decoded = GetBytes(source);
                        if (decoded == null)
                        {
                            return(null);
                        }

                        using (var texture = Texture2D.FromMemory(Device, decoded)) {
                            original = new ShaderResourceView(Device, texture);

                            size = new Size(texture.Description.Width, texture.Description.Height);
                            _sizes[sourceHashCode] = size;

                            if (size.Width > maxSize || size.Height > maxSize)
                            {
                                size = new Size(maxSize, maxSize);

                                using (var resized = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm)) {
                                    resized.Resize(DeviceContextHolder, maxSize, maxSize, null);
                                    DeviceContextHolder.GetHelper <DownsampleHelper>().Draw(DeviceContextHolder,
                                                                                            original, new Vector2(texture.Description.Width, texture.Description.Height),
                                                                                            resized.TargetView, new Vector2(maxSize, maxSize));
                                    original.Dispose();

                                    resized.KeepView = true;
                                    original         = resized.View;
                                }
                            }
                        }
                    }

                    if (source.Desaturate)
                    {
                        original = Prepare(original, view => Desaturate(view, size));
                    }

                    if (source.NormalizeMax)
                    {
                        original = Prepare(original, view => NormalizeMax(view, size));
                    }

                    storage[hashCode] = Prepare(original, preparation);
                }

                return(new SourceReady {
                    View = original,
                    ChannelsAssignments = GetChannelAssignments(source)
                });
            } catch (Exception e) {
                AcToolsLogging.NonFatalErrorNotify("Can’t load texture", null, e);
                return(null);
            }
        }