Beispiel #1
0
 private Texture BuildTexture(byte[] imageData, SlimDX.Direct3D9.Format imageFormat, int width, int height)
 {
     if (imageFormat == SlimDX.Direct3D9.Format.R8G8B8)
     {
         MemoryStream memoryStream;
         using (memoryStream = new MemoryStream())
         {
             Tex.makeRGB8(imageData, width, height).Save((Stream)memoryStream, ImageFormat.Bmp);
             memoryStream.Write(imageData, 0, imageData.Length);
             memoryStream.Position = 0L;
             return(Texture.FromStream(_Device, (Stream)memoryStream, width, height, 0, Usage.SoftwareProcessing, SlimDX.Direct3D9.Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, 0));
         }
     }
     else if (imageFormat == SlimDX.Direct3D9.Format.A8R8G8B8)
     {
         MemoryStream memoryStream;
         using (memoryStream = new MemoryStream())
         {
             Tex.makeRGB(imageData, width, height).Save((Stream)memoryStream, ImageFormat.Bmp);
             memoryStream.Write(imageData, 0, imageData.Length);
             memoryStream.Position = 0L;
             return(Texture.FromStream(_Device, (Stream)memoryStream, width, height, 0, Usage.SoftwareProcessing, SlimDX.Direct3D9.Format.A8B8G8R8, Pool.Default, Filter.None, Filter.None, 0));
         }
     }
     else
     {
         Texture texture = new Texture(_Device, width, height, 0, Usage.None, imageFormat, Pool.Managed);
         using (Stream data = (Stream)texture.LockRectangle(0, LockFlags.None).Data)
         {
             data.Write(imageData, 0, ((IEnumerable <byte>)imageData).Count <byte>());
             texture.UnlockRectangle(0);
         }
         return(texture);
     }
 }
Beispiel #2
0
        private Texture GetTextureFromFile(string FileName)
        {
            Texture texture = null;

            if (File.Exists(FileName))
            {
                Tex.ReadFile(FileName);
                SlimDX.Direct3D9.Format imageFormat = ConvFormat(Tex.GetFormat());
                texture = BuildTexture(Tex.lcTex.imageData[0], imageFormat, (int)Tex.lcTex.Header.Width, (int)Tex.lcTex.Header.Height);
            }
            return(texture);
        }
Beispiel #3
0
        private SlimDX.Direct3D9.Format ConvFormat(texFormat tFormat)
        {
            SlimDX.Direct3D9.Format format = SlimDX.Direct3D9.Format.Unknown;
            switch (tFormat)
            {
            case texFormat.RGB:
                return(SlimDX.Direct3D9.Format.R8G8B8);

            case texFormat.ARGB:
                return(SlimDX.Direct3D9.Format.A8R8G8B8);

            case texFormat.DXT1:
                return(SlimDX.Direct3D9.Format.Dxt1);

            case texFormat.DXT3:
                return(SlimDX.Direct3D9.Format.Dxt3);

            default:
                return(format);
            }
        }
Beispiel #4
0
        public ISpread <Frame> Preload(
            ISpread <int> visibleFrameIndices,
            ISpread <int> preloadFrameNrs,
            int bufferSize,
            EX9.Format preferedFormat,
            out int frameCount,
            out double durationIO,
            out double durationTexture,
            out int unusedFrames,
            ref ISpread <bool> loadedFrames)
        {
            var files = FFiles;

            frameCount      = files.Length;
            durationIO      = 0.0;
            durationTexture = 0.0;
            unusedFrames    = 0;

            // Nothing to do
            if (files.Length == 0)
            {
                loadedFrames.SliceCount = 0;
                return(new Spread <Frame>(0));
            }

            // Map frame numbers to file names
            var preloadFiles = preloadFrameNrs.Select(
                frameNr =>
            {
                var fileName = files[VMath.Zmod(frameNr, files.Length)];
                return(CreateFrameInfo(fileName, bufferSize, preferedFormat));
            })
                               .ToArray();

            // Dispose previously loaded frames
            foreach (var file in FPreloadedFrames.Keys.ToArray())
            {
                if (!preloadFiles.Contains(file))
                {
                    var frame = FPreloadedFrames[file];
                    Dispose(frame);
                }
            }

            // Ensure that there are as much dequeues as enqueues (or we'll run out of memory)
            while (FScheduledFrameInfos.Count > preloadFrameNrs.SliceCount)
            {
                var frame     = Dequeue();
                var frameInfo = frame.Metadata;
                if (!preloadFiles.Contains(frameInfo) || frameInfo.IsCanceled)
                {
                    // Not needed anymore
                    Dispose(frame);
                    unusedFrames++;
                }
                else
                {
                    FPreloadedFrames.Add(frameInfo, frame);
                }
            }

            // Cancel unused scheduled frames
            foreach (var frameInfo in FScheduledFrameInfos.Where(fi => !fi.IsCanceled))
            {
                if (!preloadFiles.Contains(frameInfo))
                {
                    frameInfo.Cancel();
                }
            }

            // Schedule new frames
            foreach (var file in preloadFiles)
            {
                if (!IsScheduled(file) && !IsPreloaded(file))
                {
                    Enqueue(file);
                }
            }

            // Write the "is loaded" state
            loadedFrames.SliceCount = preloadFrameNrs.SliceCount;
            for (int i = 0; i < loadedFrames.SliceCount; i++)
            {
                var file = preloadFiles[i];
                if (!IsPreloaded(file))
                {
                    var frameInfo = FScheduledFrameInfos.First(fi => fi == file);
                    loadedFrames[i] = frameInfo.IsLoaded;
                }
                else
                {
                    loadedFrames[i] = true;
                }
            }

            // Wait for the visible frames (and dipose unused ones)
            var visibleFrames = new Spread <Frame>(0);
            // Map frame numbers to file names
            var visibleFiles = preloadFiles.Length > 0
                ? visibleFrameIndices.Select(i => preloadFiles[VMath.Zmod(i, preloadFiles.Length)])
                : Enumerable.Empty <FrameInfo>();

            foreach (var file in visibleFiles)
            {
                while (!IsPreloaded(file))
                {
                    var frame     = Dequeue();
                    var frameInfo = frame.Metadata;
                    if (!preloadFiles.Contains(frameInfo) || frameInfo.IsCanceled)
                    {
                        // Not needed anymore
                        Dispose(frame);
                        unusedFrames++;
                    }
                    else
                    {
                        FPreloadedFrames.Add(frameInfo, frame);
                    }
                }
                var visibleFrame     = FPreloadedFrames[file];
                var visibleFrameInfo = visibleFrame.Metadata;
                durationIO      += visibleFrameInfo.DurationIO;
                durationTexture += visibleFrameInfo.DurationTexture;
                visibleFrames.Add(visibleFrame);
            }

            // Release textures of non visible frames
            foreach (var frame in FPreloadedFrames.Values.Where(f => !visibleFrames.Contains(f)))
            {
                frame.Dispose();
            }

            return(visibleFrames);
        }
Beispiel #5
0
 private FrameInfo CreateFrameInfo(string filename, int bufferSize, EX9.Format preferedFormat)
 {
     return(new FrameInfo(filename, bufferSize, (SharpDX.Direct3D9.Format)preferedFormat));
 }
Beispiel #6
0
		private void InitDevice()
		{
			Debug.Assert( _device != null );
			// get device caps
			_devCaps = _device.Capabilities;

			// get our device creation parameters
			_devParms = _device.CreationParameters;

			// get our back buffer pixel format
			D3D.Surface back = _device.GetBackBuffer( 0, 0 );
			_bbPixelFormat = back.Description.Format;
		}